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