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