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, /* 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, "%d ", 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 void telrcv(struct connectdata *conn,
956 const unsigned char *inbuf, /* Data received from socket */
957 ssize_t count) /* Number of bytes received */
962 struct SessionHandle *data = conn->data;
963 struct TELNET *tn = (struct TELNET *)data->state.proto.telnet;
965 #define startskipping() \
966 if(startwrite >= 0) \
967 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)&inbuf[startwrite], in-startwrite); \
970 #define writebyte() \
974 #define bufferflush() startskipping()
980 /*infof(data,"In rcv state %d char %d\n", tn->telrcv_state, c);*/
981 switch (tn->telrcv_state)
984 tn->telrcv_state = CURL_TS_DATA;
988 break; /* Ignore \0 after CR */
996 tn->telrcv_state = CURL_TS_IAC;
1002 tn->telrcv_state = CURL_TS_CR;
1009 DEBUGASSERT(startwrite < 0);
1013 tn->telrcv_state = CURL_TS_WILL;
1016 tn->telrcv_state = CURL_TS_WONT;
1019 tn->telrcv_state = CURL_TS_DO;
1022 tn->telrcv_state = CURL_TS_DONT;
1026 tn->telrcv_state = CURL_TS_SB;
1029 tn->telrcv_state = CURL_TS_DATA;
1036 tn->telrcv_state = CURL_TS_DATA;
1037 printoption(data, "RCVD", CURL_IAC, c);
1043 printoption(data, "RCVD", CURL_WILL, c);
1044 tn->please_negotiate = 1;
1046 tn->telrcv_state = CURL_TS_DATA;
1050 printoption(data, "RCVD", CURL_WONT, c);
1051 tn->please_negotiate = 1;
1053 tn->telrcv_state = CURL_TS_DATA;
1057 printoption(data, "RCVD", CURL_DO, c);
1058 tn->please_negotiate = 1;
1060 tn->telrcv_state = CURL_TS_DATA;
1064 printoption(data, "RCVD", CURL_DONT, c);
1065 tn->please_negotiate = 1;
1067 tn->telrcv_state = CURL_TS_DATA;
1073 tn->telrcv_state = CURL_TS_SE;
1077 CURL_SB_ACCUM(tn,c);
1087 * This is an error. We only expect to get "IAC IAC" or "IAC SE".
1088 * Several things may have happend. An IAC was not doubled, the
1089 * IAC SE was left off, or another option got inserted into the
1090 * suboption are all possibilities. If we assume that the IAC was
1091 * not doubled, and really the IAC SE was left off, we could get
1092 * into an infinate loop here. So, instead, we terminate the
1093 * suboption, and process the partial suboption if we can.
1095 CURL_SB_ACCUM(tn, CURL_IAC);
1096 CURL_SB_ACCUM(tn, c);
1097 tn->subpointer -= 2;
1100 printoption(data, "In SUBOPTION processing, RCVD", CURL_IAC, c);
1101 suboption(conn); /* handle sub-option */
1102 tn->telrcv_state = CURL_TS_IAC;
1105 CURL_SB_ACCUM(tn,c);
1106 tn->telrcv_state = CURL_TS_SB;
1110 CURL_SB_ACCUM(tn, CURL_IAC);
1111 CURL_SB_ACCUM(tn, CURL_SE);
1112 tn->subpointer -= 2;
1114 suboption(conn); /* handle sub-option */
1115 tn->telrcv_state = CURL_TS_DATA;
1124 /* Escape and send a telnet data block */
1125 /* TODO: write large chunks of data instead of one byte at a time */
1126 static CURLcode send_telnet_data(struct connectdata *conn,
1127 char *buffer, ssize_t nread)
1129 unsigned char outbuf[2];
1130 ssize_t bytes_written, total_written;
1132 CURLcode rc = CURLE_OK;
1134 while(rc == CURLE_OK && nread--) {
1135 outbuf[0] = *buffer++;
1137 if(outbuf[0] == CURL_IAC)
1138 outbuf[out_count++] = CURL_IAC;
1142 /* Make sure socket is writable to avoid EWOULDBLOCK condition */
1143 struct pollfd pfd[1];
1144 pfd[0].fd = conn->sock[FIRSTSOCKET];
1145 pfd[0].events = POLLOUT;
1146 switch (Curl_poll(pfd, 1, -1)) {
1147 case -1: /* error, abort writing */
1148 case 0: /* timeout (will never happen) */
1149 rc = CURLE_SEND_ERROR;
1151 default: /* write! */
1153 rc = Curl_write(conn, conn->sock[FIRSTSOCKET], outbuf+total_written,
1154 out_count-total_written, &bytes_written);
1155 total_written += bytes_written;
1158 /* handle partial write */
1159 } while (rc == CURLE_OK && total_written < out_count);
1164 static CURLcode telnet_done(struct connectdata *conn,
1165 CURLcode status, bool premature)
1167 struct TELNET *tn = (struct TELNET *)conn->data->state.proto.telnet;
1168 (void)status; /* unused */
1169 (void)premature; /* not used */
1171 curl_slist_free_all(tn->telnet_vars);
1173 free(conn->data->state.proto.telnet);
1174 conn->data->state.proto.telnet = NULL;
1179 static CURLcode telnet_do(struct connectdata *conn, bool *done)
1182 struct SessionHandle *data = conn->data;
1183 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
1186 WSOCK2_FUNC close_event_func;
1187 WSOCK2_FUNC create_event_func;
1188 WSOCK2_FUNC event_select_func;
1189 WSOCK2_FUNC enum_netevents_func;
1190 WSAEVENT event_handle;
1191 WSANETWORKEVENTS events;
1192 HANDLE stdin_handle;
1197 DWORD readfile_read;
1200 struct pollfd pfd[2];
1204 char *buf = data->state.buffer;
1207 *done = TRUE; /* unconditionally */
1209 code = init_telnet(conn);
1213 tn = (struct TELNET *)data->state.proto.telnet;
1215 code = check_telnet_options(conn);
1221 ** This functionality only works with WinSock >= 2.0. So,
1222 ** make sure have it.
1224 code = check_wsock2(data);
1228 /* OK, so we have WinSock 2.0. We need to dynamically */
1229 /* load ws2_32.dll and get the function pointers we need. */
1230 wsock2 = LoadLibrary("WS2_32.DLL");
1231 if(wsock2 == NULL) {
1232 failf(data,"failed to load WS2_32.DLL (%d)", ERRNO);
1233 return CURLE_FAILED_INIT;
1236 /* Grab a pointer to WSACreateEvent */
1237 create_event_func = GetProcAddress(wsock2,"WSACreateEvent");
1238 if(create_event_func == NULL) {
1239 failf(data,"failed to find WSACreateEvent function (%d)",
1241 FreeLibrary(wsock2);
1242 return CURLE_FAILED_INIT;
1245 /* And WSACloseEvent */
1246 close_event_func = GetProcAddress(wsock2,"WSACloseEvent");
1247 if(close_event_func == NULL) {
1248 failf(data,"failed to find WSACloseEvent function (%d)",
1250 FreeLibrary(wsock2);
1251 return CURLE_FAILED_INIT;
1254 /* And WSAEventSelect */
1255 event_select_func = GetProcAddress(wsock2,"WSAEventSelect");
1256 if(event_select_func == NULL) {
1257 failf(data,"failed to find WSAEventSelect function (%d)",
1259 FreeLibrary(wsock2);
1260 return CURLE_FAILED_INIT;
1263 /* And WSAEnumNetworkEvents */
1264 enum_netevents_func = GetProcAddress(wsock2,"WSAEnumNetworkEvents");
1265 if(enum_netevents_func == NULL) {
1266 failf(data,"failed to find WSAEnumNetworkEvents function (%d)",
1268 FreeLibrary(wsock2);
1269 return CURLE_FAILED_INIT;
1272 /* We want to wait for both stdin and the socket. Since
1273 ** the select() function in winsock only works on sockets
1274 ** we have to use the WaitForMultipleObjects() call.
1277 /* First, create a sockets event object */
1278 event_handle = (WSAEVENT)create_event_func();
1279 if(event_handle == WSA_INVALID_EVENT) {
1280 failf(data,"WSACreateEvent failed (%d)", SOCKERRNO);
1281 FreeLibrary(wsock2);
1282 return CURLE_FAILED_INIT;
1285 /* The get the Windows file handle for stdin */
1286 stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
1288 /* Create the list of objects to wait for */
1289 objs[0] = event_handle;
1290 objs[1] = stdin_handle;
1292 /* Tell winsock what events we want to listen to */
1293 if(event_select_func(sockfd, event_handle, FD_READ|FD_CLOSE) == SOCKET_ERROR) {
1294 close_event_func(event_handle);
1295 FreeLibrary(wsock2);
1299 /* If stdin_handle is a pipe, use PeekNamedPipe() method to check it,
1300 else use the old WaitForMultipleObjects() way */
1301 if(GetFileType(stdin_handle) == FILE_TYPE_PIPE) {
1302 /* Don't wait for stdin_handle, just wait for event_handle */
1304 /* Check stdin_handle per 100 milliseconds */
1308 wait_timeout = INFINITE;
1311 /* Keep on listening and act on events */
1313 waitret = WaitForMultipleObjects(obj_count, objs, FALSE, wait_timeout);
1318 if(!PeekNamedPipe(stdin_handle, NULL, 0, NULL, &readfile_read, NULL)) {
1320 code = CURLE_READ_ERROR;
1327 if(!ReadFile(stdin_handle, buf, sizeof(data->state.buffer),
1328 &readfile_read, NULL)) {
1330 code = CURLE_READ_ERROR;
1334 code = send_telnet_data(conn, buf, readfile_read);
1343 case WAIT_OBJECT_0 + 1:
1345 if(!ReadFile(stdin_handle, buf, sizeof(data->state.buffer),
1346 &readfile_read, NULL)) {
1348 code = CURLE_READ_ERROR;
1352 code = send_telnet_data(conn, buf, readfile_read);
1361 if(enum_netevents_func(sockfd, event_handle, &events)
1363 if(events.lNetworkEvents & FD_READ) {
1364 /* This reallu OUGHT to check its return code. */
1365 (void)Curl_read(conn, sockfd, buf, BUFSIZE - 1, &nread);
1367 telrcv(conn, (unsigned char *)buf, nread);
1371 /* Negotiate if the peer has started negotiating,
1372 otherwise don't. We don't want to speak telnet with
1373 non-telnet servers, like POP or SMTP. */
1374 if(tn->please_negotiate && !tn->already_negotiated) {
1376 tn->already_negotiated = 1;
1380 if(events.lNetworkEvents & FD_CLOSE) {
1388 /* We called WSACreateEvent, so call WSACloseEvent */
1389 if(close_event_func(event_handle) == FALSE) {
1390 infof(data,"WSACloseEvent failed (%d)", SOCKERRNO);
1393 /* "Forget" pointers into the library we're about to free */
1394 create_event_func = NULL;
1395 close_event_func = NULL;
1396 event_select_func = NULL;
1397 enum_netevents_func = NULL;
1399 /* We called LoadLibrary, so call FreeLibrary */
1400 if(!FreeLibrary(wsock2))
1401 infof(data,"FreeLibrary(wsock2) failed (%d)", ERRNO);
1404 pfd[0].events = POLLIN;
1406 pfd[1].events = POLLIN;
1407 interval_ms = 1 * 1000;
1410 switch (Curl_poll(pfd, 2, interval_ms)) {
1411 case -1: /* error, stop reading */
1414 case 0: /* timeout */
1416 default: /* read! */
1417 if(pfd[1].revents & POLLIN) { /* read from stdin */
1418 nread = read(0, buf, 255);
1419 code = send_telnet_data(conn, buf, nread);
1426 if(pfd[0].revents & POLLIN) {
1427 /* This OUGHT to check the return code... */
1428 (void)Curl_read(conn, sockfd, buf, BUFSIZE - 1, &nread);
1430 /* if we receive 0 or less here, the server closed the connection and
1431 we bail out from this! */
1437 telrcv(conn, (unsigned char *)buf, nread);
1439 /* Negotiate if the peer has started negotiating,
1440 otherwise don't. We don't want to speak telnet with
1441 non-telnet servers, like POP or SMTP. */
1442 if(tn->please_negotiate && !tn->already_negotiated) {
1444 tn->already_negotiated = 1;
1448 if(data->set.timeout) {
1449 struct timeval now; /* current time */
1451 if(Curl_tvdiff(now, conn->created) >= data->set.timeout) {
1452 failf(data, "Time-out");
1453 code = CURLE_OPERATION_TIMEDOUT;
1459 /* mark this as "no further transfer wanted" */
1460 Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);