aboutsummaryrefslogtreecommitdiff
path: root/toys/pending/telnet.c
blob: b4d5c72f228e4560770b9c1b3e799dbfe5a8decc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* telnet.c - Telnet client.
 *
 * Copyright 2012 Madhur Verma <mad.flexi@gmail.com>
 * Copyright 2013 Kyungwan Han <asura321@gmail.com>
 * Modified by Ashwini Kumar <ak.ashwini1981@gmail.com>
 *
 * Not in SUSv4.

USE_TELNET(NEWTOY(telnet, "<1>2", TOYFLAG_BIN))

config TELNET
  bool "telnet"
  default n
  help
    usage: telnet HOST [PORT]

    Connect to telnet server
*/

#define FOR_telnet
#include "toys.h"
#include <arpa/telnet.h>
#include <netinet/in.h>

GLOBALS(
  int port;
  int sfd;
  char buff[128];
  int pbuff;
  char iac[256];
  int piac;
  char *ttype;
  struct termios def_term;
  struct termios raw_term;
  uint8_t term_ok;
  uint8_t term_mode;
  uint8_t flags;
  unsigned win_width;
  unsigned win_height;
)

#define DATABUFSIZE 128
#define IACBUFSIZE  256
#define CM_TRY      0
#define CM_ON       1
#define CM_OFF      2
#define UF_ECHO     0x01
#define UF_SGA      0x02

// sets terminal mode: LINE or CHARACTER based om internal stat.
static char const es[] = "\r\nEscape character is ";
static void set_mode(void)
{
  if (TT.flags & UF_ECHO) {
    if (TT.term_mode == CM_TRY) {
      TT.term_mode = CM_ON;
      printf("\r\nEntering character mode%s'^]'.\r\n", es);
      if (TT.term_ok) tcsetattr(0, TCSADRAIN, &TT.raw_term);
    }
  } else {
    if (TT.term_mode != CM_OFF) {
      TT.term_mode = CM_OFF;
      printf("\r\nEntering line mode%s'^C'.\r\n", es);
      if (TT.term_ok) tcsetattr(0, TCSADRAIN, &TT.def_term);
    }
  }
}

// flushes all data in IAC buff to server.
static void flush_iac(void)
{
  int wlen = write(TT.sfd, TT.iac, TT.piac);

  if(wlen <= 0) error_msg("IAC : send failed.");
  TT.piac = 0;
}

// puts DATA in iac buff of length LEN and updates iac buff pointer.
static void put_iac(int len, ...)
{
  va_list va; 

  if(TT.piac + len >= IACBUFSIZE) flush_iac();
  va_start(va, len);
  for(;len > 0; TT.iac[TT.piac++] = (uint8_t)va_arg(va, int), len--);
  va_end(va);
}

// puts string STR in iac buff and updates iac buff pointer.
static void str_iac(char *str)
{
  int len = strlen(str);

  if(TT.piac + len + 1 >= IACBUFSIZE) flush_iac();
  strcpy(&TT.iac[TT.piac], str);
  TT.piac += len+1;
}

static void handle_esc(void)
{
  char input;

  if(toys.signal && TT.term_ok) tcsetattr(0, TCSADRAIN, &TT.raw_term);
  xwrite(1,"\r\nConsole escape. Commands are:\r\n\n"
      " l  go to line mode\r\n"
      " c  go to character mode\r\n"
      " z  suspend telnet\r\n"
      " e  exit telnet\r\n", 114);

  if (read(0, &input, 1) <= 0) {
    if(TT.term_ok) tcsetattr(0, TCSADRAIN, &TT.def_term);
    exit(0);
  }

  switch (input) {
  case 'l':
    if (!toys.signal) {
      TT.term_mode = CM_TRY;
      TT.flags &= ~(UF_ECHO | UF_SGA);
      set_mode();
      put_iac(6, IAC,DONT,TELOPT_ECHO,IAC,DONT, TELOPT_SGA);
      flush_iac();
      goto ret;
    }
    break;
  case 'c':
    if (toys.signal) {
      TT.term_mode = CM_TRY;
      TT.flags |= (UF_ECHO | UF_SGA);
      set_mode();
      put_iac(6, IAC,DO,TELOPT_ECHO,IAC,DO,TELOPT_SGA);
      flush_iac();
      goto ret;
    }
    break;
  case 'z':
    if(TT.term_ok) tcsetattr(0, TCSADRAIN, &TT.def_term);
    kill(0, SIGTSTP);
    if(TT.term_ok) tcsetattr(0, TCSADRAIN, &TT.raw_term);
    break;
  case 'e':
    if(TT.term_ok) tcsetattr(0, TCSADRAIN, &TT.def_term);
    exit(0);
  default: break;
  }

  xwrite(1, "continuing...\r\n", 15);
  if (toys.signal && TT.term_ok) tcsetattr(0, TCSADRAIN, &TT.def_term);

ret:
  toys.signal = 0;
}

/*
 * handles telnet SUB NEGOTIATIONS
 * only terminal type is supported.
 */
static void handle_negotiations(void)
{
  char opt = TT.buff[TT.pbuff++];

  switch(opt) {
  case TELOPT_TTYPE:
    opt =  TT.buff[TT.pbuff++];
    if(opt == TELQUAL_SEND) {
      put_iac(4, IAC,SB,TELOPT_TTYPE,TELQUAL_IS);
      str_iac(TT.ttype);
      put_iac(2, IAC,SE);
    }
    break;
  default: break;
  }
}

/*
 * handles server's DO DONT WILL WONT requests.
 * supports ECHO, SGA, TTYPE, NAWS
 */
