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