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.
22 ***************************************************************************/
26 #ifndef CURL_DISABLE_TELNET
27 /* -- WIN32 approved -- */
33 #ifdef HAVE_SYS_TYPES_H
34 #include <sys/types.h>
36 #ifdef HAVE_SYS_STAT_H
45 #ifdef HAVE_SYS_SOCKET_H
46 #include <sys/socket.h>
48 #include <netinet/in.h>
54 #ifdef HAVE_ARPA_INET_H
55 #include <arpa/inet.h>
60 #include <sys/ioctl.h>
63 #ifdef HAVE_SYS_PARAM_H
64 #include <sys/param.h>
67 #ifdef HAVE_SYS_SELECT_H
68 #include <sys/select.h>
75 #include <curl/curl.h>
80 #define _MPRINTF_REPLACE /* use our functions only */
81 #include <curl/mprintf.h>
86 #include "arpa_telnet.h"
89 /* The last #include file should be: */
92 #define SUBBUFSIZE 512
94 #define CURL_SB_CLEAR(x) x->subpointer = x->subbuffer;
95 #define CURL_SB_TERM(x) { x->subend = x->subpointer; CURL_SB_CLEAR(x); }
96 #define CURL_SB_ACCUM(x,c) \
97 if (x->subpointer < (x->subbuffer+sizeof x->subbuffer)) { \
98 *x->subpointer++ = (c); \
101 #define CURL_SB_GET(x) ((*x->subpointer++)&0xff)
102 #define CURL_SB_PEEK(x) ((*x->subpointer)&0xff)
103 #define CURL_SB_EOF(x) (x->subpointer >= x->subend)
104 #define CURL_SB_LEN(x) (x->subend - x->subpointer)
107 typedef FARPROC WSOCK2_FUNC;
108 static CURLcode check_wsock2 ( struct SessionHandle *data );
112 void telrcv(struct connectdata *,
113 unsigned char *inbuf, /* Data received from socket */
114 ssize_t count); /* Number of bytes received */
116 static void printoption(struct SessionHandle *data,
117 const char *direction,
118 int cmd, int option);
120 static void negotiate(struct connectdata *);
121 static void send_negotiation(struct connectdata *, int cmd, int option);
122 static void set_local_option(struct connectdata *, int cmd, int option);
123 static void set_remote_option(struct connectdata *, int cmd, int option);
125 static void printsub(struct SessionHandle *data,
126 int direction, unsigned char *pointer,
128 static void suboption(struct connectdata *);
130 /* For negotiation compliant to RFC 1143 */
133 #define CURL_WANTYES 2
134 #define CURL_WANTNO 3
137 #define CURL_OPPOSITE 1
140 * Telnet receiver states for fsm
151 CURL_TS_SB, /* sub-option collection */
152 CURL_TS_SE /* looking for sub-option end */
156 int please_negotiate;
157 int already_negotiated;
160 int us_preferred[256];
163 int him_preferred[256];
164 char subopt_ttype[32]; /* Set with suboption TTYPE */
165 char subopt_xdisploc[128]; /* Set with suboption XDISPLOC */
166 struct curl_slist *telnet_vars; /* Environment variables */
169 char subbuffer[SUBBUFSIZE];
170 char *subpointer, *subend; /* buffer for sub-options */
172 TelnetReceive telrcv_state;
177 check_wsock2 ( struct SessionHandle *data )
180 WORD wVersionRequested;
185 /* telnet requires at least WinSock 2.0 so ask for it. */
186 wVersionRequested = MAKEWORD(2, 0);
188 err = WSAStartup(wVersionRequested, &wsaData);
190 /* We must've called this once already, so this call */
191 /* should always succeed. But, just in case... */
193 failf(data,"WSAStartup failed (%d)",err);
194 return CURLE_FAILED_INIT;
197 /* We have to have a WSACleanup call for every successful */
198 /* WSAStartup call. */
201 /* Check that our version is supported */
202 if (LOBYTE(wsaData.wVersion) != LOBYTE(wVersionRequested) ||
203 HIBYTE(wsaData.wVersion) != HIBYTE(wVersionRequested)) {
204 /* Our version isn't supported */
205 failf(data,"insufficient winsock version to support "
207 return CURLE_FAILED_INIT;
210 /* Our version is supported */
215 CURLcode init_telnet(struct connectdata *conn)
219 tn = (struct TELNET *)calloc(1, sizeof(struct TELNET));
221 return CURLE_OUT_OF_MEMORY;
223 conn->proto.telnet = (void *)tn; /* make us known */
225 tn->telrcv_state = CURL_TS_DATA;
227 /* Init suboptions */
230 /* Set the options we want by default */
231 tn->us_preferred[CURL_TELOPT_BINARY] = CURL_YES;
232 tn->us_preferred[CURL_TELOPT_SGA] = CURL_YES;
233 tn->him_preferred[CURL_TELOPT_BINARY] = CURL_YES;
234 tn->him_preferred[CURL_TELOPT_SGA] = CURL_YES;
239 static void negotiate(struct connectdata *conn)
242 struct TELNET *tn = (struct TELNET *)conn->proto.telnet;
244 for(i = 0;i < CURL_NTELOPTS;i++)
246 if(tn->us_preferred[i] == CURL_YES)
247 set_local_option(conn, i, CURL_YES);
249 if(tn->him_preferred[i] == CURL_YES)
250 set_remote_option(conn, i, CURL_YES);
254 static void printoption(struct SessionHandle *data,
255 const char *direction, int cmd, int option)
260 if (data->set.verbose)
264 if (CURL_TELCMD_OK(option))
265 infof(data, "%s IAC %s\n", direction, CURL_TELCMD(option));
267 infof(data, "%s IAC %d\n", direction, option);
271 fmt = (cmd == CURL_WILL) ? "WILL" : (cmd == CURL_WONT) ? "WONT" :
272 (cmd == CURL_DO) ? "DO" : (cmd == CURL_DONT) ? "DONT" : 0;
275 if (CURL_TELOPT_OK(option))
276 opt = CURL_TELOPT(option);
277 else if (option == CURL_TELOPT_EXOPL)
283 infof(data, "%s %s %s\n", direction, fmt, opt);
285 infof(data, "%s %s %d\n", direction, fmt, option);
288 infof(data, "%s %d %d\n", direction, cmd, option);
293 static void send_negotiation(struct connectdata *conn, int cmd, int option)
295 unsigned char buf[3];
301 (void)swrite(conn->sock[FIRSTSOCKET], (char *)buf, 3);
303 printoption(conn->data, "SENT", cmd, option);
307 void set_remote_option(struct connectdata *conn, int option, int newstate)
309 struct TELNET *tn = (struct TELNET *)conn->proto.telnet;
310 if(newstate == CURL_YES)
312 switch(tn->him[option])
315 tn->him[option] = CURL_WANTYES;
316 send_negotiation(conn, CURL_DO, option);
320 /* Already enabled */
324 switch(tn->himq[option])
327 /* Already negotiating for CURL_YES, queue the request */
328 tn->himq[option] = CURL_OPPOSITE;
331 /* Error: already queued an enable request */
337 switch(tn->himq[option])
340 /* Error: already negotiating for enable */
343 tn->himq[option] = CURL_EMPTY;
351 switch(tn->him[option])
354 /* Already disabled */
358 tn->him[option] = CURL_WANTNO;
359 send_negotiation(conn, CURL_DONT, option);
363 switch(tn->himq[option])
366 /* Already negotiating for NO */
369 tn->himq[option] = CURL_EMPTY;
375 switch(tn->himq[option])
378 tn->himq[option] = CURL_OPPOSITE;
389 void rec_will(struct connectdata *conn, int option)
391 struct TELNET *tn = (struct TELNET *)conn->proto.telnet;
392 switch(tn->him[option])
395 if(tn->him_preferred[option] == CURL_YES)
397 tn->him[option] = CURL_YES;
398 send_negotiation(conn, CURL_DO, option);
402 send_negotiation(conn, CURL_DONT, option);
407 /* Already enabled */
411 switch(tn->himq[option])
414 /* Error: DONT answered by WILL */
415 tn->him[option] = CURL_NO;
418 /* Error: DONT answered by WILL */
419 tn->him[option] = CURL_YES;
420 tn->himq[option] = CURL_EMPTY;
426 switch(tn->himq[option])
429 tn->him[option] = CURL_YES;
432 tn->him[option] = CURL_WANTNO;
433 tn->himq[option] = CURL_EMPTY;
434 send_negotiation(conn, CURL_DONT, option);
442 void rec_wont(struct connectdata *conn, int option)
444 struct TELNET *tn = (struct TELNET *)conn->proto.telnet;
445 switch(tn->him[option])
448 /* Already disabled */
452 tn->him[option] = CURL_NO;
453 send_negotiation(conn, CURL_DONT, option);
457 switch(tn->himq[option])
460 tn->him[option] = CURL_NO;
464 tn->him[option] = CURL_WANTYES;
465 tn->himq[option] = CURL_EMPTY;
466 send_negotiation(conn, CURL_DO, option);
472 switch(tn->himq[option])
475 tn->him[option] = CURL_NO;
478 tn->him[option] = CURL_NO;
479 tn->himq[option] = CURL_EMPTY;
487 set_local_option(struct connectdata *conn, int option, int newstate)
489 struct TELNET *tn = (struct TELNET *)conn->proto.telnet;
490 if(newstate == CURL_YES)
492 switch(tn->us[option])
495 tn->us[option] = CURL_WANTYES;
496 send_negotiation(conn, CURL_WILL, option);
500 /* Already enabled */
504 switch(tn->usq[option])
507 /* Already negotiating for CURL_YES, queue the request */
508 tn->usq[option] = CURL_OPPOSITE;
511 /* Error: already queued an enable request */
517 switch(tn->usq[option])
520 /* Error: already negotiating for enable */
523 tn->usq[option] = CURL_EMPTY;
531 switch(tn->us[option])
534 /* Already disabled */
538 tn->us[option] = CURL_WANTNO;
539 send_negotiation(conn, CURL_WONT, option);
543 switch(tn->usq[option])
546 /* Already negotiating for NO */
549 tn->usq[option] = CURL_EMPTY;
555 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->proto.telnet;
572 switch(tn->us[option])
575 if(tn->us_preferred[option] == CURL_YES)
577 tn->us[option] = CURL_YES;
578 send_negotiation(conn, CURL_WILL, option);
582 send_negotiation(conn, CURL_WONT, option);
587 /* Already enabled */
591 switch(tn->usq[option])
594 /* Error: DONT answered by WILL */
595 tn->us[option] = CURL_NO;
598 /* Error: DONT answered by WILL */
599 tn->us[option] = CURL_YES;
600 tn->usq[option] = CURL_EMPTY;
606 switch(tn->usq[option])
609 tn->us[option] = CURL_YES;
612 tn->us[option] = CURL_WANTNO;
613 tn->himq[option] = CURL_EMPTY;
614 send_negotiation(conn, CURL_WONT, option);
622 void rec_dont(struct connectdata *conn, int option)
624 struct TELNET *tn = (struct TELNET *)conn->proto.telnet;
625 switch(tn->us[option])
628 /* Already disabled */
632 tn->us[option] = CURL_NO;
633 send_negotiation(conn, CURL_WONT, option);
637 switch(tn->usq[option])
640 tn->us[option] = CURL_NO;
644 tn->us[option] = CURL_WANTYES;
645 tn->usq[option] = CURL_EMPTY;
646 send_negotiation(conn, CURL_WILL, option);
652 switch(tn->usq[option])
655 tn->us[option] = CURL_NO;
658 tn->us[option] = CURL_NO;
659 tn->usq[option] = CURL_EMPTY;
667 static void printsub(struct SessionHandle *data,
668 int direction, /* '<' or '>' */
669 unsigned char *pointer, /* where suboption data is */
670 size_t length) /* length of suboption data */
674 if (data->set.verbose)
678 infof(data, "%s IAC SB ", (direction == '<')? "RCVD":"SENT");
683 i = pointer[length-2];
684 j = pointer[length-1];
686 if (i != CURL_IAC || j != CURL_SE)
688 infof(data, "(terminated by ");
689 if (CURL_TELOPT_OK(i))
690 infof(data, "%s ", CURL_TELOPT(i));
691 else if (CURL_TELCMD_OK(i))
692 infof(data, "%s ", CURL_TELCMD(i));
694 infof(data, "%d ", i);
695 if (CURL_TELOPT_OK(j))
696 infof(data, "%s", CURL_TELOPT(j));
697 else if (CURL_TELCMD_OK(j))
698 infof(data, "%s", CURL_TELCMD(j));
700 infof(data, "%d", j);
701 infof(data, ", not IAC SE!) ");
708 infof(data, "(Empty suboption?)");
712 if (CURL_TELOPT_OK(pointer[0])) {
714 case CURL_TELOPT_TTYPE:
715 case CURL_TELOPT_XDISPLOC:
716 case CURL_TELOPT_NEW_ENVIRON:
717 infof(data, "%s", CURL_TELOPT(pointer[0]));
720 infof(data, "%s (unsupported)", CURL_TELOPT(pointer[0]));
725 infof(data, "%d (unknown)", pointer[i]);
728 case CURL_TELQUAL_IS:
731 case CURL_TELQUAL_SEND:
732 infof(data, " SEND");
734 case CURL_TELQUAL_INFO:
735 infof(data, " INFO/REPLY");
737 case CURL_TELQUAL_NAME:
738 infof(data, " NAME");
743 case CURL_TELOPT_TTYPE:
744 case CURL_TELOPT_XDISPLOC:
746 infof(data, " \"%s\"", &pointer[2]);
748 case CURL_TELOPT_NEW_ENVIRON:
749 if(pointer[1] == CURL_TELQUAL_IS) {
751 for(i = 3;i < length;i++) {
753 case CURL_NEW_ENV_VAR:
756 case CURL_NEW_ENV_VALUE:
760 infof(data, "%c", pointer[i]);
767 for (i = 2; i < length; i++)
768 infof(data, " %.2x", pointer[i]);
779 static CURLcode check_telnet_options(struct connectdata *conn)
781 struct curl_slist *head;
782 char option_keyword[128];
783 char option_arg[256];
785 struct SessionHandle *data = conn->data;
786 struct TELNET *tn = (struct TELNET *)conn->proto.telnet;
788 /* Add the user name as an environment variable if it
789 was given on the command line */
790 if(conn->bits.user_passwd)
792 snprintf(option_arg, sizeof(option_arg), "USER,%s", conn->user);
793 tn->telnet_vars = curl_slist_append(tn->telnet_vars, option_arg);
795 tn->us_preferred[CURL_TELOPT_NEW_ENVIRON] = CURL_YES;
798 for(head = data->set.telnet_options; head; head=head->next) {
799 if(sscanf(head->data, "%127[^= ]%*[ =]%255s",
800 option_keyword, option_arg) == 2) {
803 if(curl_strequal(option_keyword, "TTYPE")) {
804 strncpy(tn->subopt_ttype, option_arg, 31);
805 tn->subopt_ttype[31] = 0; /* String termination */
806 tn->us_preferred[CURL_TELOPT_TTYPE] = CURL_YES;
810 /* Display variable */
811 if(curl_strequal(option_keyword, "XDISPLOC")) {
812 strncpy(tn->subopt_xdisploc, option_arg, 127);
813 tn->subopt_xdisploc[127] = 0; /* String termination */
814 tn->us_preferred[CURL_TELOPT_XDISPLOC] = CURL_YES;
818 /* Environment variable */
819 if(curl_strequal(option_keyword, "NEW_ENV")) {
820 buf = strdup(option_arg);
822 return CURLE_OUT_OF_MEMORY;
823 tn->telnet_vars = curl_slist_append(tn->telnet_vars, buf);
824 tn->us_preferred[CURL_TELOPT_NEW_ENVIRON] = CURL_YES;
828 failf(data, "Unknown telnet option %s", head->data);
829 return CURLE_UNKNOWN_TELNET_OPTION;
831 failf(data, "Syntax error in telnet option: %s", head->data);
832 return CURLE_TELNET_OPTION_SYNTAX;
842 * Look at the sub-option buffer, and try to be helpful to the other
846 static void suboption(struct connectdata *conn)
848 struct curl_slist *v;
849 unsigned char temp[2048];
854 struct SessionHandle *data = conn->data;
855 struct TELNET *tn = (struct TELNET *)conn->proto.telnet;
857 printsub(data, '<', (unsigned char *)tn->subbuffer, CURL_SB_LEN(tn)+2);
858 switch (CURL_SB_GET(tn)) {
859 case CURL_TELOPT_TTYPE:
860 len = strlen(tn->subopt_ttype) + 4 + 2;
861 snprintf((char *)temp, sizeof(temp),
862 "%c%c%c%c%s%c%c", CURL_IAC, CURL_SB, CURL_TELOPT_TTYPE,
863 CURL_TELQUAL_IS, tn->subopt_ttype, CURL_IAC, CURL_SE);
864 (void)swrite(conn->sock[FIRSTSOCKET], (char *)temp, len);
865 printsub(data, '>', &temp[2], len-2);
867 case CURL_TELOPT_XDISPLOC:
868 len = strlen(tn->subopt_xdisploc) + 4 + 2;
869 snprintf((char *)temp, sizeof(temp),
870 "%c%c%c%c%s%c%c", CURL_IAC, CURL_SB, CURL_TELOPT_XDISPLOC,
871 CURL_TELQUAL_IS, tn->subopt_xdisploc, CURL_IAC, CURL_SE);
872 (void)swrite(conn->sock[FIRSTSOCKET], (char *)temp, len);
873 printsub(data, '>', &temp[2], len-2);
875 case CURL_TELOPT_NEW_ENVIRON:
876 snprintf((char *)temp, sizeof(temp),
877 "%c%c%c%c", CURL_IAC, CURL_SB, CURL_TELOPT_NEW_ENVIRON,
881 for(v = tn->telnet_vars;v;v = v->next) {
882 tmplen = (strlen(v->data) + 1);
883 /* Add the variable only if it fits */
884 if(len + tmplen < (int)sizeof(temp)-6) {
885 sscanf(v->data, "%127[^,],%127s", varname, varval);
886 snprintf((char *)&temp[len], sizeof(temp) - len,
887 "%c%s%c%s", CURL_NEW_ENV_VAR, varname,
888 CURL_NEW_ENV_VALUE, varval);
892 snprintf((char *)&temp[len], sizeof(temp) - len,
893 "%c%c", CURL_IAC, CURL_SE);
895 (void)swrite(conn->sock[FIRSTSOCKET], (char *)temp, len);
896 printsub(data, '>', &temp[2], len-2);
903 void telrcv(struct connectdata *conn,
904 unsigned char *inbuf, /* Data received from socket */
905 ssize_t count) /* Number of bytes received */
909 struct SessionHandle *data = conn->data;
910 struct TELNET *tn = (struct TELNET *)conn->proto.telnet;
916 switch (tn->telrcv_state)
919 tn->telrcv_state = CURL_TS_DATA;
922 break; /* Ignore \0 after CR */
925 Curl_client_write(data, CLIENTWRITE_BODY, (char *)&c, 1);
931 tn->telrcv_state = CURL_TS_IAC;
936 tn->telrcv_state = CURL_TS_CR;
939 Curl_client_write(data, CLIENTWRITE_BODY, (char *)&c, 1);
947 tn->telrcv_state = CURL_TS_WILL;
950 tn->telrcv_state = CURL_TS_WONT;
953 tn->telrcv_state = CURL_TS_DO;
956 tn->telrcv_state = CURL_TS_DONT;
960 tn->telrcv_state = CURL_TS_SB;
963 Curl_client_write(data, CLIENTWRITE_BODY, (char *)&c, 1);
969 printoption(data, "RCVD", CURL_IAC, c);
972 tn->telrcv_state = CURL_TS_DATA;
976 printoption(data, "RCVD", CURL_WILL, c);
977 tn->please_negotiate = 1;
979 tn->telrcv_state = CURL_TS_DATA;
983 printoption(data, "RCVD", CURL_WONT, c);
984 tn->please_negotiate = 1;
986 tn->telrcv_state = CURL_TS_DATA;
990 printoption(data, "RCVD", CURL_DO, c);
991 tn->please_negotiate = 1;
993 tn->telrcv_state = CURL_TS_DATA;
997 printoption(data, "RCVD", CURL_DONT, c);
998 tn->please_negotiate = 1;
1000 tn->telrcv_state = CURL_TS_DATA;
1006 tn->telrcv_state = CURL_TS_SE;
1010 CURL_SB_ACCUM(tn,c);
1020 * This is an error. We only expect to get
1021 * "IAC IAC" or "IAC SE". Several things may
1022 * have happend. An IAC was not doubled, the
1023 * IAC SE was left off, or another option got
1024 * inserted into the suboption are all possibilities.
1025 * If we assume that the IAC was not doubled,
1026 * and really the IAC SE was left off, we could
1027 * get into an infinate loop here. So, instead,
1028 * we terminate the suboption, and process the
1029 * partial suboption if we can.
1031 CURL_SB_ACCUM(tn, (unsigned char)CURL_IAC);
1032 CURL_SB_ACCUM(tn, c);
1033 tn->subpointer -= 2;
1036 printoption(data, "In SUBOPTION processing, RCVD", CURL_IAC, c);
1037 suboption(conn); /* handle sub-option */
1038 tn->telrcv_state = CURL_TS_IAC;
1041 CURL_SB_ACCUM(tn,c);
1042 tn->telrcv_state = CURL_TS_SB;
1046 CURL_SB_ACCUM(tn, (unsigned char)CURL_IAC);
1047 CURL_SB_ACCUM(tn, (unsigned char)CURL_SE);
1048 tn->subpointer -= 2;
1050 suboption(conn); /* handle sub-option */
1051 tn->telrcv_state = CURL_TS_DATA;
1058 CURLcode Curl_telnet_done(struct connectdata *conn, CURLcode status)
1060 struct TELNET *tn = (struct TELNET *)conn->proto.telnet;
1061 (void)status; /* unused */
1063 curl_slist_free_all(tn->telnet_vars);
1065 free(conn->proto.telnet);
1066 conn->proto.telnet = NULL;
1071 CURLcode Curl_telnet(struct connectdata *conn)
1074 struct SessionHandle *data = conn->data;
1075 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
1078 WSOCK2_FUNC close_event_func;
1079 WSOCK2_FUNC create_event_func;
1080 WSOCK2_FUNC event_select_func;
1081 WSOCK2_FUNC enum_netevents_func;
1082 WSAEVENT event_handle;
1083 WSANETWORKEVENTS events;
1084 HANDLE stdin_handle;
1089 DWORD readfile_read;
1096 char *buf = data->state.buffer;
1099 code = init_telnet(conn);
1103 tn = (struct TELNET *)conn->proto.telnet;
1105 code = check_telnet_options(conn);
1111 ** This functionality only works with WinSock >= 2.0. So,
1112 ** make sure have it.
1114 code = check_wsock2(data);
1118 /* OK, so we have WinSock 2.0. We need to dynamically */
1119 /* load ws2_32.dll and get the function pointers we need. */
1120 wsock2 = LoadLibrary("WS2_32.DLL");
1121 if (wsock2 == NULL) {
1122 failf(data,"failed to load WS2_32.DLL (%d)",GetLastError());
1123 return CURLE_FAILED_INIT;
1126 /* Grab a pointer to WSACreateEvent */
1127 create_event_func = GetProcAddress(wsock2,"WSACreateEvent");
1128 if (create_event_func == NULL) {
1129 failf(data,"failed to find WSACreateEvent function (%d)",
1131 FreeLibrary(wsock2);
1132 return CURLE_FAILED_INIT;
1135 /* And WSACloseEvent */
1136 close_event_func = GetProcAddress(wsock2,"WSACloseEvent");
1137 if (create_event_func == NULL) {
1138 failf(data,"failed to find WSACloseEvent function (%d)",
1140 FreeLibrary(wsock2);
1141 return CURLE_FAILED_INIT;
1144 /* And WSAEventSelect */
1145 event_select_func = GetProcAddress(wsock2,"WSAEventSelect");
1146 if (event_select_func == NULL) {
1147 failf(data,"failed to find WSAEventSelect function (%d)",
1149 FreeLibrary(wsock2);
1150 return CURLE_FAILED_INIT;
1153 /* And WSAEnumNetworkEvents */
1154 enum_netevents_func = GetProcAddress(wsock2,"WSAEnumNetworkEvents");
1155 if (enum_netevents_func == NULL) {
1156 failf(data,"failed to find WSAEnumNetworkEvents function (%d)",
1158 FreeLibrary(wsock2);
1159 return CURLE_FAILED_INIT;
1162 /* We want to wait for both stdin and the socket. Since
1163 ** the select() function in winsock only works on sockets
1164 ** we have to use the WaitForMultipleObjects() call.
1167 /* First, create a sockets event object */
1168 event_handle = (WSAEVENT)create_event_func();
1169 if (event_handle == WSA_INVALID_EVENT) {
1170 failf(data,"WSACreateEvent failed (%d)",WSAGetLastError());
1171 FreeLibrary(wsock2);
1172 return CURLE_FAILED_INIT;
1175 /* The get the Windows file handle for stdin */
1176 stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
1178 /* Create the list of objects to wait for */
1179 objs[0] = event_handle;
1180 objs[1] = stdin_handle;
1182 /* Tell winsock what events we want to listen to */
1183 if(event_select_func(sockfd, event_handle, FD_READ|FD_CLOSE) == SOCKET_ERROR) {
1184 close_event_func(event_handle);
1185 FreeLibrary(wsock2);
1189 /* If stdin_handle is a pipe, use PeekNamedPipe() method to check it,
1190 else use the old WaitForMultipleObjects() way */
1191 if(GetFileType(stdin_handle) == FILE_TYPE_PIPE) {
1192 /* Don't wait for stdin_handle, just wait for event_handle */
1194 /* Check stdin_handle per 100 milliseconds */
1198 wait_timeout = INFINITE;
1201 /* Keep on listening and act on events */
1203 waitret = WaitForMultipleObjects(obj_count, objs, FALSE, wait_timeout);
1207 unsigned char outbuf[2];
1209 ssize_t bytes_written;
1213 if(!PeekNamedPipe(stdin_handle, NULL, 0, NULL, &readfile_read, NULL)) {
1217 nread = readfile_read;
1222 if(!ReadFile(stdin_handle, buf, sizeof(data->state.buffer),
1223 &readfile_read, NULL)) {
1227 nread = readfile_read;
1230 outbuf[0] = *buffer++;
1232 if(outbuf[0] == CURL_IAC)
1233 outbuf[out_count++] = CURL_IAC;
1235 Curl_write(conn, conn->sock[FIRSTSOCKET], outbuf,
1236 out_count, &bytes_written);
1242 case WAIT_OBJECT_0 + 1:
1244 unsigned char outbuf[2];
1246 ssize_t bytes_written;
1249 if(!ReadFile(stdin_handle, buf, sizeof(data->state.buffer),
1250 &readfile_read, NULL)) {
1254 nread = readfile_read;
1257 outbuf[0] = *buffer++;
1259 if(outbuf[0] == CURL_IAC)
1260 outbuf[out_count++] = CURL_IAC;
1262 Curl_write(conn, conn->sock[FIRSTSOCKET], outbuf,
1263 out_count, &bytes_written);
1269 if(enum_netevents_func(sockfd, event_handle, &events)
1271 if(events.lNetworkEvents & FD_READ) {
1272 /* This reallu OUGHT to check its return code. */
1273 (void)Curl_read(conn, sockfd, buf, BUFSIZE - 1, &nread);
1275 telrcv(conn, (unsigned char *)buf, nread);
1279 /* Negotiate if the peer has started negotiating,
1280 otherwise don't. We don't want to speak telnet with
1281 non-telnet servers, like POP or SMTP. */
1282 if(tn->please_negotiate && !tn->already_negotiated) {
1284 tn->already_negotiated = 1;
1288 if(events.lNetworkEvents & FD_CLOSE) {
1296 /* We called WSACreateEvent, so call WSACloseEvent */
1297 if (close_event_func(event_handle) == FALSE) {
1298 infof(data,"WSACloseEvent failed (%d)",WSAGetLastError());
1301 /* "Forget" pointers into the library we're about to free */
1302 create_event_func = NULL;
1303 close_event_func = NULL;
1304 event_select_func = NULL;
1305 enum_netevents_func = NULL;
1307 /* We called LoadLibrary, so call FreeLibrary */
1308 if (!FreeLibrary(wsock2))
1309 infof(data,"FreeLibrary(wsock2) failed (%d)",GetLastError());
1311 FD_ZERO (&readfd); /* clear it */
1312 FD_SET (sockfd, &readfd);
1313 FD_SET (0, &readfd);
1318 struct timeval interval;
1320 readfd = keepfd; /* set this every lap in the loop */
1321 interval.tv_sec = 1;
1322 interval.tv_usec = 0;
1324 switch (select (sockfd + 1, &readfd, NULL, NULL, &interval)) {
1325 case -1: /* error, stop reading */
1328 case 0: /* timeout */
1330 default: /* read! */
1331 if(FD_ISSET(0, &readfd)) { /* read from stdin */
1332 unsigned char outbuf[2];
1334 ssize_t bytes_written;
1337 nread = read(0, buf, 255);
1340 outbuf[0] = *buffer++;
1342 if(outbuf[0] == CURL_IAC)
1343 outbuf[out_count++] = CURL_IAC;
1345 Curl_write(conn, conn->sock[FIRSTSOCKET], outbuf,
1346 out_count, &bytes_written);
1350 if(FD_ISSET(sockfd, &readfd)) {
1351 /* This OUGHT to check the return code... */
1352 (void)Curl_read(conn, sockfd, buf, BUFSIZE - 1, &nread);
1354 /* if we receive 0 or less here, the server closed the connection and
1355 we bail out from this! */
1361 telrcv(conn, (unsigned char *)buf, nread);
1363 /* Negotiate if the peer has started negotiating,
1364 otherwise don't. We don't want to speak telnet with
1365 non-telnet servers, like POP or SMTP. */
1366 if(tn->please_negotiate && !tn->already_negotiated) {
1368 tn->already_negotiated = 1;
1372 if(data->set.timeout) {
1373 struct timeval now; /* current time */
1375 if(Curl_tvdiff(now, conn->created)/1000 >= data->set.timeout) {
1376 failf(data, "Time-out");
1377 code = CURLE_OPERATION_TIMEOUTED;
1383 /* mark this as "no further transfer wanted" */
1384 Curl_Transfer(conn, -1, -1, FALSE, NULL, -1, NULL);