dns.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. /*
  39. * dns.c: DNS resolution routines
  40. *
  41. * Rob McCool
  42. */
  43. #define DNS_GUESSING
  44. #include "netsite.h"
  45. #ifdef XP_UNIX
  46. #include "systems.h"
  47. #else /* XP_WIN32 */
  48. #include "base/systems.h"
  49. #endif /* XP_WIN32 */
  50. /* Under NT, these are taken care of by net.h including winsock.h */
  51. #ifdef XP_UNIX
  52. #include <arpa/inet.h> /* inet_ntoa */
  53. #include <netdb.h> /* struct hostent */
  54. #ifdef NEED_GHN_PROTO
  55. extern "C" int gethostname (char *name, size_t namelen);
  56. #endif
  57. #endif
  58. #include <stdio.h>
  59. #include <nspr.h>
  60. /* ---------------------------- dns_find_fqdn ----------------------------- */
  61. /* defined in dnsdmain.c */
  62. extern "C" NSAPI_PUBLIC char *dns_guess_domain(char * hname);
  63. char *net_find_fqdn(PRHostEnt *p)
  64. {
  65. int x;
  66. if((!p->h_name) || (!p->h_aliases))
  67. return NULL;
  68. if(!strchr(p->h_name, '.')) {
  69. for(x = 0; p->h_aliases[x]; ++x) {
  70. if((strchr(p->h_aliases[x], '.')) &&
  71. (!strncmp(p->h_aliases[x], p->h_name, strlen(p->h_name))))
  72. {
  73. return STRDUP(p->h_aliases[x]);
  74. }
  75. }
  76. #ifdef DNS_GUESSING
  77. return dns_guess_domain(p->h_name);
  78. #else
  79. return NULL;
  80. #endif /* DNS_GUESSING */
  81. }
  82. else
  83. return STRDUP(p->h_name);
  84. }
  85. /* ----------------------------- dns_ip2host ------------------------------ */
  86. char *dns_ip2host(char *ip, int verify)
  87. {
  88. /* struct in_addr iaddr; */
  89. PRNetAddr iaddr;
  90. char *hn;
  91. static unsigned long laddr = 0;
  92. static char myhostname[256];
  93. PRHostEnt hent;
  94. char buf[PR_NETDB_BUF_SIZE];
  95. PRStatus err;
  96. err = PR_InitializeNetAddr(PR_IpAddrNull, 0, &iaddr);
  97. if((iaddr.inet.ip = inet_addr(ip)) == -1)
  98. goto bong;
  99. /*
  100. * See if it happens to be the localhost IP address, and try
  101. * the local host name if so.
  102. */
  103. if (laddr == 0) {
  104. laddr = inet_addr("127.0.0.1");
  105. myhostname[0] = 0;
  106. PR_GetSystemInfo(PR_SI_HOSTNAME, myhostname, sizeof(myhostname));
  107. }
  108. /* Have to match the localhost IP address and have a hostname */
  109. if ((iaddr.inet.ip == laddr) && (myhostname[0] != 0)) {
  110. /*
  111. * Now try for a fully-qualified domain name, starting with
  112. * the local hostname.
  113. */
  114. err = PR_GetHostByName(myhostname,
  115. buf,
  116. PR_NETDB_BUF_SIZE,
  117. &hent);
  118. /* Don't verify if we get a fully-qualified name this way */
  119. verify = 0;
  120. }
  121. else {
  122. err = PR_GetHostByAddr(&iaddr,
  123. buf,
  124. PR_NETDB_BUF_SIZE,
  125. &hent);
  126. }
  127. if ((err == PR_FAILURE) || !(hn = net_find_fqdn(&hent))) goto bong;
  128. if(verify) {
  129. char **haddr = 0;
  130. err = PR_GetHostByName(hn,
  131. buf,
  132. PR_NETDB_BUF_SIZE,
  133. &hent);
  134. if(err == PR_SUCCESS) {
  135. for(haddr = hent.h_addr_list; *haddr; haddr++) {
  136. if(((struct in_addr *)(*haddr))->s_addr == iaddr.inet.ip)
  137. break;
  138. }
  139. }
  140. if((err == PR_FAILURE) || (!(*haddr)))
  141. goto bong;
  142. }
  143. return hn;
  144. bong:
  145. return NULL;
  146. }