]> git.saurik.com Git - apple/network_cmds.git/blame - telnet.tproj/telnet.c
network_cmds-176.4.1.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 1326unsigned char slc_reply[128];
d5fe66bd 1327unsigned char const * const slc_reply_eom = &slc_reply[sizeof(slc_reply)];
b7080c8e
A
1328unsigned char *slc_replyp;
1329
7ba0088d
A
1330void
1331slc_start_reply(void)
b7080c8e
A
1332{
1333 slc_replyp = slc_reply;
1334 *slc_replyp++ = IAC;
1335 *slc_replyp++ = SB;
1336 *slc_replyp++ = TELOPT_LINEMODE;
1337 *slc_replyp++ = LM_SLC;
1338}
1339
7ba0088d
A
1340void
1341slc_add_reply(unsigned char func, unsigned char flags, cc_t value)
b7080c8e 1342{
d5fe66bd
A
1343 /* A sequence of up to 6 bytes my be written for this member of the SLC
1344 * suboption list by this function. The end of negotiation command,
1345 * which is written by slc_end_reply(), will require 2 additional
1346 * bytes. Do not proceed unless there is sufficient space for these
1347 * items.
1348 */
1349 if (&slc_replyp[6+2] > slc_reply_eom)
1350 return;
b7080c8e
A
1351 if ((*slc_replyp++ = func) == IAC)
1352 *slc_replyp++ = IAC;
1353 if ((*slc_replyp++ = flags) == IAC)
1354 *slc_replyp++ = IAC;
1355 if ((*slc_replyp++ = (unsigned char)value) == IAC)
1356 *slc_replyp++ = IAC;
1357}
1358
7ba0088d
A
1359void
1360slc_end_reply(void)
b7080c8e 1361{
7ba0088d 1362 int len;
b7080c8e
A
1363
1364 *slc_replyp++ = IAC;
1365 *slc_replyp++ = SE;
1366 len = slc_replyp - slc_reply;
1367 if (len <= 6)
1368 return;
1369 if (NETROOM() > len) {
1370 ring_supply_data(&netoring, slc_reply, slc_replyp - slc_reply);
1371 printsub('>', &slc_reply[2], slc_replyp - slc_reply - 2);
1372 }
1373/*@*/else printf("slc_end_reply: not enough room\n");
1374}
1375
7ba0088d
A
1376int
1377slc_update(void)
b7080c8e 1378{
7ba0088d 1379 struct spc *spcp;
b7080c8e
A
1380 int need_update = 0;
1381
1382 for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1383 if (!(spcp->flags&SLC_ACK))
1384 continue;
1385 spcp->flags &= ~SLC_ACK;
1386 if (spcp->valp && (*spcp->valp != spcp->val)) {
1387 *spcp->valp = spcp->val;
1388 need_update = 1;
1389 }
1390 }
1391 return(need_update);
1392}
1393
1394#ifdef OLD_ENVIRON
1395# ifdef ENV_HACK
1396/*
1397 * Earlier version of telnet/telnetd from the BSD code had
1398 * the definitions of VALUE and VAR reversed. To ensure
1399 * maximum interoperability, we assume that the server is
1400 * an older BSD server, until proven otherwise. The newer
1401 * BSD servers should be able to handle either definition,
1402 * so it is better to use the wrong values if we don't
1403 * know what type of server it is.
1404 */
1405int env_auto = 1;
1406int old_env_var = OLD_ENV_VAR;
1407int old_env_value = OLD_ENV_VALUE;
1408# else
1409# define old_env_var OLD_ENV_VAR
1410# define old_env_value OLD_ENV_VALUE
1411# endif
1412#endif
1413
7ba0088d
A
1414void
1415env_opt(unsigned char *buf, int len)
b7080c8e 1416{
7ba0088d
A
1417 unsigned char *ep = 0, *epc = 0;
1418 int i;
b7080c8e
A
1419
1420 switch(buf[0]&0xff) {
1421 case TELQUAL_SEND:
1422 env_opt_start();
1423 if (len == 1) {
1424 env_opt_add(NULL);
1425 } else for (i = 1; i < len; i++) {
1426 switch (buf[i]&0xff) {
1427#ifdef OLD_ENVIRON
1428 case OLD_ENV_VAR:
1429# ifdef ENV_HACK
1430 if (telopt_environ == TELOPT_OLD_ENVIRON
1431 && env_auto) {
1432 /* Server has the same definitions */
1433 old_env_var = OLD_ENV_VAR;
1434 old_env_value = OLD_ENV_VALUE;
1435 }
1436 /* FALL THROUGH */
1437# endif
1438 case OLD_ENV_VALUE:
1439 /*
1440 * Although OLD_ENV_VALUE is not legal, we will
1441 * still recognize it, just in case it is an
1442 * old server that has VAR & VALUE mixed up...
1443 */
1444 /* FALL THROUGH */
1445#else
1446 case NEW_ENV_VAR:
1447#endif
1448 case ENV_USERVAR:
1449 if (ep) {
1450 *epc = 0;
1451 env_opt_add(ep);
1452 }
1453 ep = epc = &buf[i+1];
1454 break;
1455 case ENV_ESC:
1456 i++;
1457 /*FALL THROUGH*/
1458 default:
1459 if (epc)
1460 *epc++ = buf[i];
1461 break;
1462 }
1463 }
1464 if (ep) {
1465 *epc = 0;
1466 env_opt_add(ep);
1467 }
1468 env_opt_end(1);
1469 break;
1470
1471 case TELQUAL_IS:
1472 case TELQUAL_INFO:
1473 /* Ignore for now. We shouldn't get it anyway. */
1474 break;
1475
1476 default:
1477 break;
1478 }
1479}
1480
1481#define OPT_REPLY_SIZE 256
1482unsigned char *opt_reply;
1483unsigned char *opt_replyp;
1484unsigned char *opt_replyend;
1485
7ba0088d
A
1486void
1487env_opt_start(void)
b7080c8e
A
1488{
1489 if (opt_reply)
1490 opt_reply = (unsigned char *)realloc(opt_reply, OPT_REPLY_SIZE);
1491 else
1492 opt_reply = (unsigned char *)malloc(OPT_REPLY_SIZE);
1493 if (opt_reply == NULL) {
1494/*@*/ printf("env_opt_start: malloc()/realloc() failed!!!\n");
1495 opt_reply = opt_replyp = opt_replyend = NULL;
1496 return;
1497 }
1498 opt_replyp = opt_reply;
1499 opt_replyend = opt_reply + OPT_REPLY_SIZE;
1500 *opt_replyp++ = IAC;
1501 *opt_replyp++ = SB;
1502 *opt_replyp++ = telopt_environ;
1503 *opt_replyp++ = TELQUAL_IS;
1504}
1505
7ba0088d
A
1506void
1507env_opt_start_info(void)
b7080c8e
A
1508{
1509 env_opt_start();
1510 if (opt_replyp)
1511 opt_replyp[-1] = TELQUAL_INFO;
1512}
1513
7ba0088d
A
1514void
1515env_opt_add(unsigned char *ep)
b7080c8e 1516{
7ba0088d 1517 unsigned char *vp, c;
b7080c8e
A
1518
1519 if (opt_reply == NULL) /*XXX*/
1520 return; /*XXX*/
1521
1522 if (ep == NULL || *ep == '\0') {
1523 /* Send user defined variables first. */
1524 env_default(1, 0);
7ba0088d 1525 while ((ep = env_default(0, 0)))
b7080c8e
A
1526 env_opt_add(ep);
1527
1528 /* Now add the list of well know variables. */
1529 env_default(1, 1);
7ba0088d 1530 while ((ep = env_default(0, 1)))
b7080c8e
A
1531 env_opt_add(ep);
1532 return;
1533 }
1534 vp = env_getvalue(ep);
d5fe66bd
A
1535 if (opt_replyp + 2*(vp ? strlen((char *)vp) : 0) +
1536 2*strlen((char *)ep) + 6 > opt_replyend)
b7080c8e 1537 {
7ba0088d 1538 int len;
d5fe66bd 1539 opt_replyend += OPT_REPLY_SIZE + 2*strlen((char *)ep) + 2*(vp ? strlen((char *)vp) : 0);
b7080c8e
A
1540 len = opt_replyend - opt_reply;
1541 opt_reply = (unsigned char *)realloc(opt_reply, len);
1542 if (opt_reply == NULL) {
1543/*@*/ printf("env_opt_add: realloc() failed!!!\n");
1544 opt_reply = opt_replyp = opt_replyend = NULL;
1545 return;
1546 }
1547 opt_replyp = opt_reply + len - (opt_replyend - opt_replyp);
1548 opt_replyend = opt_reply + len;
1549 }
1550 if (opt_welldefined(ep))
1551#ifdef OLD_ENVIRON
1552 if (telopt_environ == TELOPT_OLD_ENVIRON)
1553 *opt_replyp++ = old_env_var;
1554 else
1555#endif
1556 *opt_replyp++ = NEW_ENV_VAR;
1557 else
1558 *opt_replyp++ = ENV_USERVAR;
1559 for (;;) {
7ba0088d 1560 while ((c = *ep++)) {
b7080c8e
A
1561 switch(c&0xff) {
1562 case IAC:
1563 *opt_replyp++ = IAC;
1564 break;
1565 case NEW_ENV_VAR:
1566 case NEW_ENV_VALUE:
1567 case ENV_ESC:
1568 case ENV_USERVAR:
1569 *opt_replyp++ = ENV_ESC;
1570 break;
1571 }
1572 *opt_replyp++ = c;
1573 }
7ba0088d 1574 if ((ep = vp)) {
b7080c8e
A
1575#ifdef OLD_ENVIRON
1576 if (telopt_environ == TELOPT_OLD_ENVIRON)
1577 *opt_replyp++ = old_env_value;
1578 else
1579#endif
1580 *opt_replyp++ = NEW_ENV_VALUE;
1581 vp = NULL;
1582 } else
1583 break;
1584 }
1585}
1586
7ba0088d
A
1587int
1588opt_welldefined(const char *ep)
b7080c8e
A
1589{
1590 if ((strcmp(ep, "USER") == 0) ||
1591 (strcmp(ep, "DISPLAY") == 0) ||
1592 (strcmp(ep, "PRINTER") == 0) ||
1593 (strcmp(ep, "SYSTEMTYPE") == 0) ||
1594 (strcmp(ep, "JOB") == 0) ||
1595 (strcmp(ep, "ACCT") == 0))
1596 return(1);
1597 return(0);
1598}
7ba0088d
A
1599
1600void
1601env_opt_end(int emptyok)
b7080c8e 1602{
7ba0088d 1603 int len;
b7080c8e
A
1604
1605 len = opt_replyp - opt_reply + 2;
1606 if (emptyok || len > 6) {
1607 *opt_replyp++ = IAC;
1608 *opt_replyp++ = SE;
1609 if (NETROOM() > len) {
1610 ring_supply_data(&netoring, opt_reply, len);
1611 printsub('>', &opt_reply[2], len - 2);
1612 }
1613/*@*/ else printf("slc_end_reply: not enough room\n");
1614 }
1615 if (opt_reply) {
1616 free(opt_reply);
1617 opt_reply = opt_replyp = opt_replyend = NULL;
1618 }
1619}
1620
1621\f
1622
7ba0088d
A
1623int
1624telrcv(void)
b7080c8e 1625{
7ba0088d
A
1626 int c;
1627 int scc;
1628 unsigned char *sbp;
b7080c8e
A
1629 int count;
1630 int returnValue = 0;
1631
1632 scc = 0;
1633 count = 0;
1634 while (TTYROOM() > 2) {
1635 if (scc == 0) {
1636 if (count) {
1637 ring_consumed(&netiring, count);
1638 returnValue = 1;
1639 count = 0;
1640 }
1641 sbp = netiring.consume;
1642 scc = ring_full_consecutive(&netiring);
1643 if (scc == 0) {
1644 /* No more data coming in */
1645 break;
1646 }
1647 }
1648
1649 c = *sbp++ & 0xff, scc--; count++;
1650#ifdef ENCRYPTION
1651 if (decrypt_input)
1652 c = (*decrypt_input)(c);
1653#endif /* ENCRYPTION */
1654
1655 switch (telrcv_state) {
1656
1657 case TS_CR:
1658 telrcv_state = TS_DATA;
1659 if (c == '\0') {
1660 break; /* Ignore \0 after CR */
1661 }
1662 else if ((c == '\n') && my_want_state_is_dont(TELOPT_ECHO) && !crmod) {
1663 TTYADD(c);
1664 break;
1665 }
1666 /* Else, fall through */
1667
1668 case TS_DATA:
1669 if (c == IAC) {
1670 telrcv_state = TS_IAC;
1671 break;
1672 }
b7080c8e
A
1673 /*
1674 * The 'crmod' hack (see following) is needed
1675 * since we can't * set CRMOD on output only.
1676 * Machines like MULTICS like to send \r without
1677 * \n; since we must turn off CRMOD to get proper
1678 * input, the mapping is done here (sigh).
1679 */
1680 if ((c == '\r') && my_want_state_is_dont(TELOPT_BINARY)) {
1681 if (scc > 0) {
1682 c = *sbp&0xff;
1683#ifdef ENCRYPTION
1684 if (decrypt_input)
1685 c = (*decrypt_input)(c);
1686#endif /* ENCRYPTION */
1687 if (c == 0) {
1688 sbp++, scc--; count++;
1689 /* a "true" CR */
1690 TTYADD('\r');
1691 } else if (my_want_state_is_dont(TELOPT_ECHO) &&
1692 (c == '\n')) {
1693 sbp++, scc--; count++;
1694 TTYADD('\n');
1695 } else {
1696#ifdef ENCRYPTION
1697 if (decrypt_input)
1698 (*decrypt_input)(-1);
1699#endif /* ENCRYPTION */
1700
1701 TTYADD('\r');
1702 if (crmod) {
1703 TTYADD('\n');
1704 }
1705 }
1706 } else {
1707 telrcv_state = TS_CR;
1708 TTYADD('\r');
1709 if (crmod) {
1710 TTYADD('\n');
1711 }
1712 }
1713 } else {
1714 TTYADD(c);
1715 }
1716 continue;
1717
1718 case TS_IAC:
1719process_iac:
1720 switch (c) {
1721
1722 case WILL:
1723 telrcv_state = TS_WILL;
1724 continue;
1725
1726 case WONT:
1727 telrcv_state = TS_WONT;
1728 continue;
1729
1730 case DO:
1731 telrcv_state = TS_DO;
1732 continue;
1733
1734 case DONT:
1735 telrcv_state = TS_DONT;
1736 continue;
1737
1738 case DM:
1739 /*
1740 * We may have missed an urgent notification,
1741 * so make sure we flush whatever is in the
1742 * buffer currently.
1743 */
1744 printoption("RCVD", IAC, DM);
1745 SYNCHing = 1;
1746 (void) ttyflush(1);
1747 SYNCHing = stilloob();
1748 settimer(gotDM);
1749 break;
1750
1751 case SB:
1752 SB_CLEAR();
1753 telrcv_state = TS_SB;
1754 continue;
1755
b7080c8e 1756 case IAC:
b7080c8e 1757 TTYADD(IAC);
b7080c8e
A
1758 break;
1759
1760 case NOP:
1761 case GA:
1762 default:
1763 printoption("RCVD", IAC, c);
1764 break;
1765 }
1766 telrcv_state = TS_DATA;
1767 continue;
1768
1769 case TS_WILL:
1770 printoption("RCVD", WILL, c);
1771 willoption(c);
b7080c8e
A
1772 telrcv_state = TS_DATA;
1773 continue;
1774
1775 case TS_WONT:
1776 printoption("RCVD", WONT, c);
1777 wontoption(c);
b7080c8e
A
1778 telrcv_state = TS_DATA;
1779 continue;
1780
1781 case TS_DO:
1782 printoption("RCVD", DO, c);
1783 dooption(c);
b7080c8e
A
1784 if (c == TELOPT_NAWS) {
1785 sendnaws();
1786 } else if (c == TELOPT_LFLOW) {
1787 localflow = 1;
1788 setcommandmode();
1789 setconnmode(0);
1790 }
1791 telrcv_state = TS_DATA;
1792 continue;
1793
1794 case TS_DONT:
1795 printoption("RCVD", DONT, c);
1796 dontoption(c);
1797 flushline = 1;
1798 setconnmode(0); /* set new tty mode (maybe) */
b7080c8e
A
1799 telrcv_state = TS_DATA;
1800 continue;
1801
1802 case TS_SB:
1803 if (c == IAC) {
1804 telrcv_state = TS_SE;
1805 } else {
1806 SB_ACCUM(c);
1807 }
1808 continue;
1809
1810 case TS_SE:
1811 if (c != SE) {
1812 if (c != IAC) {
1813 /*
1814 * This is an error. We only expect to get
1815 * "IAC IAC" or "IAC SE". Several things may
1816 * have happend. An IAC was not doubled, the
1817 * IAC SE was left off, or another option got
1818 * inserted into the suboption are all possibilities.
1819 * If we assume that the IAC was not doubled,
1820 * and really the IAC SE was left off, we could
1821 * get into an infinate loop here. So, instead,
1822 * we terminate the suboption, and process the
1823 * partial suboption if we can.
1824 */
1825 SB_ACCUM(IAC);
1826 SB_ACCUM(c);
1827 subpointer -= 2;
1828 SB_TERM();
1829
1830 printoption("In SUBOPTION processing, RCVD", IAC, c);
1831 suboption(); /* handle sub-option */
b7080c8e
A
1832 telrcv_state = TS_IAC;
1833 goto process_iac;
1834 }
1835 SB_ACCUM(c);
1836 telrcv_state = TS_SB;
1837 } else {
1838 SB_ACCUM(IAC);
1839 SB_ACCUM(SE);
1840 subpointer -= 2;
1841 SB_TERM();
1842 suboption(); /* handle sub-option */
b7080c8e
A
1843 telrcv_state = TS_DATA;
1844 }
1845 }
1846 }
1847 if (count)
1848 ring_consumed(&netiring, count);
1849 return returnValue||count;
1850}
1851
1852static int bol = 1, local = 0;
1853
7ba0088d
A
1854int
1855rlogin_susp(void)
b7080c8e
A
1856{
1857 if (local) {
1858 local = 0;
1859 bol = 1;
1860 command(0, "z\n", 2);
1861 return(1);
1862 }
1863 return(0);
1864}
1865
7ba0088d
A
1866static int
1867telsnd(void)
b7080c8e
A
1868{
1869 int tcc;
1870 int count;
1871 int returnValue = 0;
1872 unsigned char *tbp;
1873
1874 tcc = 0;
1875 count = 0;
1876 while (NETROOM() > 2) {
7ba0088d
A
1877 int sc;
1878 int c;
b7080c8e
A
1879
1880 if (tcc == 0) {
1881 if (count) {
1882 ring_consumed(&ttyiring, count);
1883 returnValue = 1;
1884 count = 0;
1885 }
1886 tbp = ttyiring.consume;
1887 tcc = ring_full_consecutive(&ttyiring);
1888 if (tcc == 0) {
1889 break;
1890 }
1891 }
1892 c = *tbp++ & 0xff, sc = strip(c), tcc--; count++;
1893 if (rlogin != _POSIX_VDISABLE) {
1894 if (bol) {
1895 bol = 0;
1896 if (sc == rlogin) {
1897 local = 1;
1898 continue;
1899 }
1900 } else if (local) {
1901 local = 0;
1902 if (sc == '.' || c == termEofChar) {
1903 bol = 1;
1904 command(0, "close\n", 6);
1905 continue;
1906 }
1907 if (sc == termSuspChar) {
1908 bol = 1;
1909 command(0, "z\n", 2);
1910 continue;
1911 }
1912 if (sc == escape) {
7ba0088d 1913 command(0, tbp, tcc);
b7080c8e
A
1914 bol = 1;
1915 count += tcc;
1916 tcc = 0;
1917 flushline = 1;
1918 break;
1919 }
1920 if (sc != rlogin) {
1921 ++tcc;
1922 --tbp;
1923 --count;
1924 c = sc = rlogin;
1925 }
1926 }
1927 if ((sc == '\n') || (sc == '\r'))
1928 bol = 1;
7ba0088d 1929 } else if (escape != _POSIX_VDISABLE && sc == escape) {
b7080c8e
A
1930 /*
1931 * Double escape is a pass through of a single escape character.
1932 */
1933 if (tcc && strip(*tbp) == escape) {
1934 tbp++;
1935 tcc--;
1936 count++;
1937 bol = 0;
1938 } else {
1939 command(0, (char *)tbp, tcc);
1940 bol = 1;
1941 count += tcc;
1942 tcc = 0;
1943 flushline = 1;
1944 break;
1945 }
1946 } else
1947 bol = 0;
1948#ifdef KLUDGELINEMODE
1949 if (kludgelinemode && (globalmode&MODE_EDIT) && (sc == echoc)) {
1950 if (tcc > 0 && strip(*tbp) == echoc) {
1951 tcc--; tbp++; count++;
1952 } else {
1953 dontlecho = !dontlecho;
1954 settimer(echotoggle);
1955 setconnmode(0);
1956 flushline = 1;
1957 break;
1958 }
1959 }
1960#endif
1961 if (MODE_LOCAL_CHARS(globalmode)) {
1962 if (TerminalSpecialChars(sc) == 0) {
1963 bol = 1;
1964 break;
1965 }
1966 }
1967 if (my_want_state_is_wont(TELOPT_BINARY)) {
1968 switch (c) {
1969 case '\n':
1970 /*
1971 * If we are in CRMOD mode (\r ==> \n)
1972 * on our local machine, then probably
1973 * a newline (unix) is CRLF (TELNET).
1974 */
1975 if (MODE_LOCAL_CHARS(globalmode)) {
1976 NETADD('\r');
1977 }
1978 NETADD('\n');
1979 bol = flushline = 1;
1980 break;
1981 case '\r':
1982 if (!crlf) {
1983 NET2ADD('\r', '\0');
1984 } else {
1985 NET2ADD('\r', '\n');
1986 }
1987 bol = flushline = 1;
1988 break;
1989 case IAC:
1990 NET2ADD(IAC, IAC);
1991 break;
1992 default:
1993 NETADD(c);
1994 break;
1995 }
1996 } else if (c == IAC) {
1997 NET2ADD(IAC, IAC);
1998 } else {
1999 NETADD(c);
2000 }
2001 }
2002 if (count)
2003 ring_consumed(&ttyiring, count);
2004 return returnValue||count; /* Non-zero if we did anything */
2005}
2006\f
2007/*
2008 * Scheduler()
2009 *
2010 * Try to do something.
2011 *
2012 * If we do something useful, return 1; else return 0.
2013 *
2014 */
2015
7ba0088d
A
2016static int
2017Scheduler(int block)
b7080c8e
A
2018{
2019 /* One wants to be a bit careful about setting returnValue
2020 * to one, since a one implies we did some useful work,
2021 * and therefore probably won't be called to block next
b7080c8e
A
2022 */
2023 int returnValue;
2024 int netin, netout, netex, ttyin, ttyout;
2025
2026 /* Decide which rings should be processed */
2027
2028 netout = ring_full_count(&netoring) &&
2029 (flushline ||
2030 (my_want_state_is_wont(TELOPT_LINEMODE)
2031#ifdef KLUDGELINEMODE
2032 && (!kludgelinemode || my_want_state_is_do(TELOPT_SGA))
2033#endif
2034 ) ||
2035 my_want_state_is_will(TELOPT_BINARY));
2036 ttyout = ring_full_count(&ttyoring);
2037
7ba0088d 2038 ttyin = ring_empty_count(&ttyiring) && (clienteof == 0);
b7080c8e 2039
b7080c8e 2040 netin = !ISend && ring_empty_count(&netiring);
b7080c8e
A
2041
2042 netex = !SYNCHing;
2043
b7080c8e
A
2044 /* Call to system code to process rings */
2045
2046 returnValue = process_rings(netin, netout, netex, ttyin, ttyout, !block);
2047
2048 /* Now, look at the input rings, looking for work to do. */
2049
2050 if (ring_full_count(&ttyiring)) {
b7080c8e 2051 returnValue |= telsnd();
b7080c8e
A
2052 }
2053
2054 if (ring_full_count(&netiring)) {
b7080c8e 2055 returnValue |= telrcv();
b7080c8e
A
2056 }
2057 return returnValue;
2058}
2059\f
7ba0088d
A
2060#ifdef AUTHENTICATION
2061#define __unusedhere
2062#else
2063#define __unusedhere __unused
2064#endif
b7080c8e
A
2065/*
2066 * Select from tty and network...
2067 */
7ba0088d
A
2068void
2069telnet(char *user __unusedhere)
b7080c8e
A
2070{
2071 sys_telnet_init();
2072
7ba0088d
A
2073#ifdef AUTHENTICATION
2074#ifdef ENCRYPTION
b7080c8e
A
2075 {
2076 static char local_host[256] = { 0 };
2077
2078 if (!local_host[0]) {
2079 gethostname(local_host, sizeof(local_host));
2080 local_host[sizeof(local_host)-1] = 0;
2081 }
2082 auth_encrypt_init(local_host, hostname, "TELNET", 0);
2083 auth_encrypt_user(user);
2084 }
7ba0088d
A
2085#endif
2086#endif
b7080c8e 2087 if (telnetport) {
7ba0088d 2088#ifdef AUTHENTICATION
b7080c8e
A
2089 if (autologin)
2090 send_will(TELOPT_AUTHENTICATION, 1);
2091#endif
2092#ifdef ENCRYPTION
2093 send_do(TELOPT_ENCRYPT, 1);
2094 send_will(TELOPT_ENCRYPT, 1);
2095#endif /* ENCRYPTION */
2096 send_do(TELOPT_SGA, 1);
2097 send_will(TELOPT_TTYPE, 1);
2098 send_will(TELOPT_NAWS, 1);
2099 send_will(TELOPT_TSPEED, 1);
2100 send_will(TELOPT_LFLOW, 1);
2101 send_will(TELOPT_LINEMODE, 1);
2102 send_will(TELOPT_NEW_ENVIRON, 1);
2103 send_do(TELOPT_STATUS, 1);
7ba0088d 2104 if (env_getvalue("DISPLAY"))
b7080c8e
A
2105 send_will(TELOPT_XDISPLOC, 1);
2106 if (eight)
2107 tel_enter_binary(eight);
2108 }
b7080c8e 2109
b7080c8e
A
2110 for (;;) {
2111 int schedValue;
2112
2113 while ((schedValue = Scheduler(0)) != 0) {
2114 if (schedValue == -1) {
2115 setcommandmode();
2116 return;
2117 }
2118 }
2119
2120 if (Scheduler(1) == -1) {
2121 setcommandmode();
2122 return;
2123 }
2124 }
b7080c8e
A
2125}
2126\f
2127#if 0 /* XXX - this not being in is a bug */
2128/*
2129 * nextitem()
2130 *
2131 * Return the address of the next "item" in the TELNET data
2132 * stream. This will be the address of the next character if
2133 * the current address is a user data character, or it will
2134 * be the address of the character following the TELNET command
2135 * if the current address is a TELNET IAC ("I Am a Command")
2136 * character.
2137 */
2138
7ba0088d
A
2139static char *
2140nextitem(char *current)
b7080c8e
A
2141{
2142 if ((*current&0xff) != IAC) {
2143 return current+1;
2144 }
2145 switch (*(current+1)&0xff) {
2146 case DO:
2147 case DONT:
2148 case WILL:
2149 case WONT:
2150 return current+3;
2151 case SB: /* loop forever looking for the SE */
2152 {
7ba0088d 2153 char *look = current+2;
b7080c8e
A
2154
2155 for (;;) {
2156 if ((*look++&0xff) == IAC) {
2157 if ((*look++&0xff) == SE) {
2158 return look;
2159 }
2160 }
2161 }
2162 }
2163 default:
2164 return current+2;
2165 }
2166}
2167#endif /* 0 */
2168
2169/*
2170 * netclear()
2171 *
2172 * We are about to do a TELNET SYNCH operation. Clear
2173 * the path to the network.
2174 *
2175 * Things are a bit tricky since we may have sent the first
2176 * byte or so of a previous TELNET command into the network.
2177 * So, we have to scan the network buffer from the beginning
2178 * until we are up to where we want to be.
2179 *
2180 * A side effect of what we do, just to keep things
2181 * simple, is to clear the urgent data pointer. The principal
2182 * caller should be setting the urgent data pointer AFTER calling
2183 * us in any case.
2184 */
2185
7ba0088d
A
2186static void
2187netclear(void)
b7080c8e 2188{
7ba0088d 2189 /* Deleted */
b7080c8e
A
2190}
2191\f
2192/*
2193 * These routines add various telnet commands to the data stream.
2194 */
2195
7ba0088d
A
2196static void
2197doflush(void)
b7080c8e
A
2198{
2199 NET2ADD(IAC, DO);
2200 NETADD(TELOPT_TM);
2201 flushline = 1;
2202 flushout = 1;
2203 (void) ttyflush(1); /* Flush/drop output */
2204 /* do printoption AFTER flush, otherwise the output gets tossed... */
2205 printoption("SENT", DO, TELOPT_TM);
2206}
2207
7ba0088d
A
2208void
2209xmitAO(void)
b7080c8e
A
2210{
2211 NET2ADD(IAC, AO);
2212 printoption("SENT", IAC, AO);
2213 if (autoflush) {
2214 doflush();
2215 }
2216}
2217
7ba0088d
A
2218void
2219xmitEL(void)
b7080c8e
A
2220{
2221 NET2ADD(IAC, EL);
2222 printoption("SENT", IAC, EL);
2223}
2224
7ba0088d
A
2225void
2226xmitEC(void)
b7080c8e
A
2227{
2228 NET2ADD(IAC, EC);
2229 printoption("SENT", IAC, EC);
2230}
2231
7ba0088d
A
2232int
2233dosynch(char *ch __unused)
b7080c8e
A
2234{
2235 netclear(); /* clear the path to the network */
2236 NETADD(IAC);
2237 setneturg();
2238 NETADD(DM);
2239 printoption("SENT", IAC, DM);
2240 return 1;
2241}
2242
2243int want_status_response = 0;
2244
7ba0088d
A
2245int
2246get_status(char *ch __unused)
b7080c8e
A
2247{
2248 unsigned char tmp[16];
7ba0088d 2249 unsigned char *cp;
b7080c8e
A
2250
2251 if (my_want_state_is_dont(TELOPT_STATUS)) {
2252 printf("Remote side does not support STATUS option\n");
2253 return 0;
2254 }
2255 cp = tmp;
2256
2257 *cp++ = IAC;
2258 *cp++ = SB;
2259 *cp++ = TELOPT_STATUS;
2260 *cp++ = TELQUAL_SEND;
2261 *cp++ = IAC;
2262 *cp++ = SE;
2263 if (NETROOM() >= cp - tmp) {
2264 ring_supply_data(&netoring, tmp, cp-tmp);
2265 printsub('>', tmp+2, cp - tmp - 2);
2266 }
2267 ++want_status_response;
2268 return 1;
2269}
2270
7ba0088d
A
2271void
2272intp(void)
b7080c8e
A
2273{
2274 NET2ADD(IAC, IP);
2275 printoption("SENT", IAC, IP);
2276 flushline = 1;
2277 if (autoflush) {
2278 doflush();
2279 }
2280 if (autosynch) {
7ba0088d 2281 dosynch(NULL);
b7080c8e
A
2282 }
2283}
2284
7ba0088d
A
2285void
2286sendbrk(void)
b7080c8e
A
2287{
2288 NET2ADD(IAC, BREAK);
2289 printoption("SENT", IAC, BREAK);
2290 flushline = 1;
2291 if (autoflush) {
2292 doflush();
2293 }
2294 if (autosynch) {
7ba0088d 2295 dosynch(NULL);
b7080c8e
A
2296 }
2297}
2298
7ba0088d
A
2299void
2300sendabort(void)
b7080c8e
A
2301{
2302 NET2ADD(IAC, ABORT);
2303 printoption("SENT", IAC, ABORT);
2304 flushline = 1;
2305 if (autoflush) {
2306 doflush();
2307 }
2308 if (autosynch) {
7ba0088d 2309 dosynch(NULL);
b7080c8e
A
2310 }
2311}
2312
7ba0088d
A
2313void
2314sendsusp(void)
b7080c8e
A
2315{
2316 NET2ADD(IAC, SUSP);
2317 printoption("SENT", IAC, SUSP);
2318 flushline = 1;
2319 if (autoflush) {
2320 doflush();
2321 }
2322 if (autosynch) {
7ba0088d 2323 dosynch(NULL);
b7080c8e
A
2324 }
2325}
2326
7ba0088d
A
2327void
2328sendeof(void)
b7080c8e
A
2329{
2330 NET2ADD(IAC, xEOF);
2331 printoption("SENT", IAC, xEOF);
2332}
2333
7ba0088d
A
2334void
2335sendayt(void)
b7080c8e
A
2336{
2337 NET2ADD(IAC, AYT);
2338 printoption("SENT", IAC, AYT);
2339}
2340
2341/*
2342 * Send a window size update to the remote system.
2343 */
2344
7ba0088d
A
2345void
2346sendnaws(void)
b7080c8e
A
2347{
2348 long rows, cols;
2349 unsigned char tmp[16];
7ba0088d 2350 unsigned char *cp;
b7080c8e
A
2351
2352 if (my_state_is_wont(TELOPT_NAWS))
2353 return;
2354
2355#define PUTSHORT(cp, x) { if ((*cp++ = ((x)>>8)&0xff) == IAC) *cp++ = IAC; \
2356 if ((*cp++ = ((x))&0xff) == IAC) *cp++ = IAC; }
2357
2358 if (TerminalWindowSize(&rows, &cols) == 0) { /* Failed */
2359 return;
2360 }
2361
2362 cp = tmp;
2363
2364 *cp++ = IAC;
2365 *cp++ = SB;
2366 *cp++ = TELOPT_NAWS;
2367 PUTSHORT(cp, cols);
2368 PUTSHORT(cp, rows);
2369 *cp++ = IAC;
2370 *cp++ = SE;
2371 if (NETROOM() >= cp - tmp) {
2372 ring_supply_data(&netoring, tmp, cp-tmp);
2373 printsub('>', tmp+2, cp - tmp - 2);
2374 }
2375}
2376
7ba0088d
A
2377void
2378tel_enter_binary(int rw)
b7080c8e
A
2379{
2380 if (rw&1)
2381 send_do(TELOPT_BINARY, 1);
2382 if (rw&2)
2383 send_will(TELOPT_BINARY, 1);
2384}
2385
7ba0088d
A
2386void
2387tel_leave_binary(int rw)
b7080c8e
A
2388{
2389 if (rw&1)
2390 send_dont(TELOPT_BINARY, 1);
2391 if (rw&2)
2392 send_wont(TELOPT_BINARY, 1);
2393}