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