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