374330e2 |
1 | #include <windows.h> |
2 | #include <stdio.h> |
3 | #include <stdlib.h> |
4 | #include <winsock.h> |
5 | |
6 | #include "putty.h" |
7 | |
8 | #ifndef FALSE |
9 | #define FALSE 0 |
10 | #endif |
11 | #ifndef TRUE |
12 | #define TRUE 1 |
13 | #endif |
14 | |
15 | static SOCKET s = INVALID_SOCKET; |
16 | |
17 | #define IAC 255 /* interpret as command: */ |
18 | #define DONT 254 /* you are not to use option */ |
19 | #define DO 253 /* please, you use option */ |
20 | #define WONT 252 /* I won't use option */ |
21 | #define WILL 251 /* I will use option */ |
22 | #define SB 250 /* interpret as subnegotiation */ |
23 | #define SE 240 /* end sub negotiation */ |
24 | |
25 | #define GA 249 /* you may reverse the line */ |
26 | #define EL 248 /* erase the current line */ |
27 | #define EC 247 /* erase the current character */ |
28 | #define AYT 246 /* are you there */ |
29 | #define AO 245 /* abort output--but let prog finish */ |
30 | #define IP 244 /* interrupt process--permanently */ |
31 | #define BREAK 243 /* break */ |
32 | #define DM 242 /* data mark--for connect. cleaning */ |
33 | #define NOP 241 /* nop */ |
34 | #define EOR 239 /* end of record (transparent mode) */ |
35 | #define ABORT 238 /* Abort process */ |
36 | #define SUSP 237 /* Suspend process */ |
37 | #define xEOF 236 /* End of file: EOF is already used... */ |
38 | |
39 | #define TELOPT_BINARY 0 /* 8-bit data path */ |
40 | #define TELOPT_ECHO 1 /* echo */ |
41 | #define TELOPT_RCP 2 /* prepare to reconnect */ |
42 | #define TELOPT_SGA 3 /* suppress go ahead */ |
43 | #define TELOPT_NAMS 4 /* approximate message size */ |
44 | #define TELOPT_STATUS 5 /* give status */ |
45 | #define TELOPT_TM 6 /* timing mark */ |
46 | #define TELOPT_RCTE 7 /* remote controlled transmission and echo */ |
47 | #define TELOPT_NAOL 8 /* negotiate about output line width */ |
48 | #define TELOPT_NAOP 9 /* negotiate about output page size */ |
49 | #define TELOPT_NAOCRD 10 /* negotiate about CR disposition */ |
50 | #define TELOPT_NAOHTS 11 /* negotiate about horizontal tabstops */ |
51 | #define TELOPT_NAOHTD 12 /* negotiate about horizontal tab disposition */ |
52 | #define TELOPT_NAOFFD 13 /* negotiate about formfeed disposition */ |
53 | #define TELOPT_NAOVTS 14 /* negotiate about vertical tab stops */ |
54 | #define TELOPT_NAOVTD 15 /* negotiate about vertical tab disposition */ |
55 | #define TELOPT_NAOLFD 16 /* negotiate about output LF disposition */ |
56 | #define TELOPT_XASCII 17 /* extended ascic character set */ |
57 | #define TELOPT_LOGOUT 18 /* force logout */ |
58 | #define TELOPT_BM 19 /* byte macro */ |
59 | #define TELOPT_DET 20 /* data entry terminal */ |
60 | #define TELOPT_SUPDUP 21 /* supdup protocol */ |
61 | #define TELOPT_SUPDUPOUTPUT 22 /* supdup output */ |
62 | #define TELOPT_SNDLOC 23 /* send location */ |
63 | #define TELOPT_TTYPE 24 /* terminal type */ |
64 | #define TELOPT_EOR 25 /* end or record */ |
65 | #define TELOPT_TUID 26 /* TACACS user identification */ |
66 | #define TELOPT_OUTMRK 27 /* output marking */ |
67 | #define TELOPT_TTYLOC 28 /* terminal location number */ |
68 | #define TELOPT_3270REGIME 29 /* 3270 regime */ |
69 | #define TELOPT_X3PAD 30 /* X.3 PAD */ |
70 | #define TELOPT_NAWS 31 /* window size */ |
71 | #define TELOPT_TSPEED 32 /* terminal speed */ |
72 | #define TELOPT_LFLOW 33 /* remote flow control */ |
73 | #define TELOPT_LINEMODE 34 /* Linemode option */ |
74 | #define TELOPT_XDISPLOC 35 /* X Display Location */ |
75 | #define TELOPT_OLD_ENVIRON 36 /* Old - Environment variables */ |
76 | #define TELOPT_AUTHENTICATION 37/* Authenticate */ |
77 | #define TELOPT_ENCRYPT 38 /* Encryption option */ |
78 | #define TELOPT_NEW_ENVIRON 39 /* New - Environment variables */ |
79 | #define TELOPT_EXOPL 255 /* extended-options-list */ |
80 | |
81 | #define TELQUAL_IS 0 /* option is... */ |
82 | #define TELQUAL_SEND 1 /* send option */ |
83 | #define TELQUAL_INFO 2 /* ENVIRON: informational version of IS */ |
84 | #define BSD_VAR 1 |
85 | #define BSD_VALUE 0 |
86 | #define RFC_VAR 0 |
87 | #define RFC_VALUE 1 |
88 | |
89 | #define CR 13 |
90 | #define LF 10 |
91 | #define NUL 0 |
92 | |
93 | #define iswritable(x) ( (x) != IAC && (x) != CR ) |
94 | |
95 | static char *telopt(int opt) { |
96 | #define i(x) if (opt == TELOPT_ ## x) return #x; |
97 | i(BINARY); i(ECHO); i(RCP); i(SGA); i(NAMS); i(STATUS); i(TM); i(RCTE); |
98 | i(NAOL); i(NAOP); i(NAOCRD); i(NAOHTS); i(NAOHTD); i(NAOFFD); i(NAOVTS); |
99 | i(NAOVTD); i(NAOLFD); i(XASCII); i(LOGOUT); i(BM); i(DET); i(SUPDUP); |
100 | i(SUPDUPOUTPUT); i(SNDLOC); i(TTYPE); i(EOR); i(TUID); i(OUTMRK); |
101 | i(TTYLOC); i(X3PAD); i(NAWS); i(TSPEED); i(LFLOW); i(LINEMODE); |
102 | i(XDISPLOC); i(OLD_ENVIRON); i(AUTHENTICATION); i(ENCRYPT); |
103 | i(NEW_ENVIRON); i(EXOPL); |
104 | #undef i |
105 | return "<unknown>"; |
106 | } |
107 | |
108 | static void telnet_size(void); |
109 | |
110 | struct Opt { |
111 | int send; /* what we initially send */ |
112 | int nsend; /* -ve send if requested to stop it */ |
113 | int ack, nak; /* +ve and -ve acknowledgements */ |
114 | int option; /* the option code */ |
115 | enum { |
116 | REQUESTED, ACTIVE, INACTIVE, REALLY_INACTIVE |
117 | } state; |
118 | }; |
119 | |
120 | static struct Opt o_naws = {WILL, WONT, DO, DONT, TELOPT_NAWS, REQUESTED}; |
121 | static struct Opt o_tspeed = {WILL, WONT, DO, DONT, TELOPT_TSPEED, REQUESTED}; |
122 | static struct Opt o_ttype = {WILL, WONT, DO, DONT, TELOPT_TTYPE, REQUESTED}; |
123 | static struct Opt o_oenv = {WILL, WONT, DO, DONT, TELOPT_OLD_ENVIRON, |
124 | INACTIVE}; |
125 | static struct Opt o_nenv = {WILL, WONT, DO, DONT, TELOPT_NEW_ENVIRON, |
126 | REQUESTED}; |
127 | static struct Opt o_echo = {DO, DONT, WILL, WONT, TELOPT_ECHO, REQUESTED}; |
128 | static struct Opt o_we_sga = {WILL, WONT, DO, DONT, TELOPT_SGA, REQUESTED}; |
129 | static struct Opt o_they_sga = {DO, DONT, WILL, WONT, TELOPT_SGA, REQUESTED}; |
130 | |
131 | static struct Opt *opts[] = { |
132 | &o_naws, &o_tspeed, &o_ttype, &o_oenv, &o_nenv, &o_echo, |
133 | &o_we_sga, &o_they_sga, NULL |
134 | }; |
135 | |
136 | #if 0 |
137 | static int in_synch; |
138 | #endif |
139 | |
140 | static int sb_opt, sb_len; |
141 | static char *sb_buf = NULL; |
142 | static int sb_size = 0; |
143 | #define SB_DELTA 1024 |
144 | |
145 | static void try_write (void) { |
146 | while (outbuf_head != outbuf_reap) { |
147 | int end = (outbuf_reap < outbuf_head ? outbuf_head : OUTBUF_SIZE); |
148 | int len = end - outbuf_reap; |
149 | int ret; |
150 | |
151 | ret = send (s, outbuf+outbuf_reap, len, 0); |
152 | if (ret > 0) |
153 | outbuf_reap = (outbuf_reap + ret) & OUTBUF_MASK; |
154 | if (ret < len) |
155 | return; |
156 | } |
157 | } |
158 | |
159 | static void s_write (void *buf, int len) { |
160 | unsigned char *p = buf; |
161 | while (len--) { |
162 | int new_head = (outbuf_head + 1) & OUTBUF_MASK; |
163 | if (new_head != outbuf_reap) { |
164 | outbuf[outbuf_head] = *p++; |
165 | outbuf_head = new_head; |
166 | } |
167 | } |
168 | try_write(); |
169 | } |
170 | |
171 | static void c_write (char *buf, int len) { |
172 | while (len--) { |
173 | int new_head = (inbuf_head + 1) & INBUF_MASK; |
374330e2 |
174 | if (new_head != inbuf_reap) { |
175 | inbuf[inbuf_head] = *buf++; |
176 | inbuf_head = new_head; |
177 | } |
178 | } |
179 | } |
180 | |
181 | static void log_option (char *sender, int cmd, int option) { |
182 | char buf[50]; |
183 | sprintf(buf, "%s:\t%s %s", sender, |
184 | (cmd == WILL ? "WILL" : cmd == WONT ? "WONT" : |
185 | cmd == DO ? "DO" : cmd == DONT ? "DONT" : "<??>"), |
186 | telopt(option)); |
c5e9c988 |
187 | logevent(buf); |
374330e2 |
188 | } |
189 | |
190 | static void send_opt (int cmd, int option) { |
191 | unsigned char b[3]; |
192 | |
193 | b[0] = IAC; b[1] = cmd; b[2] = option; |
194 | s_write (b, 3); |
195 | log_option("client", cmd, option); |
196 | } |
197 | |
198 | static void deactivate_option (struct Opt *o) { |
199 | if (o->state == REQUESTED || o->state == ACTIVE) |
200 | send_opt (o->nsend, o->option); |
201 | o->state = REALLY_INACTIVE; |
202 | } |
203 | |
204 | static void activate_option (struct Opt *o) { |
205 | if (o->send == WILL && o->option == TELOPT_NAWS) |
206 | telnet_size(); |
207 | if (o->send == WILL && |
208 | (o->option == TELOPT_NEW_ENVIRON || |
209 | o->option == TELOPT_OLD_ENVIRON)) { |
210 | /* |
211 | * We may only have one kind of ENVIRON going at a time. |
212 | * This is a hack, but who cares. |
213 | */ |
214 | deactivate_option (o->option==TELOPT_NEW_ENVIRON ? &o_oenv : &o_nenv); |
215 | } |
216 | } |
217 | |
218 | static void refused_option (struct Opt *o) { |
219 | if (o->send == WILL && o->option == TELOPT_NEW_ENVIRON && |
220 | o_oenv.state == INACTIVE) { |
221 | send_opt (WILL, TELOPT_OLD_ENVIRON); |
222 | o_oenv.state = REQUESTED; |
223 | } |
224 | } |
225 | |
226 | static void proc_rec_opt (int cmd, int option) { |
227 | struct Opt **o; |
228 | |
229 | log_option ("server", cmd, option); |
230 | for (o = opts; *o; o++) { |
231 | if ((*o)->option == option && (*o)->ack == cmd) { |
232 | switch ((*o)->state) { |
233 | case REQUESTED: |
234 | (*o)->state = ACTIVE; |
235 | activate_option (*o); |
236 | break; |
237 | case ACTIVE: |
238 | break; |
239 | case INACTIVE: |
240 | (*o)->state = ACTIVE; |
241 | send_opt ((*o)->send, option); |
242 | activate_option (*o); |
243 | break; |
244 | case REALLY_INACTIVE: |
245 | send_opt ((*o)->nsend, option); |
246 | break; |
247 | } |
248 | return; |
249 | } else if ((*o)->option == option && (*o)->nak == cmd) { |
250 | switch ((*o)->state) { |
251 | case REQUESTED: |
252 | (*o)->state = INACTIVE; |
253 | refused_option (*o); |
254 | break; |
255 | case ACTIVE: |
256 | (*o)->state = INACTIVE; |
257 | send_opt ((*o)->nsend, option); |
258 | break; |
259 | case INACTIVE: |
260 | case REALLY_INACTIVE: |
261 | break; |
262 | } |
263 | return; |
264 | } |
265 | } |
266 | /* |
267 | * If we reach here, the option was one we weren't prepared to |
268 | * cope with. So send a negative ack. |
269 | */ |
270 | send_opt ((cmd == WILL ? DONT : WONT), option); |
271 | } |
272 | |
273 | static void process_subneg (void) { |
274 | unsigned char b[2048], *p, *q; |
275 | int var, value, n; |
276 | char *e; |
277 | |
278 | switch (sb_opt) { |
279 | case TELOPT_TSPEED: |
280 | if (sb_len == 1 && sb_buf[0] == TELQUAL_SEND) { |
281 | char logbuf[sizeof(cfg.termspeed)+80]; |
282 | b[0] = IAC; b[1] = SB; b[2] = TELOPT_TSPEED; |
283 | b[3] = TELQUAL_IS; |
284 | strcpy(b+4, cfg.termspeed); |
285 | n = 4 + strlen(cfg.termspeed); |
286 | b[n] = IAC; b[n+1] = SE; |
287 | s_write (b, n+2); |
c5e9c988 |
288 | logevent("server:\tSB TSPEED SEND"); |
374330e2 |
289 | sprintf(logbuf, "client:\tSB TSPEED IS %s", cfg.termspeed); |
c5e9c988 |
290 | logevent (logbuf); |
374330e2 |
291 | } else |
c5e9c988 |
292 | logevent ("server:\tSB TSPEED <something weird>"); |
374330e2 |
293 | break; |
294 | case TELOPT_TTYPE: |
295 | if (sb_len == 1 && sb_buf[0] == TELQUAL_SEND) { |
296 | char logbuf[sizeof(cfg.termtype)+80]; |
297 | b[0] = IAC; b[1] = SB; b[2] = TELOPT_TTYPE; |
298 | b[3] = TELQUAL_IS; |
299 | for (n = 0; cfg.termtype[n]; n++) |
300 | b[n+4] = (cfg.termtype[n] >= 'a' && cfg.termtype[n] <= 'z' ? |
301 | cfg.termtype[n] + 'A'-'a' : cfg.termtype[n]); |
302 | b[n+4] = IAC; b[n+5] = SE; |
303 | s_write (b, n+6); |
304 | b[n+4] = 0; |
c5e9c988 |
305 | logevent("server:\tSB TTYPE SEND"); |
374330e2 |
306 | sprintf(logbuf, "client:\tSB TTYPE IS %s", b+4); |
c5e9c988 |
307 | logevent(logbuf); |
374330e2 |
308 | } else |
c5e9c988 |
309 | logevent("server:\tSB TTYPE <something weird>\r\n"); |
374330e2 |
310 | break; |
311 | case TELOPT_OLD_ENVIRON: |
312 | case TELOPT_NEW_ENVIRON: |
313 | p = sb_buf; |
314 | q = p + sb_len; |
315 | if (p < q && *p == TELQUAL_SEND) { |
316 | char logbuf[50]; |
317 | p++; |
318 | sprintf (logbuf, "server:\tSB %s SEND", telopt(sb_opt)); |
c5e9c988 |
319 | logevent (logbuf); |
374330e2 |
320 | if (sb_opt == TELOPT_OLD_ENVIRON) { |
321 | if (cfg.rfc_environ) { |
322 | value = RFC_VALUE; |
323 | var = RFC_VAR; |
324 | } else { |
325 | value = BSD_VALUE; |
326 | var = BSD_VAR; |
327 | } |
328 | /* |
329 | * Try to guess the sense of VAR and VALUE. |
330 | */ |
331 | while (p < q) { |
332 | if (*p == RFC_VAR) { |
333 | value = RFC_VALUE; |
334 | var = RFC_VAR; |
335 | } else if (*p == BSD_VAR) { |
336 | value = BSD_VALUE; |
337 | var = BSD_VAR; |
338 | } |
339 | p++; |
340 | } |
341 | } else { |
342 | /* |
343 | * With NEW_ENVIRON, the sense of VAR and VALUE |
344 | * isn't in doubt. |
345 | */ |
346 | value = RFC_VALUE; |
347 | var = RFC_VAR; |
348 | } |
349 | b[0] = IAC; b[1] = SB; b[2] = sb_opt; |
350 | b[3] = TELQUAL_IS; |
351 | n = 4; |
37508af4 |
352 | e = cfg.environmt; |
374330e2 |
353 | while (*e) { |
354 | b[n++] = var; |
355 | while (*e && *e != '\t') b[n++] = *e++; |
356 | if (*e == '\t') e++; |
357 | b[n++] = value; |
358 | while (*e) b[n++] = *e++; |
359 | e++; |
360 | } |
361 | if (*cfg.username) { |
362 | b[n++] = var; b[n++] = 'U'; b[n++] = 'S'; |
363 | b[n++] = 'E'; b[n++] = 'R'; b[n++] = value; |
364 | e = cfg.username; |
365 | while (*e) b[n++] = *e++; |
366 | } |
367 | b[n++] = IAC; b[n++] = SE; |
368 | s_write (b, n); |
369 | sprintf(logbuf, "client:\tSB %s IS %s", telopt(sb_opt), |
370 | n==6 ? "<nothing>" : "<stuff>"); |
c5e9c988 |
371 | logevent (logbuf); |
374330e2 |
372 | } |
373 | break; |
374 | } |
375 | } |
376 | |
377 | static enum { |
378 | TOPLEVEL, SEENIAC, SEENWILL, SEENWONT, SEENDO, SEENDONT, |
379 | SEENSB, SUBNEGOT, SUBNEG_IAC, SEENCR |
380 | } telnet_state = TOPLEVEL; |
381 | |
382 | static void do_telnet_read (char *buf, int len) { |
383 | unsigned char b[10]; |
384 | |
385 | while (len--) { |
386 | int c = (unsigned char) *buf++; |
387 | |
388 | switch (telnet_state) { |
389 | case TOPLEVEL: |
390 | case SEENCR: |
391 | if (c == NUL && telnet_state == SEENCR) |
392 | telnet_state = TOPLEVEL; |
393 | else if (c == IAC) |
394 | telnet_state = SEENIAC; |
395 | else { |
396 | b[0] = c; |
397 | #if 0 |
398 | if (!in_synch) |
399 | #endif |
400 | c_write (b, 1); |
401 | if (c == CR) |
402 | telnet_state = SEENCR; |
403 | else |
404 | telnet_state = TOPLEVEL; |
405 | } |
406 | break; |
407 | case SEENIAC: |
408 | if (c == DO) telnet_state = SEENDO; |
409 | else if (c == DONT) telnet_state = SEENDONT; |
410 | else if (c == WILL) telnet_state = SEENWILL; |
411 | else if (c == WONT) telnet_state = SEENWONT; |
412 | else if (c == SB) telnet_state = SEENSB; |
300d41b0 |
413 | else { |
414 | /* ignore (and print) everything else */ |
415 | b[0] = c; |
416 | c_write(b,1); |
417 | telnet_state = TOPLEVEL; |
418 | } |
374330e2 |
419 | break; |
420 | case SEENWILL: |
421 | proc_rec_opt (WILL, c); |
422 | telnet_state = TOPLEVEL; |
423 | break; |
424 | case SEENWONT: |
425 | proc_rec_opt (WONT, c); |
426 | telnet_state = TOPLEVEL; |
427 | break; |
428 | case SEENDO: |
429 | proc_rec_opt (DO, c); |
430 | telnet_state = TOPLEVEL; |
431 | break; |
432 | case SEENDONT: |
433 | proc_rec_opt (DONT, c); |
434 | telnet_state = TOPLEVEL; |
435 | break; |
436 | case SEENSB: |
437 | sb_opt = c; |
438 | sb_len = 0; |
439 | telnet_state = SUBNEGOT; |
440 | break; |
441 | case SUBNEGOT: |
442 | if (c == IAC) |
443 | telnet_state = SUBNEG_IAC; |
444 | else { |
445 | subneg_addchar: |
446 | if (sb_len >= sb_size) { |
447 | char *newbuf; |
448 | sb_size += SB_DELTA; |
449 | newbuf = (sb_buf ? |
450 | realloc(sb_buf, sb_size) : |
451 | malloc(sb_size)); |
452 | if (newbuf) |
453 | sb_buf = newbuf; |
454 | else |
455 | sb_size -= SB_DELTA; |
456 | } |
457 | if (sb_len < sb_size) |
458 | sb_buf[sb_len++] = c; |
459 | telnet_state = SUBNEGOT;/* in case we came here by goto */ |
460 | } |
461 | break; |
462 | case SUBNEG_IAC: |
463 | if (c != SE) |
464 | goto subneg_addchar; /* yes, it's a hack, I know, but... */ |
465 | else { |
466 | process_subneg(); |
467 | telnet_state = TOPLEVEL; |
468 | } |
469 | break; |
470 | } |
471 | } |
472 | } |
473 | |
474 | /* |
475 | * Called to set up the Telnet connection. Will arrange for |
476 | * WM_NETEVENT messages to be passed to the specified window, whose |
477 | * window procedure should then call telnet_msg(). |
478 | * |
479 | * Returns an error message, or NULL on success. |
480 | * |
481 | * Also places the canonical host name into `realhost'. |
482 | */ |
483 | static char *telnet_init (HWND hwnd, char *host, int port, char **realhost) { |
484 | SOCKADDR_IN addr; |
485 | struct hostent *h; |
486 | unsigned long a; |
487 | |
488 | /* |
489 | * Try to find host. |
490 | */ |
491 | if ( (a = inet_addr(host)) == (unsigned long) INADDR_NONE) { |
492 | if ( (h = gethostbyname(host)) == NULL) |
493 | switch (WSAGetLastError()) { |
494 | case WSAENETDOWN: return "Network is down"; |
495 | case WSAHOST_NOT_FOUND: case WSANO_DATA: |
496 | return "Host does not exist"; |
497 | case WSATRY_AGAIN: return "Host not found"; |
498 | default: return "gethostbyname: unknown error"; |
499 | } |
500 | memcpy (&a, h->h_addr, sizeof(a)); |
501 | *realhost = h->h_name; |
502 | } else |
503 | *realhost = host; |
504 | a = ntohl(a); |
505 | |
506 | if (port < 0) |
507 | port = 23; /* default telnet port */ |
508 | |
509 | /* |
510 | * Open socket. |
511 | */ |
512 | s = socket(AF_INET, SOCK_STREAM, 0); |
513 | if (s == INVALID_SOCKET) |
514 | switch (WSAGetLastError()) { |
515 | case WSAENETDOWN: return "Network is down"; |
516 | case WSAEAFNOSUPPORT: return "TCP/IP support not present"; |
517 | default: return "socket(): unknown error"; |
518 | } |
519 | |
520 | #if 0 |
521 | { |
522 | BOOL b = TRUE; |
523 | setsockopt (s, SOL_SOCKET, SO_OOBINLINE, (void *)&b, sizeof(b)); |
524 | } |
525 | #endif |
526 | |
527 | /* |
528 | * Bind to local address. |
529 | */ |
530 | addr.sin_family = AF_INET; |
531 | addr.sin_addr.s_addr = htonl(INADDR_ANY); |
532 | addr.sin_port = htons(0); |
533 | if (bind (s, (struct sockaddr *)&addr, sizeof(addr)) == SOCKET_ERROR) |
534 | switch (WSAGetLastError()) { |
535 | case WSAENETDOWN: return "Network is down"; |
536 | default: return "bind(): unknown error"; |
537 | } |
538 | |
539 | /* |
540 | * Connect to remote address. |
541 | */ |
542 | addr.sin_addr.s_addr = htonl(a); |
543 | addr.sin_port = htons((short)port); |
544 | if (connect (s, (struct sockaddr *)&addr, sizeof(addr)) == SOCKET_ERROR) |
545 | switch (WSAGetLastError()) { |
546 | case WSAENETDOWN: return "Network is down"; |
547 | case WSAECONNREFUSED: return "Connection refused"; |
548 | case WSAENETUNREACH: return "Network is unreachable"; |
549 | case WSAEHOSTUNREACH: return "No route to host"; |
550 | default: return "connect(): unknown error"; |
551 | } |
552 | |
553 | if (WSAAsyncSelect (s, hwnd, WM_NETEVENT, FD_READ | |
554 | FD_WRITE | FD_OOB | FD_CLOSE) == SOCKET_ERROR) |
555 | switch (WSAGetLastError()) { |
556 | case WSAENETDOWN: return "Network is down"; |
557 | default: return "WSAAsyncSelect(): unknown error"; |
558 | } |
559 | |
560 | /* |
561 | * Initialise option states. |
562 | */ |
563 | { |
564 | struct Opt **o; |
565 | |
566 | for (o = opts; *o; o++) |
567 | if ((*o)->state == REQUESTED) |
568 | send_opt ((*o)->send, (*o)->option); |
569 | } |
570 | |
571 | #if 0 |
572 | /* |
573 | * Set up SYNCH state. |
574 | */ |
575 | in_synch = FALSE; |
576 | #endif |
577 | |
578 | return NULL; |
579 | } |
580 | |
581 | /* |
582 | * Process a WM_NETEVENT message. Will return 0 if the connection |
583 | * has closed, or <0 for a socket error. |
584 | */ |
585 | static int telnet_msg (WPARAM wParam, LPARAM lParam) { |
586 | int ret; |
587 | char buf[256]; |
588 | |
8ce72d2c |
589 | /* |
590 | * Because reading less than the whole of the available pending |
591 | * data can generate an FD_READ event, we need to allow for the |
592 | * possibility that FD_READ may arrive with FD_CLOSE already in |
593 | * the queue; so it's possible that we can get here even with s |
594 | * invalid. If so, we return 1 and don't worry about it. |
595 | */ |
596 | if (s == INVALID_SOCKET) |
597 | return 1; |
374330e2 |
598 | |
599 | if (WSAGETSELECTERROR(lParam) != 0) |
600 | return -WSAGETSELECTERROR(lParam); |
601 | |
602 | switch (WSAGETSELECTEVENT(lParam)) { |
603 | case FD_READ: |
8ce72d2c |
604 | case FD_CLOSE: |
374330e2 |
605 | ret = recv(s, buf, sizeof(buf), 0); |
606 | if (ret < 0 && WSAGetLastError() == WSAEWOULDBLOCK) |
607 | return 1; |
608 | if (ret < 0) /* any _other_ error */ |
609 | return -10000-WSAGetLastError(); |
610 | if (ret == 0) { |
611 | s = INVALID_SOCKET; |
8ce72d2c |
612 | return 0; |
374330e2 |
613 | } |
614 | #if 0 |
615 | if (in_synch) { |
616 | BOOL i; |
617 | if (ioctlsocket (s, SIOCATMARK, &i) < 0) { |
618 | return -20000-WSAGetLastError(); |
619 | } |
620 | if (i) |
621 | in_synch = FALSE; |
622 | } |
623 | #endif |
624 | do_telnet_read (buf, ret); |
625 | return 1; |
626 | case FD_OOB: |
627 | do { |
628 | ret = recv(s, buf, sizeof(buf), 0); |
629 | } while (ret > 0); |
630 | telnet_state = TOPLEVEL; |
631 | do { |
632 | ret = recv(s, buf, 1, MSG_OOB); |
633 | if (ret > 0) |
634 | do_telnet_read (buf, ret); |
635 | } while (ret > 0); |
636 | if (ret < 0 && WSAGetLastError() != WSAEWOULDBLOCK) |
637 | return -30000-WSAGetLastError(); |
638 | return 1; |
639 | case FD_WRITE: |
640 | if (outbuf_head != outbuf_reap) |
641 | try_write(); |
642 | return 1; |
374330e2 |
643 | } |
644 | return 1; /* shouldn't happen, but WTF */ |
645 | } |
646 | |
647 | /* |
648 | * Called to send data down the Telnet connection. |
649 | */ |
650 | static void telnet_send (char *buf, int len) { |
651 | char *p; |
652 | static unsigned char iac[2] = { IAC, IAC }; |
653 | static unsigned char cr[2] = { CR, NUL }; |
654 | |
655 | if (s == INVALID_SOCKET) |
656 | return; |
657 | |
658 | p = buf; |
659 | while (p < buf+len) { |
660 | char *q = p; |
661 | |
662 | while (iswritable((unsigned char)*p) && p < buf+len) p++; |
663 | s_write (q, p-q); |
664 | |
665 | while (p < buf+len && !iswritable((unsigned char)*p)) { |
666 | s_write ((unsigned char)*p == IAC ? iac : cr, 2); |
667 | p++; |
668 | } |
669 | } |
670 | } |
671 | |
672 | /* |
673 | * Called to set the size of the window from Telnet's POV. |
674 | */ |
675 | static void telnet_size(void) { |
676 | unsigned char b[16]; |
677 | char logbuf[50]; |
678 | |
679 | if (s == INVALID_SOCKET || o_naws.state != ACTIVE) |
680 | return; |
681 | b[0] = IAC; b[1] = SB; b[2] = TELOPT_NAWS; |
682 | b[3] = cols >> 8; b[4] = cols & 0xFF; |
683 | b[5] = rows >> 8; b[6] = rows & 0xFF; |
684 | b[7] = IAC; b[8] = SE; |
685 | s_write (b, 9); |
686 | sprintf(logbuf, "client:\tSB NAWS %d,%d", |
687 | ((unsigned char)b[3] << 8) + (unsigned char)b[4], |
688 | ((unsigned char)b[5] << 8) + (unsigned char)b[6]); |
c5e9c988 |
689 | logevent (logbuf); |
374330e2 |
690 | } |
691 | |
692 | /* |
693 | * Send Telnet special codes. |
694 | */ |
695 | static void telnet_special (Telnet_Special code) { |
696 | unsigned char b[2]; |
697 | |
698 | if (s == INVALID_SOCKET) |
699 | return; |
700 | |
701 | b[0] = IAC; |
702 | switch (code) { |
703 | case TS_AYT: b[1] = AYT; s_write (b, 2); break; |
704 | case TS_BRK: b[1] = BREAK; s_write (b, 2); break; |
705 | case TS_EC: b[1] = EC; s_write (b, 2); break; |
706 | case TS_EL: b[1] = EL; s_write (b, 2); break; |
707 | case TS_GA: b[1] = GA; s_write (b, 2); break; |
708 | case TS_NOP: b[1] = NOP; s_write (b, 2); break; |
709 | case TS_ABORT: b[1] = ABORT; s_write (b, 2); break; |
710 | case TS_AO: b[1] = AO; s_write (b, 2); break; |
711 | case TS_IP: b[1] = IP; s_write (b, 2); break; |
712 | case TS_SUSP: b[1] = SUSP; s_write (b, 2); break; |
713 | case TS_EOR: b[1] = EOR; s_write (b, 2); break; |
714 | case TS_EOF: b[1] = xEOF; s_write (b, 2); break; |
715 | case TS_SYNCH: |
716 | outbuf_head = outbuf_reap = 0; |
717 | b[0] = DM; |
718 | send (s, b, 1, MSG_OOB); |
719 | break; |
720 | } |
721 | } |
722 | |
723 | Backend telnet_backend = { |
724 | telnet_init, |
725 | telnet_msg, |
726 | telnet_send, |
727 | telnet_size, |
728 | telnet_special |
729 | }; |