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