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