Debian Codegraph @

libmowgli-2/src/libmowgli/dns/evloop_reslist_win32.c

raw
1 /*
2 * reslist.c - get nameservers from windows *
3 *
4 * Copyright 1998 by the Massachusetts Institute of Technology.
5 * Copyright (C) 2007-2008 by Daniel Stenberg
6 *
7 * Permission to use, copy, modify, and distribute this
8 * software and its documentation for any purpose and without
9 * fee is hereby granted, provided that the above copyright
10 * notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting
12 * documentation, and that the name of M.I.T. not be used in
13 * advertising or publicity pertaining to distribution of the
14 * software without specific, written prior permission.
15 * M.I.T. makes no representations about the suitability of
16 * this software for any purpose. It is provided "as is"
17 * without express or implied warranty.
18 */
19
20 #include "mowgli.h"
21
22 #ifdef MOWGLI_OS_WIN
23 # include <windows.h>
24 # include <iphlpapi.h>
25
26 int
27 mowgli_dns_get_windows_nameservers(char *ret_buf, size_t ret_size)
28 {
29 FIXED_INFO *fixedinfo, tfixedinfo;
30 DWORD size = sizeof(*fixedinfo);
31
32 typedef DWORD (WINAPI * get_net_param_func)(FIXED_INFO *, DWORD *);
33 get_net_param_func get_network_params;
34 HMODULE handle;
35 IP_ADDR_STRING *ip_addr;
36 int i, count = 0;
37 size_t ip_size = sizeof("255.255.255.255");
38 size_t left = ret_size;
39 char *ret = ret_buf;
40 HRESULT res;
41
42 if (!(handle = LoadLibrary("iphlpapi.dll")))
43 return 0;
44
45 if (!(get_network_params = (get_net_param_func) GetProcAddress(handle, "GetNetworkParams")))
46 goto quit;
47
48 res = (*get_network_params)(&tfixedinfo, &size);
49
50 if ((res != ERROR_BUFFER_OVERFLOW) && (res != ERROR_SUCCESS))
51 goto quit;
52
53 fixedinfo = mowgli_alloc(size);
54
55 if (!fixedinfo || ((*get_network_params)(fixedinfo, &size) != ERROR_SUCCESS))
56 goto quit;
57
58 # ifdef DEBUG
59 mowgli_log("Host Name: %s\n", fixedinfo->HostName);
60 mowgli_log("Domain Name: %s\n", fixedinfo->DomainName);
61 mowgli_log("DNS Servers:\n\t%s (primary)\n", fixedinfo->DnsServerList.IpAddress.String);
62 # endif
63
64 if ((strlen(fixedinfo->DnsServerList.IpAddress.String) > 0) &&
65 (inet_addr(fixedinfo->DnsServerList.IpAddress.String) != INADDR_NONE) && (left > ip_size))
66 {
67 ret += sprintf(ret, "%s,", fixedinfo->DnsServerList.IpAddress.String);
68 left -= ret - ret_buf;
69 count++;
70 }
71
72 for (i = 0, ip_addr = fixedinfo->DnsServerList.Next; ip_addr && left > ip_size; ip_addr = ip_addr->Next, i++)
73 {
74 if (inet_addr(ip_addr->IpAddress.String) != INADDR_NONE)
75 {
76 ret += sprintf(ret, "%s,", ip_addr->IpAddress.String);
77 left -= ret - ret_buf;
78 count++;
79 }
80
81 # ifdef DEBUG
82 mowgli_log("\t%s (secondary %d)\n", ip_addr->IpAddress.String, i + 1);
83 # endif
84 }
85
86 mowgli_free(fixedinfo);
87
88 quit:
89
90 if (handle)
91 FreeLibrary(handle);
92
93 if (left <= ip_size)
94 mowgli_log("Too many nameservers. Truncating to %d addressess", count);
95
96 if (ret > ret_buf)
97 ret[-1] = '\0';
98
99 return count;
100 }
101
102 #endif