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