374330e2 |
1 | #include <windows.h> |
2 | #include <stdio.h> |
3 | #include <stdlib.h> |
4d331a77 |
4 | #ifndef AUTO_WINSOCK |
5 | #ifdef WINSOCK_TWO |
6 | #include <winsock2.h> |
7 | #else |
374330e2 |
8 | #include <winsock.h> |
4d331a77 |
9 | #endif |
10 | #endif |
374330e2 |
11 | |
12 | #include "putty.h" |
13 | |
14 | #ifndef FALSE |
15 | #define FALSE 0 |
16 | #endif |
17 | #ifndef TRUE |
18 | #define TRUE 1 |
19 | #endif |
20 | |
21 | static SOCKET s = INVALID_SOCKET; |
22 | |
23 | #define IAC 255 /* interpret as command: */ |
24 | #define DONT 254 /* you are not to use option */ |
25 | #define DO 253 /* please, you use option */ |
26 | #define WONT 252 /* I won't use option */ |
27 | #define WILL 251 /* I will use option */ |
28 | #define SB 250 /* interpret as subnegotiation */ |
29 | #define SE 240 /* end sub negotiation */ |
30 | |
31 | #define GA 249 /* you may reverse the line */ |
32 | #define EL 248 /* erase the current line */ |
33 | #define EC 247 /* erase the current character */ |
34 | #define AYT 246 /* are you there */ |
35 | #define AO 245 /* abort output--but let prog finish */ |
36 | #define IP 244 /* interrupt process--permanently */ |
37 | #define BREAK 243 /* break */ |
38 | #define DM 242 /* data mark--for connect. cleaning */ |
39 | #define NOP 241 /* nop */ |
40 | #define EOR 239 /* end of record (transparent mode) */ |
41 | #define ABORT 238 /* Abort process */ |
42 | #define SUSP 237 /* Suspend process */ |
43 | #define xEOF 236 /* End of file: EOF is already used... */ |
44 | |
45 | #define TELOPT_BINARY 0 /* 8-bit data path */ |
46 | #define TELOPT_ECHO 1 /* echo */ |
47 | #define TELOPT_RCP 2 /* prepare to reconnect */ |
48 | #define TELOPT_SGA 3 /* suppress go ahead */ |
49 | #define TELOPT_NAMS 4 /* approximate message size */ |
50 | #define TELOPT_STATUS 5 /* give status */ |
51 | #define TELOPT_TM 6 /* timing mark */ |
52 | #define TELOPT_RCTE 7 /* remote controlled transmission and echo */ |
53 | #define TELOPT_NAOL 8 /* negotiate about output line width */ |
54 | #define TELOPT_NAOP 9 /* negotiate about output page size */ |
55 | #define TELOPT_NAOCRD 10 /* negotiate about CR disposition */ |
56 | #define TELOPT_NAOHTS 11 /* negotiate about horizontal tabstops */ |
57 | #define TELOPT_NAOHTD 12 /* negotiate about horizontal tab disposition */ |
58 | #define TELOPT_NAOFFD 13 /* negotiate about formfeed disposition */ |
59 | #define TELOPT_NAOVTS 14 /* negotiate about vertical tab stops */ |
60 | #define TELOPT_NAOVTD 15 /* negotiate about vertical tab disposition */ |
61 | #define TELOPT_NAOLFD 16 /* negotiate about output LF disposition */ |
62 | #define TELOPT_XASCII 17 /* extended ascic character set */ |
63 | #define TELOPT_LOGOUT 18 /* force logout */ |
64 | #define TELOPT_BM 19 /* byte macro */ |
65 | #define TELOPT_DET 20 /* data entry terminal */ |
66 | #define TELOPT_SUPDUP 21 /* supdup protocol */ |
67 | #define TELOPT_SUPDUPOUTPUT 22 /* supdup output */ |
68 | #define TELOPT_SNDLOC 23 /* send location */ |
69 | #define TELOPT_TTYPE 24 /* terminal type */ |
70 | #define TELOPT_EOR 25 /* end or record */ |
71 | #define TELOPT_TUID 26 /* TACACS user identification */ |
72 | #define TELOPT_OUTMRK 27 /* output marking */ |
73 | #define TELOPT_TTYLOC 28 /* terminal location number */ |
74 | #define TELOPT_3270REGIME 29 /* 3270 regime */ |
75 | #define TELOPT_X3PAD 30 /* X.3 PAD */ |
76 | #define TELOPT_NAWS 31 /* window size */ |
77 | #define TELOPT_TSPEED 32 /* terminal speed */ |
78 | #define TELOPT_LFLOW 33 /* remote flow control */ |
79 | #define TELOPT_LINEMODE 34 /* Linemode option */ |
80 | #define TELOPT_XDISPLOC 35 /* X Display Location */ |
81 | #define TELOPT_OLD_ENVIRON 36 /* Old - Environment variables */ |
82 | #define TELOPT_AUTHENTICATION 37/* Authenticate */ |
83 | #define TELOPT_ENCRYPT 38 /* Encryption option */ |
84 | #define TELOPT_NEW_ENVIRON 39 /* New - Environment variables */ |
85 | #define TELOPT_EXOPL 255 /* extended-options-list */ |
86 | |
87 | #define TELQUAL_IS 0 /* option is... */ |
88 | #define TELQUAL_SEND 1 /* send option */ |
89 | #define TELQUAL_INFO 2 /* ENVIRON: informational version of IS */ |
90 | #define BSD_VAR 1 |
91 | #define BSD_VALUE 0 |
92 | #define RFC_VAR 0 |
93 | #define RFC_VALUE 1 |
94 | |
95 | #define CR 13 |
96 | #define LF 10 |
97 | #define NUL 0 |
98 | |
99 | #define iswritable(x) ( (x) != IAC && (x) != CR ) |
100 | |
101 | static char *telopt(int opt) { |
102 | #define i(x) if (opt == TELOPT_ ## x) return #x; |
103 | i(BINARY); i(ECHO); i(RCP); i(SGA); i(NAMS); i(STATUS); i(TM); i(RCTE); |
104 | i(NAOL); i(NAOP); i(NAOCRD); i(NAOHTS); i(NAOHTD); i(NAOFFD); i(NAOVTS); |
105 | i(NAOVTD); i(NAOLFD); i(XASCII); i(LOGOUT); i(BM); i(DET); i(SUPDUP); |
106 | i(SUPDUPOUTPUT); i(SNDLOC); i(TTYPE); i(EOR); i(TUID); i(OUTMRK); |
107 | i(TTYLOC); i(X3PAD); i(NAWS); i(TSPEED); i(LFLOW); i(LINEMODE); |
108 | i(XDISPLOC); i(OLD_ENVIRON); i(AUTHENTICATION); i(ENCRYPT); |
109 | i(NEW_ENVIRON); i(EXOPL); |
110 | #undef i |
111 | return "<unknown>"; |
112 | } |
113 | |
114 | static void telnet_size(void); |
115 | |
116 | struct Opt { |
117 | int send; /* what we initially send */ |
118 | int nsend; /* -ve send if requested to stop it */ |
119 | int ack, nak; /* +ve and -ve acknowledgements */ |
120 | int option; /* the option code */ |
121 | enum { |
122 | REQUESTED, ACTIVE, INACTIVE, REALLY_INACTIVE |
123 | } state; |
124 | }; |
125 | |
126 | static struct Opt o_naws = {WILL, WONT, DO, DONT, TELOPT_NAWS, REQUESTED}; |
127 | static struct Opt o_tspeed = {WILL, WONT, DO, DONT, TELOPT_TSPEED, REQUESTED}; |
128 | static struct Opt o_ttype = {WILL, WONT, DO, DONT, TELOPT_TTYPE, REQUESTED}; |
129 | static struct Opt o_oenv = {WILL, WONT, DO, DONT, TELOPT_OLD_ENVIRON, |
130 | INACTIVE}; |
131 | static struct Opt o_nenv = {WILL, WONT, DO, DONT, TELOPT_NEW_ENVIRON, |
132 | REQUESTED}; |
133 | static struct Opt o_echo = {DO, DONT, WILL, WONT, TELOPT_ECHO, REQUESTED}; |
134 | static struct Opt o_we_sga = {WILL, WONT, DO, DONT, TELOPT_SGA, REQUESTED}; |
135 | static struct Opt o_they_sga = {DO, DONT, WILL, WONT, TELOPT_SGA, REQUESTED}; |
136 | |
137 | static struct Opt *opts[] = { |
138 | &o_naws, &o_tspeed, &o_ttype, &o_oenv, &o_nenv, &o_echo, |
139 | &o_we_sga, &o_they_sga, NULL |
140 | }; |
141 | |
374330e2 |
142 | static int in_synch; |
374330e2 |
143 | static int sb_opt, sb_len; |
144 | static char *sb_buf = NULL; |
145 | static int sb_size = 0; |
146 | #define SB_DELTA 1024 |
147 | |
148 | static void try_write (void) { |
149 | while (outbuf_head != outbuf_reap) { |
150 | int end = (outbuf_reap < outbuf_head ? outbuf_head : OUTBUF_SIZE); |
151 | int len = end - outbuf_reap; |
152 | int ret; |
153 | |
154 | ret = send (s, outbuf+outbuf_reap, len, 0); |
155 | if (ret > 0) |
156 | outbuf_reap = (outbuf_reap + ret) & OUTBUF_MASK; |
157 | if (ret < len) |
158 | return; |
159 | } |
160 | } |
161 | |
162 | static void s_write (void *buf, int len) { |
163 | unsigned char *p = buf; |
164 | while (len--) { |
165 | int new_head = (outbuf_head + 1) & OUTBUF_MASK; |
166 | if (new_head != outbuf_reap) { |
167 | outbuf[outbuf_head] = *p++; |
168 | outbuf_head = new_head; |
169 | } |
170 | } |
171 | try_write(); |
172 | } |
173 | |
fe50e814 |
174 | static void c_write1(int c) { |
175 | char cc = (char)c; |
176 | from_backend(0, &cc, 1); |
177 | } |
178 | |
374330e2 |
179 | static void log_option (char *sender, int cmd, int option) { |
180 | char buf[50]; |
181 | sprintf(buf, "%s:\t%s %s", sender, |
182 | (cmd == WILL ? "WILL" : cmd == WONT ? "WONT" : |
183 | cmd == DO ? "DO" : cmd == DONT ? "DONT" : "<??>"), |
184 | telopt(option)); |
c5e9c988 |
185 | logevent(buf); |
374330e2 |
186 | } |
187 | |
188 | static void send_opt (int cmd, int option) { |
189 | unsigned char b[3]; |
190 | |
191 | b[0] = IAC; b[1] = cmd; b[2] = option; |
192 | s_write (b, 3); |
193 | log_option("client", cmd, option); |
194 | } |
195 | |
196 | static void deactivate_option (struct Opt *o) { |
197 | if (o->state == REQUESTED || o->state == ACTIVE) |
198 | send_opt (o->nsend, o->option); |
199 | o->state = REALLY_INACTIVE; |
200 | } |
201 | |
202 | static void activate_option (struct Opt *o) { |
203 | if (o->send == WILL && o->option == TELOPT_NAWS) |
204 | telnet_size(); |
205 | if (o->send == WILL && |
206 | (o->option == TELOPT_NEW_ENVIRON || |
207 | o->option == TELOPT_OLD_ENVIRON)) { |
208 | /* |
209 | * We may only have one kind of ENVIRON going at a time. |
210 | * This is a hack, but who cares. |
211 | */ |
212 | deactivate_option (o->option==TELOPT_NEW_ENVIRON ? &o_oenv : &o_nenv); |
213 | } |
2f938b83 |
214 | if (o->option == TELOPT_ECHO && cfg.ldisc_term) |
684d367c |
215 | ldisc = &ldisc_simple; |
374330e2 |
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 | } |
2f938b83 |
224 | if (o->option == TELOPT_ECHO && cfg.ldisc_term) |
684d367c |
225 | ldisc = &ldisc_term; |
374330e2 |
226 | } |
227 | |
228 | static void proc_rec_opt (int cmd, int option) { |
229 | struct Opt **o; |
230 | |
231 | log_option ("server", cmd, option); |
232 | for (o = opts; *o; o++) { |
233 | if ((*o)->option == option && (*o)->ack == cmd) { |
234 | switch ((*o)->state) { |
235 | case REQUESTED: |
236 | (*o)->state = ACTIVE; |
237 | activate_option (*o); |
238 | break; |
239 | case ACTIVE: |
240 | break; |
241 | case INACTIVE: |
242 | (*o)->state = ACTIVE; |
243 | send_opt ((*o)->send, option); |
244 | activate_option (*o); |
245 | break; |
246 | case REALLY_INACTIVE: |
247 | send_opt ((*o)->nsend, option); |
248 | break; |
249 | } |
250 | return; |
251 | } else if ((*o)->option == option && (*o)->nak == cmd) { |
252 | switch ((*o)->state) { |
253 | case REQUESTED: |
254 | (*o)->state = INACTIVE; |
255 | refused_option (*o); |
256 | break; |
257 | case ACTIVE: |
258 | (*o)->state = INACTIVE; |
259 | send_opt ((*o)->nsend, option); |
260 | break; |
261 | case INACTIVE: |
262 | case REALLY_INACTIVE: |
263 | break; |
264 | } |
265 | return; |
266 | } |
267 | } |
268 | /* |
269 | * If we reach here, the option was one we weren't prepared to |
270 | * cope with. So send a negative ack. |
271 | */ |
272 | send_opt ((cmd == WILL ? DONT : WONT), option); |
273 | } |
274 | |
275 | static void process_subneg (void) { |
276 | unsigned char b[2048], *p, *q; |
277 | int var, value, n; |
278 | char *e; |
279 | |
280 | switch (sb_opt) { |
281 | case TELOPT_TSPEED: |
282 | if (sb_len == 1 && sb_buf[0] == TELQUAL_SEND) { |
283 | char logbuf[sizeof(cfg.termspeed)+80]; |
284 | b[0] = IAC; b[1] = SB; b[2] = TELOPT_TSPEED; |
285 | b[3] = TELQUAL_IS; |
286 | strcpy(b+4, cfg.termspeed); |
287 | n = 4 + strlen(cfg.termspeed); |
288 | b[n] = IAC; b[n+1] = SE; |
289 | s_write (b, n+2); |
c5e9c988 |
290 | logevent("server:\tSB TSPEED SEND"); |
374330e2 |
291 | sprintf(logbuf, "client:\tSB TSPEED IS %s", cfg.termspeed); |
c5e9c988 |
292 | logevent (logbuf); |
374330e2 |
293 | } else |
c5e9c988 |
294 | logevent ("server:\tSB TSPEED <something weird>"); |
374330e2 |
295 | break; |
296 | case TELOPT_TTYPE: |
297 | if (sb_len == 1 && sb_buf[0] == TELQUAL_SEND) { |
298 | char logbuf[sizeof(cfg.termtype)+80]; |
299 | b[0] = IAC; b[1] = SB; b[2] = TELOPT_TTYPE; |
300 | b[3] = TELQUAL_IS; |
301 | for (n = 0; cfg.termtype[n]; n++) |
302 | b[n+4] = (cfg.termtype[n] >= 'a' && cfg.termtype[n] <= 'z' ? |
303 | cfg.termtype[n] + 'A'-'a' : cfg.termtype[n]); |
304 | b[n+4] = IAC; b[n+5] = SE; |
305 | s_write (b, n+6); |
306 | b[n+4] = 0; |
c5e9c988 |
307 | logevent("server:\tSB TTYPE SEND"); |
374330e2 |
308 | sprintf(logbuf, "client:\tSB TTYPE IS %s", b+4); |
c5e9c988 |
309 | logevent(logbuf); |
374330e2 |
310 | } else |
c5e9c988 |
311 | logevent("server:\tSB TTYPE <something weird>\r\n"); |
374330e2 |
312 | break; |
313 | case TELOPT_OLD_ENVIRON: |
314 | case TELOPT_NEW_ENVIRON: |
315 | p = sb_buf; |
316 | q = p + sb_len; |
317 | if (p < q && *p == TELQUAL_SEND) { |
318 | char logbuf[50]; |
319 | p++; |
320 | sprintf (logbuf, "server:\tSB %s SEND", telopt(sb_opt)); |
c5e9c988 |
321 | logevent (logbuf); |
374330e2 |
322 | if (sb_opt == TELOPT_OLD_ENVIRON) { |
323 | if (cfg.rfc_environ) { |
324 | value = RFC_VALUE; |
325 | var = RFC_VAR; |
326 | } else { |
327 | value = BSD_VALUE; |
328 | var = BSD_VAR; |
329 | } |
330 | /* |
331 | * Try to guess the sense of VAR and VALUE. |
332 | */ |
333 | while (p < q) { |
334 | if (*p == RFC_VAR) { |
335 | value = RFC_VALUE; |
336 | var = RFC_VAR; |
337 | } else if (*p == BSD_VAR) { |
338 | value = BSD_VALUE; |
339 | var = BSD_VAR; |
340 | } |
341 | p++; |
342 | } |
343 | } else { |
344 | /* |
345 | * With NEW_ENVIRON, the sense of VAR and VALUE |
346 | * isn't in doubt. |
347 | */ |
348 | value = RFC_VALUE; |
349 | var = RFC_VAR; |
350 | } |
351 | b[0] = IAC; b[1] = SB; b[2] = sb_opt; |
352 | b[3] = TELQUAL_IS; |
353 | n = 4; |
37508af4 |
354 | e = cfg.environmt; |
374330e2 |
355 | while (*e) { |
356 | b[n++] = var; |
357 | while (*e && *e != '\t') b[n++] = *e++; |
358 | if (*e == '\t') e++; |
359 | b[n++] = value; |
360 | while (*e) b[n++] = *e++; |
361 | e++; |
362 | } |
363 | if (*cfg.username) { |
364 | b[n++] = var; b[n++] = 'U'; b[n++] = 'S'; |
365 | b[n++] = 'E'; b[n++] = 'R'; b[n++] = value; |
366 | e = cfg.username; |
367 | while (*e) b[n++] = *e++; |
368 | } |
369 | b[n++] = IAC; b[n++] = SE; |
370 | s_write (b, n); |
371 | sprintf(logbuf, "client:\tSB %s IS %s", telopt(sb_opt), |
372 | n==6 ? "<nothing>" : "<stuff>"); |
c5e9c988 |
373 | logevent (logbuf); |
374330e2 |
374 | } |
375 | break; |
376 | } |
377 | } |
378 | |
379 | static enum { |
380 | TOPLEVEL, SEENIAC, SEENWILL, SEENWONT, SEENDO, SEENDONT, |
381 | SEENSB, SUBNEGOT, SUBNEG_IAC, SEENCR |
382 | } telnet_state = TOPLEVEL; |
383 | |
384 | static void do_telnet_read (char *buf, int len) { |
374330e2 |
385 | |
386 | while (len--) { |
387 | int c = (unsigned char) *buf++; |
388 | |
389 | switch (telnet_state) { |
390 | case TOPLEVEL: |
391 | case SEENCR: |
392 | if (c == NUL && telnet_state == SEENCR) |
393 | telnet_state = TOPLEVEL; |
394 | else if (c == IAC) |
395 | telnet_state = SEENIAC; |
396 | else { |
374330e2 |
397 | if (!in_synch) |
c9def1b8 |
398 | c_write1(c); |
2f938b83 |
399 | |
400 | #if 1 |
401 | /* I can't get the F***ing winsock to insert the urgent IAC |
402 | * into the right position! Even with SO_OOBINLINE it gives |
403 | * it to recv too soon. And of course the DM byte (that |
404 | * arrives in the same packet!) appears several K later!! |
405 | * |
406 | * Oh well, we do get the DM in the right place so I'll |
407 | * just stop hiding on the next 0xf2 and hope for the best. |
408 | */ |
409 | else if (c == DM) in_synch = 0; |
410 | #endif |
374330e2 |
411 | if (c == CR) |
412 | telnet_state = SEENCR; |
413 | else |
414 | telnet_state = TOPLEVEL; |
415 | } |
416 | break; |
417 | case SEENIAC: |
418 | if (c == DO) telnet_state = SEENDO; |
419 | else if (c == DONT) telnet_state = SEENDONT; |
420 | else if (c == WILL) telnet_state = SEENWILL; |
421 | else if (c == WONT) telnet_state = SEENWONT; |
422 | else if (c == SB) telnet_state = SEENSB; |
2f938b83 |
423 | else if (c == DM) { |
424 | in_synch = 0; |
425 | telnet_state = TOPLEVEL; |
426 | } |
300d41b0 |
427 | else { |
ded38628 |
428 | /* ignore everything else; print it if it's IAC */ |
429 | if (c == IAC) { |
c9def1b8 |
430 | c_write1(c); |
ded38628 |
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 ? |
c9def1b8 |
465 | srealloc(sb_buf, sb_size) : |
466 | smalloc(sb_size)); |
374330e2 |
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 | |
374330e2 |
535 | { |
536 | BOOL b = TRUE; |
537 | setsockopt (s, SOL_SOCKET, SO_OOBINLINE, (void *)&b, sizeof(b)); |
538 | } |
374330e2 |
539 | |
540 | /* |
541 | * Bind to local address. |
542 | */ |
543 | addr.sin_family = AF_INET; |
544 | addr.sin_addr.s_addr = htonl(INADDR_ANY); |
545 | addr.sin_port = htons(0); |
546 | if (bind (s, (struct sockaddr *)&addr, sizeof(addr)) == SOCKET_ERROR) |
547 | switch (WSAGetLastError()) { |
548 | case WSAENETDOWN: return "Network is down"; |
549 | default: return "bind(): unknown error"; |
550 | } |
551 | |
552 | /* |
553 | * Connect to remote address. |
554 | */ |
555 | addr.sin_addr.s_addr = htonl(a); |
556 | addr.sin_port = htons((short)port); |
557 | if (connect (s, (struct sockaddr *)&addr, sizeof(addr)) == SOCKET_ERROR) |
558 | switch (WSAGetLastError()) { |
559 | case WSAENETDOWN: return "Network is down"; |
560 | case WSAECONNREFUSED: return "Connection refused"; |
561 | case WSAENETUNREACH: return "Network is unreachable"; |
562 | case WSAEHOSTUNREACH: return "No route to host"; |
563 | default: return "connect(): unknown error"; |
564 | } |
565 | |
4017be6d |
566 | if (hwnd && WSAAsyncSelect (s, hwnd, WM_NETEVENT, FD_READ | |
374330e2 |
567 | FD_WRITE | FD_OOB | FD_CLOSE) == SOCKET_ERROR) |
568 | switch (WSAGetLastError()) { |
569 | case WSAENETDOWN: return "Network is down"; |
570 | default: return "WSAAsyncSelect(): unknown error"; |
571 | } |
572 | |
573 | /* |
574 | * Initialise option states. |
575 | */ |
eb5e1db9 |
576 | if( cfg.ldisc_term ) |
577 | { |
578 | struct Opt **o; |
579 | |
580 | for (o = opts; *o; o++) |
581 | if ((*o)->state == REQUESTED) |
582 | (*o)->state = INACTIVE; |
583 | } |
584 | else |
374330e2 |
585 | { |
586 | struct Opt **o; |
587 | |
588 | for (o = opts; *o; o++) |
589 | if ((*o)->state == REQUESTED) |
590 | send_opt ((*o)->send, (*o)->option); |
591 | } |
592 | |
374330e2 |
593 | /* |
594 | * Set up SYNCH state. |
595 | */ |
596 | in_synch = FALSE; |
6f34e365 |
597 | |
598 | /* |
599 | * We have no pre-session phase. |
600 | */ |
601 | begin_session(); |
602 | |
374330e2 |
603 | return NULL; |
604 | } |
605 | |
606 | /* |
607 | * Process a WM_NETEVENT message. Will return 0 if the connection |
608 | * has closed, or <0 for a socket error. |
609 | */ |
610 | static int telnet_msg (WPARAM wParam, LPARAM lParam) { |
611 | int ret; |
c9def1b8 |
612 | /* This needs to be larger than the packet size now that inbuf |
613 | * cannot overflow, in fact the fewer calls we make to windows |
614 | * the faster we will run! |
615 | */ |
616 | char buf[16384]; |
374330e2 |
617 | |
8ce72d2c |
618 | /* |
619 | * Because reading less than the whole of the available pending |
620 | * data can generate an FD_READ event, we need to allow for the |
621 | * possibility that FD_READ may arrive with FD_CLOSE already in |
622 | * the queue; so it's possible that we can get here even with s |
623 | * invalid. If so, we return 1 and don't worry about it. |
624 | */ |
625 | if (s == INVALID_SOCKET) |
626 | return 1; |
374330e2 |
627 | |
8d5de777 |
628 | if (WSAGETSELECTERROR(lParam) != 0) { |
629 | closesocket(s); |
630 | s = INVALID_SOCKET; |
374330e2 |
631 | return -WSAGETSELECTERROR(lParam); |
8d5de777 |
632 | } |
374330e2 |
633 | |
634 | switch (WSAGETSELECTEVENT(lParam)) { |
635 | case FD_READ: |
8ce72d2c |
636 | case FD_CLOSE: |
2f938b83 |
637 | { |
638 | int clear_of_oob = 1; |
ec55b220 |
639 | |
640 | /* Don't check for error return; some shims don't support |
641 | * this ioctl. |
642 | */ |
643 | ioctlsocket (s, SIOCATMARK, &clear_of_oob); |
2f938b83 |
644 | |
645 | in_synch = !clear_of_oob; |
646 | |
647 | do { |
648 | ret = recv(s, buf, sizeof(buf), 0); |
649 | if (ret < 0 && WSAGetLastError() == WSAEWOULDBLOCK) |
650 | return 1; |
8d5de777 |
651 | if (ret < 0) { /* any _other_ error */ |
652 | closesocket(s); |
653 | s = INVALID_SOCKET; |
2f938b83 |
654 | return -10000-WSAGetLastError(); |
8d5de777 |
655 | } |
2f938b83 |
656 | if (ret == 0) { |
657 | s = INVALID_SOCKET; |
658 | return 0; |
659 | } |
2f938b83 |
660 | do_telnet_read (buf, ret); |
661 | } while (in_synch); |
374330e2 |
662 | } |
374330e2 |
663 | return 1; |
664 | case FD_WRITE: |
665 | if (outbuf_head != outbuf_reap) |
666 | try_write(); |
667 | return 1; |
374330e2 |
668 | } |
669 | return 1; /* shouldn't happen, but WTF */ |
670 | } |
671 | |
672 | /* |
673 | * Called to send data down the Telnet connection. |
674 | */ |
675 | static void telnet_send (char *buf, int len) { |
676 | char *p; |
677 | static unsigned char iac[2] = { IAC, IAC }; |
678 | static unsigned char cr[2] = { CR, NUL }; |
eb5e1db9 |
679 | static unsigned char nl[2] = { CR, LF }; |
374330e2 |
680 | |
681 | if (s == INVALID_SOCKET) |
682 | return; |
683 | |
684 | p = buf; |
2f938b83 |
685 | while (p < buf+len) { |
374330e2 |
686 | char *q = p; |
687 | |
688 | while (iswritable((unsigned char)*p) && p < buf+len) p++; |
689 | s_write (q, p-q); |
690 | |
691 | while (p < buf+len && !iswritable((unsigned char)*p)) { |
eb5e1db9 |
692 | s_write ((unsigned char)*p == IAC ? iac : nl, 2); |
374330e2 |
693 | p++; |
694 | } |
695 | } |
696 | } |
697 | |
698 | /* |
699 | * Called to set the size of the window from Telnet's POV. |
700 | */ |
701 | static void telnet_size(void) { |
702 | unsigned char b[16]; |
703 | char logbuf[50]; |
704 | |
705 | if (s == INVALID_SOCKET || o_naws.state != ACTIVE) |
706 | return; |
707 | b[0] = IAC; b[1] = SB; b[2] = TELOPT_NAWS; |
708 | b[3] = cols >> 8; b[4] = cols & 0xFF; |
709 | b[5] = rows >> 8; b[6] = rows & 0xFF; |
710 | b[7] = IAC; b[8] = SE; |
711 | s_write (b, 9); |
712 | sprintf(logbuf, "client:\tSB NAWS %d,%d", |
713 | ((unsigned char)b[3] << 8) + (unsigned char)b[4], |
714 | ((unsigned char)b[5] << 8) + (unsigned char)b[6]); |
c5e9c988 |
715 | logevent (logbuf); |
374330e2 |
716 | } |
717 | |
718 | /* |
719 | * Send Telnet special codes. |
720 | */ |
721 | static void telnet_special (Telnet_Special code) { |
722 | unsigned char b[2]; |
723 | |
724 | if (s == INVALID_SOCKET) |
725 | return; |
726 | |
727 | b[0] = IAC; |
728 | switch (code) { |
729 | case TS_AYT: b[1] = AYT; s_write (b, 2); break; |
730 | case TS_BRK: b[1] = BREAK; s_write (b, 2); break; |
731 | case TS_EC: b[1] = EC; s_write (b, 2); break; |
732 | case TS_EL: b[1] = EL; s_write (b, 2); break; |
733 | case TS_GA: b[1] = GA; s_write (b, 2); break; |
734 | case TS_NOP: b[1] = NOP; s_write (b, 2); break; |
735 | case TS_ABORT: b[1] = ABORT; s_write (b, 2); break; |
736 | case TS_AO: b[1] = AO; s_write (b, 2); break; |
737 | case TS_IP: b[1] = IP; s_write (b, 2); break; |
738 | case TS_SUSP: b[1] = SUSP; s_write (b, 2); break; |
739 | case TS_EOR: b[1] = EOR; s_write (b, 2); break; |
740 | case TS_EOF: b[1] = xEOF; s_write (b, 2); break; |
741 | case TS_SYNCH: |
742 | outbuf_head = outbuf_reap = 0; |
684d367c |
743 | b[1] = DM; |
744 | send (s, b, 2, MSG_OOB); |
745 | break; |
746 | case TS_RECHO: |
747 | if (o_echo.state == INACTIVE || o_echo.state == REALLY_INACTIVE) { |
748 | o_echo.state = REQUESTED; |
749 | send_opt (o_echo.send, o_echo.option); |
750 | } |
751 | break; |
752 | case TS_LECHO: |
753 | if (o_echo.state == ACTIVE) { |
754 | o_echo.state = REQUESTED; |
755 | send_opt (o_echo.nsend, o_echo.option); |
756 | } |
374330e2 |
757 | break; |
ec55b220 |
758 | case TS_PING: |
759 | if (o_they_sga.state == ACTIVE) { |
760 | b[1] = NOP; |
761 | s_write (b, 2); |
762 | } |
763 | break; |
374330e2 |
764 | } |
765 | } |
766 | |
8ccc75b0 |
767 | static SOCKET telnet_socket(void) { return s; } |
768 | |
769 | static int telnet_sendok(void) { return 1; } |
4017be6d |
770 | |
374330e2 |
771 | Backend telnet_backend = { |
772 | telnet_init, |
773 | telnet_msg, |
774 | telnet_send, |
775 | telnet_size, |
4017be6d |
776 | telnet_special, |
8ccc75b0 |
777 | telnet_socket, |
97db3be4 |
778 | telnet_sendok, |
779 | 23 |
374330e2 |
780 | }; |