summaryrefslogtreecommitdiff
path: root/rtemsbsd/telnetd/pty.c
blob: a593bbd22428731a7dd75b2485377fb2a2f17982 (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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
 * /dev/ptyXX  (Support for pseudo-terminals)
 *
 *  Original Author: Fernando RUIZ CASAS (fernando.ruiz@ctv.es)
 *  May 2001
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 *  Till Straumann <strauman@slac.stanford.edu>
 *
 *   - converted into a loadable module
 *   - NAWS support / ioctls for querying/setting the window
 *     size added.
 *   - don't delete the running task when the connection
 *     is closed. Rather let 'read()' return a 0 count so
 *     they may cleanup. Some magic hack works around termios
 *     limitation.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#define DEBUG_WH    (1<<0)
#define DEBUG_DETAIL  (1<<1)

/* #define DEBUG DEBUG_WH */

/*-----------------------------------------*/
#include <sys/ttycom.h>
#include <rtems/pty.h>
#include <rtems/seterr.h>
#include <errno.h>
#include <sys/socket.h>
/*-----------------------------------------*/
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
/*-----------------------------------------*/
#define IAC_ESC    255
#define IAC_DONT   254
#define IAC_DO     253
#define IAC_WONT   252
#define IAC_WILL   251
#define IAC_SB     250
#define IAC_GA     249
#define IAC_EL     248
#define IAC_EC     247
#define IAC_AYT    246
#define IAC_AO     245
#define IAC_IP     244
#define IAC_BRK    243
#define IAC_DMARK  242
#define IAC_NOP    241
#define IAC_SE     240
#define IAC_EOR    239

#define SB_MAX     RTEMS_PTY_SB_MAX

static bool ptyPollInitialize(rtems_termios_tty *,
  rtems_termios_device_context *, struct termios *,
  rtems_libio_open_close_args_t *);
static void ptyShutdown(rtems_termios_tty *,
  rtems_termios_device_context *, rtems_libio_open_close_args_t *);
static void ptyPollWrite(rtems_termios_device_context *, const char *, size_t);
static int ptyPollRead(rtems_termios_device_context *);
static bool ptySetAttributes(rtems_termios_device_context *,
  const struct termios *);
static int my_pty_control(rtems_termios_device_context *,
  ioctl_command_t, void *);

static const rtems_termios_device_handler pty_handler = {
  .first_open = ptyPollInitialize,
  .last_close = ptyShutdown,
  .poll_read = ptyPollRead,
  .write = ptyPollWrite,
  .set_attributes = ptySetAttributes,
  .ioctl = my_pty_control
};

static
int send_iac(rtems_pty_context *pty, unsigned char mode, unsigned char option)
{
  unsigned char buf[3];

  buf[0]=IAC_ESC;
  buf[1]=mode;
  buf[2]=option;
  return write(pty->socket, buf, sizeof(buf));
}

const char *rtems_pty_initialize(rtems_pty_context *pty, uintptr_t unique)
{
  rtems_status_code sc;

  memset(pty, 0, sizeof(*pty));
  (void)snprintf(pty->name, sizeof(pty->name), "/dev/pty%" PRIuPTR, unique);
  rtems_termios_device_context_initialize(&pty->base, "pty");
  pty->socket = -1;
  sc = rtems_termios_device_install(pty->name, &pty_handler, NULL, &pty->base);
  if (sc != RTEMS_SUCCESSFUL) {
    return NULL;
  }

  return pty->name;
}

void rtems_pty_close_socket(rtems_pty_context *pty)
{
  if (pty->socket >= 0) {
    (void)close(pty->socket);
    pty->socket = -1;
  }
}

void rtems_pty_set_socket(rtems_pty_context *pty, int socket)
{
  struct timeval t;

  rtems_pty_close_socket(pty);
  pty->socket = socket;

  /* set a long polling interval to save CPU time */
  t.tv_sec=2;
  t.tv_usec=00000;
  (void)setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, &t, sizeof(t));

  /* inform the client that we will echo */
  send_iac(pty, IAC_WILL, 1);
}

