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>
33 #warning This applet has moved to netkit-tiny. After BusyBox 0.49, this
34 #warning applet will be removed from BusyBox. All maintainence efforts
35 #warning should be done in the netkit-tiny source tree.
46 #include <arpa/telnet.h>
47 #include <sys/types.h>
48 #include <sys/socket.h>
49 #include <netinet/in.h>
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
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 */
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; /* allocating so static size is smaller */
96 byte telstate; /* telnet negotiation state from network input */
97 byte telwish; /* DO, DONT, WILL, WONT */
101 /* buffer to handle telnet negotiations */
103 short iaclen; /* could even use byte */
104 struct termios termios_def;
105 struct termios termios_raw;
108 #define xUSE_GLOBALVAR_PTR /* xUSE... -> don't use :D (makes smaller code) */
110 #ifdef USE_GLOBALVAR_PTR
111 struct Globalvars * Gptr;
117 static inline void iacflush()
119 write(G.netfd, G.iacbuf, G.iaclen);
123 /* Function prototypes */
124 static int getport(char * p);
125 static struct in_addr getserver(char * p);
126 static int create_socket();
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();
130 static void cookmode();
131 static void do_linemode();
132 static void will_charmode();
133 static void telopt(byte c);
134 static int subneg(byte c);
136 static int local_bind(int port);
142 static void doexit(int ev)
148 static void conescape()
152 if (G.gotsig) /* came from line mode... go raw */
155 WriteCS(1, "\r\nConsole escape. Commands are:\r\n\n"
156 " l go to line mode\r\n"
157 " c go to character mode\r\n"
158 " z suspend telnet\r\n"
159 " e exit telnet\r\n");
161 if (read(0, &b, 1) <= 0)
189 WriteCS(1, "continuing...\r\n");
198 static void handlenetoutput()
200 /* here we could do smart tricks how to handle 0xFF:s in output
201 * stream like writing twice every sequence of FF:s (thus doing
202 * many write()s. But I think interactive telnet application does
203 * not need to be 100% 8-bit clean, so changing every 0xff:s to
209 for (i = G.len; i > 0; i--, p++)
219 write(G.netfd, G.buf, G.len);
223 static void handlenetinput()
228 for (i = 0; i < G.len; i++)
232 if (G.telstate == 0) /* most of the time state == 0 */
251 if (c == IAC) /* IAC IAC -> 0xFF */
261 G.telstate = TS_SUB1;
271 G.telstate = TS_0; /* DATA MARK must be added later */
274 case TS_OPT: /* WILL, WONT, DO, DONT */
278 case TS_SUB1: /* Subnegotiation */
279 case TS_SUB2: /* Subnegotiation */
280 if (subneg(c) == TRUE)
287 if (G.iaclen) iacflush();
288 if (G.telstate == TS_0) G.telstate = 0;
294 write(1, G.buf, G.len);
298 /* ******************************* */
300 static inline void putiac(int c)
302 G.iacbuf[G.iaclen++] = c;
306 static void putiac2(byte wwdd, byte c)
308 if (G.iaclen + 3 > IACBUFSIZE)
317 static void putiac1(byte c)
319 if (G.iaclen + 2 > IACBUFSIZE)
327 /* void putiacstring (subneg strings) */
329 /* ******************************* */
331 char const escapecharis[] = "\r\nEscape character is ";
333 static void setConMode()
335 if (G.telflags & UF_ECHO)
337 if (G.charmode == CHM_TRY) {
339 printf("\r\nEntering character mode%s'^]'.\r\n", escapecharis);
345 if (G.charmode != CHM_OFF) {
346 G.charmode = CHM_OFF;
347 printf("\r\nEntering line mode%s'^C'.\r\n", escapecharis);
353 /* ******************************* */
355 static void will_charmode()
357 G.charmode = CHM_TRY;
358 G.telflags |= (UF_ECHO | UF_SGA);
361 putiac2(DO, TELOPT_ECHO);
362 putiac2(DO, TELOPT_SGA);
366 static void do_linemode()
368 G.charmode = CHM_TRY;
369 G.telflags &= ~(UF_ECHO | UF_SGA);
372 putiac2(DONT, TELOPT_ECHO);
373 putiac2(DONT, TELOPT_SGA);
377 /* ******************************* */
379 static inline void to_notsup(char c)
381 if (G.telwish == WILL) putiac2(DONT, c);
382 else if (G.telwish == DO) putiac2(WONT, c);
385 static inline void to_echo()
387 /* if server requests ECHO, don't agree */
388 if (G.telwish == DO) { putiac2(WONT, TELOPT_ECHO); return; }
389 else if (G.telwish == DONT) return;
391 if (G.telflags & UF_ECHO)
393 if (G.telwish == WILL)
397 if (G.telwish == WONT)
400 if (G.charmode != CHM_OFF)
401 G.telflags ^= UF_ECHO;
403 if (G.telflags & UF_ECHO)
404 putiac2(DO, TELOPT_ECHO);
406 putiac2(DONT, TELOPT_ECHO);
409 WriteCS(1, "\r\n"); /* sudden modec */
412 static inline void to_sga()
414 /* daemon always sends will/wont, client do/dont */
416 if (G.telflags & UF_SGA)
418 if (G.telwish == WILL)
422 if (G.telwish == WONT)
425 if ((G.telflags ^= UF_SGA) & UF_SGA) /* toggle */
426 putiac2(DO, TELOPT_SGA);
428 putiac2(DONT, TELOPT_SGA);
433 static void telopt(byte c)
437 case TELOPT_ECHO: to_echo(c); break;
438 case TELOPT_SGA: to_sga(c); break;
439 default: to_notsup(c); break;
444 /* ******************************* */
446 /* subnegotiation -- ignore all */
448 static int subneg(byte c)
454 G.telstate = TS_SUB2;
459 G.telstate = TS_SUB1;
465 /* ******************************* */
467 static void fgotsig(int sig)
473 static void rawmode()
475 tcsetattr(0, TCSADRAIN, &G.termios_raw);
478 static void cookmode()
480 tcsetattr(0, TCSADRAIN, &G.termios_def);
483 extern int telnet_main(int argc, char** argv)
488 struct pollfd ufds[2];
495 memset(&G, 0, sizeof G);
497 if (tcgetattr(0, &G.termios_def) < 0)
500 G.termios_raw = G.termios_def;
502 cfmakeraw(&G.termios_raw);
504 if (argc < 2) usage(telnet_usage);
505 port = (argc > 2)? getport(argv[2]): 23;
507 G.buf = xmalloc(DATABUFSIZE);
508 G.iacbuf = xmalloc(IACBUFSIZE);
510 host = getserver(argv[1]);
512 G.netfd = remote_connect(host, port);
514 signal(SIGINT, fgotsig);
517 ufds[0].fd = 0; ufds[1].fd = G.netfd;
518 ufds[0].events = ufds[1].events = POLLIN;
522 FD_SET(G.netfd, &readfds);
529 fd_set rfds = readfds;
531 switch (select(maxfd, &rfds, NULL, NULL, NULL))
533 switch (poll(ufds, 2, -1))
539 /* error, ignore and/or log something, bay go to loop */
548 if (ufds[0].revents) /* well, should check POLLIN, but ... */
550 if (FD_ISSET(0, &rfds))
553 G.len = read(0, G.buf, DATABUFSIZE);
558 TRACE(0, ("Read con: %d\n", G.len));
564 if (ufds[1].revents) /* well, should check POLLIN, but ... */
566 if (FD_ISSET(G.netfd, &rfds))
569 G.len = read(G.netfd, G.buf, DATABUFSIZE);
573 WriteCS(1, "Connection closed by foreign host.\r\n");
576 TRACE(0, ("Read netfd (%d): %d\n", G.netfd, G.len));
584 static int getport(char * p)
586 unsigned int port = atoi(p);
588 if ((unsigned)(port - 1 ) > 65534)
590 error_msg_and_die("%s: bad port number\n", p);
595 static struct in_addr getserver(char * host)
600 if ((he = gethostbyname(host)) == NULL)
602 error_msg_and_die("%s: Unknown host\n", host);
604 memcpy(&addr, he->h_addr, sizeof addr);
606 TRACE(1, ("addr: %s\n", inet_ntoa(addr)));
611 static int create_socket()
613 return socket(AF_INET, SOCK_STREAM, 0);
616 static void setup_sockaddr_in(struct sockaddr_in * addr, int port)
618 memset(addr, 0, sizeof addr);
619 addr->sin_family = AF_INET;
620 addr->sin_port = htons(port);
624 static int local_bind(int port)
626 struct sockaddr_in s_addr;
627 int s = create_socket();
629 setup_sockaddr_in(&s_addr, port);
631 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
633 if (bind(s, &s_addr, sizeof s_addr) < 0)
635 char * e = sys_errlist[errno];
636 syserrorexit("bind");
645 static int remote_connect(struct in_addr addr, int port)
647 struct sockaddr_in s_addr;
648 int s = create_socket();
650 setup_sockaddr_in(&s_addr, port);
651 s_addr.sin_addr = addr;
653 setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof one);
655 if (connect(s, (struct sockaddr *)&s_addr, sizeof s_addr) < 0)
657 perror_msg_and_die("Unable to connect to remote host");
664 c-file-style: "linux"