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