Annotation of libaitrpc/src/srv.c, revision 1.14.2.1
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.14.2.1! misho 6: * $Id: srv.c,v 1.14 2013/03/07 23:10:50 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:
70: inline void
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.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.14 misho 179: rpc->call_argc = htons(array_Size(RPC_RETVARS(c)));
1.7 misho 180: /* Go Encapsulate variables */
1.14 misho 181: ret = ait_vars2buffer(buf + wlen, sizeof buf - wlen, RPC_RETVARS(c));
1.10 misho 182: /* Free return values */
1.14 misho 183: ait_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.13 misho 204: schedEvent(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
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.14 misho 224: arr = ait_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) {
1.14 misho 228: rpc_SetErr(ERPCMISMATCH, "#%d - %s", elwix_GetErrno(), elwix_GetError());
1.7 misho 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.14 misho 253: ait_freeVars(&c->cli_vars);
1.8 misho 254: rpc->call_argc ^= rpc->call_argc;
1.10 misho 255: } else {
256: /* reply */
1.14 misho 257: rpc->call_argc = htons(array_Size(RPC_RETVARS(c)));
1.10 misho 258: }
1.7 misho 259: }
260: }
261:
1.14 misho 262: array_Destroy(&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.13 misho 281: schedEvent(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
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.13 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;
1.14 misho 353: socklen_t salen = sizeof(sockaddr_t);
1.7 misho 354:
1.13 misho 355: c = _allocClient(srv, NULL);
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.14 misho 364: array_Del(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.13 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.7 misho 376:
1.10 misho 377: static void *
1.13 misho 378: txUDPPacket(sched_task_t *task)
1.10 misho 379: {
380: rpc_cli_t *c = TASK_ARG(task);
381: rpc_srv_t *s = c->cli_parent;
1.13 misho 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:
388: schedCancelby(TASK_ROOT(task), taskTIMER, CRITERIA_DATA, TASK_ARG(task), NULL);
389: schedTimer(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
390: TASK_ARG(task), ts, TASK_ARG(task), 0);
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 {
1.14 misho 403: rpc->call_argc = htons(array_Size(RPC_RETVARS(c)));
1.13 misho 404: /* Go Encapsulate variables */
1.14 misho 405: ret = ait_vars2buffer(buf + wlen, sizeof buf - wlen, RPC_RETVARS(c));
1.13 misho 406: /* Free return values */
1.14 misho 407: ait_freeVars(&c->cli_vars);
1.13 misho 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: }
1.7 misho 417:
1.13 misho 418: rpc->call_len = htons(wlen);
1.7 misho 419:
1.13 misho 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],
430: TASK_ARG(task), 0, NULL, 0);
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: int len, rlen, noreply;
442: u_short crc, off = 0;
443: u_char buf[USHRT_MAX + 1];
444: struct tagRPCCall *rpc = (struct tagRPCCall*) buf;
1.14 misho 445: sockaddr_t sa;
446: socklen_t salen;
1.13 misho 447: struct timespec ts = { DEF_RPC_TIMEOUT, 0 };
448:
449: /* receive connect packet */
1.14 misho 450: salen = sa.ss.ss_len = sizeof(sockaddr_t);
1.13 misho 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 {
459: c->cli_sock = TASK_FD(task);
460: memcpy(&c->cli_sa, &sa, sizeof c->cli_sa);
461: memcpy(AIT_GET_BUF(&c->cli_buf), buf, AIT_LEN(&c->cli_buf));
462: /* armed timer for close stateless connection */
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 */
507: schedEvent(TASK_ROOT(task), execCall, c, off, NULL, 0);
508: }
509:
510: /* send RPC reply */
511: if (!noreply)
512: schedWrite(TASK_ROOT(task), cbProto[srv->srv_proto][CB_TXPACKET],
513: c, TASK_FD(task), rpc, len);
514:
515: off += len;
516: } while (rlen > 0);
517:
518: end:
519: schedReadSelf(task);
520: return NULL;
521: }
522:
523: /* ------------------------------------------------------ */
524:
525: inline void
526: rpc_freeBLOBCli(rpc_cli_t * __restrict c)
527: {
528: rpc_srv_t *s = c->cli_parent;
529:
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:
1.14 misho 535: array_Del(s->srv_blob.clients, c->cli_id, 0);
1.10 misho 536: if (c)
1.14 misho 537: e_free(c);
1.13 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: int wlen = sizeof(struct tagBLOBHdr);
560:
561: /* send reply */
1.10 misho 562: wlen = send(TASK_FD(task), buf, wlen, MSG_NOSIGNAL);
563: if (wlen == -1 || wlen != sizeof(struct tagBLOBHdr)) {
564: /* close blob connection */
565: schedEvent(TASK_ROOT(task), closeBLOBClient, c, 42, NULL, 0);
566: }
1.7 misho 567:
568: return NULL;
569: }
1.4 misho 570:
1.7 misho 571: static void *
572: rxBLOB(sched_task_t *task)
573: {
574: rpc_cli_t *c = TASK_ARG(task);
575: rpc_srv_t *s = c->cli_parent;
576: rpc_blob_t *b;
1.10 misho 577: struct tagBLOBHdr blob;
1.7 misho 578: int rlen;
579: u_short crc;
580:
1.10 misho 581: memset(&blob, 0, sizeof blob);
582: rlen = recv(TASK_FD(task), &blob, sizeof blob, 0);
583: if (rlen < 1) {
584: /* close blob connection */
585: schedEvent(TASK_ROOT(task), closeBLOBClient, c, 42, NULL, 0);
1.7 misho 586: return NULL;
587: }
1.5 misho 588:
1.10 misho 589: /* check BLOB packet */
590: if (rlen < sizeof(struct tagBLOBHdr)) {
591: rpc_SetErr(ERPCMISMATCH, "Short BLOB packet");
1.6 misho 592:
1.10 misho 593: schedReadSelf(task);
1.7 misho 594: return NULL;
595: }
1.1 misho 596:
1.7 misho 597: /* check RPC packet session info */
1.10 misho 598: if ((crc = rpc_chkPktSession(&blob.hdr_session, &s->srv_session))) {
1.7 misho 599: rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session");
1.10 misho 600: blob.hdr_cmd = error;
1.7 misho 601: goto end;
602: }
603:
604: /* Go to proceed packet ... */
1.10 misho 605: switch (blob.hdr_cmd) {
1.7 misho 606: case get:
1.10 misho 607: if (!(b = rpc_srv_getBLOB(s, ntohl(blob.hdr_var)))) {
608: rpc_SetErr(EINVAL, "Var=%x not found", ntohl(blob.hdr_var));
609: blob.hdr_cmd = no;
610: blob.hdr_ret = RPC_ERROR(-1);
1.7 misho 611: break;
612: } else
1.10 misho 613: blob.hdr_len = htonl(b->blob_len);
1.5 misho 614:
1.7 misho 615: if (rpc_srv_blobMap(s, b) != -1) {
616: /* deliver BLOB variable to client */
1.10 misho 617: blob.hdr_ret = htonl(rpc_srv_sendBLOB(c, b));
1.7 misho 618: rpc_srv_blobUnmap(b);
619: } else {
1.10 misho 620: blob.hdr_cmd = error;
621: blob.hdr_ret = RPC_ERROR(-1);
1.7 misho 622: }
623: break;
624: case set:
1.10 misho 625: if ((b = rpc_srv_registerBLOB(s, ntohl(blob.hdr_len)))) {
1.7 misho 626: /* set new BLOB variable for reply :) */
1.10 misho 627: blob.hdr_var = htonl(b->blob_var);
1.7 misho 628:
629: /* receive BLOB from client */
1.10 misho 630: blob.hdr_ret = htonl(rpc_srv_recvBLOB(c, b));
1.7 misho 631: rpc_srv_blobUnmap(b);
1.5 misho 632: } else {
1.10 misho 633: blob.hdr_cmd = error;
634: blob.hdr_ret = RPC_ERROR(-1);
1.7 misho 635: }
636: break;
637: case unset:
1.11 misho 638: if (rpc_srv_unregisterBLOB(s, ntohl(blob.hdr_var)) == -1) {
1.10 misho 639: blob.hdr_cmd = error;
640: blob.hdr_ret = RPC_ERROR(-1);
1.1 misho 641: }
642: break;
1.7 misho 643: default:
1.10 misho 644: rpc_SetErr(EPROCUNAVAIL, "Unsupported BLOB command %d", blob.hdr_cmd);
645: blob.hdr_cmd = error;
646: blob.hdr_ret = RPC_ERROR(-1);
1.7 misho 647: }
1.1 misho 648:
1.7 misho 649: end:
1.10 misho 650: memcpy(AIT_ADDR(&c->cli_buf), &blob, sizeof blob);
651: schedWrite(TASK_ROOT(task), txBLOB, TASK_ARG(task), TASK_FD(task), NULL, 0);
652: schedReadSelf(task);
1.7 misho 653: return NULL;
1.2 misho 654: }
655:
656: static void *
1.10 misho 657: acceptBLOBClients(sched_task_t *task)
1.2 misho 658: {
1.10 misho 659: rpc_srv_t *srv = TASK_ARG(task);
660: rpc_cli_t *c = NULL;
661: register int i;
1.14 misho 662: socklen_t salen = sizeof(sockaddr_t);
1.12 misho 663: #ifdef TCP_NOPUSH
664: int n = 1;
665: #endif
1.7 misho 666:
1.10 misho 667: /* check free slots for connect */
1.14 misho 668: for (i = 0; i < array_Size(srv->srv_blob.clients) &&
669: (c = array(srv->srv_blob.clients, i, rpc_cli_t*)); i++);
1.10 misho 670: if (c) /* no more free slots! */
671: goto end;
1.14 misho 672: c = e_malloc(sizeof(rpc_cli_t));
1.10 misho 673: if (!c) {
1.7 misho 674: LOGERR;
1.10 misho 675: srv->srv_kill = srv->srv_blob.kill = 1;
1.7 misho 676: return NULL;
677: } else {
1.10 misho 678: memset(c, 0, sizeof(rpc_cli_t));
1.14 misho 679: array_Set(srv->srv_blob.clients, i, c);
1.10 misho 680: c->cli_id = i;
681: c->cli_parent = srv;
1.7 misho 682: }
1.4 misho 683:
1.10 misho 684: /* alloc empty buffer */
1.14 misho 685: AIT_SET_BUFSIZ(&c->cli_buf, 0, srv->srv_netbuf);
1.2 misho 686:
1.10 misho 687: /* accept client */
688: c->cli_sock = accept(TASK_FD(task), &c->cli_sa.sa, &salen);
689: if (c->cli_sock == -1) {
690: LOGERR;
691: AIT_FREE_VAL(&c->cli_buf);
1.14 misho 692: array_Del(srv->srv_blob.clients, i, 42);
1.10 misho 693: goto end;
1.12 misho 694: } else {
695: #ifdef TCP_NOPUSH
696: setsockopt(c->cli_sock, IPPROTO_TCP, TCP_NOPUSH, &n, sizeof n);
697: #endif
1.10 misho 698: fcntl(c->cli_sock, F_SETFL, fcntl(c->cli_sock, F_GETFL) | O_NONBLOCK);
1.12 misho 699: }
1.2 misho 700:
1.10 misho 701: schedRead(TASK_ROOT(task), rxBLOB, c, c->cli_sock, NULL, 0);
702: end:
703: schedReadSelf(task);
1.7 misho 704: return NULL;
1.1 misho 705: }
706:
1.10 misho 707: /* ------------------------------------------------------ */
1.1 misho 708:
709: /*
1.7 misho 710: * rpc_srv_initBLOBServer() - Init & create BLOB Server
711: *
1.4 misho 712: * @srv = RPC server instance
1.2 misho 713: * @Port = Port for bind server, if Port == 0 default port is selected
714: * @diskDir = Disk place for BLOB file objects
715: * return: -1 == error or 0 bind and created BLOB server instance
716: */
717: int
718: rpc_srv_initBLOBServer(rpc_srv_t * __restrict srv, u_short Port, const char *diskDir)
719: {
720: int n = 1;
721:
1.10 misho 722: if (!srv || srv->srv_kill) {
1.7 misho 723: rpc_SetErr(EINVAL, "Invalid parameters can`t init BLOB server");
1.2 misho 724: return -1;
725: }
726:
727: memset(&srv->srv_blob, 0, sizeof srv->srv_blob);
728: if (access(diskDir, R_OK | W_OK) == -1) {
729: LOGERR;
730: return -1;
731: } else
1.7 misho 732: AIT_SET_STR(&srv->srv_blob.dir, diskDir);
1.2 misho 733:
1.10 misho 734: /* init blob list */
735: TAILQ_INIT(&srv->srv_blob.blobs);
736:
1.2 misho 737: srv->srv_blob.server.cli_parent = srv;
1.4 misho 738:
1.14 misho 739: memcpy(&srv->srv_blob.server.cli_sa, &srv->srv_server.cli_sa, sizeof(sockaddr_t));
1.10 misho 740: switch (srv->srv_blob.server.cli_sa.sa.sa_family) {
1.4 misho 741: case AF_INET:
1.10 misho 742: srv->srv_blob.server.cli_sa.sin.sin_port =
743: htons(Port ? Port : ntohs(srv->srv_blob.server.cli_sa.sin.sin_port) + 1);
1.4 misho 744: break;
745: case AF_INET6:
1.10 misho 746: srv->srv_blob.server.cli_sa.sin6.sin6_port =
747: htons(Port ? Port : ntohs(srv->srv_blob.server.cli_sa.sin6.sin6_port) + 1);
1.4 misho 748: break;
749: case AF_LOCAL:
1.10 misho 750: strlcat(srv->srv_blob.server.cli_sa.sun.sun_path, ".blob",
751: sizeof srv->srv_blob.server.cli_sa.sun.sun_path);
1.4 misho 752: break;
753: default:
1.7 misho 754: AIT_FREE_VAL(&srv->srv_blob.dir);
1.4 misho 755: return -1;
1.2 misho 756: }
757:
1.4 misho 758: /* create BLOB server socket */
1.6 misho 759: srv->srv_blob.server.cli_sock = socket(srv->srv_server.cli_sa.sa.sa_family, SOCK_STREAM, 0);
1.2 misho 760: if (srv->srv_blob.server.cli_sock == -1) {
761: LOGERR;
1.7 misho 762: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 763: return -1;
764: }
765: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
766: LOGERR;
767: close(srv->srv_blob.server.cli_sock);
1.7 misho 768: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 769: return -1;
770: }
1.5 misho 771: n = srv->srv_netbuf;
772: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof n) == -1) {
773: LOGERR;
774: close(srv->srv_blob.server.cli_sock);
1.7 misho 775: AIT_FREE_VAL(&srv->srv_blob.dir);
1.5 misho 776: return -1;
777: }
778: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_RCVBUF, &n, sizeof n) == -1) {
779: LOGERR;
780: close(srv->srv_blob.server.cli_sock);
1.7 misho 781: AIT_FREE_VAL(&srv->srv_blob.dir);
1.5 misho 782: return -1;
783: }
1.6 misho 784: if (bind(srv->srv_blob.server.cli_sock, &srv->srv_blob.server.cli_sa.sa,
785: srv->srv_blob.server.cli_sa.sa.sa_len) == -1) {
1.2 misho 786: LOGERR;
787: close(srv->srv_blob.server.cli_sock);
1.7 misho 788: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 789: return -1;
1.13 misho 790: } else
791: fcntl(srv->srv_blob.server.cli_sock, F_SETFL,
792: fcntl(srv->srv_blob.server.cli_sock, F_GETFL) | O_NONBLOCK);
793:
1.2 misho 794:
1.10 misho 795: /* allocate pool for concurent blob clients */
1.14 misho 796: srv->srv_blob.clients = array_Init(array_Size(srv->srv_clients));
1.2 misho 797: if (!srv->srv_blob.clients) {
1.14 misho 798: rpc_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
1.2 misho 799: close(srv->srv_blob.server.cli_sock);
1.7 misho 800: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 801: return -1;
1.10 misho 802: }
1.2 misho 803:
1.10 misho 804: /* init blob scheduler */
805: srv->srv_blob.root = schedBegin();
806: if (!srv->srv_blob.root) {
807: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
1.14 misho 808: array_Destroy(&srv->srv_blob.clients);
1.10 misho 809: close(srv->srv_blob.server.cli_sock);
810: AIT_FREE_VAL(&srv->srv_blob.dir);
811: return -1;
812: }
1.2 misho 813:
814: return 0;
815: }
816:
817: /*
1.7 misho 818: * rpc_srv_endBLOBServer() - Destroy BLOB server, close all opened sockets and free resources
819: *
1.2 misho 820: * @srv = RPC Server instance
821: * return: none
822: */
1.10 misho 823: inline void
1.2 misho 824: rpc_srv_endBLOBServer(rpc_srv_t * __restrict srv)
825: {
1.10 misho 826: if (!srv)
1.2 misho 827: return;
828:
1.10 misho 829: srv->srv_blob.kill = 1;
1.2 misho 830: }
831:
832: /*
1.10 misho 833: * rpc_srv_loopBLOBServer() - Execute Main BLOB server loop and wait for clients requests
1.7 misho 834: *
1.2 misho 835: * @srv = RPC Server instance
836: * return: -1 error or 0 ok, infinite loop ...
837: */
838: int
1.10 misho 839: rpc_srv_loopBLOBServer(rpc_srv_t * __restrict srv)
1.2 misho 840: {
1.10 misho 841: rpc_cli_t *c;
1.2 misho 842: register int i;
1.10 misho 843: rpc_blob_t *b, *tmp;
844: struct timespec ts = { RPC_SCHED_POLLING, 0 };
1.2 misho 845:
1.10 misho 846: if (!srv || srv->srv_kill) {
1.7 misho 847: rpc_SetErr(EINVAL, "Invalid parameter can`t start BLOB server");
1.2 misho 848: return -1;
849: }
850:
1.14 misho 851: if (listen(srv->srv_blob.server.cli_sock, array_Size(srv->srv_blob.clients)) == -1) {
1.2 misho 852: LOGERR;
853: return -1;
1.10 misho 854: }
855:
856: if (!schedRead(srv->srv_blob.root, acceptBLOBClients, srv,
857: srv->srv_blob.server.cli_sock, NULL, 0)) {
858: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
859: return -1;
860: }
1.2 misho 861:
1.10 misho 862: schedPolling(srv->srv_blob.root, &ts, NULL);
863: /* main rpc loop */
864: schedRun(srv->srv_blob.root, &srv->srv_blob.kill);
1.7 misho 865:
1.10 misho 866: /* close all clients connections & server socket */
1.14 misho 867: for (i = 0; i < array_Size(srv->srv_blob.clients); i++) {
868: c = array(srv->srv_blob.clients, i, rpc_cli_t*);
1.10 misho 869: if (c) {
870: shutdown(c->cli_sock, SHUT_RDWR);
871: close(c->cli_sock);
872:
873: schedCancelby(srv->srv_blob.root, taskMAX, CRITERIA_ARG, c, NULL);
874: AIT_FREE_VAL(&c->cli_buf);
1.2 misho 875: }
1.14 misho 876: array_Del(srv->srv_blob.clients, i, 42);
1.10 misho 877: }
1.14 misho 878: array_Destroy(&srv->srv_blob.clients);
1.2 misho 879:
1.10 misho 880: close(srv->srv_blob.server.cli_sock);
1.2 misho 881:
1.10 misho 882: /* detach blobs */
883: TAILQ_FOREACH_SAFE(b, &srv->srv_blob.blobs, blob_node, tmp) {
884: TAILQ_REMOVE(&srv->srv_blob.blobs, b, blob_node);
1.2 misho 885:
1.10 misho 886: rpc_srv_blobFree(srv, b);
1.14 misho 887: e_free(b);
1.2 misho 888: }
889:
1.10 misho 890: schedEnd(&srv->srv_blob.root);
891: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 892: return 0;
893: }
894:
895:
896: /*
1.7 misho 897: * rpc_srv_initServer() - Init & create RPC Server
898: *
1.14.2.1! misho 899: * @InstID = Instance for authentication & recognition
1.1 misho 900: * @concurentClients = Concurent clients at same time to this server
1.10 misho 901: * @netBuf = Network buffer length (min:512 bytes), if =0 == BUFSIZ (also meaning max RPC packet)
1.4 misho 902: * @csHost = Host name or address for bind server, if NULL any address
1.1 misho 903: * @Port = Port for bind server, if Port == 0 default port is selected
1.13 misho 904: * @proto = Protocol, if == 0 choose SOCK_STREAM
1.1 misho 905: * return: NULL == error or !=NULL bind and created RPC server instance
906: */
907: rpc_srv_t *
1.14.2.1! misho 908: rpc_srv_initServer(u_char InstID, int concurentClients, int netBuf,
! 909: const char *csHost, u_short Port, int proto)
1.1 misho 910: {
1.10 misho 911: int n = 1;
1.1 misho 912: rpc_srv_t *srv = NULL;
1.14 misho 913: sockaddr_t sa = E_SOCKADDR_INIT;
1.1 misho 914:
1.14.2.1! misho 915: if (!concurentClients || (proto < 0 || proto > SOCK_DGRAM)) {
1.10 misho 916: rpc_SetErr(EINVAL, "Invalid parameters can`t init RPC server");
1.1 misho 917: return NULL;
918: }
1.14 misho 919: if (!e_gethostbyname(csHost, Port, &sa))
1.10 misho 920: return NULL;
1.1 misho 921: if (!Port)
922: Port = RPC_DEFPORT;
1.13 misho 923: if (!proto)
924: proto = SOCK_STREAM;
1.10 misho 925: if (netBuf < RPC_MIN_BUFSIZ)
1.5 misho 926: netBuf = BUFSIZ;
1.7 misho 927: else
1.14 misho 928: netBuf = E_ALIGN(netBuf, 2); /* align netBuf length */
1.10 misho 929:
930: #ifdef HAVE_SRANDOMDEV
931: srandomdev();
932: #else
933: time_t tim;
934:
935: srandom((time(&tim) ^ getpid()));
936: #endif
1.1 misho 937:
1.14 misho 938: srv = e_malloc(sizeof(rpc_srv_t));
1.1 misho 939: if (!srv) {
940: LOGERR;
941: return NULL;
942: } else
943: memset(srv, 0, sizeof(rpc_srv_t));
944:
1.13 misho 945: srv->srv_proto = proto;
1.5 misho 946: srv->srv_netbuf = netBuf;
1.1 misho 947: srv->srv_session.sess_version = RPC_VERSION;
1.14.2.1! misho 948: srv->srv_session.sess_instance = InstID;
1.1 misho 949:
950: srv->srv_server.cli_parent = srv;
1.10 misho 951: memcpy(&srv->srv_server.cli_sa, &sa, sizeof srv->srv_server.cli_sa);
952:
1.12 misho 953: /* init functions */
954: pthread_mutex_init(&srv->srv_funcs.mtx, NULL);
955: SLIST_INIT(&srv->srv_funcs);
956: AVL_INIT(&srv->srv_funcs);
1.10 misho 957:
958: /* init scheduler */
959: srv->srv_root = schedBegin();
960: if (!srv->srv_root) {
961: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
1.12 misho 962: pthread_mutex_destroy(&srv->srv_funcs.mtx);
1.14 misho 963: e_free(srv);
1.10 misho 964: return NULL;
965: }
966:
967: /* init pool for clients */
1.14 misho 968: srv->srv_clients = array_Init(concurentClients);
1.10 misho 969: if (!srv->srv_clients) {
1.14 misho 970: rpc_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
1.10 misho 971: schedEnd(&srv->srv_root);
1.12 misho 972: pthread_mutex_destroy(&srv->srv_funcs.mtx);
1.14 misho 973: e_free(srv);
1.10 misho 974: return NULL;
975: }
1.4 misho 976:
977: /* create server socket */
1.13 misho 978: srv->srv_server.cli_sock = socket(srv->srv_server.cli_sa.sa.sa_family, srv->srv_proto, 0);
1.1 misho 979: if (srv->srv_server.cli_sock == -1) {
980: LOGERR;
1.14 misho 981: array_Destroy(&srv->srv_clients);
1.10 misho 982: schedEnd(&srv->srv_root);
1.12 misho 983: pthread_mutex_destroy(&srv->srv_funcs.mtx);
1.14 misho 984: e_free(srv);
1.1 misho 985: return NULL;
986: }
987: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
988: LOGERR;
1.10 misho 989: goto err;
1.1 misho 990: }
1.5 misho 991: n = srv->srv_netbuf;
992: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof n) == -1) {
993: LOGERR;
1.10 misho 994: goto err;
1.5 misho 995: }
996: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_RCVBUF, &n, sizeof n) == -1) {
997: LOGERR;
1.10 misho 998: goto err;
1.5 misho 999: }
1.6 misho 1000: if (bind(srv->srv_server.cli_sock, &srv->srv_server.cli_sa.sa,
1001: srv->srv_server.cli_sa.sa.sa_len) == -1) {
1.1 misho 1002: LOGERR;
1.10 misho 1003: goto err;
1.13 misho 1004: } else
1005: fcntl(srv->srv_server.cli_sock, F_SETFL,
1006: fcntl(srv->srv_server.cli_sock, F_GETFL) | O_NONBLOCK);
1.1 misho 1007:
1.10 misho 1008: rpc_register_srvPing(srv);
1.8 misho 1009:
1.1 misho 1010: return srv;
1.10 misho 1011: err: /* error condition */
1012: close(srv->srv_server.cli_sock);
1.14 misho 1013: array_Destroy(&srv->srv_clients);
1.10 misho 1014: schedEnd(&srv->srv_root);
1.12 misho 1015: pthread_mutex_destroy(&srv->srv_funcs.mtx);
1.14 misho 1016: e_free(srv);
1.10 misho 1017: return NULL;
1.1 misho 1018: }
1019:
1020: /*
1.7 misho 1021: * rpc_srv_endServer() - Destroy RPC server, close all opened sockets and free resources
1022: *
1.6 misho 1023: * @psrv = RPC Server instance
1.1 misho 1024: * return: none
1025: */
1.10 misho 1026: inline void
1.6 misho 1027: rpc_srv_endServer(rpc_srv_t ** __restrict psrv)
1.1 misho 1028: {
1.10 misho 1029: if (!psrv || !*psrv)
1.1 misho 1030: return;
1031:
1.10 misho 1032: /* if send kill to blob server */
1033: if (!(*psrv)->srv_blob.kill)
1034: rpc_srv_endBLOBServer(*psrv);
1.1 misho 1035:
1.10 misho 1036: (*psrv)->srv_kill = 1;
1037: sleep(RPC_SCHED_POLLING);
1.2 misho 1038:
1.12 misho 1039: pthread_mutex_destroy(&(*psrv)->srv_funcs.mtx);
1.14 misho 1040: e_free(*psrv);
1.6 misho 1041: *psrv = NULL;
1.1 misho 1042: }
1043:
1044: /*
1.7 misho 1045: * rpc_srv_loopServer() - Execute Main server loop and wait for clients requests
1046: *
1.1 misho 1047: * @srv = RPC Server instance
1048: * return: -1 error or 0 ok, infinite loop ...
1049: */
1050: int
1.5 misho 1051: rpc_srv_loopServer(rpc_srv_t * __restrict srv)
1.1 misho 1052: {
1.10 misho 1053: rpc_cli_t *c;
1.1 misho 1054: register int i;
1.12 misho 1055: rpc_func_t *f;
1.10 misho 1056: struct timespec ts = { RPC_SCHED_POLLING, 0 };
1.1 misho 1057:
1058: if (!srv) {
1.10 misho 1059: rpc_SetErr(EINVAL, "Invalid parameter can`t start RPC server");
1.1 misho 1060: return -1;
1061: }
1062:
1.13 misho 1063: if (srv->srv_proto == SOCK_STREAM)
1.14 misho 1064: if (listen(srv->srv_server.cli_sock, array_Size(srv->srv_clients)) == -1) {
1.13 misho 1065: LOGERR;
1066: return -1;
1067: }
1.1 misho 1068:
1.13 misho 1069: if (!schedRead(srv->srv_root, cbProto[srv->srv_proto][CB_ACCEPTCLIENT], srv,
1070: srv->srv_server.cli_sock, NULL, 0)) {
1.10 misho 1071: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
1072: return -1;
1073: }
1.7 misho 1074:
1.10 misho 1075: schedPolling(srv->srv_root, &ts, NULL);
1.7 misho 1076: /* main rpc loop */
1.10 misho 1077: schedRun(srv->srv_root, &srv->srv_kill);
1078:
1079: /* close all clients connections & server socket */
1.14 misho 1080: for (i = 0; i < array_Size(srv->srv_clients); i++) {
1081: c = array(srv->srv_clients, i, rpc_cli_t*);
1.10 misho 1082: if (c) {
1083: shutdown(c->cli_sock, SHUT_RDWR);
1084: close(c->cli_sock);
1085:
1086: schedCancelby(srv->srv_root, taskMAX, CRITERIA_ARG, c, NULL);
1.14 misho 1087: ait_freeVars(&RPC_RETVARS(c));
1.10 misho 1088: AIT_FREE_VAL(&c->cli_buf);
1.1 misho 1089: }
1.14 misho 1090: array_Del(srv->srv_clients, i, 42);
1.10 misho 1091: }
1.14 misho 1092: array_Destroy(&srv->srv_clients);
1.2 misho 1093:
1.10 misho 1094: close(srv->srv_server.cli_sock);
1.2 misho 1095:
1.10 misho 1096: /* detach exported calls */
1.12 misho 1097: RPC_FUNCS_LOCK(&srv->srv_funcs);
1098: while ((f = SLIST_FIRST(&srv->srv_funcs))) {
1099: SLIST_REMOVE_HEAD(&srv->srv_funcs, func_next);
1.1 misho 1100:
1.10 misho 1101: AIT_FREE_VAL(&f->func_name);
1.14 misho 1102: e_free(f);
1.1 misho 1103: }
1.12 misho 1104: srv->srv_funcs.avlh_root = NULL;
1105: RPC_FUNCS_UNLOCK(&srv->srv_funcs);
1.1 misho 1106:
1.10 misho 1107: schedEnd(&srv->srv_root);
1.1 misho 1108: return 0;
1109: }
1110:
1111:
1112: /*
1113: * rpc_srv_execCall() Execute registered call from RPC server
1.7 misho 1114: *
1.10 misho 1115: * @cli = RPC client
1.1 misho 1116: * @rpc = IN RPC call structure
1.10 misho 1117: * @funcname = Execute RPC function
1.5 misho 1118: * @args = IN RPC calling arguments from RPC client
1.1 misho 1119: * return: -1 error, !=-1 ok
1120: */
1121: int
1.10 misho 1122: rpc_srv_execCall(rpc_cli_t * __restrict cli, struct tagRPCCall * __restrict rpc,
1123: ait_val_t funcname, array_t * __restrict args)
1.1 misho 1124: {
1125: rpc_callback_t func;
1126:
1.10 misho 1127: if (!cli || !rpc || !AIT_ADDR(&funcname)) {
1.7 misho 1128: rpc_SetErr(EINVAL, "Invalid parameter can`t exec function");
1.1 misho 1129: return -1;
1130: }
1131:
1.10 misho 1132: func = AIT_GET_LIKE(&funcname, rpc_callback_t);
1133: return func(cli, rpc, args);
1.1 misho 1134: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>