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