dnsdmain.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. * dnsdmain.c: DNS domain guessing stuff moved out of dns.c because of the
  8. * string ball problems
  9. */
  10. #include "netsite.h"
  11. #include <string.h>
  12. #include <stdio.h>
  13. #ifdef XP_UNIX
  14. #include <unistd.h>
  15. #endif
  16. #include <ctype.h>
  17. #include "util.h"
  18. /* Under NT, this is taken care of by net.h including winsock.h */
  19. #ifdef XP_UNIX
  20. #include <netdb.h> /* struct hostent */
  21. #endif
  22. extern "C" {
  23. #include <nspr.h>
  24. }
  25. /* This is contained in resolv.h on most systems. */
  26. #define _PATH_RESCONF "/etc/resolv.conf"
  27. #ifdef XP_UNIX
  28. NSPR_BEGIN_EXTERN_C
  29. #ifdef Linux
  30. extern int getdomainname(char *, size_t);
  31. #else
  32. extern int getdomainname(char *, int);
  33. #endif /* Linux */
  34. #if defined(HPUX) || defined (UnixWare) || defined(Linux) || defined(IRIX6_5)
  35. extern int gethostname (char *name, size_t namelen);
  36. #else
  37. #ifndef AIX
  38. extern int gethostname (char *name, int namelen);
  39. #endif
  40. #endif
  41. NSPR_END_EXTERN_C
  42. #endif
  43. /* ---------------------------- dns_guess_domain -------------------------- */
  44. extern "C" NSAPI_PUBLIC char *dns_guess_domain(char * hname)
  45. {
  46. FILE *f;
  47. char * cp;
  48. int hnlen;
  49. char line[256];
  50. static int dnlen = 0;
  51. static char * domain = 0;
  52. PRHostEnt hent;
  53. char buf[PR_NETDB_BUF_SIZE];
  54. PRStatus err;
  55. /* Sanity check */
  56. if (strchr(hname, '.')) {
  57. return STRDUP(hname);
  58. }
  59. if (dnlen == 0) {
  60. /* First try a little trick that seems to often work... */
  61. /*
  62. * Get the local host name, even it doesn't come back
  63. * fully qualified.
  64. */
  65. line[0] = 0;
  66. gethostname(line, sizeof(line));
  67. if (line[0] != 0) {
  68. /* Is it fully qualified? */
  69. domain = strchr(line, '.');
  70. if (domain == 0) {
  71. /* No, try gethostbyname() */
  72. err = PR_GetHostByName(line,
  73. buf,
  74. PR_NETDB_BUF_SIZE,
  75. &hent);
  76. if (err == PR_SUCCESS) {
  77. /* See if h_name is fully-qualified */
  78. if (hent.h_name) domain = strchr(hent.h_name, '.');
  79. /* Otherwise look for a fully qualified alias */
  80. if ((domain == 0) &&
  81. (hent.h_aliases && hent.h_aliases[0])) {
  82. char **p;
  83. for (p = hent.h_aliases; *p; ++p) {
  84. domain = strchr(*p, '.');
  85. if (domain) break;
  86. }
  87. }
  88. }
  89. }
  90. }
  91. /* Still no luck? */
  92. if (domain == 0) {
  93. f = fopen(_PATH_RESCONF, "r");
  94. /* See if there's a domain entry in their resolver configuration */
  95. if(f) {
  96. while(fgets(line, sizeof(line), f)) {
  97. if(!strncasecmp(line, "domain ", 7)) {
  98. for (cp = &line[7]; *cp && isspace(*cp); ++cp) ;
  99. if (*cp) {
  100. domain = cp;
  101. for (; *cp && !isspace(*cp); ++cp) ;
  102. *cp = 0;
  103. }
  104. break;
  105. }
  106. }
  107. fclose(f);
  108. }
  109. }
  110. #ifndef NO_DOMAINNAME
  111. if (domain == 0) {
  112. /* No domain found. Try getdomainname. */
  113. getdomainname(line, sizeof(line));
  114. if (line[0] != 0) domain = &line[0];
  115. }
  116. #endif
  117. if (domain != 0) {
  118. if (domain[0] == '.') ++domain;
  119. domain = STRDUP(domain);
  120. dnlen = strlen(domain);
  121. }
  122. else dnlen = -1;
  123. }
  124. if (domain != 0) {
  125. hnlen = strlen(hname);
  126. if ((hnlen + dnlen + 2) <= sizeof(line)) {
  127. strcpy(line, hname);
  128. line[hnlen] = '.';
  129. strcpy(&line[hnlen+1], domain);
  130. return STRDUP(line);
  131. }
  132. }
  133. return 0;
  134. }