374330e2 |
1 | #include <windows.h> |
2 | #include <stdio.h> |
3 | #include <stdlib.h> |
374330e2 |
4 | |
5 | #include "putty.h" |
6 | |
7 | #ifndef FALSE |
8 | #define FALSE 0 |
9 | #endif |
10 | #ifndef TRUE |
11 | #define TRUE 1 |
12 | #endif |
13 | |
8df7a775 |
14 | static Socket s = NULL; |
374330e2 |
15 | |
32874aea |
16 | #define IAC 255 /* interpret as command: */ |
17 | #define DONT 254 /* you are not to use option */ |
18 | #define DO 253 /* please, you use option */ |
19 | #define WONT 252 /* I won't use option */ |
20 | #define WILL 251 /* I will use option */ |
21 | #define SB 250 /* interpret as subnegotiation */ |
22 | #define SE 240 /* end sub negotiation */ |
23 | |
24 | #define GA 249 /* you may reverse the line */ |
25 | #define EL 248 /* erase the current line */ |
26 | #define EC 247 /* erase the current character */ |
27 | #define AYT 246 /* are you there */ |
28 | #define AO 245 /* abort output--but let prog finish */ |
29 | #define IP 244 /* interrupt process--permanently */ |
30 | #define BREAK 243 /* break */ |
31 | #define DM 242 /* data mark--for connect. cleaning */ |
32 | #define NOP 241 /* nop */ |
33 | #define EOR 239 /* end of record (transparent mode) */ |
34 | #define ABORT 238 /* Abort process */ |
35 | #define SUSP 237 /* Suspend process */ |
36 | #define xEOF 236 /* End of file: EOF is already used... */ |
37 | |
38 | #define TELOPT_BINARY 0 /* 8-bit data path */ |
39 | #define TELOPT_ECHO 1 /* echo */ |
40 | #define TELOPT_RCP 2 /* prepare to reconnect */ |
41 | #define TELOPT_SGA 3 /* suppress go ahead */ |
42 | #define TELOPT_NAMS 4 /* approximate message size */ |
43 | #define TELOPT_STATUS 5 /* give status */ |
44 | #define TELOPT_TM 6 /* timing mark */ |
45 | #define TELOPT_RCTE 7 /* remote controlled transmission and echo */ |
46 | #define TELOPT_NAOL 8 /* negotiate about output line width */ |
47 | #define TELOPT_NAOP 9 /* negotiate about output page size */ |
48 | #define TELOPT_NAOCRD 10 /* negotiate about CR disposition */ |
49 | #define TELOPT_NAOHTS 11 /* negotiate about horizontal tabstops */ |
50 | #define TELOPT_NAOHTD 12 /* negotiate about horizontal tab disposition */ |
51 | #define TELOPT_NAOFFD 13 /* negotiate about formfeed disposition */ |
52 | #define TELOPT_NAOVTS 14 /* negotiate about vertical tab stops */ |
53 | #define TELOPT_NAOVTD 15 /* negotiate about vertical tab disposition */ |
54 | #define TELOPT_NAOLFD 16 /* negotiate about output LF disposition */ |
55 | #define TELOPT_XASCII 17 /* extended ascic character set */ |
56 | #define TELOPT_LOGOUT 18 /* force logout */ |
57 | #define TELOPT_BM 19 /* byte macro */ |
58 | #define TELOPT_DET 20 /* data entry terminal */ |
59 | #define TELOPT_SUPDUP 21 /* supdup protocol */ |
60 | #define TELOPT_SUPDUPOUTPUT 22 /* supdup output */ |
61 | #define TELOPT_SNDLOC 23 /* send location */ |
62 | #define TELOPT_TTYPE 24 /* terminal type */ |
63 | #define TELOPT_EOR 25 /* end or record */ |
64 | #define TELOPT_TUID 26 /* TACACS user identification */ |
65 | #define TELOPT_OUTMRK 27 /* output marking */ |
66 | #define TELOPT_TTYLOC 28 /* terminal location number */ |
67 | #define TELOPT_3270REGIME 29 /* 3270 regime */ |
68 | #define TELOPT_X3PAD 30 /* X.3 PAD */ |
69 | #define TELOPT_NAWS 31 /* window size */ |
70 | #define TELOPT_TSPEED 32 /* terminal speed */ |
71 | #define TELOPT_LFLOW 33 /* remote flow control */ |
72 | #define TELOPT_LINEMODE 34 /* Linemode option */ |
73 | #define TELOPT_XDISPLOC 35 /* X Display Location */ |
74 | #define TELOPT_OLD_ENVIRON 36 /* Old - Environment variables */ |
75 | #define TELOPT_AUTHENTICATION 37 /* Authenticate */ |
76 | #define TELOPT_ENCRYPT 38 /* Encryption option */ |
77 | #define TELOPT_NEW_ENVIRON 39 /* New - Environment variables */ |
78 | #define TELOPT_EXOPL 255 /* extended-options-list */ |
79 | |
80 | #define TELQUAL_IS 0 /* option is... */ |
81 | #define TELQUAL_SEND 1 /* send option */ |
82 | #define TELQUAL_INFO 2 /* ENVIRON: informational version of IS */ |
374330e2 |
83 | #define BSD_VAR 1 |
84 | #define BSD_VALUE 0 |
85 | #define RFC_VAR 0 |
86 | #define RFC_VALUE 1 |
87 | |
88 | #define CR 13 |
89 | #define LF 10 |
90 | #define NUL 0 |
91 | |
92 | #define iswritable(x) ( (x) != IAC && (x) != CR ) |
93 | |
32874aea |
94 | static char *telopt(int opt) |
95 | { |
374330e2 |
96 | #define i(x) if (opt == TELOPT_ ## x) return #x; |
32874aea |
97 | i(BINARY); |
98 | i(ECHO); |
99 | i(RCP); |
100 | i(SGA); |
101 | i(NAMS); |
102 | i(STATUS); |
103 | i(TM); |
104 | i(RCTE); |
105 | i(NAOL); |
106 | i(NAOP); |
107 | i(NAOCRD); |
108 | i(NAOHTS); |
109 | i(NAOHTD); |
110 | i(NAOFFD); |
111 | i(NAOVTS); |
112 | i(NAOVTD); |
113 | i(NAOLFD); |
114 | i(XASCII); |
115 | i(LOGOUT); |
116 | i(BM); |
117 | i(DET); |
118 | i(SUPDUP); |
119 | i(SUPDUPOUTPUT); |
120 | i(SNDLOC); |
121 | i(TTYPE); |
122 | i(EOR); |
123 | i(TUID); |
124 | i(OUTMRK); |
125 | i(TTYLOC); |
126 | i(X3PAD); |
127 | i(NAWS); |
128 | i(TSPEED); |
129 | i(LFLOW); |
130 | i(LINEMODE); |
131 | i(XDISPLOC); |
132 | i(OLD_ENVIRON); |
133 | i(AUTHENTICATION); |
134 | i(ENCRYPT); |
135 | i(NEW_ENVIRON); |
136 | i(EXOPL); |
374330e2 |
137 | #undef i |
138 | return "<unknown>"; |
139 | } |
140 | |
141 | static void telnet_size(void); |
142 | |
143 | struct Opt { |
144 | int send; /* what we initially send */ |
145 | int nsend; /* -ve send if requested to stop it */ |
146 | int ack, nak; /* +ve and -ve acknowledgements */ |
147 | int option; /* the option code */ |
148 | enum { |
149 | REQUESTED, ACTIVE, INACTIVE, REALLY_INACTIVE |
150 | } state; |
151 | }; |
152 | |
32874aea |
153 | static struct Opt o_naws = |
154 | { WILL, WONT, DO, DONT, TELOPT_NAWS, REQUESTED }; |
155 | static struct Opt o_tspeed = |
156 | { WILL, WONT, DO, DONT, TELOPT_TSPEED, REQUESTED }; |
157 | static struct Opt o_ttype = |
158 | { WILL, WONT, DO, DONT, TELOPT_TTYPE, REQUESTED }; |
159 | static struct Opt o_oenv = { WILL, WONT, DO, DONT, TELOPT_OLD_ENVIRON, |
160 | INACTIVE |
161 | }; |
162 | static struct Opt o_nenv = { WILL, WONT, DO, DONT, TELOPT_NEW_ENVIRON, |
163 | REQUESTED |
164 | }; |
165 | static struct Opt o_echo = |
166 | { DO, DONT, WILL, WONT, TELOPT_ECHO, REQUESTED }; |
167 | static struct Opt o_we_sga = |
168 | { WILL, WONT, DO, DONT, TELOPT_SGA, REQUESTED }; |
169 | static struct Opt o_they_sga = |
170 | { DO, DONT, WILL, WONT, TELOPT_SGA, REQUESTED }; |
374330e2 |
171 | |
172 | static struct Opt *opts[] = { |
173 | &o_naws, &o_tspeed, &o_ttype, &o_oenv, &o_nenv, &o_echo, |
174 | &o_we_sga, &o_they_sga, NULL |
175 | }; |
176 | |
0965bee0 |
177 | static int echoing = TRUE, editing = TRUE; |
8faa456c |
178 | static int activated = FALSE; |
0965bee0 |
179 | |
374330e2 |
180 | static int in_synch; |
374330e2 |
181 | static int sb_opt, sb_len; |
182 | static char *sb_buf = NULL; |
183 | static int sb_size = 0; |
184 | #define SB_DELTA 1024 |
185 | |
32874aea |
186 | static void c_write1(int c) |
187 | { |
188 | char cc = (char) c; |
fe50e814 |
189 | from_backend(0, &cc, 1); |
190 | } |
191 | |
32874aea |
192 | static void log_option(char *sender, int cmd, int option) |
193 | { |
374330e2 |
194 | char buf[50]; |
195 | sprintf(buf, "%s:\t%s %s", sender, |
196 | (cmd == WILL ? "WILL" : cmd == WONT ? "WONT" : |
197 | cmd == DO ? "DO" : cmd == DONT ? "DONT" : "<??>"), |
198 | telopt(option)); |
c5e9c988 |
199 | logevent(buf); |
374330e2 |
200 | } |
201 | |
32874aea |
202 | static void send_opt(int cmd, int option) |
203 | { |
374330e2 |
204 | unsigned char b[3]; |
205 | |
32874aea |
206 | b[0] = IAC; |
207 | b[1] = cmd; |
208 | b[2] = option; |
8df7a775 |
209 | sk_write(s, b, 3); |
374330e2 |
210 | log_option("client", cmd, option); |
211 | } |
212 | |
32874aea |
213 | static void deactivate_option(struct Opt *o) |
214 | { |
374330e2 |
215 | if (o->state == REQUESTED || o->state == ACTIVE) |
32874aea |
216 | send_opt(o->nsend, o->option); |
374330e2 |
217 | o->state = REALLY_INACTIVE; |
218 | } |
219 | |
708bbbbe |
220 | /* |
221 | * Generate side effects of enabling or disabling an option. |
222 | */ |
32874aea |
223 | static void option_side_effects(struct Opt *o, int enabled) |
224 | { |
0965bee0 |
225 | if (o->option == TELOPT_ECHO && o->send == DO) |
32874aea |
226 | echoing = !enabled; |
b6c680d4 |
227 | else if (o->option == TELOPT_SGA && o->send == DO) |
32874aea |
228 | editing = !enabled; |
229 | ldisc_send(NULL, 0); /* cause ldisc to notice the change */ |
8faa456c |
230 | |
231 | /* Ensure we get the minimum options */ |
232 | if (!activated) { |
233 | if (o_echo.state == INACTIVE) { |
234 | o_echo.state = REQUESTED; |
235 | send_opt(o_echo.send, o_echo.option); |
236 | } |
237 | if (o_we_sga.state == INACTIVE) { |
238 | o_we_sga.state = REQUESTED; |
239 | send_opt(o_we_sga.send, o_we_sga.option); |
240 | } |
241 | if (o_they_sga.state == INACTIVE) { |
242 | o_they_sga.state = REQUESTED; |
243 | send_opt(o_they_sga.send, o_they_sga.option); |
244 | } |
245 | activated = TRUE; |
246 | } |
708bbbbe |
247 | } |
248 | |
32874aea |
249 | static void activate_option(struct Opt *o) |
250 | { |
374330e2 |
251 | if (o->send == WILL && o->option == TELOPT_NAWS) |
252 | telnet_size(); |
253 | if (o->send == WILL && |
254 | (o->option == TELOPT_NEW_ENVIRON || |
255 | o->option == TELOPT_OLD_ENVIRON)) { |
256 | /* |
257 | * We may only have one kind of ENVIRON going at a time. |
258 | * This is a hack, but who cares. |
259 | */ |
32874aea |
260 | deactivate_option(o->option == |
261 | TELOPT_NEW_ENVIRON ? &o_oenv : &o_nenv); |
374330e2 |
262 | } |
708bbbbe |
263 | option_side_effects(o, 1); |
374330e2 |
264 | } |
265 | |
32874aea |
266 | static void refused_option(struct Opt *o) |
267 | { |
374330e2 |
268 | if (o->send == WILL && o->option == TELOPT_NEW_ENVIRON && |
269 | o_oenv.state == INACTIVE) { |
32874aea |
270 | send_opt(WILL, TELOPT_OLD_ENVIRON); |
374330e2 |
271 | o_oenv.state = REQUESTED; |
272 | } |
708bbbbe |
273 | option_side_effects(o, 0); |
374330e2 |
274 | } |
275 | |
32874aea |
276 | static void proc_rec_opt(int cmd, int option) |
277 | { |
374330e2 |
278 | struct Opt **o; |
279 | |
32874aea |
280 | log_option("server", cmd, option); |
374330e2 |
281 | for (o = opts; *o; o++) { |
282 | if ((*o)->option == option && (*o)->ack == cmd) { |
283 | switch ((*o)->state) { |
284 | case REQUESTED: |
285 | (*o)->state = ACTIVE; |
32874aea |
286 | activate_option(*o); |
374330e2 |
287 | break; |
288 | case ACTIVE: |
289 | break; |
290 | case INACTIVE: |
291 | (*o)->state = ACTIVE; |
32874aea |
292 | send_opt((*o)->send, option); |
293 | activate_option(*o); |
374330e2 |
294 | break; |
295 | case REALLY_INACTIVE: |
32874aea |
296 | send_opt((*o)->nsend, option); |
374330e2 |
297 | break; |
298 | } |
299 | return; |
300 | } else if ((*o)->option == option && (*o)->nak == cmd) { |
301 | switch ((*o)->state) { |
302 | case REQUESTED: |
303 | (*o)->state = INACTIVE; |
32874aea |
304 | refused_option(*o); |
374330e2 |
305 | break; |
306 | case ACTIVE: |
307 | (*o)->state = INACTIVE; |
32874aea |
308 | send_opt((*o)->nsend, option); |
309 | option_side_effects(*o, 0); |
374330e2 |
310 | break; |
311 | case INACTIVE: |
312 | case REALLY_INACTIVE: |
313 | break; |
314 | } |
315 | return; |
316 | } |
317 | } |
318 | /* |
319 | * If we reach here, the option was one we weren't prepared to |
320 | * cope with. So send a negative ack. |
321 | */ |
32874aea |
322 | send_opt((cmd == WILL ? DONT : WONT), option); |
374330e2 |
323 | } |
324 | |
32874aea |
325 | static void process_subneg(void) |
326 | { |
374330e2 |
327 | unsigned char b[2048], *p, *q; |
328 | int var, value, n; |
329 | char *e; |
330 | |
331 | switch (sb_opt) { |
332 | case TELOPT_TSPEED: |
333 | if (sb_len == 1 && sb_buf[0] == TELQUAL_SEND) { |
32874aea |
334 | char logbuf[sizeof(cfg.termspeed) + 80]; |
335 | b[0] = IAC; |
336 | b[1] = SB; |
337 | b[2] = TELOPT_TSPEED; |
374330e2 |
338 | b[3] = TELQUAL_IS; |
32874aea |
339 | strcpy(b + 4, cfg.termspeed); |
374330e2 |
340 | n = 4 + strlen(cfg.termspeed); |
32874aea |
341 | b[n] = IAC; |
342 | b[n + 1] = SE; |
343 | sk_write(s, b, n + 2); |
c5e9c988 |
344 | logevent("server:\tSB TSPEED SEND"); |
374330e2 |
345 | sprintf(logbuf, "client:\tSB TSPEED IS %s", cfg.termspeed); |
32874aea |
346 | logevent(logbuf); |
374330e2 |
347 | } else |
32874aea |
348 | logevent("server:\tSB TSPEED <something weird>"); |
374330e2 |
349 | break; |
350 | case TELOPT_TTYPE: |
351 | if (sb_len == 1 && sb_buf[0] == TELQUAL_SEND) { |
32874aea |
352 | char logbuf[sizeof(cfg.termtype) + 80]; |
353 | b[0] = IAC; |
354 | b[1] = SB; |
355 | b[2] = TELOPT_TTYPE; |
374330e2 |
356 | b[3] = TELQUAL_IS; |
357 | for (n = 0; cfg.termtype[n]; n++) |
32874aea |
358 | b[n + 4] = (cfg.termtype[n] >= 'a' |
359 | && cfg.termtype[n] <= |
360 | 'z' ? cfg.termtype[n] + 'A' - |
361 | 'a' : cfg.termtype[n]); |
362 | b[n + 4] = IAC; |
363 | b[n + 5] = SE; |
364 | sk_write(s, b, n + 6); |
365 | b[n + 4] = 0; |
c5e9c988 |
366 | logevent("server:\tSB TTYPE SEND"); |
32874aea |
367 | sprintf(logbuf, "client:\tSB TTYPE IS %s", b + 4); |
c5e9c988 |
368 | logevent(logbuf); |
374330e2 |
369 | } else |
c5e9c988 |
370 | logevent("server:\tSB TTYPE <something weird>\r\n"); |
374330e2 |
371 | break; |
372 | case TELOPT_OLD_ENVIRON: |
32874aea |
373 | case TELOPT_NEW_ENVIRON: |
374330e2 |
374 | p = sb_buf; |
375 | q = p + sb_len; |
376 | if (p < q && *p == TELQUAL_SEND) { |
377 | char logbuf[50]; |
378 | p++; |
32874aea |
379 | sprintf(logbuf, "server:\tSB %s SEND", telopt(sb_opt)); |
380 | logevent(logbuf); |
374330e2 |
381 | if (sb_opt == TELOPT_OLD_ENVIRON) { |
382 | if (cfg.rfc_environ) { |
383 | value = RFC_VALUE; |
384 | var = RFC_VAR; |
385 | } else { |
386 | value = BSD_VALUE; |
387 | var = BSD_VAR; |
388 | } |
389 | /* |
390 | * Try to guess the sense of VAR and VALUE. |
391 | */ |
392 | while (p < q) { |
393 | if (*p == RFC_VAR) { |
394 | value = RFC_VALUE; |
395 | var = RFC_VAR; |
396 | } else if (*p == BSD_VAR) { |
397 | value = BSD_VALUE; |
398 | var = BSD_VAR; |
399 | } |
400 | p++; |
401 | } |
402 | } else { |
403 | /* |
404 | * With NEW_ENVIRON, the sense of VAR and VALUE |
405 | * isn't in doubt. |
406 | */ |
407 | value = RFC_VALUE; |
408 | var = RFC_VAR; |
409 | } |
32874aea |
410 | b[0] = IAC; |
411 | b[1] = SB; |
412 | b[2] = sb_opt; |
374330e2 |
413 | b[3] = TELQUAL_IS; |
414 | n = 4; |
32874aea |
415 | e = cfg.environmt; |
374330e2 |
416 | while (*e) { |
417 | b[n++] = var; |
32874aea |
418 | while (*e && *e != '\t') |
419 | b[n++] = *e++; |
420 | if (*e == '\t') |
421 | e++; |
374330e2 |
422 | b[n++] = value; |
32874aea |
423 | while (*e) |
424 | b[n++] = *e++; |
374330e2 |
425 | e++; |
426 | } |
427 | if (*cfg.username) { |
32874aea |
428 | b[n++] = var; |
429 | b[n++] = 'U'; |
430 | b[n++] = 'S'; |
431 | b[n++] = 'E'; |
432 | b[n++] = 'R'; |
433 | b[n++] = value; |
374330e2 |
434 | e = cfg.username; |
32874aea |
435 | while (*e) |
436 | b[n++] = *e++; |
374330e2 |
437 | } |
32874aea |
438 | b[n++] = IAC; |
439 | b[n++] = SE; |
8df7a775 |
440 | sk_write(s, b, n); |
374330e2 |
441 | sprintf(logbuf, "client:\tSB %s IS %s", telopt(sb_opt), |
32874aea |
442 | n == 6 ? "<nothing>" : "<stuff>"); |
443 | logevent(logbuf); |
374330e2 |
444 | } |
445 | break; |
446 | } |
447 | } |
448 | |
449 | static enum { |
450 | TOPLEVEL, SEENIAC, SEENWILL, SEENWONT, SEENDO, SEENDONT, |
451 | SEENSB, SUBNEGOT, SUBNEG_IAC, SEENCR |
452 | } telnet_state = TOPLEVEL; |
453 | |
32874aea |
454 | static void do_telnet_read(char *buf, int len) |
455 | { |
374330e2 |
456 | |
457 | while (len--) { |
458 | int c = (unsigned char) *buf++; |
459 | |
460 | switch (telnet_state) { |
461 | case TOPLEVEL: |
462 | case SEENCR: |
463 | if (c == NUL && telnet_state == SEENCR) |
464 | telnet_state = TOPLEVEL; |
465 | else if (c == IAC) |
466 | telnet_state = SEENIAC; |
467 | else { |
374330e2 |
468 | if (!in_synch) |
c9def1b8 |
469 | c_write1(c); |
2f938b83 |
470 | |
471 | #if 1 |
472 | /* I can't get the F***ing winsock to insert the urgent IAC |
473 | * into the right position! Even with SO_OOBINLINE it gives |
474 | * it to recv too soon. And of course the DM byte (that |
475 | * arrives in the same packet!) appears several K later!! |
476 | * |
477 | * Oh well, we do get the DM in the right place so I'll |
478 | * just stop hiding on the next 0xf2 and hope for the best. |
479 | */ |
32874aea |
480 | else if (c == DM) |
481 | in_synch = 0; |
2f938b83 |
482 | #endif |
374330e2 |
483 | if (c == CR) |
484 | telnet_state = SEENCR; |
485 | else |
486 | telnet_state = TOPLEVEL; |
487 | } |
488 | break; |
489 | case SEENIAC: |
32874aea |
490 | if (c == DO) |
491 | telnet_state = SEENDO; |
492 | else if (c == DONT) |
493 | telnet_state = SEENDONT; |
494 | else if (c == WILL) |
495 | telnet_state = SEENWILL; |
496 | else if (c == WONT) |
497 | telnet_state = SEENWONT; |
498 | else if (c == SB) |
499 | telnet_state = SEENSB; |
2f938b83 |
500 | else if (c == DM) { |
32874aea |
501 | in_synch = 0; |
2f938b83 |
502 | telnet_state = TOPLEVEL; |
32874aea |
503 | } else { |
ded38628 |
504 | /* ignore everything else; print it if it's IAC */ |
505 | if (c == IAC) { |
c9def1b8 |
506 | c_write1(c); |
ded38628 |
507 | } |
300d41b0 |
508 | telnet_state = TOPLEVEL; |
509 | } |
374330e2 |
510 | break; |
511 | case SEENWILL: |
32874aea |
512 | proc_rec_opt(WILL, c); |
374330e2 |
513 | telnet_state = TOPLEVEL; |
514 | break; |
515 | case SEENWONT: |
32874aea |
516 | proc_rec_opt(WONT, c); |
374330e2 |
517 | telnet_state = TOPLEVEL; |
518 | break; |
519 | case SEENDO: |
32874aea |
520 | proc_rec_opt(DO, c); |
374330e2 |
521 | telnet_state = TOPLEVEL; |
522 | break; |
523 | case SEENDONT: |
32874aea |
524 | proc_rec_opt(DONT, c); |
374330e2 |
525 | telnet_state = TOPLEVEL; |
526 | break; |
527 | case SEENSB: |
528 | sb_opt = c; |
529 | sb_len = 0; |
530 | telnet_state = SUBNEGOT; |
531 | break; |
532 | case SUBNEGOT: |
533 | if (c == IAC) |
534 | telnet_state = SUBNEG_IAC; |
535 | else { |
32874aea |
536 | subneg_addchar: |
374330e2 |
537 | if (sb_len >= sb_size) { |
538 | char *newbuf; |
539 | sb_size += SB_DELTA; |
540 | newbuf = (sb_buf ? |
c9def1b8 |
541 | srealloc(sb_buf, sb_size) : |
542 | smalloc(sb_size)); |
374330e2 |
543 | if (newbuf) |
544 | sb_buf = newbuf; |
545 | else |
546 | sb_size -= SB_DELTA; |
547 | } |
548 | if (sb_len < sb_size) |
549 | sb_buf[sb_len++] = c; |
32874aea |
550 | telnet_state = SUBNEGOT; /* in case we came here by goto */ |
374330e2 |
551 | } |
552 | break; |
553 | case SUBNEG_IAC: |
554 | if (c != SE) |
555 | goto subneg_addchar; /* yes, it's a hack, I know, but... */ |
556 | else { |
557 | process_subneg(); |
558 | telnet_state = TOPLEVEL; |
559 | } |
560 | break; |
561 | } |
562 | } |
563 | } |
564 | |
32874aea |
565 | static int telnet_closing(Plug plug, char *error_msg, int error_code, |
566 | int calling_back) |
567 | { |
7e78000d |
568 | sk_close(s); |
569 | s = NULL; |
570 | if (error_msg) { |
32874aea |
571 | /* A socket error has occurred. */ |
572 | connection_fatal(error_msg); |
573 | } /* Otherwise, the remote side closed the connection normally. */ |
7e78000d |
574 | return 0; |
575 | } |
576 | |
32874aea |
577 | static int telnet_receive(Plug plug, int urgent, char *data, int len) |
578 | { |
579 | if (urgent) |
580 | in_synch = TRUE; |
581 | do_telnet_read(data, len); |
8df7a775 |
582 | return 1; |
583 | } |
584 | |
374330e2 |
585 | /* |
8df7a775 |
586 | * Called to set up the Telnet connection. |
374330e2 |
587 | * |
588 | * Returns an error message, or NULL on success. |
589 | * |
6e1ebb76 |
590 | * Also places the canonical host name into `realhost'. It must be |
591 | * freed by the caller. |
374330e2 |
592 | */ |
32874aea |
593 | static char *telnet_init(char *host, int port, char **realhost) |
594 | { |
7e78000d |
595 | static struct plug_function_table fn_table = { |
596 | telnet_closing, |
597 | telnet_receive |
598 | }, *fn_table_ptr = &fn_table; |
599 | |
8df7a775 |
600 | SockAddr addr; |
601 | char *err; |
374330e2 |
602 | |
603 | /* |
604 | * Try to find host. |
605 | */ |
8df7a775 |
606 | addr = sk_namelookup(host, realhost); |
32874aea |
607 | if ((err = sk_addr_error(addr))) |
8df7a775 |
608 | return err; |
374330e2 |
609 | |
610 | if (port < 0) |
611 | port = 23; /* default telnet port */ |
612 | |
613 | /* |
614 | * Open socket. |
615 | */ |
7e78000d |
616 | s = sk_new(addr, port, 0, 1, &fn_table_ptr); |
32874aea |
617 | if ((err = sk_socket_error(s))) |
8df7a775 |
618 | return err; |
374330e2 |
619 | |
8df7a775 |
620 | sk_addr_free(addr); |
374330e2 |
621 | |
622 | /* |
623 | * Initialise option states. |
624 | */ |
8faa456c |
625 | if (cfg.passive_telnet) { |
626 | struct Opt **o; |
627 | |
628 | for (o = opts; *o; o++) |
629 | if ((*o)->state == REQUESTED) |
630 | (*o)->state = INACTIVE; |
631 | } else { |
374330e2 |
632 | struct Opt **o; |
633 | |
634 | for (o = opts; *o; o++) |
635 | if ((*o)->state == REQUESTED) |
32874aea |
636 | send_opt((*o)->send, (*o)->option); |
8faa456c |
637 | activated = TRUE; |
374330e2 |
638 | } |
639 | |
374330e2 |
640 | /* |
641 | * Set up SYNCH state. |
642 | */ |
643 | in_synch = FALSE; |
6f34e365 |
644 | |
374330e2 |
645 | return NULL; |
646 | } |
647 | |
648 | /* |
374330e2 |
649 | * Called to send data down the Telnet connection. |
650 | */ |
32874aea |
651 | static void telnet_send(char *buf, int len) |
652 | { |
374330e2 |
653 | char *p; |
654 | static unsigned char iac[2] = { IAC, IAC }; |
655 | static unsigned char cr[2] = { CR, NUL }; |
a5f3e637 |
656 | #if 0 |
eb5e1db9 |
657 | static unsigned char nl[2] = { CR, LF }; |
a5f3e637 |
658 | #endif |
374330e2 |
659 | |
8df7a775 |
660 | if (s == NULL) |
374330e2 |
661 | return; |
662 | |
663 | p = buf; |
32874aea |
664 | while (p < buf + len) { |
374330e2 |
665 | char *q = p; |
666 | |
32874aea |
667 | while (iswritable((unsigned char) *p) && p < buf + len) |
668 | p++; |
669 | sk_write(s, q, p - q); |
374330e2 |
670 | |
32874aea |
671 | while (p < buf + len && !iswritable((unsigned char) *p)) { |
a5f3e637 |
672 | sk_write(s, (unsigned char) *p == IAC ? iac : cr, 2); |
374330e2 |
673 | p++; |
674 | } |
675 | } |
676 | } |
677 | |
678 | /* |
679 | * Called to set the size of the window from Telnet's POV. |
680 | */ |
32874aea |
681 | static void telnet_size(void) |
682 | { |
374330e2 |
683 | unsigned char b[16]; |
684 | char logbuf[50]; |
685 | |
8df7a775 |
686 | if (s == NULL || o_naws.state != ACTIVE) |
374330e2 |
687 | return; |
32874aea |
688 | b[0] = IAC; |
689 | b[1] = SB; |
690 | b[2] = TELOPT_NAWS; |
691 | b[3] = cols >> 8; |
692 | b[4] = cols & 0xFF; |
693 | b[5] = rows >> 8; |
694 | b[6] = rows & 0xFF; |
695 | b[7] = IAC; |
696 | b[8] = SE; |
8df7a775 |
697 | sk_write(s, b, 9); |
374330e2 |
698 | sprintf(logbuf, "client:\tSB NAWS %d,%d", |
32874aea |
699 | ((unsigned char) b[3] << 8) + (unsigned char) b[4], |
700 | ((unsigned char) b[5] << 8) + (unsigned char) b[6]); |
701 | logevent(logbuf); |
374330e2 |
702 | } |
703 | |
704 | /* |
705 | * Send Telnet special codes. |
706 | */ |
32874aea |
707 | static void telnet_special(Telnet_Special code) |
708 | { |
374330e2 |
709 | unsigned char b[2]; |
710 | |
8df7a775 |
711 | if (s == NULL) |
374330e2 |
712 | return; |
713 | |
714 | b[0] = IAC; |
715 | switch (code) { |
32874aea |
716 | case TS_AYT: |
717 | b[1] = AYT; |
718 | sk_write(s, b, 2); |
719 | break; |
720 | case TS_BRK: |
721 | b[1] = BREAK; |
722 | sk_write(s, b, 2); |
723 | break; |
724 | case TS_EC: |
725 | b[1] = EC; |
726 | sk_write(s, b, 2); |
727 | break; |
728 | case TS_EL: |
729 | b[1] = EL; |
730 | sk_write(s, b, 2); |
731 | break; |
732 | case TS_GA: |
733 | b[1] = GA; |
734 | sk_write(s, b, 2); |
735 | break; |
736 | case TS_NOP: |
737 | b[1] = NOP; |
738 | sk_write(s, b, 2); |
739 | break; |
740 | case TS_ABORT: |
741 | b[1] = ABORT; |
742 | sk_write(s, b, 2); |
743 | break; |
744 | case TS_AO: |
745 | b[1] = AO; |
746 | sk_write(s, b, 2); |
747 | break; |
748 | case TS_IP: |
749 | b[1] = IP; |
750 | sk_write(s, b, 2); |
751 | break; |
752 | case TS_SUSP: |
753 | b[1] = SUSP; |
754 | sk_write(s, b, 2); |
755 | break; |
756 | case TS_EOR: |
757 | b[1] = EOR; |
758 | sk_write(s, b, 2); |
759 | break; |
760 | case TS_EOF: |
761 | b[1] = xEOF; |
762 | sk_write(s, b, 2); |
763 | break; |
a5f3e637 |
764 | case TS_EOL: |
765 | sk_write(s, "\r\n", 2); |
766 | break; |
374330e2 |
767 | case TS_SYNCH: |
32874aea |
768 | b[1] = DM; |
769 | sk_write(s, b, 1); |
770 | sk_write_oob(s, b + 1, 1); |
771 | break; |
684d367c |
772 | case TS_RECHO: |
773 | if (o_echo.state == INACTIVE || o_echo.state == REALLY_INACTIVE) { |
774 | o_echo.state = REQUESTED; |
32874aea |
775 | send_opt(o_echo.send, o_echo.option); |
684d367c |
776 | } |
777 | break; |
778 | case TS_LECHO: |
779 | if (o_echo.state == ACTIVE) { |
780 | o_echo.state = REQUESTED; |
32874aea |
781 | send_opt(o_echo.nsend, o_echo.option); |
684d367c |
782 | } |
374330e2 |
783 | break; |
32874aea |
784 | case TS_PING: |
ec55b220 |
785 | if (o_they_sga.state == ACTIVE) { |
32874aea |
786 | b[1] = NOP; |
787 | sk_write(s, b, 2); |
ec55b220 |
788 | } |
32874aea |
789 | break; |
374330e2 |
790 | } |
791 | } |
792 | |
32874aea |
793 | static Socket telnet_socket(void) |
794 | { |
795 | return s; |
796 | } |
8ccc75b0 |
797 | |
32874aea |
798 | static int telnet_sendok(void) |
799 | { |
800 | return 1; |
801 | } |
4017be6d |
802 | |
32874aea |
803 | static int telnet_ldisc(int option) |
804 | { |
805 | if (option == LD_ECHO) |
806 | return echoing; |
807 | if (option == LD_EDIT) |
808 | return editing; |
0965bee0 |
809 | return FALSE; |
810 | } |
811 | |
374330e2 |
812 | Backend telnet_backend = { |
813 | telnet_init, |
374330e2 |
814 | telnet_send, |
815 | telnet_size, |
4017be6d |
816 | telnet_special, |
8ccc75b0 |
817 | telnet_socket, |
97db3be4 |
818 | telnet_sendok, |
0965bee0 |
819 | telnet_ldisc, |
97db3be4 |
820 | 23 |
374330e2 |
821 | }; |