tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

gethost.c (7552B)


      1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 /*
      7 * File: gethost.c
      8 *
      9 * Description: tests various functions in prnetdb.h
     10 *
     11 * Usage: gethost [-6] [hostname]
     12 */
     13 
     14 #include "prio.h"
     15 #include "prnetdb.h"
     16 #include "plgetopt.h"
     17 
     18 #include <stdio.h>
     19 #include <stdlib.h>
     20 
     21 #define DEFAULT_HOST_NAME "mozilla.org"
     22 
     23 static void Help(void) {
     24  fprintf(stderr, "Usage: gethost [-h] [hostname]\n");
     25  fprintf(stderr, "\t-h          help\n");
     26  fprintf(stderr, "\thostname    Name of host    (default: %s)\n",
     27          DEFAULT_HOST_NAME);
     28 } /* Help */
     29 
     30 /*
     31 * Prints the contents of a PRHostEnt structure
     32 */
     33 void PrintHostent(const PRHostEnt* he) {
     34  int i;
     35  int j;
     36 
     37  printf("h_name: %s\n", he->h_name);
     38  for (i = 0; he->h_aliases[i]; i++) {
     39    printf("h_aliases[%d]: %s\n", i, he->h_aliases[i]);
     40  }
     41  printf("h_addrtype: %d\n", he->h_addrtype);
     42  printf("h_length: %d\n", he->h_length);
     43  for (i = 0; he->h_addr_list[i]; i++) {
     44    printf("h_addr_list[%d]: ", i);
     45    for (j = 0; j < he->h_length; j++) {
     46      if (j != 0) {
     47        printf(".");
     48      }
     49      printf("%u", (unsigned char)he->h_addr_list[i][j]);
     50    }
     51    printf("\n");
     52  }
     53 }
     54 
     55 int main(int argc, char** argv) {
     56  const char* hostName = DEFAULT_HOST_NAME;
     57  PRHostEnt he, reversehe;
     58  char buf[PR_NETDB_BUF_SIZE];
     59  char reversebuf[PR_NETDB_BUF_SIZE];
     60  PRIntn idx;
     61  PRNetAddr addr;
     62  PLOptStatus os;
     63  PLOptState* opt = PL_CreateOptState(argc, argv, "h");
     64 
     65  while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
     66    if (PL_OPT_BAD == os) {
     67      continue;
     68    }
     69    switch (opt->option) {
     70      case 0: /* naked */
     71        hostName = opt->value;
     72        break;
     73      case 'h': /* Help message */
     74      default:
     75        Help();
     76        return 2;
     77    }
     78  }
     79  PL_DestroyOptState(opt);
     80 
     81  if (PR_GetHostByName(hostName, buf, sizeof(buf), &he) == PR_FAILURE) {
     82    fprintf(stderr, "PR_GetHostByName failed\n");
     83    exit(1);
     84  }
     85  PrintHostent(&he);
     86  idx = 0;
     87  while (1) {
     88    idx = PR_EnumerateHostEnt(idx, &he, 0, &addr);
     89    if (idx == -1) {
     90      fprintf(stderr, "PR_EnumerateHostEnt failed\n");
     91      exit(1);
     92    }
     93    if (idx == 0) {
     94      break; /* normal loop termination */
     95    }
     96    printf("reverse lookup\n");
     97    if (PR_GetHostByAddr(&addr, reversebuf, sizeof(reversebuf), &reversehe) ==
     98        PR_FAILURE) {
     99      fprintf(stderr, "PR_GetHostByAddr failed\n");
    100      exit(1);
    101    }
    102    PrintHostent(&reversehe);
    103  }
    104 
    105  printf("PR_GetIPNodeByName with PR_AF_INET\n");
    106  if (PR_GetIPNodeByName(hostName, PR_AF_INET, PR_AI_DEFAULT, buf, sizeof(buf),
    107                         &he) == PR_FAILURE) {
    108    fprintf(stderr, "PR_GetIPNodeByName failed\n");
    109    exit(1);
    110  }
    111  PrintHostent(&he);
    112  printf("PR_GetIPNodeByName with PR_AF_INET6\n");
    113  if (PR_GetIPNodeByName(hostName, PR_AF_INET6, PR_AI_DEFAULT, buf, sizeof(buf),
    114                         &he) == PR_FAILURE) {
    115    fprintf(stderr, "PR_GetIPNodeByName failed\n");
    116    exit(1);
    117  }
    118  PrintHostent(&he);
    119  idx = 0;
    120  printf("PR_GetHostByAddr with PR_AF_INET6\n");
    121  while (1) {
    122    idx = PR_EnumerateHostEnt(idx, &he, 0, &addr);
    123    if (idx == -1) {
    124      fprintf(stderr, "PR_EnumerateHostEnt failed\n");
    125      exit(1);
    126    }
    127    if (idx == 0) {
    128      break; /* normal loop termination */
    129    }
    130    printf("reverse lookup\n");
    131    if (PR_GetHostByAddr(&addr, reversebuf, sizeof(reversebuf), &reversehe) ==
    132        PR_FAILURE) {
    133      fprintf(stderr, "PR_GetHostByAddr failed\n");
    134      exit(1);
    135    }
    136    PrintHostent(&reversehe);
    137  }
    138  printf("PR_GetHostByAddr with PR_AF_INET6 done\n");
    139 
    140  PR_StringToNetAddr("::1", &addr);
    141  if (PR_IsNetAddrType(&addr, PR_IpAddrV4Mapped) == PR_TRUE) {
    142    fprintf(stderr, "addr should not be ipv4 mapped address\n");
    143    exit(1);
    144  }
    145  if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
    146    fprintf(stderr, "addr should be loopback address\n");
    147    exit(1);
    148  }
    149 
    150  PR_StringToNetAddr("127.0.0.1", &addr);
    151  if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
    152    fprintf(stderr, "addr should be loopback address\n");
    153    exit(1);
    154  }
    155  PR_StringToNetAddr("::FFFF:127.0.0.1", &addr);
    156  if (PR_IsNetAddrType(&addr, PR_IpAddrV4Mapped) == PR_FALSE) {
    157    fprintf(stderr, "addr should be ipv4 mapped address\n");
    158    exit(1);
    159  }
    160  if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
    161    fprintf(stderr, "addr should be loopback address\n");
    162    exit(1);
    163  }
    164 
    165  if (PR_InitializeNetAddr(PR_IpAddrAny, 0, &addr) == PR_FAILURE) {
    166    fprintf(stderr, "PR_InitializeNetAddr failed\n");
    167    exit(1);
    168  }
    169  if (PR_IsNetAddrType(&addr, PR_IpAddrAny) == PR_FALSE) {
    170    fprintf(stderr, "addr should be unspecified address\n");
    171    exit(1);
    172  }
    173  if (PR_InitializeNetAddr(PR_IpAddrLoopback, 0, &addr) == PR_FAILURE) {
    174    fprintf(stderr, "PR_InitializeNetAddr failed\n");
    175    exit(1);
    176  }
    177  if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
    178    fprintf(stderr, "addr should be loopback address\n");
    179    exit(1);
    180  }
    181 
    182  if (PR_SetNetAddr(PR_IpAddrAny, PR_AF_INET, 0, &addr) == PR_FAILURE) {
    183    fprintf(stderr, "PR_SetNetAddr failed\n");
    184    exit(1);
    185  }
    186  if (PR_IsNetAddrType(&addr, PR_IpAddrAny) == PR_FALSE) {
    187    fprintf(stderr, "addr should be unspecified address\n");
    188    exit(1);
    189  }
    190  if (PR_SetNetAddr(PR_IpAddrLoopback, PR_AF_INET, 0, &addr) == PR_FAILURE) {
    191    fprintf(stderr, "PR_SetNetAddr failed\n");
    192    exit(1);
    193  }
    194  if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
    195    fprintf(stderr, "addr should be loopback address\n");
    196    exit(1);
    197  }
    198 
    199  addr.inet.family = PR_AF_INET;
    200  addr.inet.port = 0;
    201  addr.inet.ip = PR_htonl(PR_INADDR_ANY);
    202  if (PR_IsNetAddrType(&addr, PR_IpAddrAny) == PR_FALSE) {
    203    fprintf(stderr, "addr should be unspecified address\n");
    204    exit(1);
    205  }
    206  {
    207    char buf[256];
    208    PR_NetAddrToString(&addr, buf, 256);
    209    printf("IPv4 INADDRANY: %s\n", buf);
    210  }
    211  addr.inet.family = PR_AF_INET;
    212  addr.inet.port = 0;
    213  addr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK);
    214  if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
    215    fprintf(stderr, "addr should be loopback address\n");
    216    exit(1);
    217  }
    218  {
    219    char buf[256];
    220    PR_NetAddrToString(&addr, buf, 256);
    221    printf("IPv4 LOOPBACK: %s\n", buf);
    222  }
    223 
    224  if (PR_SetNetAddr(PR_IpAddrAny, PR_AF_INET6, 0, &addr) == PR_FAILURE) {
    225    fprintf(stderr, "PR_SetNetAddr failed\n");
    226    exit(1);
    227  }
    228  if (PR_IsNetAddrType(&addr, PR_IpAddrAny) == PR_FALSE) {
    229    fprintf(stderr, "addr should be unspecified address\n");
    230    exit(1);
    231  }
    232  {
    233    char buf[256];
    234    PR_NetAddrToString(&addr, buf, 256);
    235    printf("IPv6 INADDRANY: %s\n", buf);
    236  }
    237  if (PR_SetNetAddr(PR_IpAddrLoopback, PR_AF_INET6, 0, &addr) == PR_FAILURE) {
    238    fprintf(stderr, "PR_SetNetAddr failed\n");
    239    exit(1);
    240  }
    241  if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
    242    fprintf(stderr, "addr should be loopback address\n");
    243    exit(1);
    244  }
    245  {
    246    char buf[256];
    247    PR_NetAddrToString(&addr, buf, 256);
    248    printf("IPv6 LOOPBACK: %s\n", buf);
    249  }
    250  {
    251    PRIPv6Addr v6addr;
    252    char tmp_buf[256];
    253 
    254    PR_SetNetAddr(PR_IpAddrLoopback, PR_AF_INET, 0, &addr);
    255 
    256    PR_ConvertIPv4AddrToIPv6(addr.inet.ip, &v6addr);
    257    PR_SetNetAddr(PR_IpAddrAny, PR_AF_INET6, 0, &addr);
    258    addr.ipv6.ip = v6addr;
    259    PR_NetAddrToString(&addr, tmp_buf, 256);
    260    printf("IPv4-mapped IPv6 LOOPBACK: %s\n", tmp_buf);
    261  }
    262  printf("PASS\n");
    263  return 0;
    264 }