eaf1e20a |
1 | /* |
2 | * Telnet backend. |
3 | */ |
4 | |
374330e2 |
5 | #include <stdio.h> |
6 | #include <stdlib.h> |
374330e2 |
7 | |
8 | #include "putty.h" |
9 | |
10 | #ifndef FALSE |
11 | #define FALSE 0 |
12 | #endif |
13 | #ifndef TRUE |
14 | #define TRUE 1 |
15 | #endif |
16 | |
32874aea |
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 */ |
347e5401 |
79 | #define TELOPT_TN3270E 40 /* TN3270 enhancements */ |
80 | #define TELOPT_XAUTH 41 |
81 | #define TELOPT_CHARSET 42 /* Character set */ |
82 | #define TELOPT_RSP 43 /* Remote serial port */ |
83 | #define TELOPT_COM_PORT_OPTION 44 /* Com port control */ |
84 | #define TELOPT_SLE 45 /* Suppress local echo */ |
85 | #define TELOPT_STARTTLS 46 /* Start TLS */ |
86 | #define TELOPT_KERMIT 47 /* Automatic Kermit file transfer */ |
87 | #define TELOPT_SEND_URL 48 |
88 | #define TELOPT_FORWARD_X 49 |
89 | #define TELOPT_PRAGMA_LOGON 138 |
90 | #define TELOPT_SSPI_LOGON 139 |
91 | #define TELOPT_PRAGMA_HEARTBEAT 140 |
32874aea |
92 | #define TELOPT_EXOPL 255 /* extended-options-list */ |
93 | |
94 | #define TELQUAL_IS 0 /* option is... */ |
95 | #define TELQUAL_SEND 1 /* send option */ |
96 | #define TELQUAL_INFO 2 /* ENVIRON: informational version of IS */ |
374330e2 |
97 | #define BSD_VAR 1 |
98 | #define BSD_VALUE 0 |
99 | #define RFC_VAR 0 |
100 | #define RFC_VALUE 1 |
101 | |
102 | #define CR 13 |
103 | #define LF 10 |
104 | #define NUL 0 |
105 | |
656fa244 |
106 | #define iswritable(x) \ |
107 | ( (x) != IAC && \ |
108 | (telnet->opt_states[o_we_bin.index] == ACTIVE || (x) != CR)) |
374330e2 |
109 | |
32874aea |
110 | static char *telopt(int opt) |
111 | { |
374330e2 |
112 | #define i(x) if (opt == TELOPT_ ## x) return #x; |
32874aea |
113 | i(BINARY); |
114 | i(ECHO); |
115 | i(RCP); |
116 | i(SGA); |
117 | i(NAMS); |
118 | i(STATUS); |
119 | i(TM); |
120 | i(RCTE); |
121 | i(NAOL); |
122 | i(NAOP); |
123 | i(NAOCRD); |
124 | i(NAOHTS); |
125 | i(NAOHTD); |
126 | i(NAOFFD); |
127 | i(NAOVTS); |
128 | i(NAOVTD); |
129 | i(NAOLFD); |
130 | i(XASCII); |
131 | i(LOGOUT); |
132 | i(BM); |
133 | i(DET); |
134 | i(SUPDUP); |
135 | i(SUPDUPOUTPUT); |
136 | i(SNDLOC); |
137 | i(TTYPE); |
138 | i(EOR); |
139 | i(TUID); |
140 | i(OUTMRK); |
141 | i(TTYLOC); |
142 | i(X3PAD); |
143 | i(NAWS); |
144 | i(TSPEED); |
145 | i(LFLOW); |
146 | i(LINEMODE); |
147 | i(XDISPLOC); |
148 | i(OLD_ENVIRON); |
149 | i(AUTHENTICATION); |
150 | i(ENCRYPT); |
151 | i(NEW_ENVIRON); |
347e5401 |
152 | i(TN3270E); |
153 | i(XAUTH); |
154 | i(CHARSET); |
155 | i(RSP); |
156 | i(COM_PORT_OPTION); |
157 | i(SLE); |
158 | i(STARTTLS); |
159 | i(KERMIT); |
160 | i(SEND_URL); |
161 | i(FORWARD_X); |
162 | i(PRAGMA_LOGON); |
163 | i(SSPI_LOGON); |
164 | i(PRAGMA_HEARTBEAT); |
32874aea |
165 | i(EXOPL); |
374330e2 |
166 | #undef i |
167 | return "<unknown>"; |
168 | } |
169 | |
51470298 |
170 | static void telnet_size(void *handle, int width, int height); |
374330e2 |
171 | |
172 | struct Opt { |
173 | int send; /* what we initially send */ |
174 | int nsend; /* -ve send if requested to stop it */ |
175 | int ack, nak; /* +ve and -ve acknowledgements */ |
176 | int option; /* the option code */ |
51470298 |
177 | int index; /* index into telnet->opt_states[] */ |
374330e2 |
178 | enum { |
179 | REQUESTED, ACTIVE, INACTIVE, REALLY_INACTIVE |
51470298 |
180 | } initial_state; |
181 | }; |
182 | |
183 | enum { |
184 | OPTINDEX_NAWS, |
185 | OPTINDEX_TSPEED, |
186 | OPTINDEX_TTYPE, |
187 | OPTINDEX_OENV, |
188 | OPTINDEX_NENV, |
189 | OPTINDEX_ECHO, |
190 | OPTINDEX_WE_SGA, |
191 | OPTINDEX_THEY_SGA, |
656fa244 |
192 | OPTINDEX_WE_BIN, |
193 | OPTINDEX_THEY_BIN, |
51470298 |
194 | NUM_OPTS |
374330e2 |
195 | }; |
196 | |
51470298 |
197 | static const struct Opt o_naws = |
198 | { WILL, WONT, DO, DONT, TELOPT_NAWS, OPTINDEX_NAWS, REQUESTED }; |
199 | static const struct Opt o_tspeed = |
200 | { WILL, WONT, DO, DONT, TELOPT_TSPEED, OPTINDEX_TSPEED, REQUESTED }; |
201 | static const struct Opt o_ttype = |
202 | { WILL, WONT, DO, DONT, TELOPT_TTYPE, OPTINDEX_TTYPE, REQUESTED }; |
5f95fdf1 |
203 | static const struct Opt o_oenv = |
204 | { WILL, WONT, DO, DONT, TELOPT_OLD_ENVIRON, OPTINDEX_OENV, INACTIVE }; |
205 | static const struct Opt o_nenv = |
206 | { WILL, WONT, DO, DONT, TELOPT_NEW_ENVIRON, OPTINDEX_NENV, REQUESTED }; |
51470298 |
207 | static const struct Opt o_echo = |
208 | { DO, DONT, WILL, WONT, TELOPT_ECHO, OPTINDEX_ECHO, REQUESTED }; |
209 | static const struct Opt o_we_sga = |
210 | { WILL, WONT, DO, DONT, TELOPT_SGA, OPTINDEX_WE_SGA, REQUESTED }; |
211 | static const struct Opt o_they_sga = |
212 | { DO, DONT, WILL, WONT, TELOPT_SGA, OPTINDEX_THEY_SGA, REQUESTED }; |
656fa244 |
213 | static const struct Opt o_we_bin = |
214 | { WILL, WONT, DO, DONT, TELOPT_BINARY, OPTINDEX_WE_BIN, INACTIVE }; |
215 | static const struct Opt o_they_bin = |
216 | { DO, DONT, WILL, WONT, TELOPT_BINARY, OPTINDEX_THEY_BIN, INACTIVE }; |
51470298 |
217 | |
218 | static const struct Opt *const opts[] = { |
374330e2 |
219 | &o_naws, &o_tspeed, &o_ttype, &o_oenv, &o_nenv, &o_echo, |
656fa244 |
220 | &o_we_sga, &o_they_sga, &o_we_bin, &o_they_bin, NULL |
374330e2 |
221 | }; |
222 | |
51470298 |
223 | typedef struct telnet_tag { |
224 | const struct plug_function_table *fn; |
225 | /* the above field _must_ be first in the structure */ |
226 | |
227 | Socket s; |
228 | |
229 | void *frontend; |
b9d7bcad |
230 | void *ldisc; |
51470298 |
231 | int term_width, term_height; |
232 | |
233 | int opt_states[NUM_OPTS]; |
234 | |
235 | int echoing, editing; |
236 | int activated; |
237 | int bufsize; |
238 | int in_synch; |
239 | int sb_opt, sb_len; |
a1fd562d |
240 | unsigned char *sb_buf; |
51470298 |
241 | int sb_size; |
242 | |
243 | enum { |
244 | TOP_LEVEL, SEENIAC, SEENWILL, SEENWONT, SEENDO, SEENDONT, |
245 | SEENSB, SUBNEGOT, SUBNEG_IAC, SEENCR |
246 | } state; |
247 | |
86916870 |
248 | Config cfg; |
39934deb |
249 | |
250 | Pinger pinger; |
51470298 |
251 | } *Telnet; |
252 | |
5471d09a |
253 | #define TELNET_MAX_BACKLOG 4096 |
254 | |
374330e2 |
255 | #define SB_DELTA 1024 |
256 | |
ee00c99a |
257 | static void c_write(Telnet telnet, char *buf, int len) |
32874aea |
258 | { |
5471d09a |
259 | int backlog; |
ee00c99a |
260 | backlog = from_backend(telnet->frontend, 0, buf, len); |
51470298 |
261 | sk_set_frozen(telnet->s, backlog > TELNET_MAX_BACKLOG); |
fe50e814 |
262 | } |
263 | |
a8327734 |
264 | static void log_option(Telnet telnet, char *sender, int cmd, int option) |
32874aea |
265 | { |
57356d63 |
266 | char *buf; |
24d7f854 |
267 | /* |
268 | * The strange-looking "<?""?>" below is there to avoid a |
269 | * trigraph - a double question mark followed by > maps to a |
270 | * closing brace character! |
271 | */ |
57356d63 |
272 | buf = dupprintf("%s:\t%s %s", sender, |
273 | (cmd == WILL ? "WILL" : cmd == WONT ? "WONT" : |
274 | cmd == DO ? "DO" : cmd == DONT ? "DONT" : "<?""?>"), |
275 | telopt(option)); |
a8327734 |
276 | logevent(telnet->frontend, buf); |
57356d63 |
277 | sfree(buf); |
374330e2 |
278 | } |
279 | |
51470298 |
280 | static void send_opt(Telnet telnet, int cmd, int option) |
32874aea |
281 | { |
374330e2 |
282 | unsigned char b[3]; |
283 | |
32874aea |
284 | b[0] = IAC; |
285 | b[1] = cmd; |
286 | b[2] = option; |
a1fd562d |
287 | telnet->bufsize = sk_write(telnet->s, (char *)b, 3); |
a8327734 |
288 | log_option(telnet, "client", cmd, option); |
374330e2 |
289 | } |
290 | |
51470298 |
291 | static void deactivate_option(Telnet telnet, const struct Opt *o) |
32874aea |
292 | { |
51470298 |
293 | if (telnet->opt_states[o->index] == REQUESTED || |
294 | telnet->opt_states[o->index] == ACTIVE) |
295 | send_opt(telnet, o->nsend, o->option); |
296 | telnet->opt_states[o->index] = REALLY_INACTIVE; |
374330e2 |
297 | } |
298 | |
708bbbbe |
299 | /* |
300 | * Generate side effects of enabling or disabling an option. |
301 | */ |
51470298 |
302 | static void option_side_effects(Telnet telnet, const struct Opt *o, int enabled) |
32874aea |
303 | { |
0965bee0 |
304 | if (o->option == TELOPT_ECHO && o->send == DO) |
51470298 |
305 | telnet->echoing = !enabled; |
b6c680d4 |
306 | else if (o->option == TELOPT_SGA && o->send == DO) |
51470298 |
307 | telnet->editing = !enabled; |
b9d7bcad |
308 | if (telnet->ldisc) /* cause ldisc to notice the change */ |
309 | ldisc_send(telnet->ldisc, NULL, 0, 0); |
8faa456c |
310 | |
311 | /* Ensure we get the minimum options */ |
51470298 |
312 | if (!telnet->activated) { |
313 | if (telnet->opt_states[o_echo.index] == INACTIVE) { |
314 | telnet->opt_states[o_echo.index] = REQUESTED; |
315 | send_opt(telnet, o_echo.send, o_echo.option); |
8faa456c |
316 | } |
51470298 |
317 | if (telnet->opt_states[o_we_sga.index] == INACTIVE) { |
318 | telnet->opt_states[o_we_sga.index] = REQUESTED; |
319 | send_opt(telnet, o_we_sga.send, o_we_sga.option); |
8faa456c |
320 | } |
51470298 |
321 | if (telnet->opt_states[o_they_sga.index] == INACTIVE) { |
322 | telnet->opt_states[o_they_sga.index] = REQUESTED; |
323 | send_opt(telnet, o_they_sga.send, o_they_sga.option); |
8faa456c |
324 | } |
51470298 |
325 | telnet->activated = TRUE; |
8faa456c |
326 | } |
708bbbbe |
327 | } |
328 | |
51470298 |
329 | static void activate_option(Telnet telnet, const struct Opt *o) |
32874aea |
330 | { |
374330e2 |
331 | if (o->send == WILL && o->option == TELOPT_NAWS) |
51470298 |
332 | telnet_size(telnet, telnet->term_width, telnet->term_height); |
374330e2 |
333 | if (o->send == WILL && |
334 | (o->option == TELOPT_NEW_ENVIRON || |
335 | o->option == TELOPT_OLD_ENVIRON)) { |
336 | /* |
337 | * We may only have one kind of ENVIRON going at a time. |
338 | * This is a hack, but who cares. |
339 | */ |
51470298 |
340 | deactivate_option(telnet, o->option == |
32874aea |
341 | TELOPT_NEW_ENVIRON ? &o_oenv : &o_nenv); |
374330e2 |
342 | } |
51470298 |
343 | option_side_effects(telnet, o, 1); |
374330e2 |
344 | } |
345 | |
51470298 |
346 | static void refused_option(Telnet telnet, const struct Opt *o) |
32874aea |
347 | { |
374330e2 |
348 | if (o->send == WILL && o->option == TELOPT_NEW_ENVIRON && |
51470298 |
349 | telnet->opt_states[o_oenv.index] == INACTIVE) { |
350 | send_opt(telnet, WILL, TELOPT_OLD_ENVIRON); |
351 | telnet->opt_states[o_oenv.index] = REQUESTED; |
374330e2 |
352 | } |
51470298 |
353 | option_side_effects(telnet, o, 0); |
374330e2 |
354 | } |
355 | |
51470298 |
356 | static void proc_rec_opt(Telnet telnet, int cmd, int option) |
32874aea |
357 | { |
51470298 |
358 | const struct Opt *const *o; |
374330e2 |
359 | |
a8327734 |
360 | log_option(telnet, "server", cmd, option); |
374330e2 |
361 | for (o = opts; *o; o++) { |
362 | if ((*o)->option == option && (*o)->ack == cmd) { |
51470298 |
363 | switch (telnet->opt_states[(*o)->index]) { |
374330e2 |
364 | case REQUESTED: |
51470298 |
365 | telnet->opt_states[(*o)->index] = ACTIVE; |
366 | activate_option(telnet, *o); |
374330e2 |
367 | break; |
368 | case ACTIVE: |
369 | break; |
370 | case INACTIVE: |
51470298 |
371 | telnet->opt_states[(*o)->index] = ACTIVE; |
372 | send_opt(telnet, (*o)->send, option); |
373 | activate_option(telnet, *o); |
374330e2 |
374 | break; |
375 | case REALLY_INACTIVE: |
51470298 |
376 | send_opt(telnet, (*o)->nsend, option); |
374330e2 |
377 | break; |
378 | } |
379 | return; |
380 | } else if ((*o)->option == option && (*o)->nak == cmd) { |
51470298 |
381 | switch (telnet->opt_states[(*o)->index]) { |
374330e2 |
382 | case REQUESTED: |
51470298 |
383 | telnet->opt_states[(*o)->index] = INACTIVE; |
384 | refused_option(telnet, *o); |
374330e2 |
385 | break; |
386 | case ACTIVE: |
51470298 |
387 | telnet->opt_states[(*o)->index] = INACTIVE; |
388 | send_opt(telnet, (*o)->nsend, option); |
389 | option_side_effects(telnet, *o, 0); |
374330e2 |
390 | break; |
391 | case INACTIVE: |
392 | case REALLY_INACTIVE: |
393 | break; |
394 | } |
395 | return; |
396 | } |
397 | } |
398 | /* |
399 | * If we reach here, the option was one we weren't prepared to |
108b0b94 |
400 | * cope with. If the request was positive (WILL or DO), we send |
401 | * a negative ack to indicate refusal. If the request was |
402 | * negative (WONT / DONT), we must do nothing. |
374330e2 |
403 | */ |
108b0b94 |
404 | if (cmd == WILL || cmd == DO) |
405 | send_opt(telnet, (cmd == WILL ? DONT : WONT), option); |
374330e2 |
406 | } |
407 | |
51470298 |
408 | static void process_subneg(Telnet telnet) |
32874aea |
409 | { |
374330e2 |
410 | unsigned char b[2048], *p, *q; |
411 | int var, value, n; |
412 | char *e; |
413 | |
51470298 |
414 | switch (telnet->sb_opt) { |
374330e2 |
415 | case TELOPT_TSPEED: |
51470298 |
416 | if (telnet->sb_len == 1 && telnet->sb_buf[0] == TELQUAL_SEND) { |
57356d63 |
417 | char *logbuf; |
32874aea |
418 | b[0] = IAC; |
419 | b[1] = SB; |
420 | b[2] = TELOPT_TSPEED; |
374330e2 |
421 | b[3] = TELQUAL_IS; |
86916870 |
422 | strcpy((char *)(b + 4), telnet->cfg.termspeed); |
423 | n = 4 + strlen(telnet->cfg.termspeed); |
32874aea |
424 | b[n] = IAC; |
425 | b[n + 1] = SE; |
a1fd562d |
426 | telnet->bufsize = sk_write(telnet->s, (char *)b, n + 2); |
a8327734 |
427 | logevent(telnet->frontend, "server:\tSB TSPEED SEND"); |
86916870 |
428 | logbuf = dupprintf("client:\tSB TSPEED IS %s", telnet->cfg.termspeed); |
a8327734 |
429 | logevent(telnet->frontend, logbuf); |
57356d63 |
430 | sfree(logbuf); |
374330e2 |
431 | } else |
a8327734 |
432 | logevent(telnet->frontend, "server:\tSB TSPEED <something weird>"); |
374330e2 |
433 | break; |
434 | case TELOPT_TTYPE: |
51470298 |
435 | if (telnet->sb_len == 1 && telnet->sb_buf[0] == TELQUAL_SEND) { |
57356d63 |
436 | char *logbuf; |
32874aea |
437 | b[0] = IAC; |
438 | b[1] = SB; |
439 | b[2] = TELOPT_TTYPE; |
374330e2 |
440 | b[3] = TELQUAL_IS; |
86916870 |
441 | for (n = 0; telnet->cfg.termtype[n]; n++) |
442 | b[n + 4] = (telnet->cfg.termtype[n] >= 'a' |
443 | && telnet->cfg.termtype[n] <= |
444 | 'z' ? telnet->cfg.termtype[n] + 'A' - |
445 | 'a' : telnet->cfg.termtype[n]); |
32874aea |
446 | b[n + 4] = IAC; |
447 | b[n + 5] = SE; |
a1fd562d |
448 | telnet->bufsize = sk_write(telnet->s, (char *)b, n + 6); |
32874aea |
449 | b[n + 4] = 0; |
a8327734 |
450 | logevent(telnet->frontend, "server:\tSB TTYPE SEND"); |
57356d63 |
451 | logbuf = dupprintf("client:\tSB TTYPE IS %s", b + 4); |
a8327734 |
452 | logevent(telnet->frontend, logbuf); |
57356d63 |
453 | sfree(logbuf); |
374330e2 |
454 | } else |
a8327734 |
455 | logevent(telnet->frontend, "server:\tSB TTYPE <something weird>\r\n"); |
374330e2 |
456 | break; |
457 | case TELOPT_OLD_ENVIRON: |
32874aea |
458 | case TELOPT_NEW_ENVIRON: |
51470298 |
459 | p = telnet->sb_buf; |
460 | q = p + telnet->sb_len; |
374330e2 |
461 | if (p < q && *p == TELQUAL_SEND) { |
57356d63 |
462 | char *logbuf; |
374330e2 |
463 | p++; |
57356d63 |
464 | logbuf = dupprintf("server:\tSB %s SEND", telopt(telnet->sb_opt)); |
a8327734 |
465 | logevent(telnet->frontend, logbuf); |
57356d63 |
466 | sfree(logbuf); |
51470298 |
467 | if (telnet->sb_opt == TELOPT_OLD_ENVIRON) { |
86916870 |
468 | if (telnet->cfg.rfc_environ) { |
374330e2 |
469 | value = RFC_VALUE; |
470 | var = RFC_VAR; |
471 | } else { |
472 | value = BSD_VALUE; |
473 | var = BSD_VAR; |
474 | } |
475 | /* |
476 | * Try to guess the sense of VAR and VALUE. |
477 | */ |
478 | while (p < q) { |
479 | if (*p == RFC_VAR) { |
480 | value = RFC_VALUE; |
481 | var = RFC_VAR; |
482 | } else if (*p == BSD_VAR) { |
483 | value = BSD_VALUE; |
484 | var = BSD_VAR; |
485 | } |
486 | p++; |
487 | } |
488 | } else { |
489 | /* |
490 | * With NEW_ENVIRON, the sense of VAR and VALUE |
491 | * isn't in doubt. |
492 | */ |
493 | value = RFC_VALUE; |
494 | var = RFC_VAR; |
495 | } |
32874aea |
496 | b[0] = IAC; |
497 | b[1] = SB; |
51470298 |
498 | b[2] = telnet->sb_opt; |
374330e2 |
499 | b[3] = TELQUAL_IS; |
500 | n = 4; |
86916870 |
501 | e = telnet->cfg.environmt; |
374330e2 |
502 | while (*e) { |
503 | b[n++] = var; |
32874aea |
504 | while (*e && *e != '\t') |
505 | b[n++] = *e++; |
506 | if (*e == '\t') |
507 | e++; |
374330e2 |
508 | b[n++] = value; |
32874aea |
509 | while (*e) |
510 | b[n++] = *e++; |
374330e2 |
511 | e++; |
512 | } |
86916870 |
513 | if (*telnet->cfg.username) { |
32874aea |
514 | b[n++] = var; |
515 | b[n++] = 'U'; |
516 | b[n++] = 'S'; |
517 | b[n++] = 'E'; |
518 | b[n++] = 'R'; |
519 | b[n++] = value; |
86916870 |
520 | e = telnet->cfg.username; |
32874aea |
521 | while (*e) |
522 | b[n++] = *e++; |
374330e2 |
523 | } |
32874aea |
524 | b[n++] = IAC; |
525 | b[n++] = SE; |
a1fd562d |
526 | telnet->bufsize = sk_write(telnet->s, (char *)b, n); |
afaa639b |
527 | logbuf = dupprintf("client:\tSB %s IS %s%s%s%s", |
528 | telopt(telnet->sb_opt), |
529 | *telnet->cfg.username ? "USER=" : "", |
530 | telnet->cfg.username, |
531 | *telnet->cfg.username ? " " : "", |
532 | n == 6 ? "<nothing>" : |
533 | (*telnet->cfg.environmt ? "<stuff>" : "")); |
a8327734 |
534 | logevent(telnet->frontend, logbuf); |
57356d63 |
535 | sfree(logbuf); |
374330e2 |
536 | } |
537 | break; |
538 | } |
539 | } |
540 | |
51470298 |
541 | static void do_telnet_read(Telnet telnet, char *buf, int len) |
32874aea |
542 | { |
ee00c99a |
543 | char *outbuf = NULL; |
544 | int outbuflen = 0, outbufsize = 0; |
545 | |
546 | #define ADDTOBUF(c) do { \ |
547 | if (outbuflen >= outbufsize) { \ |
548 | outbufsize = outbuflen + 256; \ |
549 | outbuf = sresize(outbuf, outbufsize, char); \ |
550 | } \ |
551 | outbuf[outbuflen++] = (c); \ |
552 | } while (0) |
374330e2 |
553 | |
554 | while (len--) { |
555 | int c = (unsigned char) *buf++; |
556 | |
51470298 |
557 | switch (telnet->state) { |
887035a5 |
558 | case TOP_LEVEL: |
374330e2 |
559 | case SEENCR: |
51470298 |
560 | if (c == NUL && telnet->state == SEENCR) |
561 | telnet->state = TOP_LEVEL; |
374330e2 |
562 | else if (c == IAC) |
51470298 |
563 | telnet->state = SEENIAC; |
374330e2 |
564 | else { |
51470298 |
565 | if (!telnet->in_synch) |
ee00c99a |
566 | ADDTOBUF(c); |
2f938b83 |
567 | |
568 | #if 1 |
569 | /* I can't get the F***ing winsock to insert the urgent IAC |
570 | * into the right position! Even with SO_OOBINLINE it gives |
571 | * it to recv too soon. And of course the DM byte (that |
572 | * arrives in the same packet!) appears several K later!! |
573 | * |
574 | * Oh well, we do get the DM in the right place so I'll |
575 | * just stop hiding on the next 0xf2 and hope for the best. |
576 | */ |
32874aea |
577 | else if (c == DM) |
51470298 |
578 | telnet->in_synch = 0; |
2f938b83 |
579 | #endif |
656fa244 |
580 | if (c == CR && telnet->opt_states[o_they_bin.index] != ACTIVE) |
51470298 |
581 | telnet->state = SEENCR; |
374330e2 |
582 | else |
51470298 |
583 | telnet->state = TOP_LEVEL; |
374330e2 |
584 | } |
585 | break; |
586 | case SEENIAC: |
32874aea |
587 | if (c == DO) |
51470298 |
588 | telnet->state = SEENDO; |
32874aea |
589 | else if (c == DONT) |
51470298 |
590 | telnet->state = SEENDONT; |
32874aea |
591 | else if (c == WILL) |
51470298 |
592 | telnet->state = SEENWILL; |
32874aea |
593 | else if (c == WONT) |
51470298 |
594 | telnet->state = SEENWONT; |
32874aea |
595 | else if (c == SB) |
51470298 |
596 | telnet->state = SEENSB; |
2f938b83 |
597 | else if (c == DM) { |
51470298 |
598 | telnet->in_synch = 0; |
599 | telnet->state = TOP_LEVEL; |
32874aea |
600 | } else { |
ded38628 |
601 | /* ignore everything else; print it if it's IAC */ |
602 | if (c == IAC) { |
ee00c99a |
603 | ADDTOBUF(c); |
ded38628 |
604 | } |
51470298 |
605 | telnet->state = TOP_LEVEL; |
300d41b0 |
606 | } |
374330e2 |
607 | break; |
608 | case SEENWILL: |
51470298 |
609 | proc_rec_opt(telnet, WILL, c); |
610 | telnet->state = TOP_LEVEL; |
374330e2 |
611 | break; |
612 | case SEENWONT: |
51470298 |
613 | proc_rec_opt(telnet, WONT, c); |
614 | telnet->state = TOP_LEVEL; |
374330e2 |
615 | break; |
616 | case SEENDO: |
51470298 |
617 | proc_rec_opt(telnet, DO, c); |
618 | telnet->state = TOP_LEVEL; |
374330e2 |
619 | break; |
620 | case SEENDONT: |
51470298 |
621 | proc_rec_opt(telnet, DONT, c); |
622 | telnet->state = TOP_LEVEL; |
374330e2 |
623 | break; |
624 | case SEENSB: |
51470298 |
625 | telnet->sb_opt = c; |
626 | telnet->sb_len = 0; |
627 | telnet->state = SUBNEGOT; |
374330e2 |
628 | break; |
629 | case SUBNEGOT: |
630 | if (c == IAC) |
51470298 |
631 | telnet->state = SUBNEG_IAC; |
374330e2 |
632 | else { |
32874aea |
633 | subneg_addchar: |
51470298 |
634 | if (telnet->sb_len >= telnet->sb_size) { |
51470298 |
635 | telnet->sb_size += SB_DELTA; |
3d88e64d |
636 | telnet->sb_buf = sresize(telnet->sb_buf, telnet->sb_size, |
637 | unsigned char); |
374330e2 |
638 | } |
3d88e64d |
639 | telnet->sb_buf[telnet->sb_len++] = c; |
51470298 |
640 | telnet->state = SUBNEGOT; /* in case we came here by goto */ |
374330e2 |
641 | } |
642 | break; |
643 | case SUBNEG_IAC: |
644 | if (c != SE) |
645 | goto subneg_addchar; /* yes, it's a hack, I know, but... */ |
646 | else { |
51470298 |
647 | process_subneg(telnet); |
648 | telnet->state = TOP_LEVEL; |
374330e2 |
649 | } |
650 | break; |
651 | } |
652 | } |
ee00c99a |
653 | |
654 | if (outbuflen) |
655 | c_write(telnet, outbuf, outbuflen); |
656 | sfree(outbuf); |
374330e2 |
657 | } |
658 | |
7555d6a5 |
659 | static void telnet_log(Plug plug, int type, SockAddr addr, int port, |
660 | const char *error_msg, int error_code) |
661 | { |
662 | Telnet telnet = (Telnet) plug; |
663 | char addrbuf[256], *msg; |
664 | |
665 | sk_getaddr(addr, addrbuf, lenof(addrbuf)); |
666 | |
667 | if (type == 0) |
668 | msg = dupprintf("Connecting to %s port %d", addrbuf, port); |
669 | else |
670 | msg = dupprintf("Failed to connect to %s: %s", addrbuf, error_msg); |
671 | |
672 | logevent(telnet->frontend, msg); |
673 | } |
674 | |
cbe2d68f |
675 | static int telnet_closing(Plug plug, const char *error_msg, int error_code, |
32874aea |
676 | int calling_back) |
677 | { |
51470298 |
678 | Telnet telnet = (Telnet) plug; |
679 | |
680 | if (telnet->s) { |
681 | sk_close(telnet->s); |
682 | telnet->s = NULL; |
39934deb |
683 | notify_remote_exit(telnet->frontend); |
f3ab576e |
684 | } |
7e78000d |
685 | if (error_msg) { |
a8327734 |
686 | logevent(telnet->frontend, error_msg); |
971bcc0a |
687 | connection_fatal(telnet->frontend, "%s", error_msg); |
7555d6a5 |
688 | } |
689 | /* Otherwise, the remote side closed the connection normally. */ |
7e78000d |
690 | return 0; |
691 | } |
692 | |
32874aea |
693 | static int telnet_receive(Plug plug, int urgent, char *data, int len) |
694 | { |
51470298 |
695 | Telnet telnet = (Telnet) plug; |
32874aea |
696 | if (urgent) |
51470298 |
697 | telnet->in_synch = TRUE; |
698 | do_telnet_read(telnet, data, len); |
8df7a775 |
699 | return 1; |
700 | } |
701 | |
3ad9d396 |
702 | static void telnet_sent(Plug plug, int bufsize) |
703 | { |
51470298 |
704 | Telnet telnet = (Telnet) plug; |
705 | telnet->bufsize = bufsize; |
3ad9d396 |
706 | } |
707 | |
374330e2 |
708 | /* |
8df7a775 |
709 | * Called to set up the Telnet connection. |
374330e2 |
710 | * |
711 | * Returns an error message, or NULL on success. |
712 | * |
6e1ebb76 |
713 | * Also places the canonical host name into `realhost'. It must be |
714 | * freed by the caller. |
374330e2 |
715 | */ |
cbe2d68f |
716 | static const char *telnet_init(void *frontend_handle, void **backend_handle, |
717 | Config *cfg, |
718 | char *host, int port, char **realhost, |
79bf227b |
719 | int nodelay, int keepalive) |
32874aea |
720 | { |
51470298 |
721 | static const struct plug_function_table fn_table = { |
7555d6a5 |
722 | telnet_log, |
7e78000d |
723 | telnet_closing, |
3ad9d396 |
724 | telnet_receive, |
725 | telnet_sent |
51470298 |
726 | }; |
8df7a775 |
727 | SockAddr addr; |
cbe2d68f |
728 | const char *err; |
51470298 |
729 | Telnet telnet; |
730 | |
3d88e64d |
731 | telnet = snew(struct telnet_tag); |
51470298 |
732 | telnet->fn = &fn_table; |
86916870 |
733 | telnet->cfg = *cfg; /* STRUCTURE COPY */ |
51470298 |
734 | telnet->s = NULL; |
735 | telnet->echoing = TRUE; |
736 | telnet->editing = TRUE; |
737 | telnet->activated = FALSE; |
738 | telnet->sb_buf = NULL; |
739 | telnet->sb_size = 0; |
740 | telnet->frontend = frontend_handle; |
86916870 |
741 | telnet->term_width = telnet->cfg.width; |
742 | telnet->term_height = telnet->cfg.height; |
51470298 |
743 | telnet->state = TOP_LEVEL; |
cc31ab78 |
744 | telnet->ldisc = NULL; |
39934deb |
745 | telnet->pinger = NULL; |
51470298 |
746 | *backend_handle = telnet; |
887035a5 |
747 | |
374330e2 |
748 | /* |
749 | * Try to find host. |
750 | */ |
3ad9d396 |
751 | { |
57356d63 |
752 | char *buf; |
05581745 |
753 | buf = dupprintf("Looking up host \"%s\"%s", host, |
754 | (cfg->addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" : |
755 | (cfg->addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" : |
756 | ""))); |
a8327734 |
757 | logevent(telnet->frontend, buf); |
57356d63 |
758 | sfree(buf); |
3ad9d396 |
759 | } |
05581745 |
760 | addr = name_lookup(host, port, realhost, &telnet->cfg, cfg->addressfamily); |
f85e6f6e |
761 | if ((err = sk_addr_error(addr)) != NULL) { |
762 | sk_addr_free(addr); |
8df7a775 |
763 | return err; |
f85e6f6e |
764 | } |
374330e2 |
765 | |
766 | if (port < 0) |
767 | port = 23; /* default telnet port */ |
768 | |
769 | /* |
770 | * Open socket. |
771 | */ |
51470298 |
772 | telnet->s = new_connection(addr, *realhost, port, 0, 1, |
79bf227b |
773 | nodelay, keepalive, (Plug) telnet, &telnet->cfg); |
a1fd562d |
774 | if ((err = sk_socket_error(telnet->s)) != NULL) |
8df7a775 |
775 | return err; |
374330e2 |
776 | |
39934deb |
777 | telnet->pinger = pinger_new(&telnet->cfg, &telnet_backend, telnet); |
778 | |
374330e2 |
779 | /* |
780 | * Initialise option states. |
781 | */ |
86916870 |
782 | if (telnet->cfg.passive_telnet) { |
51470298 |
783 | const struct Opt *const *o; |
8faa456c |
784 | |
785 | for (o = opts; *o; o++) |
51470298 |
786 | telnet->opt_states[(*o)->index] = INACTIVE; |
8faa456c |
787 | } else { |
51470298 |
788 | const struct Opt *const *o; |
374330e2 |
789 | |
51470298 |
790 | for (o = opts; *o; o++) { |
791 | telnet->opt_states[(*o)->index] = (*o)->initial_state; |
792 | if (telnet->opt_states[(*o)->index] == REQUESTED) |
793 | send_opt(telnet, (*o)->send, (*o)->option); |
794 | } |
795 | telnet->activated = TRUE; |
374330e2 |
796 | } |
797 | |
374330e2 |
798 | /* |
799 | * Set up SYNCH state. |
800 | */ |
51470298 |
801 | telnet->in_synch = FALSE; |
6f34e365 |
802 | |
533b1743 |
803 | /* |
804 | * We can send special commands from the start. |
805 | */ |
806 | update_specials_menu(telnet->frontend); |
807 | |
374330e2 |
808 | return NULL; |
809 | } |
810 | |
fabd1805 |
811 | static void telnet_free(void *handle) |
812 | { |
813 | Telnet telnet = (Telnet) handle; |
814 | |
815 | sfree(telnet->sb_buf); |
816 | if (telnet->s) |
817 | sk_close(telnet->s); |
39934deb |
818 | if (telnet->pinger) |
819 | pinger_free(telnet->pinger); |
fabd1805 |
820 | sfree(telnet); |
821 | } |
374330e2 |
822 | /* |
86916870 |
823 | * Reconfigure the Telnet backend. There's no immediate action |
824 | * necessary, in this backend: we just save the fresh config for |
825 | * any subsequent negotiations. |
826 | */ |
827 | static void telnet_reconfig(void *handle, Config *cfg) |
828 | { |
829 | Telnet telnet = (Telnet) handle; |
39934deb |
830 | pinger_reconfig(telnet->pinger, &telnet->cfg, cfg); |
86916870 |
831 | telnet->cfg = *cfg; /* STRUCTURE COPY */ |
832 | } |
833 | |
834 | /* |
374330e2 |
835 | * Called to send data down the Telnet connection. |
836 | */ |
51470298 |
837 | static int telnet_send(void *handle, char *buf, int len) |
32874aea |
838 | { |
51470298 |
839 | Telnet telnet = (Telnet) handle; |
a1fd562d |
840 | unsigned char *p, *end; |
c85623f9 |
841 | static const unsigned char iac[2] = { IAC, IAC }; |
842 | static const unsigned char cr[2] = { CR, NUL }; |
a5f3e637 |
843 | #if 0 |
c85623f9 |
844 | static const unsigned char nl[2] = { CR, LF }; |
a5f3e637 |
845 | #endif |
374330e2 |
846 | |
51470298 |
847 | if (telnet->s == NULL) |
5471d09a |
848 | return 0; |
374330e2 |
849 | |
a1fd562d |
850 | p = (unsigned char *)buf; |
851 | end = (unsigned char *)(buf + len); |
852 | while (p < end) { |
853 | unsigned char *q = p; |
374330e2 |
854 | |
a1fd562d |
855 | while (p < end && iswritable(*p)) |
32874aea |
856 | p++; |
a1fd562d |
857 | telnet->bufsize = sk_write(telnet->s, (char *)q, p - q); |
374330e2 |
858 | |
a1fd562d |
859 | while (p < end && !iswritable(*p)) { |
51470298 |
860 | telnet->bufsize = |
a1fd562d |
861 | sk_write(telnet->s, (char *)(*p == IAC ? iac : cr), 2); |
374330e2 |
862 | p++; |
863 | } |
864 | } |
5471d09a |
865 | |
51470298 |
866 | return telnet->bufsize; |
5471d09a |
867 | } |
868 | |
869 | /* |
870 | * Called to query the current socket sendability status. |
871 | */ |
51470298 |
872 | static int telnet_sendbuffer(void *handle) |
5471d09a |
873 | { |
51470298 |
874 | Telnet telnet = (Telnet) handle; |
875 | return telnet->bufsize; |
374330e2 |
876 | } |
877 | |
878 | /* |
879 | * Called to set the size of the window from Telnet's POV. |
880 | */ |
51470298 |
881 | static void telnet_size(void *handle, int width, int height) |
32874aea |
882 | { |
51470298 |
883 | Telnet telnet = (Telnet) handle; |
ef41972c |
884 | unsigned char b[24]; |
885 | int n; |
57356d63 |
886 | char *logbuf; |
374330e2 |
887 | |
51470298 |
888 | telnet->term_width = width; |
889 | telnet->term_height = height; |
f278d6f8 |
890 | |
51470298 |
891 | if (telnet->s == NULL || telnet->opt_states[o_naws.index] != ACTIVE) |
374330e2 |
892 | return; |
ef41972c |
893 | n = 0; |
894 | b[n++] = IAC; |
895 | b[n++] = SB; |
896 | b[n++] = TELOPT_NAWS; |
897 | b[n++] = telnet->term_width >> 8; |
898 | if (b[n-1] == IAC) b[n++] = IAC; /* duplicate any IAC byte occurs */ |
899 | b[n++] = telnet->term_width & 0xFF; |
900 | if (b[n-1] == IAC) b[n++] = IAC; /* duplicate any IAC byte occurs */ |
901 | b[n++] = telnet->term_height >> 8; |
902 | if (b[n-1] == IAC) b[n++] = IAC; /* duplicate any IAC byte occurs */ |
903 | b[n++] = telnet->term_height & 0xFF; |
904 | if (b[n-1] == IAC) b[n++] = IAC; /* duplicate any IAC byte occurs */ |
905 | b[n++] = IAC; |
906 | b[n++] = SE; |
a1fd562d |
907 | telnet->bufsize = sk_write(telnet->s, (char *)b, n); |
57356d63 |
908 | logbuf = dupprintf("client:\tSB NAWS %d,%d", |
ef41972c |
909 | telnet->term_width, telnet->term_height); |
a8327734 |
910 | logevent(telnet->frontend, logbuf); |
57356d63 |
911 | sfree(logbuf); |
374330e2 |
912 | } |
913 | |
914 | /* |
915 | * Send Telnet special codes. |
916 | */ |
51470298 |
917 | static void telnet_special(void *handle, Telnet_Special code) |
32874aea |
918 | { |
51470298 |
919 | Telnet telnet = (Telnet) handle; |
374330e2 |
920 | unsigned char b[2]; |
921 | |
51470298 |
922 | if (telnet->s == NULL) |
374330e2 |
923 | return; |
924 | |
925 | b[0] = IAC; |
926 | switch (code) { |
32874aea |
927 | case TS_AYT: |
928 | b[1] = AYT; |
a1fd562d |
929 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); |
32874aea |
930 | break; |
931 | case TS_BRK: |
932 | b[1] = BREAK; |
a1fd562d |
933 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); |
32874aea |
934 | break; |
935 | case TS_EC: |
936 | b[1] = EC; |
a1fd562d |
937 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); |
32874aea |
938 | break; |
939 | case TS_EL: |
940 | b[1] = EL; |
a1fd562d |
941 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); |
32874aea |
942 | break; |
943 | case TS_GA: |
944 | b[1] = GA; |
a1fd562d |
945 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); |
32874aea |
946 | break; |
947 | case TS_NOP: |
948 | b[1] = NOP; |
a1fd562d |
949 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); |
32874aea |
950 | break; |
951 | case TS_ABORT: |
952 | b[1] = ABORT; |
a1fd562d |
953 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); |
32874aea |
954 | break; |
955 | case TS_AO: |
956 | b[1] = AO; |
a1fd562d |
957 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); |
32874aea |
958 | break; |
959 | case TS_IP: |
960 | b[1] = IP; |
a1fd562d |
961 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); |
32874aea |
962 | break; |
963 | case TS_SUSP: |
964 | b[1] = SUSP; |
a1fd562d |
965 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); |
32874aea |
966 | break; |
967 | case TS_EOR: |
968 | b[1] = EOR; |
a1fd562d |
969 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); |
32874aea |
970 | break; |
971 | case TS_EOF: |
972 | b[1] = xEOF; |
a1fd562d |
973 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); |
32874aea |
974 | break; |
a5f3e637 |
975 | case TS_EOL: |
6ee0cb6d |
976 | /* In BINARY mode, CR-LF becomes just CR - |
977 | * and without the NUL suffix too. */ |
656fa244 |
978 | if (telnet->opt_states[o_we_bin.index] == ACTIVE) |
6ee0cb6d |
979 | telnet->bufsize = sk_write(telnet->s, "\r", 1); |
656fa244 |
980 | else |
981 | telnet->bufsize = sk_write(telnet->s, "\r\n", 2); |
a5f3e637 |
982 | break; |
374330e2 |
983 | case TS_SYNCH: |
32874aea |
984 | b[1] = DM; |
a1fd562d |
985 | telnet->bufsize = sk_write(telnet->s, (char *)b, 1); |
986 | telnet->bufsize = sk_write_oob(telnet->s, (char *)(b + 1), 1); |
32874aea |
987 | break; |
684d367c |
988 | case TS_RECHO: |
51470298 |
989 | if (telnet->opt_states[o_echo.index] == INACTIVE || |
990 | telnet->opt_states[o_echo.index] == REALLY_INACTIVE) { |
991 | telnet->opt_states[o_echo.index] = REQUESTED; |
992 | send_opt(telnet, o_echo.send, o_echo.option); |
684d367c |
993 | } |
994 | break; |
995 | case TS_LECHO: |
51470298 |
996 | if (telnet->opt_states[o_echo.index] == ACTIVE) { |
997 | telnet->opt_states[o_echo.index] = REQUESTED; |
998 | send_opt(telnet, o_echo.nsend, o_echo.option); |
684d367c |
999 | } |
374330e2 |
1000 | break; |
32874aea |
1001 | case TS_PING: |
51470298 |
1002 | if (telnet->opt_states[o_they_sga.index] == ACTIVE) { |
32874aea |
1003 | b[1] = NOP; |
a1fd562d |
1004 | telnet->bufsize = sk_write(telnet->s, (char *)b, 2); |
ec55b220 |
1005 | } |
32874aea |
1006 | break; |
6f2d0cde |
1007 | default: |
1008 | break; /* never heard of it */ |
374330e2 |
1009 | } |
1010 | } |
1011 | |
125105d1 |
1012 | static const struct telnet_special *telnet_get_specials(void *handle) |
1013 | { |
1014 | static const struct telnet_special specials[] = { |
1015 | {"Are You There", TS_AYT}, |
1016 | {"Break", TS_BRK}, |
1017 | {"Synch", TS_SYNCH}, |
1018 | {"Erase Character", TS_EC}, |
1019 | {"Erase Line", TS_EL}, |
1020 | {"Go Ahead", TS_GA}, |
1021 | {"No Operation", TS_NOP}, |
6f2d0cde |
1022 | {NULL, TS_SEP}, |
125105d1 |
1023 | {"Abort Process", TS_ABORT}, |
1024 | {"Abort Output", TS_AO}, |
1025 | {"Interrupt Process", TS_IP}, |
1026 | {"Suspend Process", TS_SUSP}, |
6f2d0cde |
1027 | {NULL, TS_SEP}, |
125105d1 |
1028 | {"End Of Record", TS_EOR}, |
1029 | {"End Of File", TS_EOF}, |
6f2d0cde |
1030 | {NULL, TS_EXITMENU} |
125105d1 |
1031 | }; |
1032 | return specials; |
1033 | } |
1034 | |
6226c939 |
1035 | static int telnet_connected(void *handle) |
32874aea |
1036 | { |
51470298 |
1037 | Telnet telnet = (Telnet) handle; |
6226c939 |
1038 | return telnet->s != NULL; |
32874aea |
1039 | } |
8ccc75b0 |
1040 | |
51470298 |
1041 | static int telnet_sendok(void *handle) |
32874aea |
1042 | { |
68a49acb |
1043 | /* Telnet telnet = (Telnet) handle; */ |
32874aea |
1044 | return 1; |
1045 | } |
4017be6d |
1046 | |
51470298 |
1047 | static void telnet_unthrottle(void *handle, int backlog) |
5471d09a |
1048 | { |
51470298 |
1049 | Telnet telnet = (Telnet) handle; |
1050 | sk_set_frozen(telnet->s, backlog > TELNET_MAX_BACKLOG); |
5471d09a |
1051 | } |
1052 | |
51470298 |
1053 | static int telnet_ldisc(void *handle, int option) |
32874aea |
1054 | { |
51470298 |
1055 | Telnet telnet = (Telnet) handle; |
32874aea |
1056 | if (option == LD_ECHO) |
51470298 |
1057 | return telnet->echoing; |
32874aea |
1058 | if (option == LD_EDIT) |
51470298 |
1059 | return telnet->editing; |
0965bee0 |
1060 | return FALSE; |
1061 | } |
1062 | |
b9d7bcad |
1063 | static void telnet_provide_ldisc(void *handle, void *ldisc) |
1064 | { |
1065 | Telnet telnet = (Telnet) handle; |
1066 | telnet->ldisc = ldisc; |
1067 | } |
1068 | |
a8327734 |
1069 | static void telnet_provide_logctx(void *handle, void *logctx) |
1070 | { |
1071 | /* This is a stub. */ |
1072 | } |
1073 | |
51470298 |
1074 | static int telnet_exitcode(void *handle) |
d8d6c7e5 |
1075 | { |
0da1a790 |
1076 | Telnet telnet = (Telnet) handle; |
1077 | if (telnet->s != NULL) |
1078 | return -1; /* still connected */ |
1079 | else |
1080 | /* Telnet doesn't transmit exit codes back to the client */ |
1081 | return 0; |
d8d6c7e5 |
1082 | } |
1083 | |
f89c3294 |
1084 | /* |
1085 | * cfg_info for Telnet does nothing at all. |
1086 | */ |
1087 | static int telnet_cfg_info(void *handle) |
1088 | { |
1089 | return 0; |
1090 | } |
1091 | |
374330e2 |
1092 | Backend telnet_backend = { |
1093 | telnet_init, |
fabd1805 |
1094 | telnet_free, |
86916870 |
1095 | telnet_reconfig, |
374330e2 |
1096 | telnet_send, |
5471d09a |
1097 | telnet_sendbuffer, |
374330e2 |
1098 | telnet_size, |
4017be6d |
1099 | telnet_special, |
125105d1 |
1100 | telnet_get_specials, |
6226c939 |
1101 | telnet_connected, |
d8d6c7e5 |
1102 | telnet_exitcode, |
97db3be4 |
1103 | telnet_sendok, |
0965bee0 |
1104 | telnet_ldisc, |
b9d7bcad |
1105 | telnet_provide_ldisc, |
a8327734 |
1106 | telnet_provide_logctx, |
5471d09a |
1107 | telnet_unthrottle, |
f89c3294 |
1108 | telnet_cfg_info, |
9e164d82 |
1109 | "telnet", |
1110 | PROT_TELNET, |
97db3be4 |
1111 | 23 |
374330e2 |
1112 | }; |