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