099e7a98b610d5290560331351a58781728dd33evboxsync/* $Id$ */
099e7a98b610d5290560331351a58781728dd33evboxsync/** @file
d9241a9515e03c7c95a170b2106b365411df0c4evboxsync * Base class for Host DNS & Co services.
099e7a98b610d5290560331351a58781728dd33evboxsync */
099e7a98b610d5290560331351a58781728dd33evboxsync
099e7a98b610d5290560331351a58781728dd33evboxsync/*
099e7a98b610d5290560331351a58781728dd33evboxsync * Copyright (C) 2014 Oracle Corporation
099e7a98b610d5290560331351a58781728dd33evboxsync *
099e7a98b610d5290560331351a58781728dd33evboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
099e7a98b610d5290560331351a58781728dd33evboxsync * available from http://www.virtualbox.org. This file is free software;
099e7a98b610d5290560331351a58781728dd33evboxsync * you can redistribute it and/or modify it under the terms of the GNU
099e7a98b610d5290560331351a58781728dd33evboxsync * General Public License (GPL) as published by the Free Software
099e7a98b610d5290560331351a58781728dd33evboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
099e7a98b610d5290560331351a58781728dd33evboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
099e7a98b610d5290560331351a58781728dd33evboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
099e7a98b610d5290560331351a58781728dd33evboxsync */
099e7a98b610d5290560331351a58781728dd33evboxsync
18b1f1bb3429f8bfc1a52cfaeb2445e9fb1f55cdvboxsync/* -*- indent-tabs-mode: nil; -*- */
18b1f1bb3429f8bfc1a52cfaeb2445e9fb1f55cdvboxsync#include <VBox/com/string.h>
18b1f1bb3429f8bfc1a52cfaeb2445e9fb1f55cdvboxsync#include <VBox/com/ptr.h>
18b1f1bb3429f8bfc1a52cfaeb2445e9fb1f55cdvboxsync
18b1f1bb3429f8bfc1a52cfaeb2445e9fb1f55cdvboxsync
18b1f1bb3429f8bfc1a52cfaeb2445e9fb1f55cdvboxsync#ifdef RT_OS_OS2
18b1f1bb3429f8bfc1a52cfaeb2445e9fb1f55cdvboxsync# include <sys/socket.h>
18b1f1bb3429f8bfc1a52cfaeb2445e9fb1f55cdvboxsynctypedef int socklen_t;
18b1f1bb3429f8bfc1a52cfaeb2445e9fb1f55cdvboxsync#endif
18b1f1bb3429f8bfc1a52cfaeb2445e9fb1f55cdvboxsync
18b1f1bb3429f8bfc1a52cfaeb2445e9fb1f55cdvboxsync#include <stdio.h>
18b1f1bb3429f8bfc1a52cfaeb2445e9fb1f55cdvboxsync#include <sys/socket.h>
18b1f1bb3429f8bfc1a52cfaeb2445e9fb1f55cdvboxsync#include <netinet/in.h>
18b1f1bb3429f8bfc1a52cfaeb2445e9fb1f55cdvboxsync#include <arpa/inet.h>
18b1f1bb3429f8bfc1a52cfaeb2445e9fb1f55cdvboxsync
18b1f1bb3429f8bfc1a52cfaeb2445e9fb1f55cdvboxsync
18b1f1bb3429f8bfc1a52cfaeb2445e9fb1f55cdvboxsync#include <iprt/assert.h>
18b1f1bb3429f8bfc1a52cfaeb2445e9fb1f55cdvboxsync#include <iprt/err.h>
18b1f1bb3429f8bfc1a52cfaeb2445e9fb1f55cdvboxsync#include <iprt/file.h>
3f2299591cfeb8a37a3578bef9c44c1790860948vboxsync#include <iprt/critsect.h>
18b1f1bb3429f8bfc1a52cfaeb2445e9fb1f55cdvboxsync
18b1f1bb3429f8bfc1a52cfaeb2445e9fb1f55cdvboxsync#include <VBox/log.h>
18b1f1bb3429f8bfc1a52cfaeb2445e9fb1f55cdvboxsync
3f2299591cfeb8a37a3578bef9c44c1790860948vboxsync#include <string>
3f2299591cfeb8a37a3578bef9c44c1790860948vboxsync
3f2299591cfeb8a37a3578bef9c44c1790860948vboxsync#include "HostDnsService.h"
dbbd0cae65188dffbd927bdde2d88cbeec2e26davboxsync#include "../../Devices/Network/slirp/resolv_conf_parser.h"
3f2299591cfeb8a37a3578bef9c44c1790860948vboxsync
3f2299591cfeb8a37a3578bef9c44c1790860948vboxsync
3f2299591cfeb8a37a3578bef9c44c1790860948vboxsyncstruct HostDnsServiceResolvConf::Data
3f2299591cfeb8a37a3578bef9c44c1790860948vboxsync{
3f2299591cfeb8a37a3578bef9c44c1790860948vboxsync Data(const char *fileName):resolvConfFilename(fileName){};
3f2299591cfeb8a37a3578bef9c44c1790860948vboxsync
3f2299591cfeb8a37a3578bef9c44c1790860948vboxsync std::string resolvConfFilename;
3f2299591cfeb8a37a3578bef9c44c1790860948vboxsync};
3f2299591cfeb8a37a3578bef9c44c1790860948vboxsync
dbbd0cae65188dffbd927bdde2d88cbeec2e26davboxsyncconst std::string& HostDnsServiceResolvConf::resolvConf() const
3f2299591cfeb8a37a3578bef9c44c1790860948vboxsync{
3f2299591cfeb8a37a3578bef9c44c1790860948vboxsync return m->resolvConfFilename;
3f2299591cfeb8a37a3578bef9c44c1790860948vboxsync}
3f2299591cfeb8a37a3578bef9c44c1790860948vboxsync
18b1f1bb3429f8bfc1a52cfaeb2445e9fb1f55cdvboxsync
18b1f1bb3429f8bfc1a52cfaeb2445e9fb1f55cdvboxsyncHostDnsServiceResolvConf::~HostDnsServiceResolvConf()
18b1f1bb3429f8bfc1a52cfaeb2445e9fb1f55cdvboxsync{
c0b3f8f271f3bc2358c46ed5a99af29a28bd1b66vboxsync if (m)
c0b3f8f271f3bc2358c46ed5a99af29a28bd1b66vboxsync {
c0b3f8f271f3bc2358c46ed5a99af29a28bd1b66vboxsync delete m;
c0b3f8f271f3bc2358c46ed5a99af29a28bd1b66vboxsync m = NULL;
c0b3f8f271f3bc2358c46ed5a99af29a28bd1b66vboxsync }
18b1f1bb3429f8bfc1a52cfaeb2445e9fb1f55cdvboxsync}
18b1f1bb3429f8bfc1a52cfaeb2445e9fb1f55cdvboxsync
b5e51a17f57d4fe07a991fe3622c16742f558099vboxsyncHRESULT HostDnsServiceResolvConf::init(VirtualBox *virtualbox, const char *aResolvConfFileName)
3f2299591cfeb8a37a3578bef9c44c1790860948vboxsync{
dbbd0cae65188dffbd927bdde2d88cbeec2e26davboxsync m = new Data(aResolvConfFileName);
dbbd0cae65188dffbd927bdde2d88cbeec2e26davboxsync
b5e51a17f57d4fe07a991fe3622c16742f558099vboxsync HostDnsMonitor::init(virtualbox);
18b1f1bb3429f8bfc1a52cfaeb2445e9fb1f55cdvboxsync
3f2299591cfeb8a37a3578bef9c44c1790860948vboxsync readResolvConf();
18b1f1bb3429f8bfc1a52cfaeb2445e9fb1f55cdvboxsync
18b1f1bb3429f8bfc1a52cfaeb2445e9fb1f55cdvboxsync return S_OK;
18b1f1bb3429f8bfc1a52cfaeb2445e9fb1f55cdvboxsync}
18b1f1bb3429f8bfc1a52cfaeb2445e9fb1f55cdvboxsync
510b14148124a664875cdb3a996433bd92c9324evboxsync
3f2299591cfeb8a37a3578bef9c44c1790860948vboxsyncHRESULT HostDnsServiceResolvConf::readResolvConf()
18b1f1bb3429f8bfc1a52cfaeb2445e9fb1f55cdvboxsync{
dbbd0cae65188dffbd927bdde2d88cbeec2e26davboxsync struct rcp_state st;
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync
cb702d6d593134e47854c00a7fa1156fe48a14ccvboxsync st.rcps_flags = RCPSF_NO_STR2IPCONV;
dbbd0cae65188dffbd927bdde2d88cbeec2e26davboxsync int rc = rcp_parse(&st, m->resolvConfFilename.c_str());
dbbd0cae65188dffbd927bdde2d88cbeec2e26davboxsync if (rc == -1)
dbbd0cae65188dffbd927bdde2d88cbeec2e26davboxsync return S_OK;
3f2299591cfeb8a37a3578bef9c44c1790860948vboxsync
dbbd0cae65188dffbd927bdde2d88cbeec2e26davboxsync HostDnsInformation info;
dbbd0cae65188dffbd927bdde2d88cbeec2e26davboxsync for (unsigned i = 0; i != st.rcps_num_nameserver; ++i)
18b1f1bb3429f8bfc1a52cfaeb2445e9fb1f55cdvboxsync {
dbbd0cae65188dffbd927bdde2d88cbeec2e26davboxsync AssertBreak(st.rcps_str_nameserver[i]);
510b14148124a664875cdb3a996433bd92c9324evboxsync info.servers.push_back(st.rcps_str_nameserver[i]);
18b1f1bb3429f8bfc1a52cfaeb2445e9fb1f55cdvboxsync }
2b114c590cf5a19f8047cd7bde9c7e5ae00aa22bvboxsync
dbbd0cae65188dffbd927bdde2d88cbeec2e26davboxsync if (st.rcps_domain)
510b14148124a664875cdb3a996433bd92c9324evboxsync info.domain = st.rcps_domain;
18b1f1bb3429f8bfc1a52cfaeb2445e9fb1f55cdvboxsync
dbbd0cae65188dffbd927bdde2d88cbeec2e26davboxsync for (unsigned i = 0; i != st.rcps_num_searchlist; ++i)
dbbd0cae65188dffbd927bdde2d88cbeec2e26davboxsync {
dbbd0cae65188dffbd927bdde2d88cbeec2e26davboxsync AssertBreak(st.rcps_searchlist[i]);
510b14148124a664875cdb3a996433bd92c9324evboxsync info.searchList.push_back(st.rcps_searchlist[i]);
dbbd0cae65188dffbd927bdde2d88cbeec2e26davboxsync }
3f2299591cfeb8a37a3578bef9c44c1790860948vboxsync setInfo(info);
18b1f1bb3429f8bfc1a52cfaeb2445e9fb1f55cdvboxsync
3f2299591cfeb8a37a3578bef9c44c1790860948vboxsync return S_OK;
18b1f1bb3429f8bfc1a52cfaeb2445e9fb1f55cdvboxsync}