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