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