Annotation of libaitrpc/src/srv.c, revision 1.1.1.1.2.4
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.4! misho 6: * $Id: srv.c,v 1.1.1.1.2.3 2010/06/23 07:50:01 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.4! misho 78: rpc_srv_getBLOB(&v[i], data);
! 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:
! 150: rpc_srv_setBLOB(&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;
! 184: // rpc_srv_t *s;
! 185: int ret;
! 186:
! 187: return (void*) ret;
! 188: }
! 189:
1.1 misho 190: // -------------------------------------------------
191:
192: /*
1.1.1.1.2.4! misho 193: * rpc_srv_initBLOBServer() Init & create BLOB Server
! 194: * @Port = Port for bind server, if Port == 0 default port is selected
! 195: * return: -1 == error or 0 bind and created BLOB server instance
! 196: */
! 197: int
! 198: rpc_srv_initBLOBServer(rpc_srv_t * __restrict srv, u_short Port)
! 199: {
! 200: int n = 1;
! 201: struct sockaddr_in sin;
! 202: struct sockaddr_in6 sin6;
! 203:
! 204: if (!srv) {
! 205: rpc_SetErr(EINVAL, "Error:: Invalid parameters can`t init BLOB server ...\n");
! 206: return -1;
! 207: }
! 208: if (srv->srv_blob.state) {
! 209: rpc_SetErr(EPERM, "Warning:: Already started BLOB server!\n");
! 210: return 0;
! 211: }
! 212: if (!Port)
! 213: Port = RPC_DEFPORT + 1;
! 214:
! 215: memset(&srv->srv_blob, 0, sizeof srv->srv_blob);
! 216: srv->srv_blob.server.cli_tid = pthread_self();
! 217: srv->srv_blob.server.cli_parent = srv;
! 218: if (srv->srv_server.cli_sa.sa_family == AF_INET) {
! 219: memcpy(&sin, &srv->srv_server.cli_sa, sizeof sin);
! 220: sin.sin_port = htons(Port);
! 221: memcpy(&srv->srv_blob.server.cli_sa, &sin, sizeof(struct sockaddr));
! 222: } else {
! 223: memcpy(&sin6, &srv->srv_server.cli_sa, sizeof sin6);
! 224: sin6.sin6_port = htons(Port);
! 225: memcpy(&srv->srv_blob.server.cli_sa, &sin6, sizeof(struct sockaddr));
! 226: }
! 227:
! 228: srv->srv_blob.server.cli_sock = socket(srv->srv_server.cli_sa.sa_family, SOCK_STREAM, 0);
! 229: if (srv->srv_blob.server.cli_sock == -1) {
! 230: LOGERR;
! 231: return -1;
! 232: }
! 233: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
! 234: LOGERR;
! 235: close(srv->srv_blob.server.cli_sock);
! 236: return -1;
! 237: }
! 238: if (bind(srv->srv_blob.server.cli_sock, &srv->srv_blob.server.cli_sa,
! 239: sizeof srv->srv_blob.server.cli_sa) == -1) {
! 240: LOGERR;
! 241: close(srv->srv_blob.server.cli_sock);
! 242: return -1;
! 243: }
! 244:
! 245: srv->srv_blob.clients = calloc(srv->srv_numcli, sizeof(rpc_cli_t));
! 246: if (!srv->srv_blob.clients) {
! 247: LOGERR;
! 248: close(srv->srv_blob.server.cli_sock);
! 249: return -1;
! 250: } else
! 251: memset(srv->srv_blob.clients, 0, srv->srv_numcli * sizeof(rpc_cli_t));
! 252:
! 253: pthread_mutex_init(&srv->srv_blob.mtx, NULL);
! 254:
! 255: pthread_mutex_lock(&srv->srv_mtx);
! 256: rpc_srv_registerCall(srv, NULL, CALL_BLOBSHUTDOWN, 0);
! 257: rpc_srv_registerCall(srv, NULL, CALL_BLOBCLIENTS, 0);
! 258: rpc_srv_registerCall(srv, NULL, CALL_BLOBVARS, 0);
! 259: pthread_mutex_unlock(&srv->srv_mtx);
! 260:
! 261: srv->srv_blob.state = 1; // enable BLOB
! 262: return 0;
! 263: }
! 264:
! 265: /*
! 266: * rpc_srv_endBLOBServer() Destroy BLOB server, close all opened sockets and free resources
! 267: * @srv = RPC Server instance
! 268: * return: none
! 269: */
! 270: void
! 271: rpc_srv_endBLOBServer(rpc_srv_t * __restrict srv)
! 272: {
! 273: rpc_cli_t *c;
! 274: register int i;
! 275: rpc_blob_t *f;
! 276:
! 277: if (!srv) {
! 278: rpc_SetErr(EINVAL, "Error:: Can`t destroy server because parameter is null!\n");
! 279: return;
! 280: } else
! 281: srv->srv_blob.state = 0;
! 282:
! 283: for (i = 0, c = srv->srv_blob.clients; i < srv->srv_numcli && c; i++, c++)
! 284: if (c->cli_sa.sa_family)
! 285: shutdown(c->cli_sock, SHUT_RDWR);
! 286: close(srv->srv_blob.server.cli_sock);
! 287:
! 288: if (srv->srv_blob.clients)
! 289: free(srv->srv_blob.clients);
! 290:
! 291: pthread_mutex_lock(&srv->srv_blob.mtx);
! 292: while ((f = srv->srv_blob.blobs)) {
! 293: srv->srv_blob.blobs = f->blob_next;
! 294: free(f);
! 295: }
! 296: pthread_mutex_unlock(&srv->srv_blob.mtx);
! 297:
! 298: pthread_mutex_destroy(&srv->srv_blob.mtx);
! 299: }
! 300:
! 301: /*
! 302: * rpc_srv_execBLOBServer() Execute Main BLOB server loop and wait for clients requests
! 303: * @srv = RPC Server instance
! 304: * return: -1 error or 0 ok, infinite loop ...
! 305: */
! 306: int
! 307: rpc_srv_execBLOBServer(rpc_srv_t * __restrict srv)
! 308: {
! 309: socklen_t salen = sizeof(struct sockaddr);
! 310: register int i;
! 311: rpc_cli_t *c;
! 312: fd_set fds;
! 313: int ret;
! 314: struct timeval tv = { DEF_RPC_TIMEOUT, 0 };
! 315:
! 316: if (!srv || !srv->srv_blob.state) {
! 317: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t start BLOB server ...\n");
! 318: return -1;
! 319: }
! 320:
! 321: if (listen(srv->srv_blob.server.cli_sock, SOMAXCONN) == -1) {
! 322: LOGERR;
! 323: return -1;
! 324: }
! 325:
! 326: while (!rpc_Kill) {
! 327: for (c = srv->srv_blob.clients, i = 0; i < srv->srv_numcli && c; i++, c++)
! 328: if (!c->cli_sa.sa_family)
! 329: break;
! 330: if (c && c->cli_sa.sa_family && c->cli_parent) {
! 331: usleep(1000000);
! 332: continue;
! 333: }
! 334:
! 335: FD_ZERO(&fds);
! 336: FD_SET(srv->srv_blob.server.cli_sock, &fds);
! 337: ret = select(srv->srv_blob.server.cli_sock + 1, &fds, NULL, NULL, &tv);
! 338: if (ret == -1) {
! 339: LOGERR;
! 340: ret = 1;
! 341: break;
! 342: }
! 343: if (!ret)
! 344: continue;
! 345:
! 346: c->cli_sock = accept(srv->srv_blob.server.cli_sock, &c->cli_sa, &salen);
! 347: if (c->cli_sock == -1) {
! 348: LOGERR;
! 349: continue;
! 350: } else
! 351: c->cli_parent = srv;
! 352:
! 353: if (pthread_create(&c->cli_tid, NULL, rpc_srv_dispatchVars, c)) {
! 354: LOGERR;
! 355: continue;
! 356: }
! 357: }
! 358:
! 359: return 0;
! 360: }
! 361:
! 362:
! 363: /*
1.1 misho 364: * rpc_srv_initServer() Init & create RPC Server
365: * @regProgID = ProgramID for authentication & recognition
366: * @regProcID = ProcessID for authentication & recognition
367: * @concurentClients = Concurent clients at same time to this server
368: * @family = Family socket type, AF_INET or AF_INET6
369: * @csHost = Host name or IP address for bind server, if NULL any address
370: * @Port = Port for bind server, if Port == 0 default port is selected
371: * return: NULL == error or !=NULL bind and created RPC server instance
372: */
373: rpc_srv_t *
374: rpc_srv_initServer(u_int regProgID, u_int regProcID, int concurentClients,
375: u_short family, const char *csHost, u_short Port)
376: {
377: rpc_srv_t *srv = NULL;
378: int n = 1;
379: struct hostent *host = NULL;
380: struct sockaddr_in sin;
381: struct sockaddr_in6 sin6;
382:
383: if (!concurentClients || !regProgID || (family != AF_INET && family != AF_INET6)) {
384: rpc_SetErr(EINVAL, "Error:: Invalid parameters can`t init RPC server ...\n");
385: return NULL;
386: }
387: if (!Port)
388: Port = RPC_DEFPORT;
389: if (csHost) {
390: host = gethostbyname2(csHost, family);
391: if (!host) {
392: rpc_SetErr(h_errno, "Error:: %s\n", hstrerror(h_errno));
393: return NULL;
394: }
395: }
396: switch (family) {
397: case AF_INET:
398: memset(&sin, 0, sizeof sin);
399: sin.sin_len = sizeof sin;
400: sin.sin_family = family;
401: sin.sin_port = htons(Port);
402: if (csHost)
403: memcpy(&sin.sin_addr, host->h_addr, host->h_length);
404: break;
405: case AF_INET6:
406: memset(&sin6, 0, sizeof sin6);
407: sin6.sin6_len = sizeof sin6;
408: sin6.sin6_family = family;
409: sin6.sin6_port = htons(Port);
410: if (csHost)
411: memcpy(&sin6.sin6_addr, host->h_addr, host->h_length);
412: break;
413: default:
414: rpc_SetErr(EINVAL, "Error:: Invalid parameters can`t start RPC server ...\n");
415: return NULL;
416: }
417:
418: srv = malloc(sizeof(rpc_srv_t));
419: if (!srv) {
420: LOGERR;
421: return NULL;
422: } else
423: memset(srv, 0, sizeof(rpc_srv_t));
424:
425: srv->srv_numcli = concurentClients;
426: srv->srv_session.sess_version = RPC_VERSION;
427: srv->srv_session.sess_program = regProgID;
428: srv->srv_session.sess_process = regProcID;
429:
1.1.1.1.2.2 misho 430: srv->srv_server.cli_tid = pthread_self();
1.1 misho 431: srv->srv_server.cli_parent = srv;
432: if (family == AF_INET)
433: memcpy(&srv->srv_server.cli_sa, &sin, sizeof srv->srv_server.cli_sa);
434: else
435: memcpy(&srv->srv_server.cli_sa, &sin6, sizeof srv->srv_server.cli_sa);
436: srv->srv_server.cli_sock = socket(family, SOCK_STREAM, 0);
437: if (srv->srv_server.cli_sock == -1) {
438: LOGERR;
439: free(srv);
440: return NULL;
441: }
442: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
443: LOGERR;
444: close(srv->srv_server.cli_sock);
445: free(srv);
446: return NULL;
447: }
448: if (bind(srv->srv_server.cli_sock, &srv->srv_server.cli_sa, sizeof srv->srv_server.cli_sa) == -1) {
449: LOGERR;
450: close(srv->srv_server.cli_sock);
451: free(srv);
452: return NULL;
453: }
454:
455: srv->srv_clients = calloc(srv->srv_numcli, sizeof(rpc_cli_t));
456: if (!srv->srv_clients) {
457: LOGERR;
458: close(srv->srv_server.cli_sock);
459: free(srv);
460: return NULL;
461: } else
462: memset(srv->srv_clients, 0, srv->srv_numcli * sizeof(rpc_cli_t));
463:
1.1.1.1.2.1 misho 464: rpc_srv_registerCall(srv, NULL, CALL_SRVSHUTDOWN, 0);
1.1 misho 465: rpc_srv_registerCall(srv, NULL, CALL_SRVCLIENTS, 0);
466: rpc_srv_registerCall(srv, NULL, CALL_SRVCALLS, 0);
467: rpc_srv_registerCall(srv, NULL, CALL_SRVSESSIONS, 0);
1.1.1.1.2.1 misho 468:
1.1.1.1.2.4! misho 469: pthread_mutex_init(&srv->srv_mtx, NULL);
1.1 misho 470: return srv;
471: }
472:
473: /*
474: * rpc_srv_endServer() Destroy RPC server, close all opened sockets and free resources
475: * @srv = RPC Server instance
476: * return: none
477: */
478: void
479: rpc_srv_endServer(rpc_srv_t * __restrict srv)
480: {
481: rpc_cli_t *c;
482: register int i;
483: rpc_func_t *f;
484:
485: if (!srv) {
486: rpc_SetErr(EINVAL, "Error:: Can`t destroy server because parameter is null!\n");
487: return;
488: }
489:
1.1.1.1.2.4! misho 490: rpc_srv_endBLOBServer(srv);
1.1 misho 491:
492: for (i = 0, c = srv->srv_clients; i < srv->srv_numcli && c; i++, c++)
493: if (c->cli_sa.sa_family)
494: shutdown(c->cli_sock, SHUT_RDWR);
495: close(srv->srv_server.cli_sock);
496:
497: if (srv->srv_clients) {
498: free(srv->srv_clients);
499: srv->srv_numcli = 0;
500: }
501:
1.1.1.1.2.4! misho 502: pthread_mutex_lock(&srv->srv_mtx);
! 503: while ((f = srv->srv_funcs)) {
! 504: srv->srv_funcs = f->func_next;
! 505: free(f);
! 506: }
! 507: pthread_mutex_unlock(&srv->srv_mtx);
! 508:
! 509: pthread_mutex_destroy(&srv->srv_mtx);
! 510:
1.1 misho 511: free(srv);
512: srv = NULL;
513: }
514:
515: /*
516: * rpc_srv_execServer() Execute Main server loop and wait for clients requests
517: * @srv = RPC Server instance
518: * return: -1 error or 0 ok, infinite loop ...
519: */
520: int
521: rpc_srv_execServer(rpc_srv_t * __restrict srv)
522: {
523: socklen_t salen = sizeof(struct sockaddr);
524: register int i;
525: rpc_cli_t *c;
1.1.1.1.2.2 misho 526: fd_set fds;
527: int ret;
528: struct timeval tv = { DEF_RPC_TIMEOUT, 0 };
1.1 misho 529:
530: if (!srv) {
531: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t start RPC server ...\n");
532: return -1;
533: }
534:
535: if (listen(srv->srv_server.cli_sock, SOMAXCONN) == -1) {
536: LOGERR;
537: return -1;
538: }
539:
1.1.1.1.2.1 misho 540: while (!rpc_Kill) {
1.1 misho 541: for (c = srv->srv_clients, i = 0; i < srv->srv_numcli && c; i++, c++)
542: if (!c->cli_sa.sa_family)
543: break;
544: if (c && c->cli_sa.sa_family && c->cli_parent) {
545: usleep(1000000);
546: continue;
547: }
1.1.1.1.2.2 misho 548:
549: FD_ZERO(&fds);
550: FD_SET(srv->srv_server.cli_sock, &fds);
551: ret = select(srv->srv_server.cli_sock + 1, &fds, NULL, NULL, &tv);
552: if (ret == -1) {
553: LOGERR;
554: ret = 1;
555: break;
556: }
557: if (!ret)
558: continue;
559:
1.1 misho 560: c->cli_sock = accept(srv->srv_server.cli_sock, &c->cli_sa, &salen);
561: if (c->cli_sock == -1) {
562: LOGERR;
563: continue;
564: } else
565: c->cli_parent = srv;
566:
567: if (pthread_create(&c->cli_tid, NULL, rpc_srv_dispatchCall, c)) {
568: LOGERR;
569: continue;
570: }
571: }
572:
573: return 0;
574: }
575:
576: // ---------------------------------------------------------
577:
578: /*
579: * rpc_srv_freeValsCall() Free return variables for RPC call
580: * @call = RPC function call
581: * return: none
582: */
583: inline void
584: rpc_srv_freeValsCall(rpc_func_t * __restrict call)
585: {
586: rpc_srv_declValsCall(call, 0);
587: }
588:
589: /*
590: * rpc_srv_declValsCall() Declare return variables for RPC call
591: * @call = RPC function call
592: * @return_vals = Number of return variables
593: * return: -1 error, !=-1 ok
594: */
595: inline int
596: rpc_srv_declValsCall(rpc_func_t * __restrict call, int return_vals)
597: {
598: void *ptr;
599:
600: if (!call || return_vals < 0) {
601: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t declare return variables for RPC call...\n");
602: return -1;
603: } else
604: call->func_args = return_vals;
605:
606: if (!return_vals) {
607: if (call->func_vals) {
608: free(call->func_vals);
609: call->func_vals = NULL;
610: }
611: } else {
612: ptr = realloc(call->func_vals, return_vals * sizeof(rpc_val_t));
613: if (!ptr) {
614: LOGERR;
615: return -1;
616: } else
617: call->func_vals = ptr;
618: }
619:
620: return call->func_args;
621: }
622:
623: /*
624: * rpc_srv_delValsCall() Clean values from return variables of RPC call
625: * @call = RPC function call
626: * return: -1 error, !=-1 Returned number of cleaned RPC variables
627: */
628: inline int
629: rpc_srv_delValsCall(rpc_func_t * __restrict call)
630: {
631: if (!call) {
632: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t delete return variables ...\n");
633: return -1;
634: }
635:
636: memset(call->func_vals, 0, call->func_args * sizeof(rpc_val_t));
637: return call->func_args;
638: }
639:
640: /*
641: * rpc_srv_copyValsCall() Copy return variables for RPC call to new variable
642: * @call = RPC function call
643: * @newvals = New allocated variables array, must be free after use
644: * return: -1 error, !=-1 Returned number of copied RPC variables
645: */
646: inline int
647: rpc_srv_copyValsCall(rpc_func_t * __restrict call, rpc_val_t ** __restrict newvals)
648: {
649: if (!call || !newvals) {
650: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t copy return variables to new array\n");
651: return -1;
652: }
653:
654: *newvals = calloc(call->func_args, sizeof(rpc_val_t));
655: if (!*newvals) {
656: LOGERR;
657: return -1;
658: } else
659: memcpy(*newvals, call->func_vals, call->func_args * sizeof(rpc_val_t));
660:
661: return call->func_args;
662: }
663:
664: /*
665: * rpc_srv_getValsCall() Get return variables for RPC call
666: * @call = RPC function call
667: * @vals = Returned variables, may be NULL
668: * return: -1 error, !=-1 Number of returned variables
669: */
670: inline int
671: rpc_srv_getValsCall(rpc_func_t * __restrict call, rpc_val_t ** __restrict vals)
672: {
673: if (!call) {
674: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t get return variables ...\n");
675: return -1;
676: }
677:
678: if (vals)
679: *vals = call->func_vals;
680: return call->func_args;
681: }
682:
683: // ---------------------------------------------------------
684:
685: /*
686: * rpc_srv_registerCall() Register call to RPC server
687: * @srv = RPC Server instance
688: * @csModule = Module name, if NULL self binary
689: * @csFunc = Function name
690: * @args = Number of function arguments
691: * return: -1 error or 0 register ok
692: */
693: int
694: rpc_srv_registerCall(rpc_srv_t * __restrict srv, const char *csModule, const char *csFunc, u_char args)
695: {
696: rpc_func_t *func;
697: u_char str[MAXPATHLEN + UCHAR_MAX + 1];
698:
699: memset(str, 0, MAXPATHLEN + UCHAR_MAX + 1);
700: if (!srv || !csFunc) {
701: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t register function to RPC server ...\n");
702: return -1;
703: }
704: if (!(func = malloc(sizeof(rpc_func_t)))) {
705: LOGERR;
706: return -1;
707: } else {
708: memset(func, 0, sizeof(rpc_func_t));
709: strlcpy((char*) func->func_name, csFunc, UCHAR_MAX + 1);
710: }
711: if (csModule) {
712: strlcpy((char*) func->func_file, csModule, MAXPATHLEN);
713: strlcpy((char*) str, csModule, MAXPATHLEN + UCHAR_MAX + 1);
714: }
715: strlcat((char*) str, "__", MAXPATHLEN + UCHAR_MAX + 1);
716: strlcat((char*) str, csFunc, MAXPATHLEN + UCHAR_MAX + 1);
717:
718: func->func_tag = crcFletcher16((u_short*) str, (MAXPATHLEN + UCHAR_MAX + 1) / 2);
719: func->func_hash = hash_fnv((char*) str, MAXPATHLEN + UCHAR_MAX + 1);
720:
721: if (rpc_srv_declValsCall(func, args) == -1) {
722: free(func);
723: return -1;
724: }
725:
1.1.1.1.2.4! misho 726: pthread_mutex_lock(&srv->srv_mtx);
1.1 misho 727: func->func_next = srv->srv_funcs;
728: srv->srv_funcs = func;
1.1.1.1.2.4! misho 729: pthread_mutex_unlock(&srv->srv_mtx);
1.1 misho 730: return 0;
731: }
732:
733: /*
734: * rpc_srv_unregisterCall() Unregister call from RPC server
735: * @srv = RPC Server instance
736: * @csModule = Module name, if NULL self binary
737: * @csFunc = Function name
738: * return: -1 error, 0 not found call, 1 unregister ok
739: */
740: int
741: rpc_srv_unregisterCall(rpc_srv_t * __restrict srv, const char *csModule, const char *csFunc)
742: {
743: rpc_func_t func, *f, *curr;
744: u_char str[MAXPATHLEN + UCHAR_MAX + 1];
745:
746: memset(&func, 0, sizeof(rpc_func_t));
747: memset(str, 0, MAXPATHLEN + UCHAR_MAX + 1);
748: if (!srv || !csFunc) {
749: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t unregister function from RPC server ...\n");
750: return -1;
751: } else
752: strlcpy((char*) func.func_name, csFunc, UCHAR_MAX + 1);
753: if (csModule) {
754: strlcpy((char*) func.func_file, csModule, MAXPATHLEN);
755: strlcpy((char*) str, csModule, MAXPATHLEN + UCHAR_MAX + 1);
756: }
757: strlcat((char*) str, "__", MAXPATHLEN + UCHAR_MAX + 1);
758: strlcat((char*) str, csFunc, MAXPATHLEN + UCHAR_MAX + 1);
759:
760: func.func_tag = crcFletcher16((u_short*) str, (MAXPATHLEN + UCHAR_MAX + 1) / 2);
761: func.func_hash = hash_fnv((char*) str, MAXPATHLEN + UCHAR_MAX + 1);
762:
763: f = rpc_srv_getCall(srv, func.func_tag, func.func_hash);
764: if (!f) // not found element for unregister
765: return 0;
766:
1.1.1.1.2.4! misho 767: pthread_mutex_lock(&srv->srv_mtx);
1.1 misho 768: if (srv->srv_funcs == f) { // if is 1st element
769: srv->srv_funcs = srv->srv_funcs->func_next;
770:
771: if (f->func_args && f->func_vals)
772: free(f->func_vals);
773: free(f);
774: } else {
775: for (curr = srv->srv_funcs; curr->func_next != f; curr = curr->func_next);
776: curr->func_next = curr->func_next->func_next;
777:
778: if (f->func_args && f->func_vals)
779: free(f->func_vals);
780: free(f);
781: }
1.1.1.1.2.4! misho 782: pthread_mutex_unlock(&srv->srv_mtx);
1.1 misho 783:
784: return 1;
785: }
786:
787: /*
788: * rpc_srv_getCall() Get registered call from RPC server
789: * @srv = RPC Server instance
790: * @tag = tag for function
791: * @hash = hash for function
792: * return: NULL not found call, !=NULL return call
793: */
794: inline rpc_func_t *
795: rpc_srv_getCall(rpc_srv_t * __restrict srv, uint16_t tag, uint32_t hash)
796: {
797: rpc_func_t *f;
798:
799: if (!srv) {
800: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t get function from RPC server ...\n");
801: return NULL;
802: }
803:
804: for (f = srv->srv_funcs; f; f = f->func_next)
805: if (f->func_tag == tag && f->func_hash == hash)
806: break;
807:
808: return f;
809: }
810:
811: /*
812: * rpc_srv_getFunc() Get registered call from RPC server by Name
813: * @srv = RPC Server instance
814: * @csModule = Module name, if NULL self binary
815: * @csFunc = Function name
816: * return: NULL not found call, !=NULL return call
817: */
818: rpc_func_t *
819: rpc_srv_getFunc(rpc_srv_t * __restrict srv, const char *csModule, const char *csFunc)
820: {
821: rpc_func_t func;
822: u_char str[MAXPATHLEN + UCHAR_MAX + 1];
823:
824: memset(&func, 0, sizeof(rpc_func_t));
825: memset(str, 0, MAXPATHLEN + UCHAR_MAX + 1);
826: if (!srv || !csFunc) {
827: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t get function from RPC server ...\n");
828: return NULL;
829: } else
830: strlcpy((char*) func.func_name, csFunc, UCHAR_MAX + 1);
831: if (csModule) {
832: strlcpy((char*) func.func_file, csModule, MAXPATHLEN);
833: strlcpy((char*) str, csModule, MAXPATHLEN + UCHAR_MAX + 1);
834: }
835: strlcat((char*) str, "__", MAXPATHLEN + UCHAR_MAX + 1);
836: strlcat((char*) str, csFunc, MAXPATHLEN + UCHAR_MAX + 1);
837:
838: func.func_tag = crcFletcher16((u_short*) str, (MAXPATHLEN + UCHAR_MAX + 1) / 2);
839: func.func_hash = hash_fnv((char*) str, MAXPATHLEN + UCHAR_MAX + 1);
840:
841: return rpc_srv_getCall(srv, func.func_tag, func.func_hash);
842: }
843:
844: // ---------------------------------------------------------
845:
846: /*
847: * rpc_srv_execCall() Execute registered call from RPC server
848: * @data = RPC const data
849: * @call = Register RPC call
850: * @rpc = IN RPC call structure
851: * @args = IN RPC call array of rpc values
852: * return: -1 error, !=-1 ok
853: */
854: int
855: rpc_srv_execCall(void * const data, rpc_func_t * __restrict call,
856: struct tagRPCCall * __restrict rpc, rpc_val_t * __restrict args)
857: {
858: void *dl;
859: rpc_callback_t func;
860: int ret;
861:
862: if (!data || !call || !rpc) {
863: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t exec call from RPC server ...\n");
864: return -1;
865: }
866:
867: dl = dlopen((char*) (*call->func_file ? call->func_file : NULL), RTLD_NOW);
868: if (!dl) {
869: rpc_SetErr(ENOENT, "Error:: Can`t attach module %s!\n", dlerror());
870: return -1;
871: }
872:
873: func = dlsym(dl, (char*) call->func_name);
874: if (func)
875: ret = func(data, call, rpc->call_argc, args);
876: else {
877: rpc_SetErr(ENOEXEC, "Error:: Can`t find function %s!\n", dlerror());
878: ret = -1;
879: }
880:
881: dlclose(dl);
882: return ret;
883: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>