Annotation of ansh/src/client3.c, revision 1.1.1.1.2.2
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.2! misho 6: * $Id: client3.c,v 1.1.1.1.2.1 2011/10/05 23:57:24 misho Exp $
1.1 misho 7: *
8: *************************************************************************/
9: #include "global.h"
10:
11:
12: static int
13: SetRemoteWinz(int h, u_short id, struct sockaddr *sa, 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);
1.1.1.1.2.2! misho 30: if (icmpSend(h, id, ANSH_FLG_WINZ, Crypted, buf, sizeof ws, sa, sizeof(struct sockaddr)) == -1)
1.1 misho 31: return -1;
32:
33: return 0;
34: }
35:
36: int
37: ConnectL3(int h, u_short id, struct sockaddr *sa, int len)
38: {
39: fd_set rfd;
40: struct timeval tv = { Timeout, 0 };
41: struct termios otio;
42: int rlen, ret = 0;
43: u_short aid;
44: struct sockaddr s;
1.1.1.1.2.1 misho 45: char flg, nl = 0;
1.1.1.1.2.2! misho 46: u_char *buf, *str;
1.1 misho 47: socklen_t sl;
48:
49: FTRACE(3);
50:
51: if (!(buf = malloc(len))) {
52: printf("Error:: no enough memory #%d - %s\n", errno, strerror(errno));
53: return -1;
54: }
55:
56: if (SetRemoteWinz(h, id, sa, buf, len) == -1) {
57: free(buf);
58: return -1;
59: }
60:
61: ioSetRAWMode(STDIN_FILENO, &otio);
62:
63: while (!Kill) {
64: FD_ZERO(&rfd);
65: FD_SET(h, &rfd);
66: FD_SET(STDIN_FILENO, &rfd);
67: if (select(h + 1, &rfd, NULL, NULL, (Timeout ? &tv : NULL)) < 1) {
68: ret = -1;
69: break;
70: }
71: if (FD_ISSET(STDIN_FILENO, &rfd)) {
72: memset(buf, 0, len);
73: rlen = read(STDIN_FILENO, buf, len);
74: if (rlen == -1) {
75: printf("Error:: read(stdin) #%d - %s\n", errno, strerror(errno));
76: ret = -1;
77: break;
78: }
1.1.1.1.2.1 misho 79:
80: /* local command handling */
81: if (rlen) {
82: /* execute local command */
83: if (nl == 2) {
84: switch (*buf) {
85: case '.':
86: Kill++;
87: printf("\n");
88: VERB(1) LOG("Exit from client\n");
89: continue;
90: case '~':
91: default:
92: nl ^= nl;
93: /* send buffer, unknown command */
94: break;
95: }
96: }
97: /* skip buffer and wait for local command */
98: if (nl == 1 && *buf == '~') {
99: nl++;
100: continue;
101: }
102: /* send buffer if detect NL */
103: if (*buf == 0xa || *buf == 0xd)
104: nl = 1;
105: else
106: nl ^= nl;
107: } else
108: nl ^= nl;
109:
1.1.1.1.2.2! misho 110: if (Crypted) {
! 111: str = cryptBuffer(buf, rlen, Crypted);
! 112: if (str) {
! 113: memcpy(buf, str, rlen);
! 114: free(str);
! 115: }
! 116: }
! 117:
! 118: rlen = icmpSend(h, id, ANSH_FLG_CPOUT, Crypted, buf, rlen, sa, sizeof(struct sockaddr));
1.1 misho 119: if (rlen == ANSH_FLG_ERR) {
120: ret = -1;
121: break;
122: }
123: } else {
124: sl = sizeof s;
125: rlen = len;
126: memset(buf, 0, rlen);
1.1.1.1.2.2! misho 127: flg = icmpRecv(h, &aid, &Crypted, buf, &rlen, &s, &sl);
1.1 misho 128: if (flg == ANSH_FLG_ERR) {
129: ret = -1;
130: break;
131: }
132: if (aid != id) {
133: VERB(4) LOG("different service id %d / %d\n", aid, id);
134: continue;
135: }
136: if (flg == ANSH_FLG_EOF) {
137: VERB(3) LOG("receive EOF going down.\n");
138: Kill++;
139: }
140: if (flg != ANSH_FLG_OK)
141: continue;
1.1.1.1.2.2! misho 142:
! 143: if (Crypted) {
! 144: str = cryptBuffer(buf, rlen, Crypted);
! 145: if (str) {
! 146: memcpy(buf, str, rlen);
! 147: free(str);
! 148: }
! 149: }
! 150:
1.1 misho 151: rlen = write(STDOUT_FILENO, buf, rlen);
152: if (rlen == -1) {
153: printf("Error:: write(stdout) #%d - %s\n", errno, strerror(errno));
154: ret = -1;
155: break;
156: }
157: }
158: }
159:
160: ioRestoreMode(STDIN_FILENO, otio);
161: free(buf);
162: return ret;
163: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>