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