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