2
0

libprereq.c 2.9 KB

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