VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/nsprpub/pr/tests/ipv6.c@ 55761

最後變更 在這個檔案從55761是 1,由 vboxsync 提交於 55 年 前

import

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 8.3 KB
 
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) 1998-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#include "prio.h"
39#include "prenv.h"
40#include "prmem.h"
41#include "prlink.h"
42#include "prsystem.h"
43#include "prnetdb.h"
44#include "prprf.h"
45#include "prvrsion.h"
46
47#include "plerror.h"
48#include "plgetopt.h"
49#include "obsolete/probslet.h"
50
51#include <string.h>
52
53#define DNS_BUFFER 100
54#define ADDR_BUFFER 100
55#define HOST_BUFFER 1024
56#define PROTO_BUFFER 1500
57
58#define NETADDR_SIZE(addr) \
59 (PR_AF_INET == (addr)->raw.family ? \
60 sizeof((addr)->inet) : sizeof((addr)->ipv6))
61
62static PRFileDesc *err = NULL;
63
64static void Help(void)
65{
66 PR_fprintf(err, "Usage: [-V] [-h]\n");
67 PR_fprintf(err, "\t<nul> Name of host to lookup (default: self)\n");
68 PR_fprintf(err, "\t-V Display runtime version info (default: FALSE)\n");
69 PR_fprintf(err, "\t-h This message and nothing else\n");
70} /* Help */
71
72static void DumpAddr(const PRNetAddr* address, const char *msg)
73{
74 PRUint32 *word = (PRUint32*)address;
75 PRUint32 addr_len = sizeof(PRNetAddr);
76 PR_fprintf(err, "%s[%d]\t", msg, NETADDR_SIZE(address));
77 while (addr_len > 0)
78 {
79 PR_fprintf(err, " %08x", *word++);
80 addr_len -= sizeof(PRUint32);
81 }
82 PR_fprintf(err, "\n");
83} /* DumpAddr */
84
85static PRStatus PrintAddress(const PRNetAddr* address)
86{
87 PRNetAddr translation;
88 char buffer[ADDR_BUFFER];
89 PRStatus rv = PR_NetAddrToString(address, buffer, sizeof(buffer));
90 if (PR_FAILURE == rv) PL_FPrintError(err, "PR_NetAddrToString");
91 else
92 {
93 PR_fprintf(err, "\t%s\n", buffer);
94 memset(&translation, 0, sizeof(translation));
95 rv = PR_StringToNetAddr(buffer, &translation);
96 if (PR_FAILURE == rv) PL_FPrintError(err, "PR_StringToNetAddr");
97 else
98 {
99 PRSize addr_len = NETADDR_SIZE(address);
100 if (0 != memcmp(address, &translation, addr_len))
101 {
102 PR_fprintf(err, "Address translations do not match\n");
103 DumpAddr(address, "original");
104 DumpAddr(&translation, "translate");
105 rv = PR_FAILURE;
106 }
107 }
108 }
109 return rv;
110} /* PrintAddress */
111
112PRIntn main(PRIntn argc, char **argv)
113{
114 PRStatus rv;
115 PLOptStatus os;
116 PRHostEnt host;
117 PRProtoEnt proto;
118 const char *name = NULL;
119 PRBool failed = PR_FALSE, version = PR_FALSE;
120 PLOptState *opt = PL_CreateOptState(argc, argv, "Vh");
121
122 err = PR_GetSpecialFD(PR_StandardError);
123
124 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
125 {
126 if (PL_OPT_BAD == os) continue;
127 switch (opt->option)
128 {
129 case 0: /* Name of host to lookup */
130 name = opt->value;
131 break;
132 case 'V': /* Do version discovery */
133 version = PR_TRUE;
134 break;
135 case 'h': /* user wants some guidance */
136 default:
137 Help(); /* so give him an earful */
138 return 2; /* but not a lot else */
139 }
140 }
141 PL_DestroyOptState(opt);
142
143 if (version)
144 {
145#if defined(WINNT)
146#define NSPR_LIB "libnspr4"
147#else
148#define NSPR_LIB "nspr4"
149#endif
150 const PRVersionDescription *version_info;
151 char *nspr_path = PR_GetEnv("LD_LIBRARY_PATH");
152 char *nspr_name = PR_GetLibraryName(nspr_path, NSPR_LIB);
153 PRLibrary *runtime = PR_LoadLibrary(nspr_name);
154 if (NULL == runtime)
155 PL_FPrintError(err, "PR_LoadLibrary");
156 else
157 {
158 versionEntryPointType versionPoint = (versionEntryPointType)
159 PR_FindSymbol(runtime, "libVersionPoint");
160 if (NULL == versionPoint)
161 PL_FPrintError(err, "PR_FindSymbol");
162 else
163 {
164 char buffer[100];
165 PRExplodedTime exploded;
166 version_info = versionPoint();
167 (void)PR_fprintf(err, "Runtime library version information\n");
168 PR_ExplodeTime(
169 version_info->buildTime, PR_GMTParameters, &exploded);
170 (void)PR_FormatTime(
171 buffer, sizeof(buffer), "%d %b %Y %H:%M:%S", &exploded);
172 (void)PR_fprintf(err, " Build time: %s GMT\n", buffer);
173 (void)PR_fprintf(
174 err, " Build time: %s\n", version_info->buildTimeString);
175 (void)PR_fprintf(
176 err, " %s V%u.%u.%u (%s%s%s)\n",
177 version_info->description,
178 version_info->vMajor,
179 version_info->vMinor,
180 version_info->vPatch,
181 (version_info->beta ? " beta " : ""),
182 (version_info->debug ? " debug " : ""),
183 (version_info->special ? " special" : ""));
184 (void)PR_fprintf(err, " filename: %s\n", version_info->filename);
185 (void)PR_fprintf(err, " security: %s\n", version_info->security);
186 (void)PR_fprintf(err, " copyright: %s\n", version_info->copyright);
187 (void)PR_fprintf(err, " comment: %s\n", version_info->comment);
188 }
189 }
190 if (NULL != nspr_name) PR_FreeLibraryName(nspr_name);
191 }
192
193 {
194 if (NULL == name)
195 {
196 char *me = (char*)PR_MALLOC(DNS_BUFFER);
197 rv = PR_GetSystemInfo(PR_SI_HOSTNAME, me, DNS_BUFFER);
198 if (PR_FAILURE == rv)
199 {
200 failed = PR_TRUE;
201 PL_FPrintError(err, "PR_GetSystemInfo");
202 return 2;
203 }
204 name = me; /* just leak the storage */
205 }
206 }
207
208 {
209 char buffer[HOST_BUFFER];
210 PR_fprintf(err, "Translating the name %s ...", name);
211
212 rv = PR_GetHostByName(name, buffer, sizeof(buffer), &host);
213 if (PR_FAILURE == rv)
214 {
215 failed = PR_TRUE;
216 PL_FPrintError(err, "PR_GetHostByName");
217 }
218 else
219 {
220 PRIntn index = 0;
221 PRNetAddr address;
222 memset(&address, 0, sizeof(PRNetAddr));
223 PR_fprintf(err, "success .. enumerating results\n");
224 do
225 {
226 index = PR_EnumerateHostEnt(index, &host, 0, &address);
227 if (index > 0) PrintAddress(&address);
228 else if (-1 == index)
229 {
230 failed = PR_TRUE;
231 PL_FPrintError(err, "PR_EnumerateHostEnt");
232 }
233 } while (index > 0);
234 }
235 }
236
237
238 {
239 char buffer[PROTO_BUFFER];
240 /*
241 ** Get Proto by name/number
242 */
243 rv = PR_GetProtoByName("tcp", &buffer[1], sizeof(buffer) - 1, &proto);
244 rv = PR_GetProtoByNumber(6, &buffer[3], sizeof(buffer) - 3, &proto);
245 }
246
247 return (failed) ? 1 : 0;
248}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette