nsprpub/pr/tests/ipv6.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "prio.h"
michael@0 7 #include "prenv.h"
michael@0 8 #include "prmem.h"
michael@0 9 #include "prlink.h"
michael@0 10 #include "prsystem.h"
michael@0 11 #include "prnetdb.h"
michael@0 12 #include "prprf.h"
michael@0 13 #include "prvrsion.h"
michael@0 14
michael@0 15 #include "plerror.h"
michael@0 16 #include "plgetopt.h"
michael@0 17 #include "obsolete/probslet.h"
michael@0 18
michael@0 19 #include <string.h>
michael@0 20
michael@0 21 #define DNS_BUFFER 100
michael@0 22 #define ADDR_BUFFER 100
michael@0 23 #define HOST_BUFFER 1024
michael@0 24 #define PROTO_BUFFER 1500
michael@0 25
michael@0 26 #define NETADDR_SIZE(addr) \
michael@0 27 (PR_AF_INET == (addr)->raw.family ? \
michael@0 28 sizeof((addr)->inet) : sizeof((addr)->ipv6))
michael@0 29
michael@0 30 static PRFileDesc *err = NULL;
michael@0 31
michael@0 32 static void Help(void)
michael@0 33 {
michael@0 34 PR_fprintf(err, "Usage: [-V] [-h]\n");
michael@0 35 PR_fprintf(err, "\t<nul> Name of host to lookup (default: self)\n");
michael@0 36 PR_fprintf(err, "\t-V Display runtime version info (default: FALSE)\n");
michael@0 37 PR_fprintf(err, "\t-h This message and nothing else\n");
michael@0 38 } /* Help */
michael@0 39
michael@0 40 static void DumpAddr(const PRNetAddr* address, const char *msg)
michael@0 41 {
michael@0 42 PRUint32 *word = (PRUint32*)address;
michael@0 43 PRUint32 addr_len = sizeof(PRNetAddr);
michael@0 44 PR_fprintf(err, "%s[%d]\t", msg, NETADDR_SIZE(address));
michael@0 45 while (addr_len > 0)
michael@0 46 {
michael@0 47 PR_fprintf(err, " %08x", *word++);
michael@0 48 addr_len -= sizeof(PRUint32);
michael@0 49 }
michael@0 50 PR_fprintf(err, "\n");
michael@0 51 } /* DumpAddr */
michael@0 52
michael@0 53 static PRStatus PrintAddress(const PRNetAddr* address)
michael@0 54 {
michael@0 55 PRNetAddr translation;
michael@0 56 char buffer[ADDR_BUFFER];
michael@0 57 PRStatus rv = PR_NetAddrToString(address, buffer, sizeof(buffer));
michael@0 58 if (PR_FAILURE == rv) PL_FPrintError(err, "PR_NetAddrToString");
michael@0 59 else
michael@0 60 {
michael@0 61 PR_fprintf(err, "\t%s\n", buffer);
michael@0 62 memset(&translation, 0, sizeof(translation));
michael@0 63 rv = PR_StringToNetAddr(buffer, &translation);
michael@0 64 if (PR_FAILURE == rv) PL_FPrintError(err, "PR_StringToNetAddr");
michael@0 65 else
michael@0 66 {
michael@0 67 PRSize addr_len = NETADDR_SIZE(address);
michael@0 68 if (0 != memcmp(address, &translation, addr_len))
michael@0 69 {
michael@0 70 PR_fprintf(err, "Address translations do not match\n");
michael@0 71 DumpAddr(address, "original");
michael@0 72 DumpAddr(&translation, "translate");
michael@0 73 rv = PR_FAILURE;
michael@0 74 }
michael@0 75 }
michael@0 76 }
michael@0 77 return rv;
michael@0 78 } /* PrintAddress */
michael@0 79
michael@0 80 int main(int argc, char **argv)
michael@0 81 {
michael@0 82 PRStatus rv;
michael@0 83 PLOptStatus os;
michael@0 84 PRHostEnt host;
michael@0 85 PRProtoEnt proto;
michael@0 86 const char *name = NULL;
michael@0 87 PRBool failed = PR_FALSE, version = PR_FALSE;
michael@0 88 PLOptState *opt = PL_CreateOptState(argc, argv, "Vh");
michael@0 89
michael@0 90 err = PR_GetSpecialFD(PR_StandardError);
michael@0 91
michael@0 92 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
michael@0 93 {
michael@0 94 if (PL_OPT_BAD == os) continue;
michael@0 95 switch (opt->option)
michael@0 96 {
michael@0 97 case 0: /* Name of host to lookup */
michael@0 98 name = opt->value;
michael@0 99 break;
michael@0 100 case 'V': /* Do version discovery */
michael@0 101 version = PR_TRUE;
michael@0 102 break;
michael@0 103 case 'h': /* user wants some guidance */
michael@0 104 default:
michael@0 105 Help(); /* so give him an earful */
michael@0 106 return 2; /* but not a lot else */
michael@0 107 }
michael@0 108 }
michael@0 109 PL_DestroyOptState(opt);
michael@0 110
michael@0 111 if (version)
michael@0 112 {
michael@0 113 #if defined(WINNT)
michael@0 114 #define NSPR_LIB "libnspr4"
michael@0 115 #else
michael@0 116 #define NSPR_LIB "nspr4"
michael@0 117 #endif
michael@0 118 const PRVersionDescription *version_info;
michael@0 119 char *nspr_path = PR_GetEnv("LD_LIBRARY_PATH");
michael@0 120 char *nspr_name = PR_GetLibraryName(nspr_path, NSPR_LIB);
michael@0 121 PRLibrary *runtime = PR_LoadLibrary(nspr_name);
michael@0 122 if (NULL == runtime)
michael@0 123 PL_FPrintError(err, "PR_LoadLibrary");
michael@0 124 else
michael@0 125 {
michael@0 126 versionEntryPointType versionPoint = (versionEntryPointType)
michael@0 127 PR_FindSymbol(runtime, "libVersionPoint");
michael@0 128 if (NULL == versionPoint)
michael@0 129 PL_FPrintError(err, "PR_FindSymbol");
michael@0 130 else
michael@0 131 {
michael@0 132 char buffer[100];
michael@0 133 PRExplodedTime exploded;
michael@0 134 version_info = versionPoint();
michael@0 135 (void)PR_fprintf(err, "Runtime library version information\n");
michael@0 136 PR_ExplodeTime(
michael@0 137 version_info->buildTime, PR_GMTParameters, &exploded);
michael@0 138 (void)PR_FormatTime(
michael@0 139 buffer, sizeof(buffer), "%d %b %Y %H:%M:%S", &exploded);
michael@0 140 (void)PR_fprintf(err, " Build time: %s GMT\n", buffer);
michael@0 141 (void)PR_fprintf(
michael@0 142 err, " Build time: %s\n", version_info->buildTimeString);
michael@0 143 (void)PR_fprintf(
michael@0 144 err, " %s V%u.%u.%u (%s%s%s)\n",
michael@0 145 version_info->description,
michael@0 146 version_info->vMajor,
michael@0 147 version_info->vMinor,
michael@0 148 version_info->vPatch,
michael@0 149 (version_info->beta ? " beta " : ""),
michael@0 150 (version_info->debug ? " debug " : ""),
michael@0 151 (version_info->special ? " special" : ""));
michael@0 152 (void)PR_fprintf(err, " filename: %s\n", version_info->filename);
michael@0 153 (void)PR_fprintf(err, " security: %s\n", version_info->security);
michael@0 154 (void)PR_fprintf(err, " copyright: %s\n", version_info->copyright);
michael@0 155 (void)PR_fprintf(err, " comment: %s\n", version_info->comment);
michael@0 156 }
michael@0 157 }
michael@0 158 if (NULL != nspr_name) PR_FreeLibraryName(nspr_name);
michael@0 159 }
michael@0 160
michael@0 161 {
michael@0 162 if (NULL == name)
michael@0 163 {
michael@0 164 char *me = (char*)PR_MALLOC(DNS_BUFFER);
michael@0 165 rv = PR_GetSystemInfo(PR_SI_HOSTNAME, me, DNS_BUFFER);
michael@0 166 if (PR_FAILURE == rv)
michael@0 167 {
michael@0 168 failed = PR_TRUE;
michael@0 169 PL_FPrintError(err, "PR_GetSystemInfo");
michael@0 170 return 2;
michael@0 171 }
michael@0 172 name = me; /* just leak the storage */
michael@0 173 }
michael@0 174 }
michael@0 175
michael@0 176 {
michael@0 177 char buffer[HOST_BUFFER];
michael@0 178 PR_fprintf(err, "Translating the name %s ...", name);
michael@0 179
michael@0 180 rv = PR_GetHostByName(name, buffer, sizeof(buffer), &host);
michael@0 181 if (PR_FAILURE == rv)
michael@0 182 {
michael@0 183 failed = PR_TRUE;
michael@0 184 PL_FPrintError(err, "PR_GetHostByName");
michael@0 185 }
michael@0 186 else
michael@0 187 {
michael@0 188 PRIntn index = 0;
michael@0 189 PRNetAddr address;
michael@0 190 memset(&address, 0, sizeof(PRNetAddr));
michael@0 191 PR_fprintf(err, "success .. enumerating results\n");
michael@0 192 do
michael@0 193 {
michael@0 194 index = PR_EnumerateHostEnt(index, &host, 0, &address);
michael@0 195 if (index > 0) PrintAddress(&address);
michael@0 196 else if (-1 == index)
michael@0 197 {
michael@0 198 failed = PR_TRUE;
michael@0 199 PL_FPrintError(err, "PR_EnumerateHostEnt");
michael@0 200 }
michael@0 201 } while (index > 0);
michael@0 202 }
michael@0 203 }
michael@0 204
michael@0 205
michael@0 206 {
michael@0 207 char buffer[PROTO_BUFFER];
michael@0 208 /*
michael@0 209 ** Get Proto by name/number
michael@0 210 */
michael@0 211 rv = PR_GetProtoByName("tcp", &buffer[1], sizeof(buffer) - 1, &proto);
michael@0 212 rv = PR_GetProtoByNumber(6, &buffer[3], sizeof(buffer) - 3, &proto);
michael@0 213 }
michael@0 214
michael@0 215 return (failed) ? 1 : 0;
michael@0 216 }

mercurial