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 |
| 28 | * Modified 2000/06/13 for inclusion into BusyBox by Erik Andersen |
| 29 | * <andersen@lineo.com> |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 30 | * |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 31 | */ |
| 32 | |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 33 | #include <termios.h> |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 34 | #include <unistd.h> |
| 35 | #include <errno.h> |
| 36 | #include <stdlib.h> |
| 37 | #include <stdarg.h> |
| 38 | #include <string.h> |
| 39 | #include <signal.h> |
| 40 | #include <arpa/telnet.h> |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 41 | #include <sys/types.h> |
| 42 | #include <sys/socket.h> |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 43 | #include <netinet/in.h> |
| 44 | #include <netdb.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame^] | 45 | #include "busybox.h" |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 46 | |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 47 | #if 0 |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 48 | static const int DOTRACE = 1; |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 49 | #endif |
| 50 | |
Pavel Roskin | 616d13b | 2000-07-28 19:38:27 +0000 | [diff] [blame] | 51 | #ifdef DOTRACE |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 52 | #include <arpa/inet.h> /* for inet_ntoa()... */ |
| 53 | #define TRACE(x, y) do { if (x) printf y; } while (0) |
| 54 | #else |
| 55 | #define TRACE(x, y) |
| 56 | #endif |
| 57 | |
| 58 | #if 0 |
| 59 | #define USE_POLL |
| 60 | #include <sys/poll.h> |
| 61 | #else |
| 62 | #include <sys/time.h> |
| 63 | #endif |
| 64 | |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 65 | static const int DATABUFSIZE = 128; |
| 66 | static const int IACBUFSIZE = 128; |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 67 | |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 68 | static const int CHM_TRY = 0; |
| 69 | static const int CHM_ON = 1; |
| 70 | static const int CHM_OFF = 2; |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 71 | |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 72 | static const int UF_ECHO = 0x01; |
| 73 | static const int UF_SGA = 0x02; |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 74 | |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 75 | enum { |
| 76 | TS_0 = 1, |
| 77 | TS_IAC = 2, |
| 78 | TS_OPT = 3, |
| 79 | TS_SUB1 = 4, |
| 80 | TS_SUB2 = 5, |
| 81 | }; |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 82 | |
| 83 | #define WriteCS(fd, str) write(fd, str, sizeof str -1) |
| 84 | |
| 85 | typedef unsigned char byte; |
| 86 | |
| 87 | /* use globals to reduce size ??? */ /* test this hypothesis later */ |
| 88 | struct Globalvars { |
| 89 | int netfd; /* console fd:s are 0 and 1 (and 2) */ |
| 90 | /* same buffer used both for network and console read/write */ |
| 91 | char * buf; /* allocating so static size is smaller */ |
| 92 | short len; |
| 93 | byte telstate; /* telnet negotiation state from network input */ |
| 94 | byte telwish; /* DO, DONT, WILL, WONT */ |
| 95 | byte charmode; |
| 96 | byte telflags; |
| 97 | byte gotsig; |
| 98 | /* buffer to handle telnet negotiations */ |
| 99 | char * iacbuf; |
| 100 | short iaclen; /* could even use byte */ |
| 101 | struct termios termios_def; |
| 102 | struct termios termios_raw; |
| 103 | } G; |
| 104 | |
| 105 | #define xUSE_GLOBALVAR_PTR /* xUSE... -> don't use :D (makes smaller code) */ |
| 106 | |
| 107 | #ifdef USE_GLOBALVAR_PTR |
| 108 | struct Globalvars * Gptr; |
| 109 | #define G (*Gptr) |
| 110 | #else |
| 111 | struct Globalvars G; |
| 112 | #endif |
| 113 | |
| 114 | static inline void iacflush() |
| 115 | { |
| 116 | write(G.netfd, G.iacbuf, G.iaclen); |
| 117 | G.iaclen = 0; |
| 118 | } |
| 119 | |
| 120 | /* Function prototypes */ |
| 121 | static int getport(char * p); |
| 122 | static struct in_addr getserver(char * p); |
| 123 | static int create_socket(); |
| 124 | static void setup_sockaddr_in(struct sockaddr_in * addr, int port); |
| 125 | static int remote_connect(struct in_addr addr, int port); |
| 126 | static void rawmode(); |
| 127 | static void cookmode(); |
| 128 | static void do_linemode(); |
| 129 | static void will_charmode(); |
| 130 | static void telopt(byte c); |
| 131 | static int subneg(byte c); |
| 132 | #if 0 |
| 133 | static int local_bind(int port); |
| 134 | #endif |
| 135 | |
| 136 | /* Some globals */ |
| 137 | static int one = 1; |
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 | static void doexit(int ev) |
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 | cookmode(); |
| 142 | exit(ev); |
| 143 | } |
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 | static void conescape() |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 146 | { |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 147 | char b; |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 148 | |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 149 | if (G.gotsig) /* came from line mode... go raw */ |
| 150 | rawmode(); |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 151 | |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 152 | WriteCS(1, "\r\nConsole escape. Commands are:\r\n\n" |
| 153 | " l go to line mode\r\n" |
| 154 | " c go to character mode\r\n" |
| 155 | " z suspend telnet\r\n" |
| 156 | " e exit telnet\r\n"); |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 157 | |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 158 | if (read(0, &b, 1) <= 0) |
| 159 | doexit(1); |
| 160 | |
| 161 | switch (b) |
| 162 | { |
| 163 | case 'l': |
| 164 | if (!G.gotsig) |
| 165 | { |
| 166 | do_linemode(); |
| 167 | goto rrturn; |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 168 | } |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 169 | break; |
| 170 | case 'c': |
| 171 | if (G.gotsig) |
| 172 | { |
| 173 | will_charmode(); |
| 174 | goto rrturn; |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 175 | } |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 176 | break; |
| 177 | case 'z': |
| 178 | cookmode(); |
| 179 | kill(0, SIGTSTP); |
| 180 | rawmode(); |
| 181 | break; |
| 182 | case 'e': |
| 183 | doexit(0); |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 184 | } |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 185 | |
| 186 | WriteCS(1, "continuing...\r\n"); |
| 187 | |
| 188 | if (G.gotsig) |
| 189 | cookmode(); |
| 190 | |
| 191 | rrturn: |
| 192 | G.gotsig = 0; |
| 193 | |
| 194 | } |
| 195 | static void handlenetoutput() |
| 196 | { |
| 197 | /* here we could do smart tricks how to handle 0xFF:s in output |
| 198 | * stream like writing twice every sequence of FF:s (thus doing |
| 199 | * many write()s. But I think interactive telnet application does |
| 200 | * not need to be 100% 8-bit clean, so changing every 0xff:s to |
| 201 | * 0x7f:s */ |
| 202 | |
| 203 | int i; |
| 204 | byte * p = G.buf; |
| 205 | |
| 206 | for (i = G.len; i > 0; i--, p++) |
| 207 | { |
| 208 | if (*p == 0x1d) |
| 209 | { |
| 210 | conescape(); |
| 211 | return; |
| 212 | } |
| 213 | if (*p == 0xff) |
| 214 | *p = 0x7f; |
| 215 | } |
| 216 | write(G.netfd, G.buf, G.len); |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 219 | |
| 220 | static void handlenetinput() |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 221 | { |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 222 | int i; |
| 223 | int cstart = 0; |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 224 | |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 225 | for (i = 0; i < G.len; i++) |
| 226 | { |
| 227 | byte c = G.buf[i]; |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 228 | |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 229 | if (G.telstate == 0) /* most of the time state == 0 */ |
| 230 | { |
| 231 | if (c == IAC) |
| 232 | { |
| 233 | cstart = i; |
| 234 | G.telstate = TS_IAC; |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 235 | } |
| 236 | } |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 237 | else |
| 238 | switch (G.telstate) |
| 239 | { |
| 240 | case TS_0: |
| 241 | if (c == IAC) |
| 242 | G.telstate = TS_IAC; |
| 243 | else |
| 244 | G.buf[cstart++] = c; |
| 245 | break; |
| 246 | |
| 247 | case TS_IAC: |
| 248 | if (c == IAC) /* IAC IAC -> 0xFF */ |
| 249 | { |
| 250 | G.buf[cstart++] = c; |
| 251 | G.telstate = TS_0; |
| 252 | break; |
| 253 | } |
| 254 | /* else */ |
| 255 | switch (c) |
| 256 | { |
| 257 | case SB: |
| 258 | G.telstate = TS_SUB1; |
| 259 | break; |
| 260 | case DO: |
| 261 | case DONT: |
| 262 | case WILL: |
| 263 | case WONT: |
| 264 | G.telwish = c; |
| 265 | G.telstate = TS_OPT; |
| 266 | break; |
| 267 | default: |
| 268 | G.telstate = TS_0; /* DATA MARK must be added later */ |
| 269 | } |
| 270 | break; |
| 271 | case TS_OPT: /* WILL, WONT, DO, DONT */ |
| 272 | telopt(c); |
| 273 | G.telstate = TS_0; |
| 274 | break; |
| 275 | case TS_SUB1: /* Subnegotiation */ |
| 276 | case TS_SUB2: /* Subnegotiation */ |
| 277 | if (subneg(c) == TRUE) |
| 278 | G.telstate = TS_0; |
| 279 | break; |
| 280 | } |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 281 | } |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 282 | if (G.telstate) |
| 283 | { |
| 284 | if (G.iaclen) iacflush(); |
| 285 | if (G.telstate == TS_0) G.telstate = 0; |
| 286 | |
| 287 | G.len = cstart; |
| 288 | } |
| 289 | |
| 290 | if (G.len) |
| 291 | write(1, G.buf, G.len); |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 294 | |
| 295 | /* ******************************* */ |
| 296 | |
| 297 | static inline void putiac(int c) |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 298 | { |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 299 | G.iacbuf[G.iaclen++] = c; |
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 | static void putiac2(byte wwdd, byte c) |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 304 | { |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 305 | if (G.iaclen + 3 > IACBUFSIZE) |
| 306 | iacflush(); |
| 307 | |
| 308 | putiac(IAC); |
| 309 | putiac(wwdd); |
| 310 | putiac(c); |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 313 | #if 0 |
| 314 | static void putiac1(byte c) |
| 315 | { |
| 316 | if (G.iaclen + 2 > IACBUFSIZE) |
| 317 | iacflush(); |
| 318 | |
| 319 | putiac(IAC); |
| 320 | putiac(c); |
| 321 | } |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 322 | #endif |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 323 | |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 324 | /* void putiacstring (subneg strings) */ |
| 325 | |
| 326 | /* ******************************* */ |
| 327 | |
| 328 | char const escapecharis[] = "\r\nEscape character is "; |
| 329 | |
| 330 | static void setConMode() |
| 331 | { |
| 332 | if (G.telflags & UF_ECHO) |
| 333 | { |
| 334 | if (G.charmode == CHM_TRY) { |
| 335 | G.charmode = CHM_ON; |
Matt Kraai | 12f417e | 2001-01-18 02:57:08 +0000 | [diff] [blame] | 336 | printf("\r\nEntering character mode%s'^]'.\r\n", escapecharis); |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 337 | rawmode(); |
| 338 | } |
| 339 | } |
| 340 | else |
| 341 | { |
| 342 | if (G.charmode != CHM_OFF) { |
| 343 | G.charmode = CHM_OFF; |
Matt Kraai | 12f417e | 2001-01-18 02:57:08 +0000 | [diff] [blame] | 344 | printf("\r\nEntering line mode%s'^C'.\r\n", escapecharis); |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 345 | cookmode(); |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | /* ******************************* */ |
| 351 | |
| 352 | static void will_charmode() |
| 353 | { |
| 354 | G.charmode = CHM_TRY; |
| 355 | G.telflags |= (UF_ECHO | UF_SGA); |
| 356 | setConMode(); |
| 357 | |
| 358 | putiac2(DO, TELOPT_ECHO); |
| 359 | putiac2(DO, TELOPT_SGA); |
| 360 | iacflush(); |
| 361 | } |
| 362 | |
| 363 | static void do_linemode() |
| 364 | { |
| 365 | G.charmode = CHM_TRY; |
| 366 | G.telflags &= ~(UF_ECHO | UF_SGA); |
| 367 | setConMode(); |
| 368 | |
| 369 | putiac2(DONT, TELOPT_ECHO); |
| 370 | putiac2(DONT, TELOPT_SGA); |
| 371 | iacflush(); |
| 372 | } |
| 373 | |
| 374 | /* ******************************* */ |
| 375 | |
| 376 | static inline void to_notsup(char c) |
| 377 | { |
| 378 | if (G.telwish == WILL) putiac2(DONT, c); |
| 379 | else if (G.telwish == DO) putiac2(WONT, c); |
| 380 | } |
| 381 | |
| 382 | static inline void to_echo() |
| 383 | { |
| 384 | /* if server requests ECHO, don't agree */ |
| 385 | if (G.telwish == DO) { putiac2(WONT, TELOPT_ECHO); return; } |
| 386 | else if (G.telwish == DONT) return; |
| 387 | |
| 388 | if (G.telflags & UF_ECHO) |
| 389 | { |
| 390 | if (G.telwish == WILL) |
| 391 | return; |
| 392 | } |
| 393 | else |
| 394 | if (G.telwish == WONT) |
| 395 | return; |
| 396 | |
| 397 | if (G.charmode != CHM_OFF) |
| 398 | G.telflags ^= UF_ECHO; |
| 399 | |
| 400 | if (G.telflags & UF_ECHO) |
| 401 | putiac2(DO, TELOPT_ECHO); |
| 402 | else |
| 403 | putiac2(DONT, TELOPT_ECHO); |
| 404 | |
| 405 | setConMode(); |
| 406 | WriteCS(1, "\r\n"); /* sudden modec */ |
| 407 | } |
| 408 | |
| 409 | static inline void to_sga() |
| 410 | { |
| 411 | /* daemon always sends will/wont, client do/dont */ |
| 412 | |
| 413 | if (G.telflags & UF_SGA) |
| 414 | { |
| 415 | if (G.telwish == WILL) |
| 416 | return; |
| 417 | } |
| 418 | else |
| 419 | if (G.telwish == WONT) |
| 420 | return; |
| 421 | |
| 422 | if ((G.telflags ^= UF_SGA) & UF_SGA) /* toggle */ |
| 423 | putiac2(DO, TELOPT_SGA); |
| 424 | else |
| 425 | putiac2(DONT, TELOPT_SGA); |
| 426 | |
| 427 | return; |
| 428 | } |
| 429 | |
| 430 | static void telopt(byte c) |
| 431 | { |
| 432 | switch (c) |
| 433 | { |
| 434 | case TELOPT_ECHO: to_echo(c); break; |
| 435 | case TELOPT_SGA: to_sga(c); break; |
| 436 | default: to_notsup(c); break; |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | |
| 441 | /* ******************************* */ |
| 442 | |
| 443 | /* subnegotiation -- ignore all */ |
| 444 | |
| 445 | static int subneg(byte c) |
| 446 | { |
| 447 | switch (G.telstate) |
| 448 | { |
| 449 | case TS_SUB1: |
| 450 | if (c == IAC) |
| 451 | G.telstate = TS_SUB2; |
| 452 | break; |
| 453 | case TS_SUB2: |
| 454 | if (c == SE) |
| 455 | return TRUE; |
| 456 | G.telstate = TS_SUB1; |
| 457 | /* break; */ |
| 458 | } |
| 459 | return FALSE; |
| 460 | } |
| 461 | |
| 462 | /* ******************************* */ |
| 463 | |
| 464 | static void fgotsig(int sig) |
| 465 | { |
| 466 | G.gotsig = sig; |
| 467 | } |
| 468 | |
| 469 | |
| 470 | static void rawmode() |
| 471 | { |
| 472 | tcsetattr(0, TCSADRAIN, &G.termios_raw); |
| 473 | } |
| 474 | |
| 475 | static void cookmode() |
| 476 | { |
| 477 | tcsetattr(0, TCSADRAIN, &G.termios_def); |
| 478 | } |
| 479 | |
| 480 | extern int telnet_main(int argc, char** argv) |
| 481 | { |
| 482 | struct in_addr host; |
| 483 | int port; |
| 484 | #ifdef USE_POLL |
| 485 | struct pollfd ufds[2]; |
| 486 | #else |
| 487 | fd_set readfds; |
| 488 | int maxfd; |
| 489 | #endif |
| 490 | |
| 491 | |
| 492 | memset(&G, 0, sizeof G); |
| 493 | |
| 494 | if (tcgetattr(0, &G.termios_def) < 0) |
| 495 | exit(1); |
| 496 | |
| 497 | G.termios_raw = G.termios_def; |
| 498 | |
| 499 | cfmakeraw(&G.termios_raw); |
| 500 | |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 501 | if (argc < 2) show_usage(); |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 502 | port = (argc > 2)? getport(argv[2]): 23; |
| 503 | |
| 504 | G.buf = xmalloc(DATABUFSIZE); |
| 505 | G.iacbuf = xmalloc(IACBUFSIZE); |
| 506 | |
| 507 | host = getserver(argv[1]); |
| 508 | |
| 509 | G.netfd = remote_connect(host, port); |
| 510 | |
| 511 | signal(SIGINT, fgotsig); |
| 512 | |
| 513 | #ifdef USE_POLL |
| 514 | ufds[0].fd = 0; ufds[1].fd = G.netfd; |
| 515 | ufds[0].events = ufds[1].events = POLLIN; |
| 516 | #else |
| 517 | FD_ZERO(&readfds); |
| 518 | FD_SET(0, &readfds); |
| 519 | FD_SET(G.netfd, &readfds); |
| 520 | maxfd = G.netfd + 1; |
| 521 | #endif |
| 522 | |
| 523 | while (1) |
| 524 | { |
| 525 | #ifndef USE_POLL |
| 526 | fd_set rfds = readfds; |
| 527 | |
| 528 | switch (select(maxfd, &rfds, NULL, NULL, NULL)) |
| 529 | #else |
| 530 | switch (poll(ufds, 2, -1)) |
| 531 | #endif |
| 532 | { |
| 533 | case 0: |
| 534 | /* timeout */ |
| 535 | case -1: |
| 536 | /* error, ignore and/or log something, bay go to loop */ |
| 537 | if (G.gotsig) |
| 538 | conescape(); |
| 539 | else |
| 540 | sleep(1); |
| 541 | break; |
| 542 | default: |
| 543 | |
| 544 | #ifdef USE_POLL |
| 545 | if (ufds[0].revents) /* well, should check POLLIN, but ... */ |
| 546 | #else |
| 547 | if (FD_ISSET(0, &rfds)) |
| 548 | #endif |
| 549 | { |
| 550 | G.len = read(0, G.buf, DATABUFSIZE); |
| 551 | |
| 552 | if (G.len <= 0) |
| 553 | doexit(0); |
| 554 | |
| 555 | TRACE(0, ("Read con: %d\n", G.len)); |
| 556 | |
| 557 | handlenetoutput(); |
| 558 | } |
| 559 | |
| 560 | #ifdef USE_POLL |
| 561 | if (ufds[1].revents) /* well, should check POLLIN, but ... */ |
| 562 | #else |
| 563 | if (FD_ISSET(G.netfd, &rfds)) |
| 564 | #endif |
| 565 | { |
| 566 | G.len = read(G.netfd, G.buf, DATABUFSIZE); |
| 567 | |
| 568 | if (G.len <= 0) |
| 569 | { |
| 570 | WriteCS(1, "Connection closed by foreign host.\r\n"); |
| 571 | doexit(1); |
| 572 | } |
| 573 | TRACE(0, ("Read netfd (%d): %d\n", G.netfd, G.len)); |
| 574 | |
| 575 | handlenetinput(); |
| 576 | } |
| 577 | } |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | static int getport(char * p) |
| 582 | { |
| 583 | unsigned int port = atoi(p); |
| 584 | |
| 585 | if ((unsigned)(port - 1 ) > 65534) |
| 586 | { |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 587 | error_msg_and_die("%s: bad port number", p); |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 588 | } |
| 589 | return port; |
| 590 | } |
| 591 | |
| 592 | static struct in_addr getserver(char * host) |
| 593 | { |
| 594 | struct in_addr addr; |
| 595 | |
| 596 | struct hostent * he; |
| 597 | if ((he = gethostbyname(host)) == NULL) |
| 598 | { |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 599 | error_msg_and_die("%s: Unknown host", host); |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 600 | } |
| 601 | memcpy(&addr, he->h_addr, sizeof addr); |
| 602 | |
| 603 | TRACE(1, ("addr: %s\n", inet_ntoa(addr))); |
| 604 | |
| 605 | return addr; |
| 606 | } |
| 607 | |
| 608 | static int create_socket() |
| 609 | { |
| 610 | return socket(AF_INET, SOCK_STREAM, 0); |
| 611 | } |
| 612 | |
| 613 | static void setup_sockaddr_in(struct sockaddr_in * addr, int port) |
| 614 | { |
| 615 | memset(addr, 0, sizeof addr); |
| 616 | addr->sin_family = AF_INET; |
| 617 | addr->sin_port = htons(port); |
| 618 | } |
| 619 | |
| 620 | #if 0 |
| 621 | static int local_bind(int port) |
| 622 | { |
| 623 | struct sockaddr_in s_addr; |
| 624 | int s = create_socket(); |
| 625 | |
| 626 | setup_sockaddr_in(&s_addr, port); |
| 627 | |
| 628 | setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one); |
| 629 | |
| 630 | if (bind(s, &s_addr, sizeof s_addr) < 0) |
| 631 | { |
| 632 | char * e = sys_errlist[errno]; |
| 633 | syserrorexit("bind"); |
| 634 | exit(1); |
| 635 | } |
| 636 | listen(s, 1); |
| 637 | |
| 638 | return s; |
| 639 | } |
| 640 | #endif |
| 641 | |
| 642 | static int remote_connect(struct in_addr addr, int port) |
| 643 | { |
| 644 | struct sockaddr_in s_addr; |
| 645 | int s = create_socket(); |
| 646 | |
| 647 | setup_sockaddr_in(&s_addr, port); |
| 648 | s_addr.sin_addr = addr; |
| 649 | |
| 650 | setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof one); |
| 651 | |
| 652 | if (connect(s, (struct sockaddr *)&s_addr, sizeof s_addr) < 0) |
| 653 | { |
Matt Kraai | 1fa1ade | 2000-12-18 03:57:16 +0000 | [diff] [blame] | 654 | perror_msg_and_die("Unable to connect to remote host"); |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 655 | } |
| 656 | return s; |
Erik Andersen | f7c49ef | 2000-02-22 17:17:45 +0000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | /* |
Eric Andersen | 28c70b3 | 2000-06-14 20:42:57 +0000 | [diff] [blame] | 660 | Local Variables: |
| 661 | c-file-style: "linux" |
| 662 | c-basic-offset: 4 |
| 663 | tab-width: 4 |
| 664 | End: |
| 665 | */ |
| 666 | |