Annotation of libaitrpc/src/srv.c, revision 1.20.4.2
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.20.4.2! misho 6: * $Id: srv.c,v 1.20.4.1 2013/11/13 12:18:14 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.14 misho 15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
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.13 misho 49: /* SOCK_STREAM */
50: static void *acceptClients(sched_task_t *);
51: static void *closeClient(sched_task_t *);
52: static void *rxPacket(sched_task_t *);
53: static void *txPacket(sched_task_t *);
54:
55: /* SOCK_DGRAM */
56: static void *freeClient(sched_task_t *);
57: static void *rxUDPPacket(sched_task_t *);
58: static void *txUDPPacket(sched_task_t *);
59:
60: /* SOCK_RAW */
61:
62: static sched_task_func_t cbProto[SOCK_RAW + 1][4] = {
63: { acceptClients, closeClient, rxPacket, txPacket }, /* SOCK_STREAM */
64: { acceptClients, closeClient, rxPacket, txPacket }, /* SOCK_STREAM */
65: { rxUDPPacket, freeClient, rxUDPPacket, txUDPPacket }, /* SOCK_DGRAM */
66: { NULL, NULL, NULL, NULL } /* SOCK_RAW */
67: };
68:
69:
1.16 misho 70: void
1.13 misho 71: rpc_freeCli(rpc_cli_t * __restrict c)
1.10 misho 72: {
73: rpc_srv_t *s = c->cli_parent;
74:
1.13 misho 75: schedCancelby(s->srv_root, taskMAX, CRITERIA_ARG, c, NULL);
1.10 misho 76:
77: /* free buffer */
78: AIT_FREE_VAL(&c->cli_buf);
79:
1.14 misho 80: array_Del(s->srv_clients, c->cli_id, 0);
1.10 misho 81: if (c)
1.14 misho 82: e_free(c);
1.13 misho 83: }
84:
85:
86: static inline int
1.14 misho 87: _check4freeslot(rpc_srv_t * __restrict srv, sockaddr_t * __restrict sa)
1.13 misho 88: {
89: rpc_cli_t *c = NULL;
90: register int i;
91:
92: /* check free slots for connect */
1.14 misho 93: for (i = 0; i < array_Size(srv->srv_clients) &&
94: (c = array(srv->srv_clients, i, rpc_cli_t*)); i++)
1.13 misho 95: /* check for duplicates */
1.14 misho 96: if (sa && !e_addrcmp(&c->cli_sa, sa, 42))
1.13 misho 97: break;
1.14 misho 98: if (i >= array_Size(srv->srv_clients))
1.13 misho 99: return -1; /* no more free slots! */
100:
101: return i;
102: }
103:
104: static rpc_cli_t *
1.14 misho 105: _allocClient(rpc_srv_t * __restrict srv, sockaddr_t * __restrict sa)
1.13 misho 106: {
107: rpc_cli_t *c = NULL;
108: int n;
109:
110: n = _check4freeslot(srv, sa);
111: if (n == -1)
112: return NULL;
113: else
1.14 misho 114: c = array(srv->srv_clients, n, rpc_cli_t*);
1.13 misho 115:
116: if (!c) {
1.14 misho 117: c = e_malloc(sizeof(rpc_cli_t));
1.13 misho 118: if (!c) {
119: LOGERR;
120: srv->srv_kill = 1;
121: return NULL;
122: } else {
123: memset(c, 0, sizeof(rpc_cli_t));
1.14 misho 124: array_Set(srv->srv_clients, n, c);
1.13 misho 125: c->cli_id = n;
126: c->cli_parent = srv;
127: }
128:
129: /* alloc empty buffer */
1.14 misho 130: AIT_SET_BUFSIZ(&c->cli_buf, 0, srv->srv_netbuf);
1.13 misho 131: }
132:
133: return c;
134: }
135:
136:
137: static void *
138: freeClient(sched_task_t *task)
139: {
140: rpc_freeCli(TASK_ARG(task));
141:
142: return NULL;
143: }
144:
145: static void *
146: closeClient(sched_task_t *task)
147: {
148: int sock = ((rpc_cli_t*) TASK_ARG(task))->cli_sock;
149:
150: rpc_freeCli(TASK_ARG(task));
151:
152: /* close client socket */
153: shutdown(sock, SHUT_RDWR);
154: close(sock);
1.10 misho 155: return NULL;
156: }
1.7 misho 157:
158: static void *
159: txPacket(sched_task_t *task)
160: {
161: rpc_cli_t *c = TASK_ARG(task);
162: rpc_srv_t *s = c->cli_parent;
163: rpc_func_t *f = NULL;
1.18 misho 164: u_char *buf = AIT_GET_BUF(&c->cli_buf);
1.7 misho 165: struct tagRPCCall *rpc = (struct tagRPCCall*) buf;
1.18 misho 166: int ret, estlen, wlen = sizeof(struct tagRPCCall);
1.19 misho 167: struct pollfd pfd;
1.7 misho 168:
169: if (rpc->call_argc) {
1.10 misho 170: f = rpc_srv_getCall(s, ntohs(rpc->call_tag));
1.7 misho 171: if (!f) {
1.10 misho 172: rpc_SetErr(EPROGUNAVAIL, "Function not found at RPC server");
1.18 misho 173:
1.7 misho 174: rpc->call_argc ^= rpc->call_argc;
175: rpc->call_rep.ret = RPC_ERROR(-1);
176: rpc->call_rep.eno = RPC_ERROR(rpc_Errno);
177: } else {
1.18 misho 178: /* calc estimated length */
179: estlen = ait_resideVars(RPC_RETVARS(c)) + wlen;
180: if (estlen > AIT_LEN(&c->cli_buf))
181: AIT_RE_BUF(&c->cli_buf, estlen);
182: buf = AIT_GET_BUF(&c->cli_buf);
1.19 misho 183: rpc = (struct tagRPCCall*) buf;
1.18 misho 184:
1.14 misho 185: rpc->call_argc = htons(array_Size(RPC_RETVARS(c)));
1.7 misho 186: /* Go Encapsulate variables */
1.18 misho 187: ret = ait_vars2buffer(buf + wlen, AIT_LEN(&c->cli_buf) - wlen,
188: RPC_RETVARS(c));
1.10 misho 189: /* Free return values */
1.14 misho 190: ait_freeVars(&c->cli_vars);
1.7 misho 191: if (ret == -1) {
192: rpc_SetErr(EBADRPC, "Prepare RPC packet failed");
1.19 misho 193:
1.7 misho 194: rpc->call_argc ^= rpc->call_argc;
195: rpc->call_rep.ret = RPC_ERROR(-1);
196: rpc->call_rep.eno = RPC_ERROR(rpc_Errno);
197: } else
198: wlen += ret;
199: }
200: }
201:
1.18 misho 202: rpc->call_len = htonl(wlen);
1.8 misho 203:
1.15 misho 204: #if 0
1.7 misho 205: /* calculate CRC */
206: rpc->call_crc ^= rpc->call_crc;
1.8 misho 207: rpc->call_crc = htons(crcFletcher16((u_short*) buf, wlen / 2));
1.15 misho 208: #endif
1.7 misho 209:
210: /* send reply */
1.19 misho 211: pfd.fd = TASK_FD(task);
212: pfd.events = POLLOUT;
213: for (; wlen > 0; wlen -= ret, buf += ret) {
214: if ((ret = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) < 1 ||
215: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
216: if (ret)
217: LOGERR;
218: else
219: rpc_SetErr(ETIMEDOUT, "Timeout reached! Server not respond");
220: /* close connection */
221: schedEvent(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
222: TASK_ARG(task), 0, NULL, 0);
223: return NULL;
224: }
225: ret = send(TASK_FD(task), buf, MIN(wlen, s->srv_netbuf), MSG_NOSIGNAL);
226: if (ret == -1) {
227: /* close connection */
228: schedEvent(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
229: TASK_ARG(task), 0, NULL, 0);
230: return NULL;
231: }
1.10 misho 232: }
1.7 misho 233:
234: return NULL;
235: }
236:
237: static void *
238: execCall(sched_task_t *task)
239: {
240: rpc_cli_t *c = TASK_ARG(task);
241: rpc_srv_t *s = c->cli_parent;
242: rpc_func_t *f = NULL;
243: array_t *arr = NULL;
1.18 misho 244: u_char *buf = AIT_GET_BUF(&c->cli_buf);
1.7 misho 245: struct tagRPCCall *rpc = (struct tagRPCCall*) buf;
246: int argc = ntohs(rpc->call_argc);
247:
248: /* Go decapsulate variables ... */
1.8 misho 249: if (argc) {
1.14 misho 250: arr = ait_buffer2vars(buf + sizeof(struct tagRPCCall),
1.18 misho 251: AIT_LEN(&c->cli_buf) - sizeof(struct tagRPCCall), argc, 42);
1.7 misho 252: if (!arr) {
1.14 misho 253: rpc_SetErr(ERPCMISMATCH, "#%d - %s", elwix_GetErrno(), elwix_GetError());
1.18 misho 254:
1.7 misho 255: rpc->call_argc ^= rpc->call_argc;
256: rpc->call_rep.ret = RPC_ERROR(-1);
257: rpc->call_rep.eno = RPC_ERROR(rpc_Errno);
258: return NULL;
259: }
1.10 misho 260: } else
261: arr = NULL;
1.7 misho 262:
1.10 misho 263: if (!(f = rpc_srv_getCall(s, ntohs(rpc->call_tag)))) {
1.7 misho 264: rpc_SetErr(EPROGUNAVAIL, "Function not found at RPC server");
1.18 misho 265:
1.7 misho 266: rpc->call_argc ^= rpc->call_argc;
267: rpc->call_rep.ret = RPC_ERROR(-1);
268: rpc->call_rep.eno = RPC_ERROR(rpc_Errno);
269: } else {
1.8 misho 270: /* if client doesn't want reply */
1.10 misho 271: argc = RPC_CHK_NOREPLY(rpc);
272: rpc->call_rep.ret = RPC_ERROR(rpc_srv_execCall(c, rpc, f->func_name, arr));
1.7 misho 273: if (rpc->call_rep.ret == htonl(-1)) {
1.20 misho 274: if (!rpc->call_rep.eno) {
275: LOGERR;
276: rpc->call_rep.eno = RPC_ERROR(rpc_Errno);
277: }
1.7 misho 278: rpc->call_argc ^= rpc->call_argc;
279: } else {
280: rpc->call_rep.eno ^= rpc->call_rep.eno;
1.8 misho 281: if (argc) {
282: /* without reply */
1.14 misho 283: ait_freeVars(&c->cli_vars);
1.8 misho 284: rpc->call_argc ^= rpc->call_argc;
1.10 misho 285: } else {
286: /* reply */
1.14 misho 287: rpc->call_argc = htons(array_Size(RPC_RETVARS(c)));
1.10 misho 288: }
1.7 misho 289: }
290: }
291:
1.14 misho 292: array_Destroy(&arr);
1.7 misho 293: return NULL;
294: }
295:
296: static void *
297: rxPacket(sched_task_t *task)
298: {
299: rpc_cli_t *c = TASK_ARG(task);
300: rpc_srv_t *s = c->cli_parent;
1.18 misho 301: int len, rlen, noreply, estlen;
1.15 misho 302: #if 0
303: u_short crc;
304: #endif
1.10 misho 305: u_char *buf = AIT_GET_BUF(&c->cli_buf);
1.18 misho 306: struct tagRPCCall *rpc = (struct tagRPCCall*) buf;
1.19 misho 307: struct pollfd pfd;
1.7 misho 308:
1.18 misho 309: memset(buf, 0, sizeof(struct tagRPCCall));
1.19 misho 310: rlen = recv(TASK_FD(task), rpc, sizeof(struct tagRPCCall), MSG_PEEK);
1.18 misho 311: if (rlen < sizeof(struct tagRPCCall)) {
1.10 misho 312: /* close connection */
1.13 misho 313: schedEvent(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
314: TASK_ARG(task), 0, NULL, 0);
1.7 misho 315: return NULL;
1.10 misho 316: } else {
1.18 misho 317: estlen = ntohl(rpc->call_len);
318: if (estlen > AIT_LEN(&c->cli_buf))
319: AIT_RE_BUF(&c->cli_buf, estlen);
320: rpc = (struct tagRPCCall*) AIT_GET_BUF(&c->cli_buf);
1.19 misho 321: buf = AIT_GET_BUF(&c->cli_buf);
322: len = estlen;
1.18 misho 323: }
324:
1.19 misho 325: /* get next part of packet */
326: memset(buf, 0, len);
327: pfd.fd = TASK_FD(task);
328: pfd.events = POLLIN | POLLPRI;
329: for (; len > 0; len -= rlen, buf += rlen) {
330: if ((rlen = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) < 1 ||
331: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
332: if (rlen)
333: LOGERR;
334: else
335: rpc_SetErr(ETIMEDOUT, "Timeout reached! Server not respond");
336: schedEvent(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
337: TASK_ARG(task), 0, NULL, 0);
338: return NULL;
339: }
1.18 misho 340: rlen = recv(TASK_FD(task), buf, len, 0);
341: if (rlen == -1) {
342: /* close connection */
343: schedEvent(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
344: TASK_ARG(task), 0, NULL, 0);
1.8 misho 345: return NULL;
1.10 misho 346: }
1.18 misho 347: }
1.8 misho 348:
1.15 misho 349: #if 0
1.18 misho 350: /* check integrity of packet */
351: crc = ntohs(rpc->call_crc);
352: rpc->call_crc ^= rpc->call_crc;
353: if (crc != crcFletcher16((u_short*) rpc, len / 2)) {
354: rpc_SetErr(ERPCMISMATCH, "Bad CRC RPC packet");
355: return NULL;
356: }
1.15 misho 357: #endif
1.7 misho 358:
1.18 misho 359: noreply = RPC_CHK_NOREPLY(rpc);
1.7 misho 360:
1.18 misho 361: /* check RPC packet session info */
362: if (rpc_chkPktSession(&rpc->call_session, &s->srv_session)) {
363: rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session");
1.7 misho 364:
1.18 misho 365: rpc->call_argc ^= rpc->call_argc;
366: rpc->call_rep.ret = RPC_ERROR(-1);
367: rpc->call_rep.eno = RPC_ERROR(errno);
368: } else {
369: /* execute RPC call */
370: schedEvent(TASK_ROOT(task), execCall, TASK_ARG(task), (int) noreply, rpc, len);
371: }
1.7 misho 372:
1.18 misho 373: /* send RPC reply */
374: if (!noreply)
375: schedWrite(TASK_ROOT(task), cbProto[s->srv_proto][CB_TXPACKET],
376: TASK_ARG(task), TASK_FD(task), rpc, len);
1.7 misho 377:
378: /* lets get next packet */
1.18 misho 379: schedReadSelf(task);
1.7 misho 380: return NULL;
381: }
382:
1.1 misho 383: static void *
1.10 misho 384: acceptClients(sched_task_t *task)
1.1 misho 385: {
1.10 misho 386: rpc_srv_t *srv = TASK_ARG(task);
387: rpc_cli_t *c = NULL;
1.14 misho 388: socklen_t salen = sizeof(sockaddr_t);
1.20.4.1 misho 389: int sock;
1.7 misho 390:
1.13 misho 391: c = _allocClient(srv, NULL);
1.20.4.1 misho 392: if (!c) {
393: EVERBOSE(1, "RPC client quota exceeded! Connection will be shutdown!\n");
394: if ((sock = accept(TASK_FD(task), NULL, NULL)) != -1) {
395: shutdown(sock, SHUT_RDWR);
396: close(sock);
397: }
1.10 misho 398: goto end;
1.20.4.1 misho 399: }
1.10 misho 400:
401: /* accept client */
402: c->cli_sock = accept(TASK_FD(task), &c->cli_sa.sa, &salen);
403: if (c->cli_sock == -1) {
404: LOGERR;
405: AIT_FREE_VAL(&c->cli_buf);
1.14 misho 406: array_Del(srv->srv_clients, c->cli_id, 42);
1.10 misho 407: goto end;
1.1 misho 408: } else
1.10 misho 409: fcntl(c->cli_sock, F_SETFL, fcntl(c->cli_sock, F_GETFL) | O_NONBLOCK);
1.1 misho 410:
1.13 misho 411: schedRead(TASK_ROOT(task), cbProto[srv->srv_proto][CB_RXPACKET], c,
412: c->cli_sock, NULL, 0);
1.10 misho 413: end:
414: schedReadSelf(task);
415: return NULL;
416: }
1.5 misho 417:
1.7 misho 418:
1.10 misho 419: static void *
1.13 misho 420: txUDPPacket(sched_task_t *task)
1.10 misho 421: {
422: rpc_cli_t *c = TASK_ARG(task);
423: rpc_srv_t *s = c->cli_parent;
1.13 misho 424: rpc_func_t *f = NULL;
1.18 misho 425: u_char *buf = AIT_GET_BUF(&c->cli_buf);
1.13 misho 426: struct tagRPCCall *rpc = (struct tagRPCCall*) buf;
427: int ret, wlen = sizeof(struct tagRPCCall);
428: struct timespec ts = { DEF_RPC_TIMEOUT, 0 };
429:
430: schedCancelby(TASK_ROOT(task), taskTIMER, CRITERIA_DATA, TASK_ARG(task), NULL);
431: schedTimer(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
432: TASK_ARG(task), ts, TASK_ARG(task), 0);
433:
434: if (rpc->call_argc) {
435: f = rpc_srv_getCall(s, ntohs(rpc->call_tag));
436: if (!f) {
437: rpc_SetErr(EPROGUNAVAIL, "Function not found at RPC server");
438: rpc->call_argc ^= rpc->call_argc;
439: rpc->call_rep.ret = RPC_ERROR(-1);
440: rpc->call_rep.eno = RPC_ERROR(rpc_Errno);
441: } else {
1.14 misho 442: rpc->call_argc = htons(array_Size(RPC_RETVARS(c)));
1.13 misho 443: /* Go Encapsulate variables */
1.18 misho 444: ret = ait_vars2buffer(buf + wlen, AIT_LEN(&c->cli_buf) - wlen,
445: RPC_RETVARS(c));
1.13 misho 446: /* Free return values */
1.14 misho 447: ait_freeVars(&c->cli_vars);
1.13 misho 448: if (ret == -1) {
449: rpc_SetErr(EBADRPC, "Prepare RPC packet failed");
450: rpc->call_argc ^= rpc->call_argc;
451: rpc->call_rep.ret = RPC_ERROR(-1);
452: rpc->call_rep.eno = RPC_ERROR(rpc_Errno);
453: } else
454: wlen += ret;
455: }
456: }
1.7 misho 457:
1.18 misho 458: rpc->call_len = htonl(wlen);
1.7 misho 459:
1.13 misho 460: /* calculate CRC */
461: rpc->call_crc ^= rpc->call_crc;
462: rpc->call_crc = htons(crcFletcher16((u_short*) buf, wlen / 2));
463:
464: /* send reply */
465: ret = sendto(TASK_FD(task), buf, wlen, MSG_NOSIGNAL,
466: &c->cli_sa.sa, c->cli_sa.sa.sa_len);
467: if (ret == -1 || ret != wlen) {
468: /* close connection */
469: schedEvent(TASK_ROOT(task), cbProto[s->srv_proto][CB_CLOSECLIENT],
470: TASK_ARG(task), 0, NULL, 0);
471: }
472:
473: return NULL;
474: }
475:
476: static void *
477: rxUDPPacket(sched_task_t *task)
478: {
479: rpc_srv_t *srv = TASK_ARG(task);
480: rpc_cli_t *c = NULL;
481: int len, rlen, noreply;
482: u_short crc, off = 0;
483: u_char buf[USHRT_MAX + 1];
484: struct tagRPCCall *rpc = (struct tagRPCCall*) buf;
1.14 misho 485: sockaddr_t sa;
486: socklen_t salen;
1.13 misho 487: struct timespec ts = { DEF_RPC_TIMEOUT, 0 };
488:
489: /* receive connect packet */
1.14 misho 490: salen = sa.ss.ss_len = sizeof(sockaddr_t);
1.13 misho 491: rlen = recvfrom(TASK_FD(task), buf, sizeof buf, 0, &sa.sa, &salen);
492: if (rlen < 1)
493: goto end;
494:
495: c = _allocClient(srv, &sa);
496: if (!c)
497: goto end;
498: else {
499: c->cli_sock = TASK_FD(task);
500: memcpy(&c->cli_sa, &sa, sizeof c->cli_sa);
501: memcpy(AIT_GET_BUF(&c->cli_buf), buf, AIT_LEN(&c->cli_buf));
502: /* armed timer for close stateless connection */
503: schedCancelby(TASK_ROOT(task), taskTIMER, CRITERIA_DATA, c, NULL);
504: schedTimer(TASK_ROOT(task), cbProto[srv->srv_proto][CB_CLOSECLIENT],
505: c, ts, c, 0);
506: }
507:
508: do {
509: /* check RPC packet */
510: if (rlen < sizeof(struct tagRPCCall)) {
511: rpc_SetErr(ERPCMISMATCH, "Short RPC packet");
512: break;
513: } else
514: rpc = (struct tagRPCCall*) (AIT_GET_BUF(&c->cli_buf) + off);
515:
1.18 misho 516: len = ntohl(rpc->call_len);
1.13 misho 517: rlen -= len;
518:
519: /* check RPC packet lengths */
520: if (rlen < 0 || len < sizeof(struct tagRPCCall)) {
521: rpc_SetErr(ERPCMISMATCH, "Broken RPC packet length");
522: /* skip entire packet */
523: break;
524: }
525:
526: /* check integrity of packet */
527: crc = ntohs(rpc->call_crc);
528: rpc->call_crc ^= rpc->call_crc;
529: if (crc != crcFletcher16((u_short*) rpc, len / 2)) {
530: rpc_SetErr(ERPCMISMATCH, "Bad CRC RPC packet");
531:
532: off += len;
533: /* try next packet remaining into buffer */
534: continue;
535: }
536:
537: noreply = RPC_CHK_NOREPLY(rpc);
538:
539: /* check RPC packet session info */
540: if (rpc_chkPktSession(&rpc->call_session, &srv->srv_session)) {
541: rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session");
1.18 misho 542:
1.13 misho 543: rpc->call_argc ^= rpc->call_argc;
544: rpc->call_rep.ret = RPC_ERROR(-1);
545: rpc->call_rep.eno = RPC_ERROR(errno);
546: } else {
547: /* execute RPC call */
1.18 misho 548: schedEvent(TASK_ROOT(task), execCall, c,
549: (int) noreply, rpc, len);
1.13 misho 550: }
551:
552: /* send RPC reply */
553: if (!noreply)
554: schedWrite(TASK_ROOT(task), cbProto[srv->srv_proto][CB_TXPACKET],
555: c, TASK_FD(task), rpc, len);
556:
557: off += len;
558: } while (rlen > 0);
559:
560: end:
561: schedReadSelf(task);
562: return NULL;
563: }
564:
565: /* ------------------------------------------------------ */
566:
1.16 misho 567: void
1.13 misho 568: rpc_freeBLOBCli(rpc_cli_t * __restrict c)
569: {
570: rpc_srv_t *s = c->cli_parent;
571:
572: schedCancelby(s->srv_blob.root, taskMAX, CRITERIA_ARG, c, NULL);
1.10 misho 573:
574: /* free buffer */
575: AIT_FREE_VAL(&c->cli_buf);
576:
1.14 misho 577: array_Del(s->srv_blob.clients, c->cli_id, 0);
1.10 misho 578: if (c)
1.14 misho 579: e_free(c);
1.13 misho 580: }
581:
582:
583: static void *
584: closeBLOBClient(sched_task_t *task)
585: {
586: int sock = ((rpc_cli_t*) TASK_ARG(task))->cli_sock;
587:
588: rpc_freeBLOBCli(TASK_ARG(task));
589:
590: /* close client socket */
591: shutdown(sock, SHUT_RDWR);
592: close(sock);
1.7 misho 593: return NULL;
594: }
595:
596: static void *
597: txBLOB(sched_task_t *task)
598: {
1.10 misho 599: rpc_cli_t *c = TASK_ARG(task);
600: u_char *buf = AIT_GET_BUF(&c->cli_buf);
1.7 misho 601: int wlen = sizeof(struct tagBLOBHdr);
602:
603: /* send reply */
1.10 misho 604: wlen = send(TASK_FD(task), buf, wlen, MSG_NOSIGNAL);
605: if (wlen == -1 || wlen != sizeof(struct tagBLOBHdr)) {
606: /* close blob connection */
607: schedEvent(TASK_ROOT(task), closeBLOBClient, c, 42, NULL, 0);
608: }
1.7 misho 609:
610: return NULL;
611: }
1.4 misho 612:
1.7 misho 613: static void *
614: rxBLOB(sched_task_t *task)
615: {
616: rpc_cli_t *c = TASK_ARG(task);
617: rpc_srv_t *s = c->cli_parent;
618: rpc_blob_t *b;
1.10 misho 619: struct tagBLOBHdr blob;
1.7 misho 620: int rlen;
621:
1.10 misho 622: memset(&blob, 0, sizeof blob);
623: rlen = recv(TASK_FD(task), &blob, sizeof blob, 0);
624: if (rlen < 1) {
625: /* close blob connection */
626: schedEvent(TASK_ROOT(task), closeBLOBClient, c, 42, NULL, 0);
1.7 misho 627: return NULL;
628: }
1.5 misho 629:
1.10 misho 630: /* check BLOB packet */
631: if (rlen < sizeof(struct tagBLOBHdr)) {
632: rpc_SetErr(ERPCMISMATCH, "Short BLOB packet");
1.6 misho 633:
1.10 misho 634: schedReadSelf(task);
1.7 misho 635: return NULL;
636: }
1.1 misho 637:
1.7 misho 638: /* check RPC packet session info */
1.15 misho 639: if (rpc_chkPktSession(&blob.hdr_session, &s->srv_session)) {
1.7 misho 640: rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session");
1.10 misho 641: blob.hdr_cmd = error;
1.7 misho 642: goto end;
643: }
644:
645: /* Go to proceed packet ... */
1.10 misho 646: switch (blob.hdr_cmd) {
1.7 misho 647: case get:
1.10 misho 648: if (!(b = rpc_srv_getBLOB(s, ntohl(blob.hdr_var)))) {
649: rpc_SetErr(EINVAL, "Var=%x not found", ntohl(blob.hdr_var));
650: blob.hdr_cmd = no;
651: blob.hdr_ret = RPC_ERROR(-1);
1.7 misho 652: break;
653: } else
1.10 misho 654: blob.hdr_len = htonl(b->blob_len);
1.5 misho 655:
1.7 misho 656: if (rpc_srv_blobMap(s, b) != -1) {
657: /* deliver BLOB variable to client */
1.10 misho 658: blob.hdr_ret = htonl(rpc_srv_sendBLOB(c, b));
1.7 misho 659: rpc_srv_blobUnmap(b);
660: } else {
1.10 misho 661: blob.hdr_cmd = error;
662: blob.hdr_ret = RPC_ERROR(-1);
1.7 misho 663: }
664: break;
665: case set:
1.17 misho 666: if ((b = rpc_srv_registerBLOB(s, ntohl(blob.hdr_len),
667: ntohl(blob.hdr_ret)))) {
1.7 misho 668: /* set new BLOB variable for reply :) */
1.10 misho 669: blob.hdr_var = htonl(b->blob_var);
1.7 misho 670:
671: /* receive BLOB from client */
1.10 misho 672: blob.hdr_ret = htonl(rpc_srv_recvBLOB(c, b));
1.7 misho 673: rpc_srv_blobUnmap(b);
1.5 misho 674: } else {
1.10 misho 675: blob.hdr_cmd = error;
676: blob.hdr_ret = RPC_ERROR(-1);
1.7 misho 677: }
678: break;
679: case unset:
1.11 misho 680: if (rpc_srv_unregisterBLOB(s, ntohl(blob.hdr_var)) == -1) {
1.10 misho 681: blob.hdr_cmd = error;
682: blob.hdr_ret = RPC_ERROR(-1);
1.1 misho 683: }
684: break;
1.7 misho 685: default:
1.10 misho 686: rpc_SetErr(EPROCUNAVAIL, "Unsupported BLOB command %d", blob.hdr_cmd);
687: blob.hdr_cmd = error;
688: blob.hdr_ret = RPC_ERROR(-1);
1.7 misho 689: }
1.1 misho 690:
1.7 misho 691: end:
1.10 misho 692: memcpy(AIT_ADDR(&c->cli_buf), &blob, sizeof blob);
693: schedWrite(TASK_ROOT(task), txBLOB, TASK_ARG(task), TASK_FD(task), NULL, 0);
694: schedReadSelf(task);
1.7 misho 695: return NULL;
1.2 misho 696: }
697:
698: static void *
1.17 misho 699: flushBLOB(sched_task_t *task)
700: {
701: rpc_srv_t *srv = TASK_ARG(task);
702: rpc_blob_t *b, *tmp;
703:
704: TAILQ_FOREACH_SAFE(b, &srv->srv_blob.blobs, blob_node, tmp) {
705: TAILQ_REMOVE(&srv->srv_blob.blobs, b, blob_node);
706:
707: rpc_srv_blobFree(srv, b);
708: e_free(b);
709: }
710:
711: schedSignalSelf(task);
712: return NULL;
713: }
714:
715: static void *
1.10 misho 716: acceptBLOBClients(sched_task_t *task)
1.2 misho 717: {
1.10 misho 718: rpc_srv_t *srv = TASK_ARG(task);
719: rpc_cli_t *c = NULL;
720: register int i;
1.14 misho 721: socklen_t salen = sizeof(sockaddr_t);
1.20.4.1 misho 722: int sock;
1.12 misho 723: #ifdef TCP_NOPUSH
724: int n = 1;
725: #endif
1.7 misho 726:
1.10 misho 727: /* check free slots for connect */
1.14 misho 728: for (i = 0; i < array_Size(srv->srv_blob.clients) &&
729: (c = array(srv->srv_blob.clients, i, rpc_cli_t*)); i++);
1.20.4.1 misho 730: if (c) { /* no more free slots! */
1.20.4.2! misho 731: EVERBOSE(1, "BLOB client quota exceeded! Connection will be shutdown!\n");
1.20.4.1 misho 732: if ((sock = accept(TASK_FD(task), NULL, NULL)) != -1) {
733: shutdown(sock, SHUT_RDWR);
734: close(sock);
735: }
1.10 misho 736: goto end;
1.20.4.1 misho 737: }
738:
1.14 misho 739: c = e_malloc(sizeof(rpc_cli_t));
1.10 misho 740: if (!c) {
1.7 misho 741: LOGERR;
1.10 misho 742: srv->srv_kill = srv->srv_blob.kill = 1;
1.7 misho 743: return NULL;
744: } else {
1.10 misho 745: memset(c, 0, sizeof(rpc_cli_t));
1.14 misho 746: array_Set(srv->srv_blob.clients, i, c);
1.10 misho 747: c->cli_id = i;
748: c->cli_parent = srv;
1.7 misho 749: }
1.4 misho 750:
1.10 misho 751: /* alloc empty buffer */
1.14 misho 752: AIT_SET_BUFSIZ(&c->cli_buf, 0, srv->srv_netbuf);
1.2 misho 753:
1.10 misho 754: /* accept client */
755: c->cli_sock = accept(TASK_FD(task), &c->cli_sa.sa, &salen);
756: if (c->cli_sock == -1) {
757: LOGERR;
758: AIT_FREE_VAL(&c->cli_buf);
1.14 misho 759: array_Del(srv->srv_blob.clients, i, 42);
1.10 misho 760: goto end;
1.12 misho 761: } else {
762: #ifdef TCP_NOPUSH
763: setsockopt(c->cli_sock, IPPROTO_TCP, TCP_NOPUSH, &n, sizeof n);
764: #endif
1.10 misho 765: fcntl(c->cli_sock, F_SETFL, fcntl(c->cli_sock, F_GETFL) | O_NONBLOCK);
1.12 misho 766: }
1.2 misho 767:
1.10 misho 768: schedRead(TASK_ROOT(task), rxBLOB, c, c->cli_sock, NULL, 0);
769: end:
770: schedReadSelf(task);
1.7 misho 771: return NULL;
1.1 misho 772: }
773:
1.10 misho 774: /* ------------------------------------------------------ */
1.1 misho 775:
776: /*
1.7 misho 777: * rpc_srv_initBLOBServer() - Init & create BLOB Server
778: *
1.4 misho 779: * @srv = RPC server instance
1.2 misho 780: * @Port = Port for bind server, if Port == 0 default port is selected
781: * @diskDir = Disk place for BLOB file objects
782: * return: -1 == error or 0 bind and created BLOB server instance
783: */
784: int
785: rpc_srv_initBLOBServer(rpc_srv_t * __restrict srv, u_short Port, const char *diskDir)
786: {
787: int n = 1;
788:
1.10 misho 789: if (!srv || srv->srv_kill) {
1.7 misho 790: rpc_SetErr(EINVAL, "Invalid parameters can`t init BLOB server");
1.2 misho 791: return -1;
792: }
793:
794: memset(&srv->srv_blob, 0, sizeof srv->srv_blob);
795: if (access(diskDir, R_OK | W_OK) == -1) {
796: LOGERR;
797: return -1;
798: } else
1.7 misho 799: AIT_SET_STR(&srv->srv_blob.dir, diskDir);
1.2 misho 800:
1.10 misho 801: /* init blob list */
802: TAILQ_INIT(&srv->srv_blob.blobs);
803:
1.2 misho 804: srv->srv_blob.server.cli_parent = srv;
1.4 misho 805:
1.14 misho 806: memcpy(&srv->srv_blob.server.cli_sa, &srv->srv_server.cli_sa, sizeof(sockaddr_t));
1.10 misho 807: switch (srv->srv_blob.server.cli_sa.sa.sa_family) {
1.4 misho 808: case AF_INET:
1.10 misho 809: srv->srv_blob.server.cli_sa.sin.sin_port =
810: htons(Port ? Port : ntohs(srv->srv_blob.server.cli_sa.sin.sin_port) + 1);
1.4 misho 811: break;
812: case AF_INET6:
1.10 misho 813: srv->srv_blob.server.cli_sa.sin6.sin6_port =
814: htons(Port ? Port : ntohs(srv->srv_blob.server.cli_sa.sin6.sin6_port) + 1);
1.4 misho 815: break;
816: case AF_LOCAL:
1.10 misho 817: strlcat(srv->srv_blob.server.cli_sa.sun.sun_path, ".blob",
818: sizeof srv->srv_blob.server.cli_sa.sun.sun_path);
1.4 misho 819: break;
820: default:
1.7 misho 821: AIT_FREE_VAL(&srv->srv_blob.dir);
1.4 misho 822: return -1;
1.2 misho 823: }
824:
1.4 misho 825: /* create BLOB server socket */
1.6 misho 826: srv->srv_blob.server.cli_sock = socket(srv->srv_server.cli_sa.sa.sa_family, SOCK_STREAM, 0);
1.2 misho 827: if (srv->srv_blob.server.cli_sock == -1) {
828: LOGERR;
1.7 misho 829: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 830: return -1;
831: }
832: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
833: LOGERR;
834: close(srv->srv_blob.server.cli_sock);
1.7 misho 835: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 836: return -1;
837: }
1.5 misho 838: n = srv->srv_netbuf;
839: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof n) == -1) {
840: LOGERR;
841: close(srv->srv_blob.server.cli_sock);
1.7 misho 842: AIT_FREE_VAL(&srv->srv_blob.dir);
1.5 misho 843: return -1;
844: }
845: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_RCVBUF, &n, sizeof n) == -1) {
846: LOGERR;
847: close(srv->srv_blob.server.cli_sock);
1.7 misho 848: AIT_FREE_VAL(&srv->srv_blob.dir);
1.5 misho 849: return -1;
850: }
1.6 misho 851: if (bind(srv->srv_blob.server.cli_sock, &srv->srv_blob.server.cli_sa.sa,
852: srv->srv_blob.server.cli_sa.sa.sa_len) == -1) {
1.2 misho 853: LOGERR;
854: close(srv->srv_blob.server.cli_sock);
1.7 misho 855: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 856: return -1;
1.13 misho 857: } else
858: fcntl(srv->srv_blob.server.cli_sock, F_SETFL,
859: fcntl(srv->srv_blob.server.cli_sock, F_GETFL) | O_NONBLOCK);
860:
1.2 misho 861:
1.10 misho 862: /* allocate pool for concurent blob clients */
1.14 misho 863: srv->srv_blob.clients = array_Init(array_Size(srv->srv_clients));
1.2 misho 864: if (!srv->srv_blob.clients) {
1.14 misho 865: rpc_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
1.2 misho 866: close(srv->srv_blob.server.cli_sock);
1.7 misho 867: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 868: return -1;
1.10 misho 869: }
1.2 misho 870:
1.10 misho 871: /* init blob scheduler */
872: srv->srv_blob.root = schedBegin();
873: if (!srv->srv_blob.root) {
874: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
1.14 misho 875: array_Destroy(&srv->srv_blob.clients);
1.10 misho 876: close(srv->srv_blob.server.cli_sock);
877: AIT_FREE_VAL(&srv->srv_blob.dir);
878: return -1;
879: }
1.2 misho 880:
881: return 0;
882: }
883:
884: /*
1.7 misho 885: * rpc_srv_endBLOBServer() - Destroy BLOB server, close all opened sockets and free resources
886: *
1.2 misho 887: * @srv = RPC Server instance
888: * return: none
889: */
1.16 misho 890: void
1.2 misho 891: rpc_srv_endBLOBServer(rpc_srv_t * __restrict srv)
892: {
1.10 misho 893: if (!srv)
1.2 misho 894: return;
895:
1.10 misho 896: srv->srv_blob.kill = 1;
1.17 misho 897:
898: schedEnd(&srv->srv_blob.root);
1.2 misho 899: }
900:
901: /*
1.10 misho 902: * rpc_srv_loopBLOBServer() - Execute Main BLOB server loop and wait for clients requests
1.7 misho 903: *
1.2 misho 904: * @srv = RPC Server instance
905: * return: -1 error or 0 ok, infinite loop ...
906: */
907: int
1.10 misho 908: rpc_srv_loopBLOBServer(rpc_srv_t * __restrict srv)
1.2 misho 909: {
1.10 misho 910: rpc_cli_t *c;
1.2 misho 911: register int i;
1.10 misho 912: rpc_blob_t *b, *tmp;
913: struct timespec ts = { RPC_SCHED_POLLING, 0 };
1.2 misho 914:
1.10 misho 915: if (!srv || srv->srv_kill) {
1.7 misho 916: rpc_SetErr(EINVAL, "Invalid parameter can`t start BLOB server");
1.2 misho 917: return -1;
918: }
919:
1.14 misho 920: if (listen(srv->srv_blob.server.cli_sock, array_Size(srv->srv_blob.clients)) == -1) {
1.2 misho 921: LOGERR;
922: return -1;
1.10 misho 923: }
924:
1.17 misho 925: schedSignal(srv->srv_blob.root, flushBLOB, srv, SIGFBLOB, NULL, 0);
1.10 misho 926: if (!schedRead(srv->srv_blob.root, acceptBLOBClients, srv,
927: srv->srv_blob.server.cli_sock, NULL, 0)) {
928: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
929: return -1;
930: }
1.2 misho 931:
1.10 misho 932: schedPolling(srv->srv_blob.root, &ts, NULL);
933: /* main rpc loop */
934: schedRun(srv->srv_blob.root, &srv->srv_blob.kill);
1.7 misho 935:
1.17 misho 936: /* detach blobs */
937: TAILQ_FOREACH_SAFE(b, &srv->srv_blob.blobs, blob_node, tmp) {
938: TAILQ_REMOVE(&srv->srv_blob.blobs, b, blob_node);
939:
940: rpc_srv_blobFree(srv, b);
941: e_free(b);
942: }
943:
1.10 misho 944: /* close all clients connections & server socket */
1.14 misho 945: for (i = 0; i < array_Size(srv->srv_blob.clients); i++) {
946: c = array(srv->srv_blob.clients, i, rpc_cli_t*);
1.10 misho 947: if (c) {
948: shutdown(c->cli_sock, SHUT_RDWR);
949: close(c->cli_sock);
950:
951: schedCancelby(srv->srv_blob.root, taskMAX, CRITERIA_ARG, c, NULL);
952: AIT_FREE_VAL(&c->cli_buf);
1.2 misho 953: }
1.14 misho 954: array_Del(srv->srv_blob.clients, i, 42);
1.10 misho 955: }
1.14 misho 956: array_Destroy(&srv->srv_blob.clients);
1.2 misho 957:
1.10 misho 958: close(srv->srv_blob.server.cli_sock);
1.2 misho 959:
1.10 misho 960: AIT_FREE_VAL(&srv->srv_blob.dir);
1.2 misho 961: return 0;
962: }
963:
964:
965: /*
1.7 misho 966: * rpc_srv_initServer() - Init & create RPC Server
967: *
1.15 misho 968: * @InstID = Instance for authentication & recognition
1.1 misho 969: * @concurentClients = Concurent clients at same time to this server
1.10 misho 970: * @netBuf = Network buffer length (min:512 bytes), if =0 == BUFSIZ (also meaning max RPC packet)
1.4 misho 971: * @csHost = Host name or address for bind server, if NULL any address
1.1 misho 972: * @Port = Port for bind server, if Port == 0 default port is selected
1.13 misho 973: * @proto = Protocol, if == 0 choose SOCK_STREAM
1.1 misho 974: * return: NULL == error or !=NULL bind and created RPC server instance
975: */
976: rpc_srv_t *
1.15 misho 977: rpc_srv_initServer(u_char InstID, int concurentClients, int netBuf,
978: const char *csHost, u_short Port, int proto)
1.1 misho 979: {
1.10 misho 980: int n = 1;
1.1 misho 981: rpc_srv_t *srv = NULL;
1.14 misho 982: sockaddr_t sa = E_SOCKADDR_INIT;
1.1 misho 983:
1.15 misho 984: if (!concurentClients || (proto < 0 || proto > SOCK_DGRAM)) {
1.10 misho 985: rpc_SetErr(EINVAL, "Invalid parameters can`t init RPC server");
1.1 misho 986: return NULL;
987: }
1.14 misho 988: if (!e_gethostbyname(csHost, Port, &sa))
1.10 misho 989: return NULL;
1.1 misho 990: if (!Port)
991: Port = RPC_DEFPORT;
1.13 misho 992: if (!proto)
993: proto = SOCK_STREAM;
1.10 misho 994: if (netBuf < RPC_MIN_BUFSIZ)
1.5 misho 995: netBuf = BUFSIZ;
1.7 misho 996: else
1.14 misho 997: netBuf = E_ALIGN(netBuf, 2); /* align netBuf length */
1.10 misho 998:
999: #ifdef HAVE_SRANDOMDEV
1000: srandomdev();
1001: #else
1002: time_t tim;
1003:
1004: srandom((time(&tim) ^ getpid()));
1005: #endif
1.1 misho 1006:
1.14 misho 1007: srv = e_malloc(sizeof(rpc_srv_t));
1.1 misho 1008: if (!srv) {
1009: LOGERR;
1010: return NULL;
1011: } else
1012: memset(srv, 0, sizeof(rpc_srv_t));
1013:
1.13 misho 1014: srv->srv_proto = proto;
1.5 misho 1015: srv->srv_netbuf = netBuf;
1.1 misho 1016: srv->srv_session.sess_version = RPC_VERSION;
1.15 misho 1017: srv->srv_session.sess_instance = InstID;
1.1 misho 1018:
1019: srv->srv_server.cli_parent = srv;
1.10 misho 1020: memcpy(&srv->srv_server.cli_sa, &sa, sizeof srv->srv_server.cli_sa);
1021:
1.12 misho 1022: /* init functions */
1023: pthread_mutex_init(&srv->srv_funcs.mtx, NULL);
1024: SLIST_INIT(&srv->srv_funcs);
1025: AVL_INIT(&srv->srv_funcs);
1.10 misho 1026:
1027: /* init scheduler */
1028: srv->srv_root = schedBegin();
1029: if (!srv->srv_root) {
1030: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
1.12 misho 1031: pthread_mutex_destroy(&srv->srv_funcs.mtx);
1.14 misho 1032: e_free(srv);
1.10 misho 1033: return NULL;
1034: }
1035:
1036: /* init pool for clients */
1.14 misho 1037: srv->srv_clients = array_Init(concurentClients);
1.10 misho 1038: if (!srv->srv_clients) {
1.14 misho 1039: rpc_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
1.10 misho 1040: schedEnd(&srv->srv_root);
1.12 misho 1041: pthread_mutex_destroy(&srv->srv_funcs.mtx);
1.14 misho 1042: e_free(srv);
1.10 misho 1043: return NULL;
1044: }
1.4 misho 1045:
1046: /* create server socket */
1.13 misho 1047: srv->srv_server.cli_sock = socket(srv->srv_server.cli_sa.sa.sa_family, srv->srv_proto, 0);
1.1 misho 1048: if (srv->srv_server.cli_sock == -1) {
1049: LOGERR;
1.14 misho 1050: array_Destroy(&srv->srv_clients);
1.10 misho 1051: schedEnd(&srv->srv_root);
1.12 misho 1052: pthread_mutex_destroy(&srv->srv_funcs.mtx);
1.14 misho 1053: e_free(srv);
1.1 misho 1054: return NULL;
1055: }
1056: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
1057: LOGERR;
1.10 misho 1058: goto err;
1.1 misho 1059: }
1.5 misho 1060: n = srv->srv_netbuf;
1061: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof n) == -1) {
1062: LOGERR;
1.10 misho 1063: goto err;
1.5 misho 1064: }
1065: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_RCVBUF, &n, sizeof n) == -1) {
1066: LOGERR;
1.10 misho 1067: goto err;
1.5 misho 1068: }
1.6 misho 1069: if (bind(srv->srv_server.cli_sock, &srv->srv_server.cli_sa.sa,
1070: srv->srv_server.cli_sa.sa.sa_len) == -1) {
1.1 misho 1071: LOGERR;
1.10 misho 1072: goto err;
1.13 misho 1073: } else
1074: fcntl(srv->srv_server.cli_sock, F_SETFL,
1075: fcntl(srv->srv_server.cli_sock, F_GETFL) | O_NONBLOCK);
1.1 misho 1076:
1.10 misho 1077: rpc_register_srvPing(srv);
1.8 misho 1078:
1.1 misho 1079: return srv;
1.10 misho 1080: err: /* error condition */
1081: close(srv->srv_server.cli_sock);
1.14 misho 1082: array_Destroy(&srv->srv_clients);
1.10 misho 1083: schedEnd(&srv->srv_root);
1.12 misho 1084: pthread_mutex_destroy(&srv->srv_funcs.mtx);
1.14 misho 1085: e_free(srv);
1.10 misho 1086: return NULL;
1.1 misho 1087: }
1088:
1089: /*
1.7 misho 1090: * rpc_srv_endServer() - Destroy RPC server, close all opened sockets and free resources
1091: *
1.6 misho 1092: * @psrv = RPC Server instance
1.1 misho 1093: * return: none
1094: */
1.16 misho 1095: void
1.6 misho 1096: rpc_srv_endServer(rpc_srv_t ** __restrict psrv)
1.1 misho 1097: {
1.10 misho 1098: if (!psrv || !*psrv)
1.1 misho 1099: return;
1100:
1.10 misho 1101: /* if send kill to blob server */
1.17 misho 1102: rpc_srv_endBLOBServer(*psrv);
1.1 misho 1103:
1.10 misho 1104: (*psrv)->srv_kill = 1;
1105: sleep(RPC_SCHED_POLLING);
1.2 misho 1106:
1.17 misho 1107: schedEnd(&(*psrv)->srv_root);
1108:
1.12 misho 1109: pthread_mutex_destroy(&(*psrv)->srv_funcs.mtx);
1.14 misho 1110: e_free(*psrv);
1.6 misho 1111: *psrv = NULL;
1.1 misho 1112: }
1113:
1114: /*
1.7 misho 1115: * rpc_srv_loopServer() - Execute Main server loop and wait for clients requests
1116: *
1.1 misho 1117: * @srv = RPC Server instance
1118: * return: -1 error or 0 ok, infinite loop ...
1119: */
1120: int
1.5 misho 1121: rpc_srv_loopServer(rpc_srv_t * __restrict srv)
1.1 misho 1122: {
1.10 misho 1123: rpc_cli_t *c;
1.1 misho 1124: register int i;
1.12 misho 1125: rpc_func_t *f;
1.10 misho 1126: struct timespec ts = { RPC_SCHED_POLLING, 0 };
1.1 misho 1127:
1128: if (!srv) {
1.10 misho 1129: rpc_SetErr(EINVAL, "Invalid parameter can`t start RPC server");
1.1 misho 1130: return -1;
1131: }
1132:
1.13 misho 1133: if (srv->srv_proto == SOCK_STREAM)
1.14 misho 1134: if (listen(srv->srv_server.cli_sock, array_Size(srv->srv_clients)) == -1) {
1.13 misho 1135: LOGERR;
1136: return -1;
1137: }
1.1 misho 1138:
1.13 misho 1139: if (!schedRead(srv->srv_root, cbProto[srv->srv_proto][CB_ACCEPTCLIENT], srv,
1140: srv->srv_server.cli_sock, NULL, 0)) {
1.10 misho 1141: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
1142: return -1;
1143: }
1.7 misho 1144:
1.10 misho 1145: schedPolling(srv->srv_root, &ts, NULL);
1.7 misho 1146: /* main rpc loop */
1.10 misho 1147: schedRun(srv->srv_root, &srv->srv_kill);
1148:
1149: /* close all clients connections & server socket */
1.14 misho 1150: for (i = 0; i < array_Size(srv->srv_clients); i++) {
1151: c = array(srv->srv_clients, i, rpc_cli_t*);
1.10 misho 1152: if (c) {
1153: shutdown(c->cli_sock, SHUT_RDWR);
1154: close(c->cli_sock);
1155:
1156: schedCancelby(srv->srv_root, taskMAX, CRITERIA_ARG, c, NULL);
1.14 misho 1157: ait_freeVars(&RPC_RETVARS(c));
1.10 misho 1158: AIT_FREE_VAL(&c->cli_buf);
1.1 misho 1159: }
1.14 misho 1160: array_Del(srv->srv_clients, i, 42);
1.10 misho 1161: }
1.14 misho 1162: array_Destroy(&srv->srv_clients);
1.2 misho 1163:
1.10 misho 1164: close(srv->srv_server.cli_sock);
1.2 misho 1165:
1.10 misho 1166: /* detach exported calls */
1.12 misho 1167: RPC_FUNCS_LOCK(&srv->srv_funcs);
1168: while ((f = SLIST_FIRST(&srv->srv_funcs))) {
1169: SLIST_REMOVE_HEAD(&srv->srv_funcs, func_next);
1.1 misho 1170:
1.10 misho 1171: AIT_FREE_VAL(&f->func_name);
1.14 misho 1172: e_free(f);
1.1 misho 1173: }
1.12 misho 1174: srv->srv_funcs.avlh_root = NULL;
1175: RPC_FUNCS_UNLOCK(&srv->srv_funcs);
1.1 misho 1176:
1177: return 0;
1178: }
1179:
1180:
1181: /*
1182: * rpc_srv_execCall() Execute registered call from RPC server
1.7 misho 1183: *
1.10 misho 1184: * @cli = RPC client
1.1 misho 1185: * @rpc = IN RPC call structure
1.10 misho 1186: * @funcname = Execute RPC function
1.5 misho 1187: * @args = IN RPC calling arguments from RPC client
1.1 misho 1188: * return: -1 error, !=-1 ok
1189: */
1190: int
1.10 misho 1191: rpc_srv_execCall(rpc_cli_t * __restrict cli, struct tagRPCCall * __restrict rpc,
1192: ait_val_t funcname, array_t * __restrict args)
1.1 misho 1193: {
1194: rpc_callback_t func;
1195:
1.10 misho 1196: if (!cli || !rpc || !AIT_ADDR(&funcname)) {
1.7 misho 1197: rpc_SetErr(EINVAL, "Invalid parameter can`t exec function");
1.1 misho 1198: return -1;
1199: }
1200:
1.10 misho 1201: func = AIT_GET_LIKE(&funcname, rpc_callback_t);
1202: return func(cli, rpc, args);
1.1 misho 1203: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>