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