]> git.saurik.com Git - apple/network_cmds.git/blame - telnet.tproj/telnet.c
network_cmds-201.tar.gz
[apple/network_cmds.git] / telnet.tproj / telnet.c
CommitLineData
b7080c8e
A
1/*
2 * Copyright (c) 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
7ba0088d
A
34#include <sys/cdefs.h>
35
36#ifdef __FBSDID
37__FBSDID("$FreeBSD: src/crypto/telnet/telnet/telnet.c,v 1.4.2.5 2002/04/13 10:59:08 markm Exp $");
38#endif
39
40#ifndef __unused
41#define __unused __attribute__((__unused__))
42#endif
43
b7080c8e 44#ifndef lint
7ba0088d
A
45static const char sccsid[] = "@(#)telnet.c 8.4 (Berkeley) 5/30/95";
46#endif
b7080c8e
A
47
48#include <sys/types.h>
49
b7080c8e
A
50/* By the way, we need to include curses.h before telnet.h since,
51 * among other things, telnet.h #defines 'DO', which is a variable
52 * declared in curses.h.
53 */
b7080c8e
A
54
55#include <ctype.h>
7ba0088d
A
56#include <curses.h>
57#include <signal.h>
58#include <stdlib.h>
59#include <term.h>
60#include <unistd.h>
61#include <arpa/telnet.h>
b7080c8e
A
62
63#include "ring.h"
64
65#include "defines.h"
66#include "externs.h"
67#include "types.h"
68#include "general.h"
69
7ba0088d
A
70#ifdef AUTHENTICATION
71#include <libtelnet/auth.h>
72#endif
73#ifdef ENCRYPTION
74#include <libtelnet/encrypt.h>
75#endif
76#include <libtelnet/misc.h>
b7080c8e
A
77\f
78#define strip(x) ((my_want_state_is_wont(TELOPT_BINARY)) ? ((x)&0x7f) : (x))
79
80static unsigned char subbuffer[SUBBUFSIZE],
81 *subpointer, *subend; /* buffer for sub-options */
82#define SB_CLEAR() subpointer = subbuffer;
83#define SB_TERM() { subend = subpointer; SB_CLEAR(); }
84#define SB_ACCUM(c) if (subpointer < (subbuffer+sizeof subbuffer)) { \
85 *subpointer++ = (c); \
86 }
87
88#define SB_GET() ((*subpointer++)&0xff)
89#define SB_PEEK() ((*subpointer)&0xff)
90#define SB_EOF() (subpointer >= subend)
91#define SB_LEN() (subend - subpointer)
92
93char options[256]; /* The combined options */
94char do_dont_resp[256];
95char will_wont_resp[256];
96
97int
98 eight = 0,
99 autologin = 0, /* Autologin anyone? */
100 skiprc = 0,
101 connected,
102 showoptions,
b7080c8e
A
103 ISend, /* trying to send network data in */
104 debug = 0,
105 crmod,
106 netdata, /* Print out network data flow */
107 crlf, /* Should '\r' be mapped to <CR><LF> (or <CR><NUL>)? */
b7080c8e
A
108 telnetport,
109 SYNCHing, /* we are in TELNET SYNCH mode */
110 flushout, /* flush output */
111 autoflush = 0, /* flush output when interrupting? */
112 autosynch, /* send interrupt characters with SYNCH? */
113 localflow, /* we handle flow control locally */
114 restartany, /* if flow control enabled, restart on any character */
115 localchars, /* we recognize interrupt/quit */
116 donelclchars, /* the user has set "localchars" */
117 donebinarytoggle, /* the user has put us in binary */
118 dontlecho, /* do we suppress local echoing right now? */
7ba0088d
A
119 globalmode,
120 doaddrlookup = 1, /* do a reverse address lookup? */
121 clienteof = 0;
b7080c8e
A
122
123char *prompt = 0;
7ba0088d
A
124#ifdef ENCRYPTION
125char *line; /* hack around breakage in sra.c :-( !! */
126#endif
b7080c8e
A
127
128cc_t escape;
129cc_t rlogin;
130#ifdef KLUDGELINEMODE
131cc_t echoc;
132#endif
133
134/*
135 * Telnet receiver states for fsm
136 */
137#define TS_DATA 0
138#define TS_IAC 1
139#define TS_WILL 2
140#define TS_WONT 3
141#define TS_DO 4
142#define TS_DONT 5
143#define TS_CR 6
144#define TS_SB 7 /* sub-option collection */
145#define TS_SE 8 /* looking for sub-option end */
146
147static int telrcv_state;
148#ifdef OLD_ENVIRON
149unsigned char telopt_environ = TELOPT_NEW_ENVIRON;
150#else
151# define telopt_environ TELOPT_NEW_ENVIRON
152#endif
153
7ba0088d 154jmp_buf toplevel;
b7080c8e
A
155jmp_buf peerdied;
156
157int flushline;
158int linemode;
159
160#ifdef KLUDGELINEMODE
161int kludgelinemode = 1;
162#endif
163
7ba0088d
A
164static int is_unique(char *, char **, char **);
165
b7080c8e
A
166/*
167 * The following are some clocks used to decide how to interpret
168 * the relationship between various variables.
169 */
170
171Clocks clocks;
b7080c8e
A
172\f
173/*
174 * Initialize telnet environment.
175 */
176
7ba0088d
A
177void
178init_telnet(void)
b7080c8e
A
179{
180 env_init();
181
182 SB_CLEAR();
183 ClearArray(options);
184
7ba0088d
A
185 connected = ISend = localflow = donebinarytoggle = 0;
186#ifdef AUTHENTICATION
187#ifdef ENCRYPTION
b7080c8e 188 auth_encrypt_connect(connected);
7ba0088d
A
189#endif
190#endif
b7080c8e
A
191 restartany = -1;
192
193 SYNCHing = 0;
194
195 /* Don't change NetTrace */
196
197 escape = CONTROL(']');
198 rlogin = _POSIX_VDISABLE;
199#ifdef KLUDGELINEMODE
200 echoc = CONTROL('E');
201#endif
202
203 flushline = 1;
204 telrcv_state = TS_DATA;
205}
206\f
207
b7080c8e
A
208/*
209 * These routines are in charge of sending option negotiations
210 * to the other side.
211 *
212 * The basic idea is that we send the negotiation if either side
213 * is in disagreement as to what the current state should be.
214 */
215
7ba0088d
A
216void
217send_do(int c, int init)
b7080c8e
A
218{
219 if (init) {
220 if (((do_dont_resp[c] == 0) && my_state_is_do(c)) ||
221 my_want_state_is_do(c))
222 return;
223 set_my_want_state_do(c);
224 do_dont_resp[c]++;
225 }
226 NET2ADD(IAC, DO);
227 NETADD(c);
228 printoption("SENT", DO, c);
229}
230
7ba0088d
A
231void
232send_dont(int c, int init)
b7080c8e
A
233{
234 if (init) {
235 if (((do_dont_resp[c] == 0) && my_state_is_dont(c)) ||
236 my_want_state_is_dont(c))
237 return;
238 set_my_want_state_dont(c);
239 do_dont_resp[c]++;
240 }
241 NET2ADD(IAC, DONT);
242 NETADD(c);
243 printoption("SENT", DONT, c);
244}
245
7ba0088d
A
246void
247send_will(int c, int init)
b7080c8e
A
248{
249 if (init) {
250 if (((will_wont_resp[c] == 0) && my_state_is_will(c)) ||
251 my_want_state_is_will(c))
252 return;
253 set_my_want_state_will(c);
254 will_wont_resp[c]++;
255 }
256 NET2ADD(IAC, WILL);
257 NETADD(c);
258 printoption("SENT", WILL, c);
259}
260
7ba0088d
A
261void
262send_wont(int c, int init)
b7080c8e
A
263{
264 if (init) {
265 if (((will_wont_resp[c] == 0) && my_state_is_wont(c)) ||
266 my_want_state_is_wont(c))
267 return;
268 set_my_want_state_wont(c);
269 will_wont_resp[c]++;
270 }
271 NET2ADD(IAC, WONT);
272 NETADD(c);
273 printoption("SENT", WONT, c);
274}
275
7ba0088d
A
276void
277willoption(int option)
b7080c8e
A
278{
279 int new_state_ok = 0;
280
281 if (do_dont_resp[option]) {
282 --do_dont_resp[option];
283 if (do_dont_resp[option] && my_state_is_do(option))
284 --do_dont_resp[option];
285 }
286
287 if ((do_dont_resp[option] == 0) && my_want_state_is_dont(option)) {
288
289 switch (option) {
290
291 case TELOPT_ECHO:
b7080c8e
A
292 case TELOPT_BINARY:
293 case TELOPT_SGA:
294 settimer(modenegotiated);
295 /* FALL THROUGH */
296 case TELOPT_STATUS:
7ba0088d 297#ifdef AUTHENTICATION
b7080c8e
A
298 case TELOPT_AUTHENTICATION:
299#endif
300#ifdef ENCRYPTION
301 case TELOPT_ENCRYPT:
302#endif /* ENCRYPTION */
303 new_state_ok = 1;
304 break;
305
306 case TELOPT_TM:
307 if (flushout)
308 flushout = 0;
309 /*
310 * Special case for TM. If we get back a WILL,
311 * pretend we got back a WONT.
312 */
313 set_my_want_state_dont(option);
314 set_my_state_dont(option);
315 return; /* Never reply to TM will's/wont's */
316
317 case TELOPT_LINEMODE:
318 default:
319 break;
320 }
321
322 if (new_state_ok) {
323 set_my_want_state_do(option);
324 send_do(option, 0);
325 setconnmode(0); /* possibly set new tty mode */
326 } else {
327 do_dont_resp[option]++;
328 send_dont(option, 0);
329 }
330 }
331 set_my_state_do(option);
332#ifdef ENCRYPTION
333 if (option == TELOPT_ENCRYPT)
334 encrypt_send_support();
335#endif /* ENCRYPTION */
336}
337
7ba0088d
A
338void
339wontoption(int option)
b7080c8e
A
340{
341 if (do_dont_resp[option]) {
342 --do_dont_resp[option];
343 if (do_dont_resp[option] && my_state_is_dont(option))
344 --do_dont_resp[option];
345 }
346
347 if ((do_dont_resp[option] == 0) && my_want_state_is_do(option)) {
348
349 switch (option) {
350
351#ifdef KLUDGELINEMODE
352 case TELOPT_SGA:
353 if (!kludgelinemode)
354 break;
355 /* FALL THROUGH */
356#endif
357 case TELOPT_ECHO:
358 settimer(modenegotiated);
359 break;
360
361 case TELOPT_TM:
362 if (flushout)
363 flushout = 0;
364 set_my_want_state_dont(option);
365 set_my_state_dont(option);
366 return; /* Never reply to TM will's/wont's */
367
368 default:
369 break;
370 }
371 set_my_want_state_dont(option);
372 if (my_state_is_do(option))
373 send_dont(option, 0);
374 setconnmode(0); /* Set new tty mode */
375 } else if (option == TELOPT_TM) {
376 /*
377 * Special case for TM.
378 */
379 if (flushout)
380 flushout = 0;
381 set_my_want_state_dont(option);
382 }
383 set_my_state_dont(option);
384}
385
7ba0088d
A
386static void
387dooption(int option)
b7080c8e
A
388{
389 int new_state_ok = 0;
390
391 if (will_wont_resp[option]) {
392 --will_wont_resp[option];
393 if (will_wont_resp[option] && my_state_is_will(option))
394 --will_wont_resp[option];
395 }
396
397 if (will_wont_resp[option] == 0) {
398 if (my_want_state_is_wont(option)) {
399
400 switch (option) {
401
402 case TELOPT_TM:
403 /*
404 * Special case for TM. We send a WILL, but pretend
405 * we sent WONT.
406 */
407 send_will(option, 0);
408 set_my_want_state_wont(TELOPT_TM);
409 set_my_state_wont(TELOPT_TM);
410 return;
411
b7080c8e
A
412 case TELOPT_BINARY: /* binary mode */
413 case TELOPT_NAWS: /* window size */
414 case TELOPT_TSPEED: /* terminal speed */
415 case TELOPT_LFLOW: /* local flow control */
416 case TELOPT_TTYPE: /* terminal type option */
417 case TELOPT_SGA: /* no big deal */
418#ifdef ENCRYPTION
419 case TELOPT_ENCRYPT: /* encryption variable option */
420#endif /* ENCRYPTION */
421 new_state_ok = 1;
422 break;
423
424 case TELOPT_NEW_ENVIRON: /* New environment variable option */
425#ifdef OLD_ENVIRON
426 if (my_state_is_will(TELOPT_OLD_ENVIRON))
427 send_wont(TELOPT_OLD_ENVIRON, 1); /* turn off the old */
428 goto env_common;
429 case TELOPT_OLD_ENVIRON: /* Old environment variable option */
430 if (my_state_is_will(TELOPT_NEW_ENVIRON))
431 break; /* Don't enable if new one is in use! */
432 env_common:
433 telopt_environ = option;
434#endif
435 new_state_ok = 1;
436 break;
437
7ba0088d 438#ifdef AUTHENTICATION
b7080c8e
A
439 case TELOPT_AUTHENTICATION:
440 if (autologin)
441 new_state_ok = 1;
442 break;
443#endif
444
445 case TELOPT_XDISPLOC: /* X Display location */
7ba0088d 446 if (env_getvalue("DISPLAY"))
b7080c8e
A
447 new_state_ok = 1;
448 break;
449
450 case TELOPT_LINEMODE:
451#ifdef KLUDGELINEMODE
452 kludgelinemode = 0;
453 send_do(TELOPT_SGA, 1);
454#endif
455 set_my_want_state_will(TELOPT_LINEMODE);
456 send_will(option, 0);
457 set_my_state_will(TELOPT_LINEMODE);
458 slc_init();
459 return;
460
461 case TELOPT_ECHO: /* We're never going to echo... */
462 default:
463 break;
464 }
465
466 if (new_state_ok) {
467 set_my_want_state_will(option);
468 send_will(option, 0);
469 setconnmode(0); /* Set new tty mode */
470 } else {
471 will_wont_resp[option]++;
472 send_wont(option, 0);
473 }
474 } else {
475 /*
476 * Handle options that need more things done after the
477 * other side has acknowledged the option.
478 */
479 switch (option) {
480 case TELOPT_LINEMODE:
481#ifdef KLUDGELINEMODE
482 kludgelinemode = 0;
483 send_do(TELOPT_SGA, 1);
484#endif
485 set_my_state_will(option);
486 slc_init();
487 send_do(TELOPT_SGA, 0);
488 return;
489 }
490 }
491 }
492 set_my_state_will(option);
493}
494
7ba0088d
A
495static void
496dontoption(int option)
b7080c8e
A
497{
498
499 if (will_wont_resp[option]) {
500 --will_wont_resp[option];
501 if (will_wont_resp[option] && my_state_is_wont(option))
502 --will_wont_resp[option];
503 }
504
505 if ((will_wont_resp[option] == 0) && my_want_state_is_will(option)) {
506 switch (option) {
507 case TELOPT_LINEMODE:
508 linemode = 0; /* put us back to the default state */
509 break;
510#ifdef OLD_ENVIRON
511 case TELOPT_NEW_ENVIRON:
512 /*
513 * The new environ option wasn't recognized, try
514 * the old one.
515 */
516 send_will(TELOPT_OLD_ENVIRON, 1);
517 telopt_environ = TELOPT_OLD_ENVIRON;
518 break;
519#endif
520 }
521 /* we always accept a DONT */
522 set_my_want_state_wont(option);
523 if (my_state_is_will(option))
524 send_wont(option, 0);
525 setconnmode(0); /* Set new tty mode */
526 }
527 set_my_state_wont(option);
528}
529
530/*
531 * Given a buffer returned by tgetent(), this routine will turn
7ba0088d 532 * the pipe separated list of names in the buffer into an array
b7080c8e
A
533 * of pointers to null terminated names. We toss out any bad,
534 * duplicate, or verbose names (names with spaces).
535 */
536
7ba0088d
A
537static const char *name_unknown = "UNKNOWN";
538static const char *unknown[] = { NULL, NULL };
b7080c8e 539
7ba0088d
A
540static const char **
541mklist(char *buf, char *name)
b7080c8e 542{
7ba0088d
A
543 int n;
544 char c, *cp, **argvp, *cp2, **argv, **avt;
b7080c8e
A
545
546 if (name) {
7ba0088d 547 if (strlen(name) > 40) {
b7080c8e
A
548 name = 0;
549 unknown[0] = name_unknown;
550 } else {
551 unknown[0] = name;
552 upcase(name);
553 }
554 } else
555 unknown[0] = name_unknown;
556 /*
557 * Count up the number of names.
558 */
559 for (n = 1, cp = buf; *cp && *cp != ':'; cp++) {
560 if (*cp == '|')
561 n++;
562 }
563 /*
564 * Allocate an array to put the name pointers into
565 */
566 argv = (char **)malloc((n+3)*sizeof(char *));
567 if (argv == 0)
568 return(unknown);
569
570 /*
571 * Fill up the array of pointers to names.
572 */
573 *argv = 0;
574 argvp = argv+1;
575 n = 0;
576 for (cp = cp2 = buf; (c = *cp); cp++) {
577 if (c == '|' || c == ':') {
578 *cp++ = '\0';
579 /*
580 * Skip entries that have spaces or are over 40
581 * characters long. If this is our environment
582 * name, then put it up front. Otherwise, as
583 * long as this is not a duplicate name (case
584 * insensitive) add it to the list.
585 */
586 if (n || (cp - cp2 > 41))
587 ;
588 else if (name && (strncasecmp(name, cp2, cp-cp2) == 0))
589 *argv = cp2;
590 else if (is_unique(cp2, argv+1, argvp))
591 *argvp++ = cp2;
592 if (c == ':')
593 break;
594 /*
595 * Skip multiple delimiters. Reset cp2 to
596 * the beginning of the next name. Reset n,
597 * the flag for names with spaces.
598 */
599 while ((c = *cp) == '|')
600 cp++;
601 cp2 = cp;
602 n = 0;
603 }
604 /*
605 * Skip entries with spaces or non-ascii values.
606 * Convert lower case letters to upper case.
607 */
608 if ((c == ' ') || !isascii(c))
609 n = 1;
610 else if (islower(c))
611 *cp = toupper(c);
612 }
613
614 /*
615 * Check for an old V6 2 character name. If the second
616 * name points to the beginning of the buffer, and is
617 * only 2 characters long, move it to the end of the array.
618 */
619 if ((argv[1] == buf) && (strlen(argv[1]) == 2)) {
620 --argvp;
621 for (avt = &argv[1]; avt < argvp; avt++)
622 *avt = *(avt+1);
623 *argvp++ = buf;
624 }
625
626 /*
627 * Duplicate last name, for TTYPE option, and null
628 * terminate the array. If we didn't find a match on
629 * our terminal name, put that name at the beginning.
630 */
631 cp = *(argvp-1);
632 *argvp++ = cp;
633 *argvp = 0;
634
635 if (*argv == 0) {
636 if (name)
637 *argv = name;
638 else {
639 --argvp;
640 for (avt = argv; avt < argvp; avt++)
641 *avt = *(avt+1);
642 }
643 }
644 if (*argv)
7ba0088d 645 return((const char **)argv);
b7080c8e
A
646 else
647 return(unknown);
648}
649
7ba0088d
A
650static int
651is_unique(char *name, char **as, char **ae)
b7080c8e 652{
7ba0088d
A
653 char **ap;
654 int n;
b7080c8e
A
655
656 n = strlen(name) + 1;
657 for (ap = as; ap < ae; ap++)
658 if (strncasecmp(*ap, name, n) == 0)
659 return(0);
660 return (1);
661}
662
663#ifdef TERMCAP
664char termbuf[1024];
665
7ba0088d
A
666/*ARGSUSED*/
667static int
668setupterm(char *tname, int fd, int *errp)
b7080c8e
A
669{
670 if (tgetent(termbuf, tname) == 1) {
671 termbuf[1023] = '\0';
672 if (errp)
673 *errp = 1;
674 return(0);
675 }
676 if (errp)
677 *errp = 0;
678 return(-1);
679}
680#else
681#define termbuf ttytype
682extern char ttytype[];
683#endif
684
685int resettermname = 1;
686
7ba0088d
A
687static const char *
688gettermname(void)
b7080c8e
A
689{
690 char *tname;
7ba0088d
A
691 static const char **tnamep = 0;
692 static const char **next;
b7080c8e
A
693 int err;
694
695 if (resettermname) {
696 resettermname = 0;
697 if (tnamep && tnamep != unknown)
698 free(tnamep);
7ba0088d 699 if ((tname = env_getvalue("TERM")) &&
b7080c8e
A
700 (setupterm(tname, 1, &err) == 0)) {
701 tnamep = mklist(termbuf, tname);
702 } else {
7ba0088d 703 if (tname && (strlen(tname) <= 40)) {
b7080c8e
A
704 unknown[0] = tname;
705 upcase(tname);
706 } else
707 unknown[0] = name_unknown;
708 tnamep = unknown;
709 }
710 next = tnamep;
711 }
712 if (*next == 0)
713 next = tnamep;
714 return(*next++);
715}
716/*
717 * suboption()
718 *
719 * Look at the sub-option buffer, and try to be helpful to the other
720 * side.
721 *
722 * Currently we recognize:
723 *
724 * Terminal type, send request.
725 * Terminal speed (send request).
726 * Local flow control (is request).
727 * Linemode
728 */
729
7ba0088d
A
730static void
731suboption(void)
b7080c8e
A
732{
733 unsigned char subchar;
734
735 printsub('<', subbuffer, SB_LEN()+2);
736 switch (subchar = SB_GET()) {
737 case TELOPT_TTYPE:
738 if (my_want_state_is_wont(TELOPT_TTYPE))
739 return;
740 if (SB_EOF() || SB_GET() != TELQUAL_SEND) {
741 return;
742 } else {
7ba0088d 743 const char *name;
b7080c8e
A
744 unsigned char temp[50];
745 int len;
746
b7080c8e
A
747 name = gettermname();
748 len = strlen(name) + 4 + 2;
749 if (len < NETROOM()) {
7ba0088d 750 sprintf(temp, "%c%c%c%c%s%c%c", IAC, SB, TELOPT_TTYPE,
b7080c8e
A
751 TELQUAL_IS, name, IAC, SE);
752 ring_supply_data(&netoring, temp, len);
753 printsub('>', &temp[2], len-2);
754 } else {
755 ExitString("No room in buffer for terminal type.\n", 1);
756 /*NOTREACHED*/
757 }
758 }
759 break;
760 case TELOPT_TSPEED:
761 if (my_want_state_is_wont(TELOPT_TSPEED))
762 return;
763 if (SB_EOF())
764 return;
765 if (SB_GET() == TELQUAL_SEND) {
766 long ospeed, ispeed;
767 unsigned char temp[50];
768 int len;
769
770 TerminalSpeeds(&ispeed, &ospeed);
771
7ba0088d 772 sprintf((char *)temp, "%c%c%c%c%ld,%ld%c%c", IAC, SB, TELOPT_TSPEED,
b7080c8e
A
773 TELQUAL_IS, ospeed, ispeed, IAC, SE);
774 len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */
775
776 if (len < NETROOM()) {
777 ring_supply_data(&netoring, temp, len);
778 printsub('>', temp+2, len - 2);
779 }
780/*@*/ else printf("lm_will: not enough room in buffer\n");
781 }
782 break;
783 case TELOPT_LFLOW:
784 if (my_want_state_is_wont(TELOPT_LFLOW))
785 return;
786 if (SB_EOF())
787 return;
788 switch(SB_GET()) {
789 case LFLOW_RESTART_ANY:
790 restartany = 1;
791 break;
792 case LFLOW_RESTART_XON:
793 restartany = 0;
794 break;
795 case LFLOW_ON:
796 localflow = 1;
797 break;
798 case LFLOW_OFF:
799 localflow = 0;
800 break;
801 default:
802 return;
803 }
804 setcommandmode();
805 setconnmode(0);
806 break;
807
808 case TELOPT_LINEMODE:
809 if (my_want_state_is_wont(TELOPT_LINEMODE))
810 return;
811 if (SB_EOF())
812 return;
813 switch (SB_GET()) {
814 case WILL:
815 lm_will(subpointer, SB_LEN());
816 break;
817 case WONT:
818 lm_wont(subpointer, SB_LEN());
819 break;
820 case DO:
821 lm_do(subpointer, SB_LEN());
822 break;
823 case DONT:
824 lm_dont(subpointer, SB_LEN());
825 break;
826 case LM_SLC:
827 slc(subpointer, SB_LEN());
828 break;
829 case LM_MODE:
830 lm_mode(subpointer, SB_LEN(), 0);
831 break;
832 default:
833 break;
834 }
835 break;
836
837#ifdef OLD_ENVIRON
838 case TELOPT_OLD_ENVIRON:
839#endif
840 case TELOPT_NEW_ENVIRON:
841 if (SB_EOF())
842 return;
843 switch(SB_PEEK()) {
844 case TELQUAL_IS:
845 case TELQUAL_INFO:
846 if (my_want_state_is_dont(subchar))
847 return;
848 break;
849 case TELQUAL_SEND:
850 if (my_want_state_is_wont(subchar)) {
851 return;
852 }
853 break;
854 default:
855 return;
856 }
857 env_opt(subpointer, SB_LEN());
858 break;
859
860 case TELOPT_XDISPLOC:
861 if (my_want_state_is_wont(TELOPT_XDISPLOC))
862 return;
863 if (SB_EOF())
864 return;
865 if (SB_GET() == TELQUAL_SEND) {
866 unsigned char temp[50], *dp;
867 int len;
868
7ba0088d
A
869 if ((dp = env_getvalue("DISPLAY")) == NULL ||
870 strlen(dp) > sizeof(temp) - 7) {
b7080c8e
A
871 /*
872 * Something happened, we no longer have a DISPLAY
7ba0088d 873 * variable. Or it is too long. So, turn off the option.
b7080c8e
A
874 */
875 send_wont(TELOPT_XDISPLOC, 1);
876 break;
877 }
7ba0088d
A
878 snprintf(temp, sizeof(temp), "%c%c%c%c%s%c%c", IAC, SB,
879 TELOPT_XDISPLOC, TELQUAL_IS, dp, IAC, SE);
b7080c8e
A
880 len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */
881
882 if (len < NETROOM()) {
883 ring_supply_data(&netoring, temp, len);
884 printsub('>', temp+2, len - 2);
885 }
886/*@*/ else printf("lm_will: not enough room in buffer\n");
887 }
888 break;
889
7ba0088d 890#ifdef AUTHENTICATION
b7080c8e
A
891 case TELOPT_AUTHENTICATION: {
892 if (!autologin)
893 break;
894 if (SB_EOF())
895 return;
896 switch(SB_GET()) {
897 case TELQUAL_IS:
898 if (my_want_state_is_dont(TELOPT_AUTHENTICATION))
899 return;
900 auth_is(subpointer, SB_LEN());
901 break;
902 case TELQUAL_SEND:
903 if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
904 return;
905 auth_send(subpointer, SB_LEN());
906 break;
907 case TELQUAL_REPLY:
908 if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
909 return;
910 auth_reply(subpointer, SB_LEN());
911 break;
912 case TELQUAL_NAME:
913 if (my_want_state_is_dont(TELOPT_AUTHENTICATION))
914 return;
915 auth_name(subpointer, SB_LEN());
916 break;
917 }
918 }
919 break;
920#endif
921#ifdef ENCRYPTION
922 case TELOPT_ENCRYPT:
923 if (SB_EOF())
924 return;
925 switch(SB_GET()) {
926 case ENCRYPT_START:
927 if (my_want_state_is_dont(TELOPT_ENCRYPT))
928 return;
929 encrypt_start(subpointer, SB_LEN());
930 break;
931 case ENCRYPT_END:
932 if (my_want_state_is_dont(TELOPT_ENCRYPT))
933 return;
934 encrypt_end();
935 break;
936 case ENCRYPT_SUPPORT:
937 if (my_want_state_is_wont(TELOPT_ENCRYPT))
938 return;
939 encrypt_support(subpointer, SB_LEN());
940 break;
941 case ENCRYPT_REQSTART:
942 if (my_want_state_is_wont(TELOPT_ENCRYPT))
943 return;
944 encrypt_request_start(subpointer, SB_LEN());
945 break;
946 case ENCRYPT_REQEND:
947 if (my_want_state_is_wont(TELOPT_ENCRYPT))
948 return;
949 /*
950 * We can always send an REQEND so that we cannot
951 * get stuck encrypting. We should only get this
952 * if we have been able to get in the correct mode
953 * anyhow.
954 */
955 encrypt_request_end();
956 break;
957 case ENCRYPT_IS:
958 if (my_want_state_is_dont(TELOPT_ENCRYPT))
959 return;
960 encrypt_is(subpointer, SB_LEN());
961 break;
962 case ENCRYPT_REPLY:
963 if (my_want_state_is_wont(TELOPT_ENCRYPT))
964 return;
965 encrypt_reply(subpointer, SB_LEN());
966 break;
967 case ENCRYPT_ENC_KEYID:
968 if (my_want_state_is_dont(TELOPT_ENCRYPT))
969 return;
970 encrypt_enc_keyid(subpointer, SB_LEN());
971 break;
972 case ENCRYPT_DEC_KEYID:
973 if (my_want_state_is_wont(TELOPT_ENCRYPT))
974 return;
975 encrypt_dec_keyid(subpointer, SB_LEN());
976 break;
977 default:
978 break;
979 }
980 break;
981#endif /* ENCRYPTION */
982 default:
983 break;
984 }
985}
986
987static unsigned char str_lm[] = { IAC, SB, TELOPT_LINEMODE, 0, 0, IAC, SE };
988
7ba0088d
A
989void
990lm_will(unsigned char *cmd, int len)
b7080c8e
A
991{
992 if (len < 1) {
993/*@*/ printf("lm_will: no command!!!\n"); /* Should not happen... */
994 return;
995 }
996 switch(cmd[0]) {
997 case LM_FORWARDMASK: /* We shouldn't ever get this... */
998 default:
999 str_lm[3] = DONT;
1000 str_lm[4] = cmd[0];
7ba0088d 1001 if (NETROOM() > (int)sizeof(str_lm)) {
b7080c8e
A
1002 ring_supply_data(&netoring, str_lm, sizeof(str_lm));
1003 printsub('>', &str_lm[2], sizeof(str_lm)-2);
1004 }
1005/*@*/ else printf("lm_will: not enough room in buffer\n");
1006 break;
1007 }
1008}
1009
7ba0088d
A
1010void
1011lm_wont(unsigned char *cmd, int len)
b7080c8e
A
1012{
1013 if (len < 1) {
1014/*@*/ printf("lm_wont: no command!!!\n"); /* Should not happen... */
1015 return;
1016 }
1017 switch(cmd[0]) {
1018 case LM_FORWARDMASK: /* We shouldn't ever get this... */
1019 default:
1020 /* We are always DONT, so don't respond */
1021 return;
1022 }
1023}
1024
7ba0088d
A
1025void
1026lm_do(unsigned char *cmd, int len)
b7080c8e
A
1027{
1028 if (len < 1) {
1029/*@*/ printf("lm_do: no command!!!\n"); /* Should not happen... */
1030 return;
1031 }
1032 switch(cmd[0]) {
1033 case LM_FORWARDMASK:
1034 default:
1035 str_lm[3] = WONT;
1036 str_lm[4] = cmd[0];
7ba0088d 1037 if (NETROOM() > (int)sizeof(str_lm)) {
b7080c8e
A
1038 ring_supply_data(&netoring, str_lm, sizeof(str_lm));
1039 printsub('>', &str_lm[2], sizeof(str_lm)-2);
1040 }
1041/*@*/ else printf("lm_do: not enough room in buffer\n");
1042 break;
1043 }
1044}
1045
7ba0088d
A
1046void
1047lm_dont(unsigned char *cmd, int len)
b7080c8e
A
1048{
1049 if (len < 1) {
1050/*@*/ printf("lm_dont: no command!!!\n"); /* Should not happen... */
1051 return;
1052 }
1053 switch(cmd[0]) {
1054 case LM_FORWARDMASK:
1055 default:
1056 /* we are always WONT, so don't respond */
1057 break;
1058 }
1059}
1060
1061static unsigned char str_lm_mode[] = {
1062 IAC, SB, TELOPT_LINEMODE, LM_MODE, 0, IAC, SE
1063};
1064
7ba0088d
A
1065void
1066lm_mode(unsigned char *cmd, int len, int init)
b7080c8e
A
1067{
1068 if (len != 1)
1069 return;
1070 if ((linemode&MODE_MASK&~MODE_ACK) == *cmd)
1071 return;
1072 if (*cmd&MODE_ACK)
1073 return;
1074 linemode = *cmd&(MODE_MASK&~MODE_ACK);
1075 str_lm_mode[4] = linemode;
1076 if (!init)
1077 str_lm_mode[4] |= MODE_ACK;
7ba0088d 1078 if (NETROOM() > (int)sizeof(str_lm_mode)) {
b7080c8e
A
1079 ring_supply_data(&netoring, str_lm_mode, sizeof(str_lm_mode));
1080 printsub('>', &str_lm_mode[2], sizeof(str_lm_mode)-2);
1081 }
1082/*@*/ else printf("lm_mode: not enough room in buffer\n");
1083 setconnmode(0); /* set changed mode */
1084}
1085
1086\f
1087
1088/*
1089 * slc()
1090 * Handle special character suboption of LINEMODE.
1091 */
1092
1093struct spc {
1094 cc_t val;
1095 cc_t *valp;
1096 char flags; /* Current flags & level */
1097 char mylevel; /* Maximum level & flags */
1098} spc_data[NSLC+1];
1099
1100#define SLC_IMPORT 0
1101#define SLC_EXPORT 1
1102#define SLC_RVALUE 2
1103static int slc_mode = SLC_EXPORT;
1104
7ba0088d
A
1105void
1106slc_init(void)
b7080c8e 1107{
7ba0088d 1108 struct spc *spcp;
b7080c8e
A
1109
1110 localchars = 1;
1111 for (spcp = spc_data; spcp < &spc_data[NSLC+1]; spcp++) {
1112 spcp->val = 0;
1113 spcp->valp = 0;
1114 spcp->flags = spcp->mylevel = SLC_NOSUPPORT;
1115 }
1116
1117#define initfunc(func, flags) { \
1118 spcp = &spc_data[func]; \
7ba0088d 1119 if ((spcp->valp = tcval(func))) { \
b7080c8e
A
1120 spcp->val = *spcp->valp; \
1121 spcp->mylevel = SLC_VARIABLE|flags; \
1122 } else { \
1123 spcp->val = 0; \
1124 spcp->mylevel = SLC_DEFAULT; \
1125 } \
1126 }
1127
1128 initfunc(SLC_SYNCH, 0);
1129 /* No BRK */
1130 initfunc(SLC_AO, 0);
1131 initfunc(SLC_AYT, 0);
1132 /* No EOR */
1133 initfunc(SLC_ABORT, SLC_FLUSHIN|SLC_FLUSHOUT);
1134 initfunc(SLC_EOF, 0);
1135#ifndef SYSV_TERMIO
1136 initfunc(SLC_SUSP, SLC_FLUSHIN);
1137#endif
1138 initfunc(SLC_EC, 0);
1139 initfunc(SLC_EL, 0);
1140#ifndef SYSV_TERMIO
1141 initfunc(SLC_EW, 0);
1142 initfunc(SLC_RP, 0);
1143 initfunc(SLC_LNEXT, 0);
1144#endif
1145 initfunc(SLC_XON, 0);
1146 initfunc(SLC_XOFF, 0);
1147#ifdef SYSV_TERMIO
1148 spc_data[SLC_XON].mylevel = SLC_CANTCHANGE;
1149 spc_data[SLC_XOFF].mylevel = SLC_CANTCHANGE;
1150#endif
1151 initfunc(SLC_FORW1, 0);
1152#ifdef USE_TERMIO
1153 initfunc(SLC_FORW2, 0);
1154 /* No FORW2 */
1155#endif
1156
1157 initfunc(SLC_IP, SLC_FLUSHIN|SLC_FLUSHOUT);
1158#undef initfunc
1159
1160 if (slc_mode == SLC_EXPORT)
1161 slc_export();
1162 else
1163 slc_import(1);
1164
1165}
1166
7ba0088d
A
1167void
1168slcstate(void)
b7080c8e
A
1169{
1170 printf("Special characters are %s values\n",
1171 slc_mode == SLC_IMPORT ? "remote default" :
1172 slc_mode == SLC_EXPORT ? "local" :
1173 "remote");
1174}
1175
7ba0088d
A
1176void
1177slc_mode_export(void)
b7080c8e
A
1178{
1179 slc_mode = SLC_EXPORT;
1180 if (my_state_is_will(TELOPT_LINEMODE))
1181 slc_export();
1182}
1183
7ba0088d
A
1184void
1185slc_mode_import(int def)
b7080c8e
A
1186{
1187 slc_mode = def ? SLC_IMPORT : SLC_RVALUE;
1188 if (my_state_is_will(TELOPT_LINEMODE))
1189 slc_import(def);
1190}
1191
1192unsigned char slc_import_val[] = {
1193 IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_VARIABLE, 0, IAC, SE
1194};
1195unsigned char slc_import_def[] = {
1196 IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_DEFAULT, 0, IAC, SE
1197};
1198
7ba0088d
A
1199void
1200slc_import(int def)
b7080c8e 1201{
7ba0088d 1202 if (NETROOM() > (int)sizeof(slc_import_val)) {
b7080c8e
A
1203 if (def) {
1204 ring_supply_data(&netoring, slc_import_def, sizeof(slc_import_def));
1205 printsub('>', &slc_import_def[2], sizeof(slc_import_def)-2);
1206 } else {
1207 ring_supply_data(&netoring, slc_import_val, sizeof(slc_import_val));
1208 printsub('>', &slc_import_val[2], sizeof(slc_import_val)-2);
1209 }
1210 }
1211/*@*/ else printf("slc_import: not enough room\n");
1212}
1213
7ba0088d
A
1214void
1215slc_export(void)
b7080c8e 1216{
7ba0088d 1217 struct spc *spcp;
b7080c8e
A
1218
1219 TerminalDefaultChars();
1220
1221 slc_start_reply();
1222 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1223 if (spcp->mylevel != SLC_NOSUPPORT) {
1224 if (spcp->val == (cc_t)(_POSIX_VDISABLE))
1225 spcp->flags = SLC_NOSUPPORT;
1226 else
1227 spcp->flags = spcp->mylevel;
1228 if (spcp->valp)
1229 spcp->val = *spcp->valp;
1230 slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
1231 }
1232 }
1233 slc_end_reply();
1234 (void)slc_update();
1235 setconnmode(1); /* Make sure the character values are set */
1236}
1237
7ba0088d
A
1238void
1239slc(unsigned char *cp, int len)
b7080c8e 1240{
7ba0088d
A
1241 struct spc *spcp;
1242 int func,level;
b7080c8e
A
1243
1244 slc_start_reply();
1245
1246 for (; len >= 3; len -=3, cp +=3) {
1247
1248 func = cp[SLC_FUNC];
1249
1250 if (func == 0) {
1251 /*
1252 * Client side: always ignore 0 function.
1253 */
1254 continue;
1255 }
1256 if (func > NSLC) {
1257 if ((cp[SLC_FLAGS] & SLC_LEVELBITS) != SLC_NOSUPPORT)
1258 slc_add_reply(func, SLC_NOSUPPORT, 0);
1259 continue;
1260 }
1261
1262 spcp = &spc_data[func];
1263
1264 level = cp[SLC_FLAGS]&(SLC_LEVELBITS|SLC_ACK);
1265
1266 if ((cp[SLC_VALUE] == (unsigned char)spcp->val) &&
1267 ((level&SLC_LEVELBITS) == (spcp->flags&SLC_LEVELBITS))) {
1268 continue;
1269 }
1270
1271 if (level == (SLC_DEFAULT|SLC_ACK)) {
1272 /*
1273 * This is an error condition, the SLC_ACK
1274 * bit should never be set for the SLC_DEFAULT
1275 * level. Our best guess to recover is to
1276 * ignore the SLC_ACK bit.
1277 */
1278 cp[SLC_FLAGS] &= ~SLC_ACK;
1279 }
1280
1281 if (level == ((spcp->flags&SLC_LEVELBITS)|SLC_ACK)) {
1282 spcp->val = (cc_t)cp[SLC_VALUE];
1283 spcp->flags = cp[SLC_FLAGS]; /* include SLC_ACK */
1284 continue;
1285 }
1286
1287 level &= ~SLC_ACK;
1288
1289 if (level <= (spcp->mylevel&SLC_LEVELBITS)) {
1290 spcp->flags = cp[SLC_FLAGS]|SLC_ACK;
1291 spcp->val = (cc_t)cp[SLC_VALUE];
1292 }
1293 if (level == SLC_DEFAULT) {
1294 if ((spcp->mylevel&SLC_LEVELBITS) != SLC_DEFAULT)
1295 spcp->flags = spcp->mylevel;
1296 else
1297 spcp->flags = SLC_NOSUPPORT;
1298 }
1299 slc_add_reply(func, spcp->flags, spcp->val);
1300 }
1301 slc_end_reply();
1302 if (slc_update())
1303 setconnmode(1); /* set the new character values */
1304}
1305
7ba0088d
A
1306void
1307slc_check(void)
b7080c8e 1308{
7ba0088d 1309 struct spc *spcp;
b7080c8e
A
1310
1311 slc_start_reply();
1312 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1313 if (spcp->valp && spcp->val != *spcp->valp) {
1314 spcp->val = *spcp->valp;
1315 if (spcp->val == (cc_t)(_POSIX_VDISABLE))
1316 spcp->flags = SLC_NOSUPPORT;
1317 else
1318 spcp->flags = spcp->mylevel;
1319 slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
1320 }
1321 }
1322 slc_end_reply();
1323 setconnmode(1);
1324}
1325
b7080c8e
A
1326unsigned char slc_reply[128];
1327unsigned char *slc_replyp;
1328
7ba0088d
A
1329void
1330slc_start_reply(void)
b7080c8e
A
1331{
1332 slc_replyp = slc_reply;
1333 *slc_replyp++ = IAC;
1334 *slc_replyp++ = SB;
1335 *slc_replyp++ = TELOPT_LINEMODE;
1336 *slc_replyp++ = LM_SLC;
1337}
1338
7ba0088d
A
1339void
1340slc_add_reply(unsigned char func, unsigned char flags, cc_t value)
b7080c8e
A
1341{
1342 if ((*slc_replyp++ = func) == IAC)
1343 *slc_replyp++ = IAC;
1344 if ((*slc_replyp++ = flags) == IAC)
1345 *slc_replyp++ = IAC;
1346 if ((*slc_replyp++ = (unsigned char)value) == IAC)
1347 *slc_replyp++ = IAC;
1348}
1349
7ba0088d
A
1350void
1351slc_end_reply(void)
b7080c8e 1352{
7ba0088d 1353 int len;
b7080c8e
A
1354
1355 *slc_replyp++ = IAC;
1356 *slc_replyp++ = SE;
1357 len = slc_replyp - slc_reply;
1358 if (len <= 6)
1359 return;
1360 if (NETROOM() > len) {
1361 ring_supply_data(&netoring, slc_reply, slc_replyp - slc_reply);
1362 printsub('>', &slc_reply[2], slc_replyp - slc_reply - 2);
1363 }
1364/*@*/else printf("slc_end_reply: not enough room\n");
1365}
1366
7ba0088d
A
1367int
1368slc_update(void)
b7080c8e 1369{
7ba0088d 1370 struct spc *spcp;
b7080c8e
A
1371 int need_update = 0;
1372
1373 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1374 if (!(spcp->flags&SLC_ACK))
1375 continue;
1376 spcp->flags &= ~SLC_ACK;
1377 if (spcp->valp && (*spcp->valp != spcp->val)) {
1378 *spcp->valp = spcp->val;
1379 need_update = 1;
1380 }
1381 }
1382 return(need_update);
1383}
1384
1385#ifdef OLD_ENVIRON
1386# ifdef ENV_HACK
1387/*
1388 * Earlier version of telnet/telnetd from the BSD code had
1389 * the definitions of VALUE and VAR reversed. To ensure
1390 * maximum interoperability, we assume that the server is
1391 * an older BSD server, until proven otherwise. The newer
1392 * BSD servers should be able to handle either definition,
1393 * so it is better to use the wrong values if we don't
1394 * know what type of server it is.
1395 */
1396int env_auto = 1;
1397int old_env_var = OLD_ENV_VAR;
1398int old_env_value = OLD_ENV_VALUE;
1399# else
1400# define old_env_var OLD_ENV_VAR
1401# define old_env_value OLD_ENV_VALUE
1402# endif
1403#endif
1404
7ba0088d
A
1405void
1406env_opt(unsigned char *buf, int len)
b7080c8e 1407{
7ba0088d
A
1408 unsigned char *ep = 0, *epc = 0;
1409 int i;
b7080c8e
A
1410
1411 switch(buf[0]&0xff) {
1412 case TELQUAL_SEND:
1413 env_opt_start();
1414 if (len == 1) {
1415 env_opt_add(NULL);
1416 } else for (i = 1; i < len; i++) {
1417 switch (buf[i]&0xff) {
1418#ifdef OLD_ENVIRON
1419 case OLD_ENV_VAR:
1420# ifdef ENV_HACK
1421 if (telopt_environ == TELOPT_OLD_ENVIRON
1422 && env_auto) {
1423 /* Server has the same definitions */
1424 old_env_var = OLD_ENV_VAR;
1425 old_env_value = OLD_ENV_VALUE;
1426 }
1427 /* FALL THROUGH */
1428# endif
1429 case OLD_ENV_VALUE:
1430 /*
1431 * Although OLD_ENV_VALUE is not legal, we will
1432 * still recognize it, just in case it is an
1433 * old server that has VAR & VALUE mixed up...
1434 */
1435 /* FALL THROUGH */
1436#else
1437 case NEW_ENV_VAR:
1438#endif
1439 case ENV_USERVAR:
1440 if (ep) {
1441 *epc = 0;
1442 env_opt_add(ep);
1443 }
1444 ep = epc = &buf[i+1];
1445 break;
1446 case ENV_ESC:
1447 i++;
1448 /*FALL THROUGH*/
1449 default:
1450 if (epc)
1451 *epc++ = buf[i];
1452 break;
1453 }
1454 }
1455 if (ep) {
1456 *epc = 0;
1457 env_opt_add(ep);
1458 }
1459 env_opt_end(1);
1460 break;
1461
1462 case TELQUAL_IS:
1463 case TELQUAL_INFO:
1464 /* Ignore for now. We shouldn't get it anyway. */
1465 break;
1466
1467 default:
1468 break;
1469 }
1470}
1471
1472#define OPT_REPLY_SIZE 256
1473unsigned char *opt_reply;
1474unsigned char *opt_replyp;
1475unsigned char *opt_replyend;
1476
7ba0088d
A
1477void
1478env_opt_start(void)
b7080c8e
A
1479{
1480 if (opt_reply)
1481 opt_reply = (unsigned char *)realloc(opt_reply, OPT_REPLY_SIZE);
1482 else
1483 opt_reply = (unsigned char *)malloc(OPT_REPLY_SIZE);
1484 if (opt_reply == NULL) {
1485/*@*/ printf("env_opt_start: malloc()/realloc() failed!!!\n");
1486 opt_reply = opt_replyp = opt_replyend = NULL;
1487 return;
1488 }
1489 opt_replyp = opt_reply;
1490 opt_replyend = opt_reply + OPT_REPLY_SIZE;
1491 *opt_replyp++ = IAC;
1492 *opt_replyp++ = SB;
1493 *opt_replyp++ = telopt_environ;
1494 *opt_replyp++ = TELQUAL_IS;
1495}
1496
7ba0088d
A
1497void
1498env_opt_start_info(void)
b7080c8e
A
1499{
1500 env_opt_start();
1501 if (opt_replyp)
1502 opt_replyp[-1] = TELQUAL_INFO;
1503}
1504
7ba0088d
A
1505void
1506env_opt_add(unsigned char *ep)
b7080c8e 1507{
7ba0088d 1508 unsigned char *vp, c;
b7080c8e
A
1509
1510 if (opt_reply == NULL) /*XXX*/
1511 return; /*XXX*/
1512
1513 if (ep == NULL || *ep == '\0') {
1514 /* Send user defined variables first. */
1515 env_default(1, 0);
7ba0088d 1516 while ((ep = env_default(0, 0)))
b7080c8e
A
1517 env_opt_add(ep);
1518
1519 /* Now add the list of well know variables. */
1520 env_default(1, 1);
7ba0088d 1521 while ((ep = env_default(0, 1)))
b7080c8e
A
1522 env_opt_add(ep);
1523 return;
1524 }
1525 vp = env_getvalue(ep);
07f47057
A
1526 if (opt_replyp + (vp ? strlen((char *)vp) : 0) +
1527 strlen((char *)ep) + 6 > opt_replyend)
b7080c8e 1528 {
7ba0088d 1529 int len;
07f47057 1530 opt_replyend += OPT_REPLY_SIZE;
b7080c8e
A
1531 len = opt_replyend - opt_reply;
1532 opt_reply = (unsigned char *)realloc(opt_reply, len);
1533 if (opt_reply == NULL) {
1534/*@*/ printf("env_opt_add: realloc() failed!!!\n");
1535 opt_reply = opt_replyp = opt_replyend = NULL;
1536 return;
1537 }
1538 opt_replyp = opt_reply + len - (opt_replyend - opt_replyp);
1539 opt_replyend = opt_reply + len;
1540 }
1541 if (opt_welldefined(ep))
1542#ifdef OLD_ENVIRON
1543 if (telopt_environ == TELOPT_OLD_ENVIRON)
1544 *opt_replyp++ = old_env_var;
1545 else
1546#endif
1547 *opt_replyp++ = NEW_ENV_VAR;
1548 else
1549 *opt_replyp++ = ENV_USERVAR;
1550 for (;;) {
7ba0088d 1551 while ((c = *ep++)) {
b7080c8e
A
1552 switch(c&0xff) {
1553 case IAC:
1554 *opt_replyp++ = IAC;
1555 break;
1556 case NEW_ENV_VAR:
1557 case NEW_ENV_VALUE:
1558 case ENV_ESC:
1559 case ENV_USERVAR:
1560 *opt_replyp++ = ENV_ESC;
1561 break;
1562 }
1563 *opt_replyp++ = c;
1564 }
7ba0088d 1565 if ((ep = vp)) {
b7080c8e
A
1566#ifdef OLD_ENVIRON
1567 if (telopt_environ == TELOPT_OLD_ENVIRON)
1568 *opt_replyp++ = old_env_value;
1569 else
1570#endif
1571 *opt_replyp++ = NEW_ENV_VALUE;
1572 vp = NULL;
1573 } else
1574 break;
1575 }
1576}
1577
7ba0088d
A
1578int
1579opt_welldefined(const char *ep)
b7080c8e
A
1580{
1581 if ((strcmp(ep, "USER") == 0) ||
1582 (strcmp(ep, "DISPLAY") == 0) ||
1583 (strcmp(ep, "PRINTER") == 0) ||
1584 (strcmp(ep, "SYSTEMTYPE") == 0) ||
1585 (strcmp(ep, "JOB") == 0) ||
1586 (strcmp(ep, "ACCT") == 0))
1587 return(1);
1588 return(0);
1589}
7ba0088d
A
1590
1591void
1592env_opt_end(int emptyok)
b7080c8e 1593{
7ba0088d 1594 int len;
b7080c8e
A
1595
1596 len = opt_replyp - opt_reply + 2;
1597 if (emptyok || len > 6) {
1598 *opt_replyp++ = IAC;
1599 *opt_replyp++ = SE;
1600 if (NETROOM() > len) {
1601 ring_supply_data(&netoring, opt_reply, len);
1602 printsub('>', &opt_reply[2], len - 2);
1603 }
1604/*@*/ else printf("slc_end_reply: not enough room\n");
1605 }
1606 if (opt_reply) {
1607 free(opt_reply);
1608 opt_reply = opt_replyp = opt_replyend = NULL;
1609 }
1610}
1611
1612\f
1613
7ba0088d
A
1614int
1615telrcv(void)
b7080c8e 1616{
7ba0088d
A
1617 int c;
1618 int scc;
1619 unsigned char *sbp;
b7080c8e
A
1620 int count;
1621 int returnValue = 0;
1622
1623 scc = 0;
1624 count = 0;
1625 while (TTYROOM() > 2) {
1626 if (scc == 0) {
1627 if (count) {
1628 ring_consumed(&netiring, count);
1629 returnValue = 1;
1630 count = 0;
1631 }
1632 sbp = netiring.consume;
1633 scc = ring_full_consecutive(&netiring);
1634 if (scc == 0) {
1635 /* No more data coming in */
1636 break;
1637 }
1638 }
1639
1640 c = *sbp++ & 0xff, scc--; count++;
1641#ifdef ENCRYPTION
1642 if (decrypt_input)
1643 c = (*decrypt_input)(c);
1644#endif /* ENCRYPTION */
1645
1646 switch (telrcv_state) {
1647
1648 case TS_CR:
1649 telrcv_state = TS_DATA;
1650 if (c == '\0') {
1651 break; /* Ignore \0 after CR */
1652 }
1653 else if ((c == '\n') && my_want_state_is_dont(TELOPT_ECHO) && !crmod) {
1654 TTYADD(c);
1655 break;
1656 }
1657 /* Else, fall through */
1658
1659 case TS_DATA:
1660 if (c == IAC) {
1661 telrcv_state = TS_IAC;
1662 break;
1663 }
b7080c8e
A
1664 /*
1665 * The 'crmod' hack (see following) is needed
1666 * since we can't * set CRMOD on output only.
1667 * Machines like MULTICS like to send \r without
1668 * \n; since we must turn off CRMOD to get proper
1669 * input, the mapping is done here (sigh).
1670 */
1671 if ((c == '\r') && my_want_state_is_dont(TELOPT_BINARY)) {
1672 if (scc > 0) {
1673 c = *sbp&0xff;
1674#ifdef ENCRYPTION
1675 if (decrypt_input)
1676 c = (*decrypt_input)(c);
1677#endif /* ENCRYPTION */
1678 if (c == 0) {
1679 sbp++, scc--; count++;
1680 /* a "true" CR */
1681 TTYADD('\r');
1682 } else if (my_want_state_is_dont(TELOPT_ECHO) &&
1683 (c == '\n')) {
1684 sbp++, scc--; count++;
1685 TTYADD('\n');
1686 } else {
1687#ifdef ENCRYPTION
1688 if (decrypt_input)
1689 (*decrypt_input)(-1);
1690#endif /* ENCRYPTION */
1691
1692 TTYADD('\r');
1693 if (crmod) {
1694 TTYADD('\n');
1695 }
1696 }
1697 } else {
1698 telrcv_state = TS_CR;
1699 TTYADD('\r');
1700 if (crmod) {
1701 TTYADD('\n');
1702 }
1703 }
1704 } else {
1705 TTYADD(c);
1706 }
1707 continue;
1708
1709 case TS_IAC:
1710process_iac:
1711 switch (c) {
1712
1713 case WILL:
1714 telrcv_state = TS_WILL;
1715 continue;
1716
1717 case WONT:
1718 telrcv_state = TS_WONT;
1719 continue;
1720
1721 case DO:
1722 telrcv_state = TS_DO;
1723 continue;
1724
1725 case DONT:
1726 telrcv_state = TS_DONT;
1727 continue;
1728
1729 case DM:
1730 /*
1731 * We may have missed an urgent notification,
1732 * so make sure we flush whatever is in the
1733 * buffer currently.
1734 */
1735 printoption("RCVD", IAC, DM);
1736 SYNCHing = 1;
1737 (void) ttyflush(1);
1738 SYNCHing = stilloob();
1739 settimer(gotDM);
1740 break;
1741
1742 case SB:
1743 SB_CLEAR();
1744 telrcv_state = TS_SB;
1745 continue;
1746
b7080c8e 1747 case IAC:
b7080c8e 1748 TTYADD(IAC);
b7080c8e
A
1749 break;
1750
1751 case NOP:
1752 case GA:
1753 default:
1754 printoption("RCVD", IAC, c);
1755 break;
1756 }
1757 telrcv_state = TS_DATA;
1758 continue;
1759
1760 case TS_WILL:
1761 printoption("RCVD", WILL, c);
1762 willoption(c);
b7080c8e
A
1763 telrcv_state = TS_DATA;
1764 continue;
1765
1766 case TS_WONT:
1767 printoption("RCVD", WONT, c);
1768 wontoption(c);
b7080c8e
A
1769 telrcv_state = TS_DATA;
1770 continue;
1771
1772 case TS_DO:
1773 printoption("RCVD", DO, c);
1774 dooption(c);
b7080c8e
A
1775 if (c == TELOPT_NAWS) {
1776 sendnaws();
1777 } else if (c == TELOPT_LFLOW) {
1778 localflow = 1;
1779 setcommandmode();
1780 setconnmode(0);
1781 }
1782 telrcv_state = TS_DATA;
1783 continue;
1784
1785 case TS_DONT:
1786 printoption("RCVD", DONT, c);
1787 dontoption(c);
1788 flushline = 1;
1789 setconnmode(0); /* set new tty mode (maybe) */
b7080c8e
A
1790 telrcv_state = TS_DATA;
1791 continue;
1792
1793 case TS_SB:
1794 if (c == IAC) {
1795 telrcv_state = TS_SE;
1796 } else {
1797 SB_ACCUM(c);
1798 }
1799 continue;
1800
1801 case TS_SE:
1802 if (c != SE) {
1803 if (c != IAC) {
1804 /*
1805 * This is an error. We only expect to get
1806 * "IAC IAC" or "IAC SE". Several things may
1807 * have happend. An IAC was not doubled, the
1808 * IAC SE was left off, or another option got
1809 * inserted into the suboption are all possibilities.
1810 * If we assume that the IAC was not doubled,
1811 * and really the IAC SE was left off, we could
1812 * get into an infinate loop here. So, instead,
1813 * we terminate the suboption, and process the
1814 * partial suboption if we can.
1815 */
1816 SB_ACCUM(IAC);
1817 SB_ACCUM(c);
1818 subpointer -= 2;
1819 SB_TERM();
1820
1821 printoption("In SUBOPTION processing, RCVD", IAC, c);
1822 suboption(); /* handle sub-option */
b7080c8e
A
1823 telrcv_state = TS_IAC;
1824 goto process_iac;
1825 }
1826 SB_ACCUM(c);
1827 telrcv_state = TS_SB;
1828 } else {
1829 SB_ACCUM(IAC);
1830 SB_ACCUM(SE);
1831 subpointer -= 2;
1832 SB_TERM();
1833 suboption(); /* handle sub-option */
b7080c8e
A
1834 telrcv_state = TS_DATA;
1835 }
1836 }
1837 }
1838 if (count)
1839 ring_consumed(&netiring, count);
1840 return returnValue||count;
1841}
1842
1843static int bol = 1, local = 0;
1844
7ba0088d
A
1845int
1846rlogin_susp(void)
b7080c8e
A
1847{
1848 if (local) {
1849 local = 0;
1850 bol = 1;
1851 command(0, "z\n", 2);
1852 return(1);
1853 }
1854 return(0);
1855}
1856
7ba0088d
A
1857static int
1858telsnd(void)
b7080c8e
A
1859{
1860 int tcc;
1861 int count;
1862 int returnValue = 0;
1863 unsigned char *tbp;
1864
1865 tcc = 0;
1866 count = 0;
1867 while (NETROOM() > 2) {
7ba0088d
A
1868 int sc;
1869 int c;
b7080c8e
A
1870
1871 if (tcc == 0) {
1872 if (count) {
1873 ring_consumed(&ttyiring, count);
1874 returnValue = 1;
1875 count = 0;
1876 }
1877 tbp = ttyiring.consume;
1878 tcc = ring_full_consecutive(&ttyiring);
1879 if (tcc == 0) {
1880 break;
1881 }
1882 }
1883 c = *tbp++ & 0xff, sc = strip(c), tcc--; count++;
1884 if (rlogin != _POSIX_VDISABLE) {
1885 if (bol) {
1886 bol = 0;
1887 if (sc == rlogin) {
1888 local = 1;
1889 continue;
1890 }
1891 } else if (local) {
1892 local = 0;
1893 if (sc == '.' || c == termEofChar) {
1894 bol = 1;
1895 command(0, "close\n", 6);
1896 continue;
1897 }
1898 if (sc == termSuspChar) {
1899 bol = 1;
1900 command(0, "z\n", 2);
1901 continue;
1902 }
1903 if (sc == escape) {
7ba0088d 1904 command(0, tbp, tcc);
b7080c8e
A
1905 bol = 1;
1906 count += tcc;
1907 tcc = 0;
1908 flushline = 1;
1909 break;
1910 }
1911 if (sc != rlogin) {
1912 ++tcc;
1913 --tbp;
1914 --count;
1915 c = sc = rlogin;
1916 }
1917 }
1918 if ((sc == '\n') || (sc == '\r'))
1919 bol = 1;
7ba0088d 1920 } else if (escape != _POSIX_VDISABLE && sc == escape) {
b7080c8e
A
1921 /*
1922 * Double escape is a pass through of a single escape character.
1923 */
1924 if (tcc && strip(*tbp) == escape) {
1925 tbp++;
1926 tcc--;
1927 count++;
1928 bol = 0;
1929 } else {
1930 command(0, (char *)tbp, tcc);
1931 bol = 1;
1932 count += tcc;
1933 tcc = 0;
1934 flushline = 1;
1935 break;
1936 }
1937 } else
1938 bol = 0;
1939#ifdef KLUDGELINEMODE
1940 if (kludgelinemode && (globalmode&MODE_EDIT) && (sc == echoc)) {
1941 if (tcc > 0 && strip(*tbp) == echoc) {
1942 tcc--; tbp++; count++;
1943 } else {
1944 dontlecho = !dontlecho;
1945 settimer(echotoggle);
1946 setconnmode(0);
1947 flushline = 1;
1948 break;
1949 }
1950 }
1951#endif
1952 if (MODE_LOCAL_CHARS(globalmode)) {
1953 if (TerminalSpecialChars(sc) == 0) {
1954 bol = 1;
1955 break;
1956 }
1957 }
1958 if (my_want_state_is_wont(TELOPT_BINARY)) {
1959 switch (c) {
1960 case '\n':
1961 /*
1962 * If we are in CRMOD mode (\r ==> \n)
1963 * on our local machine, then probably
1964 * a newline (unix) is CRLF (TELNET).
1965 */
1966 if (MODE_LOCAL_CHARS(globalmode)) {
1967 NETADD('\r');
1968 }
1969 NETADD('\n');
1970 bol = flushline = 1;
1971 break;
1972 case '\r':
1973 if (!crlf) {
1974 NET2ADD('\r', '\0');
1975 } else {
1976 NET2ADD('\r', '\n');
1977 }
1978 bol = flushline = 1;
1979 break;
1980 case IAC:
1981 NET2ADD(IAC, IAC);
1982 break;
1983 default:
1984 NETADD(c);
1985 break;
1986 }
1987 } else if (c == IAC) {
1988 NET2ADD(IAC, IAC);
1989 } else {
1990 NETADD(c);
1991 }
1992 }
1993 if (count)
1994 ring_consumed(&ttyiring, count);
1995 return returnValue||count; /* Non-zero if we did anything */
1996}
1997\f
1998/*
1999 * Scheduler()
2000 *
2001 * Try to do something.
2002 *
2003 * If we do something useful, return 1; else return 0.
2004 *
2005 */
2006
7ba0088d
A
2007static int
2008Scheduler(int block)
b7080c8e
A
2009{
2010 /* One wants to be a bit careful about setting returnValue
2011 * to one, since a one implies we did some useful work,
2012 * and therefore probably won't be called to block next
b7080c8e
A
2013 */
2014 int returnValue;
2015 int netin, netout, netex, ttyin, ttyout;
2016
2017 /* Decide which rings should be processed */
2018
2019 netout = ring_full_count(&netoring) &&
2020 (flushline ||
2021 (my_want_state_is_wont(TELOPT_LINEMODE)
2022#ifdef KLUDGELINEMODE
2023 && (!kludgelinemode || my_want_state_is_do(TELOPT_SGA))
2024#endif
2025 ) ||
2026 my_want_state_is_will(TELOPT_BINARY));
2027 ttyout = ring_full_count(&ttyoring);
2028
7ba0088d 2029 ttyin = ring_empty_count(&ttyiring) && (clienteof == 0);
b7080c8e 2030
b7080c8e 2031 netin = !ISend && ring_empty_count(&netiring);
b7080c8e
A
2032
2033 netex = !SYNCHing;
2034
b7080c8e
A
2035 /* Call to system code to process rings */
2036
2037 returnValue = process_rings(netin, netout, netex, ttyin, ttyout, !block);
2038
2039 /* Now, look at the input rings, looking for work to do. */
2040
2041 if (ring_full_count(&ttyiring)) {
b7080c8e 2042 returnValue |= telsnd();
b7080c8e
A
2043 }
2044
2045 if (ring_full_count(&netiring)) {
b7080c8e 2046 returnValue |= telrcv();
b7080c8e
A
2047 }
2048 return returnValue;
2049}
2050\f
7ba0088d
A
2051#ifdef AUTHENTICATION
2052#define __unusedhere
2053#else
2054#define __unusedhere __unused
2055#endif
b7080c8e
A
2056/*
2057 * Select from tty and network...
2058 */
7ba0088d
A
2059void
2060telnet(char *user __unusedhere)
b7080c8e
A
2061{
2062 sys_telnet_init();
2063
7ba0088d
A
2064#ifdef AUTHENTICATION
2065#ifdef ENCRYPTION
b7080c8e
A
2066 {
2067 static char local_host[256] = { 0 };
2068
2069 if (!local_host[0]) {
2070 gethostname(local_host, sizeof(local_host));
2071 local_host[sizeof(local_host)-1] = 0;
2072 }
2073 auth_encrypt_init(local_host, hostname, "TELNET", 0);
2074 auth_encrypt_user(user);
2075 }
7ba0088d
A
2076#endif
2077#endif
b7080c8e 2078 if (telnetport) {
7ba0088d 2079#ifdef AUTHENTICATION
b7080c8e
A
2080 if (autologin)
2081 send_will(TELOPT_AUTHENTICATION, 1);
2082#endif
2083#ifdef ENCRYPTION
2084 send_do(TELOPT_ENCRYPT, 1);
2085 send_will(TELOPT_ENCRYPT, 1);
2086#endif /* ENCRYPTION */
2087 send_do(TELOPT_SGA, 1);
2088 send_will(TELOPT_TTYPE, 1);
2089 send_will(TELOPT_NAWS, 1);
2090 send_will(TELOPT_TSPEED, 1);
2091 send_will(TELOPT_LFLOW, 1);
2092 send_will(TELOPT_LINEMODE, 1);
2093 send_will(TELOPT_NEW_ENVIRON, 1);
2094 send_do(TELOPT_STATUS, 1);
7ba0088d 2095 if (env_getvalue("DISPLAY"))
b7080c8e
A
2096 send_will(TELOPT_XDISPLOC, 1);
2097 if (eight)
2098 tel_enter_binary(eight);
2099 }
b7080c8e 2100
b7080c8e
A
2101 for (;;) {
2102 int schedValue;
2103
2104 while ((schedValue = Scheduler(0)) != 0) {
2105 if (schedValue == -1) {
2106 setcommandmode();
2107 return;
2108 }
2109 }
2110
2111 if (Scheduler(1) == -1) {
2112 setcommandmode();
2113 return;
2114 }
2115 }
b7080c8e
A
2116}
2117\f
2118#if 0 /* XXX - this not being in is a bug */
2119/*
2120 * nextitem()
2121 *
2122 * Return the address of the next "item" in the TELNET data
2123 * stream. This will be the address of the next character if
2124 * the current address is a user data character, or it will
2125 * be the address of the character following the TELNET command
2126 * if the current address is a TELNET IAC ("I Am a Command")
2127 * character.
2128 */
2129
7ba0088d
A
2130static char *
2131nextitem(char *current)
b7080c8e
A
2132{
2133 if ((*current&0xff) != IAC) {
2134 return current+1;
2135 }
2136 switch (*(current+1)&0xff) {
2137 case DO:
2138 case DONT:
2139 case WILL:
2140 case WONT:
2141 return current+3;
2142 case SB: /* loop forever looking for the SE */
2143 {
7ba0088d 2144 char *look = current+2;
b7080c8e
A
2145
2146 for (;;) {
2147 if ((*look++&0xff) == IAC) {
2148 if ((*look++&0xff) == SE) {
2149 return look;
2150 }
2151 }
2152 }
2153 }
2154 default:
2155 return current+2;
2156 }
2157}
2158#endif /* 0 */
2159
2160/*
2161 * netclear()
2162 *
2163 * We are about to do a TELNET SYNCH operation. Clear
2164 * the path to the network.
2165 *
2166 * Things are a bit tricky since we may have sent the first
2167 * byte or so of a previous TELNET command into the network.
2168 * So, we have to scan the network buffer from the beginning
2169 * until we are up to where we want to be.
2170 *
2171 * A side effect of what we do, just to keep things
2172 * simple, is to clear the urgent data pointer. The principal
2173 * caller should be setting the urgent data pointer AFTER calling
2174 * us in any case.
2175 */
2176
7ba0088d
A
2177static void
2178netclear(void)
b7080c8e 2179{
7ba0088d 2180 /* Deleted */
b7080c8e
A
2181}
2182\f
2183/*
2184 * These routines add various telnet commands to the data stream.
2185 */
2186
7ba0088d
A
2187static void
2188doflush(void)
b7080c8e
A
2189{
2190 NET2ADD(IAC, DO);
2191 NETADD(TELOPT_TM);
2192 flushline = 1;
2193 flushout = 1;
2194 (void) ttyflush(1); /* Flush/drop output */
2195 /* do printoption AFTER flush, otherwise the output gets tossed... */
2196 printoption("SENT", DO, TELOPT_TM);
2197}
2198
7ba0088d
A
2199void
2200xmitAO(void)
b7080c8e
A
2201{
2202 NET2ADD(IAC, AO);
2203 printoption("SENT", IAC, AO);
2204 if (autoflush) {
2205 doflush();
2206 }
2207}
2208
7ba0088d
A
2209void
2210xmitEL(void)
b7080c8e
A
2211{
2212 NET2ADD(IAC, EL);
2213 printoption("SENT", IAC, EL);
2214}
2215
7ba0088d
A
2216void
2217xmitEC(void)
b7080c8e
A
2218{
2219 NET2ADD(IAC, EC);
2220 printoption("SENT", IAC, EC);
2221}
2222
7ba0088d
A
2223int
2224dosynch(char *ch __unused)
b7080c8e
A
2225{
2226 netclear(); /* clear the path to the network */
2227 NETADD(IAC);
2228 setneturg();
2229 NETADD(DM);
2230 printoption("SENT", IAC, DM);
2231 return 1;
2232}
2233
2234int want_status_response = 0;
2235
7ba0088d
A
2236int
2237get_status(char *ch __unused)
b7080c8e
A
2238{
2239 unsigned char tmp[16];
7ba0088d 2240 unsigned char *cp;
b7080c8e
A
2241
2242 if (my_want_state_is_dont(TELOPT_STATUS)) {
2243 printf("Remote side does not support STATUS option\n");
2244 return 0;
2245 }
2246 cp = tmp;
2247
2248 *cp++ = IAC;
2249 *cp++ = SB;
2250 *cp++ = TELOPT_STATUS;
2251 *cp++ = TELQUAL_SEND;
2252 *cp++ = IAC;
2253 *cp++ = SE;
2254 if (NETROOM() >= cp - tmp) {
2255 ring_supply_data(&netoring, tmp, cp-tmp);
2256 printsub('>', tmp+2, cp - tmp - 2);
2257 }
2258 ++want_status_response;
2259 return 1;
2260}
2261
7ba0088d
A
2262void
2263intp(void)
b7080c8e
A
2264{
2265 NET2ADD(IAC, IP);
2266 printoption("SENT", IAC, IP);
2267 flushline = 1;
2268 if (autoflush) {
2269 doflush();
2270 }
2271 if (autosynch) {
7ba0088d 2272 dosynch(NULL);
b7080c8e
A
2273 }
2274}
2275
7ba0088d
A
2276void
2277sendbrk(void)
b7080c8e
A
2278{
2279 NET2ADD(IAC, BREAK);
2280 printoption("SENT", IAC, BREAK);
2281 flushline = 1;
2282 if (autoflush) {
2283 doflush();
2284 }
2285 if (autosynch) {
7ba0088d 2286 dosynch(NULL);
b7080c8e
A
2287 }
2288}
2289
7ba0088d
A
2290void
2291sendabort(void)
b7080c8e
A
2292{
2293 NET2ADD(IAC, ABORT);
2294 printoption("SENT", IAC, ABORT);
2295 flushline = 1;
2296 if (autoflush) {
2297 doflush();
2298 }
2299 if (autosynch) {
7ba0088d 2300 dosynch(NULL);
b7080c8e
A
2301 }
2302}
2303
7ba0088d
A
2304void
2305sendsusp(void)
b7080c8e
A
2306{
2307 NET2ADD(IAC, SUSP);
2308 printoption("SENT", IAC, SUSP);
2309 flushline = 1;
2310 if (autoflush) {
2311 doflush();
2312 }
2313 if (autosynch) {
7ba0088d 2314 dosynch(NULL);
b7080c8e
A
2315 }
2316}
2317
7ba0088d
A
2318void
2319sendeof(void)
b7080c8e
A
2320{
2321 NET2ADD(IAC, xEOF);
2322 printoption("SENT", IAC, xEOF);
2323}
2324
7ba0088d
A
2325void
2326sendayt(void)
b7080c8e
A
2327{
2328 NET2ADD(IAC, AYT);
2329 printoption("SENT", IAC, AYT);
2330}
2331
2332/*
2333 * Send a window size update to the remote system.
2334 */
2335
7ba0088d
A
2336void
2337sendnaws(void)
b7080c8e
A
2338{
2339 long rows, cols;
2340 unsigned char tmp[16];
7ba0088d 2341 unsigned char *cp;
b7080c8e
A
2342
2343 if (my_state_is_wont(TELOPT_NAWS))
2344 return;
2345
2346#define PUTSHORT(cp, x) { if ((*cp++ = ((x)>>8)&0xff) == IAC) *cp++ = IAC; \
2347 if ((*cp++ = ((x))&0xff) == IAC) *cp++ = IAC; }
2348
2349 if (TerminalWindowSize(&rows, &cols) == 0) { /* Failed */
2350 return;
2351 }
2352
2353 cp = tmp;
2354
2355 *cp++ = IAC;
2356 *cp++ = SB;
2357 *cp++ = TELOPT_NAWS;
2358 PUTSHORT(cp, cols);
2359 PUTSHORT(cp, rows);
2360 *cp++ = IAC;
2361 *cp++ = SE;
2362 if (NETROOM() >= cp - tmp) {
2363 ring_supply_data(&netoring, tmp, cp-tmp);
2364 printsub('>', tmp+2, cp - tmp - 2);
2365 }
2366}
2367
7ba0088d
A
2368void
2369tel_enter_binary(int rw)
b7080c8e
A
2370{
2371 if (rw&1)
2372 send_do(TELOPT_BINARY, 1);
2373 if (rw&2)
2374 send_will(TELOPT_BINARY, 1);
2375}
2376
7ba0088d
A
2377void
2378tel_leave_binary(int rw)
b7080c8e
A
2379{
2380 if (rw&1)
2381 send_dont(TELOPT_BINARY, 1);
2382 if (rw&2)
2383 send_wont(TELOPT_BINARY, 1);
2384}