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