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