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; |
cabfd08c |
177 | } else { |
178 | term_out(); |
179 | if( inbuf_head == inbuf_reap ) len++; else break; |
374330e2 |
180 | } |
181 | } |
182 | } |
183 | |
184 | static void log_option (char *sender, int cmd, int option) { |
185 | char buf[50]; |
186 | sprintf(buf, "%s:\t%s %s", sender, |
187 | (cmd == WILL ? "WILL" : cmd == WONT ? "WONT" : |
188 | cmd == DO ? "DO" : cmd == DONT ? "DONT" : "<??>"), |
189 | telopt(option)); |
c5e9c988 |
190 | logevent(buf); |
374330e2 |
191 | } |
192 | |
193 | static void send_opt (int cmd, int option) { |
194 | unsigned char b[3]; |
195 | |
196 | b[0] = IAC; b[1] = cmd; b[2] = option; |
197 | s_write (b, 3); |
198 | log_option("client", cmd, option); |
199 | } |
200 | |
201 | static void deactivate_option (struct Opt *o) { |
202 | if (o->state == REQUESTED || o->state == ACTIVE) |
203 | send_opt (o->nsend, o->option); |
204 | o->state = REALLY_INACTIVE; |
205 | } |
206 | |
207 | static void activate_option (struct Opt *o) { |
208 | if (o->send == WILL && o->option == TELOPT_NAWS) |
209 | telnet_size(); |
210 | if (o->send == WILL && |
211 | (o->option == TELOPT_NEW_ENVIRON || |
212 | o->option == TELOPT_OLD_ENVIRON)) { |
213 | /* |
214 | * We may only have one kind of ENVIRON going at a time. |
215 | * This is a hack, but who cares. |
216 | */ |
217 | deactivate_option (o->option==TELOPT_NEW_ENVIRON ? &o_oenv : &o_nenv); |
218 | } |
219 | } |
220 | |
221 | static void refused_option (struct Opt *o) { |
222 | if (o->send == WILL && o->option == TELOPT_NEW_ENVIRON && |
223 | o_oenv.state == INACTIVE) { |
224 | send_opt (WILL, TELOPT_OLD_ENVIRON); |
225 | o_oenv.state = REQUESTED; |
226 | } |
227 | } |
228 | |
229 | static void proc_rec_opt (int cmd, int option) { |
230 | struct Opt **o; |
231 | |
232 | log_option ("server", cmd, option); |
233 | for (o = opts; *o; o++) { |
234 | if ((*o)->option == option && (*o)->ack == cmd) { |
235 | switch ((*o)->state) { |
236 | case REQUESTED: |
237 | (*o)->state = ACTIVE; |
238 | activate_option (*o); |
239 | break; |
240 | case ACTIVE: |
241 | break; |
242 | case INACTIVE: |
243 | (*o)->state = ACTIVE; |
244 | send_opt ((*o)->send, option); |
245 | activate_option (*o); |
246 | break; |
247 | case REALLY_INACTIVE: |
248 | send_opt ((*o)->nsend, option); |
249 | break; |
250 | } |
251 | return; |
252 | } else if ((*o)->option == option && (*o)->nak == cmd) { |
253 | switch ((*o)->state) { |
254 | case REQUESTED: |
255 | (*o)->state = INACTIVE; |
256 | refused_option (*o); |
257 | break; |
258 | case ACTIVE: |
259 | (*o)->state = INACTIVE; |
260 | send_opt ((*o)->nsend, option); |
261 | break; |
262 | case INACTIVE: |
263 | case REALLY_INACTIVE: |
264 | break; |
265 | } |
266 | return; |
267 | } |
268 | } |
269 | /* |
270 | * If we reach here, the option was one we weren't prepared to |
271 | * cope with. So send a negative ack. |
272 | */ |
273 | send_opt ((cmd == WILL ? DONT : WONT), option); |
274 | } |
275 | |
276 | static void process_subneg (void) { |
277 | unsigned char b[2048], *p, *q; |
278 | int var, value, n; |
279 | char *e; |
280 | |
281 | switch (sb_opt) { |
282 | case TELOPT_TSPEED: |
283 | if (sb_len == 1 && sb_buf[0] == TELQUAL_SEND) { |
284 | char logbuf[sizeof(cfg.termspeed)+80]; |
285 | b[0] = IAC; b[1] = SB; b[2] = TELOPT_TSPEED; |
286 | b[3] = TELQUAL_IS; |
287 | strcpy(b+4, cfg.termspeed); |
288 | n = 4 + strlen(cfg.termspeed); |
289 | b[n] = IAC; b[n+1] = SE; |
290 | s_write (b, n+2); |
c5e9c988 |
291 | logevent("server:\tSB TSPEED SEND"); |
374330e2 |
292 | sprintf(logbuf, "client:\tSB TSPEED IS %s", cfg.termspeed); |
c5e9c988 |
293 | logevent (logbuf); |
374330e2 |
294 | } else |
c5e9c988 |
295 | logevent ("server:\tSB TSPEED <something weird>"); |
374330e2 |
296 | break; |
297 | case TELOPT_TTYPE: |
298 | if (sb_len == 1 && sb_buf[0] == TELQUAL_SEND) { |
299 | char logbuf[sizeof(cfg.termtype)+80]; |
300 | b[0] = IAC; b[1] = SB; b[2] = TELOPT_TTYPE; |
301 | b[3] = TELQUAL_IS; |
302 | for (n = 0; cfg.termtype[n]; n++) |
303 | b[n+4] = (cfg.termtype[n] >= 'a' && cfg.termtype[n] <= 'z' ? |
304 | cfg.termtype[n] + 'A'-'a' : cfg.termtype[n]); |
305 | b[n+4] = IAC; b[n+5] = SE; |
306 | s_write (b, n+6); |
307 | b[n+4] = 0; |
c5e9c988 |
308 | logevent("server:\tSB TTYPE SEND"); |
374330e2 |
309 | sprintf(logbuf, "client:\tSB TTYPE IS %s", b+4); |
c5e9c988 |
310 | logevent(logbuf); |
374330e2 |
311 | } else |
c5e9c988 |
312 | logevent("server:\tSB TTYPE <something weird>\r\n"); |
374330e2 |
313 | break; |
314 | case TELOPT_OLD_ENVIRON: |
315 | case TELOPT_NEW_ENVIRON: |
316 | p = sb_buf; |
317 | q = p + sb_len; |
318 | if (p < q && *p == TELQUAL_SEND) { |
319 | char logbuf[50]; |
320 | p++; |
321 | sprintf (logbuf, "server:\tSB %s SEND", telopt(sb_opt)); |
c5e9c988 |
322 | logevent (logbuf); |
374330e2 |
323 | if (sb_opt == TELOPT_OLD_ENVIRON) { |
324 | if (cfg.rfc_environ) { |
325 | value = RFC_VALUE; |
326 | var = RFC_VAR; |
327 | } else { |
328 | value = BSD_VALUE; |
329 | var = BSD_VAR; |
330 | } |
331 | /* |
332 | * Try to guess the sense of VAR and VALUE. |
333 | */ |
334 | while (p < q) { |
335 | if (*p == RFC_VAR) { |
336 | value = RFC_VALUE; |
337 | var = RFC_VAR; |
338 | } else if (*p == BSD_VAR) { |
339 | value = BSD_VALUE; |
340 | var = BSD_VAR; |
341 | } |
342 | p++; |
343 | } |
344 | } else { |
345 | /* |
346 | * With NEW_ENVIRON, the sense of VAR and VALUE |
347 | * isn't in doubt. |
348 | */ |
349 | value = RFC_VALUE; |
350 | var = RFC_VAR; |
351 | } |
352 | b[0] = IAC; b[1] = SB; b[2] = sb_opt; |
353 | b[3] = TELQUAL_IS; |
354 | n = 4; |
37508af4 |
355 | e = cfg.environmt; |
374330e2 |
356 | while (*e) { |
357 | b[n++] = var; |
358 | while (*e && *e != '\t') b[n++] = *e++; |
359 | if (*e == '\t') e++; |
360 | b[n++] = value; |
361 | while (*e) b[n++] = *e++; |
362 | e++; |
363 | } |
364 | if (*cfg.username) { |
365 | b[n++] = var; b[n++] = 'U'; b[n++] = 'S'; |
366 | b[n++] = 'E'; b[n++] = 'R'; b[n++] = value; |
367 | e = cfg.username; |
368 | while (*e) b[n++] = *e++; |
369 | } |
370 | b[n++] = IAC; b[n++] = SE; |
371 | s_write (b, n); |
372 | sprintf(logbuf, "client:\tSB %s IS %s", telopt(sb_opt), |
373 | n==6 ? "<nothing>" : "<stuff>"); |
c5e9c988 |
374 | logevent (logbuf); |
374330e2 |
375 | } |
376 | break; |
377 | } |
378 | } |
379 | |
380 | static enum { |
381 | TOPLEVEL, SEENIAC, SEENWILL, SEENWONT, SEENDO, SEENDONT, |
382 | SEENSB, SUBNEGOT, SUBNEG_IAC, SEENCR |
383 | } telnet_state = TOPLEVEL; |
384 | |
385 | static void do_telnet_read (char *buf, int len) { |
386 | unsigned char b[10]; |
387 | |
388 | while (len--) { |
389 | int c = (unsigned char) *buf++; |
390 | |
391 | switch (telnet_state) { |
392 | case TOPLEVEL: |
393 | case SEENCR: |
394 | if (c == NUL && telnet_state == SEENCR) |
395 | telnet_state = TOPLEVEL; |
396 | else if (c == IAC) |
397 | telnet_state = SEENIAC; |
398 | else { |
399 | b[0] = c; |
400 | #if 0 |
401 | if (!in_synch) |
402 | #endif |
403 | c_write (b, 1); |
404 | if (c == CR) |
405 | telnet_state = SEENCR; |
406 | else |
407 | telnet_state = TOPLEVEL; |
408 | } |
409 | break; |
410 | case SEENIAC: |
411 | if (c == DO) telnet_state = SEENDO; |
412 | else if (c == DONT) telnet_state = SEENDONT; |
413 | else if (c == WILL) telnet_state = SEENWILL; |
414 | else if (c == WONT) telnet_state = SEENWONT; |
415 | else if (c == SB) telnet_state = SEENSB; |
300d41b0 |
416 | else { |
ded38628 |
417 | /* ignore everything else; print it if it's IAC */ |
418 | if (c == IAC) { |
419 | b[0] = c; |
420 | c_write(b,1); |
421 | } |
300d41b0 |
422 | telnet_state = TOPLEVEL; |
423 | } |
374330e2 |
424 | break; |
425 | case SEENWILL: |
426 | proc_rec_opt (WILL, c); |
427 | telnet_state = TOPLEVEL; |
428 | break; |
429 | case SEENWONT: |
430 | proc_rec_opt (WONT, c); |
431 | telnet_state = TOPLEVEL; |
432 | break; |
433 | case SEENDO: |
434 | proc_rec_opt (DO, c); |
435 | telnet_state = TOPLEVEL; |
436 | break; |
437 | case SEENDONT: |
438 | proc_rec_opt (DONT, c); |
439 | telnet_state = TOPLEVEL; |
440 | break; |
441 | case SEENSB: |
442 | sb_opt = c; |
443 | sb_len = 0; |
444 | telnet_state = SUBNEGOT; |
445 | break; |
446 | case SUBNEGOT: |
447 | if (c == IAC) |
448 | telnet_state = SUBNEG_IAC; |
449 | else { |
450 | subneg_addchar: |
451 | if (sb_len >= sb_size) { |
452 | char *newbuf; |
453 | sb_size += SB_DELTA; |
454 | newbuf = (sb_buf ? |
455 | realloc(sb_buf, sb_size) : |
456 | malloc(sb_size)); |
457 | if (newbuf) |
458 | sb_buf = newbuf; |
459 | else |
460 | sb_size -= SB_DELTA; |
461 | } |
462 | if (sb_len < sb_size) |
463 | sb_buf[sb_len++] = c; |
464 | telnet_state = SUBNEGOT;/* in case we came here by goto */ |
465 | } |
466 | break; |
467 | case SUBNEG_IAC: |
468 | if (c != SE) |
469 | goto subneg_addchar; /* yes, it's a hack, I know, but... */ |
470 | else { |
471 | process_subneg(); |
472 | telnet_state = TOPLEVEL; |
473 | } |
474 | break; |
475 | } |
476 | } |
477 | } |
478 | |
479 | /* |
480 | * Called to set up the Telnet connection. Will arrange for |
481 | * WM_NETEVENT messages to be passed to the specified window, whose |
482 | * window procedure should then call telnet_msg(). |
483 | * |
484 | * Returns an error message, or NULL on success. |
485 | * |
486 | * Also places the canonical host name into `realhost'. |
487 | */ |
488 | static char *telnet_init (HWND hwnd, char *host, int port, char **realhost) { |
489 | SOCKADDR_IN addr; |
490 | struct hostent *h; |
491 | unsigned long a; |
492 | |
493 | /* |
494 | * Try to find host. |
495 | */ |
496 | if ( (a = inet_addr(host)) == (unsigned long) INADDR_NONE) { |
497 | if ( (h = gethostbyname(host)) == NULL) |
498 | switch (WSAGetLastError()) { |
499 | case WSAENETDOWN: return "Network is down"; |
500 | case WSAHOST_NOT_FOUND: case WSANO_DATA: |
501 | return "Host does not exist"; |
502 | case WSATRY_AGAIN: return "Host not found"; |
503 | default: return "gethostbyname: unknown error"; |
504 | } |
505 | memcpy (&a, h->h_addr, sizeof(a)); |
506 | *realhost = h->h_name; |
507 | } else |
508 | *realhost = host; |
509 | a = ntohl(a); |
510 | |
511 | if (port < 0) |
512 | port = 23; /* default telnet port */ |
513 | |
514 | /* |
515 | * Open socket. |
516 | */ |
517 | s = socket(AF_INET, SOCK_STREAM, 0); |
518 | if (s == INVALID_SOCKET) |
519 | switch (WSAGetLastError()) { |
520 | case WSAENETDOWN: return "Network is down"; |
521 | case WSAEAFNOSUPPORT: return "TCP/IP support not present"; |
522 | default: return "socket(): unknown error"; |
523 | } |
524 | |
525 | #if 0 |
526 | { |
527 | BOOL b = TRUE; |
528 | setsockopt (s, SOL_SOCKET, SO_OOBINLINE, (void *)&b, sizeof(b)); |
529 | } |
530 | #endif |
531 | |
532 | /* |
533 | * Bind to local address. |
534 | */ |
535 | addr.sin_family = AF_INET; |
536 | addr.sin_addr.s_addr = htonl(INADDR_ANY); |
537 | addr.sin_port = htons(0); |
538 | if (bind (s, (struct sockaddr *)&addr, sizeof(addr)) == SOCKET_ERROR) |
539 | switch (WSAGetLastError()) { |
540 | case WSAENETDOWN: return "Network is down"; |
541 | default: return "bind(): unknown error"; |
542 | } |
543 | |
544 | /* |
545 | * Connect to remote address. |
546 | */ |
547 | addr.sin_addr.s_addr = htonl(a); |
548 | addr.sin_port = htons((short)port); |
549 | if (connect (s, (struct sockaddr *)&addr, sizeof(addr)) == SOCKET_ERROR) |
550 | switch (WSAGetLastError()) { |
551 | case WSAENETDOWN: return "Network is down"; |
552 | case WSAECONNREFUSED: return "Connection refused"; |
553 | case WSAENETUNREACH: return "Network is unreachable"; |
554 | case WSAEHOSTUNREACH: return "No route to host"; |
555 | default: return "connect(): unknown error"; |
556 | } |
557 | |
558 | if (WSAAsyncSelect (s, hwnd, WM_NETEVENT, FD_READ | |
559 | FD_WRITE | FD_OOB | FD_CLOSE) == SOCKET_ERROR) |
560 | switch (WSAGetLastError()) { |
561 | case WSAENETDOWN: return "Network is down"; |
562 | default: return "WSAAsyncSelect(): unknown error"; |
563 | } |
564 | |
565 | /* |
566 | * Initialise option states. |
567 | */ |
568 | { |
569 | struct Opt **o; |
570 | |
571 | for (o = opts; *o; o++) |
572 | if ((*o)->state == REQUESTED) |
573 | send_opt ((*o)->send, (*o)->option); |
574 | } |
575 | |
576 | #if 0 |
577 | /* |
578 | * Set up SYNCH state. |
579 | */ |
580 | in_synch = FALSE; |
581 | #endif |
582 | |
583 | return NULL; |
584 | } |
585 | |
586 | /* |
587 | * Process a WM_NETEVENT message. Will return 0 if the connection |
588 | * has closed, or <0 for a socket error. |
589 | */ |
590 | static int telnet_msg (WPARAM wParam, LPARAM lParam) { |
591 | int ret; |
592 | char buf[256]; |
593 | |
8ce72d2c |
594 | /* |
595 | * Because reading less than the whole of the available pending |
596 | * data can generate an FD_READ event, we need to allow for the |
597 | * possibility that FD_READ may arrive with FD_CLOSE already in |
598 | * the queue; so it's possible that we can get here even with s |
599 | * invalid. If so, we return 1 and don't worry about it. |
600 | */ |
601 | if (s == INVALID_SOCKET) |
602 | return 1; |
374330e2 |
603 | |
604 | if (WSAGETSELECTERROR(lParam) != 0) |
605 | return -WSAGETSELECTERROR(lParam); |
606 | |
607 | switch (WSAGETSELECTEVENT(lParam)) { |
608 | case FD_READ: |
8ce72d2c |
609 | case FD_CLOSE: |
374330e2 |
610 | ret = recv(s, buf, sizeof(buf), 0); |
611 | if (ret < 0 && WSAGetLastError() == WSAEWOULDBLOCK) |
612 | return 1; |
613 | if (ret < 0) /* any _other_ error */ |
614 | return -10000-WSAGetLastError(); |
615 | if (ret == 0) { |
616 | s = INVALID_SOCKET; |
8ce72d2c |
617 | return 0; |
374330e2 |
618 | } |
619 | #if 0 |
620 | if (in_synch) { |
621 | BOOL i; |
622 | if (ioctlsocket (s, SIOCATMARK, &i) < 0) { |
623 | return -20000-WSAGetLastError(); |
624 | } |
625 | if (i) |
626 | in_synch = FALSE; |
627 | } |
628 | #endif |
629 | do_telnet_read (buf, ret); |
630 | return 1; |
631 | case FD_OOB: |
632 | do { |
633 | ret = recv(s, buf, sizeof(buf), 0); |
634 | } while (ret > 0); |
635 | telnet_state = TOPLEVEL; |
636 | do { |
637 | ret = recv(s, buf, 1, MSG_OOB); |
638 | if (ret > 0) |
639 | do_telnet_read (buf, ret); |
640 | } while (ret > 0); |
641 | if (ret < 0 && WSAGetLastError() != WSAEWOULDBLOCK) |
642 | return -30000-WSAGetLastError(); |
643 | return 1; |
644 | case FD_WRITE: |
645 | if (outbuf_head != outbuf_reap) |
646 | try_write(); |
647 | return 1; |
374330e2 |
648 | } |
649 | return 1; /* shouldn't happen, but WTF */ |
650 | } |
651 | |
652 | /* |
653 | * Called to send data down the Telnet connection. |
654 | */ |
655 | static void telnet_send (char *buf, int len) { |
656 | char *p; |
657 | static unsigned char iac[2] = { IAC, IAC }; |
658 | static unsigned char cr[2] = { CR, NUL }; |
659 | |
660 | if (s == INVALID_SOCKET) |
661 | return; |
662 | |
663 | p = buf; |
664 | while (p < buf+len) { |
665 | char *q = p; |
666 | |
667 | while (iswritable((unsigned char)*p) && p < buf+len) p++; |
668 | s_write (q, p-q); |
669 | |
670 | while (p < buf+len && !iswritable((unsigned char)*p)) { |
671 | s_write ((unsigned char)*p == IAC ? iac : cr, 2); |
672 | p++; |
673 | } |
674 | } |
675 | } |
676 | |
677 | /* |
678 | * Called to set the size of the window from Telnet's POV. |
679 | */ |
680 | static void telnet_size(void) { |
681 | unsigned char b[16]; |
682 | char logbuf[50]; |
683 | |
684 | if (s == INVALID_SOCKET || o_naws.state != ACTIVE) |
685 | return; |
686 | b[0] = IAC; b[1] = SB; b[2] = TELOPT_NAWS; |
687 | b[3] = cols >> 8; b[4] = cols & 0xFF; |
688 | b[5] = rows >> 8; b[6] = rows & 0xFF; |
689 | b[7] = IAC; b[8] = SE; |
690 | s_write (b, 9); |
691 | sprintf(logbuf, "client:\tSB NAWS %d,%d", |
692 | ((unsigned char)b[3] << 8) + (unsigned char)b[4], |
693 | ((unsigned char)b[5] << 8) + (unsigned char)b[6]); |
c5e9c988 |
694 | logevent (logbuf); |
374330e2 |
695 | } |
696 | |
697 | /* |
698 | * Send Telnet special codes. |
699 | */ |
700 | static void telnet_special (Telnet_Special code) { |
701 | unsigned char b[2]; |
702 | |
703 | if (s == INVALID_SOCKET) |
704 | return; |
705 | |
706 | b[0] = IAC; |
707 | switch (code) { |
708 | case TS_AYT: b[1] = AYT; s_write (b, 2); break; |
709 | case TS_BRK: b[1] = BREAK; s_write (b, 2); break; |
710 | case TS_EC: b[1] = EC; s_write (b, 2); break; |
711 | case TS_EL: b[1] = EL; s_write (b, 2); break; |
712 | case TS_GA: b[1] = GA; s_write (b, 2); break; |
713 | case TS_NOP: b[1] = NOP; s_write (b, 2); break; |
714 | case TS_ABORT: b[1] = ABORT; s_write (b, 2); break; |
715 | case TS_AO: b[1] = AO; s_write (b, 2); break; |
716 | case TS_IP: b[1] = IP; s_write (b, 2); break; |
717 | case TS_SUSP: b[1] = SUSP; s_write (b, 2); break; |
718 | case TS_EOR: b[1] = EOR; s_write (b, 2); break; |
719 | case TS_EOF: b[1] = xEOF; s_write (b, 2); break; |
720 | case TS_SYNCH: |
721 | outbuf_head = outbuf_reap = 0; |
722 | b[0] = DM; |
723 | send (s, b, 1, MSG_OOB); |
724 | break; |
725 | } |
726 | } |
727 | |
728 | Backend telnet_backend = { |
729 | telnet_init, |
730 | telnet_msg, |
731 | telnet_send, |
732 | telnet_size, |
733 | telnet_special |
734 | }; |