1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2011, 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
27 #ifdef HAVE_SYS_SOCKET_H
28 #include <sys/socket.h>
30 #ifdef HAVE_NETINET_IN_H
31 #include <netinet/in.h>
39 #ifdef HAVE_ARPA_INET_H
40 #include <arpa/inet.h>
45 #ifdef HAVE_SYS_IOCTL_H
46 #include <sys/ioctl.h>
49 #ifdef HAVE_SYS_PARAM_H
50 #include <sys/param.h>
54 #include <curl/curl.h>
61 #define _MPRINTF_REPLACE /* use our functions only */
62 #include <curl/mprintf.h>
67 #include "arpa_telnet.h"
68 #include "curl_memory.h"
73 /* The last #include file should be: */
76 #define SUBBUFSIZE 512
78 #define CURL_SB_CLEAR(x) x->subpointer = x->subbuffer;
79 #define CURL_SB_TERM(x) { x->subend = x->subpointer; CURL_SB_CLEAR(x); }
80 #define CURL_SB_ACCUM(x,c) \
81 if(x->subpointer < (x->subbuffer+sizeof x->subbuffer)) { \
82 *x->subpointer++ = (c); \
85 #define CURL_SB_GET(x) ((*x->subpointer++)&0xff)
86 #define CURL_SB_PEEK(x) ((*x->subpointer)&0xff)
87 #define CURL_SB_EOF(x) (x->subpointer >= x->subend)
88 #define CURL_SB_LEN(x) (x->subend - x->subpointer)
90 #ifdef CURL_DISABLE_VERBOSE_STRINGS
91 #define printoption(a,b,c,d) do { } while(0)
95 typedef FARPROC WSOCK2_FUNC;
96 static CURLcode check_wsock2 ( struct SessionHandle *data );
100 CURLcode telrcv(struct connectdata *,
101 const unsigned char *inbuf, /* Data received from socket */
102 ssize_t count); /* Number of bytes received */
104 #ifndef CURL_DISABLE_VERBOSE_STRINGS
105 static void printoption(struct SessionHandle *data,
106 const char *direction,
107 int cmd, int option);
110 static void negotiate(struct connectdata *);
111 static void send_negotiation(struct connectdata *, int cmd, int option);
112 static void set_local_option(struct connectdata *, int cmd, int option);
113 static void set_remote_option(struct connectdata *, int cmd, int option);
115 static void printsub(struct SessionHandle *data,
116 int direction, unsigned char *pointer,
118 static void suboption(struct connectdata *);
120 static CURLcode telnet_do(struct connectdata *conn, bool *done);
121 static CURLcode telnet_done(struct connectdata *conn,
122 CURLcode, bool premature);
124 /* For negotiation compliant to RFC 1143 */
127 #define CURL_WANTYES 2
128 #define CURL_WANTNO 3
131 #define CURL_OPPOSITE 1
134 * Telnet receiver states for fsm
145 CURL_TS_SB, /* sub-option collection */
146 CURL_TS_SE /* looking for sub-option end */
150 int please_negotiate;
151 int already_negotiated;
154 int us_preferred[256];
157 int him_preferred[256];
158 char subopt_ttype[32]; /* Set with suboption TTYPE */
159 char subopt_xdisploc[128]; /* Set with suboption XDISPLOC */
160 struct curl_slist *telnet_vars; /* Environment variables */
163 unsigned char subbuffer[SUBBUFSIZE];
164 unsigned char *subpointer, *subend; /* buffer for sub-options */
166 TelnetReceive telrcv_state;
171 * TELNET protocol handler.
174 const struct Curl_handler Curl_handler_telnet = {
175 "TELNET", /* scheme */
176 ZERO_NULL, /* setup_connection */
177 telnet_do, /* do_it */
178 telnet_done, /* done */
179 ZERO_NULL, /* do_more */
180 ZERO_NULL, /* connect_it */
181 ZERO_NULL, /* connecting */
182 ZERO_NULL, /* doing */
183 ZERO_NULL, /* proto_getsock */
184 ZERO_NULL, /* doing_getsock */
185 ZERO_NULL, /* perform_getsock */
186 ZERO_NULL, /* disconnect */
187 ZERO_NULL, /* readwrite */
188 PORT_TELNET, /* defport */
189 CURLPROTO_TELNET, /* protocol */
190 PROTOPT_NONE /* flags */
196 check_wsock2 ( struct SessionHandle *data )
199 WORD wVersionRequested;
204 /* telnet requires at least WinSock 2.0 so ask for it. */
205 wVersionRequested = MAKEWORD(2, 0);
207 err = WSAStartup(wVersionRequested, &wsaData);
209 /* We must've called this once already, so this call */
210 /* should always succeed. But, just in case... */
212 failf(data,"WSAStartup failed (%d)",err);
213 return CURLE_FAILED_INIT;
216 /* We have to have a WSACleanup call for every successful */
217 /* WSAStartup call. */
220 /* Check that our version is supported */
221 if(LOBYTE(wsaData.wVersion) != LOBYTE(wVersionRequested) ||
222 HIBYTE(wsaData.wVersion) != HIBYTE(wVersionRequested)) {
223 /* Our version isn't supported */
224 failf(data,"insufficient winsock version to support "
226 return CURLE_FAILED_INIT;
229 /* Our version is supported */
235 CURLcode init_telnet(struct connectdata *conn)
239 tn = calloc(1, sizeof(struct TELNET));
241 return CURLE_OUT_OF_MEMORY;
243 conn->data->state.proto.telnet = (void *)tn; /* make us known */
245 tn->telrcv_state = CURL_TS_DATA;
247 /* Init suboptions */
250 /* Set the options we want by default */
251 tn->us_preferred[CURL_TELOPT_BINARY] = CURL_YES;
252 tn->us_preferred[CURL_TELOPT_SGA] = CURL_YES;
253 tn->him_preferred[CURL_TELOPT_BINARY] = CURL_YES;
254 tn->him_preferred[CURL_TELOPT_SGA] = CURL_YES;
259 static void negotiate(struct connectdata *conn)
262 struct TELNET *tn = (struct TELNET *) conn->data->state.proto.telnet;
264 for(i = 0;i < CURL_NTELOPTS;i++) {
265 if(tn->us_preferred[i] == CURL_YES)
266 set_local_option(conn, i, CURL_YES);
268 if(tn->him_preferred[i] == CURL_YES)
269 set_remote_option(conn, i, CURL_YES);
273 #ifndef CURL_DISABLE_VERBOSE_STRINGS
274 static void printoption(struct SessionHandle *data,
275 const char *direction, int cmd, int option)
280 if(data->set.verbose) {
281 if(cmd == CURL_IAC) {
282 if(CURL_TELCMD_OK(option))
283 infof(data, "%s IAC %s\n", direction, CURL_TELCMD(option));
285 infof(data, "%s IAC %d\n", direction, option);
288 fmt = (cmd == CURL_WILL) ? "WILL" : (cmd == CURL_WONT) ? "WONT" :
289 (cmd == CURL_DO) ? "DO" : (cmd == CURL_DONT) ? "DONT" : 0;
291 if(CURL_TELOPT_OK(option))
292 opt = CURL_TELOPT(option);
293 else if(option == CURL_TELOPT_EXOPL)
299 infof(data, "%s %s %s\n", direction, fmt, opt);
301 infof(data, "%s %s %d\n", direction, fmt, option);
304 infof(data, "%s %d %d\n", direction, cmd, option);
310 static void send_negotiation(struct connectdata *conn, int cmd, int option)
312 unsigned char buf[3];
313 ssize_t bytes_written;
315 struct SessionHandle *data = conn->data;
318 buf[1] = (unsigned char)cmd;
319 buf[2] = (unsigned char)option;
321 bytes_written = swrite(conn->sock[FIRSTSOCKET], buf, 3);
322 if(bytes_written < 0) {
324 failf(data,"Sending data failed (%d)",err);
327 printoption(conn->data, "SENT", cmd, option);
331 void set_remote_option(struct connectdata *conn, int option, int newstate)
333 struct TELNET *tn = (struct TELNET *)conn->data->state.proto.telnet;
334 if(newstate == CURL_YES) {
335 switch(tn->him[option]) {
337 tn->him[option] = CURL_WANTYES;
338 send_negotiation(conn, CURL_DO, option);
342 /* Already enabled */
346 switch(tn->himq[option]) {
348 /* Already negotiating for CURL_YES, queue the request */
349 tn->himq[option] = CURL_OPPOSITE;
352 /* Error: already queued an enable request */
358 switch(tn->himq[option]) {
360 /* Error: already negotiating for enable */
363 tn->himq[option] = CURL_EMPTY;
370 switch(tn->him[option]) {
372 /* Already disabled */
376 tn->him[option] = CURL_WANTNO;
377 send_negotiation(conn, CURL_DONT, option);
381 switch(tn->himq[option]) {
383 /* Already negotiating for NO */
386 tn->himq[option] = CURL_EMPTY;
392 switch(tn->himq[option]) {
394 tn->himq[option] = CURL_OPPOSITE;
405 void rec_will(struct connectdata *conn, int option)
407 struct TELNET *tn = (struct TELNET *)conn->data->state.proto.telnet;
408 switch(tn->him[option]) {
410 if(tn->him_preferred[option] == CURL_YES) {
411 tn->him[option] = CURL_YES;
412 send_negotiation(conn, CURL_DO, option);
415 send_negotiation(conn, CURL_DONT, option);
420 /* Already enabled */
424 switch(tn->himq[option]) {
426 /* Error: DONT answered by WILL */
427 tn->him[option] = CURL_NO;
430 /* Error: DONT answered by WILL */
431 tn->him[option] = CURL_YES;
432 tn->himq[option] = CURL_EMPTY;
438 switch(tn->himq[option]) {
440 tn->him[option] = CURL_YES;
443 tn->him[option] = CURL_WANTNO;
444 tn->himq[option] = CURL_EMPTY;
445 send_negotiation(conn, CURL_DONT, option);
453 void rec_wont(struct connectdata *conn, int option)
455 struct TELNET *tn = (struct TELNET *)conn->data->state.proto.telnet;
456 switch(tn->him[option]) {
458 /* Already disabled */
462 tn->him[option] = CURL_NO;
463 send_negotiation(conn, CURL_DONT, option);
467 switch(tn->himq[option]) {
469 tn->him[option] = CURL_NO;
473 tn->him[option] = CURL_WANTYES;
474 tn->himq[option] = CURL_EMPTY;
475 send_negotiation(conn, CURL_DO, option);
481 switch(tn->himq[option]) {
483 tn->him[option] = CURL_NO;
486 tn->him[option] = CURL_NO;
487 tn->himq[option] = CURL_EMPTY;
495 set_local_option(struct connectdata *conn, int option, int newstate)
497 struct TELNET *tn = (struct TELNET *)conn->data->state.proto.telnet;
498 if(newstate == CURL_YES) {
499 switch(tn->us[option]) {
501 tn->us[option] = CURL_WANTYES;
502 send_negotiation(conn, CURL_WILL, option);
506 /* Already enabled */
510 switch(tn->usq[option]) {
512 /* Already negotiating for CURL_YES, queue the request */
513 tn->usq[option] = CURL_OPPOSITE;
516 /* Error: already queued an enable request */
522 switch(tn->usq[option]) {
524 /* Error: already negotiating for enable */
527 tn->usq[option] = CURL_EMPTY;
534 switch(tn->us[option]) {
536 /* Already disabled */
540 tn->us[option] = CURL_WANTNO;
541 send_negotiation(conn, CURL_WONT, option);
545 switch(tn->usq[option]) {
547 /* Already negotiating for NO */
550 tn->usq[option] = CURL_EMPTY;
556 switch(tn->usq[option]) {
558 tn->usq[option] = CURL_OPPOSITE;
569 void rec_do(struct connectdata *conn, int option)
571 struct TELNET *tn = (struct TELNET *)conn->data->state.proto.telnet;
572 switch(tn->us[option]) {
574 if(tn->us_preferred[option] == CURL_YES) {
575 tn->us[option] = CURL_YES;
576 send_negotiation(conn, CURL_WILL, option);
579 send_negotiation(conn, CURL_WONT, option);
583 /* Already enabled */
587 switch(tn->usq[option]) {
589 /* Error: DONT answered by WILL */
590 tn->us[option] = CURL_NO;
593 /* Error: DONT answered by WILL */
594 tn->us[option] = CURL_YES;
595 tn->usq[option] = CURL_EMPTY;
601 switch(tn->usq[option]) {
603 tn->us[option] = CURL_YES;
606 tn->us[option] = CURL_WANTNO;
607 tn->himq[option] = CURL_EMPTY;
608 send_negotiation(conn, CURL_WONT, option);
616 void rec_dont(struct connectdata *conn, int option)
618 struct TELNET *tn = (struct TELNET *)conn->data->state.proto.telnet;
619 switch(tn->us[option]) {
621 /* Already disabled */
625 tn->us[option] = CURL_NO;
626 send_negotiation(conn, CURL_WONT, option);
630 switch(tn->usq[option]) {
632 tn->us[option] = CURL_NO;
636 tn->us[option] = CURL_WANTYES;
637 tn->usq[option] = CURL_EMPTY;
638 send_negotiation(conn, CURL_WILL, option);
644 switch(tn->usq[option]) {
646 tn->us[option] = CURL_NO;
649 tn->us[option] = CURL_NO;
650 tn->usq[option] = CURL_EMPTY;
658 static void printsub(struct SessionHandle *data,
659 int direction, /* '<' or '>' */
660 unsigned char *pointer, /* where suboption data is */
661 size_t length) /* length of suboption data */
665 if(data->set.verbose) {
667 infof(data, "%s IAC SB ", (direction == '<')? "RCVD":"SENT");
671 i = pointer[length-2];
672 j = pointer[length-1];
674 if(i != CURL_IAC || j != CURL_SE) {
675 infof(data, "(terminated by ");
676 if(CURL_TELOPT_OK(i))
677 infof(data, "%s ", CURL_TELOPT(i));
678 else if(CURL_TELCMD_OK(i))
679 infof(data, "%s ", CURL_TELCMD(i));
681 infof(data, "%u ", i);
682 if(CURL_TELOPT_OK(j))
683 infof(data, "%s", CURL_TELOPT(j));
684 else if(CURL_TELCMD_OK(j))
685 infof(data, "%s", CURL_TELCMD(j));
687 infof(data, "%d", j);
688 infof(data, ", not IAC SE!) ");
694 infof(data, "(Empty suboption?)");
698 if(CURL_TELOPT_OK(pointer[0])) {
700 case CURL_TELOPT_TTYPE:
701 case CURL_TELOPT_XDISPLOC:
702 case CURL_TELOPT_NEW_ENVIRON:
703 infof(data, "%s", CURL_TELOPT(pointer[0]));
706 infof(data, "%s (unsupported)", CURL_TELOPT(pointer[0]));
711 infof(data, "%d (unknown)", pointer[i]);
714 case CURL_TELQUAL_IS:
717 case CURL_TELQUAL_SEND:
718 infof(data, " SEND");
720 case CURL_TELQUAL_INFO:
721 infof(data, " INFO/REPLY");
723 case CURL_TELQUAL_NAME:
724 infof(data, " NAME");
729 case CURL_TELOPT_TTYPE:
730 case CURL_TELOPT_XDISPLOC:
732 infof(data, " \"%s\"", &pointer[2]);
734 case CURL_TELOPT_NEW_ENVIRON:
735 if(pointer[1] == CURL_TELQUAL_IS) {
737 for(i = 3;i < length;i++) {
739 case CURL_NEW_ENV_VAR:
742 case CURL_NEW_ENV_VALUE:
746 infof(data, "%c", pointer[i]);
753 for(i = 2; i < length; i++)
754 infof(data, " %.2x", pointer[i]);
763 static CURLcode check_telnet_options(struct connectdata *conn)
765 struct curl_slist *head;
766 char option_keyword[128];
767 char option_arg[256];
769 struct SessionHandle *data = conn->data;
770 struct TELNET *tn = (struct TELNET *)conn->data->state.proto.telnet;
772 /* Add the user name as an environment variable if it
773 was given on the command line */
774 if(conn->bits.user_passwd) {
775 snprintf(option_arg, sizeof(option_arg), "USER,%s", conn->user);
776 tn->telnet_vars = curl_slist_append(tn->telnet_vars, option_arg);
778 tn->us_preferred[CURL_TELOPT_NEW_ENVIRON] = CURL_YES;
781 for(head = data->set.telnet_options; head; head=head->next) {
782 if(sscanf(head->data, "%127[^= ]%*[ =]%255s",
783 option_keyword, option_arg) == 2) {
786 if(Curl_raw_equal(option_keyword, "TTYPE")) {
787 strncpy(tn->subopt_ttype, option_arg, 31);
788 tn->subopt_ttype[31] = 0; /* String termination */
789 tn->us_preferred[CURL_TELOPT_TTYPE] = CURL_YES;
793 /* Display variable */
794 if(Curl_raw_equal(option_keyword, "XDISPLOC")) {
795 strncpy(tn->subopt_xdisploc, option_arg, 127);
796 tn->subopt_xdisploc[127] = 0; /* String termination */
797 tn->us_preferred[CURL_TELOPT_XDISPLOC] = CURL_YES;
801 /* Environment variable */
802 if(Curl_raw_equal(option_keyword, "NEW_ENV")) {
803 buf = strdup(option_arg);
805 return CURLE_OUT_OF_MEMORY;
806 tn->telnet_vars = curl_slist_append(tn->telnet_vars, buf);
807 tn->us_preferred[CURL_TELOPT_NEW_ENVIRON] = CURL_YES;
811 failf(data, "Unknown telnet option %s", head->data);
812 return CURLE_UNKNOWN_TELNET_OPTION;
815 failf(data, "Syntax error in telnet option: %s", head->data);
816 return CURLE_TELNET_OPTION_SYNTAX;
826 * Look at the sub-option buffer, and try to be helpful to the other
830 static void suboption(struct connectdata *conn)
832 struct curl_slist *v;
833 unsigned char temp[2048];
834 ssize_t bytes_written;
840 struct SessionHandle *data = conn->data;
841 struct TELNET *tn = (struct TELNET *)data->state.proto.telnet;
843 printsub(data, '<', (unsigned char *)tn->subbuffer, CURL_SB_LEN(tn)+2);
844 switch (CURL_SB_GET(tn)) {
845 case CURL_TELOPT_TTYPE:
846 len = strlen(tn->subopt_ttype) + 4 + 2;
847 snprintf((char *)temp, sizeof(temp),
848 "%c%c%c%c%s%c%c", CURL_IAC, CURL_SB, CURL_TELOPT_TTYPE,
849 CURL_TELQUAL_IS, tn->subopt_ttype, CURL_IAC, CURL_SE);
850 bytes_written = swrite(conn->sock[FIRSTSOCKET], temp, len);
851 if(bytes_written < 0) {
853 failf(data,"Sending data failed (%d)",err);
855 printsub(data, '>', &temp[2], len-2);
857 case CURL_TELOPT_XDISPLOC:
858 len = strlen(tn->subopt_xdisploc) + 4 + 2;
859 snprintf((char *)temp, sizeof(temp),
860 "%c%c%c%c%s%c%c", CURL_IAC, CURL_SB, CURL_TELOPT_XDISPLOC,
861 CURL_TELQUAL_IS, tn->subopt_xdisploc, CURL_IAC, CURL_SE);
862 bytes_written = swrite(conn->sock[FIRSTSOCKET], temp, len);
863 if(bytes_written < 0) {
865 failf(data,"Sending data failed (%d)",err);
867 printsub(data, '>', &temp[2], len-2);
869 case CURL_TELOPT_NEW_ENVIRON:
870 snprintf((char *)temp, sizeof(temp),
871 "%c%c%c%c", CURL_IAC, CURL_SB, CURL_TELOPT_NEW_ENVIRON,
875 for(v = tn->telnet_vars;v;v = v->next) {
876 tmplen = (strlen(v->data) + 1);
877 /* Add the variable only if it fits */
878 if(len + tmplen < (int)sizeof(temp)-6) {
879 sscanf(v->data, "%127[^,],%127s", varname, varval);
880 snprintf((char *)&temp[len], sizeof(temp) - len,
881 "%c%s%c%s", CURL_NEW_ENV_VAR, varname,
882 CURL_NEW_ENV_VALUE, varval);
886 snprintf((char *)&temp[len], sizeof(temp) - len,
887 "%c%c", CURL_IAC, CURL_SE);
889 bytes_written = swrite(conn->sock[FIRSTSOCKET], temp, len);
890 if(bytes_written < 0) {
892 failf(data,"Sending data failed (%d)",err);
894 printsub(data, '>', &temp[2], len-2);
901 CURLcode telrcv(struct connectdata *conn,
902 const unsigned char *inbuf, /* Data received from socket */
903 ssize_t count) /* Number of bytes received */
909 struct SessionHandle *data = conn->data;
910 struct TELNET *tn = (struct TELNET *)data->state.proto.telnet;
912 #define startskipping() \
913 if(startwrite >= 0) { \
914 result = Curl_client_write(conn, \
916 (char *)&inbuf[startwrite], \
918 if(result != CURLE_OK) \
923 #define writebyte() \
927 #define bufferflush() startskipping()
932 switch (tn->telrcv_state) {
934 tn->telrcv_state = CURL_TS_DATA;
937 break; /* Ignore \0 after CR */
944 tn->telrcv_state = CURL_TS_IAC;
949 tn->telrcv_state = CURL_TS_CR;
955 DEBUGASSERT(startwrite < 0);
958 tn->telrcv_state = CURL_TS_WILL;
961 tn->telrcv_state = CURL_TS_WONT;
964 tn->telrcv_state = CURL_TS_DO;
967 tn->telrcv_state = CURL_TS_DONT;
971 tn->telrcv_state = CURL_TS_SB;
974 tn->telrcv_state = CURL_TS_DATA;
981 tn->telrcv_state = CURL_TS_DATA;
982 printoption(data, "RCVD", CURL_IAC, c);
988 printoption(data, "RCVD", CURL_WILL, c);
989 tn->please_negotiate = 1;
991 tn->telrcv_state = CURL_TS_DATA;
995 printoption(data, "RCVD", CURL_WONT, c);
996 tn->please_negotiate = 1;
998 tn->telrcv_state = CURL_TS_DATA;
1002 printoption(data, "RCVD", CURL_DO, c);
1003 tn->please_negotiate = 1;
1005 tn->telrcv_state = CURL_TS_DATA;
1009 printoption(data, "RCVD", CURL_DONT, c);
1010 tn->please_negotiate = 1;
1012 tn->telrcv_state = CURL_TS_DATA;
1017 tn->telrcv_state = CURL_TS_SE;
1019 CURL_SB_ACCUM(tn,c);
1026 * This is an error. We only expect to get "IAC IAC" or "IAC SE".
1027 * Several things may have happened. An IAC was not doubled, the
1028 * IAC SE was left off, or another option got inserted into the
1029 * suboption are all possibilities. If we assume that the IAC was
1030 * not doubled, and really the IAC SE was left off, we could get
1031 * into an infinate loop here. So, instead, we terminate the
1032 * suboption, and process the partial suboption if we can.
1034 CURL_SB_ACCUM(tn, CURL_IAC);
1035 CURL_SB_ACCUM(tn, c);
1036 tn->subpointer -= 2;
1039 printoption(data, "In SUBOPTION processing, RCVD", CURL_IAC, c);
1040 suboption(conn); /* handle sub-option */
1041 tn->telrcv_state = CURL_TS_IAC;
1044 CURL_SB_ACCUM(tn,c);
1045 tn->telrcv_state = CURL_TS_SB;
1049 CURL_SB_ACCUM(tn, CURL_IAC);
1050 CURL_SB_ACCUM(tn, CURL_SE);
1051 tn->subpointer -= 2;
1053 suboption(conn); /* handle sub-option */
1054 tn->telrcv_state = CURL_TS_DATA;
1064 /* Escape and send a telnet data block */
1065 /* TODO: write large chunks of data instead of one byte at a time */
1066 static CURLcode send_telnet_data(struct connectdata *conn,
1067 char *buffer, ssize_t nread)
1069 unsigned char outbuf[2];
1070 ssize_t bytes_written, total_written;
1072 CURLcode rc = CURLE_OK;
1074 while(rc == CURLE_OK && nread--) {
1075 outbuf[0] = *buffer++;
1077 if(outbuf[0] == CURL_IAC)
1078 outbuf[out_count++] = CURL_IAC;
1082 /* Make sure socket is writable to avoid EWOULDBLOCK condition */
1083 struct pollfd pfd[1];
1084 pfd[0].fd = conn->sock[FIRSTSOCKET];
1085 pfd[0].events = POLLOUT;
1086 switch (Curl_poll(pfd, 1, -1)) {
1087 case -1: /* error, abort writing */
1088 case 0: /* timeout (will never happen) */
1089 rc = CURLE_SEND_ERROR;
1091 default: /* write! */
1093 rc = Curl_write(conn, conn->sock[FIRSTSOCKET], outbuf+total_written,
1094 out_count-total_written, &bytes_written);
1095 total_written += bytes_written;
1098 /* handle partial write */
1099 } while(rc == CURLE_OK && total_written < out_count);
1104 static CURLcode telnet_done(struct connectdata *conn,
1105 CURLcode status, bool premature)
1107 struct TELNET *tn = (struct TELNET *)conn->data->state.proto.telnet;
1108 (void)status; /* unused */
1109 (void)premature; /* not used */
1111 curl_slist_free_all(tn->telnet_vars);
1113 free(conn->data->state.proto.telnet);
1114 conn->data->state.proto.telnet = NULL;
1119 static CURLcode telnet_do(struct connectdata *conn, bool *done)
1122 struct SessionHandle *data = conn->data;
1123 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
1126 WSOCK2_FUNC close_event_func;
1127 WSOCK2_FUNC create_event_func;
1128 WSOCK2_FUNC event_select_func;
1129 WSOCK2_FUNC enum_netevents_func;
1130 WSAEVENT event_handle;
1131 WSANETWORKEVENTS events;
1132 HANDLE stdin_handle;
1137 DWORD readfile_read;
1141 struct pollfd pfd[2];
1143 curl_off_t total_dl = 0;
1144 curl_off_t total_ul = 0;
1149 char *buf = data->state.buffer;
1152 *done = TRUE; /* unconditionally */
1154 code = init_telnet(conn);
1158 tn = (struct TELNET *)data->state.proto.telnet;
1160 code = check_telnet_options(conn);
1166 ** This functionality only works with WinSock >= 2.0. So,
1167 ** make sure have it.
1169 code = check_wsock2(data);
1173 /* OK, so we have WinSock 2.0. We need to dynamically */
1174 /* load ws2_32.dll and get the function pointers we need. */
1175 wsock2 = LoadLibrary("WS2_32.DLL");
1176 if(wsock2 == NULL) {
1177 failf(data,"failed to load WS2_32.DLL (%d)", ERRNO);
1178 return CURLE_FAILED_INIT;
1181 /* Grab a pointer to WSACreateEvent */
1182 create_event_func = GetProcAddress(wsock2,"WSACreateEvent");
1183 if(create_event_func == NULL) {
1184 failf(data,"failed to find WSACreateEvent function (%d)",
1186 FreeLibrary(wsock2);
1187 return CURLE_FAILED_INIT;
1190 /* And WSACloseEvent */
1191 close_event_func = GetProcAddress(wsock2,"WSACloseEvent");
1192 if(close_event_func == NULL) {
1193 failf(data,"failed to find WSACloseEvent function (%d)",
1195 FreeLibrary(wsock2);
1196 return CURLE_FAILED_INIT;
1199 /* And WSAEventSelect */
1200 event_select_func = GetProcAddress(wsock2,"WSAEventSelect");
1201 if(event_select_func == NULL) {
1202 failf(data,"failed to find WSAEventSelect function (%d)",
1204 FreeLibrary(wsock2);
1205 return CURLE_FAILED_INIT;
1208 /* And WSAEnumNetworkEvents */
1209 enum_netevents_func = GetProcAddress(wsock2,"WSAEnumNetworkEvents");
1210 if(enum_netevents_func == NULL) {
1211 failf(data,"failed to find WSAEnumNetworkEvents function (%d)",
1213 FreeLibrary(wsock2);
1214 return CURLE_FAILED_INIT;
1217 /* We want to wait for both stdin and the socket. Since
1218 ** the select() function in winsock only works on sockets
1219 ** we have to use the WaitForMultipleObjects() call.
1222 /* First, create a sockets event object */
1223 event_handle = (WSAEVENT)create_event_func();
1224 if(event_handle == WSA_INVALID_EVENT) {
1225 failf(data,"WSACreateEvent failed (%d)", SOCKERRNO);
1226 FreeLibrary(wsock2);
1227 return CURLE_FAILED_INIT;
1230 /* The get the Windows file handle for stdin */
1231 stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
1233 /* Create the list of objects to wait for */
1234 objs[0] = event_handle;
1235 objs[1] = stdin_handle;
1237 /* Tell winsock what events we want to listen to */
1238 if(event_select_func(sockfd, event_handle, FD_READ|FD_CLOSE) ==
1240 close_event_func(event_handle);
1241 FreeLibrary(wsock2);
1245 /* If stdin_handle is a pipe, use PeekNamedPipe() method to check it,
1246 else use the old WaitForMultipleObjects() way */
1247 if(GetFileType(stdin_handle) == FILE_TYPE_PIPE) {
1248 /* Don't wait for stdin_handle, just wait for event_handle */
1250 /* Check stdin_handle per 100 milliseconds */
1255 wait_timeout = 1000;
1258 /* Keep on listening and act on events */
1260 waitret = WaitForMultipleObjects(obj_count, objs, FALSE, wait_timeout);
1265 if(!PeekNamedPipe(stdin_handle, NULL, 0, NULL, &readfile_read, NULL)) {
1267 code = CURLE_READ_ERROR;
1274 if(!ReadFile(stdin_handle, buf, sizeof(data->state.buffer),
1275 &readfile_read, NULL)) {
1277 code = CURLE_READ_ERROR;
1281 code = send_telnet_data(conn, buf, readfile_read);
1290 case WAIT_OBJECT_0 + 1:
1292 if(!ReadFile(stdin_handle, buf, sizeof(data->state.buffer),
1293 &readfile_read, NULL)) {
1295 code = CURLE_READ_ERROR;
1299 code = send_telnet_data(conn, buf, readfile_read);
1309 if(SOCKET_ERROR == enum_netevents_func(sockfd, event_handle, &events)) {
1310 if((err = SOCKERRNO) != EINPROGRESS) {
1311 infof(data,"WSAEnumNetworkEvents failed (%d)", err);
1313 code = CURLE_READ_ERROR;
1317 if(events.lNetworkEvents & FD_READ) {
1318 /* read data from network */
1319 code = Curl_read(conn, sockfd, buf, BUFSIZE - 1, &nread);
1320 /* read would've blocked. Loop again */
1321 if(code == CURLE_AGAIN)
1323 /* returned not-zero, this an error */
1328 /* returned zero but actually received 0 or less here,
1329 the server closed the connection and we bail out */
1330 else if(nread <= 0) {
1335 code = telrcv(conn, (unsigned char *)buf, nread);
1341 /* Negotiate if the peer has started negotiating,
1342 otherwise don't. We don't want to speak telnet with
1343 non-telnet servers, like POP or SMTP. */
1344 if(tn->please_negotiate && !tn->already_negotiated) {
1346 tn->already_negotiated = 1;
1349 if(events.lNetworkEvents & FD_CLOSE) {
1356 if(data->set.timeout) {
1358 if(Curl_tvdiff(now, conn->created) >= data->set.timeout) {
1359 failf(data, "Time-out");
1360 code = CURLE_OPERATION_TIMEDOUT;
1366 /* We called WSACreateEvent, so call WSACloseEvent */
1367 if(close_event_func(event_handle) == FALSE) {
1368 infof(data,"WSACloseEvent failed (%d)", SOCKERRNO);
1371 /* "Forget" pointers into the library we're about to free */
1372 create_event_func = NULL;
1373 close_event_func = NULL;
1374 event_select_func = NULL;
1375 enum_netevents_func = NULL;
1377 /* We called LoadLibrary, so call FreeLibrary */
1378 if(!FreeLibrary(wsock2))
1379 infof(data,"FreeLibrary(wsock2) failed (%d)", ERRNO);
1382 pfd[0].events = POLLIN;
1384 if(data->set.is_fread_set) {
1386 interval_ms = 100; /* poll user-supplied read function */
1390 pfd[1].events = POLLIN;
1392 interval_ms = 1 * 1000;
1396 switch (Curl_poll(pfd, poll_cnt, interval_ms)) {
1397 case -1: /* error, stop reading */
1400 case 0: /* timeout */
1404 default: /* read! */
1405 if(pfd[0].revents & POLLIN) {
1406 /* read data from network */
1407 code = Curl_read(conn, sockfd, buf, BUFSIZE - 1, &nread);
1408 /* read would've blocked. Loop again */
1409 if(code == CURLE_AGAIN)
1411 /* returned not-zero, this an error */
1416 /* returned zero but actually received 0 or less here,
1417 the server closed the connection and we bail out */
1418 else if(nread <= 0) {
1424 Curl_pgrsSetDownloadCounter(data, total_dl);
1425 code = telrcv(conn, (unsigned char *)buf, nread);
1431 /* Negotiate if the peer has started negotiating,
1432 otherwise don't. We don't want to speak telnet with
1433 non-telnet servers, like POP or SMTP. */
1434 if(tn->please_negotiate && !tn->already_negotiated) {
1436 tn->already_negotiated = 1;
1442 if(pfd[1].revents & POLLIN) { /* read from stdin */
1443 nread = read(0, buf, BUFSIZE - 1);
1447 /* read from user-supplied method */
1448 nread = (int)conn->fread_func(buf, 1, BUFSIZE - 1, conn->fread_in);
1449 if(nread == CURL_READFUNC_ABORT) {
1453 if(nread == CURL_READFUNC_PAUSE)
1458 code = send_telnet_data(conn, buf, nread);
1464 Curl_pgrsSetUploadCounter(data, total_ul);
1470 } /* poll switch statement */
1472 if(data->set.timeout) {
1474 if(Curl_tvdiff(now, conn->created) >= data->set.timeout) {
1475 failf(data, "Time-out");
1476 code = CURLE_OPERATION_TIMEDOUT;
1481 if(Curl_pgrsUpdate(conn)) {
1482 code = CURLE_ABORTED_BY_CALLBACK;
1487 /* mark this as "no further transfer wanted" */
1488 Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);