Annotation of libaitrpc/src/srv.c, revision 1.8.2.6
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.8.2.6 ! misho 6: * $Id: srv.c,v 1.8.2.5 2012/05/11 12:53:35 misho Exp $
1.1 misho 7: *
1.2 misho 8: **************************************************************************
9: The ELWIX and AITNET software is distributed under the following
10: terms:
11:
12: All of the documentation and software included in the ELWIX and AITNET
13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
14:
1.7 misho 15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
1.2 misho 16: by Michael Pounov <misho@elwix.org>. All rights reserved.
17:
18: Redistribution and use in source and binary forms, with or without
19: modification, are permitted provided that the following conditions
20: are met:
21: 1. Redistributions of source code must retain the above copyright
22: notice, this list of conditions and the following disclaimer.
23: 2. Redistributions in binary form must reproduce the above copyright
24: notice, this list of conditions and the following disclaimer in the
25: documentation and/or other materials provided with the distribution.
26: 3. All advertising materials mentioning features or use of this software
27: must display the following acknowledgement:
28: This product includes software developed by Michael Pounov <misho@elwix.org>
29: ELWIX - Embedded LightWeight unIX and its contributors.
30: 4. Neither the name of AITNET nor the names of its contributors
31: may be used to endorse or promote products derived from this software
32: without specific prior written permission.
33:
34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37: ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44: SUCH DAMAGE.
45: */
1.1 misho 46: #include "global.h"
47:
48:
1.7 misho 49: static void *rxPacket(sched_task_t*);
50: static void *rxBLOB(sched_task_t*);
51:
52: static void *
53: txPacket(sched_task_t *task)
54: {
55: rpc_cli_t *c = TASK_ARG(task);
56: rpc_srv_t *s = c->cli_parent;
57: rpc_func_t *f = NULL;
58: u_char *buf = TASK_DATA(task);
59: struct tagRPCCall *rpc = (struct tagRPCCall*) buf;
60: int ret, wlen = sizeof(struct tagRPCCall);
61: array_t *arr = NULL;
62:
63: if (rpc->call_argc) {
64: f = rpc_srv_getCall(s, ntohs(rpc->call_tag), ntohl(rpc->call_hash));
65: if (!f) {
66: rpc->call_argc ^= rpc->call_argc;
67: rpc->call_rep.ret = RPC_ERROR(-1);
68: rpc->call_rep.eno = RPC_ERROR(rpc_Errno);
69: } else {
70: rpc->call_argc = htons(rpc_srv_getVars(f, &arr));
71: /* Go Encapsulate variables */
72: ret = io_vars2buffer(buf + wlen, TASK_DATLEN(task) - wlen, arr);
73: io_clrVars(f->func_vars);
74: if (ret == -1) {
75: rpc_SetErr(EBADRPC, "Prepare RPC packet failed");
76: rpc->call_argc ^= rpc->call_argc;
77: rpc->call_rep.ret = RPC_ERROR(-1);
78: rpc->call_rep.eno = RPC_ERROR(rpc_Errno);
79: } else
80: wlen += ret;
81: }
82: }
83:
1.8 misho 84: rpc->call_len = htons(wlen);
85:
1.7 misho 86: /* calculate CRC */
87: rpc->call_crc ^= rpc->call_crc;
1.8 misho 88: rpc->call_crc = htons(crcFletcher16((u_short*) buf, wlen / 2));
1.7 misho 89:
90: /* send reply */
91: ret = send(TASK_FD(task), buf, wlen, 0);
92: if (ret == -1)
93: LOGERR;
94: else if (ret != wlen)
95: rpc_SetErr(EPROCUNAVAIL, "RPC reply, should be send %d bytes, "
96: "really sended %d bytes", wlen, ret);
97: else
98: ioDEBUG(RPC_DEBUG_LEVEL, "Sended %d bytes", ret);
99:
100: return NULL;
101: }
102:
103: static void *
104: execCall(sched_task_t *task)
105: {
106: rpc_cli_t *c = TASK_ARG(task);
107: rpc_srv_t *s = c->cli_parent;
108: rpc_func_t *f = NULL;
109: array_t *arr = NULL;
1.8 misho 110: u_char *buf = TASK_DATA(task) + TASK_VAL(task);
1.7 misho 111: struct tagRPCCall *rpc = (struct tagRPCCall*) buf;
112: int argc = ntohs(rpc->call_argc);
113:
114: /* Go decapsulate variables ... */
1.8 misho 115: if (argc) {
1.7 misho 116: arr = io_buffer2vars(buf + sizeof(struct tagRPCCall),
1.8 misho 117: TASK_DATLEN(task) - TASK_VAL(task) - sizeof(struct tagRPCCall),
118: argc, 1);
1.7 misho 119: if (!arr) {
120: rpc_SetErr(ERPCMISMATCH, "#%d - %s", io_GetErrno(), io_GetError());
121: rpc->call_argc ^= rpc->call_argc;
122: rpc->call_rep.ret = RPC_ERROR(-1);
123: rpc->call_rep.eno = RPC_ERROR(rpc_Errno);
124: return NULL;
125: }
126: }
127:
128: if (!(f = rpc_srv_getCall(s, ntohs(rpc->call_tag), ntohl(rpc->call_hash)))) {
129: rpc_SetErr(EPROGUNAVAIL, "Function not found at RPC server");
130: rpc->call_argc ^= rpc->call_argc;
131: rpc->call_rep.ret = RPC_ERROR(-1);
132: rpc->call_rep.eno = RPC_ERROR(rpc_Errno);
133: } else {
134: ioDEBUG(RPC_DEBUG_LEVEL, "RPC function %s from module %s",
135: AIT_GET_STR(&f->func_name), AIT_GET_LIKE(&f->func_file, char*));
136:
1.8 misho 137: /* if client doesn't want reply */
138: argc = rpc->call_req.flags & RPC_NOREPLY;
1.7 misho 139: rpc->call_rep.ret = RPC_ERROR(rpc_srv_execCall(f, rpc, arr));
140: if (rpc->call_rep.ret == htonl(-1)) {
141: rpc->call_rep.eno = RPC_ERROR(errno);
142: rpc->call_argc ^= rpc->call_argc;
143: } else {
144: rpc->call_rep.eno ^= rpc->call_rep.eno;
1.8 misho 145: if (argc) {
146: /* without reply */
147: io_clrVars(f->func_vars);
148: rpc->call_argc ^= rpc->call_argc;
149: } else
150: rpc->call_argc = htons(rpc_srv_getVars(f, NULL));
1.7 misho 151: }
152: }
153:
154: if (arr)
155: io_arrayDestroy(&arr);
156: return NULL;
157: }
158:
159: static void *
160: rxPacket(sched_task_t *task)
161: {
162: rpc_cli_t *c = TASK_ARG(task);
163: rpc_srv_t *s = c->cli_parent;
164: u_char *buf = TASK_DATA(task);
1.8 misho 165: int rlen, noreply;
166: u_short crc, off = 0;
1.7 misho 167: struct tagRPCCall *rpc;
168: struct timespec ts;
169:
170: memset(buf, 0, TASK_DATLEN(task));
171: rlen = recv(TASK_FD(task), buf, TASK_DATLEN(task), 0);
172: if (rlen == -1) {
173: LOGERR;
174: s->srv_kill = s->srv_blob.state = kill;
175: return NULL;
176: } else if (!rlen) { /* receive EOF */
177: s->srv_kill = s->srv_blob.state = kill;
178: return NULL;
179: } else
180: ioDEBUG(RPC_DEBUG_LEVEL, "Readed %d bytes", rlen);
181:
1.8 misho 182: do {
183: if (rlen < sizeof(struct tagRPCCall)) {
184: rpc_SetErr(ERPCMISMATCH, "Too short RPC packet");
185:
186: schedRead(TASK_ROOT(task), rxPacket, TASK_ARG(task), TASK_FD(task),
187: TASK_DATA(task), TASK_DATLEN(task));
188: return NULL;
189: } else
190: rpc = (struct tagRPCCall*) (buf + off);
191:
192: rlen -= ntohs(rpc->call_len);
193:
194: /* check integrity of packet */
195: crc = ntohs(rpc->call_crc);
196: rpc->call_crc ^= rpc->call_crc;
197: if (crc != crcFletcher16((u_short*) (buf + off), ntohs(rpc->call_len) / 2)) {
198: rpc_SetErr(ERPCMISMATCH, "Bad CRC RPC packet");
1.7 misho 199:
1.8 misho 200: off += ntohs(rpc->call_len);
201: if (rlen < 1)
202: break;
203: else
204: continue;
205: }
1.7 misho 206:
1.8 misho 207: noreply = rpc->call_req.flags & RPC_NOREPLY;
1.7 misho 208:
1.8 misho 209: /* check RPC packet session info */
210: if (rpc_chkPktSession(&rpc->call_session, &s->srv_session)) {
211: rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session");
212: rpc->call_argc ^= rpc->call_argc;
213: rpc->call_rep.ret = RPC_ERROR(-1);
214: rpc->call_rep.eno = RPC_ERROR(errno);
215: } else {
216: /* change socket timeout from last packet */
217: ts.tv_sec = rpc->call_session.sess_timeout;
218: ts.tv_nsec = 0;
219: schedPolling(TASK_ROOT(task), &ts, NULL);
220:
221: /* execute RPC call */
222: schedEvent(TASK_ROOT(task), execCall, TASK_ARG(task), off,
223: TASK_DATA(task), TASK_DATLEN(task));
224: }
1.7 misho 225:
1.8 misho 226: /* send RPC reply */
227: if (!noreply)
228: schedWrite(TASK_ROOT(task), txPacket, TASK_ARG(task), TASK_FD(task),
229: buf + off, TASK_DATLEN(task) - off);
1.7 misho 230:
1.8 misho 231: off += ntohs(rpc->call_len);
232: } while (rlen > 0);
1.7 misho 233:
234: /* lets get next packet */
235: schedRead(TASK_ROOT(task), rxPacket, TASK_ARG(task), TASK_FD(task),
236: TASK_DATA(task), TASK_DATLEN(task));
237: return NULL;
238: }
239:
1.1 misho 240: static void *
241: rpc_srv_dispatchCall(void *arg)
242: {
243: rpc_cli_t *c = arg;
244: rpc_srv_t *s;
1.5 misho 245: u_char *buf;
1.7 misho 246: sched_root_task_t *root;
247: struct timespec ts = { DEF_RPC_TIMEOUT, 0 };
248:
1.1 misho 249: if (!arg) {
1.7 misho 250: rpc_SetErr(EINVAL, "Invalid parameter can`t procced RPC client");
1.1 misho 251: return NULL;
252: } else
253: s = c->cli_parent;
254:
1.7 misho 255: /* allocate net buffer */
1.5 misho 256: buf = malloc(s->srv_netbuf);
257: if (!buf) {
258: LOGERR;
259: return NULL;
260: }
261:
1.7 misho 262: root = schedBegin();
263: if (!root) {
264: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
265: free(buf);
266: return NULL;
267: } else {
268: schedTermCondition(root, kill);
269: schedPolling(root, &ts, NULL);
270: }
271:
272: schedRead(root, rxPacket, c, c->cli_sock, buf, s->srv_netbuf);
273:
274: schedRun(root, (void*) &s->srv_kill);
275: schedEnd(&root);
276:
277: shutdown(c->cli_sock, SHUT_RDWR);
278: close(c->cli_sock);
279: memset(c, 0, sizeof(rpc_cli_t));
280: free(buf);
281: return NULL;
282: }
283:
284:
285: static void *
286: txBLOB(sched_task_t *task)
287: {
288: u_char *buf = TASK_DATA(task);
289: struct tagBLOBHdr *blob = (struct tagBLOBHdr *) buf;
290: int wlen = sizeof(struct tagBLOBHdr);
291:
292: /* calculate CRC */
293: blob->hdr_crc ^= blob->hdr_crc;
1.8 misho 294: blob->hdr_crc = htons(crcFletcher16((u_short*) buf, wlen / 2));
1.7 misho 295:
296: /* send reply */
297: wlen = send(TASK_FD(task), buf, wlen, 0);
298: if (wlen == -1)
299: LOGERR;
300: else if (wlen != sizeof(struct tagBLOBHdr))
301: rpc_SetErr(EPROCUNAVAIL, "RPC reply, should be send %d bytes, "
302: "really sended %d bytes", sizeof(struct tagBLOBHdr), wlen);
303: else
304: ioDEBUG(RPC_DEBUG_LEVEL, "Sended %d bytes", wlen);
305:
306: return NULL;
307: }
1.4 misho 308:
1.7 misho 309: static void *
310: rxBLOB(sched_task_t *task)
311: {
312: rpc_cli_t *c = TASK_ARG(task);
313: rpc_srv_t *s = c->cli_parent;
314: rpc_blob_t *b;
315: u_char *buf = TASK_DATA(task);
316: struct tagBLOBHdr *blob = (struct tagBLOBHdr *) buf;
317: int rlen;
318: u_short crc;
319: struct timespec ts;
320:
321: /* check for disable service at this moment? */
1.8 misho 322: if (!s || s->srv_blob.state == disable) {
1.7 misho 323: usleep(100000);
324: #ifdef HAVE_PTHREAD_YIELD
325: pthread_yield();
326: #endif
327: schedRead(TASK_ROOT(task), rxBLOB, TASK_ARG(task), TASK_FD(task),
328: TASK_DATA(task), TASK_DATLEN(task));
329: return NULL;
330: }
1.5 misho 331:
1.7 misho 332: memset(buf, 0, TASK_DATLEN(task));
333: rlen = recv(TASK_FD(task), buf, TASK_DATLEN(task), 0);
334: if (rlen == -1) {
335: LOGERR;
336: s->srv_blob.state = kill;
337: return NULL;
338: } else if (!rlen || s->srv_kill == kill) { /* receive EOF */
339: s->srv_blob.state = kill;
340: return NULL;
341: } else
342: ioDEBUG(RPC_DEBUG_LEVEL, "Readed %d bytes", rlen);
1.6 misho 343:
1.7 misho 344: if (rlen < sizeof(struct tagBLOBHdr)) {
345: rpc_SetErr(ERPCMISMATCH, "Too short BLOB packet");
346: schedRead(TASK_ROOT(task), rxBLOB, TASK_ARG(task), TASK_FD(task),
347: TASK_DATA(task), TASK_DATLEN(task));
348: return NULL;
349: }
1.1 misho 350:
1.7 misho 351: /* check integrity of packet */
352: crc = ntohs(blob->hdr_crc);
353: blob->hdr_crc ^= blob->hdr_crc;
1.8 misho 354: if (crc != crcFletcher16((u_short*) buf, rlen / 2)) {
1.7 misho 355: rpc_SetErr(ERPCMISMATCH, "Bad CRC BLOB packet");
356: schedRead(TASK_ROOT(task), rxBLOB, TASK_ARG(task), TASK_FD(task),
357: TASK_DATA(task), TASK_DATLEN(task));
358: return NULL;
359: }
1.5 misho 360:
1.7 misho 361: /* check RPC packet session info */
362: if ((crc = rpc_chkPktSession(&blob->hdr_session, &s->srv_session))) {
363: rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session");
364: blob->hdr_cmd = error;
365: goto end;
366: } else {
367: /* change socket timeout from last packet */
368: ts.tv_sec = blob->hdr_session.sess_timeout;
369: ts.tv_nsec = 0;
370: schedPolling(TASK_ROOT(task), &ts, NULL);
371: }
372:
373: /* Go to proceed packet ... */
374: switch (blob->hdr_cmd) {
375: case get:
376: if (!(b = rpc_srv_getBLOB(s, ntohl(blob->hdr_var)))) {
377: rpc_SetErr(EINVAL, "Var=%x not found", ntohl(blob->hdr_var));
378: blob->hdr_cmd = no;
379: blob->hdr_ret = RPC_ERROR(-1);
380: break;
381: } else
382: blob->hdr_len = htonl(b->blob_len);
1.5 misho 383:
1.7 misho 384: if (rpc_srv_blobMap(s, b) != -1) {
385: /* deliver BLOB variable to client */
386: blob->hdr_ret = htonl(rpc_srv_sendBLOB(c, b));
387: rpc_srv_blobUnmap(b);
388: } else {
389: blob->hdr_cmd = error;
390: blob->hdr_ret = RPC_ERROR(-1);
391: }
392: break;
393: case set:
394: if ((b = rpc_srv_registerBLOB(s, ntohl(blob->hdr_len)))) {
395: /* set new BLOB variable for reply :) */
396: blob->hdr_var = htonl(b->blob_var);
397:
398: /* receive BLOB from client */
399: blob->hdr_ret = htonl(rpc_srv_recvBLOB(c, b));
400: rpc_srv_blobUnmap(b);
1.5 misho 401: } else {
1.7 misho 402: blob->hdr_cmd = error;
403: blob->hdr_ret = RPC_ERROR(-1);
404: }
405: break;
406: case unset:
407: if (rpc_srv_unregisterBLOB(s, blob->hdr_var) == -1) {
408: blob->hdr_cmd = error;
409: blob->hdr_ret = RPC_ERROR(-1);
1.1 misho 410: }
411: break;
1.7 misho 412: default:
413: rpc_SetErr(EPROCUNAVAIL, "Unsupported BLOB command %d", blob->hdr_cmd);
414: blob->hdr_cmd = error;
415: blob->hdr_ret = RPC_ERROR(-1);
416: }
1.1 misho 417:
1.7 misho 418: end:
419: schedWrite(TASK_ROOT(task), txBLOB, TASK_ARG(task), TASK_FD(task),
420: TASK_DATA(task), TASK_DATLEN(task));
421: schedRead(TASK_ROOT(task), rxBLOB, TASK_ARG(task), TASK_FD(task),
422: TASK_DATA(task), TASK_DATLEN(task));
423: return NULL;
1.2 misho 424: }
425:
426: static void *
427: rpc_srv_dispatchVars(void *arg)
428: {
429: rpc_cli_t *c = arg;
430: rpc_srv_t *s;
1.7 misho 431: sched_root_task_t *root;
432: u_char *buf;
433: struct timespec ts = { DEF_RPC_TIMEOUT, 0 };
434:
1.2 misho 435: if (!arg) {
1.7 misho 436: rpc_SetErr(EINVAL, "Invalid parameter can`t procced BLOB");
1.2 misho 437: return NULL;
438: } else
439: s = c->cli_parent;
440:
1.7 misho 441: /* allocate net buffer */
442: buf = malloc(sizeof(struct tagBLOBHdr));
443: if (!buf) {
444: LOGERR;
445: return NULL;
446: }
1.2 misho 447:
1.7 misho 448: root = schedBegin();
449: if (!root) {
450: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
451: free(buf);
452: return NULL;
453: } else {
454: schedTermCondition(root, kill);
455: schedPolling(root, &ts, NULL);
456: }
1.4 misho 457:
1.7 misho 458: schedRead(root, rxBLOB, c, c->cli_sock, buf, sizeof(struct tagBLOBHdr));
1.2 misho 459:
1.7 misho 460: schedRun(root, (void*) &s->srv_blob.state);
461: schedEnd(&root);
1.2 misho 462:
463: shutdown(c->cli_sock, SHUT_RDWR);
464: close(c->cli_sock);
465: memset(c, 0, sizeof(rpc_cli_t));
1.7 misho 466: free(buf);
467: return NULL;
1.1 misho 468: }
469:
470: // -------------------------------------------------
471:
472: /*
1.7 misho 473: * rpc_srv_initBLOBServer() - Init & create BLOB Server
474: *
1.4 misho 475: * @srv = RPC server instance
1.2 misho 476: * @Port = Port for bind server, if Port == 0 default port is selected
477: * @diskDir = Disk place for BLOB file objects
478: * return: -1 == error or 0 bind and created BLOB server instance
479: */
480: int
481: rpc_srv_initBLOBServer(rpc_srv_t * __restrict srv, u_short Port, const char *diskDir)
482: {
483: int n = 1;
1.6 misho 484: io_sockaddr_t sa;
1.2 misho 485:
486: if (!srv) {
1.7 misho 487: rpc_SetErr(EINVAL, "Invalid parameters can`t init BLOB server");
1.2 misho 488: return -1;
489: }
490: if (srv->srv_blob.state) {
1.7 misho 491: rpc_SetErr(EPERM, "Already started BLOB server!");
1.2 misho 492: return 0;
493: }
494:
495: memset(&srv->srv_blob, 0, sizeof srv->srv_blob);
496: if (access(diskDir, R_OK | W_OK) == -1) {
497: LOGERR;
498: return -1;
499: } else
1.7 misho 500: AIT_SET_STR(&srv->srv_blob.dir, diskDir);
1.2 misho 501:
502: srv->srv_blob.server.cli_tid = pthread_self();
503: srv->srv_blob.server.cli_parent = srv;
1.4 misho 504:
505: memcpy(&sa, &srv->srv_server.cli_sa, sizeof sa);
1.6 misho 506: switch (sa.sa.sa_family) {
1.4 misho 507: case AF_INET:
1.6 misho 508: sa.sin.sin_port = htons(Port ? Port : ntohs(sa.sin.sin_port) + 1);
1.4 misho 509: break;
510: case AF_INET6:
1.6 misho 511: sa.sin6.sin6_port = htons(Port ? Port : ntohs(sa.sin6.sin6_port) + 1);
1.4 misho 512: break;
513: case AF_LOCAL:
1.6 misho 514: strlcat(sa.sun.sun_path, ".blob", sizeof sa.sun.sun_path);
1.4 misho 515: break;
516: default:
1.7 misho 517: AIT_FREE_VAL(&srv->srv_blob.dir);
1.4 misho 518: return -1;
1.2 misho 519: }
1.6 misho 520: memcpy(&srv->srv_blob.server.cli_sa, &sa, sizeof sa);
1.2 misho 521:
1.4 misho 522: /* create BLOB server socket */
1.6 misho 523: srv->srv_blob.server.cli_sock = socket(srv->srv_server.cli_sa.sa.sa_family, SOCK_STREAM, 0);
1.2 misho 524: if (srv->srv_blob.server.cli_sock == -1) {
525: LOGERR;
1.7 misho 526: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 527: return -1;
528: }
529: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
530: LOGERR;
531: close(srv->srv_blob.server.cli_sock);
1.7 misho 532: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 533: return -1;
534: }
1.5 misho 535: n = srv->srv_netbuf;
536: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof n) == -1) {
537: LOGERR;
538: close(srv->srv_blob.server.cli_sock);
1.7 misho 539: AIT_FREE_VAL(&srv->srv_blob.dir);
1.5 misho 540: return -1;
541: }
542: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_RCVBUF, &n, sizeof n) == -1) {
543: LOGERR;
544: close(srv->srv_blob.server.cli_sock);
1.7 misho 545: AIT_FREE_VAL(&srv->srv_blob.dir);
1.5 misho 546: return -1;
547: }
1.6 misho 548: if (bind(srv->srv_blob.server.cli_sock, &srv->srv_blob.server.cli_sa.sa,
549: srv->srv_blob.server.cli_sa.sa.sa_len) == -1) {
1.2 misho 550: LOGERR;
551: close(srv->srv_blob.server.cli_sock);
1.7 misho 552: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 553: return -1;
554: }
555:
1.4 misho 556: /* allocate pool for concurent clients */
1.2 misho 557: srv->srv_blob.clients = calloc(srv->srv_numcli, sizeof(rpc_cli_t));
558: if (!srv->srv_blob.clients) {
559: LOGERR;
560: close(srv->srv_blob.server.cli_sock);
1.7 misho 561: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 562: return -1;
563: } else
564: memset(srv->srv_blob.clients, 0, srv->srv_numcli * sizeof(rpc_cli_t));
565:
566: rpc_srv_registerCall(srv, NULL, CALL_BLOBSHUTDOWN, 0);
1.5 misho 567: rpc_srv_registerCall(srv, NULL, CALL_BLOBCLIENTS, 1);
568: rpc_srv_registerCall(srv, NULL, CALL_BLOBVARS, 1);
569: rpc_srv_registerCall(srv, NULL, CALL_BLOBSTATE, 0);
1.2 misho 570:
1.4 misho 571: srv->srv_blob.state = enable; /* enable BLOB */
1.2 misho 572: return 0;
573: }
574:
575: /*
1.7 misho 576: * rpc_srv_endBLOBServer() - Destroy BLOB server, close all opened sockets and free resources
577: *
1.2 misho 578: * @srv = RPC Server instance
579: * return: none
580: */
581: void
582: rpc_srv_endBLOBServer(rpc_srv_t * __restrict srv)
583: {
584: rpc_cli_t *c;
585: register int i;
586: rpc_blob_t *f;
587:
1.8 misho 588: if (!srv || srv->srv_blob.state == disable)
1.2 misho 589: return;
1.8 misho 590: else
1.4 misho 591: srv->srv_blob.state = kill;
1.2 misho 592:
593: rpc_srv_unregisterCall(srv, NULL, CALL_BLOBSHUTDOWN);
594: rpc_srv_unregisterCall(srv, NULL, CALL_BLOBCLIENTS);
595: rpc_srv_unregisterCall(srv, NULL, CALL_BLOBVARS);
596: rpc_srv_unregisterCall(srv, NULL, CALL_BLOBSTATE);
597:
1.4 misho 598: /* close all clients connections & server socket */
1.2 misho 599: for (i = 0, c = srv->srv_blob.clients; i < srv->srv_numcli && c; i++, c++)
1.6 misho 600: if (c->cli_sa.sa.sa_family)
1.2 misho 601: shutdown(c->cli_sock, SHUT_RDWR);
602: close(srv->srv_blob.server.cli_sock);
603:
604: if (srv->srv_blob.clients) {
605: free(srv->srv_blob.clients);
606: srv->srv_blob.clients = NULL;
607: }
608:
1.4 misho 609: /* detach blobs */
1.2 misho 610: while ((f = srv->srv_blob.blobs)) {
611: srv->srv_blob.blobs = f->blob_next;
612: rpc_srv_blobFree(srv, f);
613: free(f);
614: }
615:
1.7 misho 616: AIT_FREE_VAL(&srv->srv_blob.dir);
617:
1.8 misho 618: /* at final, save disable BLOB service state */
619: srv->srv_blob.state = disable;
1.2 misho 620: }
621:
622: /*
1.7 misho 623: * rpc_srv_loopBLOB() - Execute Main BLOB server loop and wait for clients requests
624: *
1.2 misho 625: * @srv = RPC Server instance
626: * return: -1 error or 0 ok, infinite loop ...
627: */
628: int
1.5 misho 629: rpc_srv_loopBLOB(rpc_srv_t * __restrict srv)
1.2 misho 630: {
1.6 misho 631: socklen_t salen = sizeof(io_sockaddr_t);
1.2 misho 632: register int i;
633: rpc_cli_t *c;
634: int ret;
1.7 misho 635: pthread_attr_t attr;
1.8.2.2 misho 636: struct pollfd pfd;
1.7 misho 637:
1.4 misho 638: if (!srv || srv->srv_blob.state == kill) {
1.7 misho 639: rpc_SetErr(EINVAL, "Invalid parameter can`t start BLOB server");
1.2 misho 640: return -1;
641: }
642:
643: if (listen(srv->srv_blob.server.cli_sock, SOMAXCONN) == -1) {
644: LOGERR;
645: return -1;
1.8.2.6 ! misho 646: } else
1.8.2.4 misho 647: fcntl(srv->srv_blob.server.cli_sock, F_SETFL,
648: fcntl(srv->srv_blob.server.cli_sock, F_GETFL) | O_NONBLOCK);
1.2 misho 649:
1.7 misho 650: pthread_attr_init(&attr);
651: pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
652:
1.8.2.4 misho 653: pfd.fd = srv->srv_blob.server.cli_sock;
1.8.2.2 misho 654: pfd.events = POLLIN | POLLPRI;
1.7 misho 655: /* main BLOB loop */
1.4 misho 656: while (srv->srv_blob.state != kill && srv->srv_kill != kill) {
1.2 misho 657: for (c = srv->srv_blob.clients, i = 0; i < srv->srv_numcli && c; i++, c++)
1.6 misho 658: if (!c->cli_sa.sa.sa_family)
1.2 misho 659: break;
660: if (i >= srv->srv_numcli) {
1.5 misho 661: #ifdef HAVE_PTHREAD_YIELD
662: pthread_yield();
663: #else
1.2 misho 664: usleep(1000000);
1.5 misho 665: #endif
1.2 misho 666: continue;
667: }
668:
1.8.2.2 misho 669: if ((ret = poll(&pfd, 1, srv->srv_session.sess_timeout * 1000)) == -1 ||
670: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
1.2 misho 671: LOGERR;
672: ret = 1;
673: break;
1.8.2.2 misho 674: } else if (!ret)
1.2 misho 675: continue;
676:
1.6 misho 677: c->cli_sock = accept(srv->srv_blob.server.cli_sock, &c->cli_sa.sa, &salen);
1.2 misho 678: if (c->cli_sock == -1) {
679: LOGERR;
680: continue;
681: } else
682: c->cli_parent = srv;
683:
1.7 misho 684: /* spawn dispatch thread for BLOB client */
685: if (pthread_create(&c->cli_tid, &attr, rpc_srv_dispatchVars, c)) {
1.2 misho 686: LOGERR;
687: continue;
1.7 misho 688: }
1.2 misho 689: }
690:
1.5 misho 691: srv->srv_blob.state = kill;
1.2 misho 692:
1.7 misho 693: pthread_attr_destroy(&attr);
1.2 misho 694: return 0;
695: }
696:
697:
698: /*
1.7 misho 699: * rpc_srv_initServer() - Init & create RPC Server
700: *
1.1 misho 701: * @regProgID = ProgramID for authentication & recognition
702: * @regProcID = ProcessID for authentication & recognition
703: * @concurentClients = Concurent clients at same time to this server
1.5 misho 704: * @netBuf = Network buffer length, if =0 == BUFSIZ (also meaning max RPC packet)
1.4 misho 705: * @family = Family type, AF_INET, AF_INET6 or AF_LOCAL
706: * @csHost = Host name or address for bind server, if NULL any address
1.1 misho 707: * @Port = Port for bind server, if Port == 0 default port is selected
708: * return: NULL == error or !=NULL bind and created RPC server instance
709: */
710: rpc_srv_t *
711: rpc_srv_initServer(u_int regProgID, u_int regProcID, int concurentClients,
1.5 misho 712: int netBuf, u_short family, const char *csHost, u_short Port)
1.1 misho 713: {
714: rpc_srv_t *srv = NULL;
715: int n = 1;
716: struct hostent *host = NULL;
1.6 misho 717: io_sockaddr_t sa;
1.1 misho 718:
1.4 misho 719: if (!concurentClients || !regProgID ||
720: (family != AF_INET && family != AF_INET6 && family != AF_LOCAL)) {
1.1 misho 721: rpc_SetErr(EINVAL, "Error:: Invalid parameters can`t init RPC server ...\n");
722: return NULL;
723: }
724: if (!Port)
725: Port = RPC_DEFPORT;
1.5 misho 726: if (!netBuf)
727: netBuf = BUFSIZ;
1.7 misho 728: else
729: netBuf = io_align(netBuf, 1); /* align netBuf length */
1.4 misho 730: if (csHost && family != AF_LOCAL) {
1.1 misho 731: host = gethostbyname2(csHost, family);
732: if (!host) {
733: rpc_SetErr(h_errno, "Error:: %s\n", hstrerror(h_errno));
734: return NULL;
735: }
736: }
1.4 misho 737: memset(&sa, 0, sizeof sa);
1.6 misho 738: sa.sa.sa_family = family;
1.1 misho 739: switch (family) {
740: case AF_INET:
1.6 misho 741: sa.sin.sin_len = sizeof(struct sockaddr_in);
742: sa.sin.sin_port = htons(Port);
1.1 misho 743: if (csHost)
1.6 misho 744: memcpy(&sa.sin.sin_addr, host->h_addr, host->h_length);
1.1 misho 745: break;
746: case AF_INET6:
1.6 misho 747: sa.sin6.sin6_len = sizeof(struct sockaddr_in6);
748: sa.sin6.sin6_port = htons(Port);
1.4 misho 749: if (csHost)
1.6 misho 750: memcpy(&sa.sin6.sin6_addr, host->h_addr, host->h_length);
1.4 misho 751: break;
752: case AF_LOCAL:
1.6 misho 753: sa.sun.sun_len = sizeof(struct sockaddr_un);
1.1 misho 754: if (csHost)
1.6 misho 755: strlcpy(sa.sun.sun_path, csHost, sizeof sa.sun.sun_path);
756: unlink(sa.sun.sun_path);
1.1 misho 757: break;
758: default:
759: rpc_SetErr(EINVAL, "Error:: Invalid parameters can`t start RPC server ...\n");
760: return NULL;
761: }
762:
763: srv = malloc(sizeof(rpc_srv_t));
764: if (!srv) {
765: LOGERR;
766: return NULL;
767: } else
768: memset(srv, 0, sizeof(rpc_srv_t));
769:
1.5 misho 770: srv->srv_netbuf = netBuf;
1.1 misho 771: srv->srv_numcli = concurentClients;
772: srv->srv_session.sess_version = RPC_VERSION;
1.7 misho 773: srv->srv_session.sess_timeout = DEF_RPC_TIMEOUT;
1.1 misho 774: srv->srv_session.sess_program = regProgID;
775: srv->srv_session.sess_process = regProcID;
776:
1.2 misho 777: srv->srv_server.cli_tid = pthread_self();
1.1 misho 778: srv->srv_server.cli_parent = srv;
1.6 misho 779: memcpy(&srv->srv_server.cli_sa, &sa, sizeof sa);
1.4 misho 780:
781: /* create server socket */
1.1 misho 782: srv->srv_server.cli_sock = socket(family, SOCK_STREAM, 0);
783: if (srv->srv_server.cli_sock == -1) {
784: LOGERR;
785: free(srv);
786: return NULL;
787: }
788: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
789: LOGERR;
790: close(srv->srv_server.cli_sock);
791: free(srv);
792: return NULL;
793: }
1.5 misho 794: n = srv->srv_netbuf;
795: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof n) == -1) {
796: LOGERR;
797: close(srv->srv_server.cli_sock);
798: free(srv);
799: return NULL;
800: }
801: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_RCVBUF, &n, sizeof n) == -1) {
802: LOGERR;
803: close(srv->srv_server.cli_sock);
804: free(srv);
805: return NULL;
806: }
1.6 misho 807: if (bind(srv->srv_server.cli_sock, &srv->srv_server.cli_sa.sa,
808: srv->srv_server.cli_sa.sa.sa_len) == -1) {
1.1 misho 809: LOGERR;
810: close(srv->srv_server.cli_sock);
811: free(srv);
812: return NULL;
813: }
814:
1.4 misho 815: /* allocate pool for concurent clients */
1.1 misho 816: srv->srv_clients = calloc(srv->srv_numcli, sizeof(rpc_cli_t));
817: if (!srv->srv_clients) {
818: LOGERR;
819: close(srv->srv_server.cli_sock);
820: free(srv);
821: return NULL;
822: } else
823: memset(srv->srv_clients, 0, srv->srv_numcli * sizeof(rpc_cli_t));
824:
1.2 misho 825: rpc_srv_registerCall(srv, NULL, CALL_SRVSHUTDOWN, 0);
1.5 misho 826: rpc_srv_registerCall(srv, NULL, CALL_SRVCLIENTS, 1);
827: rpc_srv_registerCall(srv, NULL, CALL_SRVSESSIONS, 4);
828: rpc_srv_registerCall(srv, NULL, CALL_SRVCALLS, 1);
1.8 misho 829:
1.1 misho 830: return srv;
831: }
832:
833: /*
1.7 misho 834: * rpc_srv_endServer() - Destroy RPC server, close all opened sockets and free resources
835: *
1.6 misho 836: * @psrv = RPC Server instance
1.1 misho 837: * return: none
838: */
839: void
1.6 misho 840: rpc_srv_endServer(rpc_srv_t ** __restrict psrv)
1.1 misho 841: {
842: rpc_cli_t *c;
843: register int i;
844: rpc_func_t *f;
845:
1.6 misho 846: if (!psrv || !*psrv) {
1.1 misho 847: rpc_SetErr(EINVAL, "Error:: Can`t destroy server because parameter is null!\n");
848: return;
849: }
850:
1.6 misho 851: rpc_srv_endBLOBServer(*psrv);
1.1 misho 852:
1.4 misho 853: /* close all clients connections & server socket */
1.6 misho 854: for (i = 0, c = (*psrv)->srv_clients; i < (*psrv)->srv_numcli && c; i++, c++)
855: if (c->cli_sa.sa.sa_family) {
1.1 misho 856: shutdown(c->cli_sock, SHUT_RDWR);
1.2 misho 857: close(c->cli_sock);
858: }
1.6 misho 859: close((*psrv)->srv_server.cli_sock);
1.1 misho 860:
1.6 misho 861: if ((*psrv)->srv_clients) {
862: free((*psrv)->srv_clients);
863: (*psrv)->srv_clients = NULL;
864: (*psrv)->srv_numcli = 0;
1.1 misho 865: }
866:
1.4 misho 867: /* detach exported calls */
1.6 misho 868: while ((f = (*psrv)->srv_funcs)) {
869: (*psrv)->srv_funcs = f->func_next;
870: io_freeVars(&f->func_vars);
1.7 misho 871: AIT_FREE_VAL(&f->func_name);
872: AIT_FREE_VAL(&f->func_file);
1.2 misho 873: free(f);
874: }
875:
1.6 misho 876: free(*psrv);
877: *psrv = NULL;
1.1 misho 878: }
879:
880: /*
1.7 misho 881: * rpc_srv_loopServer() - Execute Main server loop and wait for clients requests
882: *
1.1 misho 883: * @srv = RPC Server instance
884: * return: -1 error or 0 ok, infinite loop ...
885: */
886: int
1.5 misho 887: rpc_srv_loopServer(rpc_srv_t * __restrict srv)
1.1 misho 888: {
1.6 misho 889: socklen_t salen = sizeof(io_sockaddr_t);
1.1 misho 890: register int i;
891: rpc_cli_t *c;
1.2 misho 892: int ret;
1.7 misho 893: pthread_attr_t attr;
1.8.2.2 misho 894: struct pollfd pfd;
1.7 misho 895:
1.1 misho 896: if (!srv) {
897: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t start RPC server ...\n");
898: return -1;
899: }
900:
1.5 misho 901: /* activate BLOB server worker if srv->srv_blob.state == enable */
902: rpc_srv_execBLOBServer(srv);
903:
1.1 misho 904: if (listen(srv->srv_server.cli_sock, SOMAXCONN) == -1) {
905: LOGERR;
906: return -1;
1.8.2.5 misho 907: } /*else
1.8.2.2 misho 908: fcntl(srv->srv_server.cli_sock, F_SETFL, fcntl(srv->srv_server.cli_sock, F_GETFL) | O_NONBLOCK);
1.8.2.5 misho 909: */
1.1 misho 910:
1.7 misho 911: pthread_attr_init(&attr);
912: pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
913:
1.8.2.2 misho 914: pfd.fd = srv->srv_server.cli_sock;
915: pfd.events = POLLIN | POLLPRI;
1.7 misho 916: /* main rpc loop */
1.4 misho 917: while (srv->srv_kill != kill) {
1.1 misho 918: for (c = srv->srv_clients, i = 0; i < srv->srv_numcli && c; i++, c++)
1.6 misho 919: if (!c->cli_sa.sa.sa_family)
1.1 misho 920: break;
1.2 misho 921: if (i >= srv->srv_numcli) {
1.5 misho 922: #ifdef HAVE_PTHREAD_YIELD
923: pthread_yield();
924: #else
1.1 misho 925: usleep(1000000);
1.5 misho 926: #endif
1.1 misho 927: continue;
928: }
1.2 misho 929:
1.8.2.2 misho 930: if ((ret = poll(&pfd, 1, srv->srv_session.sess_timeout * 1000)) == -1 ||
931: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
1.2 misho 932: LOGERR;
933: ret = 1;
934: break;
1.8.2.2 misho 935: } else if (!ret)
1.2 misho 936: continue;
937:
1.6 misho 938: c->cli_sock = accept(srv->srv_server.cli_sock, &c->cli_sa.sa, &salen);
1.1 misho 939: if (c->cli_sock == -1) {
940: LOGERR;
941: continue;
942: } else
943: c->cli_parent = srv;
944:
1.7 misho 945: /* spawn rpc client dispatcher */
946: if (pthread_create(&c->cli_tid, &attr, rpc_srv_dispatchCall, c)) {
1.1 misho 947: LOGERR;
948: continue;
1.7 misho 949: }
1.1 misho 950: }
951:
1.7 misho 952: pthread_attr_destroy(&attr);
1.1 misho 953: return 0;
954: }
955:
956: // ---------------------------------------------------------
957:
958: /*
959: * rpc_srv_execCall() Execute registered call from RPC server
1.7 misho 960: *
1.1 misho 961: * @call = Register RPC call
962: * @rpc = IN RPC call structure
1.5 misho 963: * @args = IN RPC calling arguments from RPC client
1.1 misho 964: * return: -1 error, !=-1 ok
965: */
966: int
1.2 misho 967: rpc_srv_execCall(rpc_func_t * __restrict call, struct tagRPCCall * __restrict rpc,
1.5 misho 968: array_t * __restrict args)
1.1 misho 969: {
970: void *dl;
971: rpc_callback_t func;
972: int ret;
973:
1.2 misho 974: if (!call || !rpc || !call->func_parent) {
1.7 misho 975: rpc_SetErr(EINVAL, "Invalid parameter can`t exec function");
1.1 misho 976: return -1;
977: }
978:
1.8.2.1 misho 979: dl = dlopen(AIT_ADDR(&call->func_file), RTLD_NOW);
1.1 misho 980: if (!dl) {
1.7 misho 981: rpc_SetErr(ENOENT, "Can`t attach module %s!", dlerror());
1.1 misho 982: return -1;
983: }
984:
1.7 misho 985: func = dlsym(dl, (const char*) AIT_GET_STR(&call->func_name));
1.1 misho 986: if (func)
1.6 misho 987: ret = func(call, ntohs(rpc->call_argc), args);
1.1 misho 988: else {
1.7 misho 989: rpc_SetErr(ENOEXEC, "Can`t find function %s!", dlerror());
1.1 misho 990: ret = -1;
991: }
992:
993: dlclose(dl);
994: return ret;
995: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>