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