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