cfg_hosts.cc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. // Copyright (C) 2014-2015 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <config.h>
  15. #include <dhcpsrv/cfg_hosts.h>
  16. #include <dhcpsrv/hosts_log.h>
  17. #include <exceptions/exceptions.h>
  18. #include <ostream>
  19. using namespace isc::asiolink;
  20. namespace isc {
  21. namespace dhcp {
  22. ConstHostCollection
  23. CfgHosts::getAll(const HWAddrPtr& hwaddr, const DuidPtr& duid) const {
  24. // Do not issue logging message here because it will be logged by
  25. // the getAllInternal method.
  26. ConstHostCollection collection;
  27. getAllInternal<ConstHostCollection>(hwaddr, duid, collection);
  28. return (collection);
  29. }
  30. HostCollection
  31. CfgHosts::getAll(const HWAddrPtr& hwaddr, const DuidPtr& duid) {
  32. // Do not issue logging message here because it will be logged by
  33. // the getAllInternal method.
  34. HostCollection collection;
  35. getAllInternal<HostCollection>(hwaddr, duid, collection);
  36. return (collection);
  37. }
  38. ConstHostCollection
  39. CfgHosts::getAll4(const IOAddress& address) const {
  40. // Do not issue logging message here because it will be logged by
  41. // the getAllInternal4 method.
  42. ConstHostCollection collection;
  43. getAllInternal4<ConstHostCollection>(address, collection);
  44. return (collection);
  45. }
  46. HostCollection
  47. CfgHosts::getAll4(const IOAddress& address) {
  48. // Do not issue logging message here because it will be logged by
  49. // the getAllInternal4 method.
  50. HostCollection collection;
  51. getAllInternal4<HostCollection>(address, collection);
  52. return (collection);
  53. }
  54. ConstHostCollection
  55. CfgHosts::getAll6(const IOAddress& address) const {
  56. // Do not issue logging message here because it will be logged by
  57. // the getAllInternal6 method.
  58. ConstHostCollection collection;
  59. getAllInternal6<ConstHostCollection>(address, collection);
  60. return (collection);
  61. }
  62. HostCollection
  63. CfgHosts::getAll6(const IOAddress& address) {
  64. // Do not issue logging message here because it will be logged by
  65. // the getAllInternal6 method.
  66. HostCollection collection;
  67. getAllInternal6<HostCollection>(address, collection);
  68. return (collection);
  69. }
  70. template<typename Storage>
  71. void
  72. CfgHosts::getAllInternal(const std::vector<uint8_t>& identifier,
  73. const Host::IdentifierType& identifier_type,
  74. Storage& storage) const {
  75. // We will need to transform the identifier into the textual format.
  76. // Until we do it, we mark it as invalid.
  77. std::string identifier_text = "(invalid)";
  78. if (!identifier.empty()) {
  79. try {
  80. // Use Host object to find the textual form of the identifier.
  81. // This may throw exception if the identifier is invalid.
  82. Host host(&identifier[0], identifier.size(), identifier_type,
  83. SubnetID(0), SubnetID(0), IOAddress::IPV4_ZERO_ADDRESS());
  84. identifier_text = host.getIdentifierAsText();
  85. } catch (...) {
  86. // Suppress exception and keep using (invalid) as an
  87. // identifier. We will log that the identifier is
  88. // invalid and return.
  89. }
  90. }
  91. // This will log that we're invoking this function with the specified
  92. // identifier. The identifier may also be marked as (invalid) if it
  93. // had 0 length or its type is unsupported.
  94. LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE, HOSTS_CFG_GET_ALL_IDENTIFIER)
  95. .arg(identifier_text);
  96. // Do nothing if the identifier specified is invalid.
  97. if (identifier_text == "(invalid)") {
  98. return;
  99. }
  100. // Use the identifier and identifier type as a composite key.
  101. const HostContainerIndex0& idx = hosts_.get<0>();
  102. boost::tuple<const std::vector<uint8_t>, const Host::IdentifierType> t =
  103. boost::make_tuple(identifier, identifier_type);
  104. // Append each Host object to the storage.
  105. for (HostContainerIndex0::iterator host = idx.lower_bound(t); host != idx.upper_bound(t);
  106. ++host) {
  107. LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE_DETAIL_DATA,
  108. HOSTS_CFG_GET_ALL_IDENTIFIER_HOST)
  109. .arg(identifier_text)
  110. .arg((*host)->toText());
  111. storage.push_back(*host);
  112. }
  113. // Log how many hosts have been found.
  114. LOG_DEBUG(hosts_logger, HOSTS_DBG_RESULTS, HOSTS_CFG_GET_ALL_IDENTIFIER_COUNT)
  115. .arg(identifier_text)
  116. .arg(storage.size());
  117. }
  118. template<typename Storage>
  119. void
  120. CfgHosts::getAllInternal(const HWAddrPtr& hwaddr, const DuidPtr& duid,
  121. Storage& storage) const {
  122. LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE, HOSTS_CFG_GET_ALL_HWADDR_DUID)
  123. .arg(hwaddr ? hwaddr->toText() : "(no-hwaddr)")
  124. .arg(duid ? duid->toText() : "(no-duid)");
  125. // Get hosts using HW address.
  126. if (hwaddr) {
  127. getAllInternal<Storage>(hwaddr->hwaddr_, Host::IDENT_HWADDR, storage);
  128. }
  129. // Get hosts using DUID.
  130. if (duid) {
  131. getAllInternal<Storage>(duid->getDuid(), Host::IDENT_DUID, storage);
  132. }
  133. }
  134. template<typename Storage>
  135. void
  136. CfgHosts::getAllInternal4(const IOAddress& address, Storage& storage) const {
  137. LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE, HOSTS_CFG_GET_ALL_ADDRESS4)
  138. .arg(address.toText());
  139. // Must not specify address other than IPv4.
  140. if (!address.isV4()) {
  141. isc_throw(BadHostAddress, "must specify an IPv4 address when searching"
  142. " for a host, specified address was " << address);
  143. }
  144. // Search for the Host using the reserved IPv4 address as a key.
  145. const HostContainerIndex1& idx = hosts_.get<1>();
  146. HostContainerIndex1Range r = idx.equal_range(address);
  147. // Append each Host object to the storage.
  148. for (HostContainerIndex1::iterator host = r.first; host != r.second;
  149. ++host) {
  150. LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE_DETAIL_DATA,
  151. HOSTS_CFG_GET_ALL_ADDRESS4_HOST)
  152. .arg(address.toText())
  153. .arg((*host)->toText());
  154. storage.push_back(*host);
  155. }
  156. LOG_DEBUG(hosts_logger, HOSTS_DBG_RESULTS, HOSTS_CFG_GET_ALL_ADDRESS4_COUNT)
  157. .arg(address.toText())
  158. .arg(storage.size());
  159. }
  160. template<typename Storage>
  161. void
  162. CfgHosts::getAllInternal6(const IOAddress& address, Storage& storage) const {
  163. LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE, HOSTS_CFG_GET_ALL_ADDRESS6)
  164. .arg(address.toText());
  165. // Must not specify address other than IPv6.
  166. if (!address.isV6()) {
  167. isc_throw(BadHostAddress, "must specify an IPv6 address when searching"
  168. " for a host, specified address was " << address);
  169. }
  170. // Search for the Host using the reserved IPv6 address as a key.
  171. const HostContainerIndex1& idx = hosts_.get<1>();
  172. HostContainerIndex1Range r = idx.equal_range(address);
  173. // Append each Host object to the storage.
  174. for (HostContainerIndex1::iterator host = r.first; host != r.second;
  175. ++host) {
  176. LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE_DETAIL_DATA,
  177. HOSTS_CFG_GET_ALL_ADDRESS6_HOST)
  178. .arg(address.toText())
  179. .arg((*host)->toText());
  180. storage.push_back(*host);
  181. }
  182. LOG_DEBUG(hosts_logger, HOSTS_DBG_RESULTS, HOSTS_CFG_GET_ALL_ADDRESS6_COUNT)
  183. .arg(address.toText())
  184. .arg(storage.size());
  185. }
  186. ConstHostPtr
  187. CfgHosts::get4(const SubnetID& subnet_id, const HWAddrPtr& hwaddr,
  188. const DuidPtr& duid) const {
  189. // Do not log here because getHostInternal logs.
  190. // The false value indicates that it is an IPv4 subnet.
  191. return (getHostInternal(subnet_id, false, hwaddr, duid));
  192. }
  193. HostPtr
  194. CfgHosts::get4(const SubnetID& subnet_id, const HWAddrPtr& hwaddr,
  195. const DuidPtr& duid) {
  196. // Do not log here because getHostInternal logs.
  197. // The false value indicates that it is an IPv4 subnet.
  198. return (getHostInternal(subnet_id, false, hwaddr, duid));
  199. }
  200. ConstHostPtr
  201. CfgHosts::get4(const SubnetID& subnet_id, const IOAddress& address) const {
  202. LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE, HOSTS_CFG_GET_ONE_SUBNET_ID_ADDRESS4)
  203. .arg(subnet_id).arg(address.toText());
  204. ConstHostCollection hosts = getAll4(address);
  205. for (ConstHostCollection::const_iterator host = hosts.begin();
  206. host != hosts.end(); ++host) {
  207. if ((*host)->getIPv4SubnetID() == subnet_id) {
  208. LOG_DEBUG(hosts_logger, HOSTS_DBG_RESULTS,
  209. HOSTS_CFG_GET_ONE_SUBNET_ID_ADDRESS4_HOST)
  210. .arg(subnet_id)
  211. .arg(address.toText())
  212. .arg((*host)->toText());
  213. return (*host);
  214. }
  215. }
  216. LOG_DEBUG(hosts_logger, HOSTS_DBG_RESULTS, HOSTS_CFG_GET_ONE_SUBNET_ID_ADDRESS4_NULL)
  217. .arg(subnet_id).arg(address.toText());
  218. return (ConstHostPtr());
  219. }
  220. ConstHostPtr
  221. CfgHosts::get6(const SubnetID& subnet_id, const DuidPtr& duid,
  222. const HWAddrPtr& hwaddr) const {
  223. // Do not log here because getHostInternal logs.
  224. // The true value indicates that it is an IPv6 subnet.
  225. return (getHostInternal(subnet_id, true, hwaddr, duid));
  226. }
  227. HostPtr
  228. CfgHosts::get6(const SubnetID& subnet_id, const DuidPtr& duid,
  229. const HWAddrPtr& hwaddr) {
  230. // Do not log here because getHostInternal logs.
  231. // The true value indicates that it is an IPv6 subnet.
  232. return (getHostInternal(subnet_id, true, hwaddr, duid));
  233. }
  234. ConstHostPtr
  235. CfgHosts::get6(const IOAddress&, const uint8_t) const {
  236. isc_throw(isc::NotImplemented,
  237. "get6(prefix, len) const is not implemented");
  238. }
  239. HostPtr
  240. CfgHosts::get6(const IOAddress&, const uint8_t) {
  241. isc_throw(isc::NotImplemented, "get6(prefix, len) is not implemented");
  242. }
  243. ConstHostPtr
  244. CfgHosts::get6(const SubnetID& subnet_id,
  245. const asiolink::IOAddress& address) const {
  246. // Do not log here because getHostInternal6 logs.
  247. return (getHostInternal6<ConstHostPtr, ConstHostCollection>(subnet_id, address));
  248. }
  249. HostPtr
  250. CfgHosts::get6(const SubnetID& subnet_id,
  251. const asiolink::IOAddress& address) {
  252. // Do not log here because getHostInternal6 logs.
  253. return (getHostInternal6<HostPtr, HostCollection>(subnet_id, address));
  254. }
  255. template<typename ReturnType, typename Storage>
  256. ReturnType
  257. CfgHosts::getHostInternal6(const SubnetID& subnet_id,
  258. const asiolink::IOAddress& address) const {
  259. LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE, HOSTS_CFG_GET_ONE_SUBNET_ID_ADDRESS6)
  260. .arg(subnet_id).arg(address.toText());
  261. Storage storage;
  262. getAllInternal6<Storage>(subnet_id, address, storage);
  263. switch (storage.size()) {
  264. case 0:
  265. LOG_DEBUG(hosts_logger, HOSTS_DBG_RESULTS,
  266. HOSTS_CFG_GET_ONE_SUBNET_ID_ADDRESS6_NULL)
  267. .arg(subnet_id)
  268. .arg(address.toText());
  269. return (HostPtr());
  270. case 1:
  271. LOG_DEBUG(hosts_logger, HOSTS_DBG_RESULTS,
  272. HOSTS_CFG_GET_ONE_SUBNET_ID_ADDRESS6_HOST)
  273. .arg(subnet_id)
  274. .arg(address.toText())
  275. .arg((*storage.begin())->toText());
  276. return (*storage.begin());
  277. default:
  278. isc_throw(DuplicateHost, "more than one reservation found"
  279. " for the host belonging to the subnet with id '"
  280. << subnet_id << "' and using the address '"
  281. << address.toText() << "'");
  282. }
  283. }
  284. template<typename Storage>
  285. void
  286. CfgHosts::getAllInternal6(const SubnetID& subnet_id,
  287. const asiolink::IOAddress& address,
  288. Storage& storage) const {
  289. LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE, HOSTS_CFG_GET_ALL_SUBNET_ID_ADDRESS6)
  290. .arg(subnet_id).arg(address.toText());
  291. // Must not specify address other than IPv6.
  292. if (!address.isV6()) {
  293. isc_throw(BadHostAddress, "must specify an IPv6 address when searching"
  294. " for a host, specified address was " << address);
  295. }
  296. // Let's get all reservations that match subnet_id, address.
  297. const HostContainer6Index1& idx = hosts6_.get<1>();
  298. HostContainer6Index1Range r = make_pair(idx.lower_bound(boost::make_tuple(subnet_id, address)),
  299. idx.upper_bound(boost::make_tuple(subnet_id, address)));
  300. // For each IPv6 reservation, add the host to the results list. Fortunately,
  301. // in all sane cases, there will be only one such host. (Each host can have
  302. // multiple addresses reserved, but for each (address, subnet_id) there should
  303. // be at most one host reserving it).
  304. for(HostContainer6Index1::iterator resrv = r.first; resrv != r.second; ++resrv) {
  305. LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE_DETAIL_DATA,
  306. HOSTS_CFG_GET_ALL_SUBNET_ID_ADDRESS6_HOST)
  307. .arg(subnet_id)
  308. .arg(address.toText())
  309. .arg(resrv->host_);
  310. storage.push_back(resrv->host_);
  311. }
  312. LOG_DEBUG(hosts_logger, HOSTS_DBG_RESULTS,
  313. HOSTS_CFG_GET_ALL_SUBNET_ID_ADDRESS6_COUNT)
  314. .arg(subnet_id)
  315. .arg(address.toText())
  316. .arg(storage.size());
  317. }
  318. HostPtr
  319. CfgHosts::getHostInternal(const SubnetID& subnet_id, const bool subnet6,
  320. const HWAddrPtr& hwaddr, const DuidPtr& duid) const {
  321. LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE, HOSTS_CFG_GET_ONE_SUBNET_ID_HWADDR_DUID)
  322. .arg(subnet6 ? "IPv6" : "IPv4")
  323. .arg(subnet_id)
  324. .arg(hwaddr ? hwaddr->toText() : "(no-hwaddr)")
  325. .arg(duid ? duid->toText() : "(no-duid)");
  326. // Get all hosts for the HW address and DUID. This may return multiple hosts
  327. // for different subnets, but the number of hosts returned should be low
  328. // because one host presumably doesn't show up in many subnets.
  329. HostCollection hosts;
  330. getAllInternal<HostCollection>(hwaddr, duid, hosts);
  331. HostPtr host;
  332. // Iterate over the returned hosts and select those for which the
  333. // subnet id matches.
  334. for (HostCollection::const_iterator host_it = hosts.begin();
  335. host_it != hosts.end(); ++host_it) {
  336. // Check if this is IPv4 subnet or IPv6 subnet.
  337. SubnetID host_subnet_id = subnet6 ? (*host_it)->getIPv6SubnetID() :
  338. (*host_it)->getIPv4SubnetID();
  339. if (subnet_id == host_subnet_id) {
  340. // If this is the first occurrence of the host for this subnet,
  341. // remember it. But, if we find that this is second @c Host object
  342. // for the same client, it is a misconfiguration. Most likely,
  343. // the administrator has specified one reservation for a HW
  344. // address and another one for the DUID, which gives an ambiguous
  345. // result, and we don't know which reservation we should choose.
  346. // Therefore, throw an exception.
  347. if (!host) {
  348. host = *host_it;
  349. } else {
  350. isc_throw(DuplicateHost, "more than one reservation found"
  351. " for the host belonging to the subnet with id '"
  352. << subnet_id << "' and using the HW address '"
  353. << (hwaddr ? hwaddr->toText(false) : "(null)")
  354. << "' and DUID '"
  355. << (duid ? duid->toText() : "(null)")
  356. << "'");
  357. }
  358. }
  359. }
  360. if (host) {
  361. LOG_DEBUG(hosts_logger, HOSTS_DBG_RESULTS,
  362. HOSTS_CFG_GET_ONE_SUBNET_ID_HWADDR_DUID)
  363. .arg(subnet_id)
  364. .arg(hwaddr ? hwaddr->toText() : "(no-hwaddr)")
  365. .arg(duid ? duid->toText() : "(no-duid)")
  366. .arg(host->toText());
  367. } else {
  368. LOG_DEBUG(hosts_logger, HOSTS_DBG_RESULTS,
  369. HOSTS_CFG_GET_ONE_SUBNET_ID_HWADDR_DUID_NULL)
  370. .arg(subnet_id)
  371. .arg(hwaddr ? hwaddr->toText() : "(no-hwaddr)")
  372. .arg(duid ? duid->toText() : "(no-duid)");
  373. }
  374. return (host);
  375. }
  376. void
  377. CfgHosts::add(const HostPtr& host) {
  378. LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE, HOSTS_CFG_ADD_HOST)
  379. .arg(host ? host->toText() : "(no-host)");
  380. // Sanity check that the host is non-null.
  381. if (!host) {
  382. isc_throw(BadValue, "specified host object must not be NULL when it"
  383. " is added to the configuration");
  384. }
  385. // At least one subnet ID must be non-zero
  386. if (host->getIPv4SubnetID() == 0 && host->getIPv6SubnetID() == 0) {
  387. isc_throw(BadValue, "must not use both IPv4 and IPv6 subnet ids of"
  388. " 0 when adding new host reservation");
  389. }
  390. add4(host);
  391. add6(host);
  392. }
  393. void
  394. CfgHosts::add4(const HostPtr& host) {
  395. /// @todo This may need further sanity checks.
  396. HWAddrPtr hwaddr = host->getHWAddress();
  397. DuidPtr duid = host->getDuid();
  398. // There should be at least one resource reserved: hostname, IPv4
  399. // address, IPv6 address or prefix.
  400. if (host->getHostname().empty() &&
  401. (host->getIPv4Reservation().isV4Zero()) &&
  402. (!host->hasIPv6Reservation())) {
  403. std::ostringstream s;
  404. if (hwaddr) {
  405. s << "for DUID: " << hwaddr->toText();
  406. } else if (duid) {
  407. s << "for HW address: " << duid->toText();
  408. }
  409. isc_throw(BadValue, "specified reservation " << s
  410. << " must include at least one resource, i.e. "
  411. "hostname, IPv4 address or IPv6 address/prefix");
  412. }
  413. // Check for duplicates for the specified IPv4 subnet.
  414. if ((host->getIPv4SubnetID() > 0) &&
  415. get4(host->getIPv4SubnetID(), hwaddr, duid)) {
  416. isc_throw(DuplicateHost, "failed to add new host using the HW"
  417. " address '" << (hwaddr ? hwaddr->toText(false) : "(null)")
  418. << " and DUID '" << (duid ? duid->toText() : "(null)")
  419. << "' to the IPv4 subnet id '" << host->getIPv4SubnetID()
  420. << "' as this host has already been added");
  421. // Check for duplicates for the specified IPv6 subnet.
  422. } else if (host->getIPv6SubnetID() &&
  423. get6(host->getIPv6SubnetID(), duid, hwaddr)) {
  424. isc_throw(DuplicateHost, "failed to add new host using the HW"
  425. " address '" << (hwaddr ? hwaddr->toText(false) : "(null)")
  426. << " and DUID '" << (duid ? duid->toText() : "(null)")
  427. << "' to the IPv6 subnet id '" << host->getIPv6SubnetID()
  428. << "' as this host has already been added");
  429. }
  430. // Check if the address is already reserved for the specified IPv4 subnet.
  431. if (!host->getIPv4Reservation().isV4Zero() &&
  432. (host->getIPv4SubnetID() > 0) &&
  433. get4(host->getIPv4SubnetID(), host->getIPv4Reservation())) {
  434. isc_throw(ReservedAddress, "failed to add new host using the HW"
  435. " address '" << (hwaddr ? hwaddr->toText(false) : "(null)")
  436. << " and DUID '" << (duid ? duid->toText() : "(null)")
  437. << "' to the IPv4 subnet id '" << host->getIPv4SubnetID()
  438. << "' for the address " << host->getIPv4Reservation()
  439. << ": There's already a reservation for this address");
  440. }
  441. /// @todo This may need further sanity checks.
  442. // This is a new instance - add it.
  443. hosts_.insert(host);
  444. }
  445. void
  446. CfgHosts::add6(const HostPtr& host) {
  447. /// @todo This may need further sanity checks.
  448. HWAddrPtr hwaddr = host->getHWAddress();
  449. DuidPtr duid = host->getDuid();
  450. // Get all reservations for this host.
  451. IPv6ResrvRange reservations = host->getIPv6Reservations();
  452. // Check if there are any IPv6 reservations.
  453. if (std::distance(reservations.first, reservations.second) == 0) {
  454. // If there aren't, we don't need to add this to hosts6_, which is used
  455. // for getting hosts by their IPv6 address reservations.
  456. return;
  457. }
  458. // Now for each reservation, insert corresponding (address, host) tuple.
  459. for (IPv6ResrvIterator it = reservations.first; it != reservations.second;
  460. ++it) {
  461. // If there's an entry for this (subnet-id, address), reject it.
  462. if (get6(host->getIPv6SubnetID(), it->second.getPrefix())) {
  463. isc_throw(DuplicateHost, "failed to add address reservation for "
  464. << "host using the HW address '"
  465. << (hwaddr ? hwaddr->toText(false) : "(null)")
  466. << " and DUID '" << (duid ? duid->toText() : "(null)")
  467. << "' to the IPv6 subnet id '" << host->getIPv6SubnetID()
  468. << "' for address/prefix " << it->second.getPrefix()
  469. << ": There's already reservation for this address/prefix");
  470. }
  471. hosts6_.insert(HostResrv6Tuple(it->second, host));
  472. }
  473. }
  474. } // end of namespace isc::dhcp
  475. } // end of namespace isc