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