static void handle_ddww(char ddww)
{
  char opt = TT.buff[TT.pbuff++];

  switch (opt) {
  case TELOPT_ECHO: /* ECHO */
    if (ddww == DO) put_iac(3, IAC,WONT,TELOPT_ECHO);
    if(ddww == DONT) break;
    if (TT.flags & UF_ECHO) {
        if (ddww == WILL) return;
      } else if (ddww == WONT) return;
    if (TT.term_mode != CM_OFF) TT.flags ^= UF_ECHO;
    (TT.flags & UF_ECHO)? put_iac(3, IAC,DO,TELOPT_ECHO) : 
      put_iac(3, IAC,DONT,TELOPT_ECHO);
    set_mode();
    printf("\r\n");
    break;

  case TELOPT_SGA: /* Supress GO Ahead */
    if (TT.flags & UF_SGA){ if (ddww == WILL) return;
    } else if (ddww == WONT) return;

    TT.flags ^= UF_SGA;
    (TT.flags & UF_SGA)? put_iac(3, IAC,DO,TELOPT_SGA) :
      put_iac(3, IAC,DONT,TELOPT_SGA);
    break;

  case TELOPT_TTYPE: /* Terminal Type */
    (TT.ttype)? put_iac(3, IAC,WILL,TELOPT_TTYPE):
      put_iac(3, IAC,WONT,TELOPT_TTYPE);
    break;

  case TELOPT_NAWS: /* Window Size */
    put_iac(3, IAC,WILL,TELOPT_NAWS);
    put_iac(9, IAC,SB,TELOPT_NAWS,(TT.win_width >> 8) & 0xff,
        TT.win_width & 0xff,(TT.win_height >> 8) & 0xff,
        TT.win_height & 0xff,IAC,SE);
    break;

  default: /* Default behaviour is to say NO */
    if(ddww == WILL) put_iac(3, IAC,DONT,opt);
    if(ddww == DO) put_iac(3, IAC,WONT,opt);
    break;
  }
}

/*
 * parses data which is read from server of length LEN.
 * and passes it to console.
 */
static int read_server(int len)
{
  int i = 0;
  char curr;
  TT.pbuff = 0;

  do {
    curr = TT.buff[TT.pbuff++];
    if (curr == IAC) {
      curr = TT.buff[TT.pbuff++];
      switch (curr) {
      case DO:    /* FALLTHROUGH */
      case DONT:    /* FALLTHROUGH */
      case WILL:    /* FALLTHROUGH */
      case WONT:
        handle_ddww(curr);
        break;
      case SB:
        handle_negotiations();
        break;
      case SE:
        break;
      default: break;
      }
    } else {
      toybuf[i++] = curr;
      if (curr == '\r') { curr = TT.buff[TT.pbuff++];
        if (curr != '\0') TT.pbuff--;
      }
    }
  } while (TT.pbuff < len);

  if (i) xwrite(0, toybuf, i);
  return 0;
}

/*
 * parses data which is read from console of length LEN
 * and passes it to server.
 */
static void write_server(int len)
{
  char *c = (char*)TT.buff;
  int i = 0;

  for (; len > 0; len--, c++) {
    if (*c == 0x1d) {
      handle_esc();
      return;
    }
    toybuf[i++] = *c;
    if (*c == IAC) toybuf[i++] = *c; /* IAC -> IAC IAC */
    else if (*c == '\r') toybuf[i++] = '\0'; /* CR -> CR NUL */
  }
  if(i) xwrite(TT.sfd, toybuf, i);
}

void telnet_main(void)
{
  char *port = "23";
  int set = 1, len;
  struct pollfd pfds[2];

  TT.win_width = 80; //columns
  TT.win_height = 24; //rows

  if (toys.optc == 2) port = toys.optargs[1];

  TT.ttype = getenv("TERM");
  if(!TT.ttype) TT.ttype = "";
  if(strlen(TT.ttype) > IACBUFSIZE-1) TT.ttype[IACBUFSIZE - 1] = '\0';

  if (!tcgetattr(0, &TT.def_term)) {
    TT.term_ok = 1;
    TT.raw_term = TT.def_term;
    cfmakeraw(&TT.raw_term);
  }
  terminal_size(&TT.win_width, &TT.win_height);

  TT.sfd = xconnectany(xgetaddrinfo(*toys.optargs, port, 0, SOCK_STREAM,
    IPPROTO_TCP, 0));
  setsockopt(TT.sfd, SOL_SOCKET, SO_REUSEADDR, &set, sizeof(set));
  setsockopt(TT.sfd, SOL_SOCKET, SO_KEEPALIVE, &set, sizeof(set));

  pfds[0].fd = 0;
  pfds[0].events = POLLIN;
  pfds[1].fd = TT.sfd;
  pfds[1].events = POLLIN;

  signal(SIGINT, generic_signal);
  while(1) {
    if(TT.piac) flush_iac();
    if(poll(pfds, 2, -1) < 0) {
      if (toys.signal) handle_esc();
      else sleep(1);

      continue;
    }
    if(pfds[0].revents) {
      len = read(0, TT.buff, DATABUFSIZE);
      if(len > 0) write_server(len);
      else return;
    }
    if(pfds[1].revents) {
      len = read(TT.sfd, TT.buff, DATABUFSIZE);
      if(len > 0) read_server(len);
      else {
        printf("Connection closed by foreign host\r\n");
        if(TT.term_ok) tcsetattr(0, TCSADRAIN, &TT.def_term);
        exit(1);
      }
    }
  }
}