Bumping manifests a=b2g-bump
[gecko.git] / nsprpub / pr / tests / gethost.c
blob779bce45e41877e7fd2c3ab2a4e7a13118333c91
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
9 * Description: tests various functions in prnetdb.h
11 * Usage: gethost [-6] [hostname]
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)
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 */
32 * Prints the contents of a PRHostEnt structure
34 void PrintHostent(const PRHostEnt *he)
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]);
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]);
51 printf("\n");
55 int main(int argc, char **argv)
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;
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);
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);
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);
99 PrintHostent(&reversehe);
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);
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);
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);
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);
131 PrintHostent(&reversehe);
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);
140 if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
141 fprintf(stderr, "addr should be loopback address\n");
142 exit(1);
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);
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);
155 if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
156 fprintf(stderr, "addr should be loopback address\n");
157 exit(1);
160 if (PR_InitializeNetAddr(PR_IpAddrAny, 0, &addr) == PR_FAILURE) {
161 fprintf(stderr, "PR_InitializeNetAddr failed\n");
162 exit(1);
164 if (PR_IsNetAddrType(&addr, PR_IpAddrAny) == PR_FALSE) {
165 fprintf(stderr, "addr should be unspecified address\n");
166 exit(1);
168 if (PR_InitializeNetAddr(PR_IpAddrLoopback, 0, &addr) == PR_FAILURE) {
169 fprintf(stderr, "PR_InitializeNetAddr failed\n");
170 exit(1);
172 if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
173 fprintf(stderr, "addr should be loopback address\n");
174 exit(1);
177 if (PR_SetNetAddr(PR_IpAddrAny, PR_AF_INET, 0, &addr) == PR_FAILURE) {
178 fprintf(stderr, "PR_SetNetAddr failed\n");
179 exit(1);
181 if (PR_IsNetAddrType(&addr, PR_IpAddrAny) == PR_FALSE) {
182 fprintf(stderr, "addr should be unspecified address\n");
183 exit(1);
185 if (PR_SetNetAddr(PR_IpAddrLoopback, PR_AF_INET, 0, &addr) == PR_FAILURE) {
186 fprintf(stderr, "PR_SetNetAddr failed\n");
187 exit(1);
189 if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
190 fprintf(stderr, "addr should be loopback address\n");
191 exit(1);
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);
202 char buf[256];
203 PR_NetAddrToString(&addr, buf, 256);
204 printf("IPv4 INADDRANY: %s\n", buf);
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);
214 char buf[256];
215 PR_NetAddrToString(&addr, buf, 256);
216 printf("IPv4 LOOPBACK: %s\n", buf);
219 if (PR_SetNetAddr(PR_IpAddrAny, PR_AF_INET6, 0, &addr) == PR_FAILURE) {
220 fprintf(stderr, "PR_SetNetAddr failed\n");
221 exit(1);
223 if (PR_IsNetAddrType(&addr, PR_IpAddrAny) == PR_FALSE) {
224 fprintf(stderr, "addr should be unspecified address\n");
225 exit(1);
228 char buf[256];
229 PR_NetAddrToString(&addr, buf, 256);
230 printf("IPv6 INADDRANY: %s\n", buf);
232 if (PR_SetNetAddr(PR_IpAddrLoopback, PR_AF_INET6, 0, &addr) == PR_FAILURE) {
233 fprintf(stderr, "PR_SetNetAddr failed\n");
234 exit(1);
236 if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
237 fprintf(stderr, "addr should be loopback address\n");
238 exit(1);
241 char buf[256];
242 PR_NetAddrToString(&addr, buf, 256);
243 printf("IPv6 LOOPBACK: %s\n", buf);
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);
257 printf("PASS\n");
258 return 0;