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
29 * <andersen@lineo.com>
30 * Modified 2001/05/07 to add ability to pass TTYPE to remote host by Jim McQuillan
42 #include <arpa/telnet.h>
43 #include <sys/types.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
50 static const int DOTRACE = 1;
54 #include <arpa/inet.h> /* for inet_ntoa()... */
55 #define TRACE(x, y) do { if (x) printf y; } while (0)
67 static const int DATABUFSIZE = 128;
68 static const int IACBUFSIZE = 128;
70 static const int CHM_TRY = 0;
71 static const int CHM_ON = 1;
72 static const int CHM_OFF = 2;
74 static const int UF_ECHO = 0x01;
75 static const int UF_SGA = 0x02;
85 #define WriteCS(fd, str) write(fd, str, sizeof str -1)
87 typedef unsigned char byte;
89 /* use globals to reduce size ??? */ /* test this hypothesis later */
90 static struct Globalvars {
91 int netfd; /* console fd:s are 0 and 1 (and 2) */
92 /* same buffer used both for network and console read/write */
93 char * buf; /* 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 */
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()
118 write(G.netfd, G.iacbuf, G.iaclen);
122 /* Function prototypes */
123 static int getport(char * p);
124 static struct in_addr getserver(char * p);
125 static int create_socket();
126 static void setup_sockaddr_in(struct sockaddr_in * addr, int port);
127 static int remote_connect(struct in_addr addr, int port);
128 static void rawmode();
129 static void cookmode();
130 static void do_linemode();
131 static void will_charmode();
132 static void telopt(byte c);
133 static int subneg(byte c);
135 static int local_bind(int port);
141 #ifdef BB_FEATURE_TELNET_TTYPE
145 static void doexit(int ev)
151 static void conescape()
155 if (G.gotsig) /* came from line mode... go raw */
158 WriteCS(1, "\r\nConsole escape. Commands are:\r\n\n"
159 " l go to line mode\r\n"
160 " c go to character mode\r\n"
161 " z suspend telnet\r\n"
162 " e exit telnet\r\n");
164 if (read(0, &b, 1) <= 0)
192 WriteCS(1, "continuing...\r\n");
201 static void handlenetoutput()
203 /* here we could do smart tricks how to handle 0xFF:s in output
204 * stream like writing twice every sequence of FF:s (thus doing
205 * many write()s. But I think interactive telnet application does
206 * not need to be 100% 8-bit clean, so changing every 0xff:s to
212 for (i = G.len; i > 0; i--, p++)
222 write(G.netfd, G.buf, G.len);
226 static void handlenetinput()
231 for (i = 0; i < G.len; i++)
235 if (G.telstate == 0) /* most of the time state == 0 */
254 if (c == IAC) /* IAC IAC -> 0xFF */
264 G.telstate = TS_SUB1;
274 G.telstate = TS_0; /* DATA MARK must be added later */
277 case TS_OPT: /* WILL, WONT, DO, DONT */
281 case TS_SUB1: /* Subnegotiation */
282 case TS_SUB2: /* Subnegotiation */
283 if (subneg(c) == TRUE)
290 if (G.iaclen) iacflush();
291 if (G.telstate == TS_0) G.telstate = 0;
297 write(1, G.buf, G.len);
301 /* ******************************* */
303 static inline void putiac(int c)
305 G.iacbuf[G.iaclen++] = c;
309 static void putiac2(byte wwdd, byte c)
311 if (G.iaclen + 3 > IACBUFSIZE)
320 static void putiac1(byte c)
322 if (G.iaclen + 2 > IACBUFSIZE)
330 #ifdef BB_FEATURE_TELNET_TTYPE
331 static void putiac_subopt(byte c, char *str)
333 int len = strlen(str) + 6; // ( 2 + 1 + 1 + strlen + 2 )
335 if (G.iaclen + len > IACBUFSIZE)
351 /* void putiacstring (subneg strings) */
353 /* ******************************* */
355 static char const escapecharis[] = "\r\nEscape character is ";
357 static void setConMode()
359 if (G.telflags & UF_ECHO)
361 if (G.charmode == CHM_TRY) {
363 printf("\r\nEntering character mode%s'^]'.\r\n", escapecharis);
369 if (G.charmode != CHM_OFF) {
370 G.charmode = CHM_OFF;
371 printf("\r\nEntering line mode%s'^C'.\r\n", escapecharis);
377 /* ******************************* */
379 static void will_charmode()
381 G.charmode = CHM_TRY;
382 G.telflags |= (UF_ECHO | UF_SGA);
385 putiac2(DO, TELOPT_ECHO);
386 putiac2(DO, TELOPT_SGA);
390 static void do_linemode()
392 G.charmode = CHM_TRY;
393 G.telflags &= ~(UF_ECHO | UF_SGA);
396 putiac2(DONT, TELOPT_ECHO);
397 putiac2(DONT, TELOPT_SGA);
401 /* ******************************* */
403 static inline void to_notsup(char c)
405 if (G.telwish == WILL) putiac2(DONT, c);
406 else if (G.telwish == DO) putiac2(WONT, c);
409 static inline void to_echo()
411 /* if server requests ECHO, don't agree */
412 if (G.telwish == DO) { putiac2(WONT, TELOPT_ECHO); return; }
413 else if (G.telwish == DONT) return;
415 if (G.telflags & UF_ECHO)
417 if (G.telwish == WILL)
421 if (G.telwish == WONT)
424 if (G.charmode != CHM_OFF)
425 G.telflags ^= UF_ECHO;
427 if (G.telflags & UF_ECHO)
428 putiac2(DO, TELOPT_ECHO);
430 putiac2(DONT, TELOPT_ECHO);
433 WriteCS(1, "\r\n"); /* sudden modec */
436 static inline void to_sga()
438 /* daemon always sends will/wont, client do/dont */
440 if (G.telflags & UF_SGA)
442 if (G.telwish == WILL)
446 if (G.telwish == WONT)
449 if ((G.telflags ^= UF_SGA) & UF_SGA) /* toggle */
450 putiac2(DO, TELOPT_SGA);
452 putiac2(DONT, TELOPT_SGA);
457 #ifdef BB_FEATURE_TELNET_TTYPE
458 static inline void to_ttype()
460 /* Tell server we will (or won't) do TTYPE */
463 putiac2(WILL, TELOPT_TTYPE);
465 putiac2(WONT, TELOPT_TTYPE);
471 static void telopt(byte c)
475 case TELOPT_ECHO: to_echo(c); break;
476 case TELOPT_SGA: to_sga(c); break;
477 #ifdef BB_FEATURE_TELNET_TTYPE
478 case TELOPT_TTYPE: to_ttype(c); break;
480 default: to_notsup(c); break;
485 /* ******************************* */
487 /* subnegotiation -- ignore all (except TTYPE) */
489 static int subneg(byte c)
495 G.telstate = TS_SUB2;
496 #ifdef BB_FEATURE_TELNET_TTYPE
498 if (c == TELOPT_TTYPE)
499 putiac_subopt(TELOPT_TTYPE,ttype);
505 G.telstate = TS_SUB1;
511 /* ******************************* */
513 static void fgotsig(int sig)
519 static void rawmode()
521 tcsetattr(0, TCSADRAIN, &G.termios_raw);
524 static void cookmode()
526 tcsetattr(0, TCSADRAIN, &G.termios_def);
529 extern int telnet_main(int argc, char** argv)
534 struct pollfd ufds[2];
540 #ifdef BB_FEATURE_TELNET_TTYPE
541 ttype = getenv("TERM");
544 memset(&G, 0, sizeof G);
546 if (tcgetattr(0, &G.termios_def) < 0)
549 G.termios_raw = G.termios_def;
551 cfmakeraw(&G.termios_raw);
553 if (argc < 2) show_usage();
554 port = (argc > 2)? getport(argv[2]): 23;
556 G.buf = xmalloc(DATABUFSIZE);
557 G.iacbuf = xmalloc(IACBUFSIZE);
559 host = getserver(argv[1]);
561 G.netfd = remote_connect(host, port);
563 signal(SIGINT, fgotsig);
566 ufds[0].fd = 0; ufds[1].fd = G.netfd;
567 ufds[0].events = ufds[1].events = POLLIN;
571 FD_SET(G.netfd, &readfds);
578 fd_set rfds = readfds;
580 switch (select(maxfd, &rfds, NULL, NULL, NULL))
582 switch (poll(ufds, 2, -1))
588 /* error, ignore and/or log something, bay go to loop */
597 if (ufds[0].revents) /* well, should check POLLIN, but ... */
599 if (FD_ISSET(0, &rfds))
602 G.len = read(0, G.buf, DATABUFSIZE);
607 TRACE(0, ("Read con: %d\n", G.len));
613 if (ufds[1].revents) /* well, should check POLLIN, but ... */
615 if (FD_ISSET(G.netfd, &rfds))
618 G.len = read(G.netfd, G.buf, DATABUFSIZE);
622 WriteCS(1, "Connection closed by foreign host.\r\n");
625 TRACE(0, ("Read netfd (%d): %d\n", G.netfd, G.len));
633 static int getport(char * p)
635 unsigned int port = atoi(p);
637 if ((unsigned)(port - 1 ) > 65534)
639 error_msg_and_die("%s: bad port number", p);
644 static struct in_addr getserver(char * host)
649 he = xgethostbyname(host);
650 memcpy(&addr, he->h_addr, sizeof addr);
652 TRACE(1, ("addr: %s\n", inet_ntoa(addr)));
657 static int create_socket()
659 return socket(AF_INET, SOCK_STREAM, 0);
662 static void setup_sockaddr_in(struct sockaddr_in * addr, int port)
664 memset(addr, 0, sizeof(struct sockaddr_in));
665 addr->sin_family = AF_INET;
666 addr->sin_port = htons(port);
670 static int local_bind(int port)
672 struct sockaddr_in s_addr;
673 int s = create_socket();
675 setup_sockaddr_in(&s_addr, port);
677 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
679 if (bind(s, &s_addr, sizeof s_addr) < 0)
681 char * e = sys_errlist[errno];
682 syserrorexit("bind");
691 static int remote_connect(struct in_addr addr, int port)
693 struct sockaddr_in s_addr;
694 int s = create_socket();
696 setup_sockaddr_in(&s_addr, port);
697 s_addr.sin_addr = addr;
699 setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof one);
701 if (connect(s, (struct sockaddr *)&s_addr, sizeof s_addr) < 0)
703 perror_msg_and_die("Unable to connect to remote host");
710 c-file-style: "linux"