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