Annotation of ansh/src/client3.c, revision 1.1.1.1.2.4
1.1 misho 1: /*************************************************************************
2: * (C) 2011 AITNET - Sofia/Bulgaria - <office@aitnet.org>
3: * by Michael Pounov <misho@elwix.org>
4: *
5: * $Author: misho $
1.1.1.1.2.4! misho 6: * $Id: client3.c,v 1.1.1.1.2.3 2011/10/13 14:29:30 misho Exp $
1.1 misho 7: *
1.1.1.1.2.4! 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:
! 15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
! 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:
49: static int
50: SetRemoteWinz(int h, u_short id, struct sockaddr *sa, u_char *buf, int buflen)
51: {
52: u_short *pos = (u_short*) buf;
53: struct winsize ws;
54:
55: FTRACE(5);
56:
57: memset(buf, 0, buflen);
58: if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1) {
59: printf("Error:: ioctl(winsize) #%d - %s\n", errno, strerror(errno));
60: return -1;
61: }
62:
63: pos[0] = htons(ws.ws_row);
64: pos[1] = htons(ws.ws_col);
65: pos[2] = htons(ws.ws_xpixel);
66: pos[3] = htons(ws.ws_ypixel);
1.1.1.1.2.3 misho 67: if (icmpSend(h, 0, id, ANSH_FLG_WINZ, Crypted, buf, sizeof ws, sa, sizeof(struct sockaddr)) == -1)
1.1 misho 68: return -1;
69:
70: return 0;
71: }
72:
73: int
74: ConnectL3(int h, u_short id, struct sockaddr *sa, int len)
75: {
76: fd_set rfd;
77: struct timeval tv = { Timeout, 0 };
78: struct termios otio;
79: int rlen, ret = 0;
80: u_short aid;
81: struct sockaddr s;
1.1.1.1.2.1 misho 82: char flg, nl = 0;
1.1.1.1.2.2 misho 83: u_char *buf, *str;
1.1 misho 84: socklen_t sl;
1.1.1.1.2.3 misho 85: u_int sq, Seq = 0;
1.1 misho 86:
87: FTRACE(3);
88:
89: if (!(buf = malloc(len))) {
90: printf("Error:: no enough memory #%d - %s\n", errno, strerror(errno));
91: return -1;
92: }
93:
94: if (SetRemoteWinz(h, id, sa, buf, len) == -1) {
95: free(buf);
96: return -1;
97: }
98:
99: ioSetRAWMode(STDIN_FILENO, &otio);
100:
101: while (!Kill) {
102: FD_ZERO(&rfd);
103: FD_SET(h, &rfd);
104: FD_SET(STDIN_FILENO, &rfd);
105: if (select(h + 1, &rfd, NULL, NULL, (Timeout ? &tv : NULL)) < 1) {
106: ret = -1;
107: break;
108: }
1.1.1.1.2.3 misho 109: if (FD_ISSET(h, &rfd)) {
110: sl = sizeof s;
111: rlen = len;
112: memset(buf, 0, rlen);
113: flg = icmpRecv(h, &sq, &aid, &Crypted, buf, &rlen, &s, &sl);
114: if (flg == ANSH_FLG_ERR) {
115: ret = -1;
116: break;
117: }
118: if (aid != id) {
119: VERB(4) LOG("different service id %d / %d\n", aid, id);
120: continue;
121: }
122: if (flg == ANSH_FLG_EOF) {
123: VERB(3) LOG("receive EOF going down.\n");
124: Kill++;
125: }
126: if (flg != ANSH_FLG_OK)
127: continue;
128: if (sq <= Seq)
129: continue;
130: else if (sq > (Seq + 1))
131: VERB(1) LOG("LOST PACKET(s) detect: %d; received seq=%d - %d",
132: sq - Seq + 1, sq, Seq);
133: Seq = sq;
134:
135: if (Crypted) {
136: str = cryptBuffer(buf, rlen, Crypted);
137: if (str) {
138: memcpy(buf, str, rlen);
139: free(str);
140: }
141: }
142:
143: rlen = write(STDOUT_FILENO, buf, rlen);
144: if (rlen == -1) {
145: printf("Error:: write(stdout) #%d - %s\n", errno, strerror(errno));
146: ret = -1;
147: break;
148: }
149: } else {
1.1 misho 150: memset(buf, 0, len);
151: rlen = read(STDIN_FILENO, buf, len);
152: if (rlen == -1) {
153: printf("Error:: read(stdin) #%d - %s\n", errno, strerror(errno));
154: ret = -1;
155: break;
156: }
1.1.1.1.2.1 misho 157:
158: /* local command handling */
159: if (rlen) {
160: /* execute local command */
161: if (nl == 2) {
162: switch (*buf) {
163: case '.':
164: Kill++;
165: printf("\n");
166: VERB(1) LOG("Exit from client\n");
167: continue;
168: case '~':
169: default:
170: nl ^= nl;
171: /* send buffer, unknown command */
172: break;
173: }
174: }
175: /* skip buffer and wait for local command */
176: if (nl == 1 && *buf == '~') {
177: nl++;
178: continue;
179: }
180: /* send buffer if detect NL */
181: if (*buf == 0xa || *buf == 0xd)
182: nl = 1;
183: else
184: nl ^= nl;
185: } else
186: nl ^= nl;
187:
1.1.1.1.2.2 misho 188: if (Crypted) {
189: str = cryptBuffer(buf, rlen, Crypted);
190: if (str) {
191: memcpy(buf, str, rlen);
192: free(str);
193: }
194: }
195:
1.1.1.1.2.3 misho 196: rlen = icmpSend(h, ++Seq, id, ANSH_FLG_CPOUT, Crypted, buf, rlen, sa, sizeof(struct sockaddr));
1.1 misho 197: if (rlen == ANSH_FLG_ERR) {
198: ret = -1;
199: break;
200: }
201: }
202: }
203:
204: ioRestoreMode(STDIN_FILENO, otio);
205: free(buf);
206: return ret;
207: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>