]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - telnet.c
`realhost', passed back from all the backend init functions, was
[PuTTY.git] / telnet.c
1 #include <windows.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4
5 #include "putty.h"
6
7 #ifndef FALSE
8 #define FALSE 0
9 #endif
10 #ifndef TRUE
11 #define TRUE 1
12 #endif
13
14 static Socket s = NULL;
15
16 #define IAC     255                    /* interpret as command: */
17 #define DONT    254                    /* you are not to use option */
18 #define DO      253                    /* please, you use option */
19 #define WONT    252                    /* I won't use option */
20 #define WILL    251                    /* I will use option */
21 #define SB      250                    /* interpret as subnegotiation */
22 #define SE      240                    /* end sub negotiation */
23
24 #define GA      249                    /* you may reverse the line */
25 #define EL      248                    /* erase the current line */
26 #define EC      247                    /* erase the current character */
27 #define AYT     246                    /* are you there */
28 #define AO      245                    /* abort output--but let prog finish */
29 #define IP      244                    /* interrupt process--permanently */
30 #define BREAK   243                    /* break */
31 #define DM      242                    /* data mark--for connect. cleaning */
32 #define NOP     241                    /* nop */
33 #define EOR     239                    /* end of record (transparent mode) */
34 #define ABORT   238                    /* Abort process */
35 #define SUSP    237                    /* Suspend process */
36 #define xEOF    236                    /* End of file: EOF is already used... */
37
38 #define TELOPT_BINARY   0              /* 8-bit data path */
39 #define TELOPT_ECHO     1              /* echo */
40 #define TELOPT_RCP      2              /* prepare to reconnect */
41 #define TELOPT_SGA      3              /* suppress go ahead */
42 #define TELOPT_NAMS     4              /* approximate message size */
43 #define TELOPT_STATUS   5              /* give status */
44 #define TELOPT_TM       6              /* timing mark */
45 #define TELOPT_RCTE     7              /* remote controlled transmission and echo */
46 #define TELOPT_NAOL     8              /* negotiate about output line width */
47 #define TELOPT_NAOP     9              /* negotiate about output page size */
48 #define TELOPT_NAOCRD   10             /* negotiate about CR disposition */
49 #define TELOPT_NAOHTS   11             /* negotiate about horizontal tabstops */
50 #define TELOPT_NAOHTD   12             /* negotiate about horizontal tab disposition */
51 #define TELOPT_NAOFFD   13             /* negotiate about formfeed disposition */
52 #define TELOPT_NAOVTS   14             /* negotiate about vertical tab stops */
53 #define TELOPT_NAOVTD   15             /* negotiate about vertical tab disposition */
54 #define TELOPT_NAOLFD   16             /* negotiate about output LF disposition */
55 #define TELOPT_XASCII   17             /* extended ascic character set */
56 #define TELOPT_LOGOUT   18             /* force logout */
57 #define TELOPT_BM       19             /* byte macro */
58 #define TELOPT_DET      20             /* data entry terminal */
59 #define TELOPT_SUPDUP   21             /* supdup protocol */
60 #define TELOPT_SUPDUPOUTPUT 22         /* supdup output */
61 #define TELOPT_SNDLOC   23             /* send location */
62 #define TELOPT_TTYPE    24             /* terminal type */
63 #define TELOPT_EOR      25             /* end or record */
64 #define TELOPT_TUID     26             /* TACACS user identification */
65 #define TELOPT_OUTMRK   27             /* output marking */
66 #define TELOPT_TTYLOC   28             /* terminal location number */
67 #define TELOPT_3270REGIME 29           /* 3270 regime */
68 #define TELOPT_X3PAD    30             /* X.3 PAD */
69 #define TELOPT_NAWS     31             /* window size */
70 #define TELOPT_TSPEED   32             /* terminal speed */
71 #define TELOPT_LFLOW    33             /* remote flow control */
72 #define TELOPT_LINEMODE 34             /* Linemode option */
73 #define TELOPT_XDISPLOC 35             /* X Display Location */
74 #define TELOPT_OLD_ENVIRON 36          /* Old - Environment variables */
75 #define TELOPT_AUTHENTICATION 37       /* Authenticate */
76 #define TELOPT_ENCRYPT  38             /* Encryption option */
77 #define TELOPT_NEW_ENVIRON 39          /* New - Environment variables */
78 #define TELOPT_EXOPL    255            /* extended-options-list */
79
80 #define TELQUAL_IS      0              /* option is... */
81 #define TELQUAL_SEND    1              /* send option */
82 #define TELQUAL_INFO    2              /* ENVIRON: informational version of IS */
83 #define BSD_VAR 1
84 #define BSD_VALUE 0
85 #define RFC_VAR 0
86 #define RFC_VALUE 1
87
88 #define CR 13
89 #define LF 10
90 #define NUL 0
91
92 #define iswritable(x) ( (x) != IAC && (x) != CR )
93
94 static char *telopt(int opt)
95 {
96 #define i(x) if (opt == TELOPT_ ## x) return #x;
97     i(BINARY);
98     i(ECHO);
99     i(RCP);
100     i(SGA);
101     i(NAMS);
102     i(STATUS);
103     i(TM);
104     i(RCTE);
105     i(NAOL);
106     i(NAOP);
107     i(NAOCRD);
108     i(NAOHTS);
109     i(NAOHTD);
110     i(NAOFFD);
111     i(NAOVTS);
112     i(NAOVTD);
113     i(NAOLFD);
114     i(XASCII);
115     i(LOGOUT);
116     i(BM);
117     i(DET);
118     i(SUPDUP);
119     i(SUPDUPOUTPUT);
120     i(SNDLOC);
121     i(TTYPE);
122     i(EOR);
123     i(TUID);
124     i(OUTMRK);
125     i(TTYLOC);
126     i(X3PAD);
127     i(NAWS);
128     i(TSPEED);
129     i(LFLOW);
130     i(LINEMODE);
131     i(XDISPLOC);
132     i(OLD_ENVIRON);
133     i(AUTHENTICATION);
134     i(ENCRYPT);
135     i(NEW_ENVIRON);
136     i(EXOPL);
137 #undef i
138     return "<unknown>";
139 }
140
141 static void telnet_size(void);
142
143 struct Opt {
144     int send;                          /* what we initially send */
145     int nsend;                         /* -ve send if requested to stop it */
146     int ack, nak;                      /* +ve and -ve acknowledgements */
147     int option;                        /* the option code */
148     enum {
149         REQUESTED, ACTIVE, INACTIVE, REALLY_INACTIVE
150     } state;
151 };
152
153 static struct Opt o_naws =
154     { WILL, WONT, DO, DONT, TELOPT_NAWS, REQUESTED };
155 static struct Opt o_tspeed =
156     { WILL, WONT, DO, DONT, TELOPT_TSPEED, REQUESTED };
157 static struct Opt o_ttype =
158     { WILL, WONT, DO, DONT, TELOPT_TTYPE, REQUESTED };
159 static struct Opt o_oenv = { WILL, WONT, DO, DONT, TELOPT_OLD_ENVIRON,
160     INACTIVE
161 };
162 static struct Opt o_nenv = { WILL, WONT, DO, DONT, TELOPT_NEW_ENVIRON,
163     REQUESTED
164 };
165 static struct Opt o_echo =
166     { DO, DONT, WILL, WONT, TELOPT_ECHO, REQUESTED };
167 static struct Opt o_we_sga =
168     { WILL, WONT, DO, DONT, TELOPT_SGA, REQUESTED };
169 static struct Opt o_they_sga =
170     { DO, DONT, WILL, WONT, TELOPT_SGA, REQUESTED };
171
172 static struct Opt *opts[] = {
173     &o_naws, &o_tspeed, &o_ttype, &o_oenv, &o_nenv, &o_echo,
174     &o_we_sga, &o_they_sga, NULL
175 };
176
177 static int echoing = TRUE, editing = TRUE;
178 static int activated = FALSE;
179
180 static int in_synch;
181 static int sb_opt, sb_len;
182 static char *sb_buf = NULL;
183 static int sb_size = 0;
184 #define SB_DELTA 1024
185
186 static void c_write1(int c)
187 {
188     char cc = (char) c;
189     from_backend(0, &cc, 1);
190 }
191
192 static void log_option(char *sender, int cmd, int option)
193 {
194     char buf[50];
195     sprintf(buf, "%s:\t%s %s", sender,
196             (cmd == WILL ? "WILL" : cmd == WONT ? "WONT" :
197              cmd == DO ? "DO" : cmd == DONT ? "DONT" : "<??>"),
198             telopt(option));
199     logevent(buf);
200 }
201
202 static void send_opt(int cmd, int option)
203 {
204     unsigned char b[3];
205
206     b[0] = IAC;
207     b[1] = cmd;
208     b[2] = option;
209     sk_write(s, b, 3);
210     log_option("client", cmd, option);
211 }
212
213 static void deactivate_option(struct Opt *o)
214 {
215     if (o->state == REQUESTED || o->state == ACTIVE)
216         send_opt(o->nsend, o->option);
217     o->state = REALLY_INACTIVE;
218 }
219
220 /*
221  * Generate side effects of enabling or disabling an option.
222  */
223 static void option_side_effects(struct Opt *o, int enabled)
224 {
225     if (o->option == TELOPT_ECHO && o->send == DO)
226         echoing = !enabled;
227     else if (o->option == TELOPT_SGA && o->send == DO)
228         editing = !enabled;
229     ldisc_send(NULL, 0);               /* cause ldisc to notice the change */
230
231     /* Ensure we get the minimum options */
232     if (!activated) {
233         if (o_echo.state == INACTIVE) {
234             o_echo.state = REQUESTED;
235             send_opt(o_echo.send, o_echo.option);
236         }
237         if (o_we_sga.state == INACTIVE) {
238             o_we_sga.state = REQUESTED;
239             send_opt(o_we_sga.send, o_we_sga.option);
240         }
241         if (o_they_sga.state == INACTIVE) {
242             o_they_sga.state = REQUESTED;
243             send_opt(o_they_sga.send, o_they_sga.option);
244         }
245         activated = TRUE;
246     }
247 }
248
249 static void activate_option(struct Opt *o)
250 {
251     if (o->send == WILL && o->option == TELOPT_NAWS)
252         telnet_size();
253     if (o->send == WILL &&
254         (o->option == TELOPT_NEW_ENVIRON ||
255          o->option == TELOPT_OLD_ENVIRON)) {
256         /*
257          * We may only have one kind of ENVIRON going at a time.
258          * This is a hack, but who cares.
259          */
260         deactivate_option(o->option ==
261                           TELOPT_NEW_ENVIRON ? &o_oenv : &o_nenv);
262     }
263     option_side_effects(o, 1);
264 }
265
266 static void refused_option(struct Opt *o)
267 {
268     if (o->send == WILL && o->option == TELOPT_NEW_ENVIRON &&
269         o_oenv.state == INACTIVE) {
270         send_opt(WILL, TELOPT_OLD_ENVIRON);
271         o_oenv.state = REQUESTED;
272     }
273     option_side_effects(o, 0);
274 }
275
276 static void proc_rec_opt(int cmd, int option)
277 {
278     struct Opt **o;
279
280     log_option("server", cmd, option);
281     for (o = opts; *o; o++) {
282         if ((*o)->option == option && (*o)->ack == cmd) {
283             switch ((*o)->state) {
284               case REQUESTED:
285                 (*o)->state = ACTIVE;
286                 activate_option(*o);
287                 break;
288               case ACTIVE:
289                 break;
290               case INACTIVE:
291                 (*o)->state = ACTIVE;
292                 send_opt((*o)->send, option);
293                 activate_option(*o);
294                 break;
295               case REALLY_INACTIVE:
296                 send_opt((*o)->nsend, option);
297                 break;
298             }
299             return;
300         } else if ((*o)->option == option && (*o)->nak == cmd) {
301             switch ((*o)->state) {
302               case REQUESTED:
303                 (*o)->state = INACTIVE;
304                 refused_option(*o);
305                 break;
306               case ACTIVE:
307                 (*o)->state = INACTIVE;
308                 send_opt((*o)->nsend, option);
309                 option_side_effects(*o, 0);
310                 break;
311               case INACTIVE:
312               case REALLY_INACTIVE:
313                 break;
314             }
315             return;
316         }
317     }
318     /*
319      * If we reach here, the option was one we weren't prepared to
320      * cope with. So send a negative ack.
321      */
322     send_opt((cmd == WILL ? DONT : WONT), option);
323 }
324
325 static void process_subneg(void)
326 {
327     unsigned char b[2048], *p, *q;
328     int var, value, n;
329     char *e;
330
331     switch (sb_opt) {
332       case TELOPT_TSPEED:
333         if (sb_len == 1 && sb_buf[0] == TELQUAL_SEND) {
334             char logbuf[sizeof(cfg.termspeed) + 80];
335             b[0] = IAC;
336             b[1] = SB;
337             b[2] = TELOPT_TSPEED;
338             b[3] = TELQUAL_IS;
339             strcpy(b + 4, cfg.termspeed);
340             n = 4 + strlen(cfg.termspeed);
341             b[n] = IAC;
342             b[n + 1] = SE;
343             sk_write(s, b, n + 2);
344             logevent("server:\tSB TSPEED SEND");
345             sprintf(logbuf, "client:\tSB TSPEED IS %s", cfg.termspeed);
346             logevent(logbuf);
347         } else
348             logevent("server:\tSB TSPEED <something weird>");
349         break;
350       case TELOPT_TTYPE:
351         if (sb_len == 1 && sb_buf[0] == TELQUAL_SEND) {
352             char logbuf[sizeof(cfg.termtype) + 80];
353             b[0] = IAC;
354             b[1] = SB;
355             b[2] = TELOPT_TTYPE;
356             b[3] = TELQUAL_IS;
357             for (n = 0; cfg.termtype[n]; n++)
358                 b[n + 4] = (cfg.termtype[n] >= 'a'
359                             && cfg.termtype[n] <=
360                             'z' ? cfg.termtype[n] + 'A' -
361                             'a' : cfg.termtype[n]);
362             b[n + 4] = IAC;
363             b[n + 5] = SE;
364             sk_write(s, b, n + 6);
365             b[n + 4] = 0;
366             logevent("server:\tSB TTYPE SEND");
367             sprintf(logbuf, "client:\tSB TTYPE IS %s", b + 4);
368             logevent(logbuf);
369         } else
370             logevent("server:\tSB TTYPE <something weird>\r\n");
371         break;
372       case TELOPT_OLD_ENVIRON:
373       case TELOPT_NEW_ENVIRON:
374         p = sb_buf;
375         q = p + sb_len;
376         if (p < q && *p == TELQUAL_SEND) {
377             char logbuf[50];
378             p++;
379             sprintf(logbuf, "server:\tSB %s SEND", telopt(sb_opt));
380             logevent(logbuf);
381             if (sb_opt == TELOPT_OLD_ENVIRON) {
382                 if (cfg.rfc_environ) {
383                     value = RFC_VALUE;
384                     var = RFC_VAR;
385                 } else {
386                     value = BSD_VALUE;
387                     var = BSD_VAR;
388                 }
389                 /*
390                  * Try to guess the sense of VAR and VALUE.
391                  */
392                 while (p < q) {
393                     if (*p == RFC_VAR) {
394                         value = RFC_VALUE;
395                         var = RFC_VAR;
396                     } else if (*p == BSD_VAR) {
397                         value = BSD_VALUE;
398                         var = BSD_VAR;
399                     }
400                     p++;
401                 }
402             } else {
403                 /*
404                  * With NEW_ENVIRON, the sense of VAR and VALUE
405                  * isn't in doubt.
406                  */
407                 value = RFC_VALUE;
408                 var = RFC_VAR;
409             }
410             b[0] = IAC;
411             b[1] = SB;
412             b[2] = sb_opt;
413             b[3] = TELQUAL_IS;
414             n = 4;
415             e = cfg.environmt;
416             while (*e) {
417                 b[n++] = var;
418                 while (*e && *e != '\t')
419                     b[n++] = *e++;
420                 if (*e == '\t')
421                     e++;
422                 b[n++] = value;
423                 while (*e)
424                     b[n++] = *e++;
425                 e++;
426             }
427             if (*cfg.username) {
428                 b[n++] = var;
429                 b[n++] = 'U';
430                 b[n++] = 'S';
431                 b[n++] = 'E';
432                 b[n++] = 'R';
433                 b[n++] = value;
434                 e = cfg.username;
435                 while (*e)
436                     b[n++] = *e++;
437             }
438             b[n++] = IAC;
439             b[n++] = SE;
440             sk_write(s, b, n);
441             sprintf(logbuf, "client:\tSB %s IS %s", telopt(sb_opt),
442                     n == 6 ? "<nothing>" : "<stuff>");
443             logevent(logbuf);
444         }
445         break;
446     }
447 }
448
449 static enum {
450     TOPLEVEL, SEENIAC, SEENWILL, SEENWONT, SEENDO, SEENDONT,
451     SEENSB, SUBNEGOT, SUBNEG_IAC, SEENCR
452 } telnet_state = TOPLEVEL;
453
454 static void do_telnet_read(char *buf, int len)
455 {
456
457     while (len--) {
458         int c = (unsigned char) *buf++;
459
460         switch (telnet_state) {
461           case TOPLEVEL:
462           case SEENCR:
463             if (c == NUL && telnet_state == SEENCR)
464                 telnet_state = TOPLEVEL;
465             else if (c == IAC)
466                 telnet_state = SEENIAC;
467             else {
468                 if (!in_synch)
469                     c_write1(c);
470
471 #if 1
472                 /* I can't get the F***ing winsock to insert the urgent IAC
473                  * into the right position! Even with SO_OOBINLINE it gives
474                  * it to recv too soon. And of course the DM byte (that
475                  * arrives in the same packet!) appears several K later!!
476                  *
477                  * Oh well, we do get the DM in the right place so I'll
478                  * just stop hiding on the next 0xf2 and hope for the best.
479                  */
480                 else if (c == DM)
481                     in_synch = 0;
482 #endif
483                 if (c == CR)
484                     telnet_state = SEENCR;
485                 else
486                     telnet_state = TOPLEVEL;
487             }
488             break;
489           case SEENIAC:
490             if (c == DO)
491                 telnet_state = SEENDO;
492             else if (c == DONT)
493                 telnet_state = SEENDONT;
494             else if (c == WILL)
495                 telnet_state = SEENWILL;
496             else if (c == WONT)
497                 telnet_state = SEENWONT;
498             else if (c == SB)
499                 telnet_state = SEENSB;
500             else if (c == DM) {
501                 in_synch = 0;
502                 telnet_state = TOPLEVEL;
503             } else {
504                 /* ignore everything else; print it if it's IAC */
505                 if (c == IAC) {
506                     c_write1(c);
507                 }
508                 telnet_state = TOPLEVEL;
509             }
510             break;
511           case SEENWILL:
512             proc_rec_opt(WILL, c);
513             telnet_state = TOPLEVEL;
514             break;
515           case SEENWONT:
516             proc_rec_opt(WONT, c);
517             telnet_state = TOPLEVEL;
518             break;
519           case SEENDO:
520             proc_rec_opt(DO, c);
521             telnet_state = TOPLEVEL;
522             break;
523           case SEENDONT:
524             proc_rec_opt(DONT, c);
525             telnet_state = TOPLEVEL;
526             break;
527           case SEENSB:
528             sb_opt = c;
529             sb_len = 0;
530             telnet_state = SUBNEGOT;
531             break;
532           case SUBNEGOT:
533             if (c == IAC)
534                 telnet_state = SUBNEG_IAC;
535             else {
536               subneg_addchar:
537                 if (sb_len >= sb_size) {
538                     char *newbuf;
539                     sb_size += SB_DELTA;
540                     newbuf = (sb_buf ?
541                               srealloc(sb_buf, sb_size) :
542                               smalloc(sb_size));
543                     if (newbuf)
544                         sb_buf = newbuf;
545                     else
546                         sb_size -= SB_DELTA;
547                 }
548                 if (sb_len < sb_size)
549                     sb_buf[sb_len++] = c;
550                 telnet_state = SUBNEGOT;        /* in case we came here by goto */
551             }
552             break;
553           case SUBNEG_IAC:
554             if (c != SE)
555                 goto subneg_addchar;   /* yes, it's a hack, I know, but... */
556             else {
557                 process_subneg();
558                 telnet_state = TOPLEVEL;
559             }
560             break;
561         }
562     }
563 }
564
565 static int telnet_closing(Plug plug, char *error_msg, int error_code,
566                           int calling_back)
567 {
568     sk_close(s);
569     s = NULL;
570     if (error_msg) {
571         /* A socket error has occurred. */
572         connection_fatal(error_msg);
573     }                                  /* Otherwise, the remote side closed the connection normally. */
574     return 0;
575 }
576
577 static int telnet_receive(Plug plug, int urgent, char *data, int len)
578 {
579     if (urgent)
580         in_synch = TRUE;
581     do_telnet_read(data, len);
582     return 1;
583 }
584
585 /*
586  * Called to set up the Telnet connection.
587  *
588  * Returns an error message, or NULL on success.
589  *
590  * Also places the canonical host name into `realhost'. It must be
591  * freed by the caller.
592  */
593 static char *telnet_init(char *host, int port, char **realhost)
594 {
595     static struct plug_function_table fn_table = {
596         telnet_closing,
597         telnet_receive
598     }, *fn_table_ptr = &fn_table;
599
600     SockAddr addr;
601     char *err;
602
603     /*
604      * Try to find host.
605      */
606     addr = sk_namelookup(host, realhost);
607     if ((err = sk_addr_error(addr)))
608         return err;
609
610     if (port < 0)
611         port = 23;                     /* default telnet port */
612
613     /*
614      * Open socket.
615      */
616     s = sk_new(addr, port, 0, 1, &fn_table_ptr);
617     if ((err = sk_socket_error(s)))
618         return err;
619
620     sk_addr_free(addr);
621
622     /*
623      * Initialise option states.
624      */
625     if (cfg.passive_telnet) {
626         struct Opt **o;
627
628         for (o = opts; *o; o++)
629             if ((*o)->state == REQUESTED)
630                 (*o)->state = INACTIVE;
631     } else {
632         struct Opt **o;
633
634         for (o = opts; *o; o++)
635             if ((*o)->state == REQUESTED)
636                 send_opt((*o)->send, (*o)->option);
637         activated = TRUE;
638     }
639
640     /*
641      * Set up SYNCH state.
642      */
643     in_synch = FALSE;
644
645     return NULL;
646 }
647
648 /*
649  * Called to send data down the Telnet connection.
650  */
651 static void telnet_send(char *buf, int len)
652 {
653     char *p;
654     static unsigned char iac[2] = { IAC, IAC };
655     static unsigned char cr[2] = { CR, NUL };
656     static unsigned char nl[2] = { CR, LF };
657
658     if (s == NULL)
659         return;
660
661     p = buf;
662     while (p < buf + len) {
663         char *q = p;
664
665         while (iswritable((unsigned char) *p) && p < buf + len)
666             p++;
667         sk_write(s, q, p - q);
668
669         while (p < buf + len && !iswritable((unsigned char) *p)) {
670             sk_write(s, (unsigned char) *p == IAC ? iac : nl, 2);
671             p++;
672         }
673     }
674 }
675
676 /*
677  * Called to set the size of the window from Telnet's POV.
678  */
679 static void telnet_size(void)
680 {
681     unsigned char b[16];
682     char logbuf[50];
683
684     if (s == NULL || o_naws.state != ACTIVE)
685         return;
686     b[0] = IAC;
687     b[1] = SB;
688     b[2] = TELOPT_NAWS;
689     b[3] = cols >> 8;
690     b[4] = cols & 0xFF;
691     b[5] = rows >> 8;
692     b[6] = rows & 0xFF;
693     b[7] = IAC;
694     b[8] = SE;
695     sk_write(s, b, 9);
696     sprintf(logbuf, "client:\tSB NAWS %d,%d",
697             ((unsigned char) b[3] << 8) + (unsigned char) b[4],
698             ((unsigned char) b[5] << 8) + (unsigned char) b[6]);
699     logevent(logbuf);
700 }
701
702 /*
703  * Send Telnet special codes.
704  */
705 static void telnet_special(Telnet_Special code)
706 {
707     unsigned char b[2];
708
709     if (s == NULL)
710         return;
711
712     b[0] = IAC;
713     switch (code) {
714       case TS_AYT:
715         b[1] = AYT;
716         sk_write(s, b, 2);
717         break;
718       case TS_BRK:
719         b[1] = BREAK;
720         sk_write(s, b, 2);
721         break;
722       case TS_EC:
723         b[1] = EC;
724         sk_write(s, b, 2);
725         break;
726       case TS_EL:
727         b[1] = EL;
728         sk_write(s, b, 2);
729         break;
730       case TS_GA:
731         b[1] = GA;
732         sk_write(s, b, 2);
733         break;
734       case TS_NOP:
735         b[1] = NOP;
736         sk_write(s, b, 2);
737         break;
738       case TS_ABORT:
739         b[1] = ABORT;
740         sk_write(s, b, 2);
741         break;
742       case TS_AO:
743         b[1] = AO;
744         sk_write(s, b, 2);
745         break;
746       case TS_IP:
747         b[1] = IP;
748         sk_write(s, b, 2);
749         break;
750       case TS_SUSP:
751         b[1] = SUSP;
752         sk_write(s, b, 2);
753         break;
754       case TS_EOR:
755         b[1] = EOR;
756         sk_write(s, b, 2);
757         break;
758       case TS_EOF:
759         b[1] = xEOF;
760         sk_write(s, b, 2);
761         break;
762       case TS_SYNCH:
763         b[1] = DM;
764         sk_write(s, b, 1);
765         sk_write_oob(s, b + 1, 1);
766         break;
767       case TS_RECHO:
768         if (o_echo.state == INACTIVE || o_echo.state == REALLY_INACTIVE) {
769             o_echo.state = REQUESTED;
770             send_opt(o_echo.send, o_echo.option);
771         }
772         break;
773       case TS_LECHO:
774         if (o_echo.state == ACTIVE) {
775             o_echo.state = REQUESTED;
776             send_opt(o_echo.nsend, o_echo.option);
777         }
778         break;
779       case TS_PING:
780         if (o_they_sga.state == ACTIVE) {
781             b[1] = NOP;
782             sk_write(s, b, 2);
783         }
784         break;
785     }
786 }
787
788 static Socket telnet_socket(void)
789 {
790     return s;
791 }
792
793 static int telnet_sendok(void)
794 {
795     return 1;
796 }
797
798 static int telnet_ldisc(int option)
799 {
800     if (option == LD_ECHO)
801         return echoing;
802     if (option == LD_EDIT)
803         return editing;
804     return FALSE;
805 }
806
807 Backend telnet_backend = {
808     telnet_init,
809     telnet_send,
810     telnet_size,
811     telnet_special,
812     telnet_socket,
813     telnet_sendok,
814     telnet_ldisc,
815     23
816 };