2
* Copyright (c) 1988, 1990, 1993
3
* The Regents of the University of California. All rights reserved.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
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.
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
34
/* based on @(#)telnet.c 8.1 (Berkeley) 6/6/93 */
40
#include <sys/types.h>
45
/* By the way, we need to include curses.h before telnet.h since,
46
* among other things, telnet.h #defines 'DO', which is a variable
47
* declared in curses.h.
49
#endif /* defined(unix) */
59
#include <arpa/telnet.h>
71
#include <libtelnet/auth.h>
75
#include <libtelnet/encrypt.h>
78
#if defined(AUTHENTICATION) || defined(ENCRYPTION)
79
#include <libtelnet/misc-proto.h>
80
#endif /* defined(AUTHENTICATION) || defined(ENCRYPTION) */
82
#include <k5-platform.h>
84
static int is_unique (char *, char **, char **);
87
#define strip(x) ((x)&0x7f)
89
static unsigned char subbuffer[SUBBUFSIZE],
90
*subpointer, *subend; /* buffer for sub-options */
91
#define SB_CLEAR() subpointer = subbuffer;
92
#define SB_TERM() { subend = subpointer; SB_CLEAR(); }
93
#define SB_ACCUM(c) if (subpointer < (subbuffer+sizeof subbuffer)) { \
94
*subpointer++ = (c); \
97
#define SB_GET() ((*subpointer++)&0xff)
98
#define SB_PEEK() ((*subpointer)&0xff)
99
#define SB_EOF() (subpointer >= subend)
100
#define SB_LEN() (subend - subpointer)
102
char options[256]; /* The combined options */
103
char do_dont_resp[256];
104
char will_wont_resp[256];
108
autologin = 0, /* Autologin anyone? */
112
In3270, /* Are we in 3270 mode? */
113
ISend, /* trying to send network data in */
116
netdata, /* Print out network data flow */
117
crlf, /* Should '\r' be mapped to <CR><LF> (or <CR><NUL>)? */
119
noasynchtty = 0,/* User specified "-noasynch" on command line */
120
noasynchnet = 0,/* User specified "-noasynch" on command line */
121
askedSGA = 0, /* We have talked about suppress go ahead */
122
#endif /* defined(TN3270) */
125
SYNCHing, /* we are in TELNET SYNCH mode */
126
flushout, /* flush output */
127
autoflush = 0, /* flush output when interrupting? */
128
autosynch, /* send interrupt characters with SYNCH? */
129
localflow, /* we handle flow control locally */
130
restartany, /* if flow control enabled, restart on any character */
131
localchars, /* we recognize interrupt/quit */
132
donelclchars, /* the user has set "localchars" */
133
donebinarytoggle, /* the user has put us in binary */
134
dontlecho, /* do we suppress local echoing right now? */
139
int scheduler_lockout_tty = 0;
143
#ifdef KLUDGELINEMODE
148
* Telnet receiver states for fsm
157
#define TS_SB 7 /* sub-option collection */
158
#define TS_SE 8 /* looking for sub-option end */
160
static int telrcv_state;
162
unsigned char telopt_environ = TELOPT_NEW_ENVIRON;
164
# define telopt_environ TELOPT_NEW_ENVIRON
167
jmp_buf toplevel = { 0 };
173
#ifdef KLUDGELINEMODE
174
int kludgelinemode = 1;
178
* The following are some clocks used to decide how to interpret
179
* the relationship between various variables.
185
Modelist modelist[] = {
186
{ "telnet command mode", COMMAND_LINE },
187
{ "character-at-a-time mode", 0 },
188
{ "character-at-a-time mode (local echo)", LOCAL_ECHO|LOCAL_CHARS },
189
{ "line-by-line mode (remote echo)", LINE | LOCAL_CHARS },
190
{ "line-by-line mode", LINE | LOCAL_ECHO | LOCAL_CHARS },
191
{ "line-by-line mode (local echoing suppressed)", LINE | LOCAL_CHARS },
198
* Initialize telnet environment.
209
connected = In3270 = ISend = localflow = donebinarytoggle = 0;
210
#if defined(AUTHENTICATION) || defined(ENCRYPTION)
211
auth_encrypt_connect(connected);
212
#endif /* defined(AUTHENTICATION) || defined(ENCRYPTION) */
217
/* Don't change NetTrace */
219
escape = CONTROL(']');
220
rlogin = _POSIX_VDISABLE;
221
#ifdef KLUDGELINEMODE
222
echoc = CONTROL('E');
226
telrcv_state = TS_DATA;
239
char buffer[100]; /* where things go */
248
ring = va_arg(ap, Ring *);
249
format = va_arg(ap, char *);
252
while ((i = *format++) != 0) {
257
*ptr++ = va_arg(ap, int);
260
string = va_arg(ap, char *);
261
ring_supply_data(ring, buffer, ptr-buffer);
262
ring_supply_data(ring, string, strlen(string));
266
ExitString("printring: trailing %%.\n", 1);
269
ExitString("printring: unknown format character.\n", 1);
276
ring_supply_data(ring, buffer, ptr-buffer);
281
* These routines are in charge of sending option negotiations
284
* The basic idea is that we send the negotiation if either side
285
* is in disagreement as to what the current state should be.
290
register int c, init;
293
if (((do_dont_resp[c] == 0) && my_state_is_do(c)) ||
294
my_want_state_is_do(c))
296
set_my_want_state_do(c);
301
printoption("SENT", DO, c);
306
register int c, init;
309
if (((do_dont_resp[c] == 0) && my_state_is_dont(c)) ||
310
my_want_state_is_dont(c))
312
set_my_want_state_dont(c);
317
printoption("SENT", DONT, c);
322
register int c, init;
325
if (((will_wont_resp[c] == 0) && my_state_is_will(c)) ||
326
my_want_state_is_will(c))
328
set_my_want_state_will(c);
333
printoption("SENT", WILL, c);
338
register int c, init;
341
if (((will_wont_resp[c] == 0) && my_state_is_wont(c)) ||
342
my_want_state_is_wont(c))
344
set_my_want_state_wont(c);
349
printoption("SENT", WONT, c);
357
int new_state_ok = 0;
359
if (do_dont_resp[option]) {
360
--do_dont_resp[option];
361
if (do_dont_resp[option] && my_state_is_do(option))
362
--do_dont_resp[option];
365
if ((do_dont_resp[option] == 0) && my_want_state_is_dont(option)) {
372
* The following is a pain in the rear-end.
373
* Various IBM servers (some versions of Wiscnet,
374
* possibly Fibronics/Spartacus, and who knows who
375
* else) will NOT allow us to send "DO SGA" too early
376
* in the setup proceedings. On the other hand,
377
* 4.2 servers (telnetd) won't set SGA correctly.
378
* So, we are stuck. Empirically (but, based on
379
* a VERY small sample), the IBM servers don't send
380
* out anything about ECHO, so we postpone our sending
381
* "DO SGA" until we see "WILL ECHO" (which 4.2 servers
387
if (my_want_state_is_dont(TELOPT_SGA))
388
send_do(TELOPT_SGA, 1);
393
#endif /* defined(TN3270) */
396
settimer(modenegotiated);
399
#if defined(AUTHENTICATION)
400
case TELOPT_AUTHENTICATION:
404
#endif /* ENCRYPTION */
412
* Special case for TM. If we get back a WILL,
413
* pretend we got back a WONT.
415
set_my_want_state_dont(option);
416
set_my_state_dont(option);
417
return; /* Never reply to TM will's/wont's */
419
case TELOPT_LINEMODE:
425
set_my_want_state_do(option);
427
setconnmode(0); /* possibly set new tty mode */
429
do_dont_resp[option]++;
430
send_dont(option, 0);
433
set_my_state_do(option);
435
if (option == TELOPT_ENCRYPT)
436
encrypt_send_support();
437
#endif /* ENCRYPTION */
444
if (do_dont_resp[option]) {
445
--do_dont_resp[option];
446
if (do_dont_resp[option] && my_state_is_dont(option))
447
--do_dont_resp[option];
450
if ((do_dont_resp[option] == 0) && my_want_state_is_do(option)) {
454
#ifdef KLUDGELINEMODE
461
settimer(modenegotiated);
467
set_my_want_state_dont(option);
468
set_my_state_dont(option);
469
return; /* Never reply to TM will's/wont's */
474
set_my_want_state_dont(option);
475
if (my_state_is_do(option))
476
send_dont(option, 0);
477
setconnmode(0); /* Set new tty mode */
478
} else if (option == TELOPT_TM) {
480
* Special case for TM.
484
set_my_want_state_dont(option);
486
set_my_state_dont(option);
493
int new_state_ok = 0;
495
if (will_wont_resp[option]) {
496
--will_wont_resp[option];
497
if (will_wont_resp[option] && my_state_is_will(option))
498
--will_wont_resp[option];
501
if (will_wont_resp[option] == 0) {
502
if (my_want_state_is_wont(option)) {
508
* Special case for TM. We send a WILL, but pretend
511
send_will(option, 0);
512
set_my_want_state_wont(TELOPT_TM);
513
set_my_state_wont(TELOPT_TM);
517
case TELOPT_EOR: /* end of record */
518
# endif /* defined(TN3270) */
519
case TELOPT_BINARY: /* binary mode */
520
case TELOPT_NAWS: /* window size */
521
case TELOPT_TSPEED: /* terminal speed */
522
case TELOPT_LFLOW: /* local flow control */
523
case TELOPT_TTYPE: /* terminal type option */
524
case TELOPT_SGA: /* no big deal */
526
case TELOPT_ENCRYPT: /* encryption variable option */
527
#endif /* ENCRYPTION */
531
case TELOPT_NEW_ENVIRON: /* New environment variable option */
533
if (my_state_is_will(TELOPT_OLD_ENVIRON))
534
send_wont(TELOPT_OLD_ENVIRON, 1); /* turn off the old */
536
case TELOPT_OLD_ENVIRON: /* Old environment variable option */
537
if (my_state_is_will(TELOPT_NEW_ENVIRON))
538
break; /* Don't enable if new one is in use! */
540
telopt_environ = option;
545
#if defined(AUTHENTICATION)
546
case TELOPT_AUTHENTICATION:
552
case TELOPT_XDISPLOC: /* X Display location */
553
if (env_getvalue((unsigned char *)"DISPLAY") &&
554
env_is_exported((unsigned char *)"DISPLAY"))
558
case TELOPT_LINEMODE:
559
#ifdef KLUDGELINEMODE
561
send_do(TELOPT_SGA, 1);
563
set_my_want_state_will(TELOPT_LINEMODE);
564
send_will(option, 0);
565
set_my_state_will(TELOPT_LINEMODE);
569
case TELOPT_ECHO: /* We're never going to echo... */
575
set_my_want_state_will(option);
576
send_will(option, 0);
577
setconnmode(0); /* Set new tty mode */
579
will_wont_resp[option]++;
580
send_wont(option, 0);
584
* Handle options that need more things done after the
585
* other side has acknowledged the option.
588
case TELOPT_LINEMODE:
589
#ifdef KLUDGELINEMODE
591
send_do(TELOPT_SGA, 1);
593
set_my_state_will(option);
595
send_do(TELOPT_SGA, 0);
600
set_my_state_will(option);
608
if (will_wont_resp[option]) {
609
--will_wont_resp[option];
610
if (will_wont_resp[option] && my_state_is_wont(option))
611
--will_wont_resp[option];
614
if ((will_wont_resp[option] == 0) && my_want_state_is_will(option)) {
616
case TELOPT_LINEMODE:
617
linemode = 0; /* put us back to the default state */
620
case TELOPT_NEW_ENVIRON:
622
* The new environ option wasn't recognized, try
625
send_will(TELOPT_OLD_ENVIRON, 1);
626
telopt_environ = TELOPT_OLD_ENVIRON;
630
/* we always accept a DONT */
631
set_my_want_state_wont(option);
632
if (my_state_is_will(option))
633
send_wont(option, 0);
634
setconnmode(0); /* Set new tty mode */
636
set_my_state_wont(option);
640
* Given a buffer returned by tgetent(), this routine will turn
641
* the pipe seperated list of names in the buffer into an array
642
* of pointers to null terminated names. We toss out any bad,
643
* duplicate, or verbose names (names with spaces).
646
static char *name_unknown = "UNKNOWN";
647
static char *unknown[] = { 0, 0 };
654
register char c, *cp, **argvp, *cp2, **argv, **avt;
657
if (strlen(name) > 40) {
659
unknown[0] = name_unknown;
665
unknown[0] = name_unknown;
667
* Count up the number of names.
669
for (n = 1, cp = buf; *cp && *cp != ':'; cp++) {
674
* Allocate an array to put the name pointers into
676
argv = (char **)malloc((n+3)*sizeof(char *));
683
* Fill up the array of pointers to names.
688
for (cp = cp2 = buf; (c = *cp); cp++) {
689
if (c == '|' || c == ':') {
692
* Skip entries that have spaces or are over 40
693
* characters long. If this is our environment
694
* name, then put it up front. Otherwise, as
695
* long as this is not a duplicate name (case
696
* insensitive) add it to the list.
698
if (n || (cp - cp2 > 41))
700
else if (name && (strncasecmp(name, cp2,
704
else if (is_unique(cp2, argv+1, argvp))
709
* Skip multiple delimiters. Reset cp2 to
710
* the beginning of the next name. Reset n,
711
* the flag for names with spaces.
713
while ((c = *cp) == '|')
719
* Skip entries with spaces or non-ascii values.
720
* Convert lower case letters to upper case.
722
if ((c == ' ') || !isascii(c))
724
else if (islower((unsigned char) c))
725
*cp = toupper((unsigned char) c);
729
* Check for an old V6 2 character name. If the second
730
* name points to the beginning of the buffer, and is
731
* only 2 characters long, move it to the end of the array.
733
if ((argv[1] == buf) && (strlen(argv[1]) == 2)) {
735
for (avt = &argv[1]; avt < argvp; avt++)
741
* Duplicate last name, for TTYPE option, and null
742
* terminate the array. If we didn't find a match on
743
* our terminal name, put that name at the beginning.
754
for (avt = argv; avt < argvp; avt++)
765
is_unique(name, as, ae)
766
register char *name, **as, **ae;
769
register unsigned int n;
771
n = strlen(name) + 1;
772
for (ap = as; ap < ae; ap++)
773
if (strncasecmp(*ap, name, n) == 0)
778
#ifndef HAVE_SETUPTERM
783
setupterm(tname, fd, errp)
787
if (tgetent(termbuf, tname) == 1) {
788
termbuf[1023] = '\0';
798
#define termbuf ttytype
799
extern char ttytype[];
802
int resettermname = 1;
808
static char **tnamep = 0;
814
if (tnamep && tnamep != unknown)
816
if ((tname = (char *)env_getvalue((unsigned char *)"TERM")) &&
817
(setupterm(tname, 1, &err) == 0)) {
818
tnamep = mklist(termbuf, tname);
820
if (tname && (strlen(tname) <= 40)) {
824
unknown[0] = name_unknown;
836
* Look at the sub-option buffer, and try to be helpful to the other
839
* Currently we recognize:
841
* Terminal type, send request.
842
* Terminal speed (send request).
843
* Local flow control (is request).
850
unsigned char subchar;
852
printsub('<', subbuffer, SB_LEN()+2);
853
switch (subchar = SB_GET()) {
855
if (my_want_state_is_wont(TELOPT_TTYPE))
857
if (SB_EOF() || SB_GET() != TELQUAL_SEND) {
861
unsigned char temp[50];
865
if (tn3270_ttype()) {
868
#endif /* defined(TN3270) */
869
name = gettermname();
870
len = strlen(name) + 4 + 2;
871
if (len < NETROOM()) {
872
snprintf((char *)temp, sizeof(temp), "%c%c%c%c%s%c%c",
873
IAC, SB, TELOPT_TTYPE, TELQUAL_IS, name, IAC, SE);
874
ring_supply_data(&netoring, temp, len);
875
printsub('>', &temp[2], len-2);
877
ExitString("No room in buffer for terminal type.\n", 1);
883
if (my_want_state_is_wont(TELOPT_TSPEED))
887
if (SB_GET() == TELQUAL_SEND) {
888
long o_speed, ispeed;
889
unsigned char temp[50];
892
TerminalSpeeds(&ispeed, &o_speed);
894
snprintf((char *)temp, sizeof(temp), "%c%c%c%c%ld,%ld%c%c", IAC,
895
SB, TELOPT_TSPEED, TELQUAL_IS, o_speed, ispeed, IAC, SE);
896
len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */
898
if (len < NETROOM()) {
899
ring_supply_data(&netoring, temp, len);
900
printsub('>', temp+2, len - 2);
902
/*@*/ else printf("lm_will: not enough room in buffer\n");
906
if (my_want_state_is_wont(TELOPT_LFLOW))
911
case LFLOW_RESTART_ANY:
914
case LFLOW_RESTART_XON:
930
case TELOPT_LINEMODE:
931
if (my_want_state_is_wont(TELOPT_LINEMODE))
937
lm_will(subpointer, SB_LEN());
940
lm_wont(subpointer, SB_LEN());
943
lm_do(subpointer, SB_LEN());
946
lm_dont(subpointer, SB_LEN());
949
slc(subpointer, SB_LEN());
952
lm_mode(subpointer, SB_LEN(), 0);
960
case TELOPT_OLD_ENVIRON:
962
case TELOPT_NEW_ENVIRON:
968
if (my_want_state_is_dont(subchar))
972
if (my_want_state_is_wont(subchar)) {
979
env_opt(subpointer, SB_LEN());
982
case TELOPT_XDISPLOC:
983
if (my_want_state_is_wont(TELOPT_XDISPLOC))
987
if (SB_GET() == TELQUAL_SEND) {
988
unsigned char temp[50], *dp;
991
if (((dp = env_getvalue((unsigned char *)"DISPLAY")) == NULL) ||
992
(! env_is_exported((unsigned char *)"DISPLAY"))) {
994
* Something happened, we no longer have a DISPLAY
995
* variable. So, turn off the option.
997
send_wont(TELOPT_XDISPLOC, 1);
1000
snprintf((char *)temp, sizeof(temp), "%c%c%c%c%s%c%c",
1001
IAC, SB, TELOPT_XDISPLOC, TELQUAL_IS, dp, IAC, SE);
1002
len = strlen((char *)temp+4) + 4; /* temp[3] is 0 ... */
1004
if (len < NETROOM()) {
1005
ring_supply_data(&netoring, temp, len);
1006
printsub('>', temp+2, len - 2);
1008
/*@*/ else printf("lm_will: not enough room in buffer\n");
1012
#if defined(AUTHENTICATION)
1013
case TELOPT_AUTHENTICATION: {
1020
if (my_want_state_is_dont(TELOPT_AUTHENTICATION))
1022
auth_is(subpointer, SB_LEN());
1025
if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
1027
auth_send(subpointer, SB_LEN());
1030
if (my_want_state_is_wont(TELOPT_AUTHENTICATION))
1032
auth_reply(subpointer, SB_LEN());
1035
if (my_want_state_is_dont(TELOPT_AUTHENTICATION))
1037
auth_name(subpointer, SB_LEN());
1044
case TELOPT_ENCRYPT:
1049
if (my_want_state_is_dont(TELOPT_ENCRYPT))
1051
encrypt_start(subpointer, SB_LEN());
1054
if (my_want_state_is_dont(TELOPT_ENCRYPT))
1058
case ENCRYPT_SUPPORT:
1059
if (my_want_state_is_wont(TELOPT_ENCRYPT))
1061
encrypt_support(subpointer, SB_LEN());
1063
case ENCRYPT_REQSTART:
1064
if (my_want_state_is_wont(TELOPT_ENCRYPT))
1066
encrypt_request_start(subpointer, SB_LEN());
1068
case ENCRYPT_REQEND:
1069
if (my_want_state_is_wont(TELOPT_ENCRYPT))
1072
* We can always send an REQEND so that we cannot
1073
* get stuck encrypting. We should only get this
1074
* if we have been able to get in the correct mode
1077
encrypt_request_end();
1080
if (my_want_state_is_dont(TELOPT_ENCRYPT))
1082
encrypt_is(subpointer, SB_LEN());
1085
if (my_want_state_is_wont(TELOPT_ENCRYPT))
1087
encrypt_reply(subpointer, SB_LEN());
1089
case ENCRYPT_ENC_KEYID:
1090
if (my_want_state_is_dont(TELOPT_ENCRYPT))
1092
encrypt_enc_keyid(subpointer, SB_LEN());
1094
case ENCRYPT_DEC_KEYID:
1095
if (my_want_state_is_wont(TELOPT_ENCRYPT))
1097
encrypt_dec_keyid(subpointer, SB_LEN());
1103
#endif /* ENCRYPTION */
1109
static unsigned char str_lm[] = { IAC, SB, TELOPT_LINEMODE, 0, 0, IAC, SE };
1117
/*@*/ printf("lm_will: no command!!!\n"); /* Should not happen... */
1121
case LM_FORWARDMASK: /* We shouldn't ever get this... */
1125
if (NETROOM() > sizeof(str_lm)) {
1126
ring_supply_data(&netoring, str_lm, sizeof(str_lm));
1127
printsub('>', &str_lm[2], sizeof(str_lm)-2);
1129
/*@*/ else printf("lm_will: not enough room in buffer\n");
1140
/*@*/ printf("lm_wont: no command!!!\n"); /* Should not happen... */
1144
case LM_FORWARDMASK: /* We shouldn't ever get this... */
1146
/* We are always DONT, so don't respond */
1157
/*@*/ printf("lm_do: no command!!!\n"); /* Should not happen... */
1161
case LM_FORWARDMASK:
1165
if (NETROOM() > sizeof(str_lm)) {
1166
ring_supply_data(&netoring, str_lm, sizeof(str_lm));
1167
printsub('>', &str_lm[2], sizeof(str_lm)-2);
1169
/*@*/ else printf("lm_do: not enough room in buffer\n");
1180
/*@*/ printf("lm_dont: no command!!!\n"); /* Should not happen... */
1184
case LM_FORWARDMASK:
1186
/* we are always WONT, so don't respond */
1191
static unsigned char str_lm_mode[] = {
1192
IAC, SB, TELOPT_LINEMODE, LM_MODE, 0, IAC, SE
1196
lm_mode(cmd, len, init)
1202
if ((linemode&MODE_MASK&~MODE_ACK) == *cmd)
1206
linemode = *cmd&(MODE_MASK&~MODE_ACK);
1207
str_lm_mode[4] = linemode;
1209
str_lm_mode[4] |= MODE_ACK;
1210
if (NETROOM() > sizeof(str_lm_mode)) {
1211
ring_supply_data(&netoring, str_lm_mode, sizeof(str_lm_mode));
1212
printsub('>', &str_lm_mode[2], sizeof(str_lm_mode)-2);
1214
/*@*/ else printf("lm_mode: not enough room in buffer\n");
1215
setconnmode(0); /* set changed mode */
1222
* Handle special character suboption of LINEMODE.
1228
char flags; /* Current flags & level */
1229
char mylevel; /* Maximum level & flags */
1232
#define SLC_IMPORT 0
1233
#define SLC_EXPORT 1
1234
#define SLC_RVALUE 2
1235
static int slc_mode = SLC_EXPORT;
1240
register struct spc *spcp;
1243
for (spcp = spc_data; spcp < &spc_data[NSLC+1]; spcp++) {
1246
spcp->flags = spcp->mylevel = SLC_NOSUPPORT;
1249
#define initfunc(func, flags) { \
1250
spcp = &spc_data[func]; \
1251
if ((spcp->valp = tcval(func)) != NULL) { \
1252
spcp->val = *spcp->valp; \
1253
spcp->mylevel = SLC_VARIABLE|flags; \
1256
spcp->mylevel = SLC_DEFAULT; \
1260
initfunc(SLC_SYNCH, 0);
1262
initfunc(SLC_AO, 0);
1263
initfunc(SLC_AYT, 0);
1265
initfunc(SLC_ABORT, SLC_FLUSHIN|SLC_FLUSHOUT);
1266
initfunc(SLC_EOF, 0);
1268
initfunc(SLC_SUSP, SLC_FLUSHIN);
1270
initfunc(SLC_EC, 0);
1271
initfunc(SLC_EL, 0);
1273
initfunc(SLC_EW, 0);
1274
initfunc(SLC_RP, 0);
1275
initfunc(SLC_LNEXT, 0);
1277
initfunc(SLC_XON, 0);
1278
initfunc(SLC_XOFF, 0);
1280
spc_data[SLC_XON].mylevel = SLC_CANTCHANGE;
1281
spc_data[SLC_XOFF].mylevel = SLC_CANTCHANGE;
1283
initfunc(SLC_FORW1, 0);
1285
initfunc(SLC_FORW2, 0);
1289
initfunc(SLC_IP, SLC_FLUSHIN|SLC_FLUSHOUT);
1292
if (slc_mode == SLC_EXPORT)
1302
printf("Special characters are %s values\n",
1303
slc_mode == SLC_IMPORT ? "remote default" :
1304
slc_mode == SLC_EXPORT ? "local" :
1311
slc_mode = SLC_EXPORT;
1312
if (my_state_is_will(TELOPT_LINEMODE))
1317
slc_mode_import(def)
1320
slc_mode = def ? SLC_IMPORT : SLC_RVALUE;
1321
if (my_state_is_will(TELOPT_LINEMODE))
1325
unsigned char slc_import_val[] = {
1326
IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_VARIABLE, 0, IAC, SE
1328
unsigned char slc_import_def[] = {
1329
IAC, SB, TELOPT_LINEMODE, LM_SLC, 0, SLC_DEFAULT, 0, IAC, SE
1336
if (NETROOM() > sizeof(slc_import_val)) {
1338
ring_supply_data(&netoring, slc_import_def, sizeof(slc_import_def));
1339
printsub('>', &slc_import_def[2], sizeof(slc_import_def)-2);
1341
ring_supply_data(&netoring, slc_import_val, sizeof(slc_import_val));
1342
printsub('>', &slc_import_val[2], sizeof(slc_import_val)-2);
1345
/*@*/ else printf("slc_import: not enough room\n");
1351
register struct spc *spcp;
1353
TerminalDefaultChars();
1356
for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1357
if (spcp->mylevel != SLC_NOSUPPORT) {
1358
if (spcp->val == (cc_t)(_POSIX_VDISABLE))
1359
spcp->flags = SLC_NOSUPPORT;
1361
spcp->flags = spcp->mylevel;
1363
spcp->val = *spcp->valp;
1364
slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
1369
setconnmode(1); /* Make sure the character values are set */
1374
register unsigned char *cp;
1377
register struct spc *spcp;
1378
register int func,level;
1382
for (; len >= 3; len -=3, cp +=3) {
1384
func = cp[SLC_FUNC];
1388
* Client side: always ignore 0 function.
1393
if ((cp[SLC_FLAGS] & SLC_LEVELBITS) != SLC_NOSUPPORT)
1394
slc_add_reply(func, SLC_NOSUPPORT, 0);
1398
spcp = &spc_data[func];
1400
level = cp[SLC_FLAGS]&(SLC_LEVELBITS|SLC_ACK);
1402
if ((cp[SLC_VALUE] == (unsigned char)spcp->val) &&
1403
((level&SLC_LEVELBITS) == (spcp->flags&SLC_LEVELBITS))) {
1407
if (level == (SLC_DEFAULT|SLC_ACK)) {
1409
* This is an error condition, the SLC_ACK
1410
* bit should never be set for the SLC_DEFAULT
1411
* level. Our best guess to recover is to
1412
* ignore the SLC_ACK bit.
1414
cp[SLC_FLAGS] &= ~SLC_ACK;
1417
if (level == ((spcp->flags&SLC_LEVELBITS)|SLC_ACK)) {
1418
spcp->val = (cc_t)cp[SLC_VALUE];
1419
spcp->flags = cp[SLC_FLAGS]; /* include SLC_ACK */
1425
if (level <= (spcp->mylevel&SLC_LEVELBITS)) {
1426
spcp->flags = cp[SLC_FLAGS]|SLC_ACK;
1427
spcp->val = (cc_t)cp[SLC_VALUE];
1429
if (level == SLC_DEFAULT) {
1430
if ((spcp->mylevel&SLC_LEVELBITS) != SLC_DEFAULT)
1431
spcp->flags = spcp->mylevel;
1433
spcp->flags = SLC_NOSUPPORT;
1435
slc_add_reply(func, spcp->flags, spcp->val);
1439
setconnmode(1); /* set the new character values */
1445
register struct spc *spcp;
1448
for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1449
if (spcp->valp && spcp->val != *spcp->valp) {
1450
spcp->val = *spcp->valp;
1451
if (spcp->val == (cc_t)(_POSIX_VDISABLE))
1452
spcp->flags = SLC_NOSUPPORT;
1454
spcp->flags = spcp->mylevel;
1455
slc_add_reply(spcp - spc_data, spcp->flags, spcp->val);
1463
unsigned char slc_reply[128];
1464
unsigned char *slc_replyp;
1469
slc_replyp = slc_reply;
1470
*slc_replyp++ = IAC;
1472
*slc_replyp++ = TELOPT_LINEMODE;
1473
*slc_replyp++ = LM_SLC;
1477
slc_add_reply(func, flags, value)
1479
unsigned char flags;
1482
if ((slc_replyp - slc_reply) + 6 > sizeof(slc_reply))
1484
if ((*slc_replyp++ = func) == IAC)
1485
*slc_replyp++ = IAC;
1486
if ((*slc_replyp++ = flags) == IAC)
1487
*slc_replyp++ = IAC;
1488
if ((*slc_replyp++ = (unsigned char)value) == IAC)
1489
*slc_replyp++ = IAC;
1497
len = slc_replyp - slc_reply;
1498
if (len <= 4 || (len + 2 > sizeof(slc_reply)))
1500
*slc_replyp++ = IAC;
1503
if (NETROOM() > len) {
1504
ring_supply_data(&netoring, slc_reply, slc_replyp - slc_reply);
1505
printsub('>', &slc_reply[2], slc_replyp - slc_reply - 2);
1507
/*@*/else printf("slc_end_reply: not enough room\n");
1513
register struct spc *spcp;
1514
int need_update = 0;
1516
for (spcp = &spc_data[1]; spcp < &spc_data[NSLC+1]; spcp++) {
1517
if (!(spcp->flags&SLC_ACK))
1519
spcp->flags &= ~SLC_ACK;
1520
if (spcp->valp && (*spcp->valp != spcp->val)) {
1521
*spcp->valp = spcp->val;
1525
return(need_update);
1531
* Earlier version of telnet/telnetd from the BSD code had
1532
* the definitions of VALUE and VAR reversed. To ensure
1533
* maximum interoperability, we assume that the server is
1534
* an older BSD server, until proven otherwise. The newer
1535
* BSD servers should be able to handle either definition,
1536
* so it is better to use the wrong values if we don't
1537
* know what type of server it is.
1540
int old_env_var = OLD_ENV_VAR;
1541
int old_env_value = OLD_ENV_VALUE;
1543
# define old_env_var OLD_ENV_VAR
1544
# define old_env_value OLD_ENV_VALUE
1550
register unsigned char *buf;
1553
register unsigned char *ep = 0, *epc = 0;
1556
switch(buf[0]&0xff) {
1561
} else for (i = 1; i < len; i++) {
1562
switch (buf[i]&0xff) {
1566
if (telopt_environ == TELOPT_OLD_ENVIRON
1568
/* Server has the same definitions */
1569
old_env_var = OLD_ENV_VAR;
1570
old_env_value = OLD_ENV_VALUE;
1576
* Although OLD_ENV_VALUE is not legal, we will
1577
* still recognize it, just in case it is an
1578
* old server that has VAR & VALUE mixed up...
1589
ep = epc = &buf[i+1];
1609
/* Ignore for now. We shouldn't get it anyway. */
1617
#define OPT_REPLY_SIZE 256
1618
unsigned char *opt_reply;
1619
unsigned char *opt_replyp;
1620
unsigned char *opt_replyend;
1626
opt_reply = (unsigned char *)realloc(opt_reply, OPT_REPLY_SIZE);
1628
opt_reply = (unsigned char *)malloc(OPT_REPLY_SIZE);
1629
if (opt_reply == NULL) {
1630
/*@*/ printf("env_opt_start: malloc()/realloc() failed!!!\n");
1631
opt_reply = opt_replyp = opt_replyend = NULL;
1634
opt_replyp = opt_reply;
1635
opt_replyend = opt_reply + OPT_REPLY_SIZE;
1636
*opt_replyp++ = IAC;
1638
*opt_replyp++ = telopt_environ;
1639
*opt_replyp++ = TELQUAL_IS;
1643
env_opt_start_info()
1647
opt_replyp[-1] = TELQUAL_INFO;
1652
register unsigned char *ep;
1654
register unsigned char *vp, c;
1655
unsigned int len, olen, elen;
1657
if (opt_reply == NULL) /*XXX*/
1660
if (ep == NULL || *ep == '\0') {
1661
/* Send user defined variables first. */
1663
while ((ep = env_default(0, 0)) != NULL)
1666
/* Now add the list of well know variables. */
1668
while ((ep = env_default(0, 1)) != NULL)
1672
vp = env_getvalue(ep);
1673
elen = 2 * (vp ? strlen((char *)vp) : 0) +
1674
2 * strlen((char *)ep) + 6;
1675
if ((opt_replyend - opt_replyp) < elen)
1677
len = opt_replyend - opt_reply + elen;
1678
olen = opt_replyp - opt_reply;
1679
opt_reply = (unsigned char *)realloc(opt_reply, len);
1680
if (opt_reply == NULL) {
1681
/*@*/ printf("env_opt_add: realloc() failed!!!\n");
1682
opt_reply = opt_replyp = opt_replyend = NULL;
1685
opt_replyp = opt_reply + olen;
1686
opt_replyend = opt_reply + len;
1688
if (opt_welldefined((char *) ep))
1690
if (telopt_environ == TELOPT_OLD_ENVIRON)
1691
*opt_replyp++ = old_env_var;
1694
*opt_replyp++ = NEW_ENV_VAR;
1696
*opt_replyp++ = ENV_USERVAR;
1698
while ((c = *ep++)) {
1701
*opt_replyp++ = IAC;
1707
*opt_replyp++ = ENV_ESC;
1712
if ((ep = vp) != NULL) {
1714
if (telopt_environ == TELOPT_OLD_ENVIRON)
1715
*opt_replyp++ = old_env_value;
1718
*opt_replyp++ = NEW_ENV_VALUE;
1729
if ((strcmp(ep, "USER") == 0) ||
1730
(strcmp(ep, "DISPLAY") == 0) ||
1731
(strcmp(ep, "PRINTER") == 0) ||
1732
(strcmp(ep, "SYSTEMTYPE") == 0) ||
1733
(strcmp(ep, "JOB") == 0) ||
1734
(strcmp(ep, "ACCT") == 0))
1739
env_opt_end(emptyok)
1740
register int emptyok;
1744
len = opt_replyp - opt_reply + 2;
1745
if (emptyok || len > 6) {
1746
*opt_replyp++ = IAC;
1748
if (NETROOM() > len) {
1749
ring_supply_data(&netoring, opt_reply, len);
1750
printsub('>', &opt_reply[2], len - 2);
1752
/*@*/ else printf("slc_end_reply: not enough room\n");
1756
opt_reply = opt_replyp = opt_replyend = NULL;
1767
register unsigned char *sbp = NULL;
1769
int returnValue = 0;
1773
while (TTYROOM() > 2) {
1776
ring_consumed(&netiring, count);
1780
sbp = netiring.consume;
1781
scc = ring_full_consecutive(&netiring);
1783
/* No more data coming in */
1788
c = *sbp++ & 0xff, scc--; count++;
1791
c = (*decrypt_input)(c);
1792
#endif /* ENCRYPTION */
1794
switch (telrcv_state) {
1797
telrcv_state = TS_DATA;
1799
break; /* Ignore \0 after CR */
1801
else if ((c == '\n') && my_want_state_is_dont(TELOPT_ECHO) && !crmod) {
1805
/* Else, fall through */
1809
telrcv_state = TS_IAC;
1812
# if defined(TN3270)
1816
c = *sbp++ & 0377, scc--; count++;
1819
c = (*decrypt_input)(c);
1820
#endif /* ENCRYPTION */
1822
telrcv_state = TS_IAC;
1828
# endif /* defined(TN3270) */
1830
* The 'crmod' hack (see following) is needed
1831
* since we can't * set CRMOD on output only.
1832
* Machines like MULTICS like to send \r without
1833
* \n; since we must turn off CRMOD to get proper
1834
* input, the mapping is done here (sigh).
1836
if ((c == '\r') && my_want_state_is_dont(TELOPT_BINARY)) {
1841
c = (*decrypt_input)(c);
1842
#endif /* ENCRYPTION */
1844
sbp++, scc--; count++;
1847
} else if (my_want_state_is_dont(TELOPT_ECHO) &&
1849
sbp++, scc--; count++;
1854
(*decrypt_input)(-1);
1855
#endif /* ENCRYPTION */
1863
telrcv_state = TS_CR;
1879
telrcv_state = TS_WILL;
1883
telrcv_state = TS_WONT;
1887
telrcv_state = TS_DO;
1891
telrcv_state = TS_DONT;
1896
* We may have missed an urgent notification,
1897
* so make sure we flush whatever is in the
1900
printoption("RCVD", IAC, DM);
1903
SYNCHing = stilloob();
1909
telrcv_state = TS_SB;
1912
# if defined(TN3270)
1915
if (Ibackp == Ifrontp) {
1916
Ibackp = Ifrontp = Ibuf;
1917
ISend = 0; /* should have been! */
1919
Ibackp += DataFromNetwork(Ibackp, Ifrontp-Ibackp, 1);
1923
printoption("RCVD", IAC, EOR);
1925
# endif /* defined(TN3270) */
1928
# if !defined(TN3270)
1930
# else /* !defined(TN3270) */
1936
# endif /* !defined(TN3270) */
1942
printoption("RCVD", IAC, c);
1945
telrcv_state = TS_DATA;
1949
printoption("RCVD", WILL, c);
1952
telrcv_state = TS_DATA;
1956
printoption("RCVD", WONT, c);
1959
telrcv_state = TS_DATA;
1963
printoption("RCVD", DO, c);
1966
if (c == TELOPT_NAWS) {
1968
} else if (c == TELOPT_LFLOW) {
1973
telrcv_state = TS_DATA;
1977
printoption("RCVD", DONT, c);
1980
setconnmode(0); /* set new tty mode (maybe) */
1982
telrcv_state = TS_DATA;
1987
telrcv_state = TS_SE;
1997
* This is an error. We only expect to get
1998
* "IAC IAC" or "IAC SE". Several things may
1999
* have happend. An IAC was not doubled, the
2000
* IAC SE was left off, or another option got
2001
* inserted into the suboption are all possibilities.
2002
* If we assume that the IAC was not doubled,
2003
* and really the IAC SE was left off, we could
2004
* get into an infinate loop here. So, instead,
2005
* we terminate the suboption, and process the
2006
* partial suboption if we can.
2013
printoption("In SUBOPTION processing, RCVD", IAC, c);
2014
suboption(); /* handle sub-option */
2016
telrcv_state = TS_IAC;
2020
telrcv_state = TS_SB;
2026
suboption(); /* handle sub-option */
2028
telrcv_state = TS_DATA;
2033
ring_consumed(&netiring, count);
2034
return returnValue||count;
2037
static int bol = 1, local = 0;
2045
command(0, "z\n", 2);
2056
int returnValue = 0;
2057
unsigned char *tbp = NULL;
2061
while (NETROOM() > 2) {
2067
ring_consumed(&ttyiring, count);
2071
tbp = ttyiring.consume;
2072
tcc = ring_full_consecutive(&ttyiring);
2077
c = *tbp++ & 0xff, sc = strip(c), tcc--; count++;
2078
if (rlogin != _POSIX_VDISABLE) {
2087
if (sc == '.' || c == termEofChar) {
2089
command(0, "close\n", 6);
2092
if (sc == termSuspChar) {
2094
command(0, "z\n", 2);
2098
command(0, (char *)tbp, tcc);
2112
if ((sc == '\n') || (sc == '\r'))
2114
} else if (sc == escape) {
2116
* Double escape is a pass through of a single escape character.
2118
if (tcc && strip(*tbp) == escape) {
2124
command(0, (char *)tbp, tcc);
2133
#ifdef KLUDGELINEMODE
2134
if (kludgelinemode && (globalmode&MODE_EDIT) && (sc == echoc)) {
2135
if (tcc > 0 && strip(*tbp) == echoc) {
2136
tcc--; tbp++; count++;
2138
dontlecho = !dontlecho;
2139
settimer(echotoggle);
2146
if (MODE_LOCAL_CHARS(globalmode)) {
2147
if (TerminalSpecialChars(sc) == 0) {
2152
if (my_want_state_is_wont(TELOPT_BINARY)) {
2156
* If we are in CRMOD mode (\r ==> \n)
2157
* on our local machine, then probably
2158
* a newline (unix) is CRLF (TELNET).
2160
if (MODE_LOCAL_CHARS(globalmode)) {
2164
bol = flushline = 1;
2168
NET2ADD('\r', '\0');
2170
NET2ADD('\r', '\n');
2172
bol = flushline = 1;
2181
} else if (c == IAC) {
2188
ring_consumed(&ttyiring, count);
2189
return returnValue||count; /* Non-zero if we did anything */
2195
* Try to do something.
2197
* If we do something useful, return 1; else return 0.
2204
int block; /* should we block in the select ? */
2206
/* One wants to be a bit careful about setting returnValue
2207
* to one, since a one implies we did some useful work,
2208
* and therefore probably won't be called to block next
2209
* time (TN3270 mode only).
2212
int netin, netout, netex, ttyin, ttyout;
2214
/* Decide which rings should be processed */
2216
netout = ring_full_count(&netoring) &&
2218
(my_want_state_is_wont(TELOPT_LINEMODE)
2219
#ifdef KLUDGELINEMODE
2220
&& (!kludgelinemode || my_want_state_is_do(TELOPT_SGA))
2223
my_want_state_is_will(TELOPT_BINARY));
2224
ttyout = ring_full_count(&ttyoring);
2227
ttyin = ring_empty_count(&ttyiring) && (shell_active == 0);
2228
#else /* defined(TN3270) */
2229
ttyin = ring_empty_count(&ttyiring);
2230
#endif /* defined(TN3270) */
2233
netin = ring_empty_count(&netiring);
2234
# else /* !defined(TN3270) */
2235
netin = !ISend && ring_empty_count(&netiring);
2236
# endif /* !defined(TN3270) */
2240
/* If we have seen a signal recently, reset things */
2241
# if defined(TN3270) && defined(unix)
2244
(void) signal(SIGIO, inputAvailable);
2246
#endif /* defined(TN3270) && defined(unix) */
2248
if (scheduler_lockout_tty) {
2252
/* Call to system code to process rings */
2254
returnValue = process_rings(netin, netout, netex, ttyin, ttyout, !block);
2256
/* Now, look at the input rings, looking for work to do. */
2258
if (ring_full_count(&ttyiring)) {
2259
# if defined(TN3270)
2263
c = DataFromTerminal(ttyiring.consume,
2264
ring_full_consecutive(&ttyiring));
2267
ring_consumed(&ttyiring, c);
2270
# endif /* defined(TN3270) */
2271
returnValue |= telsnd();
2272
# if defined(TN3270)
2274
# endif /* defined(TN3270) */
2277
if (ring_full_count(&netiring)) {
2278
# if !defined(TN3270)
2279
returnValue |= telrcv();
2280
# else /* !defined(TN3270) */
2281
returnValue = Push3270();
2282
# endif /* !defined(TN3270) */
2288
* Select from tty and network...
2294
int printed_encrypt = 0;
2298
#if defined(AUTHENTICATION) || defined(ENCRYPTION)
2300
static char local_host[256] = { 0 };
2302
if (!local_host[0]) {
2303
gethostname(local_host, sizeof(local_host));
2304
local_host[sizeof(local_host)-1] = 0;
2306
auth_encrypt_init(local_host, hostname, "TELNET", 0);
2307
auth_encrypt_user(user);
2309
#endif /* defined(AUTHENTICATION) || defined(ENCRYPTION) */
2310
# if !defined(TN3270)
2311
#if defined(AUTHENTICATION)
2313
send_will(TELOPT_AUTHENTICATION, 1);
2316
if (telnetport || wantencryption) {
2317
send_do(TELOPT_ENCRYPT, 1);
2318
send_will(TELOPT_ENCRYPT, 1);
2320
#endif /* ENCRYPTION */
2322
send_do(TELOPT_SGA, 1);
2323
send_will(TELOPT_TTYPE, 1);
2324
send_will(TELOPT_NAWS, 1);
2325
send_will(TELOPT_TSPEED, 1);
2326
send_will(TELOPT_LFLOW, 1);
2327
send_will(TELOPT_LINEMODE, 1);
2328
send_will(TELOPT_NEW_ENVIRON, 1);
2329
send_do(TELOPT_STATUS, 1);
2330
if (env_getvalue((unsigned char *)"DISPLAY") &&
2331
env_is_exported((unsigned char *)"DISPLAY"))
2332
send_will(TELOPT_XDISPLOC, 1);
2334
tel_enter_binary(eight);
2336
# endif /* !defined(TN3270) */
2340
* Note: we assume a tie to the authentication option here. This
2341
* is necessary so that authentication fails, we don't spin
2344
if (wantencryption) {
2345
extern int auth_has_failed;
2346
time_t time_out = time(0) + 60;
2348
send_do(TELOPT_ENCRYPT, 1);
2349
send_will(TELOPT_ENCRYPT, 1);
2351
if (my_want_state_is_wont(TELOPT_AUTHENTICATION)) {
2352
printf("\nServer refused to negotiate authentication, which is required\n");
2353
printf("for encryption. Good-bye.\n\r");
2356
if (auth_has_failed) {
2357
printf("\nNegotiation of authentication, which is required for encryption,\n");
2358
printf("has failed. Good-bye.\n\r");
2361
if (my_want_state_is_dont(TELOPT_ENCRYPT) ||
2362
my_want_state_is_wont(TELOPT_ENCRYPT)) {
2363
printf("\nServer refused to negotiate encryption. Good-bye.\n\r");
2366
if (encrypt_is_encrypting())
2368
if (time(0) > time_out) {
2369
printf("\nEncryption could not be enabled. Good-bye.\n\r");
2372
if (printed_encrypt == 0) {
2373
printed_encrypt = 1;
2374
printf("Waiting for encryption to be negotiated...\n");
2376
* Turn on MODE_TRAPSIG and then turn off localchars
2377
* so that ^C will cause telnet to exit.
2379
TerminalNewMode(getconnmode()|MODE_TRAPSIG);
2382
if (intr_happened) {
2383
printf("\nUser requested an interrupt. Good-bye.\n\r");
2388
if (printed_encrypt) {
2397
# if !defined(TN3270)
2401
while ((schedValue = Scheduler(0)) != 0) {
2402
if (schedValue == -1) {
2408
if (Scheduler(1) == -1) {
2413
# else /* !defined(TN3270) */
2417
while (!In3270 && !shell_active) {
2418
if (Scheduler(1) == -1) {
2424
while ((schedValue = Scheduler(0)) != 0) {
2425
if (schedValue == -1) {
2430
/* If there is data waiting to go out to terminal, don't
2431
* schedule any more data for the terminal.
2433
if (ring_full_count(&ttyoring)) {
2437
if (shell_continue() == 0) {
2440
} else if (In3270) {
2441
schedValue = DoTerminalOutput();
2444
if (schedValue && (shell_active == 0)) {
2445
if (Scheduler(1) == -1) {
2451
# endif /* !defined(TN3270) */
2454
#if 0 /* XXX - this not being in is a bug */
2458
* Return the address of the next "item" in the TELNET data
2459
* stream. This will be the address of the next character if
2460
* the current address is a user data character, or it will
2461
* be the address of the character following the TELNET command
2462
* if the current address is a TELNET IAC ("I Am a Command")
2470
if ((*current&0xff) != IAC) {
2473
switch (*(current+1)&0xff) {
2479
case SB: /* loop forever looking for the SE */
2481
register char *look = current+2;
2484
if ((*look++&0xff) == IAC) {
2485
if ((*look++&0xff) == SE) {
2500
* We are about to do a TELNET SYNCH operation. Clear
2501
* the path to the network.
2503
* Things are a bit tricky since we may have sent the first
2504
* byte or so of a previous TELNET command into the network.
2505
* So, we have to scan the network buffer from the beginning
2506
* until we are up to where we want to be.
2508
* A side effect of what we do, just to keep things
2509
* simple, is to clear the urgent data pointer. The principal
2510
* caller should be setting the urgent data pointer AFTER calling
2518
register char *thisitem, *next;
2520
#define wewant(p) ((nfrontp > p) && ((*p&0xff) == IAC) && \
2521
((*(p+1)&0xff) != EC) && ((*(p+1)&0xff) != EL))
2525
while ((next = nextitem(thisitem)) <= netobuf.send) {
2529
/* Now, thisitem is first before/at boundary. */
2531
good = netobuf; /* where the good bytes go */
2533
while (netoring.add > thisitem) {
2534
if (wewant(thisitem)) {
2539
next = nextitem(next);
2540
} while (wewant(next) && (nfrontp > next));
2541
length = next-thisitem;
2542
memcpy(good, thisitem, length);
2546
thisitem = nextitem(thisitem);
2554
* These routines add various telnet commands to the data stream.
2564
(void) ttyflush(1); /* Flush/drop output */
2565
/* do printoption AFTER flush, otherwise the output gets tossed... */
2566
printoption("SENT", DO, TELOPT_TM);
2573
printoption("SENT", IAC, AO);
2584
printoption("SENT", IAC, EL);
2591
printoption("SENT", IAC, EC);
2599
netclear(); /* clear the path to the network */
2603
printoption("SENT", IAC, DM);
2607
int want_status_response = 0;
2613
unsigned char tmp[16];
2614
register unsigned char *cp;
2616
if (my_want_state_is_dont(TELOPT_STATUS)) {
2617
printf("Remote side does not support STATUS option\n");
2624
*cp++ = TELOPT_STATUS;
2625
*cp++ = TELQUAL_SEND;
2628
if (NETROOM() >= cp - tmp) {
2629
ring_supply_data(&netoring, tmp, cp-tmp);
2630
printsub('>', tmp+2, cp - tmp - 2);
2632
++want_status_response;
2640
printoption("SENT", IAC, IP);
2653
NET2ADD(IAC, BREAK);
2654
printoption("SENT", IAC, BREAK);
2667
NET2ADD(IAC, ABORT);
2668
printoption("SENT", IAC, ABORT);
2682
printoption("SENT", IAC, SUSP);
2696
printoption("SENT", IAC, xEOF);
2703
printoption("SENT", IAC, AYT);
2707
* Send a window size update to the remote system.
2714
unsigned char tmp[16];
2715
register unsigned char *cp;
2717
if (my_state_is_wont(TELOPT_NAWS))
2720
#define PUTSHORT(cp, x) { if ((*cp++ = ((x)>>8)&0xff) == IAC) *cp++ = IAC; \
2721
if ((*cp++ = ((x))&0xff) == IAC) *cp++ = IAC; }
2723
if (TerminalWindowSize(&rows, &cols) == 0) { /* Failed */
2731
*cp++ = TELOPT_NAWS;
2736
if (NETROOM() >= cp - tmp) {
2737
ring_supply_data(&netoring, tmp, cp-tmp);
2738
printsub('>', tmp+2, cp - tmp - 2);
2743
tel_enter_binary(rw)
2747
send_do(TELOPT_BINARY, 1);
2749
send_will(TELOPT_BINARY, 1);
2753
tel_leave_binary(rw)
2757
send_dont(TELOPT_BINARY, 1);
2759
send_wont(TELOPT_BINARY, 1);