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>
48 static const int DOTRACE = 1;
52 #include <arpa/inet.h> /* for inet_ntoa()... */
53 #define TRACE(x, y) do { if (x) printf y; } while (0)
65 #define DATABUFSIZE 128
66 #define IACBUFSIZE 128
68 static const int CHM_TRY = 0;
69 static const int CHM_ON = 1;
70 static const int CHM_OFF = 2;
72 static const int UF_ECHO = 0x01;
73 static const int UF_SGA = 0x02;
83 #define WriteCS(fd, str) write(fd, str, sizeof str -1)
85 typedef unsigned char byte;
87 /* use globals to reduce size ??? */ /* test this hypothesis later */
88 static struct Globalvars {
89 int netfd; /* console fd:s are 0 and 1 (and 2) */
90 /* same buffer used both for network and console read/write */
91 char buf[DATABUFSIZE]; /* allocating so static size is smaller */
92 byte telstate; /* telnet negotiation state from network input */
93 byte telwish; /* DO, DONT, WILL, WONT */
97 /* buffer to handle telnet negotiations */
98 char iacbuf[IACBUFSIZE];
99 short iaclen; /* could even use byte */
100 struct termios termios_def;
101 struct termios termios_raw;
104 #define xUSE_GLOBALVAR_PTR /* xUSE... -> don't use :D (makes smaller code) */
106 #ifdef USE_GLOBALVAR_PTR
107 struct Globalvars * Gptr;
111 static inline void iacflush(void)
113 write(G.netfd, G.iacbuf, G.iaclen);
117 /* Function prototypes */
118 static void rawmode(void);
119 static void cookmode(void);
120 static void do_linemode(void);
121 static void will_charmode(void);
122 static void telopt(byte c);
123 static int subneg(byte c);
128 #ifdef CONFIG_FEATURE_TELNET_TTYPE
132 #ifdef CONFIG_FEATURE_AUTOWIDTH
133 static int win_width, win_height;
136 static void doexit(int ev)
142 static void conescape(void)
146 if (G.gotsig) /* came from line mode... go raw */
149 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");
155 if (read(0, &b, 1) <= 0)
183 WriteCS(1, "continuing...\r\n");
192 static void handlenetoutput(int len)
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
200 * 2002-mar-21, Przemyslaw Czerpak (druzus@polbox.com)
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.
213 byte outbuf[4*DATABUFSIZE];
215 for (i = len, j = 0; i > 0; i--, p++)
229 write(G.netfd, outbuf, j);
233 static void handlenetinput(int len)
238 for (i = 0; i < len; i++)
242 if (G.telstate == 0) /* most of the time state == 0 */
261 if (c == IAC) /* IAC IAC -> 0xFF */
271 G.telstate = TS_SUB1;
281 G.telstate = TS_0; /* DATA MARK must be added later */
284 case TS_OPT: /* WILL, WONT, DO, DONT */
288 case TS_SUB1: /* Subnegotiation */
289 case TS_SUB2: /* Subnegotiation */
297 if (G.iaclen) iacflush();
298 if (G.telstate == TS_0) G.telstate = 0;
304 write(1, G.buf, len);
308 /* ******************************* */
310 static inline void putiac(int c)
312 G.iacbuf[G.iaclen++] = c;
316 static void putiac2(byte wwdd, byte c)
318 if (G.iaclen + 3 > IACBUFSIZE)
327 static void putiac1(byte c)
329 if (G.iaclen + 2 > IACBUFSIZE)
337 #ifdef CONFIG_FEATURE_TELNET_TTYPE
338 static void putiac_subopt(byte c, char *str)
340 int len = strlen(str) + 6; // ( 2 + 1 + 1 + strlen + 2 )
342 if (G.iaclen + len > IACBUFSIZE)
358 #ifdef CONFIG_FEATURE_AUTOWIDTH
359 static void putiac_naws(byte c, int x, int y)
361 if (G.iaclen + 9 > IACBUFSIZE)
368 putiac((x >> 8) & 0xff);
370 putiac((y >> 8) & 0xff);
378 /* void putiacstring (subneg strings) */
380 /* ******************************* */
382 static char const escapecharis[] = "\r\nEscape character is ";
384 static void setConMode(void)
386 if (G.telflags & UF_ECHO)
388 if (G.charmode == CHM_TRY) {
390 printf("\r\nEntering character mode%s'^]'.\r\n", escapecharis);
396 if (G.charmode != CHM_OFF) {
397 G.charmode = CHM_OFF;
398 printf("\r\nEntering line mode%s'^C'.\r\n", escapecharis);
404 /* ******************************* */
406 static void will_charmode(void)
408 G.charmode = CHM_TRY;
409 G.telflags |= (UF_ECHO | UF_SGA);
412 putiac2(DO, TELOPT_ECHO);
413 putiac2(DO, TELOPT_SGA);
417 static void do_linemode(void)
419 G.charmode = CHM_TRY;
420 G.telflags &= ~(UF_ECHO | UF_SGA);
423 putiac2(DONT, TELOPT_ECHO);
424 putiac2(DONT, TELOPT_SGA);
428 /* ******************************* */
430 static inline void to_notsup(char c)
432 if (G.telwish == WILL) putiac2(DONT, c);
433 else if (G.telwish == DO) putiac2(WONT, c);
436 static inline void to_echo(void)
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;
442 if (G.telflags & UF_ECHO)
444 if (G.telwish == WILL)
448 if (G.telwish == WONT)
451 if (G.charmode != CHM_OFF)
452 G.telflags ^= UF_ECHO;
454 if (G.telflags & UF_ECHO)
455 putiac2(DO, TELOPT_ECHO);
457 putiac2(DONT, TELOPT_ECHO);
460 WriteCS(1, "\r\n"); /* sudden modec */
463 static inline void to_sga(void)
465 /* daemon always sends will/wont, client do/dont */
467 if (G.telflags & UF_SGA)
469 if (G.telwish == WILL)
473 if (G.telwish == WONT)
476 if ((G.telflags ^= UF_SGA) & UF_SGA) /* toggle */
477 putiac2(DO, TELOPT_SGA);
479 putiac2(DONT, TELOPT_SGA);
484 #ifdef CONFIG_FEATURE_TELNET_TTYPE
485 static inline void to_ttype(void)
487 /* Tell server we will (or won't) do TTYPE */
490 putiac2(WILL, TELOPT_TTYPE);
492 putiac2(WONT, TELOPT_TTYPE);
498 #ifdef CONFIG_FEATURE_AUTOWIDTH
499 static inline void to_naws(void)
501 /* Tell server we will do NAWS */
502 putiac2(WILL, TELOPT_NAWS);
507 static void telopt(byte c)
511 case TELOPT_ECHO: to_echo(); break;
512 case TELOPT_SGA: to_sga(); break;
513 #ifdef CONFIG_FEATURE_TELNET_TTYPE
514 case TELOPT_TTYPE: to_ttype();break;
516 #ifdef CONFIG_FEATURE_AUTOWIDTH
517 case TELOPT_NAWS: to_naws();
518 putiac_naws(c, win_width, win_height);
521 default: to_notsup(c);
527 /* ******************************* */
529 /* subnegotiation -- ignore all (except TTYPE,NAWS) */
531 static int subneg(byte c)
537 G.telstate = TS_SUB2;
538 #ifdef CONFIG_FEATURE_TELNET_TTYPE
540 if (c == TELOPT_TTYPE)
541 putiac_subopt(TELOPT_TTYPE,ttype);
547 G.telstate = TS_SUB1;
553 /* ******************************* */
555 static void fgotsig(int sig)
561 static void rawmode(void)
563 tcsetattr(0, TCSADRAIN, &G.termios_raw);
566 static void cookmode(void)
568 tcsetattr(0, TCSADRAIN, &G.termios_def);
571 extern int telnet_main(int argc, char** argv)
574 struct sockaddr_in s_in;
576 struct pollfd ufds[2];
582 #ifdef CONFIG_FEATURE_AUTOWIDTH
583 get_terminal_width_height(0, &win_width, &win_height);
586 #ifdef CONFIG_FEATURE_TELNET_TTYPE
587 ttype = getenv("TERM");
590 memset(&G, 0, sizeof G);
592 if (tcgetattr(0, &G.termios_def) < 0)
595 G.termios_raw = G.termios_def;
596 cfmakeraw(&G.termios_raw);
601 bb_lookup_host(&s_in, argv[1]);
602 s_in.sin_port = bb_lookup_port((argc == 3) ? argv[2] : "telnet", 23);
604 G.netfd = xconnect(&s_in);
606 setsockopt(G.netfd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof one);
608 signal(SIGINT, fgotsig);
611 ufds[0].fd = 0; ufds[1].fd = G.netfd;
612 ufds[0].events = ufds[1].events = POLLIN;
616 FD_SET(G.netfd, &readfds);
623 fd_set rfds = readfds;
625 switch (select(maxfd, &rfds, NULL, NULL, NULL))
627 switch (poll(ufds, 2, -1))
633 /* error, ignore and/or log something, bay go to loop */
642 if (ufds[0].revents) /* well, should check POLLIN, but ... */
644 if (FD_ISSET(0, &rfds))
647 len = read(0, G.buf, DATABUFSIZE);
652 TRACE(0, ("Read con: %d\n", len));
654 handlenetoutput(len);
658 if (ufds[1].revents) /* well, should check POLLIN, but ... */
660 if (FD_ISSET(G.netfd, &rfds))
663 len = read(G.netfd, G.buf, DATABUFSIZE);
667 WriteCS(1, "Connection closed by foreign host.\r\n");
670 TRACE(0, ("Read netfd (%d): %d\n", G.netfd, len));
680 c-file-style: "linux"