1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
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.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
25 #ifndef CURL_DISABLE_TELNET
27 #ifdef HAVE_NETINET_IN_H
28 #include <netinet/in.h>
33 #ifdef HAVE_ARPA_INET_H
34 #include <arpa/inet.h>
39 #ifdef HAVE_SYS_IOCTL_H
40 #include <sys/ioctl.h>
43 #ifdef HAVE_SYS_PARAM_H
44 #include <sys/param.h>
48 #include <curl/curl.h>
54 #include "system_win32.h"
55 #include "arpa_telnet.h"
60 /* The last 3 #include files should be in this order */
61 #include "curl_printf.h"
62 #include "curl_memory.h"
65 #define SUBBUFSIZE 512
67 #define CURL_SB_CLEAR(x) x->subpointer = x->subbuffer
68 #define CURL_SB_TERM(x) \
70 x->subend = x->subpointer; \
73 #define CURL_SB_ACCUM(x,c) \
75 if(x->subpointer < (x->subbuffer + sizeof(x->subbuffer))) \
76 *x->subpointer++ = (c); \
79 #define CURL_SB_GET(x) ((*x->subpointer++)&0xff)
80 #define CURL_SB_LEN(x) (x->subend - x->subpointer)
83 #define CURL_SB_PEEK(x) ((*x->subpointer)&0xff)
84 #define CURL_SB_EOF(x) (x->subpointer >= x->subend) */
86 #ifdef CURL_DISABLE_VERBOSE_STRINGS
87 #define printoption(a,b,c,d) Curl_nop_stmt
91 CURLcode telrcv(struct connectdata *,
92 const unsigned char *inbuf, /* Data received from socket */
93 ssize_t count); /* Number of bytes received */
95 #ifndef CURL_DISABLE_VERBOSE_STRINGS
96 static void printoption(struct Curl_easy *data,
97 const char *direction,
101 static void negotiate(struct connectdata *);
102 static void send_negotiation(struct connectdata *, int cmd, int option);
103 static void set_local_option(struct connectdata *conn,
104 int option, int newstate);
105 static void set_remote_option(struct connectdata *conn,
106 int option, int newstate);
108 static void printsub(struct Curl_easy *data,
109 int direction, unsigned char *pointer,
111 static void suboption(struct connectdata *);
112 static void sendsuboption(struct connectdata *conn, int option);
114 static CURLcode telnet_do(struct connectdata *conn, bool *done);
115 static CURLcode telnet_done(struct connectdata *conn,
116 CURLcode, bool premature);
117 static CURLcode send_telnet_data(struct connectdata *conn,
118 char *buffer, ssize_t nread);
120 /* For negotiation compliant to RFC 1143 */
123 #define CURL_WANTYES 2
124 #define CURL_WANTNO 3
127 #define CURL_OPPOSITE 1
130 * Telnet receiver states for fsm
141 CURL_TS_SB, /* sub-option collection */
142 CURL_TS_SE /* looking for sub-option end */
146 int please_negotiate;
147 int already_negotiated;
150 int us_preferred[256];
153 int him_preferred[256];
154 int subnegotiation[256];
155 char subopt_ttype[32]; /* Set with suboption TTYPE */
156 char subopt_xdisploc[128]; /* Set with suboption XDISPLOC */
157 unsigned short subopt_wsx; /* Set with suboption NAWS */
158 unsigned short subopt_wsy; /* Set with suboption NAWS */
159 struct curl_slist *telnet_vars; /* Environment variables */
162 unsigned char subbuffer[SUBBUFSIZE];
163 unsigned char *subpointer, *subend; /* buffer for sub-options */
165 TelnetReceive telrcv_state;
170 * TELNET protocol handler.
173 const struct Curl_handler Curl_handler_telnet = {
174 "TELNET", /* scheme */
175 ZERO_NULL, /* setup_connection */
176 telnet_do, /* do_it */
177 telnet_done, /* done */
178 ZERO_NULL, /* do_more */
179 ZERO_NULL, /* connect_it */
180 ZERO_NULL, /* connecting */
181 ZERO_NULL, /* doing */
182 ZERO_NULL, /* proto_getsock */
183 ZERO_NULL, /* doing_getsock */
184 ZERO_NULL, /* domore_getsock */
185 ZERO_NULL, /* perform_getsock */
186 ZERO_NULL, /* disconnect */
187 ZERO_NULL, /* readwrite */
188 ZERO_NULL, /* connection_check */
189 PORT_TELNET, /* defport */
190 CURLPROTO_TELNET, /* protocol */
191 CURLPROTO_TELNET, /* family */
192 PROTOPT_NONE | PROTOPT_NOURLQUERY /* flags */
197 CURLcode init_telnet(struct connectdata *conn)
201 tn = calloc(1, sizeof(struct TELNET));
203 return CURLE_OUT_OF_MEMORY;
205 conn->data->req.protop = tn; /* make us known */
207 tn->telrcv_state = CURL_TS_DATA;
209 /* Init suboptions */
212 /* Set the options we want by default */
213 tn->us_preferred[CURL_TELOPT_SGA] = CURL_YES;
214 tn->him_preferred[CURL_TELOPT_SGA] = CURL_YES;
216 /* To be compliant with previous releases of libcurl
217 we enable this option by default. This behaviour
218 can be changed thanks to the "BINARY" option in
219 CURLOPT_TELNETOPTIONS
221 tn->us_preferred[CURL_TELOPT_BINARY] = CURL_YES;
222 tn->him_preferred[CURL_TELOPT_BINARY] = CURL_YES;
224 /* We must allow the server to echo what we sent
225 but it is not necessary to request the server
226 to do so (it might forces the server to close
227 the connection). Hence, we ignore ECHO in the
230 tn->him_preferred[CURL_TELOPT_ECHO] = CURL_YES;
232 /* Set the subnegotiation fields to send information
233 just after negotiation passed (do/will)
235 Default values are (0,0) initialized by calloc.
236 According to the RFC1013 it is valid:
237 A value equal to zero is acceptable for the width (or height),
238 and means that no character width (or height) is being sent.
239 In this case, the width (or height) that will be assumed by the
240 Telnet server is operating system specific (it will probably be
241 based upon the terminal type information that may have been sent
242 using the TERMINAL TYPE Telnet option). */
243 tn->subnegotiation[CURL_TELOPT_NAWS] = CURL_YES;
247 static void negotiate(struct connectdata *conn)
250 struct TELNET *tn = (struct TELNET *) conn->data->req.protop;
252 for(i = 0; i < CURL_NTELOPTS; i++) {
253 if(i == CURL_TELOPT_ECHO)
256 if(tn->us_preferred[i] == CURL_YES)
257 set_local_option(conn, i, CURL_YES);
259 if(tn->him_preferred[i] == CURL_YES)
260 set_remote_option(conn, i, CURL_YES);
264 #ifndef CURL_DISABLE_VERBOSE_STRINGS
265 static void printoption(struct Curl_easy *data,
266 const char *direction, int cmd, int option)
268 if(data->set.verbose) {
269 if(cmd == CURL_IAC) {
270 if(CURL_TELCMD_OK(option))
271 infof(data, "%s IAC %s\n", direction, CURL_TELCMD(option));
273 infof(data, "%s IAC %d\n", direction, option);
276 const char *fmt = (cmd == CURL_WILL) ? "WILL" :
277 (cmd == CURL_WONT) ? "WONT" :
278 (cmd == CURL_DO) ? "DO" :
279 (cmd == CURL_DONT) ? "DONT" : 0;
282 if(CURL_TELOPT_OK(option))
283 opt = CURL_TELOPT(option);
284 else if(option == CURL_TELOPT_EXOPL)
290 infof(data, "%s %s %s\n", direction, fmt, opt);
292 infof(data, "%s %s %d\n", direction, fmt, option);
295 infof(data, "%s %d %d\n", direction, cmd, option);
301 static void send_negotiation(struct connectdata *conn, int cmd, int option)
303 unsigned char buf[3];
304 ssize_t bytes_written;
305 struct Curl_easy *data = conn->data;
308 buf[1] = (unsigned char)cmd;
309 buf[2] = (unsigned char)option;
311 bytes_written = swrite(conn->sock[FIRSTSOCKET], buf, 3);
312 if(bytes_written < 0) {
314 failf(data,"Sending data failed (%d)",err);
317 printoption(conn->data, "SENT", cmd, option);
321 void set_remote_option(struct connectdata *conn, int option, int newstate)
323 struct TELNET *tn = (struct TELNET *)conn->data->req.protop;
324 if(newstate == CURL_YES) {
325 switch(tn->him[option]) {
327 tn->him[option] = CURL_WANTYES;
328 send_negotiation(conn, CURL_DO, option);
332 /* Already enabled */
336 switch(tn->himq[option]) {
338 /* Already negotiating for CURL_YES, queue the request */
339 tn->himq[option] = CURL_OPPOSITE;
342 /* Error: already queued an enable request */
348 switch(tn->himq[option]) {
350 /* Error: already negotiating for enable */
353 tn->himq[option] = CURL_EMPTY;
360 switch(tn->him[option]) {
362 /* Already disabled */
366 tn->him[option] = CURL_WANTNO;
367 send_negotiation(conn, CURL_DONT, option);
371 switch(tn->himq[option]) {
373 /* Already negotiating for NO */
376 tn->himq[option] = CURL_EMPTY;
382 switch(tn->himq[option]) {
384 tn->himq[option] = CURL_OPPOSITE;
395 void rec_will(struct connectdata *conn, int option)
397 struct TELNET *tn = (struct TELNET *)conn->data->req.protop;
398 switch(tn->him[option]) {
400 if(tn->him_preferred[option] == CURL_YES) {
401 tn->him[option] = CURL_YES;
402 send_negotiation(conn, CURL_DO, option);
405 send_negotiation(conn, CURL_DONT, option);
410 /* Already enabled */
414 switch(tn->himq[option]) {
416 /* Error: DONT answered by WILL */
417 tn->him[option] = CURL_NO;
420 /* Error: DONT answered by WILL */
421 tn->him[option] = CURL_YES;
422 tn->himq[option] = CURL_EMPTY;
428 switch(tn->himq[option]) {
430 tn->him[option] = CURL_YES;
433 tn->him[option] = CURL_WANTNO;
434 tn->himq[option] = CURL_EMPTY;
435 send_negotiation(conn, CURL_DONT, option);
443 void rec_wont(struct connectdata *conn, int option)
445 struct TELNET *tn = (struct TELNET *)conn->data->req.protop;
446 switch(tn->him[option]) {
448 /* Already disabled */
452 tn->him[option] = CURL_NO;
453 send_negotiation(conn, CURL_DONT, option);
457 switch(tn->himq[option]) {
459 tn->him[option] = CURL_NO;
463 tn->him[option] = CURL_WANTYES;
464 tn->himq[option] = CURL_EMPTY;
465 send_negotiation(conn, CURL_DO, option);
471 switch(tn->himq[option]) {
473 tn->him[option] = CURL_NO;
476 tn->him[option] = CURL_NO;
477 tn->himq[option] = CURL_EMPTY;
485 set_local_option(struct connectdata *conn, int option, int newstate)
487 struct TELNET *tn = (struct TELNET *)conn->data->req.protop;
488 if(newstate == CURL_YES) {
489 switch(tn->us[option]) {
491 tn->us[option] = CURL_WANTYES;
492 send_negotiation(conn, CURL_WILL, option);
496 /* Already enabled */
500 switch(tn->usq[option]) {
502 /* Already negotiating for CURL_YES, queue the request */
503 tn->usq[option] = CURL_OPPOSITE;
506 /* Error: already queued an enable request */
512 switch(tn->usq[option]) {
514 /* Error: already negotiating for enable */
517 tn->usq[option] = CURL_EMPTY;
524 switch(tn->us[option]) {
526 /* Already disabled */
530 tn->us[option] = CURL_WANTNO;
531 send_negotiation(conn, CURL_WONT, option);
535 switch(tn->usq[option]) {
537 /* Already negotiating for NO */
540 tn->usq[option] = CURL_EMPTY;
546 switch(tn->usq[option]) {
548 tn->usq[option] = CURL_OPPOSITE;
559 void rec_do(struct connectdata *conn, int option)
561 struct TELNET *tn = (struct TELNET *)conn->data->req.protop;
562 switch(tn->us[option]) {
564 if(tn->us_preferred[option] == CURL_YES) {
565 tn->us[option] = CURL_YES;
566 send_negotiation(conn, CURL_WILL, option);
567 if(tn->subnegotiation[option] == CURL_YES)
568 /* transmission of data option */
569 sendsuboption(conn, option);
571 else if(tn->subnegotiation[option] == CURL_YES) {
572 /* send information to achieve this option*/
573 tn->us[option] = CURL_YES;
574 send_negotiation(conn, CURL_WILL, option);
575 sendsuboption(conn, option);
578 send_negotiation(conn, CURL_WONT, option);
582 /* Already enabled */
586 switch(tn->usq[option]) {
588 /* Error: DONT answered by WILL */
589 tn->us[option] = CURL_NO;
592 /* Error: DONT answered by WILL */
593 tn->us[option] = CURL_YES;
594 tn->usq[option] = CURL_EMPTY;
600 switch(tn->usq[option]) {
602 tn->us[option] = CURL_YES;
603 if(tn->subnegotiation[option] == CURL_YES) {
604 /* transmission of data option */
605 sendsuboption(conn, option);
609 tn->us[option] = CURL_WANTNO;
610 tn->himq[option] = CURL_EMPTY;
611 send_negotiation(conn, CURL_WONT, option);
619 void rec_dont(struct connectdata *conn, int option)
621 struct TELNET *tn = (struct TELNET *)conn->data->req.protop;
622 switch(tn->us[option]) {
624 /* Already disabled */
628 tn->us[option] = CURL_NO;
629 send_negotiation(conn, CURL_WONT, option);
633 switch(tn->usq[option]) {
635 tn->us[option] = CURL_NO;
639 tn->us[option] = CURL_WANTYES;
640 tn->usq[option] = CURL_EMPTY;
641 send_negotiation(conn, CURL_WILL, option);
647 switch(tn->usq[option]) {
649 tn->us[option] = CURL_NO;
652 tn->us[option] = CURL_NO;
653 tn->usq[option] = CURL_EMPTY;
661 static void printsub(struct Curl_easy *data,
662 int direction, /* '<' or '>' */
663 unsigned char *pointer, /* where suboption data is */
664 size_t length) /* length of suboption data */
666 if(data->set.verbose) {
669 infof(data, "%s IAC SB ", (direction == '<')? "RCVD":"SENT");
673 i = pointer[length-2];
674 j = pointer[length-1];
676 if(i != CURL_IAC || j != CURL_SE) {
677 infof(data, "(terminated by ");
678 if(CURL_TELOPT_OK(i))
679 infof(data, "%s ", CURL_TELOPT(i));
680 else if(CURL_TELCMD_OK(i))
681 infof(data, "%s ", CURL_TELCMD(i));
683 infof(data, "%u ", i);
684 if(CURL_TELOPT_OK(j))
685 infof(data, "%s", CURL_TELOPT(j));
686 else if(CURL_TELCMD_OK(j))
687 infof(data, "%s", CURL_TELCMD(j));
689 infof(data, "%d", j);
690 infof(data, ", not IAC SE!) ");
696 infof(data, "(Empty suboption?)");
700 if(CURL_TELOPT_OK(pointer[0])) {
702 case CURL_TELOPT_TTYPE:
703 case CURL_TELOPT_XDISPLOC:
704 case CURL_TELOPT_NEW_ENVIRON:
705 case CURL_TELOPT_NAWS:
706 infof(data, "%s", CURL_TELOPT(pointer[0]));
709 infof(data, "%s (unsupported)", CURL_TELOPT(pointer[0]));
714 infof(data, "%d (unknown)", pointer[i]);
717 case CURL_TELOPT_NAWS:
719 infof(data, "Width: %d ; Height: %d", (pointer[1]<<8) | pointer[2],
720 (pointer[3]<<8) | pointer[4]);
724 case CURL_TELQUAL_IS:
727 case CURL_TELQUAL_SEND:
728 infof(data, " SEND");
730 case CURL_TELQUAL_INFO:
731 infof(data, " INFO/REPLY");
733 case CURL_TELQUAL_NAME:
734 infof(data, " NAME");
739 case CURL_TELOPT_TTYPE:
740 case CURL_TELOPT_XDISPLOC:
742 infof(data, " \"%s\"", &pointer[2]);
744 case CURL_TELOPT_NEW_ENVIRON:
745 if(pointer[1] == CURL_TELQUAL_IS) {
747 for(i = 3; i < length; i++) {
749 case CURL_NEW_ENV_VAR:
752 case CURL_NEW_ENV_VALUE:
756 infof(data, "%c", pointer[i]);
763 for(i = 2; i < length; i++)
764 infof(data, " %.2x", pointer[i]);
773 static CURLcode check_telnet_options(struct connectdata *conn)
775 struct curl_slist *head;
776 struct curl_slist *beg;
777 char option_keyword[128] = "";
778 char option_arg[256] = "";
779 struct Curl_easy *data = conn->data;
780 struct TELNET *tn = (struct TELNET *)conn->data->req.protop;
781 CURLcode result = CURLE_OK;
784 /* Add the user name as an environment variable if it
785 was given on the command line */
786 if(conn->bits.user_passwd) {
787 msnprintf(option_arg, sizeof(option_arg), "USER,%s", conn->user);
788 beg = curl_slist_append(tn->telnet_vars, option_arg);
790 curl_slist_free_all(tn->telnet_vars);
791 tn->telnet_vars = NULL;
792 return CURLE_OUT_OF_MEMORY;
794 tn->telnet_vars = beg;
795 tn->us_preferred[CURL_TELOPT_NEW_ENVIRON] = CURL_YES;
798 for(head = data->set.telnet_options; head; head = head->next) {
799 if(sscanf(head->data, "%127[^= ]%*[ =]%255s",
800 option_keyword, option_arg) == 2) {
803 if(strcasecompare(option_keyword, "TTYPE")) {
804 strncpy(tn->subopt_ttype, option_arg, 31);
805 tn->subopt_ttype[31] = 0; /* String termination */
806 tn->us_preferred[CURL_TELOPT_TTYPE] = CURL_YES;
810 /* Display variable */
811 if(strcasecompare(option_keyword, "XDISPLOC")) {
812 strncpy(tn->subopt_xdisploc, option_arg, 127);
813 tn->subopt_xdisploc[127] = 0; /* String termination */
814 tn->us_preferred[CURL_TELOPT_XDISPLOC] = CURL_YES;
818 /* Environment variable */
819 if(strcasecompare(option_keyword, "NEW_ENV")) {
820 beg = curl_slist_append(tn->telnet_vars, option_arg);
822 result = CURLE_OUT_OF_MEMORY;
825 tn->telnet_vars = beg;
826 tn->us_preferred[CURL_TELOPT_NEW_ENVIRON] = CURL_YES;
831 if(strcasecompare(option_keyword, "WS")) {
832 if(sscanf(option_arg, "%hu%*[xX]%hu",
833 &tn->subopt_wsx, &tn->subopt_wsy) == 2)
834 tn->us_preferred[CURL_TELOPT_NAWS] = CURL_YES;
836 failf(data, "Syntax error in telnet option: %s", head->data);
837 result = CURLE_TELNET_OPTION_SYNTAX;
843 /* To take care or not of the 8th bit in data exchange */
844 if(strcasecompare(option_keyword, "BINARY")) {
845 binary_option = atoi(option_arg);
846 if(binary_option != 1) {
847 tn->us_preferred[CURL_TELOPT_BINARY] = CURL_NO;
848 tn->him_preferred[CURL_TELOPT_BINARY] = CURL_NO;
853 failf(data, "Unknown telnet option %s", head->data);
854 result = CURLE_UNKNOWN_OPTION;
857 failf(data, "Syntax error in telnet option: %s", head->data);
858 result = CURLE_TELNET_OPTION_SYNTAX;
863 curl_slist_free_all(tn->telnet_vars);
864 tn->telnet_vars = NULL;
873 * Look at the sub-option buffer, and try to be helpful to the other
877 static void suboption(struct connectdata *conn)
879 struct curl_slist *v;
880 unsigned char temp[2048];
881 ssize_t bytes_written;
884 char varname[128] = "";
885 char varval[128] = "";
886 struct Curl_easy *data = conn->data;
887 struct TELNET *tn = (struct TELNET *)data->req.protop;
889 printsub(data, '<', (unsigned char *)tn->subbuffer, CURL_SB_LEN(tn) + 2);
890 switch(CURL_SB_GET(tn)) {
891 case CURL_TELOPT_TTYPE:
892 len = strlen(tn->subopt_ttype) + 4 + 2;
893 msnprintf((char *)temp, sizeof(temp),
894 "%c%c%c%c%s%c%c", CURL_IAC, CURL_SB, CURL_TELOPT_TTYPE,
895 CURL_TELQUAL_IS, tn->subopt_ttype, CURL_IAC, CURL_SE);
896 bytes_written = swrite(conn->sock[FIRSTSOCKET], temp, len);
897 if(bytes_written < 0) {
899 failf(data,"Sending data failed (%d)",err);
901 printsub(data, '>', &temp[2], len-2);
903 case CURL_TELOPT_XDISPLOC:
904 len = strlen(tn->subopt_xdisploc) + 4 + 2;
905 msnprintf((char *)temp, sizeof(temp),
906 "%c%c%c%c%s%c%c", CURL_IAC, CURL_SB, CURL_TELOPT_XDISPLOC,
907 CURL_TELQUAL_IS, tn->subopt_xdisploc, CURL_IAC, CURL_SE);
908 bytes_written = swrite(conn->sock[FIRSTSOCKET], temp, len);
909 if(bytes_written < 0) {
911 failf(data,"Sending data failed (%d)",err);
913 printsub(data, '>', &temp[2], len-2);
915 case CURL_TELOPT_NEW_ENVIRON:
916 msnprintf((char *)temp, sizeof(temp),
917 "%c%c%c%c", CURL_IAC, CURL_SB, CURL_TELOPT_NEW_ENVIRON,
921 for(v = tn->telnet_vars; v; v = v->next) {
922 size_t tmplen = (strlen(v->data) + 1);
923 /* Add the variable only if it fits */
924 if(len + tmplen < (int)sizeof(temp)-6) {
925 if(sscanf(v->data, "%127[^,],%127s", varname, varval)) {
926 msnprintf((char *)&temp[len], sizeof(temp) - len,
927 "%c%s%c%s", CURL_NEW_ENV_VAR, varname,
928 CURL_NEW_ENV_VALUE, varval);
933 msnprintf((char *)&temp[len], sizeof(temp) - len,
934 "%c%c", CURL_IAC, CURL_SE);
936 bytes_written = swrite(conn->sock[FIRSTSOCKET], temp, len);
937 if(bytes_written < 0) {
939 failf(data,"Sending data failed (%d)",err);
941 printsub(data, '>', &temp[2], len-2);
951 * Send suboption information to the server side.
954 static void sendsuboption(struct connectdata *conn, int option)
956 ssize_t bytes_written;
959 unsigned char *uc1, *uc2;
961 struct Curl_easy *data = conn->data;
962 struct TELNET *tn = (struct TELNET *)data->req.protop;
965 case CURL_TELOPT_NAWS:
966 /* We prepare data to be sent */
968 CURL_SB_ACCUM(tn, CURL_IAC);
969 CURL_SB_ACCUM(tn, CURL_SB);
970 CURL_SB_ACCUM(tn, CURL_TELOPT_NAWS);
971 /* We must deal either with little or big endian processors */
972 /* Window size must be sent according to the 'network order' */
973 x = htons(tn->subopt_wsx);
974 y = htons(tn->subopt_wsy);
975 uc1 = (unsigned char *)&x;
976 uc2 = (unsigned char *)&y;
977 CURL_SB_ACCUM(tn, uc1[0]);
978 CURL_SB_ACCUM(tn, uc1[1]);
979 CURL_SB_ACCUM(tn, uc2[0]);
980 CURL_SB_ACCUM(tn, uc2[1]);
982 CURL_SB_ACCUM(tn, CURL_IAC);
983 CURL_SB_ACCUM(tn, CURL_SE);
985 /* data suboption is now ready */
987 printsub(data, '>', (unsigned char *)tn->subbuffer + 2,
990 /* we send the header of the suboption... */
991 bytes_written = swrite(conn->sock[FIRSTSOCKET], tn->subbuffer, 3);
992 if(bytes_written < 0) {
994 failf(data, "Sending data failed (%d)", err);
996 /* ... then the window size with the send_telnet_data() function
997 to deal with 0xFF cases ... */
998 send_telnet_data(conn, (char *)tn->subbuffer + 3, 4);
999 /* ... and the footer */
1000 bytes_written = swrite(conn->sock[FIRSTSOCKET], tn->subbuffer + 7, 2);
1001 if(bytes_written < 0) {
1003 failf(data, "Sending data failed (%d)", err);
1011 CURLcode telrcv(struct connectdata *conn,
1012 const unsigned char *inbuf, /* Data received from socket */
1013 ssize_t count) /* Number of bytes received */
1018 int startwrite = -1;
1019 struct Curl_easy *data = conn->data;
1020 struct TELNET *tn = (struct TELNET *)data->req.protop;
1022 #define startskipping() \
1023 if(startwrite >= 0) { \
1024 result = Curl_client_write(conn, \
1026 (char *)&inbuf[startwrite], \
1033 #define writebyte() \
1034 if(startwrite < 0) \
1037 #define bufferflush() startskipping()
1042 switch(tn->telrcv_state) {
1044 tn->telrcv_state = CURL_TS_DATA;
1047 break; /* Ignore \0 after CR */
1054 tn->telrcv_state = CURL_TS_IAC;
1059 tn->telrcv_state = CURL_TS_CR;
1065 DEBUGASSERT(startwrite < 0);
1068 tn->telrcv_state = CURL_TS_WILL;
1071 tn->telrcv_state = CURL_TS_WONT;
1074 tn->telrcv_state = CURL_TS_DO;
1077 tn->telrcv_state = CURL_TS_DONT;
1081 tn->telrcv_state = CURL_TS_SB;
1084 tn->telrcv_state = CURL_TS_DATA;
1091 tn->telrcv_state = CURL_TS_DATA;
1092 printoption(data, "RCVD", CURL_IAC, c);
1098 printoption(data, "RCVD", CURL_WILL, c);
1099 tn->please_negotiate = 1;
1101 tn->telrcv_state = CURL_TS_DATA;
1105 printoption(data, "RCVD", CURL_WONT, c);
1106 tn->please_negotiate = 1;
1108 tn->telrcv_state = CURL_TS_DATA;
1112 printoption(data, "RCVD", CURL_DO, c);
1113 tn->please_negotiate = 1;
1115 tn->telrcv_state = CURL_TS_DATA;
1119 printoption(data, "RCVD", CURL_DONT, c);
1120 tn->please_negotiate = 1;
1122 tn->telrcv_state = CURL_TS_DATA;
1127 tn->telrcv_state = CURL_TS_SE;
1129 CURL_SB_ACCUM(tn, c);
1136 * This is an error. We only expect to get "IAC IAC" or "IAC SE".
1137 * Several things may have happened. An IAC was not doubled, the
1138 * IAC SE was left off, or another option got inserted into the
1139 * suboption are all possibilities. If we assume that the IAC was
1140 * not doubled, and really the IAC SE was left off, we could get
1141 * into an infinite loop here. So, instead, we terminate the
1142 * suboption, and process the partial suboption if we can.
1144 CURL_SB_ACCUM(tn, CURL_IAC);
1145 CURL_SB_ACCUM(tn, c);
1146 tn->subpointer -= 2;
1149 printoption(data, "In SUBOPTION processing, RCVD", CURL_IAC, c);
1150 suboption(conn); /* handle sub-option */
1151 tn->telrcv_state = CURL_TS_IAC;
1154 CURL_SB_ACCUM(tn, c);
1155 tn->telrcv_state = CURL_TS_SB;
1158 CURL_SB_ACCUM(tn, CURL_IAC);
1159 CURL_SB_ACCUM(tn, CURL_SE);
1160 tn->subpointer -= 2;
1162 suboption(conn); /* handle sub-option */
1163 tn->telrcv_state = CURL_TS_DATA;
1173 /* Escape and send a telnet data block */
1174 static CURLcode send_telnet_data(struct connectdata *conn,
1175 char *buffer, ssize_t nread)
1177 ssize_t escapes, i, outlen;
1178 unsigned char *outbuf = NULL;
1179 CURLcode result = CURLE_OK;
1180 ssize_t bytes_written, total_written;
1182 /* Determine size of new buffer after escaping */
1184 for(i = 0; i < nread; i++)
1185 if((unsigned char)buffer[i] == CURL_IAC)
1187 outlen = nread + escapes;
1190 outbuf = (unsigned char *)buffer;
1193 outbuf = malloc(nread + escapes + 1);
1195 return CURLE_OUT_OF_MEMORY;
1198 for(i = 0; i < nread; i++) {
1199 outbuf[j++] = buffer[i];
1200 if((unsigned char)buffer[i] == CURL_IAC)
1201 outbuf[j++] = CURL_IAC;
1207 while(!result && total_written < outlen) {
1208 /* Make sure socket is writable to avoid EWOULDBLOCK condition */
1209 struct pollfd pfd[1];
1210 pfd[0].fd = conn->sock[FIRSTSOCKET];
1211 pfd[0].events = POLLOUT;
1212 switch(Curl_poll(pfd, 1, -1)) {
1213 case -1: /* error, abort writing */
1214 case 0: /* timeout (will never happen) */
1215 result = CURLE_SEND_ERROR;
1217 default: /* write! */
1219 result = Curl_write(conn, conn->sock[FIRSTSOCKET],
1220 outbuf + total_written,
1221 outlen - total_written,
1223 total_written += bytes_written;
1228 /* Free malloc copy if escaped */
1229 if(outbuf != (unsigned char *)buffer)
1235 static CURLcode telnet_done(struct connectdata *conn,
1236 CURLcode status, bool premature)
1238 struct TELNET *tn = (struct TELNET *)conn->data->req.protop;
1239 (void)status; /* unused */
1240 (void)premature; /* not used */
1245 curl_slist_free_all(tn->telnet_vars);
1246 tn->telnet_vars = NULL;
1248 Curl_safefree(conn->data->req.protop);
1253 static CURLcode telnet_do(struct connectdata *conn, bool *done)
1256 struct Curl_easy *data = conn->data;
1257 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
1259 WSAEVENT event_handle;
1260 WSANETWORKEVENTS events;
1261 HANDLE stdin_handle;
1265 DWORD readfile_read;
1268 timediff_t interval_ms;
1269 struct pollfd pfd[2];
1271 curl_off_t total_dl = 0;
1272 curl_off_t total_ul = 0;
1275 struct curltime now;
1277 char *buf = data->state.buffer;
1280 *done = TRUE; /* unconditionally */
1282 result = init_telnet(conn);
1286 tn = (struct TELNET *)data->req.protop;
1288 result = check_telnet_options(conn);
1293 /* We want to wait for both stdin and the socket. Since
1294 ** the select() function in winsock only works on sockets
1295 ** we have to use the WaitForMultipleObjects() call.
1298 /* First, create a sockets event object */
1299 event_handle = WSACreateEvent();
1300 if(event_handle == WSA_INVALID_EVENT) {
1301 failf(data, "WSACreateEvent failed (%d)", SOCKERRNO);
1302 return CURLE_FAILED_INIT;
1305 /* Tell winsock what events we want to listen to */
1306 if(WSAEventSelect(sockfd, event_handle, FD_READ|FD_CLOSE) == SOCKET_ERROR) {
1307 WSACloseEvent(event_handle);
1311 /* The get the Windows file handle for stdin */
1312 stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
1314 /* Create the list of objects to wait for */
1315 objs[0] = event_handle;
1316 objs[1] = stdin_handle;
1318 /* If stdin_handle is a pipe, use PeekNamedPipe() method to check it,
1319 else use the old WaitForMultipleObjects() way */
1320 if(GetFileType(stdin_handle) == FILE_TYPE_PIPE ||
1321 data->set.is_fread_set) {
1322 /* Don't wait for stdin_handle, just wait for event_handle */
1324 /* Check stdin_handle per 100 milliseconds */
1329 wait_timeout = 1000;
1332 /* Keep on listening and act on events */
1334 const DWORD buf_size = (DWORD)data->set.buffer_size;
1335 DWORD waitret = WaitForMultipleObjects(obj_count, objs,
1336 FALSE, wait_timeout);
1342 if(data->set.is_fread_set) {
1344 /* read from user-supplied method */
1345 n = data->state.fread_func(buf, 1, buf_size, data->state.in);
1346 if(n == CURL_READFUNC_ABORT) {
1348 result = CURLE_READ_ERROR;
1352 if(n == CURL_READFUNC_PAUSE)
1355 if(n == 0) /* no bytes */
1358 /* fall through with number of bytes read */
1359 readfile_read = (DWORD)n;
1362 /* read from stdin */
1363 if(!PeekNamedPipe(stdin_handle, NULL, 0, NULL,
1364 &readfile_read, NULL)) {
1366 result = CURLE_READ_ERROR;
1373 if(!ReadFile(stdin_handle, buf, buf_size,
1374 &readfile_read, NULL)) {
1376 result = CURLE_READ_ERROR;
1381 result = send_telnet_data(conn, buf, readfile_read);
1390 case WAIT_OBJECT_0 + 1:
1392 if(!ReadFile(stdin_handle, buf, buf_size,
1393 &readfile_read, NULL)) {
1395 result = CURLE_READ_ERROR;
1399 result = send_telnet_data(conn, buf, readfile_read);
1409 events.lNetworkEvents = 0;
1410 if(WSAEnumNetworkEvents(sockfd, event_handle, &events) == SOCKET_ERROR) {
1412 if(err != EINPROGRESS) {
1413 infof(data, "WSAEnumNetworkEvents failed (%d)", err);
1415 result = CURLE_READ_ERROR;
1419 if(events.lNetworkEvents & FD_READ) {
1420 /* read data from network */
1421 result = Curl_read(conn, sockfd, buf, data->set.buffer_size, &nread);
1422 /* read would've blocked. Loop again */
1423 if(result == CURLE_AGAIN)
1425 /* returned not-zero, this an error */
1430 /* returned zero but actually received 0 or less here,
1431 the server closed the connection and we bail out */
1432 else if(nread <= 0) {
1437 result = telrcv(conn, (unsigned char *) buf, nread);
1443 /* Negotiate if the peer has started negotiating,
1444 otherwise don't. We don't want to speak telnet with
1445 non-telnet servers, like POP or SMTP. */
1446 if(tn->please_negotiate && !tn->already_negotiated) {
1448 tn->already_negotiated = 1;
1451 if(events.lNetworkEvents & FD_CLOSE) {
1459 if(data->set.timeout) {
1461 if(Curl_timediff(now, conn->created) >= data->set.timeout) {
1462 failf(data, "Time-out");
1463 result = CURLE_OPERATION_TIMEDOUT;
1469 /* We called WSACreateEvent, so call WSACloseEvent */
1470 if(!WSACloseEvent(event_handle)) {
1471 infof(data, "WSACloseEvent failed (%d)", SOCKERRNO);
1475 pfd[0].events = POLLIN;
1477 if(data->set.is_fread_set) {
1479 interval_ms = 100; /* poll user-supplied read function */
1482 /* really using fread, so infile is a FILE* */
1483 pfd[1].fd = fileno((FILE *)data->state.in);
1484 pfd[1].events = POLLIN;
1486 interval_ms = 1 * 1000;
1490 switch(Curl_poll(pfd, poll_cnt, interval_ms)) {
1491 case -1: /* error, stop reading */
1494 case 0: /* timeout */
1498 default: /* read! */
1499 if(pfd[0].revents & POLLIN) {
1500 /* read data from network */
1501 result = Curl_read(conn, sockfd, buf, data->set.buffer_size, &nread);
1502 /* read would've blocked. Loop again */
1503 if(result == CURLE_AGAIN)
1505 /* returned not-zero, this an error */
1510 /* returned zero but actually received 0 or less here,
1511 the server closed the connection and we bail out */
1512 else if(nread <= 0) {
1518 Curl_pgrsSetDownloadCounter(data, total_dl);
1519 result = telrcv(conn, (unsigned char *)buf, nread);
1525 /* Negotiate if the peer has started negotiating,
1526 otherwise don't. We don't want to speak telnet with
1527 non-telnet servers, like POP or SMTP. */
1528 if(tn->please_negotiate && !tn->already_negotiated) {
1530 tn->already_negotiated = 1;
1536 if(pfd[1].revents & POLLIN) { /* read from in file */
1537 nread = read(pfd[1].fd, buf, data->set.buffer_size);
1541 /* read from user-supplied method */
1542 nread = (int)data->state.fread_func(buf, 1, data->set.buffer_size,
1544 if(nread == CURL_READFUNC_ABORT) {
1548 if(nread == CURL_READFUNC_PAUSE)
1553 result = send_telnet_data(conn, buf, nread);
1559 Curl_pgrsSetUploadCounter(data, total_ul);
1565 } /* poll switch statement */
1567 if(data->set.timeout) {
1569 if(Curl_timediff(now, conn->created) >= data->set.timeout) {
1570 failf(data, "Time-out");
1571 result = CURLE_OPERATION_TIMEDOUT;
1576 if(Curl_pgrsUpdate(conn)) {
1577 result = CURLE_ABORTED_BY_CALLBACK;
1582 /* mark this as "no further transfer wanted" */
1583 Curl_setup_transfer(data, -1, -1, FALSE, -1);