Annotation of libaitrpc/src/srv.c, revision 1.11.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.11.2.1! misho 6: * $Id: srv.c,v 1.11 2012/07/22 20:44:13 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.1! misho 449: int n = 1;
1.7 misho 450:
1.10 misho 451: /* check free slots for connect */
452: for (i = 0; i < io_arraySize(srv->srv_blob.clients) &&
453: (c = io_array(srv->srv_blob.clients, i, rpc_cli_t*)); i++);
454: if (c) /* no more free slots! */
455: goto end;
1.11 misho 456: c = io_malloc(sizeof(rpc_cli_t));
1.10 misho 457: if (!c) {
1.7 misho 458: LOGERR;
1.10 misho 459: srv->srv_kill = srv->srv_blob.kill = 1;
1.7 misho 460: return NULL;
461: } else {
1.10 misho 462: memset(c, 0, sizeof(rpc_cli_t));
463: io_arraySet(srv->srv_blob.clients, i, c);
464: c->cli_id = i;
465: c->cli_parent = srv;
1.7 misho 466: }
1.4 misho 467:
1.10 misho 468: /* alloc empty buffer */
469: AIT_SET_BUF2(&c->cli_buf, 0, srv->srv_netbuf);
1.2 misho 470:
1.10 misho 471: /* accept client */
472: c->cli_sock = accept(TASK_FD(task), &c->cli_sa.sa, &salen);
473: if (c->cli_sock == -1) {
474: LOGERR;
475: AIT_FREE_VAL(&c->cli_buf);
476: io_arrayDel(srv->srv_blob.clients, i, 42);
477: goto end;
1.11.2.1! misho 478: } else {
! 479: setsockopt(c->cli_sock, IPPROTO_TCP, TCP_NOPUSH, &n, sizeof n);
1.10 misho 480: fcntl(c->cli_sock, F_SETFL, fcntl(c->cli_sock, F_GETFL) | O_NONBLOCK);
1.11.2.1! misho 481: }
1.2 misho 482:
1.10 misho 483: schedRead(TASK_ROOT(task), rxBLOB, c, c->cli_sock, NULL, 0);
484: end:
485: schedReadSelf(task);
1.7 misho 486: return NULL;
1.1 misho 487: }
488:
1.10 misho 489: /* ------------------------------------------------------ */
1.1 misho 490:
491: /*
1.7 misho 492: * rpc_srv_initBLOBServer() - Init & create BLOB Server
493: *
1.4 misho 494: * @srv = RPC server instance
1.2 misho 495: * @Port = Port for bind server, if Port == 0 default port is selected
496: * @diskDir = Disk place for BLOB file objects
497: * return: -1 == error or 0 bind and created BLOB server instance
498: */
499: int
500: rpc_srv_initBLOBServer(rpc_srv_t * __restrict srv, u_short Port, const char *diskDir)
501: {
502: int n = 1;
503:
1.10 misho 504: if (!srv || srv->srv_kill) {
1.7 misho 505: rpc_SetErr(EINVAL, "Invalid parameters can`t init BLOB server");
1.2 misho 506: return -1;
507: }
508:
509: memset(&srv->srv_blob, 0, sizeof srv->srv_blob);
510: if (access(diskDir, R_OK | W_OK) == -1) {
511: LOGERR;
512: return -1;
513: } else
1.7 misho 514: AIT_SET_STR(&srv->srv_blob.dir, diskDir);
1.2 misho 515:
1.10 misho 516: /* init blob list */
517: TAILQ_INIT(&srv->srv_blob.blobs);
518:
1.2 misho 519: srv->srv_blob.server.cli_parent = srv;
1.4 misho 520:
1.10 misho 521: memcpy(&srv->srv_blob.server.cli_sa, &srv->srv_server.cli_sa, sizeof(io_sockaddr_t));
522: switch (srv->srv_blob.server.cli_sa.sa.sa_family) {
1.4 misho 523: case AF_INET:
1.10 misho 524: srv->srv_blob.server.cli_sa.sin.sin_port =
525: htons(Port ? Port : ntohs(srv->srv_blob.server.cli_sa.sin.sin_port) + 1);
1.4 misho 526: break;
527: case AF_INET6:
1.10 misho 528: srv->srv_blob.server.cli_sa.sin6.sin6_port =
529: htons(Port ? Port : ntohs(srv->srv_blob.server.cli_sa.sin6.sin6_port) + 1);
1.4 misho 530: break;
531: case AF_LOCAL:
1.10 misho 532: strlcat(srv->srv_blob.server.cli_sa.sun.sun_path, ".blob",
533: sizeof srv->srv_blob.server.cli_sa.sun.sun_path);
1.4 misho 534: break;
535: default:
1.7 misho 536: AIT_FREE_VAL(&srv->srv_blob.dir);
1.4 misho 537: return -1;
1.2 misho 538: }
539:
1.4 misho 540: /* create BLOB server socket */
1.6 misho 541: srv->srv_blob.server.cli_sock = socket(srv->srv_server.cli_sa.sa.sa_family, SOCK_STREAM, 0);
1.2 misho 542: if (srv->srv_blob.server.cli_sock == -1) {
543: LOGERR;
1.7 misho 544: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 545: return -1;
546: }
547: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
548: LOGERR;
549: close(srv->srv_blob.server.cli_sock);
1.7 misho 550: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 551: return -1;
552: }
1.5 misho 553: n = srv->srv_netbuf;
554: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_SNDBUF, &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.5 misho 558: return -1;
559: }
560: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_RCVBUF, &n, sizeof n) == -1) {
561: LOGERR;
562: close(srv->srv_blob.server.cli_sock);
1.7 misho 563: AIT_FREE_VAL(&srv->srv_blob.dir);
1.5 misho 564: return -1;
565: }
1.6 misho 566: if (bind(srv->srv_blob.server.cli_sock, &srv->srv_blob.server.cli_sa.sa,
567: srv->srv_blob.server.cli_sa.sa.sa_len) == -1) {
1.2 misho 568: LOGERR;
569: close(srv->srv_blob.server.cli_sock);
1.7 misho 570: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 571: return -1;
572: }
573:
1.10 misho 574: /* allocate pool for concurent blob clients */
575: srv->srv_blob.clients = io_arrayInit(io_arraySize(srv->srv_clients));
1.2 misho 576: if (!srv->srv_blob.clients) {
1.10 misho 577: rpc_SetErr(io_GetErrno(), "%s", io_GetError());
1.2 misho 578: close(srv->srv_blob.server.cli_sock);
1.7 misho 579: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 580: return -1;
1.10 misho 581: }
1.2 misho 582:
1.10 misho 583: /* init blob scheduler */
584: srv->srv_blob.root = schedBegin();
585: if (!srv->srv_blob.root) {
586: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
587: io_arrayDestroy(&srv->srv_blob.clients);
588: close(srv->srv_blob.server.cli_sock);
589: AIT_FREE_VAL(&srv->srv_blob.dir);
590: return -1;
591: }
1.2 misho 592:
593: return 0;
594: }
595:
596: /*
1.7 misho 597: * rpc_srv_endBLOBServer() - Destroy BLOB server, close all opened sockets and free resources
598: *
1.2 misho 599: * @srv = RPC Server instance
600: * return: none
601: */
1.10 misho 602: inline void
1.2 misho 603: rpc_srv_endBLOBServer(rpc_srv_t * __restrict srv)
604: {
1.10 misho 605: if (!srv)
1.2 misho 606: return;
607:
1.10 misho 608: srv->srv_blob.kill = 1;
1.2 misho 609: }
610:
611: /*
1.10 misho 612: * rpc_srv_loopBLOBServer() - Execute Main BLOB server loop and wait for clients requests
1.7 misho 613: *
1.2 misho 614: * @srv = RPC Server instance
615: * return: -1 error or 0 ok, infinite loop ...
616: */
617: int
1.10 misho 618: rpc_srv_loopBLOBServer(rpc_srv_t * __restrict srv)
1.2 misho 619: {
1.10 misho 620: rpc_cli_t *c;
1.2 misho 621: register int i;
1.10 misho 622: rpc_blob_t *b, *tmp;
623: struct timespec ts = { RPC_SCHED_POLLING, 0 };
1.2 misho 624:
1.10 misho 625: if (!srv || srv->srv_kill) {
1.7 misho 626: rpc_SetErr(EINVAL, "Invalid parameter can`t start BLOB server");
1.2 misho 627: return -1;
628: }
629:
1.10 misho 630: fcntl(srv->srv_blob.server.cli_sock, F_SETFL,
631: fcntl(srv->srv_blob.server.cli_sock, F_GETFL) | O_NONBLOCK);
632:
633: if (listen(srv->srv_blob.server.cli_sock, io_arraySize(srv->srv_blob.clients)) == -1) {
1.2 misho 634: LOGERR;
635: return -1;
1.10 misho 636: }
637:
638: if (!schedRead(srv->srv_blob.root, acceptBLOBClients, srv,
639: srv->srv_blob.server.cli_sock, NULL, 0)) {
640: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
641: return -1;
642: }
1.2 misho 643:
1.10 misho 644: schedPolling(srv->srv_blob.root, &ts, NULL);
645: /* main rpc loop */
646: schedRun(srv->srv_blob.root, &srv->srv_blob.kill);
1.7 misho 647:
1.10 misho 648: /* close all clients connections & server socket */
649: for (i = 0; i < io_arraySize(srv->srv_blob.clients); i++) {
650: c = io_array(srv->srv_blob.clients, i, rpc_cli_t*);
651: if (c) {
652: shutdown(c->cli_sock, SHUT_RDWR);
653: close(c->cli_sock);
654:
655: schedCancelby(srv->srv_blob.root, taskMAX, CRITERIA_ARG, c, NULL);
656: AIT_FREE_VAL(&c->cli_buf);
1.2 misho 657: }
1.10 misho 658: io_arrayDel(srv->srv_blob.clients, i, 42);
659: }
660: io_arrayDestroy(&srv->srv_blob.clients);
1.2 misho 661:
1.10 misho 662: close(srv->srv_blob.server.cli_sock);
1.2 misho 663:
1.10 misho 664: /* detach blobs */
665: TAILQ_FOREACH_SAFE(b, &srv->srv_blob.blobs, blob_node, tmp) {
666: TAILQ_REMOVE(&srv->srv_blob.blobs, b, blob_node);
1.2 misho 667:
1.10 misho 668: rpc_srv_blobFree(srv, b);
1.11 misho 669: io_free(b);
1.2 misho 670: }
671:
1.10 misho 672: schedEnd(&srv->srv_blob.root);
673: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 674: return 0;
675: }
676:
677:
678: /*
1.7 misho 679: * rpc_srv_initServer() - Init & create RPC Server
680: *
1.1 misho 681: * @regProgID = ProgramID for authentication & recognition
682: * @regProcID = ProcessID for authentication & recognition
683: * @concurentClients = Concurent clients at same time to this server
1.10 misho 684: * @netBuf = Network buffer length (min:512 bytes), if =0 == BUFSIZ (also meaning max RPC packet)
1.4 misho 685: * @csHost = Host name or address for bind server, if NULL any address
1.1 misho 686: * @Port = Port for bind server, if Port == 0 default port is selected
687: * return: NULL == error or !=NULL bind and created RPC server instance
688: */
689: rpc_srv_t *
1.10 misho 690: rpc_srv_initServer(u_int regProgID, u_char regProcID, int concurentClients,
691: int netBuf, const char *csHost, u_short Port)
1.1 misho 692: {
1.10 misho 693: int n = 1;
1.1 misho 694: rpc_srv_t *srv = NULL;
1.11 misho 695: io_sockaddr_t sa = IO_SOCKADDR_INIT;
1.1 misho 696:
1.10 misho 697: if (!concurentClients || !regProgID) {
698: rpc_SetErr(EINVAL, "Invalid parameters can`t init RPC server");
1.1 misho 699: return NULL;
700: }
1.10 misho 701: if (!io_gethostbyname(csHost, Port, &sa))
702: return NULL;
1.1 misho 703: if (!Port)
704: Port = RPC_DEFPORT;
1.10 misho 705: if (netBuf < RPC_MIN_BUFSIZ)
1.5 misho 706: netBuf = BUFSIZ;
1.7 misho 707: else
708: netBuf = io_align(netBuf, 1); /* align netBuf length */
1.10 misho 709:
710: #ifdef HAVE_SRANDOMDEV
711: srandomdev();
712: #else
713: time_t tim;
714:
715: srandom((time(&tim) ^ getpid()));
716: #endif
1.1 misho 717:
1.11 misho 718: srv = io_malloc(sizeof(rpc_srv_t));
1.1 misho 719: if (!srv) {
720: LOGERR;
721: return NULL;
722: } else
723: memset(srv, 0, sizeof(rpc_srv_t));
724:
1.5 misho 725: srv->srv_netbuf = netBuf;
1.1 misho 726: srv->srv_session.sess_version = RPC_VERSION;
727: srv->srv_session.sess_program = regProgID;
728: srv->srv_session.sess_process = regProcID;
729:
730: srv->srv_server.cli_parent = srv;
1.10 misho 731: memcpy(&srv->srv_server.cli_sa, &sa, sizeof srv->srv_server.cli_sa);
732:
733: /* init functions list */
734: TAILQ_INIT(&srv->srv_funcs);
735:
736: /* init scheduler */
737: srv->srv_root = schedBegin();
738: if (!srv->srv_root) {
739: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
1.11 misho 740: io_free(srv);
1.10 misho 741: return NULL;
742: }
743:
744: /* init pool for clients */
745: srv->srv_clients = io_arrayInit(concurentClients);
746: if (!srv->srv_clients) {
747: rpc_SetErr(io_GetErrno(), "%s", io_GetError());
748: schedEnd(&srv->srv_root);
1.11 misho 749: io_free(srv);
1.10 misho 750: return NULL;
751: }
1.4 misho 752:
753: /* create server socket */
1.10 misho 754: srv->srv_server.cli_sock = socket(srv->srv_server.cli_sa.sa.sa_family, SOCK_STREAM, 0);
1.1 misho 755: if (srv->srv_server.cli_sock == -1) {
756: LOGERR;
1.10 misho 757: io_arrayDestroy(&srv->srv_clients);
758: schedEnd(&srv->srv_root);
1.11 misho 759: io_free(srv);
1.1 misho 760: return NULL;
761: }
762: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
763: LOGERR;
1.10 misho 764: goto err;
1.1 misho 765: }
1.5 misho 766: n = srv->srv_netbuf;
767: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof n) == -1) {
768: LOGERR;
1.10 misho 769: goto err;
1.5 misho 770: }
771: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_RCVBUF, &n, sizeof n) == -1) {
772: LOGERR;
1.10 misho 773: goto err;
1.5 misho 774: }
1.6 misho 775: if (bind(srv->srv_server.cli_sock, &srv->srv_server.cli_sa.sa,
776: srv->srv_server.cli_sa.sa.sa_len) == -1) {
1.1 misho 777: LOGERR;
1.10 misho 778: goto err;
1.1 misho 779: }
780:
1.10 misho 781: rpc_register_srvPing(srv);
1.8 misho 782:
1.1 misho 783: return srv;
1.10 misho 784: err: /* error condition */
785: close(srv->srv_server.cli_sock);
786: io_arrayDestroy(&srv->srv_clients);
787: schedEnd(&srv->srv_root);
1.11 misho 788: io_free(srv);
1.10 misho 789: return NULL;
1.1 misho 790: }
791:
792: /*
1.7 misho 793: * rpc_srv_endServer() - Destroy RPC server, close all opened sockets and free resources
794: *
1.6 misho 795: * @psrv = RPC Server instance
1.1 misho 796: * return: none
797: */
1.10 misho 798: inline void
1.6 misho 799: rpc_srv_endServer(rpc_srv_t ** __restrict psrv)
1.1 misho 800: {
1.10 misho 801: if (!psrv || !*psrv)
1.1 misho 802: return;
803:
1.10 misho 804: /* if send kill to blob server */
805: if (!(*psrv)->srv_blob.kill)
806: rpc_srv_endBLOBServer(*psrv);
1.1 misho 807:
1.10 misho 808: (*psrv)->srv_kill = 1;
809: sleep(RPC_SCHED_POLLING);
1.2 misho 810:
1.11 misho 811: io_free(*psrv);
1.6 misho 812: *psrv = NULL;
1.1 misho 813: }
814:
815: /*
1.7 misho 816: * rpc_srv_loopServer() - Execute Main server loop and wait for clients requests
817: *
1.1 misho 818: * @srv = RPC Server instance
819: * return: -1 error or 0 ok, infinite loop ...
820: */
821: int
1.5 misho 822: rpc_srv_loopServer(rpc_srv_t * __restrict srv)
1.1 misho 823: {
1.10 misho 824: rpc_cli_t *c;
1.1 misho 825: register int i;
1.10 misho 826: rpc_func_t *f, *tmp;
827: struct timespec ts = { RPC_SCHED_POLLING, 0 };
1.1 misho 828:
829: if (!srv) {
1.10 misho 830: rpc_SetErr(EINVAL, "Invalid parameter can`t start RPC server");
1.1 misho 831: return -1;
832: }
833:
1.10 misho 834: fcntl(srv->srv_server.cli_sock, F_SETFL,
835: fcntl(srv->srv_server.cli_sock, F_GETFL) | O_NONBLOCK);
1.5 misho 836:
1.10 misho 837: if (listen(srv->srv_server.cli_sock, io_arraySize(srv->srv_clients)) == -1) {
1.1 misho 838: LOGERR;
839: return -1;
1.10 misho 840: }
1.1 misho 841:
1.10 misho 842: if (!schedRead(srv->srv_root, acceptClients, srv, srv->srv_server.cli_sock, NULL, 0)) {
843: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
844: return -1;
845: }
1.7 misho 846:
1.10 misho 847: schedPolling(srv->srv_root, &ts, NULL);
1.7 misho 848: /* main rpc loop */
1.10 misho 849: schedRun(srv->srv_root, &srv->srv_kill);
850:
851: /* close all clients connections & server socket */
852: for (i = 0; i < io_arraySize(srv->srv_clients); i++) {
853: c = io_array(srv->srv_clients, i, rpc_cli_t*);
854: if (c) {
855: shutdown(c->cli_sock, SHUT_RDWR);
856: close(c->cli_sock);
857:
858: schedCancelby(srv->srv_root, taskMAX, CRITERIA_ARG, c, NULL);
859: io_freeVars(&RPC_RETVARS(c));
860: AIT_FREE_VAL(&c->cli_buf);
1.1 misho 861: }
1.10 misho 862: io_arrayDel(srv->srv_clients, i, 42);
863: }
864: io_arrayDestroy(&srv->srv_clients);
1.2 misho 865:
1.10 misho 866: close(srv->srv_server.cli_sock);
1.2 misho 867:
1.10 misho 868: /* detach exported calls */
869: TAILQ_FOREACH_SAFE(f, &srv->srv_funcs, func_node, tmp) {
870: TAILQ_REMOVE(&srv->srv_funcs, f, func_node);
1.1 misho 871:
1.10 misho 872: AIT_FREE_VAL(&f->func_name);
1.11 misho 873: io_free(f);
1.1 misho 874: }
875:
1.10 misho 876: schedEnd(&srv->srv_root);
1.1 misho 877: return 0;
878: }
879:
880:
881: /*
882: * rpc_srv_execCall() Execute registered call from RPC server
1.7 misho 883: *
1.10 misho 884: * @cli = RPC client
1.1 misho 885: * @rpc = IN RPC call structure
1.10 misho 886: * @funcname = Execute RPC function
1.5 misho 887: * @args = IN RPC calling arguments from RPC client
1.1 misho 888: * return: -1 error, !=-1 ok
889: */
890: int
1.10 misho 891: rpc_srv_execCall(rpc_cli_t * __restrict cli, struct tagRPCCall * __restrict rpc,
892: ait_val_t funcname, array_t * __restrict args)
1.1 misho 893: {
894: rpc_callback_t func;
895:
1.10 misho 896: if (!cli || !rpc || !AIT_ADDR(&funcname)) {
1.7 misho 897: rpc_SetErr(EINVAL, "Invalid parameter can`t exec function");
1.1 misho 898: return -1;
899: }
900:
1.10 misho 901: func = AIT_GET_LIKE(&funcname, rpc_callback_t);
902: return func(cli, rpc, args);
1.1 misho 903: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>