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.

233 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 __MINGW32__
  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 /* !__MINGW32__ */
  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 __MINGW32__
  45. #define NO_IPHLPAPI
  46. #ifndef NO_IPHLPAPI
  47. /* Apparently, some systems does not have proper headers for IPHLPAIP to work.
  48. * The best is to upgrade headers, but here's another, ugly workaround for
  49. * this: compile with -DNO_IPHLPAPI.
  50. */
  51. typedef DWORD (WINAPI *GetAdaptersAddressesFunc)(
  52. ULONG Family, DWORD Flags, PVOID Reserved,
  53. PIP_ADAPTER_ADDRESSES pAdapterAddresses,
  54. PULONG pOutBufLen);
  55. static int dns_initns_iphlpapi(struct dns_ctx *ctx) {
  56. HANDLE h_iphlpapi;
  57. GetAdaptersAddressesFunc pfnGetAdAddrs;
  58. PIP_ADAPTER_ADDRESSES pAddr, pAddrBuf;
  59. PIP_ADAPTER_DNS_SERVER_ADDRESS pDnsAddr;
  60. ULONG ulOutBufLen;
  61. DWORD dwRetVal;
  62. int ret = -1;
  63. h_iphlpapi = LoadLibrary("iphlpapi.dll");
  64. if (!h_iphlpapi)
  65. return -1;
  66. pfnGetAdAddrs = (GetAdaptersAddressesFunc)
  67. GetProcAddress(h_iphlpapi, "GetAdaptersAddresses");
  68. if (!pfnGetAdAddrs) goto freelib;
  69. ulOutBufLen = 0;
  70. dwRetVal = pfnGetAdAddrs(AF_UNSPEC, 0, NULL, NULL, &ulOutBufLen);
  71. if (dwRetVal != ERROR_BUFFER_OVERFLOW) goto freelib;
  72. pAddrBuf = malloc(ulOutBufLen);
  73. if (!pAddrBuf) goto freelib;
  74. dwRetVal = pfnGetAdAddrs(AF_UNSPEC, 0, NULL, pAddrBuf, &ulOutBufLen);
  75. if (dwRetVal != ERROR_SUCCESS) goto freemem;
  76. for (pAddr = pAddrBuf; pAddr; pAddr = pAddr->Next)
  77. for (pDnsAddr = pAddr->FirstDnsServerAddress;
  78. pDnsAddr;
  79. pDnsAddr = pDnsAddr->Next)
  80. dns_add_serv_s(ctx, pDnsAddr->Address.lpSockaddr);
  81. ret = 0;
  82. freemem:
  83. free(pAddrBuf);
  84. freelib:
  85. FreeLibrary(h_iphlpapi);
  86. return ret;
  87. }
  88. #else /* NO_IPHLPAPI */
  89. #define dns_initns_iphlpapi(ctx) (-1)
  90. #endif /* NO_IPHLPAPI */
  91. static int dns_initns_registry(struct dns_ctx *ctx) {
  92. LONG res;
  93. HKEY hk;
  94. DWORD type = REG_EXPAND_SZ | REG_SZ;
  95. DWORD len;
  96. char valBuf[1024];
  97. #define REGKEY_WINNT "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters"
  98. #define REGKEY_WIN9x "SYSTEM\\CurrentControlSet\\Services\\VxD\\MSTCP"
  99. res = RegOpenKeyEx(HKEY_LOCAL_MACHINE, REGKEY_WINNT, 0, KEY_QUERY_VALUE, &hk);
  100. if (res != ERROR_SUCCESS)
  101. res = RegOpenKeyEx(HKEY_LOCAL_MACHINE, REGKEY_WIN9x,
  102. 0, KEY_QUERY_VALUE, &hk);
  103. if (res != ERROR_SUCCESS)
  104. return -1;
  105. len = sizeof(valBuf) - 1;
  106. res = RegQueryValueEx(hk, "NameServer", NULL, &type, (BYTE*)valBuf, &len);
  107. if (res != ERROR_SUCCESS || !len || !valBuf[0]) {
  108. len = sizeof(valBuf) - 1;
  109. res = RegQueryValueEx(hk, "DhcpNameServer", NULL, &type,
  110. (BYTE*)valBuf, &len);
  111. }
  112. RegCloseKey(hk);
  113. if (res != ERROR_SUCCESS || !len || !valBuf[0])
  114. return -1;
  115. valBuf[len] = '\0';
  116. /* nameservers are stored as a whitespace-seperate list:
  117. * "192.168.1.1 123.21.32.12" */
  118. dns_set_serv_internal(ctx, valBuf);
  119. return 0;
  120. }
  121. #else /* !__MINGW32__ */
  122. static int dns_init_resolvconf(struct dns_ctx *ctx) {
  123. char *v;
  124. char buf[2049]; /* this buffer is used to hold /etc/resolv.conf */
  125. int has_srch = 0;
  126. /* read resolv.conf... */
  127. { int fd = open("/etc/resolv.conf", O_RDONLY);
  128. if (fd >= 0) {
  129. int l = read(fd, buf, sizeof(buf) - 1);
  130. close(fd);
  131. buf[l < 0 ? 0 : l] = '\0';
  132. }
  133. else
  134. buf[0] = '\0';
  135. }
  136. if (buf[0]) { /* ...and parse it */
  137. char *line, *nextline;
  138. line = buf;
  139. do {
  140. nextline = strchr(line, '\n');
  141. if (nextline) *nextline++ = '\0';
  142. v = line;
  143. while(*v && !ISSPACE(*v)) ++v;
  144. if (!*v) continue;
  145. *v++ = '\0';
  146. while(ISSPACE(*v)) ++v;
  147. if (!*v) continue;
  148. if (strcmp(line, "domain") == 0) {
  149. dns_set_srch_internal(ctx, strtok(v, space));
  150. has_srch = 1;
  151. }
  152. else if (strcmp(line, "search") == 0) {
  153. dns_set_srch_internal(ctx, v);
  154. has_srch = 1;
  155. }
  156. else if (strcmp(line, "nameserver") == 0)
  157. dns_add_serv(ctx, strtok(v, space));
  158. else if (strcmp(line, "options") == 0)
  159. dns_set_opts(ctx, v);
  160. } while((line = nextline) != NULL);
  161. }
  162. buf[sizeof(buf)-1] = '\0';
  163. /* get list of nameservers from env. vars. */
  164. if ((v = getenv("NSCACHEIP")) != NULL ||
  165. (v = getenv("NAMESERVERS")) != NULL) {
  166. strncpy(buf, v, sizeof(buf) - 1);
  167. dns_set_serv_internal(ctx, buf);
  168. }
  169. /* if $LOCALDOMAIN is set, use it for search list */
  170. if ((v = getenv("LOCALDOMAIN")) != NULL) {
  171. strncpy(buf, v, sizeof(buf) - 1);
  172. dns_set_srch_internal(ctx, buf);
  173. has_srch = 1;
  174. }
  175. if ((v = getenv("RES_OPTIONS")) != NULL)
  176. dns_set_opts(ctx, v);
  177. /* if still no search list, use local domain name */
  178. if (has_srch &&
  179. gethostname(buf, sizeof(buf) - 1) == 0 &&
  180. (v = strchr(buf, '.')) != NULL &&
  181. *++v != '\0')
  182. dns_add_srch(ctx, v);
  183. return 0;
  184. }
  185. #endif /* !__MINGW32__ */
  186. int dns_init(struct dns_ctx *ctx, int do_open) {
  187. if (!ctx)
  188. ctx = &dns_defctx;
  189. dns_reset(ctx);
  190. #ifdef __MINGW32__
  191. if (dns_initns_iphlpapi(ctx) != 0)
  192. dns_initns_registry(ctx);
  193. /*XXX __MINGW32__: probably good to get default domain and search list too...
  194. * And options. Something is in registry. */
  195. /*XXX __MINGW32__: maybe environment variables are also useful? */
  196. #else
  197. dns_init_resolvconf(ctx);
  198. #endif
  199. return do_open ? dns_open(ctx) : 0;
  200. }