/*-----------------------------------------------------------*/
/*
 * The NVT terminal is negociated in PollRead and PollWrite
 * with every BYTE sendded or received.
 * A litle status machine in the pty_read_byte(int minor)
 *
 */
static const char IAC_AYT_RSP[]="\r\nAYT? Yes, RTEMS-SHELL is here\r\n";
static const char IAC_BRK_RSP[]="<*Break*>";
static const char IAC_IP_RSP []="<*Interrupt*>";

static int
handleSB(rtems_pty_context *pty)
{
  switch (pty->sb_buf[0]) {
    case 31:  /* NAWS */
      pty->width  = (pty->sb_buf[1]<<8) + pty->sb_buf[2];
      pty->height = (pty->sb_buf[3]<<8) + pty->sb_buf[4];
#if DEBUG & DEBUG_WH
      fprintf(stderr,
          "Setting width/height to %ix%i\n",
          pty->width,
          pty->height);
#endif
      break;
    default:
      break;
  }
  return 0;
}

static int ptyPollRead(rtems_termios_device_context *base)
{ /* Characters written to the client side*/
   rtems_pty_context *pty = (rtems_pty_context *)base;
   unsigned char  value;
   unsigned int  omod;
   int      count;
   int      result;

   count=read(pty->socket,&value,sizeof(value));
   if (count<0)
    return -1;

   if (count<1) {
      /* Unfortunately, there is no way of passing an EOF
       * condition through the termios driver. Hence, we
       * resort to an ugly hack. Setting cindex>ccount
       * causes the termios driver to return a read count
       * of '0' which is what we want here. We leave
       * 'errno' untouched.
       */
      pty->ttyp->cindex=pty->ttyp->ccount+1;
      return pty->ttyp->termios.c_cc[VEOF];
   };

   omod=pty->iac_mode;
   pty->iac_mode=0;
   switch(omod & 0xff) {
       case IAC_ESC:
           switch(value) {
               case IAC_ESC :
                   /* in case this is an ESC ESC sequence in SB mode */
                   pty->iac_mode = omod>>8;
                   return IAC_ESC;
               case IAC_DONT:
               case IAC_DO  :
               case IAC_WONT:
               case IAC_WILL:
                   pty->iac_mode=value;
                   return -1;
               case IAC_SB  :
#if DEBUG & DEBUG_DETAIL
                   printk("SB\n");
#endif
                   pty->iac_mode=value;
                   pty->sb_ind=0;
                   return -100;
               case IAC_GA  :
                   return -1;
               case IAC_EL  :
                   return 0x03; /* Ctrl-C*/
               case IAC_EC  :
                   return '\b';
               case IAC_AYT :
                   write(pty->socket,IAC_AYT_RSP,strlen(IAC_AYT_RSP));
                   return -1;
               case IAC_AO  :
                   return -1;
               case IAC_IP  :
                   write(pty->socket,IAC_IP_RSP,strlen(IAC_IP_RSP));
                   return -1;
               case IAC_BRK :
                   write(pty->socket,IAC_BRK_RSP,strlen(IAC_BRK_RSP));
                   return -1;
               case IAC_DMARK:
                   return -2;
               case IAC_NOP :
                   return -1;
               case IAC_SE  :
#if DEBUG & DEBUG_DETAIL
                  {
                  int i;
                  printk("SE");
                  for (i=0; i<pty->sb_ind; i++)
                    printk(" %02x",pty->sb_buf[i]);
                  printk("\n");
                  }
#endif
                  handleSB(pty);
               return -101;
               case IAC_EOR :
                   return -102;
               default      :
                   return -1;
           };
           break;

       case IAC_SB:
           pty->iac_mode=omod;
           if (IAC_ESC==value) {
             pty->iac_mode=(omod<<8)|value;
           } else {
             if (pty->sb_ind < SB_MAX)
               pty->sb_buf[pty->sb_ind++]=value;
           }
           return -1;

       case IAC_WILL:
           if (value==34){
              send_iac(pty,IAC_DONT,   34);  /*LINEMODE*/
              send_iac(pty,IAC_DO  ,    1);  /*ECHO    */
           } else if (value==31) {
              send_iac(pty,IAC_DO  ,   31);  /*NAWS    */
#if DEBUG & DEBUG_DETAIL
              printk("replied DO NAWS\n");
#endif
           } else {
              send_iac(pty,IAC_DONT,value);
           }
           return -1;
       case IAC_DONT:
           return -1;
       case IAC_DO  :
           if (value==3) {
              send_iac(pty,IAC_WILL,    3);  /* GO AHEAD*/
           } else  if (value==1) {
              send_iac(pty,IAC_WILL,    1);  /* ECHO */
           } else {
              send_iac(pty,IAC_WONT,value);
           };
           return -1;
       case IAC_WONT:
           if (value==1) {
             send_iac(pty,IAC_WILL,    1);
           } else { /* ECHO */
             send_iac(pty,IAC_WONT,value);
           }
           return -1;
       default:
           if (value==IAC_ESC) {
              pty->iac_mode=value;
              return -1;
           } else {
              result=value;
              if ( 0
                /* map CRLF to CR for symmetry */
                 || ((value=='\n') && pty->last_cr)
                /* map telnet CRNUL to CR down here */
                 || ((value==0) && pty->last_cr)
                ) result=-1;
               pty->last_cr=(value=='\r');
               return result;
           };
   };
  /* should never get here but keep compiler happy */
  return -1;
}

