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 2013/05/30 09:22:01 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: #ifdef HAVE_SRANDOMDEV
176: srandomdev();
177: #else
178: time_t tim;
179:
180: srandom((time(&tim) ^ getpid()));
181: #endif
182:
183: cli = e_malloc(sizeof(rpc_cli_t));
184: if (!cli) {
185: LOGERR;
186: return NULL;
187: } else
188: memset(cli, 0, sizeof(rpc_cli_t));
189:
190: /* build session */
191: cli->cli_parent = e_malloc(sizeof(rpc_sess_t));
192: if (!cli->cli_parent) {
193: LOGERR;
194: e_free(cli);
195: return NULL;
196: } else {
197: ((rpc_sess_t*) cli->cli_parent)->sess_version = RPC_VERSION;
198: ((rpc_sess_t*) cli->cli_parent)->sess_instance = InstID;
199: }
200:
201: cli->cli_id = proto;
202: memcpy(&cli->cli_sa, &sa, sizeof cli->cli_sa);
203: AIT_SET_BUFSIZ(&cli->cli_buf, 0, netBuf);
204:
205: /* connect to RPC server */
206: cli->cli_sock = socket(cli->cli_sa.sa.sa_family, cli->cli_id, 0);
207: if (cli->cli_sock == -1) {
208: LOGERR;
209: goto err;
210: }
211: if (setsockopt(cli->cli_sock, SOL_SOCKET, SO_SNDBUF,
212: &netBuf, sizeof netBuf) == -1) {
213: LOGERR;
214: goto err;
215: }
216: if (setsockopt(cli->cli_sock, SOL_SOCKET, SO_RCVBUF,
217: &netBuf, sizeof netBuf) == -1) {
218: LOGERR;
219: goto err;
220: }
221: if (cli->cli_id == SOCK_STREAM)
222: if (connect(cli->cli_sock, &cli->cli_sa.sa, cli->cli_sa.sa.sa_len) == -1) {
223: LOGERR;
224: goto err;
225: }
226:
227: fcntl(cli->cli_sock, F_SETFL, fcntl(cli->cli_sock, F_GETFL) | O_NONBLOCK);
228: return cli;
229: err:
230: AIT_FREE_VAL(&cli->cli_buf);
231: if (cli->cli_sock > 2)
232: close(cli->cli_sock);
233: e_free(cli->cli_parent);
234: e_free(cli);
235: return NULL;
236: }
237:
238: /*
239: * rpc_cli_closeClient() - Close connection to RPC server and free resources
240: *
241: * @cli = RPC Client session
242: * return: none
243: */
244: void
245: rpc_cli_closeClient(rpc_cli_t ** __restrict cli)
246: {
247: if (!cli || !*cli)
248: return;
249:
250: if ((*cli)->cli_id == SOCK_STREAM)
251: shutdown((*cli)->cli_sock, SHUT_RDWR);
252: close((*cli)->cli_sock);
253:
254: AIT_FREE_VAL(&(*cli)->cli_buf);
255:
256: if ((*cli)->cli_parent)
257: e_free((*cli)->cli_parent);
258:
259: e_free(*cli);
260: *cli = NULL;
261: }
262:
263: /*
264: * rpc_pkt_Send() - Send RPC packet
265: *
266: * @sock = Socket
267: * @type = Type of socket
268: * @sa = Server address
269: * @pkt = RPC packet
270: * @len = Length of packet
271: * return: -1 error or !=-1 sended bytes
272: */
273: int
274: rpc_pkt_Send(int sock, int type, sockaddr_t * __restrict sa, ait_val_t * __restrict pkt, int len)
275: {
276: struct pollfd pfd;
277: int ret;
278: u_char *buf;
279:
280: if (!pkt) {
281: rpc_SetErr(EINVAL, "Invalid argument(s)!");
282: return -1;
283: } else
284: buf = AIT_GET_BUF(pkt);
285:
286: pfd.fd = sock;
287: pfd.events = POLLOUT;
288: if ((ret = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) < 1 ||
289: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
290: if (ret)
291: LOGERR;
292: else
293: rpc_SetErr(ETIMEDOUT, "Timeout, can't send to RPC server");
294: return -1;
295: }
296: do {
297: if (type == SOCK_STREAM)
298: ret = send(sock, buf, len, MSG_NOSIGNAL);
299: else if (sa)
300: ret = sendto(sock, buf, len, MSG_NOSIGNAL, &sa->sa, sa->sa.sa_len);
301: else {
302: rpc_SetErr(EINVAL, "Invalid argument(s)!");
303: return -1;
304: }
305: if (ret == -1) {
306: if (errno == EAGAIN)
307: continue;
308: LOGERR;
309: return -1;
310: } else if (ret != len) {
311: rpc_SetErr(EPROCUNAVAIL, "RPC request, should be send %d bytes, "
312: "really sended %d bytes", len, ret);
313: return -1;
314: }
315: } while (0);
316:
317: return ret;
318: }
319:
320: /*
321: * rpc_pkt_Receive() - Receive RPC packet
322: *
323: * @sock = Socket
324: * @type = Type of socket
325: * @sa = Server address
326: * @pkt = RPC packet
327: * return: -1 error or !=-1 sended bytes
328: */
329: int
330: rpc_pkt_Receive(int sock, int type, sockaddr_t * __restrict sa, ait_val_t * __restrict pkt)
331: {
332: struct pollfd pfd;
333: int ret, len = 0;
334: u_char *buf;
335: sockaddr_t sa2;
336: socklen_t salen;
337:
338: if (!pkt) {
339: rpc_SetErr(EINVAL, "Invalid argument(s)!");
340: return -1;
341: } else
342: buf = AIT_GET_BUF(pkt);
343:
344: /* reply from RPC server */
345: pfd.fd = sock;
346: pfd.events = POLLIN | POLLPRI;
347: do {
348: if ((ret = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) < 1 ||
349: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
350: if (ret) {
351: LOGERR;
352: } else {
353: if (len++ < 7)
354: continue;
355: else
356: rpc_SetErr(ETIMEDOUT,
357: "Timeout, no answer from RPC server");
358: }
359:
360: return -1;
361: }
362:
363: memset(buf, 0, AIT_LEN(pkt));
364: if (type == SOCK_STREAM)
365: ret = recv(sock, buf, AIT_LEN(pkt), 0);
366: else {
367: memset(&sa2, 0, sizeof sa2);
368: salen = sa2.ss.ss_len = sizeof(sockaddr_t);
369: ret = recvfrom(sock, buf, AIT_LEN(pkt), 0, &sa2.sa, &salen);
370: }
371: if (ret < 1) {
372: if (ret) {
373: if (errno == EAGAIN)
374: continue;
375: else
376: LOGERR;
377: }
378: return -1;
379: }
380:
381: /* check for response from known address */
382: if (type == SOCK_DGRAM)
383: if (e_addrcmp(sa, &sa2, 42)) {
384: rpc_SetErr(ERPCMISMATCH,
385: "Received RPC response from unknown address");
386: continue;
387: }
388: } while (0);
389: if (ret < sizeof(struct tagRPCCall)) {
390: rpc_SetErr(ERPCMISMATCH, "Short RPC packet %d bytes", ret);
391: return -1;
392: }
393:
394: return ret;
395: }
396:
397: /*
398: * rpc_pkt_Request() - Build RPC Request packet
399: *
400: * @pkt = Packet buffer
401: * @sess = RPC session info
402: * @tag = Function tag for execution
403: * @vars = Function argument array of values, may be NULL
404: * @noreply = We not want RPC reply
405: * @nocrc = Without CRC calculation
406: * return: -1 error or != -1 prepared bytes into packet
407: */
408: int
409: rpc_pkt_Request(ait_val_t * __restrict pkt, rpc_sess_t * __restrict sess, u_short tag,
410: array_t * __restrict vars, int noreply, int nocrc)
411: {
412: struct tagRPCCall *rpc;
413: int ret = 0, len = sizeof(struct tagRPCCall);
414: u_char *buf;
415:
416: if (!pkt || !sess) {
417: rpc_SetErr(EINVAL, "Invalid argument(s)!");
418: return -1;
419: } else
420: buf = AIT_GET_BUF(pkt);
421:
422: /* prepare RPC call */
423: rpc = (struct tagRPCCall*) buf;
424: rpc_addPktSession(&rpc->call_session, sess);
425: rpc->call_tag = htons(tag);
426: rpc->call_seq = htons(random() % USHRT_MAX);
427: if (!vars)
428: rpc->call_argc = 0;
429: else
430: rpc->call_argc = htons(array_Size(vars));
431:
432: /* set reply */
433: rpc->call_req.flags = noreply ? RPC_NOREPLY : RPC_REPLY;
434:
435: if (array_Size(vars)) {
436: /* marshaling variables */
437: ret = ait_vars2buffer(buf + len, AIT_LEN(pkt) - len, vars);
438: if (ret == -1) {
439: rpc_SetErr(EBADRPC, "Failed to prepare RPC packet values");
440: return -1;
441: } else
442: len += ret;
443: }
444:
445: /* total packet length */
446: rpc->call_len = htons(len);
447:
448: if (!nocrc) {
449: /* calculate CRC */
450: rpc->call_crc ^= rpc->call_crc;
451: rpc->call_crc = htons(crcFletcher16((u_short*) buf, len / 2));
452: }
453:
454: return len;
455: }
456:
457: /*
458: * rpc_pkt_Replay() - Decode RPC Replay packet
459: *
460: * @pkt = Packet buffer
461: * @sess = RPC session info
462: * @tag = Function tag
463: * @vars = Function argument array of values, may be NULL
464: * @nocrc = Without CRC calculation
465: * return: -1 error or != -1 return value from function
466: */
467: int
468: rpc_pkt_Replay(ait_val_t * __restrict pkt, rpc_sess_t * __restrict sess, u_short tag,
469: array_t ** __restrict vars, int nocrc)
470: {
471: struct tagRPCCall *rpc;
472: int len;
473: u_char *buf;
474: uint16_t crc;
475:
476: if (!pkt || !sess) {
477: rpc_SetErr(EINVAL, "Invalid argument(s)!");
478: return -1;
479: } else
480: buf = AIT_GET_BUF(pkt);
481:
482: rpc = (struct tagRPCCall*) buf;
483: if (!nocrc) {
484: /* calculate CRC */
485: crc = ntohs(rpc->call_crc);
486: rpc->call_crc ^= rpc->call_crc;
487: if (crc != crcFletcher16((u_short*) buf, ntohs(rpc->call_len) / 2)) {
488: rpc_SetErr(ERPCMISMATCH, "Bad CRC RPC packet");
489: return -1;
490: }
491: }
492:
493: /* check RPC packet session info */
494: if (rpc_chkPktSession(&rpc->call_session, sess)) {
495: rpc_SetErr(ERPCMISMATCH, "Get invalid RPC session");
496: return -1;
497: }
498: if (ntohs(rpc->call_tag) != tag) {
499: rpc_SetErr(ERPCMISMATCH, "Get wrong RPC reply");
500: return -1;
501: }
502: if (ntohl(rpc->call_rep.eno) && ntohl(rpc->call_rep.ret) == -1) {
503: rpc_SetErr(ntohl(rpc->call_rep.eno), "Server side: retcode=%d #%d %s",
504: ntohl(rpc->call_rep.ret), ntohl(rpc->call_rep.eno),
505: strerror(ntohl(rpc->call_rep.eno)));
506: return -1;
507: }
508: len = ntohs(rpc->call_argc) * sizeof(ait_val_t);
509: if (len > AIT_LEN(pkt) - sizeof(struct tagRPCCall)) {
510: rpc_SetErr(EMSGSIZE, "Reply RPC packet not enough buffer space ...");
511: return -1;
512: }
513: if (len > ntohs(rpc->call_len) - sizeof(struct tagRPCCall)) {
514: rpc_SetErr(EMSGSIZE, "Reply RPC packet is too short ...");
515: return -1;
516: }
517:
518: /* RPC is OK! Go de-marshaling variables ... */
519: if (vars && ntohs(rpc->call_argc)) {
520: #ifdef CLI_RES_ZCOPY
521: *vars = ait_buffer2vars(buf + sizeof(struct tagRPCCall), len,
522: ntohs(rpc->call_argc), 42);
523: #else
524: *vars = ait_buffer2vars(buf + sizeof(struct tagRPCCall), len,
525: ntohs(rpc->call_argc), 0);
526: #endif
527: if (!*vars) {
528: rpc_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
529: return -1;
530: }
531: }
532:
533: return ntohl(rpc->call_rep.ret);
534: }
535:
536: /*
537: * rpc_cli_execCall() - Execute RPC call
538: *
539: * @cli = RPC Client session
540: * @noreply = We not want RPC reply
541: * @tag = Function tag for execution
542: * @in_vars = IN function argument array of values, may be NULL
543: * @out_vars = OUT returned array of rpc values, if !=NULL must be free after use with ait_freeVars()
544: * return: -1 error or != -1 ok result
545: */
546: int
547: rpc_cli_execCall(rpc_cli_t *cli, int noreply, u_short tag,
548: array_t * __restrict in_vars, array_t ** __restrict out_vars)
549: {
550: int type = 0, wlen;
551: u_char *buf;
552:
553: if (!cli) {
554: rpc_SetErr(EINVAL, "Can`t execute call because parameter is null or invalid!");
555: return -1;
556: } else {
557: if (cli->cli_id == SOCK_STREAM)
558: type = cli->cli_id;
559: buf = AIT_GET_BUF(&cli->cli_buf);
560: }
561: if (out_vars)
562: *out_vars = NULL;
563:
564: if ((wlen = rpc_pkt_Request(&cli->cli_buf, cli->cli_parent, tag, in_vars, noreply, type)) == -1)
565: return -1;
566:
567: if (rpc_pkt_Send(cli->cli_sock, cli->cli_id, &cli->cli_sa, &cli->cli_buf, wlen) == -1)
568: return -1;
569:
570: if (noreply) /* we not want reply */
571: return 0;
572:
573: if (rpc_pkt_Receive(cli->cli_sock, cli->cli_id, &cli->cli_sa, &cli->cli_buf) == -1)
574: return -1;
575:
576: if ((wlen = rpc_pkt_Replay(&cli->cli_buf, cli->cli_parent, tag, out_vars, type)) == -1)
577: return -1;
578:
579: return 0;
580: }
581:
582: /*
583: * rpc_cli_freeCall() - Free resouce allocated by RPC call
584: *
585: * @out_vars = Returned array with variables from RPC call
586: * return: none
587: */
588: void
589: rpc_cli_freeCall(array_t ** __restrict out_vars)
590: {
591: #ifdef CLI_RES_ZCOPY
592: array_Destroy(out_vars);
593: #else
594: ait_freeVars(out_vars);
595: #endif
596: }
597:
598: /*
599: * rpc_cli_ping() - Ping RPC server
600: *
601: * @cli = connected client
602: * return: -1 error or !=-1 ping seq id
603: */
604: int
605: rpc_cli_ping(rpc_cli_t *cli)
606: {
607: int ret = 0;
608: array_t *arr = NULL;
609:
610: if (!cli)
611: return -1;
612:
613: if (rpc_cli_execCall(cli, RPC_REPLY, CALL_SRVPING, NULL, &arr))
614: return -1;
615: else
616: ret = AIT_GET_U16(array(arr, 0, ait_val_t*));
617: rpc_cli_freeCall(&arr);
618:
619: return ret;
620: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>