Annotation of libaitrpc/src/srv.c, revision 1.2.2.1
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.2.2.1 ! misho 6: * $Id: srv.c,v 1.2 2011/05/02 23:12:08 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:
15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
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:
49: static void *
50: rpc_srv_dispatchCall(void *arg)
51: {
52: rpc_cli_t *c = arg;
53: rpc_srv_t *s;
1.2 misho 54: rpc_val_t *vals = NULL, *v = NULL;
1.1 misho 55: rpc_func_t *f;
56: struct tagRPCCall *rpc;
57: struct tagRPCRet rrpc;
58: fd_set fds;
59: u_char buf[BUFSIZ], *data;
1.2 misho 60: int ret, argc = 0, Limit = 0;
1.1 misho 61: register int i;
62:
63: if (!arg) {
64: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t procced RPC client ...\n");
65: return NULL;
66: } else
67: s = c->cli_parent;
68:
69: do {
1.2.2.1 ! misho 70: v = NULL;
1.1 misho 71: FD_ZERO(&fds);
72: FD_SET(c->cli_sock, &fds);
73: ret = select(c->cli_sock + 1, &fds, NULL, NULL, NULL);
74: if (ret == -1) {
75: ret = -2;
76: }
77: memset(&rrpc, 0, sizeof rrpc);
78: memset(buf, 0, BUFSIZ);
79: if ((ret = recv(c->cli_sock, buf, BUFSIZ, 0)) == -1) {
80: LOGERR;
81: ret = -3;
82: break;
83: }
84: if (!ret) { // receive EOF
85: ret = 0;
86: break;
87: }
88: if (ret < sizeof(struct tagRPCCall)) {
89: rpc_SetErr(EMSGSIZE, "Error:: too short RPC packet ...\n");
90: ret = -4;
91: break;
92: } else
93: rpc = (struct tagRPCCall*) buf;
94: // check RPC packet session info
95: if (memcmp(&rpc->call_session, &s->srv_session, sizeof rpc->call_session)) {
96: rpc_SetErr(EINVAL, "Error:: get invalid RPC session ...\n");
97: ret = -5;
1.2 misho 98: goto makeReply;
99: } else
100: Limit = sizeof(struct tagRPCCall);
1.1 misho 101: // RPC is OK! Go decapsulate variables ...
102: if (rpc->call_argc) {
1.2 misho 103: v = (rpc_val_t*) (buf + Limit);
104: // check RPC packet length
105: if (rpc->call_argc * sizeof(rpc_val_t) > BUFSIZ - Limit) {
106: rpc_SetErr(EMSGSIZE, "Error:: Too big RPC packet ...\n");
107: ret = -5;
108: goto makeReply;
109: } else
110: Limit += rpc->call_argc * sizeof(rpc_val_t);
1.1 misho 111: // RPC received variables types OK!
112: data = (u_char*) v + rpc->call_argc * sizeof(rpc_val_t);
113: for (i = 0; i < rpc->call_argc; i++) {
114: switch (v[i].val_type) {
115: case buffer:
1.2 misho 116: if (v[i].val_len > BUFSIZ - Limit) {
117: rpc_SetErr(EMSGSIZE, "Error:: Too big RPC packet ...\n");
118: ret = -5;
119: goto makeReply;
120: } else
121: Limit += v[i].val_len;
122:
1.1 misho 123: v[i].val.buffer = data;
124: data += v[i].val_len;
125: break;
126: case string:
1.2.2.1 ! misho 127: if (v[i].val_len > BUFSIZ - Limit) {
1.2 misho 128: rpc_SetErr(EMSGSIZE, "Error:: Too big RPC packet ...\n");
129: ret = -5;
130: goto makeReply;
131: } else
132: Limit += v[i].val_len;
133:
1.1 misho 134: v[i].val.string = (int8_t*) data;
1.2.2.1 ! misho 135: data += v[i].val_len;
1.1 misho 136: break;
1.2 misho 137: case blob:
138: if (s->srv_blob.state == disable) {
139: rpc_SetErr(ENOTSUP, "Error:: BLOB server is disabled\n");
140: ret = -5;
141: goto makeReply;
142: }
1.1 misho 143: default:
144: break;
145: }
146: }
147: }
148:
149: argc = 0;
150: vals = NULL;
151: if (!(f = rpc_srv_getCall(s, rpc->call_tag, rpc->call_hash))) {
152: rpc_SetErr(EINVAL, "Error:: call not found into RPC server ...\n");
153: ret = -6;
154: } else
1.2 misho 155: if ((ret = rpc_srv_execCall(f, rpc, v)) == -1)
156: ret = -9;
1.1 misho 157: else
158: argc = rpc_srv_getValsCall(f, &vals);
159:
1.2 misho 160: makeReply:
1.1 misho 161: memcpy(&rrpc.ret_session, &rpc->call_session, sizeof rrpc.ret_session);
162: rrpc.ret_tag = rpc->call_tag;
163: rrpc.ret_hash = rpc->call_hash;
164: rrpc.ret_errno = rpc_Errno;
165: rrpc.ret_retcode = ret;
166: rrpc.ret_argc = argc;
167:
168: memset(buf, 0, BUFSIZ);
169: memcpy(buf, &rrpc, (Limit = sizeof rrpc));
170: if (argc && vals) {
171: v = (rpc_val_t*) (buf + sizeof rrpc);
172: memcpy(v, vals, argc * sizeof(rpc_val_t));
173: Limit += argc * sizeof(rpc_val_t);
174: data = (u_char*) v + argc * sizeof(rpc_val_t);
175: for (ret = i = 0; i < argc; i++) {
176: switch (vals[i].val_type) {
177: case buffer:
178: if (ret || Limit + vals[i].val_len > BUFSIZ) {
179: rpc_SetErr(EMSGSIZE, "Error:: in prepare RPC packet (-7) ...\n");
180: rrpc.ret_retcode = ret = -7;
181: rrpc.ret_argc = 0;
182: break;
183: }
184:
185: memcpy(data, vals[i].val.buffer, vals[i].val_len);
186: data += vals[i].val_len;
187: Limit += vals[i].val_len;
188: break;
189: case string:
190: if (ret || Limit + vals[i].val_len + 1 > BUFSIZ) {
191: rpc_SetErr(EMSGSIZE, "Error:: in prepare RPC packet (-7) ...\n");
192: rrpc.ret_retcode = ret = -7;
193: rrpc.ret_argc = 0;
194: break;
195: }
196:
197: memcpy(data, vals[i].val.string, vals[i].val_len + 1);
198: data += vals[i].val_len + 1;
199: Limit += vals[i].val_len + 1;
200: break;
1.2 misho 201: case blob:
202: if (s->srv_blob.state == disable) {
203: rpc_SetErr(ENOTSUP, "Error:: BLOB server is disabled\n");
204: rrpc.ret_retcode = ret = -5;
1.1 misho 205: rrpc.ret_argc = 0;
206: break;
207: }
208: default:
209: break;
210: }
211:
212: RPC_FREE_VAL(&vals[i]);
213: }
214: }
215:
216: if ((ret = send(c->cli_sock, buf, Limit, 0)) == -1) {
217: LOGERR;
218: ret = -8;
219: break;
220: }
221: if (ret != Limit) {
1.2 misho 222: rpc_SetErr(ECANCELED, "Error:: in send RPC request, should be send %d bytes, "
1.1 misho 223: "really is %d\n", Limit, ret);
224: ret = -9;
225: break;
226: }
227: } while (ret > -1);
228:
229: shutdown(c->cli_sock, SHUT_RDWR);
230: close(c->cli_sock);
231: memset(c, 0, sizeof(rpc_cli_t));
1.2 misho 232: return (void*) (long)ret;
233: }
234:
235:
236: static void *
237: rpc_srv_dispatchVars(void *arg)
238: {
239: rpc_cli_t *c = arg;
240: rpc_srv_t *s;
241: rpc_blob_t *b;
242: int ret;
243: fd_set fds;
244: u_char buf[sizeof(struct tagBLOBHdr)];
245: struct tagBLOBHdr *blob;
246:
247: if (!arg) {
248: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t procced BLOB client ...\n");
249: return NULL;
250: } else
251: s = c->cli_parent;
252:
253: do {
254: // check for disable service at this moment?
255: if (s->srv_blob.state == disable) {
256: ret = 0;
257: break;
258: }
259:
260: FD_ZERO(&fds);
261: FD_SET(c->cli_sock, &fds);
262: ret = select(c->cli_sock + 1, &fds, NULL, NULL, NULL);
263: if (ret == -1) {
264: ret = -2;
265: }
266:
267: memset(buf, 0, sizeof buf);
268: if ((ret = recv(c->cli_sock, buf, sizeof buf, 0)) == -1) {
269: LOGERR;
270: ret = -3;
271: break;
272: }
273: if (!ret || s->srv_blob.state == disable) { // receive EOF or disable service
274: ret = 0;
275: break;
276: }
277: if (ret < sizeof(struct tagBLOBHdr)) {
278: rpc_SetErr(EMSGSIZE, "Error:: too short BLOB packet ...\n");
279: ret = -4;
280: break;
281: } else
282: blob = (struct tagBLOBHdr*) buf;
283: // check BLOB packet session info
284: if (memcmp(&blob->hdr_session, &s->srv_session, sizeof blob->hdr_session)) {
285: rpc_SetErr(EINVAL, "Error:: get invalid BLOB session ...\n");
286: ret = -5;
287: goto makeReply;
288: }
289: // Go to proceed packet ...
290: switch (blob->hdr_cmd) {
291: case get:
292: if (!(b = rpc_srv_getBLOB(s, blob->hdr_var))) {
293: rpc_SetErr(EINVAL, "Error:: var (%x) not found into BLOB server ...\n",
294: blob->hdr_var);
295: ret = -6;
296: break;
297: } else
298: blob->hdr_len = b->blob_len;
299:
300: if (rpc_srv_blobMap(s, b) != -1) {
301: ret = rpc_srv_sendBLOB(c, b);
302: rpc_srv_blobUnmap(b);
303: } else
304: ret = -7;
305: break;
306: case set:
307: if ((b = rpc_srv_registerBLOB(s, blob->hdr_len))) {
308: // set new BLOB variable for reply :)
309: blob->hdr_var = b->blob_var;
310:
311: ret = rpc_srv_recvBLOB(c, b);
312: rpc_srv_blobUnmap(b);
313: } else
314: ret = -7;
315: break;
316: case unset:
317: ret = rpc_srv_unregisterBLOB(s, blob->hdr_var);
318: if (ret == -1)
319: ret = -7;
320: break;
321: default:
322: rpc_SetErr(EINVAL, "Error:: unsupported BLOB command (%d)...\n",
323: blob->hdr_cmd);
324: ret = -7;
325: }
326:
327: makeReply:
328: // Replay to client!
329: blob->hdr_cmd = ret < 0 ? error : ok;
330: blob->hdr_ret = ret;
331: if ((ret = send(c->cli_sock, buf, sizeof buf, 0)) == -1) {
332: LOGERR;
333: ret = -8;
334: break;
335: }
336: if (ret != sizeof buf) {
337: rpc_SetErr(ECANCELED, "Error:: in send BLOB reply, should be send %d bytes, "
338: "really is %d\n", sizeof buf, ret);
339: ret = -9;
340: break;
341: }
342: } while (ret > -1);
343:
344: shutdown(c->cli_sock, SHUT_RDWR);
345: close(c->cli_sock);
346: memset(c, 0, sizeof(rpc_cli_t));
347: return (void*) (long)ret;
1.1 misho 348: }
349:
350: // -------------------------------------------------
351:
352: /*
1.2 misho 353: * rpc_srv_initBLOBServer() Init & create BLOB Server
354: * @Port = Port for bind server, if Port == 0 default port is selected
355: * @diskDir = Disk place for BLOB file objects
356: * return: -1 == error or 0 bind and created BLOB server instance
357: */
358: int
359: rpc_srv_initBLOBServer(rpc_srv_t * __restrict srv, u_short Port, const char *diskDir)
360: {
361: int n = 1;
362: struct sockaddr_in sin;
363: struct sockaddr_in6 sin6;
364:
365: if (!srv) {
366: rpc_SetErr(EINVAL, "Error:: Invalid parameters can`t init BLOB server ...\n");
367: return -1;
368: }
369: if (srv->srv_blob.state) {
370: rpc_SetErr(EPERM, "Warning:: Already started BLOB server!\n");
371: return 0;
372: }
373: if (!Port)
374: Port = RPC_DEFPORT + 1;
375:
376: memset(&srv->srv_blob, 0, sizeof srv->srv_blob);
377: if (access(diskDir, R_OK | W_OK) == -1) {
378: LOGERR;
379: return -1;
380: } else
381: strlcpy(srv->srv_blob.dir, diskDir, UCHAR_MAX + 1);
382:
383: srv->srv_blob.server.cli_tid = pthread_self();
384: srv->srv_blob.server.cli_parent = srv;
385: if (srv->srv_server.cli_sa.sa_family == AF_INET) {
386: memcpy(&sin, &srv->srv_server.cli_sa, sizeof sin);
387: sin.sin_port = htons(Port);
388: memcpy(&srv->srv_blob.server.cli_sa, &sin, sizeof(struct sockaddr));
389: } else {
390: memcpy(&sin6, &srv->srv_server.cli_sa, sizeof sin6);
391: sin6.sin6_port = htons(Port);
392: memcpy(&srv->srv_blob.server.cli_sa, &sin6, sizeof(struct sockaddr));
393: }
394:
395: srv->srv_blob.server.cli_sock = socket(srv->srv_server.cli_sa.sa_family, SOCK_STREAM, 0);
396: if (srv->srv_blob.server.cli_sock == -1) {
397: LOGERR;
398: return -1;
399: }
400: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
401: LOGERR;
402: close(srv->srv_blob.server.cli_sock);
403: return -1;
404: }
405: if (bind(srv->srv_blob.server.cli_sock, &srv->srv_blob.server.cli_sa,
406: sizeof srv->srv_blob.server.cli_sa) == -1) {
407: LOGERR;
408: close(srv->srv_blob.server.cli_sock);
409: return -1;
410: }
411:
412: srv->srv_blob.clients = calloc(srv->srv_numcli, sizeof(rpc_cli_t));
413: if (!srv->srv_blob.clients) {
414: LOGERR;
415: close(srv->srv_blob.server.cli_sock);
416: return -1;
417: } else
418: memset(srv->srv_blob.clients, 0, srv->srv_numcli * sizeof(rpc_cli_t));
419:
420: pthread_mutex_init(&srv->srv_blob.mtx, NULL);
421:
422: pthread_mutex_lock(&srv->srv_mtx);
423: rpc_srv_registerCall(srv, NULL, CALL_BLOBSHUTDOWN, 0);
424: rpc_srv_registerCall(srv, NULL, CALL_BLOBCLIENTS, 0);
425: rpc_srv_registerCall(srv, NULL, CALL_BLOBVARS, 0);
426: rpc_srv_registerCall(srv, NULL, CALL_BLOBSTATE, 1);
427: pthread_mutex_unlock(&srv->srv_mtx);
428:
429: srv->srv_blob.state = enable; // enable BLOB
430: return 0;
431: }
432:
433: /*
434: * rpc_srv_endBLOBServer() Destroy BLOB server, close all opened sockets and free resources
435: * @srv = RPC Server instance
436: * return: none
437: */
438: void
439: rpc_srv_endBLOBServer(rpc_srv_t * __restrict srv)
440: {
441: rpc_cli_t *c;
442: register int i;
443: rpc_blob_t *f;
444:
445: if (!srv) {
446: rpc_SetErr(EINVAL, "Error:: Can`t destroy server because parameter is null!\n");
447: return;
448: } else
449: srv->srv_blob.state = disable;
450:
451: rpc_srv_unregisterCall(srv, NULL, CALL_BLOBSHUTDOWN);
452: rpc_srv_unregisterCall(srv, NULL, CALL_BLOBCLIENTS);
453: rpc_srv_unregisterCall(srv, NULL, CALL_BLOBVARS);
454: rpc_srv_unregisterCall(srv, NULL, CALL_BLOBSTATE);
455:
456: for (i = 0, c = srv->srv_blob.clients; i < srv->srv_numcli && c; i++, c++)
457: if (c->cli_sa.sa_family)
458: shutdown(c->cli_sock, SHUT_RDWR);
459: close(srv->srv_blob.server.cli_sock);
460:
461: if (srv->srv_blob.clients) {
462: free(srv->srv_blob.clients);
463: srv->srv_blob.clients = NULL;
464: }
465:
466: pthread_mutex_lock(&srv->srv_blob.mtx);
467: while ((f = srv->srv_blob.blobs)) {
468: srv->srv_blob.blobs = f->blob_next;
469: rpc_srv_blobFree(srv, f);
470: free(f);
471: }
472: pthread_mutex_unlock(&srv->srv_blob.mtx);
473:
474: while (pthread_mutex_trylock(&srv->srv_blob.mtx) == EBUSY);
475: pthread_mutex_destroy(&srv->srv_blob.mtx);
476: }
477:
478: /*
479: * rpc_srv_execBLOBServer() Execute Main BLOB server loop and wait for clients requests
480: * @srv = RPC Server instance
481: * return: -1 error or 0 ok, infinite loop ...
482: */
483: int
484: rpc_srv_execBLOBServer(rpc_srv_t * __restrict srv)
485: {
486: socklen_t salen = sizeof(struct sockaddr);
487: register int i;
488: rpc_cli_t *c;
489: fd_set fds;
490: int ret;
491: struct timeval tv = { DEF_RPC_TIMEOUT, 0 };
492:
493: if (!srv || srv->srv_blob.state == disable) {
494: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t start BLOB server ...\n");
495: return -1;
496: }
497:
498: if (listen(srv->srv_blob.server.cli_sock, SOMAXCONN) == -1) {
499: LOGERR;
500: return -1;
501: }
502:
503: while (!blob_Kill && !rpc_Kill) {
504: for (c = srv->srv_blob.clients, i = 0; i < srv->srv_numcli && c; i++, c++)
505: if (!c->cli_sa.sa_family)
506: break;
507: if (i >= srv->srv_numcli) {
508: usleep(1000000);
509: continue;
510: }
511:
512: FD_ZERO(&fds);
513: FD_SET(srv->srv_blob.server.cli_sock, &fds);
514: ret = select(srv->srv_blob.server.cli_sock + 1, &fds, NULL, NULL, &tv);
515: if (ret == -1) {
516: LOGERR;
517: ret = 1;
518: break;
519: }
520: if (!ret)
521: continue;
522:
523: c->cli_sock = accept(srv->srv_blob.server.cli_sock, &c->cli_sa, &salen);
524: if (c->cli_sock == -1) {
525: LOGERR;
526: continue;
527: } else
528: c->cli_parent = srv;
529:
530: if (pthread_create(&c->cli_tid, NULL, rpc_srv_dispatchVars, c)) {
531: LOGERR;
532: continue;
533: }
534: }
535:
536: srv->srv_blob.state = disable;
537:
538: return 0;
539: }
540:
541:
542: /*
1.1 misho 543: * rpc_srv_initServer() Init & create RPC Server
544: * @regProgID = ProgramID for authentication & recognition
545: * @regProcID = ProcessID for authentication & recognition
546: * @concurentClients = Concurent clients at same time to this server
547: * @family = Family socket type, AF_INET or AF_INET6
548: * @csHost = Host name or IP address for bind server, if NULL any address
549: * @Port = Port for bind server, if Port == 0 default port is selected
550: * return: NULL == error or !=NULL bind and created RPC server instance
551: */
552: rpc_srv_t *
553: rpc_srv_initServer(u_int regProgID, u_int regProcID, int concurentClients,
554: u_short family, const char *csHost, u_short Port)
555: {
556: rpc_srv_t *srv = NULL;
557: int n = 1;
558: struct hostent *host = NULL;
559: struct sockaddr_in sin;
560: struct sockaddr_in6 sin6;
561:
562: if (!concurentClients || !regProgID || (family != AF_INET && family != AF_INET6)) {
563: rpc_SetErr(EINVAL, "Error:: Invalid parameters can`t init RPC server ...\n");
564: return NULL;
565: }
566: if (!Port)
567: Port = RPC_DEFPORT;
568: if (csHost) {
569: host = gethostbyname2(csHost, family);
570: if (!host) {
571: rpc_SetErr(h_errno, "Error:: %s\n", hstrerror(h_errno));
572: return NULL;
573: }
574: }
575: switch (family) {
576: case AF_INET:
577: memset(&sin, 0, sizeof sin);
578: sin.sin_len = sizeof sin;
579: sin.sin_family = family;
580: sin.sin_port = htons(Port);
581: if (csHost)
582: memcpy(&sin.sin_addr, host->h_addr, host->h_length);
583: break;
584: case AF_INET6:
585: memset(&sin6, 0, sizeof sin6);
586: sin6.sin6_len = sizeof sin6;
587: sin6.sin6_family = family;
588: sin6.sin6_port = htons(Port);
589: if (csHost)
590: memcpy(&sin6.sin6_addr, host->h_addr, host->h_length);
591: break;
592: default:
593: rpc_SetErr(EINVAL, "Error:: Invalid parameters can`t start RPC server ...\n");
594: return NULL;
595: }
596:
597: srv = malloc(sizeof(rpc_srv_t));
598: if (!srv) {
599: LOGERR;
600: return NULL;
601: } else
602: memset(srv, 0, sizeof(rpc_srv_t));
603:
604: srv->srv_numcli = concurentClients;
605: srv->srv_session.sess_version = RPC_VERSION;
606: srv->srv_session.sess_program = regProgID;
607: srv->srv_session.sess_process = regProcID;
608:
1.2 misho 609: srv->srv_server.cli_tid = pthread_self();
1.1 misho 610: srv->srv_server.cli_parent = srv;
611: if (family == AF_INET)
612: memcpy(&srv->srv_server.cli_sa, &sin, sizeof srv->srv_server.cli_sa);
613: else
614: memcpy(&srv->srv_server.cli_sa, &sin6, sizeof srv->srv_server.cli_sa);
615: srv->srv_server.cli_sock = socket(family, SOCK_STREAM, 0);
616: if (srv->srv_server.cli_sock == -1) {
617: LOGERR;
618: free(srv);
619: return NULL;
620: }
621: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
622: LOGERR;
623: close(srv->srv_server.cli_sock);
624: free(srv);
625: return NULL;
626: }
627: if (bind(srv->srv_server.cli_sock, &srv->srv_server.cli_sa, sizeof srv->srv_server.cli_sa) == -1) {
628: LOGERR;
629: close(srv->srv_server.cli_sock);
630: free(srv);
631: return NULL;
632: }
633:
634: srv->srv_clients = calloc(srv->srv_numcli, sizeof(rpc_cli_t));
635: if (!srv->srv_clients) {
636: LOGERR;
637: close(srv->srv_server.cli_sock);
638: free(srv);
639: return NULL;
640: } else
641: memset(srv->srv_clients, 0, srv->srv_numcli * sizeof(rpc_cli_t));
642:
1.2 misho 643: pthread_mutex_init(&srv->srv_mtx, NULL);
644:
645: rpc_srv_registerCall(srv, NULL, CALL_SRVSHUTDOWN, 0);
1.1 misho 646: rpc_srv_registerCall(srv, NULL, CALL_SRVCLIENTS, 0);
647: rpc_srv_registerCall(srv, NULL, CALL_SRVCALLS, 0);
648: rpc_srv_registerCall(srv, NULL, CALL_SRVSESSIONS, 0);
649: return srv;
650: }
651:
652: /*
653: * rpc_srv_endServer() Destroy RPC server, close all opened sockets and free resources
654: * @srv = RPC Server instance
655: * return: none
656: */
657: void
658: rpc_srv_endServer(rpc_srv_t * __restrict srv)
659: {
660: rpc_cli_t *c;
661: register int i;
662: rpc_func_t *f;
663:
664: if (!srv) {
665: rpc_SetErr(EINVAL, "Error:: Can`t destroy server because parameter is null!\n");
666: return;
667: }
668:
1.2 misho 669: rpc_srv_endBLOBServer(srv);
1.1 misho 670:
671: for (i = 0, c = srv->srv_clients; i < srv->srv_numcli && c; i++, c++)
1.2 misho 672: if (c->cli_sa.sa_family) {
1.1 misho 673: shutdown(c->cli_sock, SHUT_RDWR);
1.2 misho 674: close(c->cli_sock);
675: }
1.1 misho 676: close(srv->srv_server.cli_sock);
677:
678: if (srv->srv_clients) {
679: free(srv->srv_clients);
1.2 misho 680: srv->srv_clients = NULL;
1.1 misho 681: srv->srv_numcli = 0;
682: }
683:
1.2 misho 684: pthread_mutex_lock(&srv->srv_mtx);
685: while ((f = srv->srv_funcs)) {
686: srv->srv_funcs = f->func_next;
687: free(f);
688: }
689: pthread_mutex_unlock(&srv->srv_mtx);
690:
691: while (pthread_mutex_trylock(&srv->srv_mtx) == EBUSY);
692: pthread_mutex_destroy(&srv->srv_mtx);
693:
1.1 misho 694: free(srv);
695: srv = NULL;
696: }
697:
698: /*
699: * rpc_srv_execServer() Execute Main server loop and wait for clients requests
700: * @srv = RPC Server instance
701: * return: -1 error or 0 ok, infinite loop ...
702: */
703: int
704: rpc_srv_execServer(rpc_srv_t * __restrict srv)
705: {
706: socklen_t salen = sizeof(struct sockaddr);
707: register int i;
708: rpc_cli_t *c;
1.2 misho 709: fd_set fds;
710: int ret;
711: struct timeval tv = { DEF_RPC_TIMEOUT, 0 };
1.1 misho 712:
713: if (!srv) {
714: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t start RPC server ...\n");
715: return -1;
716: }
717:
718: if (listen(srv->srv_server.cli_sock, SOMAXCONN) == -1) {
719: LOGERR;
720: return -1;
721: }
722:
1.2 misho 723: while (!rpc_Kill) {
1.1 misho 724: for (c = srv->srv_clients, i = 0; i < srv->srv_numcli && c; i++, c++)
725: if (!c->cli_sa.sa_family)
726: break;
1.2 misho 727: if (i >= srv->srv_numcli) {
1.1 misho 728: usleep(1000000);
729: continue;
730: }
1.2 misho 731:
732: FD_ZERO(&fds);
733: FD_SET(srv->srv_server.cli_sock, &fds);
734: ret = select(srv->srv_server.cli_sock + 1, &fds, NULL, NULL, &tv);
735: if (ret == -1) {
736: LOGERR;
737: ret = 1;
738: break;
739: }
740: if (!ret)
741: continue;
742:
1.1 misho 743: c->cli_sock = accept(srv->srv_server.cli_sock, &c->cli_sa, &salen);
744: if (c->cli_sock == -1) {
745: LOGERR;
746: continue;
747: } else
748: c->cli_parent = srv;
749:
750: if (pthread_create(&c->cli_tid, NULL, rpc_srv_dispatchCall, c)) {
751: LOGERR;
752: continue;
753: }
754: }
755:
756: return 0;
757: }
758:
759: // ---------------------------------------------------------
760:
761: /*
762: * rpc_srv_execCall() Execute registered call from RPC server
763: * @call = Register RPC call
764: * @rpc = IN RPC call structure
765: * @args = IN RPC call array of rpc values
766: * return: -1 error, !=-1 ok
767: */
768: int
1.2 misho 769: rpc_srv_execCall(rpc_func_t * __restrict call, struct tagRPCCall * __restrict rpc,
770: rpc_val_t * __restrict args)
1.1 misho 771: {
772: void *dl;
773: rpc_callback_t func;
774: int ret;
775:
1.2 misho 776: if (!call || !rpc || !call->func_parent) {
1.1 misho 777: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t exec call from RPC server ...\n");
778: return -1;
779: }
780:
781: dl = dlopen((char*) (*call->func_file ? call->func_file : NULL), RTLD_NOW);
782: if (!dl) {
783: rpc_SetErr(ENOENT, "Error:: Can`t attach module %s!\n", dlerror());
784: return -1;
785: }
786:
787: func = dlsym(dl, (char*) call->func_name);
788: if (func)
1.2 misho 789: ret = func(call, rpc->call_argc, args);
1.1 misho 790: else {
791: rpc_SetErr(ENOEXEC, "Error:: Can`t find function %s!\n", dlerror());
792: ret = -1;
793: }
794:
795: dlclose(dl);
796: return ret;
797: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>