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