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