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