xref: /curl/docs/examples/connect-to.c (revision f540e43b)
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 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 https://curl.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  * SPDX-License-Identifier: curl
22  *
23  ***************************************************************************/
24 /* <DESC>
25  * Use CURLOPT_CONNECT_TO to connect to "wrong" hostname
26  * </DESC>
27  */
28 #include <stdio.h>
29 #include <curl/curl.h>
30 
main(void)31 int main(void)
32 {
33   CURL *curl;
34   CURLcode res = CURLE_OK;
35 
36   /*
37     Each single string should be written using the format
38     HOST:PORT:CONNECT-TO-HOST:CONNECT-TO-PORT where HOST is the host of the
39     request, PORT is the port of the request, CONNECT-TO-HOST is the host name
40     to connect to, and CONNECT-TO-PORT is the port to connect to.
41    */
42   /* instead of curl.se:443, it resolves and uses example.com:443 but in other
43      aspects work as if it still is curl.se */
44   struct curl_slist *host = curl_slist_append(NULL,
45                                               "curl.se:443:example.com:443");
46 
47   curl = curl_easy_init();
48   if(curl) {
49     curl_easy_setopt(curl, CURLOPT_CONNECT_TO, host);
50     curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
51     curl_easy_setopt(curl, CURLOPT_URL, "https://curl.se/");
52 
53     /* since this connects to the wrong host, checking the host name in the
54        server certificate fails, so unless we disable the check libcurl
55        returns CURLE_PEER_FAILED_VERIFICATION */
56     curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
57 
58     /* Letting the wrong host name in the certificate be okay, the transfer
59        goes through but (most likely) causes a 404 or similar because it sends
60        an unknown name in the Host: header field */
61     res = curl_easy_perform(curl);
62 
63     /* always cleanup */
64     curl_easy_cleanup(curl);
65   }
66 
67   curl_slist_free_all(host);
68 
69   return (int)res;
70 }
71