Annotation of libaitrpc/src/srv.c, revision 1.12.2.9
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.9! misho 6: * $Id: srv.c,v 1.12.2.8 2012/11/19 15:48:45 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.12.2.9! misho 528: inline void
! 529: rpc_freeBLOBCli(rpc_cli_t * __restrict c)
1.10 misho 530: {
531: rpc_srv_t *s = c->cli_parent;
1.7 misho 532:
1.12.2.9! misho 533: schedCancelby(s->srv_blob.root, taskMAX, CRITERIA_ARG, c, NULL);
1.10 misho 534:
535: /* free buffer */
536: AIT_FREE_VAL(&c->cli_buf);
537:
538: io_arrayDel(s->srv_blob.clients, c->cli_id, 0);
539: if (c)
1.11 misho 540: io_free(c);
1.12.2.9! misho 541: }
! 542:
! 543:
! 544: static void *
! 545: closeBLOBClient(sched_task_t *task)
! 546: {
! 547: int sock = ((rpc_cli_t*) TASK_ARG(task))->cli_sock;
! 548:
! 549: rpc_freeBLOBCli(TASK_ARG(task));
! 550:
! 551: /* close client socket */
! 552: shutdown(sock, SHUT_RDWR);
! 553: close(sock);
1.7 misho 554: return NULL;
555: }
556:
557: static void *
558: txBLOB(sched_task_t *task)
559: {
1.10 misho 560: rpc_cli_t *c = TASK_ARG(task);
561: u_char *buf = AIT_GET_BUF(&c->cli_buf);
1.7 misho 562: struct tagBLOBHdr *blob = (struct tagBLOBHdr *) buf;
563: int wlen = sizeof(struct tagBLOBHdr);
564:
565: /* calculate CRC */
566: blob->hdr_crc ^= blob->hdr_crc;
1.8 misho 567: blob->hdr_crc = htons(crcFletcher16((u_short*) buf, wlen / 2));
1.7 misho 568:
569: /* send reply */
1.10 misho 570: wlen = send(TASK_FD(task), buf, wlen, MSG_NOSIGNAL);
571: if (wlen == -1 || wlen != sizeof(struct tagBLOBHdr)) {
572: /* close blob connection */
573: schedEvent(TASK_ROOT(task), closeBLOBClient, c, 42, NULL, 0);
574: }
1.7 misho 575:
576: return NULL;
577: }
1.4 misho 578:
1.7 misho 579: static void *
580: rxBLOB(sched_task_t *task)
581: {
582: rpc_cli_t *c = TASK_ARG(task);
583: rpc_srv_t *s = c->cli_parent;
584: rpc_blob_t *b;
1.10 misho 585: struct tagBLOBHdr blob;
1.7 misho 586: int rlen;
587: u_short crc;
588:
1.10 misho 589: memset(&blob, 0, sizeof blob);
590: rlen = recv(TASK_FD(task), &blob, sizeof blob, 0);
591: if (rlen < 1) {
592: /* close blob connection */
593: schedEvent(TASK_ROOT(task), closeBLOBClient, c, 42, NULL, 0);
1.7 misho 594: return NULL;
595: }
1.5 misho 596:
1.10 misho 597: /* check BLOB packet */
598: if (rlen < sizeof(struct tagBLOBHdr)) {
599: rpc_SetErr(ERPCMISMATCH, "Short BLOB packet");
1.6 misho 600:
1.10 misho 601: schedReadSelf(task);
1.7 misho 602: return NULL;
603: }
1.1 misho 604:
1.7 misho 605: /* check integrity of packet */
1.10 misho 606: crc = ntohs(blob.hdr_crc);
607: blob.hdr_crc ^= blob.hdr_crc;
608: if (crc != crcFletcher16((u_short*) &blob, rlen / 2)) {
1.7 misho 609: rpc_SetErr(ERPCMISMATCH, "Bad CRC BLOB packet");
1.10 misho 610:
611: schedReadSelf(task);
1.7 misho 612: return NULL;
613: }
1.5 misho 614:
1.7 misho 615: /* check RPC packet session info */
1.10 misho 616: if ((crc = rpc_chkPktSession(&blob.hdr_session, &s->srv_session))) {
1.7 misho 617: rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session");
1.10 misho 618: blob.hdr_cmd = error;
1.7 misho 619: goto end;
620: }
621:
622: /* Go to proceed packet ... */
1.10 misho 623: switch (blob.hdr_cmd) {
1.7 misho 624: case get:
1.10 misho 625: if (!(b = rpc_srv_getBLOB(s, ntohl(blob.hdr_var)))) {
626: rpc_SetErr(EINVAL, "Var=%x not found", ntohl(blob.hdr_var));
627: blob.hdr_cmd = no;
628: blob.hdr_ret = RPC_ERROR(-1);
1.7 misho 629: break;
630: } else
1.10 misho 631: blob.hdr_len = htonl(b->blob_len);
1.5 misho 632:
1.7 misho 633: if (rpc_srv_blobMap(s, b) != -1) {
634: /* deliver BLOB variable to client */
1.10 misho 635: blob.hdr_ret = htonl(rpc_srv_sendBLOB(c, b));
1.7 misho 636: rpc_srv_blobUnmap(b);
637: } else {
1.10 misho 638: blob.hdr_cmd = error;
639: blob.hdr_ret = RPC_ERROR(-1);
1.7 misho 640: }
641: break;
642: case set:
1.10 misho 643: if ((b = rpc_srv_registerBLOB(s, ntohl(blob.hdr_len)))) {
1.7 misho 644: /* set new BLOB variable for reply :) */
1.10 misho 645: blob.hdr_var = htonl(b->blob_var);
1.7 misho 646:
647: /* receive BLOB from client */
1.10 misho 648: blob.hdr_ret = htonl(rpc_srv_recvBLOB(c, b));
1.7 misho 649: rpc_srv_blobUnmap(b);
1.5 misho 650: } else {
1.10 misho 651: blob.hdr_cmd = error;
652: blob.hdr_ret = RPC_ERROR(-1);
1.7 misho 653: }
654: break;
655: case unset:
1.11 misho 656: if (rpc_srv_unregisterBLOB(s, ntohl(blob.hdr_var)) == -1) {
1.10 misho 657: blob.hdr_cmd = error;
658: blob.hdr_ret = RPC_ERROR(-1);
1.1 misho 659: }
660: break;
1.7 misho 661: default:
1.10 misho 662: rpc_SetErr(EPROCUNAVAIL, "Unsupported BLOB command %d", blob.hdr_cmd);
663: blob.hdr_cmd = error;
664: blob.hdr_ret = RPC_ERROR(-1);
1.7 misho 665: }
1.1 misho 666:
1.7 misho 667: end:
1.10 misho 668: memcpy(AIT_ADDR(&c->cli_buf), &blob, sizeof blob);
669: schedWrite(TASK_ROOT(task), txBLOB, TASK_ARG(task), TASK_FD(task), NULL, 0);
670: schedReadSelf(task);
1.7 misho 671: return NULL;
1.2 misho 672: }
673:
674: static void *
1.10 misho 675: acceptBLOBClients(sched_task_t *task)
1.2 misho 676: {
1.10 misho 677: rpc_srv_t *srv = TASK_ARG(task);
678: rpc_cli_t *c = NULL;
679: register int i;
680: socklen_t salen = sizeof(io_sockaddr_t);
1.12 misho 681: #ifdef TCP_NOPUSH
682: int n = 1;
683: #endif
1.7 misho 684:
1.10 misho 685: /* check free slots for connect */
686: for (i = 0; i < io_arraySize(srv->srv_blob.clients) &&
687: (c = io_array(srv->srv_blob.clients, i, rpc_cli_t*)); i++);
688: if (c) /* no more free slots! */
689: goto end;
1.11 misho 690: c = io_malloc(sizeof(rpc_cli_t));
1.10 misho 691: if (!c) {
1.7 misho 692: LOGERR;
1.10 misho 693: srv->srv_kill = srv->srv_blob.kill = 1;
1.7 misho 694: return NULL;
695: } else {
1.10 misho 696: memset(c, 0, sizeof(rpc_cli_t));
697: io_arraySet(srv->srv_blob.clients, i, c);
698: c->cli_id = i;
699: c->cli_parent = srv;
1.7 misho 700: }
1.4 misho 701:
1.10 misho 702: /* alloc empty buffer */
703: AIT_SET_BUF2(&c->cli_buf, 0, srv->srv_netbuf);
1.2 misho 704:
1.10 misho 705: /* accept client */
706: c->cli_sock = accept(TASK_FD(task), &c->cli_sa.sa, &salen);
707: if (c->cli_sock == -1) {
708: LOGERR;
709: AIT_FREE_VAL(&c->cli_buf);
710: io_arrayDel(srv->srv_blob.clients, i, 42);
711: goto end;
1.12 misho 712: } else {
713: #ifdef TCP_NOPUSH
714: setsockopt(c->cli_sock, IPPROTO_TCP, TCP_NOPUSH, &n, sizeof n);
715: #endif
1.10 misho 716: fcntl(c->cli_sock, F_SETFL, fcntl(c->cli_sock, F_GETFL) | O_NONBLOCK);
1.12 misho 717: }
1.2 misho 718:
1.10 misho 719: schedRead(TASK_ROOT(task), rxBLOB, c, c->cli_sock, NULL, 0);
720: end:
721: schedReadSelf(task);
1.7 misho 722: return NULL;
1.1 misho 723: }
724:
1.10 misho 725: /* ------------------------------------------------------ */
1.1 misho 726:
727: /*
1.7 misho 728: * rpc_srv_initBLOBServer() - Init & create BLOB Server
729: *
1.4 misho 730: * @srv = RPC server instance
1.2 misho 731: * @Port = Port for bind server, if Port == 0 default port is selected
732: * @diskDir = Disk place for BLOB file objects
733: * return: -1 == error or 0 bind and created BLOB server instance
734: */
735: int
736: rpc_srv_initBLOBServer(rpc_srv_t * __restrict srv, u_short Port, const char *diskDir)
737: {
738: int n = 1;
739:
1.10 misho 740: if (!srv || srv->srv_kill) {
1.7 misho 741: rpc_SetErr(EINVAL, "Invalid parameters can`t init BLOB server");
1.2 misho 742: return -1;
743: }
744:
745: memset(&srv->srv_blob, 0, sizeof srv->srv_blob);
746: if (access(diskDir, R_OK | W_OK) == -1) {
747: LOGERR;
748: return -1;
749: } else
1.7 misho 750: AIT_SET_STR(&srv->srv_blob.dir, diskDir);
1.2 misho 751:
1.10 misho 752: /* init blob list */
753: TAILQ_INIT(&srv->srv_blob.blobs);
754:
1.2 misho 755: srv->srv_blob.server.cli_parent = srv;
1.4 misho 756:
1.10 misho 757: memcpy(&srv->srv_blob.server.cli_sa, &srv->srv_server.cli_sa, sizeof(io_sockaddr_t));
758: switch (srv->srv_blob.server.cli_sa.sa.sa_family) {
1.4 misho 759: case AF_INET:
1.10 misho 760: srv->srv_blob.server.cli_sa.sin.sin_port =
761: htons(Port ? Port : ntohs(srv->srv_blob.server.cli_sa.sin.sin_port) + 1);
1.4 misho 762: break;
763: case AF_INET6:
1.10 misho 764: srv->srv_blob.server.cli_sa.sin6.sin6_port =
765: htons(Port ? Port : ntohs(srv->srv_blob.server.cli_sa.sin6.sin6_port) + 1);
1.4 misho 766: break;
767: case AF_LOCAL:
1.10 misho 768: strlcat(srv->srv_blob.server.cli_sa.sun.sun_path, ".blob",
769: sizeof srv->srv_blob.server.cli_sa.sun.sun_path);
1.4 misho 770: break;
771: default:
1.7 misho 772: AIT_FREE_VAL(&srv->srv_blob.dir);
1.4 misho 773: return -1;
1.2 misho 774: }
775:
1.4 misho 776: /* create BLOB server socket */
1.6 misho 777: srv->srv_blob.server.cli_sock = socket(srv->srv_server.cli_sa.sa.sa_family, SOCK_STREAM, 0);
1.2 misho 778: if (srv->srv_blob.server.cli_sock == -1) {
779: LOGERR;
1.7 misho 780: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 781: return -1;
782: }
783: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &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.2 misho 787: return -1;
788: }
1.5 misho 789: n = srv->srv_netbuf;
790: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof n) == -1) {
791: LOGERR;
792: close(srv->srv_blob.server.cli_sock);
1.7 misho 793: AIT_FREE_VAL(&srv->srv_blob.dir);
1.5 misho 794: return -1;
795: }
796: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_RCVBUF, &n, sizeof n) == -1) {
797: LOGERR;
798: close(srv->srv_blob.server.cli_sock);
1.7 misho 799: AIT_FREE_VAL(&srv->srv_blob.dir);
1.5 misho 800: return -1;
801: }
1.6 misho 802: if (bind(srv->srv_blob.server.cli_sock, &srv->srv_blob.server.cli_sa.sa,
803: srv->srv_blob.server.cli_sa.sa.sa_len) == -1) {
1.2 misho 804: LOGERR;
805: close(srv->srv_blob.server.cli_sock);
1.7 misho 806: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 807: return -1;
1.12.2.4 misho 808: } else
809: fcntl(srv->srv_blob.server.cli_sock, F_SETFL,
810: fcntl(srv->srv_blob.server.cli_sock, F_GETFL) | O_NONBLOCK);
811:
1.2 misho 812:
1.10 misho 813: /* allocate pool for concurent blob clients */
814: srv->srv_blob.clients = io_arrayInit(io_arraySize(srv->srv_clients));
1.2 misho 815: if (!srv->srv_blob.clients) {
1.10 misho 816: rpc_SetErr(io_GetErrno(), "%s", io_GetError());
1.2 misho 817: close(srv->srv_blob.server.cli_sock);
1.7 misho 818: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 819: return -1;
1.10 misho 820: }
1.2 misho 821:
1.10 misho 822: /* init blob scheduler */
823: srv->srv_blob.root = schedBegin();
824: if (!srv->srv_blob.root) {
825: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
826: io_arrayDestroy(&srv->srv_blob.clients);
827: close(srv->srv_blob.server.cli_sock);
828: AIT_FREE_VAL(&srv->srv_blob.dir);
829: return -1;
830: }
1.2 misho 831:
832: return 0;
833: }
834:
835: /*
1.7 misho 836: * rpc_srv_endBLOBServer() - Destroy BLOB server, close all opened sockets and free resources
837: *
1.2 misho 838: * @srv = RPC Server instance
839: * return: none
840: */
1.10 misho 841: inline void
1.2 misho 842: rpc_srv_endBLOBServer(rpc_srv_t * __restrict srv)
843: {
1.10 misho 844: if (!srv)
1.2 misho 845: return;
846:
1.10 misho 847: srv->srv_blob.kill = 1;
1.2 misho 848: }
849:
850: /*
1.10 misho 851: * rpc_srv_loopBLOBServer() - Execute Main BLOB server loop and wait for clients requests
1.7 misho 852: *
1.2 misho 853: * @srv = RPC Server instance
854: * return: -1 error or 0 ok, infinite loop ...
855: */
856: int
1.10 misho 857: rpc_srv_loopBLOBServer(rpc_srv_t * __restrict srv)
1.2 misho 858: {
1.10 misho 859: rpc_cli_t *c;
1.2 misho 860: register int i;
1.10 misho 861: rpc_blob_t *b, *tmp;
862: struct timespec ts = { RPC_SCHED_POLLING, 0 };
1.2 misho 863:
1.10 misho 864: if (!srv || srv->srv_kill) {
1.7 misho 865: rpc_SetErr(EINVAL, "Invalid parameter can`t start BLOB server");
1.2 misho 866: return -1;
867: }
868:
1.12.2.7 misho 869: if (listen(srv->srv_blob.server.cli_sock, io_arraySize(srv->srv_blob.clients)) == -1) {
870: LOGERR;
871: return -1;
872: }
1.10 misho 873:
874: if (!schedRead(srv->srv_blob.root, acceptBLOBClients, srv,
875: srv->srv_blob.server.cli_sock, NULL, 0)) {
876: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
877: return -1;
878: }
1.2 misho 879:
1.10 misho 880: schedPolling(srv->srv_blob.root, &ts, NULL);
881: /* main rpc loop */
882: schedRun(srv->srv_blob.root, &srv->srv_blob.kill);
1.7 misho 883:
1.10 misho 884: /* close all clients connections & server socket */
885: for (i = 0; i < io_arraySize(srv->srv_blob.clients); i++) {
886: c = io_array(srv->srv_blob.clients, i, rpc_cli_t*);
887: if (c) {
888: shutdown(c->cli_sock, SHUT_RDWR);
889: close(c->cli_sock);
890:
891: schedCancelby(srv->srv_blob.root, taskMAX, CRITERIA_ARG, c, NULL);
892: AIT_FREE_VAL(&c->cli_buf);
1.2 misho 893: }
1.10 misho 894: io_arrayDel(srv->srv_blob.clients, i, 42);
895: }
896: io_arrayDestroy(&srv->srv_blob.clients);
1.2 misho 897:
1.10 misho 898: close(srv->srv_blob.server.cli_sock);
1.2 misho 899:
1.10 misho 900: /* detach blobs */
901: TAILQ_FOREACH_SAFE(b, &srv->srv_blob.blobs, blob_node, tmp) {
902: TAILQ_REMOVE(&srv->srv_blob.blobs, b, blob_node);
1.2 misho 903:
1.10 misho 904: rpc_srv_blobFree(srv, b);
1.11 misho 905: io_free(b);
1.2 misho 906: }
907:
1.10 misho 908: schedEnd(&srv->srv_blob.root);
909: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 910: return 0;
911: }
912:
913:
914: /*
1.7 misho 915: * rpc_srv_initServer() - Init & create RPC Server
916: *
1.1 misho 917: * @regProgID = ProgramID for authentication & recognition
918: * @regProcID = ProcessID for authentication & recognition
919: * @concurentClients = Concurent clients at same time to this server
1.10 misho 920: * @netBuf = Network buffer length (min:512 bytes), if =0 == BUFSIZ (also meaning max RPC packet)
1.4 misho 921: * @csHost = Host name or address for bind server, if NULL any address
1.1 misho 922: * @Port = Port for bind server, if Port == 0 default port is selected
1.12.2.1 misho 923: * @proto = Protocol, if == 0 choose SOCK_STREAM
1.1 misho 924: * return: NULL == error or !=NULL bind and created RPC server instance
925: */
926: rpc_srv_t *
1.10 misho 927: rpc_srv_initServer(u_int regProgID, u_char regProcID, int concurentClients,
1.12.2.1 misho 928: int netBuf, const char *csHost, u_short Port, int proto)
1.1 misho 929: {
1.10 misho 930: int n = 1;
1.1 misho 931: rpc_srv_t *srv = NULL;
1.11 misho 932: io_sockaddr_t sa = IO_SOCKADDR_INIT;
1.1 misho 933:
1.12.2.1 misho 934: if (!concurentClients || !regProgID || (proto < 0 || proto > SOCK_DGRAM)) {
1.10 misho 935: rpc_SetErr(EINVAL, "Invalid parameters can`t init RPC server");
1.1 misho 936: return NULL;
937: }
1.10 misho 938: if (!io_gethostbyname(csHost, Port, &sa))
939: return NULL;
1.1 misho 940: if (!Port)
941: Port = RPC_DEFPORT;
1.12.2.6 misho 942: if (!proto)
943: proto = SOCK_STREAM;
1.10 misho 944: if (netBuf < RPC_MIN_BUFSIZ)
1.5 misho 945: netBuf = BUFSIZ;
1.7 misho 946: else
1.12 misho 947: netBuf = io_align(netBuf, 2); /* align netBuf length */
1.10 misho 948:
949: #ifdef HAVE_SRANDOMDEV
950: srandomdev();
951: #else
952: time_t tim;
953:
954: srandom((time(&tim) ^ getpid()));
955: #endif
1.1 misho 956:
1.11 misho 957: srv = io_malloc(sizeof(rpc_srv_t));
1.1 misho 958: if (!srv) {
959: LOGERR;
960: return NULL;
961: } else
962: memset(srv, 0, sizeof(rpc_srv_t));
963:
1.12.2.1 misho 964: srv->srv_proto = proto;
1.5 misho 965: srv->srv_netbuf = netBuf;
1.1 misho 966: srv->srv_session.sess_version = RPC_VERSION;
967: srv->srv_session.sess_program = regProgID;
968: srv->srv_session.sess_process = regProcID;
969:
970: srv->srv_server.cli_parent = srv;
1.10 misho 971: memcpy(&srv->srv_server.cli_sa, &sa, sizeof srv->srv_server.cli_sa);
972:
1.12 misho 973: /* init functions */
974: pthread_mutex_init(&srv->srv_funcs.mtx, NULL);
975: SLIST_INIT(&srv->srv_funcs);
976: AVL_INIT(&srv->srv_funcs);
1.10 misho 977:
978: /* init scheduler */
979: srv->srv_root = schedBegin();
980: if (!srv->srv_root) {
981: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
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: }
986:
987: /* init pool for clients */
988: srv->srv_clients = io_arrayInit(concurentClients);
989: if (!srv->srv_clients) {
990: rpc_SetErr(io_GetErrno(), "%s", io_GetError());
991: schedEnd(&srv->srv_root);
1.12 misho 992: pthread_mutex_destroy(&srv->srv_funcs.mtx);
1.11 misho 993: io_free(srv);
1.10 misho 994: return NULL;
995: }
1.4 misho 996:
997: /* create server socket */
1.12.2.1 misho 998: srv->srv_server.cli_sock = socket(srv->srv_server.cli_sa.sa.sa_family, srv->srv_proto, 0);
1.1 misho 999: if (srv->srv_server.cli_sock == -1) {
1000: LOGERR;
1.10 misho 1001: io_arrayDestroy(&srv->srv_clients);
1002: schedEnd(&srv->srv_root);
1.12 misho 1003: pthread_mutex_destroy(&srv->srv_funcs.mtx);
1.11 misho 1004: io_free(srv);
1.1 misho 1005: return NULL;
1006: }
1007: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
1008: LOGERR;
1.10 misho 1009: goto err;
1.1 misho 1010: }
1.5 misho 1011: n = srv->srv_netbuf;
1012: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof n) == -1) {
1013: LOGERR;
1.10 misho 1014: goto err;
1.5 misho 1015: }
1016: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_RCVBUF, &n, sizeof n) == -1) {
1017: LOGERR;
1.10 misho 1018: goto err;
1.5 misho 1019: }
1.6 misho 1020: if (bind(srv->srv_server.cli_sock, &srv->srv_server.cli_sa.sa,
1021: srv->srv_server.cli_sa.sa.sa_len) == -1) {
1.1 misho 1022: LOGERR;
1.10 misho 1023: goto err;
1.12.2.4 misho 1024: } else
1025: fcntl(srv->srv_server.cli_sock, F_SETFL,
1026: fcntl(srv->srv_server.cli_sock, F_GETFL) | O_NONBLOCK);
1.1 misho 1027:
1.10 misho 1028: rpc_register_srvPing(srv);
1.8 misho 1029:
1.1 misho 1030: return srv;
1.10 misho 1031: err: /* error condition */
1032: close(srv->srv_server.cli_sock);
1033: io_arrayDestroy(&srv->srv_clients);
1034: schedEnd(&srv->srv_root);
1.12 misho 1035: pthread_mutex_destroy(&srv->srv_funcs.mtx);
1.11 misho 1036: io_free(srv);
1.10 misho 1037: return NULL;
1.1 misho 1038: }
1039:
1040: /*
1.7 misho 1041: * rpc_srv_endServer() - Destroy RPC server, close all opened sockets and free resources
1042: *
1.6 misho 1043: * @psrv = RPC Server instance
1.1 misho 1044: * return: none
1045: */
1.10 misho 1046: inline void
1.6 misho 1047: rpc_srv_endServer(rpc_srv_t ** __restrict psrv)
1.1 misho 1048: {
1.10 misho 1049: if (!psrv || !*psrv)
1.1 misho 1050: return;
1051:
1.10 misho 1052: /* if send kill to blob server */
1053: if (!(*psrv)->srv_blob.kill)
1054: rpc_srv_endBLOBServer(*psrv);
1.1 misho 1055:
1.10 misho 1056: (*psrv)->srv_kill = 1;
1057: sleep(RPC_SCHED_POLLING);
1.2 misho 1058:
1.12 misho 1059: pthread_mutex_destroy(&(*psrv)->srv_funcs.mtx);
1.11 misho 1060: io_free(*psrv);
1.6 misho 1061: *psrv = NULL;
1.1 misho 1062: }
1063:
1064: /*
1.7 misho 1065: * rpc_srv_loopServer() - Execute Main server loop and wait for clients requests
1066: *
1.1 misho 1067: * @srv = RPC Server instance
1068: * return: -1 error or 0 ok, infinite loop ...
1069: */
1070: int
1.5 misho 1071: rpc_srv_loopServer(rpc_srv_t * __restrict srv)
1.1 misho 1072: {
1.10 misho 1073: rpc_cli_t *c;
1.1 misho 1074: register int i;
1.12 misho 1075: rpc_func_t *f;
1.10 misho 1076: struct timespec ts = { RPC_SCHED_POLLING, 0 };
1.1 misho 1077:
1078: if (!srv) {
1.10 misho 1079: rpc_SetErr(EINVAL, "Invalid parameter can`t start RPC server");
1.1 misho 1080: return -1;
1081: }
1082:
1.12.2.7 misho 1083: if (srv->srv_proto == SOCK_STREAM)
1084: if (listen(srv->srv_server.cli_sock,
1085: io_arraySize(srv->srv_clients)) == -1) {
1086: LOGERR;
1087: return -1;
1088: }
1.1 misho 1089:
1.12.2.1 misho 1090: if (!schedRead(srv->srv_root, cbProto[srv->srv_proto][CB_ACCEPTCLIENT], srv,
1091: srv->srv_server.cli_sock, NULL, 0)) {
1.10 misho 1092: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
1093: return -1;
1094: }
1.7 misho 1095:
1.10 misho 1096: schedPolling(srv->srv_root, &ts, NULL);
1.7 misho 1097: /* main rpc loop */
1.10 misho 1098: schedRun(srv->srv_root, &srv->srv_kill);
1099:
1100: /* close all clients connections & server socket */
1101: for (i = 0; i < io_arraySize(srv->srv_clients); i++) {
1102: c = io_array(srv->srv_clients, i, rpc_cli_t*);
1103: if (c) {
1104: shutdown(c->cli_sock, SHUT_RDWR);
1105: close(c->cli_sock);
1106:
1107: schedCancelby(srv->srv_root, taskMAX, CRITERIA_ARG, c, NULL);
1108: io_freeVars(&RPC_RETVARS(c));
1109: AIT_FREE_VAL(&c->cli_buf);
1.1 misho 1110: }
1.10 misho 1111: io_arrayDel(srv->srv_clients, i, 42);
1112: }
1113: io_arrayDestroy(&srv->srv_clients);
1.2 misho 1114:
1.10 misho 1115: close(srv->srv_server.cli_sock);
1.2 misho 1116:
1.10 misho 1117: /* detach exported calls */
1.12 misho 1118: RPC_FUNCS_LOCK(&srv->srv_funcs);
1119: while ((f = SLIST_FIRST(&srv->srv_funcs))) {
1120: SLIST_REMOVE_HEAD(&srv->srv_funcs, func_next);
1.1 misho 1121:
1.10 misho 1122: AIT_FREE_VAL(&f->func_name);
1.11 misho 1123: io_free(f);
1.1 misho 1124: }
1.12 misho 1125: srv->srv_funcs.avlh_root = NULL;
1126: RPC_FUNCS_UNLOCK(&srv->srv_funcs);
1.1 misho 1127:
1.10 misho 1128: schedEnd(&srv->srv_root);
1.1 misho 1129: return 0;
1130: }
1131:
1132:
1133: /*
1134: * rpc_srv_execCall() Execute registered call from RPC server
1.7 misho 1135: *
1.10 misho 1136: * @cli = RPC client
1.1 misho 1137: * @rpc = IN RPC call structure
1.10 misho 1138: * @funcname = Execute RPC function
1.5 misho 1139: * @args = IN RPC calling arguments from RPC client
1.1 misho 1140: * return: -1 error, !=-1 ok
1141: */
1142: int
1.10 misho 1143: rpc_srv_execCall(rpc_cli_t * __restrict cli, struct tagRPCCall * __restrict rpc,
1144: ait_val_t funcname, array_t * __restrict args)
1.1 misho 1145: {
1146: rpc_callback_t func;
1147:
1.10 misho 1148: if (!cli || !rpc || !AIT_ADDR(&funcname)) {
1.7 misho 1149: rpc_SetErr(EINVAL, "Invalid parameter can`t exec function");
1.1 misho 1150: return -1;
1151: }
1152:
1.10 misho 1153: func = AIT_GET_LIKE(&funcname, rpc_callback_t);
1154: return func(cli, rpc, args);
1.1 misho 1155: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>