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