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