nsprpub/pr/tests/gethost.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial