master
1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24#include "curl_setup.h"
25#include "urldata.h"
26#include "telnet.h"
28#ifndef CURL_DISABLE_TELNET
30#ifdef HAVE_NETINET_IN_H
31#include <netinet/in.h>
32#endif
33#ifdef HAVE_NETDB_H
34#include <netdb.h>
35#endif
36#ifdef HAVE_ARPA_INET_H
37#include <arpa/inet.h>
38#endif
39#ifdef HAVE_NET_IF_H
40#include <net/if.h>
41#endif
42#ifdef HAVE_SYS_IOCTL_H
43#include <sys/ioctl.h>
44#endif
46#ifdef HAVE_SYS_PARAM_H
47#include <sys/param.h>
48#endif
50#include "url.h"
51#include "transfer.h"
52#include "sendf.h"
53#include "curl_trc.h"
54#include "progress.h"
55#include "arpa_telnet.h"
56#include "connect.h"
57#include "select.h"
58#include "curlx/strparse.h"
60#define SUBBUFSIZE 512
62#define CURL_SB_CLEAR(x) (x)->subpointer = (x)->subbuffer
63#define CURL_SB_TERM(x) \
64 do { \
65 (x)->subend = (x)->subpointer; \
66 CURL_SB_CLEAR(x); \
67 } while(0)
68#define CURL_SB_ACCUM(x, c) \
69 do { \
70 if((x)->subpointer < ((x)->subbuffer + sizeof((x)->subbuffer))) \
71 *(x)->subpointer++ = (c); \
72 } while(0)
74#define CURL_SB_GET(x) ((*(x)->subpointer++) & 0xff)
75#define CURL_SB_LEN(x) ((x)->subend - (x)->subpointer)
77/* For posterity:
78#define CURL_SB_PEEK(x) (*(x)->subpointer & 0xff)
79#define CURL_SB_EOF(x) ((x)->subpointer >= (x)->subend) */
81/* For negotiation compliant to RFC 1143 */
82#define CURL_NO 0
83#define CURL_YES 1
84#define CURL_WANTYES 2
85#define CURL_WANTNO 3
87#define CURL_EMPTY 0
88#define CURL_OPPOSITE 1
90/* meta key for storing protocol meta at easy handle */
91#define CURL_META_TELNET_EASY "meta:proto:telnet:easy"
93/*
94 * Telnet receiver states for fsm
95 */
96typedef enum {
97 CURL_TS_DATA = 0,
98 CURL_TS_IAC,
99 CURL_TS_WILL,
100 CURL_TS_WONT,
101 CURL_TS_DO,
102 CURL_TS_DONT,
103 CURL_TS_CR,
104 CURL_TS_SB, /* sub-option collection */
105 CURL_TS_SE /* looking for sub-option end */
106} TelnetReceive;
108struct TELNET {
109 int please_negotiate;
110 int already_negotiated;
111 int us[256];
112 int usq[256];
113 int us_preferred[256];
114 int him[256];
115 int himq[256];
116 int him_preferred[256];
117 int subnegotiation[256];
118 const char *subopt_ttype; /* Set with suboption TTYPE */
119 const char *subopt_xdisploc; /* Set with suboption XDISPLOC */
120 unsigned short subopt_wsx; /* Set with suboption NAWS */
121 unsigned short subopt_wsy; /* Set with suboption NAWS */
122 TelnetReceive telrcv_state;
123 struct curl_slist *telnet_vars; /* Environment variables */
124 struct dynbuf out; /* output buffer */
126 /* suboptions */
127 unsigned char subbuffer[SUBBUFSIZE];
128 unsigned char *subpointer, *subend; /* buffer for sub-options */
129};
131#ifndef CURLVERBOSE
132#define printoption(a, b, c, d) Curl_nop_stmt
133#else
134static void printoption(struct Curl_easy *data,
135 const char *direction, int cmd, int option)
136{
137 if(data->set.verbose) {
138 if(cmd == CURL_IAC) {
139 if(CURL_TELCMD_OK(option))
140 infof(data, "%s IAC %s", direction, CURL_TELCMD(option));
141 else
142 infof(data, "%s IAC %d", direction, option);
143 }
144 else {
145 const char *fmt = (cmd == CURL_WILL) ? "WILL" :
146 (cmd == CURL_WONT) ? "WONT" :
147 (cmd == CURL_DO) ? "DO" :
148 (cmd == CURL_DONT) ? "DONT" : 0;
149 if(fmt) {
150 const char *opt;
151 if(CURL_TELOPT_OK(option))
152 opt = CURL_TELOPT(option);
153 else if(option == CURL_TELOPT_EXOPL)
154 opt = "EXOPL";
155 else
156 opt = NULL;
158 if(opt)
159 infof(data, "%s %s %s", direction, fmt, opt);
160 else
161 infof(data, "%s %s %d", direction, fmt, option);
162 }
163 else
164 infof(data, "%s %d %d", direction, cmd, option);
165 }
166 }
167}
168#endif /* !CURLVERBOSE */
170static void telnet_easy_dtor(const void *key, size_t klen, void *entry)
171{
172 struct TELNET *tn = entry;
173 (void)key;
174 (void)klen;
175 curl_slist_free_all(tn->telnet_vars);
176 curlx_dyn_free(&tn->out);
177 curlx_free(tn);
178}
180static CURLcode init_telnet(struct Curl_easy *data)
181{
182 struct TELNET *tn;
184 tn = curlx_calloc(1, sizeof(struct TELNET));
185 if(!tn)
186 return CURLE_OUT_OF_MEMORY;
188 curlx_dyn_init(&tn->out, 0xffff);
190 tn->telrcv_state = CURL_TS_DATA;
192 /* Init suboptions */
193 CURL_SB_CLEAR(tn);
195 /* Set the options we want by default */
196 tn->us_preferred[CURL_TELOPT_SGA] = CURL_YES;
197 tn->him_preferred[CURL_TELOPT_SGA] = CURL_YES;
199 /* To be compliant with previous releases of libcurl we enable this option
200 by default. This behavior can be changed with the "BINARY" option in
201 CURLOPT_TELNETOPTIONS */
202 tn->us_preferred[CURL_TELOPT_BINARY] = CURL_YES;
203 tn->him_preferred[CURL_TELOPT_BINARY] = CURL_YES;
205 /* We must allow the server to echo what we sent but it is not necessary
206 to request the server to do so (it might force the server to close
207 the connection). Hence, we ignore ECHO in the negotiate function */
208 tn->him_preferred[CURL_TELOPT_ECHO] = CURL_YES;
210 /* Set the subnegotiation fields to send information after negotiation
211 passed (do/will)
213 Default values are (0,0) initialized by calloc.
214 According to the RFC1013 it is valid:
215 A value equal to zero is acceptable for the width (or height), and means
216 that no character width (or height) is being sent. In this case, the width
217 (or height) that will be assumed by the Telnet server is operating system
218 specific (it will probably be based upon the terminal type information
219 that may have been sent using the TERMINAL TYPE Telnet option). */
220 tn->subnegotiation[CURL_TELOPT_NAWS] = CURL_YES;
222 return Curl_meta_set(data, CURL_META_TELNET_EASY, tn, telnet_easy_dtor);
223}
225static void send_negotiation(struct Curl_easy *data, int cmd, int option)
226{
227 unsigned char buf[3];
228 size_t nwritten;
229 CURLcode result;
231 buf[0] = CURL_IAC;
232 buf[1] = (unsigned char)cmd;
233 buf[2] = (unsigned char)option;
235 result = Curl_xfer_send(data, buf, 3, FALSE, &nwritten);
236 if(result)
237 failf(data, "Sending data failed: %s", curl_easy_strerror(result));
239 printoption(data, "SENT", cmd, option);
240}
242static void set_remote_option(struct Curl_easy *data, struct TELNET *tn,
243 int option, int newstate)
244{
245 if(newstate == CURL_YES) {
246 switch(tn->him[option]) {
247 case CURL_NO:
248 tn->him[option] = CURL_WANTYES;
249 send_negotiation(data, CURL_DO, option);
250 break;
252 case CURL_YES:
253 /* Already enabled */
254 break;
256 case CURL_WANTNO:
257 switch(tn->himq[option]) {
258 case CURL_EMPTY:
259 /* Already negotiating for CURL_YES, queue the request */
260 tn->himq[option] = CURL_OPPOSITE;
261 break;
262 case CURL_OPPOSITE:
263 /* Error: already queued an enable request */
264 break;
265 }
266 break;
268 case CURL_WANTYES:
269 switch(tn->himq[option]) {
270 case CURL_EMPTY:
271 /* Error: already negotiating for enable */
272 break;
273 case CURL_OPPOSITE:
274 tn->himq[option] = CURL_EMPTY;
275 break;
276 }
277 break;
278 }
279 }
280 else { /* NO */
281 switch(tn->him[option]) {
282 case CURL_NO:
283 /* Already disabled */
284 break;
286 case CURL_YES:
287 tn->him[option] = CURL_WANTNO;
288 send_negotiation(data, CURL_DONT, option);
289 break;
291 case CURL_WANTNO:
292 switch(tn->himq[option]) {
293 case CURL_EMPTY:
294 /* Already negotiating for NO */
295 break;
296 case CURL_OPPOSITE:
297 tn->himq[option] = CURL_EMPTY;
298 break;
299 }
300 break;
302 case CURL_WANTYES:
303 switch(tn->himq[option]) {
304 case CURL_EMPTY:
305 tn->himq[option] = CURL_OPPOSITE;
306 break;
307 case CURL_OPPOSITE:
308 break;
309 }
310 break;
311 }
312 }
313}
315static void set_local_option(struct Curl_easy *data, struct TELNET *tn,
316 int option, int newstate)
317{
318 if(newstate == CURL_YES) {
319 switch(tn->us[option]) {
320 case CURL_NO:
321 tn->us[option] = CURL_WANTYES;
322 send_negotiation(data, CURL_WILL, option);
323 break;
325 case CURL_YES:
326 /* Already enabled */
327 break;
329 case CURL_WANTNO:
330 switch(tn->usq[option]) {
331 case CURL_EMPTY:
332 /* Already negotiating for CURL_YES, queue the request */
333 tn->usq[option] = CURL_OPPOSITE;
334 break;
335 case CURL_OPPOSITE:
336 /* Error: already queued an enable request */
337 break;
338 }
339 break;
341 case CURL_WANTYES:
342 switch(tn->usq[option]) {
343 case CURL_EMPTY:
344 /* Error: already negotiating for enable */
345 break;
346 case CURL_OPPOSITE:
347 tn->usq[option] = CURL_EMPTY;
348 break;
349 }
350 break;
351 }
352 }
353 else { /* NO */
354 switch(tn->us[option]) {
355 case CURL_NO:
356 /* Already disabled */
357 break;
359 case CURL_YES:
360 tn->us[option] = CURL_WANTNO;
361 send_negotiation(data, CURL_WONT, option);
362 break;
364 case CURL_WANTNO:
365 switch(tn->usq[option]) {
366 case CURL_EMPTY:
367 /* Already negotiating for NO */
368 break;
369 case CURL_OPPOSITE:
370 tn->usq[option] = CURL_EMPTY;
371 break;
372 }
373 break;
375 case CURL_WANTYES:
376 switch(tn->usq[option]) {
377 case CURL_EMPTY:
378 tn->usq[option] = CURL_OPPOSITE;
379 break;
380 case CURL_OPPOSITE:
381 break;
382 }
383 break;
384 }
385 }
386}
388static void telnet_negotiate(struct Curl_easy *data, struct TELNET *tn)
389{
390 int i;
392 for(i = 0; i < CURL_NTELOPTS; i++) {
393 if(i == CURL_TELOPT_ECHO)
394 continue;
396 if(tn->us_preferred[i] == CURL_YES)
397 set_local_option(data, tn, i, CURL_YES);
399 if(tn->him_preferred[i] == CURL_YES)
400 set_remote_option(data, tn, i, CURL_YES);
401 }
402}
404static void rec_will(struct Curl_easy *data, struct TELNET *tn, int option)
405{
406 switch(tn->him[option]) {
407 case CURL_NO:
408 if(tn->him_preferred[option] == CURL_YES) {
409 tn->him[option] = CURL_YES;
410 send_negotiation(data, CURL_DO, option);
411 }
412 else
413 send_negotiation(data, CURL_DONT, option);
415 break;
417 case CURL_YES:
418 /* Already enabled */
419 break;
421 case CURL_WANTNO:
422 switch(tn->himq[option]) {
423 case CURL_EMPTY:
424 /* Error: DONT answered by WILL */
425 tn->him[option] = CURL_NO;
426 break;
427 case CURL_OPPOSITE:
428 /* Error: DONT answered by WILL */
429 tn->him[option] = CURL_YES;
430 tn->himq[option] = CURL_EMPTY;
431 break;
432 }
433 break;
435 case CURL_WANTYES:
436 switch(tn->himq[option]) {
437 case CURL_EMPTY:
438 tn->him[option] = CURL_YES;
439 break;
440 case CURL_OPPOSITE:
441 tn->him[option] = CURL_WANTNO;
442 tn->himq[option] = CURL_EMPTY;
443 send_negotiation(data, CURL_DONT, option);
444 break;
445 }
446 break;
447 }
448}
450static void rec_wont(struct Curl_easy *data, struct TELNET *tn, int option)
451{
452 switch(tn->him[option]) {
453 case CURL_NO:
454 /* Already disabled */
455 break;
457 case CURL_YES:
458 tn->him[option] = CURL_NO;
459 send_negotiation(data, CURL_DONT, option);
460 break;
462 case CURL_WANTNO:
463 switch(tn->himq[option]) {
464 case CURL_EMPTY:
465 tn->him[option] = CURL_NO;
466 break;
468 case CURL_OPPOSITE:
469 tn->him[option] = CURL_WANTYES;
470 tn->himq[option] = CURL_EMPTY;
471 send_negotiation(data, CURL_DO, option);
472 break;
473 }
474 break;
476 case CURL_WANTYES:
477 switch(tn->himq[option]) {
478 case CURL_EMPTY:
479 tn->him[option] = CURL_NO;
480 break;
481 case CURL_OPPOSITE:
482 tn->him[option] = CURL_NO;
483 tn->himq[option] = CURL_EMPTY;
484 break;
485 }
486 break;
487 }
488}
490static void printsub(struct Curl_easy *data,
491 int direction, /* '<' or '>' */
492 const unsigned char *pointer, /* ptr to suboption data */
493 size_t length) /* suboption data length */
494{
495 if(data->set.verbose) {
496 unsigned int i = 0;
497 if(direction) {
498 infof(data, "%s IAC SB ", (direction == '<') ? "RCVD" : "SENT");
499 if(length >= 3) {
500 int j;
502 i = pointer[length - 2];
503 j = pointer[length - 1];
505 if(i != CURL_IAC || j != CURL_SE) {
506 infof(data, "(terminated by ");
507 if(CURL_TELOPT_OK(i))
508 infof(data, "%s ", CURL_TELOPT(i));
509 else if(CURL_TELCMD_OK(i))
510 infof(data, "%s ", CURL_TELCMD(i));
511 else
512 infof(data, "%u ", i);
513 if(CURL_TELOPT_OK(j))
514 infof(data, "%s", CURL_TELOPT(j));
515 else if(CURL_TELCMD_OK(j))
516 infof(data, "%s", CURL_TELCMD(j));
517 else
518 infof(data, "%d", j);
519 infof(data, ", not IAC SE) ");
520 }
521 }
522 if(length >= 2)
523 length -= 2;
524 else /* bad input */
525 return;
526 }
527 if(length <= 1) {
528 infof(data, "(Empty suboption?)");
529 return;
530 }
532 if(CURL_TELOPT_OK(pointer[0])) {
533 switch(pointer[0]) {
534 case CURL_TELOPT_TTYPE:
535 case CURL_TELOPT_XDISPLOC:
536 case CURL_TELOPT_NEW_ENVIRON:
537 case CURL_TELOPT_NAWS:
538 infof(data, "%s", CURL_TELOPT(pointer[0]));
539 break;
540 default:
541 infof(data, "%s (unsupported)", CURL_TELOPT(pointer[0]));
542 break;
543 }
544 }
545 else
546 infof(data, "%d (unknown)", pointer[0]);
548 switch(pointer[0]) {
549 case CURL_TELOPT_NAWS:
550 if(length > 4)
551 infof(data, "Width: %d ; Height: %d", (pointer[1] << 8) | pointer[2],
552 (pointer[3] << 8) | pointer[4]);
553 break;
554 default:
555 switch(pointer[1]) {
556 case CURL_TELQUAL_IS:
557 infof(data, " IS");
558 break;
559 case CURL_TELQUAL_SEND:
560 infof(data, " SEND");
561 break;
562 case CURL_TELQUAL_INFO:
563 infof(data, " INFO/REPLY");
564 break;
565 case CURL_TELQUAL_NAME:
566 infof(data, " NAME");
567 break;
568 }
570 switch(pointer[0]) {
571 case CURL_TELOPT_TTYPE:
572 case CURL_TELOPT_XDISPLOC:
573 infof(data, " \"%.*s\"",
574 (int)((length > 2) ? (length - 2) : 0), &pointer[2]);
575 break;
576 case CURL_TELOPT_NEW_ENVIRON:
577 if(pointer[1] == CURL_TELQUAL_IS) {
578 infof(data, " ");
579 for(i = 3; i < length; i++) {
580 switch(pointer[i]) {
581 case CURL_NEW_ENV_VAR:
582 infof(data, ", ");
583 break;
584 case CURL_NEW_ENV_VALUE:
585 infof(data, " = ");
586 break;
587 default:
588 infof(data, "%c", pointer[i]);
589 break;
590 }
591 }
592 }
593 break;
594 default:
595 for(i = 2; i < length; i++)
596 infof(data, " %.2x", pointer[i]);
597 break;
598 }
599 }
600 }
601}
603/* Escape and send a telnet data block */
604static CURLcode send_telnet_data(struct Curl_easy *data,
605 struct TELNET *tn,
606 const char *buffer, ssize_t nread)
607{
608 size_t i, outlen;
609 const unsigned char *outbuf;
610 CURLcode result = CURLE_OK;
611 size_t bytes_written;
612 size_t total_written = 0;
613 struct connectdata *conn = data->conn;
615 DEBUGASSERT(tn);
616 DEBUGASSERT(nread > 0);
617 if(nread < 0)
618 return CURLE_TOO_LARGE;
620 if(memchr(buffer, CURL_IAC, nread)) {
621 /* only use the escape buffer when necessary */
622 curlx_dyn_reset(&tn->out);
624 for(i = 0; i < (size_t)nread && !result; i++) {
625 result = curlx_dyn_addn(&tn->out, &buffer[i], 1);
626 if(!result && ((unsigned char)buffer[i] == CURL_IAC))
627 /* IAC is FF in hex */
628 result = curlx_dyn_addn(&tn->out, "\xff", 1);
629 }
631 outlen = curlx_dyn_len(&tn->out);
632 outbuf = curlx_dyn_uptr(&tn->out);
633 }
634 else {
635 outlen = (size_t)nread;
636 outbuf = (const unsigned char *)buffer;
637 }
638 while(!result && total_written < outlen) {
639 /* Make sure socket is writable to avoid EWOULDBLOCK condition */
640 struct pollfd pfd[1];
641 timediff_t timeout_ms = Curl_timeleft_ms(data);
642 pfd[0].fd = conn->sock[FIRSTSOCKET];
643 pfd[0].events = POLLOUT;
644 if(timeout_ms < 0)
645 return CURLE_OPERATION_TIMEDOUT;
646 /* 0 means no timeout configured; pass -1 to poll for infinite wait */
647 switch(Curl_poll(pfd, 1, timeout_ms ? timeout_ms : -1)) {
648 case -1: /* error, abort writing */
649 result = CURLE_SEND_ERROR;
650 break;
651 case 0: /* timeout */
652 result = CURLE_OPERATION_TIMEDOUT;
653 break;
654 default: /* write! */
655 bytes_written = 0;
656 result = Curl_xfer_send(data, outbuf + total_written,
657 outlen - total_written, FALSE, &bytes_written);
658 total_written += bytes_written;
659 break;
660 }
661 }
663 return result;
664}
666/*
667 * sendsuboption()
668 *
669 * Send suboption information to the server side.
670 */
671static void sendsuboption(struct Curl_easy *data,
672 struct TELNET *tn, int option)
673{
674 size_t nwritten;
675 unsigned short x, y;
676 const unsigned char *uc1, *uc2;
677 CURLcode result;
679 switch(option) {
680 case CURL_TELOPT_NAWS:
681 /* We prepare data to be sent */
682 CURL_SB_CLEAR(tn);
683 CURL_SB_ACCUM(tn, CURL_IAC);
684 CURL_SB_ACCUM(tn, CURL_SB);
685 CURL_SB_ACCUM(tn, CURL_TELOPT_NAWS);
686 /* We must deal either with little or big endian processors */
687 /* Window size must be sent according to the 'network order' */
688 x = htons(tn->subopt_wsx);
689 y = htons(tn->subopt_wsy);
690 uc1 = (const unsigned char *)&x;
691 uc2 = (const unsigned char *)&y;
692 CURL_SB_ACCUM(tn, uc1[0]);
693 CURL_SB_ACCUM(tn, uc1[1]);
694 CURL_SB_ACCUM(tn, uc2[0]);
695 CURL_SB_ACCUM(tn, uc2[1]);
697 CURL_SB_ACCUM(tn, CURL_IAC);
698 CURL_SB_ACCUM(tn, CURL_SE);
699 CURL_SB_TERM(tn);
700 /* data suboption is now ready */
702 printsub(data, '>', (const unsigned char *)tn->subbuffer + 2,
703 CURL_SB_LEN(tn) - 2);
705 /* we send the header of the suboption... */
706 result = Curl_xfer_send(data, tn->subbuffer, 3, FALSE, &nwritten);
707 if(result)
708 failf(data, "Sending data failed: %s", curl_easy_strerror(result));
709 /* ... then the window size with the send_telnet_data() function
710 to deal with 0xFF cases ... */
711 send_telnet_data(data, tn, (const char *)tn->subbuffer + 3, 4);
712 /* ... and the footer */
713 result = Curl_xfer_send(data, tn->subbuffer + 7, 2, FALSE, &nwritten);
714 if(result)
715 failf(data, "Sending data failed: %s", curl_easy_strerror(result));
716 break;
717 }
718}
720static void rec_do(struct Curl_easy *data, struct TELNET *tn, int option)
721{
722 switch(tn->us[option]) {
723 case CURL_NO:
724 if(tn->us_preferred[option] == CURL_YES) {
725 tn->us[option] = CURL_YES;
726 send_negotiation(data, CURL_WILL, option);
727 if(tn->subnegotiation[option] == CURL_YES)
728 /* transmission of data option */
729 sendsuboption(data, tn, option);
730 }
731 else if(tn->subnegotiation[option] == CURL_YES) {
732 /* send information to achieve this option */
733 tn->us[option] = CURL_YES;
734 send_negotiation(data, CURL_WILL, option);
735 sendsuboption(data, tn, option);
736 }
737 else
738 send_negotiation(data, CURL_WONT, option);
739 break;
741 case CURL_YES:
742 /* Already enabled */
743 break;
745 case CURL_WANTNO:
746 switch(tn->usq[option]) {
747 case CURL_EMPTY:
748 /* Error: DONT answered by WILL */
749 tn->us[option] = CURL_NO;
750 break;
751 case CURL_OPPOSITE:
752 /* Error: DONT answered by WILL */
753 tn->us[option] = CURL_YES;
754 tn->usq[option] = CURL_EMPTY;
755 break;
756 }
757 break;
759 case CURL_WANTYES:
760 switch(tn->usq[option]) {
761 case CURL_EMPTY:
762 tn->us[option] = CURL_YES;
763 if(tn->subnegotiation[option] == CURL_YES) {
764 /* transmission of data option */
765 sendsuboption(data, tn, option);
766 }
767 break;
768 case CURL_OPPOSITE:
769 tn->us[option] = CURL_WANTNO;
770 tn->usq[option] = CURL_EMPTY;
771 send_negotiation(data, CURL_WONT, option);
772 break;
773 }
774 break;
775 }
776}
778static void rec_dont(struct Curl_easy *data, struct TELNET *tn, int option)
779{
780 switch(tn->us[option]) {
781 case CURL_NO:
782 /* Already disabled */
783 break;
785 case CURL_YES:
786 tn->us[option] = CURL_NO;
787 send_negotiation(data, CURL_WONT, option);
788 break;
790 case CURL_WANTNO:
791 switch(tn->usq[option]) {
792 case CURL_EMPTY:
793 tn->us[option] = CURL_NO;
794 break;
796 case CURL_OPPOSITE:
797 tn->us[option] = CURL_WANTYES;
798 tn->usq[option] = CURL_EMPTY;
799 send_negotiation(data, CURL_WILL, option);
800 break;
801 }
802 break;
804 case CURL_WANTYES:
805 switch(tn->usq[option]) {
806 case CURL_EMPTY:
807 tn->us[option] = CURL_NO;
808 break;
809 case CURL_OPPOSITE:
810 tn->us[option] = CURL_NO;
811 tn->usq[option] = CURL_EMPTY;
812 break;
813 }
814 break;
815 }
816}
818static bool str_is_nonascii(const char *str)
819{
820 char c;
821 while((c = *str++) != 0)
822 if(c & 0x80)
823 return TRUE;
825 return FALSE;
826}
828static CURLcode check_telnet_options(struct Curl_easy *data,
829 struct TELNET *tn)
830{
831 struct curl_slist *head;
832 struct curl_slist *beg;
833 CURLcode result = CURLE_OK;
835 /* Add the username as an environment variable if it
836 was given on the command line */
837 if(data->state.creds) {
838 char buffer[256];
839 if(str_is_nonascii(Curl_creds_user(data->conn->creds))) {
840 DEBUGF(infof(data, "set a non ASCII username in telnet"));
841 return CURLE_BAD_FUNCTION_ARGUMENT;
842 }
843 curl_msnprintf(buffer, sizeof(buffer), "USER,%s",
844 Curl_creds_user(data->conn->creds));
845 beg = curl_slist_append(tn->telnet_vars, buffer);
846 if(!beg) {
847 curl_slist_free_all(tn->telnet_vars);
848 tn->telnet_vars = NULL;
849 return CURLE_OUT_OF_MEMORY;
850 }
851 tn->telnet_vars = beg;
852 tn->us_preferred[CURL_TELOPT_NEW_ENVIRON] = CURL_YES;
853 }
855 for(head = data->set.telnet_options; head && !result; head = head->next) {
856 size_t olen;
857 const char *option = head->data;
858 const char *arg;
859 const char *sep = strchr(option, '=');
860 if(sep) {
861 olen = sep - option;
862 arg = ++sep;
863 if(str_is_nonascii(arg))
864 continue;
865 switch(olen) {
866 case 5:
867 /* Terminal type */
868 if(curl_strnequal(option, "TTYPE", 5)) {
869 tn->subopt_ttype = arg;
870 tn->us_preferred[CURL_TELOPT_TTYPE] = CURL_YES;
871 break;
872 }
873 result = CURLE_UNKNOWN_OPTION;
874 break;
876 case 8:
877 /* Display variable */
878 if(curl_strnequal(option, "XDISPLOC", 8)) {
879 tn->subopt_xdisploc = arg;
880 tn->us_preferred[CURL_TELOPT_XDISPLOC] = CURL_YES;
881 break;
882 }
883 result = CURLE_UNKNOWN_OPTION;
884 break;
886 case 7:
887 /* Environment variable */
888 if(curl_strnequal(option, "NEW_ENV", 7)) {
889 beg = curl_slist_append(tn->telnet_vars, arg);
890 if(!beg) {
891 result = CURLE_OUT_OF_MEMORY;
892 break;
893 }
894 tn->telnet_vars = beg;
895 tn->us_preferred[CURL_TELOPT_NEW_ENVIRON] = CURL_YES;
896 }
897 else
898 result = CURLE_UNKNOWN_OPTION;
899 break;
901 case 2:
902 /* Window Size */
903 if(curl_strnequal(option, "WS", 2)) {
904 const char *p = arg;
905 curl_off_t x = 0;
906 curl_off_t y = 0;
907 if(curlx_str_number(&p, &x, 0xffff) ||
908 curlx_str_single(&p, 'x') ||
909 curlx_str_number(&p, &y, 0xffff)) {
910 failf(data, "Syntax error in telnet option: %s", head->data);
911 result = CURLE_SETOPT_OPTION_SYNTAX;
912 }
913 else {
914 tn->subopt_wsx = (unsigned short)x;
915 tn->subopt_wsy = (unsigned short)y;
916 tn->us_preferred[CURL_TELOPT_NAWS] = CURL_YES;
917 }
918 }
919 else
920 result = CURLE_UNKNOWN_OPTION;
921 break;
923 case 6:
924 /* To take care or not of the 8th bit in data exchange */
925 if(curl_strnequal(option, "BINARY", 6)) {
926 const char *p = arg;
927 curl_off_t binary_option;
928 if(!curlx_str_number(&p, &binary_option, 1) &&
929 (binary_option != 1)) {
930 tn->us_preferred[CURL_TELOPT_BINARY] = CURL_NO;
931 tn->him_preferred[CURL_TELOPT_BINARY] = CURL_NO;
932 }
933 }
934 else
935 result = CURLE_UNKNOWN_OPTION;
936 break;
937 default:
938 failf(data, "Unknown telnet option %s", head->data);
939 result = CURLE_UNKNOWN_OPTION;
940 break;
941 }
942 }
943 else {
944 failf(data, "Syntax error in telnet option: %s", head->data);
945 result = CURLE_SETOPT_OPTION_SYNTAX;
946 }
947 }
949 if(result) {
950 curl_slist_free_all(tn->telnet_vars);
951 tn->telnet_vars = NULL;
952 }
954 return result;
955}
957/* if the option contains an IAC code, it should be escaped in the output, but
958 as we cannot think of any legit way to send that as part of the content we
959 rather ban its use instead */
960static bool bad_option(const char *data)
961{
962 return !data || !!strchr(data, CURL_IAC);
963}
965/*
966 * suboption()
967 *
968 * Look at the sub-option buffer, and try to be helpful to the other
969 * side.
970 */
971static CURLcode suboption(struct Curl_easy *data, struct TELNET *tn)
972{
973 struct curl_slist *v;
974 unsigned char temp[2048];
975 size_t nwritten;
976 size_t len;
977 CURLcode result = CURLE_OK;
979 if(!CURL_SB_LEN(tn)) /* ignore empty suboption */
980 return CURLE_OK;
982 printsub(data, '<', (const unsigned char *)tn->subbuffer,
983 CURL_SB_LEN(tn) + 2);
984 switch(CURL_SB_GET(tn)) {
985 case CURL_TELOPT_TTYPE:
986 if(bad_option(tn->subopt_ttype))
987 return CURLE_BAD_FUNCTION_ARGUMENT;
988 if(strlen(tn->subopt_ttype) > 1000) {
989 failf(data, "Too long telnet TTYPE");
990 return CURLE_SEND_ERROR;
991 }
992 len = curl_msnprintf((char *)temp, sizeof(temp), "%c%c%c%c%s%c%c",
993 CURL_IAC, CURL_SB, CURL_TELOPT_TTYPE,
994 CURL_TELQUAL_IS, tn->subopt_ttype, CURL_IAC,
995 CURL_SE);
996 result = Curl_xfer_send(data, temp, len, FALSE, &nwritten);
997 if(result) {
998 failf(data, "Sending data failed: %s", curl_easy_strerror(result));
999 return result;
1000 }
1001 printsub(data, '>', &temp[2], len-2);
1002 break;
1003 case CURL_TELOPT_XDISPLOC:
1004 if(bad_option(tn->subopt_xdisploc))
1005 return CURLE_BAD_FUNCTION_ARGUMENT;
1006 if(strlen(tn->subopt_xdisploc) > 1000) {
1007 failf(data, "Too long telnet XDISPLOC");
1008 return CURLE_SEND_ERROR;
1009 }
1010 len = curl_msnprintf((char *)temp, sizeof(temp), "%c%c%c%c%s%c%c",
1011 CURL_IAC, CURL_SB, CURL_TELOPT_XDISPLOC,
1012 CURL_TELQUAL_IS, tn->subopt_xdisploc, CURL_IAC,
1013 CURL_SE);
1014 result = Curl_xfer_send(data, temp, len, FALSE, &nwritten);
1015 if(result) {
1016 failf(data, "Sending data failed: %s", curl_easy_strerror(result));
1017 return result;
1018 }
1019 printsub(data, '>', &temp[2], len - 2);
1020 break;
1021 case CURL_TELOPT_NEW_ENVIRON:
1022 len = curl_msnprintf((char *)temp, sizeof(temp), "%c%c%c%c",
1023 CURL_IAC, CURL_SB, CURL_TELOPT_NEW_ENVIRON,
1024 CURL_TELQUAL_IS);
1025 for(v = tn->telnet_vars; v; v = v->next) {
1026 size_t tmplen = (strlen(v->data) + 1);
1027 if(bad_option(v->data))
1028 return CURLE_BAD_FUNCTION_ARGUMENT;
1029 /* Add the variable if it fits */
1030 if(len + tmplen < sizeof(temp) - 6) {
1031 const char *s = strchr(v->data, ',');
1032 if(!s)
1033 len += curl_msnprintf((char *)&temp[len], sizeof(temp) - len,
1034 "%c%s", CURL_NEW_ENV_VAR, v->data);
1035 else {
1036 size_t vlen = s - v->data;
1037 len += curl_msnprintf((char *)&temp[len], sizeof(temp) - len,
1038 "%c%.*s%c%s", CURL_NEW_ENV_VAR,
1039 (int)vlen, v->data, CURL_NEW_ENV_VALUE, ++s);
1040 }
1041 }
1042 }
1043 curl_msnprintf((char *)&temp[len], sizeof(temp) - len,
1044 "%c%c", CURL_IAC, CURL_SE);
1045 len += 2;
1046 result = Curl_xfer_send(data, temp, len, FALSE, &nwritten);
1047 if(result) {
1048 failf(data, "Sending data failed: %s", curl_easy_strerror(result));
1049 return result;
1050 }
1051 printsub(data, '>', &temp[2], len - 2);
1052 break;
1053 }
1054 return CURLE_OK;
1055}
1057static CURLcode telrcv(struct Curl_easy *data,
1058 struct TELNET *tn,
1059 const unsigned char *inbuf, /* Data received from
1060 socket */
1061 ssize_t count) /* Number of bytes
1062 received */
1063{
1064 unsigned char c;
1065 CURLcode result;
1066 int in = 0;
1067 int startwrite = -1;
1069#define startskipping() \
1070 if(startwrite >= 0) { \
1071 result = Curl_client_write(data, \
1072 CLIENTWRITE_BODY, \
1073 (const char *)&inbuf[startwrite], \
1074 in-startwrite); \
1075 if(result) \
1076 return result; \
1077 } \
1078 startwrite = -1
1080#define writebyte() \
1081 if(startwrite < 0) \
1082 startwrite = in
1084#define bufferflush() startskipping()
1086 while(count--) {
1087 c = inbuf[in];
1089 switch(tn->telrcv_state) {
1090 case CURL_TS_CR:
1091 tn->telrcv_state = CURL_TS_DATA;
1092 if(c == '\0') {
1093 startskipping();
1094 break; /* Ignore \0 after CR */
1095 }
1096 writebyte();
1097 break;
1099 case CURL_TS_DATA:
1100 if(c == CURL_IAC) {
1101 tn->telrcv_state = CURL_TS_IAC;
1102 startskipping();
1103 break;
1104 }
1105 else if(c == '\r')
1106 tn->telrcv_state = CURL_TS_CR;
1107 writebyte();
1108 break;
1110 case CURL_TS_IAC:
1111 DEBUGASSERT(startwrite < 0);
1112 switch(c) {
1113 case CURL_WILL:
1114 tn->telrcv_state = CURL_TS_WILL;
1115 break;
1116 case CURL_WONT:
1117 tn->telrcv_state = CURL_TS_WONT;
1118 break;
1119 case CURL_DO:
1120 tn->telrcv_state = CURL_TS_DO;
1121 break;
1122 case CURL_DONT:
1123 tn->telrcv_state = CURL_TS_DONT;
1124 break;
1125 case CURL_SB:
1126 CURL_SB_CLEAR(tn);
1127 tn->telrcv_state = CURL_TS_SB;
1128 break;
1129 case CURL_IAC:
1130 tn->telrcv_state = CURL_TS_DATA;
1131 writebyte();
1132 break;
1133 case CURL_DM:
1134 case CURL_NOP:
1135 case CURL_GA:
1136 default:
1137 tn->telrcv_state = CURL_TS_DATA;
1138 printoption(data, "RCVD", CURL_IAC, c);
1139 break;
1140 }
1141 break;
1143 case CURL_TS_WILL:
1144 printoption(data, "RCVD", CURL_WILL, c);
1145 tn->please_negotiate = 1;
1146 rec_will(data, tn, c);
1147 tn->telrcv_state = CURL_TS_DATA;
1148 break;
1150 case CURL_TS_WONT:
1151 printoption(data, "RCVD", CURL_WONT, c);
1152 tn->please_negotiate = 1;
1153 rec_wont(data, tn, c);
1154 tn->telrcv_state = CURL_TS_DATA;
1155 break;
1157 case CURL_TS_DO:
1158 printoption(data, "RCVD", CURL_DO, c);
1159 tn->please_negotiate = 1;
1160 rec_do(data, tn, c);
1161 tn->telrcv_state = CURL_TS_DATA;
1162 break;
1164 case CURL_TS_DONT:
1165 printoption(data, "RCVD", CURL_DONT, c);
1166 tn->please_negotiate = 1;
1167 rec_dont(data, tn, c);
1168 tn->telrcv_state = CURL_TS_DATA;
1169 break;
1171 case CURL_TS_SB:
1172 if(c == CURL_IAC)
1173 tn->telrcv_state = CURL_TS_SE;
1174 else
1175 CURL_SB_ACCUM(tn, c);
1176 break;
1178 case CURL_TS_SE:
1179 if(c != CURL_SE) {
1180 if(c != CURL_IAC) {
1181 /*
1182 * This is an error. We only expect to get "IAC IAC" or "IAC SE".
1183 * Several things may have happened. An IAC was not doubled, the IAC
1184 * SE was left off, or another option got inserted into the
1185 * suboption are all possibilities.
1186 */
1187 failf(data, "telnet: suboption error");
1188 return CURLE_RECV_ERROR;
1189 }
1190 CURL_SB_ACCUM(tn, c);
1191 tn->telrcv_state = CURL_TS_SB;
1192 }
1193 else {
1194 CURL_SB_ACCUM(tn, CURL_IAC);
1195 CURL_SB_ACCUM(tn, CURL_SE);
1196 tn->subpointer -= 2;
1197 CURL_SB_TERM(tn);
1198 result = suboption(data, tn); /* handle sub-option */
1199 if(result)
1200 return result;
1201 tn->telrcv_state = CURL_TS_DATA;
1202 }
1203 break;
1204 }
1205 ++in;
1206 }
1207 bufferflush();
1208 return CURLE_OK;
1209}
1211static CURLcode telnet_done(struct Curl_easy *data,
1212 CURLcode status, bool premature)
1213{
1214 (void)status;
1215 (void)premature;
1216 Curl_meta_remove(data, CURL_META_TELNET_EASY);
1217 return CURLE_OK;
1218}
1220static CURLcode telnet_do(struct Curl_easy *data, bool *done)
1221{
1222 CURLcode result;
1223 struct connectdata *conn = data->conn;
1224 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
1225#ifdef USE_WINSOCK
1226 WSAEVENT event_handle;
1227 WSANETWORKEVENTS events;
1228 HANDLE stdin_handle;
1229 HANDLE objs[2];
1230 DWORD obj_count;
1231 DWORD wait_timeout;
1232 DWORD readfile_read;
1233#else
1234 timediff_t interval_ms;
1235 struct pollfd pfd[2];
1236 int poll_cnt;
1237 ssize_t snread;
1238#endif
1239 bool keepon = TRUE;
1240 char buffer[4 * 1024];
1241 struct TELNET *tn;
1243 *done = TRUE; /* unconditionally */
1245 result = init_telnet(data);
1246 if(result)
1247 return result;
1249 tn = Curl_meta_get(data, CURL_META_TELNET_EASY);
1250 if(!tn)
1251 return CURLE_FAILED_INIT;
1253 result = check_telnet_options(data, tn);
1254 if(result)
1255 return result;
1257#ifdef USE_WINSOCK
1258 /* We want to wait for both stdin and the socket. Since
1259 * the select() function in Winsock only works on sockets
1260 * we have to use the WaitForMultipleObjects() call.
1261 */
1263 /* First, create a sockets event object */
1264 event_handle = WSACreateEvent();
1265 if(event_handle == WSA_INVALID_EVENT) {
1266 failf(data, "WSACreateEvent failed (%d)", SOCKERRNO);
1267 return CURLE_FAILED_INIT;
1268 }
1270 /* Tell Winsock what events we want to listen to */
1271 if(WSAEventSelect(sockfd, event_handle, FD_READ | FD_CLOSE)) {
1272 WSACloseEvent(event_handle);
1273 return CURLE_RECV_ERROR;
1274 }
1276 /* Then get the Windows file handle for stdin */
1277 stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
1279 /* Create the list of objects to wait for */
1280 objs[0] = event_handle;
1281 objs[1] = stdin_handle;
1283 /* If stdin_handle is a pipe, use PeekNamedPipe() method to check it,
1284 else use the old WaitForMultipleObjects() way */
1285 if(GetFileType(stdin_handle) == FILE_TYPE_PIPE || data->set.is_fread_set) {
1286 /* Do not wait for stdin_handle, wait for event_handle */
1287 obj_count = 1;
1288 /* Check stdin_handle per 100 milliseconds */
1289 wait_timeout = 100;
1290 }
1291 else {
1292 obj_count = 2;
1293 wait_timeout = 1000;
1294 }
1296 /* Keep on listening and act on events */
1297 while(keepon) {
1298 const DWORD buf_size = (DWORD)sizeof(buffer);
1299 DWORD waitret = WaitForMultipleObjects(obj_count, objs,
1300 FALSE, wait_timeout);
1301 switch(waitret) {
1303 case WAIT_TIMEOUT: {
1304 for(;;) {
1305 if(data->set.is_fread_set) {
1306 size_t n;
1307 /* read from user-supplied method */
1308 n = data->state.fread_func(buffer, 1, buf_size, data->state.in);
1309 if(n == CURL_READFUNC_ABORT) {
1310 failf(data, "operation aborted by callback");
1311 keepon = FALSE;
1312 result = CURLE_ABORTED_BY_CALLBACK;
1313 break;
1314 }
1316 if(n == CURL_READFUNC_PAUSE)
1317 break;
1319 if(n == 0) /* no bytes */
1320 break;
1322 /* fall through with number of bytes read */
1323 readfile_read = (DWORD)n;
1324 }
1325 else {
1326 /* read from stdin */
1327 if(!PeekNamedPipe(stdin_handle, NULL, 0, NULL,
1328 &readfile_read, NULL)) {
1329 keepon = FALSE;
1330 result = CURLE_READ_ERROR;
1331 break;
1332 }
1334 if(!readfile_read)
1335 break;
1337 if(!ReadFile(stdin_handle, buffer, buf_size, &readfile_read, NULL)) {
1338 keepon = FALSE;
1339 result = CURLE_READ_ERROR;
1340 break;
1341 }
1342 }
1344 result = send_telnet_data(data, tn, buffer, readfile_read);
1345 if(result) {
1346 keepon = FALSE;
1347 break;
1348 }
1349 }
1350 }
1351 break;
1353 case WAIT_OBJECT_0 + 1: {
1354 if(!ReadFile(stdin_handle, buffer, buf_size, &readfile_read, NULL)) {
1355 keepon = FALSE;
1356 result = CURLE_READ_ERROR;
1357 break;
1358 }
1360 result = send_telnet_data(data, tn, buffer, readfile_read);
1361 if(result) {
1362 keepon = FALSE;
1363 break;
1364 }
1365 }
1366 break;
1368 case WAIT_OBJECT_0: {
1369 events.lNetworkEvents = 0;
1370 if(WSAEnumNetworkEvents(sockfd, event_handle, &events) != 0) {
1371 int sockerr = SOCKERRNO;
1372 if(sockerr != SOCKEINPROGRESS) {
1373 infof(data, "WSAEnumNetworkEvents failed (%d)", sockerr);
1374 keepon = FALSE;
1375 result = CURLE_READ_ERROR;
1376 }
1377 break;
1378 }
1379 if(events.lNetworkEvents & FD_READ) {
1380 /* read data from network */
1381 size_t nread;
1382 result = Curl_xfer_recv(data, buffer, sizeof(buffer), &nread);
1383 /* read would have blocked. Loop again */
1384 if(result == CURLE_AGAIN)
1385 break;
1386 /* returned not-zero, this an error */
1387 else if(result) {
1388 keepon = FALSE;
1389 break;
1390 }
1391 /* returned zero but actually received 0 or less here,
1392 the server closed the connection and we bail out */
1393 else if(!nread) {
1394 keepon = FALSE;
1395 break;
1396 }
1398 result = telrcv(data, tn, (unsigned char *)buffer, nread);
1399 if(result) {
1400 keepon = FALSE;
1401 break;
1402 }
1404 /* Negotiate if the peer has started negotiating,
1405 otherwise do not. We do not want to speak telnet with
1406 non-telnet servers, like POP or SMTP. */
1407 if(tn->please_negotiate && !tn->already_negotiated) {
1408 telnet_negotiate(data, tn);
1409 tn->already_negotiated = 1;
1410 }
1411 }
1412 if(events.lNetworkEvents & FD_CLOSE) {
1413 keepon = FALSE;
1414 }
1415 break;
1416 }
1417 } /* switch */
1419 if(Curl_timeleft_ms(data) < 0) {
1420 failf(data, "Time-out");
1421 result = CURLE_OPERATION_TIMEDOUT;
1422 keepon = FALSE;
1423 }
1424 }
1426 /* We called WSACreateEvent, so call WSACloseEvent */
1427 if(!WSACloseEvent(event_handle)) {
1428 infof(data, "WSACloseEvent failed (%d)", SOCKERRNO);
1429 }
1430#else
1431 pfd[0].fd = sockfd;
1432 pfd[0].events = POLLIN;
1434 if(data->set.is_fread_set) {
1435 poll_cnt = 1;
1436 interval_ms = 100; /* poll user-supplied read function */
1437 }
1438 else {
1439 /* really using fread, so infile is a FILE* */
1440 pfd[1].fd = fileno((FILE *)data->state.in);
1441 pfd[1].events = POLLIN;
1442 poll_cnt = 2;
1443 interval_ms = 1 * 1000;
1444 if(pfd[1].fd < 0) {
1445 failf(data, "cannot read input");
1446 result = CURLE_RECV_ERROR;
1447 keepon = FALSE;
1448 }
1449 }
1451 while(keepon) {
1452 DEBUGF(infof(data, "telnet_do, poll %d fds", poll_cnt));
1453 switch(Curl_poll(pfd, (unsigned int)poll_cnt, interval_ms)) {
1454 case -1: /* error, stop reading */
1455 result = CURLE_RECV_ERROR;
1456 keepon = FALSE;
1457 continue;
1458 case 0: /* timeout */
1459 pfd[0].revents = 0;
1460 pfd[1].revents = 0;
1461 FALLTHROUGH();
1462 default: /* read! */
1463 if(pfd[0].revents & POLLIN) {
1464 /* read data from network */
1465 size_t nread;
1466 result = Curl_xfer_recv(data, buffer, sizeof(buffer), &nread);
1467 /* read would have blocked. Loop again */
1468 if(result == CURLE_AGAIN)
1469 break;
1470 /* returned not-zero, this an error */
1471 if(result) {
1472 keepon = FALSE;
1473 /* In test 1452, macOS sees a ECONNRESET sometimes? Is this the
1474 * telnet test server not shutting down the socket in a clean way?
1475 * Seems to be timing related, happens more on slow debug build */
1476 if(data->state.os_errno == SOCKECONNRESET) {
1477 DEBUGF(infof(data, "telnet_do, unexpected ECONNRESET on recv"));
1478 }
1479 break;
1480 }
1481 /* returned zero but actually received 0 or less here,
1482 the server closed the connection and we bail out */
1483 else if(!nread) {
1484 keepon = FALSE;
1485 break;
1486 }
1488 Curl_pgrs_download_inc(data, nread);
1489 result = telrcv(data, tn, (unsigned char *)buffer, nread);
1490 if(result) {
1491 keepon = FALSE;
1492 break;
1493 }
1495 /* Negotiate if the peer has started negotiating,
1496 otherwise do not. We do not want to speak telnet with
1497 non-telnet servers, like POP or SMTP. */
1498 if(tn->please_negotiate && !tn->already_negotiated) {
1499 telnet_negotiate(data, tn);
1500 tn->already_negotiated = 1;
1501 }
1502 }
1504 snread = 0;
1505 if(poll_cnt == 2) {
1506 if(pfd[1].revents & POLLIN) { /* read from in file */
1507 snread = read(pfd[1].fd, buffer, sizeof(buffer));
1508 }
1509 }
1510 else {
1511 /* read from user-supplied method */
1512 snread = (int)data->state.fread_func(buffer, 1, sizeof(buffer),
1513 data->state.in);
1514 if(snread == CURL_READFUNC_ABORT) {
1515 failf(data, "operation aborted by callback");
1516 result = CURLE_ABORTED_BY_CALLBACK;
1517 keepon = FALSE;
1518 break;
1519 }
1520 if(snread == CURL_READFUNC_PAUSE)
1521 break;
1522 }
1524 if(snread > 0) {
1525 result = send_telnet_data(data, tn, buffer, snread);
1526 if(result) {
1527 keepon = FALSE;
1528 break;
1529 }
1530 Curl_pgrs_upload_inc(data, (size_t)snread);
1531 }
1532 else if(snread < 0) {
1533 failf(data, "Failed to read input");
1534 result = CURLE_READ_ERROR;
1535 keepon = FALSE;
1536 }
1538 break;
1539 } /* poll switch statement */
1541 if(Curl_timeleft_ms(data) < 0) {
1542 failf(data, "Time-out");
1543 result = CURLE_OPERATION_TIMEDOUT;
1544 keepon = FALSE;
1545 }
1547 if(!result) {
1548 result = Curl_pgrsUpdate(data);
1549 if(result)
1550 keepon = FALSE;
1551 }
1552 }
1553#endif
1554 /* mark this as "no further transfer wanted" */
1555 Curl_xfer_setup_nop(data);
1557 return result;
1558}
1560/*
1561 * TELNET protocol handler.
1562 */
1563const struct Curl_protocol Curl_protocol_telnet = {
1564 ZERO_NULL, /* setup_connection */
1565 ZERO_NULL, /* setup_filters */
1566 telnet_do, /* do_it */
1567 telnet_done, /* done */
1568 ZERO_NULL, /* do_more */
1569 ZERO_NULL, /* connect_it */
1570 ZERO_NULL, /* connecting */
1571 ZERO_NULL, /* doing */
1572 ZERO_NULL, /* proto_pollset */
1573 ZERO_NULL, /* doing_pollset */
1574 ZERO_NULL, /* domore_pollset */
1575 ZERO_NULL, /* perform_pollset */
1576 ZERO_NULL, /* disconnect */
1577 ZERO_NULL, /* write_resp */
1578 ZERO_NULL, /* write_resp_hd */
1579 ZERO_NULL, /* connection_is_dead */
1580 ZERO_NULL, /* attach connection */
1581 ZERO_NULL, /* follow */
1582};
1584#endif /* !CURL_DISABLE_TELNET */
Diagnostics4 git calls · 21 ms
CallStatusSizeTime021 ms
discover200152 B1 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=1 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200149 B · hit2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=149 git.response.cache=HIT git.response.duration_ms=2 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=149 git.pkt.payload.max=78 git.ls_refs.count=2 git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=10 git.pkt.data=8 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=293470 git.pkt.payload.max=65516 git.sideband.band1.bytes=293354 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=87 git.pack.bytes=293354 git.pack.ofs_delta=0 git.pack.ref_delta=1 git.pack.chain.max=1 git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0 git.pack.full=86Open pack map
fetch20011 KiB · hit17 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=17 git.response.bytes=11487 git.response.cache=HIT git.response.duration_ms=17 git.fetch.wants=12b39593d5678c947d00320b0f8c70d33f445866 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=11487 git.pkt.payload.max=11466 git.sideband.band1.bytes=11465 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
- Ref
- refs/heads/master
- Commit
- 31c911efe5652725e930f4d2e60c9060e5f6646d
- cf-ray
- a5e15f662997725b
- Request
- 7103dc1d…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format