Annotation of libaitrpc/src/srv.c, revision 1.9.2.23
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.23! misho 6: * $Id: srv.c,v 1.9.2.22 2012/05/17 12:52:30 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: */
595: void
596: rpc_srv_endBLOBServer(rpc_srv_t * __restrict srv)
597: {
598: rpc_cli_t *c;
599: register int i;
1.9.2.11 misho 600: rpc_blob_t *b, *tmp;
1.2 misho 601:
1.9.2.12 misho 602: if (!srv)
1.2 misho 603: return;
604:
1.9.2.21 misho 605: if (srv->srv_blob.tid) {
606: pthread_cancel(srv->srv_blob.tid);
607: pthread_join(srv->srv_blob.tid, NULL);
608: }
1.9.2.19 misho 609:
1.4 misho 610: /* close all clients connections & server socket */
1.9.2.11 misho 611: for (i = 0; i < io_arraySize(srv->srv_blob.clients); i++) {
612: c = io_array(srv->srv_blob.clients, i, rpc_cli_t*);
613: if (c) {
1.2 misho 614: shutdown(c->cli_sock, SHUT_RDWR);
1.9.2.11 misho 615: close(c->cli_sock);
1.2 misho 616:
1.9.2.20 misho 617: schedCancelby(srv->srv_blob.root, taskMAX, CRITERIA_ARG, c, NULL);
1.9.2.19 misho 618: AIT_FREE_VAL(&c->cli_buf);
1.9.2.11 misho 619: }
1.9.2.19 misho 620: io_arrayDel(srv->srv_blob.clients, i, 42);
1.2 misho 621: }
1.9.2.11 misho 622: io_arrayDestroy(&srv->srv_blob.clients);
623:
624: close(srv->srv_blob.server.cli_sock);
1.2 misho 625:
1.4 misho 626: /* detach blobs */
1.9.2.11 misho 627: TAILQ_FOREACH_SAFE(b, &srv->srv_blob.blobs, blob_node, tmp) {
628: TAILQ_REMOVE(&srv->srv_blob.blobs, b, blob_node);
629:
630: rpc_srv_blobFree(srv, b);
631: free(b);
1.2 misho 632: }
633:
1.9.2.12 misho 634: schedEnd(&srv->srv_blob.root);
1.7 misho 635: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 636: }
637:
638: /*
1.9.2.12 misho 639: * rpc_srv_loopBLOBServer() - Execute Main BLOB server loop and wait for clients requests
1.7 misho 640: *
1.2 misho 641: * @srv = RPC Server instance
642: * return: -1 error or 0 ok, infinite loop ...
643: */
644: int
1.9.2.12 misho 645: rpc_srv_loopBLOBServer(rpc_srv_t * __restrict srv)
1.2 misho 646: {
1.9.2.12 misho 647: if (!srv || srv->srv_kill) {
1.7 misho 648: rpc_SetErr(EINVAL, "Invalid parameter can`t start BLOB server");
1.2 misho 649: return -1;
650: }
651:
1.9.2.12 misho 652: fcntl(srv->srv_blob.server.cli_sock, F_SETFL,
653: fcntl(srv->srv_blob.server.cli_sock, F_GETFL) | O_NONBLOCK);
654:
655: if (listen(srv->srv_blob.server.cli_sock, io_arraySize(srv->srv_blob.clients)) == -1) {
1.2 misho 656: LOGERR;
657: return -1;
1.9.2.12 misho 658: }
1.2 misho 659:
1.9.2.12 misho 660: if (!schedRead(srv->srv_blob.root, acceptBLOBClients, srv,
661: srv->srv_blob.server.cli_sock, NULL, 0)) {
662: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
663: return -1;
664: }
1.7 misho 665:
1.9.2.12 misho 666: /* main rpc loop */
667: schedRun(srv->srv_blob.root, &srv->srv_blob.kill);
1.2 misho 668: return 0;
669: }
670:
671:
672: /*
1.7 misho 673: * rpc_srv_initServer() - Init & create RPC Server
674: *
1.1 misho 675: * @regProgID = ProgramID for authentication & recognition
676: * @regProcID = ProcessID for authentication & recognition
677: * @concurentClients = Concurent clients at same time to this server
1.5 misho 678: * @netBuf = Network buffer length, if =0 == BUFSIZ (also meaning max RPC packet)
1.4 misho 679: * @csHost = Host name or address for bind server, if NULL any address
1.1 misho 680: * @Port = Port for bind server, if Port == 0 default port is selected
681: * return: NULL == error or !=NULL bind and created RPC server instance
682: */
683: rpc_srv_t *
1.9.2.6 misho 684: rpc_srv_initServer(u_int regProgID, u_char regProcID, int concurentClients,
1.9.2.4 misho 685: int netBuf, const char *csHost, u_short Port)
1.1 misho 686: {
687: int n = 1;
1.9.2.4 misho 688: rpc_srv_t *srv = NULL;
1.6 misho 689: io_sockaddr_t sa;
1.1 misho 690:
1.9.2.2 misho 691: if (!concurentClients || !regProgID) {
692: rpc_SetErr(EINVAL, "Invalid parameters can`t init RPC server");
1.1 misho 693: return NULL;
694: }
1.9.2.2 misho 695: if (!io_gethostbyname(csHost, Port, &sa))
696: return NULL;
1.1 misho 697: if (!Port)
698: Port = RPC_DEFPORT;
1.5 misho 699: if (!netBuf)
700: netBuf = BUFSIZ;
1.7 misho 701: else
702: netBuf = io_align(netBuf, 1); /* align netBuf length */
1.1 misho 703:
1.9.2.16 misho 704: #ifdef HAVE_SRANDOMDEV
705: srandomdev();
706: #else
707: time_t tim;
708:
709: srandom((time(&tim) ^ getpid()));
710: #endif
711:
1.1 misho 712: srv = malloc(sizeof(rpc_srv_t));
713: if (!srv) {
714: LOGERR;
715: return NULL;
716: } else
717: memset(srv, 0, sizeof(rpc_srv_t));
718:
1.5 misho 719: srv->srv_netbuf = netBuf;
1.1 misho 720: srv->srv_session.sess_version = RPC_VERSION;
721: srv->srv_session.sess_program = regProgID;
722: srv->srv_session.sess_process = regProcID;
723:
724: srv->srv_server.cli_parent = srv;
1.9.2.4 misho 725: memcpy(&srv->srv_server.cli_sa, &sa, sizeof srv->srv_server.cli_sa);
726:
727: /* init functions list */
728: TAILQ_INIT(&srv->srv_funcs);
729:
730: /* init scheduler */
731: srv->srv_root = schedBegin();
732: if (!srv->srv_root) {
733: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
734: free(srv);
735: return NULL;
736: }
737:
738: /* init pool for clients */
739: srv->srv_clients = io_arrayInit(concurentClients);
740: if (!srv->srv_clients) {
741: rpc_SetErr(io_GetErrno(), "%s", io_GetError());
742: schedEnd(&srv->srv_root);
743: free(srv);
744: return NULL;
745: }
1.4 misho 746:
747: /* create server socket */
1.9.2.2 misho 748: srv->srv_server.cli_sock = socket(srv->srv_server.cli_sa.sa.sa_family, SOCK_STREAM, 0);
1.1 misho 749: if (srv->srv_server.cli_sock == -1) {
750: LOGERR;
1.9.2.4 misho 751: io_arrayDestroy(&srv->srv_clients);
752: schedEnd(&srv->srv_root);
1.1 misho 753: free(srv);
754: return NULL;
755: }
756: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
757: LOGERR;
1.9.2.4 misho 758: goto err;
1.1 misho 759: }
1.5 misho 760: n = srv->srv_netbuf;
761: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof n) == -1) {
762: LOGERR;
1.9.2.4 misho 763: goto err;
1.5 misho 764: }
765: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_RCVBUF, &n, sizeof n) == -1) {
766: LOGERR;
1.9.2.4 misho 767: goto err;
1.5 misho 768: }
1.6 misho 769: if (bind(srv->srv_server.cli_sock, &srv->srv_server.cli_sa.sa,
770: srv->srv_server.cli_sa.sa.sa_len) == -1) {
1.1 misho 771: LOGERR;
1.9.2.4 misho 772: goto err;
1.1 misho 773: }
774:
1.9.2.18 misho 775: rpc_register_srvPing(srv);
776:
1.1 misho 777: return srv;
1.9.2.4 misho 778: err: /* error condition */
779: close(srv->srv_server.cli_sock);
780: io_arrayDestroy(&srv->srv_clients);
781: schedEnd(&srv->srv_root);
782: free(srv);
783: return NULL;
1.1 misho 784: }
785:
786: /*
1.7 misho 787: * rpc_srv_endServer() - Destroy RPC server, close all opened sockets and free resources
788: *
1.6 misho 789: * @psrv = RPC Server instance
1.1 misho 790: * return: none
791: */
792: void
1.6 misho 793: rpc_srv_endServer(rpc_srv_t ** __restrict psrv)
1.1 misho 794: {
795: rpc_cli_t *c;
796: register int i;
1.9.2.4 misho 797: rpc_func_t *f, *tmp;
1.1 misho 798:
1.9.2.16 misho 799: if (!psrv || !*psrv)
1.1 misho 800: return;
801:
1.9.2.12 misho 802: if (!(*psrv)->srv_blob.kill)
803: rpc_srv_endBLOBServer(*psrv);
804:
1.4 misho 805: /* close all clients connections & server socket */
1.9.2.4 misho 806: for (i = 0; i < io_arraySize((*psrv)->srv_clients); i++) {
807: c = io_array((*psrv)->srv_clients, i, rpc_cli_t*);
808: if (c) {
1.1 misho 809: shutdown(c->cli_sock, SHUT_RDWR);
1.2 misho 810: close(c->cli_sock);
1.9.2.6 misho 811:
1.9.2.20 misho 812: schedCancelby((*psrv)->srv_root, taskMAX, CRITERIA_ARG, c, NULL);
1.9.2.19 misho 813: AIT_FREE_VAL(&c->cli_buf);
1.2 misho 814: }
1.9.2.19 misho 815: io_arrayDel((*psrv)->srv_clients, i, 42);
1.1 misho 816: }
1.9.2.4 misho 817: io_arrayDestroy(&(*psrv)->srv_clients);
818:
819: close((*psrv)->srv_server.cli_sock);
1.1 misho 820:
1.4 misho 821: /* detach exported calls */
1.9.2.4 misho 822: TAILQ_FOREACH_SAFE(f, &(*psrv)->srv_funcs, func_node, tmp) {
1.9.2.11 misho 823: TAILQ_REMOVE(&(*psrv)->srv_funcs, f, func_node);
824:
1.6 misho 825: io_freeVars(&f->func_vars);
1.7 misho 826: AIT_FREE_VAL(&f->func_name);
1.2 misho 827: free(f);
828: }
829:
1.9.2.4 misho 830: schedEnd(&(*psrv)->srv_root);
1.6 misho 831: free(*psrv);
832: *psrv = NULL;
1.1 misho 833: }
834:
835: /*
1.7 misho 836: * rpc_srv_loopServer() - Execute Main server loop and wait for clients requests
837: *
1.1 misho 838: * @srv = RPC Server instance
839: * return: -1 error or 0 ok, infinite loop ...
840: */
841: int
1.5 misho 842: rpc_srv_loopServer(rpc_srv_t * __restrict srv)
1.1 misho 843: {
844: if (!srv) {
1.9.2.3 misho 845: rpc_SetErr(EINVAL, "Invalid parameter can`t start RPC server");
1.1 misho 846: return -1;
847: }
848:
1.9.2.6 misho 849: fcntl(srv->srv_server.cli_sock, F_SETFL,
850: fcntl(srv->srv_server.cli_sock, F_GETFL) | O_NONBLOCK);
851:
1.9.2.4 misho 852: if (listen(srv->srv_server.cli_sock, io_arraySize(srv->srv_clients)) == -1) {
1.1 misho 853: LOGERR;
854: return -1;
1.9.2.6 misho 855: }
1.7 misho 856:
1.9.2.4 misho 857: if (!schedRead(srv->srv_root, acceptClients, srv, srv->srv_server.cli_sock, NULL, 0)) {
858: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
859: return -1;
1.1 misho 860: }
861:
1.9.2.4 misho 862: /* main rpc loop */
863: schedRun(srv->srv_root, &srv->srv_kill);
1.1 misho 864: return 0;
865: }
866:
867:
868: /*
869: * rpc_srv_execCall() Execute registered call from RPC server
1.7 misho 870: *
1.1 misho 871: * @call = Register RPC call
872: * @rpc = IN RPC call structure
1.5 misho 873: * @args = IN RPC calling arguments from RPC client
1.1 misho 874: * return: -1 error, !=-1 ok
875: */
876: int
1.2 misho 877: rpc_srv_execCall(rpc_func_t * __restrict call, struct tagRPCCall * __restrict rpc,
1.5 misho 878: array_t * __restrict args)
1.1 misho 879: {
880: rpc_callback_t func;
881:
1.9.2.6 misho 882: if (!call || !rpc || !call->func_parent || !AIT_ADDR(&call->func_name)) {
1.7 misho 883: rpc_SetErr(EINVAL, "Invalid parameter can`t exec function");
1.1 misho 884: return -1;
885: }
886:
1.9.2.6 misho 887: func = AIT_GET_LIKE(&call->func_name, rpc_callback_t);
1.9.2.18 misho 888: return func(call, rpc, args);
1.1 misho 889: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>