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