ipv6.c 7.1 KB

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