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