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