Annotation of libaitrpc/src/srv.c, revision 1.19
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.19 ! misho 6: * $Id: srv.c,v 1.18.2.2 2013/08/23 13:34:52 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.14 misho 15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
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:
62: static sched_task_func_t cbProto[SOCK_RAW + 1][4] = {
63: { acceptClients, closeClient, rxPacket, txPacket }, /* SOCK_STREAM */
64: { acceptClients, closeClient, rxPacket, txPacket }, /* SOCK_STREAM */
65: { rxUDPPacket, freeClient, rxUDPPacket, txUDPPacket }, /* SOCK_DGRAM */
66: { NULL, NULL, NULL, NULL } /* SOCK_RAW */
67: };
68:
69:
1.16 misho 70: void
1.13 misho 71: rpc_freeCli(rpc_cli_t * __restrict c)
1.10 misho 72: {
73: rpc_srv_t *s = c->cli_parent;
74:
1.13 misho 75: schedCancelby(s->srv_root, taskMAX, CRITERIA_ARG, c, NULL);
1.10 misho 76:
77: /* free buffer */
78: AIT_FREE_VAL(&c->cli_buf);
79:
1.14 misho 80: array_Del(s->srv_clients, c->cli_id, 0);
1.10 misho 81: if (c)
1.14 misho 82: e_free(c);
1.13 misho 83: }
84:
85:
86: static inline int
1.14 misho 87: _check4freeslot(rpc_srv_t * __restrict srv, sockaddr_t * __restrict sa)
1.13 misho 88: {
89: rpc_cli_t *c = NULL;
90: register int i;
91:
92: /* check free slots for connect */
1.14 misho 93: for (i = 0; i < array_Size(srv->srv_clients) &&
94: (c = array(srv->srv_clients, i, rpc_cli_t*)); i++)
1.13 misho 95: /* check for duplicates */
1.14 misho 96: if (sa && !e_addrcmp(&c->cli_sa, sa, 42))
1.13 misho 97: break;
1.14 misho 98: if (i >= array_Size(srv->srv_clients))
1.13 misho 99: return -1; /* no more free slots! */
100:
101: return i;
102: }
103:
104: static rpc_cli_t *
1.14 misho 105: _allocClient(rpc_srv_t * __restrict srv, sockaddr_t * __restrict sa)
1.13 misho 106: {
107: rpc_cli_t *c = NULL;
108: int n;
109:
110: n = _check4freeslot(srv, sa);
111: if (n == -1)
112: return NULL;
113: else
1.14 misho 114: c = array(srv->srv_clients, n, rpc_cli_t*);
1.13 misho 115:
116: if (!c) {
1.14 misho 117: c = e_malloc(sizeof(rpc_cli_t));
1.13 misho 118: if (!c) {
119: LOGERR;
120: srv->srv_kill = 1;
121: return NULL;
122: } else {
123: memset(c, 0, sizeof(rpc_cli_t));
1.14 misho 124: array_Set(srv->srv_clients, n, c);
1.13 misho 125: c->cli_id = n;
126: c->cli_parent = srv;
127: }
128:
129: /* alloc empty buffer */
1.14 misho 130: AIT_SET_BUFSIZ(&c->cli_buf, 0, srv->srv_netbuf);
1.13 misho 131: }
132:
133: return c;
134: }
135:
136:
137: static void *
138: freeClient(sched_task_t *task)
139: {
140: rpc_freeCli(TASK_ARG(task));
141:
142: return NULL;
143: }
144:
145: static void *
146: closeClient(sched_task_t *task)
147: {
148: int sock = ((rpc_cli_t*) TASK_ARG(task))->cli_sock;
149:
150: rpc_freeCli(TASK_ARG(task));
151:
152: /* close client socket */
153: shutdown(sock, SHUT_RDWR);
154: close(sock);
1.10 misho 155: return NULL;
156: }
1.7 misho 157:
158: static void *
159: txPacket(sched_task_t *task)
160: {
161: rpc_cli_t *c = TASK_ARG(task);
162: rpc_srv_t *s = c->cli_parent;
163: rpc_func_t *f = NULL;
1.18 misho 164: u_char *buf = AIT_GET_BUF(&c->cli_buf);
1.7 misho 165: struct tagRPCCall *rpc = (struct tagRPCCall*) buf;
1.18 misho 166: int ret, estlen, wlen = sizeof(struct tagRPCCall);
1.19 ! misho 167: struct pollfd pfd;
1.7 misho 168:
169: if (rpc->call_argc) {
1.10 misho 170: f = rpc_srv_getCall(s, ntohs(rpc->call_tag));
1.7 misho 171: if (!f) {
1.10 misho 172: rpc_SetErr(EPROGUNAVAIL, "Function not found at RPC server");
1.18 misho 173:
1.7 misho 174: rpc->call_argc ^= rpc->call_argc;
175: rpc->call_rep.ret = RPC_ERROR(-1);
176: rpc->call_rep.eno = RPC_ERROR(rpc_Errno);
177: } else {
1.18 misho 178: /* calc estimated length */
179: estlen = ait_resideVars(RPC_RETVARS(c)) + wlen;
180: if (estlen > AIT_LEN(&c->cli_buf))
181: AIT_RE_BUF(&c->cli_buf, estlen);
182: buf = AIT_GET_BUF(&c->cli_buf);
1.19 ! misho 183: rpc = (struct tagRPCCall*) buf;
1.18 misho 184:
1.14 misho 185: rpc->call_argc = htons(array_Size(RPC_RETVARS(c)));
1.7 misho 186: /* Go Encapsulate variables */
1.18 misho 187: ret = ait_vars2buffer(buf + wlen, AIT_LEN(&c->cli_buf) - wlen,
188: RPC_RETVARS(c));
1.10 misho 189: /* Free return values */
1.14 misho 190: ait_freeVars(&c->cli_vars);
1.7 misho 191: if (ret == -1) {
192: rpc_SetErr(EBADRPC, "Prepare RPC packet failed");
1.19 ! misho 193:
1.7 misho 194: rpc->call_argc ^= rpc->call_argc;
195: rpc->call_rep.ret = RPC_ERROR(-1);
196: rpc->call_rep.eno = RPC_ERROR(rpc_Errno);
197: } else
198: wlen += ret;
199: }
200: }
201:
1.18 misho 202: rpc->call_len = htonl(wlen);
1.8 misho 203:
1.15 misho 204: #if 0
1.7 misho 205: /* calculate CRC */
206: rpc->call_crc ^= rpc->call_crc;
1.8 misho 207: rpc->call_crc = htons(crcFletcher16((u_short*) buf, wlen / 2));
1.15 misho 208: #endif
1.7 misho 209:
210: /* send reply */
1.19 ! misho 211: pfd.fd = TASK_FD(task);
! 212: pfd.events = POLLOUT;
! 213: for (; wlen > 0; wlen -= ret, buf += ret) {
! 214: if ((ret = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) < 1 ||
! 215: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
! 216: if (ret)
! 217: LOGERR;
! 218: else
! 219: rpc_SetErr(ETIMEDOUT, "Timeout reached! Server not respond");
! 220: /* close connection */
! 221: schedEvent(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
! 222: TASK_ARG(task), 0, NULL, 0);
! 223: return NULL;
! 224: }
! 225: ret = send(TASK_FD(task), buf, MIN(wlen, s->srv_netbuf), MSG_NOSIGNAL);
! 226: if (ret == -1) {
! 227: /* close connection */
! 228: schedEvent(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
! 229: TASK_ARG(task), 0, NULL, 0);
! 230: return NULL;
! 231: }
1.10 misho 232: }
1.7 misho 233:
234: return NULL;
235: }
236:
237: static void *
238: execCall(sched_task_t *task)
239: {
240: rpc_cli_t *c = TASK_ARG(task);
241: rpc_srv_t *s = c->cli_parent;
242: rpc_func_t *f = NULL;
243: array_t *arr = NULL;
1.18 misho 244: u_char *buf = AIT_GET_BUF(&c->cli_buf);
1.7 misho 245: struct tagRPCCall *rpc = (struct tagRPCCall*) buf;
246: int argc = ntohs(rpc->call_argc);
247:
248: /* Go decapsulate variables ... */
1.8 misho 249: if (argc) {
1.14 misho 250: arr = ait_buffer2vars(buf + sizeof(struct tagRPCCall),
1.18 misho 251: AIT_LEN(&c->cli_buf) - sizeof(struct tagRPCCall), argc, 42);
1.7 misho 252: if (!arr) {
1.14 misho 253: rpc_SetErr(ERPCMISMATCH, "#%d - %s", elwix_GetErrno(), elwix_GetError());
1.18 misho 254:
1.7 misho 255: rpc->call_argc ^= rpc->call_argc;
256: rpc->call_rep.ret = RPC_ERROR(-1);
257: rpc->call_rep.eno = RPC_ERROR(rpc_Errno);
258: return NULL;
259: }
1.10 misho 260: } else
261: arr = NULL;
1.7 misho 262:
1.10 misho 263: if (!(f = rpc_srv_getCall(s, ntohs(rpc->call_tag)))) {
1.7 misho 264: rpc_SetErr(EPROGUNAVAIL, "Function not found at RPC server");
1.18 misho 265:
1.7 misho 266: rpc->call_argc ^= rpc->call_argc;
267: rpc->call_rep.ret = RPC_ERROR(-1);
268: rpc->call_rep.eno = RPC_ERROR(rpc_Errno);
269: } else {
1.8 misho 270: /* if client doesn't want reply */
1.10 misho 271: argc = RPC_CHK_NOREPLY(rpc);
272: rpc->call_rep.ret = RPC_ERROR(rpc_srv_execCall(c, rpc, f->func_name, arr));
1.7 misho 273: if (rpc->call_rep.ret == htonl(-1)) {
274: rpc->call_rep.eno = RPC_ERROR(errno);
275: rpc->call_argc ^= rpc->call_argc;
276: } else {
277: rpc->call_rep.eno ^= rpc->call_rep.eno;
1.8 misho 278: if (argc) {
279: /* without reply */
1.14 misho 280: ait_freeVars(&c->cli_vars);
1.8 misho 281: rpc->call_argc ^= rpc->call_argc;
1.10 misho 282: } else {
283: /* reply */
1.14 misho 284: rpc->call_argc = htons(array_Size(RPC_RETVARS(c)));
1.10 misho 285: }
1.7 misho 286: }
287: }
288:
1.14 misho 289: array_Destroy(&arr);
1.7 misho 290: return NULL;
291: }
292:
293: static void *
294: rxPacket(sched_task_t *task)
295: {
296: rpc_cli_t *c = TASK_ARG(task);
297: rpc_srv_t *s = c->cli_parent;
1.18 misho 298: int len, rlen, noreply, estlen;
1.15 misho 299: #if 0
300: u_short crc;
301: #endif
1.10 misho 302: u_char *buf = AIT_GET_BUF(&c->cli_buf);
1.18 misho 303: struct tagRPCCall *rpc = (struct tagRPCCall*) buf;
1.19 ! misho 304: struct pollfd pfd;
1.7 misho 305:
1.18 misho 306: memset(buf, 0, sizeof(struct tagRPCCall));
1.19 ! misho 307: rlen = recv(TASK_FD(task), rpc, sizeof(struct tagRPCCall), MSG_PEEK);
1.18 misho 308: if (rlen < sizeof(struct tagRPCCall)) {
1.10 misho 309: /* close connection */
1.13 misho 310: schedEvent(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
311: TASK_ARG(task), 0, NULL, 0);
1.7 misho 312: return NULL;
1.10 misho 313: } else {
1.18 misho 314: estlen = ntohl(rpc->call_len);
315: if (estlen > AIT_LEN(&c->cli_buf))
316: AIT_RE_BUF(&c->cli_buf, estlen);
317: rpc = (struct tagRPCCall*) AIT_GET_BUF(&c->cli_buf);
1.19 ! misho 318: buf = AIT_GET_BUF(&c->cli_buf);
! 319: len = estlen;
1.18 misho 320: }
321:
1.19 ! misho 322: /* get next part of packet */
! 323: memset(buf, 0, len);
! 324: pfd.fd = TASK_FD(task);
! 325: pfd.events = POLLIN | POLLPRI;
! 326: for (; len > 0; len -= rlen, buf += rlen) {
! 327: if ((rlen = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) < 1 ||
! 328: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
! 329: if (rlen)
! 330: LOGERR;
! 331: else
! 332: rpc_SetErr(ETIMEDOUT, "Timeout reached! Server not respond");
! 333: schedEvent(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
! 334: TASK_ARG(task), 0, NULL, 0);
! 335: return NULL;
! 336: }
1.18 misho 337: rlen = recv(TASK_FD(task), buf, len, 0);
338: if (rlen == -1) {
339: /* close connection */
340: schedEvent(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
341: TASK_ARG(task), 0, NULL, 0);
1.8 misho 342: return NULL;
1.10 misho 343: }
1.18 misho 344: }
1.8 misho 345:
1.15 misho 346: #if 0
1.18 misho 347: /* check integrity of packet */
348: crc = ntohs(rpc->call_crc);
349: rpc->call_crc ^= rpc->call_crc;
350: if (crc != crcFletcher16((u_short*) rpc, len / 2)) {
351: rpc_SetErr(ERPCMISMATCH, "Bad CRC RPC packet");
352: return NULL;
353: }
1.15 misho 354: #endif
1.7 misho 355:
1.18 misho 356: noreply = RPC_CHK_NOREPLY(rpc);
1.7 misho 357:
1.18 misho 358: /* check RPC packet session info */
359: if (rpc_chkPktSession(&rpc->call_session, &s->srv_session)) {
360: rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session");
1.7 misho 361:
1.18 misho 362: rpc->call_argc ^= rpc->call_argc;
363: rpc->call_rep.ret = RPC_ERROR(-1);
364: rpc->call_rep.eno = RPC_ERROR(errno);
365: } else {
366: /* execute RPC call */
367: schedEvent(TASK_ROOT(task), execCall, TASK_ARG(task), (int) noreply, rpc, len);
368: }
1.7 misho 369:
1.18 misho 370: /* send RPC reply */
371: if (!noreply)
372: schedWrite(TASK_ROOT(task), cbProto[s->srv_proto][CB_TXPACKET],
373: TASK_ARG(task), TASK_FD(task), rpc, len);
1.7 misho 374:
375: /* lets get next packet */
1.18 misho 376: schedReadSelf(task);
1.7 misho 377: return NULL;
378: }
379:
1.1 misho 380: static void *
1.10 misho 381: acceptClients(sched_task_t *task)
1.1 misho 382: {
1.10 misho 383: rpc_srv_t *srv = TASK_ARG(task);
384: rpc_cli_t *c = NULL;
1.14 misho 385: socklen_t salen = sizeof(sockaddr_t);
1.7 misho 386:
1.13 misho 387: c = _allocClient(srv, NULL);
388: if (!c)
1.10 misho 389: goto end;
390:
391: /* accept client */
392: c->cli_sock = accept(TASK_FD(task), &c->cli_sa.sa, &salen);
393: if (c->cli_sock == -1) {
394: LOGERR;
395: AIT_FREE_VAL(&c->cli_buf);
1.14 misho 396: array_Del(srv->srv_clients, c->cli_id, 42);
1.10 misho 397: goto end;
1.1 misho 398: } else
1.10 misho 399: fcntl(c->cli_sock, F_SETFL, fcntl(c->cli_sock, F_GETFL) | O_NONBLOCK);
1.1 misho 400:
1.13 misho 401: schedRead(TASK_ROOT(task), cbProto[srv->srv_proto][CB_RXPACKET], c,
402: c->cli_sock, NULL, 0);
1.10 misho 403: end:
404: schedReadSelf(task);
405: return NULL;
406: }
1.5 misho 407:
1.7 misho 408:
1.10 misho 409: static void *
1.13 misho 410: txUDPPacket(sched_task_t *task)
1.10 misho 411: {
412: rpc_cli_t *c = TASK_ARG(task);
413: rpc_srv_t *s = c->cli_parent;
1.13 misho 414: rpc_func_t *f = NULL;
1.18 misho 415: u_char *buf = AIT_GET_BUF(&c->cli_buf);
1.13 misho 416: struct tagRPCCall *rpc = (struct tagRPCCall*) buf;
417: int ret, wlen = sizeof(struct tagRPCCall);
418: struct timespec ts = { DEF_RPC_TIMEOUT, 0 };
419:
420: schedCancelby(TASK_ROOT(task), taskTIMER, CRITERIA_DATA, TASK_ARG(task), NULL);
421: schedTimer(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
422: TASK_ARG(task), ts, TASK_ARG(task), 0);
423:
424: if (rpc->call_argc) {
425: f = rpc_srv_getCall(s, ntohs(rpc->call_tag));
426: if (!f) {
427: rpc_SetErr(EPROGUNAVAIL, "Function not found at RPC server");
428: rpc->call_argc ^= rpc->call_argc;
429: rpc->call_rep.ret = RPC_ERROR(-1);
430: rpc->call_rep.eno = RPC_ERROR(rpc_Errno);
431: } else {
1.14 misho 432: rpc->call_argc = htons(array_Size(RPC_RETVARS(c)));
1.13 misho 433: /* Go Encapsulate variables */
1.18 misho 434: ret = ait_vars2buffer(buf + wlen, AIT_LEN(&c->cli_buf) - wlen,
435: RPC_RETVARS(c));
1.13 misho 436: /* Free return values */
1.14 misho 437: ait_freeVars(&c->cli_vars);
1.13 misho 438: if (ret == -1) {
439: rpc_SetErr(EBADRPC, "Prepare RPC packet failed");
440: rpc->call_argc ^= rpc->call_argc;
441: rpc->call_rep.ret = RPC_ERROR(-1);
442: rpc->call_rep.eno = RPC_ERROR(rpc_Errno);
443: } else
444: wlen += ret;
445: }
446: }
1.7 misho 447:
1.18 misho 448: rpc->call_len = htonl(wlen);
1.7 misho 449:
1.13 misho 450: /* calculate CRC */
451: rpc->call_crc ^= rpc->call_crc;
452: rpc->call_crc = htons(crcFletcher16((u_short*) buf, wlen / 2));
453:
454: /* send reply */
455: ret = sendto(TASK_FD(task), buf, wlen, MSG_NOSIGNAL,
456: &c->cli_sa.sa, c->cli_sa.sa.sa_len);
457: if (ret == -1 || ret != wlen) {
458: /* close connection */
459: schedEvent(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
460: TASK_ARG(task), 0, NULL, 0);
461: }
462:
463: return NULL;
464: }
465:
466: static void *
467: rxUDPPacket(sched_task_t *task)
468: {
469: rpc_srv_t *srv = TASK_ARG(task);
470: rpc_cli_t *c = NULL;
471: int len, rlen, noreply;
472: u_short crc, off = 0;
473: u_char buf[USHRT_MAX + 1];
474: struct tagRPCCall *rpc = (struct tagRPCCall*) buf;
1.14 misho 475: sockaddr_t sa;
476: socklen_t salen;
1.13 misho 477: struct timespec ts = { DEF_RPC_TIMEOUT, 0 };
478:
479: /* receive connect packet */
1.14 misho 480: salen = sa.ss.ss_len = sizeof(sockaddr_t);
1.13 misho 481: rlen = recvfrom(TASK_FD(task), buf, sizeof buf, 0, &sa.sa, &salen);
482: if (rlen < 1)
483: goto end;
484:
485: c = _allocClient(srv, &sa);
486: if (!c)
487: goto end;
488: else {
489: c->cli_sock = TASK_FD(task);
490: memcpy(&c->cli_sa, &sa, sizeof c->cli_sa);
491: memcpy(AIT_GET_BUF(&c->cli_buf), buf, AIT_LEN(&c->cli_buf));
492: /* armed timer for close stateless connection */
493: schedCancelby(TASK_ROOT(task), taskTIMER, CRITERIA_DATA, c, NULL);
494: schedTimer(TASK_ROOT(task), cbProto[srv->srv_proto][CB_CLOSECLIENT],
495: c, ts, c, 0);
496: }
497:
498: do {
499: /* check RPC packet */
500: if (rlen < sizeof(struct tagRPCCall)) {
501: rpc_SetErr(ERPCMISMATCH, "Short RPC packet");
502: break;
503: } else
504: rpc = (struct tagRPCCall*) (AIT_GET_BUF(&c->cli_buf) + off);
505:
1.18 misho 506: len = ntohl(rpc->call_len);
1.13 misho 507: rlen -= len;
508:
509: /* check RPC packet lengths */
510: if (rlen < 0 || len < sizeof(struct tagRPCCall)) {
511: rpc_SetErr(ERPCMISMATCH, "Broken RPC packet length");
512: /* skip entire packet */
513: break;
514: }
515:
516: /* check integrity of packet */
517: crc = ntohs(rpc->call_crc);
518: rpc->call_crc ^= rpc->call_crc;
519: if (crc != crcFletcher16((u_short*) rpc, len / 2)) {
520: rpc_SetErr(ERPCMISMATCH, "Bad CRC RPC packet");
521:
522: off += len;
523: /* try next packet remaining into buffer */
524: continue;
525: }
526:
527: noreply = RPC_CHK_NOREPLY(rpc);
528:
529: /* check RPC packet session info */
530: if (rpc_chkPktSession(&rpc->call_session, &srv->srv_session)) {
531: rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session");
1.18 misho 532:
1.13 misho 533: rpc->call_argc ^= rpc->call_argc;
534: rpc->call_rep.ret = RPC_ERROR(-1);
535: rpc->call_rep.eno = RPC_ERROR(errno);
536: } else {
537: /* execute RPC call */
1.18 misho 538: schedEvent(TASK_ROOT(task), execCall, c,
539: (int) noreply, rpc, len);
1.13 misho 540: }
541:
542: /* send RPC reply */
543: if (!noreply)
544: schedWrite(TASK_ROOT(task), cbProto[srv->srv_proto][CB_TXPACKET],
545: c, TASK_FD(task), rpc, len);
546:
547: off += len;
548: } while (rlen > 0);
549:
550: end:
551: schedReadSelf(task);
552: return NULL;
553: }
554:
555: /* ------------------------------------------------------ */
556:
1.16 misho 557: void
1.13 misho 558: rpc_freeBLOBCli(rpc_cli_t * __restrict c)
559: {
560: rpc_srv_t *s = c->cli_parent;
561:
562: schedCancelby(s->srv_blob.root, taskMAX, CRITERIA_ARG, c, NULL);
1.10 misho 563:
564: /* free buffer */
565: AIT_FREE_VAL(&c->cli_buf);
566:
1.14 misho 567: array_Del(s->srv_blob.clients, c->cli_id, 0);
1.10 misho 568: if (c)
1.14 misho 569: e_free(c);
1.13 misho 570: }
571:
572:
573: static void *
574: closeBLOBClient(sched_task_t *task)
575: {
576: int sock = ((rpc_cli_t*) TASK_ARG(task))->cli_sock;
577:
578: rpc_freeBLOBCli(TASK_ARG(task));
579:
580: /* close client socket */
581: shutdown(sock, SHUT_RDWR);
582: close(sock);
1.7 misho 583: return NULL;
584: }
585:
586: static void *
587: txBLOB(sched_task_t *task)
588: {
1.10 misho 589: rpc_cli_t *c = TASK_ARG(task);
590: u_char *buf = AIT_GET_BUF(&c->cli_buf);
1.7 misho 591: int wlen = sizeof(struct tagBLOBHdr);
592:
593: /* send reply */
1.10 misho 594: wlen = send(TASK_FD(task), buf, wlen, MSG_NOSIGNAL);
595: if (wlen == -1 || wlen != sizeof(struct tagBLOBHdr)) {
596: /* close blob connection */
597: schedEvent(TASK_ROOT(task), closeBLOBClient, c, 42, NULL, 0);
598: }
1.7 misho 599:
600: return NULL;
601: }
1.4 misho 602:
1.7 misho 603: static void *
604: rxBLOB(sched_task_t *task)
605: {
606: rpc_cli_t *c = TASK_ARG(task);
607: rpc_srv_t *s = c->cli_parent;
608: rpc_blob_t *b;
1.10 misho 609: struct tagBLOBHdr blob;
1.7 misho 610: int rlen;
611:
1.10 misho 612: memset(&blob, 0, sizeof blob);
613: rlen = recv(TASK_FD(task), &blob, sizeof blob, 0);
614: if (rlen < 1) {
615: /* close blob connection */
616: schedEvent(TASK_ROOT(task), closeBLOBClient, c, 42, NULL, 0);
1.7 misho 617: return NULL;
618: }
1.5 misho 619:
1.10 misho 620: /* check BLOB packet */
621: if (rlen < sizeof(struct tagBLOBHdr)) {
622: rpc_SetErr(ERPCMISMATCH, "Short BLOB packet");
1.6 misho 623:
1.10 misho 624: schedReadSelf(task);
1.7 misho 625: return NULL;
626: }
1.1 misho 627:
1.7 misho 628: /* check RPC packet session info */
1.15 misho 629: if (rpc_chkPktSession(&blob.hdr_session, &s->srv_session)) {
1.7 misho 630: rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session");
1.10 misho 631: blob.hdr_cmd = error;
1.7 misho 632: goto end;
633: }
634:
635: /* Go to proceed packet ... */
1.10 misho 636: switch (blob.hdr_cmd) {
1.7 misho 637: case get:
1.10 misho 638: if (!(b = rpc_srv_getBLOB(s, ntohl(blob.hdr_var)))) {
639: rpc_SetErr(EINVAL, "Var=%x not found", ntohl(blob.hdr_var));
640: blob.hdr_cmd = no;
641: blob.hdr_ret = RPC_ERROR(-1);
1.7 misho 642: break;
643: } else
1.10 misho 644: blob.hdr_len = htonl(b->blob_len);
1.5 misho 645:
1.7 misho 646: if (rpc_srv_blobMap(s, b) != -1) {
647: /* deliver BLOB variable to client */
1.10 misho 648: blob.hdr_ret = htonl(rpc_srv_sendBLOB(c, b));
1.7 misho 649: rpc_srv_blobUnmap(b);
650: } else {
1.10 misho 651: blob.hdr_cmd = error;
652: blob.hdr_ret = RPC_ERROR(-1);
1.7 misho 653: }
654: break;
655: case set:
1.17 misho 656: if ((b = rpc_srv_registerBLOB(s, ntohl(blob.hdr_len),
657: ntohl(blob.hdr_ret)))) {
1.7 misho 658: /* set new BLOB variable for reply :) */
1.10 misho 659: blob.hdr_var = htonl(b->blob_var);
1.7 misho 660:
661: /* receive BLOB from client */
1.10 misho 662: blob.hdr_ret = htonl(rpc_srv_recvBLOB(c, b));
1.7 misho 663: rpc_srv_blobUnmap(b);
1.5 misho 664: } else {
1.10 misho 665: blob.hdr_cmd = error;
666: blob.hdr_ret = RPC_ERROR(-1);
1.7 misho 667: }
668: break;
669: case unset:
1.11 misho 670: if (rpc_srv_unregisterBLOB(s, ntohl(blob.hdr_var)) == -1) {
1.10 misho 671: blob.hdr_cmd = error;
672: blob.hdr_ret = RPC_ERROR(-1);
1.1 misho 673: }
674: break;
1.7 misho 675: default:
1.10 misho 676: rpc_SetErr(EPROCUNAVAIL, "Unsupported BLOB command %d", blob.hdr_cmd);
677: blob.hdr_cmd = error;
678: blob.hdr_ret = RPC_ERROR(-1);
1.7 misho 679: }
1.1 misho 680:
1.7 misho 681: end:
1.10 misho 682: memcpy(AIT_ADDR(&c->cli_buf), &blob, sizeof blob);
683: schedWrite(TASK_ROOT(task), txBLOB, TASK_ARG(task), TASK_FD(task), NULL, 0);
684: schedReadSelf(task);
1.7 misho 685: return NULL;
1.2 misho 686: }
687:
688: static void *
1.17 misho 689: flushBLOB(sched_task_t *task)
690: {
691: rpc_srv_t *srv = TASK_ARG(task);
692: rpc_blob_t *b, *tmp;
693:
694: TAILQ_FOREACH_SAFE(b, &srv->srv_blob.blobs, blob_node, tmp) {
695: TAILQ_REMOVE(&srv->srv_blob.blobs, b, blob_node);
696:
697: rpc_srv_blobFree(srv, b);
698: e_free(b);
699: }
700:
701: schedSignalSelf(task);
702: return NULL;
703: }
704:
705: static void *
1.10 misho 706: acceptBLOBClients(sched_task_t *task)
1.2 misho 707: {
1.10 misho 708: rpc_srv_t *srv = TASK_ARG(task);
709: rpc_cli_t *c = NULL;
710: register int i;
1.14 misho 711: socklen_t salen = sizeof(sockaddr_t);
1.12 misho 712: #ifdef TCP_NOPUSH
713: int n = 1;
714: #endif
1.7 misho 715:
1.10 misho 716: /* check free slots for connect */
1.14 misho 717: for (i = 0; i < array_Size(srv->srv_blob.clients) &&
718: (c = array(srv->srv_blob.clients, i, rpc_cli_t*)); i++);
1.10 misho 719: if (c) /* no more free slots! */
720: goto end;
1.14 misho 721: c = e_malloc(sizeof(rpc_cli_t));
1.10 misho 722: if (!c) {
1.7 misho 723: LOGERR;
1.10 misho 724: srv->srv_kill = srv->srv_blob.kill = 1;
1.7 misho 725: return NULL;
726: } else {
1.10 misho 727: memset(c, 0, sizeof(rpc_cli_t));
1.14 misho 728: array_Set(srv->srv_blob.clients, i, c);
1.10 misho 729: c->cli_id = i;
730: c->cli_parent = srv;
1.7 misho 731: }
1.4 misho 732:
1.10 misho 733: /* alloc empty buffer */
1.14 misho 734: AIT_SET_BUFSIZ(&c->cli_buf, 0, srv->srv_netbuf);
1.2 misho 735:
1.10 misho 736: /* accept client */
737: c->cli_sock = accept(TASK_FD(task), &c->cli_sa.sa, &salen);
738: if (c->cli_sock == -1) {
739: LOGERR;
740: AIT_FREE_VAL(&c->cli_buf);
1.14 misho 741: array_Del(srv->srv_blob.clients, i, 42);
1.10 misho 742: goto end;
1.12 misho 743: } else {
744: #ifdef TCP_NOPUSH
745: setsockopt(c->cli_sock, IPPROTO_TCP, TCP_NOPUSH, &n, sizeof n);
746: #endif
1.10 misho 747: fcntl(c->cli_sock, F_SETFL, fcntl(c->cli_sock, F_GETFL) | O_NONBLOCK);
1.12 misho 748: }
1.2 misho 749:
1.10 misho 750: schedRead(TASK_ROOT(task), rxBLOB, c, c->cli_sock, NULL, 0);
751: end:
752: schedReadSelf(task);
1.7 misho 753: return NULL;
1.1 misho 754: }
755:
1.10 misho 756: /* ------------------------------------------------------ */
1.1 misho 757:
758: /*
1.7 misho 759: * rpc_srv_initBLOBServer() - Init & create BLOB Server
760: *
1.4 misho 761: * @srv = RPC server instance
1.2 misho 762: * @Port = Port for bind server, if Port == 0 default port is selected
763: * @diskDir = Disk place for BLOB file objects
764: * return: -1 == error or 0 bind and created BLOB server instance
765: */
766: int
767: rpc_srv_initBLOBServer(rpc_srv_t * __restrict srv, u_short Port, const char *diskDir)
768: {
769: int n = 1;
770:
1.10 misho 771: if (!srv || srv->srv_kill) {
1.7 misho 772: rpc_SetErr(EINVAL, "Invalid parameters can`t init BLOB server");
1.2 misho 773: return -1;
774: }
775:
776: memset(&srv->srv_blob, 0, sizeof srv->srv_blob);
777: if (access(diskDir, R_OK | W_OK) == -1) {
778: LOGERR;
779: return -1;
780: } else
1.7 misho 781: AIT_SET_STR(&srv->srv_blob.dir, diskDir);
1.2 misho 782:
1.10 misho 783: /* init blob list */
784: TAILQ_INIT(&srv->srv_blob.blobs);
785:
1.2 misho 786: srv->srv_blob.server.cli_parent = srv;
1.4 misho 787:
1.14 misho 788: memcpy(&srv->srv_blob.server.cli_sa, &srv->srv_server.cli_sa, sizeof(sockaddr_t));
1.10 misho 789: switch (srv->srv_blob.server.cli_sa.sa.sa_family) {
1.4 misho 790: case AF_INET:
1.10 misho 791: srv->srv_blob.server.cli_sa.sin.sin_port =
792: htons(Port ? Port : ntohs(srv->srv_blob.server.cli_sa.sin.sin_port) + 1);
1.4 misho 793: break;
794: case AF_INET6:
1.10 misho 795: srv->srv_blob.server.cli_sa.sin6.sin6_port =
796: htons(Port ? Port : ntohs(srv->srv_blob.server.cli_sa.sin6.sin6_port) + 1);
1.4 misho 797: break;
798: case AF_LOCAL:
1.10 misho 799: strlcat(srv->srv_blob.server.cli_sa.sun.sun_path, ".blob",
800: sizeof srv->srv_blob.server.cli_sa.sun.sun_path);
1.4 misho 801: break;
802: default:
1.7 misho 803: AIT_FREE_VAL(&srv->srv_blob.dir);
1.4 misho 804: return -1;
1.2 misho 805: }
806:
1.4 misho 807: /* create BLOB server socket */
1.6 misho 808: srv->srv_blob.server.cli_sock = socket(srv->srv_server.cli_sa.sa.sa_family, SOCK_STREAM, 0);
1.2 misho 809: if (srv->srv_blob.server.cli_sock == -1) {
810: LOGERR;
1.7 misho 811: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 812: return -1;
813: }
814: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
815: LOGERR;
816: close(srv->srv_blob.server.cli_sock);
1.7 misho 817: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 818: return -1;
819: }
1.5 misho 820: n = srv->srv_netbuf;
821: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof n) == -1) {
822: LOGERR;
823: close(srv->srv_blob.server.cli_sock);
1.7 misho 824: AIT_FREE_VAL(&srv->srv_blob.dir);
1.5 misho 825: return -1;
826: }
827: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_RCVBUF, &n, sizeof n) == -1) {
828: LOGERR;
829: close(srv->srv_blob.server.cli_sock);
1.7 misho 830: AIT_FREE_VAL(&srv->srv_blob.dir);
1.5 misho 831: return -1;
832: }
1.6 misho 833: if (bind(srv->srv_blob.server.cli_sock, &srv->srv_blob.server.cli_sa.sa,
834: srv->srv_blob.server.cli_sa.sa.sa_len) == -1) {
1.2 misho 835: LOGERR;
836: close(srv->srv_blob.server.cli_sock);
1.7 misho 837: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 838: return -1;
1.13 misho 839: } else
840: fcntl(srv->srv_blob.server.cli_sock, F_SETFL,
841: fcntl(srv->srv_blob.server.cli_sock, F_GETFL) | O_NONBLOCK);
842:
1.2 misho 843:
1.10 misho 844: /* allocate pool for concurent blob clients */
1.14 misho 845: srv->srv_blob.clients = array_Init(array_Size(srv->srv_clients));
1.2 misho 846: if (!srv->srv_blob.clients) {
1.14 misho 847: rpc_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
1.2 misho 848: close(srv->srv_blob.server.cli_sock);
1.7 misho 849: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 850: return -1;
1.10 misho 851: }
1.2 misho 852:
1.10 misho 853: /* init blob scheduler */
854: srv->srv_blob.root = schedBegin();
855: if (!srv->srv_blob.root) {
856: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
1.14 misho 857: array_Destroy(&srv->srv_blob.clients);
1.10 misho 858: close(srv->srv_blob.server.cli_sock);
859: AIT_FREE_VAL(&srv->srv_blob.dir);
860: return -1;
861: }
1.2 misho 862:
863: return 0;
864: }
865:
866: /*
1.7 misho 867: * rpc_srv_endBLOBServer() - Destroy BLOB server, close all opened sockets and free resources
868: *
1.2 misho 869: * @srv = RPC Server instance
870: * return: none
871: */
1.16 misho 872: void
1.2 misho 873: rpc_srv_endBLOBServer(rpc_srv_t * __restrict srv)
874: {
1.10 misho 875: if (!srv)
1.2 misho 876: return;
877:
1.10 misho 878: srv->srv_blob.kill = 1;
1.17 misho 879:
880: schedEnd(&srv->srv_blob.root);
1.2 misho 881: }
882:
883: /*
1.10 misho 884: * rpc_srv_loopBLOBServer() - Execute Main BLOB server loop and wait for clients requests
1.7 misho 885: *
1.2 misho 886: * @srv = RPC Server instance
887: * return: -1 error or 0 ok, infinite loop ...
888: */
889: int
1.10 misho 890: rpc_srv_loopBLOBServer(rpc_srv_t * __restrict srv)
1.2 misho 891: {
1.10 misho 892: rpc_cli_t *c;
1.2 misho 893: register int i;
1.10 misho 894: rpc_blob_t *b, *tmp;
895: struct timespec ts = { RPC_SCHED_POLLING, 0 };
1.2 misho 896:
1.10 misho 897: if (!srv || srv->srv_kill) {
1.7 misho 898: rpc_SetErr(EINVAL, "Invalid parameter can`t start BLOB server");
1.2 misho 899: return -1;
900: }
901:
1.14 misho 902: if (listen(srv->srv_blob.server.cli_sock, array_Size(srv->srv_blob.clients)) == -1) {
1.2 misho 903: LOGERR;
904: return -1;
1.10 misho 905: }
906:
1.17 misho 907: schedSignal(srv->srv_blob.root, flushBLOB, srv, SIGFBLOB, NULL, 0);
1.10 misho 908: if (!schedRead(srv->srv_blob.root, acceptBLOBClients, srv,
909: srv->srv_blob.server.cli_sock, NULL, 0)) {
910: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
911: return -1;
912: }
1.2 misho 913:
1.10 misho 914: schedPolling(srv->srv_blob.root, &ts, NULL);
915: /* main rpc loop */
916: schedRun(srv->srv_blob.root, &srv->srv_blob.kill);
1.7 misho 917:
1.17 misho 918: /* detach blobs */
919: TAILQ_FOREACH_SAFE(b, &srv->srv_blob.blobs, blob_node, tmp) {
920: TAILQ_REMOVE(&srv->srv_blob.blobs, b, blob_node);
921:
922: rpc_srv_blobFree(srv, b);
923: e_free(b);
924: }
925:
1.10 misho 926: /* close all clients connections & server socket */
1.14 misho 927: for (i = 0; i < array_Size(srv->srv_blob.clients); i++) {
928: c = array(srv->srv_blob.clients, i, rpc_cli_t*);
1.10 misho 929: if (c) {
930: shutdown(c->cli_sock, SHUT_RDWR);
931: close(c->cli_sock);
932:
933: schedCancelby(srv->srv_blob.root, taskMAX, CRITERIA_ARG, c, NULL);
934: AIT_FREE_VAL(&c->cli_buf);
1.2 misho 935: }
1.14 misho 936: array_Del(srv->srv_blob.clients, i, 42);
1.10 misho 937: }
1.14 misho 938: array_Destroy(&srv->srv_blob.clients);
1.2 misho 939:
1.10 misho 940: close(srv->srv_blob.server.cli_sock);
1.2 misho 941:
1.10 misho 942: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 943: return 0;
944: }
945:
946:
947: /*
1.7 misho 948: * rpc_srv_initServer() - Init & create RPC Server
949: *
1.15 misho 950: * @InstID = Instance for authentication & recognition
1.1 misho 951: * @concurentClients = Concurent clients at same time to this server
1.10 misho 952: * @netBuf = Network buffer length (min:512 bytes), if =0 == BUFSIZ (also meaning max RPC packet)
1.4 misho 953: * @csHost = Host name or address for bind server, if NULL any address
1.1 misho 954: * @Port = Port for bind server, if Port == 0 default port is selected
1.13 misho 955: * @proto = Protocol, if == 0 choose SOCK_STREAM
1.1 misho 956: * return: NULL == error or !=NULL bind and created RPC server instance
957: */
958: rpc_srv_t *
1.15 misho 959: rpc_srv_initServer(u_char InstID, int concurentClients, int netBuf,
960: const char *csHost, u_short Port, int proto)
1.1 misho 961: {
1.10 misho 962: int n = 1;
1.1 misho 963: rpc_srv_t *srv = NULL;
1.14 misho 964: sockaddr_t sa = E_SOCKADDR_INIT;
1.1 misho 965:
1.15 misho 966: if (!concurentClients || (proto < 0 || proto > SOCK_DGRAM)) {
1.10 misho 967: rpc_SetErr(EINVAL, "Invalid parameters can`t init RPC server");
1.1 misho 968: return NULL;
969: }
1.14 misho 970: if (!e_gethostbyname(csHost, Port, &sa))
1.10 misho 971: return NULL;
1.1 misho 972: if (!Port)
973: Port = RPC_DEFPORT;
1.13 misho 974: if (!proto)
975: proto = SOCK_STREAM;
1.10 misho 976: if (netBuf < RPC_MIN_BUFSIZ)
1.5 misho 977: netBuf = BUFSIZ;
1.7 misho 978: else
1.14 misho 979: netBuf = E_ALIGN(netBuf, 2); /* align netBuf length */
1.10 misho 980:
981: #ifdef HAVE_SRANDOMDEV
982: srandomdev();
983: #else
984: time_t tim;
985:
986: srandom((time(&tim) ^ getpid()));
987: #endif
1.1 misho 988:
1.14 misho 989: srv = e_malloc(sizeof(rpc_srv_t));
1.1 misho 990: if (!srv) {
991: LOGERR;
992: return NULL;
993: } else
994: memset(srv, 0, sizeof(rpc_srv_t));
995:
1.13 misho 996: srv->srv_proto = proto;
1.5 misho 997: srv->srv_netbuf = netBuf;
1.1 misho 998: srv->srv_session.sess_version = RPC_VERSION;
1.15 misho 999: srv->srv_session.sess_instance = InstID;
1.1 misho 1000:
1001: srv->srv_server.cli_parent = srv;
1.10 misho 1002: memcpy(&srv->srv_server.cli_sa, &sa, sizeof srv->srv_server.cli_sa);
1003:
1.12 misho 1004: /* init functions */
1005: pthread_mutex_init(&srv->srv_funcs.mtx, NULL);
1006: SLIST_INIT(&srv->srv_funcs);
1007: AVL_INIT(&srv->srv_funcs);
1.10 misho 1008:
1009: /* init scheduler */
1010: srv->srv_root = schedBegin();
1011: if (!srv->srv_root) {
1012: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
1.12 misho 1013: pthread_mutex_destroy(&srv->srv_funcs.mtx);
1.14 misho 1014: e_free(srv);
1.10 misho 1015: return NULL;
1016: }
1017:
1018: /* init pool for clients */
1.14 misho 1019: srv->srv_clients = array_Init(concurentClients);
1.10 misho 1020: if (!srv->srv_clients) {
1.14 misho 1021: rpc_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
1.10 misho 1022: schedEnd(&srv->srv_root);
1.12 misho 1023: pthread_mutex_destroy(&srv->srv_funcs.mtx);
1.14 misho 1024: e_free(srv);
1.10 misho 1025: return NULL;
1026: }
1.4 misho 1027:
1028: /* create server socket */
1.13 misho 1029: srv->srv_server.cli_sock = socket(srv->srv_server.cli_sa.sa.sa_family, srv->srv_proto, 0);
1.1 misho 1030: if (srv->srv_server.cli_sock == -1) {
1031: LOGERR;
1.14 misho 1032: array_Destroy(&srv->srv_clients);
1.10 misho 1033: schedEnd(&srv->srv_root);
1.12 misho 1034: pthread_mutex_destroy(&srv->srv_funcs.mtx);
1.14 misho 1035: e_free(srv);
1.1 misho 1036: return NULL;
1037: }
1038: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
1039: LOGERR;
1.10 misho 1040: goto err;
1.1 misho 1041: }
1.5 misho 1042: n = srv->srv_netbuf;
1043: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof n) == -1) {
1044: LOGERR;
1.10 misho 1045: goto err;
1.5 misho 1046: }
1047: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_RCVBUF, &n, sizeof n) == -1) {
1048: LOGERR;
1.10 misho 1049: goto err;
1.5 misho 1050: }
1.6 misho 1051: if (bind(srv->srv_server.cli_sock, &srv->srv_server.cli_sa.sa,
1052: srv->srv_server.cli_sa.sa.sa_len) == -1) {
1.1 misho 1053: LOGERR;
1.10 misho 1054: goto err;
1.13 misho 1055: } else
1056: fcntl(srv->srv_server.cli_sock, F_SETFL,
1057: fcntl(srv->srv_server.cli_sock, F_GETFL) | O_NONBLOCK);
1.1 misho 1058:
1.10 misho 1059: rpc_register_srvPing(srv);
1.8 misho 1060:
1.1 misho 1061: return srv;
1.10 misho 1062: err: /* error condition */
1063: close(srv->srv_server.cli_sock);
1.14 misho 1064: array_Destroy(&srv->srv_clients);
1.10 misho 1065: schedEnd(&srv->srv_root);
1.12 misho 1066: pthread_mutex_destroy(&srv->srv_funcs.mtx);
1.14 misho 1067: e_free(srv);
1.10 misho 1068: return NULL;
1.1 misho 1069: }
1070:
1071: /*
1.7 misho 1072: * rpc_srv_endServer() - Destroy RPC server, close all opened sockets and free resources
1073: *
1.6 misho 1074: * @psrv = RPC Server instance
1.1 misho 1075: * return: none
1076: */
1.16 misho 1077: void
1.6 misho 1078: rpc_srv_endServer(rpc_srv_t ** __restrict psrv)
1.1 misho 1079: {
1.10 misho 1080: if (!psrv || !*psrv)
1.1 misho 1081: return;
1082:
1.10 misho 1083: /* if send kill to blob server */
1.17 misho 1084: rpc_srv_endBLOBServer(*psrv);
1.1 misho 1085:
1.10 misho 1086: (*psrv)->srv_kill = 1;
1087: sleep(RPC_SCHED_POLLING);
1.2 misho 1088:
1.17 misho 1089: schedEnd(&(*psrv)->srv_root);
1090:
1.12 misho 1091: pthread_mutex_destroy(&(*psrv)->srv_funcs.mtx);
1.14 misho 1092: e_free(*psrv);
1.6 misho 1093: *psrv = NULL;
1.1 misho 1094: }
1095:
1096: /*
1.7 misho 1097: * rpc_srv_loopServer() - Execute Main server loop and wait for clients requests
1098: *
1.1 misho 1099: * @srv = RPC Server instance
1100: * return: -1 error or 0 ok, infinite loop ...
1101: */
1102: int
1.5 misho 1103: rpc_srv_loopServer(rpc_srv_t * __restrict srv)
1.1 misho 1104: {
1.10 misho 1105: rpc_cli_t *c;
1.1 misho 1106: register int i;
1.12 misho 1107: rpc_func_t *f;
1.10 misho 1108: struct timespec ts = { RPC_SCHED_POLLING, 0 };
1.1 misho 1109:
1110: if (!srv) {
1.10 misho 1111: rpc_SetErr(EINVAL, "Invalid parameter can`t start RPC server");
1.1 misho 1112: return -1;
1113: }
1114:
1.13 misho 1115: if (srv->srv_proto == SOCK_STREAM)
1.14 misho 1116: if (listen(srv->srv_server.cli_sock, array_Size(srv->srv_clients)) == -1) {
1.13 misho 1117: LOGERR;
1118: return -1;
1119: }
1.1 misho 1120:
1.13 misho 1121: if (!schedRead(srv->srv_root, cbProto[srv->srv_proto][CB_ACCEPTCLIENT], srv,
1122: srv->srv_server.cli_sock, NULL, 0)) {
1.10 misho 1123: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
1124: return -1;
1125: }
1.7 misho 1126:
1.10 misho 1127: schedPolling(srv->srv_root, &ts, NULL);
1.7 misho 1128: /* main rpc loop */
1.10 misho 1129: schedRun(srv->srv_root, &srv->srv_kill);
1130:
1131: /* close all clients connections & server socket */
1.14 misho 1132: for (i = 0; i < array_Size(srv->srv_clients); i++) {
1133: c = array(srv->srv_clients, i, rpc_cli_t*);
1.10 misho 1134: if (c) {
1135: shutdown(c->cli_sock, SHUT_RDWR);
1136: close(c->cli_sock);
1137:
1138: schedCancelby(srv->srv_root, taskMAX, CRITERIA_ARG, c, NULL);
1.14 misho 1139: ait_freeVars(&RPC_RETVARS(c));
1.10 misho 1140: AIT_FREE_VAL(&c->cli_buf);
1.1 misho 1141: }
1.14 misho 1142: array_Del(srv->srv_clients, i, 42);
1.10 misho 1143: }
1.14 misho 1144: array_Destroy(&srv->srv_clients);
1.2 misho 1145:
1.10 misho 1146: close(srv->srv_server.cli_sock);
1.2 misho 1147:
1.10 misho 1148: /* detach exported calls */
1.12 misho 1149: RPC_FUNCS_LOCK(&srv->srv_funcs);
1150: while ((f = SLIST_FIRST(&srv->srv_funcs))) {
1151: SLIST_REMOVE_HEAD(&srv->srv_funcs, func_next);
1.1 misho 1152:
1.10 misho 1153: AIT_FREE_VAL(&f->func_name);
1.14 misho 1154: e_free(f);
1.1 misho 1155: }
1.12 misho 1156: srv->srv_funcs.avlh_root = NULL;
1157: RPC_FUNCS_UNLOCK(&srv->srv_funcs);
1.1 misho 1158:
1159: return 0;
1160: }
1161:
1162:
1163: /*
1164: * rpc_srv_execCall() Execute registered call from RPC server
1.7 misho 1165: *
1.10 misho 1166: * @cli = RPC client
1.1 misho 1167: * @rpc = IN RPC call structure
1.10 misho 1168: * @funcname = Execute RPC function
1.5 misho 1169: * @args = IN RPC calling arguments from RPC client
1.1 misho 1170: * return: -1 error, !=-1 ok
1171: */
1172: int
1.10 misho 1173: rpc_srv_execCall(rpc_cli_t * __restrict cli, struct tagRPCCall * __restrict rpc,
1174: ait_val_t funcname, array_t * __restrict args)
1.1 misho 1175: {
1176: rpc_callback_t func;
1177:
1.10 misho 1178: if (!cli || !rpc || !AIT_ADDR(&funcname)) {
1.7 misho 1179: rpc_SetErr(EINVAL, "Invalid parameter can`t exec function");
1.1 misho 1180: return -1;
1181: }
1182:
1.10 misho 1183: func = AIT_GET_LIKE(&funcname, rpc_callback_t);
1184: return func(cli, rpc, args);
1.1 misho 1185: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>