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