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