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