lib2082.c (3146B)
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 "first.h" 25 26 struct prcs { 27 int prereq_retcode; 28 int ipv6; 29 }; 30 31 static int prereq_callback(void *clientp, 32 char *conn_primary_ip, 33 char *conn_local_ip, 34 int conn_primary_port, 35 int conn_local_port) 36 { 37 struct prcs *prereq_cb = (struct prcs *)clientp; 38 39 if(prereq_cb->ipv6) { 40 curl_mprintf("Connected to [%s]\n", conn_primary_ip); 41 curl_mprintf("Connected from [%s]\n", conn_local_ip); 42 } 43 else { 44 curl_mprintf("Connected to %s\n", conn_primary_ip); 45 curl_mprintf("Connected from %s\n", conn_local_ip); 46 } 47 48 curl_mprintf("Remote port = %d\n", conn_primary_port); 49 curl_mprintf("Local port = %d\n", conn_local_port); 50 curl_mprintf("Returning = %d\n", prereq_cb->prereq_retcode); 51 return prereq_cb->prereq_retcode; 52 } 53 54 static CURLcode test_lib2082(char *URL) /* libprereq */ 55 { 56 struct prcs prereq_cb; 57 CURLcode ret = CURLE_OK; 58 CURL *curl = NULL; 59 60 prereq_cb.prereq_retcode = CURL_PREREQFUNC_OK; 61 prereq_cb.ipv6 = 0; 62 63 curl_global_init(CURL_GLOBAL_ALL); 64 curl = curl_easy_init(); 65 66 if(curl) { 67 if(strstr(URL, "#ipv6")) { 68 /* The IP addresses should be surrounded by brackets! */ 69 prereq_cb.ipv6 = 1; 70 } 71 if(strstr(URL, "#err")) { 72 /* Set the callback to exit with failure */ 73 prereq_cb.prereq_retcode = CURL_PREREQFUNC_ABORT; 74 } 75 76 curl_easy_setopt(curl, CURLOPT_URL, URL); 77 curl_easy_setopt(curl, CURLOPT_PREREQFUNCTION, prereq_callback); 78 curl_easy_setopt(curl, CURLOPT_PREREQDATA, &prereq_cb); 79 curl_easy_setopt(curl, CURLOPT_WRITEDATA, stderr); 80 81 if(strstr(URL, "#redir")) { 82 /* Enable follow-location */ 83 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); 84 } 85 86 ret = curl_easy_perform(curl); 87 if(ret) { 88 curl_mfprintf(stderr, 89 "%s:%d curl_easy_perform() failed with code %d (%s)\n", 90 __FILE__, __LINE__, ret, curl_easy_strerror(ret)); 91 goto test_cleanup; 92 } 93 } 94 95 test_cleanup: 96 curl_easy_cleanup(curl); 97 curl_global_cleanup(); 98 99 return ret; 100 }