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