Annotation of libaitrpc/src/srv.c, revision 1.1
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 $
! 6: * $Id: aitcli.c,v 1.2.2.17 2010/06/09 09:32:30 misho Exp $
! 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;
! 77: case array:
! 78: v[i].val.array = (int8_t**) data;
! 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;
! 138: case array:
! 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:
! 146: memcpy(data, vals[i].val.array, vals[i].val_len);
! 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:
! 246: srv->srv_server.cli_parent = srv;
! 247: if (family == AF_INET)
! 248: memcpy(&srv->srv_server.cli_sa, &sin, sizeof srv->srv_server.cli_sa);
! 249: else
! 250: memcpy(&srv->srv_server.cli_sa, &sin6, sizeof srv->srv_server.cli_sa);
! 251: srv->srv_server.cli_sock = socket(family, SOCK_STREAM, 0);
! 252: if (srv->srv_server.cli_sock == -1) {
! 253: LOGERR;
! 254: free(srv);
! 255: return NULL;
! 256: }
! 257: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
! 258: LOGERR;
! 259: close(srv->srv_server.cli_sock);
! 260: free(srv);
! 261: return NULL;
! 262: }
! 263: if (bind(srv->srv_server.cli_sock, &srv->srv_server.cli_sa, sizeof srv->srv_server.cli_sa) == -1) {
! 264: LOGERR;
! 265: close(srv->srv_server.cli_sock);
! 266: free(srv);
! 267: return NULL;
! 268: }
! 269:
! 270: srv->srv_clients = calloc(srv->srv_numcli, sizeof(rpc_cli_t));
! 271: if (!srv->srv_clients) {
! 272: LOGERR;
! 273: close(srv->srv_server.cli_sock);
! 274: free(srv);
! 275: return NULL;
! 276: } else
! 277: memset(srv->srv_clients, 0, srv->srv_numcli * sizeof(rpc_cli_t));
! 278:
! 279: rpc_srv_registerCall(srv, NULL, CALL_SRVCLIENTS, 0);
! 280: rpc_srv_registerCall(srv, NULL, CALL_SRVCALLS, 0);
! 281: rpc_srv_registerCall(srv, NULL, CALL_SRVSESSIONS, 0);
! 282: return srv;
! 283: }
! 284:
! 285: /*
! 286: * rpc_srv_endServer() Destroy RPC server, close all opened sockets and free resources
! 287: * @srv = RPC Server instance
! 288: * return: none
! 289: */
! 290: void
! 291: rpc_srv_endServer(rpc_srv_t * __restrict srv)
! 292: {
! 293: rpc_cli_t *c;
! 294: register int i;
! 295: rpc_func_t *f;
! 296:
! 297: if (!srv) {
! 298: rpc_SetErr(EINVAL, "Error:: Can`t destroy server because parameter is null!\n");
! 299: return;
! 300: }
! 301:
! 302: while ((f = srv->srv_funcs)) {
! 303: srv->srv_funcs = f->func_next;
! 304: free(f);
! 305: }
! 306:
! 307: for (i = 0, c = srv->srv_clients; i < srv->srv_numcli && c; i++, c++)
! 308: if (c->cli_sa.sa_family)
! 309: shutdown(c->cli_sock, SHUT_RDWR);
! 310: close(srv->srv_server.cli_sock);
! 311:
! 312: if (srv->srv_clients) {
! 313: free(srv->srv_clients);
! 314: srv->srv_numcli = 0;
! 315: }
! 316:
! 317: free(srv);
! 318: srv = NULL;
! 319: }
! 320:
! 321: /*
! 322: * rpc_srv_execServer() Execute Main server loop and wait for clients requests
! 323: * @srv = RPC Server instance
! 324: * return: -1 error or 0 ok, infinite loop ...
! 325: */
! 326: int
! 327: rpc_srv_execServer(rpc_srv_t * __restrict srv)
! 328: {
! 329: socklen_t salen = sizeof(struct sockaddr);
! 330: register int i;
! 331: rpc_cli_t *c;
! 332:
! 333: if (!srv) {
! 334: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t start RPC server ...\n");
! 335: return -1;
! 336: }
! 337:
! 338: if (listen(srv->srv_server.cli_sock, SOMAXCONN) == -1) {
! 339: LOGERR;
! 340: return -1;
! 341: }
! 342:
! 343: while (42) {
! 344: for (c = srv->srv_clients, i = 0; i < srv->srv_numcli && c; i++, c++)
! 345: if (!c->cli_sa.sa_family)
! 346: break;
! 347: if (c && c->cli_sa.sa_family && c->cli_parent) {
! 348: usleep(1000000);
! 349: continue;
! 350: }
! 351: c->cli_sock = accept(srv->srv_server.cli_sock, &c->cli_sa, &salen);
! 352: if (c->cli_sock == -1) {
! 353: printf("%s(%d): #%d - %s\n", __func__, __LINE__, errno, strerror(errno));
! 354: LOGERR;
! 355: continue;
! 356: } else
! 357: c->cli_parent = srv;
! 358:
! 359: if (pthread_create(&c->cli_tid, NULL, rpc_srv_dispatchCall, c)) {
! 360: LOGERR;
! 361: continue;
! 362: }
! 363: }
! 364:
! 365: /* not reached !!! */
! 366: return 0;
! 367: }
! 368:
! 369: // ---------------------------------------------------------
! 370:
! 371: /*
! 372: * rpc_srv_freeValsCall() Free return variables for RPC call
! 373: * @call = RPC function call
! 374: * return: none
! 375: */
! 376: inline void
! 377: rpc_srv_freeValsCall(rpc_func_t * __restrict call)
! 378: {
! 379: rpc_srv_declValsCall(call, 0);
! 380: }
! 381:
! 382: /*
! 383: * rpc_srv_declValsCall() Declare return variables for RPC call
! 384: * @call = RPC function call
! 385: * @return_vals = Number of return variables
! 386: * return: -1 error, !=-1 ok
! 387: */
! 388: inline int
! 389: rpc_srv_declValsCall(rpc_func_t * __restrict call, int return_vals)
! 390: {
! 391: void *ptr;
! 392:
! 393: if (!call || return_vals < 0) {
! 394: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t declare return variables for RPC call...\n");
! 395: return -1;
! 396: } else
! 397: call->func_args = return_vals;
! 398:
! 399: if (!return_vals) {
! 400: if (call->func_vals) {
! 401: free(call->func_vals);
! 402: call->func_vals = NULL;
! 403: }
! 404: } else {
! 405: ptr = realloc(call->func_vals, return_vals * sizeof(rpc_val_t));
! 406: if (!ptr) {
! 407: LOGERR;
! 408: return -1;
! 409: } else
! 410: call->func_vals = ptr;
! 411: }
! 412:
! 413: return call->func_args;
! 414: }
! 415:
! 416: /*
! 417: * rpc_srv_delValsCall() Clean values from return variables of RPC call
! 418: * @call = RPC function call
! 419: * return: -1 error, !=-1 Returned number of cleaned RPC variables
! 420: */
! 421: inline int
! 422: rpc_srv_delValsCall(rpc_func_t * __restrict call)
! 423: {
! 424: if (!call) {
! 425: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t delete return variables ...\n");
! 426: return -1;
! 427: }
! 428:
! 429: memset(call->func_vals, 0, call->func_args * sizeof(rpc_val_t));
! 430: return call->func_args;
! 431: }
! 432:
! 433: /*
! 434: * rpc_srv_copyValsCall() Copy return variables for RPC call to new variable
! 435: * @call = RPC function call
! 436: * @newvals = New allocated variables array, must be free after use
! 437: * return: -1 error, !=-1 Returned number of copied RPC variables
! 438: */
! 439: inline int
! 440: rpc_srv_copyValsCall(rpc_func_t * __restrict call, rpc_val_t ** __restrict newvals)
! 441: {
! 442: if (!call || !newvals) {
! 443: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t copy return variables to new array\n");
! 444: return -1;
! 445: }
! 446:
! 447: *newvals = calloc(call->func_args, sizeof(rpc_val_t));
! 448: if (!*newvals) {
! 449: LOGERR;
! 450: return -1;
! 451: } else
! 452: memcpy(*newvals, call->func_vals, call->func_args * sizeof(rpc_val_t));
! 453:
! 454: return call->func_args;
! 455: }
! 456:
! 457: /*
! 458: * rpc_srv_getValsCall() Get return variables for RPC call
! 459: * @call = RPC function call
! 460: * @vals = Returned variables, may be NULL
! 461: * return: -1 error, !=-1 Number of returned variables
! 462: */
! 463: inline int
! 464: rpc_srv_getValsCall(rpc_func_t * __restrict call, rpc_val_t ** __restrict vals)
! 465: {
! 466: if (!call) {
! 467: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t get return variables ...\n");
! 468: return -1;
! 469: }
! 470:
! 471: if (vals)
! 472: *vals = call->func_vals;
! 473: return call->func_args;
! 474: }
! 475:
! 476: // ---------------------------------------------------------
! 477:
! 478: /*
! 479: * rpc_srv_registerCall() Register call to RPC server
! 480: * @srv = RPC Server instance
! 481: * @csModule = Module name, if NULL self binary
! 482: * @csFunc = Function name
! 483: * @args = Number of function arguments
! 484: * return: -1 error or 0 register ok
! 485: */
! 486: int
! 487: rpc_srv_registerCall(rpc_srv_t * __restrict srv, const char *csModule, const char *csFunc, u_char args)
! 488: {
! 489: rpc_func_t *func;
! 490: u_char str[MAXPATHLEN + UCHAR_MAX + 1];
! 491:
! 492: memset(str, 0, MAXPATHLEN + UCHAR_MAX + 1);
! 493: if (!srv || !csFunc) {
! 494: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t register function to RPC server ...\n");
! 495: return -1;
! 496: }
! 497: if (!(func = malloc(sizeof(rpc_func_t)))) {
! 498: LOGERR;
! 499: return -1;
! 500: } else {
! 501: memset(func, 0, sizeof(rpc_func_t));
! 502: strlcpy((char*) func->func_name, csFunc, UCHAR_MAX + 1);
! 503: }
! 504: if (csModule) {
! 505: strlcpy((char*) func->func_file, csModule, MAXPATHLEN);
! 506: strlcpy((char*) str, csModule, MAXPATHLEN + UCHAR_MAX + 1);
! 507: }
! 508: strlcat((char*) str, "__", MAXPATHLEN + UCHAR_MAX + 1);
! 509: strlcat((char*) str, csFunc, MAXPATHLEN + UCHAR_MAX + 1);
! 510:
! 511: func->func_tag = crcFletcher16((u_short*) str, (MAXPATHLEN + UCHAR_MAX + 1) / 2);
! 512: func->func_hash = hash_fnv((char*) str, MAXPATHLEN + UCHAR_MAX + 1);
! 513:
! 514: if (rpc_srv_declValsCall(func, args) == -1) {
! 515: free(func);
! 516: return -1;
! 517: }
! 518:
! 519: func->func_next = srv->srv_funcs;
! 520: srv->srv_funcs = func;
! 521: return 0;
! 522: }
! 523:
! 524: /*
! 525: * rpc_srv_unregisterCall() Unregister call from RPC server
! 526: * @srv = RPC Server instance
! 527: * @csModule = Module name, if NULL self binary
! 528: * @csFunc = Function name
! 529: * return: -1 error, 0 not found call, 1 unregister ok
! 530: */
! 531: int
! 532: rpc_srv_unregisterCall(rpc_srv_t * __restrict srv, const char *csModule, const char *csFunc)
! 533: {
! 534: rpc_func_t func, *f, *curr;
! 535: u_char str[MAXPATHLEN + UCHAR_MAX + 1];
! 536:
! 537: memset(&func, 0, sizeof(rpc_func_t));
! 538: memset(str, 0, MAXPATHLEN + UCHAR_MAX + 1);
! 539: if (!srv || !csFunc) {
! 540: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t unregister function from RPC server ...\n");
! 541: return -1;
! 542: } else
! 543: strlcpy((char*) func.func_name, csFunc, UCHAR_MAX + 1);
! 544: if (csModule) {
! 545: strlcpy((char*) func.func_file, csModule, MAXPATHLEN);
! 546: strlcpy((char*) str, csModule, MAXPATHLEN + UCHAR_MAX + 1);
! 547: }
! 548: strlcat((char*) str, "__", MAXPATHLEN + UCHAR_MAX + 1);
! 549: strlcat((char*) str, csFunc, MAXPATHLEN + UCHAR_MAX + 1);
! 550:
! 551: func.func_tag = crcFletcher16((u_short*) str, (MAXPATHLEN + UCHAR_MAX + 1) / 2);
! 552: func.func_hash = hash_fnv((char*) str, MAXPATHLEN + UCHAR_MAX + 1);
! 553:
! 554: f = rpc_srv_getCall(srv, func.func_tag, func.func_hash);
! 555: if (!f) // not found element for unregister
! 556: return 0;
! 557:
! 558: if (srv->srv_funcs == f) { // if is 1st element
! 559: srv->srv_funcs = srv->srv_funcs->func_next;
! 560:
! 561: if (f->func_args && f->func_vals)
! 562: free(f->func_vals);
! 563: free(f);
! 564: } else {
! 565: for (curr = srv->srv_funcs; curr->func_next != f; curr = curr->func_next);
! 566: curr->func_next = curr->func_next->func_next;
! 567:
! 568: if (f->func_args && f->func_vals)
! 569: free(f->func_vals);
! 570: free(f);
! 571: }
! 572:
! 573: return 1;
! 574: }
! 575:
! 576: /*
! 577: * rpc_srv_getCall() Get registered call from RPC server
! 578: * @srv = RPC Server instance
! 579: * @tag = tag for function
! 580: * @hash = hash for function
! 581: * return: NULL not found call, !=NULL return call
! 582: */
! 583: inline rpc_func_t *
! 584: rpc_srv_getCall(rpc_srv_t * __restrict srv, uint16_t tag, uint32_t hash)
! 585: {
! 586: rpc_func_t *f;
! 587:
! 588: if (!srv) {
! 589: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t get function from RPC server ...\n");
! 590: return NULL;
! 591: }
! 592:
! 593: for (f = srv->srv_funcs; f; f = f->func_next)
! 594: if (f->func_tag == tag && f->func_hash == hash)
! 595: break;
! 596:
! 597: return f;
! 598: }
! 599:
! 600: /*
! 601: * rpc_srv_getFunc() Get registered call from RPC server by Name
! 602: * @srv = RPC Server instance
! 603: * @csModule = Module name, if NULL self binary
! 604: * @csFunc = Function name
! 605: * return: NULL not found call, !=NULL return call
! 606: */
! 607: rpc_func_t *
! 608: rpc_srv_getFunc(rpc_srv_t * __restrict srv, const char *csModule, const char *csFunc)
! 609: {
! 610: rpc_func_t func;
! 611: u_char str[MAXPATHLEN + UCHAR_MAX + 1];
! 612:
! 613: memset(&func, 0, sizeof(rpc_func_t));
! 614: memset(str, 0, MAXPATHLEN + UCHAR_MAX + 1);
! 615: if (!srv || !csFunc) {
! 616: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t get function from RPC server ...\n");
! 617: return NULL;
! 618: } else
! 619: strlcpy((char*) func.func_name, csFunc, UCHAR_MAX + 1);
! 620: if (csModule) {
! 621: strlcpy((char*) func.func_file, csModule, MAXPATHLEN);
! 622: strlcpy((char*) str, csModule, MAXPATHLEN + UCHAR_MAX + 1);
! 623: }
! 624: strlcat((char*) str, "__", MAXPATHLEN + UCHAR_MAX + 1);
! 625: strlcat((char*) str, csFunc, MAXPATHLEN + UCHAR_MAX + 1);
! 626:
! 627: func.func_tag = crcFletcher16((u_short*) str, (MAXPATHLEN + UCHAR_MAX + 1) / 2);
! 628: func.func_hash = hash_fnv((char*) str, MAXPATHLEN + UCHAR_MAX + 1);
! 629:
! 630: return rpc_srv_getCall(srv, func.func_tag, func.func_hash);
! 631: }
! 632:
! 633: // ---------------------------------------------------------
! 634:
! 635: /*
! 636: * rpc_srv_execCall() Execute registered call from RPC server
! 637: * @data = RPC const data
! 638: * @call = Register RPC call
! 639: * @rpc = IN RPC call structure
! 640: * @args = IN RPC call array of rpc values
! 641: * return: -1 error, !=-1 ok
! 642: */
! 643: int
! 644: rpc_srv_execCall(void * const data, rpc_func_t * __restrict call,
! 645: struct tagRPCCall * __restrict rpc, rpc_val_t * __restrict args)
! 646: {
! 647: void *dl;
! 648: rpc_callback_t func;
! 649: int ret;
! 650:
! 651: if (!data || !call || !rpc) {
! 652: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t exec call from RPC server ...\n");
! 653: return -1;
! 654: }
! 655:
! 656: dl = dlopen((char*) (*call->func_file ? call->func_file : NULL), RTLD_NOW);
! 657: if (!dl) {
! 658: rpc_SetErr(ENOENT, "Error:: Can`t attach module %s!\n", dlerror());
! 659: return -1;
! 660: }
! 661:
! 662: func = dlsym(dl, (char*) call->func_name);
! 663: if (func)
! 664: ret = func(data, call, rpc->call_argc, args);
! 665: else {
! 666: rpc_SetErr(ENOEXEC, "Error:: Can`t find function %s!\n", dlerror());
! 667: ret = -1;
! 668: }
! 669:
! 670: dlclose(dl);
! 671: return ret;
! 672: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>