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 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 | * General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 24 | * |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 25 | * HISTORY |
| 26 | * Revision 3.1 1994/04/17 11:31:54 too |
| 27 | * initial revision |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 28 | * 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] | 29 | * Modified 2001/05/07 to add ability to pass TTYPE to remote host by Jim McQuillan |
| 30 | * <jam@ltsp.org> |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 31 | * |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 32 | */ |
| 33 | |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 34 | #include <termios.h> |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 35 | #include <unistd.h> |
| 36 | #include <errno.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <stdarg.h> |
Eric Andersen | 2276d83 | 2002-07-11 11:11:56 +0000 | [diff] [blame] | 39 | #include <string.h> |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 40 | #include <signal.h> |
| 41 | #include <arpa/telnet.h> |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 42 | #include <sys/types.h> |
| 43 | #include <sys/socket.h> |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 44 | #include <netinet/in.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 45 | #include "busybox.h" |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 46 | |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 47 | #if 0 |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 48 | static const int DOTRACE = 1; |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 49 | #endif |
| 50 | |
Pavel Roskin | 616d13b | 2000-07-28 19:38:27 +0000 | [diff] [blame] | 51 | #ifdef DOTRACE |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 52 | #include <arpa/inet.h> /* for inet_ntoa()... */ |
| 53 | #define TRACE(x, y) do { if (x) printf y; } while (0) |
| 54 | #else |
| 55 | #define TRACE(x, y) |
| 56 | #endif |
| 57 | |
| 58 | #if 0 |
| 59 | #define USE_POLL |
| 60 | #include <sys/poll.h> |
| 61 | #else |
| 62 | #include <sys/time.h> |
| 63 | #endif |
| 64 | |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 65 | #define DATABUFSIZE 128 |
| 66 | #define IACBUFSIZE 128 |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 67 | |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 68 | static const int CHM_TRY = 0; |
| 69 | static const int CHM_ON = 1; |
| 70 | static const int CHM_OFF = 2; |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 71 | |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 72 | static const int UF_ECHO = 0x01; |
| 73 | static const int UF_SGA = 0x02; |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 74 | |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 75 | enum { |
| 76 | TS_0 = 1, |
| 77 | TS_IAC = 2, |
| 78 | TS_OPT = 3, |
| 79 | TS_SUB1 = 4, |
| 80 | TS_SUB2 = 5, |
| 81 | }; |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 82 | |
| 83 | #define WriteCS(fd, str) write(fd, str, sizeof str -1) |
| 84 | |
| 85 | typedef unsigned char byte; |
| 86 | |
| 87 | /* use globals to reduce size ??? */ /* test this hypothesis later */ |
Eric Andersen | 92d2324 | 2001-03-19 23:49:41 +0000 | [diff] [blame] | 88 | static struct Globalvars { |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 89 | int netfd; /* console fd:s are 0 and 1 (and 2) */ |
| 90 | /* same buffer used both for network and console read/write */ |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 91 | char buf[DATABUFSIZE]; /* allocating so static size is smaller */ |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 92 | byte telstate; /* telnet negotiation state from network input */ |
| 93 | byte telwish; /* DO, DONT, WILL, WONT */ |
| 94 | byte charmode; |
| 95 | byte telflags; |
| 96 | byte gotsig; |
| 97 | /* buffer to handle telnet negotiations */ |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 98 | char iacbuf[IACBUFSIZE]; |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 99 | short iaclen; /* could even use byte */ |
| 100 | struct termios termios_def; |
| 101 | struct termios termios_raw; |
| 102 | } G; |
| 103 | |
| 104 | #define xUSE_GLOBALVAR_PTR /* xUSE... -> don't use :D (makes smaller code) */ |
| 105 | |
| 106 | #ifdef USE_GLOBALVAR_PTR |
| 107 | struct Globalvars * Gptr; |
| 108 | #define G (*Gptr) |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 109 | #endif |
| 110 | |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 111 | static inline void iacflush(void) |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 112 | { |
| 113 | write(G.netfd, G.iacbuf, G.iaclen); |
| 114 | G.iaclen = 0; |
| 115 | } |
| 116 | |
| 117 | /* Function prototypes */ |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 118 | static void rawmode(void); |
| 119 | static void cookmode(void); |
| 120 | static void do_linemode(void); |
| 121 | static void will_charmode(void); |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 122 | static void telopt(byte c); |
| 123 | static int subneg(byte c); |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 124 | |
| 125 | /* Some globals */ |
| 126 | static int one = 1; |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 127 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 128 | #ifdef CONFIG_FEATURE_TELNET_TTYPE |
Eric Andersen | 7e1273e | 2001-05-07 17:57:45 +0000 | [diff] [blame] | 129 | static char *ttype; |
| 130 | #endif |
| 131 | |
Eric Andersen | 0e28e1f | 2002-04-26 07:20:47 +0000 | [diff] [blame] | 132 | #ifdef CONFIG_FEATURE_AUTOWIDTH |
| 133 | static int win_width, win_height; |
| 134 | #endif |
| 135 | |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 136 | static void doexit(int ev) |
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 | cookmode(); |
| 139 | exit(ev); |
| 140 | } |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 141 | |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 142 | static void conescape(void) |
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 | char b; |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 145 | |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 146 | if (G.gotsig) /* came from line mode... go raw */ |
| 147 | rawmode(); |
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 | WriteCS(1, "\r\nConsole escape. Commands are:\r\n\n" |
| 150 | " l go to line mode\r\n" |
| 151 | " c go to character mode\r\n" |
| 152 | " z suspend telnet\r\n" |
| 153 | " e exit telnet\r\n"); |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 154 | |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 155 | if (read(0, &b, 1) <= 0) |
| 156 | doexit(1); |
| 157 | |
| 158 | switch (b) |
| 159 | { |
| 160 | case 'l': |
| 161 | if (!G.gotsig) |
| 162 | { |
| 163 | do_linemode(); |
| 164 | goto rrturn; |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 165 | } |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 166 | break; |
| 167 | case 'c': |
| 168 | if (G.gotsig) |
| 169 | { |
| 170 | will_charmode(); |
| 171 | goto rrturn; |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 172 | } |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 173 | break; |
| 174 | case 'z': |
| 175 | cookmode(); |
| 176 | kill(0, SIGTSTP); |
| 177 | rawmode(); |
| 178 | break; |
| 179 | case 'e': |
| 180 | doexit(0); |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 181 | } |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 182 | |
| 183 | WriteCS(1, "continuing...\r\n"); |
| 184 | |
| 185 | if (G.gotsig) |
| 186 | cookmode(); |
| 187 | |
| 188 | rrturn: |
| 189 | G.gotsig = 0; |
| 190 | |
| 191 | } |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 192 | static void handlenetoutput(int len) |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 193 | { |
| 194 | /* here we could do smart tricks how to handle 0xFF:s in output |
| 195 | * stream like writing twice every sequence of FF:s (thus doing |
| 196 | * many write()s. But I think interactive telnet application does |
| 197 | * 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] | 198 | * 0x7f:s |
| 199 | * |
| 200 | * 2002-mar-21, Przemyslaw Czerpak (druzus@polbox.com) |
| 201 | * I don't agree. |
| 202 | * first - I cannot use programs like sz/rz |
| 203 | * second - the 0x0D is sent as one character and if the next |
| 204 | * char is 0x0A then it's eaten by a server side. |
| 205 | * third - whay doy you have to make 'many write()s'? |
| 206 | * I don't understand. |
| 207 | * So I implemented it. It's realy useful for me. I hope that |
| 208 | * others people will find it interesting to. |
| 209 | */ |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 210 | |
Eric Andersen | 0e28e1f | 2002-04-26 07:20:47 +0000 | [diff] [blame] | 211 | int i, j; |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 212 | byte * p = G.buf; |
Eric Andersen | 0e28e1f | 2002-04-26 07:20:47 +0000 | [diff] [blame] | 213 | byte outbuf[4*DATABUFSIZE]; |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 214 | |
Eric Andersen | 0e28e1f | 2002-04-26 07:20:47 +0000 | [diff] [blame] | 215 | for (i = len, j = 0; i > 0; i--, p++) |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 216 | { |
| 217 | if (*p == 0x1d) |
| 218 | { |
| 219 | conescape(); |
| 220 | return; |
| 221 | } |
Eric Andersen | 0e28e1f | 2002-04-26 07:20:47 +0000 | [diff] [blame] | 222 | outbuf[j++] = *p; |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 223 | if (*p == 0xff) |
Eric Andersen | 0e28e1f | 2002-04-26 07:20:47 +0000 | [diff] [blame] | 224 | outbuf[j++] = 0xff; |
| 225 | else if (*p == 0x0d) |
| 226 | outbuf[j++] = 0x00; |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 227 | } |
Eric Andersen | 0e28e1f | 2002-04-26 07:20:47 +0000 | [diff] [blame] | 228 | if (j > 0 ) |
| 229 | write(G.netfd, outbuf, j); |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 232 | |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 233 | static void handlenetinput(int len) |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 234 | { |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 235 | int i; |
| 236 | int cstart = 0; |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 237 | |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 238 | for (i = 0; i < len; i++) |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 239 | { |
| 240 | byte c = G.buf[i]; |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 241 | |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 242 | if (G.telstate == 0) /* most of the time state == 0 */ |
| 243 | { |
| 244 | if (c == IAC) |
| 245 | { |
| 246 | cstart = i; |
| 247 | G.telstate = TS_IAC; |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 248 | } |
| 249 | } |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 250 | else |
| 251 | switch (G.telstate) |
| 252 | { |
| 253 | case TS_0: |
| 254 | if (c == IAC) |
| 255 | G.telstate = TS_IAC; |
| 256 | else |
| 257 | G.buf[cstart++] = c; |
| 258 | break; |
| 259 | |
| 260 | case TS_IAC: |
| 261 | if (c == IAC) /* IAC IAC -> 0xFF */ |
| 262 | { |
| 263 | G.buf[cstart++] = c; |
| 264 | G.telstate = TS_0; |
| 265 | break; |
| 266 | } |
| 267 | /* else */ |
| 268 | switch (c) |
| 269 | { |
| 270 | case SB: |
| 271 | G.telstate = TS_SUB1; |
| 272 | break; |
| 273 | case DO: |
| 274 | case DONT: |
| 275 | case WILL: |
| 276 | case WONT: |
| 277 | G.telwish = c; |
| 278 | G.telstate = TS_OPT; |
| 279 | break; |
| 280 | default: |
| 281 | G.telstate = TS_0; /* DATA MARK must be added later */ |
| 282 | } |
| 283 | break; |
| 284 | case TS_OPT: /* WILL, WONT, DO, DONT */ |
| 285 | telopt(c); |
| 286 | G.telstate = TS_0; |
| 287 | break; |
| 288 | case TS_SUB1: /* Subnegotiation */ |
| 289 | case TS_SUB2: /* Subnegotiation */ |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 290 | if (subneg(c)) |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 291 | G.telstate = TS_0; |
| 292 | break; |
| 293 | } |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 294 | } |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 295 | if (G.telstate) |
| 296 | { |
| 297 | if (G.iaclen) iacflush(); |
| 298 | if (G.telstate == TS_0) G.telstate = 0; |
| 299 | |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 300 | len = cstart; |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 303 | if (len) |
| 304 | write(1, G.buf, len); |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 307 | |
| 308 | /* ******************************* */ |
| 309 | |
| 310 | static inline void putiac(int c) |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 311 | { |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 312 | G.iacbuf[G.iaclen++] = c; |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 315 | |
| 316 | static void putiac2(byte wwdd, byte c) |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 317 | { |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 318 | if (G.iaclen + 3 > IACBUFSIZE) |
| 319 | iacflush(); |
| 320 | |
| 321 | putiac(IAC); |
| 322 | putiac(wwdd); |
| 323 | putiac(c); |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 326 | #if 0 |
| 327 | static void putiac1(byte c) |
| 328 | { |
| 329 | if (G.iaclen + 2 > IACBUFSIZE) |
| 330 | iacflush(); |
| 331 | |
| 332 | putiac(IAC); |
| 333 | putiac(c); |
| 334 | } |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 335 | #endif |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 336 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 337 | #ifdef CONFIG_FEATURE_TELNET_TTYPE |
Eric Andersen | 7e1273e | 2001-05-07 17:57:45 +0000 | [diff] [blame] | 338 | static void putiac_subopt(byte c, char *str) |
| 339 | { |
| 340 | int len = strlen(str) + 6; // ( 2 + 1 + 1 + strlen + 2 ) |
| 341 | |
| 342 | if (G.iaclen + len > IACBUFSIZE) |
| 343 | iacflush(); |
| 344 | |
| 345 | putiac(IAC); |
| 346 | putiac(SB); |
| 347 | putiac(c); |
| 348 | putiac(0); |
| 349 | |
| 350 | while(*str) |
| 351 | putiac(*str++); |
| 352 | |
| 353 | putiac(IAC); |
| 354 | putiac(SE); |
| 355 | } |
| 356 | #endif |
| 357 | |
Eric Andersen | 0e28e1f | 2002-04-26 07:20:47 +0000 | [diff] [blame] | 358 | #ifdef CONFIG_FEATURE_AUTOWIDTH |
| 359 | static void putiac_naws(byte c, int x, int y) |
| 360 | { |
| 361 | if (G.iaclen + 9 > IACBUFSIZE) |
| 362 | iacflush(); |
| 363 | |
| 364 | putiac(IAC); |
| 365 | putiac(SB); |
| 366 | putiac(c); |
| 367 | |
| 368 | putiac((x >> 8) & 0xff); |
| 369 | putiac(x & 0xff); |
| 370 | putiac((y >> 8) & 0xff); |
| 371 | putiac(y & 0xff); |
| 372 | |
| 373 | putiac(IAC); |
| 374 | putiac(SE); |
| 375 | } |
| 376 | #endif |
| 377 | |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 378 | /* void putiacstring (subneg strings) */ |
| 379 | |
| 380 | /* ******************************* */ |
| 381 | |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 382 | static char const escapecharis[] = "\r\nEscape character is "; |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 383 | |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 384 | static void setConMode(void) |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 385 | { |
| 386 | if (G.telflags & UF_ECHO) |
| 387 | { |
| 388 | if (G.charmode == CHM_TRY) { |
| 389 | G.charmode = CHM_ON; |
Matt Kraai | 12f417e | 2001-01-18 02:57:08 +0000 | [diff] [blame] | 390 | printf("\r\nEntering character mode%s'^]'.\r\n", escapecharis); |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 391 | rawmode(); |
| 392 | } |
| 393 | } |
| 394 | else |
| 395 | { |
| 396 | if (G.charmode != CHM_OFF) { |
| 397 | G.charmode = CHM_OFF; |
Matt Kraai | 12f417e | 2001-01-18 02:57:08 +0000 | [diff] [blame] | 398 | printf("\r\nEntering line mode%s'^C'.\r\n", escapecharis); |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 399 | cookmode(); |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | /* ******************************* */ |
| 405 | |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 406 | static void will_charmode(void) |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 407 | { |
| 408 | G.charmode = CHM_TRY; |
| 409 | G.telflags |= (UF_ECHO | UF_SGA); |
| 410 | setConMode(); |
| 411 | |
| 412 | putiac2(DO, TELOPT_ECHO); |
| 413 | putiac2(DO, TELOPT_SGA); |
| 414 | iacflush(); |
| 415 | } |
| 416 | |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 417 | static void do_linemode(void) |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 418 | { |
| 419 | G.charmode = CHM_TRY; |
| 420 | G.telflags &= ~(UF_ECHO | UF_SGA); |
| 421 | setConMode(); |
| 422 | |
| 423 | putiac2(DONT, TELOPT_ECHO); |
| 424 | putiac2(DONT, TELOPT_SGA); |
| 425 | iacflush(); |
| 426 | } |
| 427 | |
| 428 | /* ******************************* */ |
| 429 | |
| 430 | static inline void to_notsup(char c) |
| 431 | { |
| 432 | if (G.telwish == WILL) putiac2(DONT, c); |
| 433 | else if (G.telwish == DO) putiac2(WONT, c); |
| 434 | } |
| 435 | |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 436 | static inline void to_echo(void) |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 437 | { |
| 438 | /* if server requests ECHO, don't agree */ |
| 439 | if (G.telwish == DO) { putiac2(WONT, TELOPT_ECHO); return; } |
| 440 | else if (G.telwish == DONT) return; |
| 441 | |
| 442 | if (G.telflags & UF_ECHO) |
| 443 | { |
| 444 | if (G.telwish == WILL) |
| 445 | return; |
| 446 | } |
| 447 | else |
| 448 | if (G.telwish == WONT) |
| 449 | return; |
| 450 | |
| 451 | if (G.charmode != CHM_OFF) |
| 452 | G.telflags ^= UF_ECHO; |
| 453 | |
| 454 | if (G.telflags & UF_ECHO) |
| 455 | putiac2(DO, TELOPT_ECHO); |
| 456 | else |
| 457 | putiac2(DONT, TELOPT_ECHO); |
| 458 | |
| 459 | setConMode(); |
| 460 | WriteCS(1, "\r\n"); /* sudden modec */ |
| 461 | } |
| 462 | |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 463 | static inline void to_sga(void) |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 464 | { |
| 465 | /* daemon always sends will/wont, client do/dont */ |
| 466 | |
| 467 | if (G.telflags & UF_SGA) |
| 468 | { |
| 469 | if (G.telwish == WILL) |
| 470 | return; |
| 471 | } |
| 472 | else |
| 473 | if (G.telwish == WONT) |
| 474 | return; |
| 475 | |
| 476 | if ((G.telflags ^= UF_SGA) & UF_SGA) /* toggle */ |
| 477 | putiac2(DO, TELOPT_SGA); |
| 478 | else |
| 479 | putiac2(DONT, TELOPT_SGA); |
| 480 | |
| 481 | return; |
| 482 | } |
| 483 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 484 | #ifdef CONFIG_FEATURE_TELNET_TTYPE |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 485 | static inline void to_ttype(void) |
Eric Andersen | 7e1273e | 2001-05-07 17:57:45 +0000 | [diff] [blame] | 486 | { |
| 487 | /* Tell server we will (or won't) do TTYPE */ |
| 488 | |
| 489 | if(ttype) |
| 490 | putiac2(WILL, TELOPT_TTYPE); |
| 491 | else |
| 492 | putiac2(WONT, TELOPT_TTYPE); |
| 493 | |
| 494 | return; |
| 495 | } |
| 496 | #endif |
| 497 | |
Eric Andersen | 0e28e1f | 2002-04-26 07:20:47 +0000 | [diff] [blame] | 498 | #ifdef CONFIG_FEATURE_AUTOWIDTH |
Tim Riker | ed8e036 | 2002-04-26 07:53:39 +0000 | [diff] [blame] | 499 | static inline void to_naws(void) |
Eric Andersen | 0e28e1f | 2002-04-26 07:20:47 +0000 | [diff] [blame] | 500 | { |
| 501 | /* Tell server we will do NAWS */ |
| 502 | putiac2(WILL, TELOPT_NAWS); |
| 503 | return; |
| 504 | } |
| 505 | #endif |
| 506 | |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 507 | static void telopt(byte c) |
| 508 | { |
| 509 | switch (c) |
| 510 | { |
Eric Andersen | 4163406 | 2002-04-26 08:44:17 +0000 | [diff] [blame] | 511 | case TELOPT_ECHO: to_echo(); break; |
| 512 | case TELOPT_SGA: to_sga(); break; |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 513 | #ifdef CONFIG_FEATURE_TELNET_TTYPE |
Eric Andersen | 4163406 | 2002-04-26 08:44:17 +0000 | [diff] [blame] | 514 | case TELOPT_TTYPE: to_ttype();break; |
Eric Andersen | 7e1273e | 2001-05-07 17:57:45 +0000 | [diff] [blame] | 515 | #endif |
Eric Andersen | 0e28e1f | 2002-04-26 07:20:47 +0000 | [diff] [blame] | 516 | #ifdef CONFIG_FEATURE_AUTOWIDTH |
Eric Andersen | 4163406 | 2002-04-26 08:44:17 +0000 | [diff] [blame] | 517 | case TELOPT_NAWS: to_naws(); |
Eric Andersen | 0e28e1f | 2002-04-26 07:20:47 +0000 | [diff] [blame] | 518 | putiac_naws(c, win_width, win_height); |
| 519 | break; |
| 520 | #endif |
Eric Andersen | 8db361b | 2002-04-26 08:00:33 +0000 | [diff] [blame] | 521 | default: to_notsup(c); |
| 522 | break; |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 523 | } |
| 524 | } |
| 525 | |
| 526 | |
| 527 | /* ******************************* */ |
| 528 | |
Eric Andersen | 0e28e1f | 2002-04-26 07:20:47 +0000 | [diff] [blame] | 529 | /* subnegotiation -- ignore all (except TTYPE,NAWS) */ |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 530 | |
| 531 | static int subneg(byte c) |
| 532 | { |
| 533 | switch (G.telstate) |
| 534 | { |
| 535 | case TS_SUB1: |
| 536 | if (c == IAC) |
| 537 | G.telstate = TS_SUB2; |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 538 | #ifdef CONFIG_FEATURE_TELNET_TTYPE |
Eric Andersen | 7e1273e | 2001-05-07 17:57:45 +0000 | [diff] [blame] | 539 | else |
| 540 | if (c == TELOPT_TTYPE) |
| 541 | putiac_subopt(TELOPT_TTYPE,ttype); |
| 542 | #endif |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 543 | break; |
| 544 | case TS_SUB2: |
| 545 | if (c == SE) |
| 546 | return TRUE; |
| 547 | G.telstate = TS_SUB1; |
| 548 | /* break; */ |
| 549 | } |
| 550 | return FALSE; |
| 551 | } |
| 552 | |
| 553 | /* ******************************* */ |
| 554 | |
| 555 | static void fgotsig(int sig) |
| 556 | { |
| 557 | G.gotsig = sig; |
| 558 | } |
| 559 | |
| 560 | |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 561 | static void rawmode(void) |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 562 | { |
| 563 | tcsetattr(0, TCSADRAIN, &G.termios_raw); |
| 564 | } |
| 565 | |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 566 | static void cookmode(void) |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 567 | { |
| 568 | tcsetattr(0, TCSADRAIN, &G.termios_def); |
| 569 | } |
| 570 | |
| 571 | extern int telnet_main(int argc, char** argv) |
| 572 | { |
Eric Andersen | 0b31586 | 2002-07-03 11:51:44 +0000 | [diff] [blame] | 573 | char *host; |
| 574 | char *port; |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 575 | int len; |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 576 | #ifdef USE_POLL |
| 577 | struct pollfd ufds[2]; |
| 578 | #else |
| 579 | fd_set readfds; |
| 580 | int maxfd; |
| 581 | #endif |
| 582 | |
Eric Andersen | 0e28e1f | 2002-04-26 07:20:47 +0000 | [diff] [blame] | 583 | #ifdef CONFIG_FEATURE_AUTOWIDTH |
Eric Andersen | 8efe967 | 2003-09-15 08:33:45 +0000 | [diff] [blame^] | 584 | get_terminal_width_height(0, &win_width, &win_height); |
Eric Andersen | 0e28e1f | 2002-04-26 07:20:47 +0000 | [diff] [blame] | 585 | #endif |
| 586 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 587 | #ifdef CONFIG_FEATURE_TELNET_TTYPE |
Eric Andersen | 7e1273e | 2001-05-07 17:57:45 +0000 | [diff] [blame] | 588 | ttype = getenv("TERM"); |
| 589 | #endif |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 590 | |
| 591 | memset(&G, 0, sizeof G); |
| 592 | |
| 593 | if (tcgetattr(0, &G.termios_def) < 0) |
| 594 | exit(1); |
| 595 | |
| 596 | G.termios_raw = G.termios_def; |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 597 | cfmakeraw(&G.termios_raw); |
| 598 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 599 | if (argc < 2) bb_show_usage(); |
Eric Andersen | 0b31586 | 2002-07-03 11:51:44 +0000 | [diff] [blame] | 600 | port = (argc > 2)? argv[2] : "23"; |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 601 | |
Eric Andersen | 0b31586 | 2002-07-03 11:51:44 +0000 | [diff] [blame] | 602 | host = argv[1]; |
| 603 | |
| 604 | G.netfd = xconnect(host, port); |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 605 | |
Eric Andersen | 0b31586 | 2002-07-03 11:51:44 +0000 | [diff] [blame] | 606 | setsockopt(G.netfd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof one); |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 607 | |
| 608 | signal(SIGINT, fgotsig); |
| 609 | |
| 610 | #ifdef USE_POLL |
| 611 | ufds[0].fd = 0; ufds[1].fd = G.netfd; |
| 612 | ufds[0].events = ufds[1].events = POLLIN; |
| 613 | #else |
| 614 | FD_ZERO(&readfds); |
| 615 | FD_SET(0, &readfds); |
| 616 | FD_SET(G.netfd, &readfds); |
| 617 | maxfd = G.netfd + 1; |
| 618 | #endif |
| 619 | |
| 620 | while (1) |
| 621 | { |
| 622 | #ifndef USE_POLL |
| 623 | fd_set rfds = readfds; |
| 624 | |
| 625 | switch (select(maxfd, &rfds, NULL, NULL, NULL)) |
| 626 | #else |
| 627 | switch (poll(ufds, 2, -1)) |
| 628 | #endif |
| 629 | { |
| 630 | case 0: |
| 631 | /* timeout */ |
| 632 | case -1: |
| 633 | /* error, ignore and/or log something, bay go to loop */ |
| 634 | if (G.gotsig) |
| 635 | conescape(); |
| 636 | else |
| 637 | sleep(1); |
| 638 | break; |
| 639 | default: |
| 640 | |
| 641 | #ifdef USE_POLL |
| 642 | if (ufds[0].revents) /* well, should check POLLIN, but ... */ |
| 643 | #else |
| 644 | if (FD_ISSET(0, &rfds)) |
| 645 | #endif |
| 646 | { |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 647 | len = read(0, G.buf, DATABUFSIZE); |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 648 | |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 649 | if (len <= 0) |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 650 | doexit(0); |
| 651 | |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 652 | TRACE(0, ("Read con: %d\n", len)); |
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 | handlenetoutput(len); |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | #ifdef USE_POLL |
| 658 | if (ufds[1].revents) /* well, should check POLLIN, but ... */ |
| 659 | #else |
| 660 | if (FD_ISSET(G.netfd, &rfds)) |
| 661 | #endif |
| 662 | { |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 663 | len = read(G.netfd, G.buf, DATABUFSIZE); |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 664 | |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 665 | if (len <= 0) |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 666 | { |
| 667 | WriteCS(1, "Connection closed by foreign host.\r\n"); |
| 668 | doexit(1); |
| 669 | } |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 670 | TRACE(0, ("Read netfd (%d): %d\n", G.netfd, len)); |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 671 | |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 672 | handlenetinput(len); |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 673 | } |
| 674 | } |
| 675 | } |
| 676 | } |
| 677 | |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 678 | /* |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 679 | Local Variables: |
| 680 | c-file-style: "linux" |
| 681 | c-basic-offset: 4 |
| 682 | tab-width: 4 |
| 683 | End: |
| 684 | */ |