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