VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/darwin/HostDnsServiceDarwin.cpp@ 49963

Last change on this file since 49963 was 49243, checked in by vboxsync, 11 years ago

Main/HostDnsServiceDarwin: parent has been changed HostDnsService -> HostDnsMonitor::init();

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.0 KB
Line 
1/* $Id: HostDnsServiceDarwin.cpp 49243 2013-10-22 19:38:39Z vboxsync $ */
2/** @file
3 * Darwin specific DNS information fetching.
4 */
5
6/*
7 * Copyright (C) 2004-2013 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#include <VBox/com/string.h>
19#include <VBox/com/ptr.h>
20
21
22#include <iprt/err.h>
23#include <iprt/thread.h>
24#include <iprt/semaphore.h>
25
26#include <CoreFoundation/CoreFoundation.h>
27#include <SystemConfiguration/SCDynamicStore.h>
28
29#include <string>
30#include <vector>
31#include "../HostDnsService.h"
32
33
34
35SCDynamicStoreRef g_store;
36CFRunLoopSourceRef g_DnsWatcher;
37CFRunLoopRef g_RunLoopRef;
38RTTHREAD g_DnsMonitoringThread;
39RTSEMEVENT g_DnsInitEvent;
40
41static const CFStringRef kStateNetworkGlobalDNSKey = CFSTR("State:/Network/Global/DNS");
42
43static int hostMonitoringRoutine(RTTHREAD ThreadSelf, void *pvUser)
44{
45 NOREF(ThreadSelf);
46 NOREF(pvUser);
47 g_RunLoopRef = CFRunLoopGetCurrent();
48 AssertReturn(g_RunLoopRef, VERR_INTERNAL_ERROR);
49
50 CFRetain(g_RunLoopRef);
51
52 CFArrayRef watchingArrayRef = CFArrayCreate(NULL,
53 (const void **)&kStateNetworkGlobalDNSKey,
54 1, &kCFTypeArrayCallBacks);
55 if (!watchingArrayRef)
56 {
57 CFRelease(g_DnsWatcher);
58 return E_OUTOFMEMORY;
59 }
60
61 if(SCDynamicStoreSetNotificationKeys(g_store, watchingArrayRef, NULL))
62 CFRunLoopAddSource(CFRunLoopGetCurrent(), g_DnsWatcher, kCFRunLoopCommonModes);
63
64 CFRelease(watchingArrayRef);
65
66 RTSemEventSignal(g_DnsInitEvent);
67
68 CFRunLoopRun();
69
70 CFRelease(g_RunLoopRef);
71
72 return VINF_SUCCESS;
73}
74
75
76HostDnsServiceDarwin::HostDnsServiceDarwin(){}
77
78
79HostDnsServiceDarwin::~HostDnsServiceDarwin()
80{
81 if (g_RunLoopRef)
82 CFRunLoopStop(g_RunLoopRef);
83
84 CFRelease(g_DnsWatcher);
85
86 CFRelease(g_store);
87}
88
89
90void HostDnsServiceDarwin::hostDnsServiceStoreCallback(void *arg0, void *arg1, void *info)
91{
92 HostDnsServiceDarwin *pThis = (HostDnsServiceDarwin *)info;
93
94 NOREF(arg0); /* SCDynamicStore */
95 NOREF(arg1); /* CFArrayRef */
96
97 ALock l(pThis);
98 pThis->updateInfo();
99 pThis->notifyAll();
100}
101
102
103HRESULT HostDnsServiceDarwin::init()
104{
105 SCDynamicStoreContext ctx;
106 RT_ZERO(ctx);
107
108 ctx.info = this;
109
110 g_store = SCDynamicStoreCreate(NULL, CFSTR("org.virtualbox.VBoxSVC"),
111 (SCDynamicStoreCallBack)HostDnsServiceDarwin::hostDnsServiceStoreCallback,
112 &ctx);
113 AssertReturn(g_store, E_FAIL);
114
115 g_DnsWatcher = SCDynamicStoreCreateRunLoopSource(NULL, g_store, 0);
116 if (!g_DnsWatcher)
117 return E_OUTOFMEMORY;
118
119 HRESULT hrc = HostDnsMonitor::init();
120 AssertComRCReturn(hrc, hrc);
121
122 int rc = RTSemEventCreate(&g_DnsInitEvent);
123 AssertRCReturn(rc, E_FAIL);
124
125 rc = RTThreadCreate(&g_DnsMonitoringThread, hostMonitoringRoutine,
126 this, 128 * _1K, RTTHREADTYPE_IO, 0, "dns-monitor");
127 AssertRCReturn(rc, E_FAIL);
128
129 RTSemEventWait(g_DnsInitEvent, RT_INDEFINITE_WAIT);
130 return updateInfo();
131}
132
133
134HRESULT HostDnsServiceDarwin::updateInfo()
135{
136 CFPropertyListRef propertyRef = SCDynamicStoreCopyValue(g_store,
137 kStateNetworkGlobalDNSKey);
138 /**
139 * 0:vvl@nb-mbp-i7-2(0)# scutil
140 * > get State:/Network/Global/DNS
141 * > d.show
142 * <dictionary> {
143 * DomainName : vvl-domain
144 * SearchDomains : <array> {
145 * 0 : vvl-domain
146 * 1 : de.vvl-domain.com
147 * }
148 * ServerAddresses : <array> {
149 * 0 : 192.168.1.4
150 * 1 : 192.168.1.1
151 * 2 : 8.8.4.4
152 * }
153 * }
154 */
155
156 if (!propertyRef)
157 return S_OK;
158
159 HostDnsInformation info;
160 CFStringRef domainNameRef = (CFStringRef)CFDictionaryGetValue(
161 static_cast<CFDictionaryRef>(propertyRef), CFSTR("DomainName"));
162 if (domainNameRef)
163 {
164 const char *pszDomainName = CFStringGetCStringPtr(domainNameRef,
165 CFStringGetSystemEncoding());
166 if (pszDomainName)
167 info.domain = pszDomainName;
168 }
169
170 int i, arrayCount;
171 CFArrayRef serverArrayRef = (CFArrayRef)CFDictionaryGetValue(
172 static_cast<CFDictionaryRef>(propertyRef), CFSTR("ServerAddresses"));
173 if (serverArrayRef)
174 {
175 arrayCount = CFArrayGetCount(serverArrayRef);
176 for (i = 0; i < arrayCount; ++i)
177 {
178 CFStringRef serverAddressRef = (CFStringRef)CFArrayGetValueAtIndex(serverArrayRef, i);
179 if (!serverArrayRef)
180 continue;
181
182 const char *pszServerAddress = CFStringGetCStringPtr(serverAddressRef,
183 CFStringGetSystemEncoding());
184 if (!pszServerAddress)
185 continue;
186
187 info.servers.push_back(std::string(pszServerAddress));
188 }
189 }
190
191 CFArrayRef searchArrayRef = (CFArrayRef)CFDictionaryGetValue(
192 static_cast<CFDictionaryRef>(propertyRef), CFSTR("SearchDomains"));
193 if (searchArrayRef)
194 {
195 arrayCount = CFArrayGetCount(searchArrayRef);
196
197 for (i = 0; i < arrayCount; ++i)
198 {
199 CFStringRef searchStringRef = (CFStringRef)CFArrayGetValueAtIndex(searchArrayRef, i);
200 if (!searchArrayRef)
201 continue;
202
203 const char *pszSearchString = CFStringGetCStringPtr(searchStringRef,
204 CFStringGetSystemEncoding());
205 if (!pszSearchString)
206 continue;
207
208 info.searchList.push_back(std::string(pszSearchString));
209 }
210 }
211
212 CFRelease(propertyRef);
213
214 setInfo(info);
215
216 return S_OK;
217}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette