VirtualBox

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

Last change on this file since 48489 was 48489, checked in by vboxsync, 12 years ago

hostMonitoringRoutine: warnings.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.1 KB
Line 
1/* $Id: HostDnsServiceDarwin.cpp 48489 2013-09-16 15:03:45Z 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#include "../HostDnsService.h"
22
23#include <iprt/err.h>
24#include <iprt/thread.h>
25#include <iprt/semaphore.h>
26
27#include <CoreFoundation/CoreFoundation.h>
28#include <SystemConfiguration/SCDynamicStore.h>
29
30
31
32SCDynamicStoreRef g_store;
33CFRunLoopSourceRef g_DnsWatcher;
34CFRunLoopRef g_RunLoopRef;
35RTTHREAD g_DnsMonitoringThread;
36RTSEMEVENT g_DnsInitEvent;
37
38static const CFStringRef kStateNetworkGlobalDNSKey = CFSTR("State:/Network/Global/DNS");
39
40static int hostMonitoringRoutine(RTTHREAD ThreadSelf, void *pvUser)
41{
42 NOREF(ThreadSelf);
43 NOREF(pvUser);
44 g_RunLoopRef = CFRunLoopGetCurrent();
45 AssertReturn(g_RunLoopRef, VERR_INTERNAL_ERROR);
46
47 CFRetain(g_RunLoopRef);
48
49 CFArrayRef watchingArrayRef = CFArrayCreate(NULL,
50 (const void **)&kStateNetworkGlobalDNSKey,
51 1, &kCFTypeArrayCallBacks);
52 if (!watchingArrayRef)
53 {
54 CFRelease(g_DnsWatcher);
55 return E_OUTOFMEMORY;
56 }
57
58 if(SCDynamicStoreSetNotificationKeys(g_store, watchingArrayRef, NULL))
59 CFRunLoopAddSource(CFRunLoopGetCurrent(), g_DnsWatcher, kCFRunLoopCommonModes);
60
61 CFRelease(watchingArrayRef);
62
63 RTSemEventSignal(g_DnsInitEvent);
64
65 CFRunLoopRun();
66
67 CFRelease(g_RunLoopRef);
68
69 return VINF_SUCCESS;
70}
71
72HostDnsServiceDarwin::HostDnsServiceDarwin(){}
73HostDnsServiceDarwin::~HostDnsServiceDarwin()
74{
75 CFRelease(g_DnsWatcher);
76 CFRelease(g_store);
77}
78
79void HostDnsServiceDarwin::hostDnsServiceStoreCallback(void *arg0, void *arg1, void *info)
80{
81 HostDnsServiceDarwin *pThis = (HostDnsServiceDarwin *)info;
82
83 NOREF(arg0); /* SCDynamicStore */
84 NOREF(arg1); /* CFArrayRef */
85
86 RTCritSectEnter(&pThis->m_hCritSect);
87 pThis->update();
88 RTCritSectLeave(&pThis->m_hCritSect);
89}
90
91HRESULT HostDnsServiceDarwin::init()
92{
93 SCDynamicStoreContext ctx;
94 RT_ZERO(ctx);
95
96 ctx.info = this;
97
98 g_store = SCDynamicStoreCreate(NULL, CFSTR("org.virtualbox.VBoxSVC"),
99 (SCDynamicStoreCallBack)HostDnsServiceDarwin::hostDnsServiceStoreCallback,
100 &ctx);
101 AssertReturn(g_store, E_FAIL);
102
103 g_DnsWatcher = SCDynamicStoreCreateRunLoopSource(NULL, g_store, 0);
104 if (!g_DnsWatcher)
105 return E_OUTOFMEMORY;
106
107 HRESULT hrc = HostDnsService::init();
108 AssertComRCReturn(hrc, hrc);
109
110 int rc = RTSemEventCreate(&g_DnsInitEvent);
111 AssertRCReturn(rc, E_FAIL);
112
113 return update();
114}
115
116
117
118HRESULT HostDnsServiceDarwin::start()
119{
120 int rc = RTThreadCreate(&g_DnsMonitoringThread, hostMonitoringRoutine,
121 this, 128 * _1K, RTTHREADTYPE_IO, 0, "dns-monitor");
122 AssertRCReturn(rc, E_FAIL);
123
124 RTSemEventWait(g_DnsInitEvent, RT_INDEFINITE_WAIT);
125
126 return S_OK;
127}
128
129
130void HostDnsServiceDarwin::stop()
131{
132
133 if (g_RunLoopRef)
134 CFRunLoopStop(g_RunLoopRef);
135}
136
137
138HRESULT HostDnsServiceDarwin::update()
139{
140 m_llNameServers.clear();
141 m_llSearchStrings.clear();
142 m_DomainName.setNull();
143
144 CFPropertyListRef propertyRef = SCDynamicStoreCopyValue(g_store,
145 kStateNetworkGlobalDNSKey);
146 /**
147 * 0:vvl@nb-mbp-i7-2(0)# scutil
148 * > get State:/Network/Global/DNS
149 * > d.show
150 * <dictionary> {
151 * DomainName : vvl-domain
152 * SearchDomains : <array> {
153 * 0 : vvl-domain
154 * 1 : de.vvl-domain.com
155 * }
156 * ServerAddresses : <array> {
157 * 0 : 192.168.1.4
158 * 1 : 192.168.1.1
159 * 2 : 8.8.4.4
160 * }
161 * }
162 */
163
164 CFStringRef domainNameRef = (CFStringRef)CFDictionaryGetValue(
165 static_cast<CFDictionaryRef>(propertyRef), CFSTR("DomainName"));
166 if (domainNameRef)
167 {
168 const char *pszDomainName = CFStringGetCStringPtr(domainNameRef,
169 CFStringGetSystemEncoding());
170 if (pszDomainName)
171 m_DomainName = com::Utf8Str(pszDomainName);
172 }
173
174 int i, arrayCount;
175 CFArrayRef serverArrayRef = (CFArrayRef)CFDictionaryGetValue(
176 static_cast<CFDictionaryRef>(propertyRef), CFSTR("ServerAddresses"));
177 if (serverArrayRef)
178 {
179 arrayCount = CFArrayGetCount(serverArrayRef);
180 for (i = 0; i < arrayCount; ++i)
181 {
182 CFStringRef serverAddressRef = (CFStringRef)CFArrayGetValueAtIndex(serverArrayRef, i);
183 if (!serverArrayRef)
184 continue;
185
186 const char *pszServerAddress = CFStringGetCStringPtr(serverAddressRef,
187 CFStringGetSystemEncoding());
188 if (!pszServerAddress)
189 continue;
190
191 m_llNameServers.push_back(com::Utf8Str(pszServerAddress));
192 }
193 }
194
195 CFArrayRef searchArrayRef = (CFArrayRef)CFDictionaryGetValue(
196 static_cast<CFDictionaryRef>(propertyRef), CFSTR("SearchDomains"));
197 if (searchArrayRef)
198 {
199 arrayCount = CFArrayGetCount(searchArrayRef);
200
201 for (i = 0; i < arrayCount; ++i)
202 {
203 CFStringRef searchStringRef = (CFStringRef)CFArrayGetValueAtIndex(searchArrayRef, i);
204 if (!searchArrayRef)
205 continue;
206
207 const char *pszSearchString = CFStringGetCStringPtr(searchStringRef,
208 CFStringGetSystemEncoding());
209 if (!pszSearchString)
210 continue;
211
212 m_llSearchStrings.push_back(com::Utf8Str(pszSearchString));
213 }
214 }
215
216 CFRelease(propertyRef);
217 return S_OK;
218}
Note: See TracBrowser for help on using the repository browser.

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