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