VirtualBox

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

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

hostMonitoringRoutine: warnings [-Wunused-parameter]

  • 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 48488 2013-09-16 15:02:55Z 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
70HostDnsServiceDarwin::HostDnsServiceDarwin(){}
71HostDnsServiceDarwin::~HostDnsServiceDarwin()
72{
73 CFRelease(g_DnsWatcher);
74 CFRelease(g_store);
75}
76
77void HostDnsServiceDarwin::hostDnsServiceStoreCallback(void *arg0, void *arg1, void *info)
78{
79 HostDnsServiceDarwin *pThis = (HostDnsServiceDarwin *)info;
80
81 NOREF(arg0); /* SCDynamicStore */
82 NOREF(arg1); /* CFArrayRef */
83
84 RTCritSectEnter(&pThis->m_hCritSect);
85 pThis->update();
86 RTCritSectLeave(&pThis->m_hCritSect);
87}
88
89HRESULT HostDnsServiceDarwin::init()
90{
91 SCDynamicStoreContext ctx;
92 RT_ZERO(ctx);
93
94 ctx.info = this;
95
96 g_store = SCDynamicStoreCreate(NULL, CFSTR("org.virtualbox.VBoxSVC"),
97 (SCDynamicStoreCallBack)HostDnsServiceDarwin::hostDnsServiceStoreCallback,
98 &ctx);
99 AssertReturn(g_store, E_FAIL);
100
101 g_DnsWatcher = SCDynamicStoreCreateRunLoopSource(NULL, g_store, 0);
102 if (!g_DnsWatcher)
103 return E_OUTOFMEMORY;
104
105 HRESULT hrc = HostDnsService::init();
106 AssertComRCReturn(hrc, hrc);
107
108 int rc = RTSemEventCreate(&g_DnsInitEvent);
109 AssertRCReturn(rc, E_FAIL);
110
111 return update();
112}
113
114
115
116HRESULT HostDnsServiceDarwin::start()
117{
118 int rc = RTThreadCreate(&g_DnsMonitoringThread, hostMonitoringRoutine,
119 this, 128 * _1K, RTTHREADTYPE_IO, 0, "dns-monitor");
120 AssertRCReturn(rc, E_FAIL);
121
122 RTSemEventWait(g_DnsInitEvent, RT_INDEFINITE_WAIT);
123
124 return S_OK;
125}
126
127
128void HostDnsServiceDarwin::stop()
129{
130
131 if (g_RunLoopRef)
132 CFRunLoopStop(g_RunLoopRef);
133}
134
135
136HRESULT HostDnsServiceDarwin::update()
137{
138 m_llNameServers.clear();
139 m_llSearchStrings.clear();
140 m_DomainName.setNull();
141
142 CFPropertyListRef propertyRef = SCDynamicStoreCopyValue(g_store,
143 kStateNetworkGlobalDNSKey);
144 /**
145 * 0:vvl@nb-mbp-i7-2(0)# scutil
146 * > get State:/Network/Global/DNS
147 * > d.show
148 * <dictionary> {
149 * DomainName : vvl-domain
150 * SearchDomains : <array> {
151 * 0 : vvl-domain
152 * 1 : de.vvl-domain.com
153 * }
154 * ServerAddresses : <array> {
155 * 0 : 192.168.1.4
156 * 1 : 192.168.1.1
157 * 2 : 8.8.4.4
158 * }
159 * }
160 */
161
162 CFStringRef domainNameRef = (CFStringRef)CFDictionaryGetValue(
163 static_cast<CFDictionaryRef>(propertyRef), CFSTR("DomainName"));
164 if (domainNameRef)
165 {
166 const char *pszDomainName = CFStringGetCStringPtr(domainNameRef,
167 CFStringGetSystemEncoding());
168 if (pszDomainName)
169 m_DomainName = com::Utf8Str(pszDomainName);
170 }
171
172 int i, arrayCount;
173 CFArrayRef serverArrayRef = (CFArrayRef)CFDictionaryGetValue(
174 static_cast<CFDictionaryRef>(propertyRef), CFSTR("ServerAddresses"));
175 if (serverArrayRef)
176 {
177 arrayCount = CFArrayGetCount(serverArrayRef);
178 for (i = 0; i < arrayCount; ++i)
179 {
180 CFStringRef serverAddressRef = (CFStringRef)CFArrayGetValueAtIndex(serverArrayRef, i);
181 if (!serverArrayRef)
182 continue;
183
184 const char *pszServerAddress = CFStringGetCStringPtr(serverAddressRef,
185 CFStringGetSystemEncoding());
186 if (!pszServerAddress)
187 continue;
188
189 m_llNameServers.push_back(com::Utf8Str(pszServerAddress));
190 }
191 }
192
193 CFArrayRef searchArrayRef = (CFArrayRef)CFDictionaryGetValue(
194 static_cast<CFDictionaryRef>(propertyRef), CFSTR("SearchDomains"));
195 if (searchArrayRef)
196 {
197 arrayCount = CFArrayGetCount(searchArrayRef);
198
199 for (i = 0; i < arrayCount; ++i)
200 {
201 CFStringRef searchStringRef = (CFStringRef)CFArrayGetValueAtIndex(searchArrayRef, i);
202 if (!searchArrayRef)
203 continue;
204
205 const char *pszSearchString = CFStringGetCStringPtr(searchStringRef,
206 CFStringGetSystemEncoding());
207 if (!pszSearchString)
208 continue;
209
210 m_llSearchStrings.push_back(com::Utf8Str(pszSearchString));
211 }
212 }
213
214 CFRelease(propertyRef);
215 return S_OK;
216}
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