connect-to.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. int main(void)
  31. {
  32. CURL *curl;
  33. CURLcode res = CURLE_OK;
  34. /*
  35. Each single string should be written using the format
  36. HOST:PORT:CONNECT-TO-HOST:CONNECT-TO-PORT where HOST is the host of the
  37. request, PORT is the port of the request, CONNECT-TO-HOST is the host name
  38. to connect to, and CONNECT-TO-PORT is the port to connect to.
  39. */
  40. /* instead of curl.se:443, it resolves and uses example.com:443 but in other
  41. aspects work as if it still is curl.se */
  42. struct curl_slist *host = curl_slist_append(NULL,
  43. "curl.se:443:example.com:443");
  44. curl = curl_easy_init();
  45. if(curl) {
  46. curl_easy_setopt(curl, CURLOPT_CONNECT_TO, host);
  47. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  48. curl_easy_setopt(curl, CURLOPT_URL, "https://curl.se/");
  49. /* since this connects to the wrong host, checking the host name in the
  50. server certificate fails, so unless we disable the check libcurl
  51. returns CURLE_PEER_FAILED_VERIFICATION */
  52. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
  53. /* Letting the wrong host name in the certificate be okay, the transfer
  54. goes through but (most likely) causes a 404 or similar because it sends
  55. an unknown name in the Host: header field */
  56. res = curl_easy_perform(curl);
  57. /* always cleanup */
  58. curl_easy_cleanup(curl);
  59. }
  60. curl_slist_free_all(host);
  61. return (int)res;
  62. }