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 <andersee@debian.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>
48 #ifdef CONFIG_FEATURE_AUTOWIDTH
49 # include <sys/ioctl.h>
53 static const int DOTRACE = 1;
57 #include <arpa/inet.h> /* for inet_ntoa()... */
58 #define TRACE(x, y) do { if (x) printf y; } while (0)
70 #define DATABUFSIZE 128
71 #define IACBUFSIZE 128
73 static const int CHM_TRY = 0;
74 static const int CHM_ON = 1;
75 static const int CHM_OFF = 2;
77 static const int UF_ECHO = 0x01;
78 static const int UF_SGA = 0x02;
88 #define WriteCS(fd, str) write(fd, str, sizeof str -1)
90 typedef unsigned char byte;
92 /* use globals to reduce size ??? */ /* test this hypothesis later */
93 static struct Globalvars {
94 int netfd; /* console fd:s are 0 and 1 (and 2) */
95 /* same buffer used both for network and console read/write */
96 char buf[DATABUFSIZE]; /* allocating so static size is smaller */
97 byte telstate; /* telnet negotiation state from network input */
98 byte telwish; /* DO, DONT, WILL, WONT */
102 /* buffer to handle telnet negotiations */
103 char iacbuf[IACBUFSIZE];
104 short iaclen; /* could even use byte */
105 struct termios termios_def;
106 struct termios termios_raw;
109 #define xUSE_GLOBALVAR_PTR /* xUSE... -> don't use :D (makes smaller code) */
111 #ifdef USE_GLOBALVAR_PTR
112 struct Globalvars * Gptr;
115 static struct Globalvars G;
118 static inline void iacflush(void)
120 write(G.netfd, G.iacbuf, G.iaclen);
124 /* Function prototypes */
125 static int getport(char * p);
126 static struct in_addr getserver(char * p);
127 static void setup_sockaddr_in(struct sockaddr_in * addr, int port);
128 static int remote_connect(struct in_addr addr, int port);
129 static void rawmode(void);
130 static void cookmode(void);
131 static void do_linemode(void);
132 static void will_charmode(void);
133 static void telopt(byte c);
134 static int subneg(byte c);
136 static int local_bind(int port);
142 #ifdef CONFIG_FEATURE_TELNET_TTYPE
146 #ifdef CONFIG_FEATURE_AUTOWIDTH
147 static int win_width, win_height;
150 static void doexit(int ev)
156 static void conescape(void)
160 if (G.gotsig) /* came from line mode... go raw */
163 WriteCS(1, "\r\nConsole escape. Commands are:\r\n\n"
164 " l go to line mode\r\n"
165 " c go to character mode\r\n"
166 " z suspend telnet\r\n"
167 " e exit telnet\r\n");
169 if (read(0, &b, 1) <= 0)
197 WriteCS(1, "continuing...\r\n");
206 static void handlenetoutput(int len)
208 /* here we could do smart tricks how to handle 0xFF:s in output
209 * stream like writing twice every sequence of FF:s (thus doing
210 * many write()s. But I think interactive telnet application does
211 * not need to be 100% 8-bit clean, so changing every 0xff:s to
214 * 2002-mar-21, Przemyslaw Czerpak (druzus@polbox.com)
216 * first - I cannot use programs like sz/rz
217 * second - the 0x0D is sent as one character and if the next
218 * char is 0x0A then it's eaten by a server side.
219 * third - whay doy you have to make 'many write()s'?
220 * I don't understand.
221 * So I implemented it. It's realy useful for me. I hope that
222 * others people will find it interesting to.
227 byte outbuf[4*DATABUFSIZE];
229 for (i = len, j = 0; i > 0; i--, p++)
243 write(G.netfd, outbuf, j);
247 static void handlenetinput(int len)
252 for (i = 0; i < len; i++)
256 if (G.telstate == 0) /* most of the time state == 0 */
275 if (c == IAC) /* IAC IAC -> 0xFF */
285 G.telstate = TS_SUB1;
295 G.telstate = TS_0; /* DATA MARK must be added later */
298 case TS_OPT: /* WILL, WONT, DO, DONT */
302 case TS_SUB1: /* Subnegotiation */
303 case TS_SUB2: /* Subnegotiation */
311 if (G.iaclen) iacflush();
312 if (G.telstate == TS_0) G.telstate = 0;
318 write(1, G.buf, len);
322 /* ******************************* */
324 static inline void putiac(int c)
326 G.iacbuf[G.iaclen++] = c;
330 static void putiac2(byte wwdd, byte c)
332 if (G.iaclen + 3 > IACBUFSIZE)
341 static void putiac1(byte c)
343 if (G.iaclen + 2 > IACBUFSIZE)
351 #ifdef CONFIG_FEATURE_TELNET_TTYPE
352 static void putiac_subopt(byte c, char *str)
354 int len = strlen(str) + 6; // ( 2 + 1 + 1 + strlen + 2 )
356 if (G.iaclen + len > IACBUFSIZE)
372 #ifdef CONFIG_FEATURE_AUTOWIDTH
373 static void putiac_naws(byte c, int x, int y)
375 if (G.iaclen + 9 > IACBUFSIZE)
382 putiac((x >> 8) & 0xff);
384 putiac((y >> 8) & 0xff);
392 /* void putiacstring (subneg strings) */
394 /* ******************************* */
396 static char const escapecharis[] = "\r\nEscape character is ";
398 static void setConMode(void)
400 if (G.telflags & UF_ECHO)
402 if (G.charmode == CHM_TRY) {
404 printf("\r\nEntering character mode%s'^]'.\r\n", escapecharis);
410 if (G.charmode != CHM_OFF) {
411 G.charmode = CHM_OFF;
412 printf("\r\nEntering line mode%s'^C'.\r\n", escapecharis);
418 /* ******************************* */
420 static void will_charmode(void)
422 G.charmode = CHM_TRY;
423 G.telflags |= (UF_ECHO | UF_SGA);
426 putiac2(DO, TELOPT_ECHO);
427 putiac2(DO, TELOPT_SGA);
431 static void do_linemode(void)
433 G.charmode = CHM_TRY;
434 G.telflags &= ~(UF_ECHO | UF_SGA);
437 putiac2(DONT, TELOPT_ECHO);
438 putiac2(DONT, TELOPT_SGA);
442 /* ******************************* */
444 static inline void to_notsup(char c)
446 if (G.telwish == WILL) putiac2(DONT, c);
447 else if (G.telwish == DO) putiac2(WONT, c);
450 static inline void to_echo(void)
452 /* if server requests ECHO, don't agree */
453 if (G.telwish == DO) { putiac2(WONT, TELOPT_ECHO); return; }
454 else if (G.telwish == DONT) return;
456 if (G.telflags & UF_ECHO)
458 if (G.telwish == WILL)
462 if (G.telwish == WONT)
465 if (G.charmode != CHM_OFF)
466 G.telflags ^= UF_ECHO;
468 if (G.telflags & UF_ECHO)
469 putiac2(DO, TELOPT_ECHO);
471 putiac2(DONT, TELOPT_ECHO);
474 WriteCS(1, "\r\n"); /* sudden modec */
477 static inline void to_sga(void)
479 /* daemon always sends will/wont, client do/dont */
481 if (G.telflags & UF_SGA)
483 if (G.telwish == WILL)
487 if (G.telwish == WONT)
490 if ((G.telflags ^= UF_SGA) & UF_SGA) /* toggle */
491 putiac2(DO, TELOPT_SGA);
493 putiac2(DONT, TELOPT_SGA);
498 #ifdef CONFIG_FEATURE_TELNET_TTYPE
499 static inline void to_ttype(void)
501 /* Tell server we will (or won't) do TTYPE */
504 putiac2(WILL, TELOPT_TTYPE);
506 putiac2(WONT, TELOPT_TTYPE);
512 #ifdef CONFIG_FEATURE_AUTOWIDTH
513 static inline void to_naws(void)
515 /* Tell server we will do NAWS */
516 putiac2(WILL, TELOPT_NAWS);
521 static void telopt(byte c)
525 case TELOPT_ECHO: to_echo(); break;
526 case TELOPT_SGA: to_sga(); break;
527 #ifdef CONFIG_FEATURE_TELNET_TTYPE
528 case TELOPT_TTYPE: to_ttype();break;
530 #ifdef CONFIG_FEATURE_AUTOWIDTH
531 case TELOPT_NAWS: to_naws();
532 putiac_naws(c, win_width, win_height);
535 default: to_notsup(c);
541 /* ******************************* */
543 /* subnegotiation -- ignore all (except TTYPE,NAWS) */
545 static int subneg(byte c)
551 G.telstate = TS_SUB2;
552 #ifdef CONFIG_FEATURE_TELNET_TTYPE
554 if (c == TELOPT_TTYPE)
555 putiac_subopt(TELOPT_TTYPE,ttype);
561 G.telstate = TS_SUB1;
567 /* ******************************* */
569 static void fgotsig(int sig)
575 static void rawmode(void)
577 tcsetattr(0, TCSADRAIN, &G.termios_raw);
580 static void cookmode(void)
582 tcsetattr(0, TCSADRAIN, &G.termios_def);
585 extern int telnet_main(int argc, char** argv)
591 struct pollfd ufds[2];
597 #ifdef CONFIG_FEATURE_AUTOWIDTH
599 if( ioctl(0, TIOCGWINSZ, &winp) == 0 ) {
600 win_width = winp.ws_col;
601 win_height = winp.ws_row;
605 #ifdef CONFIG_FEATURE_TELNET_TTYPE
606 ttype = getenv("TERM");
609 memset(&G, 0, sizeof G);
611 if (tcgetattr(0, &G.termios_def) < 0)
614 G.termios_raw = G.termios_def;
615 cfmakeraw(&G.termios_raw);
617 if (argc < 2) show_usage();
618 port = (argc > 2)? getport(argv[2]): 23;
620 host = getserver(argv[1]);
622 G.netfd = remote_connect(host, port);
624 signal(SIGINT, fgotsig);
627 ufds[0].fd = 0; ufds[1].fd = G.netfd;
628 ufds[0].events = ufds[1].events = POLLIN;
632 FD_SET(G.netfd, &readfds);
639 fd_set rfds = readfds;
641 switch (select(maxfd, &rfds, NULL, NULL, NULL))
643 switch (poll(ufds, 2, -1))
649 /* error, ignore and/or log something, bay go to loop */
658 if (ufds[0].revents) /* well, should check POLLIN, but ... */
660 if (FD_ISSET(0, &rfds))
663 len = read(0, G.buf, DATABUFSIZE);
668 TRACE(0, ("Read con: %d\n", len));
670 handlenetoutput(len);
674 if (ufds[1].revents) /* well, should check POLLIN, but ... */
676 if (FD_ISSET(G.netfd, &rfds))
679 len = read(G.netfd, G.buf, DATABUFSIZE);
683 WriteCS(1, "Connection closed by foreign host.\r\n");
686 TRACE(0, ("Read netfd (%d): %d\n", G.netfd, len));
694 static int getport(char * p)
696 unsigned int port = atoi(p);
698 if ((unsigned)(port - 1 ) > 65534)
700 error_msg_and_die("%s: bad port number", p);
705 static struct in_addr getserver(char * host)
710 he = xgethostbyname(host);
711 memcpy(&addr, he->h_addr, sizeof addr);
713 TRACE(1, ("addr: %s\n", inet_ntoa(addr)));
718 static int create_socket(void)
720 return socket(AF_INET, SOCK_STREAM, 0);
723 static void setup_sockaddr_in(struct sockaddr_in * addr, int port)
725 memset(addr, 0, sizeof(struct sockaddr_in));
726 addr->sin_family = AF_INET;
727 addr->sin_port = htons(port);
731 static int local_bind(int port)
733 struct sockaddr_in s_addr;
734 int s = create_socket();
736 setup_sockaddr_in(&s_addr, port);
738 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
740 if (bind(s, &s_addr, sizeof s_addr) < 0)
742 char * e = sys_errlist[errno];
743 syserrorexit("bind");
752 static int remote_connect(struct in_addr addr, int port)
754 struct sockaddr_in s_addr;
755 int s = create_socket();
757 setup_sockaddr_in(&s_addr, port);
758 s_addr.sin_addr = addr;
760 setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof one);
762 if (connect(s, (struct sockaddr *)&s_addr, sizeof s_addr) < 0)
764 perror_msg_and_die("Unable to connect to remote host");
771 c-file-style: "linux"