Annotation of libaitrpc/src/srv.c, revision 1.3.2.4
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.4 ! misho 6: * $Id: srv.c,v 1.3.2.3 2011/08/18 21:20:54 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 */
1.3.2.4 ! misho 424: srv->srv_blob.server.cli_type = sockType ? sockType : SOCK_STREAM;
1.3.2.3 misho 425: srv->srv_blob.server.cli_sock = socket(srv->srv_server.cli_sa.sa_family,
1.3.2.4 ! misho 426: srv->srv_blob.server.cli_type, 0);
1.2 misho 427: if (srv->srv_blob.server.cli_sock == -1) {
428: LOGERR;
429: return -1;
430: }
431: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
432: LOGERR;
433: close(srv->srv_blob.server.cli_sock);
434: return -1;
435: }
436: if (bind(srv->srv_blob.server.cli_sock, &srv->srv_blob.server.cli_sa,
437: sizeof srv->srv_blob.server.cli_sa) == -1) {
438: LOGERR;
439: close(srv->srv_blob.server.cli_sock);
440: return -1;
441: }
442:
1.3.2.3 misho 443: /* allocate pool for concurent clients */
1.2 misho 444: srv->srv_blob.clients = calloc(srv->srv_numcli, sizeof(rpc_cli_t));
445: if (!srv->srv_blob.clients) {
446: LOGERR;
447: close(srv->srv_blob.server.cli_sock);
448: return -1;
449: } else
450: memset(srv->srv_blob.clients, 0, srv->srv_numcli * sizeof(rpc_cli_t));
451:
452: pthread_mutex_init(&srv->srv_blob.mtx, NULL);
453:
454: pthread_mutex_lock(&srv->srv_mtx);
455: rpc_srv_registerCall(srv, NULL, CALL_BLOBSHUTDOWN, 0);
456: rpc_srv_registerCall(srv, NULL, CALL_BLOBCLIENTS, 0);
457: rpc_srv_registerCall(srv, NULL, CALL_BLOBVARS, 0);
458: rpc_srv_registerCall(srv, NULL, CALL_BLOBSTATE, 1);
459: pthread_mutex_unlock(&srv->srv_mtx);
460:
461: srv->srv_blob.state = enable; // enable BLOB
462: return 0;
463: }
464:
465: /*
466: * rpc_srv_endBLOBServer() Destroy BLOB server, close all opened sockets and free resources
467: * @srv = RPC Server instance
468: * return: none
469: */
470: void
471: rpc_srv_endBLOBServer(rpc_srv_t * __restrict srv)
472: {
473: rpc_cli_t *c;
474: register int i;
475: rpc_blob_t *f;
476:
477: if (!srv) {
478: rpc_SetErr(EINVAL, "Error:: Can`t destroy server because parameter is null!\n");
479: return;
480: } else
481: srv->srv_blob.state = disable;
482:
483: rpc_srv_unregisterCall(srv, NULL, CALL_BLOBSHUTDOWN);
484: rpc_srv_unregisterCall(srv, NULL, CALL_BLOBCLIENTS);
485: rpc_srv_unregisterCall(srv, NULL, CALL_BLOBVARS);
486: rpc_srv_unregisterCall(srv, NULL, CALL_BLOBSTATE);
487:
1.3.2.3 misho 488: /* close all clients connections & server socket */
1.2 misho 489: for (i = 0, c = srv->srv_blob.clients; i < srv->srv_numcli && c; i++, c++)
490: if (c->cli_sa.sa_family)
491: shutdown(c->cli_sock, SHUT_RDWR);
492: close(srv->srv_blob.server.cli_sock);
493:
494: if (srv->srv_blob.clients) {
495: free(srv->srv_blob.clients);
496: srv->srv_blob.clients = NULL;
497: }
498:
1.3.2.3 misho 499: /* detach blobs */
1.2 misho 500: pthread_mutex_lock(&srv->srv_blob.mtx);
501: while ((f = srv->srv_blob.blobs)) {
502: srv->srv_blob.blobs = f->blob_next;
503: rpc_srv_blobFree(srv, f);
504: free(f);
505: }
506: pthread_mutex_unlock(&srv->srv_blob.mtx);
507:
508: while (pthread_mutex_trylock(&srv->srv_blob.mtx) == EBUSY);
509: pthread_mutex_destroy(&srv->srv_blob.mtx);
510: }
511:
512: /*
513: * rpc_srv_execBLOBServer() Execute Main BLOB server loop and wait for clients requests
514: * @srv = RPC Server instance
515: * return: -1 error or 0 ok, infinite loop ...
516: */
517: int
518: rpc_srv_execBLOBServer(rpc_srv_t * __restrict srv)
519: {
520: socklen_t salen = sizeof(struct sockaddr);
521: register int i;
522: rpc_cli_t *c;
523: fd_set fds;
524: int ret;
525: struct timeval tv = { DEF_RPC_TIMEOUT, 0 };
526:
527: if (!srv || srv->srv_blob.state == disable) {
528: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t start BLOB server ...\n");
529: return -1;
530: }
531:
1.3.2.4 ! misho 532: printf("server.cli_type=%d:%d\n", srv->srv_blob.server.cli_type, SOCK_STREAM);
1.3.2.3 misho 533: if (srv->srv_blob.server.cli_type == SOCK_STREAM ||
534: srv->srv_blob.server.cli_type == SOCK_SEQPACKET)
535: if (listen(srv->srv_blob.server.cli_sock, SOMAXCONN) == -1) {
536: LOGERR;
537: return -1;
538: }
1.2 misho 539:
540: while (!blob_Kill && !rpc_Kill) {
541: for (c = srv->srv_blob.clients, i = 0; i < srv->srv_numcli && c; i++, c++)
542: if (!c->cli_sa.sa_family)
543: break;
544: if (i >= srv->srv_numcli) {
545: usleep(1000000);
546: continue;
547: }
548:
549: FD_ZERO(&fds);
550: FD_SET(srv->srv_blob.server.cli_sock, &fds);
551: ret = select(srv->srv_blob.server.cli_sock + 1, &fds, NULL, NULL, &tv);
552: if (ret == -1) {
553: LOGERR;
554: ret = 1;
555: break;
556: }
557: if (!ret)
558: continue;
559:
1.3.2.3 misho 560: if (srv->srv_blob.server.cli_type == SOCK_STREAM ||
561: srv->srv_blob.server.cli_type == SOCK_SEQPACKET)
562: c->cli_sock = accept(srv->srv_blob.server.cli_sock, &c->cli_sa, &salen);
563: else
564: c->cli_sock = dup(srv->srv_blob.server.cli_sock);
1.2 misho 565: if (c->cli_sock == -1) {
566: LOGERR;
567: continue;
568: } else
569: c->cli_parent = srv;
570:
571: if (pthread_create(&c->cli_tid, NULL, rpc_srv_dispatchVars, c)) {
572: LOGERR;
573: continue;
1.3.2.3 misho 574: } else
575: if (srv->srv_blob.server.cli_type == SOCK_STREAM ||
576: srv->srv_blob.server.cli_type == SOCK_SEQPACKET)
577: pthread_detach(c->cli_tid);
578: else
579: pthread_join(c->cli_tid, NULL);
1.2 misho 580: }
581:
582: srv->srv_blob.state = disable;
583:
584: return 0;
585: }
586:
587:
588: /*
1.1 misho 589: * rpc_srv_initServer() Init & create RPC Server
590: * @regProgID = ProgramID for authentication & recognition
591: * @regProcID = ProcessID for authentication & recognition
592: * @concurentClients = Concurent clients at same time to this server
1.3.2.1 misho 593: * @sockType = Socket type, like SOCK_STREAM, SOCK_DGRAM or SOCK_RAW
594: * @family = Family type, AF_INET, AF_INET6 or AF_LOCAL
595: * @csHost = Host name or address for bind server, if NULL any address
1.1 misho 596: * @Port = Port for bind server, if Port == 0 default port is selected
597: * return: NULL == error or !=NULL bind and created RPC server instance
598: */
599: rpc_srv_t *
600: rpc_srv_initServer(u_int regProgID, u_int regProcID, int concurentClients,
1.3.2.1 misho 601: int sockType, u_short family, const char *csHost, u_short Port)
1.1 misho 602: {
603: rpc_srv_t *srv = NULL;
604: int n = 1;
605: struct hostent *host = NULL;
1.3.2.1 misho 606: struct sockaddr sa;
607: struct sockaddr_in *sin = (struct sockaddr_in*) &sa;
608: struct sockaddr_in6 *sin6 = (struct sockaddr_in6*) &sa;
609: struct sockaddr_un *sun = (struct sockaddr_un*) &sa;
1.1 misho 610:
1.3.2.1 misho 611: if (!concurentClients || !regProgID ||
612: (family != AF_INET && family != AF_INET6 && family != AF_LOCAL)) {
1.1 misho 613: rpc_SetErr(EINVAL, "Error:: Invalid parameters can`t init RPC server ...\n");
614: return NULL;
615: }
616: if (!Port)
617: Port = RPC_DEFPORT;
1.3.2.1 misho 618: if (csHost && family != AF_LOCAL) {
1.1 misho 619: host = gethostbyname2(csHost, family);
620: if (!host) {
621: rpc_SetErr(h_errno, "Error:: %s\n", hstrerror(h_errno));
622: return NULL;
623: }
624: }
1.3.2.1 misho 625: memset(&sa, 0, sizeof sa);
626: sa.sa_family = family;
1.1 misho 627: switch (family) {
628: case AF_INET:
1.3.2.1 misho 629: sin->sin_len = sizeof(struct sockaddr_in);
630: sin->sin_port = htons(Port);
1.1 misho 631: if (csHost)
1.3.2.1 misho 632: memcpy(&sin->sin_addr, host->h_addr, host->h_length);
1.1 misho 633: break;
634: case AF_INET6:
1.3.2.1 misho 635: sin6->sin6_len = sizeof(struct sockaddr_in6);
636: sin6->sin6_port = htons(Port);
637: if (csHost)
638: memcpy(&sin6->sin6_addr, host->h_addr, host->h_length);
639: break;
640: case AF_LOCAL:
641: sun->sun_len = sizeof(struct sockaddr_un);
1.1 misho 642: if (csHost)
1.3.2.1 misho 643: strlcpy(sun->sun_path, csHost, sizeof sun->sun_path);
1.1 misho 644: break;
645: default:
646: rpc_SetErr(EINVAL, "Error:: Invalid parameters can`t start RPC server ...\n");
647: return NULL;
648: }
649:
650: srv = malloc(sizeof(rpc_srv_t));
651: if (!srv) {
652: LOGERR;
653: return NULL;
654: } else
655: memset(srv, 0, sizeof(rpc_srv_t));
656:
657: srv->srv_numcli = concurentClients;
658: srv->srv_session.sess_version = RPC_VERSION;
659: srv->srv_session.sess_program = regProgID;
660: srv->srv_session.sess_process = regProcID;
661:
1.2 misho 662: srv->srv_server.cli_tid = pthread_self();
1.1 misho 663: srv->srv_server.cli_parent = srv;
1.3.2.1 misho 664: switch (family) {
665: case AF_INET:
1.3.2.3 misho 666: memcpy(&srv->srv_server.cli_sa, sin, sizeof srv->srv_server.cli_sa);
1.3.2.1 misho 667: break;
668: case AF_INET6:
1.3.2.3 misho 669: memcpy(&srv->srv_server.cli_sa, sin6, sizeof srv->srv_server.cli_sa);
1.3.2.1 misho 670: break;
671: case AF_LOCAL:
1.3.2.3 misho 672: memcpy(&srv->srv_server.cli_sa, sun, sizeof srv->srv_server.cli_sa);
1.3.2.1 misho 673: unlink(sun->sun_path);
674: break;
675: }
676:
677: /* create server socket */
1.3.2.4 ! misho 678: srv->srv_server.cli_type = sockType ? sockType : SOCK_STREAM;
! 679: srv->srv_server.cli_sock = socket(family, srv->srv_server.cli_type, 0);
1.1 misho 680: if (srv->srv_server.cli_sock == -1) {
681: LOGERR;
682: free(srv);
683: return NULL;
1.3.2.4 ! misho 684: }
1.1 misho 685: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
686: LOGERR;
687: close(srv->srv_server.cli_sock);
688: free(srv);
689: return NULL;
690: }
691: if (bind(srv->srv_server.cli_sock, &srv->srv_server.cli_sa, sizeof srv->srv_server.cli_sa) == -1) {
692: LOGERR;
693: close(srv->srv_server.cli_sock);
694: free(srv);
695: return NULL;
696: }
697:
1.3.2.1 misho 698: /* allocate pool for concurent clients */
1.1 misho 699: srv->srv_clients = calloc(srv->srv_numcli, sizeof(rpc_cli_t));
700: if (!srv->srv_clients) {
701: LOGERR;
702: close(srv->srv_server.cli_sock);
703: free(srv);
704: return NULL;
705: } else
706: memset(srv->srv_clients, 0, srv->srv_numcli * sizeof(rpc_cli_t));
707:
1.2 misho 708: pthread_mutex_init(&srv->srv_mtx, NULL);
709:
710: rpc_srv_registerCall(srv, NULL, CALL_SRVSHUTDOWN, 0);
1.1 misho 711: rpc_srv_registerCall(srv, NULL, CALL_SRVCLIENTS, 0);
712: rpc_srv_registerCall(srv, NULL, CALL_SRVCALLS, 0);
713: rpc_srv_registerCall(srv, NULL, CALL_SRVSESSIONS, 0);
714: return srv;
715: }
716:
717: /*
718: * rpc_srv_endServer() Destroy RPC server, close all opened sockets and free resources
719: * @srv = RPC Server instance
720: * return: none
721: */
722: void
723: rpc_srv_endServer(rpc_srv_t * __restrict srv)
724: {
725: rpc_cli_t *c;
726: register int i;
727: rpc_func_t *f;
728:
729: if (!srv) {
730: rpc_SetErr(EINVAL, "Error:: Can`t destroy server because parameter is null!\n");
731: return;
732: }
733:
1.2 misho 734: rpc_srv_endBLOBServer(srv);
1.1 misho 735:
1.3.2.1 misho 736: /* close all clients connections & server socket */
1.1 misho 737: for (i = 0, c = srv->srv_clients; i < srv->srv_numcli && c; i++, c++)
1.2 misho 738: if (c->cli_sa.sa_family) {
1.1 misho 739: shutdown(c->cli_sock, SHUT_RDWR);
1.2 misho 740: close(c->cli_sock);
741: }
1.1 misho 742: close(srv->srv_server.cli_sock);
743:
744: if (srv->srv_clients) {
745: free(srv->srv_clients);
1.2 misho 746: srv->srv_clients = NULL;
1.1 misho 747: srv->srv_numcli = 0;
748: }
749:
1.3.2.1 misho 750: /* detach exported calls */
1.2 misho 751: pthread_mutex_lock(&srv->srv_mtx);
752: while ((f = srv->srv_funcs)) {
753: srv->srv_funcs = f->func_next;
754: free(f);
755: }
756: pthread_mutex_unlock(&srv->srv_mtx);
757:
758: while (pthread_mutex_trylock(&srv->srv_mtx) == EBUSY);
759: pthread_mutex_destroy(&srv->srv_mtx);
760:
1.1 misho 761: free(srv);
762: srv = NULL;
763: }
764:
765: /*
766: * rpc_srv_execServer() Execute Main server loop and wait for clients requests
767: * @srv = RPC Server instance
768: * return: -1 error or 0 ok, infinite loop ...
769: */
770: int
771: rpc_srv_execServer(rpc_srv_t * __restrict srv)
772: {
773: socklen_t salen = sizeof(struct sockaddr);
774: register int i;
775: rpc_cli_t *c;
1.2 misho 776: fd_set fds;
777: int ret;
778: struct timeval tv = { DEF_RPC_TIMEOUT, 0 };
1.1 misho 779:
780: if (!srv) {
781: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t start RPC server ...\n");
782: return -1;
783: }
784:
1.3.2.3 misho 785: if (srv->srv_server.cli_type == SOCK_STREAM || srv->srv_server.cli_type == SOCK_SEQPACKET)
786: if (listen(srv->srv_server.cli_sock, SOMAXCONN) == -1) {
787: LOGERR;
788: return -1;
789: }
1.1 misho 790:
1.2 misho 791: while (!rpc_Kill) {
1.1 misho 792: for (c = srv->srv_clients, i = 0; i < srv->srv_numcli && c; i++, c++)
793: if (!c->cli_sa.sa_family)
794: break;
1.2 misho 795: if (i >= srv->srv_numcli) {
1.1 misho 796: usleep(1000000);
797: continue;
798: }
1.2 misho 799:
800: FD_ZERO(&fds);
801: FD_SET(srv->srv_server.cli_sock, &fds);
802: ret = select(srv->srv_server.cli_sock + 1, &fds, NULL, NULL, &tv);
803: if (ret == -1) {
804: LOGERR;
805: ret = 1;
806: break;
807: }
808: if (!ret)
809: continue;
810:
1.3.2.3 misho 811: if (srv->srv_server.cli_type == SOCK_STREAM || srv->srv_server.cli_type == SOCK_SEQPACKET)
812: c->cli_sock = accept(srv->srv_server.cli_sock, &c->cli_sa, &salen);
813: else
814: c->cli_sock = dup(srv->srv_server.cli_sock);
1.1 misho 815: if (c->cli_sock == -1) {
816: LOGERR;
817: continue;
818: } else
819: c->cli_parent = srv;
820:
821: if (pthread_create(&c->cli_tid, NULL, rpc_srv_dispatchCall, c)) {
822: LOGERR;
823: continue;
1.3.2.3 misho 824: } else {
825: if (srv->srv_server.cli_type == SOCK_STREAM || srv->srv_server.cli_type == SOCK_SEQPACKET)
826: pthread_detach(c->cli_tid);
827: else
828: pthread_join(c->cli_tid, NULL);
1.1 misho 829: }
830: }
831:
832: return 0;
833: }
834:
835: // ---------------------------------------------------------
836:
837: /*
838: * rpc_srv_execCall() Execute registered call from RPC server
839: * @call = Register RPC call
840: * @rpc = IN RPC call structure
841: * @args = IN RPC call array of rpc values
842: * return: -1 error, !=-1 ok
843: */
844: int
1.2 misho 845: rpc_srv_execCall(rpc_func_t * __restrict call, struct tagRPCCall * __restrict rpc,
846: rpc_val_t * __restrict args)
1.1 misho 847: {
848: void *dl;
849: rpc_callback_t func;
850: int ret;
851:
1.2 misho 852: if (!call || !rpc || !call->func_parent) {
1.1 misho 853: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t exec call from RPC server ...\n");
854: return -1;
855: }
856:
857: dl = dlopen((char*) (*call->func_file ? call->func_file : NULL), RTLD_NOW);
858: if (!dl) {
859: rpc_SetErr(ENOENT, "Error:: Can`t attach module %s!\n", dlerror());
860: return -1;
861: }
862:
863: func = dlsym(dl, (char*) call->func_name);
864: if (func)
1.2 misho 865: ret = func(call, rpc->call_argc, args);
1.1 misho 866: else {
867: rpc_SetErr(ENOEXEC, "Error:: Can`t find function %s!\n", dlerror());
868: ret = -1;
869: }
870:
871: dlclose(dl);
872: return ret;
873: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>