home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / tests / ipv6.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  5.3 KB  |  182 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  * 
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  * 
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #include "prio.h"
  20. #include "prmem.h"
  21. #include "prsystem.h"
  22. #include "prnetdb.h"
  23. #include "prprf.h"
  24.  
  25. #include "plerror.h"
  26. #include "plgetopt.h"
  27. #include "obsolete/probslet.h"
  28.  
  29. #include <string.h>
  30.  
  31. #define DNS_BUFFER 100
  32. #define ADDR_BUFFER 100
  33. #define HOST_BUFFER 1024
  34. #define PROTO_BUFFER 1500
  35.  
  36. static PRFileDesc *err = NULL;
  37.  
  38. static void Help(void)
  39. {
  40.     PR_fprintf(err, "Usage: [-t s] [-s] [-h]\n");
  41.     PR_fprintf(err, "\t<nul>    Name of host to lookup          (default: self)\n");
  42.     PR_fprintf(err, "\t-6       First turn on IPv6 capability   (default: FALSE)\n");
  43.     PR_fprintf(err, "\t-h       This message and nothing else\n");
  44. }  /* Help */
  45.  
  46. static void DumpAddr(const PRNetAddr* address, const char *msg)
  47. {
  48.     PRUint32 *word = (PRUint32*)address;
  49.     PRUint32 addr_len = sizeof(PRNetAddr);
  50.     PR_fprintf(err, "%s[%d]\t", msg, PR_NETADDR_SIZE(address));
  51.     while (addr_len > 0)
  52.     {
  53.         PR_fprintf(err, " %08x", *word++);
  54.         addr_len -= sizeof(PRUint32);
  55.     }
  56.     PR_fprintf(err, "\n");
  57. }  /* DumpAddr */
  58.  
  59. static PRStatus PrintAddress(const PRNetAddr* address)
  60. {
  61.     PRNetAddr translation;
  62.     char buffer[ADDR_BUFFER];
  63.     PRStatus rv = PR_NetAddrToString(address, buffer, sizeof(buffer));
  64.     if (PR_FAILURE == rv) PL_FPrintError(err, "PR_NetAddrToString");
  65.     else
  66.     {
  67.         PR_fprintf(err, "\t%s\n", buffer);
  68.         rv = PR_StringToNetAddr(buffer, &translation);
  69.         if (PR_FAILURE == rv) PL_FPrintError(err, "PR_StringToNetAddr");
  70.         else
  71.         {
  72.             PRSize addr_len = PR_NETADDR_SIZE(address);
  73.             if (0 != memcmp(address, &translation, addr_len))
  74.             {
  75.                 PR_fprintf(err, "Address translations do not match\n");
  76.                 DumpAddr(address, "original");
  77.                 DumpAddr(&translation, "translate");
  78.                 rv = PR_FAILURE;
  79.             }
  80.         }
  81.     }
  82.     return rv;
  83. }  /* PrintAddress */
  84.  
  85. PRIntn main(PRIntn argc, char **argv)
  86. {
  87.     PRStatus rv;
  88.     PLOptStatus os;
  89.     PRHostEnt host;
  90.     PRProtoEnt proto;
  91.     PRBool ipv6 = PR_FALSE;
  92.     const char *name = NULL;
  93.     PRBool failed = PR_FALSE;
  94.     PLOptState *opt = PL_CreateOptState(argc, argv, "h6");
  95.  
  96.     err = PR_GetSpecialFD(PR_StandardError);
  97.  
  98.     while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
  99.     {
  100.         if (PL_OPT_BAD == os) continue;
  101.         switch (opt->option)
  102.         {
  103.         case 0:  /* Name of host to lookup */
  104.             name = opt->value;
  105.             break;
  106.         case '6':  /* Turn on IPv6 mode */
  107.             ipv6 = PR_TRUE;
  108.             break;
  109.         case 'h':  /* user wants some guidance */
  110.          default:
  111.             Help();  /* so give him an earful */
  112.             return 2;  /* but not a lot else */
  113.         }
  114.     }
  115.     PL_DestroyOptState(opt);
  116.  
  117.     if (ipv6)
  118.     {
  119.         rv = PR_SetIPv6Enable(ipv6);
  120.         if (PR_FAILURE == rv)
  121.         {
  122.             failed = PR_TRUE;
  123.             PL_FPrintError(err, "PR_SetIPv6Enable");
  124.         }
  125.     }
  126.  
  127.     {
  128.         if (NULL == name)
  129.         {
  130.             char *me = (char*)PR_MALLOC(DNS_BUFFER);
  131.             rv = PR_GetSystemInfo(PR_SI_HOSTNAME, me, DNS_BUFFER);
  132.             if (PR_FAILURE == rv)
  133.             {
  134.                 failed = PR_TRUE;
  135.                 PL_FPrintError(err, "PR_GetHostName");
  136.                 return 2;
  137.             }
  138.             name = me;  /* just leak the storage */
  139.         }
  140.     }
  141.  
  142.     {
  143.         char buffer[HOST_BUFFER];
  144.         PR_fprintf(err, "Translating the name %s ...", name);
  145.  
  146.         rv = PR_GetHostByName(name, buffer, sizeof(buffer), &host);
  147.         if (PR_FAILURE == rv)
  148.         {
  149.             failed = PR_TRUE;
  150.             PL_FPrintError(err, "PR_GetHostByName");
  151.         }
  152.         else
  153.         {
  154.             PRIntn index = 0;
  155.             PRNetAddr address;
  156.             PR_fprintf(err, "success .. enumerating results\n");
  157.             do
  158.             {
  159.                 index = PR_EnumerateHostEnt(index, &host, 0, &address);
  160.                 if (index > 0) PrintAddress(&address);
  161.                 else if (-1 == index)
  162.                 {
  163.                     failed = PR_TRUE;
  164.                     PL_FPrintError(err, "PR_EnumerateHostEnt");
  165.                 }
  166.             } while (index > 0);
  167.         }
  168.     }
  169.  
  170.  
  171.     {
  172.         char buffer[PROTO_BUFFER];
  173.         /*
  174.         ** Get Proto by name/number
  175.         */
  176.         rv = PR_GetProtoByName("tcp", &buffer[1], sizeof(buffer) - 1, &proto);
  177.         rv = PR_GetProtoByNumber(6, &buffer[3], sizeof(buffer) - 3, &proto);
  178.     }
  179.  
  180.     return (failed) ? 1 : 0;
  181. }
  182.