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