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