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
40 #include <arpa/telnet.h>
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
46 #ifdef CONFIG_FEATURE_AUTOWIDTH
47 # include <sys/ioctl.h>
51 static const int DOTRACE = 1;
55 #include <arpa/inet.h> /* for inet_ntoa()... */
56 #define TRACE(x, y) do { if (x) printf y; } while (0)
68 #define DATABUFSIZE 128
69 #define IACBUFSIZE 128
71 static const int CHM_TRY = 0;
72 static const int CHM_ON = 1;
73 static const int CHM_OFF = 2;
75 static const int UF_ECHO = 0x01;
76 static const int UF_SGA = 0x02;
86 #define WriteCS(fd, str) write(fd, str, sizeof str -1)
88 typedef unsigned char byte;
90 /* use globals to reduce size ??? */ /* test this hypothesis later */
91 static struct Globalvars {
92 int netfd; /* console fd:s are 0 and 1 (and 2) */
93 /* same buffer used both for network and console read/write */
94 char buf[DATABUFSIZE]; /* allocating so static size is smaller */
95 byte telstate; /* telnet negotiation state from network input */
96 byte telwish; /* DO, DONT, WILL, WONT */
100 /* buffer to handle telnet negotiations */
101 char iacbuf[IACBUFSIZE];
102 short iaclen; /* could even use byte */
103 struct termios termios_def;
104 struct termios termios_raw;
107 #define xUSE_GLOBALVAR_PTR /* xUSE... -> don't use :D (makes smaller code) */
109 #ifdef USE_GLOBALVAR_PTR
110 struct Globalvars * Gptr;
113 static struct Globalvars G;
116 static inline void iacflush(void)
118 write(G.netfd, G.iacbuf, G.iaclen);
122 /* Function prototypes */
123 static void rawmode(void);
124 static void cookmode(void);
125 static void do_linemode(void);
126 static void will_charmode(void);
127 static void telopt(byte c);
128 static int subneg(byte c);
133 #ifdef CONFIG_FEATURE_TELNET_TTYPE
137 #ifdef CONFIG_FEATURE_AUTOWIDTH
138 static int win_width, win_height;
141 static void doexit(int ev)
147 static void conescape(void)
151 if (G.gotsig) /* came from line mode... go raw */
154 WriteCS(1, "\r\nConsole escape. Commands are:\r\n\n"
155 " l go to line mode\r\n"
156 " c go to character mode\r\n"
157 " z suspend telnet\r\n"
158 " e exit telnet\r\n");
160 if (read(0, &b, 1) <= 0)
188 WriteCS(1, "continuing...\r\n");
197 static void handlenetoutput(int len)
199 /* here we could do smart tricks how to handle 0xFF:s in output
200 * stream like writing twice every sequence of FF:s (thus doing
201 * many write()s. But I think interactive telnet application does
202 * not need to be 100% 8-bit clean, so changing every 0xff:s to
205 * 2002-mar-21, Przemyslaw Czerpak (druzus@polbox.com)
207 * first - I cannot use programs like sz/rz
208 * second - the 0x0D is sent as one character and if the next
209 * char is 0x0A then it's eaten by a server side.
210 * third - whay doy you have to make 'many write()s'?
211 * I don't understand.
212 * So I implemented it. It's realy useful for me. I hope that
213 * others people will find it interesting to.
218 byte outbuf[4*DATABUFSIZE];
220 for (i = len, j = 0; i > 0; i--, p++)
234 write(G.netfd, outbuf, j);
238 static void handlenetinput(int len)
243 for (i = 0; i < len; i++)
247 if (G.telstate == 0) /* most of the time state == 0 */
266 if (c == IAC) /* IAC IAC -> 0xFF */
276 G.telstate = TS_SUB1;
286 G.telstate = TS_0; /* DATA MARK must be added later */
289 case TS_OPT: /* WILL, WONT, DO, DONT */
293 case TS_SUB1: /* Subnegotiation */
294 case TS_SUB2: /* Subnegotiation */
302 if (G.iaclen) iacflush();
303 if (G.telstate == TS_0) G.telstate = 0;
309 write(1, G.buf, len);
313 /* ******************************* */
315 static inline void putiac(int c)
317 G.iacbuf[G.iaclen++] = c;
321 static void putiac2(byte wwdd, byte c)
323 if (G.iaclen + 3 > IACBUFSIZE)
332 static void putiac1(byte c)
334 if (G.iaclen + 2 > IACBUFSIZE)
342 #ifdef CONFIG_FEATURE_TELNET_TTYPE
343 static void putiac_subopt(byte c, char *str)
345 int len = strlen(str) + 6; // ( 2 + 1 + 1 + strlen + 2 )
347 if (G.iaclen + len > IACBUFSIZE)
363 #ifdef CONFIG_FEATURE_AUTOWIDTH
364 static void putiac_naws(byte c, int x, int y)
366 if (G.iaclen + 9 > IACBUFSIZE)
373 putiac((x >> 8) & 0xff);
375 putiac((y >> 8) & 0xff);
383 /* void putiacstring (subneg strings) */
385 /* ******************************* */
387 static char const escapecharis[] = "\r\nEscape character is ";
389 static void setConMode(void)
391 if (G.telflags & UF_ECHO)
393 if (G.charmode == CHM_TRY) {
395 printf("\r\nEntering character mode%s'^]'.\r\n", escapecharis);
401 if (G.charmode != CHM_OFF) {
402 G.charmode = CHM_OFF;
403 printf("\r\nEntering line mode%s'^C'.\r\n", escapecharis);
409 /* ******************************* */
411 static void will_charmode(void)
413 G.charmode = CHM_TRY;
414 G.telflags |= (UF_ECHO | UF_SGA);
417 putiac2(DO, TELOPT_ECHO);
418 putiac2(DO, TELOPT_SGA);
422 static void do_linemode(void)
424 G.charmode = CHM_TRY;
425 G.telflags &= ~(UF_ECHO | UF_SGA);
428 putiac2(DONT, TELOPT_ECHO);
429 putiac2(DONT, TELOPT_SGA);
433 /* ******************************* */
435 static inline void to_notsup(char c)
437 if (G.telwish == WILL) putiac2(DONT, c);
438 else if (G.telwish == DO) putiac2(WONT, c);
441 static inline void to_echo(void)
443 /* if server requests ECHO, don't agree */
444 if (G.telwish == DO) { putiac2(WONT, TELOPT_ECHO); return; }
445 else if (G.telwish == DONT) return;
447 if (G.telflags & UF_ECHO)
449 if (G.telwish == WILL)
453 if (G.telwish == WONT)
456 if (G.charmode != CHM_OFF)
457 G.telflags ^= UF_ECHO;
459 if (G.telflags & UF_ECHO)
460 putiac2(DO, TELOPT_ECHO);
462 putiac2(DONT, TELOPT_ECHO);
465 WriteCS(1, "\r\n"); /* sudden modec */
468 static inline void to_sga(void)
470 /* daemon always sends will/wont, client do/dont */
472 if (G.telflags & UF_SGA)
474 if (G.telwish == WILL)
478 if (G.telwish == WONT)
481 if ((G.telflags ^= UF_SGA) & UF_SGA) /* toggle */
482 putiac2(DO, TELOPT_SGA);
484 putiac2(DONT, TELOPT_SGA);
489 #ifdef CONFIG_FEATURE_TELNET_TTYPE
490 static inline void to_ttype(void)
492 /* Tell server we will (or won't) do TTYPE */
495 putiac2(WILL, TELOPT_TTYPE);
497 putiac2(WONT, TELOPT_TTYPE);
503 #ifdef CONFIG_FEATURE_AUTOWIDTH
504 static inline void to_naws(void)
506 /* Tell server we will do NAWS */
507 putiac2(WILL, TELOPT_NAWS);
512 static void telopt(byte c)
516 case TELOPT_ECHO: to_echo(); break;
517 case TELOPT_SGA: to_sga(); break;
518 #ifdef CONFIG_FEATURE_TELNET_TTYPE
519 case TELOPT_TTYPE: to_ttype();break;
521 #ifdef CONFIG_FEATURE_AUTOWIDTH
522 case TELOPT_NAWS: to_naws();
523 putiac_naws(c, win_width, win_height);
526 default: to_notsup(c);
532 /* ******************************* */
534 /* subnegotiation -- ignore all (except TTYPE,NAWS) */
536 static int subneg(byte c)
542 G.telstate = TS_SUB2;
543 #ifdef CONFIG_FEATURE_TELNET_TTYPE
545 if (c == TELOPT_TTYPE)
546 putiac_subopt(TELOPT_TTYPE,ttype);
552 G.telstate = TS_SUB1;
558 /* ******************************* */
560 static void fgotsig(int sig)
566 static void rawmode(void)
568 tcsetattr(0, TCSADRAIN, &G.termios_raw);
571 static void cookmode(void)
573 tcsetattr(0, TCSADRAIN, &G.termios_def);
576 extern int telnet_main(int argc, char** argv)
582 struct pollfd ufds[2];
588 #ifdef CONFIG_FEATURE_AUTOWIDTH
590 if( ioctl(0, TIOCGWINSZ, &winp) == 0 ) {
591 win_width = winp.ws_col;
592 win_height = winp.ws_row;
596 #ifdef CONFIG_FEATURE_TELNET_TTYPE
597 ttype = getenv("TERM");
600 memset(&G, 0, sizeof G);
602 if (tcgetattr(0, &G.termios_def) < 0)
605 G.termios_raw = G.termios_def;
606 cfmakeraw(&G.termios_raw);
608 if (argc < 2) show_usage();
609 port = (argc > 2)? argv[2] : "23";
613 G.netfd = xconnect(host, port);
615 setsockopt(G.netfd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof one);
617 signal(SIGINT, fgotsig);
620 ufds[0].fd = 0; ufds[1].fd = G.netfd;
621 ufds[0].events = ufds[1].events = POLLIN;
625 FD_SET(G.netfd, &readfds);
632 fd_set rfds = readfds;
634 switch (select(maxfd, &rfds, NULL, NULL, NULL))
636 switch (poll(ufds, 2, -1))
642 /* error, ignore and/or log something, bay go to loop */
651 if (ufds[0].revents) /* well, should check POLLIN, but ... */
653 if (FD_ISSET(0, &rfds))
656 len = read(0, G.buf, DATABUFSIZE);
661 TRACE(0, ("Read con: %d\n", len));
663 handlenetoutput(len);
667 if (ufds[1].revents) /* well, should check POLLIN, but ... */
669 if (FD_ISSET(G.netfd, &rfds))
672 len = read(G.netfd, G.buf, DATABUFSIZE);
676 WriteCS(1, "Connection closed by foreign host.\r\n");
679 TRACE(0, ("Read netfd (%d): %d\n", G.netfd, len));
689 c-file-style: "linux"