Back to index

lightning-sunbird  0.9+nobinonly
ipv6.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "prio.h"
00039 #include "prenv.h"
00040 #include "prmem.h"
00041 #include "prlink.h"
00042 #include "prsystem.h"
00043 #include "prnetdb.h"
00044 #include "prprf.h"
00045 #include "prvrsion.h"
00046 
00047 #include "plerror.h"
00048 #include "plgetopt.h"
00049 #include "obsolete/probslet.h"
00050 
00051 #include <string.h>
00052 
00053 #define DNS_BUFFER 100
00054 #define ADDR_BUFFER 100
00055 #define HOST_BUFFER 1024
00056 #define PROTO_BUFFER 1500
00057 
00058 #define NETADDR_SIZE(addr) \
00059     (PR_AF_INET == (addr)->raw.family ? \
00060     sizeof((addr)->inet) : sizeof((addr)->ipv6))
00061 
00062 static PRFileDesc *err = NULL;
00063 
00064 static void Help(void)
00065 {
00066     PR_fprintf(err, "Usage: [-V] [-h]\n");
00067     PR_fprintf(err, "\t<nul>    Name of host to lookup          (default: self)\n");
00068     PR_fprintf(err, "\t-V       Display runtime version info    (default: FALSE)\n");
00069     PR_fprintf(err, "\t-h       This message and nothing else\n");
00070 }  /* Help */
00071 
00072 static void DumpAddr(const PRNetAddr* address, const char *msg)
00073 {
00074     PRUint32 *word = (PRUint32*)address;
00075     PRUint32 addr_len = sizeof(PRNetAddr);
00076     PR_fprintf(err, "%s[%d]\t", msg, NETADDR_SIZE(address));
00077     while (addr_len > 0)
00078     {
00079         PR_fprintf(err, " %08x", *word++);
00080         addr_len -= sizeof(PRUint32);
00081     }
00082     PR_fprintf(err, "\n");
00083 }  /* DumpAddr */
00084 
00085 static PRStatus PrintAddress(const PRNetAddr* address)
00086 {
00087     PRNetAddr translation;
00088     char buffer[ADDR_BUFFER];
00089     PRStatus rv = PR_NetAddrToString(address, buffer, sizeof(buffer));
00090     if (PR_FAILURE == rv) PL_FPrintError(err, "PR_NetAddrToString");
00091     else
00092     {
00093         PR_fprintf(err, "\t%s\n", buffer);
00094         memset(&translation, 0, sizeof(translation));
00095         rv = PR_StringToNetAddr(buffer, &translation);
00096         if (PR_FAILURE == rv) PL_FPrintError(err, "PR_StringToNetAddr");
00097         else
00098         {
00099             PRSize addr_len = NETADDR_SIZE(address);
00100             if (0 != memcmp(address, &translation, addr_len))
00101             {
00102                 PR_fprintf(err, "Address translations do not match\n");
00103                 DumpAddr(address, "original");
00104                 DumpAddr(&translation, "translate");
00105                 rv = PR_FAILURE;
00106             }
00107         }
00108     }
00109     return rv;
00110 }  /* PrintAddress */
00111 
00112 PRIntn main(PRIntn argc, char **argv)
00113 {
00114     PRStatus rv;
00115     PLOptStatus os;
00116     PRHostEnt host;
00117     PRProtoEnt proto;
00118     const char *name = NULL;
00119     PRBool failed = PR_FALSE, version = PR_FALSE;
00120     PLOptState *opt = PL_CreateOptState(argc, argv, "Vh");
00121 
00122     err = PR_GetSpecialFD(PR_StandardError);
00123 
00124     while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
00125     {
00126         if (PL_OPT_BAD == os) continue;
00127         switch (opt->option)
00128         {
00129         case 0:  /* Name of host to lookup */
00130             name = opt->value;
00131             break;
00132          case 'V':  /* Do version discovery */
00133             version = PR_TRUE;
00134             break;
00135         case 'h':  /* user wants some guidance */
00136          default:
00137             Help();  /* so give him an earful */
00138             return 2;  /* but not a lot else */
00139         }
00140     }
00141     PL_DestroyOptState(opt);
00142 
00143     if (version)
00144     {
00145 #if defined(WINNT)
00146 #define NSPR_LIB "libnspr4"
00147 #else
00148 #define NSPR_LIB "nspr4"
00149 #endif
00150         const PRVersionDescription *version_info;
00151         char *nspr_path = PR_GetEnv("LD_LIBRARY_PATH");
00152         char *nspr_name = PR_GetLibraryName(nspr_path, NSPR_LIB);
00153         PRLibrary *runtime = PR_LoadLibrary(nspr_name);
00154         if (NULL == runtime)
00155             PL_FPrintError(err, "PR_LoadLibrary");
00156         else
00157         {
00158             versionEntryPointType versionPoint = (versionEntryPointType)
00159                 PR_FindSymbol(runtime, "libVersionPoint");
00160             if (NULL == versionPoint)
00161                 PL_FPrintError(err, "PR_FindSymbol");
00162             else
00163             {
00164                 char buffer[100];
00165                 PRExplodedTime exploded;
00166                 version_info = versionPoint();
00167                 (void)PR_fprintf(err, "Runtime library version information\n");
00168                 PR_ExplodeTime(
00169                     version_info->buildTime, PR_GMTParameters, &exploded);
00170                 (void)PR_FormatTime(
00171                     buffer, sizeof(buffer), "%d %b %Y %H:%M:%S", &exploded);
00172                 (void)PR_fprintf(err, "  Build time: %s GMT\n", buffer);
00173                 (void)PR_fprintf(
00174                     err, "  Build time: %s\n", version_info->buildTimeString);
00175                 (void)PR_fprintf(
00176                     err, "  %s V%u.%u.%u (%s%s%s)\n",
00177                     version_info->description,
00178                     version_info->vMajor,
00179                     version_info->vMinor,
00180                     version_info->vPatch,
00181                     (version_info->beta ? " beta " : ""),
00182                     (version_info->debug ? " debug " : ""),
00183                     (version_info->special ? " special" : ""));
00184                 (void)PR_fprintf(err, "  filename: %s\n", version_info->filename);
00185                 (void)PR_fprintf(err, "  security: %s\n", version_info->security);
00186                 (void)PR_fprintf(err, "  copyright: %s\n", version_info->copyright);
00187                 (void)PR_fprintf(err, "  comment: %s\n", version_info->comment);
00188             }
00189         }
00190         if (NULL != nspr_name) PR_FreeLibraryName(nspr_name);
00191     }
00192 
00193     {
00194         if (NULL == name)
00195         {
00196             char *me = (char*)PR_MALLOC(DNS_BUFFER);
00197             rv = PR_GetSystemInfo(PR_SI_HOSTNAME, me, DNS_BUFFER);
00198             if (PR_FAILURE == rv)
00199             {
00200                 failed = PR_TRUE;
00201                 PL_FPrintError(err, "PR_GetSystemInfo");
00202                 return 2;
00203             }
00204             name = me;  /* just leak the storage */
00205         }
00206     }
00207 
00208     {
00209         char buffer[HOST_BUFFER];
00210         PR_fprintf(err, "Translating the name %s ...", name);
00211 
00212         rv = PR_GetHostByName(name, buffer, sizeof(buffer), &host);
00213         if (PR_FAILURE == rv)
00214         {
00215             failed = PR_TRUE;
00216             PL_FPrintError(err, "PR_GetHostByName");
00217         }
00218         else
00219         {
00220             PRIntn index = 0;
00221             PRNetAddr address;
00222             memset(&address, 0, sizeof(PRNetAddr));
00223             PR_fprintf(err, "success .. enumerating results\n");
00224             do
00225             {
00226                 index = PR_EnumerateHostEnt(index, &host, 0, &address);
00227                 if (index > 0) PrintAddress(&address);
00228                 else if (-1 == index)
00229                 {
00230                     failed = PR_TRUE;
00231                     PL_FPrintError(err, "PR_EnumerateHostEnt");
00232                 }
00233             } while (index > 0);
00234         }
00235     }
00236 
00237 
00238     {
00239         char buffer[PROTO_BUFFER];
00240         /*
00241         ** Get Proto by name/number
00242         */
00243         rv = PR_GetProtoByName("tcp", &buffer[1], sizeof(buffer) - 1, &proto);
00244         rv = PR_GetProtoByNumber(6, &buffer[3], sizeof(buffer) - 3, &proto);
00245     }
00246 
00247     return (failed) ? 1 : 0;
00248 }