blob: ac6ec98ded4bf4b30dcb96f1bfb35d786ddf2eec [file] [log] [blame]
Eric Andersen28c70b32000-06-14 20:42:57 +00001/* vi: set sw=4 ts=4: */
Erik Andersenf7c49ef2000-02-22 17:17:45 +00002/*
Eric Andersen28c70b32000-06-14 20:42:57 +00003 * telnet implementation for busybox
Erik Andersenf7c49ef2000-02-22 17:17:45 +00004 *
Eric Andersen28c70b32000-06-14 20:42:57 +00005 * 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 Andersenf7c49ef2000-02-22 17:17:45 +000010 *
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 Andersen28c70b32000-06-14 20:42:57 +000025 * HISTORY
26 * Revision 3.1 1994/04/17 11:31:54 too
27 * initial revision
Eric Andersencb81e642003-07-14 21:21:08 +000028 * Modified 2000/06/13 for inclusion into BusyBox by Erik Andersen <andersen@codepoet.org>
Eric Andersen7e1273e2001-05-07 17:57:45 +000029 * Modified 2001/05/07 to add ability to pass TTYPE to remote host by Jim McQuillan
30 * <jam@ltsp.org>
Erik Andersenf7c49ef2000-02-22 17:17:45 +000031 *
Erik Andersenf7c49ef2000-02-22 17:17:45 +000032 */
33
Erik Andersenf7c49ef2000-02-22 17:17:45 +000034#include <termios.h>
Eric Andersen28c70b32000-06-14 20:42:57 +000035#include <unistd.h>
36#include <errno.h>
37#include <stdlib.h>
38#include <stdarg.h>
Eric Andersen2276d832002-07-11 11:11:56 +000039#include <string.h>
Eric Andersen28c70b32000-06-14 20:42:57 +000040#include <signal.h>
41#include <arpa/telnet.h>
Erik Andersenf7c49ef2000-02-22 17:17:45 +000042#include <sys/types.h>
43#include <sys/socket.h>
Eric Andersen28c70b32000-06-14 20:42:57 +000044#include <netinet/in.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000045#include "busybox.h"
Erik Andersenf7c49ef2000-02-22 17:17:45 +000046
Eric Andersen28c70b32000-06-14 20:42:57 +000047#if 0
Mark Whitley59ab0252001-01-23 22:30:04 +000048static const int DOTRACE = 1;
Eric Andersen28c70b32000-06-14 20:42:57 +000049#endif
50
Pavel Roskin616d13b2000-07-28 19:38:27 +000051#ifdef DOTRACE
Eric Andersen28c70b32000-06-14 20:42:57 +000052#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
Glenn L McGrath78b0e372001-06-26 02:06:08 +000065#define DATABUFSIZE 128
66#define IACBUFSIZE 128
Eric Andersen28c70b32000-06-14 20:42:57 +000067
Mark Whitley59ab0252001-01-23 22:30:04 +000068static const int CHM_TRY = 0;
69static const int CHM_ON = 1;
70static const int CHM_OFF = 2;
Eric Andersen28c70b32000-06-14 20:42:57 +000071
Mark Whitley59ab0252001-01-23 22:30:04 +000072static const int UF_ECHO = 0x01;
73static const int UF_SGA = 0x02;
Eric Andersen28c70b32000-06-14 20:42:57 +000074
Mark Whitley59ab0252001-01-23 22:30:04 +000075enum {
76 TS_0 = 1,
77 TS_IAC = 2,
78 TS_OPT = 3,
79 TS_SUB1 = 4,
80 TS_SUB2 = 5,
81};
Eric Andersen28c70b32000-06-14 20:42:57 +000082
83#define WriteCS(fd, str) write(fd, str, sizeof str -1)
84
85typedef unsigned char byte;
86
87/* use globals to reduce size ??? */ /* test this hypothesis later */
Eric Andersen92d23242001-03-19 23:49:41 +000088static struct Globalvars {
Eric Andersen28c70b32000-06-14 20:42:57 +000089 int netfd; /* console fd:s are 0 and 1 (and 2) */
90 /* same buffer used both for network and console read/write */
Glenn L McGrath78b0e372001-06-26 02:06:08 +000091 char buf[DATABUFSIZE]; /* allocating so static size is smaller */
Eric Andersen28c70b32000-06-14 20:42:57 +000092 byte telstate; /* telnet negotiation state from network input */
93 byte telwish; /* DO, DONT, WILL, WONT */
94 byte charmode;
95 byte telflags;
96 byte gotsig;
97 /* buffer to handle telnet negotiations */
Glenn L McGrath78b0e372001-06-26 02:06:08 +000098 char iacbuf[IACBUFSIZE];
Eric Andersen28c70b32000-06-14 20:42:57 +000099 short iaclen; /* could even use byte */
100 struct termios termios_def;
101 struct termios termios_raw;
102} G;
103
104#define xUSE_GLOBALVAR_PTR /* xUSE... -> don't use :D (makes smaller code) */
105
106#ifdef USE_GLOBALVAR_PTR
107struct Globalvars * Gptr;
108#define G (*Gptr)
Eric Andersen28c70b32000-06-14 20:42:57 +0000109#endif
110
Eric Andersencd8c4362001-11-10 11:22:46 +0000111static inline void iacflush(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000112{
113 write(G.netfd, G.iacbuf, G.iaclen);
114 G.iaclen = 0;
115}
116
117/* Function prototypes */
Eric Andersencd8c4362001-11-10 11:22:46 +0000118static void rawmode(void);
119static void cookmode(void);
120static void do_linemode(void);
121static void will_charmode(void);
Eric Andersen28c70b32000-06-14 20:42:57 +0000122static void telopt(byte c);
123static int subneg(byte c);
Eric Andersen28c70b32000-06-14 20:42:57 +0000124
125/* Some globals */
126static int one = 1;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000127
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000128#ifdef CONFIG_FEATURE_TELNET_TTYPE
Eric Andersen7e1273e2001-05-07 17:57:45 +0000129static char *ttype;
130#endif
131
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000132#ifdef CONFIG_FEATURE_AUTOWIDTH
133static int win_width, win_height;
134#endif
135
Eric Andersen28c70b32000-06-14 20:42:57 +0000136static void doexit(int ev)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000137{
Eric Andersen28c70b32000-06-14 20:42:57 +0000138 cookmode();
139 exit(ev);
140}
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000141
Eric Andersencd8c4362001-11-10 11:22:46 +0000142static void conescape(void)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000143{
Eric Andersen28c70b32000-06-14 20:42:57 +0000144 char b;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000145
Eric Andersen28c70b32000-06-14 20:42:57 +0000146 if (G.gotsig) /* came from line mode... go raw */
147 rawmode();
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000148
Eric Andersen28c70b32000-06-14 20:42:57 +0000149 WriteCS(1, "\r\nConsole escape. Commands are:\r\n\n"
150 " l go to line mode\r\n"
151 " c go to character mode\r\n"
152 " z suspend telnet\r\n"
153 " e exit telnet\r\n");
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000154
Eric Andersen28c70b32000-06-14 20:42:57 +0000155 if (read(0, &b, 1) <= 0)
156 doexit(1);
157
158 switch (b)
159 {
160 case 'l':
161 if (!G.gotsig)
162 {
163 do_linemode();
164 goto rrturn;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000165 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000166 break;
167 case 'c':
168 if (G.gotsig)
169 {
170 will_charmode();
171 goto rrturn;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000172 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000173 break;
174 case 'z':
175 cookmode();
176 kill(0, SIGTSTP);
177 rawmode();
178 break;
179 case 'e':
180 doexit(0);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000181 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000182
183 WriteCS(1, "continuing...\r\n");
184
185 if (G.gotsig)
186 cookmode();
187
188 rrturn:
189 G.gotsig = 0;
190
191}
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000192static void handlenetoutput(int len)
Eric Andersen28c70b32000-06-14 20:42:57 +0000193{
194 /* here we could do smart tricks how to handle 0xFF:s in output
195 * stream like writing twice every sequence of FF:s (thus doing
196 * many write()s. But I think interactive telnet application does
197 * not need to be 100% 8-bit clean, so changing every 0xff:s to
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000198 * 0x7f:s
199 *
200 * 2002-mar-21, Przemyslaw Czerpak (druzus@polbox.com)
201 * I don't agree.
202 * first - I cannot use programs like sz/rz
203 * second - the 0x0D is sent as one character and if the next
204 * char is 0x0A then it's eaten by a server side.
205 * third - whay doy you have to make 'many write()s'?
206 * I don't understand.
207 * So I implemented it. It's realy useful for me. I hope that
208 * others people will find it interesting to.
209 */
Eric Andersen28c70b32000-06-14 20:42:57 +0000210
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000211 int i, j;
Eric Andersen28c70b32000-06-14 20:42:57 +0000212 byte * p = G.buf;
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000213 byte outbuf[4*DATABUFSIZE];
Eric Andersen28c70b32000-06-14 20:42:57 +0000214
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000215 for (i = len, j = 0; i > 0; i--, p++)
Eric Andersen28c70b32000-06-14 20:42:57 +0000216 {
217 if (*p == 0x1d)
218 {
219 conescape();
220 return;
221 }
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000222 outbuf[j++] = *p;
Eric Andersen28c70b32000-06-14 20:42:57 +0000223 if (*p == 0xff)
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000224 outbuf[j++] = 0xff;
225 else if (*p == 0x0d)
226 outbuf[j++] = 0x00;
Eric Andersen28c70b32000-06-14 20:42:57 +0000227 }
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000228 if (j > 0 )
229 write(G.netfd, outbuf, j);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000230}
231
Eric Andersen28c70b32000-06-14 20:42:57 +0000232
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000233static void handlenetinput(int len)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000234{
Eric Andersen28c70b32000-06-14 20:42:57 +0000235 int i;
236 int cstart = 0;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000237
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000238 for (i = 0; i < len; i++)
Eric Andersen28c70b32000-06-14 20:42:57 +0000239 {
240 byte c = G.buf[i];
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000241
Eric Andersen28c70b32000-06-14 20:42:57 +0000242 if (G.telstate == 0) /* most of the time state == 0 */
243 {
244 if (c == IAC)
245 {
246 cstart = i;
247 G.telstate = TS_IAC;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000248 }
249 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000250 else
251 switch (G.telstate)
252 {
253 case TS_0:
254 if (c == IAC)
255 G.telstate = TS_IAC;
256 else
257 G.buf[cstart++] = c;
258 break;
259
260 case TS_IAC:
261 if (c == IAC) /* IAC IAC -> 0xFF */
262 {
263 G.buf[cstart++] = c;
264 G.telstate = TS_0;
265 break;
266 }
267 /* else */
268 switch (c)
269 {
270 case SB:
271 G.telstate = TS_SUB1;
272 break;
273 case DO:
274 case DONT:
275 case WILL:
276 case WONT:
277 G.telwish = c;
278 G.telstate = TS_OPT;
279 break;
280 default:
281 G.telstate = TS_0; /* DATA MARK must be added later */
282 }
283 break;
284 case TS_OPT: /* WILL, WONT, DO, DONT */
285 telopt(c);
286 G.telstate = TS_0;
287 break;
288 case TS_SUB1: /* Subnegotiation */
289 case TS_SUB2: /* Subnegotiation */
Matt Kraai1f0c4362001-12-20 23:13:26 +0000290 if (subneg(c))
Eric Andersen28c70b32000-06-14 20:42:57 +0000291 G.telstate = TS_0;
292 break;
293 }
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000294 }
Eric Andersen28c70b32000-06-14 20:42:57 +0000295 if (G.telstate)
296 {
297 if (G.iaclen) iacflush();
298 if (G.telstate == TS_0) G.telstate = 0;
299
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000300 len = cstart;
Eric Andersen28c70b32000-06-14 20:42:57 +0000301 }
302
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000303 if (len)
304 write(1, G.buf, len);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000305}
306
Eric Andersen28c70b32000-06-14 20:42:57 +0000307
308/* ******************************* */
309
310static inline void putiac(int c)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000311{
Eric Andersen28c70b32000-06-14 20:42:57 +0000312 G.iacbuf[G.iaclen++] = c;
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000313}
314
Eric Andersen28c70b32000-06-14 20:42:57 +0000315
316static void putiac2(byte wwdd, byte c)
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000317{
Eric Andersen28c70b32000-06-14 20:42:57 +0000318 if (G.iaclen + 3 > IACBUFSIZE)
319 iacflush();
320
321 putiac(IAC);
322 putiac(wwdd);
323 putiac(c);
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000324}
325
Eric Andersen28c70b32000-06-14 20:42:57 +0000326#if 0
327static void putiac1(byte c)
328{
329 if (G.iaclen + 2 > IACBUFSIZE)
330 iacflush();
331
332 putiac(IAC);
333 putiac(c);
334}
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000335#endif
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000336
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000337#ifdef CONFIG_FEATURE_TELNET_TTYPE
Eric Andersen7e1273e2001-05-07 17:57:45 +0000338static void putiac_subopt(byte c, char *str)
339{
340 int len = strlen(str) + 6; // ( 2 + 1 + 1 + strlen + 2 )
341
342 if (G.iaclen + len > IACBUFSIZE)
343 iacflush();
344
345 putiac(IAC);
346 putiac(SB);
347 putiac(c);
348 putiac(0);
349
350 while(*str)
351 putiac(*str++);
352
353 putiac(IAC);
354 putiac(SE);
355}
356#endif
357
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000358#ifdef CONFIG_FEATURE_AUTOWIDTH
359static void putiac_naws(byte c, int x, int y)
360{
361 if (G.iaclen + 9 > IACBUFSIZE)
362 iacflush();
363
364 putiac(IAC);
365 putiac(SB);
366 putiac(c);
367
368 putiac((x >> 8) & 0xff);
369 putiac(x & 0xff);
370 putiac((y >> 8) & 0xff);
371 putiac(y & 0xff);
372
373 putiac(IAC);
374 putiac(SE);
375}
376#endif
377
Eric Andersen28c70b32000-06-14 20:42:57 +0000378/* void putiacstring (subneg strings) */
379
380/* ******************************* */
381
Eric Andersen3e6ff902001-03-09 21:24:12 +0000382static char const escapecharis[] = "\r\nEscape character is ";
Eric Andersen28c70b32000-06-14 20:42:57 +0000383
Eric Andersencd8c4362001-11-10 11:22:46 +0000384static void setConMode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000385{
386 if (G.telflags & UF_ECHO)
387 {
388 if (G.charmode == CHM_TRY) {
389 G.charmode = CHM_ON;
Matt Kraai12f417e2001-01-18 02:57:08 +0000390 printf("\r\nEntering character mode%s'^]'.\r\n", escapecharis);
Eric Andersen28c70b32000-06-14 20:42:57 +0000391 rawmode();
392 }
393 }
394 else
395 {
396 if (G.charmode != CHM_OFF) {
397 G.charmode = CHM_OFF;
Matt Kraai12f417e2001-01-18 02:57:08 +0000398 printf("\r\nEntering line mode%s'^C'.\r\n", escapecharis);
Eric Andersen28c70b32000-06-14 20:42:57 +0000399 cookmode();
400 }
401 }
402}
403
404/* ******************************* */
405
Eric Andersencd8c4362001-11-10 11:22:46 +0000406static void will_charmode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000407{
408 G.charmode = CHM_TRY;
409 G.telflags |= (UF_ECHO | UF_SGA);
410 setConMode();
411
412 putiac2(DO, TELOPT_ECHO);
413 putiac2(DO, TELOPT_SGA);
414 iacflush();
415}
416
Eric Andersencd8c4362001-11-10 11:22:46 +0000417static void do_linemode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000418{
419 G.charmode = CHM_TRY;
420 G.telflags &= ~(UF_ECHO | UF_SGA);
421 setConMode();
422
423 putiac2(DONT, TELOPT_ECHO);
424 putiac2(DONT, TELOPT_SGA);
425 iacflush();
426}
427
428/* ******************************* */
429
430static inline void to_notsup(char c)
431{
432 if (G.telwish == WILL) putiac2(DONT, c);
433 else if (G.telwish == DO) putiac2(WONT, c);
434}
435
Eric Andersencd8c4362001-11-10 11:22:46 +0000436static inline void to_echo(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000437{
438 /* if server requests ECHO, don't agree */
439 if (G.telwish == DO) { putiac2(WONT, TELOPT_ECHO); return; }
440 else if (G.telwish == DONT) return;
441
442 if (G.telflags & UF_ECHO)
443 {
444 if (G.telwish == WILL)
445 return;
446 }
447 else
448 if (G.telwish == WONT)
449 return;
450
451 if (G.charmode != CHM_OFF)
452 G.telflags ^= UF_ECHO;
453
454 if (G.telflags & UF_ECHO)
455 putiac2(DO, TELOPT_ECHO);
456 else
457 putiac2(DONT, TELOPT_ECHO);
458
459 setConMode();
460 WriteCS(1, "\r\n"); /* sudden modec */
461}
462
Eric Andersencd8c4362001-11-10 11:22:46 +0000463static inline void to_sga(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000464{
465 /* daemon always sends will/wont, client do/dont */
466
467 if (G.telflags & UF_SGA)
468 {
469 if (G.telwish == WILL)
470 return;
471 }
472 else
473 if (G.telwish == WONT)
474 return;
475
476 if ((G.telflags ^= UF_SGA) & UF_SGA) /* toggle */
477 putiac2(DO, TELOPT_SGA);
478 else
479 putiac2(DONT, TELOPT_SGA);
480
481 return;
482}
483
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000484#ifdef CONFIG_FEATURE_TELNET_TTYPE
Eric Andersencd8c4362001-11-10 11:22:46 +0000485static inline void to_ttype(void)
Eric Andersen7e1273e2001-05-07 17:57:45 +0000486{
487 /* Tell server we will (or won't) do TTYPE */
488
489 if(ttype)
490 putiac2(WILL, TELOPT_TTYPE);
491 else
492 putiac2(WONT, TELOPT_TTYPE);
493
494 return;
495}
496#endif
497
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000498#ifdef CONFIG_FEATURE_AUTOWIDTH
Tim Rikered8e0362002-04-26 07:53:39 +0000499static inline void to_naws(void)
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000500{
501 /* Tell server we will do NAWS */
502 putiac2(WILL, TELOPT_NAWS);
503 return;
504}
505#endif
506
Eric Andersen28c70b32000-06-14 20:42:57 +0000507static void telopt(byte c)
508{
509 switch (c)
510 {
Eric Andersen41634062002-04-26 08:44:17 +0000511 case TELOPT_ECHO: to_echo(); break;
512 case TELOPT_SGA: to_sga(); break;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000513#ifdef CONFIG_FEATURE_TELNET_TTYPE
Eric Andersen41634062002-04-26 08:44:17 +0000514 case TELOPT_TTYPE: to_ttype();break;
Eric Andersen7e1273e2001-05-07 17:57:45 +0000515#endif
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000516#ifdef CONFIG_FEATURE_AUTOWIDTH
Eric Andersen41634062002-04-26 08:44:17 +0000517 case TELOPT_NAWS: to_naws();
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000518 putiac_naws(c, win_width, win_height);
519 break;
520#endif
Eric Andersen8db361b2002-04-26 08:00:33 +0000521 default: to_notsup(c);
522 break;
Eric Andersen28c70b32000-06-14 20:42:57 +0000523 }
524}
525
526
527/* ******************************* */
528
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000529/* subnegotiation -- ignore all (except TTYPE,NAWS) */
Eric Andersen28c70b32000-06-14 20:42:57 +0000530
531static int subneg(byte c)
532{
533 switch (G.telstate)
534 {
535 case TS_SUB1:
536 if (c == IAC)
537 G.telstate = TS_SUB2;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000538#ifdef CONFIG_FEATURE_TELNET_TTYPE
Eric Andersen7e1273e2001-05-07 17:57:45 +0000539 else
540 if (c == TELOPT_TTYPE)
541 putiac_subopt(TELOPT_TTYPE,ttype);
542#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000543 break;
544 case TS_SUB2:
545 if (c == SE)
546 return TRUE;
547 G.telstate = TS_SUB1;
548 /* break; */
549 }
550 return FALSE;
551}
552
553/* ******************************* */
554
555static void fgotsig(int sig)
556{
557 G.gotsig = sig;
558}
559
560
Eric Andersencd8c4362001-11-10 11:22:46 +0000561static void rawmode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000562{
563 tcsetattr(0, TCSADRAIN, &G.termios_raw);
564}
565
Eric Andersencd8c4362001-11-10 11:22:46 +0000566static void cookmode(void)
Eric Andersen28c70b32000-06-14 20:42:57 +0000567{
568 tcsetattr(0, TCSADRAIN, &G.termios_def);
569}
570
571extern int telnet_main(int argc, char** argv)
572{
Eric Andersen0b315862002-07-03 11:51:44 +0000573 char *host;
574 char *port;
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000575 int len;
Eric Andersen28c70b32000-06-14 20:42:57 +0000576#ifdef USE_POLL
577 struct pollfd ufds[2];
578#else
579 fd_set readfds;
580 int maxfd;
581#endif
582
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000583#ifdef CONFIG_FEATURE_AUTOWIDTH
Eric Andersen8efe9672003-09-15 08:33:45 +0000584 get_terminal_width_height(0, &win_width, &win_height);
Eric Andersen0e28e1f2002-04-26 07:20:47 +0000585#endif
586
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000587#ifdef CONFIG_FEATURE_TELNET_TTYPE
Eric Andersen7e1273e2001-05-07 17:57:45 +0000588 ttype = getenv("TERM");
589#endif
Eric Andersen28c70b32000-06-14 20:42:57 +0000590
591 memset(&G, 0, sizeof G);
592
593 if (tcgetattr(0, &G.termios_def) < 0)
594 exit(1);
595
596 G.termios_raw = G.termios_def;
Eric Andersen28c70b32000-06-14 20:42:57 +0000597 cfmakeraw(&G.termios_raw);
598
Manuel Novoa III cad53642003-03-19 09:13:01 +0000599 if (argc < 2) bb_show_usage();
Eric Andersen0b315862002-07-03 11:51:44 +0000600 port = (argc > 2)? argv[2] : "23";
Eric Andersen28c70b32000-06-14 20:42:57 +0000601
Eric Andersen0b315862002-07-03 11:51:44 +0000602 host = argv[1];
603
604 G.netfd = xconnect(host, port);
Eric Andersen28c70b32000-06-14 20:42:57 +0000605
Eric Andersen0b315862002-07-03 11:51:44 +0000606 setsockopt(G.netfd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof one);
Eric Andersen28c70b32000-06-14 20:42:57 +0000607
608 signal(SIGINT, fgotsig);
609
610#ifdef USE_POLL
611 ufds[0].fd = 0; ufds[1].fd = G.netfd;
612 ufds[0].events = ufds[1].events = POLLIN;
613#else
614 FD_ZERO(&readfds);
615 FD_SET(0, &readfds);
616 FD_SET(G.netfd, &readfds);
617 maxfd = G.netfd + 1;
618#endif
619
620 while (1)
621 {
622#ifndef USE_POLL
623 fd_set rfds = readfds;
624
625 switch (select(maxfd, &rfds, NULL, NULL, NULL))
626#else
627 switch (poll(ufds, 2, -1))
628#endif
629 {
630 case 0:
631 /* timeout */
632 case -1:
633 /* error, ignore and/or log something, bay go to loop */
634 if (G.gotsig)
635 conescape();
636 else
637 sleep(1);
638 break;
639 default:
640
641#ifdef USE_POLL
642 if (ufds[0].revents) /* well, should check POLLIN, but ... */
643#else
644 if (FD_ISSET(0, &rfds))
645#endif
646 {
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000647 len = read(0, G.buf, DATABUFSIZE);
Eric Andersen28c70b32000-06-14 20:42:57 +0000648
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000649 if (len <= 0)
Eric Andersen28c70b32000-06-14 20:42:57 +0000650 doexit(0);
651
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000652 TRACE(0, ("Read con: %d\n", len));
Eric Andersen28c70b32000-06-14 20:42:57 +0000653
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000654 handlenetoutput(len);
Eric Andersen28c70b32000-06-14 20:42:57 +0000655 }
656
657#ifdef USE_POLL
658 if (ufds[1].revents) /* well, should check POLLIN, but ... */
659#else
660 if (FD_ISSET(G.netfd, &rfds))
661#endif
662 {
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000663 len = read(G.netfd, G.buf, DATABUFSIZE);
Eric Andersen28c70b32000-06-14 20:42:57 +0000664
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000665 if (len <= 0)
Eric Andersen28c70b32000-06-14 20:42:57 +0000666 {
667 WriteCS(1, "Connection closed by foreign host.\r\n");
668 doexit(1);
669 }
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000670 TRACE(0, ("Read netfd (%d): %d\n", G.netfd, len));
Eric Andersen28c70b32000-06-14 20:42:57 +0000671
Glenn L McGrath78b0e372001-06-26 02:06:08 +0000672 handlenetinput(len);
Eric Andersen28c70b32000-06-14 20:42:57 +0000673 }
674 }
675 }
676}
677
Erik Andersenf7c49ef2000-02-22 17:17:45 +0000678/*
Eric Andersen28c70b32000-06-14 20:42:57 +0000679Local Variables:
680c-file-style: "linux"
681c-basic-offset: 4
682tab-width: 4
683End:
684*/