Annotation of libaitrpc/src/srv.c, revision 1.4.2.10
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.10! misho 6: * $Id: srv.c,v 1.4.2.9 2011/09/03 12:58:49 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)) {
1.4.2.7 misho 100: rpc_SetErr(ERPCMISMATCH, "Error:: too short RPC packet ...\n");
1.1 misho 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)) {
1.4.2.7 misho 107: rpc_SetErr(ERPCMISMATCH, "Error:: get invalid RPC session ...\n");
1.1 misho 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))) {
1.4.2.7 misho 126: rpc_SetErr(EPROGUNAVAIL, "Error:: call not found into RPC server ...\n");
1.1 misho 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;
1.4.2.7 misho 160: rpc_SetErr(EBADRPC, "Error:: in prepare RPC packet values (-7) ...\n");
1.3 misho 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.4.2.7 misho 176: rpc_SetErr(EPROCUNAVAIL, "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)) {
1.4.2.7 misho 243: rpc_SetErr(ERPCMISMATCH, "Error:: too short BLOB packet ...\n");
1.2 misho 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:
1.4.2.7 misho 287: rpc_SetErr(EPROCUNAVAIL, "Error:: unsupported BLOB command (%d)...\n",
1.2 misho 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) {
1.4.2.7 misho 303: rpc_SetErr(EPROCUNAVAIL, "Error:: in send BLOB reply, should be send %d bytes, "
1.2 misho 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));
1.4.2.8 misho 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: rpc_srv_registerCall(srv, NULL, CALL_BLOBSHUTDOWN, 0);
1.4.2.5 misho 414: rpc_srv_registerCall(srv, NULL, CALL_BLOBCLIENTS, 1);
415: rpc_srv_registerCall(srv, NULL, CALL_BLOBVARS, 1);
1.4.2.6 misho 416: rpc_srv_registerCall(srv, NULL, CALL_BLOBSTATE, 0);
1.2 misho 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:
1.4.2.10! misho 451: pthread_mutex_lock(&srv->srv_blob.mtx);
1.2 misho 452: if (srv->srv_blob.clients) {
453: free(srv->srv_blob.clients);
454: srv->srv_blob.clients = NULL;
455: }
456:
1.4 misho 457: /* detach blobs */
1.2 misho 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: /*
1.4.2.8 misho 470: * rpc_srv_loopBLOB() Execute Main BLOB server loop and wait for clients requests
1.2 misho 471: * @srv = RPC Server instance
472: * return: -1 error or 0 ok, infinite loop ...
473: */
474: int
1.4.2.8 misho 475: rpc_srv_loopBLOB(rpc_srv_t * __restrict srv)
1.2 misho 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) {
1.4.2.2 misho 499: #ifdef HAVE_PTHREAD_YIELD
500: pthread_yield();
1.4.2.10! misho 501: #else
! 502: usleep(1000000);
1.4.2.2 misho 503: #endif
1.2 misho 504: continue;
505: }
506:
507: FD_ZERO(&fds);
508: FD_SET(srv->srv_blob.server.cli_sock, &fds);
509: ret = select(srv->srv_blob.server.cli_sock + 1, &fds, NULL, NULL, &tv);
510: if (ret == -1) {
511: LOGERR;
512: ret = 1;
513: break;
514: }
515: if (!ret)
516: continue;
517:
518: c->cli_sock = accept(srv->srv_blob.server.cli_sock, &c->cli_sa, &salen);
519: if (c->cli_sock == -1) {
520: LOGERR;
521: continue;
522: } else
523: c->cli_parent = srv;
524:
525: if (pthread_create(&c->cli_tid, NULL, rpc_srv_dispatchVars, c)) {
526: LOGERR;
527: continue;
1.4 misho 528: } else
529: pthread_detach(c->cli_tid);
1.2 misho 530: }
531:
532: srv->srv_blob.state = disable;
533:
534: return 0;
535: }
536:
537:
538: /*
1.1 misho 539: * rpc_srv_initServer() Init & create RPC Server
540: * @regProgID = ProgramID for authentication & recognition
541: * @regProcID = ProcessID for authentication & recognition
542: * @concurentClients = Concurent clients at same time to this server
1.4.2.3 misho 543: * @netBuf = Network buffer length, if =0 == BUFSIZ (also meaning max RPC packet)
1.4 misho 544: * @family = Family type, AF_INET, AF_INET6 or AF_LOCAL
545: * @csHost = Host name or address for bind server, if NULL any address
1.1 misho 546: * @Port = Port for bind server, if Port == 0 default port is selected
547: * return: NULL == error or !=NULL bind and created RPC server instance
548: */
549: rpc_srv_t *
550: rpc_srv_initServer(u_int regProgID, u_int regProcID, int concurentClients,
1.4.2.2 misho 551: int netBuf, u_short family, const char *csHost, u_short Port)
1.1 misho 552: {
553: rpc_srv_t *srv = NULL;
554: int n = 1;
555: struct hostent *host = NULL;
1.4 misho 556: struct sockaddr sa;
557: struct sockaddr_in *sin = (struct sockaddr_in*) &sa;
558: struct sockaddr_in6 *sin6 = (struct sockaddr_in6*) &sa;
559: struct sockaddr_un *sun = (struct sockaddr_un*) &sa;
1.1 misho 560:
1.4 misho 561: if (!concurentClients || !regProgID ||
562: (family != AF_INET && family != AF_INET6 && family != AF_LOCAL)) {
1.1 misho 563: rpc_SetErr(EINVAL, "Error:: Invalid parameters can`t init RPC server ...\n");
564: return NULL;
565: }
566: if (!Port)
567: Port = RPC_DEFPORT;
1.4.2.2 misho 568: if (!netBuf)
569: netBuf = BUFSIZ;
1.4 misho 570: if (csHost && family != AF_LOCAL) {
1.1 misho 571: host = gethostbyname2(csHost, family);
572: if (!host) {
573: rpc_SetErr(h_errno, "Error:: %s\n", hstrerror(h_errno));
574: return NULL;
575: }
576: }
1.4 misho 577: memset(&sa, 0, sizeof sa);
578: sa.sa_family = family;
1.1 misho 579: switch (family) {
580: case AF_INET:
1.4 misho 581: sin->sin_len = sizeof(struct sockaddr_in);
582: sin->sin_port = htons(Port);
1.1 misho 583: if (csHost)
1.4 misho 584: memcpy(&sin->sin_addr, host->h_addr, host->h_length);
1.1 misho 585: break;
586: case AF_INET6:
1.4 misho 587: sin6->sin6_len = sizeof(struct sockaddr_in6);
588: sin6->sin6_port = htons(Port);
589: if (csHost)
590: memcpy(&sin6->sin6_addr, host->h_addr, host->h_length);
591: break;
592: case AF_LOCAL:
593: sun->sun_len = sizeof(struct sockaddr_un);
1.1 misho 594: if (csHost)
1.4 misho 595: strlcpy(sun->sun_path, csHost, sizeof sun->sun_path);
1.1 misho 596: break;
597: default:
598: rpc_SetErr(EINVAL, "Error:: Invalid parameters can`t start RPC server ...\n");
599: return NULL;
600: }
601:
602: srv = malloc(sizeof(rpc_srv_t));
603: if (!srv) {
604: LOGERR;
605: return NULL;
606: } else
607: memset(srv, 0, sizeof(rpc_srv_t));
608:
1.4.2.2 misho 609: srv->srv_netbuf = netBuf;
1.1 misho 610: srv->srv_numcli = concurentClients;
611: srv->srv_session.sess_version = RPC_VERSION;
612: srv->srv_session.sess_program = regProgID;
613: srv->srv_session.sess_process = regProcID;
614:
1.2 misho 615: srv->srv_server.cli_tid = pthread_self();
1.1 misho 616: srv->srv_server.cli_parent = srv;
1.4 misho 617: switch (family) {
618: case AF_INET:
619: memcpy(&srv->srv_server.cli_sa, sin, sizeof srv->srv_server.cli_sa);
620: break;
621: case AF_INET6:
622: memcpy(&srv->srv_server.cli_sa, sin6, sizeof srv->srv_server.cli_sa);
623: break;
624: case AF_LOCAL:
625: memcpy(&srv->srv_server.cli_sa, sun, sizeof srv->srv_server.cli_sa);
626: unlink(sun->sun_path);
627: break;
628: }
629:
630: /* create server socket */
1.1 misho 631: srv->srv_server.cli_sock = socket(family, SOCK_STREAM, 0);
632: if (srv->srv_server.cli_sock == -1) {
633: LOGERR;
634: free(srv);
635: return NULL;
636: }
637: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
638: LOGERR;
639: close(srv->srv_server.cli_sock);
640: free(srv);
641: return NULL;
642: }
1.4.2.2 misho 643: n = srv->srv_netbuf;
644: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof n) == -1) {
645: LOGERR;
646: close(srv->srv_server.cli_sock);
647: free(srv);
648: return NULL;
649: }
650: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_RCVBUF, &n, sizeof n) == -1) {
651: LOGERR;
652: close(srv->srv_server.cli_sock);
653: free(srv);
654: return NULL;
655: }
1.1 misho 656: if (bind(srv->srv_server.cli_sock, &srv->srv_server.cli_sa, sizeof srv->srv_server.cli_sa) == -1) {
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.4.2.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
684: * @srv = RPC Server instance
685: * return: none
686: */
687: void
688: rpc_srv_endServer(rpc_srv_t * __restrict srv)
689: {
690: rpc_cli_t *c;
691: register int i;
692: rpc_func_t *f;
693:
694: if (!srv) {
695: rpc_SetErr(EINVAL, "Error:: Can`t destroy server because parameter is null!\n");
696: return;
697: }
698:
1.2 misho 699: rpc_srv_endBLOBServer(srv);
1.1 misho 700:
1.4 misho 701: /* close all clients connections & server socket */
1.1 misho 702: for (i = 0, c = srv->srv_clients; i < srv->srv_numcli && c; i++, c++)
1.2 misho 703: if (c->cli_sa.sa_family) {
1.1 misho 704: shutdown(c->cli_sock, SHUT_RDWR);
1.2 misho 705: close(c->cli_sock);
706: }
1.1 misho 707: close(srv->srv_server.cli_sock);
708:
709: if (srv->srv_clients) {
710: free(srv->srv_clients);
1.2 misho 711: srv->srv_clients = NULL;
1.1 misho 712: srv->srv_numcli = 0;
713: }
714:
1.4 misho 715: /* detach exported calls */
1.2 misho 716: pthread_mutex_lock(&srv->srv_mtx);
717: while ((f = srv->srv_funcs)) {
718: srv->srv_funcs = f->func_next;
719: free(f);
720: }
721: pthread_mutex_unlock(&srv->srv_mtx);
722:
723: while (pthread_mutex_trylock(&srv->srv_mtx) == EBUSY);
724: pthread_mutex_destroy(&srv->srv_mtx);
725:
1.1 misho 726: free(srv);
727: srv = NULL;
728: }
729:
730: /*
1.4.2.8 misho 731: * rpc_srv_loopServer() Execute Main server loop and wait for clients requests
1.1 misho 732: * @srv = RPC Server instance
733: * return: -1 error or 0 ok, infinite loop ...
734: */
735: int
1.4.2.8 misho 736: rpc_srv_loopServer(rpc_srv_t * __restrict srv)
1.1 misho 737: {
738: socklen_t salen = sizeof(struct sockaddr);
739: register int i;
740: rpc_cli_t *c;
1.2 misho 741: fd_set fds;
742: int ret;
743: struct timeval tv = { DEF_RPC_TIMEOUT, 0 };
1.1 misho 744:
745: if (!srv) {
746: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t start RPC server ...\n");
747: return -1;
748: }
749:
1.4.2.8 misho 750: /* activate BLOB server worker if srv->srv_blob.state == enable */
751: rpc_srv_execBLOBServer(srv);
752:
1.1 misho 753: if (listen(srv->srv_server.cli_sock, SOMAXCONN) == -1) {
754: LOGERR;
755: return -1;
756: }
757:
1.4 misho 758: while (srv->srv_kill != kill) {
1.1 misho 759: for (c = srv->srv_clients, i = 0; i < srv->srv_numcli && c; i++, c++)
760: if (!c->cli_sa.sa_family)
761: break;
1.2 misho 762: if (i >= srv->srv_numcli) {
1.4.2.2 misho 763: #ifdef HAVE_PTHREAD_YIELD
764: pthread_yield();
1.4.2.10! misho 765: #else
! 766: usleep(1000000);
1.4.2.2 misho 767: #endif
1.1 misho 768: continue;
769: }
1.2 misho 770:
771: FD_ZERO(&fds);
772: FD_SET(srv->srv_server.cli_sock, &fds);
773: ret = select(srv->srv_server.cli_sock + 1, &fds, NULL, NULL, &tv);
774: if (ret == -1) {
775: LOGERR;
776: ret = 1;
777: break;
778: }
779: if (!ret)
780: continue;
781:
1.1 misho 782: c->cli_sock = accept(srv->srv_server.cli_sock, &c->cli_sa, &salen);
783: if (c->cli_sock == -1) {
784: LOGERR;
785: continue;
786: } else
787: c->cli_parent = srv;
788:
789: if (pthread_create(&c->cli_tid, NULL, rpc_srv_dispatchCall, c)) {
790: LOGERR;
791: continue;
1.4 misho 792: } else
793: pthread_detach(c->cli_tid);
1.1 misho 794: }
795:
796: return 0;
797: }
798:
799: // ---------------------------------------------------------
800:
801: /*
802: * rpc_srv_execCall() Execute registered call from RPC server
803: * @call = Register RPC call
804: * @rpc = IN RPC call structure
1.4.2.4 misho 805: * @args = IN RPC calling arguments from RPC client
1.1 misho 806: * return: -1 error, !=-1 ok
807: */
808: int
1.2 misho 809: rpc_srv_execCall(rpc_func_t * __restrict call, struct tagRPCCall * __restrict rpc,
1.4.2.4 misho 810: array_t * __restrict args)
1.1 misho 811: {
812: void *dl;
813: rpc_callback_t func;
814: int ret;
815:
1.2 misho 816: if (!call || !rpc || !call->func_parent) {
1.1 misho 817: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t exec call from RPC server ...\n");
818: return -1;
819: }
820:
821: dl = dlopen((char*) (*call->func_file ? call->func_file : NULL), RTLD_NOW);
822: if (!dl) {
823: rpc_SetErr(ENOENT, "Error:: Can`t attach module %s!\n", dlerror());
824: return -1;
825: }
826:
827: func = dlsym(dl, (char*) call->func_name);
828: if (func)
1.2 misho 829: ret = func(call, rpc->call_argc, args);
1.1 misho 830: else {
831: rpc_SetErr(ENOEXEC, "Error:: Can`t find function %s!\n", dlerror());
832: ret = -1;
833: }
834:
835: dlclose(dl);
836: return ret;
837: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>