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