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