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 * 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.
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.
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
26 * Revision 3.1 1994/04/17 11:31:54 too
28 * Modified 2000/06/13 for inclusion into BusyBox by Erik Andersen <andersen@codepoet.org>
29 * Modified 2001/05/07 to add ability to pass TTYPE to remote host by Jim McQuillan
41 #include <arpa/telnet.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
47 #ifdef CONFIG_FEATURE_AUTOWIDTH
48 # include <sys/ioctl.h>
52 static const int DOTRACE = 1;
56 #include <arpa/inet.h> /* for inet_ntoa()... */
57 #define TRACE(x, y) do { if (x) printf y; } while (0)
69 #define DATABUFSIZE 128
70 #define IACBUFSIZE 128
72 static const int CHM_TRY = 0;
73 static const int CHM_ON = 1;
74 static const int CHM_OFF = 2;
76 static const int UF_ECHO = 0x01;
77 static const int UF_SGA = 0x02;
87 #define WriteCS(fd, str) write(fd, str, sizeof str -1)
89 typedef unsigned char byte;
91 /* use globals to reduce size ??? */ /* test this hypothesis later */
92 static struct Globalvars {
93 int netfd; /* console fd:s are 0 and 1 (and 2) */
94 /* same buffer used both for network and console read/write */
95 char buf[DATABUFSIZE]; /* allocating so static size is smaller */
96 byte telstate; /* telnet negotiation state from network input */
97 byte telwish; /* DO, DONT, WILL, WONT */
101 /* buffer to handle telnet negotiations */
102 char iacbuf[IACBUFSIZE];
103 short iaclen; /* could even use byte */
104 struct termios termios_def;
105 struct termios termios_raw;
108 #define xUSE_GLOBALVAR_PTR /* xUSE... -> don't use :D (makes smaller code) */
110 #ifdef USE_GLOBALVAR_PTR
111 struct Globalvars * Gptr;
115 static inline void iacflush(void)
117 write(G.netfd, G.iacbuf, G.iaclen);
121 /* Function prototypes */
122 static void rawmode(void);
123 static void cookmode(void);
124 static void do_linemode(void);
125 static void will_charmode(void);
126 static void telopt(byte c);
127 static int subneg(byte c);
132 #ifdef CONFIG_FEATURE_TELNET_TTYPE
136 #ifdef CONFIG_FEATURE_AUTOWIDTH
137 static int win_width, win_height;
140 static void doexit(int ev)
146 static void conescape(void)
150 if (G.gotsig) /* came from line mode... go raw */
153 WriteCS(1, "\r\nConsole escape. Commands are:\r\n\n"
154 " l go to line mode\r\n"
155 " c go to character mode\r\n"
156 " z suspend telnet\r\n"
157 " e exit telnet\r\n");
159 if (read(0, &b, 1) <= 0)
187 WriteCS(1, "continuing...\r\n");
196 static void handlenetoutput(int len)
198 /* here we could do smart tricks how to handle 0xFF:s in output
199 * stream like writing twice every sequence of FF:s (thus doing
200 * many write()s. But I think interactive telnet application does
201 * not need to be 100% 8-bit clean, so changing every 0xff:s to
204 * 2002-mar-21, Przemyslaw Czerpak (druzus@polbox.com)
206 * first - I cannot use programs like sz/rz
207 * second - the 0x0D is sent as one character and if the next
208 * char is 0x0A then it's eaten by a server side.
209 * third - whay doy you have to make 'many write()s'?
210 * I don't understand.
211 * So I implemented it. It's realy useful for me. I hope that
212 * others people will find it interesting to.
217 byte outbuf[4*DATABUFSIZE];
219 for (i = len, j = 0; i > 0; i--, p++)
233 write(G.netfd, outbuf, j);
237 static void handlenetinput(int len)
242 for (i = 0; i < len; i++)
246 if (G.telstate == 0) /* most of the time state == 0 */
265 if (c == IAC) /* IAC IAC -> 0xFF */
275 G.telstate = TS_SUB1;
285 G.telstate = TS_0; /* DATA MARK must be added later */
288 case TS_OPT: /* WILL, WONT, DO, DONT */
292 case TS_SUB1: /* Subnegotiation */
293 case TS_SUB2: /* Subnegotiation */
301 if (G.iaclen) iacflush();
302 if (G.telstate == TS_0) G.telstate = 0;
308 write(1, G.buf, len);
312 /* ******************************* */
314 static inline void putiac(int c)
316 G.iacbuf[G.iaclen++] = c;
320 static void putiac2(byte wwdd, byte c)
322 if (G.iaclen + 3 > IACBUFSIZE)
331 static void putiac1(byte c)
333 if (G.iaclen + 2 > IACBUFSIZE)
341 #ifdef CONFIG_FEATURE_TELNET_TTYPE
342 static void putiac_subopt(byte c, char *str)
344 int len = strlen(str) + 6; // ( 2 + 1 + 1 + strlen + 2 )
346 if (G.iaclen + len > IACBUFSIZE)
362 #ifdef CONFIG_FEATURE_AUTOWIDTH
363 static void putiac_naws(byte c, int x, int y)
365 if (G.iaclen + 9 > IACBUFSIZE)
372 putiac((x >> 8) & 0xff);
374 putiac((y >> 8) & 0xff);
382 /* void putiacstring (subneg strings) */
384 /* ******************************* */
386 static char const escapecharis[] = "\r\nEscape character is ";
388 static void setConMode(void)
390 if (G.telflags & UF_ECHO)
392 if (G.charmode == CHM_TRY) {
394 printf("\r\nEntering character mode%s'^]'.\r\n", escapecharis);
400 if (G.charmode != CHM_OFF) {
401 G.charmode = CHM_OFF;
402 printf("\r\nEntering line mode%s'^C'.\r\n", escapecharis);
408 /* ******************************* */
410 static void will_charmode(void)
412 G.charmode = CHM_TRY;
413 G.telflags |= (UF_ECHO | UF_SGA);
416 putiac2(DO, TELOPT_ECHO);
417 putiac2(DO, TELOPT_SGA);
421 static void do_linemode(void)
423 G.charmode = CHM_TRY;
424 G.telflags &= ~(UF_ECHO | UF_SGA);
427 putiac2(DONT, TELOPT_ECHO);
428 putiac2(DONT, TELOPT_SGA);
432 /* ******************************* */
434 static inline void to_notsup(char c)
436 if (G.telwish == WILL) putiac2(DONT, c);
437 else if (G.telwish == DO) putiac2(WONT, c);
440 static inline void to_echo(void)
442 /* if server requests ECHO, don't agree */
443 if (G.telwish == DO) { putiac2(WONT, TELOPT_ECHO); return; }
444 else if (G.telwish == DONT) return;
446 if (G.telflags & UF_ECHO)
448 if (G.telwish == WILL)
452 if (G.telwish == WONT)
455 if (G.charmode != CHM_OFF)
456 G.telflags ^= UF_ECHO;
458 if (G.telflags & UF_ECHO)
459 putiac2(DO, TELOPT_ECHO);
461 putiac2(DONT, TELOPT_ECHO);
464 WriteCS(1, "\r\n"); /* sudden modec */
467 static inline void to_sga(void)
469 /* daemon always sends will/wont, client do/dont */
471 if (G.telflags & UF_SGA)
473 if (G.telwish == WILL)
477 if (G.telwish == WONT)
480 if ((G.telflags ^= UF_SGA) & UF_SGA) /* toggle */
481 putiac2(DO, TELOPT_SGA);
483 putiac2(DONT, TELOPT_SGA);
488 #ifdef CONFIG_FEATURE_TELNET_TTYPE
489 static inline void to_ttype(void)
491 /* Tell server we will (or won't) do TTYPE */
494 putiac2(WILL, TELOPT_TTYPE);
496 putiac2(WONT, TELOPT_TTYPE);
502 #ifdef CONFIG_FEATURE_AUTOWIDTH
503 static inline void to_naws(void)
505 /* Tell server we will do NAWS */
506 putiac2(WILL, TELOPT_NAWS);
511 static void telopt(byte c)
515 case TELOPT_ECHO: to_echo(); break;
516 case TELOPT_SGA: to_sga(); break;
517 #ifdef CONFIG_FEATURE_TELNET_TTYPE
518 case TELOPT_TTYPE: to_ttype();break;
520 #ifdef CONFIG_FEATURE_AUTOWIDTH
521 case TELOPT_NAWS: to_naws();
522 putiac_naws(c, win_width, win_height);
525 default: to_notsup(c);
531 /* ******************************* */
533 /* subnegotiation -- ignore all (except TTYPE,NAWS) */
535 static int subneg(byte c)
541 G.telstate = TS_SUB2;
542 #ifdef CONFIG_FEATURE_TELNET_TTYPE
544 if (c == TELOPT_TTYPE)
545 putiac_subopt(TELOPT_TTYPE,ttype);
551 G.telstate = TS_SUB1;
557 /* ******************************* */
559 static void fgotsig(int sig)
565 static void rawmode(void)
567 tcsetattr(0, TCSADRAIN, &G.termios_raw);
570 static void cookmode(void)
572 tcsetattr(0, TCSADRAIN, &G.termios_def);
575 extern int telnet_main(int argc, char** argv)
581 struct pollfd ufds[2];
587 #ifdef CONFIG_FEATURE_AUTOWIDTH
589 if( ioctl(0, TIOCGWINSZ, &winp) == 0 ) {
590 win_width = winp.ws_col;
591 win_height = winp.ws_row;
595 #ifdef CONFIG_FEATURE_TELNET_TTYPE
596 ttype = getenv("TERM");
599 memset(&G, 0, sizeof G);
601 if (tcgetattr(0, &G.termios_def) < 0)
604 G.termios_raw = G.termios_def;
605 cfmakeraw(&G.termios_raw);
607 if (argc < 2) bb_show_usage();
608 port = (argc > 2)? argv[2] : "23";
612 G.netfd = xconnect(host, port);
614 setsockopt(G.netfd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof one);
616 signal(SIGINT, fgotsig);
619 ufds[0].fd = 0; ufds[1].fd = G.netfd;
620 ufds[0].events = ufds[1].events = POLLIN;
624 FD_SET(G.netfd, &readfds);
631 fd_set rfds = readfds;
633 switch (select(maxfd, &rfds, NULL, NULL, NULL))
635 switch (poll(ufds, 2, -1))
641 /* error, ignore and/or log something, bay go to loop */
650 if (ufds[0].revents) /* well, should check POLLIN, but ... */
652 if (FD_ISSET(0, &rfds))
655 len = read(0, G.buf, DATABUFSIZE);
660 TRACE(0, ("Read con: %d\n", len));
662 handlenetoutput(len);
666 if (ufds[1].revents) /* well, should check POLLIN, but ... */
668 if (FD_ISSET(G.netfd, &rfds))
671 len = read(G.netfd, G.buf, DATABUFSIZE);
675 WriteCS(1, "Connection closed by foreign host.\r\n");
678 TRACE(0, ("Read netfd (%d): %d\n", G.netfd, len));
688 c-file-style: "linux"