Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : */ 4 : 5 : #ifndef dns_named_config_h_ 6 : #define dns_named_config_h_ 7 : 8 : #include <iostream> 9 : #include <fstream> 10 : #include <set> 11 : #include <string> 12 : #include <base/timer.h> 13 : 14 : class BindStatus { 15 : public: 16 : static const uint32_t kBindStatusTimeout = 2 * 1000; 17 : static const uint32_t kInitTimeout = 200; 18 : enum Event { 19 : Up, 20 : Down 21 : }; 22 : typedef boost::function<void(Event)> BindEventHandler; 23 : 24 : BindStatus(BindEventHandler handler); 25 : virtual ~BindStatus(); 26 : bool SetTrigger(); 27 121 : bool IsUp() { return (named_pid_ != (uint32_t) -1); } 28 : 29 : private: 30 : friend class DnsBindTest; 31 : friend class DnsManagerTest; 32 : 33 : bool IsBindPid(uint32_t pid); 34 : bool CheckBindStatus(); 35 : 36 : uint32_t named_pid_; 37 : BindEventHandler handler_; 38 : Timer *status_timer_; 39 : bool change_timeout_; 40 : 41 : DISALLOW_COPY_AND_ASSIGN(BindStatus); 42 : }; 43 : 44 : class NamedConfig { 45 : public: 46 : // map of zone name to list of views to which they belong 47 : typedef std::map<std::string, std::string> ZoneViewMap; 48 : typedef std::pair<std::string, std::string> ZoneViewPair; 49 : 50 : static const std::string NamedZoneFileSuffix; 51 : static const std::string NamedZoneNSPrefix; 52 : static const std::string NamedZoneMXPrefix; 53 : static const char pid_file_name[]; 54 : static const char sessionkey_file_name[]; 55 : static const int NameWidth = 30; 56 : static const int NumberWidth = 10; 57 : static const int TypeWidth = 4; 58 : 59 : struct Defaults { 60 : static const int GlobalTTL = 86400; 61 : static const int Serial = 54; 62 : static const int Refresh = 10800; 63 : static const int Retry = 900; 64 : static const int Expire = 604800; 65 : static const int Minimum = 86400; 66 : }; 67 : 68 15 : NamedConfig(const std::string& named_config_dir, 69 : const std::string& named_config_file, 70 : const std::string& named_log_file, 71 : const std::string& rndc_config_file, 72 : const std::string& rndc_secret, 73 15 : const std::string& named_max_cache_size) : 74 30 : file_(), named_log_file_(named_log_file), rndc_secret_(rndc_secret), 75 15 : named_max_cache_size_(named_max_cache_size), 76 45 : reset_flag_(false), all_zone_files_(false) { 77 15 : named_config_dir_ = named_config_dir + "/"; 78 15 : named_config_file_ = named_config_dir_ + named_config_file; 79 15 : rndc_config_file_ = named_config_dir_ + rndc_config_file; 80 15 : } 81 : 82 15 : virtual ~NamedConfig() { singleton_ = NULL; } 83 76 : static NamedConfig *GetNamedConfigObject() { return singleton_; } 84 : static void Init(const std::string& named_config_dir, 85 : const std::string& named_config_file, 86 : const std::string& named_log_file, 87 : const std::string& rndc_config_file, 88 : const std::string& rndc_secret, 89 : const std::string& named_max_cache_size); 90 : static void Shutdown(); 91 : void Reset(); 92 : virtual void AddView(const VirtualDnsConfig *vdns); 93 : virtual void ChangeView(const VirtualDnsConfig *vdns); 94 : virtual void DelView(const VirtualDnsConfig *vdns); 95 : virtual void AddAllViews(); 96 : virtual void AddZone(const Subnet &subnet, const VirtualDnsConfig *vdns); 97 : virtual void DelZone(const Subnet &subnet, const VirtualDnsConfig *vdns); 98 : 99 : virtual void UpdateNamedConf(const VirtualDnsConfig *updated_vdns = NULL); 100 : void RemoveZoneFiles(const VirtualDnsConfig *vdns, ZoneList &zones); 101 : virtual std::string GetZoneFileName(const std::string &vdns, 102 : const std::string &name); 103 : virtual std::string GetZoneFilePath(const std::string &vdns, 104 : const std::string &name); 105 0 : virtual std::string GetResolveFile() { return "/etc/resolv.conf"; } 106 : std::string GetPidFilePath(); 107 : std::string GetSessionKeyFilePath(); 108 : const std::string &named_config_dir() const { return named_config_dir_; } 109 15 : const std::string &named_config_file() const { return named_config_file_; } 110 : const std::string &named_sessionkey_file() const { 111 : return named_sessionkey_file_; 112 : } 113 : 114 : protected: 115 : void CreateRndcConf(); 116 : void CreateNamedConf(const VirtualDnsConfig *updated_vdns); 117 : void WriteOptionsConfig(); 118 : void WriteRndcConfig(); 119 : void WriteLoggingConfig(); 120 : void WriteViewConfig(const VirtualDnsConfig *updated_vdns); 121 : void WriteDefaultView(ZoneViewMap &zone_view_map); 122 : void WriteZone(const std::string &vdns, const std::string &name, 123 : bool is_master, bool is_rr, const std::string &next_dns); 124 : void AddZoneFiles(ZoneList &zones, const VirtualDnsConfig *vdns); 125 : void RemoveZoneFile(const VirtualDnsConfig *vdns, std::string &zone); 126 : std::string GetZoneNSName(const std::string domain_name); 127 : std::string GetZoneMXName(const std::string domain_name); 128 : void CreateZoneFile(std::string &zone_name, 129 : const VirtualDnsConfig *vdns, bool ns); 130 : void MakeZoneList(const VirtualDnsConfig *vdns_config, ZoneList &zones); 131 : void MakeReverseZoneList(const VirtualDnsConfig *vdns_config, 132 : ZoneList &zones); 133 : void GetDefaultForwarders(); 134 : 135 : std::ofstream file_; 136 : std::string named_config_file_; 137 : std::string named_config_dir_; 138 : std::string named_sessionkey_file_; 139 : std::string named_log_file_; 140 : std::string rndc_config_file_; 141 : std::string rndc_secret_; 142 : std::string named_max_cache_size_; 143 : std::string default_forwarders_; 144 : bool reset_flag_; 145 : bool all_zone_files_; 146 : static NamedConfig *singleton_; 147 : }; 148 : 149 : #endif // dns_named_config_h_