tor-browser

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

str2addr.c (1366B)


      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: str2addr.c
      8 * Description: a test for PR_StringToNetAddr
      9 */
     10 
     11 #include "nspr.h"
     12 
     13 #include <stdio.h>
     14 #include <stdlib.h>
     15 
     16 /* Address string to convert */
     17 #define DEFAULT_IPV4_ADDR_STR "207.200.73.41"
     18 
     19 /* Expected conversion result, in network byte order */
     20 static unsigned char default_ipv4_addr[] = {207, 200, 73, 41};
     21 
     22 int main(int argc, char** argv) {
     23  PRNetAddr addr;
     24  const char* addrStr;
     25  unsigned char* bytes;
     26  int idx;
     27 
     28  addrStr = DEFAULT_IPV4_ADDR_STR;
     29  if (PR_StringToNetAddr(addrStr, &addr) == PR_FAILURE) {
     30    fprintf(stderr, "PR_StringToNetAddr failed\n");
     31    exit(1);
     32  }
     33  if (addr.inet.family != PR_AF_INET) {
     34    fprintf(stderr, "addr.inet.family should be %d but is %d\n", PR_AF_INET,
     35            addr.inet.family);
     36    exit(1);
     37  }
     38  bytes = (unsigned char*)&addr.inet.ip;
     39  for (idx = 0; idx < 4; idx++) {
     40    if (bytes[idx] != default_ipv4_addr[idx]) {
     41      fprintf(stderr, "byte %d of IPv4 addr should be %d but is %d\n", idx,
     42              default_ipv4_addr[idx], bytes[idx]);
     43      exit(1);
     44    }
     45  }
     46 
     47  printf("PASS\n");
     48  return 0;
     49 }