VirtualBox

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

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

Main/DNS: general host specific DNS and Co handling added.
Darwin implementation has self-update mechanism, for other hosts it's in todo list.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.3 KB
Line 
1/* $Id: HostDnsServiceDarwin.cpp 48330 2013-09-06 03:16:32Z 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
25#include <CoreFoundation/CoreFoundation.h>
26#include <SystemConfiguration/SCDynamicStore.h>
27
28
29
30SCDynamicStoreRef g_store;
31CFRunLoopSourceRef g_DnsWatcher;
32
33static const CFStringRef kStateNetworkGlobalDNSKey = CFSTR("State:/Network/Global/DNS");
34
35HostDnsServiceDarwin::HostDnsServiceDarwin(){}
36HostDnsServiceDarwin::~HostDnsServiceDarwin()
37{
38 CFRelease(g_DnsWatcher);
39 CFRelease(g_store);
40}
41
42void HostDnsServiceDarwin::hostDnsServiceStoreCallback(void *arg0, void *arg1, void *info)
43{
44 HostDnsServiceDarwin *pThis = (HostDnsServiceDarwin *)info;
45
46 NOREF(arg0); /* SCDynamicStore */
47 NOREF(arg1); /* CFArrayRef */
48
49 RTCritSectEnter(&pThis->m_hCritSect);
50 pThis->update();
51 RTCritSectLeave(&pThis->m_hCritSect);
52}
53
54HRESULT HostDnsServiceDarwin::init()
55{
56 SCDynamicStoreContext ctx;
57 RT_ZERO(ctx);
58
59 ctx.info = this;
60
61 g_store = SCDynamicStoreCreate(NULL, CFSTR("org.virtualbox.VBoxSVC"),
62 (SCDynamicStoreCallBack)HostDnsServiceDarwin::hostDnsServiceStoreCallback,
63 &ctx);
64 AssertReturn(g_store, E_FAIL);
65
66 g_DnsWatcher = SCDynamicStoreCreateRunLoopSource(NULL, g_store, 0);
67 if (!g_DnsWatcher)
68 return E_OUTOFMEMORY;
69
70 CFArrayRef watchingArrayRef = CFArrayCreate(NULL,
71 (const void **)&kStateNetworkGlobalDNSKey,
72 1, &kCFTypeArrayCallBacks);
73 if (!watchingArrayRef)
74 {
75 CFRelease(g_DnsWatcher);
76 return E_OUTOFMEMORY;
77 }
78
79 if(SCDynamicStoreSetNotificationKeys(g_store, watchingArrayRef, NULL))
80 CFRunLoopAddSource(CFRunLoopGetMain(), g_DnsWatcher, kCFRunLoopCommonModes);
81
82 CFRelease(watchingArrayRef);
83
84 HRESULT hrc = HostDnsService::init();
85 AssertComRCReturn(hrc, hrc);
86
87 return update();
88}
89
90
91
92HRESULT HostDnsServiceDarwin::start()
93{
94 return S_OK;
95}
96
97
98void HostDnsServiceDarwin::stop()
99{
100}
101
102
103HRESULT HostDnsServiceDarwin::update()
104{
105 m_llNameServers.clear();
106 m_llSearchStrings.clear();
107 m_DomainName.setNull();
108
109 CFPropertyListRef propertyRef = SCDynamicStoreCopyValue(g_store,
110 kStateNetworkGlobalDNSKey);
111 /**
112 * 0:vvl@nb-mbp-i7-2(0)# scutil
113 * > get State:/Network/Global/DNS
114 * > d.show
115 * <dictionary> {
116 * DomainName : vvl-domain
117 * SearchDomains : <array> {
118 * 0 : vvl-domain
119 * 1 : de.vvl-domain.com
120 * }
121 * ServerAddresses : <array> {
122 * 0 : 192.168.1.4
123 * 1 : 192.168.1.1
124 * 2 : 8.8.4.4
125 * }
126 * }
127 */
128
129 CFStringRef domainNameRef = (CFStringRef)CFDictionaryGetValue(
130 static_cast<CFDictionaryRef>(propertyRef), CFSTR("DomainName"));
131 if (domainNameRef)
132 {
133 const char *pszDomainName = CFStringGetCStringPtr(domainNameRef,
134 CFStringGetSystemEncoding());
135 if (pszDomainName)
136 m_DomainName = com::Utf8Str(pszDomainName);
137 }
138
139 int i, arrayCount;
140 CFArrayRef serverArrayRef = (CFArrayRef)CFDictionaryGetValue(
141 static_cast<CFDictionaryRef>(propertyRef), CFSTR("ServerAddresses"));
142 if (serverArrayRef)
143 {
144 arrayCount = CFArrayGetCount(serverArrayRef);
145 for (i = 0; i < arrayCount; ++i)
146 {
147 CFStringRef serverAddressRef = (CFStringRef)CFArrayGetValueAtIndex(serverArrayRef, i);
148 if (!serverArrayRef)
149 continue;
150
151 const char *pszServerAddress = CFStringGetCStringPtr(serverAddressRef,
152 CFStringGetSystemEncoding());
153 if (!pszServerAddress)
154 continue;
155
156 m_llNameServers.push_back(com::Utf8Str(pszServerAddress));
157 }
158 }
159
160 CFArrayRef searchArrayRef = (CFArrayRef)CFDictionaryGetValue(
161 static_cast<CFDictionaryRef>(propertyRef), CFSTR("SearchDomains"));
162 if (searchArrayRef)
163 {
164 arrayCount = CFArrayGetCount(searchArrayRef);
165
166 for (i = 0; i < arrayCount; ++i)
167 {
168 CFStringRef searchStringRef = (CFStringRef)CFArrayGetValueAtIndex(searchArrayRef, i);
169 if (!searchArrayRef)
170 continue;
171
172 const char *pszSearchString = CFStringGetCStringPtr(searchStringRef,
173 CFStringGetSystemEncoding());
174 if (!pszSearchString)
175 continue;
176
177 m_llSearchStrings.push_back(com::Utf8Str(pszSearchString));
178 }
179 }
180
181 CFRelease(propertyRef);
182 return S_OK;
183}
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