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