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