Annotation of libaitrpc/src/srv.c, revision 1.4.2.9
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.9 ! misho 6: * $Id: srv.c,v 1.4.2.8 2011/09/03 12:39:27 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:
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: /*
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) {
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.4.2.5 misho 675: rpc_srv_registerCall(srv, NULL, CALL_SRVCLIENTS, 1);
676: rpc_srv_registerCall(srv, NULL, CALL_SRVSESSIONS, 4);
677: rpc_srv_registerCall(srv, NULL, CALL_SRVCALLS, 1);
1.1 misho 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: /*
1.4.2.8 misho 730: * rpc_srv_loopServer() Execute Main server loop and wait for clients requests
1.1 misho 731: * @srv = RPC Server instance
732: * return: -1 error or 0 ok, infinite loop ...
733: */
734: int
1.4.2.8 misho 735: rpc_srv_loopServer(rpc_srv_t * __restrict srv)
1.1 misho 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:
1.4.2.8 misho 749: /* activate BLOB server worker if srv->srv_blob.state == enable */
750: rpc_srv_execBLOBServer(srv);
751:
1.1 misho 752: if (listen(srv->srv_server.cli_sock, SOMAXCONN) == -1) {
753: LOGERR;
754: return -1;
755: }
756:
1.4 misho 757: while (srv->srv_kill != kill) {
1.1 misho 758: for (c = srv->srv_clients, i = 0; i < srv->srv_numcli && c; i++, c++)
759: if (!c->cli_sa.sa_family)
760: break;
1.2 misho 761: if (i >= srv->srv_numcli) {
1.1 misho 762: usleep(1000000);
1.4.2.2 misho 763: #ifdef HAVE_PTHREAD_YIELD
764: pthread_yield();
765: #endif
1.1 misho 766: continue;
767: }
1.2 misho 768:
769: FD_ZERO(&fds);
770: FD_SET(srv->srv_server.cli_sock, &fds);
771: ret = select(srv->srv_server.cli_sock + 1, &fds, NULL, NULL, &tv);
772: if (ret == -1) {
773: LOGERR;
774: ret = 1;
775: break;
776: }
777: if (!ret)
778: continue;
779:
1.1 misho 780: c->cli_sock = accept(srv->srv_server.cli_sock, &c->cli_sa, &salen);
781: if (c->cli_sock == -1) {
782: LOGERR;
783: continue;
784: } else
785: c->cli_parent = srv;
786:
787: if (pthread_create(&c->cli_tid, NULL, rpc_srv_dispatchCall, c)) {
788: LOGERR;
789: continue;
1.4 misho 790: } else
791: pthread_detach(c->cli_tid);
1.1 misho 792: }
793:
794: return 0;
795: }
796:
797: // ---------------------------------------------------------
798:
799: /*
800: * rpc_srv_execCall() Execute registered call from RPC server
801: * @call = Register RPC call
802: * @rpc = IN RPC call structure
1.4.2.4 misho 803: * @args = IN RPC calling arguments from RPC client
1.1 misho 804: * return: -1 error, !=-1 ok
805: */
806: int
1.2 misho 807: rpc_srv_execCall(rpc_func_t * __restrict call, struct tagRPCCall * __restrict rpc,
1.4.2.4 misho 808: array_t * __restrict args)
1.1 misho 809: {
810: void *dl;
811: rpc_callback_t func;
812: int ret;
813:
1.2 misho 814: if (!call || !rpc || !call->func_parent) {
1.1 misho 815: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t exec call from RPC server ...\n");
816: return -1;
817: }
818:
819: dl = dlopen((char*) (*call->func_file ? call->func_file : NULL), RTLD_NOW);
820: if (!dl) {
821: rpc_SetErr(ENOENT, "Error:: Can`t attach module %s!\n", dlerror());
822: return -1;
823: }
824:
825: func = dlsym(dl, (char*) call->func_name);
826: if (func)
1.2 misho 827: ret = func(call, rpc->call_argc, args);
1.1 misho 828: else {
829: rpc_SetErr(ENOEXEC, "Error:: Can`t find function %s!\n", dlerror());
830: ret = -1;
831: }
832:
833: dlclose(dl);
834: return ret;
835: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>