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