1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2006, 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
44 #ifdef HAVE_SYS_SOCKET_H
45 #include <sys/socket.h>
47 #include <netinet/in.h>
53 #ifdef HAVE_ARPA_INET_H
54 #include <arpa/inet.h>
59 #include <sys/ioctl.h>
62 #ifdef HAVE_SYS_PARAM_H
63 #include <sys/param.h>
70 #include <curl/curl.h>
75 #define _MPRINTF_REPLACE /* use our functions only */
76 #include <curl/mprintf.h>
81 #include "arpa_telnet.h"
85 /* The last #include file should be: */
88 #define SUBBUFSIZE 512
90 #define CURL_SB_CLEAR(x) x->subpointer = x->subbuffer;
91 #define CURL_SB_TERM(x) { x->subend = x->subpointer; CURL_SB_CLEAR(x); }
92 #define CURL_SB_ACCUM(x,c) \
93 if (x->subpointer < (x->subbuffer+sizeof x->subbuffer)) { \
94 *x->subpointer++ = (c); \
97 #define CURL_SB_GET(x) ((*x->subpointer++)&0xff)
98 #define CURL_SB_PEEK(x) ((*x->subpointer)&0xff)
99 #define CURL_SB_EOF(x) (x->subpointer >= x->subend)
100 #define CURL_SB_LEN(x) (x->subend - x->subpointer)
103 typedef FARPROC WSOCK2_FUNC;
104 static CURLcode check_wsock2 ( struct SessionHandle *data );
108 void telrcv(struct connectdata *,
109 unsigned char *inbuf, /* Data received from socket */
110 ssize_t count); /* Number of bytes received */
112 static void printoption(struct SessionHandle *data,
113 const char *direction,
114 int cmd, int option);
116 static void negotiate(struct connectdata *);
117 static void send_negotiation(struct connectdata *, int cmd, int option);
118 static void set_local_option(struct connectdata *, int cmd, int option);
119 static void set_remote_option(struct connectdata *, int cmd, int option);
121 static void printsub(struct SessionHandle *data,
122 int direction, unsigned char *pointer,
124 static void suboption(struct connectdata *);
126 /* For negotiation compliant to RFC 1143 */
129 #define CURL_WANTYES 2
130 #define CURL_WANTNO 3
133 #define CURL_OPPOSITE 1
136 * Telnet receiver states for fsm
147 CURL_TS_SB, /* sub-option collection */
148 CURL_TS_SE /* looking for sub-option end */
152 int please_negotiate;
153 int already_negotiated;
156 int us_preferred[256];
159 int him_preferred[256];
160 char subopt_ttype[32]; /* Set with suboption TTYPE */
161 char subopt_xdisploc[128]; /* Set with suboption XDISPLOC */
162 struct curl_slist *telnet_vars; /* Environment variables */
165 unsigned char subbuffer[SUBBUFSIZE];
166 unsigned char *subpointer, *subend; /* buffer for sub-options */
168 TelnetReceive telrcv_state;
173 check_wsock2 ( struct SessionHandle *data )
176 WORD wVersionRequested;
181 /* telnet requires at least WinSock 2.0 so ask for it. */
182 wVersionRequested = MAKEWORD(2, 0);
184 err = WSAStartup(wVersionRequested, &wsaData);
186 /* We must've called this once already, so this call */
187 /* should always succeed. But, just in case... */
189 failf(data,"WSAStartup failed (%d)",err);
190 return CURLE_FAILED_INIT;
193 /* We have to have a WSACleanup call for every successful */
194 /* WSAStartup call. */
197 /* Check that our version is supported */
198 if (LOBYTE(wsaData.wVersion) != LOBYTE(wVersionRequested) ||
199 HIBYTE(wsaData.wVersion) != HIBYTE(wVersionRequested)) {
200 /* Our version isn't supported */
201 failf(data,"insufficient winsock version to support "
203 return CURLE_FAILED_INIT;
206 /* Our version is supported */
211 CURLcode init_telnet(struct connectdata *conn)
215 tn = (struct TELNET *)calloc(1, sizeof(struct TELNET));
217 return CURLE_OUT_OF_MEMORY;
219 conn->proto.telnet = (void *)tn; /* make us known */
221 tn->telrcv_state = CURL_TS_DATA;
223 /* Init suboptions */
226 /* Set the options we want by default */
227 tn->us_preferred[CURL_TELOPT_BINARY] = CURL_YES;
228 tn->us_preferred[CURL_TELOPT_SGA] = CURL_YES;
229 tn->him_preferred[CURL_TELOPT_BINARY] = CURL_YES;
230 tn->him_preferred[CURL_TELOPT_SGA] = CURL_YES;
235 static void negotiate(struct connectdata *conn)
238 struct TELNET *tn = (struct TELNET *)conn->proto.telnet;
240 for(i = 0;i < CURL_NTELOPTS;i++)
242 if(tn->us_preferred[i] == CURL_YES)
243 set_local_option(conn, i, CURL_YES);
245 if(tn->him_preferred[i] == CURL_YES)
246 set_remote_option(conn, i, CURL_YES);
250 static void printoption(struct SessionHandle *data,
251 const char *direction, int cmd, int option)
256 if (data->set.verbose)
260 if (CURL_TELCMD_OK(option))
261 infof(data, "%s IAC %s\n", direction, CURL_TELCMD(option));
263 infof(data, "%s IAC %d\n", direction, option);
267 fmt = (cmd == CURL_WILL) ? "WILL" : (cmd == CURL_WONT) ? "WONT" :
268 (cmd == CURL_DO) ? "DO" : (cmd == CURL_DONT) ? "DONT" : 0;
271 if (CURL_TELOPT_OK(option))
272 opt = CURL_TELOPT(option);
273 else if (option == CURL_TELOPT_EXOPL)
279 infof(data, "%s %s %s\n", direction, fmt, opt);
281 infof(data, "%s %s %d\n", direction, fmt, option);
284 infof(data, "%s %d %d\n", direction, cmd, option);
289 static void send_negotiation(struct connectdata *conn, int cmd, int option)
291 unsigned char buf[3];
292 ssize_t bytes_written;
298 bytes_written = swrite(conn->sock[FIRSTSOCKET], buf, 3);
300 printoption(conn->data, "SENT", cmd, option);
304 void set_remote_option(struct connectdata *conn, int option, int newstate)
306 struct TELNET *tn = (struct TELNET *)conn->proto.telnet;
307 if(newstate == CURL_YES)
309 switch(tn->him[option])
312 tn->him[option] = CURL_WANTYES;
313 send_negotiation(conn, CURL_DO, option);
317 /* Already enabled */
321 switch(tn->himq[option])
324 /* Already negotiating for CURL_YES, queue the request */
325 tn->himq[option] = CURL_OPPOSITE;
328 /* Error: already queued an enable request */
334 switch(tn->himq[option])
337 /* Error: already negotiating for enable */
340 tn->himq[option] = CURL_EMPTY;
348 switch(tn->him[option])
351 /* Already disabled */
355 tn->him[option] = CURL_WANTNO;
356 send_negotiation(conn, CURL_DONT, option);
360 switch(tn->himq[option])
363 /* Already negotiating for NO */
366 tn->himq[option] = CURL_EMPTY;
372 switch(tn->himq[option])
375 tn->himq[option] = CURL_OPPOSITE;
386 void rec_will(struct connectdata *conn, int option)
388 struct TELNET *tn = (struct TELNET *)conn->proto.telnet;
389 switch(tn->him[option])
392 if(tn->him_preferred[option] == CURL_YES)
394 tn->him[option] = CURL_YES;
395 send_negotiation(conn, CURL_DO, option);
399 send_negotiation(conn, CURL_DONT, option);
404 /* Already enabled */
408 switch(tn->himq[option])
411 /* Error: DONT answered by WILL */
412 tn->him[option] = CURL_NO;
415 /* Error: DONT answered by WILL */
416 tn->him[option] = CURL_YES;
417 tn->himq[option] = CURL_EMPTY;
423 switch(tn->himq[option])
426 tn->him[option] = CURL_YES;
429 tn->him[option] = CURL_WANTNO;
430 tn->himq[option] = CURL_EMPTY;
431 send_negotiation(conn, CURL_DONT, option);
439 void rec_wont(struct connectdata *conn, int option)
441 struct TELNET *tn = (struct TELNET *)conn->proto.telnet;
442 switch(tn->him[option])
445 /* Already disabled */
449 tn->him[option] = CURL_NO;
450 send_negotiation(conn, CURL_DONT, option);
454 switch(tn->himq[option])
457 tn->him[option] = CURL_NO;
461 tn->him[option] = CURL_WANTYES;
462 tn->himq[option] = CURL_EMPTY;
463 send_negotiation(conn, CURL_DO, option);
469 switch(tn->himq[option])
472 tn->him[option] = CURL_NO;
475 tn->him[option] = CURL_NO;
476 tn->himq[option] = CURL_EMPTY;
484 set_local_option(struct connectdata *conn, int option, int newstate)
486 struct TELNET *tn = (struct TELNET *)conn->proto.telnet;
487 if(newstate == CURL_YES)
489 switch(tn->us[option])
492 tn->us[option] = CURL_WANTYES;
493 send_negotiation(conn, CURL_WILL, option);
497 /* Already enabled */
501 switch(tn->usq[option])
504 /* Already negotiating for CURL_YES, queue the request */
505 tn->usq[option] = CURL_OPPOSITE;
508 /* Error: already queued an enable request */
514 switch(tn->usq[option])
517 /* Error: already negotiating for enable */
520 tn->usq[option] = CURL_EMPTY;
528 switch(tn->us[option])
531 /* Already disabled */
535 tn->us[option] = CURL_WANTNO;
536 send_negotiation(conn, CURL_WONT, option);
540 switch(tn->usq[option])
543 /* Already negotiating for NO */
546 tn->usq[option] = CURL_EMPTY;
552 switch(tn->usq[option])
555 tn->usq[option] = CURL_OPPOSITE;
566 void rec_do(struct connectdata *conn, int option)
568 struct TELNET *tn = (struct TELNET *)conn->proto.telnet;
569 switch(tn->us[option])
572 if(tn->us_preferred[option] == CURL_YES)
574 tn->us[option] = CURL_YES;
575 send_negotiation(conn, CURL_WILL, option);
579 send_negotiation(conn, CURL_WONT, option);
584 /* Already enabled */
588 switch(tn->usq[option])
591 /* Error: DONT answered by WILL */
592 tn->us[option] = CURL_NO;
595 /* Error: DONT answered by WILL */
596 tn->us[option] = CURL_YES;
597 tn->usq[option] = CURL_EMPTY;
603 switch(tn->usq[option])
606 tn->us[option] = CURL_YES;
609 tn->us[option] = CURL_WANTNO;
610 tn->himq[option] = CURL_EMPTY;
611 send_negotiation(conn, CURL_WONT, option);
619 void rec_dont(struct connectdata *conn, int option)
621 struct TELNET *tn = (struct TELNET *)conn->proto.telnet;
622 switch(tn->us[option])
625 /* Already disabled */
629 tn->us[option] = CURL_NO;
630 send_negotiation(conn, CURL_WONT, option);
634 switch(tn->usq[option])
637 tn->us[option] = CURL_NO;
641 tn->us[option] = CURL_WANTYES;
642 tn->usq[option] = CURL_EMPTY;
643 send_negotiation(conn, CURL_WILL, option);
649 switch(tn->usq[option])
652 tn->us[option] = CURL_NO;
655 tn->us[option] = CURL_NO;
656 tn->usq[option] = CURL_EMPTY;
664 static void printsub(struct SessionHandle *data,
665 int direction, /* '<' or '>' */
666 unsigned char *pointer, /* where suboption data is */
667 size_t length) /* length of suboption data */
671 if (data->set.verbose)
675 infof(data, "%s IAC SB ", (direction == '<')? "RCVD":"SENT");
680 i = pointer[length-2];
681 j = pointer[length-1];
683 if (i != CURL_IAC || j != CURL_SE)
685 infof(data, "(terminated by ");
686 if (CURL_TELOPT_OK(i))
687 infof(data, "%s ", CURL_TELOPT(i));
688 else if (CURL_TELCMD_OK(i))
689 infof(data, "%s ", CURL_TELCMD(i));
691 infof(data, "%d ", i);
692 if (CURL_TELOPT_OK(j))
693 infof(data, "%s", CURL_TELOPT(j));
694 else if (CURL_TELCMD_OK(j))
695 infof(data, "%s", CURL_TELCMD(j));
697 infof(data, "%d", j);
698 infof(data, ", not IAC SE!) ");
705 infof(data, "(Empty suboption?)");
709 if (CURL_TELOPT_OK(pointer[0])) {
711 case CURL_TELOPT_TTYPE:
712 case CURL_TELOPT_XDISPLOC:
713 case CURL_TELOPT_NEW_ENVIRON:
714 infof(data, "%s", CURL_TELOPT(pointer[0]));
717 infof(data, "%s (unsupported)", CURL_TELOPT(pointer[0]));
722 infof(data, "%d (unknown)", pointer[i]);
725 case CURL_TELQUAL_IS:
728 case CURL_TELQUAL_SEND:
729 infof(data, " SEND");
731 case CURL_TELQUAL_INFO:
732 infof(data, " INFO/REPLY");
734 case CURL_TELQUAL_NAME:
735 infof(data, " NAME");
740 case CURL_TELOPT_TTYPE:
741 case CURL_TELOPT_XDISPLOC:
743 infof(data, " \"%s\"", &pointer[2]);
745 case CURL_TELOPT_NEW_ENVIRON:
746 if(pointer[1] == CURL_TELQUAL_IS) {
748 for(i = 3;i < length;i++) {
750 case CURL_NEW_ENV_VAR:
753 case CURL_NEW_ENV_VALUE:
757 infof(data, "%c", pointer[i]);
764 for (i = 2; i < length; i++)
765 infof(data, " %.2x", pointer[i]);
776 static CURLcode check_telnet_options(struct connectdata *conn)
778 struct curl_slist *head;
779 char option_keyword[128];
780 char option_arg[256];
782 struct SessionHandle *data = conn->data;
783 struct TELNET *tn = (struct TELNET *)conn->proto.telnet;
785 /* Add the user name as an environment variable if it
786 was given on the command line */
787 if(conn->bits.user_passwd)
789 snprintf(option_arg, sizeof(option_arg), "USER,%s", conn->user);
790 tn->telnet_vars = curl_slist_append(tn->telnet_vars, option_arg);
792 tn->us_preferred[CURL_TELOPT_NEW_ENVIRON] = CURL_YES;
795 for(head = data->set.telnet_options; head; head=head->next) {
796 if(sscanf(head->data, "%127[^= ]%*[ =]%255s",
797 option_keyword, option_arg) == 2) {
800 if(curl_strequal(option_keyword, "TTYPE")) {
801 strncpy(tn->subopt_ttype, option_arg, 31);
802 tn->subopt_ttype[31] = 0; /* String termination */
803 tn->us_preferred[CURL_TELOPT_TTYPE] = CURL_YES;
807 /* Display variable */
808 if(curl_strequal(option_keyword, "XDISPLOC")) {
809 strncpy(tn->subopt_xdisploc, option_arg, 127);
810 tn->subopt_xdisploc[127] = 0; /* String termination */
811 tn->us_preferred[CURL_TELOPT_XDISPLOC] = CURL_YES;
815 /* Environment variable */
816 if(curl_strequal(option_keyword, "NEW_ENV")) {
817 buf = strdup(option_arg);
819 return CURLE_OUT_OF_MEMORY;
820 tn->telnet_vars = curl_slist_append(tn->telnet_vars, buf);
821 tn->us_preferred[CURL_TELOPT_NEW_ENVIRON] = CURL_YES;
825 failf(data, "Unknown telnet option %s", head->data);
826 return CURLE_UNKNOWN_TELNET_OPTION;
828 failf(data, "Syntax error in telnet option: %s", head->data);
829 return CURLE_TELNET_OPTION_SYNTAX;
839 * Look at the sub-option buffer, and try to be helpful to the other
843 static void suboption(struct connectdata *conn)
845 struct curl_slist *v;
846 unsigned char temp[2048];
847 ssize_t bytes_written;
852 struct SessionHandle *data = conn->data;
853 struct TELNET *tn = (struct TELNET *)conn->proto.telnet;
855 printsub(data, '<', (unsigned char *)tn->subbuffer, CURL_SB_LEN(tn)+2);
856 switch (CURL_SB_GET(tn)) {
857 case CURL_TELOPT_TTYPE:
858 len = strlen(tn->subopt_ttype) + 4 + 2;
859 snprintf((char *)temp, sizeof(temp),
860 "%c%c%c%c%s%c%c", CURL_IAC, CURL_SB, CURL_TELOPT_TTYPE,
861 CURL_TELQUAL_IS, tn->subopt_ttype, CURL_IAC, CURL_SE);
862 bytes_written = swrite(conn->sock[FIRSTSOCKET], temp, len);
863 printsub(data, '>', &temp[2], len-2);
865 case CURL_TELOPT_XDISPLOC:
866 len = strlen(tn->subopt_xdisploc) + 4 + 2;
867 snprintf((char *)temp, sizeof(temp),
868 "%c%c%c%c%s%c%c", CURL_IAC, CURL_SB, CURL_TELOPT_XDISPLOC,
869 CURL_TELQUAL_IS, tn->subopt_xdisploc, CURL_IAC, CURL_SE);
870 bytes_written = swrite(conn->sock[FIRSTSOCKET], temp, len);
871 printsub(data, '>', &temp[2], len-2);
873 case CURL_TELOPT_NEW_ENVIRON:
874 snprintf((char *)temp, sizeof(temp),
875 "%c%c%c%c", CURL_IAC, CURL_SB, CURL_TELOPT_NEW_ENVIRON,
879 for(v = tn->telnet_vars;v;v = v->next) {
880 tmplen = (strlen(v->data) + 1);
881 /* Add the variable only if it fits */
882 if(len + tmplen < (int)sizeof(temp)-6) {
883 sscanf(v->data, "%127[^,],%127s", varname, varval);
884 snprintf((char *)&temp[len], sizeof(temp) - len,
885 "%c%s%c%s", CURL_NEW_ENV_VAR, varname,
886 CURL_NEW_ENV_VALUE, varval);
890 snprintf((char *)&temp[len], sizeof(temp) - len,
891 "%c%c", CURL_IAC, CURL_SE);
893 bytes_written = swrite(conn->sock[FIRSTSOCKET], temp, len);
894 printsub(data, '>', &temp[2], len-2);
901 void telrcv(struct connectdata *conn,
902 unsigned char *inbuf, /* Data received from socket */
903 ssize_t count) /* Number of bytes received */
907 struct SessionHandle *data = conn->data;
908 struct TELNET *tn = (struct TELNET *)conn->proto.telnet;
914 switch (tn->telrcv_state)
917 tn->telrcv_state = CURL_TS_DATA;
920 break; /* Ignore \0 after CR */
923 Curl_client_write(data, CLIENTWRITE_BODY, (char *)&c, 1);
929 tn->telrcv_state = CURL_TS_IAC;
934 tn->telrcv_state = CURL_TS_CR;
937 Curl_client_write(data, CLIENTWRITE_BODY, (char *)&c, 1);
945 tn->telrcv_state = CURL_TS_WILL;
948 tn->telrcv_state = CURL_TS_WONT;
951 tn->telrcv_state = CURL_TS_DO;
954 tn->telrcv_state = CURL_TS_DONT;
958 tn->telrcv_state = CURL_TS_SB;
961 Curl_client_write(data, CLIENTWRITE_BODY, (char *)&c, 1);
967 printoption(data, "RCVD", CURL_IAC, c);
970 tn->telrcv_state = CURL_TS_DATA;
974 printoption(data, "RCVD", CURL_WILL, c);
975 tn->please_negotiate = 1;
977 tn->telrcv_state = CURL_TS_DATA;
981 printoption(data, "RCVD", CURL_WONT, c);
982 tn->please_negotiate = 1;
984 tn->telrcv_state = CURL_TS_DATA;
988 printoption(data, "RCVD", CURL_DO, c);
989 tn->please_negotiate = 1;
991 tn->telrcv_state = CURL_TS_DATA;
995 printoption(data, "RCVD", CURL_DONT, c);
996 tn->please_negotiate = 1;
998 tn->telrcv_state = CURL_TS_DATA;
1004 tn->telrcv_state = CURL_TS_SE;
1008 CURL_SB_ACCUM(tn,c);
1018 * This is an error. We only expect to get "IAC IAC" or "IAC SE".
1019 * Several things may have happend. An IAC was not doubled, the
1020 * IAC SE was left off, or another option got inserted into the
1021 * suboption are all possibilities. If we assume that the IAC was
1022 * not doubled, and really the IAC SE was left off, we could get
1023 * into an infinate loop here. So, instead, we terminate the
1024 * suboption, and process the partial suboption if we can.
1026 CURL_SB_ACCUM(tn, CURL_IAC);
1027 CURL_SB_ACCUM(tn, c);
1028 tn->subpointer -= 2;
1031 printoption(data, "In SUBOPTION processing, RCVD", CURL_IAC, c);
1032 suboption(conn); /* handle sub-option */
1033 tn->telrcv_state = CURL_TS_IAC;
1036 CURL_SB_ACCUM(tn,c);
1037 tn->telrcv_state = CURL_TS_SB;
1041 CURL_SB_ACCUM(tn, CURL_IAC);
1042 CURL_SB_ACCUM(tn, CURL_SE);
1043 tn->subpointer -= 2;
1045 suboption(conn); /* handle sub-option */
1046 tn->telrcv_state = CURL_TS_DATA;
1053 CURLcode Curl_telnet_done(struct connectdata *conn, CURLcode status)
1055 struct TELNET *tn = (struct TELNET *)conn->proto.telnet;
1056 (void)status; /* unused */
1058 curl_slist_free_all(tn->telnet_vars);
1060 free(conn->proto.telnet);
1061 conn->proto.telnet = NULL;
1066 CURLcode Curl_telnet(struct connectdata *conn, bool *done)
1069 struct SessionHandle *data = conn->data;
1070 curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
1073 WSOCK2_FUNC close_event_func;
1074 WSOCK2_FUNC create_event_func;
1075 WSOCK2_FUNC event_select_func;
1076 WSOCK2_FUNC enum_netevents_func;
1077 WSAEVENT event_handle;
1078 WSANETWORKEVENTS events;
1079 HANDLE stdin_handle;
1084 DWORD readfile_read;
1087 struct pollfd pfd[2];
1091 char *buf = data->state.buffer;
1094 *done = TRUE; /* uncontionally */
1096 code = init_telnet(conn);
1100 tn = (struct TELNET *)conn->proto.telnet;
1102 code = check_telnet_options(conn);
1108 ** This functionality only works with WinSock >= 2.0. So,
1109 ** make sure have it.
1111 code = check_wsock2(data);
1115 /* OK, so we have WinSock 2.0. We need to dynamically */
1116 /* load ws2_32.dll and get the function pointers we need. */
1117 wsock2 = LoadLibrary("WS2_32.DLL");
1118 if (wsock2 == NULL) {
1119 failf(data,"failed to load WS2_32.DLL (%d)",GetLastError());
1120 return CURLE_FAILED_INIT;
1123 /* Grab a pointer to WSACreateEvent */
1124 create_event_func = GetProcAddress(wsock2,"WSACreateEvent");
1125 if (create_event_func == NULL) {
1126 failf(data,"failed to find WSACreateEvent function (%d)",
1128 FreeLibrary(wsock2);
1129 return CURLE_FAILED_INIT;
1132 /* And WSACloseEvent */
1133 close_event_func = GetProcAddress(wsock2,"WSACloseEvent");
1134 if (create_event_func == NULL) {
1135 failf(data,"failed to find WSACloseEvent function (%d)",
1137 FreeLibrary(wsock2);
1138 return CURLE_FAILED_INIT;
1141 /* And WSAEventSelect */
1142 event_select_func = GetProcAddress(wsock2,"WSAEventSelect");
1143 if (event_select_func == NULL) {
1144 failf(data,"failed to find WSAEventSelect function (%d)",
1146 FreeLibrary(wsock2);
1147 return CURLE_FAILED_INIT;
1150 /* And WSAEnumNetworkEvents */
1151 enum_netevents_func = GetProcAddress(wsock2,"WSAEnumNetworkEvents");
1152 if (enum_netevents_func == NULL) {
1153 failf(data,"failed to find WSAEnumNetworkEvents function (%d)",
1155 FreeLibrary(wsock2);
1156 return CURLE_FAILED_INIT;
1159 /* We want to wait for both stdin and the socket. Since
1160 ** the select() function in winsock only works on sockets
1161 ** we have to use the WaitForMultipleObjects() call.
1164 /* First, create a sockets event object */
1165 event_handle = (WSAEVENT)create_event_func();
1166 if (event_handle == WSA_INVALID_EVENT) {
1167 failf(data,"WSACreateEvent failed (%d)",WSAGetLastError());
1168 FreeLibrary(wsock2);
1169 return CURLE_FAILED_INIT;
1172 /* The get the Windows file handle for stdin */
1173 stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
1175 /* Create the list of objects to wait for */
1176 objs[0] = event_handle;
1177 objs[1] = stdin_handle;
1179 /* Tell winsock what events we want to listen to */
1180 if(event_select_func(sockfd, event_handle, FD_READ|FD_CLOSE) == SOCKET_ERROR) {
1181 close_event_func(event_handle);
1182 FreeLibrary(wsock2);
1186 /* If stdin_handle is a pipe, use PeekNamedPipe() method to check it,
1187 else use the old WaitForMultipleObjects() way */
1188 if(GetFileType(stdin_handle) == FILE_TYPE_PIPE) {
1189 /* Don't wait for stdin_handle, just wait for event_handle */
1191 /* Check stdin_handle per 100 milliseconds */
1195 wait_timeout = INFINITE;
1198 /* Keep on listening and act on events */
1200 waitret = WaitForMultipleObjects(obj_count, objs, FALSE, wait_timeout);
1204 unsigned char outbuf[2];
1206 ssize_t bytes_written;
1210 if(!PeekNamedPipe(stdin_handle, NULL, 0, NULL, &readfile_read, NULL)) {
1214 nread = readfile_read;
1219 if(!ReadFile(stdin_handle, buf, sizeof(data->state.buffer),
1220 &readfile_read, NULL)) {
1224 nread = readfile_read;
1227 outbuf[0] = *buffer++;
1229 if(outbuf[0] == CURL_IAC)
1230 outbuf[out_count++] = CURL_IAC;
1232 Curl_write(conn, conn->sock[FIRSTSOCKET], outbuf,
1233 out_count, &bytes_written);
1239 case WAIT_OBJECT_0 + 1:
1241 unsigned char outbuf[2];
1243 ssize_t bytes_written;
1246 if(!ReadFile(stdin_handle, buf, sizeof(data->state.buffer),
1247 &readfile_read, NULL)) {
1251 nread = readfile_read;
1254 outbuf[0] = *buffer++;
1256 if(outbuf[0] == CURL_IAC)
1257 outbuf[out_count++] = CURL_IAC;
1259 Curl_write(conn, conn->sock[FIRSTSOCKET], outbuf,
1260 out_count, &bytes_written);
1266 if(enum_netevents_func(sockfd, event_handle, &events)
1268 if(events.lNetworkEvents & FD_READ) {
1269 /* This reallu OUGHT to check its return code. */
1270 (void)Curl_read(conn, sockfd, buf, BUFSIZE - 1, &nread);
1272 telrcv(conn, (unsigned char *)buf, nread);
1276 /* Negotiate if the peer has started negotiating,
1277 otherwise don't. We don't want to speak telnet with
1278 non-telnet servers, like POP or SMTP. */
1279 if(tn->please_negotiate && !tn->already_negotiated) {
1281 tn->already_negotiated = 1;
1285 if(events.lNetworkEvents & FD_CLOSE) {
1293 /* We called WSACreateEvent, so call WSACloseEvent */
1294 if (close_event_func(event_handle) == FALSE) {
1295 infof(data,"WSACloseEvent failed (%d)",WSAGetLastError());
1298 /* "Forget" pointers into the library we're about to free */
1299 create_event_func = NULL;
1300 close_event_func = NULL;
1301 event_select_func = NULL;
1302 enum_netevents_func = NULL;
1304 /* We called LoadLibrary, so call FreeLibrary */
1305 if (!FreeLibrary(wsock2))
1306 infof(data,"FreeLibrary(wsock2) failed (%d)",GetLastError());
1309 pfd[0].events = POLLIN;
1311 pfd[1].events = POLLIN;
1312 interval_ms = 1 * 1000;
1315 switch (Curl_poll(pfd, 2, interval_ms)) {
1316 case -1: /* error, stop reading */
1319 case 0: /* timeout */
1321 default: /* read! */
1322 if(pfd[1].revents & POLLIN) { /* read from stdin */
1323 unsigned char outbuf[2];
1325 ssize_t bytes_written;
1328 nread = read(0, buf, 255);
1331 outbuf[0] = *buffer++;
1333 if(outbuf[0] == CURL_IAC)
1334 outbuf[out_count++] = CURL_IAC;
1336 Curl_write(conn, conn->sock[FIRSTSOCKET], outbuf,
1337 out_count, &bytes_written);
1341 if(pfd[0].revents & POLLIN) {
1342 /* This OUGHT to check the return code... */
1343 (void)Curl_read(conn, sockfd, buf, BUFSIZE - 1, &nread);
1345 /* if we receive 0 or less here, the server closed the connection and
1346 we bail out from this! */
1352 telrcv(conn, (unsigned char *)buf, nread);
1354 /* Negotiate if the peer has started negotiating,
1355 otherwise don't. We don't want to speak telnet with
1356 non-telnet servers, like POP or SMTP. */
1357 if(tn->please_negotiate && !tn->already_negotiated) {
1359 tn->already_negotiated = 1;
1363 if(data->set.timeout) {
1364 struct timeval now; /* current time */
1366 if(Curl_tvdiff(now, conn->created)/1000 >= data->set.timeout) {
1367 failf(data, "Time-out");
1368 code = CURLE_OPERATION_TIMEOUTED;
1374 /* mark this as "no further transfer wanted" */
1375 Curl_Transfer(conn, -1, -1, FALSE, NULL, -1, NULL);