HostDnsService.h revision 7ebf6d185474ed3aa5368a5d93649303ab481ef5
734b6a94890be549309b21156f8ed6d4561cac51darrenm/* $Id$ */
734b6a94890be549309b21156f8ed6d4561cac51darrenm/** @file
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Host DNS listener.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm/*
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Copyright (C) 2005-2012 Oracle Corporation
734b6a94890be549309b21156f8ed6d4561cac51darrenm *
734b6a94890be549309b21156f8ed6d4561cac51darrenm * This file is part of VirtualBox Open Source Edition (OSE), as
734b6a94890be549309b21156f8ed6d4561cac51darrenm * available from http://www.virtualbox.org. This file is free software;
734b6a94890be549309b21156f8ed6d4561cac51darrenm * you can redistribute it and/or modify it under the terms of the GNU
734b6a94890be549309b21156f8ed6d4561cac51darrenm * General Public License (GPL) as published by the Free Software
734b6a94890be549309b21156f8ed6d4561cac51darrenm * Foundation, in version 2 as it comes in the "COPYING" file of the
734b6a94890be549309b21156f8ed6d4561cac51darrenm * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
734b6a94890be549309b21156f8ed6d4561cac51darrenm * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm#ifndef ___H_DNSHOSTSERVICE
734b6a94890be549309b21156f8ed6d4561cac51darrenm#define ___H_DNSHOSTSERVICE
734b6a94890be549309b21156f8ed6d4561cac51darrenm#include "VirtualBoxBase.h"
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm#include <iprt/cdefs.h>
d3b2efc749bec3b757d5f018cf78c9a09fa29cb3Anthony Scarpino#include <iprt/critsect.h>
734b6a94890be549309b21156f8ed6d4561cac51darrenm#include <iprt/types.h>
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm#include <list>
734b6a94890be549309b21156f8ed6d4561cac51darrenm#include <vector>
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenmtypedef std::list<com::Utf8Str> Utf8StrList;
734b6a94890be549309b21156f8ed6d4561cac51darrenmtypedef Utf8StrList::iterator Utf8StrListIterator;
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenmclass HostDnsMonitorProxy;
734b6a94890be549309b21156f8ed6d4561cac51darrenmtypedef const HostDnsMonitorProxy *PCHostDnsMonitorProxy;
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenmclass Lockee
734b6a94890be549309b21156f8ed6d4561cac51darrenm{
b5a2d8455dfa3190fc977c4bec53e91c99012767Hai-May Chao public:
734b6a94890be549309b21156f8ed6d4561cac51darrenm Lockee();
734b6a94890be549309b21156f8ed6d4561cac51darrenm virtual ~Lockee();
734b6a94890be549309b21156f8ed6d4561cac51darrenm const RTCRITSECT* lock() const;
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm private:
734b6a94890be549309b21156f8ed6d4561cac51darrenm RTCRITSECT mLock;
734b6a94890be549309b21156f8ed6d4561cac51darrenm};
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenmclass ALock
734b6a94890be549309b21156f8ed6d4561cac51darrenm{
734b6a94890be549309b21156f8ed6d4561cac51darrenm public:
734b6a94890be549309b21156f8ed6d4561cac51darrenm explicit ALock(const Lockee *l);
734b6a94890be549309b21156f8ed6d4561cac51darrenm ~ALock();
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm private:
734b6a94890be549309b21156f8ed6d4561cac51darrenm const Lockee *lockee;
734b6a94890be549309b21156f8ed6d4561cac51darrenm};
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenmclass HostDnsInformation
734b6a94890be549309b21156f8ed6d4561cac51darrenm{
734b6a94890be549309b21156f8ed6d4561cac51darrenm public:
734b6a94890be549309b21156f8ed6d4561cac51darrenm std::vector<std::string> servers;
734b6a94890be549309b21156f8ed6d4561cac51darrenm std::string domain;
734b6a94890be549309b21156f8ed6d4561cac51darrenm std::vector<std::string> searchList;
734b6a94890be549309b21156f8ed6d4561cac51darrenm bool equals(const HostDnsInformation &) const;
734b6a94890be549309b21156f8ed6d4561cac51darrenm};
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm/**
734b6a94890be549309b21156f8ed6d4561cac51darrenm * This class supposed to be a real DNS monitor object it should be singleton,
734b6a94890be549309b21156f8ed6d4561cac51darrenm * it lifecycle starts and ends together with VBoxSVC.
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenmclass HostDnsMonitor : public Lockee
734b6a94890be549309b21156f8ed6d4561cac51darrenm{
734b6a94890be549309b21156f8ed6d4561cac51darrenm public:
734b6a94890be549309b21156f8ed6d4561cac51darrenm static const HostDnsMonitor *getHostDnsMonitor();
734b6a94890be549309b21156f8ed6d4561cac51darrenm static void shutdown();
8de5c4f463386063e184a851437d58080c6c626cDan OpenSolaris Anderson
734b6a94890be549309b21156f8ed6d4561cac51darrenm void addMonitorProxy(PCHostDnsMonitorProxy) const;
734b6a94890be549309b21156f8ed6d4561cac51darrenm void releaseMonitorProxy(PCHostDnsMonitorProxy) const;
734b6a94890be549309b21156f8ed6d4561cac51darrenm const HostDnsInformation &getInfo() const;
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* @note: method will wait till client call
734b6a94890be549309b21156f8ed6d4561cac51darrenm HostDnsService::monitorThreadInitializationDone() */
734b6a94890be549309b21156f8ed6d4561cac51darrenm virtual HRESULT init();
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm protected:
734b6a94890be549309b21156f8ed6d4561cac51darrenm explicit HostDnsMonitor(bool fThreaded = false);
734b6a94890be549309b21156f8ed6d4561cac51darrenm virtual ~HostDnsMonitor();
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm void setInfo(const HostDnsInformation &);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* this function used only if HostDnsMonitor::HostDnsMonitor(true) */
734b6a94890be549309b21156f8ed6d4561cac51darrenm void monitorThreadInitializationDone();
734b6a94890be549309b21156f8ed6d4561cac51darrenm virtual void monitorThreadShutdown() = 0;
734b6a94890be549309b21156f8ed6d4561cac51darrenm virtual int monitorWorker() = 0;
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm private:
734b6a94890be549309b21156f8ed6d4561cac51darrenm HostDnsMonitor(const HostDnsMonitor &);
734b6a94890be549309b21156f8ed6d4561cac51darrenm HostDnsMonitor& operator= (const HostDnsMonitor &);
734b6a94890be549309b21156f8ed6d4561cac51darrenm static int threadMonitoringRoutine(RTTHREAD, void *);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm public:
734b6a94890be549309b21156f8ed6d4561cac51darrenm struct Data;
5b675b316486993a90722f1a7ec08ce857ef7af7Vladimir Kotal Data *m;
734b6a94890be549309b21156f8ed6d4561cac51darrenm};
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm/**
734b6a94890be549309b21156f8ed6d4561cac51darrenm * This class supposed to be a proxy for events on changing Host Name Resolving configurations.
5b675b316486993a90722f1a7ec08ce857ef7af7Vladimir Kotal */
734b6a94890be549309b21156f8ed6d4561cac51darrenmclass HostDnsMonitorProxy : public Lockee
734b6a94890be549309b21156f8ed6d4561cac51darrenm{
734b6a94890be549309b21156f8ed6d4561cac51darrenm public:
734b6a94890be549309b21156f8ed6d4561cac51darrenm HostDnsMonitorProxy();
734b6a94890be549309b21156f8ed6d4561cac51darrenm ~HostDnsMonitorProxy();
734b6a94890be549309b21156f8ed6d4561cac51darrenm void init(const HostDnsMonitor *aMonitor, VirtualBox *aParent);
734b6a94890be549309b21156f8ed6d4561cac51darrenm void notify() const;
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm HRESULT GetNameServers(std::vector<com::Utf8Str> &aNameServers);
734b6a94890be549309b21156f8ed6d4561cac51darrenm HRESULT GetDomainName(com::Utf8Str *pDomainName);
734b6a94890be549309b21156f8ed6d4561cac51darrenm HRESULT GetSearchStrings(std::vector<com::Utf8Str> &aSearchStrings);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm bool operator==(PCHostDnsMonitorProxy&);
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm private:
734b6a94890be549309b21156f8ed6d4561cac51darrenm void updateInfo();
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm private:
734b6a94890be549309b21156f8ed6d4561cac51darrenm struct Data;
734b6a94890be549309b21156f8ed6d4561cac51darrenm Data *m;
734b6a94890be549309b21156f8ed6d4561cac51darrenm};
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm# ifdef RT_OS_DARWIN
734b6a94890be549309b21156f8ed6d4561cac51darrenmclass HostDnsServiceDarwin : public HostDnsMonitor
734b6a94890be549309b21156f8ed6d4561cac51darrenm{
734b6a94890be549309b21156f8ed6d4561cac51darrenm public:
734b6a94890be549309b21156f8ed6d4561cac51darrenm HostDnsServiceDarwin();
734b6a94890be549309b21156f8ed6d4561cac51darrenm ~HostDnsServiceDarwin();
734b6a94890be549309b21156f8ed6d4561cac51darrenm HRESULT init();
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm protected:
734b6a94890be549309b21156f8ed6d4561cac51darrenm virtual void monitorThreadShutdown();
734b6a94890be549309b21156f8ed6d4561cac51darrenm virtual int monitorWorker();
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm private:
734b6a94890be549309b21156f8ed6d4561cac51darrenm HRESULT updateInfo();
734b6a94890be549309b21156f8ed6d4561cac51darrenm static void hostDnsServiceStoreCallback(void *store, void *arrayRef, void *info);
734b6a94890be549309b21156f8ed6d4561cac51darrenm struct Data;
734b6a94890be549309b21156f8ed6d4561cac51darrenm Data *m;
734b6a94890be549309b21156f8ed6d4561cac51darrenm};
734b6a94890be549309b21156f8ed6d4561cac51darrenm# endif
734b6a94890be549309b21156f8ed6d4561cac51darrenm# ifdef RT_OS_WINDOWS
734b6a94890be549309b21156f8ed6d4561cac51darrenmclass HostDnsServiceWin : public HostDnsMonitor
734b6a94890be549309b21156f8ed6d4561cac51darrenm{
734b6a94890be549309b21156f8ed6d4561cac51darrenm public:
734b6a94890be549309b21156f8ed6d4561cac51darrenm HostDnsServiceWin();
734b6a94890be549309b21156f8ed6d4561cac51darrenm ~HostDnsServiceWin();
734b6a94890be549309b21156f8ed6d4561cac51darrenm HRESULT init();
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm protected:
734b6a94890be549309b21156f8ed6d4561cac51darrenm virtual void monitorThreadShutdown();
734b6a94890be549309b21156f8ed6d4561cac51darrenm virtual int monitorWorker();
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm private:
734b6a94890be549309b21156f8ed6d4561cac51darrenm HRESULT updateInfo();
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm private:
734b6a94890be549309b21156f8ed6d4561cac51darrenm struct Data;
734b6a94890be549309b21156f8ed6d4561cac51darrenm Data *m;
734b6a94890be549309b21156f8ed6d4561cac51darrenm};
734b6a94890be549309b21156f8ed6d4561cac51darrenm# endif
734b6a94890be549309b21156f8ed6d4561cac51darrenm# if defined(RT_OS_SOLARIS) || defined(RT_OS_LINUX) || defined(RT_OS_OS2) || defined(RT_OS_FREEBSD)
734b6a94890be549309b21156f8ed6d4561cac51darrenmclass HostDnsServiceResolvConf: public HostDnsMonitor
734b6a94890be549309b21156f8ed6d4561cac51darrenm{
734b6a94890be549309b21156f8ed6d4561cac51darrenm public:
734b6a94890be549309b21156f8ed6d4561cac51darrenm explicit HostDnsServiceResolvConf(bool fThreaded = false) : HostDnsMonitor(fThreaded), m(NULL) {}
734b6a94890be549309b21156f8ed6d4561cac51darrenm virtual ~HostDnsServiceResolvConf();
734b6a94890be549309b21156f8ed6d4561cac51darrenm virtual HRESULT init(const char *aResolvConfFileName);
734b6a94890be549309b21156f8ed6d4561cac51darrenm const std::string& resolvConf() const;
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm protected:
734b6a94890be549309b21156f8ed6d4561cac51darrenm HRESULT readResolvConf();
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* While not all hosts supports Hosts DNS change notifiaction
734b6a94890be549309b21156f8ed6d4561cac51darrenm * default implementation offers return VERR_IGNORE.
735564919188238196dbd0d320770dda59b38369Anthony Scarpino */
735564919188238196dbd0d320770dda59b38369Anthony Scarpino virtual void monitorThreadShutdown() {}
735564919188238196dbd0d320770dda59b38369Anthony Scarpino virtual int monitorWorker() {return VERR_IGNORED;}
6ea3c0609e50782557505b88bb391b786bca32c9Garrett D'Amore
734b6a94890be549309b21156f8ed6d4561cac51darrenm protected:
734b6a94890be549309b21156f8ed6d4561cac51darrenm struct Data;
734b6a94890be549309b21156f8ed6d4561cac51darrenm Data *m;
735564919188238196dbd0d320770dda59b38369Anthony Scarpino};
734b6a94890be549309b21156f8ed6d4561cac51darrenm# if defined(RT_OS_SOLARIS)
734b6a94890be549309b21156f8ed6d4561cac51darrenm/**
734b6a94890be549309b21156f8ed6d4561cac51darrenm * XXX: https://blogs.oracle.com/praks/entry/file_events_notification
734b6a94890be549309b21156f8ed6d4561cac51darrenm */
734b6a94890be549309b21156f8ed6d4561cac51darrenmclass HostDnsServiceSolaris : public HostDnsServiceResolvConf
734b6a94890be549309b21156f8ed6d4561cac51darrenm{
734b6a94890be549309b21156f8ed6d4561cac51darrenm public:
734b6a94890be549309b21156f8ed6d4561cac51darrenm HostDnsServiceSolaris(){}
734b6a94890be549309b21156f8ed6d4561cac51darrenm ~HostDnsServiceSolaris(){}
734b6a94890be549309b21156f8ed6d4561cac51darrenm HRESULT init(){ return HostDnsServiceResolvConf::init("/etc/resolv.conf");}
734b6a94890be549309b21156f8ed6d4561cac51darrenm};
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm# elif defined(RT_OS_LINUX)
734b6a94890be549309b21156f8ed6d4561cac51darrenmclass HostDnsServiceLinux : public HostDnsServiceResolvConf
734b6a94890be549309b21156f8ed6d4561cac51darrenm{
734b6a94890be549309b21156f8ed6d4561cac51darrenm public:
734b6a94890be549309b21156f8ed6d4561cac51darrenm HostDnsServiceLinux():HostDnsServiceResolvConf(true){}
734b6a94890be549309b21156f8ed6d4561cac51darrenm virtual ~HostDnsServiceLinux();
734b6a94890be549309b21156f8ed6d4561cac51darrenm virtual HRESULT init(){ return HostDnsServiceResolvConf::init("/etc/resolv.conf");}
734b6a94890be549309b21156f8ed6d4561cac51darrenm
d3b2efc749bec3b757d5f018cf78c9a09fa29cb3Anthony Scarpino protected:
d3b2efc749bec3b757d5f018cf78c9a09fa29cb3Anthony Scarpino virtual void monitorThreadShutdown();
d3b2efc749bec3b757d5f018cf78c9a09fa29cb3Anthony Scarpino virtual int monitorWorker();
734b6a94890be549309b21156f8ed6d4561cac51darrenm};
d3b2efc749bec3b757d5f018cf78c9a09fa29cb3Anthony Scarpino
734b6a94890be549309b21156f8ed6d4561cac51darrenm# elif defined(RT_OS_FREEBSD)
734b6a94890be549309b21156f8ed6d4561cac51darrenmclass HostDnsServiceFreebsd: public HostDnsServiceResolvConf
734b6a94890be549309b21156f8ed6d4561cac51darrenm{
734b6a94890be549309b21156f8ed6d4561cac51darrenm public:
734b6a94890be549309b21156f8ed6d4561cac51darrenm HostDnsServiceFreebsd(){}
734b6a94890be549309b21156f8ed6d4561cac51darrenm ~HostDnsServiceFreebsd(){}
734b6a94890be549309b21156f8ed6d4561cac51darrenm HRESULT init(){ return HostDnsServiceResolvConf::init("/etc/resolv.conf");}
734b6a94890be549309b21156f8ed6d4561cac51darrenm};
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm# elif defined(RT_OS_OS2)
734b6a94890be549309b21156f8ed6d4561cac51darrenmclass HostDnsServiceOs2 : public HostDnsServiceResolvConf
734b6a94890be549309b21156f8ed6d4561cac51darrenm{
734b6a94890be549309b21156f8ed6d4561cac51darrenm public:
734b6a94890be549309b21156f8ed6d4561cac51darrenm HostDnsServiceOs2(){}
734b6a94890be549309b21156f8ed6d4561cac51darrenm ~HostDnsServiceOs2(){}
734b6a94890be549309b21156f8ed6d4561cac51darrenm /* XXX: \\MPTN\\ETC should be taken from environment variable ETC */
734b6a94890be549309b21156f8ed6d4561cac51darrenm HRESULT init(){ return init("\\MPTN\\ETC\\RESOLV2");}
734b6a94890be549309b21156f8ed6d4561cac51darrenm};
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm# endif
734b6a94890be549309b21156f8ed6d4561cac51darrenm# endif
734b6a94890be549309b21156f8ed6d4561cac51darrenm
734b6a94890be549309b21156f8ed6d4561cac51darrenm#endif /* !___H_DNSHOSTSERVICE */
734b6a94890be549309b21156f8ed6d4561cac51darrenm