Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 2 | /* |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 3 | * telnet implementation for busybox |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 4 | * |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 5 | * Author: Tomi Ollila <too@iki.fi> |
| 6 | * Copyright (C) 1994-2000 by Tomi Ollila |
| 7 | * |
| 8 | * Created: Thu Apr 7 13:29:41 1994 too |
| 9 | * Last modified: Fri Jun 9 14:34:24 2000 too |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 10 | * |
Rob Landley | 1b751c8 | 2005-10-28 09:24:33 +0000 | [diff] [blame] | 11 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 12 | * |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 13 | * HISTORY |
| 14 | * Revision 3.1 1994/04/17 11:31:54 too |
| 15 | * initial revision |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 16 | * Modified 2000/06/13 for inclusion into BusyBox by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | 7e1273e | 2001-05-07 17:57:45 +0000 | [diff] [blame] | 17 | * Modified 2001/05/07 to add ability to pass TTYPE to remote host by Jim McQuillan |
| 18 | * <jam@ltsp.org> |
Eric Andersen | 539ffc9 | 2004-02-22 12:25:47 +0000 | [diff] [blame] | 19 | * Modified 2004/02/11 to add ability to pass the USER variable to remote host |
| 20 | * by Fernando Silveira <swrh@gmx.net> |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 21 | * |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 22 | */ |
| 23 | |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 24 | #include <termios.h> |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 25 | #include <arpa/telnet.h> |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 26 | #include <netinet/in.h> |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 27 | #include "libbb.h" |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 28 | |
Pavel Roskin | 616d13b | 2000-07-28 19:38:27 +0000 | [diff] [blame] | 29 | #ifdef DOTRACE |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 30 | #define TRACE(x, y) do { if (x) printf y; } while (0) |
| 31 | #else |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 32 | #define TRACE(x, y) |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 33 | #endif |
| 34 | |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 35 | enum { |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 36 | DATABUFSIZE = 128, |
| 37 | IACBUFSIZE = 128, |
| 38 | |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 39 | CHM_TRY = 0, |
| 40 | CHM_ON = 1, |
| 41 | CHM_OFF = 2, |
| 42 | |
| 43 | UF_ECHO = 0x01, |
| 44 | UF_SGA = 0x02, |
| 45 | |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 46 | TS_0 = 1, |
| 47 | TS_IAC = 2, |
| 48 | TS_OPT = 3, |
| 49 | TS_SUB1 = 4, |
| 50 | TS_SUB2 = 5, |
| 51 | }; |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 52 | |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 53 | typedef unsigned char byte; |
| 54 | |
Denis Vlasenko | 1101d1c | 2008-07-21 09:22:28 +0000 | [diff] [blame] | 55 | enum { netfd = 3 }; |
| 56 | |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 57 | struct globals { |
Denis Vlasenko | 1101d1c | 2008-07-21 09:22:28 +0000 | [diff] [blame] | 58 | int iaclen; /* could even use byte, but it's a loss on x86 */ |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 59 | byte telstate; /* telnet negotiation state from network input */ |
| 60 | byte telwish; /* DO, DONT, WILL, WONT */ |
| 61 | byte charmode; |
| 62 | byte telflags; |
| 63 | byte gotsig; |
Paul Fox | f2ddc05 | 2005-07-20 19:55:19 +0000 | [diff] [blame] | 64 | byte do_termios; |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 65 | #if ENABLE_FEATURE_TELNET_TTYPE |
| 66 | char *ttype; |
| 67 | #endif |
| 68 | #if ENABLE_FEATURE_TELNET_AUTOLOGIN |
| 69 | const char *autologin; |
| 70 | #endif |
| 71 | #if ENABLE_FEATURE_AUTOWIDTH |
Denis Vlasenko | 5599502 | 2008-05-18 22:28:26 +0000 | [diff] [blame] | 72 | unsigned win_width, win_height; |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 73 | #endif |
| 74 | /* same buffer used both for network and console read/write */ |
Denis Vlasenko | 54e3d1f | 2007-03-19 14:52:26 +0000 | [diff] [blame] | 75 | char buf[DATABUFSIZE]; |
| 76 | /* buffer to handle telnet negotiations */ |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 77 | char iacbuf[IACBUFSIZE]; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 78 | struct termios termios_def; |
| 79 | struct termios termios_raw; |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 80 | }; |
Denis Vlasenko | 4e5f82c | 2007-06-03 22:30:22 +0000 | [diff] [blame] | 81 | #define G (*(struct globals*)&bb_common_bufsiz1) |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 82 | void BUG_telnet_globals_too_big(void); |
| 83 | #define INIT_G() do { \ |
| 84 | if (sizeof(G) > COMMON_BUFSIZE) \ |
| 85 | BUG_telnet_globals_too_big(); \ |
| 86 | /* memset(&G, 0, sizeof G); - already is */ \ |
| 87 | } while (0) |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 88 | |
| 89 | /* Function prototypes */ |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 90 | static void rawmode(void); |
| 91 | static void cookmode(void); |
| 92 | static void do_linemode(void); |
| 93 | static void will_charmode(void); |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 94 | static void telopt(byte c); |
| 95 | static int subneg(byte c); |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 96 | |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 97 | static void iacflush(void) |
| 98 | { |
Denis Vlasenko | 1101d1c | 2008-07-21 09:22:28 +0000 | [diff] [blame] | 99 | write(netfd, G.iacbuf, G.iaclen); |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 100 | G.iaclen = 0; |
| 101 | } |
Eric Andersen | 7e1273e | 2001-05-07 17:57:45 +0000 | [diff] [blame] | 102 | |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 103 | #define write_str(fd, str) write(fd, str, sizeof(str) - 1) |
Eric Andersen | 539ffc9 | 2004-02-22 12:25:47 +0000 | [diff] [blame] | 104 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 105 | static void doexit(int ev) NORETURN; |
Denis Vlasenko | 54e3d1f | 2007-03-19 14:52:26 +0000 | [diff] [blame] | 106 | static void doexit(int ev) |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 107 | { |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 108 | cookmode(); |
| 109 | exit(ev); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 110 | } |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 111 | |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 112 | static void conescape(void) |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 113 | { |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 114 | char b; |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 115 | |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 116 | if (G.gotsig) /* came from line mode... go raw */ |
| 117 | rawmode(); |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 118 | |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 119 | write_str(1, "\r\nConsole escape. Commands are:\r\n\n" |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 120 | " l go to line mode\r\n" |
| 121 | " c go to character mode\r\n" |
| 122 | " z suspend telnet\r\n" |
| 123 | " e exit telnet\r\n"); |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 124 | |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 125 | if (read(STDIN_FILENO, &b, 1) <= 0) |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 126 | doexit(EXIT_FAILURE); |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 127 | |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 128 | switch (b) { |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 129 | case 'l': |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 130 | if (!G.gotsig) { |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 131 | do_linemode(); |
| 132 | goto rrturn; |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 133 | } |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 134 | break; |
| 135 | case 'c': |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 136 | if (G.gotsig) { |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 137 | will_charmode(); |
| 138 | goto rrturn; |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 139 | } |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 140 | break; |
| 141 | case 'z': |
| 142 | cookmode(); |
| 143 | kill(0, SIGTSTP); |
| 144 | rawmode(); |
| 145 | break; |
| 146 | case 'e': |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 147 | doexit(EXIT_SUCCESS); |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 148 | } |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 149 | |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 150 | write_str(1, "continuing...\r\n"); |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 151 | |
| 152 | if (G.gotsig) |
| 153 | cookmode(); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 154 | |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 155 | rrturn: |
| 156 | G.gotsig = 0; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 157 | |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 158 | } |
Denis Vlasenko | 54e3d1f | 2007-03-19 14:52:26 +0000 | [diff] [blame] | 159 | |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 160 | static void handlenetoutput(int len) |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 161 | { |
Denis Vlasenko | 1bec1b9 | 2007-11-06 02:23:39 +0000 | [diff] [blame] | 162 | /* here we could do smart tricks how to handle 0xFF:s in output |
| 163 | * stream like writing twice every sequence of FF:s (thus doing |
| 164 | * many write()s. But I think interactive telnet application does |
| 165 | * not need to be 100% 8-bit clean, so changing every 0xff:s to |
| 166 | * 0x7f:s |
Eric Andersen | 0e28e1f | 2002-04-26 07:20:47 +0000 | [diff] [blame] | 167 | * |
Denis Vlasenko | 1bec1b9 | 2007-11-06 02:23:39 +0000 | [diff] [blame] | 168 | * 2002-mar-21, Przemyslaw Czerpak (druzus@polbox.com) |
| 169 | * I don't agree. |
| 170 | * first - I cannot use programs like sz/rz |
| 171 | * second - the 0x0D is sent as one character and if the next |
| 172 | * char is 0x0A then it's eaten by a server side. |
| 173 | * third - whay doy you have to make 'many write()s'? |
| 174 | * I don't understand. |
| 175 | * So I implemented it. It's realy useful for me. I hope that |
| 176 | * others people will find it interesting too. |
Eric Andersen | 0e28e1f | 2002-04-26 07:20:47 +0000 | [diff] [blame] | 177 | */ |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 178 | |
Eric Andersen | 0e28e1f | 2002-04-26 07:20:47 +0000 | [diff] [blame] | 179 | int i, j; |
Eric Andersen | 0cb6f35 | 2006-01-30 22:30:41 +0000 | [diff] [blame] | 180 | byte * p = (byte*)G.buf; |
Eric Andersen | 0e28e1f | 2002-04-26 07:20:47 +0000 | [diff] [blame] | 181 | byte outbuf[4*DATABUFSIZE]; |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 182 | |
Denis Vlasenko | 1bec1b9 | 2007-11-06 02:23:39 +0000 | [diff] [blame] | 183 | for (i = len, j = 0; i > 0; i--, p++) { |
| 184 | if (*p == 0x1d) { |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 185 | conescape(); |
| 186 | return; |
| 187 | } |
Eric Andersen | 0e28e1f | 2002-04-26 07:20:47 +0000 | [diff] [blame] | 188 | outbuf[j++] = *p; |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 189 | if (*p == 0xff) |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 190 | outbuf[j++] = 0xff; |
Eric Andersen | 0e28e1f | 2002-04-26 07:20:47 +0000 | [diff] [blame] | 191 | else if (*p == 0x0d) |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 192 | outbuf[j++] = 0x00; |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 193 | } |
Denis Vlasenko | 219d14d | 2007-03-24 15:40:16 +0000 | [diff] [blame] | 194 | if (j > 0) |
Denis Vlasenko | 1101d1c | 2008-07-21 09:22:28 +0000 | [diff] [blame] | 195 | write(netfd, outbuf, j); |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 198 | static void handlenetinput(int len) |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 199 | { |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 200 | int i; |
| 201 | int cstart = 0; |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 202 | |
Denis Vlasenko | 1bec1b9 | 2007-11-06 02:23:39 +0000 | [diff] [blame] | 203 | for (i = 0; i < len; i++) { |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 204 | byte c = G.buf[i]; |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 205 | |
Denis Vlasenko | 1bec1b9 | 2007-11-06 02:23:39 +0000 | [diff] [blame] | 206 | if (G.telstate == 0) { /* most of the time state == 0 */ |
| 207 | if (c == IAC) { |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 208 | cstart = i; |
| 209 | G.telstate = TS_IAC; |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 210 | } |
Denis Vlasenko | 1bec1b9 | 2007-11-06 02:23:39 +0000 | [diff] [blame] | 211 | } else |
| 212 | switch (G.telstate) { |
| 213 | case TS_0: |
| 214 | if (c == IAC) |
| 215 | G.telstate = TS_IAC; |
| 216 | else |
| 217 | G.buf[cstart++] = c; |
| 218 | break; |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 219 | |
Denis Vlasenko | 1bec1b9 | 2007-11-06 02:23:39 +0000 | [diff] [blame] | 220 | case TS_IAC: |
| 221 | if (c == IAC) { /* IAC IAC -> 0xFF */ |
| 222 | G.buf[cstart++] = c; |
| 223 | G.telstate = TS_0; |
| 224 | break; |
| 225 | } |
| 226 | /* else */ |
| 227 | switch (c) { |
| 228 | case SB: |
| 229 | G.telstate = TS_SUB1; |
| 230 | break; |
| 231 | case DO: |
| 232 | case DONT: |
| 233 | case WILL: |
| 234 | case WONT: |
| 235 | G.telwish = c; |
| 236 | G.telstate = TS_OPT; |
| 237 | break; |
| 238 | default: |
| 239 | G.telstate = TS_0; /* DATA MARK must be added later */ |
| 240 | } |
| 241 | break; |
| 242 | case TS_OPT: /* WILL, WONT, DO, DONT */ |
| 243 | telopt(c); |
| 244 | G.telstate = TS_0; |
| 245 | break; |
| 246 | case TS_SUB1: /* Subnegotiation */ |
| 247 | case TS_SUB2: /* Subnegotiation */ |
| 248 | if (subneg(c)) |
| 249 | G.telstate = TS_0; |
| 250 | break; |
| 251 | } |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 252 | } |
Denis Vlasenko | 1bec1b9 | 2007-11-06 02:23:39 +0000 | [diff] [blame] | 253 | if (G.telstate) { |
| 254 | if (G.iaclen) iacflush(); |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 255 | if (G.telstate == TS_0) G.telstate = 0; |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 256 | len = cstart; |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 259 | if (len) |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 260 | write(STDOUT_FILENO, G.buf, len); |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 263 | static void putiac(int c) |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 264 | { |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 265 | G.iacbuf[G.iaclen++] = c; |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 268 | static void putiac2(byte wwdd, byte c) |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 269 | { |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 270 | if (G.iaclen + 3 > IACBUFSIZE) |
| 271 | iacflush(); |
| 272 | |
| 273 | putiac(IAC); |
| 274 | putiac(wwdd); |
| 275 | putiac(c); |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 278 | #if ENABLE_FEATURE_TELNET_TTYPE |
Eric Andersen | 7e1273e | 2001-05-07 17:57:45 +0000 | [diff] [blame] | 279 | static void putiac_subopt(byte c, char *str) |
| 280 | { |
| 281 | int len = strlen(str) + 6; // ( 2 + 1 + 1 + strlen + 2 ) |
| 282 | |
| 283 | if (G.iaclen + len > IACBUFSIZE) |
| 284 | iacflush(); |
| 285 | |
| 286 | putiac(IAC); |
| 287 | putiac(SB); |
| 288 | putiac(c); |
| 289 | putiac(0); |
| 290 | |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 291 | while (*str) |
Eric Andersen | 7e1273e | 2001-05-07 17:57:45 +0000 | [diff] [blame] | 292 | putiac(*str++); |
| 293 | |
| 294 | putiac(IAC); |
| 295 | putiac(SE); |
| 296 | } |
| 297 | #endif |
| 298 | |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 299 | #if ENABLE_FEATURE_TELNET_AUTOLOGIN |
Eric Andersen | 539ffc9 | 2004-02-22 12:25:47 +0000 | [diff] [blame] | 300 | static void putiac_subopt_autologin(void) |
| 301 | { |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 302 | int len = strlen(G.autologin) + 6; // (2 + 1 + 1 + strlen + 2) |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 303 | const char *user = "USER"; |
Eric Andersen | 539ffc9 | 2004-02-22 12:25:47 +0000 | [diff] [blame] | 304 | |
| 305 | if (G.iaclen + len > IACBUFSIZE) |
| 306 | iacflush(); |
| 307 | |
| 308 | putiac(IAC); |
| 309 | putiac(SB); |
| 310 | putiac(TELOPT_NEW_ENVIRON); |
| 311 | putiac(TELQUAL_IS); |
| 312 | putiac(NEW_ENV_VAR); |
| 313 | |
Denis Vlasenko | bf0a201 | 2006-12-26 10:42:51 +0000 | [diff] [blame] | 314 | while (*user) |
Eric Andersen | 539ffc9 | 2004-02-22 12:25:47 +0000 | [diff] [blame] | 315 | putiac(*user++); |
| 316 | |
| 317 | putiac(NEW_ENV_VALUE); |
| 318 | |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 319 | while (*G.autologin) |
| 320 | putiac(*G.autologin++); |
Eric Andersen | 539ffc9 | 2004-02-22 12:25:47 +0000 | [diff] [blame] | 321 | |
| 322 | putiac(IAC); |
| 323 | putiac(SE); |
| 324 | } |
| 325 | #endif |
| 326 | |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 327 | #if ENABLE_FEATURE_AUTOWIDTH |
Eric Andersen | 0e28e1f | 2002-04-26 07:20:47 +0000 | [diff] [blame] | 328 | static void putiac_naws(byte c, int x, int y) |
| 329 | { |
| 330 | if (G.iaclen + 9 > IACBUFSIZE) |
| 331 | iacflush(); |
| 332 | |
| 333 | putiac(IAC); |
| 334 | putiac(SB); |
| 335 | putiac(c); |
| 336 | |
| 337 | putiac((x >> 8) & 0xff); |
| 338 | putiac(x & 0xff); |
| 339 | putiac((y >> 8) & 0xff); |
| 340 | putiac(y & 0xff); |
| 341 | |
| 342 | putiac(IAC); |
| 343 | putiac(SE); |
| 344 | } |
| 345 | #endif |
| 346 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 347 | static char const escapecharis[] ALIGN1 = "\r\nEscape character is "; |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 348 | |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 349 | static void setConMode(void) |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 350 | { |
Denis Vlasenko | 54e3d1f | 2007-03-19 14:52:26 +0000 | [diff] [blame] | 351 | if (G.telflags & UF_ECHO) { |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 352 | if (G.charmode == CHM_TRY) { |
| 353 | G.charmode = CHM_ON; |
Matt Kraai | 12f417e | 2001-01-18 02:57:08 +0000 | [diff] [blame] | 354 | printf("\r\nEntering character mode%s'^]'.\r\n", escapecharis); |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 355 | rawmode(); |
| 356 | } |
Denis Vlasenko | 54e3d1f | 2007-03-19 14:52:26 +0000 | [diff] [blame] | 357 | } else { |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 358 | if (G.charmode != CHM_OFF) { |
| 359 | G.charmode = CHM_OFF; |
Matt Kraai | 12f417e | 2001-01-18 02:57:08 +0000 | [diff] [blame] | 360 | printf("\r\nEntering line mode%s'^C'.\r\n", escapecharis); |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 361 | cookmode(); |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 366 | static void will_charmode(void) |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 367 | { |
| 368 | G.charmode = CHM_TRY; |
| 369 | G.telflags |= (UF_ECHO | UF_SGA); |
| 370 | setConMode(); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 371 | |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 372 | putiac2(DO, TELOPT_ECHO); |
| 373 | putiac2(DO, TELOPT_SGA); |
| 374 | iacflush(); |
| 375 | } |
| 376 | |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 377 | static void do_linemode(void) |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 378 | { |
| 379 | G.charmode = CHM_TRY; |
| 380 | G.telflags &= ~(UF_ECHO | UF_SGA); |
| 381 | setConMode(); |
| 382 | |
| 383 | putiac2(DONT, TELOPT_ECHO); |
| 384 | putiac2(DONT, TELOPT_SGA); |
| 385 | iacflush(); |
| 386 | } |
| 387 | |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 388 | static void to_notsup(char c) |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 389 | { |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 390 | if (G.telwish == WILL) |
| 391 | putiac2(DONT, c); |
| 392 | else if (G.telwish == DO) |
| 393 | putiac2(WONT, c); |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 396 | static void to_echo(void) |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 397 | { |
| 398 | /* if server requests ECHO, don't agree */ |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 399 | if (G.telwish == DO) { |
| 400 | putiac2(WONT, TELOPT_ECHO); |
| 401 | return; |
| 402 | } |
| 403 | if (G.telwish == DONT) |
| 404 | return; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 405 | |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 406 | if (G.telflags & UF_ECHO) { |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 407 | if (G.telwish == WILL) |
| 408 | return; |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 409 | } else if (G.telwish == WONT) |
| 410 | return; |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 411 | |
| 412 | if (G.charmode != CHM_OFF) |
| 413 | G.telflags ^= UF_ECHO; |
| 414 | |
| 415 | if (G.telflags & UF_ECHO) |
| 416 | putiac2(DO, TELOPT_ECHO); |
| 417 | else |
| 418 | putiac2(DONT, TELOPT_ECHO); |
| 419 | |
| 420 | setConMode(); |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 421 | write_str(1, "\r\n"); /* sudden modec */ |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 424 | static void to_sga(void) |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 425 | { |
| 426 | /* daemon always sends will/wont, client do/dont */ |
| 427 | |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 428 | if (G.telflags & UF_SGA) { |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 429 | if (G.telwish == WILL) |
| 430 | return; |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 431 | } else if (G.telwish == WONT) |
| 432 | return; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 433 | |
Denis Vlasenko | 1bec1b9 | 2007-11-06 02:23:39 +0000 | [diff] [blame] | 434 | G.telflags ^= UF_SGA; /* toggle */ |
| 435 | if (G.telflags & UF_SGA) |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 436 | putiac2(DO, TELOPT_SGA); |
| 437 | else |
| 438 | putiac2(DONT, TELOPT_SGA); |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 441 | #if ENABLE_FEATURE_TELNET_TTYPE |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 442 | static void to_ttype(void) |
Eric Andersen | 7e1273e | 2001-05-07 17:57:45 +0000 | [diff] [blame] | 443 | { |
| 444 | /* Tell server we will (or won't) do TTYPE */ |
| 445 | |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 446 | if (G.ttype) |
Eric Andersen | 7e1273e | 2001-05-07 17:57:45 +0000 | [diff] [blame] | 447 | putiac2(WILL, TELOPT_TTYPE); |
| 448 | else |
| 449 | putiac2(WONT, TELOPT_TTYPE); |
Eric Andersen | 7e1273e | 2001-05-07 17:57:45 +0000 | [diff] [blame] | 450 | } |
| 451 | #endif |
| 452 | |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 453 | #if ENABLE_FEATURE_TELNET_AUTOLOGIN |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 454 | static void to_new_environ(void) |
Eric Andersen | 539ffc9 | 2004-02-22 12:25:47 +0000 | [diff] [blame] | 455 | { |
| 456 | /* Tell server we will (or will not) do AUTOLOGIN */ |
| 457 | |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 458 | if (G.autologin) |
Eric Andersen | 539ffc9 | 2004-02-22 12:25:47 +0000 | [diff] [blame] | 459 | putiac2(WILL, TELOPT_NEW_ENVIRON); |
| 460 | else |
| 461 | putiac2(WONT, TELOPT_NEW_ENVIRON); |
Eric Andersen | 539ffc9 | 2004-02-22 12:25:47 +0000 | [diff] [blame] | 462 | } |
| 463 | #endif |
| 464 | |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 465 | #if ENABLE_FEATURE_AUTOWIDTH |
Rob Landley | 88621d7 | 2006-08-29 19:41:06 +0000 | [diff] [blame] | 466 | static void to_naws(void) |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 467 | { |
Eric Andersen | 0e28e1f | 2002-04-26 07:20:47 +0000 | [diff] [blame] | 468 | /* Tell server we will do NAWS */ |
| 469 | putiac2(WILL, TELOPT_NAWS); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 470 | } |
Eric Andersen | 0e28e1f | 2002-04-26 07:20:47 +0000 | [diff] [blame] | 471 | #endif |
| 472 | |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 473 | static void telopt(byte c) |
| 474 | { |
Denis Vlasenko | baca175 | 2007-03-11 13:43:10 +0000 | [diff] [blame] | 475 | switch (c) { |
| 476 | case TELOPT_ECHO: |
| 477 | to_echo(); break; |
| 478 | case TELOPT_SGA: |
| 479 | to_sga(); break; |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 480 | #if ENABLE_FEATURE_TELNET_TTYPE |
Denis Vlasenko | baca175 | 2007-03-11 13:43:10 +0000 | [diff] [blame] | 481 | case TELOPT_TTYPE: |
| 482 | to_ttype(); break; |
Eric Andersen | 7e1273e | 2001-05-07 17:57:45 +0000 | [diff] [blame] | 483 | #endif |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 484 | #if ENABLE_FEATURE_TELNET_AUTOLOGIN |
Denis Vlasenko | baca175 | 2007-03-11 13:43:10 +0000 | [diff] [blame] | 485 | case TELOPT_NEW_ENVIRON: |
| 486 | to_new_environ(); break; |
Eric Andersen | 539ffc9 | 2004-02-22 12:25:47 +0000 | [diff] [blame] | 487 | #endif |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 488 | #if ENABLE_FEATURE_AUTOWIDTH |
Denis Vlasenko | baca175 | 2007-03-11 13:43:10 +0000 | [diff] [blame] | 489 | case TELOPT_NAWS: |
| 490 | to_naws(); |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 491 | putiac_naws(c, G.win_width, G.win_height); |
Denis Vlasenko | baca175 | 2007-03-11 13:43:10 +0000 | [diff] [blame] | 492 | break; |
Eric Andersen | 0e28e1f | 2002-04-26 07:20:47 +0000 | [diff] [blame] | 493 | #endif |
Denis Vlasenko | baca175 | 2007-03-11 13:43:10 +0000 | [diff] [blame] | 494 | default: |
| 495 | to_notsup(c); |
| 496 | break; |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | |
Eric Andersen | 0e28e1f | 2002-04-26 07:20:47 +0000 | [diff] [blame] | 500 | /* subnegotiation -- ignore all (except TTYPE,NAWS) */ |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 501 | static int subneg(byte c) |
| 502 | { |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 503 | switch (G.telstate) { |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 504 | case TS_SUB1: |
| 505 | if (c == IAC) |
| 506 | G.telstate = TS_SUB2; |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 507 | #if ENABLE_FEATURE_TELNET_TTYPE |
Eric Andersen | 7e1273e | 2001-05-07 17:57:45 +0000 | [diff] [blame] | 508 | else |
| 509 | if (c == TELOPT_TTYPE) |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 510 | putiac_subopt(TELOPT_TTYPE, G.ttype); |
Eric Andersen | 7e1273e | 2001-05-07 17:57:45 +0000 | [diff] [blame] | 511 | #endif |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 512 | #if ENABLE_FEATURE_TELNET_AUTOLOGIN |
Eric Andersen | 539ffc9 | 2004-02-22 12:25:47 +0000 | [diff] [blame] | 513 | else |
| 514 | if (c == TELOPT_NEW_ENVIRON) |
| 515 | putiac_subopt_autologin(); |
| 516 | #endif |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 517 | break; |
| 518 | case TS_SUB2: |
| 519 | if (c == SE) |
| 520 | return TRUE; |
| 521 | G.telstate = TS_SUB1; |
| 522 | /* break; */ |
| 523 | } |
| 524 | return FALSE; |
| 525 | } |
| 526 | |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 527 | static void fgotsig(int sig) |
| 528 | { |
| 529 | G.gotsig = sig; |
| 530 | } |
| 531 | |
| 532 | |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 533 | static void rawmode(void) |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 534 | { |
Denis Vlasenko | 54e3d1f | 2007-03-19 14:52:26 +0000 | [diff] [blame] | 535 | if (G.do_termios) |
| 536 | tcsetattr(0, TCSADRAIN, &G.termios_raw); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 537 | } |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 538 | |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 539 | static void cookmode(void) |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 540 | { |
Denis Vlasenko | 54e3d1f | 2007-03-19 14:52:26 +0000 | [diff] [blame] | 541 | if (G.do_termios) |
| 542 | tcsetattr(0, TCSADRAIN, &G.termios_def); |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Denis Vlasenko | 1bec1b9 | 2007-11-06 02:23:39 +0000 | [diff] [blame] | 545 | /* poll gives smaller (-70 bytes) code */ |
| 546 | #define USE_POLL 1 |
| 547 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 548 | int telnet_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 1101d1c | 2008-07-21 09:22:28 +0000 | [diff] [blame] | 549 | int telnet_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 550 | { |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 551 | char *host; |
| 552 | int port; |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 553 | int len; |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 554 | #ifdef USE_POLL |
| 555 | struct pollfd ufds[2]; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 556 | #else |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 557 | fd_set readfds; |
| 558 | int maxfd; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 559 | #endif |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 560 | |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 561 | INIT_G(); |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 562 | |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 563 | #if ENABLE_FEATURE_AUTOWIDTH |
| 564 | get_terminal_width_height(0, &G.win_width, &G.win_height); |
| 565 | #endif |
| 566 | |
| 567 | #if ENABLE_FEATURE_TELNET_TTYPE |
| 568 | G.ttype = getenv("TERM"); |
| 569 | #endif |
| 570 | |
Paul Fox | f2ddc05 | 2005-07-20 19:55:19 +0000 | [diff] [blame] | 571 | if (tcgetattr(0, &G.termios_def) >= 0) { |
| 572 | G.do_termios = 1; |
Paul Fox | f2ddc05 | 2005-07-20 19:55:19 +0000 | [diff] [blame] | 573 | G.termios_raw = G.termios_def; |
| 574 | cfmakeraw(&G.termios_raw); |
| 575 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 576 | |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 577 | #if ENABLE_FEATURE_TELNET_AUTOLOGIN |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 578 | if (1 & getopt32(argv, "al:", &G.autologin)) |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 579 | G.autologin = getenv("USER"); |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 580 | argv += optind; |
Eric Andersen | 539ffc9 | 2004-02-22 12:25:47 +0000 | [diff] [blame] | 581 | #else |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 582 | argv++; |
Eric Andersen | 539ffc9 | 2004-02-22 12:25:47 +0000 | [diff] [blame] | 583 | #endif |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 584 | if (!*argv) |
| 585 | bb_show_usage(); |
| 586 | host = *argv++; |
| 587 | port = bb_lookup_port(*argv ? *argv++ : "telnet", "tcp", 23); |
| 588 | if (*argv) /* extra params?? */ |
| 589 | bb_show_usage(); |
| 590 | |
Denis Vlasenko | 1101d1c | 2008-07-21 09:22:28 +0000 | [diff] [blame] | 591 | xmove_fd(create_and_connect_stream_or_die(host, port), netfd); |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 592 | |
Denis Vlasenko | 1101d1c | 2008-07-21 09:22:28 +0000 | [diff] [blame] | 593 | setsockopt(netfd, SOL_SOCKET, SO_KEEPALIVE, &const_int_1, sizeof(const_int_1)); |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 594 | |
| 595 | signal(SIGINT, fgotsig); |
| 596 | |
| 597 | #ifdef USE_POLL |
Denis Vlasenko | 1101d1c | 2008-07-21 09:22:28 +0000 | [diff] [blame] | 598 | ufds[0].fd = 0; ufds[1].fd = netfd; |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 599 | ufds[0].events = ufds[1].events = POLLIN; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 600 | #else |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 601 | FD_ZERO(&readfds); |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 602 | FD_SET(STDIN_FILENO, &readfds); |
Denis Vlasenko | 1101d1c | 2008-07-21 09:22:28 +0000 | [diff] [blame] | 603 | FD_SET(netfd, &readfds); |
| 604 | maxfd = netfd + 1; |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 605 | #endif |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 606 | |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 607 | while (1) { |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 608 | #ifndef USE_POLL |
| 609 | fd_set rfds = readfds; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 610 | |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 611 | switch (select(maxfd, &rfds, NULL, NULL, NULL)) |
| 612 | #else |
| 613 | switch (poll(ufds, 2, -1)) |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 614 | #endif |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 615 | { |
| 616 | case 0: |
| 617 | /* timeout */ |
| 618 | case -1: |
| 619 | /* error, ignore and/or log something, bay go to loop */ |
| 620 | if (G.gotsig) |
| 621 | conescape(); |
| 622 | else |
| 623 | sleep(1); |
| 624 | break; |
| 625 | default: |
| 626 | |
| 627 | #ifdef USE_POLL |
| 628 | if (ufds[0].revents) /* well, should check POLLIN, but ... */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 629 | #else |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 630 | if (FD_ISSET(STDIN_FILENO, &rfds)) |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 631 | #endif |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 632 | { |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 633 | len = read(STDIN_FILENO, G.buf, DATABUFSIZE); |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 634 | if (len <= 0) |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 635 | doexit(EXIT_SUCCESS); |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 636 | TRACE(0, ("Read con: %d\n", len)); |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 637 | handlenetoutput(len); |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | #ifdef USE_POLL |
| 641 | if (ufds[1].revents) /* well, should check POLLIN, but ... */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 642 | #else |
Denis Vlasenko | 1101d1c | 2008-07-21 09:22:28 +0000 | [diff] [blame] | 643 | if (FD_ISSET(netfd, &rfds)) |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 644 | #endif |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 645 | { |
Denis Vlasenko | 1101d1c | 2008-07-21 09:22:28 +0000 | [diff] [blame] | 646 | len = read(netfd, G.buf, DATABUFSIZE); |
Denis Vlasenko | 9de420c | 2007-01-10 09:28:01 +0000 | [diff] [blame] | 647 | if (len <= 0) { |
Denis Vlasenko | f24cdf1 | 2007-03-19 14:47:09 +0000 | [diff] [blame] | 648 | write_str(1, "Connection closed by foreign host\r\n"); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 649 | doexit(EXIT_FAILURE); |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 650 | } |
Denis Vlasenko | 1101d1c | 2008-07-21 09:22:28 +0000 | [diff] [blame] | 651 | TRACE(0, ("Read netfd (%d): %d\n", netfd, len)); |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 652 | handlenetinput(len); |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 653 | } |
| 654 | } |
Denis Vlasenko | 1101d1c | 2008-07-21 09:22:28 +0000 | [diff] [blame] | 655 | } /* while (1) */ |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 656 | } |