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