Annotation of libaitrpc/src/srv.c, revision 1.11.2.3
1.1 misho 1: /*************************************************************************
2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
3: * by Michael Pounov <misho@openbsd-bg.org>
4: *
5: * $Author: misho $
1.11.2.3! misho 6: * $Id: srv.c,v 1.11.2.2 2012/08/29 14:54:57 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.10 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:
55: schedCancelby(TASK_ROOT(task), taskMAX, CRITERIA_ARG, TASK_ARG(task), NULL);
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, 0);
66: if (c)
1.11 misho 67: io_free(c);
1.10 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.10 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);
1.10 misho 80:
81: /* copy RPC header */
82: memcpy(buf, TASK_DATA(task), wlen);
1.7 misho 83:
84: if (rpc->call_argc) {
1.10 misho 85: f = rpc_srv_getCall(s, ntohs(rpc->call_tag));
1.7 misho 86: if (!f) {
1.10 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.10 misho 92: rpc->call_argc = htons(io_arraySize(RPC_RETVARS(c)));
1.7 misho 93: /* Go Encapsulate variables */
1.10 misho 94: ret = io_vars2buffer(buf + wlen, sizeof buf - wlen, RPC_RETVARS(c));
95: /* Free return values */
96: io_freeVars(&c->cli_vars);
1.7 misho 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.10 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.10 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.10 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.10 misho 146: } else
147: arr = NULL;
1.7 misho 148:
1.10 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 */
1.10 misho 156: argc = RPC_CHK_NOREPLY(rpc);
157: rpc->call_rep.ret = RPC_ERROR(rpc_srv_execCall(c, rpc, f->func_name, arr));
1.7 misho 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 */
1.10 misho 165: io_freeVars(&c->cli_vars);
1.8 misho 166: rpc->call_argc ^= rpc->call_argc;
1.10 misho 167: } else {
168: /* reply */
169: rpc->call_argc = htons(io_arraySize(RPC_RETVARS(c)));
170: }
1.7 misho 171: }
172: }
173:
1.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.10 misho 183: int len, rlen, noreply;
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.10 misho 188: memset(buf, 0, AIT_LEN(&c->cli_buf));
189: rlen = recv(TASK_FD(task), buf + off, AIT_LEN(&c->cli_buf) - off, 0);
190: if (rlen < 1) {
191: /* close connection */
192: schedEvent(TASK_ROOT(task), closeClient, c, 42, NULL, 0);
1.7 misho 193: return NULL;
1.10 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.10 misho 200: /* check RPC packet */
1.8 misho 201: if (rlen < sizeof(struct tagRPCCall)) {
1.10 misho 202: rpc_SetErr(ERPCMISMATCH, "Short RPC packet");
1.8 misho 203:
1.10 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.10 misho 211: len = ntohs(rpc->call_len);
212: rlen -= len;
213:
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: }
1.8 misho 220:
221: /* check integrity of packet */
222: crc = ntohs(rpc->call_crc);
223: rpc->call_crc ^= rpc->call_crc;
1.10 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.10 misho 227: off += len;
228: /* try next packet remaining into buffer */
229: continue;
1.8 misho 230: }
1.7 misho 231:
1.10 misho 232: noreply = RPC_CHK_NOREPLY(rpc);
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.10 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.10 misho 247: schedWrite(TASK_ROOT(task), txPacket, TASK_ARG(task), TASK_FD(task), rpc, len);
1.7 misho 248:
1.10 misho 249: off += len;
1.8 misho 250: } while (rlen > 0);
1.7 misho 251:
252: /* lets get next packet */
1.10 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.10 misho 258: acceptClients(sched_task_t *task)
1.1 misho 259: {
1.10 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.7 misho 264:
1.10 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;
1.11 misho 270: c = io_malloc(sizeof(rpc_cli_t));
1.10 misho 271: if (!c) {
272: LOGERR;
273: srv->srv_kill = 1;
1.1 misho 274: return NULL;
1.10 misho 275: } else {
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;
280: }
281:
282: /* alloc empty buffer */
283: AIT_SET_BUF2(&c->cli_buf, 0, srv->srv_netbuf);
284:
285: /* accept client */
286: c->cli_sock = accept(TASK_FD(task), &c->cli_sa.sa, &salen);
287: if (c->cli_sock == -1) {
288: LOGERR;
289: AIT_FREE_VAL(&c->cli_buf);
290: io_arrayDel(srv->srv_clients, i, 42);
291: goto end;
1.1 misho 292: } else
1.10 misho 293: fcntl(c->cli_sock, F_SETFL, fcntl(c->cli_sock, F_GETFL) | O_NONBLOCK);
1.1 misho 294:
1.10 misho 295: schedRead(TASK_ROOT(task), rxPacket, c, c->cli_sock, NULL, 0);
296: end:
297: schedReadSelf(task);
298: return NULL;
299: }
1.5 misho 300:
1.10 misho 301: /* ------------------------------------------------------ */
1.7 misho 302:
1.10 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;
1.7 misho 308:
1.10 misho 309: schedCancelby(TASK_ROOT(task), taskMAX, CRITERIA_ARG, TASK_ARG(task), NULL);
1.7 misho 310:
1.10 misho 311: /* close client socket */
312: if (TASK_VAL(task))
313: shutdown(c->cli_sock, SHUT_RDWR);
1.7 misho 314: close(c->cli_sock);
1.10 misho 315:
316: /* free buffer */
317: AIT_FREE_VAL(&c->cli_buf);
318:
319: io_arrayDel(s->srv_blob.clients, c->cli_id, 0);
320: if (c)
1.11 misho 321: io_free(c);
1.7 misho 322: return NULL;
323: }
324:
325: static void *
326: txBLOB(sched_task_t *task)
327: {
1.10 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.10 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.10 misho 353: struct tagBLOBHdr blob;
1.7 misho 354: int rlen;
355: u_short crc;
356:
1.10 misho 357: memset(&blob, 0, sizeof blob);
358: rlen = recv(TASK_FD(task), &blob, sizeof blob, 0);
359: if (rlen < 1) {
360: /* close blob connection */
361: schedEvent(TASK_ROOT(task), closeBLOBClient, c, 42, NULL, 0);
1.7 misho 362: return NULL;
363: }
1.5 misho 364:
1.10 misho 365: /* check BLOB packet */
366: if (rlen < sizeof(struct tagBLOBHdr)) {
367: rpc_SetErr(ERPCMISMATCH, "Short BLOB packet");
1.6 misho 368:
1.10 misho 369: schedReadSelf(task);
1.7 misho 370: return NULL;
371: }
1.1 misho 372:
1.7 misho 373: /* check integrity of packet */
1.10 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.10 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.10 misho 384: if ((crc = rpc_chkPktSession(&blob.hdr_session, &s->srv_session))) {
1.7 misho 385: rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session");
1.10 misho 386: blob.hdr_cmd = error;
1.7 misho 387: goto end;
388: }
389:
390: /* Go to proceed packet ... */
1.10 misho 391: switch (blob.hdr_cmd) {
1.7 misho 392: case get:
1.10 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.10 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.10 misho 403: blob.hdr_ret = htonl(rpc_srv_sendBLOB(c, b));
1.7 misho 404: rpc_srv_blobUnmap(b);
405: } else {
1.10 misho 406: blob.hdr_cmd = error;
407: blob.hdr_ret = RPC_ERROR(-1);
1.7 misho 408: }
409: break;
410: case set:
1.10 misho 411: if ((b = rpc_srv_registerBLOB(s, ntohl(blob.hdr_len)))) {
1.7 misho 412: /* set new BLOB variable for reply :) */
1.10 misho 413: blob.hdr_var = htonl(b->blob_var);
1.7 misho 414:
415: /* receive BLOB from client */
1.10 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.10 misho 419: blob.hdr_cmd = error;
420: blob.hdr_ret = RPC_ERROR(-1);
1.7 misho 421: }
422: break;
423: case unset:
1.11 misho 424: if (rpc_srv_unregisterBLOB(s, ntohl(blob.hdr_var)) == -1) {
1.10 misho 425: blob.hdr_cmd = error;
426: blob.hdr_ret = RPC_ERROR(-1);
1.1 misho 427: }
428: break;
1.7 misho 429: default:
1.10 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.10 misho 436: memcpy(AIT_ADDR(&c->cli_buf), &blob, sizeof blob);
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.10 misho 443: acceptBLOBClients(sched_task_t *task)
1.2 misho 444: {
1.10 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.11.2.2 misho 449: #ifdef TCP_NOPUSH
1.11.2.1 misho 450: int n = 1;
1.11.2.2 misho 451: #endif
1.7 misho 452:
1.10 misho 453: /* check free slots for connect */
454: for (i = 0; i < io_arraySize(srv->srv_blob.clients) &&
455: (c = io_array(srv->srv_blob.clients, i, rpc_cli_t*)); i++);
456: if (c) /* no more free slots! */
457: goto end;
1.11 misho 458: c = io_malloc(sizeof(rpc_cli_t));
1.10 misho 459: if (!c) {
1.7 misho 460: LOGERR;
1.10 misho 461: srv->srv_kill = srv->srv_blob.kill = 1;
1.7 misho 462: return NULL;
463: } else {
1.10 misho 464: memset(c, 0, sizeof(rpc_cli_t));
465: io_arraySet(srv->srv_blob.clients, i, c);
466: c->cli_id = i;
467: c->cli_parent = srv;
1.7 misho 468: }
1.4 misho 469:
1.10 misho 470: /* alloc empty buffer */
471: AIT_SET_BUF2(&c->cli_buf, 0, srv->srv_netbuf);
1.2 misho 472:
1.10 misho 473: /* accept client */
474: c->cli_sock = accept(TASK_FD(task), &c->cli_sa.sa, &salen);
475: if (c->cli_sock == -1) {
476: LOGERR;
477: AIT_FREE_VAL(&c->cli_buf);
478: io_arrayDel(srv->srv_blob.clients, i, 42);
479: goto end;
1.11.2.1 misho 480: } else {
1.11.2.2 misho 481: #ifdef TCP_NOPUSH
1.11.2.1 misho 482: setsockopt(c->cli_sock, IPPROTO_TCP, TCP_NOPUSH, &n, sizeof n);
1.11.2.2 misho 483: #endif
1.10 misho 484: fcntl(c->cli_sock, F_SETFL, fcntl(c->cli_sock, F_GETFL) | O_NONBLOCK);
1.11.2.1 misho 485: }
1.2 misho 486:
1.10 misho 487: schedRead(TASK_ROOT(task), rxBLOB, c, c->cli_sock, NULL, 0);
488: end:
489: schedReadSelf(task);
1.7 misho 490: return NULL;
1.1 misho 491: }
492:
1.10 misho 493: /* ------------------------------------------------------ */
1.1 misho 494:
495: /*
1.7 misho 496: * rpc_srv_initBLOBServer() - Init & create BLOB Server
497: *
1.4 misho 498: * @srv = RPC server instance
1.2 misho 499: * @Port = Port for bind server, if Port == 0 default port is selected
500: * @diskDir = Disk place for BLOB file objects
501: * return: -1 == error or 0 bind and created BLOB server instance
502: */
503: int
504: rpc_srv_initBLOBServer(rpc_srv_t * __restrict srv, u_short Port, const char *diskDir)
505: {
506: int n = 1;
507:
1.10 misho 508: if (!srv || srv->srv_kill) {
1.7 misho 509: rpc_SetErr(EINVAL, "Invalid parameters can`t init BLOB server");
1.2 misho 510: return -1;
511: }
512:
513: memset(&srv->srv_blob, 0, sizeof srv->srv_blob);
514: if (access(diskDir, R_OK | W_OK) == -1) {
515: LOGERR;
516: return -1;
517: } else
1.7 misho 518: AIT_SET_STR(&srv->srv_blob.dir, diskDir);
1.2 misho 519:
1.10 misho 520: /* init blob list */
521: TAILQ_INIT(&srv->srv_blob.blobs);
522:
1.2 misho 523: srv->srv_blob.server.cli_parent = srv;
1.4 misho 524:
1.10 misho 525: memcpy(&srv->srv_blob.server.cli_sa, &srv->srv_server.cli_sa, sizeof(io_sockaddr_t));
526: switch (srv->srv_blob.server.cli_sa.sa.sa_family) {
1.4 misho 527: case AF_INET:
1.10 misho 528: srv->srv_blob.server.cli_sa.sin.sin_port =
529: htons(Port ? Port : ntohs(srv->srv_blob.server.cli_sa.sin.sin_port) + 1);
1.4 misho 530: break;
531: case AF_INET6:
1.10 misho 532: srv->srv_blob.server.cli_sa.sin6.sin6_port =
533: htons(Port ? Port : ntohs(srv->srv_blob.server.cli_sa.sin6.sin6_port) + 1);
1.4 misho 534: break;
535: case AF_LOCAL:
1.10 misho 536: strlcat(srv->srv_blob.server.cli_sa.sun.sun_path, ".blob",
537: sizeof srv->srv_blob.server.cli_sa.sun.sun_path);
1.4 misho 538: break;
539: default:
1.7 misho 540: AIT_FREE_VAL(&srv->srv_blob.dir);
1.4 misho 541: return -1;
1.2 misho 542: }
543:
1.4 misho 544: /* create BLOB server socket */
1.6 misho 545: srv->srv_blob.server.cli_sock = socket(srv->srv_server.cli_sa.sa.sa_family, SOCK_STREAM, 0);
1.2 misho 546: if (srv->srv_blob.server.cli_sock == -1) {
547: LOGERR;
1.7 misho 548: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 549: return -1;
550: }
551: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &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.2 misho 555: return -1;
556: }
1.5 misho 557: n = srv->srv_netbuf;
558: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof n) == -1) {
559: LOGERR;
560: close(srv->srv_blob.server.cli_sock);
1.7 misho 561: AIT_FREE_VAL(&srv->srv_blob.dir);
1.5 misho 562: return -1;
563: }
564: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_RCVBUF, &n, sizeof n) == -1) {
565: LOGERR;
566: close(srv->srv_blob.server.cli_sock);
1.7 misho 567: AIT_FREE_VAL(&srv->srv_blob.dir);
1.5 misho 568: return -1;
569: }
1.6 misho 570: if (bind(srv->srv_blob.server.cli_sock, &srv->srv_blob.server.cli_sa.sa,
571: srv->srv_blob.server.cli_sa.sa.sa_len) == -1) {
1.2 misho 572: LOGERR;
573: close(srv->srv_blob.server.cli_sock);
1.7 misho 574: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 575: return -1;
576: }
577:
1.10 misho 578: /* allocate pool for concurent blob clients */
579: srv->srv_blob.clients = io_arrayInit(io_arraySize(srv->srv_clients));
1.2 misho 580: if (!srv->srv_blob.clients) {
1.10 misho 581: rpc_SetErr(io_GetErrno(), "%s", io_GetError());
1.2 misho 582: close(srv->srv_blob.server.cli_sock);
1.7 misho 583: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 584: return -1;
1.10 misho 585: }
1.2 misho 586:
1.10 misho 587: /* init blob scheduler */
588: srv->srv_blob.root = schedBegin();
589: if (!srv->srv_blob.root) {
590: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
591: io_arrayDestroy(&srv->srv_blob.clients);
592: close(srv->srv_blob.server.cli_sock);
593: AIT_FREE_VAL(&srv->srv_blob.dir);
594: return -1;
595: }
1.2 misho 596:
597: return 0;
598: }
599:
600: /*
1.7 misho 601: * rpc_srv_endBLOBServer() - Destroy BLOB server, close all opened sockets and free resources
602: *
1.2 misho 603: * @srv = RPC Server instance
604: * return: none
605: */
1.10 misho 606: inline void
1.2 misho 607: rpc_srv_endBLOBServer(rpc_srv_t * __restrict srv)
608: {
1.10 misho 609: if (!srv)
1.2 misho 610: return;
611:
1.10 misho 612: srv->srv_blob.kill = 1;
1.2 misho 613: }
614:
615: /*
1.10 misho 616: * rpc_srv_loopBLOBServer() - Execute Main BLOB server loop and wait for clients requests
1.7 misho 617: *
1.2 misho 618: * @srv = RPC Server instance
619: * return: -1 error or 0 ok, infinite loop ...
620: */
621: int
1.10 misho 622: rpc_srv_loopBLOBServer(rpc_srv_t * __restrict srv)
1.2 misho 623: {
1.10 misho 624: rpc_cli_t *c;
1.2 misho 625: register int i;
1.10 misho 626: rpc_blob_t *b, *tmp;
627: struct timespec ts = { RPC_SCHED_POLLING, 0 };
1.2 misho 628:
1.10 misho 629: if (!srv || srv->srv_kill) {
1.7 misho 630: rpc_SetErr(EINVAL, "Invalid parameter can`t start BLOB server");
1.2 misho 631: return -1;
632: }
633:
1.10 misho 634: fcntl(srv->srv_blob.server.cli_sock, F_SETFL,
635: fcntl(srv->srv_blob.server.cli_sock, F_GETFL) | O_NONBLOCK);
636:
637: if (listen(srv->srv_blob.server.cli_sock, io_arraySize(srv->srv_blob.clients)) == -1) {
1.2 misho 638: LOGERR;
639: return -1;
1.10 misho 640: }
641:
642: if (!schedRead(srv->srv_blob.root, acceptBLOBClients, srv,
643: srv->srv_blob.server.cli_sock, NULL, 0)) {
644: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
645: return -1;
646: }
1.2 misho 647:
1.10 misho 648: schedPolling(srv->srv_blob.root, &ts, NULL);
649: /* main rpc loop */
650: schedRun(srv->srv_blob.root, &srv->srv_blob.kill);
1.7 misho 651:
1.10 misho 652: /* close all clients connections & server socket */
653: for (i = 0; i < io_arraySize(srv->srv_blob.clients); i++) {
654: c = io_array(srv->srv_blob.clients, i, rpc_cli_t*);
655: if (c) {
656: shutdown(c->cli_sock, SHUT_RDWR);
657: close(c->cli_sock);
658:
659: schedCancelby(srv->srv_blob.root, taskMAX, CRITERIA_ARG, c, NULL);
660: AIT_FREE_VAL(&c->cli_buf);
1.2 misho 661: }
1.10 misho 662: io_arrayDel(srv->srv_blob.clients, i, 42);
663: }
664: io_arrayDestroy(&srv->srv_blob.clients);
1.2 misho 665:
1.10 misho 666: close(srv->srv_blob.server.cli_sock);
1.2 misho 667:
1.10 misho 668: /* detach blobs */
669: TAILQ_FOREACH_SAFE(b, &srv->srv_blob.blobs, blob_node, tmp) {
670: TAILQ_REMOVE(&srv->srv_blob.blobs, b, blob_node);
1.2 misho 671:
1.10 misho 672: rpc_srv_blobFree(srv, b);
1.11 misho 673: io_free(b);
1.2 misho 674: }
675:
1.10 misho 676: schedEnd(&srv->srv_blob.root);
677: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 678: return 0;
679: }
680:
681:
682: /*
1.7 misho 683: * rpc_srv_initServer() - Init & create RPC Server
684: *
1.1 misho 685: * @regProgID = ProgramID for authentication & recognition
686: * @regProcID = ProcessID for authentication & recognition
687: * @concurentClients = Concurent clients at same time to this server
1.10 misho 688: * @netBuf = Network buffer length (min:512 bytes), if =0 == BUFSIZ (also meaning max RPC packet)
1.4 misho 689: * @csHost = Host name or address for bind server, if NULL any address
1.1 misho 690: * @Port = Port for bind server, if Port == 0 default port is selected
691: * return: NULL == error or !=NULL bind and created RPC server instance
692: */
693: rpc_srv_t *
1.10 misho 694: rpc_srv_initServer(u_int regProgID, u_char regProcID, int concurentClients,
695: int netBuf, const char *csHost, u_short Port)
1.1 misho 696: {
1.10 misho 697: int n = 1;
1.1 misho 698: rpc_srv_t *srv = NULL;
1.11 misho 699: io_sockaddr_t sa = IO_SOCKADDR_INIT;
1.1 misho 700:
1.10 misho 701: if (!concurentClients || !regProgID) {
702: rpc_SetErr(EINVAL, "Invalid parameters can`t init RPC server");
1.1 misho 703: return NULL;
704: }
1.10 misho 705: if (!io_gethostbyname(csHost, Port, &sa))
706: return NULL;
1.1 misho 707: if (!Port)
708: Port = RPC_DEFPORT;
1.10 misho 709: if (netBuf < RPC_MIN_BUFSIZ)
1.5 misho 710: netBuf = BUFSIZ;
1.7 misho 711: else
1.11.2.3! misho 712: netBuf = io_align(netBuf, 2); /* align netBuf length */
1.10 misho 713:
714: #ifdef HAVE_SRANDOMDEV
715: srandomdev();
716: #else
717: time_t tim;
718:
719: srandom((time(&tim) ^ getpid()));
720: #endif
1.1 misho 721:
1.11 misho 722: srv = io_malloc(sizeof(rpc_srv_t));
1.1 misho 723: if (!srv) {
724: LOGERR;
725: return NULL;
726: } else
727: memset(srv, 0, sizeof(rpc_srv_t));
728:
1.5 misho 729: srv->srv_netbuf = netBuf;
1.1 misho 730: srv->srv_session.sess_version = RPC_VERSION;
731: srv->srv_session.sess_program = regProgID;
732: srv->srv_session.sess_process = regProcID;
733:
734: srv->srv_server.cli_parent = srv;
1.10 misho 735: memcpy(&srv->srv_server.cli_sa, &sa, sizeof srv->srv_server.cli_sa);
736:
737: /* init functions list */
738: TAILQ_INIT(&srv->srv_funcs);
739:
740: /* init scheduler */
741: srv->srv_root = schedBegin();
742: if (!srv->srv_root) {
743: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
1.11 misho 744: io_free(srv);
1.10 misho 745: return NULL;
746: }
747:
748: /* init pool for clients */
749: srv->srv_clients = io_arrayInit(concurentClients);
750: if (!srv->srv_clients) {
751: rpc_SetErr(io_GetErrno(), "%s", io_GetError());
752: schedEnd(&srv->srv_root);
1.11 misho 753: io_free(srv);
1.10 misho 754: return NULL;
755: }
1.4 misho 756:
757: /* create server socket */
1.10 misho 758: srv->srv_server.cli_sock = socket(srv->srv_server.cli_sa.sa.sa_family, SOCK_STREAM, 0);
1.1 misho 759: if (srv->srv_server.cli_sock == -1) {
760: LOGERR;
1.10 misho 761: io_arrayDestroy(&srv->srv_clients);
762: schedEnd(&srv->srv_root);
1.11 misho 763: io_free(srv);
1.1 misho 764: return NULL;
765: }
766: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
767: LOGERR;
1.10 misho 768: goto err;
1.1 misho 769: }
1.5 misho 770: n = srv->srv_netbuf;
771: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof n) == -1) {
772: LOGERR;
1.10 misho 773: goto err;
1.5 misho 774: }
775: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_RCVBUF, &n, sizeof n) == -1) {
776: LOGERR;
1.10 misho 777: goto err;
1.5 misho 778: }
1.6 misho 779: if (bind(srv->srv_server.cli_sock, &srv->srv_server.cli_sa.sa,
780: srv->srv_server.cli_sa.sa.sa_len) == -1) {
1.1 misho 781: LOGERR;
1.10 misho 782: goto err;
1.1 misho 783: }
784:
1.10 misho 785: rpc_register_srvPing(srv);
1.8 misho 786:
1.1 misho 787: return srv;
1.10 misho 788: err: /* error condition */
789: close(srv->srv_server.cli_sock);
790: io_arrayDestroy(&srv->srv_clients);
791: schedEnd(&srv->srv_root);
1.11 misho 792: io_free(srv);
1.10 misho 793: return NULL;
1.1 misho 794: }
795:
796: /*
1.7 misho 797: * rpc_srv_endServer() - Destroy RPC server, close all opened sockets and free resources
798: *
1.6 misho 799: * @psrv = RPC Server instance
1.1 misho 800: * return: none
801: */
1.10 misho 802: inline void
1.6 misho 803: rpc_srv_endServer(rpc_srv_t ** __restrict psrv)
1.1 misho 804: {
1.10 misho 805: if (!psrv || !*psrv)
1.1 misho 806: return;
807:
1.10 misho 808: /* if send kill to blob server */
809: if (!(*psrv)->srv_blob.kill)
810: rpc_srv_endBLOBServer(*psrv);
1.1 misho 811:
1.10 misho 812: (*psrv)->srv_kill = 1;
813: sleep(RPC_SCHED_POLLING);
1.2 misho 814:
1.11 misho 815: io_free(*psrv);
1.6 misho 816: *psrv = NULL;
1.1 misho 817: }
818:
819: /*
1.7 misho 820: * rpc_srv_loopServer() - Execute Main server loop and wait for clients requests
821: *
1.1 misho 822: * @srv = RPC Server instance
823: * return: -1 error or 0 ok, infinite loop ...
824: */
825: int
1.5 misho 826: rpc_srv_loopServer(rpc_srv_t * __restrict srv)
1.1 misho 827: {
1.10 misho 828: rpc_cli_t *c;
1.1 misho 829: register int i;
1.10 misho 830: rpc_func_t *f, *tmp;
831: struct timespec ts = { RPC_SCHED_POLLING, 0 };
1.1 misho 832:
833: if (!srv) {
1.10 misho 834: rpc_SetErr(EINVAL, "Invalid parameter can`t start RPC server");
1.1 misho 835: return -1;
836: }
837:
1.10 misho 838: fcntl(srv->srv_server.cli_sock, F_SETFL,
839: fcntl(srv->srv_server.cli_sock, F_GETFL) | O_NONBLOCK);
1.5 misho 840:
1.10 misho 841: if (listen(srv->srv_server.cli_sock, io_arraySize(srv->srv_clients)) == -1) {
1.1 misho 842: LOGERR;
843: return -1;
1.10 misho 844: }
1.1 misho 845:
1.10 misho 846: if (!schedRead(srv->srv_root, acceptClients, srv, srv->srv_server.cli_sock, NULL, 0)) {
847: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
848: return -1;
849: }
1.7 misho 850:
1.10 misho 851: schedPolling(srv->srv_root, &ts, NULL);
1.7 misho 852: /* main rpc loop */
1.10 misho 853: schedRun(srv->srv_root, &srv->srv_kill);
854:
855: /* close all clients connections & server socket */
856: for (i = 0; i < io_arraySize(srv->srv_clients); i++) {
857: c = io_array(srv->srv_clients, i, rpc_cli_t*);
858: if (c) {
859: shutdown(c->cli_sock, SHUT_RDWR);
860: close(c->cli_sock);
861:
862: schedCancelby(srv->srv_root, taskMAX, CRITERIA_ARG, c, NULL);
863: io_freeVars(&RPC_RETVARS(c));
864: AIT_FREE_VAL(&c->cli_buf);
1.1 misho 865: }
1.10 misho 866: io_arrayDel(srv->srv_clients, i, 42);
867: }
868: io_arrayDestroy(&srv->srv_clients);
1.2 misho 869:
1.10 misho 870: close(srv->srv_server.cli_sock);
1.2 misho 871:
1.10 misho 872: /* detach exported calls */
873: TAILQ_FOREACH_SAFE(f, &srv->srv_funcs, func_node, tmp) {
874: TAILQ_REMOVE(&srv->srv_funcs, f, func_node);
1.1 misho 875:
1.10 misho 876: AIT_FREE_VAL(&f->func_name);
1.11 misho 877: io_free(f);
1.1 misho 878: }
879:
1.10 misho 880: schedEnd(&srv->srv_root);
1.1 misho 881: return 0;
882: }
883:
884:
885: /*
886: * rpc_srv_execCall() Execute registered call from RPC server
1.7 misho 887: *
1.10 misho 888: * @cli = RPC client
1.1 misho 889: * @rpc = IN RPC call structure
1.10 misho 890: * @funcname = Execute RPC function
1.5 misho 891: * @args = IN RPC calling arguments from RPC client
1.1 misho 892: * return: -1 error, !=-1 ok
893: */
894: int
1.10 misho 895: rpc_srv_execCall(rpc_cli_t * __restrict cli, struct tagRPCCall * __restrict rpc,
896: ait_val_t funcname, array_t * __restrict args)
1.1 misho 897: {
898: rpc_callback_t func;
899:
1.10 misho 900: if (!cli || !rpc || !AIT_ADDR(&funcname)) {
1.7 misho 901: rpc_SetErr(EINVAL, "Invalid parameter can`t exec function");
1.1 misho 902: return -1;
903: }
904:
1.10 misho 905: func = AIT_GET_LIKE(&funcname, rpc_callback_t);
906: return func(cli, rpc, args);
1.1 misho 907: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>