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/* -- WIN32 approved -- */
24
25#include "setup.h"
26
27#include <stdio.h>
28#include <string.h>
29#include <stdarg.h>
30#include <stdlib.h>
31#include <ctype.h>
32#include <errno.h>
33
34#ifdef WIN32
35#include <time.h>
36#include <io.h>
37#else
38#ifdef HAVE_SYS_SOCKET_H
39#include <sys/socket.h>
40#endif
41#ifdef HAVE_NETINET_IN_H
42#include <netinet/in.h>
43#endif
44#ifdef HAVE_SYS_TIME_H
45#include <sys/time.h>
46#endif
47#ifdef HAVE_UNISTD_H
48#include <unistd.h>
49#endif
50#ifdef HAVE_NETDB_H
51#include <netdb.h>
52#endif
53#ifdef HAVE_ARPA_INET_H
54#include <arpa/inet.h>
55#endif
56#ifdef HAVE_NET_IF_H
57#include <net/if.h>
58#endif
59#ifdef HAVE_SYS_IOCTL_H
60#include <sys/ioctl.h>
61#endif
62
63#ifdef HAVE_SYS_PARAM_H
64#include <sys/param.h>
65#endif
66
67#ifdef __VMS
68#include <in.h>
69#include <inet.h>
70#endif
71
72#ifndef HAVE_SOCKET
73#error "We can't compile without socket() support!"
74#endif
75
76#endif  /* WIN32 */
77
78#ifdef HAVE_LIMITS_H
79#include <limits.h>
80#endif
81
82#ifdef USE_LIBIDN
83#include <idna.h>
84#include <tld.h>
85#include <stringprep.h>
86#ifdef HAVE_IDN_FREE_H
87#include <idn-free.h>
88#else
89/* prototype from idn-free.h, not provided by libidn 0.4.5's make install! */
90void idn_free (void *ptr);
91#endif
92#ifndef HAVE_IDN_FREE
93/* if idn_free() was not found in this version of libidn use free() instead */
94#define idn_free(x) (free)(x)
95#endif
96#elif defined(USE_WIN32_IDN)
97/* prototype for curl_win32_idn_to_ascii() */
98int curl_win32_idn_to_ascii(const char *in, char **out);
99#endif  /* USE_LIBIDN */
100
101#include "urldata.h"
102#include "netrc.h"
103
104#include "formdata.h"
105#include "sslgen.h"
106#include "hostip.h"
107#include "transfer.h"
108#include "sendf.h"
109#include "progress.h"
110#include "cookie.h"
111#include "strequal.h"
112#include "strerror.h"
113#include "escape.h"
114#include "strtok.h"
115#include "share.h"
116#include "content_encoding.h"
117#include "http_digest.h"
118#include "http_negotiate.h"
119#include "select.h"
120#include "multiif.h"
121#include "easyif.h"
122#include "speedcheck.h"
123#include "rawstr.h"
124#include "warnless.h"
125#include "non-ascii.h"
126
127/* And now for the protocols */
128#include "ftp.h"
129#include "dict.h"
130#include "telnet.h"
131#include "tftp.h"
132#include "http.h"
133#include "file.h"
134#include "curl_ldap.h"
135#include "ssh.h"
136#include "imap.h"
137#include "url.h"
138#include "connect.h"
139#include "inet_ntop.h"
140#include "http_ntlm.h"
141#include "socks.h"
142#include "curl_rtmp.h"
143#include "gopher.h"
144
145#define _MPRINTF_REPLACE /* use our functions only */
146#include <curl/mprintf.h>
147
148#include "curl_memory.h"
149/* The last #include file should be: */
150#include "memdebug.h"
151
152/* Local static prototypes */
153static long ConnectionKillOne(struct SessionHandle *data);
154static void conn_free(struct connectdata *conn);
155static void signalPipeClose(struct curl_llist *pipeline, bool pipe_broke);
156
157/*
158 * Protocol table.
159 */
160
161static const struct Curl_handler * const protocols[] = {
162
163#ifndef CURL_DISABLE_HTTP
164  &Curl_handler_http,
165#endif
166
167#if defined(USE_SSL) && !defined(CURL_DISABLE_HTTP)
168  &Curl_handler_https,
169#endif
170
171#ifndef CURL_DISABLE_FTP
172  &Curl_handler_ftp,
173#endif
174
175#if defined(USE_SSL) && !defined(CURL_DISABLE_FTP)
176  &Curl_handler_ftps,
177#endif
178
179#ifndef CURL_DISABLE_TELNET
180  &Curl_handler_telnet,
181#endif
182
183#ifndef CURL_DISABLE_DICT
184  &Curl_handler_dict,
185#endif
186
187#ifndef CURL_DISABLE_LDAP
188  &Curl_handler_ldap,
189#if !defined(CURL_DISABLE_LDAPS) && \
190    ((defined(USE_OPENLDAP) && defined(USE_SSL)) || \
191     (!defined(USE_OPENLDAP) && defined(HAVE_LDAP_SSL)))
192  &Curl_handler_ldaps,
193#endif
194#endif
195
196#ifndef CURL_DISABLE_FILE
197  &Curl_handler_file,
198#endif
199
200#ifndef CURL_DISABLE_TFTP
201  &Curl_handler_tftp,
202#endif
203
204#ifdef USE_LIBSSH2
205  &Curl_handler_scp,
206  &Curl_handler_sftp,
207#endif
208
209#ifndef CURL_DISABLE_IMAP
210  &Curl_handler_imap,
211#ifdef USE_SSL
212  &Curl_handler_imaps,
213#endif
214#endif
215
216#ifndef CURL_DISABLE_POP3
217  &Curl_handler_pop3,
218#ifdef USE_SSL
219  &Curl_handler_pop3s,
220#endif
221#endif
222
223#ifndef CURL_DISABLE_SMTP
224  &Curl_handler_smtp,
225#ifdef USE_SSL
226  &Curl_handler_smtps,
227#endif
228#endif
229
230#ifndef CURL_DISABLE_RTSP
231  &Curl_handler_rtsp,
232#endif
233
234#ifndef CURL_DISABLE_GOPHER
235  &Curl_handler_gopher,
236#endif
237
238#ifdef USE_LIBRTMP
239  &Curl_handler_rtmp,
240  &Curl_handler_rtmpt,
241  &Curl_handler_rtmpe,
242  &Curl_handler_rtmpte,
243  &Curl_handler_rtmps,
244  &Curl_handler_rtmpts,
245#endif
246
247  (struct Curl_handler *) NULL
248};
249
250/*
251 * Dummy handler for undefined protocol schemes.
252 */
253
254static const struct Curl_handler Curl_handler_dummy = {
255  "<no protocol>",                      /* scheme */
256  ZERO_NULL,                            /* setup_connection */
257  ZERO_NULL,                            /* do_it */
258  ZERO_NULL,                            /* done */
259  ZERO_NULL,                            /* do_more */
260  ZERO_NULL,                            /* connect_it */
261  ZERO_NULL,                            /* connecting */
262  ZERO_NULL,                            /* doing */
263  ZERO_NULL,                            /* proto_getsock */
264  ZERO_NULL,                            /* doing_getsock */
265  ZERO_NULL,                            /* perform_getsock */
266  ZERO_NULL,                            /* disconnect */
267  ZERO_NULL,                            /* readwrite */
268  0,                                    /* defport */
269  0,                                    /* protocol */
270  PROTOPT_NONE                          /* flags */
271};
272
273void Curl_safefree(void *ptr)
274{
275  if(ptr)
276    free(ptr);
277}
278
279static void close_connections(struct SessionHandle *data)
280{
281  /* Loop through all open connections and kill them one by one */
282  long i;
283  do {
284    i = ConnectionKillOne(data);
285  } while(i != -1L);
286}
287
288void Curl_freeset(struct SessionHandle * data)
289{
290  /* Free all dynamic strings stored in the data->set substructure. */
291  enum dupstring i;
292  for(i=(enum dupstring)0; i < STRING_LAST; i++)
293    Curl_safefree(data->set.str[i]);
294}
295
296static CURLcode setstropt(char **charp, char * s)
297{
298  /* Release the previous storage at `charp' and replace by a dynamic storage
299     copy of `s'. Return CURLE_OK or CURLE_OUT_OF_MEMORY. */
300
301  if(*charp) {
302    free(*charp);
303    *charp = (char *) NULL;
304  }
305
306  if(s) {
307    s = strdup(s);
308
309    if(!s)
310      return CURLE_OUT_OF_MEMORY;
311
312    *charp = s;
313  }
314
315  return CURLE_OK;
316}
317
318static CURLcode setstropt_userpwd(char *option, char **user_storage,
319                                  char **pwd_storage)
320{
321  char* separator;
322  CURLcode result = CURLE_OK;
323
324  if(!option) {
325    /* we treat a NULL passed in as a hint to clear existing info */
326    Curl_safefree(*user_storage);
327    *user_storage = (char *) NULL;
328    Curl_safefree(*pwd_storage);
329    *pwd_storage = (char *) NULL;
330    return CURLE_OK;
331  }
332
333  separator = strchr(option, ':');
334  if(separator != NULL) {
335
336    /* store username part of option */
337    char * p;
338    size_t username_len = (size_t)(separator-option);
339    p = malloc(username_len+1);
340    if(!p)
341      result = CURLE_OUT_OF_MEMORY;
342    else {
343      memcpy(p, option, username_len);
344      p[username_len] = '\0';
345      Curl_safefree(*user_storage);
346      *user_storage = p;
347    }
348
349    /* store password part of option */
350    if(result == CURLE_OK)
351      result = setstropt(pwd_storage, separator+1);
352  }
353  else {
354    result = setstropt(user_storage, option);
355  }
356  return result;
357}
358
359CURLcode Curl_dupset(struct SessionHandle * dst, struct SessionHandle * src)
360{
361  CURLcode r = CURLE_OK;
362  enum dupstring i;
363
364  /* Copy src->set into dst->set first, then deal with the strings
365     afterwards */
366  dst->set = src->set;
367
368  /* clear all string pointers first */
369  memset(dst->set.str, 0, STRING_LAST * sizeof(char *));
370
371  /* duplicate all strings */
372  for(i=(enum dupstring)0; i< STRING_LAST; i++) {
373    r = setstropt(&dst->set.str[i], src->set.str[i]);
374    if(r != CURLE_OK)
375      break;
376  }
377
378  /* If a failure occurred, freeing has to be performed externally. */
379  return r;
380}
381
382/*
383 * This is the internal function curl_easy_cleanup() calls. This should
384 * cleanup and free all resources associated with this sessionhandle.
385 *
386 * NOTE: if we ever add something that attempts to write to a socket or
387 * similar here, we must ignore SIGPIPE first. It is currently only done
388 * when curl_easy_perform() is invoked.
389 */
390
391CURLcode Curl_close(struct SessionHandle *data)
392{
393  struct Curl_multi *m = data->multi;
394
395#ifdef DEBUGBUILD
396  /* only for debugging, scan through all connections and see if there's a
397     pipe reference still identifying this handle */
398
399  if(data->state.connc && data->state.connc->type == CONNCACHE_MULTI) {
400    struct conncache *c = data->state.connc;
401    long i;
402    struct curl_llist *pipeline;
403    struct curl_llist_element *curr;
404    struct connectdata *connptr;
405
406    for(i=0; i< c->num; i++) {
407      connptr = c->connects[i];
408      if(!connptr)
409        continue;
410
411      pipeline = connptr->send_pipe;
412      if(pipeline) {
413        for(curr = pipeline->head; curr; curr=curr->next) {
414          if(data == (struct SessionHandle *) curr->ptr) {
415            fprintf(stderr,
416                    "problem we %p are still in send pipe for %p done %d\n",
417                    data, connptr, (int)connptr->bits.done);
418          }
419        }
420      }
421      pipeline = connptr->recv_pipe;
422      if(pipeline) {
423        for(curr = pipeline->head; curr; curr=curr->next) {
424          if(data == (struct SessionHandle *) curr->ptr) {
425            fprintf(stderr,
426                    "problem we %p are still in recv pipe for %p done %d\n",
427                    data, connptr, (int)connptr->bits.done);
428          }
429        }
430      }
431      pipeline = connptr->done_pipe;
432      if(pipeline) {
433        for(curr = pipeline->head; curr; curr=curr->next) {
434          if(data == (struct SessionHandle *) curr->ptr) {
435            fprintf(stderr,
436                    "problem we %p are still in done pipe for %p done %d\n",
437                    data, connptr, (int)connptr->bits.done);
438          }
439        }
440      }
441      pipeline = connptr->pend_pipe;
442      if(pipeline) {
443        for(curr = pipeline->head; curr; curr=curr->next) {
444          if(data == (struct SessionHandle *) curr->ptr) {
445            fprintf(stderr,
446                    "problem we %p are still in pend pipe for %p done %d\n",
447                    data, connptr, (int)connptr->bits.done);
448          }
449        }
450      }
451    }
452  }
453#endif
454
455  Curl_expire(data, 0); /* shut off timers */
456
457  if(m)
458    /* This handle is still part of a multi handle, take care of this first
459       and detach this handle from there. */
460    curl_multi_remove_handle(data->multi, data);
461
462  /* Destroy the timeout list that is held in the easy handle. It is
463     /normally/ done by curl_multi_remove_handle() but this is "just in
464     case" */
465  if(data->state.timeoutlist) {
466    Curl_llist_destroy(data->state.timeoutlist, NULL);
467    data->state.timeoutlist = NULL;
468  }
469
470  data->magic = 0; /* force a clear AFTER the possibly enforced removal from
471                      the multi handle, since that function uses the magic
472                      field! */
473
474  if(data->state.connc) {
475
476    if(data->state.connc->type == CONNCACHE_PRIVATE) {
477      /* close all connections still alive that are in the private connection
478         cache, as we no longer have the pointer left to the shared one. */
479      close_connections(data);
480
481      /* free the connection cache if allocated privately */
482      Curl_rm_connc(data->state.connc);
483    }
484  }
485
486  if(data->state.shared_conn) {
487    /* marked to be used by a pending connection so we can't kill this handle
488       just yet */
489    data->state.closed = TRUE;
490    return CURLE_OK;
491  }
492
493  if(data->dns.hostcachetype == HCACHE_PRIVATE) {
494    Curl_hash_destroy(data->dns.hostcache);
495    data->dns.hostcachetype = HCACHE_NONE;
496    data->dns.hostcache = NULL;
497  }
498
499  if(data->state.rangestringalloc)
500    free(data->state.range);
501
502  /* Free the pathbuffer */
503  Curl_safefree(data->state.pathbuffer);
504  Curl_safefree(data->state.proto.generic);
505
506  /* Close down all open SSL info and sessions */
507  Curl_ssl_close_all(data);
508  Curl_safefree(data->state.first_host);
509  Curl_safefree(data->state.scratch);
510  Curl_ssl_free_certinfo(data);
511
512  if(data->change.referer_alloc)
513    free(data->change.referer);
514
515  if(data->change.url_alloc)
516    free(data->change.url);
517
518  Curl_safefree(data->state.headerbuff);
519
520  Curl_flush_cookies(data, 1);
521
522  Curl_digest_cleanup(data);
523
524  Curl_safefree(data->info.contenttype);
525  Curl_safefree(data->info.wouldredirect);
526
527  /* this destroys the channel and we cannot use it anymore after this */
528  Curl_resolver_cleanup(data->state.resolver);
529
530  Curl_convert_close(data);
531
532  /* No longer a dirty share, if it exists */
533  if(data->share) {
534    Curl_share_lock(data, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE);
535    data->share->dirty--;
536    Curl_share_unlock(data, CURL_LOCK_DATA_SHARE);
537  }
538
539  Curl_freeset(data);
540  free(data);
541  return CURLE_OK;
542}
543
544/* create a connection cache of a private or multi type */
545struct conncache *Curl_mk_connc(int type,
546                                long amount) /* set -1 to use default */
547{
548  /* It is subject for debate how many default connections to have for a multi
549     connection cache... */
550
551  struct conncache *c;
552  long default_amount;
553  long max_amount = (long)(((size_t)INT_MAX) / sizeof(struct connectdata *));
554
555  if(type == CONNCACHE_PRIVATE) {
556    default_amount = (amount < 1L) ? 5L : amount;
557  }
558  else {
559    default_amount = (amount < 1L) ? 10L : amount;
560  }
561
562  if(default_amount > max_amount)
563    default_amount = max_amount;
564
565  c = calloc(1, sizeof(struct conncache));
566  if(!c)
567    return NULL;
568
569  c->connects = calloc((size_t)default_amount, sizeof(struct connectdata *));
570  if(!c->connects) {
571    free(c);
572    return NULL;
573  }
574
575  c->num = default_amount;
576
577  return c;
578}
579
580/* Change number of entries of a connection cache */
581CURLcode Curl_ch_connc(struct SessionHandle *data,
582                       struct conncache *c,
583                       long newamount)
584{
585  long i;
586  struct connectdata **newptr;
587  long max_amount = (long)(((size_t)INT_MAX) / sizeof(struct connectdata *));
588
589  if(newamount < 1)
590    newamount = 1; /* we better have at least one entry */
591
592  if(!c) {
593    /* we get a NULL pointer passed in as connection cache, which means that
594       there is no cache created for this SessionHandle just yet, we create a
595       brand new with the requested size.
596    */
597    data->state.connc = Curl_mk_connc(CONNCACHE_PRIVATE, newamount);
598    if(!data->state.connc)
599      return CURLE_OUT_OF_MEMORY;
600    return CURLE_OK;
601  }
602
603  if(newamount < c->num) {
604    /* Since this number is *decreased* from the existing number, we must
605       close the possibly open connections that live on the indexes that
606       are being removed!
607
608       NOTE: for conncache_multi cases we must make sure that we only
609       close handles not in use.
610    */
611    for(i=newamount; i< c->num; i++)
612      Curl_disconnect(c->connects[i], /* dead_connection */ FALSE);
613
614    /* If the most recent connection is no longer valid, mark it
615       invalid. */
616    if(data->state.lastconnect <= newamount)
617      data->state.lastconnect = -1;
618  }
619  if(newamount > 0) {
620    if(newamount > max_amount)
621      newamount = max_amount;
622    newptr = realloc(c->connects, sizeof(struct connectdata *) * newamount);
623    if(!newptr)
624      /* we closed a few connections in vain, but so what? */
625      return CURLE_OUT_OF_MEMORY;
626
627    /* nullify the newly added pointers */
628    for(i=c->num; i<newamount; i++)
629      newptr[i] = NULL;
630
631    c->connects = newptr;
632    c->num = newamount;
633  }
634  /* we no longer support less than 1 as size for the connection cache, and
635     I'm not sure it ever worked to set it to zero */
636  return CURLE_OK;
637}
638
639/* Free a connection cache. This is called from Curl_close() and
640   curl_multi_cleanup(). */
641void Curl_rm_connc(struct conncache *c)
642{
643  if(c->connects) {
644    long i;
645    for(i = 0; i < c->num; ++i)
646      conn_free(c->connects[i]);
647
648    free(c->connects);
649  }
650
651  free(c);
652}
653
654/*
655 * Initialize the UserDefined fields within a SessionHandle.
656 * This may be safely called on a new or existing SessionHandle.
657 */
658CURLcode Curl_init_userdefined(struct UserDefined *set)
659{
660  CURLcode res = CURLE_OK;
661
662  set->out = stdout; /* default output to stdout */
663  set->in  = stdin;  /* default input from stdin */
664  set->err  = stderr;  /* default stderr to stderr */
665
666  /* use fwrite as default function to store output */
667  set->fwrite_func = (curl_write_callback)fwrite;
668
669  /* use fread as default function to read input */
670  set->fread_func = (curl_read_callback)fread;
671  set->is_fread_set = 0;
672  set->is_fwrite_set = 0;
673
674  set->seek_func = ZERO_NULL;
675  set->seek_client = ZERO_NULL;
676
677  /* conversion callbacks for non-ASCII hosts */
678  set->convfromnetwork = ZERO_NULL;
679  set->convtonetwork   = ZERO_NULL;
680  set->convfromutf8    = ZERO_NULL;
681
682  set->infilesize = -1;      /* we don't know any size */
683  set->postfieldsize = -1;   /* unknown size */
684  set->maxredirs = -1;       /* allow any amount by default */
685
686  set->httpreq = HTTPREQ_GET; /* Default HTTP request */
687  set->rtspreq = RTSPREQ_OPTIONS; /* Default RTSP request */
688  set->ftp_use_epsv = TRUE;   /* FTP defaults to EPSV operations */
689  set->ftp_use_eprt = TRUE;   /* FTP defaults to EPRT operations */
690  set->ftp_use_pret = FALSE;  /* mainly useful for drftpd servers */
691  set->ftp_filemethod = FTPFILE_MULTICWD;
692
693  set->dns_cache_timeout = 60; /* Timeout every 60 seconds by default */
694
695  /* Set the default size of the SSL session ID cache */
696  set->ssl.numsessions = 5;
697
698  set->proxyport = CURL_DEFAULT_PROXY_PORT; /* from url.h */
699  set->proxytype = CURLPROXY_HTTP; /* defaults to HTTP proxy */
700  set->httpauth = CURLAUTH_BASIC;  /* defaults to basic */
701  set->proxyauth = CURLAUTH_BASIC; /* defaults to basic */
702
703  /* make libcurl quiet by default: */
704  set->hide_progress = TRUE;  /* CURLOPT_NOPROGRESS changes these */
705
706  /*
707   * libcurl 7.10 introduced SSL verification *by default*! This needs to be
708   * switched off unless wanted.
709   */
710  set->ssl.verifypeer = TRUE;
711  set->ssl.verifyhost = 2;
712#ifdef USE_TLS_SRP
713  set->ssl.authtype = CURL_TLSAUTH_NONE;
714#endif
715  set->ssh_auth_types = CURLSSH_AUTH_DEFAULT; /* defaults to any auth
716                                                      type */
717  set->ssl.sessionid = TRUE; /* session ID caching enabled by default */
718
719  set->new_file_perms = 0644;    /* Default permissions */
720  set->new_directory_perms = 0755; /* Default permissions */
721
722  /* for the *protocols fields we don't use the CURLPROTO_ALL convenience
723     define since we internally only use the lower 16 bits for the passed
724     in bitmask to not conflict with the private bits */
725  set->allowed_protocols = CURLPROTO_ALL;
726  set->redir_protocols =
727    CURLPROTO_ALL & ~(CURLPROTO_FILE|CURLPROTO_SCP); /* not FILE or SCP */
728
729#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
730  /*
731   * disallow unprotected protection negotiation NEC reference implementation
732   * seem not to follow rfc1961 section 4.3/4.4
733   */
734  set->socks5_gssapi_nec = FALSE;
735  /* set default gssapi service name */
736  res = setstropt(&set->str[STRING_SOCKS5_GSSAPI_SERVICE],
737                  (char *) CURL_DEFAULT_SOCKS5_GSSAPI_SERVICE);
738  if(res != CURLE_OK)
739    return res;
740#endif
741
742  /* This is our preferred CA cert bundle/path since install time */
743#if defined(CURL_CA_BUNDLE)
744  res = setstropt(&set->str[STRING_SSL_CAFILE], (char *) CURL_CA_BUNDLE);
745#elif defined(CURL_CA_PATH)
746  res = setstropt(&set->str[STRING_SSL_CAPATH], (char *) CURL_CA_PATH);
747#endif
748
749  set->wildcardmatch  = FALSE;
750  set->chunk_bgn      = ZERO_NULL;
751  set->chunk_end      = ZERO_NULL;
752
753  return res;
754}
755
756/**
757 * Curl_open()
758 *
759 * @param curl is a pointer to a sessionhandle pointer that gets set by this
760 * function.
761 * @return CURLcode
762 */
763
764CURLcode Curl_open(struct SessionHandle **curl)
765{
766  CURLcode res = CURLE_OK;
767  struct SessionHandle *data;
768  CURLcode status;
769
770  /* Very simple start-up: alloc the struct, init it with zeroes and return */
771  data = calloc(1, sizeof(struct SessionHandle));
772  if(!data) {
773    /* this is a very serious error */
774    DEBUGF(fprintf(stderr, "Error: calloc of SessionHandle failed\n"));
775    return CURLE_OUT_OF_MEMORY;
776  }
777
778  data->magic = CURLEASY_MAGIC_NUMBER;
779
780  status = Curl_resolver_init(&data->state.resolver);
781  if(status) {
782    DEBUGF(fprintf(stderr, "Error: resolver_init failed\n"));
783    free(data);
784    return status;
785  }
786
787  /* We do some initial setup here, all those fields that can't be just 0 */
788
789  data->state.headerbuff = malloc(HEADERSIZE);
790  if(!data->state.headerbuff) {
791    DEBUGF(fprintf(stderr, "Error: malloc of headerbuff failed\n"));
792    res = CURLE_OUT_OF_MEMORY;
793  }
794  else {
795    Curl_easy_initHandleData(data);
796    res = Curl_init_userdefined(&data->set);
797
798    data->state.headersize=HEADERSIZE;
799
800    Curl_convert_init(data);
801
802    /* most recent connection is not yet defined */
803    data->state.lastconnect = -1;
804
805    data->progress.flags |= PGRS_HIDE;
806    data->state.current_speed = -1; /* init to negative == impossible */
807
808    data->wildcard.state = CURLWC_INIT;
809    data->wildcard.filelist = NULL;
810    data->set.fnmatch = ZERO_NULL;
811    /* This no longer creates a connection cache here. It is instead made on
812       the first call to curl_easy_perform() or when the handle is added to a
813       multi stack. */
814  }
815
816  if(res) {
817    Curl_resolver_cleanup(data->state.resolver);
818    if(data->state.headerbuff)
819      free(data->state.headerbuff);
820    Curl_freeset(data);
821    free(data);
822    data = NULL;
823  }
824  else
825    *curl = data;
826
827  return res;
828}
829
830CURLcode Curl_setopt(struct SessionHandle *data, CURLoption option,
831                     va_list param)
832{
833  char *argptr;
834  CURLcode result = CURLE_OK;
835#ifndef CURL_DISABLE_HTTP
836  curl_off_t bigsize;
837#endif
838
839  switch(option) {
840  case CURLOPT_DNS_CACHE_TIMEOUT:
841    data->set.dns_cache_timeout = va_arg(param, long);
842    break;
843  case CURLOPT_DNS_USE_GLOBAL_CACHE:
844  {
845    /* remember we want this enabled */
846    long use_cache = va_arg(param, long);
847    data->set.global_dns_cache = (bool)(0 != use_cache);
848  }
849  break;
850  case CURLOPT_SSL_CIPHER_LIST:
851    /* set a list of cipher we want to use in the SSL connection */
852    result = setstropt(&data->set.str[STRING_SSL_CIPHER_LIST],
853                       va_arg(param, char *));
854    break;
855
856  case CURLOPT_RANDOM_FILE:
857    /*
858     * This is the path name to a file that contains random data to seed
859     * the random SSL stuff with. The file is only used for reading.
860     */
861    result = setstropt(&data->set.str[STRING_SSL_RANDOM_FILE],
862                       va_arg(param, char *));
863    break;
864  case CURLOPT_EGDSOCKET:
865    /*
866     * The Entropy Gathering Daemon socket pathname
867     */
868    result = setstropt(&data->set.str[STRING_SSL_EGDSOCKET],
869                       va_arg(param, char *));
870    break;
871  case CURLOPT_MAXCONNECTS:
872    /*
873     * Set the absolute number of maximum simultaneous alive connection that
874     * libcurl is allowed to have.
875     */
876    result = Curl_ch_connc(data, data->state.connc, va_arg(param, long));
877    break;
878  case CURLOPT_FORBID_REUSE:
879    /*
880     * When this transfer is done, it must not be left to be reused by a
881     * subsequent transfer but shall be closed immediately.
882     */
883    data->set.reuse_forbid = (bool)(0 != va_arg(param, long));
884    break;
885  case CURLOPT_FRESH_CONNECT:
886    /*
887     * This transfer shall not use a previously cached connection but
888     * should be made with a fresh new connect!
889     */
890    data->set.reuse_fresh = (bool)(0 != va_arg(param, long));
891    break;
892  case CURLOPT_VERBOSE:
893    /*
894     * Verbose means infof() calls that give a lot of information about
895     * the connection and transfer procedures as well as internal choices.
896     */
897    data->set.verbose = (bool)(0 != va_arg(param, long));
898    break;
899  case CURLOPT_HEADER:
900    /*
901     * Set to include the header in the general data output stream.
902     */
903    data->set.include_header = (bool)(0 != va_arg(param, long));
904    break;
905  case CURLOPT_NOPROGRESS:
906    /*
907     * Shut off the internal supported progress meter
908     */
909    data->set.hide_progress = (bool)(0 != va_arg(param, long));
910    if(data->set.hide_progress)
911      data->progress.flags |= PGRS_HIDE;
912    else
913      data->progress.flags &= ~PGRS_HIDE;
914    break;
915  case CURLOPT_NOBODY:
916    /*
917     * Do not include the body part in the output data stream.
918     */
919    data->set.opt_no_body = (bool)(0 != va_arg(param, long));
920    break;
921  case CURLOPT_FAILONERROR:
922    /*
923     * Don't output the >=300 error code HTML-page, but instead only
924     * return error.
925     */
926    data->set.http_fail_on_error = (bool)(0 != va_arg(param, long));
927    break;
928  case CURLOPT_UPLOAD:
929  case CURLOPT_PUT:
930    /*
931     * We want to sent data to the remote host. If this is HTTP, that equals
932     * using the PUT request.
933     */
934    data->set.upload = (bool)(0 != va_arg(param, long));
935    if(data->set.upload) {
936      /* If this is HTTP, PUT is what's needed to "upload" */
937      data->set.httpreq = HTTPREQ_PUT;
938      data->set.opt_no_body = FALSE; /* this is implied */
939    }
940    else
941      /* In HTTP, the opposite of upload is GET (unless NOBODY is true as
942         then this can be changed to HEAD later on) */
943      data->set.httpreq = HTTPREQ_GET;
944    break;
945  case CURLOPT_FILETIME:
946    /*
947     * Try to get the file time of the remote document. The time will
948     * later (possibly) become available using curl_easy_getinfo().
949     */
950    data->set.get_filetime = (bool)(0 != va_arg(param, long));
951    break;
952  case CURLOPT_FTP_CREATE_MISSING_DIRS:
953    /*
954     * An FTP option that modifies an upload to create missing directories on
955     * the server.
956     */
957    switch(va_arg(param, long)) {
958    case 0:
959      data->set.ftp_create_missing_dirs = 0;
960      break;
961    case 1:
962      data->set.ftp_create_missing_dirs = 1;
963      break;
964    case 2:
965      data->set.ftp_create_missing_dirs = 2;
966      break;
967    default:
968      /* reserve other values for future use */
969      result = CURLE_UNKNOWN_OPTION;
970      break;
971    }
972    break;
973  case CURLOPT_SERVER_RESPONSE_TIMEOUT:
974    /*
975     * Option that specifies how quickly an server response must be obtained
976     * before it is considered failure. For pingpong protocols.
977     */
978    data->set.server_response_timeout = va_arg( param , long ) * 1000;
979    break;
980  case CURLOPT_TFTP_BLKSIZE:
981    /*
982     * TFTP option that specifies the block size to use for data transmission
983     */
984    data->set.tftp_blksize = va_arg(param, long);
985    break;
986  case CURLOPT_DIRLISTONLY:
987    /*
988     * An option that changes the command to one that asks for a list
989     * only, no file info details.
990     */
991    data->set.ftp_list_only = (bool)(0 != va_arg(param, long));
992    break;
993  case CURLOPT_APPEND:
994    /*
995     * We want to upload and append to an existing file.
996     */
997    data->set.ftp_append = (bool)(0 != va_arg(param, long));
998    break;
999  case CURLOPT_FTP_FILEMETHOD:
1000    /*
1001     * How do access files over FTP.
1002     */
1003    data->set.ftp_filemethod = (curl_ftpfile)va_arg(param, long);
1004    break;
1005  case CURLOPT_NETRC:
1006    /*
1007     * Parse the $HOME/.netrc file
1008     */
1009    data->set.use_netrc = (enum CURL_NETRC_OPTION)va_arg(param, long);
1010    break;
1011  case CURLOPT_NETRC_FILE:
1012    /*
1013     * Use this file instead of the $HOME/.netrc file
1014     */
1015    result = setstropt(&data->set.str[STRING_NETRC_FILE],
1016                       va_arg(param, char *));
1017    break;
1018  case CURLOPT_TRANSFERTEXT:
1019    /*
1020     * This option was previously named 'FTPASCII'. Renamed to work with
1021     * more protocols than merely FTP.
1022     *
1023     * Transfer using ASCII (instead of BINARY).
1024     */
1025    data->set.prefer_ascii = (bool)(0 != va_arg(param, long));
1026    break;
1027  case CURLOPT_TIMECONDITION:
1028    /*
1029     * Set HTTP time condition. This must be one of the defines in the
1030     * curl/curl.h header file.
1031     */
1032    data->set.timecondition = (curl_TimeCond)va_arg(param, long);
1033    break;
1034  case CURLOPT_TIMEVALUE:
1035    /*
1036     * This is the value to compare with the remote document with the
1037     * method set with CURLOPT_TIMECONDITION
1038     */
1039    data->set.timevalue = (time_t)va_arg(param, long);
1040    break;
1041  case CURLOPT_SSLVERSION:
1042    /*
1043     * Set explicit SSL version to try to connect with, as some SSL
1044     * implementations are lame.
1045     */
1046    data->set.ssl.version = va_arg(param, long);
1047    break;
1048
1049#ifndef CURL_DISABLE_HTTP
1050  case CURLOPT_AUTOREFERER:
1051    /*
1052     * Switch on automatic referer that gets set if curl follows locations.
1053     */
1054    data->set.http_auto_referer = (bool)(0 != va_arg(param, long));
1055    break;
1056
1057  case CURLOPT_ACCEPT_ENCODING:
1058    /*
1059     * String to use at the value of Accept-Encoding header.
1060     *
1061     * If the encoding is set to "" we use an Accept-Encoding header that
1062     * encompasses all the encodings we support.
1063     * If the encoding is set to NULL we don't send an Accept-Encoding header
1064     * and ignore an received Content-Encoding header.
1065     *
1066     */
1067    argptr = va_arg(param, char *);
1068    result = setstropt(&data->set.str[STRING_ENCODING],
1069                       (argptr && !*argptr)?
1070                       (char *) ALL_CONTENT_ENCODINGS: argptr);
1071    break;
1072
1073  case CURLOPT_TRANSFER_ENCODING:
1074    data->set.http_transfer_encoding = (bool)(0 != va_arg(param, long));
1075    break;
1076
1077  case CURLOPT_FOLLOWLOCATION:
1078    /*
1079     * Follow Location: header hints on a HTTP-server.
1080     */
1081    data->set.http_follow_location = (bool)(0 != va_arg(param, long));
1082    break;
1083
1084  case CURLOPT_UNRESTRICTED_AUTH:
1085    /*
1086     * Send authentication (user+password) when following locations, even when
1087     * hostname changed.
1088     */
1089    data->set.http_disable_hostname_check_before_authentication =
1090      (bool)(0 != va_arg(param, long));
1091    break;
1092
1093  case CURLOPT_MAXREDIRS:
1094    /*
1095     * The maximum amount of hops you allow curl to follow Location:
1096     * headers. This should mostly be used to detect never-ending loops.
1097     */
1098    data->set.maxredirs = va_arg(param, long);
1099    break;
1100
1101  case CURLOPT_POSTREDIR:
1102  {
1103    /*
1104     * Set the behaviour of POST when redirecting
1105     * CURL_REDIR_GET_ALL - POST is changed to GET after 301 and 302
1106     * CURL_REDIR_POST_301 - POST is kept as POST after 301
1107     * CURL_REDIR_POST_302 - POST is kept as POST after 302
1108     * CURL_REDIR_POST_ALL - POST is kept as POST after 301 and 302
1109     * other - POST is kept as POST after 301 and 302
1110     */
1111    long postRedir = va_arg(param, long);
1112    data->set.post301 = (bool)((postRedir & CURL_REDIR_POST_301)?TRUE:FALSE);
1113    data->set.post302 = (bool)((postRedir & CURL_REDIR_POST_302)?TRUE:FALSE);
1114  }
1115  break;
1116
1117  case CURLOPT_POST:
1118    /* Does this option serve a purpose anymore? Yes it does, when
1119       CURLOPT_POSTFIELDS isn't used and the POST data is read off the
1120       callback! */
1121    if(va_arg(param, long)) {
1122      data->set.httpreq = HTTPREQ_POST;
1123      data->set.opt_no_body = FALSE; /* this is implied */
1124    }
1125    else
1126      data->set.httpreq = HTTPREQ_GET;
1127    break;
1128
1129  case CURLOPT_COPYPOSTFIELDS:
1130    /*
1131     * A string with POST data. Makes curl HTTP POST. Even if it is NULL.
1132     * If needed, CURLOPT_POSTFIELDSIZE must have been set prior to
1133     *  CURLOPT_COPYPOSTFIELDS and not altered later.
1134     */
1135    argptr = va_arg(param, char *);
1136
1137    if(!argptr || data->set.postfieldsize == -1)
1138      result = setstropt(&data->set.str[STRING_COPYPOSTFIELDS], argptr);
1139    else {
1140      /*
1141       *  Check that requested length does not overflow the size_t type.
1142       */
1143
1144      if((data->set.postfieldsize < 0) ||
1145         ((sizeof(curl_off_t) != sizeof(size_t)) &&
1146          (data->set.postfieldsize > (curl_off_t)((size_t)-1))))
1147        result = CURLE_OUT_OF_MEMORY;
1148      else {
1149        char * p;
1150
1151        (void) setstropt(&data->set.str[STRING_COPYPOSTFIELDS], NULL);
1152
1153        /* Allocate even when size == 0. This satisfies the need of possible
1154           later address compare to detect the COPYPOSTFIELDS mode, and
1155           to mark that postfields is used rather than read function or
1156           form data.
1157        */
1158        p = malloc((size_t)(data->set.postfieldsize?
1159                            data->set.postfieldsize:1));
1160
1161        if(!p)
1162          result = CURLE_OUT_OF_MEMORY;
1163        else {
1164          if(data->set.postfieldsize)
1165            memcpy(p, argptr, (size_t)data->set.postfieldsize);
1166
1167          data->set.str[STRING_COPYPOSTFIELDS] = p;
1168        }
1169      }
1170    }
1171
1172    data->set.postfields = data->set.str[STRING_COPYPOSTFIELDS];
1173    data->set.httpreq = HTTPREQ_POST;
1174    break;
1175
1176  case CURLOPT_POSTFIELDS:
1177    /*
1178     * Like above, but use static data instead of copying it.
1179     */
1180    data->set.postfields = va_arg(param, void *);
1181    /* Release old copied data. */
1182    (void) setstropt(&data->set.str[STRING_COPYPOSTFIELDS], NULL);
1183    data->set.httpreq = HTTPREQ_POST;
1184    break;
1185
1186  case CURLOPT_POSTFIELDSIZE:
1187    /*
1188     * The size of the POSTFIELD data to prevent libcurl to do strlen() to
1189     * figure it out. Enables binary posts.
1190     */
1191    bigsize = va_arg(param, long);
1192
1193    if(data->set.postfieldsize < bigsize &&
1194       data->set.postfields == data->set.str[STRING_COPYPOSTFIELDS]) {
1195      /* Previous CURLOPT_COPYPOSTFIELDS is no longer valid. */
1196      (void) setstropt(&data->set.str[STRING_COPYPOSTFIELDS], NULL);
1197      data->set.postfields = NULL;
1198    }
1199
1200    data->set.postfieldsize = bigsize;
1201    break;
1202
1203  case CURLOPT_POSTFIELDSIZE_LARGE:
1204    /*
1205     * The size of the POSTFIELD data to prevent libcurl to do strlen() to
1206     * figure it out. Enables binary posts.
1207     */
1208    bigsize = va_arg(param, curl_off_t);
1209
1210    if(data->set.postfieldsize < bigsize &&
1211       data->set.postfields == data->set.str[STRING_COPYPOSTFIELDS]) {
1212      /* Previous CURLOPT_COPYPOSTFIELDS is no longer valid. */
1213      (void) setstropt(&data->set.str[STRING_COPYPOSTFIELDS], NULL);
1214      data->set.postfields = NULL;
1215    }
1216
1217    data->set.postfieldsize = bigsize;
1218    break;
1219
1220  case CURLOPT_HTTPPOST:
1221    /*
1222     * Set to make us do HTTP POST
1223     */
1224    data->set.httppost = va_arg(param, struct curl_httppost *);
1225    data->set.httpreq = HTTPREQ_POST_FORM;
1226    data->set.opt_no_body = FALSE; /* this is implied */
1227    break;
1228
1229  case CURLOPT_REFERER:
1230    /*
1231     * String to set in the HTTP Referer: field.
1232     */
1233    if(data->change.referer_alloc) {
1234      free(data->change.referer);
1235      data->change.referer_alloc = FALSE;
1236    }
1237    result = setstropt(&data->set.str[STRING_SET_REFERER],
1238                       va_arg(param, char *));
1239    data->change.referer = data->set.str[STRING_SET_REFERER];
1240    break;
1241
1242  case CURLOPT_USERAGENT:
1243    /*
1244     * String to use in the HTTP User-Agent field
1245     */
1246    result = setstropt(&data->set.str[STRING_USERAGENT],
1247                       va_arg(param, char *));
1248    break;
1249
1250  case CURLOPT_HTTPHEADER:
1251    /*
1252     * Set a list with HTTP headers to use (or replace internals with)
1253     */
1254    data->set.headers = va_arg(param, struct curl_slist *);
1255    break;
1256
1257  case CURLOPT_HTTP200ALIASES:
1258    /*
1259     * Set a list of aliases for HTTP 200 in response header
1260     */
1261    data->set.http200aliases = va_arg(param, struct curl_slist *);
1262    break;
1263
1264#if !defined(CURL_DISABLE_COOKIES)
1265  case CURLOPT_COOKIE:
1266    /*
1267     * Cookie string to send to the remote server in the request.
1268     */
1269    result = setstropt(&data->set.str[STRING_COOKIE],
1270                       va_arg(param, char *));
1271    break;
1272
1273  case CURLOPT_COOKIEFILE:
1274    /*
1275     * Set cookie file to read and parse. Can be used multiple times.
1276     */
1277    argptr = (char *)va_arg(param, void *);
1278    if(argptr) {
1279      struct curl_slist *cl;
1280      /* append the cookie file name to the list of file names, and deal with
1281         them later */
1282      cl = curl_slist_append(data->change.cookielist, argptr);
1283
1284      if(!cl)
1285        return CURLE_OUT_OF_MEMORY;
1286
1287      data->change.cookielist = cl; /* store the list for later use */
1288    }
1289    break;
1290
1291  case CURLOPT_COOKIEJAR:
1292    /*
1293     * Set cookie file name to dump all cookies to when we're done.
1294     */
1295    result = setstropt(&data->set.str[STRING_COOKIEJAR],
1296                       va_arg(param, char *));
1297
1298    /*
1299     * Activate the cookie parser. This may or may not already
1300     * have been made.
1301     */
1302    data->cookies = Curl_cookie_init(data, NULL, data->cookies,
1303                                     data->set.cookiesession);
1304    break;
1305
1306  case CURLOPT_COOKIESESSION:
1307    /*
1308     * Set this option to TRUE to start a new "cookie session". It will
1309     * prevent the forthcoming read-cookies-from-file actions to accept
1310     * cookies that are marked as being session cookies, as they belong to a
1311     * previous session.
1312     *
1313     * In the original Netscape cookie spec, "session cookies" are cookies
1314     * with no expire date set. RFC2109 describes the same action if no
1315     * 'Max-Age' is set and RFC2965 includes the RFC2109 description and adds
1316     * a 'Discard' action that can enforce the discard even for cookies that
1317     * have a Max-Age.
1318     *
1319     * We run mostly with the original cookie spec, as hardly anyone implements
1320     * anything else.
1321     */
1322    data->set.cookiesession = (bool)(0 != va_arg(param, long));
1323    break;
1324
1325  case CURLOPT_COOKIELIST:
1326    argptr = va_arg(param, char *);
1327
1328    if(argptr == NULL)
1329      break;
1330
1331    if(Curl_raw_equal(argptr, "ALL")) {
1332      /* clear all cookies */
1333      Curl_cookie_clearall(data->cookies);
1334      break;
1335    }
1336    else if(Curl_raw_equal(argptr, "SESS")) {
1337      /* clear session cookies */
1338      Curl_cookie_clearsess(data->cookies);
1339      break;
1340    }
1341    else if(Curl_raw_equal(argptr, "FLUSH")) {
1342      /* flush cookies to file */
1343      Curl_flush_cookies(data, 0);
1344      break;
1345    }
1346
1347    if(!data->cookies)
1348      /* if cookie engine was not running, activate it */
1349      data->cookies = Curl_cookie_init(data, NULL, NULL, TRUE);
1350
1351    argptr = strdup(argptr);
1352    if(!argptr) {
1353      result = CURLE_OUT_OF_MEMORY;
1354      break;
1355    }
1356
1357    if(checkprefix("Set-Cookie:", argptr))
1358      /* HTTP Header format line */
1359      Curl_cookie_add(data, data->cookies, TRUE, argptr + 11, NULL, NULL);
1360
1361    else
1362      /* Netscape format line */
1363      Curl_cookie_add(data, data->cookies, FALSE, argptr, NULL, NULL);
1364
1365    free(argptr);
1366    break;
1367#endif /* CURL_DISABLE_COOKIES */
1368
1369  case CURLOPT_HTTPGET:
1370    /*
1371     * Set to force us do HTTP GET
1372     */
1373    if(va_arg(param, long)) {
1374      data->set.httpreq = HTTPREQ_GET;
1375      data->set.upload = FALSE; /* switch off upload */
1376      data->set.opt_no_body = FALSE; /* this is implied */
1377    }
1378    break;
1379
1380  case CURLOPT_HTTP_VERSION:
1381    /*
1382     * This sets a requested HTTP version to be used. The value is one of
1383     * the listed enums in curl/curl.h.
1384     */
1385    data->set.httpversion = va_arg(param, long);
1386    break;
1387
1388  case CURLOPT_HTTPAUTH:
1389    /*
1390     * Set HTTP Authentication type BITMASK.
1391     */
1392  {
1393    long auth = va_arg(param, long);
1394
1395    /* the DIGEST_IE bit is only used to set a special marker, for all the
1396       rest we need to handle it as normal DIGEST */
1397    data->state.authhost.iestyle = (bool)((auth & CURLAUTH_DIGEST_IE)?
1398                                          TRUE:FALSE);
1399
1400    if(auth & CURLAUTH_DIGEST_IE) {
1401      auth |= CURLAUTH_DIGEST; /* set standard digest bit */
1402      auth &= ~CURLAUTH_DIGEST_IE; /* unset ie digest bit */
1403    }
1404
1405    /* switch off bits we can't support */
1406#ifndef USE_NTLM
1407    auth &= ~CURLAUTH_NTLM; /* no NTLM without SSL */
1408#endif
1409#ifndef USE_HTTP_NEGOTIATE
1410    auth &= ~CURLAUTH_GSSNEGOTIATE; /* no GSS-Negotiate without GSSAPI or
1411                                       WINDOWS_SSPI */
1412#endif
1413    if(!auth)
1414      return CURLE_NOT_BUILT_IN; /* no supported types left! */
1415
1416    data->set.httpauth = auth;
1417  }
1418  break;
1419
1420#endif   /* CURL_DISABLE_HTTP */
1421
1422  case CURLOPT_CUSTOMREQUEST:
1423    /*
1424     * Set a custom string to use as request
1425     */
1426    result = setstropt(&data->set.str[STRING_CUSTOMREQUEST],
1427                       va_arg(param, char *));
1428
1429    /* we don't set
1430       data->set.httpreq = HTTPREQ_CUSTOM;
1431       here, we continue as if we were using the already set type
1432       and this just changes the actual request keyword */
1433    break;
1434
1435#ifndef CURL_DISABLE_PROXY
1436  case CURLOPT_HTTPPROXYTUNNEL:
1437    /*
1438     * Tunnel operations through the proxy instead of normal proxy use
1439     */
1440    data->set.tunnel_thru_httpproxy = (bool)(0 != va_arg(param, long));
1441    break;
1442
1443  case CURLOPT_PROXYPORT:
1444    /*
1445     * Explicitly set HTTP proxy port number.
1446     */
1447    data->set.proxyport = va_arg(param, long);
1448    break;
1449
1450  case CURLOPT_PROXYAUTH:
1451    /*
1452     * Set HTTP Authentication type BITMASK.
1453     */
1454  {
1455    long auth = va_arg(param, long);
1456
1457    /* the DIGEST_IE bit is only used to set a special marker, for all the
1458       rest we need to handle it as normal DIGEST */
1459    data->state.authproxy.iestyle = (bool)((auth & CURLAUTH_DIGEST_IE)?
1460                                           TRUE:FALSE);
1461
1462    if(auth & CURLAUTH_DIGEST_IE) {
1463      auth |= CURLAUTH_DIGEST; /* set standard digest bit */
1464      auth &= ~CURLAUTH_DIGEST_IE; /* unset ie digest bit */
1465    }
1466    /* switch off bits we can't support */
1467#ifndef USE_NTLM
1468    auth &= ~CURLAUTH_NTLM; /* no NTLM without SSL */
1469#endif
1470#ifndef USE_HTTP_NEGOTIATE
1471    auth &= ~CURLAUTH_GSSNEGOTIATE; /* no GSS-Negotiate without GSSAPI or
1472                                       WINDOWS_SSPI */
1473#endif
1474    if(!auth)
1475      return CURLE_NOT_BUILT_IN; /* no supported types left! */
1476
1477    data->set.proxyauth = auth;
1478  }
1479  break;
1480
1481  case CURLOPT_PROXY:
1482    /*
1483     * Set proxy server:port to use as HTTP proxy.
1484     *
1485     * If the proxy is set to "" we explicitly say that we don't want to use a
1486     * proxy (even though there might be environment variables saying so).
1487     *
1488     * Setting it to NULL, means no proxy but allows the environment variables
1489     * to decide for us.
1490     */
1491    result = setstropt(&data->set.str[STRING_PROXY],
1492                       va_arg(param, char *));
1493    break;
1494
1495  case CURLOPT_PROXYTYPE:
1496    /*
1497     * Set proxy type. HTTP/HTTP_1_0/SOCKS4/SOCKS4a/SOCKS5/SOCKS5_HOSTNAME
1498     */
1499    data->set.proxytype = (curl_proxytype)va_arg(param, long);
1500    break;
1501
1502  case CURLOPT_PROXY_TRANSFER_MODE:
1503    /*
1504     * set transfer mode (;type=<a|i>) when doing FTP via an HTTP proxy
1505     */
1506    switch (va_arg(param, long)) {
1507    case 0:
1508      data->set.proxy_transfer_mode = FALSE;
1509      break;
1510    case 1:
1511      data->set.proxy_transfer_mode = TRUE;
1512      break;
1513    default:
1514      /* reserve other values for future use */
1515      result = CURLE_UNKNOWN_OPTION;
1516      break;
1517    }
1518    break;
1519#endif   /* CURL_DISABLE_PROXY */
1520
1521#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
1522  case CURLOPT_SOCKS5_GSSAPI_SERVICE:
1523    /*
1524     * Set gssapi service name
1525     */
1526    result = setstropt(&data->set.str[STRING_SOCKS5_GSSAPI_SERVICE],
1527                       va_arg(param, char *));
1528    break;
1529
1530  case CURLOPT_SOCKS5_GSSAPI_NEC:
1531    /*
1532     * set flag for nec socks5 support
1533     */
1534    data->set.socks5_gssapi_nec = (bool)(0 != va_arg(param, long));
1535    break;
1536#endif
1537
1538  case CURLOPT_WRITEHEADER:
1539    /*
1540     * Custom pointer to pass the header write callback function
1541     */
1542    data->set.writeheader = (void *)va_arg(param, void *);
1543    break;
1544  case CURLOPT_ERRORBUFFER:
1545    /*
1546     * Error buffer provided by the caller to get the human readable
1547     * error string in.
1548     */
1549    data->set.errorbuffer = va_arg(param, char *);
1550    break;
1551  case CURLOPT_FILE:
1552    /*
1553     * FILE pointer to write to or include in the data write callback
1554     */
1555    data->set.out = va_arg(param, FILE *);
1556    break;
1557  case CURLOPT_FTPPORT:
1558    /*
1559     * Use FTP PORT, this also specifies which IP address to use
1560     */
1561    result = setstropt(&data->set.str[STRING_FTPPORT],
1562                       va_arg(param, char *));
1563    data->set.ftp_use_port = (bool)(NULL != data->set.str[STRING_FTPPORT]);
1564    break;
1565
1566  case CURLOPT_FTP_USE_EPRT:
1567    data->set.ftp_use_eprt = (bool)(0 != va_arg(param, long));
1568    break;
1569
1570  case CURLOPT_FTP_USE_EPSV:
1571    data->set.ftp_use_epsv = (bool)(0 != va_arg(param, long));
1572    break;
1573
1574  case CURLOPT_FTP_USE_PRET:
1575    data->set.ftp_use_pret = (bool)(0 != va_arg(param, long));
1576    break;
1577
1578  case CURLOPT_FTP_SSL_CCC:
1579    data->set.ftp_ccc = (curl_ftpccc)va_arg(param, long);
1580    break;
1581
1582  case CURLOPT_FTP_SKIP_PASV_IP:
1583    /*
1584     * Enable or disable FTP_SKIP_PASV_IP, which will disable/enable the
1585     * bypass of the IP address in PASV responses.
1586     */
1587    data->set.ftp_skip_ip = (bool)(0 != va_arg(param, long));
1588    break;
1589
1590  case CURLOPT_INFILE:
1591    /*
1592     * FILE pointer to read the file to be uploaded from. Or possibly
1593     * used as argument to the read callback.
1594     */
1595    data->set.in = va_arg(param, FILE *);
1596    break;
1597  case CURLOPT_INFILESIZE:
1598    /*
1599     * If known, this should inform curl about the file size of the
1600     * to-be-uploaded file.
1601     */
1602    data->set.infilesize = va_arg(param, long);
1603    break;
1604  case CURLOPT_INFILESIZE_LARGE:
1605    /*
1606     * If known, this should inform curl about the file size of the
1607     * to-be-uploaded file.
1608     */
1609    data->set.infilesize = va_arg(param, curl_off_t);
1610    break;
1611  case CURLOPT_LOW_SPEED_LIMIT:
1612    /*
1613     * The low speed limit that if transfers are below this for
1614     * CURLOPT_LOW_SPEED_TIME, the transfer is aborted.
1615     */
1616    data->set.low_speed_limit=va_arg(param, long);
1617    break;
1618  case CURLOPT_MAX_SEND_SPEED_LARGE:
1619    /*
1620     * When transfer uploads are faster then CURLOPT_MAX_SEND_SPEED_LARGE
1621     * bytes per second the transfer is throttled..
1622     */
1623    data->set.max_send_speed=va_arg(param, curl_off_t);
1624    break;
1625  case CURLOPT_MAX_RECV_SPEED_LARGE:
1626    /*
1627     * When receiving data faster than CURLOPT_MAX_RECV_SPEED_LARGE bytes per
1628     * second the transfer is throttled..
1629     */
1630    data->set.max_recv_speed=va_arg(param, curl_off_t);
1631    break;
1632  case CURLOPT_LOW_SPEED_TIME:
1633    /*
1634     * The low speed time that if transfers are below the set
1635     * CURLOPT_LOW_SPEED_LIMIT during this time, the transfer is aborted.
1636     */
1637    data->set.low_speed_time=va_arg(param, long);
1638    break;
1639  case CURLOPT_URL:
1640    /*
1641     * The URL to fetch.
1642     */
1643    if(data->change.url_alloc) {
1644      /* the already set URL is allocated, free it first! */
1645      free(data->change.url);
1646      data->change.url_alloc=FALSE;
1647    }
1648    result = setstropt(&data->set.str[STRING_SET_URL],
1649                       va_arg(param, char *));
1650    data->change.url = data->set.str[STRING_SET_URL];
1651    break;
1652  case CURLOPT_PORT:
1653    /*
1654     * The port number to use when getting the URL
1655     */
1656    data->set.use_port = va_arg(param, long);
1657    break;
1658  case CURLOPT_TIMEOUT:
1659    /*
1660     * The maximum time you allow curl to use for a single transfer
1661     * operation.
1662     */
1663    data->set.timeout = va_arg(param, long) * 1000L;
1664    break;
1665
1666  case CURLOPT_TIMEOUT_MS:
1667    data->set.timeout = va_arg(param, long);
1668    break;
1669
1670  case CURLOPT_CONNECTTIMEOUT:
1671    /*
1672     * The maximum time you allow curl to use to connect.
1673     */
1674    data->set.connecttimeout = va_arg(param, long) * 1000L;
1675    break;
1676
1677  case CURLOPT_CONNECTTIMEOUT_MS:
1678    data->set.connecttimeout = va_arg(param, long);
1679    break;
1680
1681  case CURLOPT_USERPWD:
1682    /*
1683     * user:password to use in the operation
1684     */
1685    result = setstropt_userpwd(va_arg(param, char *),
1686                               &data->set.str[STRING_USERNAME],
1687                               &data->set.str[STRING_PASSWORD]);
1688    break;
1689  case CURLOPT_USERNAME:
1690    /*
1691     * authentication user name to use in the operation
1692     */
1693    result = setstropt(&data->set.str[STRING_USERNAME],
1694                       va_arg(param, char *));
1695    break;
1696  case CURLOPT_PASSWORD:
1697    /*
1698     * authentication password to use in the operation
1699     */
1700    result = setstropt(&data->set.str[STRING_PASSWORD],
1701                       va_arg(param, char *));
1702    break;
1703  case CURLOPT_POSTQUOTE:
1704    /*
1705     * List of RAW FTP commands to use after a transfer
1706     */
1707    data->set.postquote = va_arg(param, struct curl_slist *);
1708    break;
1709  case CURLOPT_PREQUOTE:
1710    /*
1711     * List of RAW FTP commands to use prior to RETR (Wesley Laxton)
1712     */
1713    data->set.prequote = va_arg(param, struct curl_slist *);
1714    break;
1715  case CURLOPT_QUOTE:
1716    /*
1717     * List of RAW FTP commands to use before a transfer
1718     */
1719    data->set.quote = va_arg(param, struct curl_slist *);
1720    break;
1721  case CURLOPT_RESOLVE:
1722    /*
1723     * List of NAME:[address] names to populate the DNS cache with
1724     * Prefix the NAME with dash (-) to _remove_ the name from the cache.
1725     *
1726     * Names added with this API will remain in the cache until explicitly
1727     * removed or the handle is cleaned up.
1728     *
1729     * This API can remove any name from the DNS cache, but only entries
1730     * that aren't actually in use right now will be pruned immediately.
1731     */
1732    data->set.resolve = va_arg(param, struct curl_slist *);
1733    data->change.resolve = data->set.resolve;
1734    break;
1735  case CURLOPT_PROGRESSFUNCTION:
1736    /*
1737     * Progress callback function
1738     */
1739    data->set.fprogress = va_arg(param, curl_progress_callback);
1740    if(data->set.fprogress)
1741      data->progress.callback = TRUE; /* no longer internal */
1742    else
1743      data->progress.callback = FALSE; /* NULL enforces internal */
1744
1745    break;
1746  case CURLOPT_PROGRESSDATA:
1747    /*
1748     * Custom client data to pass to the progress callback
1749     */
1750    data->set.progress_client = va_arg(param, void *);
1751    break;
1752
1753#ifndef CURL_DISABLE_PROXY
1754  case CURLOPT_PROXYUSERPWD:
1755    /*
1756     * user:password needed to use the proxy
1757     */
1758    result = setstropt_userpwd(va_arg(param, char *),
1759                               &data->set.str[STRING_PROXYUSERNAME],
1760                               &data->set.str[STRING_PROXYPASSWORD]);
1761    break;
1762  case CURLOPT_PROXYUSERNAME:
1763    /*
1764     * authentication user name to use in the operation
1765     */
1766    result = setstropt(&data->set.str[STRING_PROXYUSERNAME],
1767                       va_arg(param, char *));
1768    break;
1769  case CURLOPT_PROXYPASSWORD:
1770    /*
1771     * authentication password to use in the operation
1772     */
1773    result = setstropt(&data->set.str[STRING_PROXYPASSWORD],
1774                       va_arg(param, char *));
1775    break;
1776  case CURLOPT_NOPROXY:
1777    /*
1778     * proxy exception list
1779     */
1780    result = setstropt(&data->set.str[STRING_NOPROXY],
1781                       va_arg(param, char *));
1782    break;
1783#endif
1784
1785  case CURLOPT_RANGE:
1786    /*
1787     * What range of the file you want to transfer
1788     */
1789    result = setstropt(&data->set.str[STRING_SET_RANGE],
1790                       va_arg(param, char *));
1791    break;
1792  case CURLOPT_RESUME_FROM:
1793    /*
1794     * Resume transfer at the give file position
1795     */
1796    data->set.set_resume_from = va_arg(param, long);
1797    break;
1798  case CURLOPT_RESUME_FROM_LARGE:
1799    /*
1800     * Resume transfer at the give file position
1801     */
1802    data->set.set_resume_from = va_arg(param, curl_off_t);
1803    break;
1804  case CURLOPT_DEBUGFUNCTION:
1805    /*
1806     * stderr write callback.
1807     */
1808    data->set.fdebug = va_arg(param, curl_debug_callback);
1809    /*
1810     * if the callback provided is NULL, it'll use the default callback
1811     */
1812    break;
1813  case CURLOPT_DEBUGDATA:
1814    /*
1815     * Set to a void * that should receive all error writes. This
1816     * defaults to CURLOPT_STDERR for normal operations.
1817     */
1818    data->set.debugdata = va_arg(param, void *);
1819    break;
1820  case CURLOPT_STDERR:
1821    /*
1822     * Set to a FILE * that should receive all error writes. This
1823     * defaults to stderr for normal operations.
1824     */
1825    data->set.err = va_arg(param, FILE *);
1826    if(!data->set.err)
1827      data->set.err = stderr;
1828    break;
1829  case CURLOPT_HEADERFUNCTION:
1830    /*
1831     * Set header write callback
1832     */
1833    data->set.fwrite_header = va_arg(param, curl_write_callback);
1834    break;
1835  case CURLOPT_WRITEFUNCTION:
1836    /*
1837     * Set data write callback
1838     */
1839    data->set.fwrite_func = va_arg(param, curl_write_callback);
1840    if(!data->set.fwrite_func) {
1841      data->set.is_fwrite_set = 0;
1842      /* When set to NULL, reset to our internal default function */
1843      data->set.fwrite_func = (curl_write_callback)fwrite;
1844    }
1845    else
1846      data->set.is_fwrite_set = 1;
1847    break;
1848  case CURLOPT_READFUNCTION:
1849    /*
1850     * Read data callback
1851     */
1852    data->set.fread_func = va_arg(param, curl_read_callback);
1853    if(!data->set.fread_func) {
1854      data->set.is_fread_set = 0;
1855      /* When set to NULL, reset to our internal default function */
1856      data->set.fread_func = (curl_read_callback)fread;
1857    }
1858    else
1859      data->set.is_fread_set = 1;
1860    break;
1861  case CURLOPT_SEEKFUNCTION:
1862    /*
1863     * Seek callback. Might be NULL.
1864     */
1865    data->set.seek_func = va_arg(param, curl_seek_callback);
1866    break;
1867  case CURLOPT_SEEKDATA:
1868    /*
1869     * Seek control callback. Might be NULL.
1870     */
1871    data->set.seek_client = va_arg(param, void *);
1872    break;
1873  case CURLOPT_CONV_FROM_NETWORK_FUNCTION:
1874    /*
1875     * "Convert from network encoding" callback
1876     */
1877    data->set.convfromnetwork = va_arg(param, curl_conv_callback);
1878    break;
1879  case CURLOPT_CONV_TO_NETWORK_FUNCTION:
1880    /*
1881     * "Convert to network encoding" callback
1882     */
1883    data->set.convtonetwork = va_arg(param, curl_conv_callback);
1884    break;
1885  case CURLOPT_CONV_FROM_UTF8_FUNCTION:
1886    /*
1887     * "Convert from UTF-8 encoding" callback
1888     */
1889    data->set.convfromutf8 = va_arg(param, curl_conv_callback);
1890    break;
1891  case CURLOPT_IOCTLFUNCTION:
1892    /*
1893     * I/O control callback. Might be NULL.
1894     */
1895    data->set.ioctl_func = va_arg(param, curl_ioctl_callback);
1896    break;
1897  case CURLOPT_IOCTLDATA:
1898    /*
1899     * I/O control data pointer. Might be NULL.
1900     */
1901    data->set.ioctl_client = va_arg(param, void *);
1902    break;
1903  case CURLOPT_SSLCERT:
1904    /*
1905     * String that holds file name of the SSL certificate to use
1906     */
1907    result = setstropt(&data->set.str[STRING_CERT],
1908                       va_arg(param, char *));
1909    break;
1910  case CURLOPT_SSLCERTTYPE:
1911    /*
1912     * String that holds file type of the SSL certificate to use
1913     */
1914    result = setstropt(&data->set.str[STRING_CERT_TYPE],
1915                       va_arg(param, char *));
1916    break;
1917  case CURLOPT_SSLKEY:
1918    /*
1919     * String that holds file name of the SSL key to use
1920     */
1921    result = setstropt(&data->set.str[STRING_KEY],
1922                       va_arg(param, char *));
1923    break;
1924  case CURLOPT_SSLKEYTYPE:
1925    /*
1926     * String that holds file type of the SSL key to use
1927     */
1928    result = setstropt(&data->set.str[STRING_KEY_TYPE],
1929                       va_arg(param, char *));
1930    break;
1931  case CURLOPT_KEYPASSWD:
1932    /*
1933     * String that holds the SSL or SSH private key password.
1934     */
1935    result = setstropt(&data->set.str[STRING_KEY_PASSWD],
1936                       va_arg(param, char *));
1937    break;
1938  case CURLOPT_SSLENGINE:
1939    /*
1940     * String that holds the SSL crypto engine.
1941     */
1942    argptr = va_arg(param, char *);
1943    if(argptr && argptr[0])
1944      result = Curl_ssl_set_engine(data, argptr);
1945    break;
1946
1947  case CURLOPT_SSLENGINE_DEFAULT:
1948    /*
1949     * flag to set engine as default.
1950     */
1951    result = Curl_ssl_set_engine_default(data);
1952    break;
1953  case CURLOPT_CRLF:
1954    /*
1955     * Kludgy option to enable CRLF conversions. Subject for removal.
1956     */
1957    data->set.crlf = (bool)(0 != va_arg(param, long));
1958    break;
1959
1960  case CURLOPT_INTERFACE:
1961    /*
1962     * Set what interface or address/hostname to bind the socket to when
1963     * performing an operation and thus what from-IP your connection will use.
1964     */
1965    result = setstropt(&data->set.str[STRING_DEVICE],
1966                       va_arg(param, char *));
1967    break;
1968  case CURLOPT_LOCALPORT:
1969    /*
1970     * Set what local port to bind the socket to when performing an operation.
1971     */
1972    data->set.localport = curlx_sltous(va_arg(param, long));
1973    break;
1974  case CURLOPT_LOCALPORTRANGE:
1975    /*
1976     * Set number of local ports to try, starting with CURLOPT_LOCALPORT.
1977     */
1978    data->set.localportrange = curlx_sltosi(va_arg(param, long));
1979    break;
1980  case CURLOPT_KRBLEVEL:
1981    /*
1982     * A string that defines the kerberos security level.
1983     */
1984    result = setstropt(&data->set.str[STRING_KRB_LEVEL],
1985                       va_arg(param, char *));
1986    data->set.krb = (bool)(NULL != data->set.str[STRING_KRB_LEVEL]);
1987    break;
1988  case CURLOPT_SSL_VERIFYPEER:
1989    /*
1990     * Enable peer SSL verifying.
1991     */
1992    data->set.ssl.verifypeer = va_arg(param, long);
1993    break;
1994  case CURLOPT_SSL_VERIFYHOST:
1995    /*
1996     * Enable verification of the CN contained in the peer certificate
1997     */
1998    data->set.ssl.verifyhost = va_arg(param, long);
1999    break;
2000#ifdef USE_SSLEAY
2001    /* since these two options are only possible to use on an OpenSSL-
2002       powered libcurl we #ifdef them on this condition so that libcurls
2003       built against other SSL libs will return a proper error when trying
2004       to set this option! */
2005  case CURLOPT_SSL_CTX_FUNCTION:
2006    /*
2007     * Set a SSL_CTX callback
2008     */
2009    data->set.ssl.fsslctx = va_arg(param, curl_ssl_ctx_callback);
2010    break;
2011  case CURLOPT_SSL_CTX_DATA:
2012    /*
2013     * Set a SSL_CTX callback parameter pointer
2014     */
2015    data->set.ssl.fsslctxp = va_arg(param, void *);
2016    break;
2017  case CURLOPT_CERTINFO:
2018    data->set.ssl.certinfo = (bool)(0 != va_arg(param, long));
2019    break;
2020#endif
2021  case CURLOPT_CAINFO:
2022    /*
2023     * Set CA info for SSL connection. Specify file name of the CA certificate
2024     */
2025    result = setstropt(&data->set.str[STRING_SSL_CAFILE],
2026                       va_arg(param, char *));
2027    break;
2028  case CURLOPT_CAPATH:
2029    /*
2030     * Set CA path info for SSL connection. Specify directory name of the CA
2031     * certificates which have been prepared using openssl c_rehash utility.
2032     */
2033    /* This does not work on windows. */
2034    result = setstropt(&data->set.str[STRING_SSL_CAPATH],
2035                       va_arg(param, char *));
2036    break;
2037  case CURLOPT_CRLFILE:
2038    /*
2039     * Set CRL file info for SSL connection. Specify file name of the CRL
2040     * to check certificates revocation
2041     */
2042    result = setstropt(&data->set.str[STRING_SSL_CRLFILE],
2043                       va_arg(param, char *));
2044    break;
2045  case CURLOPT_ISSUERCERT:
2046    /*
2047     * Set Issuer certificate file
2048     * to check certificates issuer
2049     */
2050    result = setstropt(&data->set.str[STRING_SSL_ISSUERCERT],
2051                       va_arg(param, char *));
2052    break;
2053  case CURLOPT_TELNETOPTIONS:
2054    /*
2055     * Set a linked list of telnet options
2056     */
2057    data->set.telnet_options = va_arg(param, struct curl_slist *);
2058    break;
2059
2060  case CURLOPT_BUFFERSIZE:
2061    /*
2062     * The application kindly asks for a differently sized receive buffer.
2063     * If it seems reasonable, we'll use it.
2064     */
2065    data->set.buffer_size = va_arg(param, long);
2066
2067    if((data->set.buffer_size> (BUFSIZE -1 )) ||
2068       (data->set.buffer_size < 1))
2069      data->set.buffer_size = 0; /* huge internal default */
2070
2071    break;
2072
2073  case CURLOPT_NOSIGNAL:
2074    /*
2075     * The application asks not to set any signal() or alarm() handlers,
2076     * even when using a timeout.
2077     */
2078    data->set.no_signal = (bool)(0 != va_arg(param, long));
2079    break;
2080
2081  case CURLOPT_SHARE:
2082  {
2083    struct Curl_share *set;
2084    set = va_arg(param, struct Curl_share *);
2085
2086    /* disconnect from old share, if any */
2087    if(data->share) {
2088      Curl_share_lock(data, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE);
2089
2090      if(data->dns.hostcachetype == HCACHE_SHARED) {
2091        data->dns.hostcache = NULL;
2092        data->dns.hostcachetype = HCACHE_NONE;
2093      }
2094
2095      if(data->share->cookies == data->cookies)
2096        data->cookies = NULL;
2097
2098      data->share->dirty--;
2099
2100      Curl_share_unlock(data, CURL_LOCK_DATA_SHARE);
2101      data->share = NULL;
2102    }
2103
2104    /* use new share if it set */
2105    data->share = set;
2106    if(data->share) {
2107
2108      Curl_share_lock(data, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE);
2109
2110      data->share->dirty++;
2111
2112      if(data->share->hostcache) {
2113        /* use shared host cache, first free the private one if any */
2114        if(data->dns.hostcachetype == HCACHE_PRIVATE)
2115          Curl_hash_destroy(data->dns.hostcache);
2116
2117        data->dns.hostcache = data->share->hostcache;
2118        data->dns.hostcachetype = HCACHE_SHARED;
2119      }
2120#if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
2121      if(data->share->cookies) {
2122        /* use shared cookie list, first free own one if any */
2123        if(data->cookies)
2124          Curl_cookie_cleanup(data->cookies);
2125        /* enable cookies since we now use a share that uses cookies! */
2126        data->cookies = data->share->cookies;
2127      }
2128#endif   /* CURL_DISABLE_HTTP */
2129      Curl_share_unlock(data, CURL_LOCK_DATA_SHARE);
2130
2131    }
2132    /* check for host cache not needed,
2133     * it will be done by curl_easy_perform */
2134  }
2135  break;
2136
2137  case CURLOPT_PRIVATE:
2138    /*
2139     * Set private data pointer.
2140     */
2141    data->set.private_data = va_arg(param, void *);
2142    break;
2143
2144  case CURLOPT_MAXFILESIZE:
2145    /*
2146     * Set the maximum size of a file to download.
2147     */
2148    data->set.max_filesize = va_arg(param, long);
2149    break;
2150
2151#ifdef USE_SSL
2152  case CURLOPT_USE_SSL:
2153    /*
2154     * Make transfers attempt to use SSL/TLS.
2155     */
2156    data->set.ftp_ssl = (curl_usessl)va_arg(param, long);
2157    break;
2158#endif
2159  case CURLOPT_FTPSSLAUTH:
2160    /*
2161     * Set a specific auth for FTP-SSL transfers.
2162     */
2163    data->set.ftpsslauth = (curl_ftpauth)va_arg(param, long);
2164    break;
2165
2166  case CURLOPT_IPRESOLVE:
2167    data->set.ipver = va_arg(param, long);
2168    break;
2169
2170  case CURLOPT_MAXFILESIZE_LARGE:
2171    /*
2172     * Set the maximum size of a file to download.
2173     */
2174    data->set.max_filesize = va_arg(param, curl_off_t);
2175    break;
2176
2177  case CURLOPT_TCP_NODELAY:
2178    /*
2179     * Enable or disable TCP_NODELAY, which will disable/enable the Nagle
2180     * algorithm
2181     */
2182    data->set.tcp_nodelay = (bool)(0 != va_arg(param, long));
2183    break;
2184
2185  case CURLOPT_FTP_ACCOUNT:
2186    result = setstropt(&data->set.str[STRING_FTP_ACCOUNT],
2187                       va_arg(param, char *));
2188    break;
2189
2190  case CURLOPT_IGNORE_CONTENT_LENGTH:
2191    data->set.ignorecl = (bool)(0 != va_arg(param, long));
2192    break;
2193
2194  case CURLOPT_CONNECT_ONLY:
2195    /*
2196     * No data transfer, set up connection and let application use the socket
2197     */
2198    data->set.connect_only = (bool)(0 != va_arg(param, long));
2199    break;
2200
2201  case CURLOPT_FTP_ALTERNATIVE_TO_USER:
2202    result = setstropt(&data->set.str[STRING_FTP_ALTERNATIVE_TO_USER],
2203                       va_arg(param, char *));
2204    break;
2205
2206  case CURLOPT_SOCKOPTFUNCTION:
2207    /*
2208     * socket callback function: called after socket() but before connect()
2209     */
2210    data->set.fsockopt = va_arg(param, curl_sockopt_callback);
2211    break;
2212
2213  case CURLOPT_SOCKOPTDATA:
2214    /*
2215     * socket callback data pointer. Might be NULL.
2216     */
2217    data->set.sockopt_client = va_arg(param, void *);
2218    break;
2219
2220  case CURLOPT_OPENSOCKETFUNCTION:
2221    /*
2222     * open/create socket callback function: called instead of socket(),
2223     * before connect()
2224     */
2225    data->set.fopensocket = va_arg(param, curl_opensocket_callback);
2226    break;
2227
2228  case CURLOPT_OPENSOCKETDATA:
2229    /*
2230     * socket callback data pointer. Might be NULL.
2231     */
2232    data->set.opensocket_client = va_arg(param, void *);
2233    break;
2234
2235  case CURLOPT_CLOSESOCKETFUNCTION:
2236    /*
2237     * close socket callback function: called instead of close()
2238     * when shutting down a connection
2239     */
2240    data->set.fclosesocket = va_arg(param, curl_closesocket_callback);
2241    break;
2242
2243  case CURLOPT_CLOSESOCKETDATA:
2244    /*
2245     * socket callback data pointer. Might be NULL.
2246     */
2247    data->set.closesocket_client = va_arg(param, void *);
2248    break;
2249
2250  case CURLOPT_SSL_SESSIONID_CACHE:
2251    data->set.ssl.sessionid = (bool)(0 != va_arg(param, long));
2252    break;
2253
2254#ifdef USE_LIBSSH2
2255    /* we only include SSH options if explicitly built to support SSH */
2256  case CURLOPT_SSH_AUTH_TYPES:
2257    data->set.ssh_auth_types = va_arg(param, long);
2258    break;
2259
2260  case CURLOPT_SSH_PUBLIC_KEYFILE:
2261    /*
2262     * Use this file instead of the $HOME/.ssh/id_dsa.pub file
2263     */
2264    result = setstropt(&data->set.str[STRING_SSH_PUBLIC_KEY],
2265                       va_arg(param, char *));
2266    break;
2267
2268  case CURLOPT_SSH_PRIVATE_KEYFILE:
2269    /*
2270     * Use this file instead of the $HOME/.ssh/id_dsa file
2271     */
2272    result = setstropt(&data->set.str[STRING_SSH_PRIVATE_KEY],
2273                       va_arg(param, char *));
2274    break;
2275  case CURLOPT_SSH_HOST_PUBLIC_KEY_MD5:
2276    /*
2277     * Option to allow for the MD5 of the host public key to be checked
2278     * for validation purposes.
2279     */
2280    result = setstropt(&data->set.str[STRING_SSH_HOST_PUBLIC_KEY_MD5],
2281                       va_arg(param, char *));
2282    break;
2283#ifdef HAVE_LIBSSH2_KNOWNHOST_API
2284  case CURLOPT_SSH_KNOWNHOSTS:
2285    /*
2286     * Store the file name to read known hosts from.
2287     */
2288    result = setstropt(&data->set.str[STRING_SSH_KNOWNHOSTS],
2289                       va_arg(param, char *));
2290    break;
2291
2292  case CURLOPT_SSH_KEYFUNCTION:
2293    /* setting to NULL is fine since the ssh.c functions themselves will
2294       then rever to use the internal default */
2295    data->set.ssh_keyfunc = va_arg(param, curl_sshkeycallback);
2296    break;
2297
2298  case CURLOPT_SSH_KEYDATA:
2299    /*
2300     * Custom client data to pass to the SSH keyfunc callback
2301     */
2302    data->set.ssh_keyfunc_userp = va_arg(param, void *);
2303    break;
2304#endif /* HAVE_LIBSSH2_KNOWNHOST_API */
2305
2306#endif /* USE_LIBSSH2 */
2307
2308  case CURLOPT_HTTP_TRANSFER_DECODING:
2309    /*
2310     * disable libcurl transfer encoding is used
2311     */
2312    data->set.http_te_skip = (bool)(0 == va_arg(param, long));
2313    break;
2314
2315  case CURLOPT_HTTP_CONTENT_DECODING:
2316    /*
2317     * raw data passed to the application when content encoding is used
2318     */
2319    data->set.http_ce_skip = (bool)(0 == va_arg(param, long));
2320    break;
2321
2322  case CURLOPT_NEW_FILE_PERMS:
2323    /*
2324     * Uses these permissions instead of 0644
2325     */
2326    data->set.new_file_perms = va_arg(param, long);
2327    break;
2328
2329  case CURLOPT_NEW_DIRECTORY_PERMS:
2330    /*
2331     * Uses these permissions instead of 0755
2332     */
2333    data->set.new_directory_perms = va_arg(param, long);
2334    break;
2335
2336  case CURLOPT_ADDRESS_SCOPE:
2337    /*
2338     * We always get longs when passed plain numericals, but for this value we
2339     * know that an unsigned int will always hold the value so we blindly
2340     * typecast to this type
2341     */
2342    data->set.scope = curlx_sltoui(va_arg(param, long));
2343    break;
2344
2345  case CURLOPT_PROTOCOLS:
2346    /* set the bitmask for the protocols that are allowed to be used for the
2347       transfer, which thus helps the app which takes URLs from users or other
2348       external inputs and want to restrict what protocol(s) to deal
2349       with. Defaults to CURLPROTO_ALL. */
2350    data->set.allowed_protocols = va_arg(param, long);
2351    break;
2352
2353  case CURLOPT_REDIR_PROTOCOLS:
2354    /* set the bitmask for the protocols that libcurl is allowed to follow to,
2355       as a subset of the CURLOPT_PROTOCOLS ones. That means the protocol needs
2356       to be set in both bitmasks to be allowed to get redirected to. Defaults
2357       to all protocols except FILE and SCP. */
2358    data->set.redir_protocols = va_arg(param, long);
2359    break;
2360
2361  case CURLOPT_MAIL_FROM:
2362    result = setstropt(&data->set.str[STRING_MAIL_FROM],
2363                       va_arg(param, char *));
2364    break;
2365
2366  case CURLOPT_MAIL_RCPT:
2367    /* get a list of mail recipients */
2368    data->set.mail_rcpt = va_arg(param, struct curl_slist *);
2369    break;
2370
2371  case CURLOPT_RTSP_REQUEST:
2372    {
2373      /*
2374       * Set the RTSP request method (OPTIONS, SETUP, PLAY, etc...)
2375       * Would this be better if the RTSPREQ_* were just moved into here?
2376       */
2377      long curl_rtspreq = va_arg(param, long);
2378      Curl_RtspReq rtspreq = RTSPREQ_NONE;
2379      switch(curl_rtspreq) {
2380        case CURL_RTSPREQ_OPTIONS:
2381          rtspreq = RTSPREQ_OPTIONS;
2382          break;
2383
2384        case CURL_RTSPREQ_DESCRIBE:
2385          rtspreq = RTSPREQ_DESCRIBE;
2386          break;
2387
2388        case CURL_RTSPREQ_ANNOUNCE:
2389          rtspreq = RTSPREQ_ANNOUNCE;
2390          break;
2391
2392        case CURL_RTSPREQ_SETUP:
2393          rtspreq = RTSPREQ_SETUP;
2394          break;
2395
2396        case CURL_RTSPREQ_PLAY:
2397          rtspreq = RTSPREQ_PLAY;
2398          break;
2399
2400        case CURL_RTSPREQ_PAUSE:
2401          rtspreq = RTSPREQ_PAUSE;
2402          break;
2403
2404        case CURL_RTSPREQ_TEARDOWN:
2405          rtspreq = RTSPREQ_TEARDOWN;
2406          break;
2407
2408        case CURL_RTSPREQ_GET_PARAMETER:
2409          rtspreq = RTSPREQ_GET_PARAMETER;
2410          break;
2411
2412        case CURL_RTSPREQ_SET_PARAMETER:
2413          rtspreq = RTSPREQ_SET_PARAMETER;
2414          break;
2415
2416        case CURL_RTSPREQ_RECORD:
2417          rtspreq = RTSPREQ_RECORD;
2418          break;
2419
2420        case CURL_RTSPREQ_RECEIVE:
2421          rtspreq = RTSPREQ_RECEIVE;
2422          break;
2423        default:
2424          rtspreq = RTSPREQ_NONE;
2425      }
2426
2427      data->set.rtspreq = rtspreq;
2428    break;
2429    }
2430
2431
2432  case CURLOPT_RTSP_SESSION_ID:
2433    /*
2434     * Set the RTSP Session ID manually. Useful if the application is
2435     * resuming a previously established RTSP session
2436     */
2437    result = setstropt(&data->set.str[STRING_RTSP_SESSION_ID],
2438                       va_arg(param, char *));
2439    break;
2440
2441  case CURLOPT_RTSP_STREAM_URI:
2442    /*
2443     * Set the Stream URI for the RTSP request. Unless the request is
2444     * for generic server options, the application will need to set this.
2445     */
2446    result = setstropt(&data->set.str[STRING_RTSP_STREAM_URI],
2447                       va_arg(param, char *));
2448    break;
2449
2450  case CURLOPT_RTSP_TRANSPORT:
2451    /*
2452     * The content of the Transport: header for the RTSP request
2453     */
2454    result = setstropt(&data->set.str[STRING_RTSP_TRANSPORT],
2455                       va_arg(param, char *));
2456    break;
2457
2458  case CURLOPT_RTSP_CLIENT_CSEQ:
2459    /*
2460     * Set the CSEQ number to issue for the next RTSP request. Useful if the
2461     * application is resuming a previously broken connection. The CSEQ
2462     * will increment from this new number henceforth.
2463     */
2464    data->state.rtsp_next_client_CSeq = va_arg(param, long);
2465    break;
2466
2467  case CURLOPT_RTSP_SERVER_CSEQ:
2468    /* Same as the above, but for server-initiated requests */
2469    data->state.rtsp_next_client_CSeq = va_arg(param, long);
2470    break;
2471
2472  case CURLOPT_INTERLEAVEDATA:
2473    data->set.rtp_out = va_arg(param, void *);
2474    break;
2475  case CURLOPT_INTERLEAVEFUNCTION:
2476    /* Set the user defined RTP write function */
2477    data->set.fwrite_rtp = va_arg(param, curl_write_callback);
2478    break;
2479
2480  case CURLOPT_WILDCARDMATCH:
2481    data->set.wildcardmatch = (bool)(0 != va_arg(param, long));
2482    break;
2483  case CURLOPT_CHUNK_BGN_FUNCTION:
2484    data->set.chunk_bgn = va_arg(param, curl_chunk_bgn_callback);
2485    break;
2486  case CURLOPT_CHUNK_END_FUNCTION:
2487    data->set.chunk_end = va_arg(param, curl_chunk_end_callback);
2488    break;
2489  case CURLOPT_FNMATCH_FUNCTION:
2490    data->set.fnmatch = va_arg(param, curl_fnmatch_callback);
2491    break;
2492  case CURLOPT_CHUNK_DATA:
2493    data->wildcard.customptr = va_arg(param, void *);
2494    break;
2495  case CURLOPT_FNMATCH_DATA:
2496    data->set.fnmatch_data = va_arg(param, void *);
2497    break;
2498#ifdef USE_TLS_SRP
2499  case CURLOPT_TLSAUTH_USERNAME:
2500    result = setstropt(&data->set.str[STRING_TLSAUTH_USERNAME],
2501                       va_arg(param, char *));
2502    if(data->set.str[STRING_TLSAUTH_USERNAME] && !data->set.ssl.authtype)
2503      data->set.ssl.authtype = CURL_TLSAUTH_SRP; /* default to SRP */
2504    break;
2505  case CURLOPT_TLSAUTH_PASSWORD:
2506    result = setstropt(&data->set.str[STRING_TLSAUTH_PASSWORD],
2507                       va_arg(param, char *));
2508    if(data->set.str[STRING_TLSAUTH_USERNAME] && !data->set.ssl.authtype)
2509      data->set.ssl.authtype = CURL_TLSAUTH_SRP; /* default to SRP */
2510    break;
2511  case CURLOPT_TLSAUTH_TYPE:
2512    if(strncmp((char *)va_arg(param, char *), "SRP", strlen("SRP")) == 0)
2513      data->set.ssl.authtype = CURL_TLSAUTH_SRP;
2514    else
2515      data->set.ssl.authtype = CURL_TLSAUTH_NONE;
2516    break;
2517#endif
2518  default:
2519    /* unknown tag and its companion, just ignore: */
2520    result = CURLE_UNKNOWN_OPTION;
2521    break;
2522  }
2523
2524  return result;
2525}
2526
2527static void conn_free(struct connectdata *conn)
2528{
2529  if(!conn)
2530    return;
2531
2532  /* possible left-overs from the async name resolvers */
2533  Curl_resolver_cancel(conn);
2534
2535  /* close the SSL stuff before we close any sockets since they will/may
2536     write to the sockets */
2537  Curl_ssl_close(conn, FIRSTSOCKET);
2538  Curl_ssl_close(conn, SECONDARYSOCKET);
2539
2540  /* close possibly still open sockets */
2541  if(CURL_SOCKET_BAD != conn->sock[SECONDARYSOCKET])
2542    Curl_closesocket(conn, conn->sock[SECONDARYSOCKET]);
2543  if(CURL_SOCKET_BAD != conn->sock[FIRSTSOCKET])
2544    Curl_closesocket(conn, conn->sock[FIRSTSOCKET]);
2545
2546  Curl_safefree(conn->user);
2547  Curl_safefree(conn->passwd);
2548  Curl_safefree(conn->proxyuser);
2549  Curl_safefree(conn->proxypasswd);
2550  Curl_safefree(conn->allocptr.proxyuserpwd);
2551  Curl_safefree(conn->allocptr.uagent);
2552  Curl_safefree(conn->allocptr.userpwd);
2553  Curl_safefree(conn->allocptr.accept_encoding);
2554  Curl_safefree(conn->allocptr.te);
2555  Curl_safefree(conn->allocptr.rangeline);
2556  Curl_safefree(conn->allocptr.ref);
2557  Curl_safefree(conn->allocptr.host);
2558  Curl_safefree(conn->allocptr.cookiehost);
2559  Curl_safefree(conn->allocptr.rtsp_transport);
2560  Curl_safefree(conn->trailer);
2561  Curl_safefree(conn->host.rawalloc); /* host name buffer */
2562  Curl_safefree(conn->proxy.rawalloc); /* proxy name buffer */
2563  Curl_safefree(conn->master_buffer);
2564
2565  Curl_llist_destroy(conn->send_pipe, NULL);
2566  Curl_llist_destroy(conn->recv_pipe, NULL);
2567  Curl_llist_destroy(conn->pend_pipe, NULL);
2568  Curl_llist_destroy(conn->done_pipe, NULL);
2569
2570  Curl_safefree(conn->localdev);
2571  Curl_free_ssl_config(&conn->ssl_config);
2572
2573  free(conn); /* free all the connection oriented data */
2574}
2575
2576CURLcode Curl_disconnect(struct connectdata *conn, bool dead_connection)
2577{
2578  struct SessionHandle *data;
2579  if(!conn)
2580    return CURLE_OK; /* this is closed and fine already */
2581  data = conn->data;
2582
2583  if(!data) {
2584    DEBUGF(infof(data, "DISCONNECT without easy handle, ignoring\n"));
2585    return CURLE_OK;
2586  }
2587
2588  if(conn->dns_entry != NULL) {
2589    Curl_resolv_unlock(data, conn->dns_entry);
2590    conn->dns_entry = NULL;
2591  }
2592
2593#if defined(DEBUGBUILD) && defined(AGGRESIVE_TEST)
2594  /* scan for DNS cache entries still marked as in use */
2595  Curl_hash_apply(data->hostcache,
2596                  NULL, Curl_scan_cache_used);
2597#endif
2598
2599  Curl_hostcache_prune(data); /* kill old DNS cache entries */
2600
2601  {
2602    int has_host_ntlm = (conn->ntlm.state != NTLMSTATE_NONE);
2603    int has_proxy_ntlm = (conn->proxyntlm.state != NTLMSTATE_NONE);
2604
2605    /* Authentication data is a mix of connection-related and sessionhandle-
2606       related stuff. NTLM is connection-related so when we close the shop
2607       we shall forget. */
2608
2609    if(has_host_ntlm) {
2610      data->state.authhost.done = FALSE;
2611      data->state.authhost.picked =
2612        data->state.authhost.want;
2613    }
2614
2615    if(has_proxy_ntlm) {
2616      data->state.authproxy.done = FALSE;
2617      data->state.authproxy.picked =
2618        data->state.authproxy.want;
2619    }
2620
2621    if(has_host_ntlm || has_proxy_ntlm) {
2622      data->state.authproblem = FALSE;
2623
2624      Curl_ntlm_cleanup(conn);
2625    }
2626  }
2627
2628  /* Cleanup possible redirect junk */
2629  if(data->req.newurl) {
2630    free(data->req.newurl);
2631    data->req.newurl = NULL;
2632  }
2633
2634  if(conn->handler->disconnect)
2635    /* This is set if protocol-specific cleanups should be made */
2636    conn->handler->disconnect(conn, dead_connection);
2637
2638  if(-1 != conn->connectindex) {
2639    /* unlink ourselves! */
2640    infof(data, "Closing connection #%ld\n", conn->connectindex);
2641    if(data->state.connc)
2642      /* only clear the table entry if we still know in which cache we
2643         used to be in */
2644      data->state.connc->connects[conn->connectindex] = NULL;
2645  }
2646
2647#if defined(USE_LIBIDN)
2648  if(conn->host.encalloc)
2649    idn_free(conn->host.encalloc); /* encoded host name buffer, must be freed
2650                                      with idn_free() since this was allocated
2651                                      by libidn */
2652  if(conn->proxy.encalloc)
2653    idn_free(conn->proxy.encalloc); /* encoded proxy name buffer, must be
2654                                       freed with idn_free() since this was
2655                                       allocated by libidn */
2656#elif defined(USE_WIN32_IDN)
2657  free(conn->host.encalloc); /* encoded host name buffer, must be freed with
2658                                idn_free() since this was allocated by
2659                                curl_win32_idn_to_ascii */
2660  if(conn->proxy.encalloc)
2661    free(conn->proxy.encalloc); /* encoded proxy name buffer, must be freed
2662                                   with idn_free() since this was allocated by
2663                                   curl_win32_idn_to_ascii */
2664#endif
2665
2666  Curl_ssl_close(conn, FIRSTSOCKET);
2667
2668  /* Indicate to all handles on the pipe that we're dead */
2669  if(Curl_isPipeliningEnabled(data)) {
2670    signalPipeClose(conn->send_pipe, TRUE);
2671    signalPipeClose(conn->recv_pipe, TRUE);
2672    signalPipeClose(conn->pend_pipe, TRUE);
2673    signalPipeClose(conn->done_pipe, FALSE);
2674  }
2675
2676  conn_free(conn);
2677  data->state.current_conn = NULL;
2678  Curl_speedinit(data);
2679
2680  return CURLE_OK;
2681}
2682
2683/*
2684 * This function should return TRUE if the socket is to be assumed to
2685 * be dead. Most commonly this happens when the server has closed the
2686 * connection due to inactivity.
2687 */
2688static bool SocketIsDead(curl_socket_t sock)
2689{
2690  int sval;
2691  bool ret_val = TRUE;
2692
2693  sval = Curl_socket_ready(sock, CURL_SOCKET_BAD, 0);
2694  if(sval == 0)
2695    /* timeout */
2696    ret_val = FALSE;
2697
2698  return ret_val;
2699}
2700
2701static bool IsPipeliningPossible(const struct SessionHandle *handle,
2702                                 const struct connectdata *conn)
2703{
2704  if((conn->handler->protocol & CURLPROTO_HTTP) &&
2705     handle->multi && Curl_multi_canPipeline(handle->multi) &&
2706     (handle->set.httpreq == HTTPREQ_GET ||
2707      handle->set.httpreq == HTTPREQ_HEAD) &&
2708     handle->set.httpversion != CURL_HTTP_VERSION_1_0)
2709    return TRUE;
2710
2711  return FALSE;
2712}
2713
2714bool Curl_isPipeliningEnabled(const struct SessionHandle *handle)
2715{
2716  if(handle->multi && Curl_multi_canPipeline(handle->multi))
2717    return TRUE;
2718
2719  return FALSE;
2720}
2721
2722CURLcode Curl_addHandleToPipeline(struct SessionHandle *data,
2723                                  struct curl_llist *pipeline)
2724{
2725  if(!Curl_llist_insert_next(pipeline, pipeline->tail, data))
2726    return CURLE_OUT_OF_MEMORY;
2727  return CURLE_OK;
2728}
2729
2730int Curl_removeHandleFromPipeline(struct SessionHandle *handle,
2731                                  struct curl_llist *pipeline)
2732{
2733  struct curl_llist_element *curr;
2734
2735  curr = pipeline->head;
2736  while(curr) {
2737    if(curr->ptr == handle) {
2738      Curl_llist_remove(pipeline, curr, NULL);
2739      return 1; /* we removed a handle */
2740    }
2741    curr = curr->next;
2742  }
2743
2744  return 0;
2745}
2746
2747#if 0 /* this code is saved here as it is useful for debugging purposes */
2748static void Curl_printPipeline(struct curl_llist *pipeline)
2749{
2750  struct curl_llist_element *curr;
2751
2752  curr = pipeline->head;
2753  while(curr) {
2754    struct SessionHandle *data = (struct SessionHandle *) curr->ptr;
2755    infof(data, "Handle in pipeline: %s\n", data->state.path);
2756    curr = curr->next;
2757  }
2758}
2759#endif
2760
2761static struct SessionHandle* gethandleathead(struct curl_llist *pipeline)
2762{
2763  struct curl_llist_element *curr = pipeline->head;
2764  if(curr) {
2765    return (struct SessionHandle *) curr->ptr;
2766  }
2767
2768  return NULL;
2769}
2770
2771/* remove the specified connection from all (possible) pipelines and related
2772   queues */
2773void Curl_getoff_all_pipelines(struct SessionHandle *data,
2774                               struct connectdata *conn)
2775{
2776  bool recv_head = (bool)(conn->readchannel_inuse &&
2777    (gethandleathead(conn->recv_pipe) == data));
2778
2779  bool send_head = (bool)(conn->writechannel_inuse &&
2780    (gethandleathead(conn->send_pipe) == data));
2781
2782  if(Curl_removeHandleFromPipeline(data, conn->recv_pipe) && recv_head)
2783    conn->readchannel_inuse = FALSE;
2784  if(Curl_removeHandleFromPipeline(data, conn->send_pipe) && send_head)
2785    conn->writechannel_inuse = FALSE;
2786  Curl_removeHandleFromPipeline(data, conn->pend_pipe);
2787  Curl_removeHandleFromPipeline(data, conn->done_pipe);
2788}
2789
2790static void signalPipeClose(struct curl_llist *pipeline, bool pipe_broke)
2791{
2792  struct curl_llist_element *curr;
2793
2794  if(!pipeline)
2795    return;
2796
2797  curr = pipeline->head;
2798  while(curr) {
2799    struct curl_llist_element *next = curr->next;
2800    struct SessionHandle *data = (struct SessionHandle *) curr->ptr;
2801
2802#ifdef DEBUGBUILD /* debug-only code */
2803    if(data->magic != CURLEASY_MAGIC_NUMBER) {
2804      /* MAJOR BADNESS */
2805      infof(data, "signalPipeClose() found BAAD easy handle\n");
2806    }
2807#endif
2808
2809    if(pipe_broke)
2810      data->state.pipe_broke = TRUE;
2811    Curl_multi_handlePipeBreak(data);
2812    Curl_llist_remove(pipeline, curr, NULL);
2813    curr = next;
2814  }
2815}
2816
2817
2818/*
2819 * Given one filled in connection struct (named needle), this function should
2820 * detect if there already is one that has all the significant details
2821 * exactly the same and thus should be used instead.
2822 *
2823 * If there is a match, this function returns TRUE - and has marked the
2824 * connection as 'in-use'. It must later be called with ConnectionDone() to
2825 * return back to 'idle' (unused) state.
2826 */
2827static bool
2828ConnectionExists(struct SessionHandle *data,
2829                 struct connectdata *needle,
2830                 struct connectdata **usethis)
2831{
2832  long i;
2833  struct connectdata *check;
2834  bool canPipeline = IsPipeliningPossible(data, needle);
2835
2836  for(i=0; i< data->state.connc->num; i++) {
2837    bool match = FALSE;
2838    size_t pipeLen = 0;
2839    /*
2840     * Note that if we use a HTTP proxy, we check connections to that
2841     * proxy and not to the actual remote server.
2842     */
2843    check = data->state.connc->connects[i];
2844    if(!check)
2845      /* NULL pointer means not filled-in entry */
2846      continue;
2847
2848    pipeLen = check->send_pipe->size + check->recv_pipe->size;
2849
2850    if(check->connectindex == -1) {
2851      check->connectindex = i; /* Set this appropriately since it might have
2852                                  been set to -1 when the easy was removed
2853                                  from the multi */
2854    }
2855
2856    if(!pipeLen && !check->inuse) {
2857      /* The check for a dead socket makes sense only if there are no
2858         handles in pipeline and the connection isn't already marked in
2859         use */
2860      bool dead;
2861      if(check->handler->protocol & CURLPROTO_RTSP)
2862        /* RTSP is a special case due to RTP interleaving */
2863        dead = Curl_rtsp_connisdead(check);
2864      else
2865        dead = SocketIsDead(check->sock[FIRSTSOCKET]);
2866
2867      if(dead) {
2868        check->data = data;
2869        infof(data, "Connection #%ld seems to be dead!\n", i);
2870
2871        /* disconnect resources */
2872        Curl_disconnect(check, /* dead_connection */ TRUE);
2873        data->state.connc->connects[i]=NULL; /* nothing here */
2874
2875        continue;
2876      }
2877    }
2878
2879    if(canPipeline) {
2880      /* Make sure the pipe has only GET requests */
2881      struct SessionHandle* sh = gethandleathead(check->send_pipe);
2882      struct SessionHandle* rh = gethandleathead(check->recv_pipe);
2883      if(sh) {
2884        if(!IsPipeliningPossible(sh, check))
2885          continue;
2886      }
2887      else if(rh) {
2888        if(!IsPipeliningPossible(rh, check))
2889          continue;
2890      }
2891
2892#ifdef DEBUGBUILD
2893      if(pipeLen > MAX_PIPELINE_LENGTH) {
2894        infof(data, "BAD! Connection #%ld has too big pipeline!\n",
2895              check->connectindex);
2896      }
2897#endif
2898    }
2899    else {
2900      if(pipeLen > 0) {
2901        /* can only happen within multi handles, and means that another easy
2902           handle is using this connection */
2903        continue;
2904      }
2905
2906      if(Curl_resolver_asynch()) {
2907        /* ip_addr_str[0] is NUL only if the resolving of the name hasn't
2908           completed yet and until then we don't re-use this connection */
2909        if(!check->ip_addr_str[0]) {
2910          infof(data,
2911                "Connection #%ld hasn't finished name resolve, can't reuse\n",
2912                check->connectindex);
2913          continue;
2914        }
2915      }
2916
2917      if((check->sock[FIRSTSOCKET] == CURL_SOCKET_BAD) || check->bits.close) {
2918        /* Don't pick a connection that hasn't connected yet or that is going
2919           to get closed. */
2920        infof(data, "Connection #%ld isn't open enough, can't reuse\n",
2921              check->connectindex);
2922#ifdef DEBUGBUILD
2923        if(check->recv_pipe->size > 0) {
2924          infof(data, "BAD! Unconnected #%ld has a non-empty recv pipeline!\n",
2925                check->connectindex);
2926        }
2927#endif
2928        continue;
2929      }
2930    }
2931
2932    if((needle->handler->flags&PROTOPT_SSL) !=
2933       (check->handler->flags&PROTOPT_SSL))
2934      /* don't do mixed SSL and non-SSL connections */
2935      continue;
2936
2937    if(needle->handler->flags&PROTOPT_SSL) {
2938      if((data->set.ssl.verifypeer != check->verifypeer) ||
2939         (data->set.ssl.verifyhost != check->verifyhost))
2940        continue;
2941    }
2942
2943    if(needle->bits.proxy != check->bits.proxy)
2944      /* don't do mixed proxy and non-proxy connections */
2945      continue;
2946
2947    if(!canPipeline && check->inuse)
2948      /* this request can't be pipelined but the checked connection is already
2949         in use so we skip it */
2950      continue;
2951
2952    if(needle->localdev || needle->localport) {
2953      /* If we are bound to a specific local end (IP+port), we must not re-use
2954         a random other one, although if we didn't ask for a particular one we
2955         can reuse one that was bound.
2956
2957         This comparison is a bit rough and too strict. Since the input
2958         parameters can be specified in numerous ways and still end up the
2959         same it would take a lot of processing to make it really accurate.
2960         Instead, this matching will assume that re-uses of bound connections
2961         will most likely also re-use the exact same binding parameters and
2962         missing out a few edge cases shouldn't hurt anyone very much.
2963      */
2964      if((check->localport != needle->localport) ||
2965         (check->localportrange != needle->localportrange) ||
2966         !check->localdev ||
2967         !needle->localdev ||
2968         strcmp(check->localdev, needle->localdev))
2969        continue;
2970    }
2971
2972    if(!needle->bits.httpproxy || needle->handler->flags&PROTOPT_SSL ||
2973       (needle->bits.httpproxy && check->bits.httpproxy &&
2974        needle->bits.tunnel_proxy && check->bits.tunnel_proxy &&
2975        Curl_raw_equal(needle->proxy.name, check->proxy.name) &&
2976        (needle->port == check->port))) {
2977      /* The requested connection does not use a HTTP proxy or it uses SSL or
2978         it is a non-SSL protocol tunneled over the same http proxy name and
2979         port number */
2980
2981      if(Curl_raw_equal(needle->handler->scheme, check->handler->scheme) &&
2982         Curl_raw_equal(needle->host.name, check->host.name) &&
2983         (needle->remote_port == check->remote_port) ) {
2984        if(needle->handler->flags & PROTOPT_SSL) {
2985          /* This is SSL, verify that we're using the same
2986             ssl options as well */
2987          if(!Curl_ssl_config_matches(&needle->ssl_config,
2988                                      &check->ssl_config)) {
2989            DEBUGF(infof(data,
2990                         "Connection #%ld has different SSL parameters, "
2991                         "can't reuse\n",
2992                         check->connectindex));
2993            continue;
2994          }
2995          else if(check->ssl[FIRSTSOCKET].state != ssl_connection_complete) {
2996            DEBUGF(infof(data,
2997                         "Connection #%ld has not started ssl connect, "
2998                         "can't reuse\n",
2999                         check->connectindex));
3000            continue;
3001          }
3002        }
3003        if((needle->handler->protocol & CURLPROTO_FTP) ||
3004           ((needle->handler->protocol & CURLPROTO_HTTP) &&
3005            (data->state.authhost.want==CURLAUTH_NTLM))) {
3006          /* This is FTP or HTTP+NTLM, verify that we're using the same name
3007             and password as well */
3008          if(!strequal(needle->user, check->user) ||
3009             !strequal(needle->passwd, check->passwd)) {
3010            /* one of them was different */
3011            continue;
3012          }
3013        }
3014        match = TRUE;
3015      }
3016    }
3017    else { /* The requested needle connection is using a proxy,
3018              is the checked one using the same host, port and type? */
3019      if(check->bits.proxy &&
3020         (needle->proxytype == check->proxytype) &&
3021         (needle->bits.tunnel_proxy == check->bits.tunnel_proxy) &&
3022         Curl_raw_equal(needle->proxy.name, check->proxy.name) &&
3023         needle->port == check->port) {
3024        /* This is the same proxy connection, use it! */
3025        match = TRUE;
3026      }
3027    }
3028
3029    if(match) {
3030      check->inuse = TRUE; /* mark this as being in use so that no other
3031                              handle in a multi stack may nick it */
3032
3033      *usethis = check;
3034      return TRUE; /* yes, we found one to use! */
3035    }
3036  }
3037
3038  return FALSE; /* no matching connecting exists */
3039}
3040
3041
3042
3043/*
3044 * This function kills and removes an existing connection in the connection
3045 * cache. The connection that has been unused for the longest time.
3046 *
3047 * Returns -1 if it can't find any unused connection to kill.
3048 */
3049static long
3050ConnectionKillOne(struct SessionHandle *data)
3051{
3052  long i;
3053  struct connectdata *conn;
3054  long highscore=-1;
3055  long connindex=-1;
3056  long score;
3057  struct timeval now;
3058
3059  now = Curl_tvnow();
3060
3061  for(i=0; data->state.connc && (i< data->state.connc->num); i++) {
3062    conn = data->state.connc->connects[i];
3063
3064    if(!conn || conn->inuse)
3065      continue;
3066
3067    /* Set higher score for the age passed since the connection was used */
3068    score = Curl_tvdiff(now, conn->now);
3069
3070    if(score > highscore) {
3071      highscore = score;
3072      connindex = i;
3073    }
3074  }
3075  if(connindex >= 0) {
3076    /* Set the connection's owner correctly */
3077    conn = data->state.connc->connects[connindex];
3078    conn->data = data;
3079
3080    /* the winner gets the honour of being disconnected */
3081    (void)Curl_disconnect(conn, /* dead_connection */ FALSE);
3082
3083    /* clean the array entry */
3084    data->state.connc->connects[connindex] = NULL;
3085  }
3086
3087  return connindex; /* return the available index or -1 */
3088}
3089
3090/* this connection can now be marked 'idle' */
3091static void
3092ConnectionDone(struct connectdata *conn)
3093{
3094  conn->inuse = FALSE;
3095}
3096
3097/*
3098 * The given input connection struct pointer is to be stored in the connection
3099 * cache. If the cache is already full, least interesting existing connection
3100 * (if any) gets closed.
3101 *
3102 * The given connection should be unique. That must've been checked prior to
3103 * this call.
3104 */
3105static void ConnectionStore(struct SessionHandle *data,
3106                            struct connectdata *conn)
3107{
3108  long i;
3109  for(i=0; i< data->state.connc->num; i++) {
3110    if(!data->state.connc->connects[i])
3111      break;
3112  }
3113  if(i == data->state.connc->num) {
3114    /* there was no room available, kill one */
3115    i = ConnectionKillOne(data);
3116    if(-1 != i)
3117      infof(data, "Connection (#%ld) was killed to make room (holds %ld)\n",
3118            i, data->state.connc->num);
3119    else
3120      infof(data, "This connection did not fit in the connection cache\n");
3121  }
3122
3123  conn->connectindex = i; /* Make the child know where the pointer to this
3124                             particular data is stored. But note that this -1
3125                             if this is not within the cache and this is
3126                             probably not checked for everywhere (yet). */
3127  conn->inuse = TRUE;
3128  if(-1 != i) {
3129    /* Only do this if a true index was returned, if -1 was returned there
3130       is no room in the cache for an unknown reason and we cannot store
3131       this there.
3132
3133       TODO: make sure we really can work with more handles than positions in
3134       the cache, or possibly we should (allow to automatically) resize the
3135       connection cache when we add more easy handles to a multi handle!
3136    */
3137    data->state.connc->connects[i] = conn; /* fill in this */
3138    conn->data = data;
3139  }
3140}
3141
3142/* after a TCP connection to the proxy has been verified, this function does
3143   the next magic step.
3144
3145   Note: this function's sub-functions call failf()
3146
3147*/
3148CURLcode Curl_connected_proxy(struct connectdata *conn)
3149{
3150  switch(conn->proxytype) {
3151#ifndef CURL_DISABLE_PROXY
3152  case CURLPROXY_SOCKS5:
3153  case CURLPROXY_SOCKS5_HOSTNAME:
3154    return Curl_SOCKS5(conn->proxyuser, conn->proxypasswd,
3155                       conn->host.name, conn->remote_port,
3156                       FIRSTSOCKET, conn);
3157
3158  case CURLPROXY_SOCKS4:
3159    return Curl_SOCKS4(conn->proxyuser, conn->host.name,
3160                       conn->remote_port, FIRSTSOCKET, conn, FALSE);
3161
3162  case CURLPROXY_SOCKS4A:
3163    return Curl_SOCKS4(conn->proxyuser, conn->host.name,
3164                       conn->remote_port, FIRSTSOCKET, conn, TRUE);
3165
3166#endif /* CURL_DISABLE_PROXY */
3167  case CURLPROXY_HTTP:
3168  case CURLPROXY_HTTP_1_0:
3169    /* do nothing here. handled later. */
3170    break;
3171  default:
3172    break;
3173  } /* switch proxytype */
3174
3175  return CURLE_OK;
3176}
3177
3178static CURLcode ConnectPlease(struct SessionHandle *data,
3179                              struct connectdata *conn,
3180                              bool *connected)
3181{
3182  CURLcode result;
3183  Curl_addrinfo *addr;
3184#ifndef CURL_DISABLE_VERBOSE_STRINGS
3185  char *hostname = conn->bits.proxy?conn->proxy.name:conn->host.name;
3186
3187  infof(data, "About to connect() to %s%s port %ld (#%ld)\n",
3188        conn->bits.proxy?"proxy ":"",
3189        hostname, conn->port, conn->connectindex);
3190#else
3191  (void)data;
3192#endif
3193
3194  /*************************************************************
3195   * Connect to server/proxy
3196   *************************************************************/
3197  result= Curl_connecthost(conn,
3198                           conn->dns_entry,
3199                           &conn->sock[FIRSTSOCKET],
3200                           &addr,
3201                           connected);
3202  if(CURLE_OK == result) {
3203    /* All is cool, we store the current information */
3204    conn->ip_addr = addr;
3205
3206    if(*connected)
3207      result = Curl_connected_proxy(conn);
3208  }
3209
3210  if(result)
3211    *connected = FALSE; /* mark it as not connected */
3212
3213  return result;
3214}
3215
3216/*
3217 * verboseconnect() displays verbose information after a connect
3218 */
3219#ifndef CURL_DISABLE_VERBOSE_STRINGS
3220void Curl_verboseconnect(struct connectdata *conn)
3221{
3222  if(conn->data->set.verbose)
3223    infof(conn->data, "Connected to %s (%s) port %ld (#%ld)\n",
3224          conn->bits.proxy ? conn->proxy.dispname : conn->host.dispname,
3225          conn->ip_addr_str, conn->port, conn->connectindex);
3226}
3227#endif
3228
3229int Curl_protocol_getsock(struct connectdata *conn,
3230                          curl_socket_t *socks,
3231                          int numsocks)
3232{
3233  if(conn->handler->proto_getsock)
3234    return conn->handler->proto_getsock(conn, socks, numsocks);
3235  return GETSOCK_BLANK;
3236}
3237
3238int Curl_doing_getsock(struct connectdata *conn,
3239                       curl_socket_t *socks,
3240                       int numsocks)
3241{
3242  if(conn && conn->handler->doing_getsock)
3243    return conn->handler->doing_getsock(conn, socks, numsocks);
3244  return GETSOCK_BLANK;
3245}
3246
3247/*
3248 * We are doing protocol-specific connecting and this is being called over and
3249 * over from the multi interface until the connection phase is done on
3250 * protocol layer.
3251 */
3252
3253CURLcode Curl_protocol_connecting(struct connectdata *conn,
3254                                  bool *done)
3255{
3256  CURLcode result=CURLE_OK;
3257
3258  if(conn && conn->handler->connecting) {
3259    *done = FALSE;
3260    result = conn->handler->connecting(conn, done);
3261  }
3262  else
3263    *done = TRUE;
3264
3265  return result;
3266}
3267
3268/*
3269 * We are DOING this is being called over and over from the multi interface
3270 * until the DOING phase is done on protocol layer.
3271 */
3272
3273CURLcode Curl_protocol_doing(struct connectdata *conn, bool *done)
3274{
3275  CURLcode result=CURLE_OK;
3276
3277  if(conn && conn->handler->doing) {
3278    *done = FALSE;
3279    result = conn->handler->doing(conn, done);
3280  }
3281  else
3282    *done = TRUE;
3283
3284  return result;
3285}
3286
3287/*
3288 * We have discovered that the TCP connection has been successful, we can now
3289 * proceed with some action.
3290 *
3291 */
3292CURLcode Curl_protocol_connect(struct connectdata *conn,
3293                               bool *protocol_done)
3294{
3295  CURLcode result=CURLE_OK;
3296  struct SessionHandle *data = conn->data;
3297
3298  *protocol_done = FALSE;
3299
3300  if(conn->bits.tcpconnect && conn->bits.protoconnstart) {
3301    /* We already are connected, get back. This may happen when the connect
3302       worked fine in the first call, like when we connect to a local server
3303       or proxy. Note that we don't know if the protocol is actually done.
3304
3305       Unless this protocol doesn't have any protocol-connect callback, as
3306       then we know we're done. */
3307    if(!conn->handler->connecting)
3308      *protocol_done = TRUE;
3309
3310    return CURLE_OK;
3311  }
3312
3313  if(!conn->bits.tcpconnect) {
3314
3315    Curl_pgrsTime(data, TIMER_CONNECT); /* connect done */
3316    Curl_verboseconnect(conn);
3317  }
3318
3319  if(!conn->bits.protoconnstart) {
3320    if(conn->handler->connect_it) {
3321      /* is there a protocol-specific connect() procedure? */
3322
3323      /* Set start time here for timeout purposes in the connect procedure, it
3324         is later set again for the progress meter purpose */
3325      conn->now = Curl_tvnow();
3326
3327      /* Call the protocol-specific connect function */
3328      result = conn->handler->connect_it(conn, protocol_done);
3329    }
3330    else
3331      *protocol_done = TRUE;
3332
3333    /* it has started, possibly even completed but that knowledge isn't stored
3334       in this bit! */
3335    if(!result)
3336      conn->bits.protoconnstart = TRUE;
3337  }
3338
3339  return result; /* pass back status */
3340}
3341
3342/*
3343 * Helpers for IDNA convertions.
3344 */
3345static bool is_ASCII_name(const char *hostname)
3346{
3347  const unsigned char *ch = (const unsigned char*)hostname;
3348
3349  while(*ch) {
3350    if(*ch++ & 0x80)
3351      return FALSE;
3352  }
3353  return TRUE;
3354}
3355
3356#ifdef USE_LIBIDN
3357/*
3358 * Check if characters in hostname is allowed in Top Level Domain.
3359 */
3360static bool tld_check_name(struct SessionHandle *data,
3361                           const char *ace_hostname)
3362{
3363  size_t err_pos;
3364  char *uc_name = NULL;
3365  int rc;
3366#ifndef CURL_DISABLE_VERBOSE_STRINGS
3367  const char *tld_errmsg = "<no msg>";
3368#else
3369  (void)data;
3370#endif
3371
3372  /* Convert (and downcase) ACE-name back into locale's character set */
3373  rc = idna_to_unicode_lzlz(ace_hostname, &uc_name, 0);
3374  if(rc != IDNA_SUCCESS)
3375    return FALSE;
3376
3377  rc = tld_check_lz(uc_name, &err_pos, NULL);
3378#ifndef CURL_DISABLE_VERBOSE_STRINGS
3379#ifdef HAVE_TLD_STRERROR
3380  if(rc != TLD_SUCCESS)
3381    tld_errmsg = tld_strerror((Tld_rc)rc);
3382#endif
3383  if(rc == TLD_INVALID)
3384    infof(data, "WARNING: %s; pos %u = `%c'/0x%02X\n",
3385          tld_errmsg, err_pos, uc_name[err_pos],
3386          uc_name[err_pos] & 255);
3387  else if(rc != TLD_SUCCESS)
3388    infof(data, "WARNING: TLD check for %s failed; %s\n",
3389          uc_name, tld_errmsg);
3390#endif /* CURL_DISABLE_VERBOSE_STRINGS */
3391  if(uc_name)
3392     idn_free(uc_name);
3393  if(rc != TLD_SUCCESS)
3394    return FALSE;
3395
3396  return TRUE;
3397}
3398#endif
3399
3400/*
3401 * Perform any necessary IDN conversion of hostname
3402 */
3403static void fix_hostname(struct SessionHandle *data,
3404                         struct connectdata *conn, struct hostname *host)
3405{
3406#ifndef USE_LIBIDN
3407  (void)data;
3408  (void)conn;
3409#elif defined(CURL_DISABLE_VERBOSE_STRINGS)
3410  (void)conn;
3411#endif
3412
3413  /* set the name we use to display the host name */
3414  host->dispname = host->name;
3415  if(!is_ASCII_name(host->name)) {
3416#ifdef USE_LIBIDN
3417  /*************************************************************
3418   * Check name for non-ASCII and convert hostname to ACE form.
3419   *************************************************************/
3420  if(stringprep_check_version(LIBIDN_REQUIRED_VERSION)) {
3421    char *ace_hostname = NULL;
3422    int rc = idna_to_ascii_lz(host->name, &ace_hostname, 0);
3423    infof (data, "Input domain encoded as `%s'\n",
3424           stringprep_locale_charset ());
3425    if(rc != IDNA_SUCCESS)
3426      infof(data, "Failed to convert %s to ACE; %s\n",
3427            host->name, Curl_idn_strerror(conn,rc));
3428    else {
3429      /* tld_check_name() displays a warning if the host name contains
3430         "illegal" characters for this TLD */
3431      (void)tld_check_name(data, ace_hostname);
3432
3433      host->encalloc = ace_hostname;
3434      /* change the name pointer to point to the encoded hostname */
3435      host->name = host->encalloc;
3436    }
3437  }
3438#elif defined(USE_WIN32_IDN)
3439  /*************************************************************
3440   * Check name for non-ASCII and convert hostname to ACE form.
3441   *************************************************************/
3442    char *ace_hostname = NULL;
3443    int rc = curl_win32_idn_to_ascii(host->name, &ace_hostname);
3444    if(rc == 0)
3445      infof(data, "Failed to convert %s to ACE;\n",
3446            host->name);
3447    else {
3448      host->encalloc = ace_hostname;
3449      /* change the name pointer to point to the encoded hostname */
3450      host->name = host->encalloc;
3451    }
3452#else
3453    infof (data, "IDN support not present, can't parse Unicode domains");
3454#endif
3455  }
3456}
3457
3458static void llist_dtor(void *user, void *element)
3459{
3460  (void)user;
3461  (void)element;
3462  /* Do nothing */
3463}
3464
3465/*
3466 * Allocate and initialize a new connectdata object.
3467 */
3468static struct connectdata *allocate_conn(struct SessionHandle *data)
3469{
3470  struct connectdata *conn = calloc(1, sizeof(struct connectdata));
3471  if(!conn)
3472    return NULL;
3473
3474  conn->handler = &Curl_handler_dummy;  /* Be sure we have a handler defined
3475                                           already from start to avoid NULL
3476                                           situations and checks */
3477
3478  /* and we setup a few fields in case we end up actually using this struct */
3479
3480  conn->sock[FIRSTSOCKET] = CURL_SOCKET_BAD;     /* no file descriptor */
3481  conn->sock[SECONDARYSOCKET] = CURL_SOCKET_BAD; /* no file descriptor */
3482  conn->connectindex = -1;    /* no index */
3483  conn->port = -1; /* unknown at this point */
3484
3485  /* Default protocol-independent behavior doesn't support persistent
3486     connections, so we set this to force-close. Protocols that support
3487     this need to set this to FALSE in their "curl_do" functions. */
3488  conn->bits.close = TRUE;
3489
3490  /* Store creation time to help future close decision making */
3491  conn->created = Curl_tvnow();
3492
3493  conn->data = data; /* Setup the association between this connection
3494                        and the SessionHandle */
3495
3496  conn->proxytype = data->set.proxytype; /* type */
3497
3498#ifdef CURL_DISABLE_PROXY
3499
3500  conn->bits.proxy = FALSE;
3501  conn->bits.httpproxy = FALSE;
3502  conn->bits.proxy_user_passwd = FALSE;
3503  conn->bits.tunnel_proxy = FALSE;
3504
3505#else /* CURL_DISABLE_PROXY */
3506
3507  conn->bits.proxy = (bool)(data->set.str[STRING_PROXY] &&
3508                            *data->set.str[STRING_PROXY]);
3509  conn->bits.httpproxy = (bool)(conn->bits.proxy &&
3510                                (conn->proxytype == CURLPROXY_HTTP ||
3511                                 conn->proxytype == CURLPROXY_HTTP_1_0));
3512  conn->bits.proxy_user_passwd =
3513    (bool)(NULL != data->set.str[STRING_PROXYUSERNAME]);
3514  conn->bits.tunnel_proxy = data->set.tunnel_thru_httpproxy;
3515
3516#endif /* CURL_DISABLE_PROXY */
3517
3518  conn->bits.user_passwd = (bool)(NULL != data->set.str[STRING_USERNAME]);
3519  conn->bits.ftp_use_epsv = data->set.ftp_use_epsv;
3520  conn->bits.ftp_use_eprt = data->set.ftp_use_eprt;
3521
3522  conn->verifypeer = data->set.ssl.verifypeer;
3523  conn->verifyhost = data->set.ssl.verifyhost;
3524
3525  conn->ip_version = data->set.ipver;
3526
3527  if(data->multi && Curl_multi_canPipeline(data->multi) &&
3528      !conn->master_buffer) {
3529    /* Allocate master_buffer to be used for pipelining */
3530    conn->master_buffer = calloc(BUFSIZE, sizeof (char));
3531    if(!conn->master_buffer)
3532      goto error;
3533  }
3534
3535  /* Initialize the pipeline lists */
3536  conn->send_pipe = Curl_llist_alloc((curl_llist_dtor) llist_dtor);
3537  conn->recv_pipe = Curl_llist_alloc((curl_llist_dtor) llist_dtor);
3538  conn->pend_pipe = Curl_llist_alloc((curl_llist_dtor) llist_dtor);
3539  conn->done_pipe = Curl_llist_alloc((curl_llist_dtor) llist_dtor);
3540  if(!conn->send_pipe || !conn->recv_pipe || !conn->pend_pipe ||
3541     !conn->done_pipe)
3542    goto error;
3543
3544#if defined(HAVE_KRB4) || defined(HAVE_GSSAPI)
3545  conn->data_prot = PROT_CLEAR;
3546#endif
3547
3548  /* Store the local bind parameters that will be used for this connection */
3549  if(data->set.str[STRING_DEVICE]) {
3550    conn->localdev = strdup(data->set.str[STRING_DEVICE]);
3551    if(!conn->localdev)
3552      goto error;
3553  }
3554  conn->localportrange = data->set.localportrange;
3555  conn->localport = data->set.localport;
3556
3557  /* the close socket stuff needs to be copied to the connection struct as
3558     it may live on without (this specific) SessionHandle */
3559  conn->fclosesocket = data->set.fclosesocket;
3560  conn->closesocket_client = data->set.closesocket_client;
3561
3562  return conn;
3563  error:
3564
3565  Curl_llist_destroy(conn->send_pipe, NULL);
3566  Curl_llist_destroy(conn->recv_pipe, NULL);
3567  Curl_llist_destroy(conn->pend_pipe, NULL);
3568  Curl_llist_destroy(conn->done_pipe, NULL);
3569  Curl_safefree(conn->master_buffer);
3570  Curl_safefree(conn->localdev);
3571  Curl_safefree(conn);
3572  return NULL;
3573}
3574
3575static CURLcode findprotocol(struct SessionHandle *data,
3576                             struct connectdata *conn,
3577                             const char *protostr)
3578{
3579  const struct Curl_handler * const *pp;
3580  const struct Curl_handler *p;
3581
3582  /* Scan protocol handler table and match against 'protostr' to set a few
3583     variables based on the URL. Now that the handler may be changed later
3584     when the protocol specific setup function is called. */
3585  for(pp = protocols; (p = *pp) != NULL; pp++) {
3586    if(Curl_raw_equal(p->scheme, protostr)) {
3587      /* Protocol found in table. Check if allowed */
3588      if(!(data->set.allowed_protocols & p->protocol))
3589        /* nope, get out */
3590        break;
3591
3592      /* it is allowed for "normal" request, now do an extra check if this is
3593         the result of a redirect */
3594      if(data->state.this_is_a_follow &&
3595         !(data->set.redir_protocols & p->protocol))
3596        /* nope, get out */
3597        break;
3598
3599      /* Perform setup complement if some. */
3600      conn->handler = conn->given = p;
3601
3602      /* 'port' and 'remote_port' are set in setup_connection_internals() */
3603      return CURLE_OK;
3604    }
3605  }
3606
3607
3608  /* The protocol was not found in the table, but we don't have to assign it
3609     to anything since it is already assigned to a dummy-struct in the
3610     create_conn() function when the connectdata struct is allocated. */
3611  failf(data, "Protocol %s not supported or disabled in " LIBCURL_NAME,
3612        protostr);
3613
3614  return CURLE_UNSUPPORTED_PROTOCOL;
3615}
3616
3617/*
3618 * Parse URL and fill in the relevant members of the connection struct.
3619 */
3620static CURLcode parseurlandfillconn(struct SessionHandle *data,
3621                                    struct connectdata *conn,
3622                                    bool *prot_missing)
3623{
3624  char *at;
3625  char *fragment;
3626  char *path = data->state.path;
3627  char *query;
3628  int rc;
3629  char protobuf[16];
3630  const char *protop;
3631
3632  *prot_missing = FALSE;
3633
3634  /*************************************************************
3635   * Parse the URL.
3636   *
3637   * We need to parse the url even when using the proxy, because we will need
3638   * the hostname and port in case we are trying to SSL connect through the
3639   * proxy -- and we don't know if we will need to use SSL until we parse the
3640   * url ...
3641   ************************************************************/
3642  if((2 == sscanf(data->change.url, "%15[^:]:%[^\n]",
3643                  protobuf, path)) &&
3644     Curl_raw_equal(protobuf, "file")) {
3645    if(path[0] == '/' && path[1] == '/') {
3646      /* Allow omitted hostname (e.g. file:/<path>).  This is not strictly
3647       * speaking a valid file: URL by RFC 1738, but treating file:/<path> as
3648       * file://localhost/<path> is similar to how other schemes treat missing
3649       * hostnames.  See RFC 1808. */
3650
3651      /* This cannot be done with strcpy() in a portable manner, since the
3652         memory areas overlap! */
3653      memmove(path, path + 2, strlen(path + 2)+1);
3654    }
3655    /*
3656     * we deal with file://<host>/<path> differently since it supports no
3657     * hostname other than "localhost" and "127.0.0.1", which is unique among
3658     * the URL protocols specified in RFC 1738
3659     */
3660    if(path[0] != '/') {
3661      /* the URL included a host name, we ignore host names in file:// URLs
3662         as the standards don't define what to do with them */
3663      char *ptr=strchr(path, '/');
3664      if(ptr) {
3665        /* there was a slash present
3666
3667           RFC1738 (section 3.1, page 5) says:
3668
3669           The rest of the locator consists of data specific to the scheme,
3670           and is known as the "url-path". It supplies the details of how the
3671           specified resource can be accessed. Note that the "/" between the
3672           host (or port) and the url-path is NOT part of the url-path.
3673
3674           As most agents use file://localhost/foo to get '/foo' although the
3675           slash preceding foo is a separator and not a slash for the path,
3676           a URL as file://localhost//foo must be valid as well, to refer to
3677           the same file with an absolute path.
3678        */
3679
3680        if(ptr[1] && ('/' == ptr[1]))
3681          /* if there was two slashes, we skip the first one as that is then
3682             used truly as a separator */
3683          ptr++;
3684
3685        /* This cannot be made with strcpy, as the memory chunks overlap! */
3686        memmove(path, ptr, strlen(ptr)+1);
3687      }
3688    }
3689
3690    protop = "file"; /* protocol string */
3691  }
3692  else {
3693    /* clear path */
3694    path[0]=0;
3695
3696    if(2 > sscanf(data->change.url,
3697                   "%15[^\n:]://%[^\n/?]%[^\n]",
3698                   protobuf,
3699                   conn->host.name, path)) {
3700
3701      /*
3702       * The URL was badly formatted, let's try the browser-style _without_
3703       * protocol specified like 'http://'.
3704       */
3705      rc = sscanf(data->change.url, "%[^\n/?]%[^\n]", conn->host.name, path);
3706      if(1 > rc) {
3707        /*
3708         * We couldn't even get this format.
3709         * djgpp 2.04 has a sscanf() bug where 'conn->host.name' is
3710         * assigned, but the return value is EOF!
3711         */
3712#if defined(__DJGPP__) && (DJGPP_MINOR == 4)
3713        if(!(rc == -1 && *conn->host.name))
3714#endif
3715        {
3716          failf(data, "<url> malformed");
3717          return CURLE_URL_MALFORMAT;
3718        }
3719      }
3720
3721      /*
3722       * Since there was no protocol part specified, we guess what protocol it
3723       * is based on the first letters of the server name.
3724       */
3725
3726      /* Note: if you add a new protocol, please update the list in
3727       * lib/version.c too! */
3728
3729      if(checkprefix("FTP.", conn->host.name))
3730        protop = "ftp";
3731      else if(checkprefix("DICT.", conn->host.name))
3732        protop = "DICT";
3733      else if(checkprefix("LDAP.", conn->host.name))
3734        protop = "LDAP";
3735      else if(checkprefix("IMAP.", conn->host.name))
3736        protop = "IMAP";
3737      else {
3738        protop = "http";
3739      }
3740
3741      *prot_missing = TRUE; /* not given in URL */
3742    }
3743    else
3744      protop = protobuf;
3745  }
3746
3747  /* We search for '?' in the host name (but only on the right side of a
3748   * @-letter to allow ?-letters in username and password) to handle things
3749   * like http://example.com?param= (notice the missing '/').
3750   */
3751  at = strchr(conn->host.name, '@');
3752  if(at)
3753    query = strchr(at+1, '?');
3754  else
3755    query = strchr(conn->host.name, '?');
3756
3757  if(query) {
3758    /* We must insert a slash before the '?'-letter in the URL. If the URL had
3759       a slash after the '?', that is where the path currently begins and the
3760       '?string' is still part of the host name.
3761
3762       We must move the trailing part from the host name and put it first in
3763       the path. And have it all prefixed with a slash.
3764    */
3765
3766    size_t hostlen = strlen(query);
3767    size_t pathlen = strlen(path);
3768
3769    /* move the existing path plus the zero byte forward, to make room for
3770       the host-name part */
3771    memmove(path+hostlen+1, path, pathlen+1);
3772
3773     /* now copy the trailing host part in front of the existing path */
3774    memcpy(path+1, query, hostlen);
3775
3776    path[0]='/'; /* prepend the missing slash */
3777
3778    *query=0; /* now cut off the hostname at the ? */
3779  }
3780  else if(!path[0]) {
3781    /* if there's no path set, use a single slash */
3782    strcpy(path, "/");
3783  }
3784
3785  /* If the URL is malformatted (missing a '/' after hostname before path) we
3786   * insert a slash here. The only letter except '/' we accept to start a path
3787   * is '?'.
3788   */
3789  if(path[0] == '?') {
3790    /* We need this function to deal with overlapping memory areas. We know
3791       that the memory area 'path' points to is 'urllen' bytes big and that
3792       is bigger than the path. Use +1 to move the zero byte too. */
3793    memmove(&path[1], path, strlen(path)+1);
3794    path[0] = '/';
3795  }
3796
3797  if(conn->host.name[0] == '[') {
3798    /* This looks like an IPv6 address literal.  See if there is an address
3799       scope.  */
3800    char *percent = strstr (conn->host.name, "%25");
3801    if(percent) {
3802      char *endp;
3803      unsigned long scope = strtoul (percent + 3, &endp, 10);
3804      if(*endp == ']') {
3805        /* The address scope was well formed.  Knock it out of the
3806           hostname. */
3807        memmove(percent, endp, strlen(endp)+1);
3808        if(!data->state.this_is_a_follow)
3809          /* Don't honour a scope given in a Location: header */
3810          conn->scope = (unsigned int)scope;
3811      }
3812      else
3813        infof(data, "Invalid IPv6 address format\n");
3814    }
3815  }
3816
3817  if(data->set.scope)
3818    /* Override any scope that was set above.  */
3819    conn->scope = data->set.scope;
3820
3821  /* Remove the fragment part of the path. Per RFC 2396, this is always the
3822     last part of the URI. We are looking for the first '#' so that we deal
3823     gracefully with non conformant URI such as http://example.com#foo#bar. */
3824  fragment = strchr(path, '#');
3825  if(fragment)
3826    *fragment = 0;
3827
3828  /*
3829   * So if the URL was A://B/C#D,
3830   *   protop is A
3831   *   conn->host.name is B
3832   *   data->state.path is /C
3833   */
3834
3835  return findprotocol(data, conn, protop);
3836}
3837
3838/*
3839 * If we're doing a resumed transfer, we need to setup our stuff
3840 * properly.
3841 */
3842static CURLcode setup_range(struct SessionHandle *data)
3843{
3844  struct UrlState *s = &data->state;
3845  s->resume_from = data->set.set_resume_from;
3846  if(s->resume_from || data->set.str[STRING_SET_RANGE]) {
3847    if(s->rangestringalloc)
3848      free(s->range);
3849
3850    if(s->resume_from)
3851      s->range = aprintf("%" FORMAT_OFF_TU "-", s->resume_from);
3852    else
3853      s->range = strdup(data->set.str[STRING_SET_RANGE]);
3854
3855    s->rangestringalloc = (bool)(s->range?TRUE:FALSE);
3856
3857    if(!s->range)
3858      return CURLE_OUT_OF_MEMORY;
3859
3860    /* tell ourselves to fetch this range */
3861    s->use_range = TRUE;        /* enable range download */
3862  }
3863  else
3864    s->use_range = FALSE; /* disable range download */
3865
3866  return CURLE_OK;
3867}
3868
3869
3870/***************************************************************
3871* Setup connection internals specific to the requested protocol.
3872* This MUST get called after proxy magic has been figured out.
3873***************************************************************/
3874static CURLcode setup_connection_internals(struct connectdata *conn)
3875{
3876  const struct Curl_handler * p;
3877  CURLcode result;
3878
3879  conn->socktype = SOCK_STREAM; /* most of them are TCP streams */
3880
3881  /* Scan protocol handler table. */
3882
3883  /* Perform setup complement if some. */
3884  p = conn->handler;
3885
3886  if(p->setup_connection) {
3887    result = (*p->setup_connection)(conn);
3888
3889    if(result != CURLE_OK)
3890      return result;
3891
3892    p = conn->handler;              /* May have changed. */
3893  }
3894
3895  if(conn->port < 0)
3896    /* we check for -1 here since if proxy was detected already, this
3897       was very likely already set to the proxy port */
3898    conn->port = p->defport;
3899  conn->remote_port = (unsigned short)conn->given->defport;
3900
3901  return CURLE_OK;
3902}
3903
3904#ifndef CURL_DISABLE_PROXY
3905/****************************************************************
3906* Checks if the host is in the noproxy list. returns true if it matches
3907* and therefore the proxy should NOT be used.
3908****************************************************************/
3909static bool check_noproxy(const char* name, const char* no_proxy)
3910{
3911  /* no_proxy=domain1.dom,host.domain2.dom
3912   *   (a comma-separated list of hosts which should
3913   *   not be proxied, or an asterisk to override
3914   *   all proxy variables)
3915   */
3916  size_t tok_start;
3917  size_t tok_end;
3918  const char* separator = ", ";
3919  size_t no_proxy_len;
3920  size_t namelen;
3921  char *endptr;
3922
3923  if(no_proxy && no_proxy[0]) {
3924    if(Curl_raw_equal("*", no_proxy)) {
3925      return TRUE;
3926    }
3927
3928    /* NO_PROXY was specified and it wasn't just an asterisk */
3929
3930    no_proxy_len = strlen(no_proxy);
3931    endptr = strchr(name, ':');
3932    if(endptr)
3933      namelen = endptr - name;
3934    else
3935      namelen = strlen(name);
3936
3937    for(tok_start = 0; tok_start < no_proxy_len; tok_start = tok_end + 1) {
3938      while(tok_start < no_proxy_len &&
3939            strchr(separator, no_proxy[tok_start]) != NULL) {
3940        /* Look for the beginning of the token. */
3941        ++tok_start;
3942      }
3943
3944      if(tok_start == no_proxy_len)
3945        break; /* It was all trailing separator chars, no more tokens. */
3946
3947      for(tok_end = tok_start; tok_end < no_proxy_len &&
3948            strchr(separator, no_proxy[tok_end]) == NULL; ++tok_end)
3949        /* Look for the end of the token. */
3950        ;
3951
3952      /* To match previous behaviour, where it was necessary to specify
3953       * ".local.com" to prevent matching "notlocal.com", we will leave
3954       * the '.' off.
3955       */
3956      if(no_proxy[tok_start] == '.')
3957        ++tok_start;
3958
3959      if((tok_end - tok_start) <= namelen) {
3960        /* Match the last part of the name to the domain we are checking. */
3961        const char *checkn = name + namelen - (tok_end - tok_start);
3962        if(Curl_raw_nequal(no_proxy + tok_start, checkn,
3963                           tok_end - tok_start)) {
3964          if((tok_end - tok_start) == namelen || *(checkn - 1) == '.') {
3965            /* We either have an exact match, or the previous character is a .
3966             * so it is within the same domain, so no proxy for this host.
3967             */
3968            return TRUE;
3969          }
3970        }
3971      } /* if((tok_end - tok_start) <= namelen) */
3972    } /* for(tok_start = 0; tok_start < no_proxy_len;
3973         tok_start = tok_end + 1) */
3974  } /* NO_PROXY was specified and it wasn't just an asterisk */
3975
3976  return FALSE;
3977}
3978
3979/****************************************************************
3980* Detect what (if any) proxy to use. Remember that this selects a host
3981* name and is not limited to HTTP proxies only.
3982* The returned pointer must be freed by the caller (unless NULL)
3983****************************************************************/
3984static char *detect_proxy(struct connectdata *conn)
3985{
3986  char *proxy = NULL;
3987
3988#ifndef CURL_DISABLE_HTTP
3989  /* If proxy was not specified, we check for default proxy environment
3990   * variables, to enable i.e Lynx compliance:
3991   *
3992   * http_proxy=http://some.server.dom:port/
3993   * https_proxy=http://some.server.dom:port/
3994   * ftp_proxy=http://some.server.dom:port/
3995   * no_proxy=domain1.dom,host.domain2.dom
3996   *   (a comma-separated list of hosts which should
3997   *   not be proxied, or an asterisk to override
3998   *   all proxy variables)
3999   * all_proxy=http://some.server.dom:port/
4000   *   (seems to exist for the CERN www lib. Probably
4001   *   the first to check for.)
4002   *
4003   * For compatibility, the all-uppercase versions of these variables are
4004   * checked if the lowercase versions don't exist.
4005   */
4006  char *no_proxy=NULL;
4007  char proxy_env[128];
4008
4009  no_proxy=curl_getenv("no_proxy");
4010  if(!no_proxy)
4011    no_proxy=curl_getenv("NO_PROXY");
4012
4013  if(!check_noproxy(conn->host.name, no_proxy)) {
4014    /* It was not listed as without proxy */
4015    const char *protop = conn->handler->scheme;
4016    char *envp = proxy_env;
4017    char *prox;
4018
4019    /* Now, build <protocol>_proxy and check for such a one to use */
4020    while(*protop)
4021      *envp++ = (char)tolower((int)*protop++);
4022
4023    /* append _proxy */
4024    strcpy(envp, "_proxy");
4025
4026    /* read the protocol proxy: */
4027    prox=curl_getenv(proxy_env);
4028
4029    /*
4030     * We don't try the uppercase version of HTTP_PROXY because of
4031     * security reasons:
4032     *
4033     * When curl is used in a webserver application
4034     * environment (cgi or php), this environment variable can
4035     * be controlled by the web server user by setting the
4036     * http header 'Proxy:' to some value.
4037     *
4038     * This can cause 'internal' http/ftp requests to be
4039     * arbitrarily redirected by any external attacker.
4040     */
4041    if(!prox && !Curl_raw_equal("http_proxy", proxy_env)) {
4042      /* There was no lowercase variable, try the uppercase version: */
4043      Curl_strntoupper(proxy_env, proxy_env, sizeof(proxy_env));
4044      prox=curl_getenv(proxy_env);
4045    }
4046
4047    if(prox && *prox) { /* don't count "" strings */
4048      proxy = prox; /* use this */
4049    }
4050    else {
4051      proxy = curl_getenv("all_proxy"); /* default proxy to use */
4052      if(!proxy)
4053        proxy=curl_getenv("ALL_PROXY");
4054    }
4055  } /* if(!check_noproxy(conn->host.name, no_proxy)) - it wasn't specified
4056       non-proxy */
4057  if(no_proxy)
4058    free(no_proxy);
4059
4060#else /* !CURL_DISABLE_HTTP */
4061
4062  (void)conn;
4063#endif /* CURL_DISABLE_HTTP */
4064
4065  return proxy;
4066}
4067
4068/*
4069 * If this is supposed to use a proxy, we need to figure out the proxy
4070 * host name, so that we can re-use an existing connection
4071 * that may exist registered to the same proxy host.
4072 * proxy will be freed before this function returns.
4073 */
4074static CURLcode parse_proxy(struct SessionHandle *data,
4075                            struct connectdata *conn, char *proxy)
4076{
4077  char *prox_portno;
4078  char *endofprot;
4079
4080  /* We use 'proxyptr' to point to the proxy name from now on... */
4081  char *proxyptr;
4082  char *portptr;
4083  char *atsign;
4084
4085  /* We do the proxy host string parsing here. We want the host name and the
4086   * port name. Accept a protocol:// prefix
4087   */
4088
4089  /* Parse the protocol part if present */
4090  endofprot = strstr(proxy, "://");
4091  if(endofprot) {
4092    proxyptr = endofprot+3;
4093    if(checkprefix("socks5h", proxy))
4094      conn->proxytype = CURLPROXY_SOCKS5_HOSTNAME;
4095    else if(checkprefix("socks5", proxy))
4096      conn->proxytype = CURLPROXY_SOCKS5;
4097    else if(checkprefix("socks4a", proxy))
4098      conn->proxytype = CURLPROXY_SOCKS4A;
4099    else if(checkprefix("socks4", proxy))
4100      conn->proxytype = CURLPROXY_SOCKS4;
4101    /* Any other xxx:// : change to http proxy */
4102  }
4103  else
4104    proxyptr = proxy; /* No xxx:// head: It's a HTTP proxy */
4105
4106  /* Is there a username and password given in this proxy url? */
4107  atsign = strchr(proxyptr, '@');
4108  if(atsign) {
4109    char proxyuser[MAX_CURL_USER_LENGTH];
4110    char proxypasswd[MAX_CURL_PASSWORD_LENGTH];
4111    proxypasswd[0] = 0;
4112
4113    if(1 <= sscanf(proxyptr,
4114                   "%" MAX_CURL_USER_LENGTH_TXT"[^:@]:"
4115                   "%" MAX_CURL_PASSWORD_LENGTH_TXT "[^@]",
4116                   proxyuser, proxypasswd)) {
4117      CURLcode res = CURLE_OK;
4118
4119      /* found user and password, rip them out.  note that we are
4120         unescaping them, as there is otherwise no way to have a
4121         username or password with reserved characters like ':' in
4122         them. */
4123      Curl_safefree(conn->proxyuser);
4124      conn->proxyuser = curl_easy_unescape(data, proxyuser, 0, NULL);
4125
4126      if(!conn->proxyuser)
4127        res = CURLE_OUT_OF_MEMORY;
4128      else {
4129        Curl_safefree(conn->proxypasswd);
4130        conn->proxypasswd = curl_easy_unescape(data, proxypasswd, 0, NULL);
4131
4132        if(!conn->proxypasswd)
4133          res = CURLE_OUT_OF_MEMORY;
4134      }
4135
4136      if(CURLE_OK == res) {
4137        conn->bits.proxy_user_passwd = TRUE; /* enable it */
4138        atsign = strdup(atsign+1); /* the right side of the @-letter */
4139
4140        if(atsign) {
4141          free(proxy); /* free the former proxy string */
4142          proxy = proxyptr = atsign; /* now use this instead */
4143        }
4144        else
4145          res = CURLE_OUT_OF_MEMORY;
4146      }
4147
4148      if(res) {
4149        free(proxy); /* free the allocated proxy string */
4150        return res;
4151      }
4152    }
4153  }
4154
4155  /* start scanning for port number at this point */
4156  portptr = proxyptr;
4157
4158  /* detect and extract RFC2732-style IPv6-addresses */
4159  if(*proxyptr == '[') {
4160    char *ptr = ++proxyptr; /* advance beyond the initial bracket */
4161    while(*ptr && (ISXDIGIT(*ptr) || (*ptr == ':') || (*ptr == '%') ||
4162                   (*ptr == '.')))
4163      ptr++;
4164    if(*ptr == ']')
4165      /* yeps, it ended nicely with a bracket as well */
4166      *ptr++ = 0;
4167    else
4168      infof(data, "Invalid IPv6 address format\n");
4169    portptr = ptr;
4170    /* Note that if this didn't end with a bracket, we still advanced the
4171     * proxyptr first, but I can't see anything wrong with that as no host
4172     * name nor a numeric can legally start with a bracket.
4173     */
4174  }
4175
4176  /* Get port number off proxy.server.com:1080 */
4177  prox_portno = strchr(portptr, ':');
4178  if(prox_portno) {
4179    *prox_portno = 0x0; /* cut off number from host name */
4180    prox_portno ++;
4181    /* now set the local port number */
4182    conn->port = strtol(prox_portno, NULL, 10);
4183  }
4184  else {
4185    /* without a port number after the host name, some people seem to use
4186       a slash so we strip everything from the first slash */
4187    atsign = strchr(proxyptr, '/');
4188    if(atsign)
4189      *atsign = 0x0; /* cut off path part from host name */
4190
4191    if(data->set.proxyport)
4192      /* None given in the proxy string, then get the default one if it is
4193         given */
4194      conn->port = data->set.proxyport;
4195  }
4196
4197  /* now, clone the cleaned proxy host name */
4198  conn->proxy.rawalloc = strdup(proxyptr);
4199  conn->proxy.name = conn->proxy.rawalloc;
4200
4201  free(proxy);
4202  if(!conn->proxy.rawalloc)
4203    return CURLE_OUT_OF_MEMORY;
4204
4205  return CURLE_OK;
4206}
4207
4208/*
4209 * Extract the user and password from the authentication string
4210 */
4211static CURLcode parse_proxy_auth(struct SessionHandle *data,
4212                                 struct connectdata *conn)
4213{
4214  char proxyuser[MAX_CURL_USER_LENGTH]="";
4215  char proxypasswd[MAX_CURL_PASSWORD_LENGTH]="";
4216
4217  if(data->set.str[STRING_PROXYUSERNAME] != NULL) {
4218    strncpy(proxyuser, data->set.str[STRING_PROXYUSERNAME],
4219            MAX_CURL_USER_LENGTH);
4220    proxyuser[MAX_CURL_USER_LENGTH-1] = '\0';   /*To be on safe side*/
4221  }
4222  if(data->set.str[STRING_PROXYPASSWORD] != NULL) {
4223    strncpy(proxypasswd, data->set.str[STRING_PROXYPASSWORD],
4224            MAX_CURL_PASSWORD_LENGTH);
4225    proxypasswd[MAX_CURL_PASSWORD_LENGTH-1] = '\0'; /*To be on safe side*/
4226  }
4227
4228  conn->proxyuser = curl_easy_unescape(data, proxyuser, 0, NULL);
4229  if(!conn->proxyuser)
4230    return CURLE_OUT_OF_MEMORY;
4231
4232  conn->proxypasswd = curl_easy_unescape(data, proxypasswd, 0, NULL);
4233  if(!conn->proxypasswd)
4234    return CURLE_OUT_OF_MEMORY;
4235
4236  return CURLE_OK;
4237}
4238#endif /* CURL_DISABLE_PROXY */
4239
4240/*
4241 *
4242 * Parse a user name and password in the URL and strip it out of the host name
4243 *
4244 * Inputs: data->set.use_netrc (CURLOPT_NETRC)
4245 *         conn->host.name
4246 *
4247 * Outputs: (almost :- all currently undefined)
4248 *          conn->bits.user_passwd  - non-zero if non-default passwords exist
4249 *          user                    - non-zero length if defined
4250 *          passwd                  -   ditto
4251 *          conn->host.name         - remove user name and password
4252 */
4253static CURLcode parse_url_userpass(struct SessionHandle *data,
4254                                   struct connectdata *conn,
4255                                   char *user, char *passwd)
4256{
4257  /* At this point, we're hoping all the other special cases have
4258   * been taken care of, so conn->host.name is at most
4259   *    [user[:password]]@]hostname
4260   *
4261   * We need somewhere to put the embedded details, so do that first.
4262   */
4263
4264  char *ptr=strchr(conn->host.name, '@');
4265  char *userpass = conn->host.name;
4266
4267  user[0] =0;   /* to make everything well-defined */
4268  passwd[0]=0;
4269
4270  /* We will now try to extract the
4271   * possible user+password pair in a string like:
4272   * ftp://user:password@ftp.my.site:8021/README */
4273  if(ptr != NULL) {
4274    /* there's a user+password given here, to the left of the @ */
4275
4276    conn->host.name = ++ptr;
4277
4278    /* So the hostname is sane.  Only bother interpreting the
4279     * results if we could care.  It could still be wasted
4280     * work because it might be overtaken by the programmatically
4281     * set user/passwd, but doing that first adds more cases here :-(
4282     */
4283
4284    conn->bits.userpwd_in_url = TRUE;
4285    if(data->set.use_netrc != CURL_NETRC_REQUIRED) {
4286      /* We could use the one in the URL */
4287
4288      conn->bits.user_passwd = TRUE; /* enable user+password */
4289
4290      if(*userpass != ':') {
4291        /* the name is given, get user+password */
4292        sscanf(userpass, "%" MAX_CURL_USER_LENGTH_TXT "[^:@]:"
4293               "%" MAX_CURL_PASSWORD_LENGTH_TXT "[^@]",
4294               user, passwd);
4295      }
4296      else
4297        /* no name given, get the password only */
4298        sscanf(userpass, ":%" MAX_CURL_PASSWORD_LENGTH_TXT "[^@]", passwd);
4299
4300      if(user[0]) {
4301        char *newname=curl_easy_unescape(data, user, 0, NULL);
4302        if(!newname)
4303          return CURLE_OUT_OF_MEMORY;
4304        if(strlen(newname) < MAX_CURL_USER_LENGTH)
4305          strcpy(user, newname);
4306
4307        /* if the new name is longer than accepted, then just use
4308           the unconverted name, it'll be wrong but what the heck */
4309        free(newname);
4310      }
4311      if(passwd[0]) {
4312        /* we have a password found in the URL, decode it! */
4313        char *newpasswd=curl_easy_unescape(data, passwd, 0, NULL);
4314        if(!newpasswd)
4315          return CURLE_OUT_OF_MEMORY;
4316        if(strlen(newpasswd) < MAX_CURL_PASSWORD_LENGTH)
4317          strcpy(passwd, newpasswd);
4318
4319        free(newpasswd);
4320      }
4321    }
4322  }
4323  return CURLE_OK;
4324}
4325
4326/*************************************************************
4327 * Figure out the remote port number and fix it in the URL
4328 *
4329 * No matter if we use a proxy or not, we have to figure out the remote
4330 * port number of various reasons.
4331 *
4332 * To be able to detect port number flawlessly, we must not confuse them
4333 * IPv6-specified addresses in the [0::1] style. (RFC2732)
4334 *
4335 * The conn->host.name is currently [user:passwd@]host[:port] where host
4336 * could be a hostname, IPv4 address or IPv6 address.
4337 *
4338 * The port number embedded in the URL is replaced, if necessary.
4339 *************************************************************/
4340static CURLcode parse_remote_port(struct SessionHandle *data,
4341                                  struct connectdata *conn)
4342{
4343  char *portptr;
4344  char endbracket;
4345
4346  /* Note that at this point, the IPv6 address cannot contain any scope
4347     suffix as that has already been removed in the parseurlandfillconn()
4348     function */
4349  if((1 == sscanf(conn->host.name, "[%*45[0123456789abcdefABCDEF:.]%c",
4350                  &endbracket)) &&
4351     (']' == endbracket)) {
4352    /* this is a RFC2732-style specified IP-address */
4353    conn->bits.ipv6_ip = TRUE;
4354
4355    conn->host.name++; /* skip over the starting bracket */
4356    portptr = strchr(conn->host.name, ']');
4357    if(portptr) {
4358      *portptr++ = '\0'; /* zero terminate, killing the bracket */
4359      if(':' != *portptr)
4360        portptr = NULL; /* no port number available */
4361    }
4362  }
4363  else
4364    portptr = strrchr(conn->host.name, ':');
4365
4366  if(data->set.use_port && data->state.allow_port) {
4367    /* if set, we use this and ignore the port possibly given in the URL */
4368    conn->remote_port = (unsigned short)data->set.use_port;
4369    if(portptr)
4370      *portptr = '\0'; /* cut off the name there anyway - if there was a port
4371                      number - since the port number is to be ignored! */
4372    if(conn->bits.httpproxy) {
4373      /* we need to create new URL with the new port number */
4374      char *url;
4375      char type[12]="";
4376
4377      if(conn->bits.type_set)
4378        snprintf(type, sizeof(type), ";type=%c",
4379                 data->set.prefer_ascii?'A':
4380                 (data->set.ftp_list_only?'D':'I'));
4381
4382      /*
4383       * This synthesized URL isn't always right--suffixes like ;type=A are
4384       * stripped off. It would be better to work directly from the original
4385       * URL and simply replace the port part of it.
4386       */
4387      url = aprintf("%s://%s%s%s:%hu%s%s%s", conn->given->scheme,
4388                    conn->bits.ipv6_ip?"[":"", conn->host.name,
4389                    conn->bits.ipv6_ip?"]":"", conn->remote_port,
4390                    data->state.slash_removed?"/":"", data->state.path,
4391                    type);
4392      if(!url)
4393        return CURLE_OUT_OF_MEMORY;
4394
4395      if(data->change.url_alloc)
4396        free(data->change.url);
4397
4398      data->change.url = url;
4399      data->change.url_alloc = TRUE;
4400    }
4401  }
4402  else if(portptr) {
4403    /* no CURLOPT_PORT given, extract the one from the URL */
4404
4405    char *rest;
4406    unsigned long port;
4407
4408    port=strtoul(portptr+1, &rest, 10);  /* Port number must be decimal */
4409
4410    if(rest != (portptr+1) && *rest == '\0') {
4411      /* The colon really did have only digits after it,
4412       * so it is either a port number or a mistake */
4413
4414      if(port > 0xffff) {   /* Single unix standard says port numbers are
4415                              * 16 bits long */
4416        failf(data, "Port number too large: %lu", port);
4417        return CURLE_URL_MALFORMAT;
4418      }
4419
4420      *portptr = '\0'; /* cut off the name there */
4421      conn->remote_port = curlx_ultous(port);
4422    }
4423    else if(!port)
4424      /* Browser behavior adaptation. If there's a colon with no digits after,
4425         just cut off the name there which makes us ignore the colon and just
4426         use the default port. Firefox and Chrome both do that. */
4427      *portptr = '\0';
4428  }
4429  return CURLE_OK;
4430}
4431
4432/*
4433 * Override a user name and password from the URL with that in the
4434 * CURLOPT_USERPWD option or a .netrc file, if applicable.
4435 */
4436static void override_userpass(struct SessionHandle *data,
4437                              struct connectdata *conn,
4438                              char *user, char *passwd)
4439{
4440  if(data->set.str[STRING_USERNAME] != NULL) {
4441    strncpy(user, data->set.str[STRING_USERNAME], MAX_CURL_USER_LENGTH);
4442    user[MAX_CURL_USER_LENGTH-1] = '\0';   /*To be on safe side*/
4443  }
4444  if(data->set.str[STRING_PASSWORD] != NULL) {
4445    strncpy(passwd, data->set.str[STRING_PASSWORD], MAX_CURL_PASSWORD_LENGTH);
4446    passwd[MAX_CURL_PASSWORD_LENGTH-1] = '\0'; /*To be on safe side*/
4447  }
4448
4449  conn->bits.netrc = FALSE;
4450  if(data->set.use_netrc != CURL_NETRC_IGNORED) {
4451    if(Curl_parsenetrc(conn->host.name,
4452                       user, passwd,
4453                       data->set.str[STRING_NETRC_FILE])) {
4454      infof(data, "Couldn't find host %s in the "
4455            DOT_CHAR "netrc file; using defaults\n",
4456            conn->host.name);
4457    }
4458    else {
4459      /* set bits.netrc TRUE to remember that we got the name from a .netrc
4460         file, so that it is safe to use even if we followed a Location: to a
4461         different host or similar. */
4462      conn->bits.netrc = TRUE;
4463
4464      conn->bits.user_passwd = TRUE; /* enable user+password */
4465    }
4466  }
4467}
4468
4469/*
4470 * Set password so it's available in the connection.
4471 */
4472static CURLcode set_userpass(struct connectdata *conn,
4473                             const char *user, const char *passwd)
4474{
4475  /* If our protocol needs a password and we have none, use the defaults */
4476  if((conn->handler->flags & PROTOPT_NEEDSPWD) &&
4477     !conn->bits.user_passwd) {
4478
4479    conn->user = strdup(CURL_DEFAULT_USER);
4480    if(conn->user)
4481      conn->passwd = strdup(CURL_DEFAULT_PASSWORD);
4482    else
4483      conn->passwd = NULL;
4484    /* This is the default password, so DON'T set conn->bits.user_passwd */
4485  }
4486  else {
4487    /* store user + password, zero-length if not set */
4488    conn->user = strdup(user);
4489    if(conn->user)
4490      conn->passwd = strdup(passwd);
4491    else
4492      conn->passwd = NULL;
4493  }
4494  if(!conn->user || !conn->passwd)
4495    return CURLE_OUT_OF_MEMORY;
4496
4497  return CURLE_OK;
4498}
4499
4500/*************************************************************
4501 * Resolve the address of the server or proxy
4502 *************************************************************/
4503static CURLcode resolve_server(struct SessionHandle *data,
4504                               struct connectdata *conn,
4505                               bool *async)
4506{
4507  CURLcode result=CURLE_OK;
4508  long timeout_ms = Curl_timeleft(data, NULL, TRUE);
4509
4510  /*************************************************************
4511   * Resolve the name of the server or proxy
4512   *************************************************************/
4513  if(conn->bits.reuse) {
4514    /* We're reusing the connection - no need to resolve anything */
4515    *async = FALSE;
4516
4517    if(conn->bits.proxy)
4518      fix_hostname(data, conn, &conn->host);
4519  }
4520  else {
4521    /* this is a fresh connect */
4522    int rc;
4523    struct Curl_dns_entry *hostaddr;
4524
4525    /* set a pointer to the hostname we display */
4526    fix_hostname(data, conn, &conn->host);
4527
4528    if(!conn->proxy.name || !*conn->proxy.name) {
4529      /* If not connecting via a proxy, extract the port from the URL, if it is
4530       * there, thus overriding any defaults that might have been set above. */
4531      conn->port =  conn->remote_port; /* it is the same port */
4532
4533      /* Resolve target host right on */
4534      rc = Curl_resolv_timeout(conn, conn->host.name, (int)conn->port,
4535                               &hostaddr, timeout_ms);
4536      if(rc == CURLRESOLV_PENDING)
4537        *async = TRUE;
4538
4539      else if(rc == CURLRESOLV_TIMEDOUT)
4540        result = CURLE_OPERATION_TIMEDOUT;
4541
4542      else if(!hostaddr) {
4543        failf(data, "Couldn't resolve host '%s'", conn->host.dispname);
4544        result =  CURLE_COULDNT_RESOLVE_HOST;
4545        /* don't return yet, we need to clean up the timeout first */
4546      }
4547    }
4548    else {
4549      /* This is a proxy that hasn't been resolved yet. */
4550
4551      /* IDN-fix the proxy name */
4552      fix_hostname(data, conn, &conn->proxy);
4553
4554      /* resolve proxy */
4555      rc = Curl_resolv_timeout(conn, conn->proxy.name, (int)conn->port,
4556                               &hostaddr, timeout_ms);
4557
4558      if(rc == CURLRESOLV_PENDING)
4559        *async = TRUE;
4560
4561      else if(rc == CURLRESOLV_TIMEDOUT)
4562        result = CURLE_OPERATION_TIMEDOUT;
4563
4564      else if(!hostaddr) {
4565        failf(data, "Couldn't resolve proxy '%s'", conn->proxy.dispname);
4566        result = CURLE_COULDNT_RESOLVE_PROXY;
4567        /* don't return yet, we need to clean up the timeout first */
4568      }
4569    }
4570    DEBUGASSERT(conn->dns_entry == NULL);
4571    conn->dns_entry = hostaddr;
4572  }
4573
4574  return result;
4575}
4576
4577/*
4578 * Cleanup the connection just allocated before we can move along and use the
4579 * previously existing one.  All relevant data is copied over and old_conn is
4580 * ready for freeing once this function returns.
4581 */
4582static void reuse_conn(struct connectdata *old_conn,
4583                       struct connectdata *conn)
4584{
4585  if(old_conn->proxy.rawalloc)
4586    free(old_conn->proxy.rawalloc);
4587
4588  /* free the SSL config struct from this connection struct as this was
4589     allocated in vain and is targeted for destruction */
4590  Curl_free_ssl_config(&old_conn->ssl_config);
4591
4592  conn->data = old_conn->data;
4593
4594  /* get the user+password information from the old_conn struct since it may
4595   * be new for this request even when we re-use an existing connection */
4596  conn->bits.user_passwd = old_conn->bits.user_passwd;
4597  if(conn->bits.user_passwd) {
4598    /* use the new user name and password though */
4599    Curl_safefree(conn->user);
4600    Curl_safefree(conn->passwd);
4601    conn->user = old_conn->user;
4602    conn->passwd = old_conn->passwd;
4603    old_conn->user = NULL;
4604    old_conn->passwd = NULL;
4605  }
4606
4607  conn->bits.proxy_user_passwd = old_conn->bits.proxy_user_passwd;
4608  if(conn->bits.proxy_user_passwd) {
4609    /* use the new proxy user name and proxy password though */
4610    Curl_safefree(conn->proxyuser);
4611    Curl_safefree(conn->proxypasswd);
4612    conn->proxyuser = old_conn->proxyuser;
4613    conn->proxypasswd = old_conn->proxypasswd;
4614    old_conn->proxyuser = NULL;
4615    old_conn->proxypasswd = NULL;
4616  }
4617
4618  /* host can change, when doing keepalive with a proxy ! */
4619  if(conn->bits.proxy) {
4620    free(conn->host.rawalloc);
4621    conn->host=old_conn->host;
4622  }
4623  else
4624    free(old_conn->host.rawalloc); /* free the newly allocated name buffer */
4625
4626  /* persist connection info in session handle */
4627  Curl_persistconninfo(conn);
4628
4629  /* re-use init */
4630  conn->bits.reuse = TRUE; /* yes, we're re-using here */
4631
4632  Curl_safefree(old_conn->user);
4633  Curl_safefree(old_conn->passwd);
4634  Curl_safefree(old_conn->proxyuser);
4635  Curl_safefree(old_conn->proxypasswd);
4636  Curl_llist_destroy(old_conn->send_pipe, NULL);
4637  Curl_llist_destroy(old_conn->recv_pipe, NULL);
4638  Curl_llist_destroy(old_conn->pend_pipe, NULL);
4639  Curl_llist_destroy(old_conn->done_pipe, NULL);
4640  Curl_safefree(old_conn->master_buffer);
4641}
4642
4643/**
4644 * create_conn() sets up a new connectdata struct, or re-uses an already
4645 * existing one, and resolves host name.
4646 *
4647 * if this function returns CURLE_OK and *async is set to TRUE, the resolve
4648 * response will be coming asynchronously. If *async is FALSE, the name is
4649 * already resolved.
4650 *
4651 * @param data The sessionhandle pointer
4652 * @param in_connect is set to the next connection data pointer
4653 * @param async is set TRUE when an async DNS resolution is pending
4654 * @see Curl_setup_conn()
4655 *
4656 * *NOTE* this function assigns the conn->data pointer!
4657 */
4658
4659static CURLcode create_conn(struct SessionHandle *data,
4660                            struct connectdata **in_connect,
4661                            bool *async)
4662{
4663  CURLcode result=CURLE_OK;
4664  struct connectdata *conn;
4665  struct connectdata *conn_temp = NULL;
4666  size_t urllen;
4667  char user[MAX_CURL_USER_LENGTH];
4668  char passwd[MAX_CURL_PASSWORD_LENGTH];
4669  bool reuse;
4670  char *proxy = NULL;
4671  bool prot_missing = FALSE;
4672
4673  *async = FALSE;
4674
4675  /*************************************************************
4676   * Check input data
4677   *************************************************************/
4678
4679  if(!data->change.url)
4680    return CURLE_URL_MALFORMAT;
4681
4682  /* First, split up the current URL in parts so that we can use the
4683     parts for checking against the already present connections. In order
4684     to not have to modify everything at once, we allocate a temporary
4685     connection data struct and fill in for comparison purposes. */
4686  conn = allocate_conn(data);
4687
4688  if(!conn)
4689    return CURLE_OUT_OF_MEMORY;
4690
4691  /* We must set the return variable as soon as possible, so that our
4692     parent can cleanup any possible allocs we may have done before
4693     any failure */
4694  *in_connect = conn;
4695
4696  /* This initing continues below, see the comment "Continue connectdata
4697   * initialization here" */
4698
4699  /***********************************************************
4700   * We need to allocate memory to store the path in. We get the size of the
4701   * full URL to be sure, and we need to make it at least 256 bytes since
4702   * other parts of the code will rely on this fact
4703   ***********************************************************/
4704#define LEAST_PATH_ALLOC 256
4705  urllen=strlen(data->change.url);
4706  if(urllen < LEAST_PATH_ALLOC)
4707    urllen=LEAST_PATH_ALLOC;
4708
4709  /*
4710   * We malloc() the buffers below urllen+2 to make room for to possibilities:
4711   * 1 - an extra terminating zero
4712   * 2 - an extra slash (in case a syntax like "www.host.com?moo" is used)
4713   */
4714
4715  Curl_safefree(data->state.pathbuffer);
4716  data->state.pathbuffer = malloc(urllen+2);
4717  if(NULL == data->state.pathbuffer)
4718    return CURLE_OUT_OF_MEMORY; /* really bad error */
4719  data->state.path = data->state.pathbuffer;
4720
4721  conn->host.rawalloc = malloc(urllen+2);
4722  if(NULL == conn->host.rawalloc)
4723    return CURLE_OUT_OF_MEMORY;
4724
4725  conn->host.name = conn->host.rawalloc;
4726  conn->host.name[0] = 0;
4727
4728  result = parseurlandfillconn(data, conn, &prot_missing);
4729  if(result != CURLE_OK)
4730    return result;
4731
4732  /*************************************************************
4733   * No protocol part in URL was used, add it!
4734   *************************************************************/
4735  if(prot_missing) {
4736    /* We're guessing prefixes here and if we're told to use a proxy or if
4737       we're gonna follow a Location: later or... then we need the protocol
4738       part added so that we have a valid URL. */
4739    char *reurl;
4740
4741    reurl = aprintf("%s://%s", conn->handler->scheme, data->change.url);
4742
4743    if(!reurl) {
4744      Curl_safefree(proxy);
4745      return CURLE_OUT_OF_MEMORY;
4746    }
4747
4748    data->change.url = reurl;
4749    data->change.url_alloc = TRUE; /* free this later */
4750  }
4751
4752  /*************************************************************
4753   * Parse a user name and password in the URL and strip it out
4754   * of the host name
4755   *************************************************************/
4756  result = parse_url_userpass(data, conn, user, passwd);
4757  if(result != CURLE_OK)
4758    return result;
4759
4760#ifndef CURL_DISABLE_PROXY
4761  /*************************************************************
4762   * Extract the user and password from the authentication string
4763   *************************************************************/
4764  if(conn->bits.proxy_user_passwd) {
4765    result = parse_proxy_auth(data, conn);
4766    if(result != CURLE_OK)
4767        return result;
4768  }
4769
4770  /*************************************************************
4771   * Detect what (if any) proxy to use
4772   *************************************************************/
4773  if(data->set.str[STRING_PROXY]) {
4774    proxy = strdup(data->set.str[STRING_PROXY]);
4775    /* if global proxy is set, this is it */
4776    if(NULL == proxy) {
4777      failf(data, "memory shortage");
4778      return CURLE_OUT_OF_MEMORY;
4779    }
4780  }
4781
4782  if(data->set.str[STRING_NOPROXY] &&
4783     check_noproxy(conn->host.name, data->set.str[STRING_NOPROXY])) {
4784    if(proxy) {
4785      free(proxy);  /* proxy is in exception list */
4786      proxy = NULL;
4787    }
4788  }
4789  else if(!proxy)
4790    proxy = detect_proxy(conn);
4791
4792  if(proxy && (!*proxy || (conn->handler->flags & PROTOPT_NONETWORK))) {
4793    free(proxy);  /* Don't bother with an empty proxy string or if the
4794                     protocol doesn't work with network */
4795    proxy = NULL;
4796  }
4797
4798  /***********************************************************************
4799   * If this is supposed to use a proxy, we need to figure out the proxy host
4800   * name, proxy type and port number, so that we can re-use an existing
4801   * connection that may exist registered to the same proxy host.
4802   ***********************************************************************/
4803  if(proxy) {
4804    result = parse_proxy(data, conn, proxy);
4805
4806    /* parse_proxy has freed the proxy string, so don't try to use it again */
4807    if(result != CURLE_OK)
4808      return result;
4809
4810    if((conn->proxytype == CURLPROXY_HTTP) ||
4811       (conn->proxytype == CURLPROXY_HTTP_1_0)) {
4812#ifdef CURL_DISABLE_HTTP
4813      /* asking for a HTTP proxy is a bit funny when HTTP is disabled... */
4814      return CURLE_UNSUPPORTED_PROTOCOL;
4815#else
4816      /* force this connection's protocol to become HTTP if not already
4817         compatible */
4818      if(!(conn->handler->protocol & CURLPROTO_HTTP))
4819        conn->handler = &Curl_handler_http;
4820
4821      conn->bits.httpproxy = TRUE;
4822#endif
4823    }
4824    conn->bits.proxy = TRUE;
4825  }
4826  else {
4827    /* we aren't using the proxy after all... */
4828    conn->bits.proxy = FALSE;
4829    conn->bits.httpproxy = FALSE;
4830    conn->bits.proxy_user_passwd = FALSE;
4831    conn->bits.tunnel_proxy = FALSE;
4832  }
4833
4834#endif /* CURL_DISABLE_PROXY */
4835
4836  /*************************************************************
4837   * Setup internals depending on protocol. Needs to be done after
4838   * we figured out what/if proxy to use.
4839   *************************************************************/
4840  result = setup_connection_internals(conn);
4841  if(result != CURLE_OK) {
4842    Curl_safefree(proxy);
4843    return result;
4844  }
4845
4846  conn->recv[FIRSTSOCKET] = Curl_recv_plain;
4847  conn->send[FIRSTSOCKET] = Curl_send_plain;
4848  conn->recv[SECONDARYSOCKET] = Curl_recv_plain;
4849  conn->send[SECONDARYSOCKET] = Curl_send_plain;
4850
4851  /***********************************************************************
4852   * file: is a special case in that it doesn't need a network connection
4853   ***********************************************************************/
4854#ifndef CURL_DISABLE_FILE
4855  if(conn->handler->flags & PROTOPT_NONETWORK) {
4856    bool done;
4857    /* this is supposed to be the connect function so we better at least check
4858       that the file is present here! */
4859    DEBUGASSERT(conn->handler->connect_it);
4860    result = conn->handler->connect_it(conn, &done);
4861
4862    /* Setup a "faked" transfer that'll do nothing */
4863    if(CURLE_OK == result) {
4864      conn->data = data;
4865      conn->bits.tcpconnect = TRUE; /* we are "connected */
4866
4867      ConnectionStore(data, conn);
4868
4869      /*
4870       * Setup whatever necessary for a resumed transfer
4871       */
4872      result = setup_range(data);
4873      if(result) {
4874        DEBUGASSERT(conn->handler->done);
4875        /* we ignore the return code for the protocol-specific DONE */
4876        (void)conn->handler->done(conn, result, FALSE);
4877        return result;
4878      }
4879
4880      Curl_setup_transfer(conn, -1, -1, FALSE, NULL, /* no download */
4881                          -1, NULL); /* no upload */
4882    }
4883
4884    return result;
4885  }
4886#endif
4887
4888  /*************************************************************
4889   * If the protocol is using SSL and HTTP proxy is used, we set
4890   * the tunnel_proxy bit.
4891   *************************************************************/
4892  if((conn->given->flags&PROTOPT_SSL) && conn->bits.httpproxy)
4893    conn->bits.tunnel_proxy = TRUE;
4894
4895  /*************************************************************
4896   * Figure out the remote port number and fix it in the URL
4897   *************************************************************/
4898  result = parse_remote_port(data, conn);
4899  if(result != CURLE_OK)
4900    return result;
4901
4902  /*************************************************************
4903   * Check for an overridden user name and password, then set it
4904   * for use
4905   *************************************************************/
4906  override_userpass(data, conn, user, passwd);
4907  result = set_userpass(conn, user, passwd);
4908  if(result != CURLE_OK)
4909    return result;
4910
4911  /* Get a cloned copy of the SSL config situation stored in the
4912     connection struct. But to get this going nicely, we must first make
4913     sure that the strings in the master copy are pointing to the correct
4914     strings in the session handle strings array!
4915
4916     Keep in mind that the pointers in the master copy are pointing to strings
4917     that will be freed as part of the SessionHandle struct, but all cloned
4918     copies will be separately allocated.
4919  */
4920  data->set.ssl.CApath = data->set.str[STRING_SSL_CAPATH];
4921  data->set.ssl.CAfile = data->set.str[STRING_SSL_CAFILE];
4922  data->set.ssl.CRLfile = data->set.str[STRING_SSL_CRLFILE];
4923  data->set.ssl.issuercert = data->set.str[STRING_SSL_ISSUERCERT];
4924  data->set.ssl.random_file = data->set.str[STRING_SSL_RANDOM_FILE];
4925  data->set.ssl.egdsocket = data->set.str[STRING_SSL_EGDSOCKET];
4926  data->set.ssl.cipher_list = data->set.str[STRING_SSL_CIPHER_LIST];
4927#ifdef USE_TLS_SRP
4928  data->set.ssl.username = data->set.str[STRING_TLSAUTH_USERNAME];
4929  data->set.ssl.password = data->set.str[STRING_TLSAUTH_PASSWORD];
4930#endif
4931
4932  if(!Curl_clone_ssl_config(&data->set.ssl, &conn->ssl_config))
4933    return CURLE_OUT_OF_MEMORY;
4934
4935  /*************************************************************
4936   * Check the current list of connections to see if we can
4937   * re-use an already existing one or if we have to create a
4938   * new one.
4939   *************************************************************/
4940
4941  /* reuse_fresh is TRUE if we are told to use a new connection by force, but
4942     we only acknowledge this option if this is not a re-used connection
4943     already (which happens due to follow-location or during a HTTP
4944     authentication phase). */
4945  if(data->set.reuse_fresh && !data->state.this_is_a_follow)
4946    reuse = FALSE;
4947  else
4948    reuse = ConnectionExists(data, conn, &conn_temp);
4949
4950  if(reuse) {
4951    /*
4952     * We already have a connection for this, we got the former connection
4953     * in the conn_temp variable and thus we need to cleanup the one we
4954     * just allocated before we can move along and use the previously
4955     * existing one.
4956     */
4957    reuse_conn(conn, conn_temp);
4958    free(conn);          /* we don't need this anymore */
4959    conn = conn_temp;
4960    *in_connect = conn;
4961    infof(data, "Re-using existing connection! (#%ld) with host %s\n",
4962          conn->connectindex,
4963          conn->proxy.name?conn->proxy.dispname:conn->host.dispname);
4964  }
4965  else {
4966    /*
4967     * This is a brand new connection, so let's store it in the connection
4968     * cache of ours!
4969     */
4970    ConnectionStore(data, conn);
4971  }
4972
4973  /*
4974   * Setup whatever necessary for a resumed transfer
4975   */
4976  result = setup_range(data);
4977  if(result)
4978    return result;
4979
4980  /* Continue connectdata initialization here. */
4981
4982  /*
4983   * Inherit the proper values from the urldata struct AFTER we have arranged
4984   * the persistent connection stuff
4985   */
4986  conn->fread_func = data->set.fread_func;
4987  conn->fread_in = data->set.in;
4988  conn->seek_func = data->set.seek_func;
4989  conn->seek_client = data->set.seek_client;
4990
4991  /*************************************************************
4992   * Resolve the address of the server or proxy
4993   *************************************************************/
4994  result = resolve_server(data, conn, async);
4995
4996  return result;
4997}
4998
4999/* Curl_setup_conn() is called after the name resolve initiated in
5000 * create_conn() is all done.
5001 *
5002 * Curl_setup_conn() also handles reused connections
5003 *
5004 * conn->data MUST already have been setup fine (in create_conn)
5005 */
5006
5007CURLcode Curl_setup_conn(struct connectdata *conn,
5008                         bool *protocol_done)
5009{
5010  CURLcode result=CURLE_OK;
5011  struct SessionHandle *data = conn->data;
5012
5013  Curl_pgrsTime(data, TIMER_NAMELOOKUP);
5014
5015  if(conn->handler->flags & PROTOPT_NONETWORK) {
5016    /* nothing to setup when not using a network */
5017    *protocol_done = TRUE;
5018    return result;
5019  }
5020  *protocol_done = FALSE; /* default to not done */
5021
5022  /* set proxy_connect_closed to false unconditionally already here since it
5023     is used strictly to provide extra information to a parent function in the
5024     case of proxy CONNECT failures and we must make sure we don't have it
5025     lingering set from a previous invoke */
5026  conn->bits.proxy_connect_closed = FALSE;
5027
5028  /*
5029   * Set user-agent. Used for HTTP, but since we can attempt to tunnel
5030   * basically anything through a http proxy we can't limit this based on
5031   * protocol.
5032   */
5033  if(data->set.str[STRING_USERAGENT]) {
5034    Curl_safefree(conn->allocptr.uagent);
5035    conn->allocptr.uagent =
5036      aprintf("User-Agent: %s\r\n", data->set.str[STRING_USERAGENT]);
5037    if(!conn->allocptr.uagent)
5038      return CURLE_OUT_OF_MEMORY;
5039  }
5040
5041  data->req.headerbytecount = 0;
5042
5043#ifdef CURL_DO_LINEEND_CONV
5044  data->state.crlf_conversions = 0; /* reset CRLF conversion counter */
5045#endif /* CURL_DO_LINEEND_CONV */
5046
5047  for(;;) {
5048    /* loop for CURL_SERVER_CLOSED_CONNECTION */
5049
5050    if(CURL_SOCKET_BAD == conn->sock[FIRSTSOCKET]) {
5051      /* Try to connect only if not already connected */
5052      bool connected = FALSE;
5053
5054      result = ConnectPlease(data, conn, &connected);
5055
5056      if(connected) {
5057        result = Curl_protocol_connect(conn, protocol_done);
5058        if(CURLE_OK == result)
5059          conn->bits.tcpconnect = TRUE;
5060      }
5061      else
5062        conn->bits.tcpconnect = FALSE;
5063
5064      /* if the connection was closed by the server while exchanging
5065         authentication informations, retry with the new set
5066         authentication information */
5067      if(conn->bits.proxy_connect_closed) {
5068        /* reset the error buffer */
5069        if(data->set.errorbuffer)
5070          data->set.errorbuffer[0] = '\0';
5071        data->state.errorbuf = FALSE;
5072        continue;
5073      }
5074
5075      if(CURLE_OK != result)
5076        return result;
5077    }
5078    else {
5079      Curl_pgrsTime(data, TIMER_CONNECT); /* we're connected already */
5080      Curl_pgrsTime(data, TIMER_APPCONNECT); /* we're connected already */
5081      conn->bits.tcpconnect = TRUE;
5082      *protocol_done = TRUE;
5083      Curl_verboseconnect(conn);
5084      Curl_updateconninfo(conn, conn->sock[FIRSTSOCKET]);
5085    }
5086    /* Stop the loop now */
5087    break;
5088  }
5089
5090  conn->now = Curl_tvnow(); /* time this *after* the connect is done, we
5091                               set this here perhaps a second time */
5092
5093#ifdef __EMX__
5094  /*
5095   * This check is quite a hack. We're calling _fsetmode to fix the problem
5096   * with fwrite converting newline characters (you get mangled text files,
5097   * and corrupted binary files when you download to stdout and redirect it to
5098   * a file).
5099   */
5100
5101  if((data->set.out)->_handle == NULL) {
5102    _fsetmode(stdout, "b");
5103  }
5104#endif
5105
5106  return result;
5107}
5108
5109CURLcode Curl_connect(struct SessionHandle *data,
5110                      struct connectdata **in_connect,
5111                      bool *asyncp,
5112                      bool *protocol_done)
5113{
5114  CURLcode code;
5115
5116  *asyncp = FALSE; /* assume synchronous resolves by default */
5117
5118  /* call the stuff that needs to be called */
5119  code = create_conn(data, in_connect, asyncp);
5120
5121  if(CURLE_OK == code) {
5122    /* no error */
5123    if((*in_connect)->send_pipe->size || (*in_connect)->recv_pipe->size)
5124      /* pipelining */
5125      *protocol_done = TRUE;
5126    else if(!*asyncp) {
5127      /* DNS resolution is done: that's either because this is a reused
5128         connection, in which case DNS was unnecessary, or because DNS
5129         really did finish already (synch resolver/fast async resolve) */
5130      code = Curl_setup_conn(*in_connect, protocol_done);
5131    }
5132  }
5133
5134  if(code && *in_connect) {
5135    /* We're not allowed to return failure with memory left allocated
5136       in the connectdata struct, free those here */
5137    Curl_disconnect(*in_connect, FALSE); /* close the connection */
5138    *in_connect = NULL;           /* return a NULL */
5139  }
5140
5141  return code;
5142}
5143
5144CURLcode Curl_done(struct connectdata **connp,
5145                   CURLcode status,  /* an error if this is called after an
5146                                        error was detected */
5147                   bool premature)
5148{
5149  CURLcode result;
5150  struct connectdata *conn;
5151  struct SessionHandle *data;
5152
5153  DEBUGASSERT(*connp);
5154
5155  conn = *connp;
5156  data = conn->data;
5157
5158  if(conn->bits.done)
5159    /* Stop if Curl_done() has already been called */
5160    return CURLE_OK;
5161
5162  Curl_getoff_all_pipelines(data, conn);
5163
5164  if((conn->send_pipe->size + conn->recv_pipe->size != 0 &&
5165      !data->set.reuse_forbid &&
5166      !conn->bits.close))
5167    /* Stop if pipeline is not empty and we do not have to close
5168       connection. */
5169    return CURLE_OK;
5170
5171  conn->bits.done = TRUE; /* called just now! */
5172
5173  /* Cleanup possible redirect junk */
5174  if(data->req.newurl) {
5175    free(data->req.newurl);
5176    data->req.newurl = NULL;
5177  }
5178  if(data->req.location) {
5179    free(data->req.location);
5180    data->req.location = NULL;
5181  }
5182
5183  Curl_resolver_cancel(conn);
5184
5185  if(conn->dns_entry) {
5186    Curl_resolv_unlock(data, conn->dns_entry); /* done with this */
5187    conn->dns_entry = NULL;
5188  }
5189
5190  /* this calls the protocol-specific function pointer previously set */
5191  if(conn->handler->done)
5192    result = conn->handler->done(conn, status, premature);
5193  else
5194    result = CURLE_OK;
5195
5196  Curl_pgrsDone(conn); /* done with the operation */
5197
5198  /* if the transfer was completed in a paused state there can be buffered
5199     data left to write and then kill */
5200  if(data->state.tempwrite) {
5201    free(data->state.tempwrite);
5202    data->state.tempwrite = NULL;
5203  }
5204
5205  /* if data->set.reuse_forbid is TRUE, it means the libcurl client has
5206     forced us to close this no matter what we think.
5207
5208     if conn->bits.close is TRUE, it means that the connection should be
5209     closed in spite of all our efforts to be nice, due to protocol
5210     restrictions in our or the server's end
5211
5212     if premature is TRUE, it means this connection was said to be DONE before
5213     the entire request operation is complete and thus we can't know in what
5214     state it is for re-using, so we're forced to close it. In a perfect world
5215     we can add code that keep track of if we really must close it here or not,
5216     but currently we have no such detail knowledge.
5217
5218     connectindex == -1 here means that the connection has no spot in the
5219     connection cache and thus we must disconnect it here.
5220  */
5221  if(data->set.reuse_forbid || conn->bits.close || premature ||
5222     (-1 == conn->connectindex)) {
5223    CURLcode res2 = Curl_disconnect(conn, premature); /* close connection */
5224
5225    /* If we had an error already, make sure we return that one. But
5226       if we got a new error, return that. */
5227    if(!result && res2)
5228      result = res2;
5229  }
5230  else {
5231    ConnectionDone(conn); /* the connection is no longer in use */
5232
5233    /* remember the most recently used connection */
5234    data->state.lastconnect = conn->connectindex;
5235
5236    infof(data, "Connection #%ld to host %s left intact\n",
5237          conn->connectindex,
5238          conn->bits.httpproxy?conn->proxy.dispname:conn->host.dispname);
5239  }
5240
5241  *connp = NULL; /* to make the caller of this function better detect that
5242                    this was either closed or handed over to the connection
5243                    cache here, and therefore cannot be used from this point on
5244                 */
5245
5246  return result;
5247}
5248
5249/*
5250 * do_init() inits the readwrite session. This is inited each time (in the DO
5251 * function before the protocol-specific DO functions are invoked) for a
5252 * transfer, sometimes multiple times on the same SessionHandle. Make sure
5253 * nothing in here depends on stuff that are setup dynamically for the
5254 * transfer.
5255 */
5256
5257static CURLcode do_init(struct connectdata *conn)
5258{
5259  struct SessionHandle *data = conn->data;
5260  struct SingleRequest *k = &data->req;
5261
5262  conn->bits.done = FALSE; /* Curl_done() is not called yet */
5263  conn->bits.do_more = FALSE; /* by default there's no curl_do_more() to use */
5264  data->state.expect100header = FALSE;
5265
5266  if(data->set.opt_no_body)
5267    /* in HTTP lingo, no body means using the HEAD request... */
5268    data->set.httpreq = HTTPREQ_HEAD;
5269  else if(HTTPREQ_HEAD == data->set.httpreq)
5270    /* ... but if unset there really is no perfect method that is the
5271       "opposite" of HEAD but in reality most people probably think GET
5272       then. The important thing is that we can't let it remain HEAD if the
5273       opt_no_body is set FALSE since then we'll behave wrong when getting
5274       HTTP. */
5275    data->set.httpreq = HTTPREQ_GET;
5276
5277  /* NB: the content encoding software depends on this initialization */
5278  Curl_easy_initHandleData(data);
5279
5280  k->start = Curl_tvnow(); /* start time */
5281  k->now = k->start;   /* current time is now */
5282  k->header = TRUE; /* assume header */
5283
5284  k->bytecount = 0;
5285
5286  k->buf = data->state.buffer;
5287  k->uploadbuf = data->state.uploadbuffer;
5288  k->hbufp = data->state.headerbuff;
5289  k->ignorebody=FALSE;
5290
5291  Curl_pgrsTime(data, TIMER_PRETRANSFER);
5292  Curl_speedinit(data);
5293
5294  Curl_pgrsSetUploadCounter(data, 0);
5295  Curl_pgrsSetDownloadCounter(data, 0);
5296
5297  return CURLE_OK;
5298}
5299
5300/*
5301 * do_complete is called when the DO actions are complete.
5302 *
5303 * We init chunking and trailer bits to their default values here immediately
5304 * before receiving any header data for the current request in the pipeline.
5305 */
5306static void do_complete(struct connectdata *conn)
5307{
5308  conn->data->req.chunk=FALSE;
5309  conn->data->req.maxfd = (conn->sockfd>conn->writesockfd?
5310                           conn->sockfd:conn->writesockfd)+1;
5311}
5312
5313CURLcode Curl_do(struct connectdata **connp, bool *done)
5314{
5315  CURLcode result=CURLE_OK;
5316  struct connectdata *conn = *connp;
5317  struct SessionHandle *data = conn->data;
5318
5319  /* setup and init stuff before DO starts, in preparing for the transfer */
5320  do_init(conn);
5321
5322  if(conn->handler->do_it) {
5323    /* generic protocol-specific function pointer set in curl_connect() */
5324    result = conn->handler->do_it(conn, done);
5325
5326    /* This was formerly done in transfer.c, but we better do it here */
5327    if((CURLE_SEND_ERROR == result) && conn->bits.reuse) {
5328      /*
5329       * If the connection is using an easy handle, call reconnect
5330       * to re-establish the connection.  Otherwise, let the multi logic
5331       * figure out how to re-establish the connection.
5332       */
5333      if(!data->multi) {
5334        result = Curl_reconnect_request(connp);
5335
5336        if(result == CURLE_OK) {
5337          /* ... finally back to actually retry the DO phase */
5338          conn = *connp; /* re-assign conn since Curl_reconnect_request
5339                            creates a new connection */
5340          result = conn->handler->do_it(conn, done);
5341        }
5342      }
5343      else
5344        return result;
5345    }
5346
5347    if((result == CURLE_OK) && *done)
5348      /* do_complete must be called after the protocol-specific DO function */
5349      do_complete(conn);
5350  }
5351  return result;
5352}
5353
5354CURLcode Curl_do_more(struct connectdata *conn)
5355{
5356  CURLcode result=CURLE_OK;
5357
5358  if(conn->handler->do_more)
5359    result = conn->handler->do_more(conn);
5360
5361  if(result == CURLE_OK)
5362    /* do_complete must be called after the protocol-specific DO function */
5363    do_complete(conn);
5364
5365  return result;
5366}
5367
5368/* Called on connect, and if there's already a protocol-specific struct
5369   allocated for a different connection, this frees it that it can be setup
5370   properly later on. */
5371void Curl_reset_reqproto(struct connectdata *conn)
5372{
5373  struct SessionHandle *data = conn->data;
5374  if(data->state.proto.generic && data->state.current_conn != conn) {
5375    free(data->state.proto.generic);
5376    data->state.proto.generic = NULL;
5377  }
5378  data->state.current_conn = conn;
5379}
5380