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