1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2008, 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.
22 ***************************************************************************/
26 #ifndef CURL_DISABLE_TELNET
27 /* -- WIN32 approved -- */
38 #ifdef HAVE_SYS_SOCKET_H
39 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #ifdef HAVE_SYS_TIME_H
49 #ifdef HAVE_ARPA_INET_H
50 #include <arpa/inet.h>
55 #ifdef HAVE_SYS_IOCTL_H
56 #include <sys/ioctl.h>
59 #ifdef HAVE_SYS_PARAM_H
60 #include <sys/param.h>
66 #include <curl/curl.h>
72 #define _MPRINTF_REPLACE /* use our functions only */
73 #include <curl/mprintf.h>
78 #include "arpa_telnet.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 void 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, /* disconnect */
197 PORT_TELNET, /* defport */
198 PROT_TELNET /* protocol */
204 check_wsock2 ( struct SessionHandle *data )
207 WORD wVersionRequested;
212 /* telnet requires at least WinSock 2.0 so ask for it. */
213 wVersionRequested = MAKEWORD(2, 0);
215 err = WSAStartup(wVersionRequested, &wsaData);
217 /* We must've called this once already, so this call */
218 /* should always succeed. But, just in case... */
220 failf(data,"WSAStartup failed (%d)",err);
221 return CURLE_FAILED_INIT;
224 /* We have to have a WSACleanup call for every successful */
225 /* WSAStartup call. */
228 /* Check that our version is supported */
229 if(LOBYTE(wsaData.wVersion) != LOBYTE(wVersionRequested) ||
230 HIBYTE(wsaData.wVersion) != HIBYTE(wVersionRequested)) {
231 /* Our version isn't supported */
232 failf(data,"insufficient winsock version to support "
234 return CURLE_FAILED_INIT;
237 /* Our version is supported */
243 CURLcode init_telnet(struct connectdata *conn)
247 tn = calloc(1, sizeof(struct TELNET));
249 return CURLE_OUT_OF_MEMORY;
251 conn->data->state.proto.telnet = (void *)tn; /* make us known */
253 tn->telrcv_state = CURL_TS_DATA;
255 /* Init suboptions */
258 /* Set the options we want by default */
259 tn->us_preferred[CURL_TELOPT_BINARY] = CURL_YES;
260 tn->us_preferred[CURL_TELOPT_SGA] = CURL_YES;
261 tn->him_preferred[CURL_TELOPT_BINARY] = CURL_YES;
262 tn->him_preferred[CURL_TELOPT_SGA] = CURL_YES;
267 static void negotiate(struct connectdata *conn)
270 struct TELNET *tn = (struct TELNET *) conn->data->state.proto.telnet;
272 for(i = 0;i < CURL_NTELOPTS;i++)
274 if(tn->us_preferred[i] == CURL_YES)
275 set_local_option(conn, i, CURL_YES);
277 if(tn->him_preferred[i] == CURL_YES)
278 set_remote_option(conn, i, CURL_YES);
282 #ifndef CURL_DISABLE_VERBOSE_STRINGS
283 static void printoption(struct SessionHandle *data,
284 const char *direction, int cmd, int option)
289 if(data->set.verbose)
293 if(CURL_TELCMD_OK(option))
294 infof(data, "%s IAC %s\n", direction, CURL_TELCMD(option));
296 infof(data, "%s IAC %d\n", direction, option);
300 fmt = (cmd == CURL_WILL) ? "WILL" : (cmd == CURL_WONT) ? "WONT" :
301 (cmd == CURL_DO) ? "DO" : (cmd == CURL_DONT) ? "DONT" : 0;
304 if(CURL_TELOPT_OK(option))
305 opt = CURL_TELOPT(option);
306 else if(option == CURL_TELOPT_EXOPL)
312 infof(data, "%s %s %s\n", direction, fmt, opt);
314 infof(data, "%s %s %d\n", direction, fmt, option);
317 infof(data, "%s %d %d\n", direction, cmd, option);
323 static void send_negotiation(struct connectdata *conn, int cmd, int option)
325 unsigned char buf[3];
326 ssize_t bytes_written;
328 struct SessionHandle *data = conn->data;
331 buf[1] = (unsigned char)cmd;
332 buf[2] = (unsigned char)option;
334 bytes_written = swrite(conn->sock[FIRSTSOCKET], buf, 3);
335 if(bytes_written < 0) {
337 failf(data,"Sending data failed (%d)",err);
340 printoption(conn->data, "SENT", cmd, option);
344 void set_remote_option(struct connectdata *conn, int option, int newstate)
346 struct TELNET *tn = (struct TELNET *)conn->data->state.proto.telnet;
347 if(newstate == CURL_YES)
349 switch(tn->him[option])
352 tn->him[option] = CURL_WANTYES;
353 send_negotiation(conn, CURL_DO, option);
357 /* Already enabled */
361 switch(tn->himq[option])
364 /* Already negotiating for CURL_YES, queue the request */
365 tn->himq[option] = CURL_OPPOSITE;
368 /* Error: already queued an enable request */
374 switch(tn->himq[option])
377 /* Error: already negotiating for enable */
380 tn->himq[option] = CURL_EMPTY;
388 switch(tn->him[option])
391 /* Already disabled */
395 tn->him[option] = CURL_WANTNO;
396 send_negotiation(conn, CURL_DONT, option);
400 switch(tn->himq[option])
403 /* Already negotiating for NO */
406 tn->himq[option] = CURL_EMPTY;
412 switch(tn->himq[option])
415 tn->himq[option] = CURL_OPPOSITE;
426 void rec_will(struct connectdata *conn, int option)
428 struct TELNET *tn = (struct TELNET *)conn->data->state.proto.telnet;
429 switch(tn->him[option])
432 if(tn->him_preferred[option] == CURL_YES)
434 tn->him[option] = CURL_YES;
435 send_negotiation(conn, CURL_DO, option);
439 send_negotiation(conn, CURL_DONT, option);
444 /* Already enabled */
448 switch(tn->himq[option])
451 /* Error: DONT answered by WILL */
452 tn->him[option] = CURL_NO;
455 /* Error: DONT answered by WILL */
456 tn->him[option] = CURL_YES;
457 tn->himq[option] = CURL_EMPTY;
463 switch(tn->himq[option])
466 tn->him[option] = CURL_YES;
469 tn->him[option] = CURL_WANTNO;
470 tn->himq[option] = CURL_EMPTY;
471 send_negotiation(conn, CURL_DONT, option);
479 void rec_wont(struct connectdata *conn, int option)
481 struct TELNET *tn = (struct TELNET *)conn->data->state.proto.telnet;
482 switch(tn->him[option])
485 /* Already disabled */
489 tn->him[option] = CURL_NO;
490 send_negotiation(conn, CURL_DONT, option);
494 switch(tn->himq[option])
497 tn->him[option] = CURL_NO;
501 tn->him[option] = CURL_WANTYES;
502 tn->himq[option] = CURL_EMPTY;
503 send_negotiation(conn, CURL_DO, option);
509 switch(tn->himq[option])
512 tn->him[option] = CURL_NO;
515 tn->him[option] = CURL_NO;
516 tn->himq[option] = CURL_EMPTY;
524 set_local_option(struct connectdata *conn, int option, int newstate)
526 struct TELNET *tn = (struct TELNET *)conn->data->state.proto.telnet;
527 if(newstate == CURL_YES)
529 switch(tn->us[option])
532 tn->us[option] = CURL_WANTYES;
533 send_negotiation(conn, CURL_WILL, option);
537 /* Already enabled */
541 switch(tn->usq[option])
544 /* Already negotiating for CURL_YES, queue the request */
545 tn->usq[option] = CURL_OPPOSITE;
548 /* Error: already queued an enable request */
554 switch(tn->usq[option])
557 /* Error: already negotiating for enable */
560 tn->usq[option] = CURL_EMPTY;
568 switch(tn->us[option])
571 /* Already disabled */
575 tn->us[option] = CURL_WANTNO;
576 send_negotiation(conn, CURL_WONT, option);
580 switch(tn->usq[option])
583 /* Already negotiating for NO */
586 tn->usq[option] = CURL_EMPTY;
592 switch(tn->usq[option])
595 tn->usq[option] = CURL_OPPOSITE;
606 void rec_do(struct connectdata *conn, int option)
608 struct TELNET *tn = (struct TELNET *)conn->data->state.proto.telnet;
609 switch(tn->us[option])
612 if(tn->us_preferred[option] == CURL_YES)
614 tn->us[option] = CURL_YES;
615 send_negotiation(conn, CURL_WILL, option);
619 send_negotiation(conn, CURL_WONT, option);
624 /* Already enabled */
628 switch(tn->usq[option])
631 /* Error: DONT answered by WILL */
632 tn->us[option] = CURL_NO;
635 /* Error: DONT answered by WILL */
636 tn->us[option] = CURL_YES;
637 tn->usq[option] = CURL_EMPTY;
643 switch(tn->usq[option])
646 tn->us[option] = CURL_YES;
649 tn->us[option] = CURL_WANTNO;
650 tn->himq[option] = CURL_EMPTY;
651 send_negotiation(conn, CURL_WONT, option);
659 void rec_dont(struct connectdata *conn, int option)
661 struct TELNET *tn = (struct TELNET *)conn->data->state.proto.telnet;
662 switch(tn->us[option])
665 /* Already disabled */
669 tn->us[option] = CURL_NO;
670 send_negotiation(conn, CURL_WONT, option);
674 switch(tn->usq[option])
677 tn->us[option] = CURL_NO;
681 tn->us[option] = CURL_WANTYES;
682 tn->usq[option] = CURL_EMPTY;
683 send_negotiation(conn, CURL_WILL, option);
689 switch(tn->usq[option])
692 tn->us[option] = CURL_NO;
695 tn->us[option] = CURL_NO;
696 tn->usq[option] = CURL_EMPTY;
704 static void printsub(struct SessionHandle *data,
705 int direction, /* '<' or '>' */
706 unsigned char *pointer, /* where suboption data is */
707 size_t length) /* length of suboption data */
711 if(data->set.verbose)
715 infof(data, "%s IAC SB ", (direction == '<')? "RCVD":"SENT");
720 i = pointer[length-2];
721 j = pointer[length-1];
723 if(i != CURL_IAC || j != CURL_SE)
725 infof(data, "(terminated by ");
726 if(CURL_TELOPT_OK(i))
727 infof(data, "%s ", CURL_TELOPT(i));
728 else if(CURL_TELCMD_OK(i))
729 infof(data, "%s ", CURL_TELCMD(i));
731 infof(data, "%d ", i);
732 if(CURL_TELOPT_OK(j))
733 infof(data, "%s", CURL_TELOPT(j));
734 else if(CURL_TELCMD_OK(j))
735 infof(data, "%s", CURL_TELCMD(j));
737 infof(data, "%d", j);
738 infof(data, ", not IAC SE!) ");
745 infof(data, "(Empty suboption?)");
749 if(CURL_TELOPT_OK(pointer[0])) {
751 case CURL_TELOPT_TTYPE:
752 case CURL_TELOPT_XDISPLOC:
753 case CURL_TELOPT_NEW_ENVIRON:
754 infof(data, "%s", CURL_TELOPT(pointer[0]));
757 infof(data, "%s (unsupported)", CURL_TELOPT(pointer[0]));
762 infof(data, "%d (unknown)", pointer[i]);
765 case CURL_TELQUAL_IS:
768 case CURL_TELQUAL_SEND:
769 infof(data, " SEND");
771 case CURL_TELQUAL_INFO:
772 infof(data, " INFO/REPLY");
774 case CURL_TELQUAL_NAME:
775 infof(data, " NAME");
780 case CURL_TELOPT_TTYPE:
781 case CURL_TELOPT_XDISPLOC:
783 infof(data, " \"%s\"", &pointer[2]);
785 case CURL_TELOPT_NEW_ENVIRON:
786 if(pointer[1] == CURL_TELQUAL_IS) {
788 for(i = 3;i < length;i++) {
790 case CURL_NEW_ENV_VAR:
793 case CURL_NEW_ENV_VALUE:
797 infof(data, "%c", pointer[i]);
804 for (i = 2; i < length; i++)
805 infof(data, " %.2x", pointer[i]);
816 static CURLcode check_telnet_options(struct connectdata *conn)
818 struct curl_slist *head;
819 char option_keyword[128];
820 char option_arg[256];
822 struct SessionHandle *data = conn->data;
823 struct TELNET *tn = (struct TELNET *)conn->data->state.proto.telnet;
825 /* Add the user name as an environment variable if it
826 was given on the command line */
827 if(conn->bits.user_passwd)
829 snprintf(option_arg, sizeof(option_arg), "USER,%s", conn->user);
830 tn->telnet_vars = curl_slist_append(tn->telnet_vars, option_arg);
832 tn->us_preferred[CURL_TELOPT_NEW_ENVIRON] = CURL_YES;
835 for(head = data->set.telnet_options; head; head=head->next) {
836 if(sscanf(head->data, "%127[^= ]%*[ =]%255s",
837 option_keyword, option_arg) == 2) {
840 if(Curl_raw_equal(option_keyword, "TTYPE")) {
841 strncpy(tn->subopt_ttype, option_arg, 31);
842 tn->subopt_ttype[31] = 0; /* String termination */
843 tn->us_preferred[CURL_TELOPT_TTYPE] = CURL_YES;
847 /* Display variable */
848 if(Curl_raw_equal(option_keyword, "XDISPLOC")) {
849 strncpy(tn->subopt_xdisploc, option_arg, 127);
850 tn->subopt_xdisploc[127] = 0; /* String termination */
851 tn->us_preferred[CURL_TELOPT_XDISPLOC] = CURL_YES;
855 /* Environment variable */
856 if(Curl_raw_equal(option_keyword, "NEW_ENV")) {
857 buf = strdup(option_arg);
859 return CURLE_OUT_OF_MEMORY;
860 tn->telnet_vars = curl_slist_append(tn->telnet_vars, buf);
861 tn->us_preferred[CURL_TELOPT_NEW_ENVIRON] = CURL_YES;
865 failf(data, "Unknown telnet option %s", head->data);
866 return CURLE_UNKNOWN_TELNET_OPTION;
868 failf(data, "Syntax error in telnet option: %s", head->data);
869 return CURLE_TELNET_OPTION_SYNTAX;
879 * Look at the sub-option buffer, and try to be helpful to the other
883 static void suboption(struct connectdata *conn)
885 struct curl_slist *v;
886 unsigned char temp[2048];
887 ssize_t bytes_written;
893 struct SessionHandle *data = conn->data;
894 struct TELNET *tn = (struct TELNET *)data->state.proto.telnet;
896 printsub(data, '<', (unsigned char *)tn->subbuffer, CURL_SB_LEN(tn)+2);
897 switch (CURL_SB_GET(tn)) {
898 case CURL_TELOPT_TTYPE:
899 len = strlen(tn->subopt_ttype) + 4 + 2;
900 snprintf((char *)temp, sizeof(temp),
901 "%c%c%c%c%s%c%c", CURL_IAC, CURL_SB, CURL_TELOPT_TTYPE,
902 CURL_TELQUAL_IS, tn->subopt_ttype, CURL_IAC, CURL_SE);
903 bytes_written = swrite(conn->sock[FIRSTSOCKET], temp, len);
904 if(bytes_written < 0) {
906 failf(data,"Sending data failed (%d)",err);
908 printsub(data, '>', &temp[2], len-2);
910 case CURL_TELOPT_XDISPLOC:
911 len = strlen(tn->subopt_xdisploc) + 4 + 2;
912 snprintf((char *)temp, sizeof(temp),
913 "%c%c%c%c%s%c%c", CURL_IAC, CURL_SB, CURL_TELOPT_XDISPLOC,
914 CURL_TELQUAL_IS, tn->subopt_xdisploc, CURL_IAC, CURL_SE);
915 bytes_written = swrite(conn->sock[FIRSTSOCKET], temp, len);
916 if(bytes_written < 0) {
918 failf(data,"Sending data failed (%d)",err);
920 printsub(data, '>', &temp[2], len-2);
922 case CURL_TELOPT_NEW_ENVIRON:
923 snprintf((char *)temp, sizeof(temp),
924 "%c%c%c%c", CURL_IAC, CURL_SB, CURL_TELOPT_NEW_ENVIRON,
928 for(v = tn->telnet_vars;v;v = v->next) {
929 tmplen = (strlen(v->data) + 1);
930 /* Add the variable only if it fits */
931 if(len + tmplen < (int)sizeof(temp)-6) {
932 sscanf(v->data, "%127[^,],%127s", varname, varval);
933 snprintf((char *)&temp[len], sizeof(temp) - len,
934 "%c%s%c%s", CURL_NEW_ENV_VAR, varname,
935 CURL_NEW_ENV_VALUE, varval);
939 snprintf((char *)&temp[len], sizeof(temp) - len,
940 "%c%c", CURL_IAC, CURL_SE);
942 bytes_written = swrite(conn->sock[FIRSTSOCKET], temp, len);
943 if(bytes_written < 0) {
945 failf(data,"Sending data failed (%d)",err);
947 printsub(data, '>', &temp[2], len-2);
954 void telrcv(struct connectdata *conn,
955 const unsigned char *inbuf, /* Data received from socket */
956 ssize_t count) /* Number of bytes received */
961 struct SessionHandle *data = conn->data;
962 struct TELNET *tn = (struct TELNET *)data->state.proto.telnet;
964 #define startskipping() \
965 if(startwrite >= 0) \
966 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)&inbuf[startwrite], in-startwrite); \
969 #define writebyte() \
973 #define bufferflush() startskipping()
979 /*infof(data,"In rcv state %d char %d\n", tn->telrcv_state, c);*/
980 switch (tn->telrcv_state)
983 tn->telrcv_state = CURL_TS_DATA;
987 break; /* Ignore \0 after CR */
995 tn->telrcv_state = CURL_TS_IAC;
1001 tn->telrcv_state = CURL_TS_CR;
1008 DEBUGASSERT(startwrite < 0);
1012 tn->telrcv_state = CURL_TS_WILL;
1015 tn->telrcv_state = CURL_TS_WONT;
1018 tn->telrcv_state = CURL_TS_DO;
1021 tn->telrcv_state = CURL_TS_DONT;
1025 tn->telrcv_state = CURL_TS_SB;
1028 tn->telrcv_state = CURL_TS_DATA;
1035 tn->telrcv_state = CURL_TS_DATA;
1036 printoption(data, "RCVD", CURL_IAC, c);
1042 printoption(data, "RCVD", CURL_WILL, c);
1043 tn->please_negotiate = 1;
1045 tn->telrcv_state = CURL_TS_DATA;
1049 printoption(data, "RCVD", CURL_WONT, c);
1050 tn->please_negotiate = 1;
1052 tn->telrcv_state = CURL_TS_DATA;
1056 printoption(data, "RCVD", CURL_DO, c);
1057 tn->please_negotiate = 1;
1059 tn->telrcv_state = CURL_TS_DATA;
1063 printoption(data, "RCVD", CURL_DONT, c);
1064 tn->please_negotiate = 1;
1066 tn->telrcv_state = CURL_TS_DATA;
1072 tn->telrcv_state = CURL_TS_SE;
1076 CURL_SB_ACCUM(tn,c);
1086 * This is an error. We only expect to get "IAC IAC" or "IAC SE".
1087 * Several things may have happend. An IAC was not doubled, the
1088 * IAC SE was left off, or another option got inserted into the
1089 * suboption are all possibilities. If we assume that the IAC was
1090 * not doubled, and really the IAC SE was left off, we could get
1091 * into an infinate loop here. So, instead, we terminate the
1092 * suboption, and process the partial suboption if we can.
1094 CURL_SB_ACCUM(tn, CURL_IAC);
1095 CURL_SB_ACCUM(tn, c);
1096 tn->subpointer -= 2;
1099 printoption(data, "In SUBOPTION processing, RCVD", CURL_IAC, c);
1100 suboption(conn); /* handle sub-option */
1101 tn->telrcv_state = CURL_TS_IAC;
1104 CURL_SB_ACCUM(tn,c);
1105 tn->telrcv_state = CURL_TS_SB;
1109 CURL_SB_ACCUM(tn, CURL_IAC);
1110 CURL_SB_ACCUM(tn, CURL_SE);
1111 tn->subpointer -= 2;
1113 suboption(conn); /* handle sub-option */
1114 tn->telrcv_state = CURL_TS_DATA;
1123 /* Escape and send a telnet data block */
1124 /* TODO: write large chunks of data instead of one byte at a time */
1125 static CURLcode send_telnet_data(struct connectdata *conn,
1126 char *buffer, ssize_t nread)
1128 unsigned char outbuf[2];
1129 ssize_t bytes_written, total_written;
1131 CURLcode rc = CURLE_OK;
1133 while(rc == CURLE_OK && nread--) {
1134 outbuf[0] = *buffer++;
1136 if(outbuf[0] == CURL_IAC)
1137 outbuf[out_count++] = CURL_IAC;
1141 /* Make sure socket is writable to avoid EWOULDBLOCK condition */
1142 struct pollfd pfd[1];
1143 pfd[0].fd = conn->sock[FIRSTSOCKET];
1144 pfd[0].events = POLLOUT;
1145 switch (Curl_poll(pfd, 1, -1)) {
1146 case -1: /* error, abort writing */
1147 case 0: /* timeout (will never happen) */
1148 rc = CURLE_SEND_ERROR;
1150 default: /* write! */
1152 rc = Curl_write(conn, conn->sock[FIRSTSOCKET], outbuf+total_written,
1153 out_count-total_written, &bytes_written);
1154 total_written += bytes_written;
1157 /* handle partial write */
1158 } while (rc == CURLE_OK && total_written < out_count);
1163 static CURLcode telnet_done(struct connectdata *conn,
1164 CURLcode status, bool premature)
1166 struct TELNET *tn = (struct TELNET *)conn->data->state.proto.telnet;
1167 (void)status; /* unused */
1168 (void)premature; /* not used */
1170 curl_slist_free_all(tn->telnet_vars);
1172 free(conn->data->state.proto.telnet);
1173 conn->data->state.proto.telnet = NULL;
1178 static CURLcode telnet_do(struct connectdata *conn, bool *done)
1181 struct SessionHandle *data = conn->data;
1182 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
1185 WSOCK2_FUNC close_event_func;
1186 WSOCK2_FUNC create_event_func;
1187 WSOCK2_FUNC event_select_func;
1188 WSOCK2_FUNC enum_netevents_func;
1189 WSAEVENT event_handle;
1190 WSANETWORKEVENTS events;
1191 HANDLE stdin_handle;
1196 DWORD readfile_read;
1199 struct pollfd pfd[2];
1203 char *buf = data->state.buffer;
1206 *done = TRUE; /* unconditionally */
1208 code = init_telnet(conn);
1212 tn = (struct TELNET *)data->state.proto.telnet;
1214 code = check_telnet_options(conn);
1220 ** This functionality only works with WinSock >= 2.0. So,
1221 ** make sure have it.
1223 code = check_wsock2(data);
1227 /* OK, so we have WinSock 2.0. We need to dynamically */
1228 /* load ws2_32.dll and get the function pointers we need. */
1229 wsock2 = LoadLibrary("WS2_32.DLL");
1230 if(wsock2 == NULL) {
1231 failf(data,"failed to load WS2_32.DLL (%d)", ERRNO);
1232 return CURLE_FAILED_INIT;
1235 /* Grab a pointer to WSACreateEvent */
1236 create_event_func = GetProcAddress(wsock2,"WSACreateEvent");
1237 if(create_event_func == NULL) {
1238 failf(data,"failed to find WSACreateEvent function (%d)",
1240 FreeLibrary(wsock2);
1241 return CURLE_FAILED_INIT;
1244 /* And WSACloseEvent */
1245 close_event_func = GetProcAddress(wsock2,"WSACloseEvent");
1246 if(close_event_func == NULL) {
1247 failf(data,"failed to find WSACloseEvent function (%d)",
1249 FreeLibrary(wsock2);
1250 return CURLE_FAILED_INIT;
1253 /* And WSAEventSelect */
1254 event_select_func = GetProcAddress(wsock2,"WSAEventSelect");
1255 if(event_select_func == NULL) {
1256 failf(data,"failed to find WSAEventSelect function (%d)",
1258 FreeLibrary(wsock2);
1259 return CURLE_FAILED_INIT;
1262 /* And WSAEnumNetworkEvents */
1263 enum_netevents_func = GetProcAddress(wsock2,"WSAEnumNetworkEvents");
1264 if(enum_netevents_func == NULL) {
1265 failf(data,"failed to find WSAEnumNetworkEvents function (%d)",
1267 FreeLibrary(wsock2);
1268 return CURLE_FAILED_INIT;
1271 /* We want to wait for both stdin and the socket. Since
1272 ** the select() function in winsock only works on sockets
1273 ** we have to use the WaitForMultipleObjects() call.
1276 /* First, create a sockets event object */
1277 event_handle = (WSAEVENT)create_event_func();
1278 if(event_handle == WSA_INVALID_EVENT) {
1279 failf(data,"WSACreateEvent failed (%d)", SOCKERRNO);
1280 FreeLibrary(wsock2);
1281 return CURLE_FAILED_INIT;
1284 /* The get the Windows file handle for stdin */
1285 stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
1287 /* Create the list of objects to wait for */
1288 objs[0] = event_handle;
1289 objs[1] = stdin_handle;
1291 /* Tell winsock what events we want to listen to */
1292 if(event_select_func(sockfd, event_handle, FD_READ|FD_CLOSE) == SOCKET_ERROR) {
1293 close_event_func(event_handle);
1294 FreeLibrary(wsock2);
1298 /* If stdin_handle is a pipe, use PeekNamedPipe() method to check it,
1299 else use the old WaitForMultipleObjects() way */
1300 if(GetFileType(stdin_handle) == FILE_TYPE_PIPE) {
1301 /* Don't wait for stdin_handle, just wait for event_handle */
1303 /* Check stdin_handle per 100 milliseconds */
1307 wait_timeout = INFINITE;
1310 /* Keep on listening and act on events */
1312 waitret = WaitForMultipleObjects(obj_count, objs, FALSE, wait_timeout);
1317 if(!PeekNamedPipe(stdin_handle, NULL, 0, NULL, &readfile_read, NULL)) {
1319 code = CURLE_READ_ERROR;
1326 if(!ReadFile(stdin_handle, buf, sizeof(data->state.buffer),
1327 &readfile_read, NULL)) {
1329 code = CURLE_READ_ERROR;
1333 code = send_telnet_data(conn, buf, readfile_read);
1342 case WAIT_OBJECT_0 + 1:
1344 if(!ReadFile(stdin_handle, buf, sizeof(data->state.buffer),
1345 &readfile_read, NULL)) {
1347 code = CURLE_READ_ERROR;
1351 code = send_telnet_data(conn, buf, readfile_read);
1360 if(enum_netevents_func(sockfd, event_handle, &events)
1362 if(events.lNetworkEvents & FD_READ) {
1363 /* This reallu OUGHT to check its return code. */
1364 (void)Curl_read(conn, sockfd, buf, BUFSIZE - 1, &nread);
1366 telrcv(conn, (unsigned char *)buf, nread);
1370 /* Negotiate if the peer has started negotiating,
1371 otherwise don't. We don't want to speak telnet with
1372 non-telnet servers, like POP or SMTP. */
1373 if(tn->please_negotiate && !tn->already_negotiated) {
1375 tn->already_negotiated = 1;
1379 if(events.lNetworkEvents & FD_CLOSE) {
1387 /* We called WSACreateEvent, so call WSACloseEvent */
1388 if(close_event_func(event_handle) == FALSE) {
1389 infof(data,"WSACloseEvent failed (%d)", SOCKERRNO);
1392 /* "Forget" pointers into the library we're about to free */
1393 create_event_func = NULL;
1394 close_event_func = NULL;
1395 event_select_func = NULL;
1396 enum_netevents_func = NULL;
1398 /* We called LoadLibrary, so call FreeLibrary */
1399 if(!FreeLibrary(wsock2))
1400 infof(data,"FreeLibrary(wsock2) failed (%d)", ERRNO);
1403 pfd[0].events = POLLIN;
1405 pfd[1].events = POLLIN;
1406 interval_ms = 1 * 1000;
1409 switch (Curl_poll(pfd, 2, interval_ms)) {
1410 case -1: /* error, stop reading */
1413 case 0: /* timeout */
1415 default: /* read! */
1416 if(pfd[1].revents & POLLIN) { /* read from stdin */
1417 nread = read(0, buf, 255);
1418 code = send_telnet_data(conn, buf, nread);
1425 if(pfd[0].revents & POLLIN) {
1426 /* This OUGHT to check the return code... */
1427 (void)Curl_read(conn, sockfd, buf, BUFSIZE - 1, &nread);
1429 /* if we receive 0 or less here, the server closed the connection and
1430 we bail out from this! */
1436 telrcv(conn, (unsigned char *)buf, nread);
1438 /* Negotiate if the peer has started negotiating,
1439 otherwise don't. We don't want to speak telnet with
1440 non-telnet servers, like POP or SMTP. */
1441 if(tn->please_negotiate && !tn->already_negotiated) {
1443 tn->already_negotiated = 1;
1447 if(data->set.timeout) {
1448 struct timeval now; /* current time */
1450 if(Curl_tvdiff(now, conn->created) >= data->set.timeout) {
1451 failf(data, "Time-out");
1452 code = CURLE_OPERATION_TIMEDOUT;
1458 /* mark this as "no further transfer wanted" */
1459 Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);