Annotation of libaitrpc/src/srv.c, revision 1.1.1.1.2.6
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.6! misho 6: * $Id: srv.c,v 1.1.1.1.2.5 2010/06/23 15:07:15 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: cx++;
200:
201: FD_ZERO(&fds);
202: FD_SET(c->cli_sock, &fds);
203: ret = select(c->cli_sock + 1, &fds, NULL, NULL, NULL);
204: if (ret == -1) {
205: ret = -2;
206: }
1.1.1.1.2.6! misho 207: memset(buf, 0, sizeof buf);
! 208: if ((ret = recv(c->cli_sock, buf, sizeof buf, 0)) == -1) {
1.1.1.1.2.5 misho 209: LOGERR;
210: ret = -3;
211: break;
212: }
213: if (!ret) { // receive EOF
214: ret = 0;
215: break;
216: }
217: if (ret < sizeof(struct tagBLOBHdr)) {
218: rpc_SetErr(EMSGSIZE, "Error:: too short BLOB packet ...\n");
219: ret = -4;
220: break;
221: } else
222: blob = (struct tagBLOBHdr*) buf;
223: // check BLOB packet session info
224: if (memcmp(&blob->hdr_session, &s->srv_session, sizeof blob->hdr_session) ||
225: blob->hdr_seq != cx) {
226: rpc_SetErr(EINVAL, "Error:: get invalid BLOB session in seq=%d...\n", blob->hdr_seq);
227: ret = -5;
228: break;
229: }
230: // Go to decapsulate packet ...
231: if (!(b = rpc_srv_getBLOB(s, blob->hdr_var))) {
232: rpc_SetErr(EINVAL, "Error:: var (%x) not found into BLOB server ...\n", blob->hdr_var);
233: ret = -6;
234: break;
235: }
236: switch (blob->hdr_cmd) {
237: case get:
1.1.1.1.2.6! misho 238: if (rpc_srv_blobMap(s, b) != -1) {
! 239: ret = rpc_srv_sendBLOB(c, b);
! 240: rpc_srv_blobUnmap(b);
! 241: } else
! 242: ret = -7;
1.1.1.1.2.5 misho 243: break;
244: case set:
245: ret = rpc_srv_recvBLOB(c, b);
1.1.1.1.2.6! misho 246: if (ret == -1)
! 247: ret = -7;
1.1.1.1.2.5 misho 248: break;
249: case unset:
1.1.1.1.2.6! misho 250: ret = rpc_srv_deleteBLOB(c, b);
! 251: if (ret == -1)
! 252: ret = -7;
1.1.1.1.2.5 misho 253: break;
254: default:
255: rpc_SetErr(EINVAL, "Error:: unsupported BLOB command (%d)...\n",
256: blob->hdr_cmd);
257: ret -7;
258: }
1.1.1.1.2.6! misho 259:
! 260: // Replay to client!
1.1.1.1.2.5 misho 261: } while (ret > -1);
1.1.1.1.2.4 misho 262:
1.1.1.1.2.5 misho 263: shutdown(c->cli_sock, SHUT_RDWR);
264: close(c->cli_sock);
265: memset(c, 0, sizeof(rpc_cli_t));
1.1.1.1.2.4 misho 266: return (void*) ret;
267: }
268:
1.1 misho 269: // -------------------------------------------------
270:
271: /*
1.1.1.1.2.4 misho 272: * rpc_srv_initBLOBServer() Init & create BLOB Server
273: * @Port = Port for bind server, if Port == 0 default port is selected
1.1.1.1.2.5 misho 274: * @diskDir = Disk place for BLOB file objects
1.1.1.1.2.4 misho 275: * return: -1 == error or 0 bind and created BLOB server instance
276: */
277: int
1.1.1.1.2.5 misho 278: rpc_srv_initBLOBServer(rpc_srv_t * __restrict srv, u_short Port, const char *diskDir)
1.1.1.1.2.4 misho 279: {
280: int n = 1;
281: struct sockaddr_in sin;
282: struct sockaddr_in6 sin6;
283:
284: if (!srv) {
285: rpc_SetErr(EINVAL, "Error:: Invalid parameters can`t init BLOB server ...\n");
286: return -1;
287: }
288: if (srv->srv_blob.state) {
289: rpc_SetErr(EPERM, "Warning:: Already started BLOB server!\n");
290: return 0;
291: }
292: if (!Port)
293: Port = RPC_DEFPORT + 1;
294:
295: memset(&srv->srv_blob, 0, sizeof srv->srv_blob);
1.1.1.1.2.5 misho 296: if (access(diskDir, R_OK | W_OK) == -1) {
297: LOGERR;
298: return -1;
299: } else
300: strlcpy(srv->srv_blob.dir, diskDir, UCHAR_MAX + 1);
301:
1.1.1.1.2.4 misho 302: srv->srv_blob.server.cli_tid = pthread_self();
303: srv->srv_blob.server.cli_parent = srv;
304: if (srv->srv_server.cli_sa.sa_family == AF_INET) {
305: memcpy(&sin, &srv->srv_server.cli_sa, sizeof sin);
306: sin.sin_port = htons(Port);
307: memcpy(&srv->srv_blob.server.cli_sa, &sin, sizeof(struct sockaddr));
308: } else {
309: memcpy(&sin6, &srv->srv_server.cli_sa, sizeof sin6);
310: sin6.sin6_port = htons(Port);
311: memcpy(&srv->srv_blob.server.cli_sa, &sin6, sizeof(struct sockaddr));
312: }
313:
314: srv->srv_blob.server.cli_sock = socket(srv->srv_server.cli_sa.sa_family, SOCK_STREAM, 0);
315: if (srv->srv_blob.server.cli_sock == -1) {
316: LOGERR;
317: return -1;
318: }
319: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
320: LOGERR;
321: close(srv->srv_blob.server.cli_sock);
322: return -1;
323: }
324: if (bind(srv->srv_blob.server.cli_sock, &srv->srv_blob.server.cli_sa,
325: sizeof srv->srv_blob.server.cli_sa) == -1) {
326: LOGERR;
327: close(srv->srv_blob.server.cli_sock);
328: return -1;
329: }
330:
331: srv->srv_blob.clients = calloc(srv->srv_numcli, sizeof(rpc_cli_t));
332: if (!srv->srv_blob.clients) {
333: LOGERR;
334: close(srv->srv_blob.server.cli_sock);
335: return -1;
336: } else
337: memset(srv->srv_blob.clients, 0, srv->srv_numcli * sizeof(rpc_cli_t));
338:
339: pthread_mutex_init(&srv->srv_blob.mtx, NULL);
340:
341: pthread_mutex_lock(&srv->srv_mtx);
342: rpc_srv_registerCall(srv, NULL, CALL_BLOBSHUTDOWN, 0);
343: rpc_srv_registerCall(srv, NULL, CALL_BLOBCLIENTS, 0);
344: rpc_srv_registerCall(srv, NULL, CALL_BLOBVARS, 0);
345: pthread_mutex_unlock(&srv->srv_mtx);
346:
1.1.1.1.2.5 misho 347: srv->srv_blob.state = enable; // enable BLOB
1.1.1.1.2.4 misho 348: return 0;
349: }
350:
351: /*
352: * rpc_srv_endBLOBServer() Destroy BLOB server, close all opened sockets and free resources
353: * @srv = RPC Server instance
354: * return: none
355: */
356: void
357: rpc_srv_endBLOBServer(rpc_srv_t * __restrict srv)
358: {
359: rpc_cli_t *c;
360: register int i;
361: rpc_blob_t *f;
362:
363: if (!srv) {
364: rpc_SetErr(EINVAL, "Error:: Can`t destroy server because parameter is null!\n");
365: return;
366: } else
1.1.1.1.2.5 misho 367: srv->srv_blob.state = disable;
1.1.1.1.2.4 misho 368:
369: for (i = 0, c = srv->srv_blob.clients; i < srv->srv_numcli && c; i++, c++)
370: if (c->cli_sa.sa_family)
371: shutdown(c->cli_sock, SHUT_RDWR);
372: close(srv->srv_blob.server.cli_sock);
373:
374: if (srv->srv_blob.clients)
375: free(srv->srv_blob.clients);
376:
377: pthread_mutex_lock(&srv->srv_blob.mtx);
378: while ((f = srv->srv_blob.blobs)) {
379: srv->srv_blob.blobs = f->blob_next;
380: free(f);
381: }
382: pthread_mutex_unlock(&srv->srv_blob.mtx);
383:
384: pthread_mutex_destroy(&srv->srv_blob.mtx);
385: }
386:
387: /*
388: * rpc_srv_execBLOBServer() Execute Main BLOB server loop and wait for clients requests
389: * @srv = RPC Server instance
390: * return: -1 error or 0 ok, infinite loop ...
391: */
392: int
393: rpc_srv_execBLOBServer(rpc_srv_t * __restrict srv)
394: {
395: socklen_t salen = sizeof(struct sockaddr);
396: register int i;
397: rpc_cli_t *c;
398: fd_set fds;
399: int ret;
400: struct timeval tv = { DEF_RPC_TIMEOUT, 0 };
401:
402: if (!srv || !srv->srv_blob.state) {
403: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t start BLOB server ...\n");
404: return -1;
405: }
406:
407: if (listen(srv->srv_blob.server.cli_sock, SOMAXCONN) == -1) {
408: LOGERR;
409: return -1;
410: }
411:
412: while (!rpc_Kill) {
413: for (c = srv->srv_blob.clients, i = 0; i < srv->srv_numcli && c; i++, c++)
414: if (!c->cli_sa.sa_family)
415: break;
416: if (c && c->cli_sa.sa_family && c->cli_parent) {
417: usleep(1000000);
418: continue;
419: }
420:
421: FD_ZERO(&fds);
422: FD_SET(srv->srv_blob.server.cli_sock, &fds);
423: ret = select(srv->srv_blob.server.cli_sock + 1, &fds, NULL, NULL, &tv);
424: if (ret == -1) {
425: LOGERR;
426: ret = 1;
427: break;
428: }
429: if (!ret)
430: continue;
431:
432: c->cli_sock = accept(srv->srv_blob.server.cli_sock, &c->cli_sa, &salen);
433: if (c->cli_sock == -1) {
434: LOGERR;
435: continue;
436: } else
437: c->cli_parent = srv;
438:
439: if (pthread_create(&c->cli_tid, NULL, rpc_srv_dispatchVars, c)) {
440: LOGERR;
441: continue;
442: }
443: }
444:
445: return 0;
446: }
447:
448:
449: /*
1.1 misho 450: * rpc_srv_initServer() Init & create RPC Server
451: * @regProgID = ProgramID for authentication & recognition
452: * @regProcID = ProcessID for authentication & recognition
453: * @concurentClients = Concurent clients at same time to this server
454: * @family = Family socket type, AF_INET or AF_INET6
455: * @csHost = Host name or IP address for bind server, if NULL any address
456: * @Port = Port for bind server, if Port == 0 default port is selected
457: * return: NULL == error or !=NULL bind and created RPC server instance
458: */
459: rpc_srv_t *
460: rpc_srv_initServer(u_int regProgID, u_int regProcID, int concurentClients,
461: u_short family, const char *csHost, u_short Port)
462: {
463: rpc_srv_t *srv = NULL;
464: int n = 1;
465: struct hostent *host = NULL;
466: struct sockaddr_in sin;
467: struct sockaddr_in6 sin6;
468:
469: if (!concurentClients || !regProgID || (family != AF_INET && family != AF_INET6)) {
470: rpc_SetErr(EINVAL, "Error:: Invalid parameters can`t init RPC server ...\n");
471: return NULL;
472: }
473: if (!Port)
474: Port = RPC_DEFPORT;
475: if (csHost) {
476: host = gethostbyname2(csHost, family);
477: if (!host) {
478: rpc_SetErr(h_errno, "Error:: %s\n", hstrerror(h_errno));
479: return NULL;
480: }
481: }
482: switch (family) {
483: case AF_INET:
484: memset(&sin, 0, sizeof sin);
485: sin.sin_len = sizeof sin;
486: sin.sin_family = family;
487: sin.sin_port = htons(Port);
488: if (csHost)
489: memcpy(&sin.sin_addr, host->h_addr, host->h_length);
490: break;
491: case AF_INET6:
492: memset(&sin6, 0, sizeof sin6);
493: sin6.sin6_len = sizeof sin6;
494: sin6.sin6_family = family;
495: sin6.sin6_port = htons(Port);
496: if (csHost)
497: memcpy(&sin6.sin6_addr, host->h_addr, host->h_length);
498: break;
499: default:
500: rpc_SetErr(EINVAL, "Error:: Invalid parameters can`t start RPC server ...\n");
501: return NULL;
502: }
503:
504: srv = malloc(sizeof(rpc_srv_t));
505: if (!srv) {
506: LOGERR;
507: return NULL;
508: } else
509: memset(srv, 0, sizeof(rpc_srv_t));
510:
511: srv->srv_numcli = concurentClients;
512: srv->srv_session.sess_version = RPC_VERSION;
513: srv->srv_session.sess_program = regProgID;
514: srv->srv_session.sess_process = regProcID;
515:
1.1.1.1.2.2 misho 516: srv->srv_server.cli_tid = pthread_self();
1.1 misho 517: srv->srv_server.cli_parent = srv;
518: if (family == AF_INET)
519: memcpy(&srv->srv_server.cli_sa, &sin, sizeof srv->srv_server.cli_sa);
520: else
521: memcpy(&srv->srv_server.cli_sa, &sin6, sizeof srv->srv_server.cli_sa);
522: srv->srv_server.cli_sock = socket(family, SOCK_STREAM, 0);
523: if (srv->srv_server.cli_sock == -1) {
524: LOGERR;
525: free(srv);
526: return NULL;
527: }
528: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
529: LOGERR;
530: close(srv->srv_server.cli_sock);
531: free(srv);
532: return NULL;
533: }
534: if (bind(srv->srv_server.cli_sock, &srv->srv_server.cli_sa, sizeof srv->srv_server.cli_sa) == -1) {
535: LOGERR;
536: close(srv->srv_server.cli_sock);
537: free(srv);
538: return NULL;
539: }
540:
541: srv->srv_clients = calloc(srv->srv_numcli, sizeof(rpc_cli_t));
542: if (!srv->srv_clients) {
543: LOGERR;
544: close(srv->srv_server.cli_sock);
545: free(srv);
546: return NULL;
547: } else
548: memset(srv->srv_clients, 0, srv->srv_numcli * sizeof(rpc_cli_t));
549:
1.1.1.1.2.1 misho 550: rpc_srv_registerCall(srv, NULL, CALL_SRVSHUTDOWN, 0);
1.1 misho 551: rpc_srv_registerCall(srv, NULL, CALL_SRVCLIENTS, 0);
552: rpc_srv_registerCall(srv, NULL, CALL_SRVCALLS, 0);
553: rpc_srv_registerCall(srv, NULL, CALL_SRVSESSIONS, 0);
1.1.1.1.2.1 misho 554:
1.1.1.1.2.4 misho 555: pthread_mutex_init(&srv->srv_mtx, NULL);
1.1 misho 556: return srv;
557: }
558:
559: /*
560: * rpc_srv_endServer() Destroy RPC server, close all opened sockets and free resources
561: * @srv = RPC Server instance
562: * return: none
563: */
564: void
565: rpc_srv_endServer(rpc_srv_t * __restrict srv)
566: {
567: rpc_cli_t *c;
568: register int i;
569: rpc_func_t *f;
570:
571: if (!srv) {
572: rpc_SetErr(EINVAL, "Error:: Can`t destroy server because parameter is null!\n");
573: return;
574: }
575:
1.1.1.1.2.4 misho 576: rpc_srv_endBLOBServer(srv);
1.1 misho 577:
578: for (i = 0, c = srv->srv_clients; i < srv->srv_numcli && c; i++, c++)
579: if (c->cli_sa.sa_family)
580: shutdown(c->cli_sock, SHUT_RDWR);
581: close(srv->srv_server.cli_sock);
582:
583: if (srv->srv_clients) {
584: free(srv->srv_clients);
585: srv->srv_numcli = 0;
586: }
587:
1.1.1.1.2.4 misho 588: pthread_mutex_lock(&srv->srv_mtx);
589: while ((f = srv->srv_funcs)) {
590: srv->srv_funcs = f->func_next;
591: free(f);
592: }
593: pthread_mutex_unlock(&srv->srv_mtx);
594:
595: pthread_mutex_destroy(&srv->srv_mtx);
596:
1.1 misho 597: free(srv);
598: srv = NULL;
599: }
600:
601: /*
602: * rpc_srv_execServer() Execute Main server loop and wait for clients requests
603: * @srv = RPC Server instance
604: * return: -1 error or 0 ok, infinite loop ...
605: */
606: int
607: rpc_srv_execServer(rpc_srv_t * __restrict srv)
608: {
609: socklen_t salen = sizeof(struct sockaddr);
610: register int i;
611: rpc_cli_t *c;
1.1.1.1.2.2 misho 612: fd_set fds;
613: int ret;
614: struct timeval tv = { DEF_RPC_TIMEOUT, 0 };
1.1 misho 615:
616: if (!srv) {
617: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t start RPC server ...\n");
618: return -1;
619: }
620:
621: if (listen(srv->srv_server.cli_sock, SOMAXCONN) == -1) {
622: LOGERR;
623: return -1;
624: }
625:
1.1.1.1.2.1 misho 626: while (!rpc_Kill) {
1.1 misho 627: for (c = srv->srv_clients, i = 0; i < srv->srv_numcli && c; i++, c++)
628: if (!c->cli_sa.sa_family)
629: break;
630: if (c && c->cli_sa.sa_family && c->cli_parent) {
631: usleep(1000000);
632: continue;
633: }
1.1.1.1.2.2 misho 634:
635: FD_ZERO(&fds);
636: FD_SET(srv->srv_server.cli_sock, &fds);
637: ret = select(srv->srv_server.cli_sock + 1, &fds, NULL, NULL, &tv);
638: if (ret == -1) {
639: LOGERR;
640: ret = 1;
641: break;
642: }
643: if (!ret)
644: continue;
645:
1.1 misho 646: c->cli_sock = accept(srv->srv_server.cli_sock, &c->cli_sa, &salen);
647: if (c->cli_sock == -1) {
648: LOGERR;
649: continue;
650: } else
651: c->cli_parent = srv;
652:
653: if (pthread_create(&c->cli_tid, NULL, rpc_srv_dispatchCall, c)) {
654: LOGERR;
655: continue;
656: }
657: }
658:
659: return 0;
660: }
661:
662: // ---------------------------------------------------------
663:
664: /*
665: * rpc_srv_execCall() Execute registered call from RPC server
666: * @data = RPC const data
667: * @call = Register RPC call
668: * @rpc = IN RPC call structure
669: * @args = IN RPC call array of rpc values
670: * return: -1 error, !=-1 ok
671: */
672: int
673: rpc_srv_execCall(void * const data, rpc_func_t * __restrict call,
674: struct tagRPCCall * __restrict rpc, rpc_val_t * __restrict args)
675: {
676: void *dl;
677: rpc_callback_t func;
678: int ret;
679:
680: if (!data || !call || !rpc) {
681: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t exec call from RPC server ...\n");
682: return -1;
683: }
684:
685: dl = dlopen((char*) (*call->func_file ? call->func_file : NULL), RTLD_NOW);
686: if (!dl) {
687: rpc_SetErr(ENOENT, "Error:: Can`t attach module %s!\n", dlerror());
688: return -1;
689: }
690:
691: func = dlsym(dl, (char*) call->func_name);
692: if (func)
693: ret = func(data, call, rpc->call_argc, args);
694: else {
695: rpc_SetErr(ENOEXEC, "Error:: Can`t find function %s!\n", dlerror());
696: ret = -1;
697: }
698:
699: dlclose(dl);
700: return ret;
701: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>