dns.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright 2001 Sun Microsystems, Inc.
  3. * Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
  4. * All rights reserved.
  5. * END COPYRIGHT BLOCK **/
  6. /*
  7. * dns.c: DNS resolution routines
  8. *
  9. * Rob McCool
  10. */
  11. #define DNS_GUESSING
  12. #include "netsite.h"
  13. #ifdef XP_UNIX
  14. #include "systems.h"
  15. #else /* XP_WIN32 */
  16. #include "base/systems.h"
  17. #endif /* XP_WIN32 */
  18. /* Under NT, these are taken care of by net.h including winsock.h */
  19. #ifdef XP_UNIX
  20. #include <arpa/inet.h> /* inet_ntoa */
  21. #include <netdb.h> /* struct hostent */
  22. #ifdef NEED_GHN_PROTO
  23. extern "C" int gethostname (char *name, size_t namelen);
  24. #endif
  25. #endif
  26. #include <stdio.h>
  27. #include <nspr.h>
  28. /* ---------------------------- dns_find_fqdn ----------------------------- */
  29. /* defined in dnsdmain.c */
  30. extern "C" NSAPI_PUBLIC char *dns_guess_domain(char * hname);
  31. char *net_find_fqdn(PRHostEnt *p)
  32. {
  33. int x;
  34. if((!p->h_name) || (!p->h_aliases))
  35. return NULL;
  36. if(!strchr(p->h_name, '.')) {
  37. for(x = 0; p->h_aliases[x]; ++x) {
  38. if((strchr(p->h_aliases[x], '.')) &&
  39. (!strncmp(p->h_aliases[x], p->h_name, strlen(p->h_name))))
  40. {
  41. return STRDUP(p->h_aliases[x]);
  42. }
  43. }
  44. #ifdef DNS_GUESSING
  45. return dns_guess_domain(p->h_name);
  46. #else
  47. return NULL;
  48. #endif /* DNS_GUESSING */
  49. }
  50. else
  51. return STRDUP(p->h_name);
  52. }
  53. /* ----------------------------- dns_ip2host ------------------------------ */
  54. char *dns_ip2host(char *ip, int verify)
  55. {
  56. /* struct in_addr iaddr; */
  57. PRNetAddr iaddr;
  58. char *hn;
  59. static unsigned long laddr = 0;
  60. static char myhostname[256];
  61. PRHostEnt hent;
  62. char buf[PR_NETDB_BUF_SIZE];
  63. PRStatus err;
  64. err = PR_InitializeNetAddr(PR_IpAddrNull, 0, &iaddr);
  65. if((iaddr.inet.ip = inet_addr(ip)) == -1)
  66. goto bong;
  67. /*
  68. * See if it happens to be the localhost IP address, and try
  69. * the local host name if so.
  70. */
  71. if (laddr == 0) {
  72. laddr = inet_addr("127.0.0.1");
  73. myhostname[0] = 0;
  74. PR_GetSystemInfo(PR_SI_HOSTNAME, myhostname, sizeof(myhostname));
  75. }
  76. /* Have to match the localhost IP address and have a hostname */
  77. if ((iaddr.inet.ip == laddr) && (myhostname[0] != 0)) {
  78. /*
  79. * Now try for a fully-qualified domain name, starting with
  80. * the local hostname.
  81. */
  82. err = PR_GetHostByName(myhostname,
  83. buf,
  84. PR_NETDB_BUF_SIZE,
  85. &hent);
  86. /* Don't verify if we get a fully-qualified name this way */
  87. verify = 0;
  88. }
  89. else {
  90. err = PR_GetHostByAddr(&iaddr,
  91. buf,
  92. PR_NETDB_BUF_SIZE,
  93. &hent);
  94. }
  95. if ((err == PR_FAILURE) || !(hn = net_find_fqdn(&hent))) goto bong;
  96. if(verify) {
  97. char **haddr = 0;
  98. err = PR_GetHostByName(hn,
  99. buf,
  100. PR_NETDB_BUF_SIZE,
  101. &hent);
  102. if(err == PR_SUCCESS) {
  103. for(haddr = hent.h_addr_list; *haddr; haddr++) {
  104. if(((struct in_addr *)(*haddr))->s_addr == iaddr.inet.ip)
  105. break;
  106. }
  107. }
  108. if((err == PR_FAILURE) || (!(*haddr)))
  109. goto bong;
  110. }
  111. return hn;
  112. bong:
  113. return NULL;
  114. }