1 /* telnet.c - Telnet client.
3 * Copyright 2012 Madhur Verma <mad.flexi@gmail.com>
4 * Copyright 2013 Kyungwan Han <asura321@gmail.com>
5 * Modified by Ashwini Kumar <ak.ashwini1981@gmail.com>
9 USE_TELNET(NEWTOY(telnet, "<1>2", TOYFLAG_BIN))
15 usage: telnet HOST [PORT]
17 Connect to telnet server
22 #include <arpa/telnet.h>
23 #include <netinet/in.h>
34 struct termios def_term;
35 struct termios raw_term;
43 #define DATABUFSIZE 128
44 #define IACBUFSIZE 256
51 // sets terminal mode: LINE or CHARACTER based om internal stat.
52 static char const es[] = "\r\nEscape character is ";
53 static void set_mode(void)
55 if (TT.flags & UF_ECHO) {
56 if (TT.term_mode == CM_TRY) {
58 printf("\r\nEntering character mode%s'^]'.\r\n", es);
59 if (TT.term_ok) tcsetattr(0, TCSADRAIN, &TT.raw_term);
62 if (TT.term_mode != CM_OFF) {
63 TT.term_mode = CM_OFF;
64 printf("\r\nEntering line mode%s'^C'.\r\n", es);
65 if (TT.term_ok) tcsetattr(0, TCSADRAIN, &TT.def_term);
70 // flushes all data in IAC buff to server.
71 static void flush_iac(void)
73 int wlen = write(TT.sfd, TT.iac, TT.piac);
75 if(wlen <= 0) error_msg("IAC : send failed.");
79 // puts DATA in iac buff of length LEN and updates iac buff pointer.
80 static void put_iac(int len, ...)
84 if(TT.piac + len >= IACBUFSIZE) flush_iac();
86 for(;len > 0; TT.iac[TT.piac++] = (uint8_t)va_arg(va, int), len--);
90 // puts string STR in iac buff and updates iac buff pointer.
91 static void str_iac(char *str)
93 int len = strlen(str);
95 if(TT.piac + len + 1 >= IACBUFSIZE) flush_iac();
96 strcpy(&TT.iac[TT.piac], str);
100 static void handle_esc(void)
104 if(toys.signal && TT.term_ok) tcsetattr(0, TCSADRAIN, &TT.raw_term);
105 xwrite(1,"\r\nConsole escape. Commands are:\r\n\n"
106 " l go to line mode\r\n"
107 " c go to character mode\r\n"
108 " z suspend telnet\r\n"
109 " e exit telnet\r\n", 114);
111 if (read(STDIN_FILENO, &input, 1) <= 0) {
112 if(TT.term_ok) tcsetattr(0, TCSADRAIN, &TT.def_term);
119 TT.term_mode = CM_TRY;
120 TT.flags &= ~(UF_ECHO | UF_SGA);
122 put_iac(6, IAC,DONT,TELOPT_ECHO,IAC,DONT, TELOPT_SGA);
129 TT.term_mode = CM_TRY;
130 TT.flags |= (UF_ECHO | UF_SGA);
132 put_iac(6, IAC,DO,TELOPT_ECHO,IAC,DO,TELOPT_SGA);
138 if(TT.term_ok) tcsetattr(0, TCSADRAIN, &TT.def_term);
140 if(TT.term_ok) tcsetattr(0, TCSADRAIN, &TT.raw_term);
143 if(TT.term_ok) tcsetattr(0, TCSADRAIN, &TT.def_term);
148 xwrite(1, "continuing...\r\n", 15);
149 if (toys.signal && TT.term_ok) tcsetattr(0, TCSADRAIN, &TT.def_term);
156 * handles telnet SUB NEGOTIATIONS
157 * only terminal type is supported.
159 static void handle_negotiations(void)
161 char opt = TT.buff[TT.pbuff++];
165 opt = TT.buff[TT.pbuff++];
166 if(opt == TELQUAL_SEND) {
167 put_iac(4, IAC,SB,TELOPT_TTYPE,TELQUAL_IS);
177 * handles server's DO DONT WILL WONT requests.
178 * supports ECHO, SGA, TTYPE, NAWS
180 static void handle_ddww(char ddww)
182 char opt = TT.buff[TT.pbuff++];
185 case TELOPT_ECHO: /* ECHO */
186 if (ddww == DO) put_iac(3, IAC,WONT,TELOPT_ECHO);
187 if(ddww == DONT) break;
188 if (TT.flags & UF_ECHO) {
189 if (ddww == WILL) return;
190 } else if (ddww == WONT) return;
191 if (TT.term_mode != CM_OFF) TT.flags ^= UF_ECHO;
192 (TT.flags & UF_ECHO)? put_iac(3, IAC,DO,TELOPT_ECHO) :
193 put_iac(3, IAC,DONT,TELOPT_ECHO);
198 case TELOPT_SGA: /* Supress GO Ahead */
199 if (TT.flags & UF_SGA){ if (ddww == WILL) return;
200 } else if (ddww == WONT) return;
203 (TT.flags & UF_SGA)? put_iac(3, IAC,DO,TELOPT_SGA) :
204 put_iac(3, IAC,DONT,TELOPT_SGA);
207 case TELOPT_TTYPE: /* Terminal Type */
208 (TT.ttype)? put_iac(3, IAC,WILL,TELOPT_TTYPE):
209 put_iac(3, IAC,WONT,TELOPT_TTYPE);
212 case TELOPT_NAWS: /* Window Size */
213 put_iac(3, IAC,WILL,TELOPT_NAWS);
214 put_iac(9, IAC,SB,TELOPT_NAWS,(TT.win_width >> 8) & 0xff,
215 TT.win_width & 0xff,(TT.win_height >> 8) & 0xff,
216 TT.win_height & 0xff,IAC,SE);
219 default: /* Default behaviour is to say NO */
220 if(ddww == WILL) put_iac(3, IAC,DONT,opt);
221 if(ddww == DO) put_iac(3, IAC,WONT,opt);
227 * parses data which is read from server of length LEN.
228 * and passes it to console.
230 static int read_server(int len)
237 curr = TT.buff[TT.pbuff++];
239 curr = TT.buff[TT.pbuff++];
241 case DO: /* FALLTHROUGH */
242 case DONT: /* FALLTHROUGH */
243 case WILL: /* FALLTHROUGH */
248 handle_negotiations();
256 if (curr == '\r') { curr = TT.buff[TT.pbuff++];
257 if (curr != '\0') TT.pbuff--;
260 } while (TT.pbuff < len);
262 if (i) xwrite(STDIN_FILENO, toybuf, i);
267 * parses data which is read from console of length LEN
268 * and passes it to server.
270 static void write_server(int len)
272 char *c = (char*)TT.buff;
275 for (; len > 0; len--, c++) {
281 if (*c == IAC) toybuf[i++] = *c; /* IAC -> IAC IAC */
282 else if (*c == '\r') toybuf[i++] = '\0'; /* CR -> CR NUL */
284 if(i) xwrite(TT.sfd, toybuf, i);
287 void telnet_main(void)
291 struct pollfd pfds[2];
293 TT.win_width = 80; //columns
294 TT.win_height = 24; //rows
296 if (toys.optc == 2) port = toys.optargs[1];
298 TT.ttype = getenv("TERM");
299 if(!TT.ttype) TT.ttype = "";
300 if(strlen(TT.ttype) > IACBUFSIZE-1) TT.ttype[IACBUFSIZE - 1] = '\0';
302 if (!tcgetattr(0, &TT.def_term)) {
304 TT.raw_term = TT.def_term;
305 cfmakeraw(&TT.raw_term);
307 terminal_size(&TT.win_width, &TT.win_height);
309 TT.sfd = xconnect(*toys.optargs, port, 0, SOCK_STREAM, IPPROTO_TCP, 0);
310 setsockopt(TT.sfd, SOL_SOCKET, SO_REUSEADDR, &set, sizeof(set));
311 setsockopt(TT.sfd, SOL_SOCKET, SO_KEEPALIVE, &set, sizeof(set));
313 pfds[0].fd = STDIN_FILENO;
314 pfds[0].events = POLLIN;
316 pfds[1].events = POLLIN;
318 signal(SIGINT, generic_signal);
320 if(TT.piac) flush_iac();
321 if(poll(pfds, 2, -1) < 0) {
322 if (toys.signal) handle_esc();
327 if(pfds[0].revents) {
328 len = read(STDIN_FILENO, TT.buff, DATABUFSIZE);
329 if(len > 0) write_server(len);
332 if(pfds[1].revents) {
333 len = read(TT.sfd, TT.buff, DATABUFSIZE);
334 if(len > 0) read_server(len);
336 printf("Connection closed by foreign host\r\n");
337 if(TT.term_ok) tcsetattr(0, TCSADRAIN, &TT.def_term);