1 | /* $Id: HostDnsServiceResolvConf.cpp 52932 2014-10-02 13:26:56Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Base class fo Host DNS & Co services.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2014 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 | /* -*- indent-tabs-mode: nil; -*- */
|
---|
19 | #include <VBox/com/string.h>
|
---|
20 | #include <VBox/com/ptr.h>
|
---|
21 |
|
---|
22 |
|
---|
23 | #ifdef RT_OS_OS2
|
---|
24 | # include <sys/socket.h>
|
---|
25 | typedef int socklen_t;
|
---|
26 | #endif
|
---|
27 |
|
---|
28 | #include <stdio.h>
|
---|
29 | #include <sys/socket.h>
|
---|
30 | #include <netinet/in.h>
|
---|
31 | #include <arpa/inet.h>
|
---|
32 |
|
---|
33 |
|
---|
34 | #include <iprt/assert.h>
|
---|
35 | #include <iprt/err.h>
|
---|
36 | #include <iprt/file.h>
|
---|
37 | #include <iprt/critsect.h>
|
---|
38 |
|
---|
39 | #include <VBox/log.h>
|
---|
40 |
|
---|
41 | #include <string>
|
---|
42 |
|
---|
43 | #include "HostDnsService.h"
|
---|
44 | #include "../../Devices/Network/slirp/resolv_conf_parser.h"
|
---|
45 |
|
---|
46 |
|
---|
47 | struct HostDnsServiceResolvConf::Data
|
---|
48 | {
|
---|
49 | Data(const char *fileName):resolvConfFilename(fileName){};
|
---|
50 |
|
---|
51 | std::string resolvConfFilename;
|
---|
52 | };
|
---|
53 |
|
---|
54 | const std::string& HostDnsServiceResolvConf::resolvConf() const
|
---|
55 | {
|
---|
56 | return m->resolvConfFilename;
|
---|
57 | }
|
---|
58 |
|
---|
59 |
|
---|
60 | HostDnsServiceResolvConf::~HostDnsServiceResolvConf()
|
---|
61 | {
|
---|
62 | if (m)
|
---|
63 | {
|
---|
64 | delete m;
|
---|
65 | m = NULL;
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | HRESULT HostDnsServiceResolvConf::init(const char *aResolvConfFileName)
|
---|
70 | {
|
---|
71 | m = new Data(aResolvConfFileName);
|
---|
72 |
|
---|
73 | HostDnsMonitor::init();
|
---|
74 |
|
---|
75 | readResolvConf();
|
---|
76 |
|
---|
77 | return S_OK;
|
---|
78 | }
|
---|
79 |
|
---|
80 | HRESULT HostDnsServiceResolvConf::readResolvConf()
|
---|
81 | {
|
---|
82 | struct rcp_state st;
|
---|
83 | wchar_t *pwczTmpStr;
|
---|
84 |
|
---|
85 | st.rcps_flags = RCPSF_NO_STR2IPCONV;
|
---|
86 | int rc = rcp_parse(&st, m->resolvConfFilename.c_str());
|
---|
87 | if (rc == -1)
|
---|
88 | return S_OK;
|
---|
89 |
|
---|
90 | HostDnsInformation info;
|
---|
91 | for (unsigned i = 0; i != st.rcps_num_nameserver; ++i)
|
---|
92 | {
|
---|
93 | AssertBreak(st.rcps_str_nameserver[i]);
|
---|
94 |
|
---|
95 | pwczTmpStr = NULL;
|
---|
96 | rc = RTStrToUtf16(st.rcps_str_nameserver[i], (RTUTF16 **)&pwczTmpStr);
|
---|
97 | if (RT_SUCCESS(rc) && pwczTmpStr)
|
---|
98 | {
|
---|
99 | info.servers.push_back(std::wstring(pwczTmpStr));
|
---|
100 | RTUtf16Free((RTUTF16 *)pwczTmpStr);
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | if (st.rcps_domain)
|
---|
105 | {
|
---|
106 | pwczTmpStr = NULL;
|
---|
107 | rc = RTStrToUtf16(st.rcps_domain, (RTUTF16 **)&pwczTmpStr);
|
---|
108 | if (RT_SUCCESS(rc) && pwczTmpStr)
|
---|
109 | {
|
---|
110 | info.domain = std::wstring(pwczTmpStr);
|
---|
111 | RTUtf16Free((RTUTF16 *)pwczTmpStr);
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | for (unsigned i = 0; i != st.rcps_num_searchlist; ++i)
|
---|
116 | {
|
---|
117 | AssertBreak(st.rcps_searchlist[i]);
|
---|
118 | pwczTmpStr = NULL;
|
---|
119 | rc = RTStrToUtf16(st.rcps_searchlist[i], (RTUTF16 **)&pwczTmpStr);
|
---|
120 | if (RT_SUCCESS(rc) && pwczTmpStr)
|
---|
121 | {
|
---|
122 | info.searchList.push_back(std::wstring(pwczTmpStr));
|
---|
123 | RTUtf16Free((RTUTF16 *)pwczTmpStr);
|
---|
124 | }
|
---|
125 | }
|
---|
126 | setInfo(info);
|
---|
127 |
|
---|
128 | return S_OK;
|
---|
129 | }
|
---|