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