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