~ubuntu-branches/ubuntu/gutsy/virtualbox-ose/gutsy

« back to all changes in this revision

Viewing changes to src/libs/xpcom18a4/nsprpub/pr/tests/gethost.c

  • Committer: Bazaar Package Importer
  • Author(s): Steve Kowalik
  • Date: 2007-09-08 16:44:58 UTC
  • Revision ID: james.westby@ubuntu.com-20070908164458-wao29470vqtr8ksy
Tags: upstream-1.5.0-dfsg2
ImportĀ upstreamĀ versionĀ 1.5.0-dfsg2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
4
 *
 
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/
 
9
 *
 
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
 
13
 * License.
 
14
 *
 
15
 * The Original Code is the Netscape Portable Runtime (NSPR).
 
16
 *
 
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.
 
21
 *
 
22
 * Contributor(s):
 
23
 *
 
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.
 
35
 *
 
36
 * ***** END LICENSE BLOCK ***** */
 
37
 
 
38
/*
 
39
 * File: gethost.c
 
40
 *
 
41
 * Description: tests various functions in prnetdb.h
 
42
 *
 
43
 * Usage: gethost [-6] [hostname]
 
44
 */
 
45
 
 
46
#include "prio.h"
 
47
#include "prnetdb.h"
 
48
#include "plgetopt.h"
 
49
 
 
50
#include <stdio.h>
 
51
#include <stdlib.h>
 
52
 
 
53
#define DEFAULT_HOST_NAME "mcom.com"
 
54
 
 
55
static void Help(void)
 
56
{
 
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",
 
60
            DEFAULT_HOST_NAME);
 
61
}  /* Help */
 
62
 
 
63
/*
 
64
 * Prints the contents of a PRHostEnt structure
 
65
 */
 
66
void PrintHostent(const PRHostEnt *he)
 
67
{
 
68
    int i;
 
69
    int j;
 
70
 
 
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]);
 
74
    }
 
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]);
 
82
        }
 
83
        printf("\n");
 
84
    }
 
85
}
 
86
 
 
87
int main(int argc, char **argv)
 
88
{
 
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];
 
93
    PRIntn idx;
 
94
    PRNetAddr addr;
 
95
    PLOptStatus os;
 
96
    PLOptState *opt = PL_CreateOptState(argc, argv, "h");
 
97
 
 
98
    while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
 
99
        if (PL_OPT_BAD == os) continue;
 
100
        switch (opt->option) {
 
101
            case 0:  /* naked */
 
102
                hostName = opt->value;
 
103
                break;
 
104
            case 'h':  /* Help message */
 
105
            default:
 
106
                Help();
 
107
                return 2;
 
108
        }
 
109
    }
 
110
    PL_DestroyOptState(opt);
 
111
 
 
112
    if (PR_GetHostByName(hostName, buf, sizeof(buf), &he) == PR_FAILURE) {
 
113
        fprintf(stderr, "PR_GetHostByName failed\n");
 
114
        exit(1);
 
115
    }
 
116
    PrintHostent(&he);
 
117
    idx = 0;
 
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
 
 
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");
 
138
        exit(1);
 
139
    }
 
140
    PrintHostent(&he);
 
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");
 
145
        exit(1);
 
146
    }
 
147
    PrintHostent(&he);
 
148
    idx = 0;
 
149
    printf("PR_GetHostByAddr with PR_AF_INET6\n");
 
150
    while (1) {
 
151
        idx = PR_EnumerateHostEnt(idx, &he, 0, &addr);
 
152
        if (idx == -1) {
 
153
            fprintf(stderr, "PR_EnumerateHostEnt failed\n");
 
154
            exit(1);
 
155
        }
 
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");
 
161
            exit(1);
 
162
        }
 
163
        PrintHostent(&reversehe);
 
164
    }
 
165
    printf("PR_GetHostByAddr with PR_AF_INET6 done\n");
 
166
  
 
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");
 
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
    }
 
176
 
 
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");
 
180
        exit(1);
 
181
    }
 
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");
 
185
        exit(1);
 
186
    }
 
187
    if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
 
188
        fprintf(stderr, "addr should be loopback address\n");
 
189
        exit(1);
 
190
    }
 
