1: /*************************************************************************
2: * (C) 2011 AITNET - Sofia/Bulgaria - <office@aitnet.org>
3: * by Michael Pounov <misho@elwix.org>
4: *
5: * $Author: misho $
6: * $Id: client2.c,v 1.1.1.1.2.2 2011/10/10 13:56:30 misho Exp $
7: *
8: *************************************************************************/
9: #include "global.h"
10:
11:
12: static int
13: SetRemoteWinz(int h, struct ether_addr *ea, u_char *buf, int buflen)
14: {
15: u_short *pos = (u_short*) buf;
16: struct winsize ws;
17:
18: FTRACE(5);
19:
20: memset(buf, 0, buflen);
21: if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1) {
22: printf("Error:: ioctl(winsize) #%d - %s\n", errno, strerror(errno));
23: return -1;
24: }
25:
26: pos[0] = htons(ws.ws_row);
27: pos[1] = htons(ws.ws_col);
28: pos[2] = htons(ws.ws_xpixel);
29: pos[3] = htons(ws.ws_ypixel);
30: if (pktSend(h, ANSH_FLG_WINZ, Crypted, buf, sizeof ws, ea) == -1)
31: return -1;
32:
33: return 0;
34: }
35:
36: int
37: ConnectL2(int h, struct ether_addr *ea, int len)
38: {
39: fd_set rfd;
40: struct ether_header eth;
41: struct timeval tv = { 10, 0 };
42: struct termios otio;
43: int rlen, ret = 0;
44: char flg, nl = 0;
45: u_char *buf, *str;
46:
47: FTRACE(3);
48:
49: if (!(buf = malloc(len))) {
50: printf("Error:: no enough memory #%d - %s\n", errno, strerror(errno));
51: return -1;
52: }
53:
54: if (SetRemoteWinz(h, ea, buf, len) == -1) {
55: free(buf);
56: return -1;
57: }
58:
59: ioSetRAWMode(STDIN_FILENO, &otio);
60:
61: while (!Kill) {
62: FD_ZERO(&rfd);
63: FD_SET(h, &rfd);
64: FD_SET(STDIN_FILENO, &rfd);
65: if (select(h + 1, &rfd, NULL, NULL, (Timeout ? &tv : NULL)) < 1) {
66: ret = -1;
67: break;
68: }
69: if (FD_ISSET(STDIN_FILENO, &rfd)) {
70: memset(buf, 0, len);
71: rlen = read(STDIN_FILENO, buf, len);
72: if (rlen == -1) {
73: printf("Error:: read(stdin) #%d - %s\n", errno, strerror(errno));
74: ret = -1;
75: break;
76: }
77:
78: /* local command handling */
79: if (rlen) {
80: /* execute local command */
81: if (nl == 2) {
82: switch (*buf) {
83: case '.':
84: Kill++;
85: printf("\n");
86: VERB(1) LOG("Exit from client\n");
87: continue;
88: case '~':
89: default:
90: nl ^= nl;
91: /* send buffer, unknown command */
92: break;
93: }
94: }
95: /* skip buffer and wait for local command */
96: if (nl == 1 && *buf == '~') {
97: nl++;
98: continue;
99: }
100: /* send buffer if detect NL */
101: if (*buf == 0xa || *buf == 0xd)
102: nl = 1;
103: else
104: nl ^= nl;
105: } else
106: nl ^= nl;
107:
108: if (Crypted) {
109: str = cryptBuffer(buf, rlen, Crypted);
110: if (str) {
111: memcpy(buf, str, rlen);
112: free(str);
113: }
114: }
115:
116: rlen = pktSend(h, ANSH_FLG_CPOUT, Crypted, buf, rlen, ea);
117: if (rlen == ANSH_FLG_ERR) {
118: ret = -1;
119: break;
120: }
121: } else {
122: rlen = len;
123: memset(buf, 0, rlen);
124: flg = pktRecv(h, &Crypted, buf, &rlen, ð);
125: if (flg == ANSH_FLG_ERR) {
126: ret = -1;
127: break;
128: }
129: if (ntohs(eth.ether_type) != ANSH_ID) {
130: VERB(4) LOG("different service id %d / %d\n", eth.ether_type, ANSH_ID);
131: continue;
132: }
133: if (flg == ANSH_FLG_EOF) {
134: VERB(3) LOG("receive EOF going down.\n");
135: Kill++;
136: }
137: if (flg != ANSH_FLG_OK)
138: continue;
139:
140: if (Crypted) {
141: str = cryptBuffer(buf, rlen, Crypted);
142: if (str) {
143: memcpy(buf, str, rlen);
144: free(str);
145: }
146: }
147:
148: rlen = write(STDOUT_FILENO, buf, rlen);
149: if (rlen == -1) {
150: printf("Error:: write(stdout) #%d - %s\n", errno, strerror(errno));
151: ret = -1;
152: break;
153: }
154: }
155: }
156:
157: ioRestoreMode(STDIN_FILENO, otio);
158: free(buf);
159: return ret;
160: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>