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