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