1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2004, 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 * $Id: telnet.c,v 1.57 2004/03/11 13:13:35 bagder Exp $
22 ***************************************************************************/
26 #ifndef CURL_DISABLE_TELNET
27 /* -- WIN32 approved -- */
33 #include <sys/types.h>
42 #ifdef HAVE_SYS_SOCKET_H
43 #include <sys/socket.h>
45 #include <netinet/in.h>
51 #ifdef HAVE_ARPA_INET_H
52 #include <arpa/inet.h>
57 #include <sys/ioctl.h>
60 #ifdef HAVE_SYS_PARAM_H
61 #include <sys/param.h>
64 #ifdef HAVE_SYS_SELECT_H
65 #include <sys/select.h>
72 #include <curl/curl.h>
77 #define _MPRINTF_REPLACE /* use our functions only */
78 #include <curl/mprintf.h>
83 #include "arpa_telnet.h"
85 /* The last #include file should be: */
90 #define SUBBUFSIZE 512
92 #define CURL_SB_CLEAR(x) x->subpointer = x->subbuffer;
93 #define CURL_SB_TERM(x) { x->subend = x->subpointer; CURL_SB_CLEAR(x); }
94 #define CURL_SB_ACCUM(x,c) \
95 if (x->subpointer < (x->subbuffer+sizeof x->subbuffer)) { \
96 *x->subpointer++ = (c); \
99 #define CURL_SB_GET(x) ((*x->subpointer++)&0xff)
100 #define CURL_SB_PEEK(x) ((*x->subpointer)&0xff)
101 #define CURL_SB_EOF(x) (x->subpointer >= x->subend)
102 #define CURL_SB_LEN(x) (x->subend - x->subpointer)
105 typedef FARPROC WSOCK2_FUNC;
106 static CURLcode check_wsock2 ( struct SessionHandle *data );
110 void telrcv(struct connectdata *,
111 unsigned char *inbuf, /* Data received from socket */
112 int count); /* Number of bytes received */
114 static void printoption(struct SessionHandle *data,
115 const char *direction,
116 int cmd, int option);
118 static void negotiate(struct connectdata *);
119 static void send_negotiation(struct connectdata *, int cmd, int option);
120 static void set_local_option(struct connectdata *, int cmd, int option);
121 static void set_remote_option(struct connectdata *, int cmd, int option);
123 static void printsub(struct SessionHandle *data,
124 int direction, unsigned char *pointer, int length);
125 static void suboption(struct connectdata *);
127 /* For negotiation compliant to RFC 1143 */
130 #define CURL_WANTYES 2
131 #define CURL_WANTNO 3
134 #define CURL_OPPOSITE 1
137 * Telnet receiver states for fsm
148 CURL_TS_SB, /* sub-option collection */
149 CURL_TS_SE /* looking for sub-option end */
153 int please_negotiate;
154 int already_negotiated;
157 int us_preferred[256];
160 int him_preferred[256];
161 char subopt_ttype[32]; /* Set with suboption TTYPE */
162 char subopt_xdisploc[128]; /* Set with suboption XDISPLOC */
163 struct curl_slist *telnet_vars; /* Environment variables */
166 char subbuffer[SUBBUFSIZE];
167 char *subpointer, *subend; /* buffer for sub-options */
169 TelnetReceive telrcv_state;
174 check_wsock2 ( struct SessionHandle *data )
177 WORD wVersionRequested;
182 /* telnet requires at least WinSock 2.0 so ask for it. */
183 wVersionRequested = MAKEWORD(2, 0);
185 err = WSAStartup(wVersionRequested, &wsaData);
187 /* We must've called this once already, so this call */
188 /* should always succeed. But, just in case... */
190 failf(data,"WSAStartup failed (%d)",err);
191 return CURLE_FAILED_INIT;
194 /* We have to have a WSACleanup call for every successful */
195 /* WSAStartup call. */
198 /* Check that our version is supported */
199 if (LOBYTE(wsaData.wVersion) != LOBYTE(wVersionRequested) ||
200 HIBYTE(wsaData.wVersion) != HIBYTE(wVersionRequested)) {
201 /* Our version isn't supported */
202 failf(data,"insufficient winsock version to support "
204 return CURLE_FAILED_INIT;
207 /* Our version is supported */
212 CURLcode init_telnet(struct connectdata *conn)
216 tn = (struct TELNET *)malloc(sizeof(struct TELNET));
218 return CURLE_OUT_OF_MEMORY;
220 conn->proto.telnet = (void *)tn; /* make us known */
222 memset(tn, 0, sizeof(struct TELNET));
224 tn->telrcv_state = CURL_TS_DATA;
226 /* Init suboptions */
229 /* Set all options to NO */
231 /* NO is zero => default fill pattern */
232 memset(tn->us, CURL_NO, 256);
233 memset(tn->usq, CURL_NO, 256);
234 memset(tn->us_preferred, CURL_NO, 256);
235 memset(tn->him, CURL_NO, 256);
236 memset(tn->himq, CURL_NO, 256);
237 memset(tn->him_preferred, CURL_NO, 256);
239 /* Set the options we want by default */
240 tn->us_preferred[CURL_TELOPT_BINARY] = CURL_YES;
241 tn->us_preferred[CURL_TELOPT_SGA] = CURL_YES;
242 tn->him_preferred[CURL_TELOPT_BINARY] = CURL_YES;
243 tn->him_preferred[CURL_TELOPT_SGA] = CURL_YES;
248 static void negotiate(struct connectdata *conn)
251 struct TELNET *tn = (struct TELNET *)conn->proto.telnet;
253 for(i = 0;i < CURL_NTELOPTS;i++)
255 if(tn->us_preferred[i] == CURL_YES)
256 set_local_option(conn, i, CURL_YES);
258 if(tn->him_preferred[i] == CURL_YES)
259 set_remote_option(conn, i, CURL_YES);
263 static void printoption(struct SessionHandle *data,
264 const char *direction, int cmd, int option)
269 if (data->set.verbose)
273 if (CURL_TELCMD_OK(option))
274 Curl_infof(data, "%s IAC %s\n", direction, CURL_TELCMD(option));
276 Curl_infof(data, "%s IAC %d\n", direction, option);
280 fmt = (cmd == CURL_WILL) ? "WILL" : (cmd == CURL_WONT) ? "WONT" :
281 (cmd == CURL_DO) ? "DO" : (cmd == CURL_DONT) ? "DONT" : 0;
284 if (CURL_TELOPT_OK(option))
285 opt = CURL_TELOPT(option);
286 else if (option == CURL_TELOPT_EXOPL)
292 Curl_infof(data, "%s %s %s\n", direction, fmt, opt);
294 Curl_infof(data, "%s %s %d\n", direction, fmt, option);
297 Curl_infof(data, "%s %d %d\n", direction, cmd, option);
302 static void send_negotiation(struct connectdata *conn, int cmd, int option)
304 unsigned char buf[3];
310 (void)swrite(conn->sock[FIRSTSOCKET], buf, 3);
312 printoption(conn->data, "SENT", cmd, option);
316 void set_remote_option(struct connectdata *conn, int option, int newstate)
318 struct TELNET *tn = (struct TELNET *)conn->proto.telnet;
319 if(newstate == CURL_YES)
321 switch(tn->him[option])
324 tn->him[option] = CURL_WANTYES;
325 send_negotiation(conn, CURL_DO, option);
329 /* Already enabled */
333 switch(tn->himq[option])
336 /* Already negotiating for CURL_YES, queue the request */
337 tn->himq[option] = CURL_OPPOSITE;
340 /* Error: already queued an enable request */
346 switch(tn->himq[option])
349 /* Error: already negotiating for enable */
352 tn->himq[option] = CURL_EMPTY;
360 switch(tn->him[option])
363 /* Already disabled */
367 tn->him[option] = CURL_WANTNO;
368 send_negotiation(conn, CURL_DONT, option);
372 switch(tn->himq[option])
375 /* Already negotiating for NO */
378 tn->himq[option] = CURL_EMPTY;
384 switch(tn->himq[option])
387 tn->himq[option] = CURL_OPPOSITE;
398 void rec_will(struct connectdata *conn, int option)
400 struct TELNET *tn = (struct TELNET *)conn->proto.telnet;
401 switch(tn->him[option])
404 if(tn->him_preferred[option] == CURL_YES)
406 tn->him[option] = CURL_YES;
407 send_negotiation(conn, CURL_DO, option);
411 send_negotiation(conn, CURL_DONT, option);
416 /* Already enabled */
420 switch(tn->himq[option])
423 /* Error: DONT answered by WILL */
424 tn->him[option] = CURL_NO;
427 /* Error: DONT answered by WILL */
428 tn->him[option] = CURL_YES;
429 tn->himq[option] = CURL_EMPTY;
435 switch(tn->himq[option])
438 tn->him[option] = CURL_YES;
441 tn->him[option] = CURL_WANTNO;
442 tn->himq[option] = CURL_EMPTY;
443 send_negotiation(conn, CURL_DONT, option);
451 void rec_wont(struct connectdata *conn, int option)
453 struct TELNET *tn = (struct TELNET *)conn->proto.telnet;
454 switch(tn->him[option])
457 /* Already disabled */
461 tn->him[option] = CURL_NO;
462 send_negotiation(conn, CURL_DONT, option);
466 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])
484 tn->him[option] = CURL_NO;
487 tn->him[option] = CURL_NO;
488 tn->himq[option] = CURL_EMPTY;
496 set_local_option(struct connectdata *conn, int option, int newstate)
498 struct TELNET *tn = (struct TELNET *)conn->proto.telnet;
499 if(newstate == CURL_YES)
501 switch(tn->us[option])
504 tn->us[option] = CURL_WANTYES;
505 send_negotiation(conn, CURL_WILL, option);
509 /* Already enabled */
513 switch(tn->usq[option])
516 /* Already negotiating for CURL_YES, queue the request */
517 tn->usq[option] = CURL_OPPOSITE;
520 /* Error: already queued an enable request */
526 switch(tn->usq[option])
529 /* Error: already negotiating for enable */
532 tn->usq[option] = CURL_EMPTY;
540 switch(tn->us[option])
543 /* Already disabled */
547 tn->us[option] = CURL_WANTNO;
548 send_negotiation(conn, CURL_WONT, option);
552 switch(tn->usq[option])
555 /* Already negotiating for NO */
558 tn->usq[option] = CURL_EMPTY;
564 switch(tn->usq[option])
567 tn->usq[option] = CURL_OPPOSITE;
578 void rec_do(struct connectdata *conn, int option)
580 struct TELNET *tn = (struct TELNET *)conn->proto.telnet;
581 switch(tn->us[option])
584 if(tn->us_preferred[option] == CURL_YES)
586 tn->us[option] = CURL_YES;
587 send_negotiation(conn, CURL_WILL, option);
591 send_negotiation(conn, CURL_WONT, option);
596 /* Already enabled */
600 switch(tn->usq[option])
603 /* Error: DONT answered by WILL */
604 tn->us[option] = CURL_NO;
607 /* Error: DONT answered by WILL */
608 tn->us[option] = CURL_YES;
609 tn->usq[option] = CURL_EMPTY;
615 switch(tn->usq[option])
618 tn->us[option] = CURL_YES;
621 tn->us[option] = CURL_WANTNO;
622 tn->himq[option] = CURL_EMPTY;
623 send_negotiation(conn, CURL_WONT, option);
631 void rec_dont(struct connectdata *conn, int option)
633 struct TELNET *tn = (struct TELNET *)conn->proto.telnet;
634 switch(tn->us[option])
637 /* Already disabled */
641 tn->us[option] = CURL_NO;
642 send_negotiation(conn, CURL_WONT, option);
646 switch(tn->usq[option])
649 tn->us[option] = CURL_NO;
653 tn->us[option] = CURL_WANTYES;
654 tn->usq[option] = CURL_EMPTY;
655 send_negotiation(conn, CURL_WILL, option);
661 switch(tn->usq[option])
664 tn->us[option] = CURL_NO;
667 tn->us[option] = CURL_NO;
668 tn->usq[option] = CURL_EMPTY;
676 static void printsub(struct SessionHandle *data,
677 int direction, /* '<' or '>' */
678 unsigned char *pointer, /* where suboption data is */
679 int length) /* length of suboption data */
683 if (data->set.verbose)
687 Curl_infof(data, "%s IAC SB ", (direction == '<')? "RCVD":"SENT");
692 i = pointer[length-2];
693 j = pointer[length-1];
695 if (i != CURL_IAC || j != CURL_SE)
697 Curl_infof(data, "(terminated by ");
698 if (CURL_TELOPT_OK(i))
699 Curl_infof(data, "%s ", CURL_TELOPT(i));
700 else if (CURL_TELCMD_OK(i))
701 Curl_infof(data, "%s ", CURL_TELCMD(i));
703 Curl_infof(data, "%d ", i);
704 if (CURL_TELOPT_OK(j))
705 Curl_infof(data, "%s", CURL_TELOPT(j));
706 else if (CURL_TELCMD_OK(j))
707 Curl_infof(data, "%s", CURL_TELCMD(j));
709 Curl_infof(data, "%d", j);
710 Curl_infof(data, ", not IAC SE!) ");
717 Curl_infof(data, "(Empty suboption?)");
721 if (CURL_TELOPT_OK(pointer[0])) {
723 case CURL_TELOPT_TTYPE:
724 case CURL_TELOPT_XDISPLOC:
725 case CURL_TELOPT_NEW_ENVIRON:
726 Curl_infof(data, "%s", CURL_TELOPT(pointer[0]));
729 Curl_infof(data, "%s (unsupported)", CURL_TELOPT(pointer[0]));
734 Curl_infof(data, "%d (unknown)", pointer[i]);
737 case CURL_TELQUAL_IS:
738 Curl_infof(data, " IS");
740 case CURL_TELQUAL_SEND:
741 Curl_infof(data, " SEND");
743 case CURL_TELQUAL_INFO:
744 Curl_infof(data, " INFO/REPLY");
746 case CURL_TELQUAL_NAME:
747 Curl_infof(data, " NAME");
752 case CURL_TELOPT_TTYPE:
753 case CURL_TELOPT_XDISPLOC:
755 Curl_infof(data, " \"%s\"", &pointer[2]);
757 case CURL_TELOPT_NEW_ENVIRON:
758 if(pointer[1] == CURL_TELQUAL_IS) {
759 Curl_infof(data, " ");
760 for(i = 3;i < length;i++) {
762 case CURL_NEW_ENV_VAR:
763 Curl_infof(data, ", ");
765 case CURL_NEW_ENV_VALUE:
766 Curl_infof(data, " = ");
769 Curl_infof(data, "%c", pointer[i]);
776 for (i = 2; i < length; i++)
777 Curl_infof(data, " %.2x", pointer[i]);
783 Curl_infof(data, "\n");
788 static CURLcode check_telnet_options(struct connectdata *conn)
790 struct curl_slist *head;
791 char option_keyword[128];
792 char option_arg[256];
794 struct SessionHandle *data = conn->data;
795 struct TELNET *tn = (struct TELNET *)conn->proto.telnet;
797 /* Add the user name as an environment variable if it
798 was given on the command line */
799 if(conn->bits.user_passwd)
801 snprintf(option_arg, sizeof(option_arg), "USER,%s", conn->user);
802 tn->telnet_vars = curl_slist_append(tn->telnet_vars, option_arg);
804 tn->us_preferred[CURL_TELOPT_NEW_ENVIRON] = CURL_YES;
807 for(head = data->set.telnet_options; head; head=head->next) {
808 if(sscanf(head->data, "%127[^= ]%*[ =]%255s",
809 option_keyword, option_arg) == 2) {
812 if(curl_strequal(option_keyword, "TTYPE")) {
813 strncpy(tn->subopt_ttype, option_arg, 31);
814 tn->subopt_ttype[31] = 0; /* String termination */
815 tn->us_preferred[CURL_TELOPT_TTYPE] = CURL_YES;
819 /* Display variable */
820 if(curl_strequal(option_keyword, "XDISPLOC")) {
821 strncpy(tn->subopt_xdisploc, option_arg, 127);
822 tn->subopt_xdisploc[127] = 0; /* String termination */
823 tn->us_preferred[CURL_TELOPT_XDISPLOC] = CURL_YES;
827 /* Environment variable */
828 if(curl_strequal(option_keyword, "NEW_ENV")) {
829 buf = strdup(option_arg);
831 return CURLE_OUT_OF_MEMORY;
832 tn->telnet_vars = curl_slist_append(tn->telnet_vars, buf);
833 tn->us_preferred[CURL_TELOPT_NEW_ENVIRON] = CURL_YES;
837 failf(data, "Unknown telnet option %s", head->data);
838 return CURLE_UNKNOWN_TELNET_OPTION;
840 failf(data, "Syntax error in telnet option: %s", head->data);
841 return CURLE_TELNET_OPTION_SYNTAX;
851 * Look at the sub-option buffer, and try to be helpful to the other
855 static void suboption(struct connectdata *conn)
857 struct curl_slist *v;
858 unsigned char temp[2048];
863 struct SessionHandle *data = conn->data;
864 struct TELNET *tn = (struct TELNET *)conn->proto.telnet;
866 printsub(data, '<', (unsigned char *)tn->subbuffer, CURL_SB_LEN(tn)+2);
867 switch (CURL_SB_GET(tn)) {
868 case CURL_TELOPT_TTYPE:
869 len = strlen(tn->subopt_ttype) + 4 + 2;
870 snprintf((char *)temp, sizeof(temp),
871 "%c%c%c%c%s%c%c", CURL_IAC, CURL_SB, CURL_TELOPT_TTYPE,
872 CURL_TELQUAL_IS, tn->subopt_ttype, CURL_IAC, CURL_SE);
873 (void)swrite(conn->sock[FIRSTSOCKET], temp, len);
874 printsub(data, '>', &temp[2], len-2);
876 case CURL_TELOPT_XDISPLOC:
877 len = strlen(tn->subopt_xdisploc) + 4 + 2;
878 snprintf((char *)temp, sizeof(temp),
879 "%c%c%c%c%s%c%c", CURL_IAC, CURL_SB, CURL_TELOPT_XDISPLOC,
880 CURL_TELQUAL_IS, tn->subopt_xdisploc, CURL_IAC, CURL_SE);
881 (void)swrite(conn->sock[FIRSTSOCKET], temp, len);
882 printsub(data, '>', &temp[2], len-2);
884 case CURL_TELOPT_NEW_ENVIRON:
885 snprintf((char *)temp, sizeof(temp),
886 "%c%c%c%c", CURL_IAC, CURL_SB, CURL_TELOPT_NEW_ENVIRON,
890 for(v = tn->telnet_vars;v;v = v->next) {
891 tmplen = (strlen(v->data) + 1);
892 /* Add the variable only if it fits */
893 if(len + tmplen < (int)sizeof(temp)-6) {
894 sscanf(v->data, "%127[^,],%s", varname, varval);
895 snprintf((char *)&temp[len], sizeof(temp) - len,
896 "%c%s%c%s", CURL_NEW_ENV_VAR, varname,
897 CURL_NEW_ENV_VALUE, varval);
901 snprintf((char *)&temp[len], sizeof(temp) - len,
902 "%c%c", CURL_IAC, CURL_SE);
904 (void)swrite(conn->sock[FIRSTSOCKET], temp, len);
905 printsub(data, '>', &temp[2], len-2);
912 void telrcv(struct connectdata *conn,
913 unsigned char *inbuf, /* Data received from socket */
914 int count) /* Number of bytes received */
918 struct SessionHandle *data = conn->data;
919 struct TELNET *tn = (struct TELNET *)conn->proto.telnet;
925 switch (tn->telrcv_state)
928 tn->telrcv_state = CURL_TS_DATA;
931 break; /* Ignore \0 after CR */
934 Curl_client_write(data, CLIENTWRITE_BODY, (char *)&c, 1);
940 tn->telrcv_state = CURL_TS_IAC;
945 tn->telrcv_state = CURL_TS_CR;
948 Curl_client_write(data, CLIENTWRITE_BODY, (char *)&c, 1);
956 tn->telrcv_state = CURL_TS_WILL;
959 tn->telrcv_state = CURL_TS_WONT;
962 tn->telrcv_state = CURL_TS_DO;
965 tn->telrcv_state = CURL_TS_DONT;
969 tn->telrcv_state = CURL_TS_SB;
972 Curl_client_write(data, CLIENTWRITE_BODY, (char *)&c, 1);
978 printoption(data, "RCVD", CURL_IAC, c);
981 tn->telrcv_state = CURL_TS_DATA;
985 printoption(data, "RCVD", CURL_WILL, c);
986 tn->please_negotiate = 1;
988 tn->telrcv_state = CURL_TS_DATA;
992 printoption(data, "RCVD", CURL_WONT, c);
993 tn->please_negotiate = 1;
995 tn->telrcv_state = CURL_TS_DATA;
999 printoption(data, "RCVD", CURL_DO, c);
1000 tn->please_negotiate = 1;
1002 tn->telrcv_state = CURL_TS_DATA;
1006 printoption(data, "RCVD", CURL_DONT, c);
1007 tn->please_negotiate = 1;
1009 tn->telrcv_state = CURL_TS_DATA;
1015 tn->telrcv_state = CURL_TS_SE;
1019 CURL_SB_ACCUM(tn,c);
1029 * This is an error. We only expect to get
1030 * "IAC IAC" or "IAC SE". Several things may
1031 * have happend. An IAC was not doubled, the
1032 * IAC SE was left off, or another option got
1033 * inserted into the suboption are all possibilities.
1034 * If we assume that the IAC was not doubled,
1035 * and really the IAC SE was left off, we could
1036 * get into an infinate loop here. So, instead,
1037 * we terminate the suboption, and process the
1038 * partial suboption if we can.
1040 CURL_SB_ACCUM(tn, (unsigned char)CURL_IAC);
1041 CURL_SB_ACCUM(tn, c);
1042 tn->subpointer -= 2;
1045 printoption(data, "In SUBOPTION processing, RCVD", CURL_IAC, c);
1046 suboption(conn); /* handle sub-option */
1047 tn->telrcv_state = CURL_TS_IAC;
1050 CURL_SB_ACCUM(tn,c);
1051 tn->telrcv_state = CURL_TS_SB;
1055 CURL_SB_ACCUM(tn, (unsigned char)CURL_IAC);
1056 CURL_SB_ACCUM(tn, (unsigned char)CURL_SE);
1057 tn->subpointer -= 2;
1059 suboption(conn); /* handle sub-option */
1060 tn->telrcv_state = CURL_TS_DATA;
1067 CURLcode Curl_telnet_done(struct connectdata *conn)
1069 struct TELNET *tn = (struct TELNET *)conn->proto.telnet;
1070 curl_slist_free_all(tn->telnet_vars);
1072 free(conn->proto.telnet);
1073 conn->proto.telnet = NULL;
1078 CURLcode Curl_telnet(struct connectdata *conn)
1081 struct SessionHandle *data = conn->data;
1082 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
1085 WSOCK2_FUNC close_event_func;
1086 WSOCK2_FUNC create_event_func;
1087 WSOCK2_FUNC event_select_func;
1088 WSOCK2_FUNC enum_netevents_func;
1089 WSAEVENT event_handle;
1090 WSANETWORKEVENTS events;
1091 HANDLE stdin_handle;
1094 DWORD readfile_read;
1101 char *buf = data->state.buffer;
1104 code = init_telnet(conn);
1108 tn = (struct TELNET *)conn->proto.telnet;
1110 code = check_telnet_options(conn);
1116 ** This functionality only works with WinSock >= 2.0. So,
1117 ** make sure have it.
1119 code = check_wsock2(data);
1123 /* OK, so we have WinSock 2.0. We need to dynamically */
1124 /* load ws2_32.dll and get the function pointers we need. */
1125 wsock2 = LoadLibrary("WS2_32.DLL");
1126 if (wsock2 == NULL) {
1127 failf(data,"failed to load WS2_32.DLL (%d)",GetLastError());
1128 return CURLE_FAILED_INIT;
1131 /* Grab a pointer to WSACreateEvent */
1132 create_event_func = GetProcAddress(wsock2,"WSACreateEvent");
1133 if (create_event_func == NULL) {
1134 failf(data,"failed to find WSACreateEvent function (%d)",
1136 FreeLibrary(wsock2);
1137 return CURLE_FAILED_INIT;
1140 /* And WSACloseEvent */
1141 close_event_func = GetProcAddress(wsock2,"WSACloseEvent");
1142 if (create_event_func == NULL) {
1143 failf(data,"failed to find WSACloseEvent function (%d)",
1145 FreeLibrary(wsock2);
1146 return CURLE_FAILED_INIT;
1149 /* And WSAEventSelect */
1150 event_select_func = GetProcAddress(wsock2,"WSAEventSelect");
1151 if (event_select_func == NULL) {
1152 failf(data,"failed to find WSAEventSelect function (%d)",
1154 FreeLibrary(wsock2);
1155 return CURLE_FAILED_INIT;
1158 /* And WSAEnumNetworkEvents */
1159 enum_netevents_func = GetProcAddress(wsock2,"WSAEnumNetworkEvents");
1160 if (enum_netevents_func == NULL) {
1161 failf(data,"failed to find WSAEnumNetworkEvents function (%d)",
1163 FreeLibrary(wsock2);
1164 return CURLE_FAILED_INIT;
1167 /* We want to wait for both stdin and the socket. Since
1168 ** the select() function in winsock only works on sockets
1169 ** we have to use the WaitForMultipleObjects() call.
1172 /* First, create a sockets event object */
1173 event_handle = (WSAEVENT)create_event_func();
1174 if (event_handle == WSA_INVALID_EVENT) {
1175 failf(data,"WSACreateEvent failed (%d)",WSAGetLastError());
1176 FreeLibrary(wsock2);
1177 return CURLE_FAILED_INIT;
1180 /* The get the Windows file handle for stdin */
1181 stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
1183 /* Create the list of objects to wait for */
1184 objs[0] = stdin_handle;
1185 objs[1] = event_handle;
1187 /* Tell winsock what events we want to listen to */
1188 if(event_select_func(sockfd, event_handle, FD_READ|FD_CLOSE) == SOCKET_ERROR) {
1189 close_event_func(event_handle);
1190 FreeLibrary(wsock2);
1194 /* Keep on listening and act on events */
1196 waitret = WaitForMultipleObjects(2, objs, FALSE, INFINITE);
1197 switch(waitret - WAIT_OBJECT_0) {
1200 unsigned char outbuf[2];
1202 ssize_t bytes_written;
1205 if(!ReadFile(stdin_handle, buf, sizeof(data->state.buffer),
1206 &readfile_read, NULL)) {
1210 nread = readfile_read;
1213 outbuf[0] = *buffer++;
1215 if(outbuf[0] == CURL_IAC)
1216 outbuf[out_count++] = CURL_IAC;
1218 Curl_write(conn, conn->sock[FIRSTSOCKET], outbuf,
1219 out_count, &bytes_written);
1225 if(enum_netevents_func(sockfd, event_handle, &events)
1227 if(events.lNetworkEvents & FD_READ) {
1228 /* This reallu OUGHT to check its return code. */
1229 (void)Curl_read(conn, sockfd, buf, BUFSIZE - 1, &nread);
1231 telrcv(conn, (unsigned char *)buf, nread);
1235 /* Negotiate if the peer has started negotiating,
1236 otherwise don't. We don't want to speak telnet with
1237 non-telnet servers, like POP or SMTP. */
1238 if(tn->please_negotiate && !tn->already_negotiated) {
1240 tn->already_negotiated = 1;
1244 if(events.lNetworkEvents & FD_CLOSE) {
1252 /* We called WSACreateEvent, so call WSACloseEvent */
1253 if (close_event_func(event_handle) == FALSE) {
1254 infof(data,"WSACloseEvent failed (%d)",WSAGetLastError());
1257 /* "Forget" pointers into the library we're about to free */
1258 create_event_func = NULL;
1259 close_event_func = NULL;
1260 event_select_func = NULL;
1261 enum_netevents_func = NULL;
1263 /* We called LoadLibrary, so call FreeLibrary */
1264 if (!FreeLibrary(wsock2))
1265 infof(data,"FreeLibrary(wsock2) failed (%d)",GetLastError());
1267 FD_ZERO (&readfd); /* clear it */
1268 FD_SET (sockfd, &readfd);
1269 FD_SET (0, &readfd);
1274 struct timeval interval;
1276 readfd = keepfd; /* set this every lap in the loop */
1277 interval.tv_sec = 1;
1278 interval.tv_usec = 0;
1280 switch (select (sockfd + 1, &readfd, NULL, NULL, &interval)) {
1281 case -1: /* error, stop reading */
1284 case 0: /* timeout */
1286 default: /* read! */
1287 if(FD_ISSET(0, &readfd)) { /* read from stdin */
1288 unsigned char outbuf[2];
1290 ssize_t bytes_written;
1293 nread = read(0, buf, 255);
1296 outbuf[0] = *buffer++;
1298 if(outbuf[0] == CURL_IAC)
1299 outbuf[out_count++] = CURL_IAC;
1301 Curl_write(conn, conn->sock[FIRSTSOCKET], outbuf,
1302 out_count, &bytes_written);
1306 if(FD_ISSET(sockfd, &readfd)) {
1307 /* This OUGHT to check the return code... */
1308 (void)Curl_read(conn, sockfd, buf, BUFSIZE - 1, &nread);
1310 /* if we receive 0 or less here, the server closed the connection and
1311 we bail out from this! */
1317 telrcv(conn, (unsigned char *)buf, nread);
1319 /* Negotiate if the peer has started negotiating,
1320 otherwise don't. We don't want to speak telnet with
1321 non-telnet servers, like POP or SMTP. */
1322 if(tn->please_negotiate && !tn->already_negotiated) {
1324 tn->already_negotiated = 1;
1328 if(data->set.timeout) {
1329 struct timeval now; /* current time */
1331 if(Curl_tvdiff(now, conn->created)/1000 >= data->set.timeout) {
1332 failf(data, "Time-out");
1333 code = CURLE_OPERATION_TIMEOUTED;
1339 /* mark this as "no further transfer wanted" */
1340 Curl_Transfer(conn, -1, -1, FALSE, NULL, -1, NULL);