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