/*-----------------------------------------------------------*/
/* Set the 'Hardware'                                        */
/*-----------------------------------------------------------*/
static bool
ptySetAttributes(rtems_termios_device_context *base, const struct termios *t)
{
  rtems_pty_context *pty = (rtems_pty_context *)base;
  pty->c_cflag = t->c_cflag;
  return true;
}
/*-----------------------------------------------------------*/
static bool
ptyPollInitialize(rtems_termios_tty *ttyp,
  rtems_termios_device_context *base, struct termios *t,
  rtems_libio_open_close_args_t *args)
{
  rtems_pty_context *pty = (rtems_pty_context *)base;
  pty->ttyp = ttyp;
  pty->iac_mode = 0;
  pty->sb_ind = 0;
  pty->width = 0;
  pty->height = 0;
  return ptySetAttributes(&pty->base, t);
}
/*-----------------------------------------------------------*/
static void
ptyShutdown(rtems_termios_tty *ttyp,
  rtems_termios_device_context *base, rtems_libio_open_close_args_t *arg)
{
  rtems_pty_context *pty = (rtems_pty_context *)base;
  close(pty->socket);
}
/*-----------------------------------------------------------*/
/* Write Characters into pty device                          */
/*-----------------------------------------------------------*/
static void
ptyPollWrite(rtems_termios_device_context *base, const char *buf, size_t len)
{
  rtems_pty_context *pty = (rtems_pty_context *)base;

  while (len > 0) {
    ssize_t n = write(pty->socket, buf, len);
    if (n <= 0) {
      break;
    }

    buf += (size_t)n;
    len -= (size_t)n;
  }
}

static int
my_pty_control(rtems_termios_device_context *base,
  ioctl_command_t request, void *buffer)
{
  rtems_pty_context *p = (rtems_pty_context *)base;
  struct winsize *wp = buffer;

  switch (request) {
    case TIOCGWINSZ:
      wp->ws_row = p->height;
      wp->ws_col = p->width;
#if DEBUG & DEBUG_WH
      fprintf(stderr,
          "ioctl(TIOCGWINSZ), returning %ix%i\n",
          wp->ws_col,
          wp->ws_row);
#endif
      break;
    case TIOCSWINSZ:
#if DEBUG & DEBUG_WH
      fprintf(stderr,
          "ioctl(TIOCGWINSZ), setting %ix%i\n",
          wp->ws_col,
          wp->ws_row);
#endif

      p->height = wp->ws_row;
      p->width  = wp->ws_col;
      break;
    default:
      rtems_set_errno_and_return_minus_one(EINVAL);
      break;
  }

  return 0;
}