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