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