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