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