Annotation of libaitrpc/src/srv.c, revision 1.1.1.1.2.3
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.3! misho 6: * $Id: srv.c,v 1.1.1.1.2.2 2010/06/18 15:47:34 misho Exp $
1.1 misho 7: *
8: *************************************************************************/
9: #include "global.h"
10:
11:
12: static void *
13: rpc_srv_dispatchCall(void *arg)
14: {
15: rpc_cli_t *c = arg;
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:
! 78: v[i].val.blob = (int8_t**) data;
1.1 misho 79: data += v[i].val_len;
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 misho 139: if (ret || Limit + vals[i].val_len > BUFSIZ) {
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.3! misho 146: memcpy(data, vals[i].val.blob, vals[i].val_len);
1.1 misho 147: data += vals[i].val_len;
148: Limit += vals[i].val_len;
149: break;
150: default:
151: break;
152: }
153:
154: RPC_FREE_VAL(&vals[i]);
155: }
156: }
157:
158: if ((ret = send(c->cli_sock, buf, Limit, 0)) == -1) {
159: LOGERR;
160: ret = -8;
161: break;
162: }
163: if (ret != Limit) {
164: rpc_SetErr(EBADMSG, "Error:: in send RPC request, should be send %d bytes, "
165: "really is %d\n", Limit, ret);
166: ret = -9;
167: break;
168: }
169: } while (ret > -1);
170:
171: shutdown(c->cli_sock, SHUT_RDWR);
172: close(c->cli_sock);
173: memset(c, 0, sizeof(rpc_cli_t));
174: return (void*) ret;
175: }
176:
177: // -------------------------------------------------
178:
179: /*
180: * rpc_srv_initServer() Init & create RPC Server
181: * @regProgID = ProgramID for authentication & recognition
182: * @regProcID = ProcessID for authentication & recognition
183: * @concurentClients = Concurent clients at same time to this server
184: * @family = Family socket type, AF_INET or AF_INET6
185: * @csHost = Host name or IP address for bind server, if NULL any address
186: * @Port = Port for bind server, if Port == 0 default port is selected
187: * return: NULL == error or !=NULL bind and created RPC server instance
188: */
189: rpc_srv_t *
190: rpc_srv_initServer(u_int regProgID, u_int regProcID, int concurentClients,
191: u_short family, const char *csHost, u_short Port)
192: {
193: rpc_srv_t *srv = NULL;
194: int n = 1;
195: struct hostent *host = NULL;
196: struct sockaddr_in sin;
197: struct sockaddr_in6 sin6;
198:
199: if (!concurentClients || !regProgID || (family != AF_INET && family != AF_INET6)) {
200: rpc_SetErr(EINVAL, "Error:: Invalid parameters can`t init RPC server ...\n");
201: return NULL;
202: }
203: if (!Port)
204: Port = RPC_DEFPORT;
205: if (csHost) {
206: host = gethostbyname2(csHost, family);
207: if (!host) {
208: rpc_SetErr(h_errno, "Error:: %s\n", hstrerror(h_errno));
209: return NULL;
210: }
211: }
212: switch (family) {
213: case AF_INET:
214: memset(&sin, 0, sizeof sin);
215: sin.sin_len = sizeof sin;
216: sin.sin_family = family;
217: sin.sin_port = htons(Port);
218: if (csHost)
219: memcpy(&sin.sin_addr, host->h_addr, host->h_length);
220: break;
221: case AF_INET6:
222: memset(&sin6, 0, sizeof sin6);
223: sin6.sin6_len = sizeof sin6;
224: sin6.sin6_family = family;
225: sin6.sin6_port = htons(Port);
226: if (csHost)
227: memcpy(&sin6.sin6_addr, host->h_addr, host->h_length);
228: break;
229: default:
230: rpc_SetErr(EINVAL, "Error:: Invalid parameters can`t start RPC server ...\n");
231: return NULL;
232: }
233:
234: srv = malloc(sizeof(rpc_srv_t));
235: if (!srv) {
236: LOGERR;
237: return NULL;
238: } else
239: memset(srv, 0, sizeof(rpc_srv_t));
240:
241: srv->srv_numcli = concurentClients;
242: srv->srv_session.sess_version = RPC_VERSION;
243: srv->srv_session.sess_program = regProgID;
244: srv->srv_session.sess_process = regProcID;
245:
1.1.1.1.2.2 misho 246: srv->srv_server.cli_tid = pthread_self();
1.1 misho 247: srv->srv_server.cli_parent = srv;
248: if (family == AF_INET)
249: memcpy(&srv->srv_server.cli_sa, &sin, sizeof srv->srv_server.cli_sa);
250: else
251: memcpy(&srv->srv_server.cli_sa, &sin6, sizeof srv->srv_server.cli_sa);
252: srv->srv_server.cli_sock = socket(family, SOCK_STREAM, 0);
253: if (srv->srv_server.cli_sock == -1) {
254: LOGERR;
255: free(srv);
256: return NULL;
257: }
258: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
259: LOGERR;
260: close(srv->srv_server.cli_sock);
261: free(srv);
262: return NULL;
263: }
264: if (bind(srv->srv_server.cli_sock, &srv->srv_server.cli_sa, sizeof srv->srv_server.cli_sa) == -1) {
265: LOGERR;
266: close(srv->srv_server.cli_sock);
267: free(srv);
268: return NULL;
269: }
270:
271: srv->srv_clients = calloc(srv->srv_numcli, sizeof(rpc_cli_t));
272: if (!srv->srv_clients) {
273: LOGERR;
274: close(srv->srv_server.cli_sock);
275: free(srv);
276: return NULL;
277: } else
278: memset(srv->srv_clients, 0, srv->srv_numcli * sizeof(rpc_cli_t));
279:
1.1.1.1.2.1 misho 280: rpc_srv_registerCall(srv, NULL, CALL_SRVSHUTDOWN, 0);
1.1 misho 281: rpc_srv_registerCall(srv, NULL, CALL_SRVCLIENTS, 0);
282: rpc_srv_registerCall(srv, NULL, CALL_SRVCALLS, 0);
283: rpc_srv_registerCall(srv, NULL, CALL_SRVSESSIONS, 0);
1.1.1.1.2.1 misho 284:
285: pthread_mutex_init(&rpc_mtx, NULL);
1.1 misho 286: return srv;
287: }
288:
289: /*
290: * rpc_srv_endServer() Destroy RPC server, close all opened sockets and free resources
291: * @srv = RPC Server instance
292: * return: none
293: */
294: void
295: rpc_srv_endServer(rpc_srv_t * __restrict srv)
296: {
297: rpc_cli_t *c;
298: register int i;
299: rpc_func_t *f;
300:
301: if (!srv) {
302: rpc_SetErr(EINVAL, "Error:: Can`t destroy server because parameter is null!\n");
303: return;
304: }
305:
1.1.1.1.2.1 misho 306: pthread_mutex_destroy(&rpc_mtx);
307:
1.1 misho 308: while ((f = srv->srv_funcs)) {
309: srv->srv_funcs = f->func_next;
310: free(f);
311: }
312:
313: for (i = 0, c = srv->srv_clients; i < srv->srv_numcli && c; i++, c++)
314: if (c->cli_sa.sa_family)
315: shutdown(c->cli_sock, SHUT_RDWR);
316: close(srv->srv_server.cli_sock);
317:
318: if (srv->srv_clients) {
319: free(srv->srv_clients);
320: srv->srv_numcli = 0;
321: }
322:
323: free(srv);
324: srv = NULL;
325: }
326:
327: /*
328: * rpc_srv_execServer() Execute Main server loop and wait for clients requests
329: * @srv = RPC Server instance
330: * return: -1 error or 0 ok, infinite loop ...
331: */
332: int
333: rpc_srv_execServer(rpc_srv_t * __restrict srv)
334: {
335: socklen_t salen = sizeof(struct sockaddr);
336: register int i;
337: rpc_cli_t *c;
1.1.1.1.2.2 misho 338: fd_set fds;
339: int ret;
340: struct timeval tv = { DEF_RPC_TIMEOUT, 0 };
1.1 misho 341:
342: if (!srv) {
343: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t start RPC server ...\n");
344: return -1;
345: }
346:
347: if (listen(srv->srv_server.cli_sock, SOMAXCONN) == -1) {
348: LOGERR;
349: return -1;
350: }
351:
1.1.1.1.2.1 misho 352: while (!rpc_Kill) {
1.1 misho 353: for (c = srv->srv_clients, i = 0; i < srv->srv_numcli && c; i++, c++)
354: if (!c->cli_sa.sa_family)
355: break;
356: if (c && c->cli_sa.sa_family && c->cli_parent) {
357: usleep(1000000);
358: continue;
359: }
1.1.1.1.2.2 misho 360:
361: FD_ZERO(&fds);
362: FD_SET(srv->srv_server.cli_sock, &fds);
363: ret = select(srv->srv_server.cli_sock + 1, &fds, NULL, NULL, &tv);
364: if (ret == -1) {
365: LOGERR;
366: ret = 1;
367: break;
368: }
369: if (!ret)
370: continue;
371:
1.1 misho 372: c->cli_sock = accept(srv->srv_server.cli_sock, &c->cli_sa, &salen);
373: if (c->cli_sock == -1) {
374: LOGERR;
375: continue;
376: } else
377: c->cli_parent = srv;
378:
379: if (pthread_create(&c->cli_tid, NULL, rpc_srv_dispatchCall, c)) {
380: LOGERR;
381: continue;
382: }
383: }
384:
385: return 0;
386: }
387:
388: // ---------------------------------------------------------
389:
390: /*
391: * rpc_srv_freeValsCall() Free return variables for RPC call
392: * @call = RPC function call
393: * return: none
394: */
395: inline void
396: rpc_srv_freeValsCall(rpc_func_t * __restrict call)
397: {
398: rpc_srv_declValsCall(call, 0);
399: }
400:
401: /*
402: * rpc_srv_declValsCall() Declare return variables for RPC call
403: * @call = RPC function call
404: * @return_vals = Number of return variables
405: * return: -1 error, !=-1 ok
406: */
407: inline int
408: rpc_srv_declValsCall(rpc_func_t * __restrict call, int return_vals)
409: {
410: void *ptr;
411:
412: if (!call || return_vals < 0) {
413: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t declare return variables for RPC call...\n");
414: return -1;
415: } else
416: call->func_args = return_vals;
417:
418: if (!return_vals) {
419: if (call->func_vals) {
420: free(call->func_vals);
421: call->func_vals = NULL;
422: }
423: } else {
424: ptr = realloc(call->func_vals, return_vals * sizeof(rpc_val_t));
425: if (!ptr) {
426: LOGERR;
427: return -1;
428: } else
429: call->func_vals = ptr;
430: }
431:
432: return call->func_args;
433: }
434:
435: /*
436: * rpc_srv_delValsCall() Clean values from return variables of RPC call
437: * @call = RPC function call
438: * return: -1 error, !=-1 Returned number of cleaned RPC variables
439: */
440: inline int
441: rpc_srv_delValsCall(rpc_func_t * __restrict call)
442: {
443: if (!call) {
444: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t delete return variables ...\n");
445: return -1;
446: }
447:
448: memset(call->func_vals, 0, call->func_args * sizeof(rpc_val_t));
449: return call->func_args;
450: }
451:
452: /*
453: * rpc_srv_copyValsCall() Copy return variables for RPC call to new variable
454: * @call = RPC function call
455: * @newvals = New allocated variables array, must be free after use
456: * return: -1 error, !=-1 Returned number of copied RPC variables
457: */
458: inline int
459: rpc_srv_copyValsCall(rpc_func_t * __restrict call, rpc_val_t ** __restrict newvals)
460: {
461: if (!call || !newvals) {
462: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t copy return variables to new array\n");
463: return -1;
464: }
465:
466: *newvals = calloc(call->func_args, sizeof(rpc_val_t));
467: if (!*newvals) {
468: LOGERR;
469: return -1;
470: } else
471: memcpy(*newvals, call->func_vals, call->func_args * sizeof(rpc_val_t));
472:
473: return call->func_args;
474: }
475:
476: /*
477: * rpc_srv_getValsCall() Get return variables for RPC call
478: * @call = RPC function call
479: * @vals = Returned variables, may be NULL
480: * return: -1 error, !=-1 Number of returned variables
481: */
482: inline int
483: rpc_srv_getValsCall(rpc_func_t * __restrict call, rpc_val_t ** __restrict vals)
484: {
485: if (!call) {
486: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t get return variables ...\n");
487: return -1;
488: }
489:
490: if (vals)
491: *vals = call->func_vals;
492: return call->func_args;
493: }
494:
495: // ---------------------------------------------------------
496:
497: /*
498: * rpc_srv_registerCall() Register call to RPC server
499: * @srv = RPC Server instance
500: * @csModule = Module name, if NULL self binary
501: * @csFunc = Function name
502: * @args = Number of function arguments
503: * return: -1 error or 0 register ok
504: */
505: int
506: rpc_srv_registerCall(rpc_srv_t * __restrict srv, const char *csModule, const char *csFunc, u_char args)
507: {
508: rpc_func_t *func;
509: u_char str[MAXPATHLEN + UCHAR_MAX + 1];
510:
511: memset(str, 0, MAXPATHLEN + UCHAR_MAX + 1);
512: if (!srv || !csFunc) {
513: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t register function to RPC server ...\n");
514: return -1;
515: }
516: if (!(func = malloc(sizeof(rpc_func_t)))) {
517: LOGERR;
518: return -1;
519: } else {
520: memset(func, 0, sizeof(rpc_func_t));
521: strlcpy((char*) func->func_name, csFunc, UCHAR_MAX + 1);
522: }
523: if (csModule) {
524: strlcpy((char*) func->func_file, csModule, MAXPATHLEN);
525: strlcpy((char*) str, csModule, MAXPATHLEN + UCHAR_MAX + 1);
526: }
527: strlcat((char*) str, "__", MAXPATHLEN + UCHAR_MAX + 1);
528: strlcat((char*) str, csFunc, MAXPATHLEN + UCHAR_MAX + 1);
529:
530: func->func_tag = crcFletcher16((u_short*) str, (MAXPATHLEN + UCHAR_MAX + 1) / 2);
531: func->func_hash = hash_fnv((char*) str, MAXPATHLEN + UCHAR_MAX + 1);
532:
533: if (rpc_srv_declValsCall(func, args) == -1) {
534: free(func);
535: return -1;
536: }
537:
538: func->func_next = srv->srv_funcs;
539: srv->srv_funcs = func;
540: return 0;
541: }
542:
543: /*
544: * rpc_srv_unregisterCall() Unregister call from RPC server
545: * @srv = RPC Server instance
546: * @csModule = Module name, if NULL self binary
547: * @csFunc = Function name
548: * return: -1 error, 0 not found call, 1 unregister ok
549: */
550: int
551: rpc_srv_unregisterCall(rpc_srv_t * __restrict srv, const char *csModule, const char *csFunc)
552: {
553: rpc_func_t func, *f, *curr;
554: u_char str[MAXPATHLEN + UCHAR_MAX + 1];
555:
556: memset(&func, 0, sizeof(rpc_func_t));
557: memset(str, 0, MAXPATHLEN + UCHAR_MAX + 1);
558: if (!srv || !csFunc) {
559: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t unregister function from RPC server ...\n");
560: return -1;
561: } else
562: strlcpy((char*) func.func_name, csFunc, UCHAR_MAX + 1);
563: if (csModule) {
564: strlcpy((char*) func.func_file, csModule, MAXPATHLEN);
565: strlcpy((char*) str, csModule, MAXPATHLEN + UCHAR_MAX + 1);
566: }
567: strlcat((char*) str, "__", MAXPATHLEN + UCHAR_MAX + 1);
568: strlcat((char*) str, csFunc, MAXPATHLEN + UCHAR_MAX + 1);
569:
570: func.func_tag = crcFletcher16((u_short*) str, (MAXPATHLEN + UCHAR_MAX + 1) / 2);
571: func.func_hash = hash_fnv((char*) str, MAXPATHLEN + UCHAR_MAX + 1);
572:
573: f = rpc_srv_getCall(srv, func.func_tag, func.func_hash);
574: if (!f) // not found element for unregister
575: return 0;
576:
577: if (srv->srv_funcs == f) { // if is 1st element
578: srv->srv_funcs = srv->srv_funcs->func_next;
579:
580: if (f->func_args && f->func_vals)
581: free(f->func_vals);
582: free(f);
583: } else {
584: for (curr = srv->srv_funcs; curr->func_next != f; curr = curr->func_next);
585: curr->func_next = curr->func_next->func_next;
586:
587: if (f->func_args && f->func_vals)
588: free(f->func_vals);
589: free(f);
590: }
591:
592: return 1;
593: }
594:
595: /*
596: * rpc_srv_getCall() Get registered call from RPC server
597: * @srv = RPC Server instance
598: * @tag = tag for function
599: * @hash = hash for function
600: * return: NULL not found call, !=NULL return call
601: */
602: inline rpc_func_t *
603: rpc_srv_getCall(rpc_srv_t * __restrict srv, uint16_t tag, uint32_t hash)
604: {
605: rpc_func_t *f;
606:
607: if (!srv) {
608: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t get function from RPC server ...\n");
609: return NULL;
610: }
611:
612: for (f = srv->srv_funcs; f; f = f->func_next)
613: if (f->func_tag == tag && f->func_hash == hash)
614: break;
615:
616: return f;
617: }
618:
619: /*
620: * rpc_srv_getFunc() Get registered call from RPC server by Name
621: * @srv = RPC Server instance
622: * @csModule = Module name, if NULL self binary
623: * @csFunc = Function name
624: * return: NULL not found call, !=NULL return call
625: */
626: rpc_func_t *
627: rpc_srv_getFunc(rpc_srv_t * __restrict srv, const char *csModule, const char *csFunc)
628: {
629: rpc_func_t func;
630: u_char str[MAXPATHLEN + UCHAR_MAX + 1];
631:
632: memset(&func, 0, sizeof(rpc_func_t));
633: memset(str, 0, MAXPATHLEN + UCHAR_MAX + 1);
634: if (!srv || !csFunc) {
635: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t get function from RPC server ...\n");
636: return NULL;
637: } else
638: strlcpy((char*) func.func_name, csFunc, UCHAR_MAX + 1);
639: if (csModule) {
640: strlcpy((char*) func.func_file, csModule, MAXPATHLEN);
641: strlcpy((char*) str, csModule, MAXPATHLEN + UCHAR_MAX + 1);
642: }
643: strlcat((char*) str, "__", MAXPATHLEN + UCHAR_MAX + 1);
644: strlcat((char*) str, csFunc, MAXPATHLEN + UCHAR_MAX + 1);
645:
646: func.func_tag = crcFletcher16((u_short*) str, (MAXPATHLEN + UCHAR_MAX + 1) / 2);
647: func.func_hash = hash_fnv((char*) str, MAXPATHLEN + UCHAR_MAX + 1);
648:
649: return rpc_srv_getCall(srv, func.func_tag, func.func_hash);
650: }
651:
652: // ---------------------------------------------------------
653:
654: /*
655: * rpc_srv_execCall() Execute registered call from RPC server
656: * @data = RPC const data
657: * @call = Register RPC call
658: * @rpc = IN RPC call structure
659: * @args = IN RPC call array of rpc values
660: * return: -1 error, !=-1 ok
661: */
662: int
663: rpc_srv_execCall(void * const data, rpc_func_t * __restrict call,
664: struct tagRPCCall * __restrict rpc, rpc_val_t * __restrict args)
665: {
666: void *dl;
667: rpc_callback_t func;
668: int ret;
669:
670: if (!data || !call || !rpc) {
671: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t exec call from RPC server ...\n");
672: return -1;
673: }
674:
675: dl = dlopen((char*) (*call->func_file ? call->func_file : NULL), RTLD_NOW);
676: if (!dl) {
677: rpc_SetErr(ENOENT, "Error:: Can`t attach module %s!\n", dlerror());
678: return -1;
679: }
680:
681: func = dlsym(dl, (char*) call->func_name);
682: if (func)
683: ret = func(data, call, rpc->call_argc, args);
684: else {
685: rpc_SetErr(ENOEXEC, "Error:: Can`t find function %s!\n", dlerror());
686: ret = -1;
687: }
688:
689: dlclose(dl);
690: return ret;
691: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>