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