dns.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /*
  13. * dns.c: DNS resolution routines
  14. *
  15. * Rob McCool
  16. */
  17. #define DNS_GUESSING
  18. #include "netsite.h"
  19. #include "systems.h"
  20. /* Under NT, these are taken care of by net.h including winsock.h */
  21. #include <arpa/inet.h> /* inet_ntoa */
  22. #include <netdb.h> /* struct hostent */
  23. #ifdef NEED_GHN_PROTO
  24. extern "C" int gethostname (char *name, size_t namelen);
  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. /* richm: ipv6 cleanup - use inet_aton or other more appropriate function
  66. instead of inet_addr */
  67. if((iaddr.inet.ip = inet_addr(ip)) == (in_addr_t)-1)
  68. goto bong;
  69. /*
  70. * See if it happens to be the localhost IP address, and try
  71. * the local host name if so.
  72. */
  73. if (laddr == 0) {
  74. laddr = inet_addr("127.0.0.1");
  75. myhostname[0] = 0;
  76. PR_GetSystemInfo(PR_SI_HOSTNAME, myhostname, sizeof(myhostname));
  77. }
  78. /* Have to match the localhost IP address and have a hostname */
  79. if ((iaddr.inet.ip == laddr) && (myhostname[0] != 0)) {
  80. /*
  81. * Now try for a fully-qualified domain name, starting with
  82. * the local hostname.
  83. */
  84. err = PR_GetHostByName(myhostname,
  85. buf,
  86. PR_NETDB_BUF_SIZE,
  87. &hent);
  88. /* Don't verify if we get a fully-qualified name this way */
  89. verify = 0;
  90. }
  91. else {
  92. err = PR_GetHostByAddr(&iaddr,
  93. buf,
  94. PR_NETDB_BUF_SIZE,
  95. &hent);
  96. }
  97. if ((err == PR_FAILURE) || !(hn = net_find_fqdn(&hent))) goto bong;
  98. if(verify) {
  99. char **haddr = 0;
  100. err = PR_GetHostByName(hn,
  101. buf,
  102. PR_NETDB_BUF_SIZE,
  103. &hent);
  104. if(err == PR_SUCCESS) {
  105. for(haddr = hent.h_addr_list; *haddr; haddr++) {
  106. if(((struct in_addr *)(*haddr))->s_addr == iaddr.inet.ip)
  107. break;
  108. }
  109. }
  110. if((err == PR_FAILURE) || (!(*haddr)))
  111. goto bong;
  112. }
  113. return hn;
  114. bong:
  115. return NULL;
  116. }