summaryrefslogtreecommitdiff
path: root/teln.c
blob: f434a1c9377218f401624a1abac201de53194902 (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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/* Copyright (c) 1993-2002
 *      Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
 *      Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
 * Copyright (c) 1987 Oliver Laumann
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * 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.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program (see the file COPYING); if not, write to the
 * Free Software Foundation, Inc.,
 * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
 *
 ****************************************************************
 */

#include "rcs.h"
RCS_ID("$Id: teln.c,v 1.6 1994/05/31 12:32:15 mlschroe Exp $ FAU")

#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <netdb.h>

#include "config.h"

#ifdef BUILTIN_TELNET

#include "screen.h"
#include "extern.h"

extern struct win *fore;
extern struct layer *flayer;
extern int visual_bell;
extern char screenterm[];

static void TelReply __P((struct win *, char *, int));
static void TelDocmd __P((struct win *, int, int));
static void TelDosub __P((struct win *));

#define TEL_DEFPORT	23
#define TEL_CONNECTING	(-2)

#define TC_IAC          255
#define TC_DONT         254
#define TC_DO           253
#define TC_WONT         252
#define TC_WILL         251
#define TC_SB           250
#define TC_BREAK        243
#define TC_SE           240

#define TC_S "S  b      swWdDc"

#define TO_BINARY       0
#define TO_ECHO         1
#define TO_SGA          3
#define TO_TM           6
#define TO_TTYPE        24
#define TO_NAWS         31
#define TO_TSPEED       32
#define TO_LFLOW        33
#define TO_XDISPLOC     35
#define TO_NEWENV       39

#define TO_S "be c                    t      wsf xE  E"


static unsigned char tn_init[] = {
  TC_IAC, TC_DO, TO_SGA,
  TC_IAC, TC_WILL, TO_TTYPE,
  TC_IAC, TC_WILL, TO_NAWS,
  TC_IAC, TC_WILL, TO_LFLOW,
};

static void
tel_connev_fn(ev, data)
struct event *ev;
char *data;
{
  struct win *p = (struct win *)data;
  if (connect(p->w_ptyfd, (struct sockaddr *)&p->w_telsa, sizeof(p->w_telsa)) && errno != EISCONN)
    {
      char buf[1024];
      buf[0] = ' ';
      strncpy(buf + 1, strerror(errno), sizeof(buf) - 2);
      buf[sizeof(buf) - 1] = 0;
      WriteString(p, buf, strlen(buf));
      WindowDied(p);
      return;
    }
  WriteString(p, "connected.\r\n", 12);
  evdeq(&p->w_telconnev);
  p->w_telstate = 0;
}

int
TelOpen(args)
char **args;
{
  int fd;
  int on = 1;

  if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
    {
      Msg(errno, "TelOpen: socket");
      return -1;
    }
  if (setsockopt(fd, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof(on)))
    Msg(errno, "TelOpen: setsockopt SO_OOBINLINE");
  return fd;
}

int
TelConnect(p)
struct win *p;
{
  int port = TEL_DEFPORT;
  struct hostent *hp;
  char **args;
  char buf[256];

  args = p->w_cmdargs + 1;

  if (!*args)
    {
      Msg(0, "Usage: screen //telnet host [port]");
      return -1;
    }
  if (args[1])
    port = atoi(args[1]);
  p->w_telsa.sin_family = AF_INET;
  if((p->w_telsa.sin_addr.s_addr = inet_addr(*args)) == -1)
    {
      if ((hp = gethostbyname(*args)) == NULL)
        {
	  Msg(0, "unknown host: %s", *args);
	  return -1;
        }
      if (hp->h_length != sizeof(p->w_telsa.sin_addr.s_addr) || hp->h_addrtype != AF_INET)
	{
	  Msg(0, "Bad address type for %s", hp->h_name);
	  return -1;
	}
      bcopy((char *)hp->h_addr,(char *)&p->w_telsa.sin_addr.s_addr, hp->h_length);
      p->w_telsa.sin_family = hp->h_addrtype;
    }
  p->w_telsa.sin_port = htons(port);
  if (port != TEL_DEFPORT)
    sprintf(buf, "Trying %s %d...", inet_ntoa(p->w_telsa.sin_addr), port);
  else
    sprintf(buf, "Trying %s...", inet_ntoa(p->w_telsa.sin_addr));
  WriteString(p, buf, strlen(buf));
  if (connect(p->w_ptyfd, (struct sockaddr *)&p->w_telsa, sizeof(p->w_telsa)))
    {
      if (errno == EINPROGRESS)
        {
	  p->w_telstate = TEL_CONNECTING;
	  p->w_telconnev.fd = p->w_ptyfd;
	  p->w_telconnev.handler = tel_connev_fn;
	  p->w_telconnev.data = (char *)p;
	  p->w_telconnev.type = EV_WRITE;
	  p->w_telconnev.pri = 1;
	  debug("telnet connect in progress...\n");
	  evenq(&p->w_telconnev);
	}
      else
        {
	  Msg(errno, "TelOpen: connect");
	  return -1;
	}
    }
  else
    WriteString(p, "connected.\r\n", 12);
  if (port == TEL_DEFPORT)
    TelReply(p, (char *)tn_init, sizeof(tn_init));
  return 0;
}

int
TelIsline(p)
struct win *p;
{
  return !fore->w_telropts[TO_SGA];
}

void
TelProcessLine(bufpp, lenp)
char **bufpp;
int *lenp;
{
  int echo = !fore->w_telropts[TO_ECHO];
  unsigned char c;
  char *tb;
  int tl;

  char *buf = *bufpp;
  int l = *lenp;
  while (l--)
    {
      c = *(unsigned char *)buf++;
      if (fore->w_telbufl + 2 >= IOSIZE)
	{
	  WBell(fore, visual_bell);
	  continue;
	}
      if (c == '\r')
	{
	  if (echo)
	    WriteString(fore, "\r\n", 2);
	  fore->w_telbuf[fore->w_telbufl++] = '\r';
	  fore->w_telbuf[fore->w_telbufl++] = '\n';
	  tb = fore->w_telbuf;
	  tl = fore->w_telbufl;
	  LayProcess(&tb, &tl);
	  fore->w_telbufl = 0;
	  continue;
	}
      if (c == '\b' && fore->w_telbufl > 0)
	{
	  if (echo)
	    {
	      WriteString(fore, (char *)&c, 1);
	      WriteString(fore, " ", 1);
	      WriteString(fore, (char *)&c, 1);
	    }
	  fore->w_telbufl--;
	}
      if ((c >= 0x20 && c <= 0x7e) || (c >= 0xa0 && c <= 0xff))
	{
	  if (echo)
	    WriteString(fore, (char *)&c, 1);
	  fore->w_telbuf[fore->w_telbufl++] = c;
	}
    }
  *lenp = 0;
}

int
DoTelnet(buf, lenp, f)
char *buf;
int *lenp;
int f;
{
  int echo = !fore->w_telropts[TO_ECHO];
  int cmode = fore->w_telropts[TO_SGA];
  int bin = fore->w_telropts[TO_BINARY];
  char *p = buf, *sbuf;
  int trunc = 0;
  int c;
  int l = *lenp;

  sbuf = p;
  while (l-- > 0)
    {
      c = *(unsigned char *)p++;
      if (c == TC_IAC || (c == '\r' && (l ==0 || *p != '\n') && cmode && !bin))
	{
	  if (cmode && echo)
	    {
	      WriteString(fore, sbuf, p - sbuf);
	      sbuf = p;
	    }
	  if (f-- <= 0)
	    {
	      trunc++;
	      l--;
	    }
	  if (l < 0)
	    {
	      p--;	/* drop char */
	      break;
	    }
	  if (l)
	    bcopy(p, p + 1, l);
	  if (c == TC_IAC)
	    *p++ = c;
	  else if (c == '\r')
	    *p++ = 0;
	  else if (c == '\n')
	    {
	      p[-1] = '\r';
	      *p++ = '\n';
	    }
	}
    }
  *lenp = p - buf;
  return trunc;
}

/* modifies data in-place, returns new length */
int
TelIn(p, buf, len, free)
struct win *p;
char *buf;
int len;
int free;
{
  char *rp, *wp;
  int c;

  rp = wp = buf;
  while (len-- > 0)
    {
      c = *(unsigned char *)rp++;

      if (p->w_telstate >= TC_WILL && p->w_telstate <= TC_DONT)
	{
	  TelDocmd(p, p->w_telstate, c);
	  p->w_telstate = 0;
	  continue;
	}
      if (p->w_telstate == TC_SB || p->w_telstate == TC_SE)
	{
	  if (p->w_telstate == TC_SE && c == TC_IAC)
	    p->w_telsubidx--;
	  if (p->w_telstate == TC_SE && c == TC_SE)
	    {
	      p->w_telsubidx--;
	      TelDosub(p);
	      p->w_telstate = 0;
	      continue;
	    }
	  if (p->w_telstate == TC_SB && c == TC_IAC)
	    p->w_telstate = TC_SE;
	  else
	    p->w_telstate = TC_SB;
	  p->w_telsubbuf[p->w_telsubidx] = c;
	  if (p->w_telsubidx < sizeof(p->w_telsubbuf) - 1)
	    p->w_telsubidx++;
	  continue;
	}
      if (p->w_telstate == TC_IAC)
	{
	  if ((c >= TC_WILL && c <= TC_DONT) || c == TC_SB)
	    {
	      p->w_telsubidx = 0;
	      p->w_telstate = c;
	      continue;
	    }
	  p->w_telstate = 0;
	  if (c != TC_IAC)
	    continue;
	}
      else if (c == TC_IAC)
	{
	  p->w_telstate = c;
	  continue;
	}
      if (p->w_telstate == '\r')
	{
	  p->w_telstate = 0;
	  if (c == 0)
	    continue;		/* suppress trailing \0 */
	}
      else if (c == '\n' && !p->w_telropts[TO_SGA])
	{
	  /* oops... simulate terminal line mode: insert \r */
	  if (wp + 1 == rp)
	    {
	      if (free-- > 0)
		{
		  if (len)
		    bcopy(rp, rp + 1, len);
		  rp++;
		  *wp++ = '\r';
		}
	    }
	  else
	    *wp++ = '\r';
	}
      if (c == '\r')
        p->w_telstate = c;
      *wp++ = c;
    }
  return wp - buf;
}

static void
TelReply(p, str, len)
struct win *p;
char *str;
int len;
{
  if (len <= 0)
    return;
  if (p->w_inlen + len > IOSIZE)
    {
      Msg(0, "Warning: telnet protocol overrun!");
      return;
    }
  bcopy(str, p->w_inbuf + p->w_inlen, len);
  p->w_inlen += len;
}

static void
TelDocmd(p, cmd, opt)
struct win *p;
int cmd, opt;
{
  unsigned char b[3];
  int repl = 0;

  if (cmd == TC_WONT)
    debug2("[WONT %c %d]\n", TO_S[opt], opt);
  if (cmd == TC_WILL)
    debug2("[WILL %c %d]\n", TO_S[opt], opt);
  if (cmd == TC_DONT)
    debug2("[DONT %c %d]\n", TO_S[opt], opt);
  if (cmd == TC_DO)
    debug2("[DO  %c %d]\n", TO_S[opt], opt);

  switch(cmd)
    {
    case TC_WILL:
      if (p->w_telropts[opt] || opt == TO_TM)
	return;
      repl = TC_DONT;
      if (opt == TO_ECHO || opt == TO_SGA || opt == TO_BINARY)
	{
	  p->w_telropts[opt] = 1;
	  /* setcon(); */
	  repl = TC_DO;
	}
      break;
    case TC_WONT:
      if (!p->w_telropts[opt] || opt == TO_TM)
	return;
      repl = TC_DONT;
#if 0
      if (opt == TO_ECHO || opt == TO_SGA)
	setcon();
#endif
      p->w_telropts[opt] = 0;
      break;
    case TC_DO:
      if (p->w_telmopts[opt])
	return;
      repl = TC_WONT;
      if (opt == TO_TTYPE || opt == TO_SGA || opt == TO_BINARY || opt == TO_NAWS || opt == TO_TM || opt == TO_LFLOW)
	{
	  repl = TC_WILL;
	  p->w_telmopts[opt] = 1;
	}
      p->w_telmopts[TO_TM] = 0;
      break;
    case TC_DONT:
      if (!p->w_telmopts[opt])
	return;
      repl = TC_WONT;
      p->w_telmopts[opt] = 0;
      break;
    }
  b[0] = TC_IAC;
  b[1] = repl;
  b[2] = opt;
  TelReply(p, (char *)b, 3);
  if (cmd == TC_DO && opt == TO_NAWS)
    TelWindowSize(p);
}


static void
TelDosub(p)
struct win *p;
{
  char trepl[20 + 6 + 1];
  int l;

  switch(p->w_telsubbuf[0])
    {
    case TO_TTYPE:
      if (p->w_telsubidx != 2 || p->w_telsubbuf[1] != 1)
	return;
      l = strlen(screenterm);
      if (l >= 20)
	break;
      sprintf(trepl, "%c%c%c%c%s%c%c", TC_IAC, TC_SB, TO_TTYPE, 0, screenterm, TC_IAC, TC_SE);
      TelReply(p, trepl, l + 6);
      break;
    case TO_LFLOW:
      if (p->w_telsubidx != 2)
	return;
      debug1("[FLOW %d]\r\n", p->w_telsubbuf[1]);
      break;
    default:
      break;
   }
}

void
TelBreak(p)
struct win *p;
{
  static unsigned char tel_break[] = { TC_IAC, TC_BREAK };
  TelReply(p, (char *)tel_break, 2);
}

void
TelWindowSize(p)
struct win *p;
{
  char s[20], trepl[20], *t;
  int i;

  debug2("TelWindowSize %d %d\n", p->w_width, p->w_height);
  if (p->w_width == 0 || p->w_height == 0 || !p->w_telmopts[TO_NAWS])
    return;
  sprintf(s, "%c%c%c%c%c%c%c%c%c", TC_SB, TC_SB, TO_NAWS, p->w_width / 256, p->w_width & 255, p->w_height / 256, p->w_height & 255, TC_SE, TC_SE);
  t = trepl;
  for (i = 0; i < 9; i++)
    if ((unsigned char)(*t++ = s[i]) == TC_IAC)
      *t++ = TC_IAC;
  trepl[0] = TC_IAC;
  t[-2] = TC_IAC;
  debug(" - sending");
  for (i = 0; trepl + i < t; i++)
    debug1(" %02x", (unsigned char)trepl[i]);
  debug("\n");
  TelReply(p, trepl, t - trepl);
}

static char tc_s[] = TC_S;
static char to_s[] = TO_S;

void
TelStatus(p, buf, l)
struct win *p;
char *buf;
int l;
{
  int i;

  *buf++ = '[';
  for (i = 0; to_s[i]; i++)
    {
      if (to_s[i] == ' ' || p->w_telmopts[i] == 0)
	continue;
      *buf++ = to_s[i];
    }
  *buf++ = ':';
  for (i = 0; to_s[i]; i++)
    {
      if (to_s[i] == ' ' || p->w_telropts[i] == 0)
	continue;
      *buf++ = to_s[i];
    }
  if (p->w_telstate == TEL_CONNECTING)
    buf[-1] = 'C';
  else if (p->w_telstate && p->w_telstate != '\r')
    {
      *buf++ = ':';
      *buf++ = tc_s[p->w_telstate - TC_SE];
    }
  *buf++ = ']';
  *buf = 0;
  return;
}

#endif /* BUILTIN_TELNET */