1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Mozilla Public License Version
6
* 1.1 (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/MPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is the Netscape Portable Runtime (NSPR).
17
* The Initial Developer of the Original Code is
18
* Netscape Communications Corporation.
19
* Portions created by the Initial Developer are Copyright (C) 1999-2000
20
* the Initial Developer. All Rights Reserved.
24
* Alternatively, the contents of this file may be used under the terms of
25
* either the GNU General Public License Version 2 or later (the "GPL"), or
26
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27
* in which case the provisions of the GPL or the LGPL are applicable instead
28
* of those above. If you wish to allow use of your version of this file only
29
* under the terms of either the GPL or the LGPL, and not to allow others to
30
* use your version of this file under the terms of the MPL, indicate your
31
* decision by deleting the provisions above and replace them with the notice
32
* and other provisions required by the GPL or the LGPL. If you do not delete
33
* the provisions above, a recipient may use your version of this file under
34
* the terms of any one of the MPL, the GPL or the LGPL.
36
* ***** END LICENSE BLOCK ***** */
41
* Description: tests various functions in prnetdb.h
43
* Usage: gethost [-6] [hostname]
53
#define DEFAULT_HOST_NAME "mcom.com"
55
static void Help(void)
57
fprintf(stderr, "Usage: gethost [-h] [hostname]\n");
58
fprintf(stderr, "\t-h help\n");
59
fprintf(stderr, "\thostname Name of host (default: %s)\n",
64
* Prints the contents of a PRHostEnt structure
66
void PrintHostent(const PRHostEnt *he)
71
printf("h_name: %s\n", he->h_name);
72
for (i = 0; he->h_aliases[i]; i++) {
73
printf("h_aliases[%d]: %s\n", i, he->h_aliases[i]);
75
printf("h_addrtype: %d\n", he->h_addrtype);
76
printf("h_length: %d\n", he->h_length);
77
for (i = 0; he->h_addr_list[i]; i++) {
78
printf("h_addr_list[%d]: ", i);
79
for (j = 0; j < he->h_length; j++) {
80
if (j != 0) printf(".");
81
printf("%u", (unsigned char)he->h_addr_list[i][j]);
87
int main(int argc, char **argv)
89
const char *hostName = DEFAULT_HOST_NAME;
90
PRHostEnt he, reversehe;
91
char buf[PR_NETDB_BUF_SIZE];
92
char reversebuf[PR_NETDB_BUF_SIZE];
96
PLOptState *opt = PL_CreateOptState(argc, argv, "h");
98
while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
99
if (PL_OPT_BAD == os) continue;
100
switch (opt->option) {
102
hostName = opt->value;
104
case 'h': /* Help message */
110
PL_DestroyOptState(opt);
112
if (PR_GetHostByName(hostName, buf, sizeof(buf), &he) == PR_FAILURE) {
113
fprintf(stderr, "PR_GetHostByName failed\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);
134
printf("PR_GetIPNodeByName with PR_AF_INET\n");
135
if (PR_GetIPNodeByName(hostName, PR_AF_INET, PR_AI_DEFAULT,
136
buf, sizeof(buf), &he) == PR_FAILURE) {
137
fprintf(stderr, "PR_GetIPNodeByName failed\n");
141
printf("PR_GetIPNodeByName with PR_AF_INET6\n");
142
if (PR_GetIPNodeByName(hostName, PR_AF_INET6, PR_AI_DEFAULT,
143
buf, sizeof(buf), &he) == PR_FAILURE) {
144
fprintf(stderr, "PR_GetIPNodeByName failed\n");
149
printf("PR_GetHostByAddr with PR_AF_INET6\n");
151
idx = PR_EnumerateHostEnt(idx, &he, 0, &addr);
153
fprintf(stderr, "PR_EnumerateHostEnt failed\n");
156
if (idx == 0) break; /* normal loop termination */
157
printf("reverse lookup\n");
158
if (PR_GetHostByAddr(&addr, reversebuf, sizeof(reversebuf),
159
&reversehe) == PR_FAILURE) {
160
fprintf(stderr, "PR_GetHostByAddr failed\n");
163
PrintHostent(&reversehe);
165
printf("PR_GetHostByAddr with PR_AF_INET6 done\n");
167
PR_StringToNetAddr("::1", &addr);
168
if (PR_IsNetAddrType(&addr, PR_IpAddrV4Mapped) == PR_TRUE) {
169
fprintf(stderr, "addr should not be ipv4 mapped address\n");
172
if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
173
fprintf(stderr, "addr should be loopback address\n");
177
PR_StringToNetAddr("127.0.0.1", &addr);
178
if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
179
fprintf(stderr, "addr should be loopback address\n");
182
PR_StringToNetAddr("::FFFF:127.0.0.1", &addr);
183
if (PR_IsNetAddrType(&addr, PR_IpAddrV4Mapped) == PR_FALSE) {
184
fprintf(stderr, "addr should be ipv4 mapped address\n");
187
if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
188
fprintf(stderr, "addr should be loopback address\n");
192
if (PR_InitializeNetAddr(PR_IpAddrAny, 0, &addr) == PR_FAILURE) {
193
fprintf(stderr, "PR_InitializeNetAddr failed\n");
196
if (PR_IsNetAddrType(&addr, PR_IpAddrAny) == PR_FALSE) {
197
fprintf(stderr, "addr should be unspecified address\n");
200
if (PR_InitializeNetAddr(PR_IpAddrLoopback, 0, &addr) == PR_FAILURE) {
201
fprintf(stderr, "PR_InitializeNetAddr failed\n");
204
if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
205
fprintf(stderr, "addr should be loopback address\n");
209
if (PR_SetNetAddr(PR_IpAddrAny, PR_AF_INET, 0, &addr) == PR_FAILURE) {
210
fprintf(stderr, "PR_SetNetAddr failed\n");
213
if (PR_IsNetAddrType(&addr, PR_IpAddrAny) == PR_FALSE) {
214
fprintf(stderr, "addr should be unspecified address\n");
217
if (PR_SetNetAddr(PR_IpAddrLoopback, PR_AF_INET, 0, &addr) == PR_FAILURE) {
218
fprintf(stderr, "PR_SetNetAddr failed\n");
221
if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
222
fprintf(stderr, "addr should be loopback address\n");
226
addr.inet.family = PR_AF_INET;
228
addr.inet.ip = PR_htonl(PR_INADDR_ANY);
229
if (PR_IsNetAddrType(&addr, PR_IpAddrAny) == PR_FALSE) {
230
fprintf(stderr, "addr should be unspecified address\n");
235
PR_NetAddrToString(&addr, buf, 256);
236
printf("IPv4 INADDRANY: %s\n", buf);
238
addr.inet.family = PR_AF_INET;
240
addr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK);
241
if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
242
fprintf(stderr, "addr should be loopback address\n");
247
PR_NetAddrToString(&addr, buf, 256);
248
printf("IPv4 LOOPBACK: %s\n", buf);
251
if (PR_SetNetAddr(PR_IpAddrAny, PR_AF_INET6, 0, &addr) == PR_FAILURE) {
252
fprintf(stderr, "PR_SetNetAddr failed\n");
255
if (PR_IsNetAddrType(&addr, PR_IpAddrAny) == PR_FALSE) {
256
fprintf(stderr, "addr should be unspecified address\n");
261
PR_NetAddrToString(&addr, buf, 256);
262
printf("IPv6 INADDRANY: %s\n", buf);
264
if (PR_SetNetAddr(PR_IpAddrLoopback, PR_AF_INET6, 0, &addr) == PR_FAILURE) {
265
fprintf(stderr, "PR_SetNetAddr failed\n");
268
if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
269
fprintf(stderr, "addr should be loopback address\n");
274
PR_NetAddrToString(&addr, buf, 256);
275
printf("IPv6 LOOPBACK: %s\n", buf);
281
PR_SetNetAddr(PR_IpAddrLoopback, PR_AF_INET, 0, &addr);
283
PR_ConvertIPv4AddrToIPv6(addr.inet.ip, &v6addr);
284
PR_SetNetAddr(PR_IpAddrAny, PR_AF_INET6, 0, &addr);
285
addr.ipv6.ip = v6addr;
286
PR_NetAddrToString(&addr, tmp_buf, 256);
287
printf("IPv4-mapped IPv6 LOOPBACK: %s\n", tmp_buf);