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