Annotation of libaitrpc/src/srv.c, revision 1.23.6.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.23.6.5! misho 6: * $Id: srv.c,v 1.23.6.4 2014/12/17 01:31:21 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);
1.23.6.5! misho 590: goto end;
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);
1.23.6.5! misho 598: goto end;
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");
1.23.6.5! misho 610: /* close connection */
! 611: schedEvent(TASK_ROOT(task), cbProto[srv->srv_proto][CB_CLOSECLIENT],
! 612: c, 0, NULL, 0);
! 613: goto end;
1.22 misho 614: }
1.13 misho 615:
1.22 misho 616: noreply = RPC_CHK_NOREPLY(rpc);
1.18 misho 617:
1.22 misho 618: /* check RPC packet session info */
619: if (rpc_chkPktSession(&rpc->call_session, &srv->srv_session)) {
620: rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session");
1.13 misho 621:
1.22 misho 622: rpc->call_argc ^= rpc->call_argc;
623: rpc->call_rep.ret = RPC_ERROR(-1);
624: rpc->call_rep.eno = RPC_ERROR(errno);
625: } else {
626: /* execute RPC call */
627: schedEvent(TASK_ROOT(task), execCall, c, (int) noreply, rpc, len);
628: }
1.13 misho 629:
1.22 misho 630: /* send RPC reply */
631: if (!noreply)
632: schedWrite(TASK_ROOT(task), cbProto[srv->srv_proto][CB_TXPACKET],
633: c, TASK_FD(task), rpc, len);
1.13 misho 634: end:
635: schedReadSelf(task);
636: return NULL;
637: }
638:
1.23.6.3 misho 639:
640: static void *
641: txBPFPacket(sched_task_t *task)
642: {
643: rpc_cli_t *c = TASK_ARG(task);
644: rpc_srv_t *s = c->cli_parent;
645: rpc_func_t *f = NULL;
646: u_char *buf = AIT_GET_BUF(&c->cli_buf);
647: struct tagRPCCall *rpc = (struct tagRPCCall*) buf;
648: int ret, estlen, wlen = sizeof(struct tagRPCCall);
649: struct timespec ts = { DEF_RPC_TIMEOUT, 0 };
650: struct pollfd pfd;
651:
652: schedCancelby(TASK_ROOT(task), taskTIMER, CRITERIA_DATA, TASK_ARG(task), NULL);
653: schedTimer(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
654: TASK_ARG(task), ts, TASK_ARG(task), 0);
655:
656: if (rpc->call_argc) {
657: f = rpc_srv_getCall(s, ntohs(rpc->call_tag));
658: if (!f) {
659: rpc_SetErr(EPROGUNAVAIL, "Function not found at RPC server");
660: rpc->call_argc ^= rpc->call_argc;
661: rpc->call_rep.ret = RPC_ERROR(-1);
662: rpc->call_rep.eno = RPC_ERROR(rpc_Errno);
663: } else {
664: /* calc estimated length */
665: estlen = ait_resideVars(RPC_RETVARS(c)) + wlen;
666: if (estlen > AIT_LEN(&c->cli_buf))
667: AIT_RE_BUF(&c->cli_buf, estlen);
668: buf = AIT_GET_BUF(&c->cli_buf);
669: rpc = (struct tagRPCCall*) buf;
670:
671: rpc->call_argc = htons(array_Size(RPC_RETVARS(c)));
672: /* Go Encapsulate variables */
673: ret = ait_vars2buffer(buf + wlen, AIT_LEN(&c->cli_buf) - wlen,
674: RPC_RETVARS(c));
675: /* Free return values */
676: ait_freeVars(&c->cli_vars);
677: if (ret == -1) {
678: rpc_SetErr(EBADRPC, "Prepare RPC packet failed");
679: rpc->call_argc ^= rpc->call_argc;
680: rpc->call_rep.ret = RPC_ERROR(-1);
681: rpc->call_rep.eno = RPC_ERROR(rpc_Errno);
682: } else
683: wlen += ret;
684: }
685: }
686:
687: rpc->call_len = htonl(wlen);
688:
689: /* calculate CRC */
690: rpc->call_crc ^= rpc->call_crc;
691: rpc->call_crc = htons(crcFletcher16((u_short*) buf, wlen / 2));
692:
693: /* send reply */
694: pfd.fd = TASK_FD(task);
695: pfd.events = POLLOUT;
696: for (; wlen > 0; wlen -= ret, buf += ret) {
697: if ((ret = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) < 1 ||
698: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
699: if (ret)
700: LOGERR;
701: else
702: rpc_SetErr(ETIMEDOUT, "Timeout reached! Client not respond");
703: /* close connection */
704: schedEvent(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
705: TASK_ARG(task), 0, NULL, 0);
706: return NULL;
707: }
708: ret = write(TASK_FD(task), buf, MIN(wlen, s->srv_netbuf));
709: if (ret == -1) {
710: /* close connection */
711: schedEvent(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
712: TASK_ARG(task), 0, NULL, 0);
713: return NULL;
714: }
715: }
716:
717: return NULL;
718: }
719:
720: static void *
721: rxBPFPacket(sched_task_t *task)
722: {
723: rpc_srv_t *srv = TASK_ARG(task);
724: rpc_cli_t *c = NULL;
1.23.6.5! misho 725: int len, rlen, noreply;
1.23.6.3 misho 726: u_short crc;
1.23.6.4 misho 727: struct tagRPCCall *rpc;
1.23.6.3 misho 728: sockaddr_t sa;
729: struct timespec ts = { DEF_RPC_TIMEOUT, 0 };
730: struct bpf_hdr *h;
731: struct ether_header *eh;
732: ait_val_t b = AIT_VAL_INIT;
733:
734: /* receive connect packet */
735: AIT_SET_BUF(&b, NULL, srv->srv_netbuf);
736: rlen = read(TASK_FD(task), AIT_GET_BUF(&b), AIT_LEN(&b));
1.23.6.5! misho 737: h = (struct bpf_hdr*) AIT_GET_BUF(&b);
! 738: rlen -= h->bh_hdrlen;
! 739: if (rlen < h->bh_caplen || h->bh_caplen != h->bh_datalen ||
! 740: rlen < ETHER_HDR_LEN + sizeof(struct tagRPCCall)) {
1.23.6.3 misho 741: rpc_SetErr(ERPCMISMATCH, "Short RPC packet");
742: goto end;
743: } else {
1.23.6.5! misho 744: rlen = h->bh_caplen;
1.23.6.3 misho 745: eh = (struct ether_header*) (AIT_GET_BUF(&b) + h->bh_hdrlen);
1.23.6.5! misho 746: rlen -= ETHER_HDR_LEN;
! 747: rpc = (struct tagRPCCall*) (eh + 1);
! 748: if (eh->ether_type != ntohs(RPC_DEFPORT))
! 749: goto end;
! 750: else
! 751: e_getlinkbymac((const ether_addr_t*) eh->ether_shost, &sa);
1.23.6.3 misho 752: }
753:
754: c = _allocClient(srv, &sa);
755: if (!c) {
756: EVERBOSE(1, "RPC client quota exceeded! Connection will be shutdown!\n");
757: usleep(2000); /* blocked client delay */
758: goto end;
759: } else {
1.23.6.5! misho 760: len = ntohl(rpc->call_len);
! 761: if (len > AIT_LEN(&c->cli_buf))
! 762: AIT_RE_BUF(&c->cli_buf, len);
! 763: memcpy(AIT_GET_BUF(&c->cli_buf), rpc, AIT_LEN(&c->cli_buf));
1.23.6.3 misho 764: rpc = (struct tagRPCCall*) AIT_GET_BUF(&c->cli_buf);
765:
766: c->cli_sock = TASK_FD(task);
767: memcpy(&c->cli_sa, &sa, sizeof c->cli_sa);
768:
769: /* armed timer for close stateless connection */
770: schedCancelby(TASK_ROOT(task), taskTIMER, CRITERIA_DATA, c, NULL);
771: schedTimer(TASK_ROOT(task), cbProto[srv->srv_proto][CB_CLOSECLIENT],
772: c, ts, c, 0);
773: }
774:
775: /* check integrity of packet */
776: crc = ntohs(rpc->call_crc);
777: rpc->call_crc ^= rpc->call_crc;
778: if (crc != crcFletcher16((u_short*) rpc, len / 2)) {
779: rpc_SetErr(ERPCMISMATCH, "Bad CRC RPC packet");
1.23.6.5! misho 780: /* close connection */
! 781: schedEvent(TASK_ROOT(task), cbProto[srv->srv_proto][CB_CLOSECLIENT],
! 782: c, 0, NULL, 0);
! 783: goto end;
1.23.6.3 misho 784: }
785:
786: noreply = RPC_CHK_NOREPLY(rpc);
787:
788: /* check RPC packet session info */
789: if (rpc_chkPktSession(&rpc->call_session, &srv->srv_session)) {
790: rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session");
791:
792: rpc->call_argc ^= rpc->call_argc;
793: rpc->call_rep.ret = RPC_ERROR(-1);
794: rpc->call_rep.eno = RPC_ERROR(errno);
795: } else {
796: /* execute RPC call */
797: schedEvent(TASK_ROOT(task), execCall, c, (int) noreply, rpc, len);
798: }
799:
800: /* send RPC reply */
801: if (!noreply)
802: schedWrite(TASK_ROOT(task), cbProto[srv->srv_proto][CB_TXPACKET],
803: c, TASK_FD(task), rpc, len);
804: end:
805: AIT_FREE_VAL(&b);
806: schedReadSelf(task);
807: return NULL;
808: }
809:
1.13 misho 810: /* ------------------------------------------------------ */
811:
1.16 misho 812: void
1.13 misho 813: rpc_freeBLOBCli(rpc_cli_t * __restrict c)
814: {
815: rpc_srv_t *s = c->cli_parent;
816:
817: schedCancelby(s->srv_blob.root, taskMAX, CRITERIA_ARG, c, NULL);
1.10 misho 818:
819: /* free buffer */
820: AIT_FREE_VAL(&c->cli_buf);
821:
1.14 misho 822: array_Del(s->srv_blob.clients, c->cli_id, 0);
1.10 misho 823: if (c)
1.14 misho 824: e_free(c);
1.13 misho 825: }
826:
827:
828: static void *
829: closeBLOBClient(sched_task_t *task)
830: {
831: int sock = ((rpc_cli_t*) TASK_ARG(task))->cli_sock;
832:
833: rpc_freeBLOBCli(TASK_ARG(task));
834:
835: /* close client socket */
836: shutdown(sock, SHUT_RDWR);
837: close(sock);
1.7 misho 838: return NULL;
839: }
840:
841: static void *
842: txBLOB(sched_task_t *task)
843: {
1.10 misho 844: rpc_cli_t *c = TASK_ARG(task);
845: u_char *buf = AIT_GET_BUF(&c->cli_buf);
1.7 misho 846: int wlen = sizeof(struct tagBLOBHdr);
847:
848: /* send reply */
1.10 misho 849: wlen = send(TASK_FD(task), buf, wlen, MSG_NOSIGNAL);
850: if (wlen == -1 || wlen != sizeof(struct tagBLOBHdr)) {
851: /* close blob connection */
852: schedEvent(TASK_ROOT(task), closeBLOBClient, c, 42, NULL, 0);
853: }
1.7 misho 854:
855: return NULL;
856: }
1.4 misho 857:
1.7 misho 858: static void *
859: rxBLOB(sched_task_t *task)
860: {
861: rpc_cli_t *c = TASK_ARG(task);
862: rpc_srv_t *s = c->cli_parent;
863: rpc_blob_t *b;
1.10 misho 864: struct tagBLOBHdr blob;
1.7 misho 865: int rlen;
866:
1.10 misho 867: memset(&blob, 0, sizeof blob);
868: rlen = recv(TASK_FD(task), &blob, sizeof blob, 0);
869: if (rlen < 1) {
870: /* close blob connection */
871: schedEvent(TASK_ROOT(task), closeBLOBClient, c, 42, NULL, 0);
1.7 misho 872: return NULL;
873: }
1.5 misho 874:
1.10 misho 875: /* check BLOB packet */
876: if (rlen < sizeof(struct tagBLOBHdr)) {
877: rpc_SetErr(ERPCMISMATCH, "Short BLOB packet");
1.6 misho 878:
1.10 misho 879: schedReadSelf(task);
1.7 misho 880: return NULL;
881: }
1.1 misho 882:
1.7 misho 883: /* check RPC packet session info */
1.15 misho 884: if (rpc_chkPktSession(&blob.hdr_session, &s->srv_session)) {
1.7 misho 885: rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session");
1.10 misho 886: blob.hdr_cmd = error;
1.7 misho 887: goto end;
888: }
889:
890: /* Go to proceed packet ... */
1.10 misho 891: switch (blob.hdr_cmd) {
1.7 misho 892: case get:
1.10 misho 893: if (!(b = rpc_srv_getBLOB(s, ntohl(blob.hdr_var)))) {
894: rpc_SetErr(EINVAL, "Var=%x not found", ntohl(blob.hdr_var));
895: blob.hdr_cmd = no;
896: blob.hdr_ret = RPC_ERROR(-1);
1.7 misho 897: break;
898: } else
1.10 misho 899: blob.hdr_len = htonl(b->blob_len);
1.5 misho 900:
1.7 misho 901: if (rpc_srv_blobMap(s, b) != -1) {
902: /* deliver BLOB variable to client */
1.10 misho 903: blob.hdr_ret = htonl(rpc_srv_sendBLOB(c, b));
1.7 misho 904: rpc_srv_blobUnmap(b);
905: } else {
1.10 misho 906: blob.hdr_cmd = error;
907: blob.hdr_ret = RPC_ERROR(-1);
1.7 misho 908: }
909: break;
910: case set:
1.17 misho 911: if ((b = rpc_srv_registerBLOB(s, ntohl(blob.hdr_len),
912: ntohl(blob.hdr_ret)))) {
1.7 misho 913: /* set new BLOB variable for reply :) */
1.10 misho 914: blob.hdr_var = htonl(b->blob_var);
1.7 misho 915:
916: /* receive BLOB from client */
1.10 misho 917: blob.hdr_ret = htonl(rpc_srv_recvBLOB(c, b));
1.7 misho 918: rpc_srv_blobUnmap(b);
1.5 misho 919: } else {
1.10 misho 920: blob.hdr_cmd = error;
921: blob.hdr_ret = RPC_ERROR(-1);
1.7 misho 922: }
923: break;
924: case unset:
1.11 misho 925: if (rpc_srv_unregisterBLOB(s, ntohl(blob.hdr_var)) == -1) {
1.10 misho 926: blob.hdr_cmd = error;
927: blob.hdr_ret = RPC_ERROR(-1);
1.1 misho 928: }
929: break;
1.7 misho 930: default:
1.10 misho 931: rpc_SetErr(EPROCUNAVAIL, "Unsupported BLOB command %d", blob.hdr_cmd);
932: blob.hdr_cmd = error;
933: blob.hdr_ret = RPC_ERROR(-1);
1.7 misho 934: }
1.1 misho 935:
1.7 misho 936: end:
1.10 misho 937: memcpy(AIT_ADDR(&c->cli_buf), &blob, sizeof blob);
938: schedWrite(TASK_ROOT(task), txBLOB, TASK_ARG(task), TASK_FD(task), NULL, 0);
939: schedReadSelf(task);
1.7 misho 940: return NULL;
1.2 misho 941: }
942:
943: static void *
1.17 misho 944: flushBLOB(sched_task_t *task)
945: {
1.23 misho 946: uintptr_t sigArg = atomic_load_acq_ptr(&_glSigArg);
947: rpc_srv_t *srv = sigArg ? (void*) sigArg : TASK_ARG(task);
1.17 misho 948: rpc_blob_t *b, *tmp;
949:
950: TAILQ_FOREACH_SAFE(b, &srv->srv_blob.blobs, blob_node, tmp) {
951: TAILQ_REMOVE(&srv->srv_blob.blobs, b, blob_node);
952:
953: rpc_srv_blobFree(srv, b);
954: e_free(b);
955: }
956:
1.23 misho 957: if (!schedSignalSelf(task)) {
958: /* disabled kqueue support in libaitsched */
959: struct sigaction sa;
960:
961: memset(&sa, 0, sizeof sa);
962: sigemptyset(&sa.sa_mask);
963: sa.sa_handler = (void (*)(int)) flushBLOB;
964: sa.sa_flags = SA_RESTART | SA_RESETHAND;
965: sigaction(SIGFBLOB, &sa, NULL);
966: }
967:
1.17 misho 968: return NULL;
969: }
970:
971: static void *
1.10 misho 972: acceptBLOBClients(sched_task_t *task)
1.2 misho 973: {
1.10 misho 974: rpc_srv_t *srv = TASK_ARG(task);
975: rpc_cli_t *c = NULL;
976: register int i;
1.14 misho 977: socklen_t salen = sizeof(sockaddr_t);
1.21 misho 978: int sock;
1.12 misho 979: #ifdef TCP_NOPUSH
980: int n = 1;
981: #endif
1.7 misho 982:
1.10 misho 983: /* check free slots for connect */
1.14 misho 984: for (i = 0; i < array_Size(srv->srv_blob.clients) &&
985: (c = array(srv->srv_blob.clients, i, rpc_cli_t*)); i++);
1.21 misho 986: if (c) { /* no more free slots! */
987: EVERBOSE(1, "BLOB client quota exceeded! Connection will be shutdown!\n");
988: if ((sock = accept(TASK_FD(task), NULL, NULL)) != -1) {
989: shutdown(sock, SHUT_RDWR);
990: close(sock);
991: }
1.10 misho 992: goto end;
1.21 misho 993: }
994:
1.14 misho 995: c = e_malloc(sizeof(rpc_cli_t));
1.10 misho 996: if (!c) {
1.7 misho 997: LOGERR;
1.10 misho 998: srv->srv_kill = srv->srv_blob.kill = 1;
1.7 misho 999: return NULL;
1000: } else {
1.10 misho 1001: memset(c, 0, sizeof(rpc_cli_t));
1.14 misho 1002: array_Set(srv->srv_blob.clients, i, c);
1.10 misho 1003: c->cli_id = i;
1004: c->cli_parent = srv;
1.7 misho 1005: }
1.4 misho 1006:
1.10 misho 1007: /* alloc empty buffer */
1.14 misho 1008: AIT_SET_BUFSIZ(&c->cli_buf, 0, srv->srv_netbuf);
1.2 misho 1009:
1.10 misho 1010: /* accept client */
1011: c->cli_sock = accept(TASK_FD(task), &c->cli_sa.sa, &salen);
1012: if (c->cli_sock == -1) {
1013: LOGERR;
1014: AIT_FREE_VAL(&c->cli_buf);
1.14 misho 1015: array_Del(srv->srv_blob.clients, i, 42);
1.10 misho 1016: goto end;
1.12 misho 1017: } else {
1018: #ifdef TCP_NOPUSH
1019: setsockopt(c->cli_sock, IPPROTO_TCP, TCP_NOPUSH, &n, sizeof n);
1020: #endif
1.10 misho 1021: fcntl(c->cli_sock, F_SETFL, fcntl(c->cli_sock, F_GETFL) | O_NONBLOCK);
1.12 misho 1022: }
1.2 misho 1023:
1.10 misho 1024: schedRead(TASK_ROOT(task), rxBLOB, c, c->cli_sock, NULL, 0);
1025: end:
1026: schedReadSelf(task);
1.7 misho 1027: return NULL;
1.1 misho 1028: }
1029:
1.10 misho 1030: /* ------------------------------------------------------ */
1.1 misho 1031:
1032: /*
1.7 misho 1033: * rpc_srv_initBLOBServer() - Init & create BLOB Server
1034: *
1.4 misho 1035: * @srv = RPC server instance
1.2 misho 1036: * @Port = Port for bind server, if Port == 0 default port is selected
1037: * @diskDir = Disk place for BLOB file objects
1038: * return: -1 == error or 0 bind and created BLOB server instance
1039: */
1040: int
1041: rpc_srv_initBLOBServer(rpc_srv_t * __restrict srv, u_short Port, const char *diskDir)
1042: {
1043: int n = 1;
1044:
1.10 misho 1045: if (!srv || srv->srv_kill) {
1.7 misho 1046: rpc_SetErr(EINVAL, "Invalid parameters can`t init BLOB server");
1.2 misho 1047: return -1;
1048: }
1049:
1050: memset(&srv->srv_blob, 0, sizeof srv->srv_blob);
1051: if (access(diskDir, R_OK | W_OK) == -1) {
1052: LOGERR;
1053: return -1;
1054: } else
1.7 misho 1055: AIT_SET_STR(&srv->srv_blob.dir, diskDir);
1.2 misho 1056:
1.10 misho 1057: /* init blob list */
1058: TAILQ_INIT(&srv->srv_blob.blobs);
1059:
1.2 misho 1060: srv->srv_blob.server.cli_parent = srv;
1.4 misho 1061:
1.14 misho 1062: memcpy(&srv->srv_blob.server.cli_sa, &srv->srv_server.cli_sa, sizeof(sockaddr_t));
1.10 misho 1063: switch (srv->srv_blob.server.cli_sa.sa.sa_family) {
1.4 misho 1064: case AF_INET:
1.10 misho 1065: srv->srv_blob.server.cli_sa.sin.sin_port =
1066: htons(Port ? Port : ntohs(srv->srv_blob.server.cli_sa.sin.sin_port) + 1);
1.4 misho 1067: break;
1068: case AF_INET6:
1.10 misho 1069: srv->srv_blob.server.cli_sa.sin6.sin6_port =
1070: htons(Port ? Port : ntohs(srv->srv_blob.server.cli_sa.sin6.sin6_port) + 1);
1.4 misho 1071: break;
1072: case AF_LOCAL:
1.10 misho 1073: strlcat(srv->srv_blob.server.cli_sa.sun.sun_path, ".blob",
1074: sizeof srv->srv_blob.server.cli_sa.sun.sun_path);
1.4 misho 1075: break;
1076: default:
1.7 misho 1077: AIT_FREE_VAL(&srv->srv_blob.dir);
1.4 misho 1078: return -1;
1.2 misho 1079: }
1080:
1.4 misho 1081: /* create BLOB server socket */
1.6 misho 1082: srv->srv_blob.server.cli_sock = socket(srv->srv_server.cli_sa.sa.sa_family, SOCK_STREAM, 0);
1.2 misho 1083: if (srv->srv_blob.server.cli_sock == -1) {
1084: LOGERR;
1.7 misho 1085: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 1086: return -1;
1087: }
1088: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
1089: LOGERR;
1090: close(srv->srv_blob.server.cli_sock);
1.7 misho 1091: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 1092: return -1;
1093: }
1.5 misho 1094: n = srv->srv_netbuf;
1095: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof n) == -1) {
1096: LOGERR;
1097: close(srv->srv_blob.server.cli_sock);
1.7 misho 1098: AIT_FREE_VAL(&srv->srv_blob.dir);
1.5 misho 1099: return -1;
1100: }
1101: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_RCVBUF, &n, sizeof n) == -1) {
1102: LOGERR;
1103: close(srv->srv_blob.server.cli_sock);
1.7 misho 1104: AIT_FREE_VAL(&srv->srv_blob.dir);
1.5 misho 1105: return -1;
1106: }
1.6 misho 1107: if (bind(srv->srv_blob.server.cli_sock, &srv->srv_blob.server.cli_sa.sa,
1108: srv->srv_blob.server.cli_sa.sa.sa_len) == -1) {
1.2 misho 1109: LOGERR;
1110: close(srv->srv_blob.server.cli_sock);
1.7 misho 1111: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 1112: return -1;
1.13 misho 1113: } else
1114: fcntl(srv->srv_blob.server.cli_sock, F_SETFL,
1115: fcntl(srv->srv_blob.server.cli_sock, F_GETFL) | O_NONBLOCK);
1116:
1.2 misho 1117:
1.10 misho 1118: /* allocate pool for concurent blob clients */
1.14 misho 1119: srv->srv_blob.clients = array_Init(array_Size(srv->srv_clients));
1.2 misho 1120: if (!srv->srv_blob.clients) {
1.14 misho 1121: rpc_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
1.2 misho 1122: close(srv->srv_blob.server.cli_sock);
1.7 misho 1123: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 1124: return -1;
1.10 misho 1125: }
1.2 misho 1126:
1.10 misho 1127: /* init blob scheduler */
1128: srv->srv_blob.root = schedBegin();
1129: if (!srv->srv_blob.root) {
1130: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
1.14 misho 1131: array_Destroy(&srv->srv_blob.clients);
1.10 misho 1132: close(srv->srv_blob.server.cli_sock);
1133: AIT_FREE_VAL(&srv->srv_blob.dir);
1134: return -1;
1135: }
1.2 misho 1136:
1137: return 0;
1138: }
1139:
1140: /*
1.7 misho 1141: * rpc_srv_endBLOBServer() - Destroy BLOB server, close all opened sockets and free resources
1142: *
1.2 misho 1143: * @srv = RPC Server instance
1144: * return: none
1145: */
1.16 misho 1146: void
1.2 misho 1147: rpc_srv_endBLOBServer(rpc_srv_t * __restrict srv)
1148: {
1.10 misho 1149: if (!srv)
1.2 misho 1150: return;
1151:
1.10 misho 1152: srv->srv_blob.kill = 1;
1.17 misho 1153:
1154: schedEnd(&srv->srv_blob.root);
1.2 misho 1155: }
1156:
1157: /*
1.10 misho 1158: * rpc_srv_loopBLOBServer() - Execute Main BLOB server loop and wait for clients requests
1.7 misho 1159: *
1.2 misho 1160: * @srv = RPC Server instance
1161: * return: -1 error or 0 ok, infinite loop ...
1162: */
1163: int
1.10 misho 1164: rpc_srv_loopBLOBServer(rpc_srv_t * __restrict srv)
1.2 misho 1165: {
1.10 misho 1166: rpc_cli_t *c;
1.2 misho 1167: register int i;
1.10 misho 1168: rpc_blob_t *b, *tmp;
1169: struct timespec ts = { RPC_SCHED_POLLING, 0 };
1.2 misho 1170:
1.10 misho 1171: if (!srv || srv->srv_kill) {
1.7 misho 1172: rpc_SetErr(EINVAL, "Invalid parameter can`t start BLOB server");
1.2 misho 1173: return -1;
1174: }
1175:
1.14 misho 1176: if (listen(srv->srv_blob.server.cli_sock, array_Size(srv->srv_blob.clients)) == -1) {
1.2 misho 1177: LOGERR;
1178: return -1;
1.10 misho 1179: }
1180:
1.23 misho 1181: if (!schedSignal(srv->srv_blob.root, flushBLOB, srv, SIGFBLOB, NULL, 0)) {
1182: /* disabled kqueue support in libaitsched */
1183: struct sigaction sa;
1184:
1185: atomic_store_rel_ptr(&_glSigArg, (uintptr_t) srv);
1186:
1187: memset(&sa, 0, sizeof sa);
1188: sigemptyset(&sa.sa_mask);
1189: sa.sa_handler = (void (*)(int)) flushBLOB;
1190: sa.sa_flags = SA_RESTART | SA_RESETHAND;
1191: sigaction(SIGFBLOB, &sa, NULL);
1192: }
1193:
1.10 misho 1194: if (!schedRead(srv->srv_blob.root, acceptBLOBClients, srv,
1195: srv->srv_blob.server.cli_sock, NULL, 0)) {
1196: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
1197: return -1;
1198: }
1.2 misho 1199:
1.10 misho 1200: schedPolling(srv->srv_blob.root, &ts, NULL);
1201: /* main rpc loop */
1202: schedRun(srv->srv_blob.root, &srv->srv_blob.kill);
1.7 misho 1203:
1.17 misho 1204: /* detach blobs */
1205: TAILQ_FOREACH_SAFE(b, &srv->srv_blob.blobs, blob_node, tmp) {
1206: TAILQ_REMOVE(&srv->srv_blob.blobs, b, blob_node);
1207:
1208: rpc_srv_blobFree(srv, b);
1209: e_free(b);
1210: }
1211:
1.10 misho 1212: /* close all clients connections & server socket */
1.14 misho 1213: for (i = 0; i < array_Size(srv->srv_blob.clients); i++) {
1214: c = array(srv->srv_blob.clients, i, rpc_cli_t*);
1.10 misho 1215: if (c) {
1216: shutdown(c->cli_sock, SHUT_RDWR);
1217: close(c->cli_sock);
1218:
1219: schedCancelby(srv->srv_blob.root, taskMAX, CRITERIA_ARG, c, NULL);
1220: AIT_FREE_VAL(&c->cli_buf);
1.2 misho 1221: }
1.14 misho 1222: array_Del(srv->srv_blob.clients, i, 42);
1.10 misho 1223: }
1.14 misho 1224: array_Destroy(&srv->srv_blob.clients);
1.2 misho 1225:
1.10 misho 1226: close(srv->srv_blob.server.cli_sock);
1.2 misho 1227:
1.10 misho 1228: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 1229: return 0;
1230: }
1231:
1232:
1233: /*
1.7 misho 1234: * rpc_srv_initServer() - Init & create RPC Server
1235: *
1.15 misho 1236: * @InstID = Instance for authentication & recognition
1.1 misho 1237: * @concurentClients = Concurent clients at same time to this server
1.10 misho 1238: * @netBuf = Network buffer length (min:512 bytes), if =0 == BUFSIZ (also meaning max RPC packet)
1.4 misho 1239: * @csHost = Host name or address for bind server, if NULL any address
1.1 misho 1240: * @Port = Port for bind server, if Port == 0 default port is selected
1.13 misho 1241: * @proto = Protocol, if == 0 choose SOCK_STREAM
1.1 misho 1242: * return: NULL == error or !=NULL bind and created RPC server instance
1243: */
1244: rpc_srv_t *
1.15 misho 1245: rpc_srv_initServer(u_char InstID, int concurentClients, int netBuf,
1246: const char *csHost, u_short Port, int proto)
1.1 misho 1247: {
1.10 misho 1248: int n = 1;
1.1 misho 1249: rpc_srv_t *srv = NULL;
1.14 misho 1250: sockaddr_t sa = E_SOCKADDR_INIT;
1.1 misho 1251:
1.15 misho 1252: if (!concurentClients || (proto < 0 || proto > SOCK_DGRAM)) {
1.10 misho 1253: rpc_SetErr(EINVAL, "Invalid parameters can`t init RPC server");
1.1 misho 1254: return NULL;
1255: }
1.14 misho 1256: if (!e_gethostbyname(csHost, Port, &sa))
1.10 misho 1257: return NULL;
1.1 misho 1258: if (!Port)
1259: Port = RPC_DEFPORT;
1.13 misho 1260: if (!proto)
1261: proto = SOCK_STREAM;
1.10 misho 1262: if (netBuf < RPC_MIN_BUFSIZ)
1.5 misho 1263: netBuf = BUFSIZ;
1.7 misho 1264: else
1.14 misho 1265: netBuf = E_ALIGN(netBuf, 2); /* align netBuf length */
1.10 misho 1266:
1267: #ifdef HAVE_SRANDOMDEV
1268: srandomdev();
1269: #else
1270: time_t tim;
1271:
1272: srandom((time(&tim) ^ getpid()));
1273: #endif
1.1 misho 1274:
1.14 misho 1275: srv = e_malloc(sizeof(rpc_srv_t));
1.1 misho 1276: if (!srv) {
1277: LOGERR;
1278: return NULL;
1279: } else
1280: memset(srv, 0, sizeof(rpc_srv_t));
1281:
1.13 misho 1282: srv->srv_proto = proto;
1.5 misho 1283: srv->srv_netbuf = netBuf;
1.1 misho 1284: srv->srv_session.sess_version = RPC_VERSION;
1.15 misho 1285: srv->srv_session.sess_instance = InstID;
1.1 misho 1286:
1287: srv->srv_server.cli_parent = srv;
1.10 misho 1288: memcpy(&srv->srv_server.cli_sa, &sa, sizeof srv->srv_server.cli_sa);
1289:
1.12 misho 1290: /* init functions */
1291: pthread_mutex_init(&srv->srv_funcs.mtx, NULL);
1292: SLIST_INIT(&srv->srv_funcs);
1293: AVL_INIT(&srv->srv_funcs);
1.10 misho 1294:
1295: /* init scheduler */
1296: srv->srv_root = schedBegin();
1297: if (!srv->srv_root) {
1298: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
1.12 misho 1299: pthread_mutex_destroy(&srv->srv_funcs.mtx);
1.14 misho 1300: e_free(srv);
1.10 misho 1301: return NULL;
1302: }
1303:
1304: /* init pool for clients */
1.14 misho 1305: srv->srv_clients = array_Init(concurentClients);
1.10 misho 1306: if (!srv->srv_clients) {
1.14 misho 1307: rpc_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
1.10 misho 1308: schedEnd(&srv->srv_root);
1.12 misho 1309: pthread_mutex_destroy(&srv->srv_funcs.mtx);
1.14 misho 1310: e_free(srv);
1.10 misho 1311: return NULL;
1312: }
1.4 misho 1313:
1314: /* create server socket */
1.13 misho 1315: srv->srv_server.cli_sock = socket(srv->srv_server.cli_sa.sa.sa_family, srv->srv_proto, 0);
1.1 misho 1316: if (srv->srv_server.cli_sock == -1) {
1317: LOGERR;
1.14 misho 1318: array_Destroy(&srv->srv_clients);
1.10 misho 1319: schedEnd(&srv->srv_root);
1.12 misho 1320: pthread_mutex_destroy(&srv->srv_funcs.mtx);
1.14 misho 1321: e_free(srv);
1.1 misho 1322: return NULL;
1323: }
1324: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
1325: LOGERR;
1.10 misho 1326: goto err;
1.1 misho 1327: }
1.5 misho 1328: n = srv->srv_netbuf;
1329: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof n) == -1) {
1330: LOGERR;
1.10 misho 1331: goto err;
1.5 misho 1332: }
1333: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_RCVBUF, &n, sizeof n) == -1) {
1334: LOGERR;
1.10 misho 1335: goto err;
1.5 misho 1336: }
1.6 misho 1337: if (bind(srv->srv_server.cli_sock, &srv->srv_server.cli_sa.sa,
1338: srv->srv_server.cli_sa.sa.sa_len) == -1) {
1.1 misho 1339: LOGERR;
1.10 misho 1340: goto err;
1.13 misho 1341: } else
1342: fcntl(srv->srv_server.cli_sock, F_SETFL,
1343: fcntl(srv->srv_server.cli_sock, F_GETFL) | O_NONBLOCK);
1.1 misho 1344:
1.10 misho 1345: rpc_register_srvPing(srv);
1.8 misho 1346:
1.1 misho 1347: return srv;
1.10 misho 1348: err: /* error condition */
1349: close(srv->srv_server.cli_sock);
1.14 misho 1350: array_Destroy(&srv->srv_clients);
1.10 misho 1351: schedEnd(&srv->srv_root);
1.12 misho 1352: pthread_mutex_destroy(&srv->srv_funcs.mtx);
1.14 misho 1353: e_free(srv);
1.10 misho 1354: return NULL;
1.1 misho 1355: }
1356:
1357: /*
1.7 misho 1358: * rpc_srv_endServer() - Destroy RPC server, close all opened sockets and free resources
1359: *
1.6 misho 1360: * @psrv = RPC Server instance
1.1 misho 1361: * return: none
1362: */
1.16 misho 1363: void
1.6 misho 1364: rpc_srv_endServer(rpc_srv_t ** __restrict psrv)
1.1 misho 1365: {
1.10 misho 1366: if (!psrv || !*psrv)
1.1 misho 1367: return;
1368:
1.10 misho 1369: /* if send kill to blob server */
1.17 misho 1370: rpc_srv_endBLOBServer(*psrv);
1.1 misho 1371:
1.10 misho 1372: (*psrv)->srv_kill = 1;
1373: sleep(RPC_SCHED_POLLING);
1.2 misho 1374:
1.17 misho 1375: schedEnd(&(*psrv)->srv_root);
1376:
1.12 misho 1377: pthread_mutex_destroy(&(*psrv)->srv_funcs.mtx);
1.14 misho 1378: e_free(*psrv);
1.6 misho 1379: *psrv = NULL;
1.1 misho 1380: }
1381:
1382: /*
1.7 misho 1383: * rpc_srv_loopServer() - Execute Main server loop and wait for clients requests
1384: *
1.1 misho 1385: * @srv = RPC Server instance
1386: * return: -1 error or 0 ok, infinite loop ...
1387: */
1388: int
1.5 misho 1389: rpc_srv_loopServer(rpc_srv_t * __restrict srv)
1.1 misho 1390: {
1.10 misho 1391: rpc_cli_t *c;
1.1 misho 1392: register int i;
1.12 misho 1393: rpc_func_t *f;
1.10 misho 1394: struct timespec ts = { RPC_SCHED_POLLING, 0 };
1.1 misho 1395:
1396: if (!srv) {
1.10 misho 1397: rpc_SetErr(EINVAL, "Invalid parameter can`t start RPC server");
1.1 misho 1398: return -1;
1399: }
1400:
1.13 misho 1401: if (srv->srv_proto == SOCK_STREAM)
1.14 misho 1402: if (listen(srv->srv_server.cli_sock, array_Size(srv->srv_clients)) == -1) {
1.13 misho 1403: LOGERR;
1404: return -1;
1405: }
1.1 misho 1406:
1.13 misho 1407: if (!schedRead(srv->srv_root, cbProto[srv->srv_proto][CB_ACCEPTCLIENT], srv,
1408: srv->srv_server.cli_sock, NULL, 0)) {
1.10 misho 1409: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
1410: return -1;
1411: }
1.7 misho 1412:
1.10 misho 1413: schedPolling(srv->srv_root, &ts, NULL);
1.7 misho 1414: /* main rpc loop */
1.10 misho 1415: schedRun(srv->srv_root, &srv->srv_kill);
1416:
1417: /* close all clients connections & server socket */
1.14 misho 1418: for (i = 0; i < array_Size(srv->srv_clients); i++) {
1419: c = array(srv->srv_clients, i, rpc_cli_t*);
1.10 misho 1420: if (c) {
1.23.6.3 misho 1421: if (srv->srv_proto == SOCK_STREAM) {
1422: shutdown(c->cli_sock, SHUT_RDWR);
1423: close(c->cli_sock);
1424: }
1.10 misho 1425:
1426: schedCancelby(srv->srv_root, taskMAX, CRITERIA_ARG, c, NULL);
1.14 misho 1427: ait_freeVars(&RPC_RETVARS(c));
1.10 misho 1428: AIT_FREE_VAL(&c->cli_buf);
1.1 misho 1429: }
1.14 misho 1430: array_Del(srv->srv_clients, i, 42);
1.10 misho 1431: }
1.14 misho 1432: array_Destroy(&srv->srv_clients);
1.2 misho 1433:
1.10 misho 1434: close(srv->srv_server.cli_sock);
1.2 misho 1435:
1.10 misho 1436: /* detach exported calls */
1.12 misho 1437: RPC_FUNCS_LOCK(&srv->srv_funcs);
1438: while ((f = SLIST_FIRST(&srv->srv_funcs))) {
1439: SLIST_REMOVE_HEAD(&srv->srv_funcs, func_next);
1.1 misho 1440:
1.10 misho 1441: AIT_FREE_VAL(&f->func_name);
1.14 misho 1442: e_free(f);
1.1 misho 1443: }
1.12 misho 1444: srv->srv_funcs.avlh_root = NULL;
1445: RPC_FUNCS_UNLOCK(&srv->srv_funcs);
1.1 misho 1446:
1447: return 0;
1448: }
1449:
1450:
1451: /*
1452: * rpc_srv_execCall() Execute registered call from RPC server
1.7 misho 1453: *
1.10 misho 1454: * @cli = RPC client
1.1 misho 1455: * @rpc = IN RPC call structure
1.10 misho 1456: * @funcname = Execute RPC function
1.5 misho 1457: * @args = IN RPC calling arguments from RPC client
1.1 misho 1458: * return: -1 error, !=-1 ok
1459: */
1460: int
1.10 misho 1461: rpc_srv_execCall(rpc_cli_t * __restrict cli, struct tagRPCCall * __restrict rpc,
1462: ait_val_t funcname, array_t * __restrict args)
1.1 misho 1463: {
1464: rpc_callback_t func;
1465:
1.10 misho 1466: if (!cli || !rpc || !AIT_ADDR(&funcname)) {
1.7 misho 1467: rpc_SetErr(EINVAL, "Invalid parameter can`t exec function");
1.1 misho 1468: return -1;
1469: }
1470:
1.10 misho 1471: func = AIT_GET_LIKE(&funcname, rpc_callback_t);
1472: return func(cli, rpc, args);
1.1 misho 1473: }
1.23.6.1 misho 1474:
1475:
1476: /*
1477: * rpc_srv_initServer2() - Init & create layer2 RPC Server
1478: *
1479: * @InstID = Instance for authentication & recognition
1480: * @concurentClients = Concurent clients at same time to this server
1481: * @netBuf = Network buffer length (min:512 bytes), if =0 == BUFSIZ (also meaning max RPC packet)
1482: * @csIface = Interface name for bind server, if NULL first interface on host
1483: * return: NULL == error or !=NULL bind and created RPC server instance
1484: */
1485: rpc_srv_t *
1.23.6.2 misho 1486: rpc_srv_initServer2(u_char InstID, int concurentClients, int netBuf, const char *csIface)
1.23.6.1 misho 1487: {
1488: int n = 1;
1489: rpc_srv_t *srv = NULL;
1490: sockaddr_t sa = E_SOCKADDR_INIT;
1491: char szIface[64], szStr[STRSIZ];
1492: register int i;
1493: struct ifreq ifr;
1.23.6.3 misho 1494: struct bpf_insn insns[] = {
1495: BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12),
1496: BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, RPC_DEFPORT, 0, 1),
1497: BPF_STMT(BPF_RET + BPF_K, -1),
1498: BPF_STMT(BPF_RET + BPF_K, 0),
1499: };
1500: struct bpf_program fcode = {
1501: .bf_len = sizeof(insns) / sizeof(struct bpf_insn),
1502: .bf_insns = insns
1503: };
1.23.6.1 misho 1504:
1505: if (!concurentClients) {
1506: rpc_SetErr(EINVAL, "Invalid parameters can`t init RPC server");
1507: return NULL;
1508: }
1509: if (!csIface) {
1510: if (e_get1stiface(szIface, sizeof szIface))
1511: return NULL;
1512: } else
1513: strlcpy(szIface, csIface, sizeof szIface);
1.23.6.3 misho 1514: if (!e_getifacebyname(szIface, &sa))
1.23.6.1 misho 1515: return NULL;
1516:
1517: #ifdef HAVE_SRANDOMDEV
1518: srandomdev();
1519: #else
1520: time_t tim;
1521:
1522: srandom((time(&tim) ^ getpid()));
1523: #endif
1524:
1525: srv = e_malloc(sizeof(rpc_srv_t));
1526: if (!srv) {
1527: LOGERR;
1528: return NULL;
1529: } else
1530: memset(srv, 0, sizeof(rpc_srv_t));
1531:
1.23.6.2 misho 1532: srv->srv_proto = SOCK_BPF;
1.23.6.1 misho 1533: srv->srv_netbuf = netBuf;
1534: srv->srv_session.sess_version = RPC_VERSION;
1535: srv->srv_session.sess_instance = InstID;
1536:
1537: srv->srv_server.cli_parent = srv;
1538: memcpy(&srv->srv_server.cli_sa, &sa, sizeof srv->srv_server.cli_sa);
1539:
1540: /* init functions */
1541: pthread_mutex_init(&srv->srv_funcs.mtx, NULL);
1542: SLIST_INIT(&srv->srv_funcs);
1543: AVL_INIT(&srv->srv_funcs);
1544:
1545: /* init scheduler */
1546: srv->srv_root = schedBegin();
1547: if (!srv->srv_root) {
1548: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
1549: pthread_mutex_destroy(&srv->srv_funcs.mtx);
1550: e_free(srv);
1551: return NULL;
1552: }
1553:
1554: /* init pool for clients */
1555: srv->srv_clients = array_Init(concurentClients);
1556: if (!srv->srv_clients) {
1557: rpc_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
1558: schedEnd(&srv->srv_root);
1559: pthread_mutex_destroy(&srv->srv_funcs.mtx);
1560: e_free(srv);
1561: return NULL;
1562: }
1563:
1564: /* create server handler */
1565: for (i = 0; i < 10; i++) {
1566: memset(szStr, 0, sizeof szStr);
1567: snprintf(szStr, sizeof szStr, "/dev/bpf%d", i);
1568: srv->srv_server.cli_sock = open(szStr, O_RDWR);
1569: if (srv->srv_server.cli_sock > STDERR_FILENO)
1570: break;
1571: }
1572: if (srv->srv_server.cli_sock < 3) {
1573: LOGERR;
1574: array_Destroy(&srv->srv_clients);
1575: schedEnd(&srv->srv_root);
1576: pthread_mutex_destroy(&srv->srv_funcs.mtx);
1577: e_free(srv);
1578: return NULL;
1579: }
1580:
1581: if (ioctl(srv->srv_server.cli_sock, BIOCIMMEDIATE, &n) == -1) {
1582: LOGERR;
1583: goto err;
1584: }
1.23.6.3 misho 1585: if (ioctl(srv->srv_server.cli_sock, BIOCSETF, &fcode) == -1) {
1586: LOGERR;
1587: goto err;
1588: }
1.23.6.1 misho 1589: n = (netBuf < RPC_MIN_BUFSIZ) ? getpagesize() : E_ALIGN(netBuf, 2);
1590: if (ioctl(srv->srv_server.cli_sock, BIOCSBLEN, &n) == -1) {
1591: LOGERR;
1592: goto err;
1593: } else
1594: srv->srv_netbuf = n;
1595:
1596: memset(&ifr, 0, sizeof ifr);
1597: strlcpy(ifr.ifr_name, szIface, sizeof ifr.ifr_name);
1598: if (ioctl(srv->srv_server.cli_sock, BIOCSETIF, &ifr) == -1) {
1599: LOGERR;
1600: goto err;
1601: } else
1602: fcntl(srv->srv_server.cli_sock, F_SETFL,
1603: fcntl(srv->srv_server.cli_sock, F_GETFL) | O_NONBLOCK);
1604:
1605: rpc_register_srvPing(srv);
1606:
1607: return srv;
1608: err: /* error condition */
1609: close(srv->srv_server.cli_sock);
1610: array_Destroy(&srv->srv_clients);
1611: schedEnd(&srv->srv_root);
1612: pthread_mutex_destroy(&srv->srv_funcs.mtx);
1613: e_free(srv);
1614: return NULL;
1615: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>