stdio.h, stdlib.h, string.h, stdarg.h and ctype.h inclusion done in setup_once.h
[platform/upstream/curl.git] / lib / telnet.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
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.
13  *
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.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  ***************************************************************************/
22
23 #include "setup.h"
24
25 #ifndef CURL_DISABLE_TELNET
26
27 #ifdef HAVE_SYS_SOCKET_H
28 #include <sys/socket.h>
29 #endif
30 #ifdef HAVE_NETINET_IN_H
31 #include <netinet/in.h>
32 #endif
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36 #ifdef HAVE_NETDB_H
37 #include <netdb.h>
38 #endif
39 #ifdef HAVE_ARPA_INET_H
40 #include <arpa/inet.h>
41 #endif
42 #ifdef HAVE_NET_IF_H
43 #include <net/if.h>
44 #endif
45 #ifdef HAVE_SYS_IOCTL_H
46 #include <sys/ioctl.h>
47 #endif
48
49 #ifdef HAVE_SYS_PARAM_H
50 #include <sys/param.h>
51 #endif
52
53 #include "urldata.h"
54 #include <curl/curl.h>
55 #include "transfer.h"
56 #include "sendf.h"
57 #include "telnet.h"
58 #include "connect.h"
59 #include "progress.h"
60
61 #define _MPRINTF_REPLACE /* use our functions only */
62 #include <curl/mprintf.h>
63
64 #define  TELOPTS
65 #define  TELCMDS
66
67 #include "arpa_telnet.h"
68 #include "curl_memory.h"
69 #include "select.h"
70 #include "strequal.h"
71 #include "rawstr.h"
72
73 /* The last #include file should be: */
74 #include "memdebug.h"
75
76 #define SUBBUFSIZE 512
77
78 #define CURL_SB_CLEAR(x)  x->subpointer = x->subbuffer;
79 #define CURL_SB_TERM(x)   { x->subend = x->subpointer; CURL_SB_CLEAR(x); }
80 #define CURL_SB_ACCUM(x,c) \
81   if(x->subpointer < (x->subbuffer+sizeof x->subbuffer)) { \
82     *x->subpointer++ = (c); \
83   }
84
85 #define  CURL_SB_GET(x) ((*x->subpointer++)&0xff)
86 #define  CURL_SB_PEEK(x)   ((*x->subpointer)&0xff)
87 #define  CURL_SB_EOF(x) (x->subpointer >= x->subend)
88 #define  CURL_SB_LEN(x) (x->subend - x->subpointer)
89
90 #ifdef CURL_DISABLE_VERBOSE_STRINGS
91 #define printoption(a,b,c,d)  do { } while(0)
92 #endif
93
94 #ifdef USE_WINSOCK
95 typedef FARPROC WSOCK2_FUNC;
96 static CURLcode check_wsock2 ( struct SessionHandle *data );
97 #endif
98
99 static
100 CURLcode telrcv(struct connectdata *,
101                 const unsigned char *inbuf, /* Data received from socket */
102                 ssize_t count);             /* Number of bytes received */
103
104 #ifndef CURL_DISABLE_VERBOSE_STRINGS
105 static void printoption(struct SessionHandle *data,
106                         const char *direction,
107                         int cmd, int option);
108 #endif
109
110 static void negotiate(struct connectdata *);
111 static void send_negotiation(struct connectdata *, int cmd, int option);
112 static void set_local_option(struct connectdata *, int cmd, int option);
113 static void set_remote_option(struct connectdata *, int cmd, int option);
114
115 static void printsub(struct SessionHandle *data,
116                      int direction, unsigned char *pointer,
117                      size_t length);
118 static void suboption(struct connectdata *);
119
120 static CURLcode telnet_do(struct connectdata *conn, bool *done);
121 static CURLcode telnet_done(struct connectdata *conn,
122                                  CURLcode, bool premature);
123
124 /* For negotiation compliant to RFC 1143 */
125 #define CURL_NO          0
126 #define CURL_YES         1
127 #define CURL_WANTYES     2
128 #define CURL_WANTNO      3
129
130 #define CURL_EMPTY       0
131 #define CURL_OPPOSITE    1
132
133 /*
134  * Telnet receiver states for fsm
135  */
136 typedef enum
137 {
138    CURL_TS_DATA = 0,
139    CURL_TS_IAC,
140    CURL_TS_WILL,
141    CURL_TS_WONT,
142    CURL_TS_DO,
143    CURL_TS_DONT,
144    CURL_TS_CR,
145    CURL_TS_SB,   /* sub-option collection */
146    CURL_TS_SE   /* looking for sub-option end */
147 } TelnetReceive;
148
149 struct TELNET {
150   int please_negotiate;
151   int already_negotiated;
152   int us[256];
153   int usq[256];
154   int us_preferred[256];
155   int him[256];
156   int himq[256];
157   int him_preferred[256];
158   char subopt_ttype[32];             /* Set with suboption TTYPE */
159   char subopt_xdisploc[128];          /* Set with suboption XDISPLOC */
160   struct curl_slist *telnet_vars; /* Environment variables */
161
162   /* suboptions */
163   unsigned char subbuffer[SUBBUFSIZE];
164   unsigned char *subpointer, *subend;      /* buffer for sub-options */
165
166   TelnetReceive telrcv_state;
167 };
168
169
170 /*
171  * TELNET protocol handler.
172  */
173
174 const struct Curl_handler Curl_handler_telnet = {
175   "TELNET",                             /* scheme */
176   ZERO_NULL,                            /* setup_connection */
177   telnet_do,                            /* do_it */
178   telnet_done,                          /* done */
179   ZERO_NULL,                            /* do_more */
180   ZERO_NULL,                            /* connect_it */
181   ZERO_NULL,                            /* connecting */
182   ZERO_NULL,                            /* doing */
183   ZERO_NULL,                            /* proto_getsock */
184   ZERO_NULL,                            /* doing_getsock */
185   ZERO_NULL,                            /* perform_getsock */
186   ZERO_NULL,                            /* disconnect */
187   ZERO_NULL,                            /* readwrite */
188   PORT_TELNET,                          /* defport */
189   CURLPROTO_TELNET,                     /* protocol */
190   PROTOPT_NONE                          /* flags */
191 };
192
193
194 #ifdef USE_WINSOCK
195 static CURLcode
196 check_wsock2 ( struct SessionHandle *data )
197 {
198   int err;
199   WORD wVersionRequested;
200   WSADATA wsaData;
201
202   DEBUGASSERT(data);
203
204   /* telnet requires at least WinSock 2.0 so ask for it. */
205   wVersionRequested = MAKEWORD(2, 0);
206
207   err = WSAStartup(wVersionRequested, &wsaData);
208
209   /* We must've called this once already, so this call */
210   /* should always succeed.  But, just in case... */
211   if(err != 0) {
212     failf(data,"WSAStartup failed (%d)",err);
213     return CURLE_FAILED_INIT;
214   }
215
216   /* We have to have a WSACleanup call for every successful */
217   /* WSAStartup call. */
218   WSACleanup();
219
220   /* Check that our version is supported */
221   if(LOBYTE(wsaData.wVersion) != LOBYTE(wVersionRequested) ||
222       HIBYTE(wsaData.wVersion) != HIBYTE(wVersionRequested)) {
223       /* Our version isn't supported */
224       failf(data,"insufficient winsock version to support "
225             "telnet");
226       return CURLE_FAILED_INIT;
227   }
228
229   /* Our version is supported */
230   return CURLE_OK;
231 }
232 #endif
233
234 static
235 CURLcode init_telnet(struct connectdata *conn)
236 {
237   struct TELNET *tn;
238
239   tn = calloc(1, sizeof(struct TELNET));
240   if(!tn)
241     return CURLE_OUT_OF_MEMORY;
242
243   conn->data->state.proto.telnet = (void *)tn; /* make us known */
244
245   tn->telrcv_state = CURL_TS_DATA;
246
247   /* Init suboptions */
248   CURL_SB_CLEAR(tn);
249
250   /* Set the options we want by default */
251   tn->us_preferred[CURL_TELOPT_BINARY] = CURL_YES;
252   tn->us_preferred[CURL_TELOPT_SGA] = CURL_YES;
253   tn->him_preferred[CURL_TELOPT_BINARY] = CURL_YES;
254   tn->him_preferred[CURL_TELOPT_SGA] = CURL_YES;
255
256   return CURLE_OK;
257 }
258
259 static void negotiate(struct connectdata *conn)
260 {
261   int i;
262   struct TELNET *tn = (struct TELNET *) conn->data->state.proto.telnet;
263
264   for(i = 0;i < CURL_NTELOPTS;i++) {
265     if(tn->us_preferred[i] == CURL_YES)
266       set_local_option(conn, i, CURL_YES);
267
268     if(tn->him_preferred[i] == CURL_YES)
269       set_remote_option(conn, i, CURL_YES);
270   }
271 }
272
273 #ifndef CURL_DISABLE_VERBOSE_STRINGS
274 static void printoption(struct SessionHandle *data,
275                         const char *direction, int cmd, int option)
276 {
277   const char *fmt;
278   const char *opt;
279
280   if(data->set.verbose) {
281     if(cmd == CURL_IAC) {
282       if(CURL_TELCMD_OK(option))
283         infof(data, "%s IAC %s\n", direction, CURL_TELCMD(option));
284       else
285         infof(data, "%s IAC %d\n", direction, option);
286     }
287     else {
288       fmt = (cmd == CURL_WILL) ? "WILL" : (cmd == CURL_WONT) ? "WONT" :
289         (cmd == CURL_DO) ? "DO" : (cmd == CURL_DONT) ? "DONT" : 0;
290       if(fmt) {
291         if(CURL_TELOPT_OK(option))
292           opt = CURL_TELOPT(option);
293         else if(option == CURL_TELOPT_EXOPL)
294           opt = "EXOPL";
295         else
296           opt = NULL;
297
298         if(opt)
299           infof(data, "%s %s %s\n", direction, fmt, opt);
300         else
301           infof(data, "%s %s %d\n", direction, fmt, option);
302       }
303       else
304         infof(data, "%s %d %d\n", direction, cmd, option);
305     }
306   }
307 }
308 #endif
309
310 static void send_negotiation(struct connectdata *conn, int cmd, int option)
311 {
312    unsigned char buf[3];
313    ssize_t bytes_written;
314    int err;
315    struct SessionHandle *data = conn->data;
316
317    buf[0] = CURL_IAC;
318    buf[1] = (unsigned char)cmd;
319    buf[2] = (unsigned char)option;
320
321    bytes_written = swrite(conn->sock[FIRSTSOCKET], buf, 3);
322    if(bytes_written < 0) {
323      err = SOCKERRNO;
324      failf(data,"Sending data failed (%d)",err);
325    }
326
327    printoption(conn->data, "SENT", cmd, option);
328 }
329
330 static
331 void set_remote_option(struct connectdata *conn, int option, int newstate)
332 {
333   struct TELNET *tn = (struct TELNET *)conn->data->state.proto.telnet;
334   if(newstate == CURL_YES) {
335     switch(tn->him[option]) {
336     case CURL_NO:
337       tn->him[option] = CURL_WANTYES;
338       send_negotiation(conn, CURL_DO, option);
339       break;
340
341     case CURL_YES:
342       /* Already enabled */
343       break;
344
345     case CURL_WANTNO:
346       switch(tn->himq[option]) {
347       case CURL_EMPTY:
348         /* Already negotiating for CURL_YES, queue the request */
349         tn->himq[option] = CURL_OPPOSITE;
350         break;
351       case CURL_OPPOSITE:
352         /* Error: already queued an enable request */
353         break;
354       }
355       break;
356
357     case CURL_WANTYES:
358       switch(tn->himq[option]) {
359       case CURL_EMPTY:
360         /* Error: already negotiating for enable */
361         break;
362       case CURL_OPPOSITE:
363         tn->himq[option] = CURL_EMPTY;
364         break;
365       }
366       break;
367     }
368   }
369   else { /* NO */
370     switch(tn->him[option]) {
371     case CURL_NO:
372       /* Already disabled */
373       break;
374
375     case CURL_YES:
376       tn->him[option] = CURL_WANTNO;
377       send_negotiation(conn, CURL_DONT, option);
378       break;
379
380     case CURL_WANTNO:
381       switch(tn->himq[option]) {
382       case CURL_EMPTY:
383         /* Already negotiating for NO */
384         break;
385       case CURL_OPPOSITE:
386         tn->himq[option] = CURL_EMPTY;
387         break;
388       }
389       break;
390
391     case CURL_WANTYES:
392       switch(tn->himq[option]) {
393       case CURL_EMPTY:
394         tn->himq[option] = CURL_OPPOSITE;
395         break;
396       case CURL_OPPOSITE:
397         break;
398       }
399       break;
400     }
401   }
402 }
403
404 static
405 void rec_will(struct connectdata *conn, int option)
406 {
407   struct TELNET *tn = (struct TELNET *)conn->data->state.proto.telnet;
408   switch(tn->him[option]) {
409   case CURL_NO:
410     if(tn->him_preferred[option] == CURL_YES) {
411       tn->him[option] = CURL_YES;
412       send_negotiation(conn, CURL_DO, option);
413     }
414     else
415       send_negotiation(conn, CURL_DONT, option);
416
417     break;
418
419   case CURL_YES:
420     /* Already enabled */
421     break;
422
423   case CURL_WANTNO:
424     switch(tn->himq[option]) {
425     case CURL_EMPTY:
426       /* Error: DONT answered by WILL */
427       tn->him[option] = CURL_NO;
428       break;
429     case CURL_OPPOSITE:
430       /* Error: DONT answered by WILL */
431       tn->him[option] = CURL_YES;
432       tn->himq[option] = CURL_EMPTY;
433       break;
434     }
435     break;
436
437   case CURL_WANTYES:
438     switch(tn->himq[option]) {
439     case CURL_EMPTY:
440       tn->him[option] = CURL_YES;
441       break;
442     case CURL_OPPOSITE:
443       tn->him[option] = CURL_WANTNO;
444       tn->himq[option] = CURL_EMPTY;
445       send_negotiation(conn, CURL_DONT, option);
446       break;
447     }
448     break;
449   }
450 }
451
452 static
453 void rec_wont(struct connectdata *conn, int option)
454 {
455   struct TELNET *tn = (struct TELNET *)conn->data->state.proto.telnet;
456   switch(tn->him[option]) {
457   case CURL_NO:
458     /* Already disabled */
459     break;
460
461   case CURL_YES:
462     tn->him[option] = CURL_NO;
463     send_negotiation(conn, CURL_DONT, option);
464     break;
465
466   case CURL_WANTNO:
467     switch(tn->himq[option]) {
468     case CURL_EMPTY:
469       tn->him[option] = CURL_NO;
470       break;
471
472     case CURL_OPPOSITE:
473       tn->him[option] = CURL_WANTYES;
474       tn->himq[option] = CURL_EMPTY;
475       send_negotiation(conn, CURL_DO, option);
476       break;
477     }
478     break;
479
480   case CURL_WANTYES:
481     switch(tn->himq[option]) {
482     case CURL_EMPTY:
483       tn->him[option] = CURL_NO;
484       break;
485     case CURL_OPPOSITE:
486       tn->him[option] = CURL_NO;
487       tn->himq[option] = CURL_EMPTY;
488       break;
489     }
490     break;
491   }
492 }
493
494 static void
495 set_local_option(struct connectdata *conn, int option, int newstate)
496 {
497   struct TELNET *tn = (struct TELNET *)conn->data->state.proto.telnet;
498   if(newstate == CURL_YES) {
499     switch(tn->us[option]) {
500     case CURL_NO:
501       tn->us[option] = CURL_WANTYES;
502       send_negotiation(conn, CURL_WILL, option);
503       break;
504
505     case CURL_YES:
506       /* Already enabled */
507       break;
508
509     case CURL_WANTNO:
510       switch(tn->usq[option]) {
511       case CURL_EMPTY:
512         /* Already negotiating for CURL_YES, queue the request */
513         tn->usq[option] = CURL_OPPOSITE;
514         break;
515       case CURL_OPPOSITE:
516         /* Error: already queued an enable request */
517         break;
518       }
519       break;
520
521     case CURL_WANTYES:
522       switch(tn->usq[option]) {
523       case CURL_EMPTY:
524         /* Error: already negotiating for enable */
525         break;
526       case CURL_OPPOSITE:
527         tn->usq[option] = CURL_EMPTY;
528         break;
529       }
530       break;
531     }
532   }
533   else { /* NO */
534     switch(tn->us[option]) {
535     case CURL_NO:
536       /* Already disabled */
537       break;
538
539     case CURL_YES:
540       tn->us[option] = CURL_WANTNO;
541       send_negotiation(conn, CURL_WONT, option);
542       break;
543
544     case CURL_WANTNO:
545       switch(tn->usq[option]) {
546       case CURL_EMPTY:
547         /* Already negotiating for NO */
548         break;
549       case CURL_OPPOSITE:
550         tn->usq[option] = CURL_EMPTY;
551         break;
552       }
553       break;
554
555     case CURL_WANTYES:
556       switch(tn->usq[option]) {
557       case CURL_EMPTY:
558         tn->usq[option] = CURL_OPPOSITE;
559         break;
560       case CURL_OPPOSITE:
561         break;
562       }
563       break;
564     }
565   }
566 }
567
568 static
569 void rec_do(struct connectdata *conn, int option)
570 {
571   struct TELNET *tn = (struct TELNET *)conn->data->state.proto.telnet;
572   switch(tn->us[option]) {
573   case CURL_NO:
574     if(tn->us_preferred[option] == CURL_YES) {
575       tn->us[option] = CURL_YES;
576       send_negotiation(conn, CURL_WILL, option);
577     }
578     else
579       send_negotiation(conn, CURL_WONT, option);
580     break;
581
582   case CURL_YES:
583     /* Already enabled */
584     break;
585
586   case CURL_WANTNO:
587     switch(tn->usq[option]) {
588     case CURL_EMPTY:
589       /* Error: DONT answered by WILL */
590       tn->us[option] = CURL_NO;
591       break;
592     case CURL_OPPOSITE:
593       /* Error: DONT answered by WILL */
594       tn->us[option] = CURL_YES;
595       tn->usq[option] = CURL_EMPTY;
596       break;
597     }
598     break;
599
600   case CURL_WANTYES:
601     switch(tn->usq[option]) {
602     case CURL_EMPTY:
603       tn->us[option] = CURL_YES;
604       break;
605     case CURL_OPPOSITE:
606       tn->us[option] = CURL_WANTNO;
607       tn->himq[option] = CURL_EMPTY;
608       send_negotiation(conn, CURL_WONT, option);
609       break;
610     }
611     break;
612   }
613 }
614
615 static
616 void rec_dont(struct connectdata *conn, int option)
617 {
618   struct TELNET *tn = (struct TELNET *)conn->data->state.proto.telnet;
619   switch(tn->us[option]) {
620   case CURL_NO:
621     /* Already disabled */
622     break;
623
624   case CURL_YES:
625     tn->us[option] = CURL_NO;
626     send_negotiation(conn, CURL_WONT, option);
627     break;
628
629   case CURL_WANTNO:
630     switch(tn->usq[option]) {
631     case CURL_EMPTY:
632       tn->us[option] = CURL_NO;
633       break;
634
635     case CURL_OPPOSITE:
636       tn->us[option] = CURL_WANTYES;
637       tn->usq[option] = CURL_EMPTY;
638       send_negotiation(conn, CURL_WILL, option);
639       break;
640     }
641     break;
642
643   case CURL_WANTYES:
644     switch(tn->usq[option]) {
645     case CURL_EMPTY:
646       tn->us[option] = CURL_NO;
647       break;
648     case CURL_OPPOSITE:
649       tn->us[option] = CURL_NO;
650       tn->usq[option] = CURL_EMPTY;
651       break;
652     }
653     break;
654   }
655 }
656
657
658 static void printsub(struct SessionHandle *data,
659                      int direction,             /* '<' or '>' */
660                      unsigned char *pointer,    /* where suboption data is */
661                      size_t length)             /* length of suboption data */
662 {
663   unsigned int i = 0;
664
665   if(data->set.verbose) {
666     if(direction) {
667       infof(data, "%s IAC SB ", (direction == '<')? "RCVD":"SENT");
668       if(length >= 3) {
669         int j;
670
671         i = pointer[length-2];
672         j = pointer[length-1];
673
674         if(i != CURL_IAC || j != CURL_SE) {
675           infof(data, "(terminated by ");
676           if(CURL_TELOPT_OK(i))
677             infof(data, "%s ", CURL_TELOPT(i));
678           else if(CURL_TELCMD_OK(i))
679             infof(data, "%s ", CURL_TELCMD(i));
680           else
681             infof(data, "%u ", i);
682           if(CURL_TELOPT_OK(j))
683             infof(data, "%s", CURL_TELOPT(j));
684           else if(CURL_TELCMD_OK(j))
685             infof(data, "%s", CURL_TELCMD(j));
686           else
687             infof(data, "%d", j);
688           infof(data, ", not IAC SE!) ");
689         }
690       }
691       length -= 2;
692     }
693     if(length < 1) {
694       infof(data, "(Empty suboption?)");
695       return;
696     }
697
698     if(CURL_TELOPT_OK(pointer[0])) {
699       switch(pointer[0]) {
700         case CURL_TELOPT_TTYPE:
701         case CURL_TELOPT_XDISPLOC:
702         case CURL_TELOPT_NEW_ENVIRON:
703           infof(data, "%s", CURL_TELOPT(pointer[0]));
704           break;
705         default:
706           infof(data, "%s (unsupported)", CURL_TELOPT(pointer[0]));
707           break;
708       }
709     }
710     else
711       infof(data, "%d (unknown)", pointer[i]);
712
713     switch(pointer[1]) {
714       case CURL_TELQUAL_IS:
715         infof(data, " IS");
716         break;
717       case CURL_TELQUAL_SEND:
718         infof(data, " SEND");
719         break;
720       case CURL_TELQUAL_INFO:
721         infof(data, " INFO/REPLY");
722         break;
723       case CURL_TELQUAL_NAME:
724         infof(data, " NAME");
725         break;
726     }
727
728     switch(pointer[0]) {
729       case CURL_TELOPT_TTYPE:
730       case CURL_TELOPT_XDISPLOC:
731         pointer[length] = 0;
732         infof(data, " \"%s\"", &pointer[2]);
733         break;
734       case CURL_TELOPT_NEW_ENVIRON:
735         if(pointer[1] == CURL_TELQUAL_IS) {
736           infof(data, " ");
737           for(i = 3;i < length;i++) {
738             switch(pointer[i]) {
739               case CURL_NEW_ENV_VAR:
740                 infof(data, ", ");
741                 break;
742               case CURL_NEW_ENV_VALUE:
743                 infof(data, " = ");
744                 break;
745               default:
746                 infof(data, "%c", pointer[i]);
747                 break;
748             }
749           }
750         }
751         break;
752       default:
753         for(i = 2; i < length; i++)
754           infof(data, " %.2x", pointer[i]);
755         break;
756     }
757
758     if(direction)
759       infof(data, "\n");
760   }
761 }
762
763 static CURLcode check_telnet_options(struct connectdata *conn)
764 {
765   struct curl_slist *head;
766   char option_keyword[128];
767   char option_arg[256];
768   char *buf;
769   struct SessionHandle *data = conn->data;
770   struct TELNET *tn = (struct TELNET *)conn->data->state.proto.telnet;
771
772   /* Add the user name as an environment variable if it
773      was given on the command line */
774   if(conn->bits.user_passwd) {
775     snprintf(option_arg, sizeof(option_arg), "USER,%s", conn->user);
776     tn->telnet_vars = curl_slist_append(tn->telnet_vars, option_arg);
777
778     tn->us_preferred[CURL_TELOPT_NEW_ENVIRON] = CURL_YES;
779   }
780
781   for(head = data->set.telnet_options; head; head=head->next) {
782     if(sscanf(head->data, "%127[^= ]%*[ =]%255s",
783               option_keyword, option_arg) == 2) {
784
785       /* Terminal type */
786       if(Curl_raw_equal(option_keyword, "TTYPE")) {
787         strncpy(tn->subopt_ttype, option_arg, 31);
788         tn->subopt_ttype[31] = 0; /* String termination */
789         tn->us_preferred[CURL_TELOPT_TTYPE] = CURL_YES;
790         continue;
791       }
792
793       /* Display variable */
794       if(Curl_raw_equal(option_keyword, "XDISPLOC")) {
795         strncpy(tn->subopt_xdisploc, option_arg, 127);
796         tn->subopt_xdisploc[127] = 0; /* String termination */
797         tn->us_preferred[CURL_TELOPT_XDISPLOC] = CURL_YES;
798         continue;
799       }
800
801       /* Environment variable */
802       if(Curl_raw_equal(option_keyword, "NEW_ENV")) {
803         buf = strdup(option_arg);
804         if(!buf)
805           return CURLE_OUT_OF_MEMORY;
806         tn->telnet_vars = curl_slist_append(tn->telnet_vars, buf);
807         tn->us_preferred[CURL_TELOPT_NEW_ENVIRON] = CURL_YES;
808         continue;
809       }
810
811       failf(data, "Unknown telnet option %s", head->data);
812       return CURLE_UNKNOWN_TELNET_OPTION;
813     }
814     else {
815       failf(data, "Syntax error in telnet option: %s", head->data);
816       return CURLE_TELNET_OPTION_SYNTAX;
817     }
818   }
819
820   return CURLE_OK;
821 }
822
823 /*
824  * suboption()
825  *
826  * Look at the sub-option buffer, and try to be helpful to the other
827  * side.
828  */
829
830 static void suboption(struct connectdata *conn)
831 {
832   struct curl_slist *v;
833   unsigned char temp[2048];
834   ssize_t bytes_written;
835   size_t len;
836   size_t tmplen;
837   int err;
838   char varname[128];
839   char varval[128];
840   struct SessionHandle *data = conn->data;
841   struct TELNET *tn = (struct TELNET *)data->state.proto.telnet;
842
843   printsub(data, '<', (unsigned char *)tn->subbuffer, CURL_SB_LEN(tn)+2);
844   switch (CURL_SB_GET(tn)) {
845     case CURL_TELOPT_TTYPE:
846       len = strlen(tn->subopt_ttype) + 4 + 2;
847       snprintf((char *)temp, sizeof(temp),
848                "%c%c%c%c%s%c%c", CURL_IAC, CURL_SB, CURL_TELOPT_TTYPE,
849                CURL_TELQUAL_IS, tn->subopt_ttype, CURL_IAC, CURL_SE);
850       bytes_written = swrite(conn->sock[FIRSTSOCKET], temp, len);
851       if(bytes_written < 0) {
852         err = SOCKERRNO;
853         failf(data,"Sending data failed (%d)",err);
854       }
855       printsub(data, '>', &temp[2], len-2);
856       break;
857     case CURL_TELOPT_XDISPLOC:
858       len = strlen(tn->subopt_xdisploc) + 4 + 2;
859       snprintf((char *)temp, sizeof(temp),
860                "%c%c%c%c%s%c%c", CURL_IAC, CURL_SB, CURL_TELOPT_XDISPLOC,
861                CURL_TELQUAL_IS, tn->subopt_xdisploc, CURL_IAC, CURL_SE);
862       bytes_written = swrite(conn->sock[FIRSTSOCKET], temp, len);
863       if(bytes_written < 0) {
864         err = SOCKERRNO;
865         failf(data,"Sending data failed (%d)",err);
866       }
867       printsub(data, '>', &temp[2], len-2);
868       break;
869     case CURL_TELOPT_NEW_ENVIRON:
870       snprintf((char *)temp, sizeof(temp),
871                "%c%c%c%c", CURL_IAC, CURL_SB, CURL_TELOPT_NEW_ENVIRON,
872                CURL_TELQUAL_IS);
873       len = 4;
874
875       for(v = tn->telnet_vars;v;v = v->next) {
876         tmplen = (strlen(v->data) + 1);
877         /* Add the variable only if it fits */
878         if(len + tmplen < (int)sizeof(temp)-6) {
879           sscanf(v->data, "%127[^,],%127s", varname, varval);
880           snprintf((char *)&temp[len], sizeof(temp) - len,
881                    "%c%s%c%s", CURL_NEW_ENV_VAR, varname,
882                    CURL_NEW_ENV_VALUE, varval);
883           len += tmplen;
884         }
885       }
886       snprintf((char *)&temp[len], sizeof(temp) - len,
887                "%c%c", CURL_IAC, CURL_SE);
888       len += 2;
889       bytes_written = swrite(conn->sock[FIRSTSOCKET], temp, len);
890       if(bytes_written < 0) {
891         err = SOCKERRNO;
892         failf(data,"Sending data failed (%d)",err);
893       }
894       printsub(data, '>', &temp[2], len-2);
895       break;
896   }
897   return;
898 }
899
900 static
901 CURLcode telrcv(struct connectdata *conn,
902                 const unsigned char *inbuf, /* Data received from socket */
903                 ssize_t count)              /* Number of bytes received */
904 {
905   unsigned char c;
906   CURLcode result;
907   int in = 0;
908   int startwrite=-1;
909   struct SessionHandle *data = conn->data;
910   struct TELNET *tn = (struct TELNET *)data->state.proto.telnet;
911
912 #define startskipping()                                       \
913   if(startwrite >= 0) {                                       \
914     result = Curl_client_write(conn,                          \
915                                CLIENTWRITE_BODY,              \
916                                (char *)&inbuf[startwrite],    \
917                                in-startwrite);                \
918     if(result != CURLE_OK)                                    \
919       return result;                                          \
920   }                                                           \
921   startwrite = -1
922
923 #define writebyte() \
924     if(startwrite < 0) \
925       startwrite = in
926
927 #define bufferflush() startskipping()
928
929   while(count--) {
930     c = inbuf[in];
931
932     switch (tn->telrcv_state) {
933     case CURL_TS_CR:
934       tn->telrcv_state = CURL_TS_DATA;
935       if(c == '\0') {
936         startskipping();
937         break;   /* Ignore \0 after CR */
938       }
939       writebyte();
940       break;
941
942     case CURL_TS_DATA:
943       if(c == CURL_IAC) {
944         tn->telrcv_state = CURL_TS_IAC;
945         startskipping();
946         break;
947       }
948       else if(c == '\r')
949         tn->telrcv_state = CURL_TS_CR;
950       writebyte();
951       break;
952
953     case CURL_TS_IAC:
954     process_iac:
955       DEBUGASSERT(startwrite < 0);
956       switch (c) {
957       case CURL_WILL:
958         tn->telrcv_state = CURL_TS_WILL;
959         break;
960       case CURL_WONT:
961         tn->telrcv_state = CURL_TS_WONT;
962         break;
963       case CURL_DO:
964         tn->telrcv_state = CURL_TS_DO;
965         break;
966       case CURL_DONT:
967         tn->telrcv_state = CURL_TS_DONT;
968         break;
969       case CURL_SB:
970         CURL_SB_CLEAR(tn);
971         tn->telrcv_state = CURL_TS_SB;
972         break;
973       case CURL_IAC:
974         tn->telrcv_state = CURL_TS_DATA;
975         writebyte();
976         break;
977       case CURL_DM:
978       case CURL_NOP:
979       case CURL_GA:
980       default:
981         tn->telrcv_state = CURL_TS_DATA;
982         printoption(data, "RCVD", CURL_IAC, c);
983         break;
984       }
985       break;
986
987       case CURL_TS_WILL:
988         printoption(data, "RCVD", CURL_WILL, c);
989         tn->please_negotiate = 1;
990         rec_will(conn, c);
991         tn->telrcv_state = CURL_TS_DATA;
992         break;
993
994       case CURL_TS_WONT:
995         printoption(data, "RCVD", CURL_WONT, c);
996         tn->please_negotiate = 1;
997         rec_wont(conn, c);
998         tn->telrcv_state = CURL_TS_DATA;
999         break;
1000
1001       case CURL_TS_DO:
1002         printoption(data, "RCVD", CURL_DO, c);
1003         tn->please_negotiate = 1;
1004         rec_do(conn, c);
1005         tn->telrcv_state = CURL_TS_DATA;
1006         break;
1007
1008       case CURL_TS_DONT:
1009         printoption(data, "RCVD", CURL_DONT, c);
1010         tn->please_negotiate = 1;
1011         rec_dont(conn, c);
1012         tn->telrcv_state = CURL_TS_DATA;
1013         break;
1014
1015       case CURL_TS_SB:
1016         if(c == CURL_IAC)
1017           tn->telrcv_state = CURL_TS_SE;
1018         else
1019           CURL_SB_ACCUM(tn,c);
1020         break;
1021
1022       case CURL_TS_SE:
1023         if(c != CURL_SE) {
1024           if(c != CURL_IAC) {
1025             /*
1026              * This is an error.  We only expect to get "IAC IAC" or "IAC SE".
1027              * Several things may have happened.  An IAC was not doubled, the
1028              * IAC SE was left off, or another option got inserted into the
1029              * suboption are all possibilities.  If we assume that the IAC was
1030              * not doubled, and really the IAC SE was left off, we could get
1031              * into an infinate loop here.  So, instead, we terminate the
1032              * suboption, and process the partial suboption if we can.
1033              */
1034             CURL_SB_ACCUM(tn, CURL_IAC);
1035             CURL_SB_ACCUM(tn, c);
1036             tn->subpointer -= 2;
1037             CURL_SB_TERM(tn);
1038
1039             printoption(data, "In SUBOPTION processing, RCVD", CURL_IAC, c);
1040             suboption(conn);   /* handle sub-option */
1041             tn->telrcv_state = CURL_TS_IAC;
1042             goto process_iac;
1043           }
1044           CURL_SB_ACCUM(tn,c);
1045           tn->telrcv_state = CURL_TS_SB;
1046         }
1047         else
1048         {
1049           CURL_SB_ACCUM(tn, CURL_IAC);
1050           CURL_SB_ACCUM(tn, CURL_SE);
1051           tn->subpointer -= 2;
1052           CURL_SB_TERM(tn);
1053           suboption(conn);   /* handle sub-option */
1054           tn->telrcv_state = CURL_TS_DATA;
1055         }
1056         break;
1057     }
1058     ++in;
1059   }
1060   bufferflush();
1061   return CURLE_OK;
1062 }
1063
1064 /* Escape and send a telnet data block */
1065 /* TODO: write large chunks of data instead of one byte at a time */
1066 static CURLcode send_telnet_data(struct connectdata *conn,
1067                                  char *buffer, ssize_t nread)
1068 {
1069   unsigned char outbuf[2];
1070   ssize_t bytes_written, total_written;
1071   int out_count;
1072   CURLcode rc = CURLE_OK;
1073
1074   while(rc == CURLE_OK && nread--) {
1075     outbuf[0] = *buffer++;
1076     out_count = 1;
1077     if(outbuf[0] == CURL_IAC)
1078       outbuf[out_count++] = CURL_IAC;
1079
1080     total_written = 0;
1081     do {
1082       /* Make sure socket is writable to avoid EWOULDBLOCK condition */
1083       struct pollfd pfd[1];
1084       pfd[0].fd = conn->sock[FIRSTSOCKET];
1085       pfd[0].events = POLLOUT;
1086       switch (Curl_poll(pfd, 1, -1)) {
1087         case -1:                    /* error, abort writing */
1088         case 0:                     /* timeout (will never happen) */
1089           rc = CURLE_SEND_ERROR;
1090           break;
1091         default:                    /* write! */
1092           bytes_written = 0;
1093           rc = Curl_write(conn, conn->sock[FIRSTSOCKET], outbuf+total_written,
1094                           out_count-total_written, &bytes_written);
1095           total_written += bytes_written;
1096           break;
1097       }
1098     /* handle partial write */
1099     } while(rc == CURLE_OK && total_written < out_count);
1100   }
1101   return rc;
1102 }
1103
1104 static CURLcode telnet_done(struct connectdata *conn,
1105                                  CURLcode status, bool premature)
1106 {
1107   struct TELNET *tn = (struct TELNET *)conn->data->state.proto.telnet;
1108   (void)status; /* unused */
1109   (void)premature; /* not used */
1110
1111   curl_slist_free_all(tn->telnet_vars);
1112
1113   free(conn->data->state.proto.telnet);
1114   conn->data->state.proto.telnet = NULL;
1115
1116   return CURLE_OK;
1117 }
1118
1119 static CURLcode telnet_do(struct connectdata *conn, bool *done)
1120 {
1121   CURLcode code;
1122   struct SessionHandle *data = conn->data;
1123   curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
1124 #ifdef USE_WINSOCK
1125   HMODULE wsock2;
1126   WSOCK2_FUNC close_event_func;
1127   WSOCK2_FUNC create_event_func;
1128   WSOCK2_FUNC event_select_func;
1129   WSOCK2_FUNC enum_netevents_func;
1130   WSAEVENT event_handle;
1131   WSANETWORKEVENTS events;
1132   HANDLE stdin_handle;
1133   HANDLE objs[2];
1134   DWORD  obj_count;
1135   DWORD  wait_timeout;
1136   DWORD waitret;
1137   DWORD readfile_read;
1138   int err;
1139 #else
1140   int interval_ms;
1141   struct pollfd pfd[2];
1142   int poll_cnt;
1143   curl_off_t total_dl = 0;
1144   curl_off_t total_ul = 0;
1145 #endif
1146   ssize_t nread;
1147   struct timeval now;
1148   bool keepon = TRUE;
1149   char *buf = data->state.buffer;
1150   struct TELNET *tn;
1151
1152   *done = TRUE; /* unconditionally */
1153
1154   code = init_telnet(conn);
1155   if(code)
1156     return code;
1157
1158   tn = (struct TELNET *)data->state.proto.telnet;
1159
1160   code = check_telnet_options(conn);
1161   if(code)
1162     return code;
1163
1164 #ifdef USE_WINSOCK
1165   /*
1166   ** This functionality only works with WinSock >= 2.0.  So,
1167   ** make sure have it.
1168   */
1169   code = check_wsock2(data);
1170   if(code)
1171     return code;
1172
1173   /* OK, so we have WinSock 2.0.  We need to dynamically */
1174   /* load ws2_32.dll and get the function pointers we need. */
1175   wsock2 = LoadLibrary("WS2_32.DLL");
1176   if(wsock2 == NULL) {
1177     failf(data,"failed to load WS2_32.DLL (%d)", ERRNO);
1178     return CURLE_FAILED_INIT;
1179   }
1180
1181   /* Grab a pointer to WSACreateEvent */
1182   create_event_func = GetProcAddress(wsock2,"WSACreateEvent");
1183   if(create_event_func == NULL) {
1184     failf(data,"failed to find WSACreateEvent function (%d)",
1185           ERRNO);
1186     FreeLibrary(wsock2);
1187     return CURLE_FAILED_INIT;
1188   }
1189
1190   /* And WSACloseEvent */
1191   close_event_func = GetProcAddress(wsock2,"WSACloseEvent");
1192   if(close_event_func == NULL) {
1193     failf(data,"failed to find WSACloseEvent function (%d)",
1194           ERRNO);
1195     FreeLibrary(wsock2);
1196     return CURLE_FAILED_INIT;
1197   }
1198
1199   /* And WSAEventSelect */
1200   event_select_func = GetProcAddress(wsock2,"WSAEventSelect");
1201   if(event_select_func == NULL) {
1202     failf(data,"failed to find WSAEventSelect function (%d)",
1203           ERRNO);
1204     FreeLibrary(wsock2);
1205     return CURLE_FAILED_INIT;
1206   }
1207
1208   /* And WSAEnumNetworkEvents */
1209   enum_netevents_func = GetProcAddress(wsock2,"WSAEnumNetworkEvents");
1210   if(enum_netevents_func == NULL) {
1211     failf(data,"failed to find WSAEnumNetworkEvents function (%d)",
1212           ERRNO);
1213     FreeLibrary(wsock2);
1214     return CURLE_FAILED_INIT;
1215   }
1216
1217   /* We want to wait for both stdin and the socket. Since
1218   ** the select() function in winsock only works on sockets
1219   ** we have to use the WaitForMultipleObjects() call.
1220   */
1221
1222   /* First, create a sockets event object */
1223   event_handle = (WSAEVENT)create_event_func();
1224   if(event_handle == WSA_INVALID_EVENT) {
1225     failf(data,"WSACreateEvent failed (%d)", SOCKERRNO);
1226     FreeLibrary(wsock2);
1227     return CURLE_FAILED_INIT;
1228   }
1229
1230   /* The get the Windows file handle for stdin */
1231   stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
1232
1233   /* Create the list of objects to wait for */
1234   objs[0] = event_handle;
1235   objs[1] = stdin_handle;
1236
1237   /* Tell winsock what events we want to listen to */
1238   if(event_select_func(sockfd, event_handle, FD_READ|FD_CLOSE) ==
1239      SOCKET_ERROR) {
1240     close_event_func(event_handle);
1241     FreeLibrary(wsock2);
1242     return CURLE_OK;
1243   }
1244
1245   /* If stdin_handle is a pipe, use PeekNamedPipe() method to check it,
1246      else use the old WaitForMultipleObjects() way */
1247   if(GetFileType(stdin_handle) == FILE_TYPE_PIPE) {
1248     /* Don't wait for stdin_handle, just wait for event_handle */
1249     obj_count = 1;
1250     /* Check stdin_handle per 100 milliseconds */
1251     wait_timeout = 100;
1252   }
1253   else {
1254     obj_count = 2;
1255     wait_timeout = 1000;
1256   }
1257
1258   /* Keep on listening and act on events */
1259   while(keepon) {
1260     waitret = WaitForMultipleObjects(obj_count, objs, FALSE, wait_timeout);
1261     switch(waitret) {
1262     case WAIT_TIMEOUT:
1263     {
1264       for(;;) {
1265         if(!PeekNamedPipe(stdin_handle, NULL, 0, NULL, &readfile_read, NULL)) {
1266           keepon = FALSE;
1267           code = CURLE_READ_ERROR;
1268           break;
1269         }
1270
1271         if(!readfile_read)
1272           break;
1273
1274         if(!ReadFile(stdin_handle, buf, sizeof(data->state.buffer),
1275                      &readfile_read, NULL)) {
1276           keepon = FALSE;
1277           code = CURLE_READ_ERROR;
1278           break;
1279         }
1280
1281         code = send_telnet_data(conn, buf, readfile_read);
1282         if(code) {
1283           keepon = FALSE;
1284           break;
1285         }
1286       }
1287     }
1288     break;
1289
1290     case WAIT_OBJECT_0 + 1:
1291     {
1292       if(!ReadFile(stdin_handle, buf, sizeof(data->state.buffer),
1293                    &readfile_read, NULL)) {
1294         keepon = FALSE;
1295         code = CURLE_READ_ERROR;
1296         break;
1297       }
1298
1299       code = send_telnet_data(conn, buf, readfile_read);
1300       if(code) {
1301         keepon = FALSE;
1302         break;
1303       }
1304     }
1305     break;
1306
1307     case WAIT_OBJECT_0:
1308
1309       if(SOCKET_ERROR == enum_netevents_func(sockfd, event_handle, &events)) {
1310         if((err = SOCKERRNO) != EINPROGRESS) {
1311           infof(data,"WSAEnumNetworkEvents failed (%d)", err);
1312           keepon = FALSE;
1313           code = CURLE_READ_ERROR;
1314         }
1315         break;
1316       }
1317       if(events.lNetworkEvents & FD_READ) {
1318         /* read data from network */
1319         code = Curl_read(conn, sockfd, buf, BUFSIZE - 1, &nread);
1320         /* read would've blocked. Loop again */
1321         if(code == CURLE_AGAIN)
1322           break;
1323         /* returned not-zero, this an error */
1324         else if(code) {
1325           keepon = FALSE;
1326           break;
1327         }
1328         /* returned zero but actually received 0 or less here,
1329            the server closed the connection and we bail out */
1330         else if(nread <= 0) {
1331           keepon = FALSE;
1332           break;
1333         }
1334
1335         code = telrcv(conn, (unsigned char *)buf, nread);
1336         if(code) {
1337           keepon = FALSE;
1338           break;
1339         }
1340
1341         /* Negotiate if the peer has started negotiating,
1342            otherwise don't. We don't want to speak telnet with
1343            non-telnet servers, like POP or SMTP. */
1344         if(tn->please_negotiate && !tn->already_negotiated) {
1345           negotiate(conn);
1346           tn->already_negotiated = 1;
1347         }
1348       }
1349       if(events.lNetworkEvents & FD_CLOSE) {
1350         keepon = FALSE;
1351       }
1352       break;
1353
1354     }
1355
1356     if(data->set.timeout) {
1357       now = Curl_tvnow();
1358       if(Curl_tvdiff(now, conn->created) >= data->set.timeout) {
1359         failf(data, "Time-out");
1360         code = CURLE_OPERATION_TIMEDOUT;
1361         keepon = FALSE;
1362       }
1363     }
1364   }
1365
1366   /* We called WSACreateEvent, so call WSACloseEvent */
1367   if(close_event_func(event_handle) == FALSE) {
1368     infof(data,"WSACloseEvent failed (%d)", SOCKERRNO);
1369   }
1370
1371   /* "Forget" pointers into the library we're about to free */
1372   create_event_func = NULL;
1373   close_event_func = NULL;
1374   event_select_func = NULL;
1375   enum_netevents_func = NULL;
1376
1377   /* We called LoadLibrary, so call FreeLibrary */
1378   if(!FreeLibrary(wsock2))
1379     infof(data,"FreeLibrary(wsock2) failed (%d)", ERRNO);
1380 #else
1381   pfd[0].fd = sockfd;
1382   pfd[0].events = POLLIN;
1383
1384   if(data->set.is_fread_set) {
1385     poll_cnt = 1;
1386     interval_ms = 100; /* poll user-supplied read function */
1387   }
1388   else {
1389     pfd[1].fd = 0;
1390     pfd[1].events = POLLIN;
1391     poll_cnt = 2;
1392     interval_ms = 1 * 1000;
1393   }
1394
1395   while(keepon) {
1396     switch (Curl_poll(pfd, poll_cnt, interval_ms)) {
1397     case -1:                    /* error, stop reading */
1398       keepon = FALSE;
1399       continue;
1400     case 0:                     /* timeout */
1401       pfd[0].revents = 0;
1402       pfd[1].revents = 0;
1403       /* fall through */
1404     default:                    /* read! */
1405       if(pfd[0].revents & POLLIN) {
1406         /* read data from network */
1407         code = Curl_read(conn, sockfd, buf, BUFSIZE - 1, &nread);
1408         /* read would've blocked. Loop again */
1409         if(code == CURLE_AGAIN)
1410           break;
1411         /* returned not-zero, this an error */
1412         else if(code) {
1413           keepon = FALSE;
1414           break;
1415         }
1416         /* returned zero but actually received 0 or less here,
1417            the server closed the connection and we bail out */
1418         else if(nread <= 0) {
1419           keepon = FALSE;
1420           break;
1421         }
1422
1423         total_dl += nread;
1424         Curl_pgrsSetDownloadCounter(data, total_dl);
1425         code = telrcv(conn, (unsigned char *)buf, nread);
1426         if(code) {
1427           keepon = FALSE;
1428           break;
1429         }
1430
1431         /* Negotiate if the peer has started negotiating,
1432            otherwise don't. We don't want to speak telnet with
1433            non-telnet servers, like POP or SMTP. */
1434         if(tn->please_negotiate && !tn->already_negotiated) {
1435           negotiate(conn);
1436           tn->already_negotiated = 1;
1437         }
1438       }
1439
1440       nread = 0;
1441       if(poll_cnt == 2) {
1442         if(pfd[1].revents & POLLIN) { /* read from stdin */
1443           nread = read(0, buf, BUFSIZE - 1);
1444         }
1445       }
1446       else {
1447         /* read from user-supplied method */
1448         nread = (int)conn->fread_func(buf, 1, BUFSIZE - 1, conn->fread_in);
1449         if(nread == CURL_READFUNC_ABORT) {
1450           keepon = FALSE;
1451           break;
1452         }
1453         if(nread == CURL_READFUNC_PAUSE)
1454           break;
1455       }
1456
1457       if(nread > 0) {
1458         code = send_telnet_data(conn, buf, nread);
1459         if(code) {
1460           keepon = FALSE;
1461           break;
1462         }
1463         total_ul += nread;
1464         Curl_pgrsSetUploadCounter(data, total_ul);
1465       }
1466       else if(nread < 0)
1467         keepon = FALSE;
1468
1469       break;
1470     } /* poll switch statement */
1471
1472     if(data->set.timeout) {
1473       now = Curl_tvnow();
1474       if(Curl_tvdiff(now, conn->created) >= data->set.timeout) {
1475         failf(data, "Time-out");
1476         code = CURLE_OPERATION_TIMEDOUT;
1477         keepon = FALSE;
1478       }
1479     }
1480
1481     if(Curl_pgrsUpdate(conn)) {
1482       code = CURLE_ABORTED_BY_CALLBACK;
1483       break;
1484     }
1485   }
1486 #endif
1487   /* mark this as "no further transfer wanted" */
1488   Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
1489
1490   return code;
1491 }
1492 #endif