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