1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2010, 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 http://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 ***************************************************************************/
25 #ifndef CURL_DISABLE_TELNET
26 /* -- WIN32 approved -- */
37 #ifdef HAVE_SYS_SOCKET_H
38 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #ifdef HAVE_SYS_TIME_H
48 #ifdef HAVE_ARPA_INET_H
49 #include <arpa/inet.h>
54 #ifdef HAVE_SYS_IOCTL_H
55 #include <sys/ioctl.h>
58 #ifdef HAVE_SYS_PARAM_H
59 #include <sys/param.h>
65 #include <curl/curl.h>
72 #define _MPRINTF_REPLACE /* use our functions only */
73 #include <curl/mprintf.h>
78 #include "arpa_telnet.h"
79 #include "curl_memory.h"
84 /* The last #include file should be: */
87 #define SUBBUFSIZE 512
89 #define CURL_SB_CLEAR(x) x->subpointer = x->subbuffer;
90 #define CURL_SB_TERM(x) { x->subend = x->subpointer; CURL_SB_CLEAR(x); }
91 #define CURL_SB_ACCUM(x,c) \
92 if(x->subpointer < (x->subbuffer+sizeof x->subbuffer)) { \
93 *x->subpointer++ = (c); \
96 #define CURL_SB_GET(x) ((*x->subpointer++)&0xff)
97 #define CURL_SB_PEEK(x) ((*x->subpointer)&0xff)
98 #define CURL_SB_EOF(x) (x->subpointer >= x->subend)
99 #define CURL_SB_LEN(x) (x->subend - x->subpointer)
101 #ifdef CURL_DISABLE_VERBOSE_STRINGS
102 #define printoption(a,b,c,d) do { } while(0)
106 typedef FARPROC WSOCK2_FUNC;
107 static CURLcode check_wsock2 ( struct SessionHandle *data );
111 CURLcode telrcv(struct connectdata *,
112 const unsigned char *inbuf, /* Data received from socket */
113 ssize_t count); /* Number of bytes received */
115 #ifndef CURL_DISABLE_VERBOSE_STRINGS
116 static void printoption(struct SessionHandle *data,
117 const char *direction,
118 int cmd, int option);
121 static void negotiate(struct connectdata *);
122 static void send_negotiation(struct connectdata *, int cmd, int option);
123 static void set_local_option(struct connectdata *, int cmd, int option);
124 static void set_remote_option(struct connectdata *, int cmd, int option);
126 static void printsub(struct SessionHandle *data,
127 int direction, unsigned char *pointer,
129 static void suboption(struct connectdata *);
131 static CURLcode telnet_do(struct connectdata *conn, bool *done);
132 static CURLcode telnet_done(struct connectdata *conn,
133 CURLcode, bool premature);
135 /* For negotiation compliant to RFC 1143 */
138 #define CURL_WANTYES 2
139 #define CURL_WANTNO 3
142 #define CURL_OPPOSITE 1
145 * Telnet receiver states for fsm
156 CURL_TS_SB, /* sub-option collection */
157 CURL_TS_SE /* looking for sub-option end */
161 int please_negotiate;
162 int already_negotiated;
165 int us_preferred[256];
168 int him_preferred[256];
169 char subopt_ttype[32]; /* Set with suboption TTYPE */
170 char subopt_xdisploc[128]; /* Set with suboption XDISPLOC */
171 struct curl_slist *telnet_vars; /* Environment variables */
174 unsigned char subbuffer[SUBBUFSIZE];
175 unsigned char *subpointer, *subend; /* buffer for sub-options */
177 TelnetReceive telrcv_state;
182 * TELNET protocol handler.
185 const struct Curl_handler Curl_handler_telnet = {
186 "TELNET", /* scheme */
187 ZERO_NULL, /* setup_connection */
188 telnet_do, /* do_it */
189 telnet_done, /* done */
190 ZERO_NULL, /* do_more */
191 ZERO_NULL, /* connect_it */
192 ZERO_NULL, /* connecting */
193 ZERO_NULL, /* doing */
194 ZERO_NULL, /* proto_getsock */
195 ZERO_NULL, /* doing_getsock */
196 ZERO_NULL, /* perform_getsock */
197 ZERO_NULL, /* disconnect */
198 PORT_TELNET, /* defport */
199 PROT_TELNET /* protocol */
205 check_wsock2 ( struct SessionHandle *data )
208 WORD wVersionRequested;
213 /* telnet requires at least WinSock 2.0 so ask for it. */
214 wVersionRequested = MAKEWORD(2, 0);
216 err = WSAStartup(wVersionRequested, &wsaData);
218 /* We must've called this once already, so this call */
219 /* should always succeed. But, just in case... */
221 failf(data,"WSAStartup failed (%d)",err);
222 return CURLE_FAILED_INIT;
225 /* We have to have a WSACleanup call for every successful */
226 /* WSAStartup call. */
229 /* Check that our version is supported */
230 if(LOBYTE(wsaData.wVersion) != LOBYTE(wVersionRequested) ||
231 HIBYTE(wsaData.wVersion) != HIBYTE(wVersionRequested)) {
232 /* Our version isn't supported */
233 failf(data,"insufficient winsock version to support "
235 return CURLE_FAILED_INIT;
238 /* Our version is supported */
244 CURLcode init_telnet(struct connectdata *conn)
248 tn = calloc(1, sizeof(struct TELNET));
250 return CURLE_OUT_OF_MEMORY;
252 conn->data->state.proto.telnet = (void *)tn; /* make us known */
254 tn->telrcv_state = CURL_TS_DATA;
256 /* Init suboptions */
259 /* Set the options we want by default */
260 tn->us_preferred[CURL_TELOPT_BINARY] = CURL_YES;
261 tn->us_preferred[CURL_TELOPT_SGA] = CURL_YES;
262 tn->him_preferred[CURL_TELOPT_BINARY] = CURL_YES;
263 tn->him_preferred[CURL_TELOPT_SGA] = CURL_YES;
268 static void negotiate(struct connectdata *conn)
271 struct TELNET *tn = (struct TELNET *) conn->data->state.proto.telnet;
273 for(i = 0;i < CURL_NTELOPTS;i++)
275 if(tn->us_preferred[i] == CURL_YES)
276 set_local_option(conn, i, CURL_YES);
278 if(tn->him_preferred[i] == CURL_YES)
279 set_remote_option(conn, i, CURL_YES);
283 #ifndef CURL_DISABLE_VERBOSE_STRINGS
284 static void printoption(struct SessionHandle *data,
285 const char *direction, int cmd, int option)
290 if(data->set.verbose)
294 if(CURL_TELCMD_OK(option))
295 infof(data, "%s IAC %s\n", direction, CURL_TELCMD(option));
297 infof(data, "%s IAC %d\n", direction, option);
301 fmt = (cmd == CURL_WILL) ? "WILL" : (cmd == CURL_WONT) ? "WONT" :
302 (cmd == CURL_DO) ? "DO" : (cmd == CURL_DONT) ? "DONT" : 0;
305 if(CURL_TELOPT_OK(option))
306 opt = CURL_TELOPT(option);
307 else if(option == CURL_TELOPT_EXOPL)
313 infof(data, "%s %s %s\n", direction, fmt, opt);
315 infof(data, "%s %s %d\n", direction, fmt, option);
318 infof(data, "%s %d %d\n", direction, cmd, option);
324 static void send_negotiation(struct connectdata *conn, int cmd, int option)
326 unsigned char buf[3];
327 ssize_t bytes_written;
329 struct SessionHandle *data = conn->data;
332 buf[1] = (unsigned char)cmd;
333 buf[2] = (unsigned char)option;
335 bytes_written = swrite(conn->sock[FIRSTSOCKET], buf, 3);
336 if(bytes_written < 0) {
338 failf(data,"Sending data failed (%d)",err);
341 printoption(conn->data, "SENT", cmd, option);
345 void set_remote_option(struct connectdata *conn, int option, int newstate)
347 struct TELNET *tn = (struct TELNET *)conn->data->state.proto.telnet;
348 if(newstate == CURL_YES)
350 switch(tn->him[option])
353 tn->him[option] = CURL_WANTYES;
354 send_negotiation(conn, CURL_DO, option);
358 /* Already enabled */
362 switch(tn->himq[option])
365 /* Already negotiating for CURL_YES, queue the request */
366 tn->himq[option] = CURL_OPPOSITE;
369 /* Error: already queued an enable request */
375 switch(tn->himq[option])
378 /* Error: already negotiating for enable */
381 tn->himq[option] = CURL_EMPTY;
389 switch(tn->him[option])
392 /* Already disabled */
396 tn->him[option] = CURL_WANTNO;
397 send_negotiation(conn, CURL_DONT, option);
401 switch(tn->himq[option])
404 /* Already negotiating for NO */
407 tn->himq[option] = CURL_EMPTY;
413 switch(tn->himq[option])
416 tn->himq[option] = CURL_OPPOSITE;
427 void rec_will(struct connectdata *conn, int option)
429 struct TELNET *tn = (struct TELNET *)conn->data->state.proto.telnet;
430 switch(tn->him[option])
433 if(tn->him_preferred[option] == CURL_YES)
435 tn->him[option] = CURL_YES;
436 send_negotiation(conn, CURL_DO, option);
440 send_negotiation(conn, CURL_DONT, option);
445 /* Already enabled */
449 switch(tn->himq[option])
452 /* Error: DONT answered by WILL */
453 tn->him[option] = CURL_NO;
456 /* Error: DONT answered by WILL */
457 tn->him[option] = CURL_YES;
458 tn->himq[option] = CURL_EMPTY;
464 switch(tn->himq[option])
467 tn->him[option] = CURL_YES;
470 tn->him[option] = CURL_WANTNO;
471 tn->himq[option] = CURL_EMPTY;
472 send_negotiation(conn, CURL_DONT, option);
480 void rec_wont(struct connectdata *conn, int option)
482 struct TELNET *tn = (struct TELNET *)conn->data->state.proto.telnet;
483 switch(tn->him[option])
486 /* Already disabled */
490 tn->him[option] = CURL_NO;
491 send_negotiation(conn, CURL_DONT, option);
495 switch(tn->himq[option])
498 tn->him[option] = CURL_NO;
502 tn->him[option] = CURL_WANTYES;
503 tn->himq[option] = CURL_EMPTY;
504 send_negotiation(conn, CURL_DO, option);
510 switch(tn->himq[option])
513 tn->him[option] = CURL_NO;
516 tn->him[option] = CURL_NO;
517 tn->himq[option] = CURL_EMPTY;
525 set_local_option(struct connectdata *conn, int option, int newstate)
527 struct TELNET *tn = (struct TELNET *)conn->data->state.proto.telnet;
528 if(newstate == CURL_YES)
530 switch(tn->us[option])
533 tn->us[option] = CURL_WANTYES;
534 send_negotiation(conn, CURL_WILL, option);
538 /* Already enabled */
542 switch(tn->usq[option])
545 /* Already negotiating for CURL_YES, queue the request */
546 tn->usq[option] = CURL_OPPOSITE;
549 /* Error: already queued an enable request */
555 switch(tn->usq[option])
558 /* Error: already negotiating for enable */
561 tn->usq[option] = CURL_EMPTY;
569 switch(tn->us[option])
572 /* Already disabled */
576 tn->us[option] = CURL_WANTNO;
577 send_negotiation(conn, CURL_WONT, option);
581 switch(tn->usq[option])
584 /* Already negotiating for NO */
587 tn->usq[option] = CURL_EMPTY;
593 switch(tn->usq[option])
596 tn->usq[option] = CURL_OPPOSITE;
607 void rec_do(struct connectdata *conn, int option)
609 struct TELNET *tn = (struct TELNET *)conn->data->state.proto.telnet;
610 switch(tn->us[option])
613 if(tn->us_preferred[option] == CURL_YES)
615 tn->us[option] = CURL_YES;
616 send_negotiation(conn, CURL_WILL, option);
620 send_negotiation(conn, CURL_WONT, option);
625 /* Already enabled */
629 switch(tn->usq[option])
632 /* Error: DONT answered by WILL */
633 tn->us[option] = CURL_NO;
636 /* Error: DONT answered by WILL */
637 tn->us[option] = CURL_YES;
638 tn->usq[option] = CURL_EMPTY;
644 switch(tn->usq[option])
647 tn->us[option] = CURL_YES;
650 tn->us[option] = CURL_WANTNO;
651 tn->himq[option] = CURL_EMPTY;
652 send_negotiation(conn, CURL_WONT, option);
660 void rec_dont(struct connectdata *conn, int option)
662 struct TELNET *tn = (struct TELNET *)conn->data->state.proto.telnet;
663 switch(tn->us[option])
666 /* Already disabled */
670 tn->us[option] = CURL_NO;
671 send_negotiation(conn, CURL_WONT, option);
675 switch(tn->usq[option])
678 tn->us[option] = CURL_NO;
682 tn->us[option] = CURL_WANTYES;
683 tn->usq[option] = CURL_EMPTY;
684 send_negotiation(conn, CURL_WILL, option);
690 switch(tn->usq[option])
693 tn->us[option] = CURL_NO;
696 tn->us[option] = CURL_NO;
697 tn->usq[option] = CURL_EMPTY;
705 static void printsub(struct SessionHandle *data,
706 int direction, /* '<' or '>' */
707 unsigned char *pointer, /* where suboption data is */
708 size_t length) /* length of suboption data */
712 if(data->set.verbose)
716 infof(data, "%s IAC SB ", (direction == '<')? "RCVD":"SENT");
721 i = pointer[length-2];
722 j = pointer[length-1];
724 if(i != CURL_IAC || j != CURL_SE)
726 infof(data, "(terminated by ");
727 if(CURL_TELOPT_OK(i))
728 infof(data, "%s ", CURL_TELOPT(i));
729 else if(CURL_TELCMD_OK(i))
730 infof(data, "%s ", CURL_TELCMD(i));
732 infof(data, "%u ", i);
733 if(CURL_TELOPT_OK(j))
734 infof(data, "%s", CURL_TELOPT(j));
735 else if(CURL_TELCMD_OK(j))
736 infof(data, "%s", CURL_TELCMD(j));
738 infof(data, "%d", j);
739 infof(data, ", not IAC SE!) ");
746 infof(data, "(Empty suboption?)");
750 if(CURL_TELOPT_OK(pointer[0])) {
752 case CURL_TELOPT_TTYPE:
753 case CURL_TELOPT_XDISPLOC:
754 case CURL_TELOPT_NEW_ENVIRON:
755 infof(data, "%s", CURL_TELOPT(pointer[0]));
758 infof(data, "%s (unsupported)", CURL_TELOPT(pointer[0]));
763 infof(data, "%d (unknown)", pointer[i]);
766 case CURL_TELQUAL_IS:
769 case CURL_TELQUAL_SEND:
770 infof(data, " SEND");
772 case CURL_TELQUAL_INFO:
773 infof(data, " INFO/REPLY");
775 case CURL_TELQUAL_NAME:
776 infof(data, " NAME");
781 case CURL_TELOPT_TTYPE:
782 case CURL_TELOPT_XDISPLOC:
784 infof(data, " \"%s\"", &pointer[2]);
786 case CURL_TELOPT_NEW_ENVIRON:
787 if(pointer[1] == CURL_TELQUAL_IS) {
789 for(i = 3;i < length;i++) {
791 case CURL_NEW_ENV_VAR:
794 case CURL_NEW_ENV_VALUE:
798 infof(data, "%c", pointer[i]);
805 for (i = 2; i < length; i++)
806 infof(data, " %.2x", pointer[i]);
817 static CURLcode check_telnet_options(struct connectdata *conn)
819 struct curl_slist *head;
820 char option_keyword[128];
821 char option_arg[256];
823 struct SessionHandle *data = conn->data;
824 struct TELNET *tn = (struct TELNET *)conn->data->state.proto.telnet;
826 /* Add the user name as an environment variable if it
827 was given on the command line */
828 if(conn->bits.user_passwd)
830 snprintf(option_arg, sizeof(option_arg), "USER,%s", conn->user);
831 tn->telnet_vars = curl_slist_append(tn->telnet_vars, option_arg);
833 tn->us_preferred[CURL_TELOPT_NEW_ENVIRON] = CURL_YES;
836 for(head = data->set.telnet_options; head; head=head->next) {
837 if(sscanf(head->data, "%127[^= ]%*[ =]%255s",
838 option_keyword, option_arg) == 2) {
841 if(Curl_raw_equal(option_keyword, "TTYPE")) {
842 strncpy(tn->subopt_ttype, option_arg, 31);
843 tn->subopt_ttype[31] = 0; /* String termination */
844 tn->us_preferred[CURL_TELOPT_TTYPE] = CURL_YES;
848 /* Display variable */
849 if(Curl_raw_equal(option_keyword, "XDISPLOC")) {
850 strncpy(tn->subopt_xdisploc, option_arg, 127);
851 tn->subopt_xdisploc[127] = 0; /* String termination */
852 tn->us_preferred[CURL_TELOPT_XDISPLOC] = CURL_YES;
856 /* Environment variable */
857 if(Curl_raw_equal(option_keyword, "NEW_ENV")) {
858 buf = strdup(option_arg);
860 return CURLE_OUT_OF_MEMORY;
861 tn->telnet_vars = curl_slist_append(tn->telnet_vars, buf);
862 tn->us_preferred[CURL_TELOPT_NEW_ENVIRON] = CURL_YES;
866 failf(data, "Unknown telnet option %s", head->data);
867 return CURLE_UNKNOWN_TELNET_OPTION;
869 failf(data, "Syntax error in telnet option: %s", head->data);
870 return CURLE_TELNET_OPTION_SYNTAX;
880 * Look at the sub-option buffer, and try to be helpful to the other
884 static void suboption(struct connectdata *conn)
886 struct curl_slist *v;
887 unsigned char temp[2048];
888 ssize_t bytes_written;
894 struct SessionHandle *data = conn->data;
895 struct TELNET *tn = (struct TELNET *)data->state.proto.telnet;
897 printsub(data, '<', (unsigned char *)tn->subbuffer, CURL_SB_LEN(tn)+2);
898 switch (CURL_SB_GET(tn)) {
899 case CURL_TELOPT_TTYPE:
900 len = strlen(tn->subopt_ttype) + 4 + 2;
901 snprintf((char *)temp, sizeof(temp),
902 "%c%c%c%c%s%c%c", CURL_IAC, CURL_SB, CURL_TELOPT_TTYPE,
903 CURL_TELQUAL_IS, tn->subopt_ttype, CURL_IAC, CURL_SE);
904 bytes_written = swrite(conn->sock[FIRSTSOCKET], temp, len);
905 if(bytes_written < 0) {
907 failf(data,"Sending data failed (%d)",err);
909 printsub(data, '>', &temp[2], len-2);
911 case CURL_TELOPT_XDISPLOC:
912 len = strlen(tn->subopt_xdisploc) + 4 + 2;
913 snprintf((char *)temp, sizeof(temp),
914 "%c%c%c%c%s%c%c", CURL_IAC, CURL_SB, CURL_TELOPT_XDISPLOC,
915 CURL_TELQUAL_IS, tn->subopt_xdisploc, CURL_IAC, CURL_SE);
916 bytes_written = swrite(conn->sock[FIRSTSOCKET], temp, len);
917 if(bytes_written < 0) {
919 failf(data,"Sending data failed (%d)",err);
921 printsub(data, '>', &temp[2], len-2);
923 case CURL_TELOPT_NEW_ENVIRON:
924 snprintf((char *)temp, sizeof(temp),
925 "%c%c%c%c", CURL_IAC, CURL_SB, CURL_TELOPT_NEW_ENVIRON,
929 for(v = tn->telnet_vars;v;v = v->next) {
930 tmplen = (strlen(v->data) + 1);
931 /* Add the variable only if it fits */
932 if(len + tmplen < (int)sizeof(temp)-6) {
933 sscanf(v->data, "%127[^,],%127s", varname, varval);
934 snprintf((char *)&temp[len], sizeof(temp) - len,
935 "%c%s%c%s", CURL_NEW_ENV_VAR, varname,
936 CURL_NEW_ENV_VALUE, varval);
940 snprintf((char *)&temp[len], sizeof(temp) - len,
941 "%c%c", CURL_IAC, CURL_SE);
943 bytes_written = swrite(conn->sock[FIRSTSOCKET], temp, len);
944 if(bytes_written < 0) {
946 failf(data,"Sending data failed (%d)",err);
948 printsub(data, '>', &temp[2], len-2);
955 CURLcode telrcv(struct connectdata *conn,
956 const unsigned char *inbuf, /* Data received from socket */
957 ssize_t count) /* Number of bytes received */
963 struct SessionHandle *data = conn->data;
964 struct TELNET *tn = (struct TELNET *)data->state.proto.telnet;
966 #define startskipping() \
967 if(startwrite >= 0) { \
968 result = Curl_client_write(conn, \
970 (char *)&inbuf[startwrite], \
972 if(result != CURLE_OK) \
977 #define writebyte() \
981 #define bufferflush() startskipping()
987 /*infof(data,"In rcv state %d char %d\n", tn->telrcv_state, c);*/
988 switch (tn->telrcv_state)
991 tn->telrcv_state = CURL_TS_DATA;
995 break; /* Ignore \0 after CR */
1003 tn->telrcv_state = CURL_TS_IAC;
1009 tn->telrcv_state = CURL_TS_CR;
1016 DEBUGASSERT(startwrite < 0);
1020 tn->telrcv_state = CURL_TS_WILL;
1023 tn->telrcv_state = CURL_TS_WONT;
1026 tn->telrcv_state = CURL_TS_DO;
1029 tn->telrcv_state = CURL_TS_DONT;
1033 tn->telrcv_state = CURL_TS_SB;
1036 tn->telrcv_state = CURL_TS_DATA;
1043 tn->telrcv_state = CURL_TS_DATA;
1044 printoption(data, "RCVD", CURL_IAC, c);
1050 printoption(data, "RCVD", CURL_WILL, c);
1051 tn->please_negotiate = 1;
1053 tn->telrcv_state = CURL_TS_DATA;
1057 printoption(data, "RCVD", CURL_WONT, c);
1058 tn->please_negotiate = 1;
1060 tn->telrcv_state = CURL_TS_DATA;
1064 printoption(data, "RCVD", CURL_DO, c);
1065 tn->please_negotiate = 1;
1067 tn->telrcv_state = CURL_TS_DATA;
1071 printoption(data, "RCVD", CURL_DONT, c);
1072 tn->please_negotiate = 1;
1074 tn->telrcv_state = CURL_TS_DATA;
1080 tn->telrcv_state = CURL_TS_SE;
1084 CURL_SB_ACCUM(tn,c);
1094 * This is an error. We only expect to get "IAC IAC" or "IAC SE".
1095 * Several things may have happend. An IAC was not doubled, the
1096 * IAC SE was left off, or another option got inserted into the
1097 * suboption are all possibilities. If we assume that the IAC was
1098 * not doubled, and really the IAC SE was left off, we could get
1099 * into an infinate loop here. So, instead, we terminate the
1100 * suboption, and process the partial suboption if we can.
1102 CURL_SB_ACCUM(tn, CURL_IAC);
1103 CURL_SB_ACCUM(tn, c);
1104 tn->subpointer -= 2;
1107 printoption(data, "In SUBOPTION processing, RCVD", CURL_IAC, c);
1108 suboption(conn); /* handle sub-option */
1109 tn->telrcv_state = CURL_TS_IAC;
1112 CURL_SB_ACCUM(tn,c);
1113 tn->telrcv_state = CURL_TS_SB;
1117 CURL_SB_ACCUM(tn, CURL_IAC);
1118 CURL_SB_ACCUM(tn, CURL_SE);
1119 tn->subpointer -= 2;
1121 suboption(conn); /* handle sub-option */
1122 tn->telrcv_state = CURL_TS_DATA;
1132 /* Escape and send a telnet data block */
1133 /* TODO: write large chunks of data instead of one byte at a time */
1134 static CURLcode send_telnet_data(struct connectdata *conn,
1135 char *buffer, ssize_t nread)
1137 unsigned char outbuf[2];
1138 ssize_t bytes_written, total_written;
1140 CURLcode rc = CURLE_OK;
1142 while(rc == CURLE_OK && nread--) {
1143 outbuf[0] = *buffer++;
1145 if(outbuf[0] == CURL_IAC)
1146 outbuf[out_count++] = CURL_IAC;
1150 /* Make sure socket is writable to avoid EWOULDBLOCK condition */
1151 struct pollfd pfd[1];
1152 pfd[0].fd = conn->sock[FIRSTSOCKET];
1153 pfd[0].events = POLLOUT;
1154 switch (Curl_poll(pfd, 1, -1)) {
1155 case -1: /* error, abort writing */
1156 case 0: /* timeout (will never happen) */
1157 rc = CURLE_SEND_ERROR;
1159 default: /* write! */
1161 rc = Curl_write(conn, conn->sock[FIRSTSOCKET], outbuf+total_written,
1162 out_count-total_written, &bytes_written);
1163 total_written += bytes_written;
1166 /* handle partial write */
1167 } while (rc == CURLE_OK && total_written < out_count);
1172 static CURLcode telnet_done(struct connectdata *conn,
1173 CURLcode status, bool premature)
1175 struct TELNET *tn = (struct TELNET *)conn->data->state.proto.telnet;
1176 (void)status; /* unused */
1177 (void)premature; /* not used */
1179 curl_slist_free_all(tn->telnet_vars);
1181 free(conn->data->state.proto.telnet);
1182 conn->data->state.proto.telnet = NULL;
1187 static CURLcode telnet_do(struct connectdata *conn, bool *done)
1190 struct SessionHandle *data = conn->data;
1191 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
1194 WSOCK2_FUNC close_event_func;
1195 WSOCK2_FUNC create_event_func;
1196 WSOCK2_FUNC event_select_func;
1197 WSOCK2_FUNC enum_netevents_func;
1198 WSAEVENT event_handle;
1199 WSANETWORKEVENTS events;
1200 HANDLE stdin_handle;
1205 DWORD readfile_read;
1209 struct pollfd pfd[2];
1211 curl_off_t total_dl = 0;
1212 curl_off_t total_ul = 0;
1217 char *buf = data->state.buffer;
1220 *done = TRUE; /* unconditionally */
1222 code = init_telnet(conn);
1226 tn = (struct TELNET *)data->state.proto.telnet;
1228 code = check_telnet_options(conn);
1234 ** This functionality only works with WinSock >= 2.0. So,
1235 ** make sure have it.
1237 code = check_wsock2(data);
1241 /* OK, so we have WinSock 2.0. We need to dynamically */
1242 /* load ws2_32.dll and get the function pointers we need. */
1243 wsock2 = LoadLibrary("WS2_32.DLL");
1244 if(wsock2 == NULL) {
1245 failf(data,"failed to load WS2_32.DLL (%d)", ERRNO);
1246 return CURLE_FAILED_INIT;
1249 /* Grab a pointer to WSACreateEvent */
1250 create_event_func = GetProcAddress(wsock2,"WSACreateEvent");
1251 if(create_event_func == NULL) {
1252 failf(data,"failed to find WSACreateEvent function (%d)",
1254 FreeLibrary(wsock2);
1255 return CURLE_FAILED_INIT;
1258 /* And WSACloseEvent */
1259 close_event_func = GetProcAddress(wsock2,"WSACloseEvent");
1260 if(close_event_func == NULL) {
1261 failf(data,"failed to find WSACloseEvent function (%d)",
1263 FreeLibrary(wsock2);
1264 return CURLE_FAILED_INIT;
1267 /* And WSAEventSelect */
1268 event_select_func = GetProcAddress(wsock2,"WSAEventSelect");
1269 if(event_select_func == NULL) {
1270 failf(data,"failed to find WSAEventSelect function (%d)",
1272 FreeLibrary(wsock2);
1273 return CURLE_FAILED_INIT;
1276 /* And WSAEnumNetworkEvents */
1277 enum_netevents_func = GetProcAddress(wsock2,"WSAEnumNetworkEvents");
1278 if(enum_netevents_func == NULL) {
1279 failf(data,"failed to find WSAEnumNetworkEvents function (%d)",
1281 FreeLibrary(wsock2);
1282 return CURLE_FAILED_INIT;
1285 /* We want to wait for both stdin and the socket. Since
1286 ** the select() function in winsock only works on sockets
1287 ** we have to use the WaitForMultipleObjects() call.
1290 /* First, create a sockets event object */
1291 event_handle = (WSAEVENT)create_event_func();
1292 if(event_handle == WSA_INVALID_EVENT) {
1293 failf(data,"WSACreateEvent failed (%d)", SOCKERRNO);
1294 FreeLibrary(wsock2);
1295 return CURLE_FAILED_INIT;
1298 /* The get the Windows file handle for stdin */
1299 stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
1301 /* Create the list of objects to wait for */
1302 objs[0] = event_handle;
1303 objs[1] = stdin_handle;
1305 /* Tell winsock what events we want to listen to */
1306 if(event_select_func(sockfd, event_handle, FD_READ|FD_CLOSE) == SOCKET_ERROR) {
1307 close_event_func(event_handle);
1308 FreeLibrary(wsock2);
1312 /* If stdin_handle is a pipe, use PeekNamedPipe() method to check it,
1313 else use the old WaitForMultipleObjects() way */
1314 if(GetFileType(stdin_handle) == FILE_TYPE_PIPE) {
1315 /* Don't wait for stdin_handle, just wait for event_handle */
1317 /* Check stdin_handle per 100 milliseconds */
1321 wait_timeout = 1000;
1324 /* Keep on listening and act on events */
1326 waitret = WaitForMultipleObjects(obj_count, objs, FALSE, wait_timeout);
1331 if(!PeekNamedPipe(stdin_handle, NULL, 0, NULL, &readfile_read, NULL)) {
1333 code = CURLE_READ_ERROR;
1340 if(!ReadFile(stdin_handle, buf, sizeof(data->state.buffer),
1341 &readfile_read, NULL)) {
1343 code = CURLE_READ_ERROR;
1347 code = send_telnet_data(conn, buf, readfile_read);
1356 case WAIT_OBJECT_0 + 1:
1358 if(!ReadFile(stdin_handle, buf, sizeof(data->state.buffer),
1359 &readfile_read, NULL)) {
1361 code = CURLE_READ_ERROR;
1365 code = send_telnet_data(conn, buf, readfile_read);
1375 if(SOCKET_ERROR == enum_netevents_func(sockfd, event_handle, &events)) {
1376 if((err = SOCKERRNO) != EINPROGRESS) {
1377 infof(data,"WSAEnumNetworkEvents failed (%d)", err);
1379 code = CURLE_READ_ERROR;
1383 if(events.lNetworkEvents & FD_READ) {
1384 /* read data from network */
1385 code = Curl_read(conn, sockfd, buf, BUFSIZE - 1, &nread);
1386 /* read would've blocked. Loop again */
1387 if(code == CURLE_AGAIN)
1389 /* returned not-zero, this an error */
1394 /* returned zero but actually received 0 or less here,
1395 the server closed the connection and we bail out */
1396 else if(nread <= 0) {
1401 code = telrcv(conn, (unsigned char *)buf, nread);
1407 /* Negotiate if the peer has started negotiating,
1408 otherwise don't. We don't want to speak telnet with
1409 non-telnet servers, like POP or SMTP. */
1410 if(tn->please_negotiate && !tn->already_negotiated) {
1412 tn->already_negotiated = 1;
1415 if(events.lNetworkEvents & FD_CLOSE) {
1422 if(data->set.timeout) {
1424 if(Curl_tvdiff(now, conn->created) >= data->set.timeout) {
1425 failf(data, "Time-out");
1426 code = CURLE_OPERATION_TIMEDOUT;
1432 /* We called WSACreateEvent, so call WSACloseEvent */
1433 if(close_event_func(event_handle) == FALSE) {
1434 infof(data,"WSACloseEvent failed (%d)", SOCKERRNO);
1437 /* "Forget" pointers into the library we're about to free */
1438 create_event_func = NULL;
1439 close_event_func = NULL;
1440 event_select_func = NULL;
1441 enum_netevents_func = NULL;
1443 /* We called LoadLibrary, so call FreeLibrary */
1444 if(!FreeLibrary(wsock2))
1445 infof(data,"FreeLibrary(wsock2) failed (%d)", ERRNO);
1448 pfd[0].events = POLLIN;
1450 if (data->set.is_fread_set) {
1452 interval_ms = 100; /* poll user-supplied read function */
1456 pfd[1].events = POLLIN;
1458 interval_ms = 1 * 1000;
1462 switch (Curl_poll(pfd, poll_cnt, interval_ms)) {
1463 case -1: /* error, stop reading */
1466 case 0: /* timeout */
1470 default: /* read! */
1471 if(pfd[0].revents & POLLIN) {
1472 /* read data from network */
1473 code = Curl_read(conn, sockfd, buf, BUFSIZE - 1, &nread);
1474 /* read would've blocked. Loop again */
1475 if(code == CURLE_AGAIN)
1477 /* returned not-zero, this an error */
1482 /* returned zero but actually received 0 or less here,
1483 the server closed the connection and we bail out */
1484 else if(nread <= 0) {
1490 Curl_pgrsSetDownloadCounter(data, total_dl);
1491 code = telrcv(conn, (unsigned char *)buf, nread);
1497 /* Negotiate if the peer has started negotiating,
1498 otherwise don't. We don't want to speak telnet with
1499 non-telnet servers, like POP or SMTP. */
1500 if(tn->please_negotiate && !tn->already_negotiated) {
1502 tn->already_negotiated = 1;
1507 if (poll_cnt == 2) {
1508 if(pfd[1].revents & POLLIN) { /* read from stdin */
1509 nread = read(0, buf, BUFSIZE - 1);
1513 /* read from user-supplied method */
1514 nread = (int)conn->fread_func(buf, 1, BUFSIZE - 1, conn->fread_in);
1515 if (nread == CURL_READFUNC_ABORT) {
1519 if (nread == CURL_READFUNC_PAUSE)
1524 code = send_telnet_data(conn, buf, nread);
1530 Curl_pgrsSetUploadCounter(data, total_ul);
1536 } /* poll switch statement */
1538 if(data->set.timeout) {
1540 if(Curl_tvdiff(now, conn->created) >= data->set.timeout) {
1541 failf(data, "Time-out");
1542 code = CURLE_OPERATION_TIMEDOUT;
1547 if(Curl_pgrsUpdate(conn)) {
1548 code = CURLE_ABORTED_BY_CALLBACK;
1553 /* mark this as "no further transfer wanted" */
1554 Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);