dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / cmd-inet / usr.bin / telnet / telnet.c
blobbfca016f3320049e14330a2fdff3215b7b7421ba
1 /*
2 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
4 */
6 /*
7 * Copyright (c) 1988, 1990, 1993
8 * The Regents of the University of California. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
39 static char sccsid[] = "@(#)telnet.c 8.1 (Berkeley) 6/6/93";
41 #include <netdb.h>
42 #include <sys/types.h>
44 #include <curses.h>
45 #include <signal.h>
47 * By the way, we need to include curses.h before telnet.h since,
48 * among other things, telnet.h #defines 'DO', which is a variable
49 * declared in curses.h.
52 #include <arpa/telnet.h>
54 #include <ctype.h>
56 #include "ring.h"
58 #include "defines.h"
59 #include "externs.h"
60 #include "types.h"
61 #include "general.h"
63 #include "auth.h"
64 #include "encrypt.h"
66 #define strip(x) ((x)&0x7f)
68 /* Buffer for sub-options */
69 static unsigned char subbuffer[SUBBUFSIZE];
70 static unsigned char *subpointer;
71 static unsigned char *subend;
73 #define SB_CLEAR() subpointer = subbuffer;
74 #define SB_TERM() { subend = subpointer; SB_CLEAR(); }
75 #define SB_ACCUM(c) if (subpointer < (subbuffer+sizeof (subbuffer))) { \
76 *subpointer++ = (c); \
79 #define SB_GET() ((*subpointer++)&0xff)
80 #define SB_PEEK() ((*subpointer)&0xff)
81 #define SB_EOF() (subpointer >= subend)
82 #define SB_LEN() (subend - subpointer)
84 char options[SUBBUFSIZE]; /* The combined options */
85 char do_dont_resp[SUBBUFSIZE];
86 char will_wont_resp[SUBBUFSIZE];
88 int eight = 0;
89 int autologin = 0; /* Autologin anyone? */
90 int skiprc = 0;
91 int connected;
92 int showoptions;
93 static int ISend; /* trying to send network data in */
94 int debug = 0;
95 int crmod;
96 int netdata; /* Print out network data flow */
97 int crlf; /* Should '\r' be mapped to <CR><LF> (or <CR><NUL>)? */
98 int telnetport;
99 int SYNCHing; /* we are in TELNET SYNCH mode */
100 int flushout; /* flush output */
101 int autoflush = 0; /* flush output when interrupting? */
102 int autosynch; /* send interrupt characters with SYNCH? */
103 int localflow; /* we handle flow control locally */
104 int restartany; /* if flow control enabled, restart on any character */
105 int localchars; /* we recognize interrupt/quit */
106 int donelclchars; /* the user has set "localchars" */
107 int donebinarytoggle; /* the user has put us in binary */
108 int dontlecho; /* do we suppress local echoing right now? */
109 int eof_pending = 0; /* we received a genuine EOF on input, send IAC-EOF */
110 int globalmode;
112 /* spin while waiting for authentication */
113 boolean_t scheduler_lockout_tty = B_FALSE;
114 int encrypt_flag = 0;
115 int forwardable_flag = 0;
116 int forward_flag = 0;
117 boolean_t wantencryption = B_FALSE;
119 char *prompt = 0;
121 cc_t escape;
122 cc_t rlogin;
123 boolean_t escape_valid = B_TRUE;
124 #ifdef KLUDGELINEMODE
125 cc_t echoc;
126 #endif
129 * Telnet receiver states for fsm
131 #define TS_DATA 0
132 #define TS_IAC 1
133 #define TS_WILL 2
134 #define TS_WONT 3
135 #define TS_DO 4
136 #define TS_DONT 5
137 #define TS_CR 6
138 #define TS_SB 7 /* sub-option collection */
139 #define TS_SE 8 /* looking for sub-option end */
141 static int telrcv_state;
142 #ifdef OLD_ENVIRON
143 static unsigned char telopt_environ = TELOPT_NEW_ENVIRON;
144 #else
145 #define telopt_environ TELOPT_NEW_ENVIRON
146 #endif
148 jmp_buf toplevel = { 0 };
149 jmp_buf peerdied;
151 static int flushline;
152 int linemode;
154 int reqd_linemode = 0; /* Set if either new or old line mode in */
155 /* effect since before initial negotiations */
157 #ifdef KLUDGELINEMODE
158 int kludgelinemode = 1;
159 #endif
162 * The following are some clocks used to decide how to interpret
163 * the relationship between various variables.
166 Clocks clocks;
168 #ifdef notdef
169 Modelist modelist[] = {
170 { "telnet command mode", COMMAND_LINE },
171 { "character-at-a-time mode", 0 },
172 { "character-at-a-time mode (local echo)", LOCAL_ECHO|LOCAL_CHARS },
173 { "line-by-line mode (remote echo)", LINE | LOCAL_CHARS },
174 { "line-by-line mode", LINE | LOCAL_ECHO | LOCAL_CHARS },
175 { "line-by-line mode (local echoing suppressed)", LINE | LOCAL_CHARS },
176 { "3270 mode", 0 },
178 #endif
180 static void willoption(int);
181 static void wontoption(int);
182 static void lm_will(unsigned char *, int);
183 static void lm_wont(unsigned char *, int);
184 static void lm_do(unsigned char *, int);
185 static void lm_dont(unsigned char *, int);
186 static void slc_init(void);
187 static void slc_import(int);
188 static void slc_export(void);
189 static void slc_start_reply(size_t);
190 static void slc_add_reply(unsigned char, unsigned char, cc_t);
191 static void slc_end_reply(void);
192 static void slc(unsigned char *, int);
193 static int slc_update(void);
194 static void env_opt(unsigned char *, int);
195 static void env_opt_start(void);
196 static void sendeof(void);
197 static int is_unique(register char *, register char **, register char **);
200 * Initialize telnet environment.
204 init_telnet()
206 if (env_init() == 0)
207 return (0);
209 SB_CLEAR();
210 ClearArray(options);
212 connected = ISend = localflow = donebinarytoggle = 0;
213 restartany = -1;
215 SYNCHing = 0;
217 /* Don't change NetTrace */
219 escape = CONTROL(']');
220 rlogin = _POSIX_VDISABLE;
221 #ifdef KLUDGELINEMODE
222 echoc = CONTROL('E');
223 #endif
225 flushline = 1;
226 telrcv_state = TS_DATA;
228 return (1);
232 #ifdef notdef
233 #include <varargs.h>
235 /*VARARGS*/
236 static void
237 printring(va_alist)
238 va_dcl
240 va_list ap;
241 char buffer[100]; /* where things go */
242 char *ptr;
243 char *format;
244 char *string;
245 Ring *ring;
246 int i;
248 va_start(ap);
250 ring = va_arg(ap, Ring *);
251 format = va_arg(ap, char *);
252 ptr = buffer;
254 while ((i = *format++) != 0) {
255 if (i == '%') {
256 i = *format++;
257 switch (i) {
258 case 'c':
259 *ptr++ = va_arg(ap, int);
260 break;
261 case 's':
262 string = va_arg(ap, char *);
263 ring_supply_data(ring, buffer, ptr-buffer);
264 ring_supply_data(ring, string, strlen(string));
265 ptr = buffer;
266 break;
267 case 0:
268 ExitString("printring: trailing %%.\n",
269 EXIT_FAILURE);
270 /*NOTREACHED*/
271 default:
272 ExitString("printring: unknown format "
273 "character.\n", EXIT_FAILURE);
274 /*NOTREACHED*/
276 } else {
277 *ptr++ = i;
280 ring_supply_data(ring, buffer, ptr-buffer);
282 #endif
285 * These routines are in charge of sending option negotiations
286 * to the other side.
288 * The basic idea is that we send the negotiation if either side
289 * is in disagreement as to what the current state should be.
292 void
293 send_do(c, init)
294 register int c, init;
296 if (init) {
297 if (((do_dont_resp[c] == 0) && my_state_is_do(c)) ||
298 my_want_state_is_do(c))
299 return;
300 set_my_want_state_do(c);
301 do_dont_resp[c]++;
303 NET2ADD(IAC, DO);
304 NETADD(c);
305 printoption("SENT", DO, c);
308 void
309 send_dont(c, init)
310 register int c, init;
312 if (init) {
313 if (((do_dont_resp[c] == 0) && my_state_is_dont(c)) ||
314 my_want_state_is_dont(c))
315 return;
316 set_my_want_state_dont(c);
317 do_dont_resp[c]++;
319 NET2ADD(IAC, DONT);
320 NETADD(c);
321 printoption("SENT", DONT, c);
324 void
325 send_will(c, init)
326 register int c, init;
328 if (init) {
329 if (((will_wont_resp[c] == 0) && my_state_is_will(c)) ||
330 my_want_state_is_will(c))
331 return;
332 set_my_want_state_will(c);
333 will_wont_resp[c]++;
335 NET2ADD(IAC, WILL);
336 NETADD(c);
337 printoption("SENT", WILL, c);
340 void
341 send_wont(c, init)
342 register int c, init;
344 if (init) {
345 if (((will_wont_resp[c] == 0) && my_state_is_wont(c)) ||
346 my_want_state_is_wont(c))
347 return;
348 set_my_want_state_wont(c);
349 will_wont_resp[c]++;
351 NET2ADD(IAC, WONT);
352 NETADD(c);
353 printoption("SENT", WONT, c);
357 static void
358 willoption(option)
359 int option;
361 int new_state_ok = 0;
363 if (do_dont_resp[option]) {
364 --do_dont_resp[option];
365 if (do_dont_resp[option] && my_state_is_do(option))
366 --do_dont_resp[option];
369 if ((do_dont_resp[option] == 0) && my_want_state_is_dont(option)) {
371 switch (option) {
373 case TELOPT_ECHO:
374 case TELOPT_SGA:
375 if (reqd_linemode && my_state_is_dont(option)) {
376 break;
378 /* FALLTHROUGH */
379 case TELOPT_BINARY:
380 settimer(modenegotiated);
381 /* FALLTHROUGH */
382 case TELOPT_STATUS:
383 case TELOPT_AUTHENTICATION:
384 /* FALLTHROUGH */
385 case TELOPT_ENCRYPT:
386 new_state_ok = 1;
387 break;
389 case TELOPT_TM:
390 if (flushout)
391 flushout = 0;
393 * Special case for TM. If we get back a WILL,
394 * pretend we got back a WONT.
396 set_my_want_state_dont(option);
397 set_my_state_dont(option);
398 return; /* Never reply to TM will's/wont's */
400 case TELOPT_LINEMODE:
401 default:
402 break;
405 if (new_state_ok) {
406 set_my_want_state_do(option);
407 send_do(option, 0);
408 setconnmode(0); /* possibly set new tty mode */
409 } else {
410 do_dont_resp[option]++;
411 send_dont(option, 0);
414 set_my_state_do(option);
415 if (option == TELOPT_ENCRYPT)
416 encrypt_send_support();
419 static void
420 wontoption(option)
421 int option;
423 if (do_dont_resp[option]) {
424 --do_dont_resp[option];
425 if (do_dont_resp[option] && my_state_is_dont(option))
426 --do_dont_resp[option];
429 if ((do_dont_resp[option] == 0) && my_want_state_is_do(option)) {
431 switch (option) {
433 #ifdef KLUDGELINEMODE
434 case TELOPT_SGA:
435 if (!kludgelinemode)
436 break;
437 /* FALLTHROUGH */
438 #endif
439 case TELOPT_ECHO:
440 settimer(modenegotiated);
441 break;
443 case TELOPT_TM:
444 if (flushout)
445 flushout = 0;
446 set_my_want_state_dont(option);
447 set_my_state_dont(option);
448 return; /* Never reply to TM will's/wont's */
450 default:
451 break;
453 set_my_want_state_dont(option);
454 if (my_state_is_do(option))
455 send_dont(option, 0);
456 setconnmode(0); /* Set new tty mode */
457 } else if (option == TELOPT_TM) {
459 * Special case for TM.
461 if (flushout)
462 flushout = 0;
463 set_my_want_state_dont(option);
465 set_my_state_dont(option);
468 static void
469 dooption(option)
470 int option;
472 int new_state_ok = 0;
474 if (will_wont_resp[option]) {
475 --will_wont_resp[option];
476 if (will_wont_resp[option] && my_state_is_will(option))
477 --will_wont_resp[option];
480 if (will_wont_resp[option] == 0) {
481 if (my_want_state_is_wont(option)) {
483 switch (option) {
485 case TELOPT_TM:
487 * Special case for TM. We send a WILL,
488 * but pretend we sent WONT.
490 send_will(option, 0);
491 set_my_want_state_wont(TELOPT_TM);
492 set_my_state_wont(TELOPT_TM);
493 return;
495 case TELOPT_BINARY: /* binary mode */
496 case TELOPT_NAWS: /* window size */
497 case TELOPT_TSPEED: /* terminal speed */
498 case TELOPT_LFLOW: /* local flow control */
499 case TELOPT_TTYPE: /* terminal type option */
500 case TELOPT_SGA: /* no big deal */
501 case TELOPT_ENCRYPT: /* encryption variable option */
502 new_state_ok = 1;
503 break;
505 case TELOPT_NEW_ENVIRON:
506 /* New environment variable option */
507 #ifdef OLD_ENVIRON
508 if (my_state_is_will(TELOPT_OLD_ENVIRON))
509 /* turn off the old */
510 send_wont(TELOPT_OLD_ENVIRON, 1);
511 goto env_common;
512 case TELOPT_OLD_ENVIRON:
513 /* Old environment variable option */
514 if (my_state_is_will(TELOPT_NEW_ENVIRON))
515 /* Don't enable if new one is in use! */
516 break;
517 env_common:
518 telopt_environ = option;
519 #endif
520 new_state_ok = 1;
521 break;
523 case TELOPT_AUTHENTICATION:
524 if (autologin)
525 new_state_ok = 1;
526 break;
528 case TELOPT_XDISPLOC: /* X Display location */
529 if (env_getvalue((unsigned char *)"DISPLAY"))
530 new_state_ok = 1;
531 break;
533 case TELOPT_LINEMODE:
534 #ifdef KLUDGELINEMODE
535 kludgelinemode = 0;
536 send_do(TELOPT_SGA, 1);
537 #endif
538 set_my_want_state_will(TELOPT_LINEMODE);
539 send_will(option, 0);
540 set_my_state_will(TELOPT_LINEMODE);
541 slc_init();
542 return;
544 case TELOPT_ECHO: /* We're never going to echo... */
545 default:
546 break;
549 if (new_state_ok) {
550 set_my_want_state_will(option);
551 send_will(option, 0);
552 setconnmode(0); /* Set new tty mode */
553 } else {
554 will_wont_resp[option]++;
555 send_wont(option, 0);
557 } else {
559 * Handle options that need more things done after the
560 * other side has acknowledged the option.
562 switch (option) {
563 case TELOPT_LINEMODE:
564 #ifdef KLUDGELINEMODE
565 kludgelinemode = 0;
566 send_do(TELOPT_SGA, 1);
567 #endif
568 set_my_state_will(option);
569 slc_init();
570 send_do(TELOPT_SGA, 0);
571 return;
575 set_my_state_will(option);
578 static void
579 dontoption(option)
580 int option;
583 if (will_wont_resp[option]) {
584 --will_wont_resp[option];
585 if (will_wont_resp[option] && my_state_is_wont(option))
586 --will_wont_resp[option];
589 if ((will_wont_resp[option] == 0) && my_want_state_is_will(option)) {
590 switch (option) {
591 case TELOPT_LINEMODE:
592 linemode = 0; /* put us back to the default state */
593 break;
594 #ifdef OLD_ENVIRON
595 case TELOPT_NEW_ENVIRON:
597 * The new environ option wasn't recognized, try
598 * the old one.
600 send_will(TELOPT_OLD_ENVIRON, 1);
601 telopt_environ = TELOPT_OLD_ENVIRON;
602 break;
603 #endif
605 /* we always accept a DONT */
606 set_my_want_state_wont(option);
607 if (my_state_is_will(option))
608 send_wont(option, 0);
609 setconnmode(0); /* Set new tty mode */
611 set_my_state_wont(option);
615 * Given a buffer returned by tgetent(), this routine will turn
616 * the pipe seperated list of names in the buffer into an array
617 * of pointers to null terminated names. We toss out any bad,
618 * duplicate, or verbose names (names with spaces).
621 static char *name_unknown = "UNKNOWN";
622 static char *unknown[] = { 0, 0 };
624 static char **
625 mklist(buf, name)
626 char *buf, *name;
628 register int n;
629 register char c, *cp, **argvp, *cp2, **argv, **avt;
631 if (name) {
632 if (strlen(name) > 40u) {
633 name = 0;
634 unknown[0] = name_unknown;
635 } else {
636 unknown[0] = name;
637 upcase(name);
639 } else
640 unknown[0] = name_unknown;
642 * Count up the number of names.
644 for (n = 1, cp = buf; *cp && *cp != ':'; cp++) {
645 if (*cp == '|')
646 n++;
649 * Allocate an array to put the name pointers into
651 argv = malloc((n+3)*sizeof (char *));
652 if (argv == 0)
653 return (unknown);
656 * Fill up the array of pointers to names.
658 *argv = 0;
659 argvp = argv+1;
660 n = 0;
661 for (cp = cp2 = buf; (c = *cp) != '\0'; cp++) {
662 if (c == '|' || c == ':') {
663 *cp++ = '\0';
665 * Skip entries that have spaces or are over 40
666 * characters long. If this is our environment
667 * name, then put it up front. Otherwise, as
668 * long as this is not a duplicate name (case
669 * insensitive) add it to the list.
671 if (n || (cp - cp2 > 41))
672 /* EMPTY */;
673 else if (name && (strncasecmp(name, cp2, cp-cp2) == 0))
674 *argv = cp2;
675 else if (is_unique(cp2, argv+1, argvp))
676 *argvp++ = cp2;
677 if (c == ':')
678 break;
680 * Skip multiple delimiters. Reset cp2 to
681 * the beginning of the next name. Reset n,
682 * the flag for names with spaces.
684 while ((c = *cp) == '|')
685 cp++;
686 cp2 = cp;
687 n = 0;
690 * Skip entries with spaces or non-ascii values.
691 * Convert lower case letters to upper case.
693 if ((c == ' ') || !isascii(c))
694 n = 1;
695 else if (islower(c))
696 *cp = toupper(c);
700 * Check for an old V6 2 character name. If the second
701 * name points to the beginning of the buffer, and is
702 * only 2 characters long, move it to the end of the array.
704 if ((argv[1] == buf) && (strlen(argv[1]) == 2)) {
705 --argvp;
706 for (avt = &argv[1]; avt < argvp; avt++)
707 *avt = *(avt+1);
708 *argvp++ = buf;
712 * Duplicate last name, for TTYPE option, and null
713 * terminate the array. If we didn't find a match on
714 * our terminal name, put that name at the beginning.
716 cp = *(argvp-1);
717 *argvp++ = cp;
718 *argvp = 0;
720 if (*argv == 0) {
721 if (name)
722 *argv = name;
723 else {
724 --argvp;
725 for (avt = argv; avt < argvp; avt++)
726 *avt = *(avt+1);
729 if (*argv)
730 return (argv);
731 else
732 return (unknown);
735 static int
736 is_unique(name, as, ae)
737 register char *name, **as, **ae;
739 register char **ap;
740 register int n;
742 n = strlen(name) + 1;
743 for (ap = as; ap < ae; ap++)
744 if (strncasecmp(*ap, name, n) == 0)
745 return (0);
746 return (1);
749 #define termbuf ttytype
750 extern char ttytype[];
752 int resettermname = 1;
754 static char *
755 gettermname(void)
757 char *tname;
758 static char **tnamep = 0;
759 static char **next;
760 int err;
762 if (resettermname) {
763 resettermname = 0;
764 if (tnamep && tnamep != unknown)
765 free(tnamep);
766 tname = (char *)env_getvalue((unsigned char *)"TERM");
767 if ((tname != NULL) && (setupterm(tname, 1, &err) == 0)) {
768 tnamep = mklist(termbuf, tname);
769 } else {
770 if (tname && (strlen(tname) <= 40u)) {
771 unknown[0] = tname;
772 upcase(tname);
773 } else
774 unknown[0] = name_unknown;
775 tnamep = unknown;
777 next = tnamep;
779 if (*next == 0)
780 next = tnamep;
781 return (*next++);
784 * suboption()
786 * Look at the sub-option buffer, and try to be helpful to the other
787 * side.
789 * Currently we recognize:
791 * Terminal type, send request.
792 * Terminal speed (send request).
793 * Local flow control (is request).
794 * Linemode
797 static void
798 suboption()
800 unsigned char subchar;
802 printsub('<', subbuffer, SB_LEN()+2);
803 switch (subchar = SB_GET()) {
804 case TELOPT_TTYPE:
805 if (my_want_state_is_wont(TELOPT_TTYPE))
806 return;
807 if (SB_EOF() || SB_GET() != TELQUAL_SEND) {
808 return;
809 } else {
810 char *name;
811 unsigned char temp[50];
812 int len, bytes;
814 name = gettermname();
815 len = strlen(name) + 4 + 2;
816 bytes = snprintf((char *)temp, sizeof (temp),
817 "%c%c%c%c%s%c%c", IAC, SB,
818 TELOPT_TTYPE, TELQUAL_IS, name, IAC, SE);
819 if ((len < NETROOM()) && (bytes < sizeof (temp))) {
820 ring_supply_data(&netoring, temp, len);
821 printsub('>', &temp[2], len-2);
822 } else {
823 ExitString("No room in buffer for "
824 "terminal type.\n", EXIT_FAILURE);
825 /*NOTREACHED*/
828 break;
829 case TELOPT_TSPEED:
830 if (my_want_state_is_wont(TELOPT_TSPEED))
831 return;
832 if (SB_EOF())
833 return;
834 if (SB_GET() == TELQUAL_SEND) {
835 int ospeed, ispeed;
836 unsigned char temp[50];
837 int len, bytes;
839 TerminalSpeeds(&ispeed, &ospeed);
841 bytes = snprintf((char *)temp, sizeof (temp),
842 "%c%c%c%c%d,%d%c%c", IAC, SB,
843 TELOPT_TSPEED, TELQUAL_IS, ospeed, ispeed, IAC, SE);
844 len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */
846 if ((len < NETROOM()) && (bytes < sizeof (temp))) {
847 ring_supply_data(&netoring, temp, len);
848 printsub('>', temp+2, len - 2);
850 else
851 (void) printf(
852 "telnet: not enough room in buffer "
853 "for terminal speed option reply\n");
855 break;
856 case TELOPT_LFLOW:
857 if (my_want_state_is_wont(TELOPT_LFLOW))
858 return;
859 if (SB_EOF())
860 return;
861 switch (SB_GET()) {
862 case LFLOW_RESTART_ANY:
863 restartany = 1;
864 break;
865 case LFLOW_RESTART_XON:
866 restartany = 0;
867 break;
868 case LFLOW_ON:
869 localflow = 1;
870 break;
871 case LFLOW_OFF:
872 localflow = 0;
873 break;
874 default:
875 return;
877 setcommandmode();
878 setconnmode(0);
879 break;
881 case TELOPT_LINEMODE:
882 if (my_want_state_is_wont(TELOPT_LINEMODE))
883 return;
884 if (SB_EOF())
885 return;
886 switch (SB_GET()) {
887 case WILL:
888 lm_will(subpointer, SB_LEN());
889 break;
890 case WONT:
891 lm_wont(subpointer, SB_LEN());
892 break;
893 case DO:
894 lm_do(subpointer, SB_LEN());
895 break;
896 case DONT:
897 lm_dont(subpointer, SB_LEN());
898 break;
899 case LM_SLC:
900 slc(subpointer, SB_LEN());
901 break;
902 case LM_MODE:
903 lm_mode(subpointer, SB_LEN(), 0);
904 break;
905 default:
906 break;
908 break;
910 #ifdef OLD_ENVIRON
911 case TELOPT_OLD_ENVIRON:
912 #endif
913 case TELOPT_NEW_ENVIRON:
914 if (SB_EOF())
915 return;
916 switch (SB_PEEK()) {
917 case TELQUAL_IS:
918 case TELQUAL_INFO:
919 if (my_want_state_is_dont(subchar))
920 return;
921 break;
922 case TELQUAL_SEND:
923 if (my_want_state_is_wont(subchar)) {
924 return;
926 break;
927 default:
928 return;
930 env_opt(subpointer, SB_LEN());
931 break;
933 case TELOPT_XDISPLOC:
934 if (my_want_state_is_wont(TELOPT_XDISPLOC))
935 return;
936 if (SB_EOF())
937 return;
938 if (SB_GET() == TELQUAL_SEND) {
939 unsigned char temp[50], *dp;
940 int len, bytes;
942 if ((dp = env_getvalue((unsigned char *)"DISPLAY")) ==
943 NULL) {
945 * Something happened, we no longer have a
946 * DISPLAY variable. So, turn off the option.
948 send_wont(TELOPT_XDISPLOC, 1);
949 break;
951 bytes = snprintf((char *)temp, sizeof (temp),
952 "%c%c%c%c%s%c%c", IAC, SB,
953 TELOPT_XDISPLOC, TELQUAL_IS, dp, IAC, SE);
954 len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */
956 if ((len < NETROOM()) && (bytes < sizeof (temp))) {
957 ring_supply_data(&netoring, temp, len);
958 printsub('>', temp+2, len - 2);
960 else
961 (void) printf(
962 "telnet: not enough room in buffer"
963 " for display location option reply\n");
965 break;
967 case TELOPT_AUTHENTICATION: {
968 if (!autologin)
969 break;
970 if (SB_EOF())
971 return;
972 switch (SB_GET()) {
973 case TELQUAL_SEND:
974 if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
975 return;
976 auth_send(subpointer, SB_LEN());
977 break;
978 case TELQUAL_REPLY:
979 if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
980 return;
981 auth_reply(subpointer, SB_LEN());
982 break;
985 break;
987 case TELOPT_ENCRYPT:
988 if (SB_EOF())
989 return;
990 switch (SB_GET()) {
991 case ENCRYPT_START:
992 if (my_want_state_is_dont(TELOPT_ENCRYPT))
993 return;
994 encrypt_start(subpointer, SB_LEN());
995 break;
996 case ENCRYPT_END:
997 if (my_want_state_is_dont(TELOPT_ENCRYPT))
998 return;
999 encrypt_end();
1000 break;
1001 case ENCRYPT_SUPPORT:
1002 if (my_want_state_is_wont(TELOPT_ENCRYPT))
1003 return;
1004 encrypt_support(subpointer, SB_LEN());
1005 break;
1006 case ENCRYPT_REQSTART:
1007 if (my_want_state_is_wont(TELOPT_ENCRYPT))
1008 return;
1009 encrypt_request_start(subpointer, SB_LEN());
1010 break;
1011 case ENCRYPT_REQEND:
1012 if (my_want_state_is_wont(TELOPT_ENCRYPT))
1013 return;
1015 * We can always send an REQEND so that we cannot
1016 * get stuck encrypting. We should only get this
1017 * if we have been able to get in the correct mode
1018 * anyhow.
1020 encrypt_request_end();
1021 break;
1022 case ENCRYPT_IS:
1023 if (my_want_state_is_dont(TELOPT_ENCRYPT))
1024 return;
1025 encrypt_is(subpointer, SB_LEN());
1026 break;
1027 case ENCRYPT_REPLY:
1028 if (my_want_state_is_wont(TELOPT_ENCRYPT))
1029 return;
1030 encrypt_reply(subpointer, SB_LEN());
1031 break;
1032 case ENCRYPT_ENC_KEYID:
1033 if (my_want_state_is_dont(TELOPT_ENCRYPT))
1034 return;
1035 encrypt_enc_keyid(subpointer, SB_LEN());
1036 break;
1037 case ENCRYPT_DEC_KEYID:
1038 if (my_want_state_is_wont(TELOPT_ENCRYPT))
1039 return;
1040 encrypt_dec_keyid(subpointer, SB_LEN());
1041 break;
1042 default:
1043 break;
1045 break;
1046 default:
1047 break;
1051 static unsigned char str_lm[] = { IAC, SB, TELOPT_LINEMODE, 0, 0, IAC, SE };
1053 static void
1054 lm_will(cmd, len)
1055 unsigned char *cmd;
1056 int len;
1058 if (len < 1) {
1059 /* Should not happen... */
1060 (void) printf(
1061 "telnet: command missing from linemode WILL request\n");
1062 return;
1064 switch (cmd[0]) {
1065 case LM_FORWARDMASK: /* We shouldn't ever get this... */
1066 default:
1067 str_lm[3] = DONT;
1068 str_lm[4] = cmd[0];
1069 if (NETROOM() > sizeof (str_lm)) {
1070 ring_supply_data(&netoring, str_lm, sizeof (str_lm));
1071 printsub('>', &str_lm[2], sizeof (str_lm)-2);
1073 else
1074 (void) printf("telnet: not enough room in buffer for"
1075 "reply to linemode WILL request\n");
1076 break;
1080 static void
1081 lm_wont(cmd, len)
1082 unsigned char *cmd;
1083 int len;
1085 if (len < 1) {
1086 /* Should not happen... */
1087 (void) printf(
1088 "telnet: command missing from linemode WONT request\n");
1089 return;
1091 switch (cmd[0]) {
1092 case LM_FORWARDMASK: /* We shouldn't ever get this... */
1093 default:
1094 /* We are always DONT, so don't respond */
1095 return;
1099 static void
1100 lm_do(cmd, len)
1101 unsigned char *cmd;
1102 int len;
1104 if (len < 1) {
1105 /* Should not happen... */
1106 (void) printf(
1107 "telnet: command missing from linemode DO request\n");
1108 return;
1110 switch (cmd[0]) {
1111 case LM_FORWARDMASK:
1112 default:
1113 str_lm[3] = WONT;
1114 str_lm[4] = cmd[0];
1115 if (NETROOM() > sizeof (str_lm)) {
1116 ring_supply_data(&netoring, str_lm, sizeof (str_lm));
1117 printsub('>', &str_lm[2], sizeof (str_lm)-2);
1119 else
1120 (void) printf("telnet: not enough room in buffer for"
1121 "reply to linemode DO request\n");
1122 break;
1126 static void
1127 lm_dont(cmd, len)
1128 unsigned char *cmd;
1129 int len;
1131 if (len < 1) {
1132 /* Should not happen... */
1133 (void) printf(
1134 "telnet: command missing from linemode DONT request\n");
1135 return;
1137 switch (cmd[0]) {
1138 case LM_FORWARDMASK:
1139 default:
1140 /* we are always WONT, so don't respond */
1141 break;
1145 static unsigned char str_lm_mode[] = {
1146 IAC, SB, TELOPT_LINEMODE, LM_MODE, 0, IAC, SE
1149 void
1150 lm_mode(cmd, len, init)
1151 unsigned char *cmd;
1152 int len, init;
1154 if (len != 1)
1155 return;
1156 if ((linemode&MODE_MASK&~MODE_ACK) == *cmd)
1157 return;
1158 linemode = *cmd&(MODE_MASK&~MODE_ACK);
1159 str_lm_mode[4] = linemode;
1160 if (!init)
1161 str_lm_mode[4] |= MODE_ACK;
1162 if (NETROOM() > sizeof (str_lm_mode)) {
1163 ring_supply_data(&netoring, str_lm_mode, sizeof (str_lm_mode));
1164 printsub('>', &str_lm_mode[2], sizeof (str_lm_mode)-2);
1166 else
1167 (void) printf("telnet: not enough room in buffer for"
1168 "reply to linemode request\n");
1169 setconnmode(0); /* set changed mode */
1175 * slc()
1176 * Handle special character suboption of LINEMODE.
1179 static struct spc {
1180 cc_t val;
1181 cc_t *valp;
1182 char flags; /* Current flags & level */
1183 char mylevel; /* Maximum level & flags */
1184 } spc_data[NSLC+1];
1186 #define SLC_IMPORT 0
1187 #define SLC_EXPORT 1
1188 #define SLC_RVALUE 2
1189 static int slc_mode = SLC_EXPORT;
1191 static void
1192 slc_init()
1194 register struct spc *spcp;
1196 localchars = 1;
1197 for (spcp = spc_data; spcp < &spc_data[NSLC+1]; spcp++) {
1198 spcp->val = 0;
1199 spcp->valp = 0;
1200 spcp->flags = spcp->mylevel = SLC_NOSUPPORT;
1203 #define initfunc(func, flags) { \
1204 spcp = &spc_data[func]; \
1205 if (spcp->valp = tcval(func)) { \
1206 spcp->val = *spcp->valp; \
1207 spcp->mylevel = SLC_VARIABLE|(flags);\
1208 } else { \
1209 spcp->val = 0; \
1210 spcp->mylevel = SLC_DEFAULT; \
1214 initfunc(SLC_SYNCH, 0);
1215 /* No BRK */
1216 initfunc(SLC_AO, 0);
1217 initfunc(SLC_AYT, 0);
1218 /* No EOR */
1219 initfunc(SLC_ABORT, SLC_FLUSHIN|SLC_FLUSHOUT);
1220 initfunc(SLC_EOF, 0);
1221 initfunc(SLC_SUSP, SLC_FLUSHIN);
1222 initfunc(SLC_EC, 0);
1223 initfunc(SLC_EL, 0);
1224 initfunc(SLC_EW, 0);
1225 initfunc(SLC_RP, 0);
1226 initfunc(SLC_LNEXT, 0);
1227 initfunc(SLC_XON, 0);
1228 initfunc(SLC_XOFF, 0);
1229 initfunc(SLC_FORW1, 0);
1230 #ifdef USE_TERMIO
1231 initfunc(SLC_FORW2, 0);
1232 /* No FORW2 */
1233 #endif
1235 initfunc(SLC_IP, SLC_FLUSHIN|SLC_FLUSHOUT);
1236 #undef initfunc
1238 if (slc_mode == SLC_EXPORT)
1239 slc_export();
1240 else
1241 slc_import(1);
1245 void
1246 slcstate()
1248 (void) printf("Special characters are %s values\n",
1249 slc_mode == SLC_IMPORT ? "remote default" :
1250 slc_mode == SLC_EXPORT ? "local" :
1251 "remote");
1254 void
1255 slc_mode_export()
1257 slc_mode = SLC_EXPORT;
1258 if (my_state_is_will(TELOPT_LINEMODE))
1259 slc_export();
1262 void
1263 slc_mode_import(def)
1264 int def;
1266 slc_mode = def ? SLC_IMPORT : SLC_RVALUE;
1267 if (my_state_is_will(TELOPT_LINEMODE))
1268 slc_import(def);
1271 static unsigned char slc_import_val[] = {
1272 IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_VARIABLE, 0, IAC, SE
1274 static unsigned char slc_import_def[] = {
1275 IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_DEFAULT, 0, IAC, SE
1278 static void
1279 slc_import(def)
1280 int def;
1282 if (NETROOM() > sizeof (slc_import_val)) {
1283 if (def) {
1284 ring_supply_data(&netoring, slc_import_def,
1285 sizeof (slc_import_def));
1286 printsub('>', &slc_import_def[2],
1287 sizeof (slc_import_def)-2);
1288 } else {
1289 ring_supply_data(&netoring, slc_import_val,
1290 sizeof (slc_import_val));
1291 printsub('>', &slc_import_val[2],
1292 sizeof (slc_import_val)-2);
1295 else
1296 (void) printf(
1297 "telnet: not enough room in buffer for slc import"
1298 " request\n");
1301 static uchar_t *slc_reply = NULL;
1302 static uchar_t *slc_replyp = NULL;
1304 * The SLC reply consists of: IAC, SB, TELOPT_LINEMODE, LM_SLC,
1305 * SLC triplets[], IAC, SE. i.e. it has a 'wrapper' of 6 control characters.
1307 #define SLC_WRAPPER_SIZE 6
1309 static void
1310 slc_export()
1312 register struct spc *spcp;
1314 TerminalDefaultChars();
1316 slc_start_reply(NSLC * 3); /* 3 bytes needed per triplet */
1317 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1318 if (spcp->mylevel != SLC_NOSUPPORT) {
1319 if (spcp->val == (cc_t)(_POSIX_VDISABLE))
1320 spcp->flags = SLC_NOSUPPORT;
1321 else
1322 spcp->flags = spcp->mylevel;
1323 if (spcp->valp)
1324 spcp->val = *spcp->valp;
1325 slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
1328 slc_end_reply();
1329 (void) slc_update();
1330 setconnmode(1); /* Make sure the character values are set */
1333 static void
1334 slc(cp, len)
1335 register unsigned char *cp;
1336 int len;
1338 register struct spc *spcp;
1339 register int func, level;
1341 slc_start_reply(len);
1343 for (; len >= 3; len -= 3, cp += 3) {
1345 func = cp[SLC_FUNC];
1347 if (func == 0) {
1349 * Client side: always ignore 0 function.
1351 continue;
1353 if (func > NSLC) {
1354 if ((cp[SLC_FLAGS] & SLC_LEVELBITS) != SLC_NOSUPPORT)
1355 slc_add_reply(func, SLC_NOSUPPORT, 0);
1356 continue;
1359 spcp = &spc_data[func];
1361 level = cp[SLC_FLAGS]&(SLC_LEVELBITS|SLC_ACK);
1363 if ((cp[SLC_VALUE] == (unsigned char)spcp->val) &&
1364 ((level&SLC_LEVELBITS) == (spcp->flags&SLC_LEVELBITS))) {
1365 continue;
1368 if (level == (SLC_DEFAULT|SLC_ACK)) {
1370 * This is an error condition, the SLC_ACK
1371 * bit should never be set for the SLC_DEFAULT
1372 * level. Our best guess to recover is to
1373 * ignore the SLC_ACK bit.
1375 cp[SLC_FLAGS] &= ~SLC_ACK;
1378 if (level == ((spcp->flags&SLC_LEVELBITS)|SLC_ACK)) {
1379 spcp->val = (cc_t)cp[SLC_VALUE];
1380 spcp->flags = cp[SLC_FLAGS]; /* include SLC_ACK */
1381 continue;
1384 level &= ~SLC_ACK;
1386 if (level <= (spcp->mylevel&SLC_LEVELBITS)) {
1387 spcp->flags = cp[SLC_FLAGS]|SLC_ACK;
1388 spcp->val = (cc_t)cp[SLC_VALUE];
1390 if (level == SLC_DEFAULT) {
1391 if ((spcp->mylevel&SLC_LEVELBITS) != SLC_DEFAULT)
1392 spcp->flags = spcp->mylevel;
1393 else
1394 spcp->flags = SLC_NOSUPPORT;
1396 slc_add_reply(func, spcp->flags, spcp->val);
1398 slc_end_reply();
1399 if (slc_update())
1400 setconnmode(1); /* set the new character values */
1403 void
1404 slc_check()
1406 register struct spc *spcp;
1408 slc_start_reply(NSLC * 3); /* 3 bytes needed per triplet */
1409 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1410 if (spcp->valp && spcp->val != *spcp->valp) {
1411 spcp->val = *spcp->valp;
1412 if (spcp->val == (cc_t)(_POSIX_VDISABLE))
1413 spcp->flags = SLC_NOSUPPORT;
1414 else
1415 spcp->flags = spcp->mylevel;
1416 slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
1419 slc_end_reply();
1420 setconnmode(1);
1423 static void
1424 slc_start_reply(size_t len)
1427 * SLC triplets may contain escaped characters, allow for
1428 * worst case by allocating 2 bytes for every character.
1430 slc_reply = realloc(slc_reply, (len * 2) + SLC_WRAPPER_SIZE);
1431 if (slc_reply == NULL) {
1432 fprintf(stderr, "telnet: error allocating SLC reply memory\n");
1433 return;
1435 slc_replyp = slc_reply;
1436 *slc_replyp++ = IAC;
1437 *slc_replyp++ = SB;
1438 *slc_replyp++ = TELOPT_LINEMODE;
1439 *slc_replyp++ = LM_SLC;
1442 static void
1443 slc_add_reply(unsigned char func, unsigned char flags, cc_t value)
1445 if ((*slc_replyp++ = func) == IAC)
1446 *slc_replyp++ = IAC;
1447 if ((*slc_replyp++ = flags) == IAC)
1448 *slc_replyp++ = IAC;
1449 if ((*slc_replyp++ = (unsigned char)value) == IAC)
1450 *slc_replyp++ = IAC;
1453 static void
1454 slc_end_reply()
1456 register int len;
1458 *slc_replyp++ = IAC;
1459 *slc_replyp++ = SE;
1460 len = slc_replyp - slc_reply;
1461 if (len <= SLC_WRAPPER_SIZE)
1462 return;
1463 if (NETROOM() > len) {
1464 ring_supply_data(&netoring, slc_reply, slc_replyp - slc_reply);
1465 printsub('>', &slc_reply[2], slc_replyp - slc_reply - 2);
1467 else
1468 (void) printf("telnet: not enough room in buffer for slc end "
1469 "reply\n");
1472 static int
1473 slc_update()
1475 register struct spc *spcp;
1476 int need_update = 0;
1478 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1479 if (!(spcp->flags&SLC_ACK))
1480 continue;
1481 spcp->flags &= ~SLC_ACK;
1482 if (spcp->valp && (*spcp->valp != spcp->val)) {
1483 *spcp->valp = spcp->val;
1484 need_update = 1;
1487 return (need_update);
1490 #ifdef OLD_ENVIRON
1491 #ifdef ENV_HACK
1493 * Earlier version of telnet/telnetd from the BSD code had
1494 * the definitions of VALUE and VAR reversed. To ensure
1495 * maximum interoperability, we assume that the server is
1496 * an older BSD server, until proven otherwise. The newer
1497 * BSD servers should be able to handle either definition,
1498 * so it is better to use the wrong values if we don't
1499 * know what type of server it is.
1501 int env_auto = 1;
1502 int old_env_var = OLD_ENV_VAR;
1503 int old_env_value = OLD_ENV_VALUE;
1504 #else
1505 #define old_env_var OLD_ENV_VAR
1506 #define old_env_value OLD_ENV_VALUE
1507 #endif
1508 #endif
1510 static void
1511 env_opt(buf, len)
1512 register unsigned char *buf;
1513 register int len;
1515 register unsigned char *ep = 0, *epc = 0;
1516 register int i;
1518 switch (buf[0]&0xff) {
1519 case TELQUAL_SEND:
1520 env_opt_start();
1521 if (len == 1) {
1522 env_opt_add(NULL);
1523 } else for (i = 1; i < len; i++) {
1524 switch (buf[i]&0xff) {
1525 #ifdef OLD_ENVIRON
1526 case OLD_ENV_VAR:
1527 #ifdef ENV_HACK
1528 if (telopt_environ == TELOPT_OLD_ENVIRON &&
1529 env_auto) {
1530 /* Server has the same definitions */
1531 old_env_var = OLD_ENV_VAR;
1532 old_env_value = OLD_ENV_VALUE;
1534 /* FALLTHROUGH */
1535 #endif
1536 case OLD_ENV_VALUE:
1538 * Although OLD_ENV_VALUE is not legal, we will
1539 * still recognize it, just in case it is an
1540 * old server that has VAR & VALUE mixed up...
1542 /* FALLTHROUGH */
1543 #else
1544 case NEW_ENV_VAR:
1545 #endif
1546 case ENV_USERVAR:
1547 if (ep) {
1548 *epc = 0;
1549 env_opt_add(ep);
1551 ep = epc = &buf[i+1];
1552 break;
1553 case ENV_ESC:
1554 i++;
1555 /*FALLTHROUGH*/
1556 default:
1557 if (epc)
1558 *epc++ = buf[i];
1559 break;
1562 if (ep) {
1563 *epc = 0;
1564 env_opt_add(ep);
1566 env_opt_end(1);
1567 break;
1569 case TELQUAL_IS:
1570 case TELQUAL_INFO:
1571 /* Ignore for now. We shouldn't get it anyway. */
1572 break;
1574 default:
1575 break;
1579 static unsigned char *opt_reply;
1580 static unsigned char *opt_replyp;
1581 static unsigned char *opt_replyend;
1582 #define OPT_REPLY_INITIAL_SIZE 256
1584 * The opt reply consists of: IAC, SB, telopt_environ, TELQUAL_IS,
1585 * value, IAC, SE. i.e. it has a 'wrapper' of 6 control characters.
1587 #define OPT_WRAPPER_SIZE 6
1589 static void
1590 env_opt_start()
1592 opt_reply = realloc(opt_reply, OPT_REPLY_INITIAL_SIZE);
1593 if (opt_reply == NULL) {
1594 (void) printf(
1595 "telnet: error allocating environment option memory\n");
1596 opt_reply = opt_replyp = opt_replyend = NULL;
1597 return;
1599 opt_replyp = opt_reply;
1600 opt_replyend = opt_reply + OPT_REPLY_INITIAL_SIZE;
1601 *opt_replyp++ = IAC;
1602 *opt_replyp++ = SB;
1603 *opt_replyp++ = telopt_environ;
1604 *opt_replyp++ = TELQUAL_IS;
1607 void
1608 env_opt_start_info()
1610 env_opt_start();
1611 if (opt_replyp)
1612 opt_replyp[-1] = TELQUAL_INFO;
1615 void
1616 env_opt_add(ep)
1617 register unsigned char *ep;
1619 register unsigned char *vp, c;
1620 int opt_reply_size;
1621 int opt_reply_used;
1623 if (opt_reply == NULL) /* XXX */
1624 return; /* XXX */
1626 if (ep == NULL || *ep == '\0') {
1627 /* Send user defined variables first. */
1628 (void) env_default(1, 0);
1629 while (ep = env_default(0, 0))
1630 env_opt_add(ep);
1632 /* Now add the list of well know variables. */
1633 (void) env_default(1, 1);
1634 while (ep = env_default(0, 1))
1635 env_opt_add(ep);
1636 return;
1638 vp = env_getvalue(ep);
1641 * Calculate space required for opt_reply and allocate more if required.
1642 * Assume worst case that every character is escaped, so needs 2 bytes.
1644 opt_reply_used = opt_replyp - opt_reply; /* existing contents */
1645 opt_reply_size = opt_reply_used + OPT_WRAPPER_SIZE +
1646 (2 * (strlen((char *)ep))) +
1647 (vp == NULL ? 0 : (2 * strlen((char *)vp)));
1649 if (opt_reply_size > (opt_replyend - opt_reply)) {
1650 opt_reply = realloc(opt_reply, opt_reply_size);
1651 if (opt_reply == NULL) {
1652 (void) printf(
1653 "telnet: can't allocate environment option "
1654 "reply\n");
1655 opt_reply = opt_replyp = opt_replyend = NULL;
1656 return;
1658 opt_replyp = opt_reply + opt_reply_used;
1659 opt_replyend = opt_reply + opt_reply_size;
1662 if (opt_welldefined((char *)ep))
1663 #ifdef OLD_ENVIRON
1664 if (telopt_environ == TELOPT_OLD_ENVIRON)
1665 *opt_replyp++ = old_env_var;
1666 else
1667 #endif
1668 *opt_replyp++ = NEW_ENV_VAR;
1669 else
1670 *opt_replyp++ = ENV_USERVAR;
1671 for (;;) {
1672 while ((c = *ep++) != '\0') {
1673 switch (c&0xff) {
1674 case IAC:
1675 *opt_replyp++ = IAC;
1676 break;
1677 case NEW_ENV_VAR:
1678 case NEW_ENV_VALUE:
1679 case ENV_ESC:
1680 case ENV_USERVAR:
1681 *opt_replyp++ = ENV_ESC;
1682 break;
1684 *opt_replyp++ = c;
1686 if ((ep = vp) != NULL) {
1687 #ifdef OLD_ENVIRON
1688 if (telopt_environ == TELOPT_OLD_ENVIRON)
1689 *opt_replyp++ = old_env_value;
1690 else
1691 #endif
1692 *opt_replyp++ = NEW_ENV_VALUE;
1693 vp = NULL;
1694 } else
1695 break;
1700 opt_welldefined(ep)
1701 char *ep;
1703 if ((strcmp(ep, "USER") == 0) ||
1704 (strcmp(ep, "DISPLAY") == 0) ||
1705 (strcmp(ep, "PRINTER") == 0) ||
1706 (strcmp(ep, "SYSTEMTYPE") == 0) ||
1707 (strcmp(ep, "JOB") == 0) ||
1708 (strcmp(ep, "ACCT") == 0))
1709 return (1);
1710 return (0);
1712 void
1713 env_opt_end(emptyok)
1714 register int emptyok;
1716 register int len;
1718 len = opt_replyp - opt_reply + 2;
1719 if (emptyok || len > OPT_WRAPPER_SIZE) {
1720 *opt_replyp++ = IAC;
1721 *opt_replyp++ = SE;
1722 if (NETROOM() > len) {
1723 ring_supply_data(&netoring, opt_reply, len);
1724 printsub('>', &opt_reply[2], len - 2);
1726 else
1727 (void) printf("telnet: not enough room in buffer for "
1728 "environment option end reply\n");
1730 if (opt_reply) {
1731 free(opt_reply);
1732 opt_reply = opt_replyp = opt_replyend = NULL;
1739 telrcv()
1741 register int c;
1742 register int scc;
1743 register unsigned char *sbp;
1744 int count;
1745 int returnValue = 0;
1746 int min_room = 0;
1748 scc = 0;
1749 count = 0;
1750 while (--min_room > 2 || (min_room = TTYROOM()) > 2) {
1751 if (scc == 0) {
1752 if (count) {
1753 ring_consumed(&netiring, count);
1754 returnValue = 1;
1755 count = 0;
1757 sbp = netiring.consume;
1758 scc = ring_full_consecutive(&netiring);
1759 if (scc == 0) {
1760 /* No more data coming in */
1761 break;
1765 c = *sbp++ & 0xff, scc--; count++;
1767 if (decrypt_input)
1768 c = (*decrypt_input)(c);
1770 switch (telrcv_state) {
1772 case TS_CR:
1773 telrcv_state = TS_DATA;
1774 if (c == '\0') {
1775 break; /* Ignore \0 after CR */
1776 } else if ((c == '\n') &&
1777 my_want_state_is_dont(TELOPT_ECHO) && !crmod) {
1778 TTYADD(c);
1779 break;
1781 /* FALLTHROUGH */
1783 case TS_DATA:
1784 if (c == IAC) {
1785 telrcv_state = TS_IAC;
1786 break;
1789 * The 'crmod' hack (see following) is needed
1790 * since we can't * set CRMOD on output only.
1791 * Machines like MULTICS like to send \r without
1792 * \n; since we must turn off CRMOD to get proper
1793 * input, the mapping is done here (sigh).
1795 if ((c == '\r') &&
1796 my_want_state_is_dont(TELOPT_BINARY)) {
1797 if (scc > 0) {
1798 c = *sbp&0xff;
1800 if (decrypt_input)
1801 c = (*decrypt_input)(c);
1803 if (c == 0) {
1804 sbp++, scc--; count++;
1805 /* a "true" CR */
1806 TTYADD('\r');
1807 } else if (my_want_state_is_dont(
1808 TELOPT_ECHO) && (c == '\n')) {
1809 sbp++, scc--; count++;
1810 TTYADD('\n');
1811 } else {
1813 if (decrypt_input)
1814 (*decrypt_input)(-1);
1816 TTYADD('\r');
1817 if (crmod) {
1818 TTYADD('\n');
1821 } else {
1822 telrcv_state = TS_CR;
1823 TTYADD('\r');
1824 if (crmod) {
1825 TTYADD('\n');
1828 } else {
1829 TTYADD(c);
1831 continue;
1833 case TS_IAC:
1834 process_iac:
1835 switch (c) {
1837 case WILL:
1838 telrcv_state = TS_WILL;
1839 continue;
1841 case WONT:
1842 telrcv_state = TS_WONT;
1843 continue;
1845 case DO:
1846 telrcv_state = TS_DO;
1847 continue;
1849 case DONT:
1850 telrcv_state = TS_DONT;
1851 continue;
1853 case DM:
1855 * We may have missed an urgent notification,
1856 * so make sure we flush whatever is in the
1857 * buffer currently.
1859 printoption("RCVD", IAC, DM);
1860 SYNCHing = 1;
1861 if (ttyflush(1) == -2) {
1862 /* This will not return. */
1863 fatal_tty_error("write");
1865 SYNCHing = stilloob();
1866 settimer(gotDM);
1867 break;
1869 case SB:
1870 SB_CLEAR();
1871 telrcv_state = TS_SB;
1872 continue;
1874 case IAC:
1875 TTYADD(IAC);
1876 break;
1878 case NOP:
1879 case GA:
1880 default:
1881 printoption("RCVD", IAC, c);
1882 break;
1884 telrcv_state = TS_DATA;
1885 continue;
1887 case TS_WILL:
1888 printoption("RCVD", WILL, c);
1889 willoption(c);
1890 telrcv_state = TS_DATA;
1891 continue;
1893 case TS_WONT:
1894 printoption("RCVD", WONT, c);
1895 wontoption(c);
1896 telrcv_state = TS_DATA;
1897 continue;
1899 case TS_DO:
1900 printoption("RCVD", DO, c);
1901 dooption(c);
1902 if (c == TELOPT_NAWS) {
1903 sendnaws();
1904 } else if (c == TELOPT_LFLOW) {
1905 localflow = 1;
1906 setcommandmode();
1907 setconnmode(0);
1909 telrcv_state = TS_DATA;
1910 continue;
1912 case TS_DONT:
1913 printoption("RCVD", DONT, c);
1914 dontoption(c);
1915 flushline = 1;
1916 setconnmode(0); /* set new tty mode (maybe) */
1917 telrcv_state = TS_DATA;
1918 continue;
1920 case TS_SB:
1921 if (c == IAC) {
1922 telrcv_state = TS_SE;
1923 } else {
1924 SB_ACCUM(c);
1926 continue;
1928 case TS_SE:
1929 if (c != SE) {
1930 if (c != IAC) {
1932 * This is an error. We only expect to get
1933 * "IAC IAC" or "IAC SE". Several things may
1934 * have happend. An IAC was not doubled, the
1935 * IAC SE was left off, or another option got
1936 * inserted into the suboption are all possibilities.
1937 * If we assume that the IAC was not doubled,
1938 * and really the IAC SE was left off, we could
1939 * get into an infinate loop here. So, instead,
1940 * we terminate the suboption, and process the
1941 * partial suboption if we can.
1943 SB_ACCUM(IAC);
1944 SB_ACCUM(c);
1945 subpointer -= 2;
1946 SB_TERM();
1948 printoption("In SUBOPTION processing, "
1949 "RCVD", IAC, c);
1950 suboption(); /* handle sub-option */
1951 telrcv_state = TS_IAC;
1952 goto process_iac;
1954 SB_ACCUM(c);
1955 telrcv_state = TS_SB;
1956 } else {
1957 SB_ACCUM(IAC);
1958 SB_ACCUM(SE);
1959 subpointer -= 2;
1960 SB_TERM();
1961 suboption(); /* handle sub-option */
1962 telrcv_state = TS_DATA;
1966 if (count)
1967 ring_consumed(&netiring, count);
1968 return (returnValue||count);
1971 static int bol = 1, local = 0;
1974 rlogin_susp()
1976 if (local) {
1977 local = 0;
1978 bol = 1;
1979 command(0, "z\n", 2);
1980 return (1);
1982 return (0);
1985 static int
1986 telsnd()
1988 int tcc;
1989 int count;
1990 int returnValue = 0;
1991 unsigned char *tbp;
1993 tcc = 0;
1994 count = 0;
1995 while (NETROOM() > 2) {
1996 register int sc;
1997 register int c;
1999 if (tcc == 0) {
2000 if (count) {
2001 ring_consumed(&ttyiring, count);
2002 returnValue = 1;
2003 count = 0;
2005 tbp = ttyiring.consume;
2006 tcc = ring_full_consecutive(&ttyiring);
2007 if (tcc == 0) {
2008 break;
2011 c = *tbp++ & 0xff, sc = strip(c), tcc--; count++;
2012 if (rlogin != _POSIX_VDISABLE) {
2013 if (bol) {
2014 bol = 0;
2015 if (sc == rlogin) {
2016 local = 1;
2017 continue;
2019 } else if (local) {
2020 local = 0;
2021 if (sc == '.' || c == termEofChar) {
2022 bol = 1;
2023 command(0, "close\n", 6);
2024 continue;
2026 if (sc == termSuspChar) {
2027 bol = 1;
2028 command(0, "z\n", 2);
2029 continue;
2031 if (sc == escape) {
2032 command(0, (char *)tbp, tcc);
2033 bol = 1;
2034 count += tcc;
2035 tcc = 0;
2036 flushline = 1;
2037 break;
2039 if (sc != rlogin) {
2040 ++tcc;
2041 --tbp;
2042 --count;
2043 c = sc = rlogin;
2046 if ((sc == '\n') || (sc == '\r'))
2047 bol = 1;
2048 } else if (sc == escape && escape_valid) {
2050 * Double escape is a pass through of a single
2051 * escape character.
2053 if (tcc && strip(*tbp) == escape) {
2054 tbp++;
2055 tcc--;
2056 count++;
2057 bol = 0;
2058 } else {
2059 command(0, (char *)tbp, tcc);
2060 bol = 1;
2061 count += tcc;
2062 tcc = 0;
2063 flushline = 1;
2064 break;
2066 } else
2067 bol = 0;
2068 #ifdef KLUDGELINEMODE
2069 if (kludgelinemode && (globalmode&MODE_EDIT) && (sc == echoc)) {
2070 if (tcc > 0 && strip(*tbp) == echoc) {
2071 tcc--; tbp++; count++;
2072 } else {
2073 dontlecho = !dontlecho;
2074 settimer(echotoggle);
2075 setconnmode(0);
2076 flushline = 1;
2077 break;
2080 #endif
2081 if (MODE_LOCAL_CHARS(globalmode)) {
2082 if (TerminalSpecialChars(sc) == 0) {
2083 bol = 1;
2084 break;
2087 if (my_want_state_is_wont(TELOPT_BINARY)) {
2088 switch (c) {
2089 case '\n':
2091 * If we are in CRMOD mode (\r ==> \n)
2092 * on our local machine, then probably
2093 * a newline (unix) is CRLF (TELNET).
2095 if (MODE_LOCAL_CHARS(globalmode)) {
2096 NETADD('\r');
2098 NETADD('\n');
2099 bol = flushline = 1;
2100 break;
2101 case '\r':
2102 if (!crlf) {
2103 NET2ADD('\r', '\0');
2104 } else {
2105 NET2ADD('\r', '\n');
2107 bol = flushline = 1;
2108 break;
2109 case IAC:
2110 NET2ADD(IAC, IAC);
2111 break;
2112 default:
2113 NETADD(c);
2114 break;
2116 } else if (c == IAC) {
2117 NET2ADD(IAC, IAC);
2118 } else {
2119 NETADD(c);
2122 if (count)
2123 ring_consumed(&ttyiring, count);
2124 return (returnValue||count); /* Non-zero if we did anything */
2128 * Scheduler()
2130 * Try to do something.
2132 * If we do something useful, return 1; else return 0.
2138 Scheduler(block)
2139 int block; /* should we block in the select ? */
2142 * One wants to be a bit careful about setting returnValue
2143 * to one, since a one implies we did some useful work,
2144 * and therefore probably won't be called to block next
2145 * time (TN3270 mode only).
2147 int returnValue;
2148 int netin, netout, netex, ttyin, ttyout;
2150 /* Decide which rings should be processed */
2152 netout = ring_full_count(&netoring) &&
2153 (flushline ||
2154 (my_want_state_is_wont(TELOPT_LINEMODE)
2155 #ifdef KLUDGELINEMODE
2156 /* X */ && (!kludgelinemode || my_want_state_is_do(TELOPT_SGA))
2157 #endif
2158 /* XXX */) ||
2159 my_want_state_is_will(TELOPT_BINARY));
2160 ttyout = ring_full_count(&ttyoring);
2162 ttyin = (ring_empty_count(&ttyiring) && !eof_pending);
2164 netin = !ISend && ring_empty_count(&netiring);
2166 netex = !SYNCHing;
2168 if (scheduler_lockout_tty) {
2169 ttyin = ttyout = 0;
2172 /* Call to system code to process rings */
2174 returnValue = process_rings(netin, netout, netex, ttyin, ttyout,
2175 !block);
2177 /* Now, look at the input rings, looking for work to do. */
2179 if (ring_full_count(&ttyiring)) {
2180 returnValue |= telsnd();
2181 } else {
2183 * If ttyiring is empty, check to see if there is a real EOF
2184 * pending. If so, we can maybe do the EOF write now.
2186 if (eof_pending) {
2187 eof_pending = 0;
2188 sendeof();
2192 if (ring_full_count(&netiring)) {
2193 returnValue |= telrcv();
2195 return (returnValue);
2199 * Select from tty and network...
2201 void
2202 telnet(user)
2203 char *user;
2205 sys_telnet_init();
2208 static char local_host[MAXHOSTNAMELEN] = { 0 };
2210 if (!local_host[0]) {
2211 (void) gethostname(local_host, sizeof (local_host));
2212 local_host[sizeof (local_host)-1] = 0;
2214 auth_encrypt_init(local_host, hostname, "TELNET");
2215 auth_encrypt_user(user);
2218 if (autologin)
2219 send_will(TELOPT_AUTHENTICATION, 1);
2221 if (telnetport || wantencryption) {
2222 send_do(TELOPT_ENCRYPT, 1);
2223 send_will(TELOPT_ENCRYPT, 1);
2226 if (telnetport) {
2227 if (!reqd_linemode)
2228 send_do(TELOPT_SGA, 1);
2229 send_will(TELOPT_TTYPE, 1);
2230 send_will(TELOPT_NAWS, 1);
2231 send_will(TELOPT_TSPEED, 1);
2232 send_will(TELOPT_LFLOW, 1);
2233 if (!reqd_linemode)
2234 send_will(TELOPT_LINEMODE, 1);
2235 send_will(TELOPT_NEW_ENVIRON, 1);
2236 send_do(TELOPT_STATUS, 1);
2237 if (env_getvalue((unsigned char *)"DISPLAY"))
2238 send_will(TELOPT_XDISPLOC, 1);
2239 if (eight)
2240 tel_enter_binary(eight);
2244 * Note: we assume a tie to the authentication option here. This
2245 * is necessary so that authentication fails, we don't spin
2246 * forever.
2248 if (wantencryption) {
2249 boolean_t printed_encrypt = B_FALSE;
2250 extern boolean_t auth_has_failed;
2251 time_t timeout = time(0) + 60;
2253 send_do(TELOPT_ENCRYPT, 1);
2254 send_will(TELOPT_ENCRYPT, 1);
2255 for (;;) {
2256 if (my_want_state_is_wont(TELOPT_AUTHENTICATION)) {
2257 (void) printf(gettext(
2258 "\nServer refused to negotiate "
2259 "authentication, which is required\n"
2260 "for encryption. Good-bye.\n\r"));
2261 Exit(EXIT_FAILURE);
2263 if (auth_has_failed) {
2264 (void) printf(gettext(
2265 "\nAuthentication negotation has failed, "
2266 "which is required for\n"
2267 "encryption. Good-bye.\n\r"));
2268 Exit(EXIT_FAILURE);
2270 if (my_want_state_is_dont(TELOPT_ENCRYPT) ||
2271 my_want_state_is_wont(TELOPT_ENCRYPT)) {
2272 (void) printf(gettext(
2273 "\nServer refused to negotiate encryption. "
2274 "Good-bye.\n\r"));
2275 Exit(EXIT_FAILURE);
2277 if (encrypt_is_encrypting())
2278 break;
2280 if (time(0) > timeout) {
2281 (void) printf(gettext(
2282 "\nEncryption could not be enabled. "
2283 "Good-bye.\n\r"));
2284 Exit(EXIT_FAILURE);
2286 if (printed_encrypt == B_FALSE) {
2287 printed_encrypt = B_TRUE;
2288 (void) printf(gettext(
2289 "Waiting for encryption to be negotiated...\n"));
2291 * Turn on MODE_TRAPSIG and then turn off localchars
2292 * so that ^C will cause telnet to exit.
2294 TerminalNewMode(getconnmode()|MODE_TRAPSIG);
2295 intr_waiting = 1;
2297 if (intr_happened) {
2298 (void) printf(gettext(
2299 "\nUser requested an interrupt. Good-bye.\n\r"));
2300 Exit(EXIT_FAILURE);
2302 telnet_spin();
2304 if (printed_encrypt) {
2305 (void) printf(gettext("done.\n"));
2306 intr_waiting = 0;
2307 setconnmode(0);
2311 for (;;) {
2312 int schedValue;
2314 while ((schedValue = Scheduler(0)) != 0) {
2315 if (schedValue == -1) {
2316 setcommandmode();
2317 return;
2321 if (Scheduler(1) == -1) {
2322 setcommandmode();
2323 return;
2328 #if 0 /* XXX - this not being in is a bug */
2330 * nextitem()
2332 * Return the address of the next "item" in the TELNET data
2333 * stream. This will be the address of the next character if
2334 * the current address is a user data character, or it will
2335 * be the address of the character following the TELNET command
2336 * if the current address is a TELNET IAC ("I Am a Command")
2337 * character.
2340 static char *
2341 nextitem(current)
2342 char *current;
2344 if ((*current&0xff) != IAC) {
2345 return (current+1);
2347 switch (*(current+1)&0xff) {
2348 case DO:
2349 case DONT:
2350 case WILL:
2351 case WONT:
2352 return (current+3);
2353 case SB: /* loop forever looking for the SE */
2355 register char *look = current+2;
2357 for (;;) {
2358 if ((*look++&0xff) == IAC) {
2359 if ((*look++&0xff) == SE) {
2360 return (look);
2365 default:
2366 return (current+2);
2369 #endif /* 0 */
2372 * netclear()
2374 * We are about to do a TELNET SYNCH operation. Clear
2375 * the path to the network.
2377 * Things are a bit tricky since we may have sent the first
2378 * byte or so of a previous TELNET command into the network.
2379 * So, we have to scan the network buffer from the beginning
2380 * until we are up to where we want to be.
2382 * A side effect of what we do, just to keep things
2383 * simple, is to clear the urgent data pointer. The principal
2384 * caller should be setting the urgent data pointer AFTER calling
2385 * us in any case.
2388 static void
2389 netclear()
2391 #if 0 /* XXX */
2392 register char *thisitem, *next;
2393 char *good;
2394 #define wewant(p) ((nfrontp > p) && ((*p&0xff) == IAC) && \
2395 ((*(p+1)&0xff) != EC) && ((*(p+1)&0xff) != EL))
2397 thisitem = netobuf;
2399 while ((next = nextitem(thisitem)) <= netobuf.send) {
2400 thisitem = next;
2403 /* Now, thisitem is first before/at boundary. */
2405 good = netobuf; /* where the good bytes go */
2407 while (netoring.add > thisitem) {
2408 if (wewant(thisitem)) {
2409 int length;
2411 next = thisitem;
2412 do {
2413 next = nextitem(next);
2414 } while (wewant(next) && (nfrontp > next));
2415 length = next-thisitem;
2416 memcpy(good, thisitem, length);
2417 good += length;
2418 thisitem = next;
2419 } else {
2420 thisitem = nextitem(thisitem);
2424 #endif /* 0 */
2428 * These routines add various telnet commands to the data stream.
2432 * doflush - Send do timing mark (for network connection flush) & then
2433 * get rid of anything in the output buffer. Return -1 if there was a
2434 * non-EWOULDBLOCK error on the tty flush, and otherwise return 0.
2436 static int
2437 doflush()
2439 NET2ADD(IAC, DO);
2440 NETADD(TELOPT_TM);
2441 flushline = 1;
2442 flushout = 1;
2444 /* Drop pending tty output */
2445 if (ttyflush(1) == -2)
2446 return (-1);
2448 /* do printoption AFTER flush, otherwise the output gets tossed... */
2449 printoption("SENT", DO, TELOPT_TM);
2450 return (0);
2454 xmitAO()
2456 NET2ADD(IAC, AO);
2457 printoption("SENT", IAC, AO);
2458 if (autoflush) {
2459 if (doflush() == -1)
2460 return (-1);
2462 return (0);
2466 void
2467 xmitEL()
2469 NET2ADD(IAC, EL);
2470 printoption("SENT", IAC, EL);
2473 void
2474 xmitEC()
2476 NET2ADD(IAC, EC);
2477 printoption("SENT", IAC, EC);
2482 dosynch()
2484 netclear(); /* clear the path to the network */
2485 NETADD(IAC);
2486 setneturg();
2487 NETADD(DM);
2488 printoption("SENT", IAC, DM);
2489 return (1);
2492 int want_status_response = 0;
2495 get_status()
2497 unsigned char tmp[16];
2498 register unsigned char *cp;
2500 if (my_want_state_is_dont(TELOPT_STATUS)) {
2501 (void) printf("Remote side does not support STATUS option\n");
2502 return (0);
2504 cp = tmp;
2506 *cp++ = IAC;
2507 *cp++ = SB;
2508 *cp++ = TELOPT_STATUS;
2509 *cp++ = TELQUAL_SEND;
2510 *cp++ = IAC;
2511 *cp++ = SE;
2512 if (NETROOM() >= cp - tmp) {
2513 ring_supply_data(&netoring, tmp, cp-tmp);
2514 printsub('>', tmp+2, cp - tmp - 2);
2516 ++want_status_response;
2517 return (1);
2520 void
2521 intp()
2523 NET2ADD(IAC, IP);
2524 printoption("SENT", IAC, IP);
2525 flushline = 1;
2526 if (autoflush) {
2527 /* Ignore return as we're ending off anyway. */
2528 (void) doflush();
2530 if (autosynch) {
2531 (void) dosynch();
2536 sendbrk()
2538 NET2ADD(IAC, BREAK);
2539 printoption("SENT", IAC, BREAK);
2540 flushline = 1;
2541 if (autoflush) {
2542 if (doflush() == -1)
2543 return (-1);
2545 if (autosynch) {
2546 (void) dosynch();
2548 return (0);
2551 void
2552 sendabort()
2554 NET2ADD(IAC, ABORT);
2555 printoption("SENT", IAC, ABORT);
2556 flushline = 1;
2557 if (autoflush) {
2559 * Since sendabort() gets called while aborting,
2560 * ignore the doflush() return
2562 (void) doflush();
2564 if (autosynch) {
2565 (void) dosynch();
2569 void
2570 sendsusp()
2572 NET2ADD(IAC, SUSP);
2573 printoption("SENT", IAC, SUSP);
2574 flushline = 1;
2575 if (autoflush) {
2576 if (doflush() == -1) {
2577 /* The following will not return. */
2578 fatal_tty_error("write");
2581 if (autosynch) {
2582 (void) dosynch();
2586 static void
2587 sendeof()
2589 NET2ADD(IAC, xEOF);
2590 printoption("SENT", IAC, xEOF);
2594 * Send a window size update to the remote system.
2597 void
2598 sendnaws()
2600 unsigned short rows, cols;
2601 unsigned char tmp[16];
2602 register unsigned char *cp;
2604 if (my_state_is_wont(TELOPT_NAWS))
2605 return;
2607 #define PUTSHORT(cp, x) { if ((*cp++ = ((x)>>8)&0xff) == IAC) *cp++ = IAC; \
2608 if ((*cp++ = ((x))&0xff) == IAC) *cp++ = IAC; }
2610 if (TerminalWindowSize(&rows, &cols) == 0) { /* Failed */
2611 return;
2614 cp = tmp;
2616 *cp++ = IAC;
2617 *cp++ = SB;
2618 *cp++ = TELOPT_NAWS;
2619 PUTSHORT(cp, cols);
2620 PUTSHORT(cp, rows);
2621 *cp++ = IAC;
2622 *cp++ = SE;
2623 if (NETROOM() >= cp - tmp) {
2624 ring_supply_data(&netoring, tmp, cp-tmp);
2625 printsub('>', tmp+2, cp - tmp - 2);
2629 void
2630 tel_enter_binary(rw)
2631 int rw;
2633 if (rw&1)
2634 send_do(TELOPT_BINARY, 1);
2635 if (rw&2)
2636 send_will(TELOPT_BINARY, 1);
2639 void
2640 tel_leave_binary(rw)
2641 int rw;
2643 if (rw&1)
2644 send_dont(TELOPT_BINARY, 1);
2645 if (rw&2)
2646 send_wont(TELOPT_BINARY, 1);