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 | |
5471d09a |
177 | #define TELNET_MAX_BACKLOG 4096 |
178 | |
0965bee0 |
179 | static int echoing = TRUE, editing = TRUE; |
8faa456c |
180 | static int activated = FALSE; |
5471d09a |
181 | static int telnet_bufsize; |
374330e2 |
182 | static int in_synch; |
374330e2 |
183 | static int sb_opt, sb_len; |
184 | static char *sb_buf = NULL; |
185 | static int sb_size = 0; |
186 | #define SB_DELTA 1024 |
187 | |
32874aea |
188 | static void c_write1(int c) |
189 | { |
5471d09a |
190 | int backlog; |
32874aea |
191 | char cc = (char) c; |
5471d09a |
192 | backlog = from_backend(0, &cc, 1); |
193 | sk_set_frozen(s, backlog > TELNET_MAX_BACKLOG); |
fe50e814 |
194 | } |
195 | |
32874aea |
196 | static void log_option(char *sender, int cmd, int option) |
197 | { |
374330e2 |
198 | char buf[50]; |
199 | sprintf(buf, "%s:\t%s %s", sender, |
200 | (cmd == WILL ? "WILL" : cmd == WONT ? "WONT" : |
201 | cmd == DO ? "DO" : cmd == DONT ? "DONT" : "<??>"), |
202 | telopt(option)); |
c5e9c988 |
203 | logevent(buf); |
374330e2 |
204 | } |
205 | |
32874aea |
206 | static void send_opt(int cmd, int option) |
207 | { |
374330e2 |
208 | unsigned char b[3]; |
209 | |
32874aea |
210 | b[0] = IAC; |
211 | b[1] = cmd; |
212 | b[2] = option; |
5471d09a |
213 | telnet_bufsize = sk_write(s, b, 3); |
374330e2 |
214 | log_option("client", cmd, option); |
215 | } |
216 | |
32874aea |
217 | static void deactivate_option(struct Opt *o) |
218 | { |
374330e2 |
219 | if (o->state == REQUESTED || o->state == ACTIVE) |
32874aea |
220 | send_opt(o->nsend, o->option); |
374330e2 |
221 | o->state = REALLY_INACTIVE; |
222 | } |
223 | |
708bbbbe |
224 | /* |
225 | * Generate side effects of enabling or disabling an option. |
226 | */ |
32874aea |
227 | static void option_side_effects(struct Opt *o, int enabled) |
228 | { |
0965bee0 |
229 | if (o->option == TELOPT_ECHO && o->send == DO) |
32874aea |
230 | echoing = !enabled; |
b6c680d4 |
231 | else if (o->option == TELOPT_SGA && o->send == DO) |
32874aea |
232 | editing = !enabled; |
233 | ldisc_send(NULL, 0); /* cause ldisc to notice the change */ |
8faa456c |
234 | |
235 | /* Ensure we get the minimum options */ |
236 | if (!activated) { |
237 | if (o_echo.state == INACTIVE) { |
238 | o_echo.state = REQUESTED; |
239 | send_opt(o_echo.send, o_echo.option); |
240 | } |
241 | if (o_we_sga.state == INACTIVE) { |
242 | o_we_sga.state = REQUESTED; |
243 | send_opt(o_we_sga.send, o_we_sga.option); |
244 | } |
245 | if (o_they_sga.state == INACTIVE) { |
246 | o_they_sga.state = REQUESTED; |
247 | send_opt(o_they_sga.send, o_they_sga.option); |
248 | } |
249 | activated = TRUE; |
250 | } |
708bbbbe |
251 | } |
252 | |
32874aea |
253 | static void activate_option(struct Opt *o) |
254 | { |
374330e2 |
255 | if (o->send == WILL && o->option == TELOPT_NAWS) |
256 | telnet_size(); |
257 | if (o->send == WILL && |
258 | (o->option == TELOPT_NEW_ENVIRON || |
259 | o->option == TELOPT_OLD_ENVIRON)) { |
260 | /* |
261 | * We may only have one kind of ENVIRON going at a time. |
262 | * This is a hack, but who cares. |
263 | */ |
32874aea |
264 | deactivate_option(o->option == |
265 | TELOPT_NEW_ENVIRON ? &o_oenv : &o_nenv); |
374330e2 |
266 | } |
708bbbbe |
267 | option_side_effects(o, 1); |
374330e2 |
268 | } |
269 | |
32874aea |
270 | static void refused_option(struct Opt *o) |
271 | { |
374330e2 |
272 | if (o->send == WILL && o->option == TELOPT_NEW_ENVIRON && |
273 | o_oenv.state == INACTIVE) { |
32874aea |
274 | send_opt(WILL, TELOPT_OLD_ENVIRON); |
374330e2 |
275 | o_oenv.state = REQUESTED; |
276 | } |
708bbbbe |
277 | option_side_effects(o, 0); |
374330e2 |
278 | } |
279 | |
32874aea |
280 | static void proc_rec_opt(int cmd, int option) |
281 | { |
374330e2 |
282 | struct Opt **o; |
283 | |
32874aea |
284 | log_option("server", cmd, option); |
374330e2 |
285 | for (o = opts; *o; o++) { |
286 | if ((*o)->option == option && (*o)->ack == cmd) { |
287 | switch ((*o)->state) { |
288 | case REQUESTED: |
289 | (*o)->state = ACTIVE; |
32874aea |
290 | activate_option(*o); |
374330e2 |
291 | break; |
292 | case ACTIVE: |
293 | break; |
294 | case INACTIVE: |
295 | (*o)->state = ACTIVE; |
32874aea |
296 | send_opt((*o)->send, option); |
297 | activate_option(*o); |
374330e2 |
298 | break; |
299 | case REALLY_INACTIVE: |
32874aea |
300 | send_opt((*o)->nsend, option); |
374330e2 |
301 | break; |
302 | } |
303 | return; |
304 | } else if ((*o)->option == option && (*o)->nak == cmd) { |
305 | switch ((*o)->state) { |
306 | case REQUESTED: |
307 | (*o)->state = INACTIVE; |
32874aea |
308 | refused_option(*o); |
374330e2 |
309 | break; |
310 | case ACTIVE: |
311 | (*o)->state = INACTIVE; |
32874aea |
312 | send_opt((*o)->nsend, option); |
313 | option_side_effects(*o, 0); |
374330e2 |
314 | break; |
315 | case INACTIVE: |
316 | case REALLY_INACTIVE: |
317 | break; |
318 | } |
319 | return; |
320 | } |
321 | } |
322 | /* |
323 | * If we reach here, the option was one we weren't prepared to |
324 | * cope with. So send a negative ack. |
325 | */ |
32874aea |
326 | send_opt((cmd == WILL ? DONT : WONT), option); |
374330e2 |
327 | } |
328 | |
32874aea |
329 | static void process_subneg(void) |
330 | { |
374330e2 |
331 | unsigned char b[2048], *p, *q; |
332 | int var, value, n; |
333 | char *e; |
334 | |
335 | switch (sb_opt) { |
336 | case TELOPT_TSPEED: |
337 | if (sb_len == 1 && sb_buf[0] == TELQUAL_SEND) { |
32874aea |
338 | char logbuf[sizeof(cfg.termspeed) + 80]; |
339 | b[0] = IAC; |
340 | b[1] = SB; |
341 | b[2] = TELOPT_TSPEED; |
374330e2 |
342 | b[3] = TELQUAL_IS; |
32874aea |
343 | strcpy(b + 4, cfg.termspeed); |
374330e2 |
344 | n = 4 + strlen(cfg.termspeed); |
32874aea |
345 | b[n] = IAC; |
346 | b[n + 1] = SE; |
5471d09a |
347 | telnet_bufsize = sk_write(s, b, n + 2); |
c5e9c988 |
348 | logevent("server:\tSB TSPEED SEND"); |
374330e2 |
349 | sprintf(logbuf, "client:\tSB TSPEED IS %s", cfg.termspeed); |
32874aea |
350 | logevent(logbuf); |
374330e2 |
351 | } else |
32874aea |
352 | logevent("server:\tSB TSPEED <something weird>"); |
374330e2 |
353 | break; |
354 | case TELOPT_TTYPE: |
355 | if (sb_len == 1 && sb_buf[0] == TELQUAL_SEND) { |
32874aea |
356 | char logbuf[sizeof(cfg.termtype) + 80]; |
357 | b[0] = IAC; |
358 | b[1] = SB; |
359 | b[2] = TELOPT_TTYPE; |
374330e2 |
360 | b[3] = TELQUAL_IS; |
361 | for (n = 0; cfg.termtype[n]; n++) |
32874aea |
362 | b[n + 4] = (cfg.termtype[n] >= 'a' |
363 | && cfg.termtype[n] <= |
364 | 'z' ? cfg.termtype[n] + 'A' - |
365 | 'a' : cfg.termtype[n]); |
366 | b[n + 4] = IAC; |
367 | b[n + 5] = SE; |
5471d09a |
368 | telnet_bufsize = sk_write(s, b, n + 6); |
32874aea |
369 | b[n + 4] = 0; |
c5e9c988 |
370 | logevent("server:\tSB TTYPE SEND"); |
32874aea |
371 | sprintf(logbuf, "client:\tSB TTYPE IS %s", b + 4); |
c5e9c988 |
372 | logevent(logbuf); |
374330e2 |
373 | } else |
c5e9c988 |
374 | logevent("server:\tSB TTYPE <something weird>\r\n"); |
374330e2 |
375 | break; |
376 | case TELOPT_OLD_ENVIRON: |
32874aea |
377 | case TELOPT_NEW_ENVIRON: |
374330e2 |
378 | p = sb_buf; |
379 | q = p + sb_len; |
380 | if (p < q && *p == TELQUAL_SEND) { |
381 | char logbuf[50]; |
382 | p++; |
32874aea |
383 | sprintf(logbuf, "server:\tSB %s SEND", telopt(sb_opt)); |
384 | logevent(logbuf); |
374330e2 |
385 | if (sb_opt == TELOPT_OLD_ENVIRON) { |
386 | if (cfg.rfc_environ) { |
387 | value = RFC_VALUE; |
388 | var = RFC_VAR; |
389 | } else { |
390 | value = BSD_VALUE; |
391 | var = BSD_VAR; |
392 | } |
393 | /* |
394 | * Try to guess the sense of VAR and VALUE. |
395 | */ |
396 | while (p < q) { |
397 | if (*p == RFC_VAR) { |
398 | value = RFC_VALUE; |
399 | var = RFC_VAR; |
400 | } else if (*p == BSD_VAR) { |
401 | value = BSD_VALUE; |
402 | var = BSD_VAR; |
403 | } |
404 | p++; |
405 | } |
406 | } else { |
407 | /* |
408 | * With NEW_ENVIRON, the sense of VAR and VALUE |
409 | * isn't in doubt. |
410 | */ |
411 | value = RFC_VALUE; |
412 | var = RFC_VAR; |
413 | } |
32874aea |
414 | b[0] = IAC; |
415 | b[1] = SB; |
416 | b[2] = sb_opt; |
374330e2 |
417 | b[3] = TELQUAL_IS; |
418 | n = 4; |
32874aea |
419 | e = cfg.environmt; |
374330e2 |
420 | while (*e) { |
421 | b[n++] = var; |
32874aea |
422 | while (*e && *e != '\t') |
423 | b[n++] = *e++; |
424 | if (*e == '\t') |
425 | e++; |
374330e2 |
426 | b[n++] = value; |
32874aea |
427 | while (*e) |
428 | b[n++] = *e++; |
374330e2 |
429 | e++; |
430 | } |
431 | if (*cfg.username) { |
32874aea |
432 | b[n++] = var; |
433 | b[n++] = 'U'; |
434 | b[n++] = 'S'; |
435 | b[n++] = 'E'; |
436 | b[n++] = 'R'; |
437 | b[n++] = value; |
374330e2 |
438 | e = cfg.username; |
32874aea |
439 | while (*e) |
440 | b[n++] = *e++; |
374330e2 |
441 | } |
32874aea |
442 | b[n++] = IAC; |
443 | b[n++] = SE; |
5471d09a |
444 | telnet_bufsize = sk_write(s, b, n); |
374330e2 |
445 | sprintf(logbuf, "client:\tSB %s IS %s", telopt(sb_opt), |
32874aea |
446 | n == 6 ? "<nothing>" : "<stuff>"); |
447 | logevent(logbuf); |
374330e2 |
448 | } |
449 | break; |
450 | } |
451 | } |
452 | |
453 | static enum { |
454 | TOPLEVEL, SEENIAC, SEENWILL, SEENWONT, SEENDO, SEENDONT, |
455 | SEENSB, SUBNEGOT, SUBNEG_IAC, SEENCR |
456 | } telnet_state = TOPLEVEL; |
457 | |
32874aea |
458 | static void do_telnet_read(char *buf, int len) |
459 | { |
374330e2 |
460 | |
461 | while (len--) { |
462 | int c = (unsigned char) *buf++; |
463 | |
464 | switch (telnet_state) { |
465 | case TOPLEVEL: |
466 | case SEENCR: |
467 | if (c == NUL && telnet_state == SEENCR) |
468 | telnet_state = TOPLEVEL; |
469 | else if (c == IAC) |
470 | telnet_state = SEENIAC; |
471 | else { |
374330e2 |
472 | if (!in_synch) |
c9def1b8 |
473 | c_write1(c); |
2f938b83 |
474 | |
475 | #if 1 |
476 | /* I can't get the F***ing winsock to insert the urgent IAC |
477 | * into the right position! Even with SO_OOBINLINE it gives |
478 | * it to recv too soon. And of course the DM byte (that |
479 | * arrives in the same packet!) appears several K later!! |
480 | * |
481 | * Oh well, we do get the DM in the right place so I'll |
482 | * just stop hiding on the next 0xf2 and hope for the best. |
483 | */ |
32874aea |
484 | else if (c == DM) |
485 | in_synch = 0; |
2f938b83 |
486 | #endif |
374330e2 |
487 | if (c == CR) |
488 | telnet_state = SEENCR; |
489 | else |
490 | telnet_state = TOPLEVEL; |
491 | } |
492 | break; |
493 | case SEENIAC: |
32874aea |
494 | if (c == DO) |
495 | telnet_state = SEENDO; |
496 | else if (c == DONT) |
497 | telnet_state = SEENDONT; |
498 | else if (c == WILL) |
499 | telnet_state = SEENWILL; |
500 | else if (c == WONT) |
501 | telnet_state = SEENWONT; |
502 | else if (c == SB) |
503 | telnet_state = SEENSB; |
2f938b83 |
504 | else if (c == DM) { |
32874aea |
505 | in_synch = 0; |
2f938b83 |
506 | telnet_state = TOPLEVEL; |
32874aea |
507 | } else { |
ded38628 |
508 | /* ignore everything else; print it if it's IAC */ |
509 | if (c == IAC) { |
c9def1b8 |
510 | c_write1(c); |
ded38628 |
511 | } |
300d41b0 |
512 | telnet_state = TOPLEVEL; |
513 | } |
374330e2 |
514 | break; |
515 | case SEENWILL: |
32874aea |
516 | proc_rec_opt(WILL, c); |
374330e2 |
517 | telnet_state = TOPLEVEL; |
518 | break; |
519 | case SEENWONT: |
32874aea |
520 | proc_rec_opt(WONT, c); |
374330e2 |
521 | telnet_state = TOPLEVEL; |
522 | break; |
523 | case SEENDO: |
32874aea |
524 | proc_rec_opt(DO, c); |
374330e2 |
525 | telnet_state = TOPLEVEL; |
526 | break; |
527 | case SEENDONT: |
32874aea |
528 | proc_rec_opt(DONT, c); |
374330e2 |
529 | telnet_state = TOPLEVEL; |
530 | break; |
531 | case SEENSB: |
532 | sb_opt = c; |
533 | sb_len = 0; |
534 | telnet_state = SUBNEGOT; |
535 | break; |
536 | case SUBNEGOT: |
537 | if (c == IAC) |
538 | telnet_state = SUBNEG_IAC; |
539 | else { |
32874aea |
540 | subneg_addchar: |
374330e2 |
541 | if (sb_len >= sb_size) { |
542 | char *newbuf; |
543 | sb_size += SB_DELTA; |
544 | newbuf = (sb_buf ? |
c9def1b8 |
545 | srealloc(sb_buf, sb_size) : |
546 | smalloc(sb_size)); |
374330e2 |
547 | if (newbuf) |
548 | sb_buf = newbuf; |
549 | else |
550 | sb_size -= SB_DELTA; |
551 | } |
552 | if (sb_len < sb_size) |
553 | sb_buf[sb_len++] = c; |
32874aea |
554 | telnet_state = SUBNEGOT; /* in case we came here by goto */ |
374330e2 |
555 | } |
556 | break; |
557 | case SUBNEG_IAC: |
558 | if (c != SE) |
559 | goto subneg_addchar; /* yes, it's a hack, I know, but... */ |
560 | else { |
561 | process_subneg(); |
562 | telnet_state = TOPLEVEL; |
563 | } |
564 | break; |
565 | } |
566 | } |
567 | } |
568 | |
32874aea |
569 | static int telnet_closing(Plug plug, char *error_msg, int error_code, |
570 | int calling_back) |
571 | { |
f3ab576e |
572 | if (s) { |
573 | sk_close(s); |
574 | s = NULL; |
575 | } |
7e78000d |
576 | if (error_msg) { |
32874aea |
577 | /* A socket error has occurred. */ |
578 | connection_fatal(error_msg); |
579 | } /* Otherwise, the remote side closed the connection normally. */ |
7e78000d |
580 | return 0; |
581 | } |
582 | |
32874aea |
583 | static int telnet_receive(Plug plug, int urgent, char *data, int len) |
584 | { |
585 | if (urgent) |
586 | in_synch = TRUE; |
587 | do_telnet_read(data, len); |
8df7a775 |
588 | return 1; |
589 | } |
590 | |
3ad9d396 |
591 | static void telnet_sent(Plug plug, int bufsize) |
592 | { |
593 | telnet_bufsize = bufsize; |
594 | } |
595 | |
374330e2 |
596 | /* |
8df7a775 |
597 | * Called to set up the Telnet connection. |
374330e2 |
598 | * |
599 | * Returns an error message, or NULL on success. |
600 | * |
6e1ebb76 |
601 | * Also places the canonical host name into `realhost'. It must be |
602 | * freed by the caller. |
374330e2 |
603 | */ |
32874aea |
604 | static char *telnet_init(char *host, int port, char **realhost) |
605 | { |
7e78000d |
606 | static struct plug_function_table fn_table = { |
607 | telnet_closing, |
3ad9d396 |
608 | telnet_receive, |
609 | telnet_sent |
7e78000d |
610 | }, *fn_table_ptr = &fn_table; |
611 | |
8df7a775 |
612 | SockAddr addr; |
613 | char *err; |
374330e2 |
614 | |
615 | /* |
616 | * Try to find host. |
617 | */ |
3ad9d396 |
618 | { |
619 | char buf[200]; |
620 | sprintf(buf, "Looking up host \"%.170s\"", host); |
621 | logevent(buf); |
622 | } |
8df7a775 |
623 | addr = sk_namelookup(host, realhost); |
32874aea |
624 | if ((err = sk_addr_error(addr))) |
8df7a775 |
625 | return err; |
374330e2 |
626 | |
627 | if (port < 0) |
628 | port = 23; /* default telnet port */ |
629 | |
630 | /* |
631 | * Open socket. |
632 | */ |
3ad9d396 |
633 | { |
634 | char buf[200], addrbuf[100]; |
635 | sk_getaddr(addr, addrbuf, 100); |
636 | sprintf(buf, "Connecting to %.100s port %d", addrbuf, port); |
637 | logevent(buf); |
638 | } |
7e78000d |
639 | s = sk_new(addr, port, 0, 1, &fn_table_ptr); |
32874aea |
640 | if ((err = sk_socket_error(s))) |
8df7a775 |
641 | return err; |
374330e2 |
642 | |
8df7a775 |
643 | sk_addr_free(addr); |
374330e2 |
644 | |
645 | /* |
646 | * Initialise option states. |
647 | */ |
8faa456c |
648 | if (cfg.passive_telnet) { |
649 | struct Opt **o; |
650 | |
651 | for (o = opts; *o; o++) |
652 | if ((*o)->state == REQUESTED) |
653 | (*o)->state = INACTIVE; |
654 | } else { |
374330e2 |
655 | struct Opt **o; |
656 | |
657 | for (o = opts; *o; o++) |
658 | if ((*o)->state == REQUESTED) |
32874aea |
659 | send_opt((*o)->send, (*o)->option); |
8faa456c |
660 | activated = TRUE; |
374330e2 |
661 | } |
662 | |
374330e2 |
663 | /* |
664 | * Set up SYNCH state. |
665 | */ |
666 | in_synch = FALSE; |
6f34e365 |
667 | |
374330e2 |
668 | return NULL; |
669 | } |
670 | |
671 | /* |
374330e2 |
672 | * Called to send data down the Telnet connection. |
673 | */ |
5471d09a |
674 | static int telnet_send(char *buf, int len) |
32874aea |
675 | { |
374330e2 |
676 | char *p; |
677 | static unsigned char iac[2] = { IAC, IAC }; |
678 | static unsigned char cr[2] = { CR, NUL }; |
a5f3e637 |
679 | #if 0 |
eb5e1db9 |
680 | static unsigned char nl[2] = { CR, LF }; |
a5f3e637 |
681 | #endif |
374330e2 |
682 | |
8df7a775 |
683 | if (s == NULL) |
5471d09a |
684 | return 0; |
374330e2 |
685 | |
686 | p = buf; |
32874aea |
687 | while (p < buf + len) { |
374330e2 |
688 | char *q = p; |
689 | |
32874aea |
690 | while (iswritable((unsigned char) *p) && p < buf + len) |
691 | p++; |
5471d09a |
692 | telnet_bufsize = sk_write(s, q, p - q); |
374330e2 |
693 | |
32874aea |
694 | while (p < buf + len && !iswritable((unsigned char) *p)) { |
5471d09a |
695 | telnet_bufsize = |
696 | sk_write(s, (unsigned char) *p == IAC ? iac : cr, 2); |
374330e2 |
697 | p++; |
698 | } |
699 | } |
5471d09a |
700 | |
701 | return telnet_bufsize; |
702 | } |
703 | |
704 | /* |
705 | * Called to query the current socket sendability status. |
706 | */ |
707 | static int telnet_sendbuffer(void) |
708 | { |
709 | return telnet_bufsize; |
374330e2 |
710 | } |
711 | |
712 | /* |
713 | * Called to set the size of the window from Telnet's POV. |
714 | */ |
32874aea |
715 | static void telnet_size(void) |
716 | { |
374330e2 |
717 | unsigned char b[16]; |
718 | char logbuf[50]; |
719 | |
8df7a775 |
720 | if (s == NULL || o_naws.state != ACTIVE) |
374330e2 |
721 | return; |
32874aea |
722 | b[0] = IAC; |
723 | b[1] = SB; |
724 | b[2] = TELOPT_NAWS; |
725 | b[3] = cols >> 8; |
726 | b[4] = cols & 0xFF; |
727 | b[5] = rows >> 8; |
728 | b[6] = rows & 0xFF; |
729 | b[7] = IAC; |
730 | b[8] = SE; |
5471d09a |
731 | telnet_bufsize = sk_write(s, b, 9); |
374330e2 |
732 | sprintf(logbuf, "client:\tSB NAWS %d,%d", |
32874aea |
733 | ((unsigned char) b[3] << 8) + (unsigned char) b[4], |
734 | ((unsigned char) b[5] << 8) + (unsigned char) b[6]); |
735 | logevent(logbuf); |
374330e2 |
736 | } |
737 | |
738 | /* |
739 | * Send Telnet special codes. |
740 | */ |
32874aea |
741 | static void telnet_special(Telnet_Special code) |
742 | { |
374330e2 |
743 | unsigned char b[2]; |
744 | |
8df7a775 |
745 | if (s == NULL) |
374330e2 |
746 | return; |
747 | |
748 | b[0] = IAC; |
749 | switch (code) { |
32874aea |
750 | case TS_AYT: |
751 | b[1] = AYT; |
5471d09a |
752 | telnet_bufsize = sk_write(s, b, 2); |
32874aea |
753 | break; |
754 | case TS_BRK: |
755 | b[1] = BREAK; |
5471d09a |
756 | telnet_bufsize = sk_write(s, b, 2); |
32874aea |
757 | break; |
758 | case TS_EC: |
759 | b[1] = EC; |
5471d09a |
760 | telnet_bufsize = sk_write(s, b, 2); |
32874aea |
761 | break; |
762 | case TS_EL: |
763 | b[1] = EL; |
5471d09a |
764 | telnet_bufsize = sk_write(s, b, 2); |
32874aea |
765 | break; |
766 | case TS_GA: |
767 | b[1] = GA; |
5471d09a |
768 | telnet_bufsize = sk_write(s, b, 2); |
32874aea |
769 | break; |
770 | case TS_NOP: |
771 | b[1] = NOP; |
5471d09a |
772 | telnet_bufsize = sk_write(s, b, 2); |
32874aea |
773 | break; |
774 | case TS_ABORT: |
775 | b[1] = ABORT; |
5471d09a |
776 | telnet_bufsize = sk_write(s, b, 2); |
32874aea |
777 | break; |
778 | case TS_AO: |
779 | b[1] = AO; |
5471d09a |
780 | telnet_bufsize = sk_write(s, b, 2); |
32874aea |
781 | break; |
782 | case TS_IP: |
783 | b[1] = IP; |
5471d09a |
784 | telnet_bufsize = sk_write(s, b, 2); |
32874aea |
785 | break; |
786 | case TS_SUSP: |
787 | b[1] = SUSP; |
5471d09a |
788 | telnet_bufsize = sk_write(s, b, 2); |
32874aea |
789 | break; |
790 | case TS_EOR: |
791 | b[1] = EOR; |
5471d09a |
792 | telnet_bufsize = sk_write(s, b, 2); |
32874aea |
793 | break; |
794 | case TS_EOF: |
795 | b[1] = xEOF; |
5471d09a |
796 | telnet_bufsize = sk_write(s, b, 2); |
32874aea |
797 | break; |
a5f3e637 |
798 | case TS_EOL: |
5471d09a |
799 | telnet_bufsize = sk_write(s, "\r\n", 2); |
a5f3e637 |
800 | break; |
374330e2 |
801 | case TS_SYNCH: |
32874aea |
802 | b[1] = DM; |
5471d09a |
803 | telnet_bufsize = sk_write(s, b, 1); |
804 | telnet_bufsize = sk_write_oob(s, b + 1, 1); |
32874aea |
805 | break; |
684d367c |
806 | case TS_RECHO: |
807 | if (o_echo.state == INACTIVE || o_echo.state == REALLY_INACTIVE) { |
808 | o_echo.state = REQUESTED; |
32874aea |
809 | send_opt(o_echo.send, o_echo.option); |
684d367c |
810 | } |
811 | break; |
812 | case TS_LECHO: |
813 | if (o_echo.state == ACTIVE) { |
814 | o_echo.state = REQUESTED; |
32874aea |
815 | send_opt(o_echo.nsend, o_echo.option); |
684d367c |
816 | } |
374330e2 |
817 | break; |
32874aea |
818 | case TS_PING: |
ec55b220 |
819 | if (o_they_sga.state == ACTIVE) { |
32874aea |
820 | b[1] = NOP; |
5471d09a |
821 | telnet_bufsize = sk_write(s, b, 2); |
ec55b220 |
822 | } |
32874aea |
823 | break; |
374330e2 |
824 | } |
825 | } |
826 | |
32874aea |
827 | static Socket telnet_socket(void) |
828 | { |
829 | return s; |
830 | } |
8ccc75b0 |
831 | |
32874aea |
832 | static int telnet_sendok(void) |
833 | { |
834 | return 1; |
835 | } |
4017be6d |
836 | |
5471d09a |
837 | static void telnet_unthrottle(int backlog) |
838 | { |
839 | sk_set_frozen(s, backlog > TELNET_MAX_BACKLOG); |
840 | } |
841 | |
32874aea |
842 | static int telnet_ldisc(int option) |
843 | { |
844 | if (option == LD_ECHO) |
845 | return echoing; |
846 | if (option == LD_EDIT) |
847 | return editing; |
0965bee0 |
848 | return FALSE; |
849 | } |
850 | |
374330e2 |
851 | Backend telnet_backend = { |
852 | telnet_init, |
374330e2 |
853 | telnet_send, |
5471d09a |
854 | telnet_sendbuffer, |
374330e2 |
855 | telnet_size, |
4017be6d |
856 | telnet_special, |
8ccc75b0 |
857 | telnet_socket, |
97db3be4 |
858 | telnet_sendok, |
0965bee0 |
859 | telnet_ldisc, |
5471d09a |
860 | telnet_unthrottle, |
97db3be4 |
861 | 23 |
374330e2 |
862 | }; |