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