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