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