quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

resolve.c (3348B)


      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 #include "first.h"
     25 
     26 /* Purpose
     27  *
     28  * Resolve the given name, using system name resolve functions (NOT any
     29  * function provided by libcurl). Used to see if the name exists and thus if
     30  * we can allow a test case to use it for testing.
     31  *
     32  * Like if 'localhost' actual exists etc.
     33  *
     34  */
     35 
     36 static int test_resolve(int argc, char *argv[])
     37 {
     38   int arg = 1;
     39   const char *host = NULL;
     40   int rc = 0;
     41 
     42   while(argc > arg) {
     43     if(!strcmp("--version", argv[arg])) {
     44       printf("resolve IPv4%s\n",
     45 #if defined(CURLRES_IPV6)
     46              "/IPv6"
     47 #else
     48              ""
     49 #endif
     50              );
     51       return 0;
     52     }
     53     else if(!strcmp("--ipv6", argv[arg])) {
     54 #if defined(CURLRES_IPV6)
     55       ipv_inuse = "IPv6";
     56       use_ipv6 = TRUE;
     57       arg++;
     58 #else
     59       puts("IPv6 support has been disabled in this program");
     60       return 1;
     61 #endif
     62     }
     63     else if(!strcmp("--ipv4", argv[arg])) {
     64       /* for completeness, we support this option as well */
     65       ipv_inuse = "IPv4";
     66 #if defined(CURLRES_IPV6)
     67       use_ipv6 = FALSE;
     68 #endif
     69       arg++;
     70     }
     71     else {
     72       host = argv[arg++];
     73     }
     74   }
     75   if(!host) {
     76     puts("Usage: resolve [option] <host>\n"
     77          " --version\n"
     78          " --ipv4"
     79 #if defined(CURLRES_IPV6)
     80          "\n --ipv6"
     81 #endif
     82          );
     83     return 1;
     84   }
     85 
     86 #ifdef _WIN32
     87   if(win32_init())
     88     return 2;
     89 #endif
     90 
     91 #if defined(CURLRES_IPV6)
     92   if(use_ipv6) {
     93     /* Check that the system has IPv6 enabled before checking the resolver */
     94     curl_socket_t s = socket(PF_INET6, SOCK_DGRAM, 0);
     95     if(s == CURL_SOCKET_BAD)
     96       /* an IPv6 address was requested and we can't get/use one */
     97       rc = -1;
     98     else {
     99       sclose(s);
    100     }
    101   }
    102 
    103   if(rc == 0) {
    104     /* getaddrinfo() resolve */
    105     struct addrinfo *ai;
    106     struct addrinfo hints;
    107 
    108     memset(&hints, 0, sizeof(hints));
    109     hints.ai_family = use_ipv6 ? PF_INET6 : PF_INET;
    110     hints.ai_socktype = SOCK_STREAM;
    111     hints.ai_flags = 0;
    112     rc = getaddrinfo(host, "80", &hints, &ai);
    113     if(rc == 0)
    114       freeaddrinfo(ai);
    115   }
    116 #else
    117   {
    118     struct hostent *he;  /* gethostbyname() resolve */
    119 
    120 #ifdef __AMIGA__
    121     he = gethostbyname((unsigned char *)CURL_UNCONST(host));
    122 #else
    123     he = gethostbyname(host);
    124 #endif
    125 
    126     rc = !he;
    127   }
    128 #endif
    129 
    130   if(rc)
    131     printf("Resolving %s '%s' didn't work\n", ipv_inuse, host);
    132 
    133   return !!rc;
    134 }