Annotation of libaitrpc/src/srv.c, revision 1.1.1.1.2.14
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.14! misho 6: * $Id: srv.c,v 1.1.1.1.2.13 2010/06/28 15:18:43 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);
1.1.1.1.2.13 misho 367: rpc_srv_registerCall(srv, NULL, CALL_BLOBSTATE, 1);
1.1.1.1.2.4 misho 368: pthread_mutex_unlock(&srv->srv_mtx);
369:
1.1.1.1.2.5 misho 370: srv->srv_blob.state = enable; // enable BLOB
1.1.1.1.2.4 misho 371: return 0;
372: }
373:
374: /*
375: * rpc_srv_endBLOBServer() Destroy BLOB server, close all opened sockets and free resources
376: * @srv = RPC Server instance
377: * return: none
378: */
379: void
380: rpc_srv_endBLOBServer(rpc_srv_t * __restrict srv)
381: {
382: rpc_cli_t *c;
383: register int i;
384: rpc_blob_t *f;
385:
386: if (!srv) {
387: rpc_SetErr(EINVAL, "Error:: Can`t destroy server because parameter is null!\n");
388: return;
389: } else
1.1.1.1.2.5 misho 390: srv->srv_blob.state = disable;
1.1.1.1.2.4 misho 391:
1.1.1.1.2.10 misho 392: rpc_srv_unregisterCall(srv, NULL, CALL_BLOBSHUTDOWN);
393: rpc_srv_unregisterCall(srv, NULL, CALL_BLOBCLIENTS);
394: rpc_srv_unregisterCall(srv, NULL, CALL_BLOBVARS);
1.1.1.1.2.13 misho 395: rpc_srv_unregisterCall(srv, NULL, CALL_BLOBSTATE);
1.1.1.1.2.10 misho 396:
1.1.1.1.2.4 misho 397: for (i = 0, c = srv->srv_blob.clients; i < srv->srv_numcli && c; i++, c++)
398: if (c->cli_sa.sa_family)
399: shutdown(c->cli_sock, SHUT_RDWR);
400: close(srv->srv_blob.server.cli_sock);
401:
1.1.1.1.2.13 misho 402: if (srv->srv_blob.clients) {
1.1.1.1.2.4 misho 403: free(srv->srv_blob.clients);
1.1.1.1.2.13 misho 404: srv->srv_blob.clients = NULL;
405: }
1.1.1.1.2.4 misho 406:
407: pthread_mutex_lock(&srv->srv_blob.mtx);
408: while ((f = srv->srv_blob.blobs)) {
409: srv->srv_blob.blobs = f->blob_next;
1.1.1.1.2.14! misho 410: rpc_srv_blobFree(srv, f);
1.1.1.1.2.4 misho 411: free(f);
412: }
413: pthread_mutex_unlock(&srv->srv_blob.mtx);
414:
415: pthread_mutex_destroy(&srv->srv_blob.mtx);
416: }
417:
418: /*
419: * rpc_srv_execBLOBServer() Execute Main BLOB server loop and wait for clients requests
420: * @srv = RPC Server instance
421: * return: -1 error or 0 ok, infinite loop ...
422: */
423: int
424: rpc_srv_execBLOBServer(rpc_srv_t * __restrict srv)
425: {
426: socklen_t salen = sizeof(struct sockaddr);
427: register int i;
428: rpc_cli_t *c;
429: fd_set fds;
430: int ret;
431: struct timeval tv = { DEF_RPC_TIMEOUT, 0 };
432:
1.1.1.1.2.12 misho 433: if (!srv || srv->srv_blob.state == disable) {
1.1.1.1.2.4 misho 434: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t start BLOB server ...\n");
435: return -1;
436: }
437:
438: if (listen(srv->srv_blob.server.cli_sock, SOMAXCONN) == -1) {
439: LOGERR;
440: return -1;
441: }
442:
1.1.1.1.2.11 misho 443: while (!blob_Kill && !rpc_Kill) {
1.1.1.1.2.4 misho 444: for (c = srv->srv_blob.clients, i = 0; i < srv->srv_numcli && c; i++, c++)
445: if (!c->cli_sa.sa_family)
446: break;
1.1.1.1.2.13 misho 447: if (i >= srv->srv_numcli) {
1.1.1.1.2.4 misho 448: usleep(1000000);
449: continue;
450: }
451:
452: FD_ZERO(&fds);
453: FD_SET(srv->srv_blob.server.cli_sock, &fds);
454: ret = select(srv->srv_blob.server.cli_sock + 1, &fds, NULL, NULL, &tv);
455: if (ret == -1) {
456: LOGERR;
457: ret = 1;
458: break;
459: }
460: if (!ret)
461: continue;
462:
463: c->cli_sock = accept(srv->srv_blob.server.cli_sock, &c->cli_sa, &salen);
464: if (c->cli_sock == -1) {
465: LOGERR;
466: continue;
467: } else
468: c->cli_parent = srv;
469:
470: if (pthread_create(&c->cli_tid, NULL, rpc_srv_dispatchVars, c)) {
471: LOGERR;
472: continue;
473: }
474: }
475:
1.1.1.1.2.11 misho 476: srv->srv_blob.state = disable;
477:
1.1.1.1.2.4 misho 478: return 0;
479: }
480:
481:
482: /*
1.1 misho 483: * rpc_srv_initServer() Init & create RPC Server
484: * @regProgID = ProgramID for authentication & recognition
485: * @regProcID = ProcessID for authentication & recognition
486: * @concurentClients = Concurent clients at same time to this server
487: * @family = Family socket type, AF_INET or AF_INET6
488: * @csHost = Host name or IP address for bind server, if NULL any address
489: * @Port = Port for bind server, if Port == 0 default port is selected
490: * return: NULL == error or !=NULL bind and created RPC server instance
491: */
492: rpc_srv_t *
493: rpc_srv_initServer(u_int regProgID, u_int regProcID, int concurentClients,
494: u_short family, const char *csHost, u_short Port)
495: {
496: rpc_srv_t *srv = NULL;
497: int n = 1;
498: struct hostent *host = NULL;
499: struct sockaddr_in sin;
500: struct sockaddr_in6 sin6;
501:
502: if (!concurentClients || !regProgID || (family != AF_INET && family != AF_INET6)) {
503: rpc_SetErr(EINVAL, "Error:: Invalid parameters can`t init RPC server ...\n");
504: return NULL;
505: }
506: if (!Port)
507: Port = RPC_DEFPORT;
508: if (csHost) {
509: host = gethostbyname2(csHost, family);
510: if (!host) {
511: rpc_SetErr(h_errno, "Error:: %s\n", hstrerror(h_errno));
512: return NULL;
513: }
514: }
515: switch (family) {
516: case AF_INET:
517: memset(&sin, 0, sizeof sin);
518: sin.sin_len = sizeof sin;
519: sin.sin_family = family;
520: sin.sin_port = htons(Port);
521: if (csHost)
522: memcpy(&sin.sin_addr, host->h_addr, host->h_length);
523: break;
524: case AF_INET6:
525: memset(&sin6, 0, sizeof sin6);
526: sin6.sin6_len = sizeof sin6;
527: sin6.sin6_family = family;
528: sin6.sin6_port = htons(Port);
529: if (csHost)
530: memcpy(&sin6.sin6_addr, host->h_addr, host->h_length);
531: break;
532: default:
533: rpc_SetErr(EINVAL, "Error:: Invalid parameters can`t start RPC server ...\n");
534: return NULL;
535: }
536:
537: srv = malloc(sizeof(rpc_srv_t));
538: if (!srv) {
539: LOGERR;
540: return NULL;
541: } else
542: memset(srv, 0, sizeof(rpc_srv_t));
543:
544: srv->srv_numcli = concurentClients;
545: srv->srv_session.sess_version = RPC_VERSION;
546: srv->srv_session.sess_program = regProgID;
547: srv->srv_session.sess_process = regProcID;
548:
1.1.1.1.2.2 misho 549: srv->srv_server.cli_tid = pthread_self();
1.1 misho 550: srv->srv_server.cli_parent = srv;
551: if (family == AF_INET)
552: memcpy(&srv->srv_server.cli_sa, &sin, sizeof srv->srv_server.cli_sa);
553: else
554: memcpy(&srv->srv_server.cli_sa, &sin6, sizeof srv->srv_server.cli_sa);
555: srv->srv_server.cli_sock = socket(family, SOCK_STREAM, 0);
556: if (srv->srv_server.cli_sock == -1) {
557: LOGERR;
558: free(srv);
559: return NULL;
560: }
561: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
562: LOGERR;
563: close(srv->srv_server.cli_sock);
564: free(srv);
565: return NULL;
566: }
567: if (bind(srv->srv_server.cli_sock, &srv->srv_server.cli_sa, sizeof srv->srv_server.cli_sa) == -1) {
568: LOGERR;
569: close(srv->srv_server.cli_sock);
570: free(srv);
571: return NULL;
572: }
573:
574: srv->srv_clients = calloc(srv->srv_numcli, sizeof(rpc_cli_t));
575: if (!srv->srv_clients) {
576: LOGERR;
577: close(srv->srv_server.cli_sock);
578: free(srv);
579: return NULL;
580: } else
581: memset(srv->srv_clients, 0, srv->srv_numcli * sizeof(rpc_cli_t));
582:
1.1.1.1.2.1 misho 583: rpc_srv_registerCall(srv, NULL, CALL_SRVSHUTDOWN, 0);
1.1 misho 584: rpc_srv_registerCall(srv, NULL, CALL_SRVCLIENTS, 0);
585: rpc_srv_registerCall(srv, NULL, CALL_SRVCALLS, 0);
586: rpc_srv_registerCall(srv, NULL, CALL_SRVSESSIONS, 0);
1.1.1.1.2.1 misho 587:
1.1.1.1.2.4 misho 588: pthread_mutex_init(&srv->srv_mtx, NULL);
1.1 misho 589: return srv;
590: }
591:
592: /*
593: * rpc_srv_endServer() Destroy RPC server, close all opened sockets and free resources
594: * @srv = RPC Server instance
595: * return: none
596: */
597: void
598: rpc_srv_endServer(rpc_srv_t * __restrict srv)
599: {
600: rpc_cli_t *c;
601: register int i;
602: rpc_func_t *f;
603:
604: if (!srv) {
605: rpc_SetErr(EINVAL, "Error:: Can`t destroy server because parameter is null!\n");
606: return;
607: }
608:
1.1.1.1.2.4 misho 609: rpc_srv_endBLOBServer(srv);
1.1 misho 610:
611: for (i = 0, c = srv->srv_clients; i < srv->srv_numcli && c; i++, c++)
612: if (c->cli_sa.sa_family)
613: shutdown(c->cli_sock, SHUT_RDWR);
614: close(srv->srv_server.cli_sock);
615:
616: if (srv->srv_clients) {
617: free(srv->srv_clients);
1.1.1.1.2.13 misho 618: srv->srv_clients = NULL;
1.1 misho 619: srv->srv_numcli = 0;
620: }
621:
1.1.1.1.2.4 misho 622: pthread_mutex_lock(&srv->srv_mtx);
623: while ((f = srv->srv_funcs)) {
624: srv->srv_funcs = f->func_next;
625: free(f);
626: }
627: pthread_mutex_unlock(&srv->srv_mtx);
628:
629: pthread_mutex_destroy(&srv->srv_mtx);
630:
1.1 misho 631: free(srv);
632: srv = NULL;
633: }
634:
635: /*
636: * rpc_srv_execServer() Execute Main server loop and wait for clients requests
637: * @srv = RPC Server instance
638: * return: -1 error or 0 ok, infinite loop ...
639: */
640: int
641: rpc_srv_execServer(rpc_srv_t * __restrict srv)
642: {
643: socklen_t salen = sizeof(struct sockaddr);
644: register int i;
645: rpc_cli_t *c;
1.1.1.1.2.2 misho 646: fd_set fds;
647: int ret;
648: struct timeval tv = { DEF_RPC_TIMEOUT, 0 };
1.1 misho 649:
650: if (!srv) {
651: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t start RPC server ...\n");
652: return -1;
653: }
654:
655: if (listen(srv->srv_server.cli_sock, SOMAXCONN) == -1) {
656: LOGERR;
657: return -1;
658: }
659:
1.1.1.1.2.1 misho 660: while (!rpc_Kill) {
1.1 misho 661: for (c = srv->srv_clients, i = 0; i < srv->srv_numcli && c; i++, c++)
662: if (!c->cli_sa.sa_family)
663: break;
1.1.1.1.2.13 misho 664: if (i >= srv->srv_numcli) {
1.1 misho 665: usleep(1000000);
666: continue;
667: }
1.1.1.1.2.2 misho 668:
669: FD_ZERO(&fds);
670: FD_SET(srv->srv_server.cli_sock, &fds);
671: ret = select(srv->srv_server.cli_sock + 1, &fds, NULL, NULL, &tv);
672: if (ret == -1) {
673: LOGERR;
674: ret = 1;
675: break;
676: }
677: if (!ret)
678: continue;
679:
1.1 misho 680: c->cli_sock = accept(srv->srv_server.cli_sock, &c->cli_sa, &salen);
681: if (c->cli_sock == -1) {
682: LOGERR;
683: continue;
684: } else
685: c->cli_parent = srv;
686:
687: if (pthread_create(&c->cli_tid, NULL, rpc_srv_dispatchCall, c)) {
688: LOGERR;
689: continue;
690: }
691: }
692:
693: return 0;
694: }
695:
696: // ---------------------------------------------------------
697:
698: /*
699: * rpc_srv_execCall() Execute registered call from RPC server
700: * @call = Register RPC call
701: * @rpc = IN RPC call structure
702: * @args = IN RPC call array of rpc values
703: * return: -1 error, !=-1 ok
704: */
705: int
1.1.1.1.2.11 misho 706: rpc_srv_execCall(rpc_func_t * __restrict call, struct tagRPCCall * __restrict rpc,
707: rpc_val_t * __restrict args)
1.1 misho 708: {
709: void *dl;
710: rpc_callback_t func;
711: int ret;
712:
1.1.1.1.2.11 misho 713: if (!call || !rpc || !call->func_parent) {
1.1 misho 714: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t exec call from RPC server ...\n");
715: return -1;
716: }
717:
718: dl = dlopen((char*) (*call->func_file ? call->func_file : NULL), RTLD_NOW);
719: if (!dl) {
720: rpc_SetErr(ENOENT, "Error:: Can`t attach module %s!\n", dlerror());
721: return -1;
722: }
723:
724: func = dlsym(dl, (char*) call->func_name);
725: if (func)
1.1.1.1.2.11 misho 726: ret = func(call, rpc->call_argc, args);
1.1 misho 727: else {
728: rpc_SetErr(ENOEXEC, "Error:: Can`t find function %s!\n", dlerror());
729: ret = -1;
730: }
731:
732: dlclose(dl);
733: return ret;
734: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>