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