191
 
 
192
    if (PR_InitializeNetAddr(PR_IpAddrAny, 0, &addr) == PR_FAILURE) {
 
193
        fprintf(stderr, "PR_InitializeNetAddr failed\n");
 
194
        exit(1);
 
195
    }
 
196
    if (PR_IsNetAddrType(&addr, PR_IpAddrAny) == PR_FALSE) {
 
197
        fprintf(stderr, "addr should be unspecified address\n");
 
198
        exit(1);
 
199
    }
 
200
    if (PR_InitializeNetAddr(PR_IpAddrLoopback, 0, &addr) == PR_FAILURE) {
 
201
        fprintf(stderr, "PR_InitializeNetAddr failed\n");
 
202
        exit(1);
 
203
    }
 
204
    if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
 
205
        fprintf(stderr, "addr should be loopback address\n");
 
206
        exit(1);
 
207
    }
 
208
 
 
209
    if (PR_SetNetAddr(PR_IpAddrAny, PR_AF_INET, 0, &addr) == PR_FAILURE) {
 
210
        fprintf(stderr, "PR_SetNetAddr failed\n");
 
211
        exit(1);
 
212
    }
 
213
    if (PR_IsNetAddrType(&addr, PR_IpAddrAny) == PR_FALSE) {
 
214
        fprintf(stderr, "addr should be unspecified address\n");
 
215
        exit(1);
 
216
    }
 
217
    if (PR_SetNetAddr(PR_IpAddrLoopback, PR_AF_INET, 0, &addr) == PR_FAILURE) {
 
218
        fprintf(stderr, "PR_SetNetAddr failed\n");
 
219
        exit(1);
 
220
    }
 
221
    if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
 
222
        fprintf(stderr, "addr should be loopback address\n");
 
223
        exit(1);
 
224
    }
 
225
 
 
226
    addr.inet.family = PR_AF_INET;
 
227
    addr.inet.port = 0;
 
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");
 
231
        exit(1);
 
232
    }
 
233
        {
 
234
                char buf[256];
 
235
                PR_NetAddrToString(&addr, buf, 256);
 
236
                printf("IPv4 INADDRANY: %s\n", buf);
 
237
        }
 
238
    addr.inet.family = PR_AF_INET;
 
239
    addr.inet.port = 0;
 
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");
 
243
        exit(1);
 
244
    }
 
245
        {
 
246
                char buf[256];
 
247
                PR_NetAddrToString(&addr, buf, 256);
 
248
                printf("IPv4 LOOPBACK: %s\n", buf);
 
249
        }
 
250
 
 
251
    if (PR_SetNetAddr(PR_IpAddrAny, PR_AF_INET6, 0, &addr) == PR_FAILURE) {
 
252
        fprintf(stderr, "PR_SetNetAddr failed\n");
 
253
        exit(1);
 
254
    }
 
255
    if (PR_IsNetAddrType(&addr, PR_IpAddrAny) == PR_FALSE) {
 
256
        fprintf(stderr, "addr should be unspecified address\n");
 
257
        exit(1);
 
258
    }
 
259
        {
 
260
                char buf[256];
 
261
                PR_NetAddrToString(&addr, buf, 256);
 
262
                printf("IPv6 INADDRANY: %s\n", buf);
 
263
        }
 
264
    if (PR_SetNetAddr(PR_IpAddrLoopback, PR_AF_INET6, 0, &addr) == PR_FAILURE) {
 
265
        fprintf(stderr, "PR_SetNetAddr failed\n");
 
266
        exit(1);
 
267
    }
 
268
    if (PR_IsNetAddrType(&addr, PR_IpAddrLoopback) == PR_FALSE) {
 
269
        fprintf(stderr, "addr should be loopback address\n");
 
270
        exit(1);
 
271
    }
 
272
        {
 
273
                char buf[256];
 
274
                PR_NetAddrToString(&addr, buf, 256);
 
275
                printf("IPv6 LOOPBACK: %s\n", buf);
 
276
        }
 
277
        {
 
278
                PRIPv6Addr v6addr;
 
279
                char tmp_buf[256];
 
280
 
 
281
        PR_SetNetAddr(PR_IpAddrLoopback, PR_AF_INET, 0, &addr);
 
282
 
 
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);
 
288
        }
 
289
    printf("PASS\n");
 
290
    return 0;
 
291
}