You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

231 lines
6.7 KiB

  1. /* udns_init.c
  2. resolver initialisation stuff
  3. Copyright (C) 2006 Michael Tokarev <mjt@corpit.ru>
  4. This file is part of UDNS library, an async DNS stub resolver.
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library, in file named COPYING.LGPL; if not,
  15. write to the Free Software Foundation, Inc., 59 Temple Place,
  16. Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. # include "config.h"
  20. #endif
  21. #ifdef WINDOWS
  22. # include <winsock2.h> /* includes <windows.h> */
  23. # include <iphlpapi.h> /* for dns server addresses etc */
  24. #else
  25. # include <sys/types.h>
  26. # include <unistd.h>
  27. # include <fcntl.h>
  28. #endif /* !WINDOWS */
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include "udns.h"
  32. #define ISSPACE(x) (x == ' ' || x == '\t' || x == '\r' || x == '\n')
  33. static const char space[] = " \t\r\n";
  34. static void dns_set_serv_internal(struct dns_ctx *ctx, char *serv) {
  35. dns_add_serv(ctx, NULL);
  36. for(serv = strtok(serv, space); serv; serv = strtok(NULL, space))
  37. dns_add_serv(ctx, serv);
  38. }
  39. static void dns_set_srch_internal(struct dns_ctx *ctx, char *srch) {
  40. dns_add_srch(ctx, NULL);
  41. for(srch = strtok(srch, space); srch; srch = strtok(NULL, space))
  42. dns_add_srch(ctx, srch);
  43. }
  44. #ifdef WINDOWS
  45. #ifndef NO_IPHLPAPI
  46. /* Apparently, some systems does not have proper headers for IPHLPAIP to work.
  47. * The best is to upgrade headers, but here's another, ugly workaround for
  48. * this: compile with -DNO_IPHLPAPI.
  49. */
  50. typedef DWORD (WINAPI *GetAdaptersAddressesFunc)(
  51. ULONG Family, DWORD Flags, PVOID Reserved,
  52. PIP_ADAPTER_ADDRESSES pAdapterAddresses,
  53. PULONG pOutBufLen);
  54. static int dns_initns_iphlpapi(struct dns_ctx *ctx) {
  55. HANDLE h_iphlpapi;
  56. GetAdaptersAddressesFunc pfnGetAdAddrs;
  57. PIP_ADAPTER_ADDRESSES pAddr, pAddrBuf;
  58. PIP_ADAPTER_DNS_SERVER_ADDRESS pDnsAddr;
  59. ULONG ulOutBufLen;
  60. DWORD dwRetVal;
  61. int ret = -1;
  62. h_iphlpapi = LoadLibrary("iphlpapi.dll");
  63. if (!h_iphlpapi)
  64. return -1;
  65. pfnGetAdAddrs = (GetAdaptersAddressesFunc)
  66. GetProcAddress(h_iphlpapi, "GetAdaptersAddresses");
  67. if (!pfnGetAdAddrs) goto freelib;
  68. ulOutBufLen = 0;
  69. dwRetVal = pfnGetAdAddrs(AF_UNSPEC, 0, NULL, NULL, &ulOutBufLen);
  70. if (dwRetVal != ERROR_BUFFER_OVERFLOW) goto freelib;
  71. pAddrBuf = malloc(ulOutBufLen);
  72. if (!pAddrBuf) goto freelib;
  73. dwRetVal = pfnGetAdAddrs(AF_UNSPEC, 0, NULL, pAddrBuf, &ulOutBufLen);
  74. if (dwRetVal != ERROR_SUCCESS) goto freemem;
  75. for (pAddr = pAddrBuf; pAddr; pAddr = pAddr->Next)
  76. for (pDnsAddr = pAddr->FirstDnsServerAddress;
  77. pDnsAddr;
  78. pDnsAddr = pDnsAddr->Next)
  79. dns_add_serv_s(ctx, pDnsAddr->Address.lpSockaddr);
  80. ret = 0;
  81. freemem:
  82. free(pAddrBuf);
  83. freelib:
  84. FreeLibrary(h_iphlpapi);
  85. return ret;
  86. }
  87. #else /* NO_IPHLPAPI */
  88. #define dns_initns_iphlpapi(ctx) (-1)
  89. #endif /* NO_IPHLPAPI */
  90. static int dns_initns_registry(struct dns_ctx *ctx) {
  91. LONG res;
  92. HKEY hk;
  93. DWORD type = REG_EXPAND_SZ | REG_SZ;
  94. DWORD len;
  95. char valBuf[1024];
  96. #define REGKEY_WINNT "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters"
  97. #define REGKEY_WIN9x "SYSTEM\\CurrentControlSet\\Services\\VxD\\MSTCP"
  98. res = RegOpenKeyEx(HKEY_LOCAL_MACHINE, REGKEY_WINNT, 0, KEY_QUERY_VALUE, &hk);
  99. if (res != ERROR_SUCCESS)
  100. res = RegOpenKeyEx(HKEY_LOCAL_MACHINE, REGKEY_WIN9x,
  101. 0, KEY_QUERY_VALUE, &hk);
  102. if (res != ERROR_SUCCESS)
  103. return -1;
  104. len = sizeof(valBuf) - 1;
  105. res = RegQueryValueEx(hk, "NameServer", NULL, &type, (BYTE*)valBuf, &len);
  106. if (res != ERROR_SUCCESS || !len || !valBuf[0]) {
  107. len = sizeof(valBuf) - 1;
  108. res = RegQueryValueEx(hk, "DhcpNameServer", NULL, &type,
  109. (BYTE*)valBuf, &len);
  110. }
  111. RegCloseKey(hk);
  112. if (res != ERROR_SUCCESS || !len || !valBuf[0])
  113. return -1;
  114. valBuf[len] = '\0';
  115. /* nameservers are stored as a whitespace-seperate list:
  116. * "192.168.1.1 123.21.32.12" */
  117. dns_set_serv_internal(ctx, valBuf);
  118. return 0;
  119. }
  120. #else /* !WINDOWS */
  121. static int dns_init_resolvconf(struct dns_ctx *ctx) {
  122. char *v;
  123. char buf[2049]; /* this buffer is used to hold /etc/resolv.conf */
  124. int has_srch = 0;
  125. /* read resolv.conf... */
  126. { int fd = open("/etc/resolv.conf", O_RDONLY);
  127. if (fd >= 0) {
  128. int l = read(fd, buf, sizeof(buf) - 1);
  129. close(fd);
  130. buf[l < 0 ? 0 : l] = '\0';
  131. }
  132. else
  133. buf[0] = '\0';
  134. }
  135. if (buf[0]) { /* ...and parse it */
  136. char *line, *nextline;
  137. line = buf;
  138. do {
  139. nextline = strchr(line, '\n');
  140. if (nextline) *nextline++ = '\0';
  141. v = line;
  142. while(*v && !ISSPACE(*v)) ++v;
  143. if (!*v) continue;
  144. *v++ = '\0';
  145. while(ISSPACE(*v)) ++v;
  146. if (!*v) continue;
  147. if (strcmp(line, "domain") == 0) {
  148. dns_set_srch_internal(ctx, strtok(v, space));
  149. has_srch = 1;
  150. }
  151. else if (strcmp(line, "search") == 0) {
  152. dns_set_srch_internal(ctx, v);
  153. has_srch = 1;
  154. }
  155. else if (strcmp(line, "nameserver") == 0)
  156. dns_add_serv(ctx, strtok(v, space));
  157. else if (strcmp(line, "options") == 0)
  158. dns_set_opts(ctx, v);
  159. } while((line = nextline) != NULL);
  160. }
  161. buf[sizeof(buf)-1] = '\0';
  162. /* get list of nameservers from env. vars. */
  163. if ((v = getenv("NSCACHEIP")) != NULL ||
  164. (v = getenv("NAMESERVERS")) != NULL) {
  165. strncpy(buf, v, sizeof(buf) - 1);
  166. dns_set_serv_internal(ctx, buf);
  167. }
  168. /* if $LOCALDOMAIN is set, use it for search list */
  169. if ((v = getenv("LOCALDOMAIN")) != NULL) {
  170. strncpy(buf, v, sizeof(buf) - 1);
  171. dns_set_srch_internal(ctx, buf);
  172. has_srch = 1;
  173. }
  174. if ((v = getenv("RES_OPTIONS")) != NULL)
  175. dns_set_opts(ctx, v);
  176. /* if still no search list, use local domain name */
  177. if (has_srch &&
  178. gethostname(buf, sizeof(buf) - 1) == 0 &&
  179. (v = strchr(buf, '.')) != NULL &&
  180. *++v != '\0')
  181. dns_add_srch(ctx, v);
  182. return 0;
  183. }
  184. #endif /* !WINDOWS */
  185. int dns_init(struct dns_ctx *ctx, int do_open) {
  186. if (!ctx)
  187. ctx = &dns_defctx;
  188. dns_reset(ctx);
  189. #ifdef WINDOWS
  190. if (dns_initns_iphlpapi(ctx) != 0)
  191. dns_initns_registry(ctx);
  192. /*XXX WINDOWS: probably good to get default domain and search list too...
  193. * And options. Something is in registry. */
  194. /*XXX WINDOWS: maybe environment variables are also useful? */
  195. #else
  196. dns_init_resolvconf(ctx);
  197. #endif
  198. return do_open ? dns_open(ctx) : 0;
  199. }