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