Annotation of libaitrpc/src/srv.c, revision 1.1.1.1.2.15
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.15! misho 6: * $Id: srv.c,v 1.1.1.1.2.14 2010/07/07 09:29:42 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: }
1.1.1.1.2.15! misho 240: printf("++++++ %p\n", b);
1.1.1.1.2.9 misho 241:
1.1.1.1.2.6 misho 242: if (rpc_srv_blobMap(s, b) != -1) {
243: ret = rpc_srv_sendBLOB(c, b);
244: rpc_srv_blobUnmap(b);
245: } else
246: ret = -7;
1.1.1.1.2.5 misho 247: break;
248: case set:
1.1.1.1.2.9 misho 249: if ((b = rpc_srv_registerBLOB(s, blob->hdr_len))) {
250: // set new BLOB variable for reply :)
251: blob->hdr_var = b->blob_var;
1.1.1.1.2.8 misho 252:
1.1.1.1.2.9 misho 253: ret = rpc_srv_recvBLOB(c, b);
254: rpc_srv_blobUnmap(b);
255: } else
1.1.1.1.2.6 misho 256: ret = -7;
1.1.1.1.2.5 misho 257: break;
258: case unset:
1.1.1.1.2.9 misho 259: ret = rpc_srv_unregisterBLOB(s, blob->hdr_var);
1.1.1.1.2.6 misho 260: if (ret == -1)
261: ret = -7;
1.1.1.1.2.5 misho 262: break;
263: default:
264: rpc_SetErr(EINVAL, "Error:: unsupported BLOB command (%d)...\n",
265: blob->hdr_cmd);
1.1.1.1.2.7 misho 266: ret = -7;
1.1.1.1.2.5 misho 267: }
1.1.1.1.2.15! misho 268: cx++;
1.1.1.1.2.6 misho 269:
1.1.1.1.2.12 misho 270: makeReply:
1.1.1.1.2.6 misho 271: // Replay to client!
1.1.1.1.2.7 misho 272: blob->hdr_cmd = ret < 0 ? error : ok;
273: blob->hdr_seq = ret;
274: if ((ret = send(c->cli_sock, buf, sizeof buf, 0)) == -1) {
275: LOGERR;
276: ret = -8;
277: break;
278: }
279: if (ret != sizeof buf) {
280: rpc_SetErr(EBADMSG, "Error:: in send BLOB reply, should be send %d bytes, "
281: "really is %d\n", sizeof buf, ret);
282: ret = -9;
283: break;
284: }
1.1.1.1.2.15! misho 285: cx = -1;
! 286:
1.1.1.1.2.5 misho 287: } while (ret > -1);
1.1.1.1.2.4 misho 288:
1.1.1.1.2.5 misho 289: shutdown(c->cli_sock, SHUT_RDWR);
290: close(c->cli_sock);
291: memset(c, 0, sizeof(rpc_cli_t));
1.1.1.1.2.4 misho 292: return (void*) ret;
293: }
294:
1.1 misho 295: // -------------------------------------------------
296:
297: /*
1.1.1.1.2.4 misho 298: * rpc_srv_initBLOBServer() Init & create BLOB Server
299: * @Port = Port for bind server, if Port == 0 default port is selected
1.1.1.1.2.5 misho 300: * @diskDir = Disk place for BLOB file objects
1.1.1.1.2.4 misho 301: * return: -1 == error or 0 bind and created BLOB server instance
302: */
303: int
1.1.1.1.2.5 misho 304: rpc_srv_initBLOBServer(rpc_srv_t * __restrict srv, u_short Port, const char *diskDir)
1.1.1.1.2.4 misho 305: {
306: int n = 1;
307: struct sockaddr_in sin;
308: struct sockaddr_in6 sin6;
309:
310: if (!srv) {
311: rpc_SetErr(EINVAL, "Error:: Invalid parameters can`t init BLOB server ...\n");
312: return -1;
313: }
314: if (srv->srv_blob.state) {
315: rpc_SetErr(EPERM, "Warning:: Already started BLOB server!\n");
316: return 0;
317: }
318: if (!Port)
319: Port = RPC_DEFPORT + 1;
320:
321: memset(&srv->srv_blob, 0, sizeof srv->srv_blob);
1.1.1.1.2.5 misho 322: if (access(diskDir, R_OK | W_OK) == -1) {
323: LOGERR;
324: return -1;
325: } else
326: strlcpy(srv->srv_blob.dir, diskDir, UCHAR_MAX + 1);
327:
1.1.1.1.2.4 misho 328: srv->srv_blob.server.cli_tid = pthread_self();
329: srv->srv_blob.server.cli_parent = srv;
330: if (srv->srv_server.cli_sa.sa_family == AF_INET) {
331: memcpy(&sin, &srv->srv_server.cli_sa, sizeof sin);
332: sin.sin_port = htons(Port);
333: memcpy(&srv->srv_blob.server.cli_sa, &sin, sizeof(struct sockaddr));
334: } else {
335: memcpy(&sin6, &srv->srv_server.cli_sa, sizeof sin6);
336: sin6.sin6_port = htons(Port);
337: memcpy(&srv->srv_blob.server.cli_sa, &sin6, sizeof(struct sockaddr));
338: }
339:
340: srv->srv_blob.server.cli_sock = socket(srv->srv_server.cli_sa.sa_family, SOCK_STREAM, 0);
341: if (srv->srv_blob.server.cli_sock == -1) {
342: LOGERR;
343: return -1;
344: }
345: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
346: LOGERR;
347: close(srv->srv_blob.server.cli_sock);
348: return -1;
349: }
350: if (bind(srv->srv_blob.server.cli_sock, &srv->srv_blob.server.cli_sa,
351: sizeof srv->srv_blob.server.cli_sa) == -1) {
352: LOGERR;
353: close(srv->srv_blob.server.cli_sock);
354: return -1;
355: }
356:
357: srv->srv_blob.clients = calloc(srv->srv_numcli, sizeof(rpc_cli_t));
358: if (!srv->srv_blob.clients) {
359: LOGERR;
360: close(srv->srv_blob.server.cli_sock);
361: return -1;
362: } else
363: memset(srv->srv_blob.clients, 0, srv->srv_numcli * sizeof(rpc_cli_t));
364:
365: pthread_mutex_init(&srv->srv_blob.mtx, NULL);
366:
367: pthread_mutex_lock(&srv->srv_mtx);
368: rpc_srv_registerCall(srv, NULL, CALL_BLOBSHUTDOWN, 0);
369: rpc_srv_registerCall(srv, NULL, CALL_BLOBCLIENTS, 0);
370: rpc_srv_registerCall(srv, NULL, CALL_BLOBVARS, 0);
1.1.1.1.2.13 misho 371: rpc_srv_registerCall(srv, NULL, CALL_BLOBSTATE, 1);
1.1.1.1.2.4 misho 372: pthread_mutex_unlock(&srv->srv_mtx);
373:
1.1.1.1.2.5 misho 374: srv->srv_blob.state = enable; // enable BLOB
1.1.1.1.2.4 misho 375: return 0;
376: }
377:
378: /*
379: * rpc_srv_endBLOBServer() Destroy BLOB server, close all opened sockets and free resources
380: * @srv = RPC Server instance
381: * return: none
382: */
383: void
384: rpc_srv_endBLOBServer(rpc_srv_t * __restrict srv)
385: {
386: rpc_cli_t *c;
387: register int i;
388: rpc_blob_t *f;
389:
390: if (!srv) {
391: rpc_SetErr(EINVAL, "Error:: Can`t destroy server because parameter is null!\n");
392: return;
393: } else
1.1.1.1.2.5 misho 394: srv->srv_blob.state = disable;
1.1.1.1.2.4 misho 395:
1.1.1.1.2.10 misho 396: rpc_srv_unregisterCall(srv, NULL, CALL_BLOBSHUTDOWN);
397: rpc_srv_unregisterCall(srv, NULL, CALL_BLOBCLIENTS);
398: rpc_srv_unregisterCall(srv, NULL, CALL_BLOBVARS);
1.1.1.1.2.13 misho 399: rpc_srv_unregisterCall(srv, NULL, CALL_BLOBSTATE);
1.1.1.1.2.10 misho 400:
1.1.1.1.2.4 misho 401: for (i = 0, c = srv->srv_blob.clients; i < srv->srv_numcli && c; i++, c++)
402: if (c->cli_sa.sa_family)
403: shutdown(c->cli_sock, SHUT_RDWR);
404: close(srv->srv_blob.server.cli_sock);
405:
1.1.1.1.2.13 misho 406: if (srv->srv_blob.clients) {
1.1.1.1.2.4 misho 407: free(srv->srv_blob.clients);
1.1.1.1.2.13 misho 408: srv->srv_blob.clients = NULL;
409: }
1.1.1.1.2.4 misho 410:
411: pthread_mutex_lock(&srv->srv_blob.mtx);
412: while ((f = srv->srv_blob.blobs)) {
413: srv->srv_blob.blobs = f->blob_next;
1.1.1.1.2.14 misho 414: rpc_srv_blobFree(srv, f);
1.1.1.1.2.4 misho 415: free(f);
416: }
417: pthread_mutex_unlock(&srv->srv_blob.mtx);
418:
419: pthread_mutex_destroy(&srv->srv_blob.mtx);
420: }
421:
422: /*
423: * rpc_srv_execBLOBServer() Execute Main BLOB server loop and wait for clients requests
424: * @srv = RPC Server instance
425: * return: -1 error or 0 ok, infinite loop ...
426: */
427: int
428: rpc_srv_execBLOBServer(rpc_srv_t * __restrict srv)
429: {
430: socklen_t salen = sizeof(struct sockaddr);
431: register int i;
432: rpc_cli_t *c;
433: fd_set fds;
434: int ret;
435: struct timeval tv = { DEF_RPC_TIMEOUT, 0 };
436:
1.1.1.1.2.12 misho 437: if (!srv || srv->srv_blob.state == disable) {
1.1.1.1.2.4 misho 438: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t start BLOB server ...\n");
439: return -1;
440: }
441:
442: if (listen(srv->srv_blob.server.cli_sock, SOMAXCONN) == -1) {
443: LOGERR;
444: return -1;
445: }
446:
1.1.1.1.2.11 misho 447: while (!blob_Kill && !rpc_Kill) {
1.1.1.1.2.4 misho 448: for (c = srv->srv_blob.clients, i = 0; i < srv->srv_numcli && c; i++, c++)
449: if (!c->cli_sa.sa_family)
450: break;
1.1.1.1.2.13 misho 451: if (i >= srv->srv_numcli) {
1.1.1.1.2.4 misho 452: usleep(1000000);
453: continue;
454: }
455:
456: FD_ZERO(&fds);
457: FD_SET(srv->srv_blob.server.cli_sock, &fds);
458: ret = select(srv->srv_blob.server.cli_sock + 1, &fds, NULL, NULL, &tv);
459: if (ret == -1) {
460: LOGERR;
461: ret = 1;
462: break;
463: }
464: if (!ret)
465: continue;
466:
467: c->cli_sock = accept(srv->srv_blob.server.cli_sock, &c->cli_sa, &salen);
468: if (c->cli_sock == -1) {
469: LOGERR;
470: continue;
471: } else
472: c->cli_parent = srv;
473:
474: if (pthread_create(&c->cli_tid, NULL, rpc_srv_dispatchVars, c)) {
475: LOGERR;
476: continue;
477: }
478: }
479:
1.1.1.1.2.11 misho 480: srv->srv_blob.state = disable;
481:
1.1.1.1.2.4 misho 482: return 0;
483: }
484:
485:
486: /*
1.1 misho 487: * rpc_srv_initServer() Init & create RPC Server
488: * @regProgID = ProgramID for authentication & recognition
489: * @regProcID = ProcessID for authentication & recognition
490: * @concurentClients = Concurent clients at same time to this server
491: * @family = Family socket type, AF_INET or AF_INET6
492: * @csHost = Host name or IP address for bind server, if NULL any address
493: * @Port = Port for bind server, if Port == 0 default port is selected
494: * return: NULL == error or !=NULL bind and created RPC server instance
495: */
496: rpc_srv_t *
497: rpc_srv_initServer(u_int regProgID, u_int regProcID, int concurentClients,
498: u_short family, const char *csHost, u_short Port)
499: {
500: rpc_srv_t *srv = NULL;
501: int n = 1;
502: struct hostent *host = NULL;
503: struct sockaddr_in sin;
504: struct sockaddr_in6 sin6;
505:
506: if (!concurentClients || !regProgID || (family != AF_INET && family != AF_INET6)) {
507: rpc_SetErr(EINVAL, "Error:: Invalid parameters can`t init RPC server ...\n");
508: return NULL;
509: }
510: if (!Port)
511: Port = RPC_DEFPORT;
512: if (csHost) {
513: host = gethostbyname2(csHost, family);
514: if (!host) {
515: rpc_SetErr(h_errno, "Error:: %s\n", hstrerror(h_errno));
516: return NULL;
517: }
518: }
519: switch (family) {
520: case AF_INET:
521: memset(&sin, 0, sizeof sin);
522: sin.sin_len = sizeof sin;
523: sin.sin_family = family;
524: sin.sin_port = htons(Port);
525: if (csHost)
526: memcpy(&sin.sin_addr, host->h_addr, host->h_length);
527: break;
528: case AF_INET6:
529: memset(&sin6, 0, sizeof sin6);
530: sin6.sin6_len = sizeof sin6;
531: sin6.sin6_family = family;
532: sin6.sin6_port = htons(Port);
533: if (csHost)
534: memcpy(&sin6.sin6_addr, host->h_addr, host->h_length);
535: break;
536: default:
537: rpc_SetErr(EINVAL, "Error:: Invalid parameters can`t start RPC server ...\n");
538: return NULL;
539: }
540:
541: srv = malloc(sizeof(rpc_srv_t));
542: if (!srv) {
543: LOGERR;
544: return NULL;
545: } else
546: memset(srv, 0, sizeof(rpc_srv_t));
547:
548: srv->srv_numcli = concurentClients;
549: srv->srv_session.sess_version = RPC_VERSION;
550: srv->srv_session.sess_program = regProgID;
551: srv->srv_session.sess_process = regProcID;
552:
1.1.1.1.2.2 misho 553: srv->srv_server.cli_tid = pthread_self();
1.1 misho 554: srv->srv_server.cli_parent = srv;
555: if (family == AF_INET)
556: memcpy(&srv->srv_server.cli_sa, &sin, sizeof srv->srv_server.cli_sa);
557: else
558: memcpy(&srv->srv_server.cli_sa, &sin6, sizeof srv->srv_server.cli_sa);
559: srv->srv_server.cli_sock = socket(family, SOCK_STREAM, 0);
560: if (srv->srv_server.cli_sock == -1) {
561: LOGERR;
562: free(srv);
563: return NULL;
564: }
565: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
566: LOGERR;
567: close(srv->srv_server.cli_sock);
568: free(srv);
569: return NULL;
570: }
571: if (bind(srv->srv_server.cli_sock, &srv->srv_server.cli_sa, sizeof srv->srv_server.cli_sa) == -1) {
572: LOGERR;
573: close(srv->srv_server.cli_sock);
574: free(srv);
575: return NULL;
576: }
577:
578: srv->srv_clients = calloc(srv->srv_numcli, sizeof(rpc_cli_t));
579: if (!srv->srv_clients) {
580: LOGERR;
581: close(srv->srv_server.cli_sock);
582: free(srv);
583: return NULL;
584: } else
585: memset(srv->srv_clients, 0, srv->srv_numcli * sizeof(rpc_cli_t));
586:
1.1.1.1.2.1 misho 587: rpc_srv_registerCall(srv, NULL, CALL_SRVSHUTDOWN, 0);
1.1 misho 588: rpc_srv_registerCall(srv, NULL, CALL_SRVCLIENTS, 0);
589: rpc_srv_registerCall(srv, NULL, CALL_SRVCALLS, 0);
590: rpc_srv_registerCall(srv, NULL, CALL_SRVSESSIONS, 0);
1.1.1.1.2.1 misho 591:
1.1.1.1.2.4 misho 592: pthread_mutex_init(&srv->srv_mtx, NULL);
1.1 misho 593: return srv;
594: }
595:
596: /*
597: * rpc_srv_endServer() Destroy RPC server, close all opened sockets and free resources
598: * @srv = RPC Server instance
599: * return: none
600: */
601: void
602: rpc_srv_endServer(rpc_srv_t * __restrict srv)
603: {
604: rpc_cli_t *c;
605: register int i;
606: rpc_func_t *f;
607:
608: if (!srv) {
609: rpc_SetErr(EINVAL, "Error:: Can`t destroy server because parameter is null!\n");
610: return;
611: }
612:
1.1.1.1.2.4 misho 613: rpc_srv_endBLOBServer(srv);
1.1 misho 614:
615: for (i = 0, c = srv->srv_clients; i < srv->srv_numcli && c; i++, c++)
616: if (c->cli_sa.sa_family)
617: shutdown(c->cli_sock, SHUT_RDWR);
618: close(srv->srv_server.cli_sock);
619:
620: if (srv->srv_clients) {
621: free(srv->srv_clients);
1.1.1.1.2.13 misho 622: srv->srv_clients = NULL;
1.1 misho 623: srv->srv_numcli = 0;
624: }
625:
1.1.1.1.2.4 misho 626: pthread_mutex_lock(&srv->srv_mtx);
627: while ((f = srv->srv_funcs)) {
628: srv->srv_funcs = f->func_next;
629: free(f);
630: }
631: pthread_mutex_unlock(&srv->srv_mtx);
632:
633: pthread_mutex_destroy(&srv->srv_mtx);
634:
1.1 misho 635: free(srv);
636: srv = NULL;
637: }
638:
639: /*
640: * rpc_srv_execServer() Execute Main server loop and wait for clients requests
641: * @srv = RPC Server instance
642: * return: -1 error or 0 ok, infinite loop ...
643: */
644: int
645: rpc_srv_execServer(rpc_srv_t * __restrict srv)
646: {
647: socklen_t salen = sizeof(struct sockaddr);
648: register int i;
649: rpc_cli_t *c;
1.1.1.1.2.2 misho 650: fd_set fds;
651: int ret;
652: struct timeval tv = { DEF_RPC_TIMEOUT, 0 };
1.1 misho 653:
654: if (!srv) {
655: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t start RPC server ...\n");
656: return -1;
657: }
658:
659: if (listen(srv->srv_server.cli_sock, SOMAXCONN) == -1) {
660: LOGERR;
661: return -1;
662: }
663:
1.1.1.1.2.1 misho 664: while (!rpc_Kill) {
1.1 misho 665: for (c = srv->srv_clients, i = 0; i < srv->srv_numcli && c; i++, c++)
666: if (!c->cli_sa.sa_family)
667: break;
1.1.1.1.2.13 misho 668: if (i >= srv->srv_numcli) {
1.1 misho 669: usleep(1000000);
670: continue;
671: }
1.1.1.1.2.2 misho 672:
673: FD_ZERO(&fds);
674: FD_SET(srv->srv_server.cli_sock, &fds);
675: ret = select(srv->srv_server.cli_sock + 1, &fds, NULL, NULL, &tv);
676: if (ret == -1) {
677: LOGERR;
678: ret = 1;
679: break;
680: }
681: if (!ret)
682: continue;
683:
1.1 misho 684: c->cli_sock = accept(srv->srv_server.cli_sock, &c->cli_sa, &salen);
685: if (c->cli_sock == -1) {
686: LOGERR;
687: continue;
688: } else
689: c->cli_parent = srv;
690:
691: if (pthread_create(&c->cli_tid, NULL, rpc_srv_dispatchCall, c)) {
692: LOGERR;
693: continue;
694: }
695: }
696:
697: return 0;
698: }
699:
700: // ---------------------------------------------------------
701:
702: /*
703: * rpc_srv_execCall() Execute registered call from RPC server
704: * @call = Register RPC call
705: * @rpc = IN RPC call structure
706: * @args = IN RPC call array of rpc values
707: * return: -1 error, !=-1 ok
708: */
709: int
1.1.1.1.2.11 misho 710: rpc_srv_execCall(rpc_func_t * __restrict call, struct tagRPCCall * __restrict rpc,
711: rpc_val_t * __restrict args)
1.1 misho 712: {
713: void *dl;
714: rpc_callback_t func;
715: int ret;
716:
1.1.1.1.2.11 misho 717: if (!call || !rpc || !call->func_parent) {
1.1 misho 718: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t exec call from RPC server ...\n");
719: return -1;
720: }
721:
722: dl = dlopen((char*) (*call->func_file ? call->func_file : NULL), RTLD_NOW);
723: if (!dl) {
724: rpc_SetErr(ENOENT, "Error:: Can`t attach module %s!\n", dlerror());
725: return -1;
726: }
727:
728: func = dlsym(dl, (char*) call->func_name);
729: if (func)
1.1.1.1.2.11 misho 730: ret = func(call, rpc->call_argc, args);
1.1 misho 731: else {
732: rpc_SetErr(ENOEXEC, "Error:: Can`t find function %s!\n", dlerror());
733: ret = -1;
734: }
735:
736: dlclose(dl);
737: return ret;
738: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>