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