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