]> git.saurik.com Git - apple/network_cmds.git/blob - telnetd.tproj/state.c
network_cmds-77.tar.gz
[apple/network_cmds.git] / telnetd.tproj / state.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License."
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /*
25 * Copyright (c) 1989, 1993
26 * The Regents of the University of California. All rights reserved.
27 *
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * SUCH DAMAGE.
55 */
56
57 #ifndef lint
58 static char sccsid[] = "@(#)state.c 8.5 (Berkeley) 5/30/95";
59 #endif /* not lint */
60
61 #include "telnetd.h"
62 #if defined(AUTHENTICATION)
63 #include <libtelnet/auth.h>
64 #endif
65
66 unsigned char doopt[] = { IAC, DO, '%', 'c', 0 };
67 unsigned char dont[] = { IAC, DONT, '%', 'c', 0 };
68 unsigned char will[] = { IAC, WILL, '%', 'c', 0 };
69 unsigned char wont[] = { IAC, WONT, '%', 'c', 0 };
70 int not42 = 1;
71
72 /*
73 * Buffer for sub-options, and macros
74 * for suboptions buffer manipulations
75 */
76 unsigned char subbuffer[512], *subpointer= subbuffer, *subend= subbuffer;
77
78 #define SB_CLEAR() subpointer = subbuffer
79 #define SB_TERM() { subend = subpointer; SB_CLEAR(); }
80 #define SB_ACCUM(c) if (subpointer < (subbuffer+sizeof subbuffer)) { \
81 *subpointer++ = (c); \
82 }
83 #define SB_GET() ((*subpointer++)&0xff)
84 #define SB_EOF() (subpointer >= subend)
85 #define SB_LEN() (subend - subpointer)
86
87 #ifdef ENV_HACK
88 unsigned char *subsave;
89 #define SB_SAVE() subsave = subpointer;
90 #define SB_RESTORE() subpointer = subsave;
91 #endif
92
93
94 /*
95 * State for recv fsm
96 */
97 #define TS_DATA 0 /* base state */
98 #define TS_IAC 1 /* look for double IAC's */
99 #define TS_CR 2 /* CR-LF ->'s CR */
100 #define TS_SB 3 /* throw away begin's... */
101 #define TS_SE 4 /* ...end's (suboption negotiation) */
102 #define TS_WILL 5 /* will option negotiation */
103 #define TS_WONT 6 /* wont " */
104 #define TS_DO 7 /* do " */
105 #define TS_DONT 8 /* dont " */
106
107 void
108 telrcv()
109 {
110 register int c;
111 static int state = TS_DATA;
112 #if defined(CRAY2) && defined(UNICOS5)
113 char *opfrontp = pfrontp;
114 #endif
115
116 while (ncc > 0) {
117 if ((&ptyobuf[BUFSIZ] - pfrontp) < 2)
118 break;
119 c = *netip++ & 0377, ncc--;
120 #ifdef ENCRYPTION
121 if (decrypt_input)
122 c = (*decrypt_input)(c);
123 #endif /* ENCRYPTION */
124 switch (state) {
125
126 case TS_CR:
127 state = TS_DATA;
128 /* Strip off \n or \0 after a \r */
129 if ((c == 0) || (c == '\n')) {
130 break;
131 }
132 /* FALL THROUGH */
133
134 case TS_DATA:
135 if (c == IAC) {
136 state = TS_IAC;
137 break;
138 }
139 /*
140 * We now map \r\n ==> \r for pragmatic reasons.
141 * Many client implementations send \r\n when
142 * the user hits the CarriageReturn key.
143 *
144 * We USED to map \r\n ==> \n, since \r\n says
145 * that we want to be in column 1 of the next
146 * printable line, and \n is the standard
147 * unix way of saying that (\r is only good
148 * if CRMOD is set, which it normally is).
149 */
150 if ((c == '\r') && his_state_is_wont(TELOPT_BINARY)) {
151 int nc = *netip;
152 #ifdef ENCRYPTION
153 if (decrypt_input)
154 nc = (*decrypt_input)(nc & 0xff);
155 #endif /* ENCRYPTION */
156 #ifdef LINEMODE
157 /*
158 * If we are operating in linemode,
159 * convert to local end-of-line.
160 */
161 if (linemode && (ncc > 0) && (('\n' == nc) ||
162 ((0 == nc) && tty_iscrnl())) ) {
163 netip++; ncc--;
164 c = '\n';
165 } else
166 #endif
167 {
168 #ifdef ENCRYPTION
169 if (decrypt_input)
170 (void)(*decrypt_input)(-1);
171 #endif /* ENCRYPTION */
172 state = TS_CR;
173 }
174 }
175 *pfrontp++ = c;
176 break;
177
178 case TS_IAC:
179 gotiac: switch (c) {
180
181 /*
182 * Send the process on the pty side an
183 * interrupt. Do this with a NULL or
184 * interrupt char; depending on the tty mode.
185 */
186 case IP:
187 DIAG(TD_OPTIONS,
188 printoption("td: recv IAC", c));
189 interrupt();
190 break;
191
192 case BREAK:
193 DIAG(TD_OPTIONS,
194 printoption("td: recv IAC", c));
195 sendbrk();
196 break;
197
198 /*
199 * Are You There?
200 */
201 case AYT:
202 DIAG(TD_OPTIONS,
203 printoption("td: recv IAC", c));
204 recv_ayt();
205 break;
206
207 /*
208 * Abort Output
209 */
210 case AO:
211 {
212 DIAG(TD_OPTIONS,
213 printoption("td: recv IAC", c));
214 ptyflush(); /* half-hearted */
215 init_termbuf();
216
217 if (slctab[SLC_AO].sptr &&
218 *slctab[SLC_AO].sptr != (cc_t)(_POSIX_VDISABLE)) {
219 *pfrontp++ =
220 (unsigned char)*slctab[SLC_AO].sptr;
221 }
222
223 netclear(); /* clear buffer back */
224 *nfrontp++ = IAC;
225 *nfrontp++ = DM;
226 neturg = nfrontp-1; /* off by one XXX */
227 DIAG(TD_OPTIONS,
228 printoption("td: send IAC", DM));
229 break;
230 }
231
232 /*
233 * Erase Character and
234 * Erase Line
235 */
236 case EC:
237 case EL:
238 {
239 cc_t ch;
240
241 DIAG(TD_OPTIONS,
242 printoption("td: recv IAC", c));
243 ptyflush(); /* half-hearted */
244 init_termbuf();
245 if (c == EC)
246 ch = *slctab[SLC_EC].sptr;
247 else
248 ch = *slctab[SLC_EL].sptr;
249 if (ch != (cc_t)(_POSIX_VDISABLE))
250 *pfrontp++ = (unsigned char)ch;
251 break;
252 }
253
254 /*
255 * Check for urgent data...
256 */
257 case DM:
258 DIAG(TD_OPTIONS,
259 printoption("td: recv IAC", c));
260 SYNCHing = stilloob(net);
261 settimer(gotDM);
262 break;
263
264
265 /*
266 * Begin option subnegotiation...
267 */
268 case SB:
269 state = TS_SB;
270 SB_CLEAR();
271 continue;
272
273 case WILL:
274 state = TS_WILL;
275 continue;
276
277 case WONT:
278 state = TS_WONT;
279 continue;
280
281 case DO:
282 state = TS_DO;
283 continue;
284
285 case DONT:
286 state = TS_DONT;
287 continue;
288 case EOR:
289 if (his_state_is_will(TELOPT_EOR))
290 doeof();
291 break;
292
293 /*
294 * Handle RFC 10xx Telnet linemode option additions
295 * to command stream (EOF, SUSP, ABORT).
296 */
297 case xEOF:
298 doeof();
299 break;
300
301 case SUSP:
302 sendsusp();
303 break;
304
305 case ABORT:
306 sendbrk();
307 break;
308
309 case IAC:
310 *pfrontp++ = c;
311 break;
312 }
313 state = TS_DATA;
314 break;
315
316 case TS_SB:
317 if (c == IAC) {
318 state = TS_SE;
319 } else {
320 SB_ACCUM(c);
321 }
322 break;
323
324 case TS_SE:
325 if (c != SE) {
326 if (c != IAC) {
327 /*
328 * bad form of suboption negotiation.
329 * handle it in such a way as to avoid
330 * damage to local state. Parse
331 * suboption buffer found so far,
332 * then treat remaining stream as
333 * another command sequence.
334 */
335
336 /* for DIAGNOSTICS */
337 SB_ACCUM(IAC);
338 SB_ACCUM(c);
339 subpointer -= 2;
340
341 SB_TERM();
342 suboption();
343 state = TS_IAC;
344 goto gotiac;
345 }
346 SB_ACCUM(c);
347 state = TS_SB;
348 } else {
349 /* for DIAGNOSTICS */
350 SB_ACCUM(IAC);
351 SB_ACCUM(SE);
352 subpointer -= 2;
353
354 SB_TERM();
355 suboption(); /* handle sub-option */
356 state = TS_DATA;
357 }
358 break;
359
360 case TS_WILL:
361 willoption(c);
362 state = TS_DATA;
363 continue;
364
365 case TS_WONT:
366 wontoption(c);
367 state = TS_DATA;
368 continue;
369
370 case TS_DO:
371 dooption(c);
372 state = TS_DATA;
373 continue;
374
375 case TS_DONT:
376 dontoption(c);
377 state = TS_DATA;
378 continue;
379
380 default:
381 syslog(LOG_ERR, "telnetd: panic state=%d\n", state);
382 printf("telnetd: panic state=%d\n", state);
383 exit(1);
384 }
385 }
386 #if defined(CRAY2) && defined(UNICOS5)
387 if (!linemode) {
388 char xptyobuf[BUFSIZ+NETSLOP];
389 char xbuf2[BUFSIZ];
390 register char *cp;
391 int n = pfrontp - opfrontp, oc;
392 memmove(xptyobuf, opfrontp, n);
393 pfrontp = opfrontp;
394 pfrontp += term_input(xptyobuf, pfrontp, n, BUFSIZ+NETSLOP,
395 xbuf2, &oc, BUFSIZ);
396 for (cp = xbuf2; oc > 0; --oc)
397 if ((*nfrontp++ = *cp++) == IAC)
398 *nfrontp++ = IAC;
399 }
400 #endif /* defined(CRAY2) && defined(UNICOS5) */
401 } /* end of telrcv */
402
403 /*
404 * The will/wont/do/dont state machines are based on Dave Borman's
405 * Telnet option processing state machine.
406 *
407 * These correspond to the following states:
408 * my_state = the last negotiated state
409 * want_state = what I want the state to go to
410 * want_resp = how many requests I have sent
411 * All state defaults are negative, and resp defaults to 0.
412 *
413 * When initiating a request to change state to new_state:
414 *
415 * if ((want_resp == 0 && new_state == my_state) || want_state == new_state) {
416 * do nothing;
417 * } else {
418 * want_state = new_state;
419 * send new_state;
420 * want_resp++;
421 * }
422 *
423 * When receiving new_state:
424 *
425 * if (want_resp) {
426 * want_resp--;
427 * if (want_resp && (new_state == my_state))
428 * want_resp--;
429 * }
430 * if ((want_resp == 0) && (new_state != want_state)) {
431 * if (ok_to_switch_to new_state)
432 * want_state = new_state;
433 * else
434 * want_resp++;
435 * send want_state;
436 * }
437 * my_state = new_state;
438 *
439 * Note that new_state is implied in these functions by the function itself.
440 * will and do imply positive new_state, wont and dont imply negative.
441 *
442 * Finally, there is one catch. If we send a negative response to a
443 * positive request, my_state will be the positive while want_state will
444 * remain negative. my_state will revert to negative when the negative
445 * acknowlegment arrives from the peer. Thus, my_state generally tells
446 * us not only the last negotiated state, but also tells us what the peer
447 * wants to be doing as well. It is important to understand this difference
448 * as we may wish to be processing data streams based on our desired state
449 * (want_state) or based on what the peer thinks the state is (my_state).
450 *
451 * This all works fine because if the peer sends a positive request, the data
452 * that we receive prior to negative acknowlegment will probably be affected
453 * by the positive state, and we can process it as such (if we can; if we
454 * can't then it really doesn't matter). If it is that important, then the
455 * peer probably should be buffering until this option state negotiation
456 * is complete.
457 *
458 */
459 void
460 send_do(option, init)
461 int option, init;
462 {
463 if (init) {
464 if ((do_dont_resp[option] == 0 && his_state_is_will(option)) ||
465 his_want_state_is_will(option))
466 return;
467 /*
468 * Special case for TELOPT_TM: We send a DO, but pretend
469 * that we sent a DONT, so that we can send more DOs if
470 * we want to.
471 */
472 if (option == TELOPT_TM)
473 set_his_want_state_wont(option);
474 else
475 set_his_want_state_will(option);
476 do_dont_resp[option]++;
477 }
478 (void) sprintf(nfrontp, (char *)doopt, option);
479 nfrontp += sizeof (dont) - 2;
480
481 DIAG(TD_OPTIONS, printoption("td: send do", option));
482 }
483
484 #ifdef AUTHENTICATION
485 extern void auth_request();
486 #endif
487 #ifdef LINEMODE
488 extern void doclientstat();
489 #endif
490 #ifdef ENCRYPTION
491 extern void encrypt_send_support();
492 #endif /* ENCRYPTION */
493
494 void
495 willoption(option)
496 int option;
497 {
498 int changeok = 0;
499 void (*func)() = 0;
500
501 /*
502 * process input from peer.
503 */
504
505 DIAG(TD_OPTIONS, printoption("td: recv will", option));
506
507 if (do_dont_resp[option]) {
508 do_dont_resp[option]--;
509 if (do_dont_resp[option] && his_state_is_will(option))
510 do_dont_resp[option]--;
511 }
512 if (do_dont_resp[option] == 0) {
513 if (his_want_state_is_wont(option)) {
514 switch (option) {
515
516 case TELOPT_BINARY:
517 init_termbuf();
518 tty_binaryin(1);
519 set_termbuf();
520 changeok++;
521 break;
522
523 case TELOPT_ECHO:
524 /*
525 * See comments below for more info.
526 */
527 not42 = 0; /* looks like a 4.2 system */
528 break;
529
530 case TELOPT_TM:
531 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
532 /*
533 * This telnetd implementation does not really
534 * support timing marks, it just uses them to
535 * support the kludge linemode stuff. If we
536 * receive a will or wont TM in response to our
537 * do TM request that may have been sent to
538 * determine kludge linemode support, process
539 * it, otherwise TM should get a negative
540 * response back.
541 */
542 /*
543 * Handle the linemode kludge stuff.
544 * If we are not currently supporting any
545 * linemode at all, then we assume that this
546 * is the client telling us to use kludge
547 * linemode in response to our query. Set the
548 * linemode type that is to be supported, note
549 * that the client wishes to use linemode, and
550 * eat the will TM as though it never arrived.
551 */
552 if (lmodetype < KLUDGE_LINEMODE) {
553 lmodetype = KLUDGE_LINEMODE;
554 clientstat(TELOPT_LINEMODE, WILL, 0);
555 send_wont(TELOPT_SGA, 1);
556 } else if (lmodetype == NO_AUTOKLUDGE) {
557 lmodetype = KLUDGE_OK;
558 }
559 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
560 /*
561 * We never respond to a WILL TM, and
562 * we leave the state WONT.
563 */
564 return;
565
566 case TELOPT_LFLOW:
567 /*
568 * If we are going to support flow control
569 * option, then don't worry peer that we can't
570 * change the flow control characters.
571 */
572 slctab[SLC_XON].defset.flag &= ~SLC_LEVELBITS;
573 slctab[SLC_XON].defset.flag |= SLC_DEFAULT;
574 slctab[SLC_XOFF].defset.flag &= ~SLC_LEVELBITS;
575 slctab[SLC_XOFF].defset.flag |= SLC_DEFAULT;
576 case TELOPT_TTYPE:
577 case TELOPT_SGA:
578 case TELOPT_NAWS:
579 case TELOPT_TSPEED:
580 case TELOPT_XDISPLOC:
581 case TELOPT_NEW_ENVIRON:
582 case TELOPT_OLD_ENVIRON:
583 changeok++;
584 break;
585
586 #ifdef LINEMODE
587 case TELOPT_LINEMODE:
588 # ifdef KLUDGELINEMODE
589 /*
590 * Note client's desire to use linemode.
591 */
592 lmodetype = REAL_LINEMODE;
593 # endif /* KLUDGELINEMODE */
594 func = doclientstat;
595 changeok++;
596 break;
597 #endif /* LINEMODE */
598
599 #ifdef AUTHENTICATION
600 case TELOPT_AUTHENTICATION:
601 func = auth_request;
602 changeok++;
603 break;
604 #endif
605
606 #ifdef ENCRYPTION
607 case TELOPT_ENCRYPT:
608 func = encrypt_send_support;
609 changeok++;
610 break;
611 #endif /* ENCRYPTION */
612
613 default:
614 break;
615 }
616 if (changeok) {
617 set_his_want_state_will(option);
618 send_do(option, 0);
619 } else {
620 do_dont_resp[option]++;
621 send_dont(option, 0);
622 }
623 } else {
624 /*
625 * Option processing that should happen when
626 * we receive conformation of a change in
627 * state that we had requested.
628 */
629 switch (option) {
630 case TELOPT_ECHO:
631 not42 = 0; /* looks like a 4.2 system */
632 /*
633 * Egads, he responded "WILL ECHO". Turn
634 * it off right now!
635 */
636 send_dont(option, 1);
637 /*
638 * "WILL ECHO". Kludge upon kludge!
639 * A 4.2 client is now echoing user input at
640 * the tty. This is probably undesireable and
641 * it should be stopped. The client will
642 * respond WONT TM to the DO TM that we send to
643 * check for kludge linemode. When the WONT TM
644 * arrives, linemode will be turned off and a
645 * change propogated to the pty. This change
646 * will cause us to process the new pty state
647 * in localstat(), which will notice that
648 * linemode is off and send a WILL ECHO
649 * so that we are properly in character mode and
650 * all is well.
651 */
652 break;
653 #ifdef LINEMODE
654 case TELOPT_LINEMODE:
655 # ifdef KLUDGELINEMODE
656 /*
657 * Note client's desire to use linemode.
658 */
659 lmodetype = REAL_LINEMODE;
660 # endif /* KLUDGELINEMODE */
661 func = doclientstat;
662 break;
663 #endif /* LINEMODE */
664
665 #ifdef AUTHENTICATION
666 case TELOPT_AUTHENTICATION:
667 func = auth_request;
668 break;
669 #endif
670
671 #ifdef ENCRYPTION
672 case TELOPT_ENCRYPT:
673 func = encrypt_send_support;
674 break;
675 #endif /* ENCRYPTION */
676 case TELOPT_LFLOW:
677 func = flowstat;
678 break;
679 }
680 }
681 }
682 set_his_state_will(option);
683 if (func)
684 (*func)();
685 } /* end of willoption */
686
687 void
688 send_dont(option, init)
689 int option, init;
690 {
691 if (init) {
692 if ((do_dont_resp[option] == 0 && his_state_is_wont(option)) ||
693 his_want_state_is_wont(option))
694 return;
695 set_his_want_state_wont(option);
696 do_dont_resp[option]++;
697 }
698 (void) sprintf(nfrontp, (char *)dont, option);
699 nfrontp += sizeof (doopt) - 2;
700
701 DIAG(TD_OPTIONS, printoption("td: send dont", option));
702 }
703
704 void
705 wontoption(option)
706 int option;
707 {
708 /*
709 * Process client input.
710 */
711
712 DIAG(TD_OPTIONS, printoption("td: recv wont", option));
713
714 if (do_dont_resp[option]) {
715 do_dont_resp[option]--;
716 if (do_dont_resp[option] && his_state_is_wont(option))
717 do_dont_resp[option]--;
718 }
719 if (do_dont_resp[option] == 0) {
720 if (his_want_state_is_will(option)) {
721 /* it is always ok to change to negative state */
722 switch (option) {
723 case TELOPT_ECHO:
724 not42 = 1; /* doesn't seem to be a 4.2 system */
725 break;
726
727 case TELOPT_BINARY:
728 init_termbuf();
729 tty_binaryin(0);
730 set_termbuf();
731 break;
732
733 #ifdef LINEMODE
734 case TELOPT_LINEMODE:
735 # ifdef KLUDGELINEMODE
736 /*
737 * If real linemode is supported, then client is
738 * asking to turn linemode off.
739 */
740 if (lmodetype != REAL_LINEMODE)
741 break;
742 # endif /* KLUDGELINEMODE */
743 clientstat(TELOPT_LINEMODE, WONT, 0);
744 break;
745 #endif /* LINEMODE */
746
747 case TELOPT_TM:
748 /*
749 * If we get a WONT TM, and had sent a DO TM,
750 * don't respond with a DONT TM, just leave it
751 * as is. Short circut the state machine to
752 * achive this.
753 */
754 set_his_want_state_wont(TELOPT_TM);
755 return;
756
757 case TELOPT_LFLOW:
758 /*
759 * If we are not going to support flow control
760 * option, then let peer know that we can't
761 * change the flow control characters.
762 */
763 slctab[SLC_XON].defset.flag &= ~SLC_LEVELBITS;
764 slctab[SLC_XON].defset.flag |= SLC_CANTCHANGE;
765 slctab[SLC_XOFF].defset.flag &= ~SLC_LEVELBITS;
766 slctab[SLC_XOFF].defset.flag |= SLC_CANTCHANGE;
767 break;
768
769 #if defined(AUTHENTICATION)
770 case TELOPT_AUTHENTICATION:
771 auth_finished(0, AUTH_REJECT);
772 break;
773 #endif
774
775 /*
776 * For options that we might spin waiting for
777 * sub-negotiation, if the client turns off the
778 * option rather than responding to the request,
779 * we have to treat it here as if we got a response
780 * to the sub-negotiation, (by updating the timers)
781 * so that we'll break out of the loop.
782 */
783 case TELOPT_TTYPE:
784 settimer(ttypesubopt);
785 break;
786
787 case TELOPT_TSPEED:
788 settimer(tspeedsubopt);
789 break;
790
791 case TELOPT_XDISPLOC:
792 settimer(xdisplocsubopt);
793 break;
794
795 case TELOPT_OLD_ENVIRON:
796 settimer(oenvironsubopt);
797 break;
798
799 case TELOPT_NEW_ENVIRON:
800 settimer(environsubopt);
801 break;
802
803 default:
804 break;
805 }
806 set_his_want_state_wont(option);
807 if (his_state_is_will(option))
808 send_dont(option, 0);
809 } else {
810 switch (option) {
811 case TELOPT_TM:
812 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
813 if (lmodetype < NO_AUTOKLUDGE) {
814 lmodetype = NO_LINEMODE;
815 clientstat(TELOPT_LINEMODE, WONT, 0);
816 send_will(TELOPT_SGA, 1);
817 send_will(TELOPT_ECHO, 1);
818 }
819 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
820 break;
821
822 #if defined(AUTHENTICATION)
823 case TELOPT_AUTHENTICATION:
824 auth_finished(0, AUTH_REJECT);
825 break;
826 #endif
827 default:
828 break;
829 }
830 }
831 }
832 set_his_state_wont(option);
833
834 } /* end of wontoption */
835
836 void
837 send_will(option, init)
838 int option, init;
839 {
840 if (init) {
841 if ((will_wont_resp[option] == 0 && my_state_is_will(option))||
842 my_want_state_is_will(option))
843 return;
844 set_my_want_state_will(option);
845 will_wont_resp[option]++;
846 }
847 (void) sprintf(nfrontp, (char *)will, option);
848 nfrontp += sizeof (doopt) - 2;
849
850 DIAG(TD_OPTIONS, printoption("td: send will", option));
851 }
852
853 #if !defined(LINEMODE) || !defined(KLUDGELINEMODE)
854 /*
855 * When we get a DONT SGA, we will try once to turn it
856 * back on. If the other side responds DONT SGA, we
857 * leave it at that. This is so that when we talk to
858 * clients that understand KLUDGELINEMODE but not LINEMODE,
859 * we'll keep them in char-at-a-time mode.
860 */
861 int turn_on_sga = 0;
862 #endif
863
864 void
865 dooption(option)
866 int option;
867 {
868 int changeok = 0;
869
870 /*
871 * Process client input.
872 */
873
874 DIAG(TD_OPTIONS, printoption("td: recv do", option));
875
876 if (will_wont_resp[option]) {
877 will_wont_resp[option]--;
878 if (will_wont_resp[option] && my_state_is_will(option))
879 will_wont_resp[option]--;
880 }
881 if ((will_wont_resp[option] == 0) && (my_want_state_is_wont(option))) {
882 switch (option) {
883 case TELOPT_ECHO:
884 #ifdef LINEMODE
885 # ifdef KLUDGELINEMODE
886 if (lmodetype == NO_LINEMODE)
887 # else
888 if (his_state_is_wont(TELOPT_LINEMODE))
889 # endif
890 #endif
891 {
892 init_termbuf();
893 tty_setecho(1);
894 set_termbuf();
895 }
896 changeok++;
897 break;
898
899 case TELOPT_BINARY:
900 init_termbuf();
901 tty_binaryout(1);
902 set_termbuf();
903 changeok++;
904 break;
905
906 case TELOPT_SGA:
907 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
908 /*
909 * If kludge linemode is in use, then we must
910 * process an incoming do SGA for linemode
911 * purposes.
912 */
913 if (lmodetype == KLUDGE_LINEMODE) {
914 /*
915 * Receipt of "do SGA" in kludge
916 * linemode is the peer asking us to
917 * turn off linemode. Make note of
918 * the request.
919 */
920 clientstat(TELOPT_LINEMODE, WONT, 0);
921 /*
922 * If linemode did not get turned off
923 * then don't tell peer that we did.
924 * Breaking here forces a wont SGA to
925 * be returned.
926 */
927 if (linemode)
928 break;
929 }
930 #else
931 turn_on_sga = 0;
932 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
933 changeok++;
934 break;
935
936 case TELOPT_STATUS:
937 changeok++;
938 break;
939
940 case TELOPT_TM:
941 /*
942 * Special case for TM. We send a WILL, but
943 * pretend we sent a WONT.
944 */
945 send_will(option, 0);
946 set_my_want_state_wont(option);
947 set_my_state_wont(option);
948 return;
949
950 case TELOPT_LOGOUT:
951 /*
952 * When we get a LOGOUT option, respond
953 * with a WILL LOGOUT, make sure that
954 * it gets written out to the network,
955 * and then just go away...
956 */
957 set_my_want_state_will(TELOPT_LOGOUT);
958 send_will(TELOPT_LOGOUT, 0);
959 set_my_state_will(TELOPT_LOGOUT);
960 (void)netflush();
961 cleanup(0);
962 /* NOT REACHED */
963 break;
964
965 #ifdef ENCRYPTION
966 case TELOPT_ENCRYPT:
967 changeok++;
968 break;
969 #endif /* ENCRYPTION */
970 case TELOPT_LINEMODE:
971 case TELOPT_TTYPE:
972 case TELOPT_NAWS:
973 case TELOPT_TSPEED:
974 case TELOPT_LFLOW:
975 case TELOPT_XDISPLOC:
976 #ifdef TELOPT_ENVIRON
977 case TELOPT_NEW_ENVIRON:
978 #endif
979 case TELOPT_OLD_ENVIRON:
980 default:
981 break;
982 }
983 if (changeok) {
984 set_my_want_state_will(option);
985 send_will(option, 0);
986 } else {
987 will_wont_resp[option]++;
988 send_wont(option, 0);
989 }
990 }
991 set_my_state_will(option);
992
993 } /* end of dooption */
994
995 void
996 send_wont(option, init)
997 int option, init;
998 {
999 if (init) {
1000 if ((will_wont_resp[option] == 0 && my_state_is_wont(option)) ||
1001 my_want_state_is_wont(option))
1002 return;
1003 set_my_want_state_wont(option);
1004 will_wont_resp[option]++;
1005 }
1006 (void) sprintf(nfrontp, (char *)wont, option);
1007 nfrontp += sizeof (wont) - 2;
1008
1009 DIAG(TD_OPTIONS, printoption("td: send wont", option));
1010 }
1011
1012 void
1013 dontoption(option)
1014 int option;
1015 {
1016 /*
1017 * Process client input.
1018 */
1019
1020
1021 DIAG(TD_OPTIONS, printoption("td: recv dont", option));
1022
1023 if (will_wont_resp[option]) {
1024 will_wont_resp[option]--;
1025 if (will_wont_resp[option] && my_state_is_wont(option))
1026 will_wont_resp[option]--;
1027 }
1028 if ((will_wont_resp[option] == 0) && (my_want_state_is_will(option))) {
1029 switch (option) {
1030 case TELOPT_BINARY:
1031 init_termbuf();
1032 tty_binaryout(0);
1033 set_termbuf();
1034 break;
1035
1036 case TELOPT_ECHO: /* we should stop echoing */
1037 #ifdef LINEMODE
1038 # ifdef KLUDGELINEMODE
1039 if ((lmodetype != REAL_LINEMODE) &&
1040 (lmodetype != KLUDGE_LINEMODE))
1041 # else
1042 if (his_state_is_wont(TELOPT_LINEMODE))
1043 # endif
1044 #endif
1045 {
1046 init_termbuf();
1047 tty_setecho(0);
1048 set_termbuf();
1049 }
1050 break;
1051
1052 case TELOPT_SGA:
1053 #if defined(LINEMODE) && defined(KLUDGELINEMODE)
1054 /*
1055 * If kludge linemode is in use, then we
1056 * must process an incoming do SGA for
1057 * linemode purposes.
1058 */
1059 if ((lmodetype == KLUDGE_LINEMODE) ||
1060 (lmodetype == KLUDGE_OK)) {
1061 /*
1062 * The client is asking us to turn
1063 * linemode on.
1064 */
1065 lmodetype = KLUDGE_LINEMODE;
1066 clientstat(TELOPT_LINEMODE, WILL, 0);
1067 /*
1068 * If we did not turn line mode on,
1069 * then what do we say? Will SGA?
1070 * This violates design of telnet.
1071 * Gross. Very Gross.
1072 */
1073 }
1074 break;
1075 #else
1076 set_my_want_state_wont(option);
1077 if (my_state_is_will(option))
1078 send_wont(option, 0);
1079 set_my_state_wont(option);
1080 if (turn_on_sga ^= 1)
1081 send_will(option, 1);
1082 return;
1083 #endif /* defined(LINEMODE) && defined(KLUDGELINEMODE) */
1084
1085 default:
1086 break;
1087 }
1088
1089 set_my_want_state_wont(option);
1090 if (my_state_is_will(option))
1091 send_wont(option, 0);
1092 }
1093 set_my_state_wont(option);
1094
1095 } /* end of dontoption */
1096
1097 #ifdef ENV_HACK
1098 int env_ovar = -1;
1099 int env_ovalue = -1;
1100 #else /* ENV_HACK */
1101 # define env_ovar OLD_ENV_VAR
1102 # define env_ovalue OLD_ENV_VALUE
1103 #endif /* ENV_HACK */
1104
1105 /*
1106 * suboption()
1107 *
1108 * Look at the sub-option buffer, and try to be helpful to the other
1109 * side.
1110 *
1111 * Currently we recognize:
1112 *
1113 * Terminal type is
1114 * Linemode
1115 * Window size
1116 * Terminal speed
1117 */
1118 void
1119 suboption()
1120 {
1121 register int subchar;
1122
1123 DIAG(TD_OPTIONS, {netflush(); printsub('<', subpointer, SB_LEN()+2);});
1124
1125 subchar = SB_GET();
1126 switch (subchar) {
1127 case TELOPT_TSPEED: {
1128 register int xspeed, rspeed;
1129
1130 if (his_state_is_wont(TELOPT_TSPEED)) /* Ignore if option disabled */
1131 break;
1132
1133 settimer(tspeedsubopt);
1134
1135 if (SB_EOF() || SB_GET() != TELQUAL_IS)
1136 return;
1137
1138 xspeed = atoi((char *)subpointer);
1139
1140 while (SB_GET() != ',' && !SB_EOF());
1141 if (SB_EOF())
1142 return;
1143
1144 rspeed = atoi((char *)subpointer);
1145 clientstat(TELOPT_TSPEED, xspeed, rspeed);
1146
1147 break;
1148
1149 } /* end of case TELOPT_TSPEED */
1150
1151 case TELOPT_TTYPE: { /* Yaaaay! */
1152 static char terminalname[41];
1153
1154 if (his_state_is_wont(TELOPT_TTYPE)) /* Ignore if option disabled */
1155 break;
1156 settimer(ttypesubopt);
1157
1158 if (SB_EOF() || SB_GET() != TELQUAL_IS) {
1159 return; /* ??? XXX but, this is the most robust */
1160 }
1161
1162 terminaltype = terminalname;
1163
1164 while ((terminaltype < (terminalname + sizeof terminalname-1)) &&
1165 !SB_EOF()) {
1166 register int c;
1167
1168 c = SB_GET();
1169 if (isupper(c)) {
1170 c = tolower(c);
1171 }
1172 *terminaltype++ = c; /* accumulate name */
1173 }
1174 *terminaltype = 0;
1175 terminaltype = terminalname;
1176 break;
1177 } /* end of case TELOPT_TTYPE */
1178
1179 case TELOPT_NAWS: {
1180 register int xwinsize, ywinsize;
1181
1182 if (his_state_is_wont(TELOPT_NAWS)) /* Ignore if option disabled */
1183 break;
1184
1185 if (SB_EOF())
1186 return;
1187 xwinsize = SB_GET() << 8;
1188 if (SB_EOF())
1189 return;
1190 xwinsize |= SB_GET();
1191 if (SB_EOF())
1192 return;
1193 ywinsize = SB_GET() << 8;
1194 if (SB_EOF())
1195 return;
1196 ywinsize |= SB_GET();
1197 clientstat(TELOPT_NAWS, xwinsize, ywinsize);
1198
1199 break;
1200
1201 } /* end of case TELOPT_NAWS */
1202
1203 #ifdef LINEMODE
1204 case TELOPT_LINEMODE: {
1205 register int request;
1206
1207 if (his_state_is_wont(TELOPT_LINEMODE)) /* Ignore if option disabled */
1208 break;
1209 /*
1210 * Process linemode suboptions.
1211 */
1212 if (SB_EOF())
1213 break; /* garbage was sent */
1214 request = SB_GET(); /* get will/wont */
1215
1216 if (SB_EOF())
1217 break; /* another garbage check */
1218
1219 if (request == LM_SLC) { /* SLC is not preceeded by WILL or WONT */
1220 /*
1221 * Process suboption buffer of slc's
1222 */
1223 start_slc(1);
1224 do_opt_slc(subpointer, subend - subpointer);
1225 (void) end_slc(0);
1226 break;
1227 } else if (request == LM_MODE) {
1228 if (SB_EOF())
1229 return;
1230 useeditmode = SB_GET(); /* get mode flag */
1231 clientstat(LM_MODE, 0, 0);
1232 break;
1233 }
1234
1235 if (SB_EOF())
1236 break;
1237 switch (SB_GET()) { /* what suboption? */
1238 case LM_FORWARDMASK:
1239 /*
1240 * According to spec, only server can send request for
1241 * forwardmask, and client can only return a positive response.
1242 * So don't worry about it.
1243 */
1244
1245 default:
1246 break;
1247 }
1248 break;
1249 } /* end of case TELOPT_LINEMODE */
1250 #endif
1251 case TELOPT_STATUS: {
1252 int mode;
1253
1254 if (SB_EOF())
1255 break;
1256 mode = SB_GET();
1257 switch (mode) {
1258 case TELQUAL_SEND:
1259 if (my_state_is_will(TELOPT_STATUS))
1260 send_status();
1261 break;
1262
1263 case TELQUAL_IS:
1264 break;
1265
1266 default:
1267 break;
1268 }
1269 break;
1270 } /* end of case TELOPT_STATUS */
1271
1272 case TELOPT_XDISPLOC: {
1273 if (SB_EOF() || SB_GET() != TELQUAL_IS)
1274 return;
1275 settimer(xdisplocsubopt);
1276 subpointer[SB_LEN()] = '\0';
1277 (void)setenv("DISPLAY", (char *)subpointer, 1);
1278 break;
1279 } /* end of case TELOPT_XDISPLOC */
1280
1281 #ifdef TELOPT_NEW_ENVIRON
1282 case TELOPT_NEW_ENVIRON:
1283 #endif
1284 case TELOPT_OLD_ENVIRON: {
1285 register int c;
1286 register char *cp, *varp, *valp;
1287
1288 if (SB_EOF())
1289 return;
1290 c = SB_GET();
1291 if (c == TELQUAL_IS) {
1292 if (subchar == TELOPT_OLD_ENVIRON)
1293 settimer(oenvironsubopt);
1294 else
1295 settimer(environsubopt);
1296 } else if (c != TELQUAL_INFO) {
1297 return;
1298 }
1299
1300 #ifdef TELOPT_NEW_ENVIRON
1301 if (subchar == TELOPT_NEW_ENVIRON) {
1302 while (!SB_EOF()) {
1303 c = SB_GET();
1304 if ((c == NEW_ENV_VAR) || (c == ENV_USERVAR))
1305 break;
1306 }
1307 } else
1308 #endif
1309 {
1310 #ifdef ENV_HACK
1311 /*
1312 * We only want to do this if we haven't already decided
1313 * whether or not the other side has its VALUE and VAR
1314 * reversed.
1315 */
1316 if (env_ovar < 0) {
1317 register int last = -1; /* invalid value */
1318 int empty = 0;
1319 int got_var = 0, got_value = 0, got_uservar = 0;
1320
1321 /*
1322 * The other side might have its VALUE and VAR values
1323 * reversed. To be interoperable, we need to determine
1324 * which way it is. If the first recognized character
1325 * is a VAR or VALUE, then that will tell us what
1326 * type of client it is. If the fist recognized
1327 * character is a USERVAR, then we continue scanning
1328 * the suboption looking for two consecutive
1329 * VAR or VALUE fields. We should not get two
1330 * consecutive VALUE fields, so finding two
1331 * consecutive VALUE or VAR fields will tell us
1332 * what the client is.
1333 */
1334 SB_SAVE();
1335 while (!SB_EOF()) {
1336 c = SB_GET();
1337 switch(c) {
1338 case OLD_ENV_VAR:
1339 if (last < 0 || last == OLD_ENV_VAR
1340 || (empty && (last == OLD_ENV_VALUE)))
1341 goto env_ovar_ok;
1342 got_var++;
1343 last = OLD_ENV_VAR;
1344 break;
1345 case OLD_ENV_VALUE:
1346 if (last < 0 || last == OLD_ENV_VALUE
1347 || (empty && (last == OLD_ENV_VAR)))
1348 goto env_ovar_wrong;
1349 got_value++;
1350 last = OLD_ENV_VALUE;
1351 break;
1352 case ENV_USERVAR:
1353 /* count strings of USERVAR as one */
1354 if (last != ENV_USERVAR)
1355 got_uservar++;
1356 if (empty) {
1357 if (last == OLD_ENV_VALUE)
1358 goto env_ovar_ok;
1359 if (last == OLD_ENV_VAR)
1360 goto env_ovar_wrong;
1361 }
1362 last = ENV_USERVAR;
1363 break;
1364 case ENV_ESC:
1365 if (!SB_EOF())
1366 c = SB_GET();
1367 /* FALL THROUGH */
1368 default:
1369 empty = 0;
1370 continue;
1371 }
1372 empty = 1;
1373 }
1374 if (empty) {
1375 if (last == OLD_ENV_VALUE)
1376 goto env_ovar_ok;
1377 if (last == OLD_ENV_VAR)
1378 goto env_ovar_wrong;
1379 }
1380 /*
1381 * Ok, the first thing was a USERVAR, and there
1382 * are not two consecutive VAR or VALUE commands,
1383 * and none of the VAR or VALUE commands are empty.
1384 * If the client has sent us a well-formed option,
1385 * then the number of VALUEs received should always
1386 * be less than or equal to the number of VARs and
1387 * USERVARs received.
1388 *
1389 * If we got exactly as many VALUEs as VARs and
1390 * USERVARs, the client has the same definitions.
1391 *
1392 * If we got exactly as many VARs as VALUEs and
1393 * USERVARS, the client has reversed definitions.
1394 */
1395 if (got_uservar + got_var == got_value) {
1396 env_ovar_ok:
1397 env_ovar = OLD_ENV_VAR;
1398 env_ovalue = OLD_ENV_VALUE;
1399 } else if (got_uservar + got_value == got_var) {
1400 env_ovar_wrong:
1401 env_ovar = OLD_ENV_VALUE;
1402 env_ovalue = OLD_ENV_VAR;
1403 DIAG(TD_OPTIONS, {sprintf(nfrontp,
1404 "ENVIRON VALUE and VAR are reversed!\r\n");
1405 nfrontp += strlen(nfrontp);});
1406
1407 }
1408 }
1409 SB_RESTORE();
1410 #endif
1411
1412 while (!SB_EOF()) {
1413 c = SB_GET();
1414 if ((c == env_ovar) || (c == ENV_USERVAR))
1415 break;
1416 }
1417 }
1418
1419 if (SB_EOF())
1420 return;
1421
1422 cp = varp = (char *)subpointer;
1423 valp = 0;
1424
1425 while (!SB_EOF()) {
1426 c = SB_GET();
1427 if (subchar == TELOPT_OLD_ENVIRON) {
1428 if (c == env_ovar)
1429 c = NEW_ENV_VAR;
1430 else if (c == env_ovalue)
1431 c = NEW_ENV_VALUE;
1432 }
1433 switch (c) {
1434
1435 case NEW_ENV_VALUE:
1436 *cp = '\0';
1437 cp = valp = (char *)subpointer;
1438 break;
1439
1440 case NEW_ENV_VAR:
1441 case ENV_USERVAR:
1442 *cp = '\0';
1443 if (valp)
1444 (void)setenv(varp, valp, 1);
1445 else
1446 unsetenv(varp);
1447 cp = varp = (char *)subpointer;
1448 valp = 0;
1449 break;
1450
1451 case ENV_ESC:
1452 if (SB_EOF())
1453 break;
1454 c = SB_GET();
1455 /* FALL THROUGH */
1456 default:
1457 *cp++ = c;
1458 break;
1459 }
1460 }
1461 *cp = '\0';
1462 if (valp)
1463 (void)setenv(varp, valp, 1);
1464 else
1465 unsetenv(varp);
1466 break;
1467 } /* end of case TELOPT_NEW_ENVIRON */
1468 #if defined(AUTHENTICATION)
1469 case TELOPT_AUTHENTICATION:
1470 if (SB_EOF())
1471 break;
1472 switch(SB_GET()) {
1473 case TELQUAL_SEND:
1474 case TELQUAL_REPLY:
1475 /*
1476 * These are sent by us and cannot be sent by
1477 * the client.
1478 */
1479 break;
1480 case TELQUAL_IS:
1481 auth_is(subpointer, SB_LEN());
1482 break;
1483 case TELQUAL_NAME:
1484 auth_name(subpointer, SB_LEN());
1485 break;
1486 }
1487 break;
1488 #endif
1489 #ifdef ENCRYPTION
1490 case TELOPT_ENCRYPT:
1491 if (SB_EOF())
1492 break;
1493 switch(SB_GET()) {
1494 case ENCRYPT_SUPPORT:
1495 encrypt_support(subpointer, SB_LEN());
1496 break;
1497 case ENCRYPT_IS:
1498 encrypt_is(subpointer, SB_LEN());
1499 break;
1500 case ENCRYPT_REPLY:
1501 encrypt_reply(subpointer, SB_LEN());
1502 break;
1503 case ENCRYPT_START:
1504 encrypt_start(subpointer, SB_LEN());
1505 break;
1506 case ENCRYPT_END:
1507 encrypt_end();
1508 break;
1509 case ENCRYPT_REQSTART:
1510 encrypt_request_start(subpointer, SB_LEN());
1511 break;
1512 case ENCRYPT_REQEND:
1513 /*
1514 * We can always send an REQEND so that we cannot
1515 * get stuck encrypting. We should only get this
1516 * if we have been able to get in the correct mode
1517 * anyhow.
1518 */
1519 encrypt_request_end();
1520 break;
1521 case ENCRYPT_ENC_KEYID:
1522 encrypt_enc_keyid(subpointer, SB_LEN());
1523 break;
1524 case ENCRYPT_DEC_KEYID:
1525 encrypt_dec_keyid(subpointer, SB_LEN());
1526 break;
1527 default:
1528 break;
1529 }
1530 break;
1531 #endif /* ENCRYPTION */
1532
1533 default:
1534 break;
1535 } /* end of switch */
1536
1537 } /* end of suboption */
1538
1539 void
1540 doclientstat()
1541 {
1542 clientstat(TELOPT_LINEMODE, WILL, 0);
1543 }
1544
1545 #define ADD(c) *ncp++ = c
1546 #define ADD_DATA(c) { *ncp++ = c; if (c == SE || c == IAC) *ncp++ = c; }
1547 void
1548 send_status()
1549 {
1550 unsigned char statusbuf[256];
1551 register unsigned char *ncp;
1552 register unsigned char i;
1553
1554 ncp = statusbuf;
1555
1556 netflush(); /* get rid of anything waiting to go out */
1557
1558 ADD(IAC);
1559 ADD(SB);
1560 ADD(TELOPT_STATUS);
1561 ADD(TELQUAL_IS);
1562
1563 /*
1564 * We check the want_state rather than the current state,
1565 * because if we received a DO/WILL for an option that we
1566 * don't support, and the other side didn't send a DONT/WONT
1567 * in response to our WONT/DONT, then the "state" will be
1568 * WILL/DO, and the "want_state" will be WONT/DONT. We
1569 * need to go by the latter.
1570 */
1571 for (i = 0; i < (unsigned char)NTELOPTS; i++) {
1572 if (my_want_state_is_will(i)) {
1573 ADD(WILL);
1574 ADD_DATA(i);
1575 }
1576 if (his_want_state_is_will(i)) {
1577 ADD(DO);
1578 ADD_DATA(i);
1579 }
1580 }
1581
1582 if (his_want_state_is_will(TELOPT_LFLOW)) {
1583 ADD(SB);
1584 ADD(TELOPT_LFLOW);
1585 if (flowmode) {
1586 ADD(LFLOW_ON);
1587 } else {
1588 ADD(LFLOW_OFF);
1589 }
1590 ADD(SE);
1591
1592 if (restartany >= 0) {
1593 ADD(SB);
1594 ADD(TELOPT_LFLOW);
1595 if (restartany) {
1596 ADD(LFLOW_RESTART_ANY);
1597 } else {
1598 ADD(LFLOW_RESTART_XON);
1599 }
1600 ADD(SE);
1601 }
1602 }
1603
1604 #ifdef LINEMODE
1605 if (his_want_state_is_will(TELOPT_LINEMODE)) {
1606 unsigned char *cp, *cpe;
1607 int len;
1608
1609 ADD(SB);
1610 ADD(TELOPT_LINEMODE);
1611 ADD(LM_MODE);
1612 ADD_DATA(editmode);
1613 ADD(SE);
1614
1615 ADD(SB);
1616 ADD(TELOPT_LINEMODE);
1617 ADD(LM_SLC);
1618 start_slc(0);
1619 send_slc();
1620 len = end_slc(&cp);
1621 for (cpe = cp + len; cp < cpe; cp++)
1622 ADD_DATA(*cp);
1623 ADD(SE);
1624 }
1625 #endif /* LINEMODE */
1626
1627 ADD(IAC);
1628 ADD(SE);
1629
1630 writenet(statusbuf, ncp - statusbuf);
1631 netflush(); /* Send it on its way */
1632
1633 DIAG(TD_OPTIONS,
1634 {printsub('>', statusbuf, ncp - statusbuf); netflush();});
1635 }