Annotation of libaitrpc/src/srv.c, revision 1.23.6.2
1.1 misho 1: /*************************************************************************
2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
3: * by Michael Pounov <misho@openbsd-bg.org>
4: *
5: * $Author: misho $
1.23.6.2! misho 6: * $Id: srv.c,v 1.23.6.1 2014/11/17 23:28:55 misho Exp $
1.1 misho 7: *
1.2 misho 8: **************************************************************************
9: The ELWIX and AITNET software is distributed under the following
10: terms:
11:
12: All of the documentation and software included in the ELWIX and AITNET
13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
14:
1.23 misho 15: Copyright 2004 - 2014
1.2 misho 16: by Michael Pounov <misho@elwix.org>. All rights reserved.
17:
18: Redistribution and use in source and binary forms, with or without
19: modification, are permitted provided that the following conditions
20: are met:
21: 1. Redistributions of source code must retain the above copyright
22: notice, this list of conditions and the following disclaimer.
23: 2. Redistributions in binary form must reproduce the above copyright
24: notice, this list of conditions and the following disclaimer in the
25: documentation and/or other materials provided with the distribution.
26: 3. All advertising materials mentioning features or use of this software
27: must display the following acknowledgement:
28: This product includes software developed by Michael Pounov <misho@elwix.org>
29: ELWIX - Embedded LightWeight unIX and its contributors.
30: 4. Neither the name of AITNET nor the names of its contributors
31: may be used to endorse or promote products derived from this software
32: without specific prior written permission.
33:
34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37: ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44: SUCH DAMAGE.
45: */
1.1 misho 46: #include "global.h"
47:
48:
1.13 misho 49: /* SOCK_STREAM */
50: static void *acceptClients(sched_task_t *);
51: static void *closeClient(sched_task_t *);
52: static void *rxPacket(sched_task_t *);
53: static void *txPacket(sched_task_t *);
54:
55: /* SOCK_DGRAM */
56: static void *freeClient(sched_task_t *);
57: static void *rxUDPPacket(sched_task_t *);
58: static void *txUDPPacket(sched_task_t *);
59:
60: /* SOCK_RAW */
61:
1.23.6.2! misho 62: /* SOCK_BPF */
! 63: static void *rxBPFPacket(sched_task_t *);
! 64: static void *txBPFPacket(sched_task_t *);
! 65:
! 66: static sched_task_func_t cbProto[SOCK_BPF + 1][4] = {
1.13 misho 67: { acceptClients, closeClient, rxPacket, txPacket }, /* SOCK_STREAM */
68: { acceptClients, closeClient, rxPacket, txPacket }, /* SOCK_STREAM */
69: { rxUDPPacket, freeClient, rxUDPPacket, txUDPPacket }, /* SOCK_DGRAM */
1.23.6.2! misho 70: { NULL, NULL, NULL, NULL }, /* SOCK_RAW */
! 71: { rxBPFPacket, freeClient, rxBPFPacket, txBPFPacket } /* SOCK_BPF */
1.13 misho 72: };
73:
1.23 misho 74: /* Global Signal Argument when kqueue support disabled */
75:
76: static volatile uintptr_t _glSigArg = 0;
77:
1.13 misho 78:
1.16 misho 79: void
1.13 misho 80: rpc_freeCli(rpc_cli_t * __restrict c)
1.10 misho 81: {
82: rpc_srv_t *s = c->cli_parent;
83:
1.13 misho 84: schedCancelby(s->srv_root, taskMAX, CRITERIA_ARG, c, NULL);
1.10 misho 85:
86: /* free buffer */
87: AIT_FREE_VAL(&c->cli_buf);
88:
1.14 misho 89: array_Del(s->srv_clients, c->cli_id, 0);
1.10 misho 90: if (c)
1.14 misho 91: e_free(c);
1.13 misho 92: }
93:
94:
95: static inline int
1.14 misho 96: _check4freeslot(rpc_srv_t * __restrict srv, sockaddr_t * __restrict sa)
1.13 misho 97: {
98: rpc_cli_t *c = NULL;
99: register int i;
100:
101: /* check free slots for connect */
1.14 misho 102: for (i = 0; i < array_Size(srv->srv_clients) &&
103: (c = array(srv->srv_clients, i, rpc_cli_t*)); i++)
1.13 misho 104: /* check for duplicates */
1.14 misho 105: if (sa && !e_addrcmp(&c->cli_sa, sa, 42))
1.13 misho 106: break;
1.14 misho 107: if (i >= array_Size(srv->srv_clients))
1.13 misho 108: return -1; /* no more free slots! */
109:
110: return i;
111: }
112:
113: static rpc_cli_t *
1.14 misho 114: _allocClient(rpc_srv_t * __restrict srv, sockaddr_t * __restrict sa)
1.13 misho 115: {
116: rpc_cli_t *c = NULL;
117: int n;
118:
119: n = _check4freeslot(srv, sa);
120: if (n == -1)
121: return NULL;
122: else
1.14 misho 123: c = array(srv->srv_clients, n, rpc_cli_t*);
1.13 misho 124:
125: if (!c) {
1.14 misho 126: c = e_malloc(sizeof(rpc_cli_t));
1.13 misho 127: if (!c) {
128: LOGERR;
129: srv->srv_kill = 1;
130: return NULL;
131: } else {
132: memset(c, 0, sizeof(rpc_cli_t));
1.14 misho 133: array_Set(srv->srv_clients, n, c);
1.13 misho 134: c->cli_id = n;
135: c->cli_parent = srv;
136: }
137:
138: /* alloc empty buffer */
1.14 misho 139: AIT_SET_BUFSIZ(&c->cli_buf, 0, srv->srv_netbuf);
1.13 misho 140: }
141:
142: return c;
143: }
144:
145:
146: static void *
147: freeClient(sched_task_t *task)
148: {
149: rpc_freeCli(TASK_ARG(task));
150:
151: return NULL;
152: }
153:
154: static void *
155: closeClient(sched_task_t *task)
156: {
157: int sock = ((rpc_cli_t*) TASK_ARG(task))->cli_sock;
158:
159: rpc_freeCli(TASK_ARG(task));
160:
161: /* close client socket */
162: shutdown(sock, SHUT_RDWR);
163: close(sock);
1.10 misho 164: return NULL;
165: }
1.7 misho 166:
167: static void *
168: txPacket(sched_task_t *task)
169: {
170: rpc_cli_t *c = TASK_ARG(task);
171: rpc_srv_t *s = c->cli_parent;
172: rpc_func_t *f = NULL;
1.18 misho 173: u_char *buf = AIT_GET_BUF(&c->cli_buf);
1.7 misho 174: struct tagRPCCall *rpc = (struct tagRPCCall*) buf;
1.18 misho 175: int ret, estlen, wlen = sizeof(struct tagRPCCall);
1.19 misho 176: struct pollfd pfd;
1.21 misho 177: #ifdef TCP_SESSION_TIMEOUT
178: struct timespec ts = { DEF_RPC_TIMEOUT, 0 };
179:
180: schedCancelby(TASK_ROOT(task), taskTIMER, CRITERIA_DATA, TASK_ARG(task), NULL);
181: schedTimer(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
182: TASK_ARG(task), ts, TASK_ARG(task), 0);
183: #endif
1.7 misho 184:
185: if (rpc->call_argc) {
1.10 misho 186: f = rpc_srv_getCall(s, ntohs(rpc->call_tag));
1.7 misho 187: if (!f) {
1.10 misho 188: rpc_SetErr(EPROGUNAVAIL, "Function not found at RPC server");
1.18 misho 189:
1.7 misho 190: rpc->call_argc ^= rpc->call_argc;
191: rpc->call_rep.ret = RPC_ERROR(-1);
192: rpc->call_rep.eno = RPC_ERROR(rpc_Errno);
193: } else {
1.18 misho 194: /* calc estimated length */
195: estlen = ait_resideVars(RPC_RETVARS(c)) + wlen;
196: if (estlen > AIT_LEN(&c->cli_buf))
197: AIT_RE_BUF(&c->cli_buf, estlen);
198: buf = AIT_GET_BUF(&c->cli_buf);
1.19 misho 199: rpc = (struct tagRPCCall*) buf;
1.18 misho 200:
1.14 misho 201: rpc->call_argc = htons(array_Size(RPC_RETVARS(c)));
1.7 misho 202: /* Go Encapsulate variables */
1.18 misho 203: ret = ait_vars2buffer(buf + wlen, AIT_LEN(&c->cli_buf) - wlen,
204: RPC_RETVARS(c));
1.10 misho 205: /* Free return values */
1.14 misho 206: ait_freeVars(&c->cli_vars);
1.7 misho 207: if (ret == -1) {
208: rpc_SetErr(EBADRPC, "Prepare RPC packet failed");
1.19 misho 209:
1.7 misho 210: rpc->call_argc ^= rpc->call_argc;
211: rpc->call_rep.ret = RPC_ERROR(-1);
212: rpc->call_rep.eno = RPC_ERROR(rpc_Errno);
213: } else
214: wlen += ret;
215: }
216: }
217:
1.18 misho 218: rpc->call_len = htonl(wlen);
1.8 misho 219:
1.15 misho 220: #if 0
1.7 misho 221: /* calculate CRC */
222: rpc->call_crc ^= rpc->call_crc;
1.8 misho 223: rpc->call_crc = htons(crcFletcher16((u_short*) buf, wlen / 2));
1.15 misho 224: #endif
1.7 misho 225:
226: /* send reply */
1.19 misho 227: pfd.fd = TASK_FD(task);
228: pfd.events = POLLOUT;
229: for (; wlen > 0; wlen -= ret, buf += ret) {
230: if ((ret = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) < 1 ||
231: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
232: if (ret)
233: LOGERR;
234: else
1.22 misho 235: rpc_SetErr(ETIMEDOUT, "Timeout reached! Client not respond");
1.19 misho 236: /* close connection */
237: schedEvent(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
238: TASK_ARG(task), 0, NULL, 0);
239: return NULL;
240: }
241: ret = send(TASK_FD(task), buf, MIN(wlen, s->srv_netbuf), MSG_NOSIGNAL);
242: if (ret == -1) {
243: /* close connection */
244: schedEvent(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
245: TASK_ARG(task), 0, NULL, 0);
246: return NULL;
247: }
1.10 misho 248: }
1.7 misho 249:
250: return NULL;
251: }
252:
253: static void *
254: execCall(sched_task_t *task)
255: {
256: rpc_cli_t *c = TASK_ARG(task);
257: rpc_srv_t *s = c->cli_parent;
258: rpc_func_t *f = NULL;
259: array_t *arr = NULL;
1.18 misho 260: u_char *buf = AIT_GET_BUF(&c->cli_buf);
1.7 misho 261: struct tagRPCCall *rpc = (struct tagRPCCall*) buf;
262: int argc = ntohs(rpc->call_argc);
263:
264: /* Go decapsulate variables ... */
1.8 misho 265: if (argc) {
1.14 misho 266: arr = ait_buffer2vars(buf + sizeof(struct tagRPCCall),
1.18 misho 267: AIT_LEN(&c->cli_buf) - sizeof(struct tagRPCCall), argc, 42);
1.7 misho 268: if (!arr) {
1.14 misho 269: rpc_SetErr(ERPCMISMATCH, "#%d - %s", elwix_GetErrno(), elwix_GetError());
1.18 misho 270:
1.7 misho 271: rpc->call_argc ^= rpc->call_argc;
272: rpc->call_rep.ret = RPC_ERROR(-1);
273: rpc->call_rep.eno = RPC_ERROR(rpc_Errno);
274: return NULL;
275: }
1.10 misho 276: } else
277: arr = NULL;
1.7 misho 278:
1.10 misho 279: if (!(f = rpc_srv_getCall(s, ntohs(rpc->call_tag)))) {
1.7 misho 280: rpc_SetErr(EPROGUNAVAIL, "Function not found at RPC server");
1.18 misho 281:
1.7 misho 282: rpc->call_argc ^= rpc->call_argc;
283: rpc->call_rep.ret = RPC_ERROR(-1);
284: rpc->call_rep.eno = RPC_ERROR(rpc_Errno);
285: } else {
1.8 misho 286: /* if client doesn't want reply */
1.10 misho 287: argc = RPC_CHK_NOREPLY(rpc);
288: rpc->call_rep.ret = RPC_ERROR(rpc_srv_execCall(c, rpc, f->func_name, arr));
1.7 misho 289: if (rpc->call_rep.ret == htonl(-1)) {
1.20 misho 290: if (!rpc->call_rep.eno) {
291: LOGERR;
292: rpc->call_rep.eno = RPC_ERROR(rpc_Errno);
293: }
1.7 misho 294: rpc->call_argc ^= rpc->call_argc;
295: } else {
296: rpc->call_rep.eno ^= rpc->call_rep.eno;
1.8 misho 297: if (argc) {
298: /* without reply */
1.14 misho 299: ait_freeVars(&c->cli_vars);
1.8 misho 300: rpc->call_argc ^= rpc->call_argc;
1.10 misho 301: } else {
302: /* reply */
1.14 misho 303: rpc->call_argc = htons(array_Size(RPC_RETVARS(c)));
1.10 misho 304: }
1.7 misho 305: }
306: }
307:
1.14 misho 308: array_Destroy(&arr);
1.7 misho 309: return NULL;
310: }
311:
312: static void *
313: rxPacket(sched_task_t *task)
314: {
315: rpc_cli_t *c = TASK_ARG(task);
316: rpc_srv_t *s = c->cli_parent;
1.18 misho 317: int len, rlen, noreply, estlen;
1.15 misho 318: #if 0
319: u_short crc;
320: #endif
1.10 misho 321: u_char *buf = AIT_GET_BUF(&c->cli_buf);
1.18 misho 322: struct tagRPCCall *rpc = (struct tagRPCCall*) buf;
1.19 misho 323: struct pollfd pfd;
1.21 misho 324: #ifdef TCP_SESSION_TIMEOUT
325: struct timespec ts = { DEF_RPC_TIMEOUT, 0 };
326:
327: schedCancelby(TASK_ROOT(task), taskTIMER, CRITERIA_DATA, TASK_ARG(task), NULL);
328: schedTimer(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
329: TASK_ARG(task), ts, TASK_ARG(task), 0);
330: #endif
1.7 misho 331:
1.18 misho 332: memset(buf, 0, sizeof(struct tagRPCCall));
1.19 misho 333: rlen = recv(TASK_FD(task), rpc, sizeof(struct tagRPCCall), MSG_PEEK);
1.18 misho 334: if (rlen < sizeof(struct tagRPCCall)) {
1.10 misho 335: /* close connection */
1.13 misho 336: schedEvent(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
337: TASK_ARG(task), 0, NULL, 0);
1.7 misho 338: return NULL;
1.10 misho 339: } else {
1.18 misho 340: estlen = ntohl(rpc->call_len);
341: if (estlen > AIT_LEN(&c->cli_buf))
342: AIT_RE_BUF(&c->cli_buf, estlen);
343: rpc = (struct tagRPCCall*) AIT_GET_BUF(&c->cli_buf);
1.19 misho 344: buf = AIT_GET_BUF(&c->cli_buf);
345: len = estlen;
1.18 misho 346: }
347:
1.19 misho 348: /* get next part of packet */
349: memset(buf, 0, len);
350: pfd.fd = TASK_FD(task);
351: pfd.events = POLLIN | POLLPRI;
352: for (; len > 0; len -= rlen, buf += rlen) {
353: if ((rlen = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) < 1 ||
354: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
355: if (rlen)
356: LOGERR;
357: else
1.22 misho 358: rpc_SetErr(ETIMEDOUT, "Timeout reached! Client not respond");
1.19 misho 359: schedEvent(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
360: TASK_ARG(task), 0, NULL, 0);
361: return NULL;
362: }
1.18 misho 363: rlen = recv(TASK_FD(task), buf, len, 0);
364: if (rlen == -1) {
365: /* close connection */
366: schedEvent(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
367: TASK_ARG(task), 0, NULL, 0);
1.8 misho 368: return NULL;
1.10 misho 369: }
1.18 misho 370: }
1.22 misho 371: len = estlen;
1.8 misho 372:
1.15 misho 373: #if 0
1.18 misho 374: /* check integrity of packet */
375: crc = ntohs(rpc->call_crc);
376: rpc->call_crc ^= rpc->call_crc;
377: if (crc != crcFletcher16((u_short*) rpc, len / 2)) {
378: rpc_SetErr(ERPCMISMATCH, "Bad CRC RPC packet");
379: return NULL;
380: }
1.15 misho 381: #endif
1.7 misho 382:
1.18 misho 383: noreply = RPC_CHK_NOREPLY(rpc);
1.7 misho 384:
1.18 misho 385: /* check RPC packet session info */
386: if (rpc_chkPktSession(&rpc->call_session, &s->srv_session)) {
387: rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session");
1.7 misho 388:
1.18 misho 389: rpc->call_argc ^= rpc->call_argc;
390: rpc->call_rep.ret = RPC_ERROR(-1);
391: rpc->call_rep.eno = RPC_ERROR(errno);
392: } else {
393: /* execute RPC call */
394: schedEvent(TASK_ROOT(task), execCall, TASK_ARG(task), (int) noreply, rpc, len);
395: }
1.7 misho 396:
1.18 misho 397: /* send RPC reply */
398: if (!noreply)
399: schedWrite(TASK_ROOT(task), cbProto[s->srv_proto][CB_TXPACKET],
400: TASK_ARG(task), TASK_FD(task), rpc, len);
1.7 misho 401:
402: /* lets get next packet */
1.18 misho 403: schedReadSelf(task);
1.7 misho 404: return NULL;
405: }
406:
1.1 misho 407: static void *
1.10 misho 408: acceptClients(sched_task_t *task)
1.1 misho 409: {
1.10 misho 410: rpc_srv_t *srv = TASK_ARG(task);
411: rpc_cli_t *c = NULL;
1.14 misho 412: socklen_t salen = sizeof(sockaddr_t);
1.21 misho 413: int sock;
414: #ifdef TCP_SESSION_TIMEOUT
415: struct timespec ts = { DEF_RPC_TIMEOUT, 0 };
416: #endif
1.7 misho 417:
1.13 misho 418: c = _allocClient(srv, NULL);
1.21 misho 419: if (!c) {
420: EVERBOSE(1, "RPC client quota exceeded! Connection will be shutdown!\n");
421: if ((sock = accept(TASK_FD(task), NULL, NULL)) != -1) {
422: shutdown(sock, SHUT_RDWR);
423: close(sock);
424: }
1.10 misho 425: goto end;
1.21 misho 426: }
1.10 misho 427:
428: /* accept client */
429: c->cli_sock = accept(TASK_FD(task), &c->cli_sa.sa, &salen);
430: if (c->cli_sock == -1) {
431: LOGERR;
432: AIT_FREE_VAL(&c->cli_buf);
1.14 misho 433: array_Del(srv->srv_clients, c->cli_id, 42);
1.10 misho 434: goto end;
1.1 misho 435: } else
1.10 misho 436: fcntl(c->cli_sock, F_SETFL, fcntl(c->cli_sock, F_GETFL) | O_NONBLOCK);
1.1 misho 437:
1.21 misho 438: #ifdef TCP_SESSION_TIMEOUT
439: /* armed timer for close stateless connection */
440: schedCancelby(TASK_ROOT(task), taskTIMER, CRITERIA_DATA, c, NULL);
441: schedTimer(TASK_ROOT(task), cbProto[srv->srv_proto][CB_CLOSECLIENT], c,
442: ts, c, 0);
443: #endif
1.13 misho 444: schedRead(TASK_ROOT(task), cbProto[srv->srv_proto][CB_RXPACKET], c,
445: c->cli_sock, NULL, 0);
1.10 misho 446: end:
447: schedReadSelf(task);
448: return NULL;
449: }
1.5 misho 450:
1.7 misho 451:
1.10 misho 452: static void *
1.13 misho 453: txUDPPacket(sched_task_t *task)
1.10 misho 454: {
455: rpc_cli_t *c = TASK_ARG(task);
456: rpc_srv_t *s = c->cli_parent;
1.13 misho 457: rpc_func_t *f = NULL;
1.18 misho 458: u_char *buf = AIT_GET_BUF(&c->cli_buf);
1.13 misho 459: struct tagRPCCall *rpc = (struct tagRPCCall*) buf;
1.22 misho 460: int ret, estlen, wlen = sizeof(struct tagRPCCall);
1.13 misho 461: struct timespec ts = { DEF_RPC_TIMEOUT, 0 };
1.22 misho 462: struct pollfd pfd;
1.13 misho 463:
464: schedCancelby(TASK_ROOT(task), taskTIMER, CRITERIA_DATA, TASK_ARG(task), NULL);
465: schedTimer(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
466: TASK_ARG(task), ts, TASK_ARG(task), 0);
467:
468: if (rpc->call_argc) {
469: f = rpc_srv_getCall(s, ntohs(rpc->call_tag));
470: if (!f) {
471: rpc_SetErr(EPROGUNAVAIL, "Function not found at RPC server");
472: rpc->call_argc ^= rpc->call_argc;
473: rpc->call_rep.ret = RPC_ERROR(-1);
474: rpc->call_rep.eno = RPC_ERROR(rpc_Errno);
475: } else {
1.22 misho 476: /* calc estimated length */
477: estlen = ait_resideVars(RPC_RETVARS(c)) + wlen;
478: if (estlen > AIT_LEN(&c->cli_buf))
479: AIT_RE_BUF(&c->cli_buf, estlen);
480: buf = AIT_GET_BUF(&c->cli_buf);
481: rpc = (struct tagRPCCall*) buf;
482:
1.14 misho 483: rpc->call_argc = htons(array_Size(RPC_RETVARS(c)));
1.13 misho 484: /* Go Encapsulate variables */
1.18 misho 485: ret = ait_vars2buffer(buf + wlen, AIT_LEN(&c->cli_buf) - wlen,
486: RPC_RETVARS(c));
1.13 misho 487: /* Free return values */
1.14 misho 488: ait_freeVars(&c->cli_vars);
1.13 misho 489: if (ret == -1) {
490: rpc_SetErr(EBADRPC, "Prepare RPC packet failed");
491: rpc->call_argc ^= rpc->call_argc;
492: rpc->call_rep.ret = RPC_ERROR(-1);
493: rpc->call_rep.eno = RPC_ERROR(rpc_Errno);
494: } else
495: wlen += ret;
496: }
497: }
1.7 misho 498:
1.18 misho 499: rpc->call_len = htonl(wlen);
1.7 misho 500:
1.13 misho 501: /* calculate CRC */
502: rpc->call_crc ^= rpc->call_crc;
503: rpc->call_crc = htons(crcFletcher16((u_short*) buf, wlen / 2));
504:
505: /* send reply */
1.22 misho 506: pfd.fd = TASK_FD(task);
507: pfd.events = POLLOUT;
508: for (; wlen > 0; wlen -= ret, buf += ret) {
509: if ((ret = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) < 1 ||
510: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
511: if (ret)
512: LOGERR;
513: else
514: rpc_SetErr(ETIMEDOUT, "Timeout reached! Client not respond");
515: /* close connection */
516: schedEvent(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
517: TASK_ARG(task), 0, NULL, 0);
518: return NULL;
519: }
520: ret = sendto(TASK_FD(task), buf, MIN(wlen, s->srv_netbuf), MSG_NOSIGNAL,
521: &c->cli_sa.sa, c->cli_sa.sa.sa_len);
522: if (ret == -1) {
523: /* close connection */
524: schedEvent(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
525: TASK_ARG(task), 0, NULL, 0);
526: return NULL;
527: }
1.13 misho 528: }
529:
530: return NULL;
531: }
532:
533: static void *
534: rxUDPPacket(sched_task_t *task)
535: {
536: rpc_srv_t *srv = TASK_ARG(task);
537: rpc_cli_t *c = NULL;
1.22 misho 538: int len, rlen, noreply, estlen;
539: u_short crc;
540: u_char *buf, b[sizeof(struct tagRPCCall)];
541: struct tagRPCCall *rpc = (struct tagRPCCall*) b;
1.14 misho 542: sockaddr_t sa;
543: socklen_t salen;
1.13 misho 544: struct timespec ts = { DEF_RPC_TIMEOUT, 0 };
1.22 misho 545: struct pollfd pfd;
1.13 misho 546:
547: /* receive connect packet */
1.14 misho 548: salen = sa.ss.ss_len = sizeof(sockaddr_t);
1.22 misho 549: rlen = recvfrom(TASK_FD(task), b, sizeof b, MSG_PEEK, &sa.sa, &salen);
550: if (rlen < sizeof(struct tagRPCCall)) {
551: rpc_SetErr(ERPCMISMATCH, "Short RPC packet");
1.13 misho 552: goto end;
1.22 misho 553: }
1.13 misho 554:
555: c = _allocClient(srv, &sa);
1.22 misho 556: if (!c) {
557: EVERBOSE(1, "RPC client quota exceeded! Connection will be shutdown!\n");
558: usleep(2000); /* blocked client delay */
1.13 misho 559: goto end;
1.22 misho 560: } else {
561: estlen = ntohl(rpc->call_len);
562: if (estlen > AIT_LEN(&c->cli_buf))
563: AIT_RE_BUF(&c->cli_buf, estlen);
564: rpc = (struct tagRPCCall*) AIT_GET_BUF(&c->cli_buf);
565: buf = AIT_GET_BUF(&c->cli_buf);
566: len = estlen;
567:
1.13 misho 568: c->cli_sock = TASK_FD(task);
569: memcpy(&c->cli_sa, &sa, sizeof c->cli_sa);
1.22 misho 570:
1.13 misho 571: /* armed timer for close stateless connection */
572: schedCancelby(TASK_ROOT(task), taskTIMER, CRITERIA_DATA, c, NULL);
573: schedTimer(TASK_ROOT(task), cbProto[srv->srv_proto][CB_CLOSECLIENT],
574: c, ts, c, 0);
575: }
576:
1.22 misho 577: /* get next part of packet */
578: memset(buf, 0, len);
579: pfd.fd = TASK_FD(task);
580: pfd.events = POLLIN | POLLPRI;
581: for (; len > 0; len -= rlen, buf += rlen) {
582: if ((rlen = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) < 1 ||
583: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
584: if (rlen)
585: LOGERR;
586: else
587: rpc_SetErr(ETIMEDOUT, "Timeout reached! Client not respond");
588: schedEvent(TASK_ROOT(task), cbProto[srv->srv_proto][CB_CLOSECLIENT],
589: c, 0, NULL, 0);
590: return NULL;
1.13 misho 591: }
1.22 misho 592: salen = sa.ss.ss_len = sizeof(sockaddr_t);
593: rlen = recvfrom(TASK_FD(task), buf, len, 0, &sa.sa, &salen);
594: if (rlen == -1) {
595: /* close connection */
596: schedEvent(TASK_ROOT(task), cbProto[srv->srv_proto][CB_CLOSECLIENT],
597: c, 0, NULL, 0);
598: return NULL;
1.13 misho 599: }
1.22 misho 600: if (e_addrcmp(&c->cli_sa, &sa, 42))
601: rlen ^= rlen; /* skip if arrive from different address */
602: }
603: len = estlen;
1.13 misho 604:
1.22 misho 605: /* check integrity of packet */
606: crc = ntohs(rpc->call_crc);
607: rpc->call_crc ^= rpc->call_crc;
608: if (crc != crcFletcher16((u_short*) rpc, len / 2)) {
609: rpc_SetErr(ERPCMISMATCH, "Bad CRC RPC packet");
610: return NULL;
611: }
1.13 misho 612:
1.22 misho 613: noreply = RPC_CHK_NOREPLY(rpc);
1.18 misho 614:
1.22 misho 615: /* check RPC packet session info */
616: if (rpc_chkPktSession(&rpc->call_session, &srv->srv_session)) {
617: rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session");
1.13 misho 618:
1.22 misho 619: rpc->call_argc ^= rpc->call_argc;
620: rpc->call_rep.ret = RPC_ERROR(-1);
621: rpc->call_rep.eno = RPC_ERROR(errno);
622: } else {
623: /* execute RPC call */
624: schedEvent(TASK_ROOT(task), execCall, c, (int) noreply, rpc, len);
625: }
1.13 misho 626:
1.22 misho 627: /* send RPC reply */
628: if (!noreply)
629: schedWrite(TASK_ROOT(task), cbProto[srv->srv_proto][CB_TXPACKET],
630: c, TASK_FD(task), rpc, len);
1.13 misho 631: end:
632: schedReadSelf(task);
633: return NULL;
634: }
635:
636: /* ------------------------------------------------------ */
637:
1.16 misho 638: void
1.13 misho 639: rpc_freeBLOBCli(rpc_cli_t * __restrict c)
640: {
641: rpc_srv_t *s = c->cli_parent;
642:
643: schedCancelby(s->srv_blob.root, taskMAX, CRITERIA_ARG, c, NULL);
1.10 misho 644:
645: /* free buffer */
646: AIT_FREE_VAL(&c->cli_buf);
647:
1.14 misho 648: array_Del(s->srv_blob.clients, c->cli_id, 0);
1.10 misho 649: if (c)
1.14 misho 650: e_free(c);
1.13 misho 651: }
652:
653:
654: static void *
655: closeBLOBClient(sched_task_t *task)
656: {
657: int sock = ((rpc_cli_t*) TASK_ARG(task))->cli_sock;
658:
659: rpc_freeBLOBCli(TASK_ARG(task));
660:
661: /* close client socket */
662: shutdown(sock, SHUT_RDWR);
663: close(sock);
1.7 misho 664: return NULL;
665: }
666:
667: static void *
668: txBLOB(sched_task_t *task)
669: {
1.10 misho 670: rpc_cli_t *c = TASK_ARG(task);
671: u_char *buf = AIT_GET_BUF(&c->cli_buf);
1.7 misho 672: int wlen = sizeof(struct tagBLOBHdr);
673:
674: /* send reply */
1.10 misho 675: wlen = send(TASK_FD(task), buf, wlen, MSG_NOSIGNAL);
676: if (wlen == -1 || wlen != sizeof(struct tagBLOBHdr)) {
677: /* close blob connection */
678: schedEvent(TASK_ROOT(task), closeBLOBClient, c, 42, NULL, 0);
679: }
1.7 misho 680:
681: return NULL;
682: }
1.4 misho 683:
1.7 misho 684: static void *
685: rxBLOB(sched_task_t *task)
686: {
687: rpc_cli_t *c = TASK_ARG(task);
688: rpc_srv_t *s = c->cli_parent;
689: rpc_blob_t *b;
1.10 misho 690: struct tagBLOBHdr blob;
1.7 misho 691: int rlen;
692:
1.10 misho 693: memset(&blob, 0, sizeof blob);
694: rlen = recv(TASK_FD(task), &blob, sizeof blob, 0);
695: if (rlen < 1) {
696: /* close blob connection */
697: schedEvent(TASK_ROOT(task), closeBLOBClient, c, 42, NULL, 0);
1.7 misho 698: return NULL;
699: }
1.5 misho 700:
1.10 misho 701: /* check BLOB packet */
702: if (rlen < sizeof(struct tagBLOBHdr)) {
703: rpc_SetErr(ERPCMISMATCH, "Short BLOB packet");
1.6 misho 704:
1.10 misho 705: schedReadSelf(task);
1.7 misho 706: return NULL;
707: }
1.1 misho 708:
1.7 misho 709: /* check RPC packet session info */
1.15 misho 710: if (rpc_chkPktSession(&blob.hdr_session, &s->srv_session)) {
1.7 misho 711: rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session");
1.10 misho 712: blob.hdr_cmd = error;
1.7 misho 713: goto end;
714: }
715:
716: /* Go to proceed packet ... */
1.10 misho 717: switch (blob.hdr_cmd) {
1.7 misho 718: case get:
1.10 misho 719: if (!(b = rpc_srv_getBLOB(s, ntohl(blob.hdr_var)))) {
720: rpc_SetErr(EINVAL, "Var=%x not found", ntohl(blob.hdr_var));
721: blob.hdr_cmd = no;
722: blob.hdr_ret = RPC_ERROR(-1);
1.7 misho 723: break;
724: } else
1.10 misho 725: blob.hdr_len = htonl(b->blob_len);
1.5 misho 726:
1.7 misho 727: if (rpc_srv_blobMap(s, b) != -1) {
728: /* deliver BLOB variable to client */
1.10 misho 729: blob.hdr_ret = htonl(rpc_srv_sendBLOB(c, b));
1.7 misho 730: rpc_srv_blobUnmap(b);
731: } else {
1.10 misho 732: blob.hdr_cmd = error;
733: blob.hdr_ret = RPC_ERROR(-1);
1.7 misho 734: }
735: break;
736: case set:
1.17 misho 737: if ((b = rpc_srv_registerBLOB(s, ntohl(blob.hdr_len),
738: ntohl(blob.hdr_ret)))) {
1.7 misho 739: /* set new BLOB variable for reply :) */
1.10 misho 740: blob.hdr_var = htonl(b->blob_var);
1.7 misho 741:
742: /* receive BLOB from client */
1.10 misho 743: blob.hdr_ret = htonl(rpc_srv_recvBLOB(c, b));
1.7 misho 744: rpc_srv_blobUnmap(b);
1.5 misho 745: } else {
1.10 misho 746: blob.hdr_cmd = error;
747: blob.hdr_ret = RPC_ERROR(-1);
1.7 misho 748: }
749: break;
750: case unset:
1.11 misho 751: if (rpc_srv_unregisterBLOB(s, ntohl(blob.hdr_var)) == -1) {
1.10 misho 752: blob.hdr_cmd = error;
753: blob.hdr_ret = RPC_ERROR(-1);
1.1 misho 754: }
755: break;
1.7 misho 756: default:
1.10 misho 757: rpc_SetErr(EPROCUNAVAIL, "Unsupported BLOB command %d", blob.hdr_cmd);
758: blob.hdr_cmd = error;
759: blob.hdr_ret = RPC_ERROR(-1);
1.7 misho 760: }
1.1 misho 761:
1.7 misho 762: end:
1.10 misho 763: memcpy(AIT_ADDR(&c->cli_buf), &blob, sizeof blob);
764: schedWrite(TASK_ROOT(task), txBLOB, TASK_ARG(task), TASK_FD(task), NULL, 0);
765: schedReadSelf(task);
1.7 misho 766: return NULL;
1.2 misho 767: }
768:
769: static void *
1.17 misho 770: flushBLOB(sched_task_t *task)
771: {
1.23 misho 772: uintptr_t sigArg = atomic_load_acq_ptr(&_glSigArg);
773: rpc_srv_t *srv = sigArg ? (void*) sigArg : TASK_ARG(task);
1.17 misho 774: rpc_blob_t *b, *tmp;
775:
776: TAILQ_FOREACH_SAFE(b, &srv->srv_blob.blobs, blob_node, tmp) {
777: TAILQ_REMOVE(&srv->srv_blob.blobs, b, blob_node);
778:
779: rpc_srv_blobFree(srv, b);
780: e_free(b);
781: }
782:
1.23 misho 783: if (!schedSignalSelf(task)) {
784: /* disabled kqueue support in libaitsched */
785: struct sigaction sa;
786:
787: memset(&sa, 0, sizeof sa);
788: sigemptyset(&sa.sa_mask);
789: sa.sa_handler = (void (*)(int)) flushBLOB;
790: sa.sa_flags = SA_RESTART | SA_RESETHAND;
791: sigaction(SIGFBLOB, &sa, NULL);
792: }
793:
1.17 misho 794: return NULL;
795: }
796:
797: static void *
1.10 misho 798: acceptBLOBClients(sched_task_t *task)
1.2 misho 799: {
1.10 misho 800: rpc_srv_t *srv = TASK_ARG(task);
801: rpc_cli_t *c = NULL;
802: register int i;
1.14 misho 803: socklen_t salen = sizeof(sockaddr_t);
1.21 misho 804: int sock;
1.12 misho 805: #ifdef TCP_NOPUSH
806: int n = 1;
807: #endif
1.7 misho 808:
1.10 misho 809: /* check free slots for connect */
1.14 misho 810: for (i = 0; i < array_Size(srv->srv_blob.clients) &&
811: (c = array(srv->srv_blob.clients, i, rpc_cli_t*)); i++);
1.21 misho 812: if (c) { /* no more free slots! */
813: EVERBOSE(1, "BLOB client quota exceeded! Connection will be shutdown!\n");
814: if ((sock = accept(TASK_FD(task), NULL, NULL)) != -1) {
815: shutdown(sock, SHUT_RDWR);
816: close(sock);
817: }
1.10 misho 818: goto end;
1.21 misho 819: }
820:
1.14 misho 821: c = e_malloc(sizeof(rpc_cli_t));
1.10 misho 822: if (!c) {
1.7 misho 823: LOGERR;
1.10 misho 824: srv->srv_kill = srv->srv_blob.kill = 1;
1.7 misho 825: return NULL;
826: } else {
1.10 misho 827: memset(c, 0, sizeof(rpc_cli_t));
1.14 misho 828: array_Set(srv->srv_blob.clients, i, c);
1.10 misho 829: c->cli_id = i;
830: c->cli_parent = srv;
1.7 misho 831: }
1.4 misho 832:
1.10 misho 833: /* alloc empty buffer */
1.14 misho 834: AIT_SET_BUFSIZ(&c->cli_buf, 0, srv->srv_netbuf);
1.2 misho 835:
1.10 misho 836: /* accept client */
837: c->cli_sock = accept(TASK_FD(task), &c->cli_sa.sa, &salen);
838: if (c->cli_sock == -1) {
839: LOGERR;
840: AIT_FREE_VAL(&c->cli_buf);
1.14 misho 841: array_Del(srv->srv_blob.clients, i, 42);
1.10 misho 842: goto end;
1.12 misho 843: } else {
844: #ifdef TCP_NOPUSH
845: setsockopt(c->cli_sock, IPPROTO_TCP, TCP_NOPUSH, &n, sizeof n);
846: #endif
1.10 misho 847: fcntl(c->cli_sock, F_SETFL, fcntl(c->cli_sock, F_GETFL) | O_NONBLOCK);
1.12 misho 848: }
1.2 misho 849:
1.10 misho 850: schedRead(TASK_ROOT(task), rxBLOB, c, c->cli_sock, NULL, 0);
851: end:
852: schedReadSelf(task);
1.7 misho 853: return NULL;
1.1 misho 854: }
855:
1.10 misho 856: /* ------------------------------------------------------ */
1.1 misho 857:
858: /*
1.7 misho 859: * rpc_srv_initBLOBServer() - Init & create BLOB Server
860: *
1.4 misho 861: * @srv = RPC server instance
1.2 misho 862: * @Port = Port for bind server, if Port == 0 default port is selected
863: * @diskDir = Disk place for BLOB file objects
864: * return: -1 == error or 0 bind and created BLOB server instance
865: */
866: int
867: rpc_srv_initBLOBServer(rpc_srv_t * __restrict srv, u_short Port, const char *diskDir)
868: {
869: int n = 1;
870:
1.10 misho 871: if (!srv || srv->srv_kill) {
1.7 misho 872: rpc_SetErr(EINVAL, "Invalid parameters can`t init BLOB server");
1.2 misho 873: return -1;
874: }
875:
876: memset(&srv->srv_blob, 0, sizeof srv->srv_blob);
877: if (access(diskDir, R_OK | W_OK) == -1) {
878: LOGERR;
879: return -1;
880: } else
1.7 misho 881: AIT_SET_STR(&srv->srv_blob.dir, diskDir);
1.2 misho 882:
1.10 misho 883: /* init blob list */
884: TAILQ_INIT(&srv->srv_blob.blobs);
885:
1.2 misho 886: srv->srv_blob.server.cli_parent = srv;
1.4 misho 887:
1.14 misho 888: memcpy(&srv->srv_blob.server.cli_sa, &srv->srv_server.cli_sa, sizeof(sockaddr_t));
1.10 misho 889: switch (srv->srv_blob.server.cli_sa.sa.sa_family) {
1.4 misho 890: case AF_INET:
1.10 misho 891: srv->srv_blob.server.cli_sa.sin.sin_port =
892: htons(Port ? Port : ntohs(srv->srv_blob.server.cli_sa.sin.sin_port) + 1);
1.4 misho 893: break;
894: case AF_INET6:
1.10 misho 895: srv->srv_blob.server.cli_sa.sin6.sin6_port =
896: htons(Port ? Port : ntohs(srv->srv_blob.server.cli_sa.sin6.sin6_port) + 1);
1.4 misho 897: break;
898: case AF_LOCAL:
1.10 misho 899: strlcat(srv->srv_blob.server.cli_sa.sun.sun_path, ".blob",
900: sizeof srv->srv_blob.server.cli_sa.sun.sun_path);
1.4 misho 901: break;
902: default:
1.7 misho 903: AIT_FREE_VAL(&srv->srv_blob.dir);
1.4 misho 904: return -1;
1.2 misho 905: }
906:
1.4 misho 907: /* create BLOB server socket */
1.6 misho 908: srv->srv_blob.server.cli_sock = socket(srv->srv_server.cli_sa.sa.sa_family, SOCK_STREAM, 0);
1.2 misho 909: if (srv->srv_blob.server.cli_sock == -1) {
910: LOGERR;
1.7 misho 911: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 912: return -1;
913: }
914: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
915: LOGERR;
916: close(srv->srv_blob.server.cli_sock);
1.7 misho 917: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 918: return -1;
919: }
1.5 misho 920: n = srv->srv_netbuf;
921: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof n) == -1) {
922: LOGERR;
923: close(srv->srv_blob.server.cli_sock);
1.7 misho 924: AIT_FREE_VAL(&srv->srv_blob.dir);
1.5 misho 925: return -1;
926: }
927: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_RCVBUF, &n, sizeof n) == -1) {
928: LOGERR;
929: close(srv->srv_blob.server.cli_sock);
1.7 misho 930: AIT_FREE_VAL(&srv->srv_blob.dir);
1.5 misho 931: return -1;
932: }
1.6 misho 933: if (bind(srv->srv_blob.server.cli_sock, &srv->srv_blob.server.cli_sa.sa,
934: srv->srv_blob.server.cli_sa.sa.sa_len) == -1) {
1.2 misho 935: LOGERR;
936: close(srv->srv_blob.server.cli_sock);
1.7 misho 937: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 938: return -1;
1.13 misho 939: } else
940: fcntl(srv->srv_blob.server.cli_sock, F_SETFL,
941: fcntl(srv->srv_blob.server.cli_sock, F_GETFL) | O_NONBLOCK);
942:
1.2 misho 943:
1.10 misho 944: /* allocate pool for concurent blob clients */
1.14 misho 945: srv->srv_blob.clients = array_Init(array_Size(srv->srv_clients));
1.2 misho 946: if (!srv->srv_blob.clients) {
1.14 misho 947: rpc_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
1.2 misho 948: close(srv->srv_blob.server.cli_sock);
1.7 misho 949: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 950: return -1;
1.10 misho 951: }
1.2 misho 952:
1.10 misho 953: /* init blob scheduler */
954: srv->srv_blob.root = schedBegin();
955: if (!srv->srv_blob.root) {
956: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
1.14 misho 957: array_Destroy(&srv->srv_blob.clients);
1.10 misho 958: close(srv->srv_blob.server.cli_sock);
959: AIT_FREE_VAL(&srv->srv_blob.dir);
960: return -1;
961: }
1.2 misho 962:
963: return 0;
964: }
965:
966: /*
1.7 misho 967: * rpc_srv_endBLOBServer() - Destroy BLOB server, close all opened sockets and free resources
968: *
1.2 misho 969: * @srv = RPC Server instance
970: * return: none
971: */
1.16 misho 972: void
1.2 misho 973: rpc_srv_endBLOBServer(rpc_srv_t * __restrict srv)
974: {
1.10 misho 975: if (!srv)
1.2 misho 976: return;
977:
1.10 misho 978: srv->srv_blob.kill = 1;
1.17 misho 979:
980: schedEnd(&srv->srv_blob.root);
1.2 misho 981: }
982:
983: /*
1.10 misho 984: * rpc_srv_loopBLOBServer() - Execute Main BLOB server loop and wait for clients requests
1.7 misho 985: *
1.2 misho 986: * @srv = RPC Server instance
987: * return: -1 error or 0 ok, infinite loop ...
988: */
989: int
1.10 misho 990: rpc_srv_loopBLOBServer(rpc_srv_t * __restrict srv)
1.2 misho 991: {
1.10 misho 992: rpc_cli_t *c;
1.2 misho 993: register int i;
1.10 misho 994: rpc_blob_t *b, *tmp;
995: struct timespec ts = { RPC_SCHED_POLLING, 0 };
1.2 misho 996:
1.10 misho 997: if (!srv || srv->srv_kill) {
1.7 misho 998: rpc_SetErr(EINVAL, "Invalid parameter can`t start BLOB server");
1.2 misho 999: return -1;
1000: }
1001:
1.14 misho 1002: if (listen(srv->srv_blob.server.cli_sock, array_Size(srv->srv_blob.clients)) == -1) {
1.2 misho 1003: LOGERR;
1004: return -1;
1.10 misho 1005: }
1006:
1.23 misho 1007: if (!schedSignal(srv->srv_blob.root, flushBLOB, srv, SIGFBLOB, NULL, 0)) {
1008: /* disabled kqueue support in libaitsched */
1009: struct sigaction sa;
1010:
1011: atomic_store_rel_ptr(&_glSigArg, (uintptr_t) srv);
1012:
1013: memset(&sa, 0, sizeof sa);
1014: sigemptyset(&sa.sa_mask);
1015: sa.sa_handler = (void (*)(int)) flushBLOB;
1016: sa.sa_flags = SA_RESTART | SA_RESETHAND;
1017: sigaction(SIGFBLOB, &sa, NULL);
1018: }
1019:
1.10 misho 1020: if (!schedRead(srv->srv_blob.root, acceptBLOBClients, srv,
1021: srv->srv_blob.server.cli_sock, NULL, 0)) {
1022: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
1023: return -1;
1024: }
1.2 misho 1025:
1.10 misho 1026: schedPolling(srv->srv_blob.root, &ts, NULL);
1027: /* main rpc loop */
1028: schedRun(srv->srv_blob.root, &srv->srv_blob.kill);
1.7 misho 1029:
1.17 misho 1030: /* detach blobs */
1031: TAILQ_FOREACH_SAFE(b, &srv->srv_blob.blobs, blob_node, tmp) {
1032: TAILQ_REMOVE(&srv->srv_blob.blobs, b, blob_node);
1033:
1034: rpc_srv_blobFree(srv, b);
1035: e_free(b);
1036: }
1037:
1.10 misho 1038: /* close all clients connections & server socket */
1.14 misho 1039: for (i = 0; i < array_Size(srv->srv_blob.clients); i++) {
1040: c = array(srv->srv_blob.clients, i, rpc_cli_t*);
1.10 misho 1041: if (c) {
1042: shutdown(c->cli_sock, SHUT_RDWR);
1043: close(c->cli_sock);
1044:
1045: schedCancelby(srv->srv_blob.root, taskMAX, CRITERIA_ARG, c, NULL);
1046: AIT_FREE_VAL(&c->cli_buf);
1.2 misho 1047: }
1.14 misho 1048: array_Del(srv->srv_blob.clients, i, 42);
1.10 misho 1049: }
1.14 misho 1050: array_Destroy(&srv->srv_blob.clients);
1.2 misho 1051:
1.10 misho 1052: close(srv->srv_blob.server.cli_sock);
1.2 misho 1053:
1.10 misho 1054: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 1055: return 0;
1056: }
1057:
1058:
1059: /*
1.7 misho 1060: * rpc_srv_initServer() - Init & create RPC Server
1061: *
1.15 misho 1062: * @InstID = Instance for authentication & recognition
1.1 misho 1063: * @concurentClients = Concurent clients at same time to this server
1.10 misho 1064: * @netBuf = Network buffer length (min:512 bytes), if =0 == BUFSIZ (also meaning max RPC packet)
1.4 misho 1065: * @csHost = Host name or address for bind server, if NULL any address
1.1 misho 1066: * @Port = Port for bind server, if Port == 0 default port is selected
1.13 misho 1067: * @proto = Protocol, if == 0 choose SOCK_STREAM
1.1 misho 1068: * return: NULL == error or !=NULL bind and created RPC server instance
1069: */
1070: rpc_srv_t *
1.15 misho 1071: rpc_srv_initServer(u_char InstID, int concurentClients, int netBuf,
1072: const char *csHost, u_short Port, int proto)
1.1 misho 1073: {
1.10 misho 1074: int n = 1;
1.1 misho 1075: rpc_srv_t *srv = NULL;
1.14 misho 1076: sockaddr_t sa = E_SOCKADDR_INIT;
1.1 misho 1077:
1.15 misho 1078: if (!concurentClients || (proto < 0 || proto > SOCK_DGRAM)) {
1.10 misho 1079: rpc_SetErr(EINVAL, "Invalid parameters can`t init RPC server");
1.1 misho 1080: return NULL;
1081: }
1.14 misho 1082: if (!e_gethostbyname(csHost, Port, &sa))
1.10 misho 1083: return NULL;
1.1 misho 1084: if (!Port)
1085: Port = RPC_DEFPORT;
1.13 misho 1086: if (!proto)
1087: proto = SOCK_STREAM;
1.10 misho 1088: if (netBuf < RPC_MIN_BUFSIZ)
1.5 misho 1089: netBuf = BUFSIZ;
1.7 misho 1090: else
1.14 misho 1091: netBuf = E_ALIGN(netBuf, 2); /* align netBuf length */
1.10 misho 1092:
1093: #ifdef HAVE_SRANDOMDEV
1094: srandomdev();
1095: #else
1096: time_t tim;
1097:
1098: srandom((time(&tim) ^ getpid()));
1099: #endif
1.1 misho 1100:
1.14 misho 1101: srv = e_malloc(sizeof(rpc_srv_t));
1.1 misho 1102: if (!srv) {
1103: LOGERR;
1104: return NULL;
1105: } else
1106: memset(srv, 0, sizeof(rpc_srv_t));
1107:
1.13 misho 1108: srv->srv_proto = proto;
1.5 misho 1109: srv->srv_netbuf = netBuf;
1.1 misho 1110: srv->srv_session.sess_version = RPC_VERSION;
1.15 misho 1111: srv->srv_session.sess_instance = InstID;
1.1 misho 1112:
1113: srv->srv_server.cli_parent = srv;
1.10 misho 1114: memcpy(&srv->srv_server.cli_sa, &sa, sizeof srv->srv_server.cli_sa);
1115:
1.12 misho 1116: /* init functions */
1117: pthread_mutex_init(&srv->srv_funcs.mtx, NULL);
1118: SLIST_INIT(&srv->srv_funcs);
1119: AVL_INIT(&srv->srv_funcs);
1.10 misho 1120:
1121: /* init scheduler */
1122: srv->srv_root = schedBegin();
1123: if (!srv->srv_root) {
1124: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
1.12 misho 1125: pthread_mutex_destroy(&srv->srv_funcs.mtx);
1.14 misho 1126: e_free(srv);
1.10 misho 1127: return NULL;
1128: }
1129:
1130: /* init pool for clients */
1.14 misho 1131: srv->srv_clients = array_Init(concurentClients);
1.10 misho 1132: if (!srv->srv_clients) {
1.14 misho 1133: rpc_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
1.10 misho 1134: schedEnd(&srv->srv_root);
1.12 misho 1135: pthread_mutex_destroy(&srv->srv_funcs.mtx);
1.14 misho 1136: e_free(srv);
1.10 misho 1137: return NULL;
1138: }
1.4 misho 1139:
1140: /* create server socket */
1.13 misho 1141: srv->srv_server.cli_sock = socket(srv->srv_server.cli_sa.sa.sa_family, srv->srv_proto, 0);
1.1 misho 1142: if (srv->srv_server.cli_sock == -1) {
1143: LOGERR;
1.14 misho 1144: array_Destroy(&srv->srv_clients);
1.10 misho 1145: schedEnd(&srv->srv_root);
1.12 misho 1146: pthread_mutex_destroy(&srv->srv_funcs.mtx);
1.14 misho 1147: e_free(srv);
1.1 misho 1148: return NULL;
1149: }
1150: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
1151: LOGERR;
1.10 misho 1152: goto err;
1.1 misho 1153: }
1.5 misho 1154: n = srv->srv_netbuf;
1155: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof n) == -1) {
1156: LOGERR;
1.10 misho 1157: goto err;
1.5 misho 1158: }
1159: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_RCVBUF, &n, sizeof n) == -1) {
1160: LOGERR;
1.10 misho 1161: goto err;
1.5 misho 1162: }
1.6 misho 1163: if (bind(srv->srv_server.cli_sock, &srv->srv_server.cli_sa.sa,
1164: srv->srv_server.cli_sa.sa.sa_len) == -1) {
1.1 misho 1165: LOGERR;
1.10 misho 1166: goto err;
1.13 misho 1167: } else
1168: fcntl(srv->srv_server.cli_sock, F_SETFL,
1169: fcntl(srv->srv_server.cli_sock, F_GETFL) | O_NONBLOCK);
1.1 misho 1170:
1.10 misho 1171: rpc_register_srvPing(srv);
1.8 misho 1172:
1.1 misho 1173: return srv;
1.10 misho 1174: err: /* error condition */
1175: close(srv->srv_server.cli_sock);
1.14 misho 1176: array_Destroy(&srv->srv_clients);
1.10 misho 1177: schedEnd(&srv->srv_root);
1.12 misho 1178: pthread_mutex_destroy(&srv->srv_funcs.mtx);
1.14 misho 1179: e_free(srv);
1.10 misho 1180: return NULL;
1.1 misho 1181: }
1182:
1183: /*
1.7 misho 1184: * rpc_srv_endServer() - Destroy RPC server, close all opened sockets and free resources
1185: *
1.6 misho 1186: * @psrv = RPC Server instance
1.1 misho 1187: * return: none
1188: */
1.16 misho 1189: void
1.6 misho 1190: rpc_srv_endServer(rpc_srv_t ** __restrict psrv)
1.1 misho 1191: {
1.10 misho 1192: if (!psrv || !*psrv)
1.1 misho 1193: return;
1194:
1.10 misho 1195: /* if send kill to blob server */
1.17 misho 1196: rpc_srv_endBLOBServer(*psrv);
1.1 misho 1197:
1.10 misho 1198: (*psrv)->srv_kill = 1;
1199: sleep(RPC_SCHED_POLLING);
1.2 misho 1200:
1.17 misho 1201: schedEnd(&(*psrv)->srv_root);
1202:
1.12 misho 1203: pthread_mutex_destroy(&(*psrv)->srv_funcs.mtx);
1.14 misho 1204: e_free(*psrv);
1.6 misho 1205: *psrv = NULL;
1.1 misho 1206: }
1207:
1208: /*
1.7 misho 1209: * rpc_srv_loopServer() - Execute Main server loop and wait for clients requests
1210: *
1.1 misho 1211: * @srv = RPC Server instance
1212: * return: -1 error or 0 ok, infinite loop ...
1213: */
1214: int
1.5 misho 1215: rpc_srv_loopServer(rpc_srv_t * __restrict srv)
1.1 misho 1216: {
1.10 misho 1217: rpc_cli_t *c;
1.1 misho 1218: register int i;
1.12 misho 1219: rpc_func_t *f;
1.10 misho 1220: struct timespec ts = { RPC_SCHED_POLLING, 0 };
1.1 misho 1221:
1222: if (!srv) {
1.10 misho 1223: rpc_SetErr(EINVAL, "Invalid parameter can`t start RPC server");
1.1 misho 1224: return -1;
1225: }
1226:
1.13 misho 1227: if (srv->srv_proto == SOCK_STREAM)
1.14 misho 1228: if (listen(srv->srv_server.cli_sock, array_Size(srv->srv_clients)) == -1) {
1.13 misho 1229: LOGERR;
1230: return -1;
1231: }
1.1 misho 1232:
1.13 misho 1233: if (!schedRead(srv->srv_root, cbProto[srv->srv_proto][CB_ACCEPTCLIENT], srv,
1234: srv->srv_server.cli_sock, NULL, 0)) {
1.10 misho 1235: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
1236: return -1;
1237: }
1.7 misho 1238:
1.10 misho 1239: schedPolling(srv->srv_root, &ts, NULL);
1.7 misho 1240: /* main rpc loop */
1.10 misho 1241: schedRun(srv->srv_root, &srv->srv_kill);
1242:
1243: /* close all clients connections & server socket */
1.14 misho 1244: for (i = 0; i < array_Size(srv->srv_clients); i++) {
1245: c = array(srv->srv_clients, i, rpc_cli_t*);
1.10 misho 1246: if (c) {
1247: shutdown(c->cli_sock, SHUT_RDWR);
1248: close(c->cli_sock);
1249:
1250: schedCancelby(srv->srv_root, taskMAX, CRITERIA_ARG, c, NULL);
1.14 misho 1251: ait_freeVars(&RPC_RETVARS(c));
1.10 misho 1252: AIT_FREE_VAL(&c->cli_buf);
1.1 misho 1253: }
1.14 misho 1254: array_Del(srv->srv_clients, i, 42);
1.10 misho 1255: }
1.14 misho 1256: array_Destroy(&srv->srv_clients);
1.2 misho 1257:
1.10 misho 1258: close(srv->srv_server.cli_sock);
1.2 misho 1259:
1.10 misho 1260: /* detach exported calls */
1.12 misho 1261: RPC_FUNCS_LOCK(&srv->srv_funcs);
1262: while ((f = SLIST_FIRST(&srv->srv_funcs))) {
1263: SLIST_REMOVE_HEAD(&srv->srv_funcs, func_next);
1.1 misho 1264:
1.10 misho 1265: AIT_FREE_VAL(&f->func_name);
1.14 misho 1266: e_free(f);
1.1 misho 1267: }
1.12 misho 1268: srv->srv_funcs.avlh_root = NULL;
1269: RPC_FUNCS_UNLOCK(&srv->srv_funcs);
1.1 misho 1270:
1271: return 0;
1272: }
1273:
1274:
1275: /*
1276: * rpc_srv_execCall() Execute registered call from RPC server
1.7 misho 1277: *
1.10 misho 1278: * @cli = RPC client
1.1 misho 1279: * @rpc = IN RPC call structure
1.10 misho 1280: * @funcname = Execute RPC function
1.5 misho 1281: * @args = IN RPC calling arguments from RPC client
1.1 misho 1282: * return: -1 error, !=-1 ok
1283: */
1284: int
1.10 misho 1285: rpc_srv_execCall(rpc_cli_t * __restrict cli, struct tagRPCCall * __restrict rpc,
1286: ait_val_t funcname, array_t * __restrict args)
1.1 misho 1287: {
1288: rpc_callback_t func;
1289:
1.10 misho 1290: if (!cli || !rpc || !AIT_ADDR(&funcname)) {
1.7 misho 1291: rpc_SetErr(EINVAL, "Invalid parameter can`t exec function");
1.1 misho 1292: return -1;
1293: }
1294:
1.10 misho 1295: func = AIT_GET_LIKE(&funcname, rpc_callback_t);
1296: return func(cli, rpc, args);
1.1 misho 1297: }
1.23.6.1 misho 1298:
1299:
1300: /*
1301: * rpc_srv_initServer2() - Init & create layer2 RPC Server
1302: *
1303: * @InstID = Instance for authentication & recognition
1304: * @concurentClients = Concurent clients at same time to this server
1305: * @netBuf = Network buffer length (min:512 bytes), if =0 == BUFSIZ (also meaning max RPC packet)
1306: * @csIface = Interface name for bind server, if NULL first interface on host
1307: * return: NULL == error or !=NULL bind and created RPC server instance
1308: */
1309: rpc_srv_t *
1.23.6.2! misho 1310: rpc_srv_initServer2(u_char InstID, int concurentClients, int netBuf, const char *csIface)
1.23.6.1 misho 1311: {
1312: int n = 1;
1313: rpc_srv_t *srv = NULL;
1314: sockaddr_t sa = E_SOCKADDR_INIT;
1315: char szIface[64], szStr[STRSIZ];
1316: register int i;
1317: struct ifreq ifr;
1318:
1319: if (!concurentClients) {
1320: rpc_SetErr(EINVAL, "Invalid parameters can`t init RPC server");
1321: return NULL;
1322: }
1323: if (!csIface) {
1324: if (e_get1stiface(szIface, sizeof szIface))
1325: return NULL;
1326: } else
1327: strlcpy(szIface, csIface, sizeof szIface);
1328: if (e_getifacebyname(szIface, &sa))
1329: return NULL;
1330:
1331: #ifdef HAVE_SRANDOMDEV
1332: srandomdev();
1333: #else
1334: time_t tim;
1335:
1336: srandom((time(&tim) ^ getpid()));
1337: #endif
1338:
1339: srv = e_malloc(sizeof(rpc_srv_t));
1340: if (!srv) {
1341: LOGERR;
1342: return NULL;
1343: } else
1344: memset(srv, 0, sizeof(rpc_srv_t));
1345:
1.23.6.2! misho 1346: srv->srv_proto = SOCK_BPF;
1.23.6.1 misho 1347: srv->srv_netbuf = netBuf;
1348: srv->srv_session.sess_version = RPC_VERSION;
1349: srv->srv_session.sess_instance = InstID;
1350:
1351: srv->srv_server.cli_parent = srv;
1352: memcpy(&srv->srv_server.cli_sa, &sa, sizeof srv->srv_server.cli_sa);
1353:
1354: /* init functions */
1355: pthread_mutex_init(&srv->srv_funcs.mtx, NULL);
1356: SLIST_INIT(&srv->srv_funcs);
1357: AVL_INIT(&srv->srv_funcs);
1358:
1359: /* init scheduler */
1360: srv->srv_root = schedBegin();
1361: if (!srv->srv_root) {
1362: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
1363: pthread_mutex_destroy(&srv->srv_funcs.mtx);
1364: e_free(srv);
1365: return NULL;
1366: }
1367:
1368: /* init pool for clients */
1369: srv->srv_clients = array_Init(concurentClients);
1370: if (!srv->srv_clients) {
1371: rpc_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
1372: schedEnd(&srv->srv_root);
1373: pthread_mutex_destroy(&srv->srv_funcs.mtx);
1374: e_free(srv);
1375: return NULL;
1376: }
1377:
1378: /* create server handler */
1379: for (i = 0; i < 10; i++) {
1380: memset(szStr, 0, sizeof szStr);
1381: snprintf(szStr, sizeof szStr, "/dev/bpf%d", i);
1382: srv->srv_server.cli_sock = open(szStr, O_RDWR);
1383: if (srv->srv_server.cli_sock > STDERR_FILENO)
1384: break;
1385: }
1386: if (srv->srv_server.cli_sock < 3) {
1387: LOGERR;
1388: array_Destroy(&srv->srv_clients);
1389: schedEnd(&srv->srv_root);
1390: pthread_mutex_destroy(&srv->srv_funcs.mtx);
1391: e_free(srv);
1392: return NULL;
1393: }
1394:
1395: if (ioctl(srv->srv_server.cli_sock, BIOCIMMEDIATE, &n) == -1) {
1396: LOGERR;
1397: goto err;
1398: }
1399: n = (netBuf < RPC_MIN_BUFSIZ) ? getpagesize() : E_ALIGN(netBuf, 2);
1400: if (ioctl(srv->srv_server.cli_sock, BIOCSBLEN, &n) == -1) {
1401: LOGERR;
1402: goto err;
1403: } else
1404: srv->srv_netbuf = n;
1405:
1406: memset(&ifr, 0, sizeof ifr);
1407: strlcpy(ifr.ifr_name, szIface, sizeof ifr.ifr_name);
1408: if (ioctl(srv->srv_server.cli_sock, BIOCSETIF, &ifr) == -1) {
1409: LOGERR;
1410: goto err;
1411: } else
1412: fcntl(srv->srv_server.cli_sock, F_SETFL,
1413: fcntl(srv->srv_server.cli_sock, F_GETFL) | O_NONBLOCK);
1414:
1415: rpc_register_srvPing(srv);
1416:
1417: return srv;
1418: err: /* error condition */
1419: close(srv->srv_server.cli_sock);
1420: array_Destroy(&srv->srv_clients);
1421: schedEnd(&srv->srv_root);
1422: pthread_mutex_destroy(&srv->srv_funcs.mtx);
1423: e_free(srv);
1424: return NULL;
1425: }
1426:
1.23.6.2! misho 1427: /*
! 1428: * rpc_srv_loopServer2() - Execute Main layer2 server loop and wait for clients requests
! 1429: *
! 1430: * @srv = RPC Server instance
! 1431: * return: -1 error or 0 ok, infinite loop ...
! 1432: */
! 1433: int
! 1434: rpc_srv_loopServer2(rpc_srv_t * __restrict srv)
! 1435: {
! 1436: rpc_cli_t *c;
! 1437: register int i;
! 1438: rpc_func_t *f;
! 1439: struct timespec ts = { RPC_SCHED_POLLING, 0 };
! 1440:
! 1441: if (!srv) {
! 1442: rpc_SetErr(EINVAL, "Invalid parameter can`t start RPC server");
! 1443: return -1;
! 1444: }
! 1445:
! 1446: if (!schedRead(srv->srv_root, cbProto[srv->srv_proto][CB_ACCEPTCLIENT], srv,
! 1447: srv->srv_server.cli_sock, NULL, 0)) {
! 1448: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
! 1449: return -1;
! 1450: }
! 1451:
! 1452: schedPolling(srv->srv_root, &ts, NULL);
! 1453: /* main rpc loop */
! 1454: schedRun(srv->srv_root, &srv->srv_kill);
! 1455:
! 1456: /* close all clients connections & server socket */
! 1457: for (i = 0; i < array_Size(srv->srv_clients); i++) {
! 1458: c = array(srv->srv_clients, i, rpc_cli_t*);
! 1459: if (c) {
! 1460: schedCancelby(srv->srv_root, taskMAX, CRITERIA_ARG, c, NULL);
! 1461: ait_freeVars(&RPC_RETVARS(c));
! 1462: AIT_FREE_VAL(&c->cli_buf);
! 1463: }
! 1464: array_Del(srv->srv_clients, i, 42);
! 1465: }
! 1466: array_Destroy(&srv->srv_clients);
! 1467:
! 1468: close(srv->srv_server.cli_sock);
! 1469:
! 1470: /* detach exported calls */
! 1471: RPC_FUNCS_LOCK(&srv->srv_funcs);
! 1472: while ((f = SLIST_FIRST(&srv->srv_funcs))) {
! 1473: SLIST_REMOVE_HEAD(&srv->srv_funcs, func_next);
! 1474:
! 1475: AIT_FREE_VAL(&f->func_name);
! 1476: e_free(f);
! 1477: }
! 1478: srv->srv_funcs.avlh_root = NULL;
! 1479: RPC_FUNCS_UNLOCK(&srv->srv_funcs);
! 1480:
! 1481: return 0;
! 1482: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>