1 /* vi: set sw=4 ts=4: */
3 * telnet implementation for busybox
5 * Author: Tomi Ollila <too@iki.fi>
6 * Copyright (C) 1994-2000 by Tomi Ollila
8 * Created: Thu Apr 7 13:29:41 1994 too
9 * Last modified: Fri Jun 9 14:34:24 2000 too
11 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
14 * Revision 3.1 1994/04/17 11:31:54 too
16 * Modified 2000/06/13 for inclusion into BusyBox by Erik Andersen <andersen@codepoet.org>
17 * Modified 2001/05/07 to add ability to pass TTYPE to remote host by Jim McQuillan
19 * Modified 2004/02/11 to add ability to pass the USER variable to remote host
20 * by Fernando Silveira <swrh@gmx.net>
31 #include <arpa/telnet.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
38 static const int DOTRACE = 1;
42 #include <arpa/inet.h> /* for inet_ntoa()... */
43 #define TRACE(x, y) do { if (x) printf y; } while (0)
55 #define DATABUFSIZE 128
56 #define IACBUFSIZE 128
58 static const int CHM_TRY = 0;
59 static const int CHM_ON = 1;
60 static const int CHM_OFF = 2;
62 static const int UF_ECHO = 0x01;
63 static const int UF_SGA = 0x02;
73 #define WriteCS(fd, str) write(fd, str, sizeof str -1)
75 typedef unsigned char byte;
77 /* use globals to reduce size ??? */ /* test this hypothesis later */
78 static struct Globalvars {
79 int netfd; /* console fd:s are 0 and 1 (and 2) */
80 /* same buffer used both for network and console read/write */
81 char buf[DATABUFSIZE]; /* allocating so static size is smaller */
82 byte telstate; /* telnet negotiation state from network input */
83 byte telwish; /* DO, DONT, WILL, WONT */
88 /* buffer to handle telnet negotiations */
89 char iacbuf[IACBUFSIZE];
90 short iaclen; /* could even use byte */
91 struct termios termios_def;
92 struct termios termios_raw;
95 #define xUSE_GLOBALVAR_PTR /* xUSE... -> don't use :D (makes smaller code) */
97 #ifdef USE_GLOBALVAR_PTR
98 struct Globalvars * Gptr;
102 static inline void iacflush(void)
104 write(G.netfd, G.iacbuf, G.iaclen);
108 /* Function prototypes */
109 static void rawmode(void);
110 static void cookmode(void);
111 static void do_linemode(void);
112 static void will_charmode(void);
113 static void telopt(byte c);
114 static int subneg(byte c);
119 #ifdef CONFIG_FEATURE_TELNET_TTYPE
123 #ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
124 static const char *autologin;
127 #ifdef CONFIG_FEATURE_AUTOWIDTH
128 static int win_width, win_height;
131 static void doexit(int ev)
137 static void conescape(void)
141 if (G.gotsig) /* came from line mode... go raw */
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");
150 if (read(0, &b, 1) <= 0)
178 WriteCS(1, "continuing...\r\n");
187 static void handlenetoutput(int len)
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
195 * 2002-mar-21, Przemyslaw Czerpak (druzus@polbox.com)
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.
207 byte * p = (byte*)G.buf;
208 byte outbuf[4*DATABUFSIZE];
210 for (i = len, j = 0; i > 0; i--, p++)
224 write(G.netfd, outbuf, j);
228 static void handlenetinput(int len)
233 for (i = 0; i < len; i++)
237 if (G.telstate == 0) /* most of the time state == 0 */
256 if (c == IAC) /* IAC IAC -> 0xFF */
266 G.telstate = TS_SUB1;
276 G.telstate = TS_0; /* DATA MARK must be added later */
279 case TS_OPT: /* WILL, WONT, DO, DONT */
283 case TS_SUB1: /* Subnegotiation */
284 case TS_SUB2: /* Subnegotiation */
292 if (G.iaclen) iacflush();
293 if (G.telstate == TS_0) G.telstate = 0;
299 write(1, G.buf, len);
303 /* ******************************* */
305 static inline void putiac(int c)
307 G.iacbuf[G.iaclen++] = c;
311 static void putiac2(byte wwdd, byte c)
313 if (G.iaclen + 3 > IACBUFSIZE)
322 static void putiac1(byte c)
324 if (G.iaclen + 2 > IACBUFSIZE)
332 #ifdef CONFIG_FEATURE_TELNET_TTYPE
333 static void putiac_subopt(byte c, char *str)
335 int len = strlen(str) + 6; // ( 2 + 1 + 1 + strlen + 2 )
337 if (G.iaclen + len > IACBUFSIZE)
353 #ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
354 static void putiac_subopt_autologin(void)
356 int len = strlen(autologin) + 6; // (2 + 1 + 1 + strlen + 2)
359 if (G.iaclen + len > IACBUFSIZE)
364 putiac(TELOPT_NEW_ENVIRON);
371 putiac(NEW_ENV_VALUE);
374 putiac(*autologin++);
381 #ifdef CONFIG_FEATURE_AUTOWIDTH
382 static void putiac_naws(byte c, int x, int y)
384 if (G.iaclen + 9 > IACBUFSIZE)
391 putiac((x >> 8) & 0xff);
393 putiac((y >> 8) & 0xff);
401 /* void putiacstring (subneg strings) */
403 /* ******************************* */
405 static char const escapecharis[] = "\r\nEscape character is ";
407 static void setConMode(void)
409 if (G.telflags & UF_ECHO)
411 if (G.charmode == CHM_TRY) {
413 printf("\r\nEntering character mode%s'^]'.\r\n", escapecharis);
419 if (G.charmode != CHM_OFF) {
420 G.charmode = CHM_OFF;
421 printf("\r\nEntering line mode%s'^C'.\r\n", escapecharis);
427 /* ******************************* */
429 static void will_charmode(void)
431 G.charmode = CHM_TRY;
432 G.telflags |= (UF_ECHO | UF_SGA);
435 putiac2(DO, TELOPT_ECHO);
436 putiac2(DO, TELOPT_SGA);
440 static void do_linemode(void)
442 G.charmode = CHM_TRY;
443 G.telflags &= ~(UF_ECHO | UF_SGA);
446 putiac2(DONT, TELOPT_ECHO);
447 putiac2(DONT, TELOPT_SGA);
451 /* ******************************* */
453 static inline void to_notsup(char c)
455 if (G.telwish == WILL) putiac2(DONT, c);
456 else if (G.telwish == DO) putiac2(WONT, c);
459 static inline void to_echo(void)
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;
465 if (G.telflags & UF_ECHO)
467 if (G.telwish == WILL)
471 if (G.telwish == WONT)
474 if (G.charmode != CHM_OFF)
475 G.telflags ^= UF_ECHO;
477 if (G.telflags & UF_ECHO)
478 putiac2(DO, TELOPT_ECHO);
480 putiac2(DONT, TELOPT_ECHO);
483 WriteCS(1, "\r\n"); /* sudden modec */
486 static inline void to_sga(void)
488 /* daemon always sends will/wont, client do/dont */
490 if (G.telflags & UF_SGA)
492 if (G.telwish == WILL)
496 if (G.telwish == WONT)
499 if ((G.telflags ^= UF_SGA) & UF_SGA) /* toggle */
500 putiac2(DO, TELOPT_SGA);
502 putiac2(DONT, TELOPT_SGA);
507 #ifdef CONFIG_FEATURE_TELNET_TTYPE
508 static inline void to_ttype(void)
510 /* Tell server we will (or won't) do TTYPE */
513 putiac2(WILL, TELOPT_TTYPE);
515 putiac2(WONT, TELOPT_TTYPE);
521 #ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
522 static inline void to_new_environ(void)
524 /* Tell server we will (or will not) do AUTOLOGIN */
527 putiac2(WILL, TELOPT_NEW_ENVIRON);
529 putiac2(WONT, TELOPT_NEW_ENVIRON);
535 #ifdef CONFIG_FEATURE_AUTOWIDTH
536 static inline void to_naws(void)
538 /* Tell server we will do NAWS */
539 putiac2(WILL, TELOPT_NAWS);
544 static void telopt(byte c)
548 case TELOPT_ECHO: to_echo(); break;
549 case TELOPT_SGA: to_sga(); break;
550 #ifdef CONFIG_FEATURE_TELNET_TTYPE
551 case TELOPT_TTYPE: to_ttype();break;
553 #ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
554 case TELOPT_NEW_ENVIRON: to_new_environ(); break;
556 #ifdef CONFIG_FEATURE_AUTOWIDTH
557 case TELOPT_NAWS: to_naws();
558 putiac_naws(c, win_width, win_height);
561 default: to_notsup(c);
567 /* ******************************* */
569 /* subnegotiation -- ignore all (except TTYPE,NAWS) */
571 static int subneg(byte c)
577 G.telstate = TS_SUB2;
578 #ifdef CONFIG_FEATURE_TELNET_TTYPE
580 if (c == TELOPT_TTYPE)
581 putiac_subopt(TELOPT_TTYPE,ttype);
583 #ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
585 if (c == TELOPT_NEW_ENVIRON)
586 putiac_subopt_autologin();
592 G.telstate = TS_SUB1;
598 /* ******************************* */
600 static void fgotsig(int sig)
606 static void rawmode(void)
608 if (G.do_termios) tcsetattr(0, TCSADRAIN, &G.termios_raw);
611 static void cookmode(void)
613 if (G.do_termios) tcsetattr(0, TCSADRAIN, &G.termios_def);
616 extern int telnet_main(int argc, char** argv)
619 struct sockaddr_in s_in;
621 struct pollfd ufds[2];
627 #ifdef CONFIG_FEATURE_AUTOWIDTH
628 get_terminal_width_height(0, &win_width, &win_height);
631 #ifdef CONFIG_FEATURE_TELNET_TTYPE
632 ttype = getenv("TERM");
635 memset(&G, 0, sizeof G);
637 if (tcgetattr(0, &G.termios_def) >= 0) {
640 G.termios_raw = G.termios_def;
641 cfmakeraw(&G.termios_raw);
647 #ifdef CONFIG_FEATURE_TELNET_AUTOLOGIN
648 if (1 & bb_getopt_ulflags(argc, argv, "al:", &autologin))
649 autologin = getenv("USER");
652 bb_lookup_host(&s_in, argv[optind++]);
653 s_in.sin_port = bb_lookup_port((optind < argc) ? argv[optind++] :
654 "telnet", "tcp", 23);
660 bb_lookup_host(&s_in, argv[1]);
661 s_in.sin_port = bb_lookup_port((argc == 3) ? argv[2] : "telnet", "tcp", 23);
664 G.netfd = xconnect(&s_in);
666 setsockopt(G.netfd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof one);
668 signal(SIGINT, fgotsig);
671 ufds[0].fd = 0; ufds[1].fd = G.netfd;
672 ufds[0].events = ufds[1].events = POLLIN;
676 FD_SET(G.netfd, &readfds);
683 fd_set rfds = readfds;
685 switch (select(maxfd, &rfds, NULL, NULL, NULL))
687 switch (poll(ufds, 2, -1))
693 /* error, ignore and/or log something, bay go to loop */
702 if (ufds[0].revents) /* well, should check POLLIN, but ... */
704 if (FD_ISSET(0, &rfds))
707 len = read(0, G.buf, DATABUFSIZE);
712 TRACE(0, ("Read con: %d\n", len));
714 handlenetoutput(len);
718 if (ufds[1].revents) /* well, should check POLLIN, but ... */
720 if (FD_ISSET(G.netfd, &rfds))
723 len = read(G.netfd, G.buf, DATABUFSIZE);
727 WriteCS(1, "Connection closed by foreign host.\r\n");
730 TRACE(0, ("Read netfd (%d): %d\n", G.netfd, len));
740 c-file-style: "linux"