Annotation of libaitrpc/src/srv.c, revision 1.30.2.4
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.30.2.4! misho 6: * $Id: srv.c,v 1.30.2.3 2024/02/26 11:00:37 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.30.2.4! misho 15: Copyright 2004 - 2024
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 */
1.26 misho 61: static void *rxRAWPacket(sched_task_t *);
62: static void *txRAWPacket(sched_task_t *);
1.13 misho 63:
1.24 misho 64: /* SOCK_BPF */
65: static void *rxBPFPacket(sched_task_t *);
66: static void *txBPFPacket(sched_task_t *);
67:
1.25 misho 68: /* SOCK_EXT */
69: static void *rxEXTPacket(sched_task_t *);
70: static void *txEXTPacket(sched_task_t *);
71:
72: static sched_task_func_t cbProto[SOCK_MAX_SUPPORT][4] = {
1.27 misho 73: { acceptClients, closeClient, rxPacket, txPacket }, /* SOCK_STREAM */
74: { acceptClients, closeClient, rxPacket, txPacket }, /* SOCK_STREAM */
75: { rxUDPPacket, freeClient, NULL /*rxUDPPacket*/, txUDPPacket }, /* SOCK_DGRAM */
76: { rxRAWPacket, freeClient, NULL /*rxRAWPacket*/, txRAWPacket }, /* SOCK_RAW */
77: { rxBPFPacket, freeClient, NULL /*rxBPFPacket*/, txBPFPacket }, /* SOCK_BPF */
78: { rxEXTPacket, freeClient, NULL /*rxEXTPacket*/, txEXTPacket } /* SOCK_EXT */
1.13 misho 79: };
80:
1.23 misho 81: /* Global Signal Argument when kqueue support disabled */
82:
83: static volatile uintptr_t _glSigArg = 0;
84:
1.16 misho 85: void
1.13 misho 86: rpc_freeCli(rpc_cli_t * __restrict c)
1.10 misho 87: {
88: rpc_srv_t *s = c->cli_parent;
89:
1.13 misho 90: schedCancelby(s->srv_root, taskMAX, CRITERIA_ARG, c, NULL);
1.10 misho 91:
92: /* free buffer */
93: AIT_FREE_VAL(&c->cli_buf);
94:
1.14 misho 95: array_Del(s->srv_clients, c->cli_id, 0);
1.10 misho 96: if (c)
1.14 misho 97: e_free(c);
1.13 misho 98: }
99:
100:
101: static inline int
1.14 misho 102: _check4freeslot(rpc_srv_t * __restrict srv, sockaddr_t * __restrict sa)
1.13 misho 103: {
104: rpc_cli_t *c = NULL;
105: register int i;
106:
107: /* check free slots for connect */
1.14 misho 108: for (i = 0; i < array_Size(srv->srv_clients) &&
109: (c = array(srv->srv_clients, i, rpc_cli_t*)); i++)
1.13 misho 110: /* check for duplicates */
1.14 misho 111: if (sa && !e_addrcmp(&c->cli_sa, sa, 42))
1.13 misho 112: break;
1.14 misho 113: if (i >= array_Size(srv->srv_clients))
1.13 misho 114: return -1; /* no more free slots! */
115:
116: return i;
117: }
118:
119: static rpc_cli_t *
1.14 misho 120: _allocClient(rpc_srv_t * __restrict srv, sockaddr_t * __restrict sa)
1.13 misho 121: {
122: rpc_cli_t *c = NULL;
123: int n;
124:
1.25 misho 125: if (srv->srv_proto != SOCK_EXT)
126: n = _check4freeslot(srv, sa);
127: else
128: n = 0;
1.13 misho 129: if (n == -1)
130: return NULL;
131: else
1.14 misho 132: c = array(srv->srv_clients, n, rpc_cli_t*);
1.13 misho 133:
134: if (!c) {
1.14 misho 135: c = e_malloc(sizeof(rpc_cli_t));
1.13 misho 136: if (!c) {
137: LOGERR;
138: srv->srv_kill = 1;
139: return NULL;
140: } else {
141: memset(c, 0, sizeof(rpc_cli_t));
1.14 misho 142: array_Set(srv->srv_clients, n, c);
1.13 misho 143: c->cli_id = n;
144: c->cli_parent = srv;
145: }
146:
147: /* alloc empty buffer */
1.14 misho 148: AIT_SET_BUFSIZ(&c->cli_buf, 0, srv->srv_netbuf);
1.13 misho 149: }
150:
151: return c;
152: }
153:
154:
155: static void *
156: freeClient(sched_task_t *task)
157: {
158: rpc_freeCli(TASK_ARG(task));
159:
1.27 misho 160: taskExit(task, NULL);
1.13 misho 161: }
162:
163: static void *
164: closeClient(sched_task_t *task)
165: {
166: int sock = ((rpc_cli_t*) TASK_ARG(task))->cli_sock;
167:
168: rpc_freeCli(TASK_ARG(task));
169:
170: /* close client socket */
171: shutdown(sock, SHUT_RDWR);
172: close(sock);
1.27 misho 173: taskExit(task, NULL);
1.10 misho 174: }
1.7 misho 175:
176: static void *
177: txPacket(sched_task_t *task)
178: {
179: rpc_cli_t *c = TASK_ARG(task);
180: rpc_srv_t *s = c->cli_parent;
181: rpc_func_t *f = NULL;
1.18 misho 182: u_char *buf = AIT_GET_BUF(&c->cli_buf);
1.7 misho 183: struct tagRPCCall *rpc = (struct tagRPCCall*) buf;
1.27 misho 184: int ret, wlen = sizeof(struct tagRPCCall);
1.21 misho 185: #ifdef TCP_SESSION_TIMEOUT
186: struct timespec ts = { DEF_RPC_TIMEOUT, 0 };
187:
188: schedCancelby(TASK_ROOT(task), taskTIMER, CRITERIA_DATA, TASK_ARG(task), NULL);
189: schedTimer(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
190: TASK_ARG(task), ts, TASK_ARG(task), 0);
191: #endif
1.7 misho 192:
193: if (rpc->call_argc) {
1.10 misho 194: f = rpc_srv_getCall(s, ntohs(rpc->call_tag));
1.7 misho 195: if (!f) {
1.10 misho 196: rpc_SetErr(EPROGUNAVAIL, "Function not found at RPC server");
1.18 misho 197:
1.7 misho 198: rpc->call_argc ^= rpc->call_argc;
1.27 misho 199: RPC_SET_RETURN(rpc, -1);
200: RPC_SET_ERRNO(rpc, rpc_Errno);
201: } else if (rpc_pktFreeSpace(c) > s->srv_netbuf) {
202: rpc_SetErr(EMSGSIZE, "Message too long");
203:
204: rpc->call_argc ^= rpc->call_argc;
205: RPC_SET_RETURN(rpc, -1);
206: RPC_SET_ERRNO(rpc, rpc_Errno);
1.7 misho 207: } else {
1.25 misho 208: rpc->call_argc = (u_char) array_Size(RPC_RETVARS(c));
1.7 misho 209: /* Go Encapsulate variables */
1.18 misho 210: ret = ait_vars2buffer(buf + wlen, AIT_LEN(&c->cli_buf) - wlen,
211: RPC_RETVARS(c));
1.7 misho 212: if (ret == -1) {
213: rpc_SetErr(EBADRPC, "Prepare RPC packet failed");
1.19 misho 214:
1.7 misho 215: rpc->call_argc ^= rpc->call_argc;
1.27 misho 216: RPC_SET_RETURN(rpc, -1);
217: RPC_SET_ERRNO(rpc, rpc_Errno);
1.7 misho 218: } else
219: wlen += ret;
220: }
221: }
222:
1.27 misho 223: /* Free return values */
224: ait_freeVars(&c->cli_vars);
225:
1.18 misho 226: rpc->call_len = htonl(wlen);
1.25 misho 227: rpc->call_io = RPC_ACK;
1.8 misho 228:
1.15 misho 229: #if 0
1.7 misho 230: /* calculate CRC */
231: rpc->call_crc ^= rpc->call_crc;
1.8 misho 232: rpc->call_crc = htons(crcFletcher16((u_short*) buf, wlen / 2));
1.15 misho 233: #endif
1.7 misho 234:
235: /* send reply */
1.27 misho 236: ret = send(TASK_FD(task), buf, wlen, MSG_NOSIGNAL);
237: if (ret == -1) {
238: /* close connection */
239: schedEvent(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
240: TASK_ARG(task), 0, NULL, 0);
1.10 misho 241: }
1.7 misho 242:
1.27 misho 243: taskExit(task, NULL);
1.7 misho 244: }
245:
246: static void *
247: execCall(sched_task_t *task)
248: {
249: rpc_cli_t *c = TASK_ARG(task);
250: rpc_srv_t *s = c->cli_parent;
251: rpc_func_t *f = NULL;
252: array_t *arr = NULL;
1.18 misho 253: u_char *buf = AIT_GET_BUF(&c->cli_buf);
1.7 misho 254: struct tagRPCCall *rpc = (struct tagRPCCall*) buf;
1.25 misho 255: int argc = rpc->call_argc;
1.7 misho 256:
257: /* Go decapsulate variables ... */
1.8 misho 258: if (argc) {
1.14 misho 259: arr = ait_buffer2vars(buf + sizeof(struct tagRPCCall),
1.18 misho 260: AIT_LEN(&c->cli_buf) - sizeof(struct tagRPCCall), argc, 42);
1.7 misho 261: if (!arr) {
1.14 misho 262: rpc_SetErr(ERPCMISMATCH, "#%d - %s", elwix_GetErrno(), elwix_GetError());
1.18 misho 263:
1.7 misho 264: rpc->call_argc ^= rpc->call_argc;
1.27 misho 265: RPC_SET_RETURN(rpc, -1);
266: RPC_SET_ERRNO(rpc, rpc_Errno);
267: taskExit(task, NULL);
1.7 misho 268: }
1.10 misho 269: } else
270: arr = NULL;
1.7 misho 271:
1.10 misho 272: if (!(f = rpc_srv_getCall(s, ntohs(rpc->call_tag)))) {
1.7 misho 273: rpc_SetErr(EPROGUNAVAIL, "Function not found at RPC server");
1.18 misho 274:
1.7 misho 275: rpc->call_argc ^= rpc->call_argc;
1.27 misho 276: RPC_SET_RETURN(rpc, -1);
277: RPC_SET_ERRNO(rpc, rpc_Errno);
1.7 misho 278: } else {
1.8 misho 279: /* if client doesn't want reply */
1.27 misho 280: RPC_SET_RETURN(rpc, rpc_srv_execCall(c, rpc, f->func_name, arr));
1.7 misho 281: if (rpc->call_rep.ret == htonl(-1)) {
1.20 misho 282: if (!rpc->call_rep.eno) {
283: LOGERR;
1.27 misho 284: RPC_SET_ERRNO(rpc, rpc_Errno);
1.20 misho 285: }
1.7 misho 286: rpc->call_argc ^= rpc->call_argc;
1.27 misho 287: ait_freeVars(&c->cli_vars);
1.7 misho 288: } else {
289: rpc->call_rep.eno ^= rpc->call_rep.eno;
1.27 misho 290: rpc->call_argc ^= rpc->call_argc;
291: if (TASK_VAL(task)) {
1.8 misho 292: /* without reply */
1.14 misho 293: ait_freeVars(&c->cli_vars);
1.10 misho 294: } else {
295: /* reply */
1.25 misho 296: rpc->call_argc = (u_char) array_Size(RPC_RETVARS(c));
1.10 misho 297: }
1.7 misho 298: }
299: }
300:
1.14 misho 301: array_Destroy(&arr);
1.27 misho 302: taskExit(task, NULL);
1.7 misho 303: }
304:
305: static void *
306: rxPacket(sched_task_t *task)
307: {
308: rpc_cli_t *c = TASK_ARG(task);
309: rpc_srv_t *s = c->cli_parent;
1.27 misho 310: int len, noreply = 0, rlen = AIT_LEN(&c->cli_buf);
1.15 misho 311: #if 0
312: u_short crc;
313: #endif
1.10 misho 314: u_char *buf = AIT_GET_BUF(&c->cli_buf);
1.29 misho 315: struct tagRPCCall b, *rpc = (struct tagRPCCall*) buf;
1.21 misho 316: #ifdef TCP_SESSION_TIMEOUT
317: struct timespec ts = { DEF_RPC_TIMEOUT, 0 };
318:
319: schedCancelby(TASK_ROOT(task), taskTIMER, CRITERIA_DATA, TASK_ARG(task), NULL);
320: schedTimer(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
321: TASK_ARG(task), ts, TASK_ARG(task), 0);
322: #endif
1.7 misho 323:
1.27 misho 324: /* prepare rx */
1.29 misho 325: len = recv(TASK_FD(task), &b, sizeof b, MSG_PEEK);
1.27 misho 326: if (len == sizeof b)
1.29 misho 327: rlen = ntohl(b.call_len);
1.27 misho 328:
329: rlen = recv(TASK_FD(task), buf, rlen, 0);
330: if (rlen == -1) {
1.10 misho 331: /* close connection */
1.13 misho 332: schedEvent(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
333: TASK_ARG(task), 0, NULL, 0);
1.27 misho 334: taskExit(task, NULL);
1.18 misho 335: }
1.27 misho 336: if (rlen < sizeof(struct tagRPCCall)) {
337: rpc_SetErr(ERPCMISMATCH, "Short RPC packet");
1.18 misho 338:
1.27 misho 339: rpc->call_argc ^= rpc->call_argc;
340: RPC_SET_RETURN(rpc, -1);
341: RPC_SET_ERRNO(rpc, rpc_Errno);
342: goto err;
343: } else
344: len = ntohl(rpc->call_len);
345: if (rlen < len || len > AIT_LEN(&c->cli_buf)) {
346: rpc_SetErr(ERPCMISMATCH, "Short RPC packet");
347:
348: rpc->call_argc ^= rpc->call_argc;
349: RPC_SET_RETURN(rpc, -1);
350: RPC_SET_ERRNO(rpc, rpc_Errno);
351: goto err;
1.18 misho 352: }
1.8 misho 353:
1.25 misho 354: /* skip loop packet */
355: if (rpc->call_io & RPC_ACK) {
356: schedReadSelf(task);
1.27 misho 357: taskExit(task, NULL);
1.25 misho 358: }
359:
1.15 misho 360: #if 0
1.18 misho 361: /* check integrity of packet */
362: crc = ntohs(rpc->call_crc);
363: rpc->call_crc ^= rpc->call_crc;
364: if (crc != crcFletcher16((u_short*) rpc, len / 2)) {
365: rpc_SetErr(ERPCMISMATCH, "Bad CRC RPC packet");
1.27 misho 366:
367: rpc->call_argc ^= rpc->call_argc;
368: RPC_SET_RETURN(rpc, -1);
369: RPC_SET_ERRNO(rpc, rpc_Errno);
370: goto err;
1.18 misho 371: }
1.15 misho 372: #endif
1.7 misho 373:
1.18 misho 374: /* check RPC packet session info */
375: if (rpc_chkPktSession(&rpc->call_session, &s->srv_session)) {
376: rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session");
1.7 misho 377:
1.18 misho 378: rpc->call_argc ^= rpc->call_argc;
1.27 misho 379: RPC_SET_RETURN(rpc, -1);
380: RPC_SET_ERRNO(rpc, rpc_Errno);
381: goto err;
1.18 misho 382: }
1.7 misho 383:
1.27 misho 384: noreply = RPC_CHK_NOREPLY(rpc);
385:
386: /* execute RPC call */
387: schedEvent(TASK_ROOT(task), execCall, TASK_ARG(task), (int) noreply, rpc, len);
388: err:
1.18 misho 389: /* send RPC reply */
1.30.2.3 misho 390: if (!noreply && (rpc->call_io & RPC_REQ))
1.18 misho 391: schedWrite(TASK_ROOT(task), cbProto[s->srv_proto][CB_TXPACKET],
392: TASK_ARG(task), TASK_FD(task), rpc, len);
1.7 misho 393:
394: /* lets get next packet */
1.18 misho 395: schedReadSelf(task);
1.27 misho 396: taskExit(task, NULL);
1.7 misho 397: }
398:
1.1 misho 399: static void *
1.10 misho 400: acceptClients(sched_task_t *task)
1.1 misho 401: {
1.10 misho 402: rpc_srv_t *srv = TASK_ARG(task);
403: rpc_cli_t *c = NULL;
1.29 misho 404: socklen_t salen = E_SOCKADDR_MAX;
1.21 misho 405: int sock;
406: #ifdef TCP_SESSION_TIMEOUT
407: struct timespec ts = { DEF_RPC_TIMEOUT, 0 };
408: #endif
1.7 misho 409:
1.13 misho 410: c = _allocClient(srv, NULL);
1.21 misho 411: if (!c) {
412: EVERBOSE(1, "RPC client quota exceeded! Connection will be shutdown!\n");
413: if ((sock = accept(TASK_FD(task), NULL, NULL)) != -1) {
414: shutdown(sock, SHUT_RDWR);
415: close(sock);
416: }
1.10 misho 417: goto end;
1.21 misho 418: }
1.10 misho 419:
420: /* accept client */
421: c->cli_sock = accept(TASK_FD(task), &c->cli_sa.sa, &salen);
422: if (c->cli_sock == -1) {
423: LOGERR;
424: AIT_FREE_VAL(&c->cli_buf);
1.14 misho 425: array_Del(srv->srv_clients, c->cli_id, 42);
1.10 misho 426: goto end;
1.30 misho 427: } else {
1.10 misho 428: fcntl(c->cli_sock, F_SETFL, fcntl(c->cli_sock, F_GETFL) | O_NONBLOCK);
1.30 misho 429: fcntl(c->cli_sock, F_SETFD, FD_CLOEXEC);
430: }
1.1 misho 431:
1.21 misho 432: #ifdef TCP_SESSION_TIMEOUT
433: /* armed timer for close stateless connection */
434: schedCancelby(TASK_ROOT(task), taskTIMER, CRITERIA_DATA, c, NULL);
435: schedTimer(TASK_ROOT(task), cbProto[srv->srv_proto][CB_CLOSECLIENT], c,
436: ts, c, 0);
437: #endif
1.13 misho 438: schedRead(TASK_ROOT(task), cbProto[srv->srv_proto][CB_RXPACKET], c,
439: c->cli_sock, NULL, 0);
1.10 misho 440: end:
441: schedReadSelf(task);
1.27 misho 442: taskExit(task, NULL);
1.10 misho 443: }
1.5 misho 444:
1.7 misho 445:
1.10 misho 446: static void *
1.13 misho 447: txUDPPacket(sched_task_t *task)
1.10 misho 448: {
449: rpc_cli_t *c = TASK_ARG(task);
450: rpc_srv_t *s = c->cli_parent;
1.13 misho 451: rpc_func_t *f = NULL;
1.18 misho 452: u_char *buf = AIT_GET_BUF(&c->cli_buf);
1.13 misho 453: struct tagRPCCall *rpc = (struct tagRPCCall*) buf;
1.27 misho 454: int ret, wlen = sizeof(struct tagRPCCall);
1.13 misho 455: struct timespec ts = { DEF_RPC_TIMEOUT, 0 };
456:
457: schedCancelby(TASK_ROOT(task), taskTIMER, CRITERIA_DATA, TASK_ARG(task), NULL);
458: schedTimer(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
459: TASK_ARG(task), ts, TASK_ARG(task), 0);
460:
461: if (rpc->call_argc) {
462: f = rpc_srv_getCall(s, ntohs(rpc->call_tag));
463: if (!f) {
464: rpc_SetErr(EPROGUNAVAIL, "Function not found at RPC server");
1.27 misho 465:
1.13 misho 466: rpc->call_argc ^= rpc->call_argc;
1.27 misho 467: RPC_SET_RETURN(rpc, -1);
468: RPC_SET_ERRNO(rpc, rpc_Errno);
469: } else if (rpc_pktFreeSpace(c) > s->srv_netbuf) {
470: rpc_SetErr(EMSGSIZE, "Message too long");
471:
472: rpc->call_argc ^= rpc->call_argc;
473: RPC_SET_RETURN(rpc, -1);
474: RPC_SET_ERRNO(rpc, rpc_Errno);
1.13 misho 475: } else {
1.25 misho 476: rpc->call_argc = (u_char) array_Size(RPC_RETVARS(c));
1.13 misho 477: /* Go Encapsulate variables */
1.18 misho 478: ret = ait_vars2buffer(buf + wlen, AIT_LEN(&c->cli_buf) - wlen,
479: RPC_RETVARS(c));
1.13 misho 480: if (ret == -1) {
481: rpc_SetErr(EBADRPC, "Prepare RPC packet failed");
1.27 misho 482:
1.13 misho 483: rpc->call_argc ^= rpc->call_argc;
1.27 misho 484: RPC_SET_RETURN(rpc, -1);
485: RPC_SET_ERRNO(rpc, rpc_Errno);
1.13 misho 486: } else
487: wlen += ret;
488: }
489: }
1.7 misho 490:
1.27 misho 491: /* Free return values */
492: ait_freeVars(&c->cli_vars);
493:
1.18 misho 494: rpc->call_len = htonl(wlen);
1.25 misho 495: rpc->call_io = RPC_ACK;
1.7 misho 496:
1.13 misho 497: /* calculate CRC */
498: rpc->call_crc ^= rpc->call_crc;
499: rpc->call_crc = htons(crcFletcher16((u_short*) buf, wlen / 2));
500:
501: /* send reply */
1.27 misho 502: ret = sendto(TASK_FD(task), buf, wlen, MSG_NOSIGNAL,
1.29 misho 503: &c->cli_sa.sa, e_addrlen(&c->cli_sa));
1.27 misho 504: if (ret == -1) {
505: /* close connection */
506: schedEvent(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
507: TASK_ARG(task), 0, NULL, 0);
1.13 misho 508: }
509:
1.27 misho 510: taskExit(task, NULL);
1.13 misho 511: }
512:
513: static void *
514: rxUDPPacket(sched_task_t *task)
515: {
516: rpc_srv_t *srv = TASK_ARG(task);
517: rpc_cli_t *c = NULL;
1.27 misho 518: int len, noreply = 0, rlen;
1.22 misho 519: u_short crc;
1.27 misho 520: struct tagRPCCall *rpc;
1.14 misho 521: sockaddr_t sa;
1.29 misho 522: socklen_t salen = E_SOCKADDR_MAX;
1.13 misho 523: struct timespec ts = { DEF_RPC_TIMEOUT, 0 };
1.27 misho 524: ait_val_t b = AIT_VAL_INIT;
1.13 misho 525:
526: /* receive connect packet */
1.27 misho 527: AIT_SET_BUF(&b, NULL, srv->srv_netbuf);
1.29 misho 528: #ifndef __linux__
529: sa.ss.ss_len = salen;
530: #endif
1.27 misho 531: rlen = recvfrom(TASK_FD(task), AIT_GET_BUF(&b), AIT_LEN(&b), 0, &sa.sa, &salen);
532: rpc = (struct tagRPCCall*) AIT_GET_BUF(&b);
533: if (rlen < sizeof(struct tagRPCCall))
534: goto end;
535: else
536: len = ntohl(rpc->call_len);
537: if (rlen < len || len > srv->srv_netbuf)
538: goto end;
539:
540: /* skip loop packet */
541: if (rpc->call_io & RPC_ACK)
542: goto end;
543:
544: /* check integrity of packet */
545: crc = ntohs(rpc->call_crc);
546: rpc->call_crc ^= rpc->call_crc;
1.30.2.2 misho 547: if (crc != crcFletcher16((u_short*) AIT_GET_BUF(&b), len / 2))
1.27 misho 548: goto end;
549:
550: /* check RPC packet session info */
551: if (rpc_chkPktSession(&rpc->call_session, &srv->srv_session))
1.13 misho 552: goto end;
553:
554: c = _allocClient(srv, &sa);
1.22 misho 555: if (!c) {
556: EVERBOSE(1, "RPC client quota exceeded! Connection will be shutdown!\n");
557: usleep(2000); /* blocked client delay */
1.13 misho 558: goto end;
1.22 misho 559: } else {
1.27 misho 560: memcpy(AIT_GET_BUF(&c->cli_buf), AIT_GET_BUF(&b), len);
1.22 misho 561: rpc = (struct tagRPCCall*) AIT_GET_BUF(&c->cli_buf);
562:
1.13 misho 563: c->cli_sock = TASK_FD(task);
564: memcpy(&c->cli_sa, &sa, sizeof c->cli_sa);
1.22 misho 565:
1.13 misho 566: /* armed timer for close stateless connection */
567: schedCancelby(TASK_ROOT(task), taskTIMER, CRITERIA_DATA, c, NULL);
568: schedTimer(TASK_ROOT(task), cbProto[srv->srv_proto][CB_CLOSECLIENT],
569: c, ts, c, 0);
570: }
571:
1.24 misho 572: noreply = RPC_CHK_NOREPLY(rpc);
573:
1.27 misho 574: /* execute RPC call */
575: schedEvent(TASK_ROOT(task), execCall, c, (int) noreply, rpc, len);
1.24 misho 576:
577: /* send RPC reply */
1.30.2.3 misho 578: if (!noreply && (rpc->call_io & RPC_REQ))
1.24 misho 579: schedWrite(TASK_ROOT(task), cbProto[srv->srv_proto][CB_TXPACKET],
580: c, TASK_FD(task), rpc, len);
581: end:
1.27 misho 582: AIT_FREE_VAL(&b);
1.24 misho 583: schedReadSelf(task);
1.27 misho 584: taskExit(task, NULL);
1.24 misho 585: }
586:
587:
588: static void *
1.26 misho 589: txRAWPacket(sched_task_t *task)
590: {
591: rpc_cli_t *c = TASK_ARG(task);
592: rpc_srv_t *s = c->cli_parent;
593: rpc_func_t *f = NULL;
594: u_char *buf = AIT_GET_BUF(&c->cli_buf);
595: struct tagRPCCall *rpc = (struct tagRPCCall*) buf;
1.27 misho 596: int ret, wlen = sizeof(struct tagRPCCall);
1.26 misho 597: struct timespec ts = { DEF_RPC_TIMEOUT, 0 };
598:
599: schedCancelby(TASK_ROOT(task), taskTIMER, CRITERIA_DATA, TASK_ARG(task), NULL);
600: schedTimer(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
601: TASK_ARG(task), ts, TASK_ARG(task), 0);
602:
603: if (rpc->call_argc) {
604: f = rpc_srv_getCall(s, ntohs(rpc->call_tag));
605: if (!f) {
606: rpc_SetErr(EPROGUNAVAIL, "Function not found at RPC server");
1.27 misho 607:
1.26 misho 608: rpc->call_argc ^= rpc->call_argc;
1.27 misho 609: RPC_SET_RETURN(rpc, -1);
610: RPC_SET_ERRNO(rpc, rpc_Errno);
611: } else if (rpc_pktFreeSpace(c) > s->srv_netbuf) {
612: rpc_SetErr(EMSGSIZE, "Message too long");
613:
614: rpc->call_argc ^= rpc->call_argc;
615: RPC_SET_RETURN(rpc, -1);
616: RPC_SET_ERRNO(rpc, rpc_Errno);
1.26 misho 617: } else {
618: rpc->call_argc = (u_char) array_Size(RPC_RETVARS(c));
619: /* Go Encapsulate variables */
620: ret = ait_vars2buffer(buf + wlen, AIT_LEN(&c->cli_buf) - wlen,
621: RPC_RETVARS(c));
622: if (ret == -1) {
623: rpc_SetErr(EBADRPC, "Prepare RPC packet failed");
1.27 misho 624:
1.26 misho 625: rpc->call_argc ^= rpc->call_argc;
1.27 misho 626: RPC_SET_RETURN(rpc, -1);
627: RPC_SET_ERRNO(rpc, rpc_Errno);
1.26 misho 628: } else
629: wlen += ret;
630: }
631: }
632:
1.27 misho 633: /* Free return values */
634: ait_freeVars(&c->cli_vars);
635:
1.26 misho 636: rpc->call_len = htonl(wlen);
637: rpc->call_io = RPC_ACK;
638:
639: /* calculate CRC */
640: rpc->call_crc ^= rpc->call_crc;
641: rpc->call_crc = htons(crcFletcher16((u_short*) buf, wlen / 2));
642:
643: /* send reply */
1.27 misho 644: ret = sendto(TASK_FD(task), buf, wlen, MSG_NOSIGNAL,
1.29 misho 645: &c->cli_sa.sa, e_addrlen(&c->cli_sa));
1.27 misho 646: if (ret == -1) {
647: /* close connection */
648: schedEvent(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
649: TASK_ARG(task), 0, NULL, 0);
1.26 misho 650: }
651:
1.27 misho 652: taskExit(task, NULL);
1.26 misho 653: }
654:
655: static void *
656: rxRAWPacket(sched_task_t *task)
657: {
658: rpc_srv_t *srv = TASK_ARG(task);
659: rpc_cli_t *c = NULL;
1.27 misho 660: int len, noreply = 0, rlen;
1.26 misho 661: u_short crc;
662: struct tagRPCCall *rpc;
663: sockaddr_t sa;
1.29 misho 664: socklen_t salen = E_SOCKADDR_MAX;
1.26 misho 665: struct timespec ts = { DEF_RPC_TIMEOUT, 0 };
1.27 misho 666: ait_val_t b = AIT_VAL_INIT;
1.26 misho 667:
668: /* receive connect packet */
1.27 misho 669: AIT_SET_BUF(&b, NULL, srv->srv_netbuf);
1.29 misho 670: #ifndef __linux__
671: sa.ss.ss_len = salen;
672: #endif
1.27 misho 673: rlen = recvfrom(TASK_FD(task), AIT_GET_BUF(&b), AIT_LEN(&b), 0, &sa.sa, &salen);
674: if (sa.sa.sa_family == AF_INET) {
675: struct ip *h;
676: h = (struct ip*) AIT_GET_BUF(&b);
677: if (rlen < ntohs(h->ip_len) || h->ip_p != IPPROTO_ERPC)
678: goto end;
679: else {
680: rlen -= sizeof(struct ip);
681: rpc = (struct tagRPCCall*) (h + 1);
682: }
683: } else {
1.29 misho 684: #ifdef IPV6_REMOVE_HEADER
1.27 misho 685: struct ip6_hdr *h;
686: h = (struct ip6_hdr*) AIT_GET_BUF(&b);
1.29 misho 687: if (rlen < ntohs(h->ip6_plen) || h->ip6_nxt != IPPROTO_ERPC)
1.27 misho 688: goto end;
689: else {
690: rlen -= sizeof(struct ip6_hdr);
691: rpc = (struct tagRPCCall*) (h + 1);
692: }
1.29 misho 693: #else
694: rpc = (struct tagRPCCall*) AIT_GET_BUF(&b);
695: #endif
1.27 misho 696: }
697: if (rlen < sizeof(struct tagRPCCall))
698: goto end;
699: else
700: len = ntohl(rpc->call_len);
701: if (rlen < len || len > srv->srv_netbuf)
702: goto end;
703:
704: /* skip loop packet */
705: if (rpc->call_io & RPC_ACK)
706: goto end;
707:
708: /* check integrity of packet */
709: crc = ntohs(rpc->call_crc);
710: rpc->call_crc ^= rpc->call_crc;
1.30.2.2 misho 711: if (crc != crcFletcher16((u_short*) AIT_GET_BUF(&b), len / 2))
1.27 misho 712: goto end;
713:
714: /* check RPC packet session info */
715: if (rpc_chkPktSession(&rpc->call_session, &srv->srv_session))
1.26 misho 716: goto end;
717:
718: c = _allocClient(srv, &sa);
719: if (!c) {
720: EVERBOSE(1, "RPC client quota exceeded! Connection will be shutdown!\n");
721: usleep(2000); /* blocked client delay */
722: goto end;
723: } else {
1.27 misho 724: memcpy(AIT_GET_BUF(&c->cli_buf), rpc, len);
725: rpc = (struct tagRPCCall*) AIT_GET_BUF(&c->cli_buf);
1.26 misho 726:
727: c->cli_sock = TASK_FD(task);
728: memcpy(&c->cli_sa, &sa, sizeof c->cli_sa);
729:
730: /* armed timer for close stateless connection */
731: schedCancelby(TASK_ROOT(task), taskTIMER, CRITERIA_DATA, c, NULL);
732: schedTimer(TASK_ROOT(task), cbProto[srv->srv_proto][CB_CLOSECLIENT],
733: c, ts, c, 0);
734: }
735:
736: noreply = RPC_CHK_NOREPLY(rpc);
737:
1.27 misho 738: /* execute RPC call */
739: schedEvent(TASK_ROOT(task), execCall, c, (int) noreply, rpc, len);
1.26 misho 740:
741: /* send RPC reply */
1.30.2.3 misho 742: if (!noreply && (rpc->call_io & RPC_REQ))
1.26 misho 743: schedWrite(TASK_ROOT(task), cbProto[srv->srv_proto][CB_TXPACKET],
744: c, TASK_FD(task), rpc, len);
745: end:
1.27 misho 746: AIT_FREE_VAL(&b);
1.26 misho 747: schedReadSelf(task);
1.27 misho 748: taskExit(task, NULL);
1.26 misho 749: }
750:
751:
752: static void *
1.24 misho 753: txBPFPacket(sched_task_t *task)
754: {
1.29 misho 755: #ifndef __linux__
1.24 misho 756: rpc_cli_t *c = TASK_ARG(task);
757: rpc_srv_t *s = c->cli_parent;
758: rpc_func_t *f = NULL;
759: u_char *buf = AIT_GET_BUF(&c->cli_buf);
760: struct tagRPCCall *rpc = (struct tagRPCCall*) buf;
1.27 misho 761: int ret, wlen = sizeof(struct tagRPCCall);
1.24 misho 762: struct timespec ts = { DEF_RPC_TIMEOUT, 0 };
763: struct ether_header *eh;
764: ait_val_t b = AIT_VAL_INIT;
765:
766: schedCancelby(TASK_ROOT(task), taskTIMER, CRITERIA_DATA, TASK_ARG(task), NULL);
767: schedTimer(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
768: TASK_ARG(task), ts, TASK_ARG(task), 0);
769:
770: if (rpc->call_argc) {
771: f = rpc_srv_getCall(s, ntohs(rpc->call_tag));
772: if (!f) {
773: rpc_SetErr(EPROGUNAVAIL, "Function not found at RPC server");
1.27 misho 774:
1.24 misho 775: rpc->call_argc ^= rpc->call_argc;
1.27 misho 776: RPC_SET_RETURN(rpc, -1);
777: RPC_SET_ERRNO(rpc, rpc_Errno);
778: } else if (rpc_pktFreeSpace(c) > s->srv_netbuf) {
779: rpc_SetErr(EMSGSIZE, "Message too long");
780:
781: rpc->call_argc ^= rpc->call_argc;
782: RPC_SET_RETURN(rpc, -1);
783: RPC_SET_ERRNO(rpc, rpc_Errno);
1.24 misho 784: } else {
1.25 misho 785: rpc->call_argc = (u_char) array_Size(RPC_RETVARS(c));
1.24 misho 786: /* Go Encapsulate variables */
787: ret = ait_vars2buffer(buf + wlen, AIT_LEN(&c->cli_buf) - wlen,
788: RPC_RETVARS(c));
789: if (ret == -1) {
790: rpc_SetErr(EBADRPC, "Prepare RPC packet failed");
1.27 misho 791:
1.24 misho 792: rpc->call_argc ^= rpc->call_argc;
1.27 misho 793: RPC_SET_RETURN(rpc, -1);
794: RPC_SET_ERRNO(rpc, rpc_Errno);
1.24 misho 795: } else
796: wlen += ret;
797: }
798: }
799:
1.27 misho 800: /* Free return values */
801: ait_freeVars(&RPC_RETVARS(c));
802:
1.24 misho 803: rpc->call_len = htonl(wlen);
1.25 misho 804: rpc->call_io = RPC_ACK;
1.24 misho 805:
806: /* calculate CRC */
807: rpc->call_crc ^= rpc->call_crc;
808: rpc->call_crc = htons(crcFletcher16((u_short*) buf, wlen / 2));
809:
810: /* send reply */
1.27 misho 811: AIT_SET_BUF(&b, NULL, wlen + ETHER_HDR_LEN);
1.24 misho 812: eh = (struct ether_header*) AIT_GET_BUF(&b);
813: memcpy(eh->ether_dhost, LLADDR(&c->cli_sa.sdl), ETHER_ADDR_LEN);
814: eh->ether_type = htons(RPC_DEFPORT);
1.27 misho 815: memcpy(eh + 1, buf, wlen);
1.24 misho 816:
817: ret = write(TASK_FD(task), AIT_GET_BUF(&b), AIT_LEN(&b));
818: AIT_FREE_VAL(&b);
819: if (ret == -1) {
820: /* close connection */
821: schedEvent(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
822: TASK_ARG(task), 0, NULL, 0);
1.22 misho 823: }
1.29 misho 824: #else
825: rpc_SetErr(ENOTSUP, "Feature isn't supported on Linux!");
826: #endif
1.13 misho 827:
1.27 misho 828: taskExit(task, NULL);
1.24 misho 829: }
830:
831: static void *
832: rxBPFPacket(sched_task_t *task)
833: {
1.29 misho 834: #ifndef __linux__
1.24 misho 835: rpc_srv_t *srv = TASK_ARG(task);
836: rpc_cli_t *c = NULL;
837: int len, rlen, noreply;
838: u_short crc;
839: struct tagRPCCall *rpc;
840: sockaddr_t sa;
841: struct timespec ts = { DEF_RPC_TIMEOUT, 0 };
842: struct bpf_hdr *h;
843: struct ether_header *eh;
844: ait_val_t b = AIT_VAL_INIT;
845:
846: /* receive connect packet */
847: AIT_SET_BUF(&b, NULL, srv->srv_netbuf);
848: rlen = read(TASK_FD(task), AIT_GET_BUF(&b), AIT_LEN(&b));
849: h = (struct bpf_hdr*) AIT_GET_BUF(&b);
850: rlen -= h->bh_hdrlen;
851: if (rlen < h->bh_datalen || h->bh_caplen != h->bh_datalen ||
1.27 misho 852: rlen < ETHER_HDR_LEN + sizeof(struct tagRPCCall))
1.24 misho 853: goto end;
1.27 misho 854: else {
1.24 misho 855: rlen = h->bh_caplen;
856: eh = (struct ether_header*) (AIT_GET_BUF(&b) + h->bh_hdrlen);
857: rlen -= ETHER_HDR_LEN;
858: rpc = (struct tagRPCCall*) (eh + 1);
1.25 misho 859:
1.24 misho 860: if (eh->ether_type != ntohs(RPC_DEFPORT))
861: goto end;
862: else
863: e_getlinkbymac((const ether_addr_t*) eh->ether_shost, &sa);
864: }
1.27 misho 865: if (rlen < sizeof(struct tagRPCCall))
866: goto end;
867: else
868: len = ntohl(rpc->call_len);
869: if (rlen < len || len > srv->srv_netbuf)
870: goto end;
871:
872: #ifdef CHECK_ETHACK
873: /* skip loop packet */
874: if (rpc->call_io & RPC_ACK)
875: goto end;
876: #endif
877:
878: /* check integrity of packet */
879: crc = ntohs(rpc->call_crc);
880: rpc->call_crc ^= rpc->call_crc;
881: if (crc != crcFletcher16((u_short*) rpc, len / 2))
882: goto end;
883:
884: /* check RPC packet session info */
885: if (rpc_chkPktSession(&rpc->call_session, &srv->srv_session))
886: goto end;
1.24 misho 887:
888: c = _allocClient(srv, &sa);
889: if (!c) {
890: EVERBOSE(1, "RPC client quota exceeded! Connection will be shutdown!\n");
891: usleep(2000); /* blocked client delay */
892: goto end;
893: } else {
1.27 misho 894: memcpy(AIT_GET_BUF(&c->cli_buf), rpc, len);
1.24 misho 895: rpc = (struct tagRPCCall*) AIT_GET_BUF(&c->cli_buf);
896:
897: c->cli_sock = TASK_FD(task);
898: memcpy(&c->cli_sa, &sa, sizeof c->cli_sa);
899:
900: /* armed timer for close stateless connection */
901: schedCancelby(TASK_ROOT(task), taskTIMER, CRITERIA_DATA, c, NULL);
902: schedTimer(TASK_ROOT(task), cbProto[srv->srv_proto][CB_CLOSECLIENT],
903: c, ts, c, 0);
904: }
905:
1.22 misho 906: noreply = RPC_CHK_NOREPLY(rpc);
1.18 misho 907:
1.27 misho 908: /* execute RPC call */
909: schedEvent(TASK_ROOT(task), execCall, c, (int) noreply, rpc, len);
1.13 misho 910:
1.22 misho 911: /* send RPC reply */
1.30.2.3 misho 912: if (!noreply && (rpc->call_io & RPC_REQ))
1.24 misho 913: schedEvent(TASK_ROOT(task), cbProto[srv->srv_proto][CB_TXPACKET],
1.22 misho 914: c, TASK_FD(task), rpc, len);
1.13 misho 915: end:
1.24 misho 916: AIT_FREE_VAL(&b);
1.13 misho 917: schedReadSelf(task);
1.29 misho 918: #else
919: rpc_SetErr(ENOTSUP, "Feature isn't supported on Linux!");
920: #endif
921:
1.27 misho 922: taskExit(task, NULL);
1.13 misho 923: }
924:
1.25 misho 925:
926: static void *
927: txEXTPacket(sched_task_t *task)
928: {
929: rpc_cli_t *c = TASK_ARG(task);
930: rpc_srv_t *s = c->cli_parent;
931: rpc_func_t *f = NULL;
932: u_char *buf = AIT_GET_BUF(&c->cli_buf);
933: struct tagRPCCall *rpc = (struct tagRPCCall*) buf;
1.27 misho 934: int ret, wlen = sizeof(struct tagRPCCall);
1.25 misho 935: struct timespec ts = { DEF_RPC_TIMEOUT, 0 };
936:
937: schedCancelby(TASK_ROOT(task), taskTIMER, CRITERIA_DATA, TASK_ARG(task), NULL);
938: schedTimer(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
939: TASK_ARG(task), ts, TASK_ARG(task), 0);
940:
941: if (rpc->call_argc) {
942: f = rpc_srv_getCall(s, ntohs(rpc->call_tag));
943: if (!f) {
944: rpc_SetErr(EPROGUNAVAIL, "Function not found at RPC server");
1.27 misho 945:
946: rpc->call_argc ^= rpc->call_argc;
947: RPC_SET_RETURN(rpc, -1);
948: RPC_SET_ERRNO(rpc, rpc_Errno);
949: } else if (rpc_pktFreeSpace(c) > s->srv_netbuf) {
950: rpc_SetErr(EMSGSIZE, "Message too long");
951:
1.25 misho 952: rpc->call_argc ^= rpc->call_argc;
1.27 misho 953: RPC_SET_RETURN(rpc, -1);
954: RPC_SET_ERRNO(rpc, rpc_Errno);
1.25 misho 955: } else {
956: rpc->call_argc = (u_char) array_Size(RPC_RETVARS(c));
957: /* Go Encapsulate variables */
958: ret = ait_vars2buffer(buf + wlen, AIT_LEN(&c->cli_buf) - wlen,
959: RPC_RETVARS(c));
960: if (ret == -1) {
961: rpc_SetErr(EBADRPC, "Prepare RPC packet failed");
1.27 misho 962:
1.25 misho 963: rpc->call_argc ^= rpc->call_argc;
1.27 misho 964: RPC_SET_RETURN(rpc, -1);
965: RPC_SET_ERRNO(rpc, rpc_Errno);
1.25 misho 966: } else
967: wlen += ret;
968: }
969: }
970:
1.27 misho 971: /* Free return values */
972: ait_freeVars(&RPC_RETVARS(c));
973:
1.25 misho 974: rpc->call_len = htonl(wlen);
975: rpc->call_io = RPC_ACK;
976:
977: /* send reply */
1.27 misho 978: ret = write(TASK_FD(task), buf, wlen);
1.25 misho 979: if (ret == -1) {
980: /* close connection */
981: schedEvent(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
982: TASK_ARG(task), 0, NULL, 0);
983: }
984:
1.27 misho 985: taskExit(task, NULL);
1.25 misho 986: }
987:
988: static void *
989: rxEXTPacket(sched_task_t *task)
990: {
991: rpc_srv_t *srv = TASK_ARG(task);
992: rpc_cli_t *c = NULL;
1.27 misho 993: int len, noreply = 0, rlen = AIT_LEN(&c->cli_buf);
1.25 misho 994: struct tagRPCCall *rpc;
995: struct timespec ts = { DEF_RPC_TIMEOUT, 0 };
1.27 misho 996: sockaddr_t sa;
1.25 misho 997: ait_val_t b = AIT_VAL_INIT;
998:
999: memset(&sa, 0, sizeof sa);
1000: /* receive connect packet */
1001: AIT_SET_BUF(&b, NULL, srv->srv_netbuf);
1002: rlen = read(TASK_FD(task), AIT_GET_BUF(&b), AIT_LEN(&b));
1.27 misho 1003: rpc = (struct tagRPCCall*) AIT_GET_BUF(&b);
1004: if (rlen < sizeof(struct tagRPCCall))
1005: goto end;
1006: else
1007: len = ntohl(rpc->call_len);
1008: if (rlen < len || len > srv->srv_netbuf)
1009: goto end;
1010:
1011: /* skip loop packet */
1012: if (rpc->call_io & RPC_ACK)
1.25 misho 1013: goto end;
1014:
1.27 misho 1015: /* check RPC packet session info */
1016: if (rpc_chkPktSession(&rpc->call_session, &srv->srv_session))
1017: goto end;
1.25 misho 1018:
1019: c = _allocClient(srv, &sa);
1020: if (!c) {
1021: EVERBOSE(1, "RPC client quota exceeded! Connection will be shutdown!\n");
1022: usleep(2000); /* blocked client delay */
1023: goto end;
1024: } else {
1.27 misho 1025: memcpy(AIT_GET_BUF(&c->cli_buf), AIT_GET_BUF(&b), len);
1.25 misho 1026: rpc = (struct tagRPCCall*) AIT_GET_BUF(&c->cli_buf);
1027:
1028: c->cli_sock = TASK_FD(task);
1029:
1030: /* armed timer for close stateless connection */
1031: schedCancelby(TASK_ROOT(task), taskTIMER, CRITERIA_DATA, c, NULL);
1032: schedTimer(TASK_ROOT(task), cbProto[srv->srv_proto][CB_CLOSECLIENT],
1033: c, ts, c, 0);
1034: }
1035:
1036: noreply = RPC_CHK_NOREPLY(rpc);
1037:
1.27 misho 1038: /* execute RPC call */
1039: schedEvent(TASK_ROOT(task), execCall, c, (int) noreply, rpc, len);
1.25 misho 1040:
1041: /* send RPC reply */
1.30.2.3 misho 1042: if (!noreply && (rpc->call_io & RPC_REQ))
1.25 misho 1043: schedWrite(TASK_ROOT(task), cbProto[srv->srv_proto][CB_TXPACKET],
1044: c, TASK_FD(task), rpc, len);
1045: end:
1046: AIT_FREE_VAL(&b);
1047: schedReadSelf(task);
1.27 misho 1048: taskExit(task, NULL);
1.25 misho 1049: }
1050:
1.13 misho 1051: /* ------------------------------------------------------ */
1052:
1.16 misho 1053: void
1.13 misho 1054: rpc_freeBLOBCli(rpc_cli_t * __restrict c)
1055: {
1056: rpc_srv_t *s = c->cli_parent;
1057:
1058: schedCancelby(s->srv_blob.root, taskMAX, CRITERIA_ARG, c, NULL);
1.10 misho 1059:
1060: /* free buffer */
1061: AIT_FREE_VAL(&c->cli_buf);
1062:
1.14 misho 1063: array_Del(s->srv_blob.clients, c->cli_id, 0);
1.10 misho 1064: if (c)
1.14 misho 1065: e_free(c);
1.13 misho 1066: }
1067:
1068:
1069: static void *
1070: closeBLOBClient(sched_task_t *task)
1071: {
1072: int sock = ((rpc_cli_t*) TASK_ARG(task))->cli_sock;
1073:
1074: rpc_freeBLOBCli(TASK_ARG(task));
1075:
1076: /* close client socket */
1077: shutdown(sock, SHUT_RDWR);
1078: close(sock);
1.27 misho 1079: taskExit(task, NULL);
1.7 misho 1080: }
1081:
1082: static void *
1083: txBLOB(sched_task_t *task)
1084: {
1.10 misho 1085: rpc_cli_t *c = TASK_ARG(task);
1086: u_char *buf = AIT_GET_BUF(&c->cli_buf);
1.7 misho 1087: int wlen = sizeof(struct tagBLOBHdr);
1088:
1089: /* send reply */
1.10 misho 1090: wlen = send(TASK_FD(task), buf, wlen, MSG_NOSIGNAL);
1091: if (wlen == -1 || wlen != sizeof(struct tagBLOBHdr)) {
1092: /* close blob connection */
1093: schedEvent(TASK_ROOT(task), closeBLOBClient, c, 42, NULL, 0);
1094: }
1.7 misho 1095:
1.27 misho 1096: taskExit(task, NULL);
1.7 misho 1097: }
1.4 misho 1098:
1.7 misho 1099: static void *
1100: rxBLOB(sched_task_t *task)
1101: {
1102: rpc_cli_t *c = TASK_ARG(task);
1103: rpc_srv_t *s = c->cli_parent;
1104: rpc_blob_t *b;
1.10 misho 1105: struct tagBLOBHdr blob;
1.7 misho 1106: int rlen;
1107:
1.10 misho 1108: memset(&blob, 0, sizeof blob);
1109: rlen = recv(TASK_FD(task), &blob, sizeof blob, 0);
1110: if (rlen < 1) {
1111: /* close blob connection */
1112: schedEvent(TASK_ROOT(task), closeBLOBClient, c, 42, NULL, 0);
1.27 misho 1113: taskExit(task, NULL);
1.7 misho 1114: }
1.5 misho 1115:
1.10 misho 1116: /* check BLOB packet */
1117: if (rlen < sizeof(struct tagBLOBHdr)) {
1118: rpc_SetErr(ERPCMISMATCH, "Short BLOB packet");
1.6 misho 1119:
1.10 misho 1120: schedReadSelf(task);
1.27 misho 1121: taskExit(task, NULL);
1.7 misho 1122: }
1.1 misho 1123:
1.7 misho 1124: /* check RPC packet session info */
1.15 misho 1125: if (rpc_chkPktSession(&blob.hdr_session, &s->srv_session)) {
1.7 misho 1126: rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session");
1.10 misho 1127: blob.hdr_cmd = error;
1.7 misho 1128: goto end;
1129: }
1130:
1131: /* Go to proceed packet ... */
1.10 misho 1132: switch (blob.hdr_cmd) {
1.7 misho 1133: case get:
1.10 misho 1134: if (!(b = rpc_srv_getBLOB(s, ntohl(blob.hdr_var)))) {
1135: rpc_SetErr(EINVAL, "Var=%x not found", ntohl(blob.hdr_var));
1136: blob.hdr_cmd = no;
1.27 misho 1137: RPC_SET_BLOB_RET(&blob, -1);
1.7 misho 1138: break;
1139: } else
1.10 misho 1140: blob.hdr_len = htonl(b->blob_len);
1.5 misho 1141:
1.7 misho 1142: if (rpc_srv_blobMap(s, b) != -1) {
1143: /* deliver BLOB variable to client */
1.10 misho 1144: blob.hdr_ret = htonl(rpc_srv_sendBLOB(c, b));
1.7 misho 1145: rpc_srv_blobUnmap(b);
1146: } else {
1.10 misho 1147: blob.hdr_cmd = error;
1.27 misho 1148: RPC_SET_BLOB_RET(&blob, -1);
1.7 misho 1149: }
1150: break;
1151: case set:
1.17 misho 1152: if ((b = rpc_srv_registerBLOB(s, ntohl(blob.hdr_len),
1153: ntohl(blob.hdr_ret)))) {
1.7 misho 1154: /* set new BLOB variable for reply :) */
1.10 misho 1155: blob.hdr_var = htonl(b->blob_var);
1.7 misho 1156:
1157: /* receive BLOB from client */
1.10 misho 1158: blob.hdr_ret = htonl(rpc_srv_recvBLOB(c, b));
1.7 misho 1159: rpc_srv_blobUnmap(b);
1.5 misho 1160: } else {
1.10 misho 1161: blob.hdr_cmd = error;
1.27 misho 1162: RPC_SET_BLOB_RET(&blob, -1);
1.7 misho 1163: }
1164: break;
1165: case unset:
1.11 misho 1166: if (rpc_srv_unregisterBLOB(s, ntohl(blob.hdr_var)) == -1) {
1.10 misho 1167: blob.hdr_cmd = error;
1.27 misho 1168: RPC_SET_BLOB_RET(&blob, -1);
1.1 misho 1169: }
1170: break;
1.7 misho 1171: default:
1.10 misho 1172: rpc_SetErr(EPROCUNAVAIL, "Unsupported BLOB command %d", blob.hdr_cmd);
1173: blob.hdr_cmd = error;
1.27 misho 1174: RPC_SET_BLOB_RET(&blob, -1);
1.7 misho 1175: }
1.1 misho 1176:
1.7 misho 1177: end:
1.10 misho 1178: memcpy(AIT_ADDR(&c->cli_buf), &blob, sizeof blob);
1179: schedWrite(TASK_ROOT(task), txBLOB, TASK_ARG(task), TASK_FD(task), NULL, 0);
1180: schedReadSelf(task);
1.27 misho 1181: taskExit(task, NULL);
1.2 misho 1182: }
1183:
1184: static void *
1.17 misho 1185: flushBLOB(sched_task_t *task)
1186: {
1.28 misho 1187: #ifdef atomic_load_acq_ptr
1.23 misho 1188: uintptr_t sigArg = atomic_load_acq_ptr(&_glSigArg);
1.28 misho 1189: #else
1190: uintptr_t sigArg = *((volatile uintptr_t*) &_glSigArg);
1191: #endif
1.23 misho 1192: rpc_srv_t *srv = sigArg ? (void*) sigArg : TASK_ARG(task);
1.17 misho 1193: rpc_blob_t *b, *tmp;
1194:
1195: TAILQ_FOREACH_SAFE(b, &srv->srv_blob.blobs, blob_node, tmp) {
1196: TAILQ_REMOVE(&srv->srv_blob.blobs, b, blob_node);
1197:
1198: rpc_srv_blobFree(srv, b);
1199: e_free(b);
1200: }
1201:
1.29 misho 1202: if (sigArg) {
1.23 misho 1203: /* disabled kqueue support in libaitsched */
1204: struct sigaction sa;
1205:
1206: memset(&sa, 0, sizeof sa);
1207: sigemptyset(&sa.sa_mask);
1208: sa.sa_handler = (void (*)(int)) flushBLOB;
1209: sa.sa_flags = SA_RESTART | SA_RESETHAND;
1210: sigaction(SIGFBLOB, &sa, NULL);
1.29 misho 1211: return NULL;
1212: } else {
1213: schedSignalSelf(task);
1214: taskExit(task, NULL);
1.23 misho 1215: }
1.17 misho 1216: }
1217:
1218: static void *
1.10 misho 1219: acceptBLOBClients(sched_task_t *task)
1.2 misho 1220: {
1.10 misho 1221: rpc_srv_t *srv = TASK_ARG(task);
1222: rpc_cli_t *c = NULL;
1223: register int i;
1.29 misho 1224: socklen_t salen = E_SOCKADDR_MAX;
1.21 misho 1225: int sock;
1.12 misho 1226: #ifdef TCP_NOPUSH
1227: int n = 1;
1228: #endif
1.7 misho 1229:
1.10 misho 1230: /* check free slots for connect */
1.14 misho 1231: for (i = 0; i < array_Size(srv->srv_blob.clients) &&
1232: (c = array(srv->srv_blob.clients, i, rpc_cli_t*)); i++);
1.21 misho 1233: if (c) { /* no more free slots! */
1234: EVERBOSE(1, "BLOB client quota exceeded! Connection will be shutdown!\n");
1235: if ((sock = accept(TASK_FD(task), NULL, NULL)) != -1) {
1236: shutdown(sock, SHUT_RDWR);
1237: close(sock);
1238: }
1.10 misho 1239: goto end;
1.21 misho 1240: }
1241:
1.14 misho 1242: c = e_malloc(sizeof(rpc_cli_t));
1.10 misho 1243: if (!c) {
1.7 misho 1244: LOGERR;
1.10 misho 1245: srv->srv_kill = srv->srv_blob.kill = 1;
1.27 misho 1246: taskExit(task, NULL);
1.7 misho 1247: } else {
1.10 misho 1248: memset(c, 0, sizeof(rpc_cli_t));
1.14 misho 1249: array_Set(srv->srv_blob.clients, i, c);
1.10 misho 1250: c->cli_id = i;
1251: c->cli_parent = srv;
1.7 misho 1252: }
1.4 misho 1253:
1.10 misho 1254: /* alloc empty buffer */
1.14 misho 1255: AIT_SET_BUFSIZ(&c->cli_buf, 0, srv->srv_netbuf);
1.2 misho 1256:
1.10 misho 1257: /* accept client */
1258: c->cli_sock = accept(TASK_FD(task), &c->cli_sa.sa, &salen);
1259: if (c->cli_sock == -1) {
1260: LOGERR;
1261: AIT_FREE_VAL(&c->cli_buf);
1.14 misho 1262: array_Del(srv->srv_blob.clients, i, 42);
1.10 misho 1263: goto end;
1.12 misho 1264: } else {
1265: #ifdef TCP_NOPUSH
1266: setsockopt(c->cli_sock, IPPROTO_TCP, TCP_NOPUSH, &n, sizeof n);
1267: #endif
1.10 misho 1268: fcntl(c->cli_sock, F_SETFL, fcntl(c->cli_sock, F_GETFL) | O_NONBLOCK);
1.30 misho 1269: fcntl(c->cli_sock, F_SETFD, FD_CLOEXEC);
1.12 misho 1270: }
1.2 misho 1271:
1.10 misho 1272: schedRead(TASK_ROOT(task), rxBLOB, c, c->cli_sock, NULL, 0);
1273: end:
1274: schedReadSelf(task);
1.27 misho 1275: taskExit(task, NULL);
1.1 misho 1276: }
1277:
1.10 misho 1278: /* ------------------------------------------------------ */
1.1 misho 1279:
1280: /*
1.7 misho 1281: * rpc_srv_initBLOBServer() - Init & create BLOB Server
1282: *
1.4 misho 1283: * @srv = RPC server instance
1.2 misho 1284: * @Port = Port for bind server, if Port == 0 default port is selected
1285: * @diskDir = Disk place for BLOB file objects
1286: * return: -1 == error or 0 bind and created BLOB server instance
1287: */
1288: int
1289: rpc_srv_initBLOBServer(rpc_srv_t * __restrict srv, u_short Port, const char *diskDir)
1290: {
1291: int n = 1;
1.29 misho 1292: socklen_t salen;
1.2 misho 1293:
1.10 misho 1294: if (!srv || srv->srv_kill) {
1.7 misho 1295: rpc_SetErr(EINVAL, "Invalid parameters can`t init BLOB server");
1.2 misho 1296: return -1;
1297: }
1298:
1299: memset(&srv->srv_blob, 0, sizeof srv->srv_blob);
1300: if (access(diskDir, R_OK | W_OK) == -1) {
1301: LOGERR;
1302: return -1;
1303: } else
1.7 misho 1304: AIT_SET_STR(&srv->srv_blob.dir, diskDir);
1.2 misho 1305:
1.10 misho 1306: /* init blob list */
1307: TAILQ_INIT(&srv->srv_blob.blobs);
1308:
1.2 misho 1309: srv->srv_blob.server.cli_parent = srv;
1.4 misho 1310:
1.29 misho 1311: memcpy(&srv->srv_blob.server.cli_sa, &srv->srv_server.cli_sa, sizeof srv->srv_blob.server.cli_sa);
1.10 misho 1312: switch (srv->srv_blob.server.cli_sa.sa.sa_family) {
1.4 misho 1313: case AF_INET:
1.10 misho 1314: srv->srv_blob.server.cli_sa.sin.sin_port =
1315: htons(Port ? Port : ntohs(srv->srv_blob.server.cli_sa.sin.sin_port) + 1);
1.29 misho 1316: salen = sizeof srv->srv_blob.server.cli_sa.sin;
1.4 misho 1317: break;
1318: case AF_INET6:
1.10 misho 1319: srv->srv_blob.server.cli_sa.sin6.sin6_port =
1320: htons(Port ? Port : ntohs(srv->srv_blob.server.cli_sa.sin6.sin6_port) + 1);
1.29 misho 1321: salen = sizeof srv->srv_blob.server.cli_sa.sin6;
1.4 misho 1322: break;
1323: case AF_LOCAL:
1.10 misho 1324: strlcat(srv->srv_blob.server.cli_sa.sun.sun_path, ".blob",
1325: sizeof srv->srv_blob.server.cli_sa.sun.sun_path);
1.29 misho 1326: salen = sizeof srv->srv_blob.server.cli_sa.sun;
1.4 misho 1327: break;
1328: default:
1.7 misho 1329: AIT_FREE_VAL(&srv->srv_blob.dir);
1.4 misho 1330: return -1;
1.2 misho 1331: }
1332:
1.4 misho 1333: /* create BLOB server socket */
1.6 misho 1334: srv->srv_blob.server.cli_sock = socket(srv->srv_server.cli_sa.sa.sa_family, SOCK_STREAM, 0);
1.2 misho 1335: if (srv->srv_blob.server.cli_sock == -1) {
1336: LOGERR;
1.7 misho 1337: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 1338: return -1;
1339: }
1340: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
1341: LOGERR;
1342: close(srv->srv_blob.server.cli_sock);
1.7 misho 1343: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 1344: return -1;
1345: }
1.5 misho 1346: n = srv->srv_netbuf;
1347: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof n) == -1) {
1348: LOGERR;
1349: close(srv->srv_blob.server.cli_sock);
1.7 misho 1350: AIT_FREE_VAL(&srv->srv_blob.dir);
1.5 misho 1351: return -1;
1352: }
1353: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_RCVBUF, &n, sizeof n) == -1) {
1354: LOGERR;
1355: close(srv->srv_blob.server.cli_sock);
1.7 misho 1356: AIT_FREE_VAL(&srv->srv_blob.dir);
1.5 misho 1357: return -1;
1358: }
1.29 misho 1359: if (bind(srv->srv_blob.server.cli_sock, &srv->srv_blob.server.cli_sa.sa, salen) == -1) {
1.2 misho 1360: LOGERR;
1361: close(srv->srv_blob.server.cli_sock);
1.7 misho 1362: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 1363: return -1;
1.13 misho 1364: } else
1365: fcntl(srv->srv_blob.server.cli_sock, F_SETFL,
1366: fcntl(srv->srv_blob.server.cli_sock, F_GETFL) | O_NONBLOCK);
1367:
1.2 misho 1368:
1.10 misho 1369: /* allocate pool for concurent blob clients */
1.14 misho 1370: srv->srv_blob.clients = array_Init(array_Size(srv->srv_clients));
1.2 misho 1371: if (!srv->srv_blob.clients) {
1.14 misho 1372: rpc_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
1.2 misho 1373: close(srv->srv_blob.server.cli_sock);
1.7 misho 1374: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 1375: return -1;
1.10 misho 1376: }
1.2 misho 1377:
1.10 misho 1378: /* init blob scheduler */
1379: srv->srv_blob.root = schedBegin();
1380: if (!srv->srv_blob.root) {
1381: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
1.14 misho 1382: array_Destroy(&srv->srv_blob.clients);
1.10 misho 1383: close(srv->srv_blob.server.cli_sock);
1384: AIT_FREE_VAL(&srv->srv_blob.dir);
1385: return -1;
1.30.2.3 misho 1386: } else
1387: schedSignalDispatch(srv->srv_blob.root, 42);
1.2 misho 1388:
1389: return 0;
1390: }
1391:
1392: /*
1.7 misho 1393: * rpc_srv_endBLOBServer() - Destroy BLOB server, close all opened sockets and free resources
1394: *
1.2 misho 1395: * @srv = RPC Server instance
1396: * return: none
1397: */
1.16 misho 1398: void
1.2 misho 1399: rpc_srv_endBLOBServer(rpc_srv_t * __restrict srv)
1400: {
1.10 misho 1401: if (!srv)
1.2 misho 1402: return;
1403:
1.10 misho 1404: srv->srv_blob.kill = 1;
1.17 misho 1405:
1.27 misho 1406: if (srv->srv_blob.server.cli_sa.sa.sa_family == AF_LOCAL)
1407: unlink(srv->srv_blob.server.cli_sa.sun.sun_path);
1.28 misho 1408:
1409: schedEnd(&srv->srv_blob.root);
1.2 misho 1410: }
1411:
1412: /*
1.10 misho 1413: * rpc_srv_loopBLOBServer() - Execute Main BLOB server loop and wait for clients requests
1.7 misho 1414: *
1.2 misho 1415: * @srv = RPC Server instance
1416: * return: -1 error or 0 ok, infinite loop ...
1417: */
1418: int
1.10 misho 1419: rpc_srv_loopBLOBServer(rpc_srv_t * __restrict srv)
1.2 misho 1420: {
1.10 misho 1421: rpc_cli_t *c;
1.2 misho 1422: register int i;
1.10 misho 1423: rpc_blob_t *b, *tmp;
1424: struct timespec ts = { RPC_SCHED_POLLING, 0 };
1.2 misho 1425:
1.10 misho 1426: if (!srv || srv->srv_kill) {
1.7 misho 1427: rpc_SetErr(EINVAL, "Invalid parameter can`t start BLOB server");
1.2 misho 1428: return -1;
1429: }
1430:
1.14 misho 1431: if (listen(srv->srv_blob.server.cli_sock, array_Size(srv->srv_blob.clients)) == -1) {
1.2 misho 1432: LOGERR;
1433: return -1;
1.10 misho 1434: }
1435:
1.23 misho 1436: if (!schedSignal(srv->srv_blob.root, flushBLOB, srv, SIGFBLOB, NULL, 0)) {
1437: /* disabled kqueue support in libaitsched */
1438: struct sigaction sa;
1439:
1.28 misho 1440: #ifdef atomic_store_rel_ptr
1.23 misho 1441: atomic_store_rel_ptr(&_glSigArg, (uintptr_t) srv);
1.28 misho 1442: #else
1443: *((volatile uintptr_t*) &_glSigArg) = (uintptr_t) srv;
1444: #endif
1.23 misho 1445:
1446: memset(&sa, 0, sizeof sa);
1447: sigemptyset(&sa.sa_mask);
1448: sa.sa_handler = (void (*)(int)) flushBLOB;
1449: sa.sa_flags = SA_RESTART | SA_RESETHAND;
1450: sigaction(SIGFBLOB, &sa, NULL);
1451: }
1452:
1.10 misho 1453: if (!schedRead(srv->srv_blob.root, acceptBLOBClients, srv,
1454: srv->srv_blob.server.cli_sock, NULL, 0)) {
1455: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
1456: return -1;
1457: }
1.2 misho 1458:
1.10 misho 1459: schedPolling(srv->srv_blob.root, &ts, NULL);
1460: /* main rpc loop */
1461: schedRun(srv->srv_blob.root, &srv->srv_blob.kill);
1.30.2.3 misho 1462: schedSignalDispatch(srv->srv_blob.root, 0);
1.7 misho 1463:
1.17 misho 1464: /* detach blobs */
1465: TAILQ_FOREACH_SAFE(b, &srv->srv_blob.blobs, blob_node, tmp) {
1466: TAILQ_REMOVE(&srv->srv_blob.blobs, b, blob_node);
1467:
1468: rpc_srv_blobFree(srv, b);
1469: e_free(b);
1470: }
1471:
1.10 misho 1472: /* close all clients connections & server socket */
1.14 misho 1473: for (i = 0; i < array_Size(srv->srv_blob.clients); i++) {
1474: c = array(srv->srv_blob.clients, i, rpc_cli_t*);
1.10 misho 1475: if (c) {
1476: shutdown(c->cli_sock, SHUT_RDWR);
1477: close(c->cli_sock);
1478:
1479: schedCancelby(srv->srv_blob.root, taskMAX, CRITERIA_ARG, c, NULL);
1480: AIT_FREE_VAL(&c->cli_buf);
1.2 misho 1481: }
1.14 misho 1482: array_Del(srv->srv_blob.clients, i, 42);
1.10 misho 1483: }
1.14 misho 1484: array_Destroy(&srv->srv_blob.clients);
1.2 misho 1485:
1.10 misho 1486: close(srv->srv_blob.server.cli_sock);
1.2 misho 1487:
1.10 misho 1488: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 1489: return 0;
1490: }
1491:
1492:
1493: /*
1.7 misho 1494: * rpc_srv_initServer() - Init & create RPC Server
1495: *
1.15 misho 1496: * @InstID = Instance for authentication & recognition
1.1 misho 1497: * @concurentClients = Concurent clients at same time to this server
1.10 misho 1498: * @netBuf = Network buffer length (min:512 bytes), if =0 == BUFSIZ (also meaning max RPC packet)
1.4 misho 1499: * @csHost = Host name or address for bind server, if NULL any address
1.1 misho 1500: * @Port = Port for bind server, if Port == 0 default port is selected
1.13 misho 1501: * @proto = Protocol, if == 0 choose SOCK_STREAM
1.1 misho 1502: * return: NULL == error or !=NULL bind and created RPC server instance
1503: */
1504: rpc_srv_t *
1.15 misho 1505: rpc_srv_initServer(u_char InstID, int concurentClients, int netBuf,
1506: const char *csHost, u_short Port, int proto)
1.1 misho 1507: {
1.10 misho 1508: int n = 1;
1.1 misho 1509: rpc_srv_t *srv = NULL;
1.14 misho 1510: sockaddr_t sa = E_SOCKADDR_INIT;
1.29 misho 1511: socklen_t salen;
1.1 misho 1512:
1.25 misho 1513: if (!concurentClients || (proto < 0 || proto > SOCK_RAW)) {
1.10 misho 1514: rpc_SetErr(EINVAL, "Invalid parameters can`t init RPC server");
1.1 misho 1515: return NULL;
1516: }
1.27 misho 1517: if (!Port && proto < SOCK_RAW)
1518: Port = RPC_DEFPORT;
1.29 misho 1519: if (!(salen = e_gethostbyname(csHost, Port, &sa)))
1.10 misho 1520: return NULL;
1.13 misho 1521: if (!proto)
1522: proto = SOCK_STREAM;
1.10 misho 1523: if (netBuf < RPC_MIN_BUFSIZ)
1.5 misho 1524: netBuf = BUFSIZ;
1.7 misho 1525: else
1.14 misho 1526: netBuf = E_ALIGN(netBuf, 2); /* align netBuf length */
1.10 misho 1527:
1528: #ifdef HAVE_SRANDOMDEV
1529: srandomdev();
1530: #else
1531: time_t tim;
1532:
1533: srandom((time(&tim) ^ getpid()));
1534: #endif
1.1 misho 1535:
1.14 misho 1536: srv = e_malloc(sizeof(rpc_srv_t));
1.1 misho 1537: if (!srv) {
1538: LOGERR;
1539: return NULL;
1540: } else
1541: memset(srv, 0, sizeof(rpc_srv_t));
1542:
1.13 misho 1543: srv->srv_proto = proto;
1.5 misho 1544: srv->srv_netbuf = netBuf;
1.1 misho 1545: srv->srv_session.sess_version = RPC_VERSION;
1.15 misho 1546: srv->srv_session.sess_instance = InstID;
1.1 misho 1547:
1548: srv->srv_server.cli_parent = srv;
1.10 misho 1549: memcpy(&srv->srv_server.cli_sa, &sa, sizeof srv->srv_server.cli_sa);
1550:
1.12 misho 1551: /* init functions */
1552: pthread_mutex_init(&srv->srv_funcs.mtx, NULL);
1553: SLIST_INIT(&srv->srv_funcs);
1554: AVL_INIT(&srv->srv_funcs);
1.10 misho 1555:
1556: /* init scheduler */
1557: srv->srv_root = schedBegin();
1558: if (!srv->srv_root) {
1559: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
1.12 misho 1560: pthread_mutex_destroy(&srv->srv_funcs.mtx);
1.14 misho 1561: e_free(srv);
1.10 misho 1562: return NULL;
1.30.2.3 misho 1563: } else
1564: schedSignalDispatch(srv->srv_root, 42);
1.10 misho 1565:
1566: /* init pool for clients */
1.14 misho 1567: srv->srv_clients = array_Init(concurentClients);
1.10 misho 1568: if (!srv->srv_clients) {
1.14 misho 1569: rpc_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
1.10 misho 1570: schedEnd(&srv->srv_root);
1.12 misho 1571: pthread_mutex_destroy(&srv->srv_funcs.mtx);
1.14 misho 1572: e_free(srv);
1.10 misho 1573: return NULL;
1574: }
1.4 misho 1575:
1576: /* create server socket */
1.26 misho 1577: srv->srv_server.cli_sock = socket(srv->srv_server.cli_sa.sa.sa_family,
1578: srv->srv_proto, srv->srv_proto == SOCK_RAW ? IPPROTO_ERPC : 0);
1.1 misho 1579: if (srv->srv_server.cli_sock == -1) {
1580: LOGERR;
1.14 misho 1581: array_Destroy(&srv->srv_clients);
1.10 misho 1582: schedEnd(&srv->srv_root);
1.12 misho 1583: pthread_mutex_destroy(&srv->srv_funcs.mtx);
1.14 misho 1584: e_free(srv);
1.1 misho 1585: return NULL;
1586: }
1587: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
1588: LOGERR;
1.10 misho 1589: goto err;
1.1 misho 1590: }
1.27 misho 1591: if (srv->srv_proto == SOCK_STREAM)
1592: setsockopt(srv->srv_server.cli_sock, IPPROTO_TCP, TCP_NODELAY, &n, sizeof n);
1.5 misho 1593: n = srv->srv_netbuf;
1594: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof n) == -1) {
1595: LOGERR;
1.10 misho 1596: goto err;
1.5 misho 1597: }
1598: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_RCVBUF, &n, sizeof n) == -1) {
1599: LOGERR;
1.10 misho 1600: goto err;
1.5 misho 1601: }
1.29 misho 1602: if (bind(srv->srv_server.cli_sock, &srv->srv_server.cli_sa.sa, salen) == -1) {
1.1 misho 1603: LOGERR;
1.10 misho 1604: goto err;
1.13 misho 1605: } else
1606: fcntl(srv->srv_server.cli_sock, F_SETFL,
1607: fcntl(srv->srv_server.cli_sock, F_GETFL) | O_NONBLOCK);
1.1 misho 1608:
1.10 misho 1609: rpc_register_srvPing(srv);
1.8 misho 1610:
1.1 misho 1611: return srv;
1.10 misho 1612: err: /* error condition */
1613: close(srv->srv_server.cli_sock);
1.14 misho 1614: array_Destroy(&srv->srv_clients);
1.10 misho 1615: schedEnd(&srv->srv_root);
1.12 misho 1616: pthread_mutex_destroy(&srv->srv_funcs.mtx);
1.14 misho 1617: e_free(srv);
1.10 misho 1618: return NULL;
1.1 misho 1619: }
1620:
1621: /*
1.7 misho 1622: * rpc_srv_endServer() - Destroy RPC server, close all opened sockets and free resources
1623: *
1.6 misho 1624: * @psrv = RPC Server instance
1.1 misho 1625: * return: none
1626: */
1.16 misho 1627: void
1.6 misho 1628: rpc_srv_endServer(rpc_srv_t ** __restrict psrv)
1.1 misho 1629: {
1.10 misho 1630: if (!psrv || !*psrv)
1.1 misho 1631: return;
1632:
1.10 misho 1633: /* if send kill to blob server */
1.17 misho 1634: rpc_srv_endBLOBServer(*psrv);
1.28 misho 1635: /* wait for BLOB server done */
1636: while (*(&(*psrv)->srv_blob.root))
1637: usleep(1000);
1.1 misho 1638:
1.10 misho 1639: (*psrv)->srv_kill = 1;
1640: sleep(RPC_SCHED_POLLING);
1.2 misho 1641:
1.27 misho 1642: if ((*psrv)->srv_server.cli_sa.sa.sa_family == AF_LOCAL)
1643: unlink((*psrv)->srv_server.cli_sa.sun.sun_path);
1644:
1.28 misho 1645: schedEnd(&(*psrv)->srv_root);
1646:
1.12 misho 1647: pthread_mutex_destroy(&(*psrv)->srv_funcs.mtx);
1.14 misho 1648: e_free(*psrv);
1.6 misho 1649: *psrv = NULL;
1.1 misho 1650: }
1651:
1652: /*
1.7 misho 1653: * rpc_srv_loopServer() - Execute Main server loop and wait for clients requests
1654: *
1.1 misho 1655: * @srv = RPC Server instance
1656: * return: -1 error or 0 ok, infinite loop ...
1657: */
1658: int
1.5 misho 1659: rpc_srv_loopServer(rpc_srv_t * __restrict srv)
1.1 misho 1660: {
1.10 misho 1661: rpc_cli_t *c;
1.1 misho 1662: register int i;
1.12 misho 1663: rpc_func_t *f;
1.10 misho 1664: struct timespec ts = { RPC_SCHED_POLLING, 0 };
1.1 misho 1665:
1666: if (!srv) {
1.10 misho 1667: rpc_SetErr(EINVAL, "Invalid parameter can`t start RPC server");
1.1 misho 1668: return -1;
1669: }
1670:
1.13 misho 1671: if (srv->srv_proto == SOCK_STREAM)
1.14 misho 1672: if (listen(srv->srv_server.cli_sock, array_Size(srv->srv_clients)) == -1) {
1.13 misho 1673: LOGERR;
1674: return -1;
1675: }
1.1 misho 1676:
1.13 misho 1677: if (!schedRead(srv->srv_root, cbProto[srv->srv_proto][CB_ACCEPTCLIENT], srv,
1678: srv->srv_server.cli_sock, NULL, 0)) {
1.10 misho 1679: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
1680: return -1;
1681: }
1.7 misho 1682:
1.10 misho 1683: schedPolling(srv->srv_root, &ts, NULL);
1.7 misho 1684: /* main rpc loop */
1.10 misho 1685: schedRun(srv->srv_root, &srv->srv_kill);
1.30.2.3 misho 1686: schedSignalDispatch(srv->srv_root, 0);
1.10 misho 1687:
1688: /* close all clients connections & server socket */
1.14 misho 1689: for (i = 0; i < array_Size(srv->srv_clients); i++) {
1690: c = array(srv->srv_clients, i, rpc_cli_t*);
1.10 misho 1691: if (c) {
1.24 misho 1692: if (srv->srv_proto == SOCK_STREAM) {
1693: shutdown(c->cli_sock, SHUT_RDWR);
1694: close(c->cli_sock);
1695: }
1.10 misho 1696:
1697: schedCancelby(srv->srv_root, taskMAX, CRITERIA_ARG, c, NULL);
1.14 misho 1698: ait_freeVars(&RPC_RETVARS(c));
1.10 misho 1699: AIT_FREE_VAL(&c->cli_buf);
1.1 misho 1700: }
1.14 misho 1701: array_Del(srv->srv_clients, i, 42);
1.10 misho 1702: }
1.14 misho 1703: array_Destroy(&srv->srv_clients);
1.2 misho 1704:
1.25 misho 1705: if (srv->srv_proto != SOCK_EXT)
1706: close(srv->srv_server.cli_sock);
1.2 misho 1707:
1.10 misho 1708: /* detach exported calls */
1.12 misho 1709: RPC_FUNCS_LOCK(&srv->srv_funcs);
1710: while ((f = SLIST_FIRST(&srv->srv_funcs))) {
1711: SLIST_REMOVE_HEAD(&srv->srv_funcs, func_next);
1.1 misho 1712:
1.10 misho 1713: AIT_FREE_VAL(&f->func_name);
1.14 misho 1714: e_free(f);
1.1 misho 1715: }
1.12 misho 1716: srv->srv_funcs.avlh_root = NULL;
1717: RPC_FUNCS_UNLOCK(&srv->srv_funcs);
1.1 misho 1718:
1719: return 0;
1720: }
1721:
1722:
1723: /*
1724: * rpc_srv_execCall() Execute registered call from RPC server
1.7 misho 1725: *
1.10 misho 1726: * @cli = RPC client
1.1 misho 1727: * @rpc = IN RPC call structure
1.10 misho 1728: * @funcname = Execute RPC function
1.5 misho 1729: * @args = IN RPC calling arguments from RPC client
1.1 misho 1730: * return: -1 error, !=-1 ok
1731: */
1732: int
1.10 misho 1733: rpc_srv_execCall(rpc_cli_t * __restrict cli, struct tagRPCCall * __restrict rpc,
1734: ait_val_t funcname, array_t * __restrict args)
1.1 misho 1735: {
1736: rpc_callback_t func;
1737:
1.10 misho 1738: if (!cli || !rpc || !AIT_ADDR(&funcname)) {
1.7 misho 1739: rpc_SetErr(EINVAL, "Invalid parameter can`t exec function");
1.1 misho 1740: return -1;
1741: }
1742:
1.10 misho 1743: func = AIT_GET_LIKE(&funcname, rpc_callback_t);
1744: return func(cli, rpc, args);
1.1 misho 1745: }
1.24 misho 1746:
1747:
1748: /*
1749: * rpc_srv_initServer2() - Init & create layer2 RPC Server
1750: *
1751: * @InstID = Instance for authentication & recognition
1752: * @concurentClients = Concurent clients at same time to this server
1753: * @netBuf = Network buffer length (min:512 bytes), if =0 == BUFSIZ (also meaning max RPC packet)
1754: * @csIface = Interface name for bind server, if NULL first interface on host
1755: * return: NULL == error or !=NULL bind and created RPC server instance
1756: */
1757: rpc_srv_t *
1758: rpc_srv_initServer2(u_char InstID, int concurentClients, int netBuf, const char *csIface)
1759: {
1.29 misho 1760: #ifndef __linux__
1.24 misho 1761: int n = 1;
1762: rpc_srv_t *srv = NULL;
1763: sockaddr_t sa = E_SOCKADDR_INIT;
1764: char szIface[64], szStr[STRSIZ];
1765: register int i;
1766: struct ifreq ifr;
1767: struct bpf_insn insns[] = {
1768: BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12),
1769: BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, RPC_DEFPORT, 0, 1),
1770: BPF_STMT(BPF_RET + BPF_K, -1),
1771: BPF_STMT(BPF_RET + BPF_K, 0),
1772: };
1773: struct bpf_program fcode = {
1774: .bf_len = sizeof(insns) / sizeof(struct bpf_insn),
1775: .bf_insns = insns
1776: };
1777:
1778: if (!concurentClients) {
1779: rpc_SetErr(EINVAL, "Invalid parameters can`t init RPC server");
1780: return NULL;
1781: }
1782: if (!csIface) {
1783: if (e_get1stiface(szIface, sizeof szIface))
1784: return NULL;
1785: } else
1786: strlcpy(szIface, csIface, sizeof szIface);
1787: if (!e_getifacebyname(szIface, &sa))
1788: return NULL;
1789:
1790: #ifdef HAVE_SRANDOMDEV
1791: srandomdev();
1792: #else
1793: time_t tim;
1794:
1795: srandom((time(&tim) ^ getpid()));
1796: #endif
1797:
1798: srv = e_malloc(sizeof(rpc_srv_t));
1799: if (!srv) {
1800: LOGERR;
1801: return NULL;
1802: } else
1803: memset(srv, 0, sizeof(rpc_srv_t));
1804:
1805: srv->srv_proto = SOCK_BPF;
1806: srv->srv_netbuf = netBuf;
1807: srv->srv_session.sess_version = RPC_VERSION;
1808: srv->srv_session.sess_instance = InstID;
1809:
1810: srv->srv_server.cli_parent = srv;
1811: memcpy(&srv->srv_server.cli_sa, &sa, sizeof srv->srv_server.cli_sa);
1812:
1813: /* init functions */
1814: pthread_mutex_init(&srv->srv_funcs.mtx, NULL);
1815: SLIST_INIT(&srv->srv_funcs);
1816: AVL_INIT(&srv->srv_funcs);
1817:
1818: /* init scheduler */
1819: srv->srv_root = schedBegin();
1820: if (!srv->srv_root) {
1821: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
1822: pthread_mutex_destroy(&srv->srv_funcs.mtx);
1823: e_free(srv);
1824: return NULL;
1.30.2.3 misho 1825: } else
1826: schedSignalDispatch(srv->srv_root, 42);
1.24 misho 1827:
1828: /* init pool for clients */
1829: srv->srv_clients = array_Init(concurentClients);
1830: if (!srv->srv_clients) {
1831: rpc_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
1832: schedEnd(&srv->srv_root);
1833: pthread_mutex_destroy(&srv->srv_funcs.mtx);
1834: e_free(srv);
1835: return NULL;
1836: }
1837:
1838: /* create server handler */
1839: for (i = 0; i < 10; i++) {
1840: memset(szStr, 0, sizeof szStr);
1841: snprintf(szStr, sizeof szStr, "/dev/bpf%d", i);
1842: srv->srv_server.cli_sock = open(szStr, O_RDWR);
1843: if (srv->srv_server.cli_sock > STDERR_FILENO)
1844: break;
1845: }
1846: if (srv->srv_server.cli_sock < 3) {
1847: LOGERR;
1848: array_Destroy(&srv->srv_clients);
1849: schedEnd(&srv->srv_root);
1850: pthread_mutex_destroy(&srv->srv_funcs.mtx);
1851: e_free(srv);
1852: return NULL;
1853: }
1854:
1855: if (ioctl(srv->srv_server.cli_sock, BIOCIMMEDIATE, &n) == -1) {
1856: LOGERR;
1857: goto err;
1858: }
1859: if (ioctl(srv->srv_server.cli_sock, BIOCSETF, &fcode) == -1) {
1860: LOGERR;
1861: goto err;
1862: }
1863: n = (netBuf < RPC_MIN_BUFSIZ) ? getpagesize() : E_ALIGN(netBuf, 2);
1864: if (ioctl(srv->srv_server.cli_sock, BIOCSBLEN, &n) == -1) {
1865: LOGERR;
1866: goto err;
1867: } else
1868: srv->srv_netbuf = n;
1869:
1870: memset(&ifr, 0, sizeof ifr);
1871: strlcpy(ifr.ifr_name, szIface, sizeof ifr.ifr_name);
1872: if (ioctl(srv->srv_server.cli_sock, BIOCSETIF, &ifr) == -1) {
1873: LOGERR;
1874: goto err;
1875: } else
1876: fcntl(srv->srv_server.cli_sock, F_SETFL,
1877: fcntl(srv->srv_server.cli_sock, F_GETFL) | O_NONBLOCK);
1878:
1879: rpc_register_srvPing(srv);
1880:
1881: return srv;
1882: err: /* error condition */
1883: close(srv->srv_server.cli_sock);
1884: array_Destroy(&srv->srv_clients);
1885: schedEnd(&srv->srv_root);
1886: pthread_mutex_destroy(&srv->srv_funcs.mtx);
1887: e_free(srv);
1.29 misho 1888: #else
1889: rpc_SetErr(ENOTSUP, "Feature isn't supported on Linux!");
1890: #endif
1891:
1.24 misho 1892: return NULL;
1893: }
1.25 misho 1894:
1895: /*
1896: * rpc_srv_initServerExt() - Init & create pipe RPC Server
1897: *
1898: * @InstID = Instance for authentication & recognition
1899: * @netBuf = Network buffer length (min:512 bytes), if =0 == BUFSIZ (also meaning max RPC packet)
1900: * @fd = File descriptor
1901: * return: NULL == error or !=NULL bind and created RPC server instance
1902: */
1903: rpc_srv_t *
1904: rpc_srv_initServerExt(u_char InstID, int netBuf, int fd)
1905: {
1906: rpc_srv_t *srv = NULL;
1907:
1908: #ifdef HAVE_SRANDOMDEV
1909: srandomdev();
1910: #else
1911: time_t tim;
1912:
1913: srandom((time(&tim) ^ getpid()));
1914: #endif
1915:
1916: srv = e_malloc(sizeof(rpc_srv_t));
1917: if (!srv) {
1918: LOGERR;
1919: return NULL;
1920: } else
1921: memset(srv, 0, sizeof(rpc_srv_t));
1922:
1923: srv->srv_proto = SOCK_EXT;
1924: srv->srv_netbuf = (netBuf < RPC_MIN_BUFSIZ) ?
1925: getpagesize() : E_ALIGN(netBuf, 2);
1926: srv->srv_session.sess_version = RPC_VERSION;
1927: srv->srv_session.sess_instance = InstID;
1928:
1929: srv->srv_server.cli_parent = srv;
1930: srv->srv_server.cli_sock = fd;
1931:
1932: /* init functions */
1933: pthread_mutex_init(&srv->srv_funcs.mtx, NULL);
1934: SLIST_INIT(&srv->srv_funcs);
1935: AVL_INIT(&srv->srv_funcs);
1936:
1937: /* init scheduler */
1938: srv->srv_root = schedBegin();
1939: if (!srv->srv_root) {
1940: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
1941: pthread_mutex_destroy(&srv->srv_funcs.mtx);
1942: e_free(srv);
1943: return NULL;
1.30.2.3 misho 1944: } else
1945: schedSignalDispatch(srv->srv_root, 42);
1.25 misho 1946:
1947: /* init pool for clients */
1948: srv->srv_clients = array_Init(1);
1949: if (!srv->srv_clients) {
1950: rpc_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
1951: schedEnd(&srv->srv_root);
1952: pthread_mutex_destroy(&srv->srv_funcs.mtx);
1953: e_free(srv);
1954: return NULL;
1955: }
1956:
1957: fcntl(srv->srv_server.cli_sock, F_SETFL,
1958: fcntl(srv->srv_server.cli_sock, F_GETFL) | O_NONBLOCK);
1959:
1960: rpc_register_srvPing(srv);
1961:
1962: return srv;
1963: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>