1: /*************************************************************************
2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
3: * by Michael Pounov <misho@openbsd-bg.org>
4: *
5: * $Author: misho $
6: * $Id: cli.c,v 1.16.8.4 2013/08/22 11:55:45 misho Exp $
7: *
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:
15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
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: */
46: #include "global.h"
47:
48:
49: /*
50: * rpc_cli_openBLOBClient() - Connect to BLOB Server
51: *
52: * @rpccli = RPC Client session
53: * @Port = Port for bind server, if Port == 0 default port is selected
54: * return: NULL == error or !=NULL connection to BLOB server established
55: */
56: rpc_cli_t *
57: rpc_cli_openBLOBClient(rpc_cli_t * __restrict rpccli, u_short Port)
58: {
59: rpc_cli_t *cli = NULL;
60: int n;
61:
62: if (!rpccli) {
63: rpc_SetErr(EINVAL, "Invalid parameters can`t connect to BLOB server");
64: return NULL;
65: }
66:
67: cli = e_malloc(sizeof(rpc_cli_t));
68: if (!cli) {
69: LOGERR;
70: return NULL;
71: } else
72: memcpy(cli, rpccli, sizeof(rpc_cli_t));
73:
74: memcpy(&cli->cli_sa, &rpccli->cli_sa, sizeof(sockaddr_t));
75: switch (cli->cli_sa.sa.sa_family) {
76: case AF_INET:
77: cli->cli_sa.sin.sin_port =
78: htons(Port ? Port : ntohs(cli->cli_sa.sin.sin_port) + 1);
79: break;
80: case AF_INET6:
81: cli->cli_sa.sin6.sin6_port =
82: htons(Port ? Port : ntohs(cli->cli_sa.sin6.sin6_port) + 1);
83: break;
84: case AF_LOCAL:
85: strlcat(cli->cli_sa.sun.sun_path, ".blob", sizeof cli->cli_sa.sun.sun_path);
86: break;
87: default:
88: rpc_SetErr(EINVAL, "Invalid socket type %d", cli->cli_sa.sa.sa_family);
89: return NULL;
90: }
91:
92: AIT_COPY_VAL(&cli->cli_buf, &rpccli->cli_buf);
93: n = AIT_LEN(&cli->cli_buf);
94:
95: /* connect to BLOB server */
96: cli->cli_sock = socket(cli->cli_sa.sa.sa_family, SOCK_STREAM, 0);
97: if (cli->cli_sock == -1) {
98: LOGERR;
99: e_free(cli);
100: return NULL;
101: }
102: if (setsockopt(cli->cli_sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof n) == -1) {
103: LOGERR;
104: close(cli->cli_sock);
105: e_free(cli);
106: return NULL;
107: }
108: if (setsockopt(cli->cli_sock, SOL_SOCKET, SO_RCVBUF, &n, sizeof n) == -1) {
109: LOGERR;
110: close(cli->cli_sock);
111: e_free(cli);
112: return NULL;
113: }
114: if (connect(cli->cli_sock, &cli->cli_sa.sa, cli->cli_sa.sa.sa_len) == -1) {
115: LOGERR;
116: close(cli->cli_sock);
117: e_free(cli);
118: return NULL;
119: } else
120: fcntl(cli->cli_sock, F_SETFL, fcntl(cli->cli_sock, F_GETFL) | O_NONBLOCK);
121:
122: return cli;
123: }
124:
125: /*
126: * rpc_cli_closeBLOBClient() - Close connection to BLOB server and free resources
127: *
128: * @cli = BLOB Client session
129: * return: none
130: */
131: void
132: rpc_cli_closeBLOBClient(rpc_cli_t ** __restrict cli)
133: {
134: if (!cli || !*cli)
135: return;
136:
137: shutdown((*cli)->cli_sock, SHUT_RDWR);
138: close((*cli)->cli_sock);
139:
140: AIT_FREE_VAL(&(*cli)->cli_buf);
141:
142: e_free(*cli);
143: *cli = NULL;
144: }
145:
146: /* -------------------------------------------------------------- */
147:
148: /*
149: * rpc_cli_openClient() - Connect to RPC Server
150: *
151: * @InstID = InstID for RPC session request
152: * @netBuf = Network buffer length (min:512 bytes), if =0 == BUFSIZ (also meaning max RPC packet)
153: * @csHost = Host name or IP address for bind server
154: * @Port = Port for bind server, if Port == 0 default port is selected
155: * @proto = Protocol, if == 0 choose SOCK_STREAM
156: * return: NULL == error or !=NULL connection to RPC server established
157: */
158: rpc_cli_t *
159: rpc_cli_openClient(u_char InstID, int netBuf, const char *csHost, u_short Port, int proto)
160: {
161: rpc_cli_t *cli = NULL;
162: sockaddr_t sa = E_SOCKADDR_INIT;
163:
164: if (!e_gethostbyname(csHost, Port, &sa))
165: return NULL;
166: if (!Port)
167: Port = RPC_DEFPORT;
168: if (!proto)
169: proto = SOCK_STREAM;
170: if (netBuf < RPC_MIN_BUFSIZ)
171: netBuf = BUFSIZ;
172: else
173: netBuf = E_ALIGN(netBuf, 2); /* align netBuf length */
174:
175: cli = e_malloc(sizeof(rpc_cli_t));
176: if (!cli) {
177: LOGERR;
178: return NULL;
179: } else
180: memset(cli, 0, sizeof(rpc_cli_t));
181:
182: /* build session */
183: cli->cli_parent = e_malloc(sizeof(rpc_sess_t));
184: if (!cli->cli_parent) {
185: LOGERR;
186: e_free(cli);
187: return NULL;
188: } else {
189: ((rpc_sess_t*) cli->cli_parent)->sess_version = RPC_VERSION;
190: ((rpc_sess_t*) cli->cli_parent)->sess_instance = InstID;
191: }
192:
193: cli->cli_id = proto;
194: memcpy(&cli->cli_sa, &sa, sizeof cli->cli_sa);
195: AIT_SET_BUFSIZ(&cli->cli_buf, 0, netBuf);
196:
197: /* connect to RPC server */
198: cli->cli_sock = socket(cli->cli_sa.sa.sa_family, cli->cli_id, 0);
199: if (cli->cli_sock == -1) {
200: LOGERR;
201: goto err;
202: }
203: if (setsockopt(cli->cli_sock, SOL_SOCKET, SO_SNDBUF,
204: &netBuf, sizeof netBuf) == -1) {
205: LOGERR;
206: goto err;
207: }
208: if (setsockopt(cli->cli_sock, SOL_SOCKET, SO_RCVBUF,
209: &netBuf, sizeof netBuf) == -1) {
210: LOGERR;
211: goto err;
212: }
213: if (cli->cli_id == SOCK_STREAM)
214: if (connect(cli->cli_sock, &cli->cli_sa.sa, cli->cli_sa.sa.sa_len) == -1) {
215: LOGERR;
216: goto err;
217: }
218:
219: fcntl(cli->cli_sock, F_SETFL, fcntl(cli->cli_sock, F_GETFL) | O_NONBLOCK);
220: return cli;
221: err:
222: AIT_FREE_VAL(&cli->cli_buf);
223: if (cli->cli_sock > 2)
224: close(cli->cli_sock);
225: e_free(cli->cli_parent);
226: e_free(cli);
227: return NULL;
228: }
229:
230: /*
231: * rpc_cli_closeClient() - Close connection to RPC server and free resources
232: *
233: * @cli = RPC Client session
234: * return: none
235: */
236: void
237: rpc_cli_closeClient(rpc_cli_t ** __restrict cli)
238: {
239: if (!cli || !*cli)
240: return;
241:
242: if ((*cli)->cli_id == SOCK_STREAM)
243: shutdown((*cli)->cli_sock, SHUT_RDWR);
244: close((*cli)->cli_sock);
245:
246: AIT_FREE_VAL(&(*cli)->cli_buf);
247:
248: if ((*cli)->cli_parent)
249: e_free((*cli)->cli_parent);
250:
251: e_free(*cli);
252: *cli = NULL;
253: }
254:
255: /*
256: * rpc_pkt_Send() - Send RPC packet
257: *
258: * @sock = Socket
259: * @type = Type of socket
260: * @sa = Server address
261: * @pkt = RPC packet
262: * @len = Length of packet
263: * return: -1 error or !=-1 sended bytes
264: */
265: int
266: rpc_pkt_Send(int sock, int type, sockaddr_t * __restrict sa, ait_val_t * __restrict pkt, int len)
267: {
268: struct pollfd pfd;
269: int ret;
270: u_char *buf;
271:
272: if (!pkt) {
273: rpc_SetErr(EINVAL, "Invalid argument(s)!");
274: return -1;
275: } else
276: buf = AIT_GET_BUF(pkt);
277:
278: pfd.fd = sock;
279: pfd.events = POLLOUT;
280: if ((ret = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) < 1 ||
281: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
282: if (ret)
283: LOGERR;
284: else
285: rpc_SetErr(ETIMEDOUT, "Timeout, can't send to RPC server");
286: return -1;
287: }
288: do {
289: if (type == SOCK_STREAM)
290: ret = send(sock, buf, len, MSG_NOSIGNAL);
291: else if (sa)
292: ret = sendto(sock, buf, len, MSG_NOSIGNAL, &sa->sa, sa->sa.sa_len);
293: else {
294: rpc_SetErr(EINVAL, "Invalid argument(s)!");
295: return -1;
296: }
297: if (ret == -1) {
298: if (errno == EAGAIN)
299: continue;
300: LOGERR;
301: return -1;
302: } else if (ret != len) {
303: rpc_SetErr(EPROCUNAVAIL, "RPC request, should be send %d bytes, "
304: "really sended %d bytes", len, ret);
305: return -1;
306: }
307: } while (0);
308:
309: return ret;
310: }
311:
312: /*
313: * rpc_pkt_Receive() - Receive RPC packet
314: *
315: * @sock = Socket
316: * @type = Type of socket
317: * @sa = Server address
318: * @pkt = RPC packet
319: * return: -1 error or !=-1 sended bytes
320: */
321: int
322: rpc_pkt_Receive(int sock, int type, sockaddr_t * __restrict sa, ait_val_t * __restrict pkt)
323: {
324: struct pollfd pfd;
325: int ret, estlen, blen = sizeof(struct tagRPCCall), len = 0;
326: u_char *buf;
327: sockaddr_t sa2;
328: socklen_t salen;
329: struct tagRPCCall *rpc;
330:
331: if (!pkt) {
332: rpc_SetErr(EINVAL, "Invalid argument(s)!");
333: return -1;
334: } else
335: buf = AIT_GET_BUF(pkt);
336:
337: /* reply from RPC server */
338: pfd.fd = sock;
339: pfd.events = POLLIN | POLLPRI;
340: do {
341: if ((ret = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) < 1 ||
342: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
343: if (ret) {
344: LOGERR;
345: } else {
346: if (len++ < 7)
347: continue;
348: else
349: rpc_SetErr(ETIMEDOUT,
350: "Timeout, no answer from RPC server");
351: }
352:
353: return -1;
354: }
355:
356: if (type == SOCK_STREAM) {
357: memset(buf, 0, blen);
358: ret = recv(sock, buf, blen, 0);
359: } else {
360: blen = AIT_LEN(pkt);
361: memset(buf, 0, blen);
362: memset(&sa2, 0, sizeof sa2);
363: salen = sa2.ss.ss_len = sizeof(sockaddr_t);
364: ret = recvfrom(sock, buf, blen, 0, &sa2.sa, &salen);
365: }
366: if (ret < 1) {
367: if (ret) {
368: if (errno == EAGAIN)
369: continue;
370: else
371: LOGERR;
372: }
373: return -1;
374: }
375:
376: /* check for response from known address */
377: if (type == SOCK_DGRAM) {
378: if (e_addrcmp(sa, &sa2, 42)) {
379: rpc_SetErr(ERPCMISMATCH,
380: "Received RPC response from unknown address");
381: continue;
382: }
383: } else {
384: /* 1st read for RPC header */
385: if (buf == AIT_GET_BUF(pkt)) {
386: if (ret < sizeof(struct tagRPCCall)) {
387: rpc_SetErr(ERPCMISMATCH, "Short RPC packet %d bytes", ret);
388: return -1;
389: }
390:
391: /* calc estimated length */
392: rpc = (struct tagRPCCall*) buf;
393: estlen = ntohl(rpc->call_len) + blen;
394: printf("ret = %d len=%d\n", ret, estlen);
395: if (estlen > AIT_LEN(pkt))
396: AIT_RE_BUF(pkt, estlen);
397: buf = AIT_GET_BUF(pkt) + blen;
398: blen = AIT_LEN(pkt) - blen;
399: } else
400: ret += sizeof(struct tagRPCCall);
401: }
402: } while (0);
403:
404: if (ret < sizeof(struct tagRPCCall)) {
405: rpc_SetErr(ERPCMISMATCH, "Short RPC packet %d bytes", ret);
406: return -1;
407: }
408:
409: return ret;
410: }
411:
412: /*
413: * rpc_pkt_Request() - Build RPC Request packet
414: *
415: * @pkt = Packet buffer
416: * @sess = RPC session info
417: * @tag = Function tag for execution
418: * @vars = Function argument array of values, may be NULL
419: * @noreply = We not want RPC reply
420: * @nocrc = Without CRC calculation
421: * return: -1 error or != -1 prepared bytes into packet
422: */
423: int
424: rpc_pkt_Request(ait_val_t * __restrict pkt, rpc_sess_t * __restrict sess, u_short tag,
425: array_t * __restrict vars, int noreply, int nocrc)
426: {
427: struct tagRPCCall *rpc;
428: int ret = 0, estlen, len = sizeof(struct tagRPCCall);
429: u_char *buf;
430:
431: if (!pkt || !sess) {
432: rpc_SetErr(EINVAL, "Invalid argument(s)!");
433: return -1;
434: }
435:
436: /* calc estimated length */
437: estlen = ait_resideVars(vars) + len;
438: if (estlen > AIT_LEN(pkt))
439: AIT_RE_BUF(pkt, estlen);
440: buf = AIT_GET_BUF(pkt);
441:
442: /* prepare RPC call */
443: rpc = (struct tagRPCCall*) buf;
444: rpc_addPktSession(&rpc->call_session, sess);
445: rpc->call_tag = htons(tag);
446: if (!vars)
447: rpc->call_argc = 0;
448: else
449: rpc->call_argc = htons(array_Size(vars));
450:
451: /* set reply */
452: rpc->call_req.flags = noreply ? RPC_NOREPLY : RPC_REPLY;
453:
454: if (array_Size(vars)) {
455: /* marshaling variables */
456: ret = ait_vars2buffer(buf + len, AIT_LEN(pkt) - len, vars);
457: if (ret == -1) {
458: rpc_SetErr(EBADRPC, "Failed to prepare RPC packet values");
459: return -1;
460: } else
461: len += ret;
462: }
463:
464: /* total packet length */
465: rpc->call_len = htonl(len);
466:
467: if (!nocrc) {
468: /* calculate CRC */
469: rpc->call_crc ^= rpc->call_crc;
470: rpc->call_crc = htons(crcFletcher16((u_short*) buf, len / 2));
471: }
472:
473: return len;
474: }
475:
476: /*
477: * rpc_pkt_Replay() - Decode RPC Replay packet
478: *
479: * @pkt = Packet buffer
480: * @sess = RPC session info
481: * @tag = Function tag
482: * @vars = Function argument array of values, may be NULL
483: * @nocrc = Without CRC calculation
484: * return: -1 error or != -1 return value from function
485: */
486: int
487: rpc_pkt_Replay(ait_val_t * __restrict pkt, rpc_sess_t * __restrict sess, u_short tag,
488: array_t ** __restrict vars, int nocrc)
489: {
490: struct tagRPCCall *rpc;
491: int len;
492: u_char *buf;
493: uint16_t crc;
494:
495: if (!pkt || !sess) {
496: rpc_SetErr(EINVAL, "Invalid argument(s)!");
497: return -1;
498: } else
499: buf = AIT_GET_BUF(pkt);
500:
501: rpc = (struct tagRPCCall*) buf;
502: if (!nocrc) {
503: /* calculate CRC */
504: crc = ntohs(rpc->call_crc);
505: rpc->call_crc ^= rpc->call_crc;
506: if (crc != crcFletcher16((u_short*) buf, ntohl(rpc->call_len) / 2)) {
507: rpc_SetErr(ERPCMISMATCH, "Bad CRC RPC packet");
508: return -1;
509: }
510: }
511:
512: /* check RPC packet session info */
513: if (rpc_chkPktSession(&rpc->call_session, sess)) {
514: rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session");
515: return -1;
516: }
517: if (ntohs(rpc->call_tag) != tag) {
518: rpc_SetErr(ERPCMISMATCH, "Get wrong RPC reply");
519: return -1;
520: }
521: if (ntohl(rpc->call_rep.eno) && ntohl(rpc->call_rep.ret) == -1) {
522: rpc_SetErr(ntohl(rpc->call_rep.eno), "Server side: retcode=%d #%d %s",
523: ntohl(rpc->call_rep.ret), ntohl(rpc->call_rep.eno),
524: strerror(ntohl(rpc->call_rep.eno)));
525: return -1;
526: }
527: len = ntohs(rpc->call_argc) * sizeof(ait_val_t);
528: if (len > AIT_LEN(pkt) - sizeof(struct tagRPCCall)) {
529: rpc_SetErr(EMSGSIZE, "Reply RPC packet not enough buffer space ...");
530: return -1;
531: }
532: if (len > ntohl(rpc->call_len) - sizeof(struct tagRPCCall)) {
533: rpc_SetErr(EMSGSIZE, "Reply RPC packet is too short ...");
534: return -1;
535: }
536:
537: /* RPC is OK! Go de-marshaling variables ... */
538: if (vars && ntohs(rpc->call_argc)) {
539: #ifdef CLI_RES_ZCOPY
540: *vars = ait_buffer2vars(buf + sizeof(struct tagRPCCall), len,
541: ntohs(rpc->call_argc), 42);
542: #else
543: *vars = ait_buffer2vars(buf + sizeof(struct tagRPCCall), len,
544: ntohs(rpc->call_argc), 0);
545: #endif
546: if (!*vars) {
547: rpc_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
548: return -1;
549: }
550: }
551:
552: return ntohl(rpc->call_rep.ret);
553: }
554:
555: /*
556: * rpc_cli_execCall() - Execute RPC call
557: *
558: * @cli = RPC Client session
559: * @noreply = We not want RPC reply
560: * @tag = Function tag for execution
561: * @in_vars = IN function argument array of values, may be NULL
562: * @out_vars = OUT returned array of rpc values, if !=NULL must be free after use with ait_freeVars()
563: * return: -1 error or != -1 ok result
564: */
565: int
566: rpc_cli_execCall(rpc_cli_t *cli, int noreply, u_short tag,
567: array_t * __restrict in_vars, array_t ** __restrict out_vars)
568: {
569: int type = 0, wlen;
570: u_char *buf;
571:
572: if (!cli) {
573: rpc_SetErr(EINVAL, "Can`t execute call because parameter is null or invalid!");
574: return -1;
575: } else {
576: if (cli->cli_id == SOCK_STREAM)
577: type = cli->cli_id;
578: buf = AIT_GET_BUF(&cli->cli_buf);
579: }
580: if (out_vars)
581: *out_vars = NULL;
582:
583: if ((wlen = rpc_pkt_Request(&cli->cli_buf, cli->cli_parent, tag, in_vars, noreply, type)) == -1)
584: return -1;
585:
586: if (rpc_pkt_Send(cli->cli_sock, cli->cli_id, &cli->cli_sa, &cli->cli_buf, wlen) == -1)
587: return -1;
588:
589: if (noreply) /* we not want reply */
590: return 0;
591:
592: if (rpc_pkt_Receive(cli->cli_sock, cli->cli_id, &cli->cli_sa, &cli->cli_buf) == -1)
593: return -1;
594:
595: if ((wlen = rpc_pkt_Replay(&cli->cli_buf, cli->cli_parent, tag, out_vars, type)) == -1)
596: return -1;
597:
598: return 0;
599: }
600:
601: /*
602: * rpc_cli_freeCall() - Free resouce allocated by RPC call
603: *
604: * @out_vars = Returned array with variables from RPC call
605: * return: none
606: */
607: void
608: rpc_cli_freeCall(array_t ** __restrict out_vars)
609: {
610: #ifdef CLI_RES_ZCOPY
611: array_Destroy(out_vars);
612: #else
613: ait_freeVars(out_vars);
614: #endif
615: }
616:
617: /*
618: * rpc_cli_ping() - Ping RPC server
619: *
620: * @cli = connected client
621: * return: -1 error or !=-1 ping seq id
622: */
623: int
624: rpc_cli_ping(rpc_cli_t *cli)
625: {
626: int ret = 0;
627: array_t *arr = NULL;
628:
629: if (!cli)
630: return -1;
631:
632: if (rpc_cli_execCall(cli, RPC_REPLY, CALL_SRVPING, NULL, &arr))
633: return -1;
634: else
635: ret = AIT_GET_U16(array(arr, 0, ait_val_t*));
636: rpc_cli_freeCall(&arr);
637:
638: return ret;
639: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>