Annotation of libaitrpc/src/srv.c, revision 1.1.1.1.2.20
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.20! misho 6: * $Id: srv.c,v 1.1.1.1.2.19 2010/07/08 08:02:02 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;
1.1.1.1.2.19 misho 237: } else
238: blob->hdr_len = b->blob_len;
1.1.1.1.2.9 misho 239:
1.1.1.1.2.6 misho 240: if (rpc_srv_blobMap(s, b) != -1) {
241: ret = rpc_srv_sendBLOB(c, b);
242: rpc_srv_blobUnmap(b);
243: } else
244: ret = -7;
1.1.1.1.2.5 misho 245: break;
246: case set:
1.1.1.1.2.9 misho 247: if ((b = rpc_srv_registerBLOB(s, blob->hdr_len))) {
248: // set new BLOB variable for reply :)
249: blob->hdr_var = b->blob_var;
1.1.1.1.2.8 misho 250:
1.1.1.1.2.9 misho 251: ret = rpc_srv_recvBLOB(c, b);
252: rpc_srv_blobUnmap(b);
253: } else
1.1.1.1.2.6 misho 254: ret = -7;
1.1.1.1.2.5 misho 255: break;
256: case unset:
1.1.1.1.2.9 misho 257: ret = rpc_srv_unregisterBLOB(s, blob->hdr_var);
1.1.1.1.2.6 misho 258: if (ret == -1)
259: ret = -7;
1.1.1.1.2.5 misho 260: break;
261: default:
262: rpc_SetErr(EINVAL, "Error:: unsupported BLOB command (%d)...\n",
263: blob->hdr_cmd);
1.1.1.1.2.7 misho 264: ret = -7;
1.1.1.1.2.5 misho 265: }
1.1.1.1.2.6 misho 266:
1.1.1.1.2.12 misho 267: makeReply:
1.1.1.1.2.6 misho 268: // Replay to client!
1.1.1.1.2.7 misho 269: blob->hdr_cmd = ret < 0 ? error : ok;
1.1.1.1.2.18 misho 270: blob->hdr_ret = ret;
1.1.1.1.2.7 misho 271: if ((ret = send(c->cli_sock, buf, sizeof buf, 0)) == -1) {
272: LOGERR;
273: ret = -8;
274: break;
275: }
276: if (ret != sizeof buf) {
277: rpc_SetErr(EBADMSG, "Error:: in send BLOB reply, should be send %d bytes, "
278: "really is %d\n", sizeof buf, ret);
279: ret = -9;
280: break;
281: }
1.1.1.1.2.5 misho 282: } while (ret > -1);
1.1.1.1.2.4 misho 283:
1.1.1.1.2.5 misho 284: shutdown(c->cli_sock, SHUT_RDWR);
285: close(c->cli_sock);
286: memset(c, 0, sizeof(rpc_cli_t));
1.1.1.1.2.4 misho 287: return (void*) ret;
288: }
289:
1.1 misho 290: // -------------------------------------------------
291:
292: /*
1.1.1.1.2.4 misho 293: * rpc_srv_initBLOBServer() Init & create BLOB Server
294: * @Port = Port for bind server, if Port == 0 default port is selected
1.1.1.1.2.5 misho 295: * @diskDir = Disk place for BLOB file objects
1.1.1.1.2.4 misho 296: * return: -1 == error or 0 bind and created BLOB server instance
297: */
298: int
1.1.1.1.2.5 misho 299: rpc_srv_initBLOBServer(rpc_srv_t * __restrict srv, u_short Port, const char *diskDir)
1.1.1.1.2.4 misho 300: {
301: int n = 1;
302: struct sockaddr_in sin;
303: struct sockaddr_in6 sin6;
304:
305: if (!srv) {
306: rpc_SetErr(EINVAL, "Error:: Invalid parameters can`t init BLOB server ...\n");
307: return -1;
308: }
309: if (srv->srv_blob.state) {
310: rpc_SetErr(EPERM, "Warning:: Already started BLOB server!\n");
311: return 0;
312: }
313: if (!Port)
314: Port = RPC_DEFPORT + 1;
315:
316: memset(&srv->srv_blob, 0, sizeof srv->srv_blob);
1.1.1.1.2.5 misho 317: if (access(diskDir, R_OK | W_OK) == -1) {
318: LOGERR;
319: return -1;
320: } else
321: strlcpy(srv->srv_blob.dir, diskDir, UCHAR_MAX + 1);
322:
1.1.1.1.2.4 misho 323: srv->srv_blob.server.cli_tid = pthread_self();
324: srv->srv_blob.server.cli_parent = srv;
325: if (srv->srv_server.cli_sa.sa_family == AF_INET) {
326: memcpy(&sin, &srv->srv_server.cli_sa, sizeof sin);
327: sin.sin_port = htons(Port);
328: memcpy(&srv->srv_blob.server.cli_sa, &sin, sizeof(struct sockaddr));
329: } else {
330: memcpy(&sin6, &srv->srv_server.cli_sa, sizeof sin6);
331: sin6.sin6_port = htons(Port);
332: memcpy(&srv->srv_blob.server.cli_sa, &sin6, sizeof(struct sockaddr));
333: }
334:
335: srv->srv_blob.server.cli_sock = socket(srv->srv_server.cli_sa.sa_family, SOCK_STREAM, 0);
336: if (srv->srv_blob.server.cli_sock == -1) {
337: LOGERR;
338: return -1;
339: }
340: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
341: LOGERR;
342: close(srv->srv_blob.server.cli_sock);
343: return -1;
344: }
345: if (bind(srv->srv_blob.server.cli_sock, &srv->srv_blob.server.cli_sa,
346: sizeof srv->srv_blob.server.cli_sa) == -1) {
347: LOGERR;
348: close(srv->srv_blob.server.cli_sock);
349: return -1;
350: }
351:
352: srv->srv_blob.clients = calloc(srv->srv_numcli, sizeof(rpc_cli_t));
353: if (!srv->srv_blob.clients) {
354: LOGERR;
355: close(srv->srv_blob.server.cli_sock);
356: return -1;
357: } else
358: memset(srv->srv_blob.clients, 0, srv->srv_numcli * sizeof(rpc_cli_t));
359:
360: pthread_mutex_init(&srv->srv_blob.mtx, NULL);
361:
362: pthread_mutex_lock(&srv->srv_mtx);
363: rpc_srv_registerCall(srv, NULL, CALL_BLOBSHUTDOWN, 0);
364: rpc_srv_registerCall(srv, NULL, CALL_BLOBCLIENTS, 0);
365: rpc_srv_registerCall(srv, NULL, CALL_BLOBVARS, 0);
1.1.1.1.2.13 misho 366: rpc_srv_registerCall(srv, NULL, CALL_BLOBSTATE, 1);
1.1.1.1.2.4 misho 367: pthread_mutex_unlock(&srv->srv_mtx);
368:
1.1.1.1.2.5 misho 369: srv->srv_blob.state = enable; // enable BLOB
1.1.1.1.2.4 misho 370: return 0;
371: }
372:
373: /*
374: * rpc_srv_endBLOBServer() Destroy BLOB server, close all opened sockets and free resources
375: * @srv = RPC Server instance
376: * return: none
377: */
378: void
379: rpc_srv_endBLOBServer(rpc_srv_t * __restrict srv)
380: {
381: rpc_cli_t *c;
382: register int i;
383: rpc_blob_t *f;
384:
385: if (!srv) {
386: rpc_SetErr(EINVAL, "Error:: Can`t destroy server because parameter is null!\n");
387: return;
388: } else
1.1.1.1.2.5 misho 389: srv->srv_blob.state = disable;
1.1.1.1.2.4 misho 390:
1.1.1.1.2.10 misho 391: rpc_srv_unregisterCall(srv, NULL, CALL_BLOBSHUTDOWN);
392: rpc_srv_unregisterCall(srv, NULL, CALL_BLOBCLIENTS);
393: rpc_srv_unregisterCall(srv, NULL, CALL_BLOBVARS);
1.1.1.1.2.13 misho 394: rpc_srv_unregisterCall(srv, NULL, CALL_BLOBSTATE);
1.1.1.1.2.10 misho 395:
1.1.1.1.2.4 misho 396: for (i = 0, c = srv->srv_blob.clients; i < srv->srv_numcli && c; i++, c++)
397: if (c->cli_sa.sa_family)
398: shutdown(c->cli_sock, SHUT_RDWR);
399: close(srv->srv_blob.server.cli_sock);
400:
1.1.1.1.2.13 misho 401: if (srv->srv_blob.clients) {
1.1.1.1.2.4 misho 402: free(srv->srv_blob.clients);
1.1.1.1.2.13 misho 403: srv->srv_blob.clients = NULL;
404: }
1.1.1.1.2.4 misho 405:
406: pthread_mutex_lock(&srv->srv_blob.mtx);
407: while ((f = srv->srv_blob.blobs)) {
408: srv->srv_blob.blobs = f->blob_next;
1.1.1.1.2.14 misho 409: rpc_srv_blobFree(srv, f);
1.1.1.1.2.4 misho 410: free(f);
411: }
412: pthread_mutex_unlock(&srv->srv_blob.mtx);
413:
1.1.1.1.2.20! misho 414: while (pthread_mutex_trylock(&srv->srv_blob.mtx) == EBUSY);
1.1.1.1.2.4 misho 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:
1.1.1.1.2.20! misho 629: while (pthread_mutex_trylock(&srv->srv_mtx) == EBUSY);
1.1.1.1.2.4 misho 630: pthread_mutex_destroy(&srv->srv_mtx);
631:
1.1 misho 632: free(srv);
633: srv = NULL;
634: }
635:
636: /*
637: * rpc_srv_execServer() Execute Main server loop and wait for clients requests
638: * @srv = RPC Server instance
639: * return: -1 error or 0 ok, infinite loop ...
640: */
641: int
642: rpc_srv_execServer(rpc_srv_t * __restrict srv)
643: {
644: socklen_t salen = sizeof(struct sockaddr);
645: register int i;
646: rpc_cli_t *c;
1.1.1.1.2.2 misho 647: fd_set fds;
648: int ret;
649: struct timeval tv = { DEF_RPC_TIMEOUT, 0 };
1.1 misho 650:
651: if (!srv) {
652: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t start RPC server ...\n");
653: return -1;
654: }
655:
656: if (listen(srv->srv_server.cli_sock, SOMAXCONN) == -1) {
657: LOGERR;
658: return -1;
659: }
660:
1.1.1.1.2.1 misho 661: while (!rpc_Kill) {
1.1 misho 662: for (c = srv->srv_clients, i = 0; i < srv->srv_numcli && c; i++, c++)
663: if (!c->cli_sa.sa_family)
664: break;
1.1.1.1.2.13 misho 665: if (i >= srv->srv_numcli) {
1.1 misho 666: usleep(1000000);
667: continue;
668: }
1.1.1.1.2.2 misho 669:
670: FD_ZERO(&fds);
671: FD_SET(srv->srv_server.cli_sock, &fds);
672: ret = select(srv->srv_server.cli_sock + 1, &fds, NULL, NULL, &tv);
673: if (ret == -1) {
674: LOGERR;
675: ret = 1;
676: break;
677: }
678: if (!ret)
679: continue;
680:
1.1 misho 681: c->cli_sock = accept(srv->srv_server.cli_sock, &c->cli_sa, &salen);
682: if (c->cli_sock == -1) {
683: LOGERR;
684: continue;
685: } else
686: c->cli_parent = srv;
687:
688: if (pthread_create(&c->cli_tid, NULL, rpc_srv_dispatchCall, c)) {
689: LOGERR;
690: continue;
691: }
692: }
693:
694: return 0;
695: }
696:
697: // ---------------------------------------------------------
698:
699: /*
700: * rpc_srv_execCall() Execute registered call from RPC server
701: * @call = Register RPC call
702: * @rpc = IN RPC call structure
703: * @args = IN RPC call array of rpc values
704: * return: -1 error, !=-1 ok
705: */
706: int
1.1.1.1.2.11 misho 707: rpc_srv_execCall(rpc_func_t * __restrict call, struct tagRPCCall * __restrict rpc,
708: rpc_val_t * __restrict args)
1.1 misho 709: {
710: void *dl;
711: rpc_callback_t func;
712: int ret;
713:
1.1.1.1.2.11 misho 714: if (!call || !rpc || !call->func_parent) {
1.1 misho 715: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t exec call from RPC server ...\n");
716: return -1;
717: }
718:
719: dl = dlopen((char*) (*call->func_file ? call->func_file : NULL), RTLD_NOW);
720: if (!dl) {
721: rpc_SetErr(ENOENT, "Error:: Can`t attach module %s!\n", dlerror());
722: return -1;
723: }
724:
725: func = dlsym(dl, (char*) call->func_name);
726: if (func)
1.1.1.1.2.11 misho 727: ret = func(call, rpc->call_argc, args);
1.1 misho 728: else {
729: rpc_SetErr(ENOEXEC, "Error:: Can`t find function %s!\n", dlerror());
730: ret = -1;
731: }
732:
733: dlclose(dl);
734: return ret;
735: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>