]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - telnet.c
Run entire source base through GNU indent to tidy up the varying
[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
179 static int in_synch;
180 static int sb_opt, sb_len;
181 static char *sb_buf = NULL;
182 static int sb_size = 0;
183 #define SB_DELTA 1024
184
185 static void c_write1(int c)
186 {
187     char cc = (char) c;
188     from_backend(0, &cc, 1);
189 }
190
191 static void log_option(char *sender, int cmd, int option)
192 {
193     char buf[50];
194     sprintf(buf, "%s:\t%s %s", sender,
195             (cmd == WILL ? "WILL" : cmd == WONT ? "WONT" :
196              cmd == DO ? "DO" : cmd == DONT ? "DONT" : "<??>"),
197             telopt(option));
198     logevent(buf);
199 }
200
201 static void send_opt(int cmd, int option)
202 {
203     unsigned char b[3];
204
205     b[0] = IAC;
206     b[1] = cmd;
207     b[2] = option;
208     sk_write(s, b, 3);
209     log_option("client", cmd, option);
210 }
211
212 static void deactivate_option(struct Opt *o)
213 {
214     if (o->state == REQUESTED || o->state == ACTIVE)
215         send_opt(o->nsend, o->option);
216     o->state = REALLY_INACTIVE;
217 }
218
219 /*
220  * Generate side effects of enabling or disabling an option.
221  */
222 static void option_side_effects(struct Opt *o, int enabled)
223 {
224     if (o->option == TELOPT_ECHO && o->send == DO)
225         echoing = !enabled;
226     else if (o->option == TELOPT_SGA && o->send == DO)
227         editing = !enabled;
228     ldisc_send(NULL, 0);               /* cause ldisc to notice the change */
229 }
230
231 static void activate_option(struct Opt *o)
232 {
233     if (o->send == WILL && o->option == TELOPT_NAWS)
234         telnet_size();
235     if (o->send == WILL &&
236         (o->option == TELOPT_NEW_ENVIRON ||
237          o->option == TELOPT_OLD_ENVIRON)) {
238         /*
239          * We may only have one kind of ENVIRON going at a time.
240          * This is a hack, but who cares.
241          */
242         deactivate_option(o->option ==
243                           TELOPT_NEW_ENVIRON ? &o_oenv : &o_nenv);
244     }
245     option_side_effects(o, 1);
246 }
247
248 static void refused_option(struct Opt *o)
249 {
250     if (o->send == WILL && o->option == TELOPT_NEW_ENVIRON &&
251         o_oenv.state == INACTIVE) {
252         send_opt(WILL, TELOPT_OLD_ENVIRON);
253         o_oenv.state = REQUESTED;
254     }
255     option_side_effects(o, 0);
256 }
257
258 static void proc_rec_opt(int cmd, int option)
259 {
260     struct Opt **o;
261
262     log_option("server", cmd, option);
263     for (o = opts; *o; o++) {
264         if ((*o)->option == option && (*o)->ack == cmd) {
265             switch ((*o)->state) {
266               case REQUESTED:
267                 (*o)->state = ACTIVE;
268                 activate_option(*o);
269                 break;
270               case ACTIVE:
271                 break;
272               case INACTIVE:
273                 (*o)->state = ACTIVE;
274                 send_opt((*o)->send, option);
275                 activate_option(*o);
276                 break;
277               case REALLY_INACTIVE:
278                 send_opt((*o)->nsend, option);
279                 break;
280             }
281             return;
282         } else if ((*o)->option == option && (*o)->nak == cmd) {
283             switch ((*o)->state) {
284               case REQUESTED:
285                 (*o)->state = INACTIVE;
286                 refused_option(*o);
287                 break;
288               case ACTIVE:
289                 (*o)->state = INACTIVE;
290                 send_opt((*o)->nsend, option);
291                 option_side_effects(*o, 0);
292                 break;
293               case INACTIVE:
294               case REALLY_INACTIVE:
295                 break;
296             }
297             return;
298         }
299     }
300     /*
301      * If we reach here, the option was one we weren't prepared to
302      * cope with. So send a negative ack.
303      */
304     send_opt((cmd == WILL ? DONT : WONT), option);
305 }
306
307 static void process_subneg(void)
308 {
309     unsigned char b[2048], *p, *q;
310     int var, value, n;
311     char *e;
312
313     switch (sb_opt) {
314       case TELOPT_TSPEED:
315         if (sb_len == 1 && sb_buf[0] == TELQUAL_SEND) {
316             char logbuf[sizeof(cfg.termspeed) + 80];
317             b[0] = IAC;
318             b[1] = SB;
319             b[2] = TELOPT_TSPEED;
320             b[3] = TELQUAL_IS;
321             strcpy(b + 4, cfg.termspeed);
322             n = 4 + strlen(cfg.termspeed);
323             b[n] = IAC;
324             b[n + 1] = SE;
325             sk_write(s, b, n + 2);
326             logevent("server:\tSB TSPEED SEND");
327             sprintf(logbuf, "client:\tSB TSPEED IS %s", cfg.termspeed);
328             logevent(logbuf);
329         } else
330             logevent("server:\tSB TSPEED <something weird>");
331         break;
332       case TELOPT_TTYPE:
333         if (sb_len == 1 && sb_buf[0] == TELQUAL_SEND) {
334             char logbuf[sizeof(cfg.termtype) + 80];
335             b[0] = IAC;
336             b[1] = SB;
337             b[2] = TELOPT_TTYPE;
338             b[3] = TELQUAL_IS;
339             for (n = 0; cfg.termtype[n]; n++)
340                 b[n + 4] = (cfg.termtype[n] >= 'a'
341                             && cfg.termtype[n] <=
342                             'z' ? cfg.termtype[n] + 'A' -
343                             'a' : cfg.termtype[n]);
344             b[n + 4] = IAC;
345             b[n + 5] = SE;
346             sk_write(s, b, n + 6);
347             b[n + 4] = 0;
348             logevent("server:\tSB TTYPE SEND");
349             sprintf(logbuf, "client:\tSB TTYPE IS %s", b + 4);
350             logevent(logbuf);
351         } else
352             logevent("server:\tSB TTYPE <something weird>\r\n");
353         break;
354       case TELOPT_OLD_ENVIRON:
355       case TELOPT_NEW_ENVIRON:
356         p = sb_buf;
357         q = p + sb_len;
358         if (p < q && *p == TELQUAL_SEND) {
359             char logbuf[50];
360             p++;
361             sprintf(logbuf, "server:\tSB %s SEND", telopt(sb_opt));
362             logevent(logbuf);
363             if (sb_opt == TELOPT_OLD_ENVIRON) {
364                 if (cfg.rfc_environ) {
365                     value = RFC_VALUE;
366                     var = RFC_VAR;
367                 } else {
368                     value = BSD_VALUE;
369                     var = BSD_VAR;
370                 }
371                 /*
372                  * Try to guess the sense of VAR and VALUE.
373                  */
374                 while (p < q) {
375                     if (*p == RFC_VAR) {
376                         value = RFC_VALUE;
377                         var = RFC_VAR;
378                     } else if (*p == BSD_VAR) {
379                         value = BSD_VALUE;
380                         var = BSD_VAR;
381                     }
382                     p++;
383                 }
384             } else {
385                 /*
386                  * With NEW_ENVIRON, the sense of VAR and VALUE
387                  * isn't in doubt.
388                  */
389                 value = RFC_VALUE;
390                 var = RFC_VAR;
391             }
392             b[0] = IAC;
393             b[1] = SB;
394             b[2] = sb_opt;
395             b[3] = TELQUAL_IS;
396             n = 4;
397             e = cfg.environmt;
398             while (*e) {
399                 b[n++] = var;
400                 while (*e && *e != '\t')
401                     b[n++] = *e++;
402                 if (*e == '\t')
403                     e++;
404                 b[n++] = value;
405                 while (*e)
406                     b[n++] = *e++;
407                 e++;
408             }
409             if (*cfg.username) {
410                 b[n++] = var;
411                 b[n++] = 'U';
412                 b[n++] = 'S';
413                 b[n++] = 'E';
414                 b[n++] = 'R';
415                 b[n++] = value;
416                 e = cfg.username;
417                 while (*e)
418                     b[n++] = *e++;
419             }
420             b[n++] = IAC;
421             b[n++] = SE;
422             sk_write(s, b, n);
423             sprintf(logbuf, "client:\tSB %s IS %s", telopt(sb_opt),
424                     n == 6 ? "<nothing>" : "<stuff>");
425             logevent(logbuf);
426         }
427         break;
428     }
429 }
430
431 static enum {
432     TOPLEVEL, SEENIAC, SEENWILL, SEENWONT, SEENDO, SEENDONT,
433     SEENSB, SUBNEGOT, SUBNEG_IAC, SEENCR
434 } telnet_state = TOPLEVEL;
435
436 static void do_telnet_read(char *buf, int len)
437 {
438
439     while (len--) {
440         int c = (unsigned char) *buf++;
441
442         switch (telnet_state) {
443           case TOPLEVEL:
444           case SEENCR:
445             if (c == NUL && telnet_state == SEENCR)
446                 telnet_state = TOPLEVEL;
447             else if (c == IAC)
448                 telnet_state = SEENIAC;
449             else {
450                 if (!in_synch)
451                     c_write1(c);
452
453 #if 1
454                 /* I can't get the F***ing winsock to insert the urgent IAC
455                  * into the right position! Even with SO_OOBINLINE it gives
456                  * it to recv too soon. And of course the DM byte (that
457                  * arrives in the same packet!) appears several K later!!
458                  *
459                  * Oh well, we do get the DM in the right place so I'll
460                  * just stop hiding on the next 0xf2 and hope for the best.
461                  */
462                 else if (c == DM)
463                     in_synch = 0;
464 #endif
465                 if (c == CR)
466                     telnet_state = SEENCR;
467                 else
468                     telnet_state = TOPLEVEL;
469             }
470             break;
471           case SEENIAC:
472             if (c == DO)
473                 telnet_state = SEENDO;
474             else if (c == DONT)
475                 telnet_state = SEENDONT;
476             else if (c == WILL)
477                 telnet_state = SEENWILL;
478             else if (c == WONT)
479                 telnet_state = SEENWONT;
480             else if (c == SB)
481                 telnet_state = SEENSB;
482             else if (c == DM) {
483                 in_synch = 0;
484                 telnet_state = TOPLEVEL;
485             } else {
486                 /* ignore everything else; print it if it's IAC */
487                 if (c == IAC) {
488                     c_write1(c);
489                 }
490                 telnet_state = TOPLEVEL;
491             }
492             break;
493           case SEENWILL:
494             proc_rec_opt(WILL, c);
495             telnet_state = TOPLEVEL;
496             break;
497           case SEENWONT:
498             proc_rec_opt(WONT, c);
499             telnet_state = TOPLEVEL;
500             break;
501           case SEENDO:
502             proc_rec_opt(DO, c);
503             telnet_state = TOPLEVEL;
504             break;
505           case SEENDONT:
506             proc_rec_opt(DONT, c);
507             telnet_state = TOPLEVEL;
508             break;
509           case SEENSB:
510             sb_opt = c;
511             sb_len = 0;
512             telnet_state = SUBNEGOT;
513             break;
514           case SUBNEGOT:
515             if (c == IAC)
516                 telnet_state = SUBNEG_IAC;
517             else {
518               subneg_addchar:
519                 if (sb_len >= sb_size) {
520                     char *newbuf;
521                     sb_size += SB_DELTA;
522                     newbuf = (sb_buf ?
523                               srealloc(sb_buf, sb_size) :
524                               smalloc(sb_size));
525                     if (newbuf)
526                         sb_buf = newbuf;
527                     else
528                         sb_size -= SB_DELTA;
529                 }
530                 if (sb_len < sb_size)
531                     sb_buf[sb_len++] = c;
532                 telnet_state = SUBNEGOT;        /* in case we came here by goto */
533             }
534             break;
535           case SUBNEG_IAC:
536             if (c != SE)
537                 goto subneg_addchar;   /* yes, it's a hack, I know, but... */
538             else {
539                 process_subneg();
540                 telnet_state = TOPLEVEL;
541             }
542             break;
543         }
544     }
545 }
546
547 static int telnet_closing(Plug plug, char *error_msg, int error_code,
548                           int calling_back)
549 {
550     sk_close(s);
551     s = NULL;
552     if (error_msg) {
553         /* A socket error has occurred. */
554         connection_fatal(error_msg);
555     }                                  /* Otherwise, the remote side closed the connection normally. */
556     return 0;
557 }
558
559 static int telnet_receive(Plug plug, int urgent, char *data, int len)
560 {
561     if (urgent)
562         in_synch = TRUE;
563     do_telnet_read(data, len);
564     return 1;
565 }
566
567 /*
568  * Called to set up the Telnet connection.
569  *
570  * Returns an error message, or NULL on success.
571  *
572  * Also places the canonical host name into `realhost'.
573  */
574 static char *telnet_init(char *host, int port, char **realhost)
575 {
576     static struct plug_function_table fn_table = {
577         telnet_closing,
578         telnet_receive
579     }, *fn_table_ptr = &fn_table;
580
581     SockAddr addr;
582     char *err;
583
584     /*
585      * Try to find host.
586      */
587     addr = sk_namelookup(host, realhost);
588     if ((err = sk_addr_error(addr)))
589         return err;
590
591     if (port < 0)
592         port = 23;                     /* default telnet port */
593
594     /*
595      * Open socket.
596      */
597     s = sk_new(addr, port, 0, 1, &fn_table_ptr);
598     if ((err = sk_socket_error(s)))
599         return err;
600
601     sk_addr_free(addr);
602
603     /*
604      * Initialise option states.
605      */
606     {
607         struct Opt **o;
608
609         for (o = opts; *o; o++)
610             if ((*o)->state == REQUESTED)
611                 send_opt((*o)->send, (*o)->option);
612     }
613
614     /*
615      * Set up SYNCH state.
616      */
617     in_synch = FALSE;
618
619     return NULL;
620 }
621
622 /*
623  * Called to send data down the Telnet connection.
624  */
625 static void telnet_send(char *buf, int len)
626 {
627     char *p;
628     static unsigned char iac[2] = { IAC, IAC };
629     static unsigned char cr[2] = { CR, NUL };
630     static unsigned char nl[2] = { CR, LF };
631
632     if (s == NULL)
633         return;
634
635     p = buf;
636     while (p < buf + len) {
637         char *q = p;
638
639         while (iswritable((unsigned char) *p) && p < buf + len)
640             p++;
641         sk_write(s, q, p - q);
642
643         while (p < buf + len && !iswritable((unsigned char) *p)) {
644             sk_write(s, (unsigned char) *p == IAC ? iac : nl, 2);
645             p++;
646         }
647     }
648 }
649
650 /*
651  * Called to set the size of the window from Telnet's POV.
652  */
653 static void telnet_size(void)
654 {
655     unsigned char b[16];
656     char logbuf[50];
657
658     if (s == NULL || o_naws.state != ACTIVE)
659         return;
660     b[0] = IAC;
661     b[1] = SB;
662     b[2] = TELOPT_NAWS;
663     b[3] = cols >> 8;
664     b[4] = cols & 0xFF;
665     b[5] = rows >> 8;
666     b[6] = rows & 0xFF;
667     b[7] = IAC;
668     b[8] = SE;
669     sk_write(s, b, 9);
670     sprintf(logbuf, "client:\tSB NAWS %d,%d",
671             ((unsigned char) b[3] << 8) + (unsigned char) b[4],
672             ((unsigned char) b[5] << 8) + (unsigned char) b[6]);
673     logevent(logbuf);
674 }
675
676 /*
677  * Send Telnet special codes.
678  */
679 static void telnet_special(Telnet_Special code)
680 {
681     unsigned char b[2];
682
683     if (s == NULL)
684         return;
685
686     b[0] = IAC;
687     switch (code) {
688       case TS_AYT:
689         b[1] = AYT;
690         sk_write(s, b, 2);
691         break;
692       case TS_BRK:
693         b[1] = BREAK;
694         sk_write(s, b, 2);
695         break;
696       case TS_EC:
697         b[1] = EC;
698         sk_write(s, b, 2);
699         break;
700       case TS_EL:
701         b[1] = EL;
702         sk_write(s, b, 2);
703         break;
704       case TS_GA:
705         b[1] = GA;
706         sk_write(s, b, 2);
707         break;
708       case TS_NOP:
709         b[1] = NOP;
710         sk_write(s, b, 2);
711         break;
712       case TS_ABORT:
713         b[1] = ABORT;
714         sk_write(s, b, 2);
715         break;
716       case TS_AO:
717         b[1] = AO;
718         sk_write(s, b, 2);
719         break;
720       case TS_IP:
721         b[1] = IP;
722         sk_write(s, b, 2);
723         break;
724       case TS_SUSP:
725         b[1] = SUSP;
726         sk_write(s, b, 2);
727         break;
728       case TS_EOR:
729         b[1] = EOR;
730         sk_write(s, b, 2);
731         break;
732       case TS_EOF:
733         b[1] = xEOF;
734         sk_write(s, b, 2);
735         break;
736       case TS_SYNCH:
737         b[1] = DM;
738         sk_write(s, b, 1);
739         sk_write_oob(s, b + 1, 1);
740         break;
741       case TS_RECHO:
742         if (o_echo.state == INACTIVE || o_echo.state == REALLY_INACTIVE) {
743             o_echo.state = REQUESTED;
744             send_opt(o_echo.send, o_echo.option);
745         }
746         break;
747       case TS_LECHO:
748         if (o_echo.state == ACTIVE) {
749             o_echo.state = REQUESTED;
750             send_opt(o_echo.nsend, o_echo.option);
751         }
752         break;
753       case TS_PING:
754         if (o_they_sga.state == ACTIVE) {
755             b[1] = NOP;
756             sk_write(s, b, 2);
757         }
758         break;
759     }
760 }
761
762 static Socket telnet_socket(void)
763 {
764     return s;
765 }
766
767 static int telnet_sendok(void)
768 {
769     return 1;
770 }
771
772 static int telnet_ldisc(int option)
773 {
774     if (option == LD_ECHO)
775         return echoing;
776     if (option == LD_EDIT)
777         return editing;
778     return FALSE;
779 }
780
781 Backend telnet_backend = {
782     telnet_init,
783     telnet_send,
784     telnet_size,
785     telnet_special,
786     telnet_socket,
787     telnet_sendok,
788     telnet_ldisc,
789     23
790 };