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