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