|
| 1 | +/** |
| 2 | + * The configuration |
| 3 | + * |
| 4 | + * Object that holds the nameservers, loaded from /etc/resolv.conf and |
| 5 | + * the /etc/hosts file settings |
| 6 | + * |
| 7 | + * @author Emiel Bruijntjes <[email protected]> |
| 8 | + * @copyright 2025 Copernica BV |
| 9 | + */ |
| 10 | + |
| 11 | +/** |
| 12 | + * Begin of namespace |
| 13 | + */ |
| 14 | +namespace DNS { |
| 15 | + |
| 16 | +/** |
| 17 | + * Class definition |
| 18 | + */ |
| 19 | +class Config |
| 20 | +{ |
| 21 | +private: |
| 22 | + /** |
| 23 | + * The IP addresses of the servers that can be accessed |
| 24 | + * @var std::vector<Ip> |
| 25 | + */ |
| 26 | + std::vector<Ip> _nameservers; |
| 27 | + |
| 28 | + /** |
| 29 | + * The IP addresses of the servers that can be accessed |
| 30 | + * @var std::vector<Ip> |
| 31 | + */ |
| 32 | + std::vector<std::string> _searchpaths; |
| 33 | + |
| 34 | + /** |
| 35 | + * The contents of the /etc/hosts file |
| 36 | + * @var Hosts |
| 37 | + */ |
| 38 | + Hosts _hosts; |
| 39 | + |
| 40 | + /** |
| 41 | + * Max time that we wait for a response |
| 42 | + * @var double |
| 43 | + */ |
| 44 | + double _timeout = 60.0; |
| 45 | + |
| 46 | + /** |
| 47 | + * Max number of attempts / requests to send per query |
| 48 | + * @var size_t |
| 49 | + */ |
| 50 | + size_t _attempts = 5; |
| 51 | + |
| 52 | + /** |
| 53 | + * Interval before a datagram is sent again |
| 54 | + * @var double |
| 55 | + */ |
| 56 | + double _interval = 2.0; |
| 57 | + |
| 58 | + /** |
| 59 | + * Should all nameservers be rotated? otherwise they will be tried in-order |
| 60 | + * @var bool |
| 61 | + */ |
| 62 | + bool _rotate = false; |
| 63 | + |
| 64 | + /** |
| 65 | + * The 'ndots' setting from resolv.conf |
| 66 | + * @var ndots |
| 67 | + */ |
| 68 | + uint8_t _ndots = 1; |
| 69 | + |
| 70 | + |
| 71 | +public: |
| 72 | + /** |
| 73 | + * Default constructor |
| 74 | + */ |
| 75 | + Config() = default; |
| 76 | + |
| 77 | + /** |
| 78 | + * Default constructor |
| 79 | + * @param settings |
| 80 | + */ |
| 81 | + Config(const ResolvConf &settings) : |
| 82 | + _timeout(settings.timeout()), |
| 83 | + _attempts(settings.attempts()), |
| 84 | + _interval(settings.timeout()), |
| 85 | + _rotate(settings.rotate()), |
| 86 | + _ndots(settings.ndots()) |
| 87 | + { |
| 88 | + // construct the nameservers and search paths |
| 89 | + for (size_t i = 0; i < settings.nameservers(); ++i) _nameservers.emplace_back(settings.nameserver(i)); |
| 90 | + for (size_t i = 0; i < settings.searchpaths(); ++i) _searchpaths.emplace_back(settings.searchpath(i)); |
| 91 | + |
| 92 | + // we also have to load /etc/hosts |
| 93 | + if (!_hosts.load()) throw std::runtime_error("failed to load /etc/hosts"); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Expose the /etc/hosts file |
| 98 | + * @return Hosts |
| 99 | + */ |
| 100 | + const Hosts &hosts() const |
| 101 | + { |
| 102 | + return _hosts; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Clear the list of nameservers |
| 107 | + */ |
| 108 | + void clear() |
| 109 | + { |
| 110 | + // empty the list |
| 111 | + _nameservers.clear(); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Add a nameserver |
| 116 | + * @param ip |
| 117 | + */ |
| 118 | + void nameserver(const Ip &ip) |
| 119 | + { |
| 120 | + // add to the member in the base class |
| 121 | + _nameservers.emplace_back(ip); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Total number of nameservers |
| 126 | + * @return size_t |
| 127 | + */ |
| 128 | + size_t nameservers() const |
| 129 | + { |
| 130 | + return _nameservers.size(); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Expose to one of the nameservers |
| 135 | + * @param index |
| 136 | + * @return Ip |
| 137 | + */ |
| 138 | + const Ip &nameserver(size_t index) const |
| 139 | + { |
| 140 | + return _nameservers[index]; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * The total number of searchpaths |
| 145 | + * @return size_t |
| 146 | + */ |
| 147 | + size_t searchpaths() const |
| 148 | + { |
| 149 | + return _searchpaths.size(); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Expose one of the search-paths |
| 154 | + * @param index |
| 155 | + * @return std::string |
| 156 | + */ |
| 157 | + const std::string &searchpath(size_t index) const |
| 158 | + { |
| 159 | + return _searchpaths[index]; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Does a certain hostname exists in /etc/hosts? In that case a NXDOMAIN error should not be given |
| 164 | + * @param hostname hostname to check |
| 165 | + * @return bool does it exists in /etc/hosts? |
| 166 | + */ |
| 167 | + bool exists(const char *hostname) const |
| 168 | + { |
| 169 | + // lookup the name |
| 170 | + return _hosts.lookup(hostname) != nullptr; |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Max time that we wait for a response |
| 175 | + * @return double |
| 176 | + */ |
| 177 | + double timeout() const { return _timeout; } |
| 178 | + |
| 179 | + /** |
| 180 | + * Set max time to wait for a response |
| 181 | + * @param timeout time in seconds |
| 182 | + */ |
| 183 | + void timeout(double timeout) { _timeout = std::max(timeout, 0.1); } |
| 184 | + |
| 185 | + /** |
| 186 | + * Max number of attempts / requests to send per query |
| 187 | + * @return size_t |
| 188 | + */ |
| 189 | + size_t attempts() const { return _attempts; } |
| 190 | + |
| 191 | + /** |
| 192 | + * Set the max number of attempts |
| 193 | + * @param attempt max number of attemps |
| 194 | + */ |
| 195 | + void attempts(size_t attempts) { _attempts = attempts; } |
| 196 | + |
| 197 | + /** |
| 198 | + * Interval before a datagram is sent again |
| 199 | + * @return double |
| 200 | + */ |
| 201 | + double interval() const { return _interval; } |
| 202 | + |
| 203 | + /** |
| 204 | + * Set interval before a datagram is sent again |
| 205 | + * @param interval time in seconds |
| 206 | + */ |
| 207 | + void interval(double interval) { _interval = std::max(interval, 0.1); } |
| 208 | + |
| 209 | + /** |
| 210 | + * Should all nameservers be rotated? otherwise they will be tried in-order |
| 211 | + * @return bool |
| 212 | + */ |
| 213 | + bool rotate() const { return _rotate; } |
| 214 | + |
| 215 | + /** |
| 216 | + * Set the rotate setting: If true, nameservers will be rotated, if false, nameservers are tried in-order |
| 217 | + * @param rotate the new setting |
| 218 | + */ |
| 219 | + void rotate(bool rotate) { _rotate = rotate; } |
| 220 | + |
| 221 | + /** |
| 222 | + * The 'ndots' setting from resolv.conf |
| 223 | + * @return ndots |
| 224 | + */ |
| 225 | + uint8_t ndots() const { return _ndots; } |
| 226 | + |
| 227 | + /** |
| 228 | + * Change the ndots setting |
| 229 | + * @param value the new value |
| 230 | + */ |
| 231 | + void ndots(uint8_t value) { _ndots = value; } |
| 232 | +}; |
| 233 | + |
| 234 | +/** |
| 235 | + * End of namespace |
| 236 | + */ |
| 237 | +} |
| 238 | + |
0 commit comments