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 #define DATABUFSIZE 128
68 #define 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[DATABUFSIZE]; /* allocating so static size is smaller */
94 byte telstate; /* telnet negotiation state from network input */
95 byte telwish; /* DO, DONT, WILL, WONT */
99 /* buffer to handle telnet negotiations */
100 char iacbuf[IACBUFSIZE];
101 short iaclen; /* could even use byte */
102 struct termios termios_def;
103 struct termios termios_raw;
106 #define xUSE_GLOBALVAR_PTR /* xUSE... -> don't use :D (makes smaller code) */
108 #ifdef USE_GLOBALVAR_PTR
109 struct Globalvars * Gptr;
112 static struct Globalvars G;
115 static inline void iacflush()
117 write(G.netfd, G.iacbuf, G.iaclen);
121 /* Function prototypes */
122 static int getport(char * p);
123 static struct in_addr getserver(char * p);
124 static int create_socket();
125 static void setup_sockaddr_in(struct sockaddr_in * addr, int port);
126 static int remote_connect(struct in_addr addr, int port);
127 static void rawmode();
128 static void cookmode();
129 static void do_linemode();
130 static void will_charmode();
131 static void telopt(byte c);
132 static int subneg(byte c);
134 static int local_bind(int port);
140 #ifdef BB_FEATURE_TELNET_TTYPE
144 static void doexit(int ev)
150 static void conescape()
154 if (G.gotsig) /* came from line mode... go raw */
157 WriteCS(1, "\r\nConsole escape. Commands are:\r\n\n"
158 " l go to line mode\r\n"
159 " c go to character mode\r\n"
160 " z suspend telnet\r\n"
161 " e exit telnet\r\n");
163 if (read(0, &b, 1) <= 0)
191 WriteCS(1, "continuing...\r\n");
200 static void handlenetoutput(int len)
202 /* here we could do smart tricks how to handle 0xFF:s in output
203 * stream like writing twice every sequence of FF:s (thus doing
204 * many write()s. But I think interactive telnet application does
205 * not need to be 100% 8-bit clean, so changing every 0xff:s to
211 for (i = len; i > 0; i--, p++)
221 write(G.netfd, G.buf, len);
225 static void handlenetinput(int len)
230 for (i = 0; i < len; i++)
234 if (G.telstate == 0) /* most of the time state == 0 */
253 if (c == IAC) /* IAC IAC -> 0xFF */
263 G.telstate = TS_SUB1;
273 G.telstate = TS_0; /* DATA MARK must be added later */
276 case TS_OPT: /* WILL, WONT, DO, DONT */
280 case TS_SUB1: /* Subnegotiation */
281 case TS_SUB2: /* Subnegotiation */
282 if (subneg(c) == TRUE)
289 if (G.iaclen) iacflush();
290 if (G.telstate == TS_0) G.telstate = 0;
296 write(1, G.buf, len);
300 /* ******************************* */
302 static inline void putiac(int c)
304 G.iacbuf[G.iaclen++] = c;
308 static void putiac2(byte wwdd, byte c)
310 if (G.iaclen + 3 > IACBUFSIZE)
319 static void putiac1(byte c)
321 if (G.iaclen + 2 > IACBUFSIZE)
329 #ifdef BB_FEATURE_TELNET_TTYPE
330 static void putiac_subopt(byte c, char *str)
332 int len = strlen(str) + 6; // ( 2 + 1 + 1 + strlen + 2 )
334 if (G.iaclen + len > IACBUFSIZE)
350 /* void putiacstring (subneg strings) */
352 /* ******************************* */
354 static char const escapecharis[] = "\r\nEscape character is ";
356 static void setConMode()
358 if (G.telflags & UF_ECHO)
360 if (G.charmode == CHM_TRY) {
362 printf("\r\nEntering character mode%s'^]'.\r\n", escapecharis);
368 if (G.charmode != CHM_OFF) {
369 G.charmode = CHM_OFF;
370 printf("\r\nEntering line mode%s'^C'.\r\n", escapecharis);
376 /* ******************************* */
378 static void will_charmode()
380 G.charmode = CHM_TRY;
381 G.telflags |= (UF_ECHO | UF_SGA);
384 putiac2(DO, TELOPT_ECHO);
385 putiac2(DO, TELOPT_SGA);
389 static void do_linemode()
391 G.charmode = CHM_TRY;
392 G.telflags &= ~(UF_ECHO | UF_SGA);
395 putiac2(DONT, TELOPT_ECHO);
396 putiac2(DONT, TELOPT_SGA);
400 /* ******************************* */
402 static inline void to_notsup(char c)
404 if (G.telwish == WILL) putiac2(DONT, c);
405 else if (G.telwish == DO) putiac2(WONT, c);
408 static inline void to_echo()
410 /* if server requests ECHO, don't agree */
411 if (G.telwish == DO) { putiac2(WONT, TELOPT_ECHO); return; }
412 else if (G.telwish == DONT) return;
414 if (G.telflags & UF_ECHO)
416 if (G.telwish == WILL)
420 if (G.telwish == WONT)
423 if (G.charmode != CHM_OFF)
424 G.telflags ^= UF_ECHO;
426 if (G.telflags & UF_ECHO)
427 putiac2(DO, TELOPT_ECHO);
429 putiac2(DONT, TELOPT_ECHO);
432 WriteCS(1, "\r\n"); /* sudden modec */
435 static inline void to_sga()
437 /* daemon always sends will/wont, client do/dont */
439 if (G.telflags & UF_SGA)
441 if (G.telwish == WILL)
445 if (G.telwish == WONT)
448 if ((G.telflags ^= UF_SGA) & UF_SGA) /* toggle */
449 putiac2(DO, TELOPT_SGA);
451 putiac2(DONT, TELOPT_SGA);
456 #ifdef BB_FEATURE_TELNET_TTYPE
457 static inline void to_ttype()
459 /* Tell server we will (or won't) do TTYPE */
462 putiac2(WILL, TELOPT_TTYPE);
464 putiac2(WONT, TELOPT_TTYPE);
470 static void telopt(byte c)
474 case TELOPT_ECHO: to_echo(c); break;
475 case TELOPT_SGA: to_sga(c); break;
476 #ifdef BB_FEATURE_TELNET_TTYPE
477 case TELOPT_TTYPE: to_ttype(c); break;
479 default: to_notsup(c); break;
484 /* ******************************* */
486 /* subnegotiation -- ignore all (except TTYPE) */
488 static int subneg(byte c)
494 G.telstate = TS_SUB2;
495 #ifdef BB_FEATURE_TELNET_TTYPE
497 if (c == TELOPT_TTYPE)
498 putiac_subopt(TELOPT_TTYPE,ttype);
504 G.telstate = TS_SUB1;
510 /* ******************************* */
512 static void fgotsig(int sig)
518 static void rawmode()
520 tcsetattr(0, TCSADRAIN, &G.termios_raw);
523 static void cookmode()
525 tcsetattr(0, TCSADRAIN, &G.termios_def);
528 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;
550 cfmakeraw(&G.termios_raw);
552 if (argc < 2) show_usage();
553 port = (argc > 2)? getport(argv[2]): 23;
555 host = getserver(argv[1]);
557 G.netfd = remote_connect(host, port);
559 signal(SIGINT, fgotsig);
562 ufds[0].fd = 0; ufds[1].fd = G.netfd;
563 ufds[0].events = ufds[1].events = POLLIN;
567 FD_SET(G.netfd, &readfds);
574 fd_set rfds = readfds;
576 switch (select(maxfd, &rfds, NULL, NULL, NULL))
578 switch (poll(ufds, 2, -1))
584 /* error, ignore and/or log something, bay go to loop */
593 if (ufds[0].revents) /* well, should check POLLIN, but ... */
595 if (FD_ISSET(0, &rfds))
598 len = read(0, G.buf, DATABUFSIZE);
603 TRACE(0, ("Read con: %d\n", len));
605 handlenetoutput(len);
609 if (ufds[1].revents) /* well, should check POLLIN, but ... */
611 if (FD_ISSET(G.netfd, &rfds))
614 len = read(G.netfd, G.buf, DATABUFSIZE);
618 WriteCS(1, "Connection closed by foreign host.\r\n");
621 TRACE(0, ("Read netfd (%d): %d\n", G.netfd, len));
629 static int getport(char * p)
631 unsigned int port = atoi(p);
633 if ((unsigned)(port - 1 ) > 65534)
635 error_msg_and_die("%s: bad port number", p);
640 static struct in_addr getserver(char * host)
645 he = xgethostbyname(host);
646 memcpy(&addr, he->h_addr, sizeof addr);
648 TRACE(1, ("addr: %s\n", inet_ntoa(addr)));
653 static int create_socket()
655 return socket(AF_INET, SOCK_STREAM, 0);
658 static void setup_sockaddr_in(struct sockaddr_in * addr, int port)
660 memset(addr, 0, sizeof(struct sockaddr_in));
661 addr->sin_family = AF_INET;
662 addr->sin_port = htons(port);
666 static int local_bind(int port)
668 struct sockaddr_in s_addr;
669 int s = create_socket();
671 setup_sockaddr_in(&s_addr, port);
673 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
675 if (bind(s, &s_addr, sizeof s_addr) < 0)
677 char * e = sys_errlist[errno];
678 syserrorexit("bind");
687 static int remote_connect(struct in_addr addr, int port)
689 struct sockaddr_in s_addr;
690 int s = create_socket();
692 setup_sockaddr_in(&s_addr, port);
693 s_addr.sin_addr = addr;
695 setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof one);
697 if (connect(s, (struct sockaddr *)&s_addr, sizeof s_addr) < 0)
699 perror_msg_and_die("Unable to connect to remote host");
706 c-file-style: "linux"