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