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