Annotation of libaitrpc/src/srv.c, revision 1.1.1.1.2.19
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.19! misho 6: * $Id: srv.c,v 1.1.1.1.2.18 2010/07/08 07:32:53 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:
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>