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