2 #else /* not macintosh */
5 #endif /* not macintosh */
12 static SOCKET s = INVALID_SOCKET;
13 /* kludge till we decide where to put telnet state */
16 #define IAC 255 /* interpret as command: */
17 #define DONT 254 /* you are not to use option */
18 #define DO 253 /* please, you use option */
19 #define WONT 252 /* I won't use option */
20 #define WILL 251 /* I will use option */
21 #define SB 250 /* interpret as subnegotiation */
22 #define SE 240 /* end sub negotiation */
24 #define GA 249 /* you may reverse the line */
25 #define EL 248 /* erase the current line */
26 #define EC 247 /* erase the current character */
27 #define AYT 246 /* are you there */
28 #define AO 245 /* abort output--but let prog finish */
29 #define IP 244 /* interrupt process--permanently */
30 #define BREAK 243 /* break */
31 #define DM 242 /* data mark--for connect. cleaning */
32 #define NOP 241 /* nop */
33 #define EOR 239 /* end of record (transparent mode) */
34 #define ABORT 238 /* Abort process */
35 #define SUSP 237 /* Suspend process */
36 #define xEOF 236 /* End of file: EOF is already used... */
38 #define TELOPT_BINARY 0 /* 8-bit data path */
39 #define TELOPT_ECHO 1 /* echo */
40 #define TELOPT_RCP 2 /* prepare to reconnect */
41 #define TELOPT_SGA 3 /* suppress go ahead */
42 #define TELOPT_NAMS 4 /* approximate message size */
43 #define TELOPT_STATUS 5 /* give status */
44 #define TELOPT_TM 6 /* timing mark */
45 #define TELOPT_RCTE 7 /* remote controlled transmission and echo */
46 #define TELOPT_NAOL 8 /* negotiate about output line width */
47 #define TELOPT_NAOP 9 /* negotiate about output page size */
48 #define TELOPT_NAOCRD 10 /* negotiate about CR disposition */
49 #define TELOPT_NAOHTS 11 /* negotiate about horizontal tabstops */
50 #define TELOPT_NAOHTD 12 /* negotiate about horizontal tab disposition */
51 #define TELOPT_NAOFFD 13 /* negotiate about formfeed disposition */
52 #define TELOPT_NAOVTS 14 /* negotiate about vertical tab stops */
53 #define TELOPT_NAOVTD 15 /* negotiate about vertical tab disposition */
54 #define TELOPT_NAOLFD 16 /* negotiate about output LF disposition */
55 #define TELOPT_XASCII 17 /* extended ascic character set */
56 #define TELOPT_LOGOUT 18 /* force logout */
57 #define TELOPT_BM 19 /* byte macro */
58 #define TELOPT_DET 20 /* data entry terminal */
59 #define TELOPT_SUPDUP 21 /* supdup protocol */
60 #define TELOPT_SUPDUPOUTPUT 22 /* supdup output */
61 #define TELOPT_SNDLOC 23 /* send location */
62 #define TELOPT_TTYPE 24 /* terminal type */
63 #define TELOPT_EOR 25 /* end or record */
64 #define TELOPT_TUID 26 /* TACACS user identification */
65 #define TELOPT_OUTMRK 27 /* output marking */
66 #define TELOPT_TTYLOC 28 /* terminal location number */
67 #define TELOPT_3270REGIME 29 /* 3270 regime */
68 #define TELOPT_X3PAD 30 /* X.3 PAD */
69 #define TELOPT_NAWS 31 /* window size */
70 #define TELOPT_TSPEED 32 /* terminal speed */
71 #define TELOPT_LFLOW 33 /* remote flow control */
72 #define TELOPT_LINEMODE 34 /* Linemode option */
73 #define TELOPT_XDISPLOC 35 /* X Display Location */
74 #define TELOPT_OLD_ENVIRON 36 /* Old - Environment variables */
75 #define TELOPT_AUTHENTICATION 37/* Authenticate */
76 #define TELOPT_ENCRYPT 38 /* Encryption option */
77 #define TELOPT_NEW_ENVIRON 39 /* New - Environment variables */
78 #define TELOPT_EXOPL 255 /* extended-options-list */
80 #define TELQUAL_IS 0 /* option is... */
81 #define TELQUAL_SEND 1 /* send option */
82 #define TELQUAL_INFO 2 /* ENVIRON: informational version of IS */
92 #define iswritable(x) ( (x) != IAC && (x) != CR )
94 static char *telopt(int opt) {
95 #define i(x) if (opt == TELOPT_ ## x) return #x;
96 i(BINARY); i(ECHO); i(RCP); i(SGA); i(NAMS); i(STATUS); i(TM); i(RCTE);
97 i(NAOL); i(NAOP); i(NAOCRD); i(NAOHTS); i(NAOHTD); i(NAOFFD); i(NAOVTS);
98 i(NAOVTD); i(NAOLFD); i(XASCII); i(LOGOUT); i(BM); i(DET); i(SUPDUP);
99 i(SUPDUPOUTPUT); i(SNDLOC); i(TTYPE); i(EOR); i(TUID); i(OUTMRK);
100 i(TTYLOC); i(X3PAD); i(NAWS); i(TSPEED); i(LFLOW); i(LINEMODE);
101 i(XDISPLOC); i(OLD_ENVIRON); i(AUTHENTICATION); i(ENCRYPT);
102 i(NEW_ENVIRON); i(EXOPL);
107 static void telnet_size(Session *ignored);
110 int send; /* what we initially send */
111 int nsend; /* -ve send if requested to stop it */
112 int ack, nak; /* +ve and -ve acknowledgements */
113 int option; /* the option code */
115 REQUESTED, ACTIVE, INACTIVE, REALLY_INACTIVE
119 static struct Opt o_naws = {WILL, WONT, DO, DONT, TELOPT_NAWS, REQUESTED};
120 static struct Opt o_tspeed = {WILL, WONT, DO, DONT, TELOPT_TSPEED, REQUESTED};
121 static struct Opt o_ttype = {WILL, WONT, DO, DONT, TELOPT_TTYPE, REQUESTED};
122 static struct Opt o_oenv = {WILL, WONT, DO, DONT, TELOPT_OLD_ENVIRON,
124 static struct Opt o_nenv = {WILL, WONT, DO, DONT, TELOPT_NEW_ENVIRON,
126 static struct Opt o_echo = {DO, DONT, WILL, WONT, TELOPT_ECHO, REQUESTED};
127 static struct Opt o_we_sga = {WILL, WONT, DO, DONT, TELOPT_SGA, REQUESTED};
128 static struct Opt o_they_sga = {DO, DONT, WILL, WONT, TELOPT_SGA, REQUESTED};
130 static struct Opt *opts[] = {
131 &o_naws, &o_tspeed, &o_ttype, &o_oenv, &o_nenv, &o_echo,
132 &o_we_sga, &o_they_sga, NULL
139 static int sb_opt, sb_len;
140 static unsigned char *sb_buf = NULL;
141 static int sb_size = 0;
142 #define SB_DELTA 1024
144 static void try_write (void) {
145 while (sess->outbuf_head != sess->outbuf_reap) {
146 int end = (sess->outbuf_reap < sess->outbuf_head ? sess->outbuf_head : OUTBUF_SIZE);
147 int len = end - sess->outbuf_reap;
150 ret = net_send (s, sess->outbuf+sess->outbuf_reap, len, 0);
152 sess->outbuf_reap = (sess->outbuf_reap + ret) & OUTBUF_MASK;
158 static void s_write (void *buf, int len) {
159 unsigned char *p = buf;
161 int new_head = (sess->outbuf_head + 1) & OUTBUF_MASK;
162 if (new_head != sess->outbuf_reap) {
163 sess->outbuf[sess->outbuf_head] = *p++;
164 sess->outbuf_head = new_head;
170 static void c_write (char *buf, int len) {
172 int new_head = (sess->inbuf_head + 1) & INBUF_MASK;
173 int c = (unsigned char) *buf;
174 if (new_head != sess->inbuf_reap) {
175 sess->inbuf[sess->inbuf_head] = *buf++;
176 sess->inbuf_head = new_head;
181 static void log_option (char *sender, int cmd, int option) {
183 sprintf(buf, "%s:\t%s %s", sender,
184 (cmd == WILL ? "WILL" : cmd == WONT ? "WONT" :
185 cmd == DO ? "DO" : cmd == DONT ? "DONT" : "<??>"),
190 static void send_opt (int cmd, int option) {
193 b[0] = IAC; b[1] = cmd; b[2] = option;
195 log_option("client", cmd, option);
198 static void deactivate_option (struct Opt *o) {
199 if (o->state == REQUESTED || o->state == ACTIVE)
200 send_opt (o->nsend, o->option);
201 o->state = REALLY_INACTIVE;
204 static void activate_option (struct Opt *o) {
205 if (o->send == WILL && o->option == TELOPT_NAWS)
207 if (o->send == WILL &&
208 (o->option == TELOPT_NEW_ENVIRON ||
209 o->option == TELOPT_OLD_ENVIRON)) {
211 * We may only have one kind of ENVIRON going at a time.
212 * This is a hack, but who cares.
214 deactivate_option (o->option==TELOPT_NEW_ENVIRON ? &o_oenv : &o_nenv);
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;
226 static void proc_rec_opt (int cmd, int option) {
229 log_option ("server", cmd, option);
230 for (o = opts; *o; o++) {
231 if ((*o)->option == option && (*o)->ack == cmd) {
232 switch ((*o)->state) {
234 (*o)->state = ACTIVE;
235 activate_option (*o);
240 (*o)->state = ACTIVE;
241 send_opt ((*o)->send, option);
242 activate_option (*o);
244 case REALLY_INACTIVE:
245 send_opt ((*o)->nsend, option);
249 } else if ((*o)->option == option && (*o)->nak == cmd) {
250 switch ((*o)->state) {
252 (*o)->state = INACTIVE;
256 (*o)->state = INACTIVE;
257 send_opt ((*o)->nsend, option);
260 case REALLY_INACTIVE:
267 * If we reach here, the option was one we weren't prepared to
268 * cope with. So send a negative ack.
270 send_opt ((cmd == WILL ? DONT : WONT), option);
273 static void process_subneg (void) {
274 unsigned char b[2048], *p, *q;
280 if (sb_len == 1 && sb_buf[0] == TELQUAL_SEND) {
281 char logbuf[sizeof(sess->cfg.termspeed)+80];
282 b[0] = IAC; b[1] = SB; b[2] = TELOPT_TSPEED;
284 strcpy((char *)b+4, sess->cfg.termspeed);
285 n = 4 + strlen(sess->cfg.termspeed);
286 b[n] = IAC; b[n+1] = SE;
288 lognegot("server:\tSB TSPEED SEND");
289 sprintf(logbuf, "client:\tSB TSPEED IS %s", sess->cfg.termspeed);
292 lognegot ("server:\tSB TSPEED <something weird>");
295 if (sb_len == 1 && sb_buf[0] == TELQUAL_SEND) {
296 char logbuf[sizeof(sess->cfg.termtype)+80];
297 b[0] = IAC; b[1] = SB; b[2] = TELOPT_TTYPE;
299 for (n = 0; sess->cfg.termtype[n]; n++)
300 b[n+4] = (sess->cfg.termtype[n] >= 'a' && sess->cfg.termtype[n] <= 'z' ?
301 sess->cfg.termtype[n] + 'A'-'a' : sess->cfg.termtype[n]);
302 b[n+4] = IAC; b[n+5] = SE;
305 lognegot("server:\tSB TTYPE SEND");
306 sprintf(logbuf, "client:\tSB TTYPE IS %s", b+4);
309 lognegot("server:\tSB TTYPE <something weird>\r\n");
311 case TELOPT_OLD_ENVIRON:
312 case TELOPT_NEW_ENVIRON:
315 if (p < q && *p == TELQUAL_SEND) {
318 sprintf (logbuf, "server:\tSB %s SEND", telopt(sb_opt));
320 if (sb_opt == TELOPT_OLD_ENVIRON) {
321 if (sess->cfg.rfc_environ) {
329 * Try to guess the sense of VAR and VALUE.
335 } else if (*p == BSD_VAR) {
343 * With NEW_ENVIRON, the sense of VAR and VALUE
349 b[0] = IAC; b[1] = SB; b[2] = sb_opt;
352 e = sess->cfg.environmt;
355 while (*e && *e != '\t') b[n++] = *e++;
358 while (*e) b[n++] = *e++;
361 if (*sess->cfg.username) {
362 b[n++] = var; b[n++] = 'U'; b[n++] = 'S';
363 b[n++] = 'E'; b[n++] = 'R'; b[n++] = value;
364 e = sess->cfg.username;
365 while (*e) b[n++] = *e++;
367 b[n++] = IAC; b[n++] = SE;
369 sprintf(logbuf, "client:\tSB %s IS %s", telopt(sb_opt),
370 n==6 ? "<nothing>" : "<stuff>");
378 TELNET_TOPLEVEL, SEENIAC, SEENWILL, SEENWONT, SEENDO, SEENDONT,
379 SEENSB, SUBNEGOT, SUBNEG_IAC, SEENCR
380 } telnet_state = TELNET_TOPLEVEL;
382 static void do_telnet_read (char *buf, int len) {
386 int c = (unsigned char) *buf++;
388 switch (telnet_state) {
389 case TELNET_TOPLEVEL:
391 if (c == NUL && telnet_state == SEENCR)
392 telnet_state = TELNET_TOPLEVEL;
394 telnet_state = SEENIAC;
400 c_write ((char *)b, 1);
402 telnet_state = SEENCR;
404 telnet_state = TELNET_TOPLEVEL;
408 if (c == DO) telnet_state = SEENDO;
409 else if (c == DONT) telnet_state = SEENDONT;
410 else if (c == WILL) telnet_state = SEENWILL;
411 else if (c == WONT) telnet_state = SEENWONT;
412 else if (c == SB) telnet_state = SEENSB;
413 else telnet_state = TELNET_TOPLEVEL;/* ignore _everything_ else! */
416 proc_rec_opt (WILL, c);
417 telnet_state = TELNET_TOPLEVEL;
420 proc_rec_opt (WONT, c);
421 telnet_state = TELNET_TOPLEVEL;
424 proc_rec_opt (DO, c);
425 telnet_state = TELNET_TOPLEVEL;
428 proc_rec_opt (DONT, c);
429 telnet_state = TELNET_TOPLEVEL;
434 telnet_state = SUBNEGOT;
438 telnet_state = SUBNEG_IAC;
441 if (sb_len >= sb_size) {
442 unsigned char *newbuf;
445 realloc(sb_buf, sb_size) :
452 if (sb_len < sb_size)
453 sb_buf[sb_len++] = c;
454 telnet_state = SUBNEGOT;/* in case we came here by goto */
459 goto subneg_addchar; /* yes, it's a hack, I know, but... */
462 telnet_state = TELNET_TOPLEVEL;
470 * Called to set up the Telnet connection. Will arrange for
471 * WM_NETEVENT messages to be passed to the specified window, whose
472 * window procedure should then call telnet_msg().
474 * Returns an error message, or NULL on success.
477 static char *telnet_init (Session *this_sess) {
480 s = net_open(sess, sess->cfg.host, sess->cfg.port);
483 * Initialise option states.
488 for (o = opts; *o; o++)
489 if ((*o)->state == REQUESTED)
490 send_opt ((*o)->send, (*o)->option);
495 * Set up SYNCH state.
504 * Process a WM_NETEVENT message. Will return 0 if the connection
505 * has closed, or <0 for a socket error.
507 static int telnet_msg (Session *sess, SOCKET sock, Net_Event_Type ne) {
511 if (s == INVALID_SOCKET) /* how the hell did we get here?! */
516 ret = net_recv(s, buf, sizeof(buf), 0);
517 if (ret < 0) /* any _other_ error */
521 return 0; /* can't happen, in theory */
526 if (ioctlsocket (s, SIOCATMARK, &i) < 0) {
527 return -20000-WSAGetLastError();
533 do_telnet_read (buf, ret);
537 ret = net_recv(s, buf, sizeof(buf), 0);
539 telnet_state = TELNET_TOPLEVEL;
541 ret = net_recv(s, buf, 1, /*MSG_OOB*/ 0);
543 do_telnet_read (buf, ret);
552 return 1; /* shouldn't happen, but WTF */
556 * Called to send data down the Telnet connection.
558 static void telnet_send (Session *this_sess, char *buf, int len) {
560 static unsigned char iac[2] = { IAC, IAC };
561 static unsigned char cr[2] = { CR, NUL };
563 if (s == INVALID_SOCKET)
567 while (p < buf+len) {
570 while (iswritable((unsigned char)*p) && p < buf+len) p++;
573 while (p < buf+len && !iswritable((unsigned char)*p)) {
574 s_write ((unsigned char)*p == IAC ? iac : cr, 2);
581 * Called to set the size of the window from Telnet's POV.
583 static void telnet_size(Session *sess) {
587 if (s == INVALID_SOCKET || o_naws.state != ACTIVE)
589 b[0] = IAC; b[1] = SB; b[2] = TELOPT_NAWS;
590 b[3] = sess->cols >> 8; b[4] = sess->cols & 0xFF;
591 b[5] = sess->rows >> 8; b[6] = sess->rows & 0xFF;
592 b[7] = IAC; b[8] = SE;
594 sprintf(logbuf, "client:\tSB NAWS %d,%d",
595 ((unsigned char)b[3] << 8) + (unsigned char)b[4],
596 ((unsigned char)b[5] << 8) + (unsigned char)b[6]);
601 * Send Telnet special codes.
603 static void telnet_special (Session *sess, Telnet_Special code) {
606 if (s == INVALID_SOCKET)
611 case TS_AYT: b[1] = AYT; s_write (b, 2); break;
612 case TS_BRK: b[1] = BREAK; s_write (b, 2); break;
613 case TS_EC: b[1] = EC; s_write (b, 2); break;
614 case TS_EL: b[1] = EL; s_write (b, 2); break;
615 case TS_GA: b[1] = GA; s_write (b, 2); break;
616 case TS_NOP: b[1] = NOP; s_write (b, 2); break;
617 case TS_ABORT: b[1] = ABORT; s_write (b, 2); break;
618 case TS_AO: b[1] = AO; s_write (b, 2); break;
619 case TS_IP: b[1] = IP; s_write (b, 2); break;
620 case TS_SUSP: b[1] = SUSP; s_write (b, 2); break;
621 case TS_EOR: b[1] = EOR; s_write (b, 2); break;
622 case TS_EOF: b[1] = xEOF; s_write (b, 2); break;
624 sess->outbuf_head = sess->outbuf_reap = 0;
626 net_send (s, b, 1, SEND_URG);
631 Backend telnet_backend = {