1 | /* -*- indent-tabs-mode: nil; -*- */
|
---|
2 | #include <VBox/com/string.h>
|
---|
3 | #include <VBox/com/ptr.h>
|
---|
4 |
|
---|
5 |
|
---|
6 | #include <iprt/assert.h>
|
---|
7 | #include <iprt/err.h>
|
---|
8 |
|
---|
9 | #include <Windows.h>
|
---|
10 |
|
---|
11 | #include <string>
|
---|
12 | #include <vector>
|
---|
13 | #include "../HostDnsService.h"
|
---|
14 |
|
---|
15 | static HKEY g_hKeyTcpipParameters;
|
---|
16 |
|
---|
17 | HostDnsServiceWin::HostDnsServiceWin()
|
---|
18 | {
|
---|
19 | RegOpenKeyEx(HKEY_LOCAL_MACHINE,
|
---|
20 | TEXT("SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters"),
|
---|
21 | 0, KEY_READ, &g_hKeyTcpipParameters);
|
---|
22 | }
|
---|
23 |
|
---|
24 |
|
---|
25 | HostDnsServiceWin::~HostDnsServiceWin()
|
---|
26 | {
|
---|
27 | if (!g_hKeyTcpipParameters)
|
---|
28 | {
|
---|
29 | RegCloseKey(g_hKeyTcpipParameters);
|
---|
30 | g_hKeyTcpipParameters = 0;
|
---|
31 | }
|
---|
32 | }
|
---|
33 |
|
---|
34 |
|
---|
35 | HRESULT HostDnsServiceWin::init()
|
---|
36 | {
|
---|
37 | HRESULT hrc = HostDnsMonitor::init();
|
---|
38 | AssertComRCReturn(hrc, hrc);
|
---|
39 |
|
---|
40 | return updateInfo();
|
---|
41 | }
|
---|
42 |
|
---|
43 |
|
---|
44 | HRESULT HostDnsServiceWin::updateInfo()
|
---|
45 | {
|
---|
46 | HRESULT hrc;
|
---|
47 | DWORD regIndex;
|
---|
48 | BYTE abDomain[256];
|
---|
49 | BYTE abNameServers[256];
|
---|
50 | BYTE abSearchList[256];
|
---|
51 |
|
---|
52 | RT_ZERO(abDomain);
|
---|
53 | RT_ZERO(abNameServers);
|
---|
54 | RT_ZERO(abSearchList);
|
---|
55 |
|
---|
56 | regIndex = 0;
|
---|
57 | do {
|
---|
58 | CHAR keyName[256];
|
---|
59 | DWORD cbKeyName = sizeof(keyName);
|
---|
60 | DWORD keyType = 0;
|
---|
61 | BYTE keyData[1024];
|
---|
62 | DWORD cbKeyData = sizeof(keyData);
|
---|
63 |
|
---|
64 | hrc = RegEnumValueA(g_hKeyTcpipParameters, regIndex, keyName, &cbKeyName, 0,
|
---|
65 | &keyType, keyData, &cbKeyData);
|
---|
66 | if ( hrc == ERROR_SUCCESS
|
---|
67 | || hrc == ERROR_MORE_DATA)
|
---|
68 | {
|
---|
69 | if ( RTStrICmp("Domain", keyName) == 0
|
---|
70 | && cbKeyData > 1
|
---|
71 | && cbKeyData < sizeof(abDomain))
|
---|
72 | memcpy(abDomain, keyData, cbKeyData);
|
---|
73 |
|
---|
74 | else if ( RTStrICmp("DhcpDomain", keyName) == 0
|
---|
75 | && cbKeyData > 1
|
---|
76 | && abDomain[0] == 0
|
---|
77 | && cbKeyData < sizeof(abDomain))
|
---|
78 | memcpy(abDomain, keyData, cbKeyData);
|
---|
79 |
|
---|
80 | else if ( RTStrICmp("NameServer", keyName) == 0
|
---|
81 | && cbKeyData > 1
|
---|
82 | && cbKeyData < sizeof(abNameServers))
|
---|
83 | memcpy(abNameServers, keyData, cbKeyData);
|
---|
84 |
|
---|
85 | else if ( RTStrICmp("DhcpNameServer", keyName) == 0
|
---|
86 | && cbKeyData > 1
|
---|
87 | && abNameServers[0] == 0
|
---|
88 | && cbKeyData < sizeof(abNameServers))
|
---|
89 | memcpy(abNameServers, keyData, cbKeyData);
|
---|
90 |
|
---|
91 | else if ( RTStrICmp("SearchList", keyName) == 0
|
---|
92 | && cbKeyData > 1
|
---|
93 | && cbKeyData < sizeof(abSearchList))
|
---|
94 | memcpy(abSearchList, keyData, cbKeyData);
|
---|
95 | }
|
---|
96 | regIndex++;
|
---|
97 | } while (hrc != ERROR_NO_MORE_ITEMS);
|
---|
98 |
|
---|
99 | /* OK, now parse and update DNS structures. */
|
---|
100 | /* domain name */
|
---|
101 | HostDnsInformation info;
|
---|
102 | info.domain = (char*)abDomain;
|
---|
103 |
|
---|
104 | /* server list */
|
---|
105 | strList2List(info.servers, (char *)abNameServers);
|
---|
106 | /* search list */
|
---|
107 | strList2List(info.searchList, (char *)abSearchList);
|
---|
108 |
|
---|
109 | HostDnsMonitor::setInfo(info);
|
---|
110 |
|
---|
111 | return S_OK;
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 |
|
---|
116 | void HostDnsServiceWin::strList2List(std::vector<std::string>& lst, char *strLst)
|
---|
117 | {
|
---|
118 | char *next, *current;
|
---|
119 | char address[512];
|
---|
120 |
|
---|
121 | AssertPtrReturnVoid(strLst);
|
---|
122 |
|
---|
123 | if (strlen(strLst) == 0)
|
---|
124 | return;
|
---|
125 |
|
---|
126 | current = strLst;
|
---|
127 | do {
|
---|
128 | RT_ZERO(address);
|
---|
129 | next = RTStrStr(current, " ");
|
---|
130 |
|
---|
131 | if (next)
|
---|
132 | strncpy(address, current, RT_MIN(sizeof(address)-1, next - current));
|
---|
133 | else
|
---|
134 | strcpy(address, current);
|
---|
135 |
|
---|
136 | lst.push_back(std::string(address));
|
---|
137 |
|
---|
138 | current = next + 1;
|
---|
139 | } while(next);
|
---|
140 |
|
---|
141 | }
|
---|