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/. */
9 * Description: tests various functions in prnetdb.h
11 * Usage: gethost [-6] [hostname]
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",
32 * Prints the contents of a PRHostEnt structure
34 void PrintHostent(const PRHostEnt
*he
)
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
]);
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
];
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
) {
70 hostName
= opt
->value
;
72 case 'h': /* Help message */
78 PL_DestroyOptState(opt
);
80 if (PR_GetHostByName(hostName
, buf
, sizeof(buf
), &he
) == PR_FAILURE
) {
81 fprintf(stderr
, "PR_GetHostByName failed\n");
87 idx
= PR_EnumerateHostEnt(idx
, &he
, 0, &addr
);
89 fprintf(stderr
, "PR_EnumerateHostEnt failed\n");
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");
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");
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");
117 printf("PR_GetHostByAddr with PR_AF_INET6\n");
119 idx
= PR_EnumerateHostEnt(idx
, &he
, 0, &addr
);
121 fprintf(stderr
, "PR_EnumerateHostEnt failed\n");
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");
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");
140 if (PR_IsNetAddrType(&addr
, PR_IpAddrLoopback
) == PR_FALSE
) {
141 fprintf(stderr
, "addr should be loopback address\n");
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");
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");
155 if (PR_IsNetAddrType(&addr
, PR_IpAddrLoopback
) == PR_FALSE
) {
156 fprintf(stderr
, "addr should be loopback address\n");
160 if (PR_InitializeNetAddr(PR_IpAddrAny
, 0, &addr
) == PR_FAILURE
) {
161 fprintf(stderr
, "PR_InitializeNetAddr failed\n");
164 if (PR_IsNetAddrType(&addr
, PR_IpAddrAny
) == PR_FALSE
) {
165 fprintf(stderr
, "addr should be unspecified address\n");
168 if (PR_InitializeNetAddr(PR_IpAddrLoopback
, 0, &addr
) == PR_FAILURE
) {
169 fprintf(stderr
, "PR_InitializeNetAddr failed\n");
172 if (PR_IsNetAddrType(&addr
, PR_IpAddrLoopback
) == PR_FALSE
) {
173 fprintf(stderr
, "addr should be loopback address\n");
177 if (PR_SetNetAddr(PR_IpAddrAny
, PR_AF_INET
, 0, &addr
) == PR_FAILURE
) {
178 fprintf(stderr
, "PR_SetNetAddr failed\n");
181 if (PR_IsNetAddrType(&addr
, PR_IpAddrAny
) == PR_FALSE
) {
182 fprintf(stderr
, "addr should be unspecified address\n");
185 if (PR_SetNetAddr(PR_IpAddrLoopback
, PR_AF_INET
, 0, &addr
) == PR_FAILURE
) {
186 fprintf(stderr
, "PR_SetNetAddr failed\n");
189 if (PR_IsNetAddrType(&addr
, PR_IpAddrLoopback
) == PR_FALSE
) {
190 fprintf(stderr
, "addr should be loopback address\n");
194 addr
.inet
.family
= PR_AF_INET
;
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");
203 PR_NetAddrToString(&addr
, buf
, 256);
204 printf("IPv4 INADDRANY: %s\n", buf
);
206 addr
.inet
.family
= PR_AF_INET
;
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");
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");
223 if (PR_IsNetAddrType(&addr
, PR_IpAddrAny
) == PR_FALSE
) {
224 fprintf(stderr
, "addr should be unspecified address\n");
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");
236 if (PR_IsNetAddrType(&addr
, PR_IpAddrLoopback
) == PR_FALSE
) {
237 fprintf(stderr
, "addr should be loopback address\n");
242 PR_NetAddrToString(&addr
, buf
, 256);
243 printf("IPv6 LOOPBACK: %s\n", buf
);
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
);