libprereq.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Max Dymond, <max.dymond@microsoft.com>
  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. #include "test.h"
  25. typedef struct prcs {
  26. int prereq_retcode;
  27. int ipv6;
  28. } PRCS;
  29. static int prereq_callback(void *clientp,
  30. char *conn_primary_ip,
  31. char *conn_local_ip,
  32. int conn_primary_port,
  33. int conn_local_port)
  34. {
  35. PRCS *prereq_cb = (PRCS *)clientp;
  36. if(prereq_cb->ipv6) {
  37. printf("Connected to [%s]\n", conn_primary_ip);
  38. printf("Connected from [%s]\n", conn_local_ip);
  39. }
  40. else {
  41. printf("Connected to %s\n", conn_primary_ip);
  42. printf("Connected from %s\n", conn_local_ip);
  43. }
  44. printf("Remote port = %d\n", conn_primary_port);
  45. printf("Local port = %d\n", conn_local_port);
  46. printf("Returning = %d\n", prereq_cb->prereq_retcode);
  47. return prereq_cb->prereq_retcode;
  48. }
  49. int test(char *URL)
  50. {
  51. PRCS prereq_cb;
  52. CURLcode ret = CURLE_OK;
  53. CURL *curl = NULL;
  54. prereq_cb.prereq_retcode = CURL_PREREQFUNC_OK;
  55. prereq_cb.ipv6 = 0;
  56. curl_global_init(CURL_GLOBAL_ALL);
  57. curl = curl_easy_init();
  58. if(curl) {
  59. if(strstr(URL, "#ipv6")) {
  60. /* The IP addresses should be surrounded by brackets! */
  61. prereq_cb.ipv6 = 1;
  62. }
  63. if(strstr(URL, "#err")) {
  64. /* Set the callback to exit with failure */
  65. prereq_cb.prereq_retcode = CURL_PREREQFUNC_ABORT;
  66. }
  67. curl_easy_setopt(curl, CURLOPT_URL, URL);
  68. curl_easy_setopt(curl, CURLOPT_PREREQFUNCTION, prereq_callback);
  69. curl_easy_setopt(curl, CURLOPT_PREREQDATA, &prereq_cb);
  70. curl_easy_setopt(curl, CURLOPT_WRITEDATA, stderr);
  71. if(strstr(URL, "#redir")) {
  72. /* Enable follow-location */
  73. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
  74. }
  75. ret = curl_easy_perform(curl);
  76. if(ret) {
  77. fprintf(stderr, "%s:%d curl_easy_perform() failed with code %d (%s)\n",
  78. __FILE__, __LINE__, ret, curl_easy_strerror(ret));
  79. goto test_cleanup;
  80. }
  81. }
  82. test_cleanup:
  83. curl_easy_cleanup(curl);
  84. curl_global_cleanup();
  85. return ret;
  86. }