cfg_hosts.cc 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. // Copyright (C) 2014-2017 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #include <config.h>
  7. #include <dhcpsrv/cfg_hosts.h>
  8. #include <dhcpsrv/cfg_hosts_util.h>
  9. #include <dhcpsrv/hosts_log.h>
  10. #include <dhcpsrv/cfgmgr.h>
  11. #include <exceptions/exceptions.h>
  12. #include <util/encode/hex.h>
  13. #include <ostream>
  14. #include <string>
  15. #include <vector>
  16. using namespace isc::asiolink;
  17. using namespace isc::data;
  18. namespace isc {
  19. namespace dhcp {
  20. ConstHostCollection
  21. CfgHosts::getAll(const HWAddrPtr& hwaddr, const DuidPtr& duid) const {
  22. // Do not issue logging message here because it will be logged by
  23. // the getAllInternal method.
  24. ConstHostCollection collection;
  25. getAllInternal<ConstHostCollection>(hwaddr, duid, collection);
  26. return (collection);
  27. }
  28. HostCollection
  29. CfgHosts::getAll(const HWAddrPtr& hwaddr, const DuidPtr& duid) {
  30. // Do not issue logging message here because it will be logged by
  31. // the getAllInternal method.
  32. HostCollection collection;
  33. getAllInternal<HostCollection>(hwaddr, duid, collection);
  34. return (collection);
  35. }
  36. ConstHostCollection
  37. CfgHosts::getAll(const Host::IdentifierType& identifier_type,
  38. const uint8_t* identifier_begin,
  39. const size_t identifier_len) const {
  40. // Do not issue logging message here because it will be logged by
  41. // the getAllInternal method.
  42. ConstHostCollection collection;
  43. getAllInternal<ConstHostCollection>(identifier_type, identifier_begin,
  44. identifier_len, collection);
  45. return (collection);
  46. }
  47. HostCollection
  48. CfgHosts::getAll(const Host::IdentifierType& identifier_type,
  49. const uint8_t* identifier_begin, const size_t identifier_len) {
  50. // Do not issue logging message here because it will be logged by
  51. // the getAllInternal method.
  52. HostCollection collection;
  53. getAllInternal<HostCollection>(identifier_type, identifier_begin,
  54. identifier_len, collection);
  55. return (collection);
  56. }
  57. ConstHostCollection
  58. CfgHosts::getAll4(const IOAddress& address) const {
  59. // Do not issue logging message here because it will be logged by
  60. // the getAllInternal4 method.
  61. ConstHostCollection collection;
  62. getAllInternal4<ConstHostCollection>(address, collection);
  63. return (collection);
  64. }
  65. HostCollection
  66. CfgHosts::getAll4(const IOAddress& address) {
  67. // Do not issue logging message here because it will be logged by
  68. // the getAllInternal4 method.
  69. HostCollection collection;
  70. getAllInternal4<HostCollection>(address, collection);
  71. return (collection);
  72. }
  73. ConstHostCollection
  74. CfgHosts::getAll6(const IOAddress& address) const {
  75. // Do not issue logging message here because it will be logged by
  76. // the getAllInternal6 method.
  77. ConstHostCollection collection;
  78. getAllInternal6<ConstHostCollection>(address, collection);
  79. return (collection);
  80. }
  81. HostCollection
  82. CfgHosts::getAll6(const IOAddress& address) {
  83. // Do not issue logging message here because it will be logged by
  84. // the getAllInternal6 method.
  85. HostCollection collection;
  86. getAllInternal6<HostCollection>(address, collection);
  87. return (collection);
  88. }
  89. template<typename Storage>
  90. void
  91. CfgHosts::getAllInternal(const Host::IdentifierType& identifier_type,
  92. const uint8_t* identifier,
  93. const size_t identifier_len,
  94. Storage& storage) const {
  95. // Convert host identifier into textual format for logging purposes.
  96. // This conversion is exception free.
  97. std::string identifier_text = Host::getIdentifierAsText(identifier_type,
  98. identifier,
  99. identifier_len);
  100. LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE, HOSTS_CFG_GET_ALL_IDENTIFIER)
  101. .arg(identifier_text);
  102. // Use the identifier and identifier type as a composite key.
  103. const HostContainerIndex0& idx = hosts_.get<0>();
  104. boost::tuple<const std::vector<uint8_t>, const Host::IdentifierType> t =
  105. boost::make_tuple(std::vector<uint8_t>(identifier,
  106. identifier + identifier_len),
  107. identifier_type);
  108. // Append each Host object to the storage.
  109. for (HostContainerIndex0::iterator host = idx.lower_bound(t);
  110. host != idx.upper_bound(t);
  111. ++host) {
  112. LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE_DETAIL_DATA,
  113. HOSTS_CFG_GET_ALL_IDENTIFIER_HOST)
  114. .arg(identifier_text)
  115. .arg((*host)->toText());
  116. storage.push_back(*host);
  117. }
  118. // Log how many hosts have been found.
  119. LOG_DEBUG(hosts_logger, HOSTS_DBG_RESULTS, HOSTS_CFG_GET_ALL_IDENTIFIER_COUNT)
  120. .arg(identifier_text)
  121. .arg(storage.size());
  122. }
  123. template<typename Storage>
  124. void
  125. CfgHosts::getAllInternal(const HWAddrPtr& hwaddr, const DuidPtr& duid,
  126. Storage& storage) const {
  127. LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE, HOSTS_CFG_GET_ALL_HWADDR_DUID)
  128. .arg(hwaddr ? hwaddr->toText() : "(no-hwaddr)")
  129. .arg(duid ? duid->toText() : "(no-duid)");
  130. // Get hosts using HW address.
  131. if (hwaddr && !hwaddr->hwaddr_.empty()) {
  132. getAllInternal<Storage>(Host::IDENT_HWADDR, &hwaddr->hwaddr_[0],
  133. hwaddr->hwaddr_.size(), storage);
  134. }
  135. // Get hosts using DUID.
  136. if (duid && !duid->getDuid().empty()) {
  137. getAllInternal<Storage>(Host::IDENT_DUID, &duid->getDuid()[0],
  138. duid->getDuid().size(), storage);
  139. }
  140. }
  141. template<typename Storage>
  142. void
  143. CfgHosts::getAllInternal4(const IOAddress& address, Storage& storage) const {
  144. LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE, HOSTS_CFG_GET_ALL_ADDRESS4)
  145. .arg(address.toText());
  146. // Must not specify address other than IPv4.
  147. if (!address.isV4()) {
  148. isc_throw(BadHostAddress, "must specify an IPv4 address when searching"
  149. " for a host, specified address was " << address);
  150. }
  151. // Search for the Host using the reserved IPv4 address as a key.
  152. const HostContainerIndex1& idx = hosts_.get<1>();
  153. HostContainerIndex1Range r = idx.equal_range(address);
  154. // Append each Host object to the storage.
  155. for (HostContainerIndex1::iterator host = r.first; host != r.second;
  156. ++host) {
  157. LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE_DETAIL_DATA,
  158. HOSTS_CFG_GET_ALL_ADDRESS4_HOST)
  159. .arg(address.toText())
  160. .arg((*host)->toText());
  161. storage.push_back(*host);
  162. }
  163. LOG_DEBUG(hosts_logger, HOSTS_DBG_RESULTS, HOSTS_CFG_GET_ALL_ADDRESS4_COUNT)
  164. .arg(address.toText())
  165. .arg(storage.size());
  166. }
  167. template<typename Storage>
  168. void
  169. CfgHosts::getAllInternal6(const IOAddress& address, Storage& storage) const {
  170. LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE, HOSTS_CFG_GET_ALL_ADDRESS6)
  171. .arg(address.toText());
  172. // Must not specify address other than IPv6.
  173. if (!address.isV6()) {
  174. isc_throw(BadHostAddress, "must specify an IPv6 address when searching"
  175. " for a host, specified address was " << address);
  176. }
  177. // Search for the Host using the reserved IPv6 address as a key.
  178. const HostContainerIndex1& idx = hosts_.get<1>();
  179. HostContainerIndex1Range r = idx.equal_range(address);
  180. // Append each Host object to the storage.
  181. for (HostContainerIndex1::iterator host = r.first; host != r.second;
  182. ++host) {
  183. LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE_DETAIL_DATA,
  184. HOSTS_CFG_GET_ALL_ADDRESS6_HOST)
  185. .arg(address.toText())
  186. .arg((*host)->toText());
  187. storage.push_back(*host);
  188. }
  189. LOG_DEBUG(hosts_logger, HOSTS_DBG_RESULTS, HOSTS_CFG_GET_ALL_ADDRESS6_COUNT)
  190. .arg(address.toText())
  191. .arg(storage.size());
  192. }
  193. ConstHostPtr
  194. CfgHosts::get4(const SubnetID& subnet_id, const HWAddrPtr& hwaddr,
  195. const DuidPtr& duid) const {
  196. // Do not log here because getHostInternal logs.
  197. // The false value indicates that it is an IPv4 subnet.
  198. HostPtr host;
  199. if (hwaddr && !hwaddr->hwaddr_.empty()) {
  200. host = getHostInternal(subnet_id, false, Host::IDENT_HWADDR,
  201. &hwaddr->hwaddr_[0],
  202. hwaddr->hwaddr_.size());
  203. }
  204. if (!host && duid && !duid->getDuid().empty()) {
  205. host = getHostInternal(subnet_id, false, Host::IDENT_DUID,
  206. &duid->getDuid()[0],
  207. duid->getDuid().size());
  208. }
  209. return (host);
  210. }
  211. HostPtr
  212. CfgHosts::get4(const SubnetID& subnet_id, const HWAddrPtr& hwaddr,
  213. const DuidPtr& duid) {
  214. // Do not log here because getHostInternal logs.
  215. // The false value indicates that it is an IPv4 subnet.
  216. HostPtr host;
  217. if (hwaddr && !hwaddr->hwaddr_.empty()) {
  218. host = getHostInternal(subnet_id, false, Host::IDENT_HWADDR,
  219. &hwaddr->hwaddr_[0],
  220. hwaddr->hwaddr_.size());
  221. }
  222. if (!host && duid && !duid->getDuid().empty()) {
  223. host = getHostInternal(subnet_id, false, Host::IDENT_DUID,
  224. &duid->getDuid()[0],
  225. duid->getDuid().size());
  226. }
  227. return (host);
  228. }
  229. ConstHostPtr
  230. CfgHosts::get4(const SubnetID& subnet_id,
  231. const Host::IdentifierType& identifier_type,
  232. const uint8_t* identifier_begin,
  233. const size_t identifier_len) const {
  234. return (getHostInternal(subnet_id, false, identifier_type, identifier_begin,
  235. identifier_len));
  236. }
  237. HostPtr
  238. CfgHosts::get4(const SubnetID& subnet_id,
  239. const Host::IdentifierType& identifier_type,
  240. const uint8_t* identifier_begin,
  241. const size_t identifier_len) {
  242. return (getHostInternal(subnet_id, false, identifier_type, identifier_begin,
  243. identifier_len));
  244. }
  245. ConstHostPtr
  246. CfgHosts::get4(const SubnetID& subnet_id, const IOAddress& address) const {
  247. LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE, HOSTS_CFG_GET_ONE_SUBNET_ID_ADDRESS4)
  248. .arg(subnet_id).arg(address.toText());
  249. ConstHostCollection hosts = getAll4(address);
  250. for (ConstHostCollection::const_iterator host = hosts.begin();
  251. host != hosts.end(); ++host) {
  252. if ((*host)->getIPv4SubnetID() == subnet_id) {
  253. LOG_DEBUG(hosts_logger, HOSTS_DBG_RESULTS,
  254. HOSTS_CFG_GET_ONE_SUBNET_ID_ADDRESS4_HOST)
  255. .arg(subnet_id)
  256. .arg(address.toText())
  257. .arg((*host)->toText());
  258. return (*host);
  259. }
  260. }
  261. LOG_DEBUG(hosts_logger, HOSTS_DBG_RESULTS, HOSTS_CFG_GET_ONE_SUBNET_ID_ADDRESS4_NULL)
  262. .arg(subnet_id).arg(address.toText());
  263. return (ConstHostPtr());
  264. }
  265. ConstHostPtr
  266. CfgHosts::get6(const SubnetID& subnet_id, const DuidPtr& duid,
  267. const HWAddrPtr& hwaddr) const {
  268. // Do not log here because getHostInternal logs.
  269. // The true value indicates that it is an IPv6 subnet.
  270. HostPtr host;
  271. if (duid && !duid->getDuid().empty()) {
  272. host = getHostInternal(subnet_id, true, Host::IDENT_DUID,
  273. &duid->getDuid()[0],
  274. duid->getDuid().size());
  275. }
  276. if (!host && hwaddr && !hwaddr->hwaddr_.empty()) {
  277. host = getHostInternal(subnet_id, true, Host::IDENT_HWADDR,
  278. &hwaddr->hwaddr_[0],
  279. hwaddr->hwaddr_.size());
  280. }
  281. return (host);
  282. }
  283. HostPtr
  284. CfgHosts::get6(const SubnetID& subnet_id, const DuidPtr& duid,
  285. const HWAddrPtr& hwaddr) {
  286. // Do not log here because getHostInternal logs.
  287. // The true value indicates that it is an IPv6 subnet.
  288. HostPtr host;
  289. if (duid && !duid->getDuid().empty()) {
  290. host = getHostInternal(subnet_id, true, Host::IDENT_DUID,
  291. &duid->getDuid()[0],
  292. duid->getDuid().size());
  293. }
  294. if (!host && hwaddr && !hwaddr->hwaddr_.empty()) {
  295. host = getHostInternal(subnet_id, true, Host::IDENT_HWADDR,
  296. &hwaddr->hwaddr_[0],
  297. hwaddr->hwaddr_.size());
  298. }
  299. return (host);
  300. }
  301. ConstHostPtr
  302. CfgHosts::get6(const SubnetID& subnet_id,
  303. const Host::IdentifierType& identifier_type,
  304. const uint8_t* identifier_begin,
  305. const size_t identifier_len) const {
  306. return (getHostInternal(subnet_id, true, identifier_type, identifier_begin,
  307. identifier_len));
  308. }
  309. HostPtr
  310. CfgHosts::get6(const SubnetID& subnet_id,
  311. const Host::IdentifierType& identifier_type,
  312. const uint8_t* identifier_begin,
  313. const size_t identifier_len) {
  314. return (getHostInternal(subnet_id, true, identifier_type, identifier_begin,
  315. identifier_len));
  316. }
  317. ConstHostPtr
  318. CfgHosts::get6(const IOAddress& prefix, const uint8_t prefix_len) const {
  319. return (getHostInternal6<ConstHostPtr>(prefix, prefix_len));
  320. }
  321. HostPtr
  322. CfgHosts::get6(const IOAddress& prefix, const uint8_t prefix_len) {
  323. return (getHostInternal6<HostPtr>(prefix, prefix_len));
  324. }
  325. ConstHostPtr
  326. CfgHosts::get6(const SubnetID& subnet_id,
  327. const asiolink::IOAddress& address) const {
  328. // Do not log here because getHostInternal6 logs.
  329. return (getHostInternal6<ConstHostPtr, ConstHostCollection>(subnet_id, address));
  330. }
  331. HostPtr
  332. CfgHosts::get6(const SubnetID& subnet_id,
  333. const asiolink::IOAddress& address) {
  334. // Do not log here because getHostInternal6 logs.
  335. return (getHostInternal6<HostPtr, HostCollection>(subnet_id, address));
  336. }
  337. template<typename ReturnType, typename Storage>
  338. ReturnType
  339. CfgHosts::getHostInternal6(const SubnetID& subnet_id,
  340. const asiolink::IOAddress& address) const {
  341. LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE, HOSTS_CFG_GET_ONE_SUBNET_ID_ADDRESS6)
  342. .arg(subnet_id).arg(address.toText());
  343. Storage storage;
  344. getAllInternal6<Storage>(subnet_id, address, storage);
  345. switch (storage.size()) {
  346. case 0:
  347. LOG_DEBUG(hosts_logger, HOSTS_DBG_RESULTS,
  348. HOSTS_CFG_GET_ONE_SUBNET_ID_ADDRESS6_NULL)
  349. .arg(subnet_id)
  350. .arg(address.toText());
  351. return (HostPtr());
  352. case 1:
  353. LOG_DEBUG(hosts_logger, HOSTS_DBG_RESULTS,
  354. HOSTS_CFG_GET_ONE_SUBNET_ID_ADDRESS6_HOST)
  355. .arg(subnet_id)
  356. .arg(address.toText())
  357. .arg((*storage.begin())->toText());
  358. return (*storage.begin());
  359. default:
  360. isc_throw(DuplicateHost, "more than one reservation found"
  361. " for the host belonging to the subnet with id '"
  362. << subnet_id << "' and using the address '"
  363. << address.toText() << "'");
  364. }
  365. }
  366. template<typename ReturnType>
  367. ReturnType
  368. CfgHosts::getHostInternal6(const asiolink::IOAddress& prefix,
  369. const uint8_t prefix_len) const {
  370. LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE, HOSTS_CFG_GET_ONE_PREFIX)
  371. .arg(prefix.toText()).arg(static_cast<int>(prefix_len));
  372. // Let's get all reservations that match subnet_id, address.
  373. const HostContainer6Index0& idx = hosts6_.get<0>();
  374. HostContainer6Index0Range r = make_pair(idx.lower_bound(prefix),
  375. idx.upper_bound(prefix));
  376. for (HostContainer6Index0::iterator resrv = r.first; resrv != r.second;
  377. ++resrv) {
  378. if (resrv->resrv_.getPrefixLen() == prefix_len) {
  379. LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE_DETAIL_DATA,
  380. HOSTS_CFG_GET_ONE_PREFIX_HOST)
  381. .arg(prefix.toText())
  382. .arg(static_cast<int>(prefix_len))
  383. .arg(resrv->host_->toText());
  384. return (resrv->host_);
  385. }
  386. }
  387. LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE_DETAIL_DATA,
  388. HOSTS_CFG_GET_ONE_PREFIX_NULL)
  389. .arg(prefix.toText())
  390. .arg(static_cast<int>(prefix_len));
  391. return (ReturnType());
  392. }
  393. template<typename Storage>
  394. void
  395. CfgHosts::getAllInternal6(const SubnetID& subnet_id,
  396. const asiolink::IOAddress& address,
  397. Storage& storage) const {
  398. LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE, HOSTS_CFG_GET_ALL_SUBNET_ID_ADDRESS6)
  399. .arg(subnet_id).arg(address.toText());
  400. // Must not specify address other than IPv6.
  401. if (!address.isV6()) {
  402. isc_throw(BadHostAddress, "must specify an IPv6 address when searching"
  403. " for a host, specified address was " << address);
  404. }
  405. // Let's get all reservations that match subnet_id, address.
  406. const HostContainer6Index1& idx = hosts6_.get<1>();
  407. HostContainer6Index1Range r = make_pair(idx.lower_bound(boost::make_tuple(subnet_id, address)),
  408. idx.upper_bound(boost::make_tuple(subnet_id, address)));
  409. // For each IPv6 reservation, add the host to the results list. Fortunately,
  410. // in all sane cases, there will be only one such host. (Each host can have
  411. // multiple addresses reserved, but for each (address, subnet_id) there should
  412. // be at most one host reserving it).
  413. for(HostContainer6Index1::iterator resrv = r.first; resrv != r.second; ++resrv) {
  414. LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE_DETAIL_DATA,
  415. HOSTS_CFG_GET_ALL_SUBNET_ID_ADDRESS6_HOST)
  416. .arg(subnet_id)
  417. .arg(address.toText())
  418. .arg(resrv->host_->toText());
  419. storage.push_back(resrv->host_);
  420. }
  421. LOG_DEBUG(hosts_logger, HOSTS_DBG_RESULTS,
  422. HOSTS_CFG_GET_ALL_SUBNET_ID_ADDRESS6_COUNT)
  423. .arg(subnet_id)
  424. .arg(address.toText())
  425. .arg(storage.size());
  426. }
  427. HostPtr
  428. CfgHosts::getHostInternal(const SubnetID& subnet_id, const bool subnet6,
  429. const Host::IdentifierType& identifier_type,
  430. const uint8_t* identifier,
  431. const size_t identifier_len) const {
  432. LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE, HOSTS_CFG_GET_ONE_SUBNET_ID_IDENTIFIER)
  433. .arg(subnet6 ? "IPv6" : "IPv4")
  434. .arg(subnet_id)
  435. .arg(Host::getIdentifierAsText(identifier_type, identifier, identifier_len));
  436. // Get all hosts for a specified identifier. This may return multiple hosts
  437. // for different subnets, but the number of hosts returned should be low
  438. // because one host presumably doesn't show up in many subnets.
  439. HostCollection hosts;
  440. getAllInternal<HostCollection>(identifier_type, identifier, identifier_len,
  441. hosts);
  442. HostPtr host;
  443. // Iterate over the returned hosts and select those for which the
  444. // subnet id matches.
  445. for (HostCollection::const_iterator host_it = hosts.begin();
  446. host_it != hosts.end(); ++host_it) {
  447. // Check if this is IPv4 subnet or IPv6 subnet.
  448. SubnetID host_subnet_id = subnet6 ? (*host_it)->getIPv6SubnetID() :
  449. (*host_it)->getIPv4SubnetID();
  450. if (subnet_id == host_subnet_id) {
  451. // If this is the first occurrence of the host for this subnet,
  452. // remember it. But, if we find that this is second @c Host object
  453. // for the same client, it is a misconfiguration. Most likely,
  454. // the administrator has specified one reservation for a HW
  455. // address and another one for the DUID, which gives an ambiguous
  456. // result, and we don't know which reservation we should choose.
  457. // Therefore, throw an exception.
  458. if (!host) {
  459. host = *host_it;
  460. } else {
  461. isc_throw(DuplicateHost, "more than one reservation found"
  462. " for the host belonging to the subnet with id '"
  463. << subnet_id << "' and using the identifier '"
  464. << Host::getIdentifierAsText(identifier_type,
  465. identifier,
  466. identifier_len)
  467. << "'");
  468. }
  469. }
  470. }
  471. if (host) {
  472. LOG_DEBUG(hosts_logger, HOSTS_DBG_RESULTS,
  473. HOSTS_CFG_GET_ONE_SUBNET_ID_IDENTIFIER_HOST)
  474. .arg(subnet_id)
  475. .arg(Host::getIdentifierAsText(identifier_type, identifier,
  476. identifier_len))
  477. .arg(host->toText());
  478. } else {
  479. LOG_DEBUG(hosts_logger, HOSTS_DBG_RESULTS,
  480. HOSTS_CFG_GET_ONE_SUBNET_ID_IDENTIFIER_NULL)
  481. .arg(subnet_id)
  482. .arg(Host::getIdentifierAsText(identifier_type, identifier,
  483. identifier_len));
  484. }
  485. return (host);
  486. }
  487. void
  488. CfgHosts::add(const HostPtr& host) {
  489. LOG_DEBUG(hosts_logger, HOSTS_DBG_TRACE, HOSTS_CFG_ADD_HOST)
  490. .arg(host ? host->toText() : "(no-host)");
  491. // Sanity check that the host is non-null.
  492. if (!host) {
  493. isc_throw(BadValue, "specified host object must not be NULL when it"
  494. " is added to the configuration");
  495. }
  496. // At least one subnet ID must be non-zero
  497. if (host->getIPv4SubnetID() == 0 && host->getIPv6SubnetID() == 0) {
  498. isc_throw(BadValue, "must not use both IPv4 and IPv6 subnet ids of"
  499. " 0 when adding new host reservation");
  500. }
  501. add4(host);
  502. add6(host);
  503. }
  504. void
  505. CfgHosts::add4(const HostPtr& host) {
  506. HWAddrPtr hwaddr = host->getHWAddress();
  507. DuidPtr duid = host->getDuid();
  508. // There should be at least one resource reserved: hostname, IPv4
  509. // address, siaddr, sname, file or IPv6 address or prefix.
  510. /// @todo: this check should be done in add(), not in add4()
  511. if (host->getHostname().empty() &&
  512. (host->getIPv4Reservation().isV4Zero()) &&
  513. !host->hasIPv6Reservation() &&
  514. host->getNextServer().isV4Zero() &&
  515. host->getServerHostname().empty() &&
  516. host->getBootFileName().empty() &&
  517. host->getCfgOption4()->empty() &&
  518. host->getCfgOption6()->empty() &&
  519. host->getClientClasses4().empty() &&
  520. host->getClientClasses6().empty()) {
  521. std::ostringstream s;
  522. if (hwaddr) {
  523. s << "for DUID: " << hwaddr->toText();
  524. } else if (duid) {
  525. s << "for HW address: " << duid->toText();
  526. }
  527. isc_throw(BadValue, "specified reservation " << s.str()
  528. << " must include at least one resource, i.e. "
  529. "hostname, IPv4 address, IPv6 address/prefix, "
  530. "options");
  531. }
  532. // Check for duplicates for the specified IPv4 subnet.
  533. if ((host->getIPv4SubnetID() > 0) &&
  534. get4(host->getIPv4SubnetID(), hwaddr, duid)) {
  535. isc_throw(DuplicateHost, "failed to add new host using the HW"
  536. " address '" << (hwaddr ? hwaddr->toText(false) : "(null)")
  537. << " and DUID '" << (duid ? duid->toText() : "(null)")
  538. << "' to the IPv4 subnet id '" << host->getIPv4SubnetID()
  539. << "' as this host has already been added");
  540. // Check for duplicates for the specified IPv6 subnet.
  541. } else if (host->getIPv6SubnetID() &&
  542. get6(host->getIPv6SubnetID(), duid, hwaddr)) {
  543. isc_throw(DuplicateHost, "failed to add new host using the HW"
  544. " address '" << (hwaddr ? hwaddr->toText(false) : "(null)")
  545. << " and DUID '" << (duid ? duid->toText() : "(null)")
  546. << "' to the IPv6 subnet id '" << host->getIPv6SubnetID()
  547. << "' as this host has already been added");
  548. }
  549. // Check if the address is already reserved for the specified IPv4 subnet.
  550. if (!host->getIPv4Reservation().isV4Zero() &&
  551. (host->getIPv4SubnetID() > 0) &&
  552. get4(host->getIPv4SubnetID(), host->getIPv4Reservation())) {
  553. isc_throw(ReservedAddress, "failed to add new host using the HW"
  554. " address '" << (hwaddr ? hwaddr->toText(false) : "(null)")
  555. << " and DUID '" << (duid ? duid->toText() : "(null)")
  556. << "' to the IPv4 subnet id '" << host->getIPv4SubnetID()
  557. << "' for the address " << host->getIPv4Reservation()
  558. << ": There's already a reservation for this address");
  559. }
  560. // Check if the (identifier type, identifier) tuple is already used.
  561. const std::vector<uint8_t>& id = host->getIdentifier();
  562. if ((host->getIPv4SubnetID() > 0) && !id.empty()) {
  563. if (get4(host->getIPv4SubnetID(), host->getIdentifierType(), &id[0],
  564. id.size())) {
  565. isc_throw(DuplicateHost, "failed to add duplicate IPv4 host using identifier: "
  566. << Host::getIdentifierAsText(host->getIdentifierType(),
  567. &id[0], id.size()));
  568. }
  569. }
  570. // This is a new instance - add it.
  571. hosts_.insert(host);
  572. }
  573. void
  574. CfgHosts::add6(const HostPtr& host) {
  575. if (host->getIPv6SubnetID() == 0) {
  576. // This is IPv4-only host. No need to add it to v6 tables.
  577. return;
  578. }
  579. HWAddrPtr hwaddr = host->getHWAddress();
  580. DuidPtr duid = host->getDuid();
  581. // Get all reservations for this host.
  582. IPv6ResrvRange reservations = host->getIPv6Reservations();
  583. // Check if there are any IPv6 reservations.
  584. if (std::distance(reservations.first, reservations.second) == 0) {
  585. // If there aren't, we don't need to add this to hosts6_, which is used
  586. // for getting hosts by their IPv6 address reservations.
  587. return;
  588. }
  589. // Now for each reservation, insert corresponding (address, host) tuple.
  590. for (IPv6ResrvIterator it = reservations.first; it != reservations.second;
  591. ++it) {
  592. // If there's an entry for this (subnet-id, address), reject it.
  593. if (get6(host->getIPv6SubnetID(), it->second.getPrefix())) {
  594. isc_throw(DuplicateHost, "failed to add address reservation for "
  595. << "host using the HW address '"
  596. << (hwaddr ? hwaddr->toText(false) : "(null)")
  597. << " and DUID '" << (duid ? duid->toText() : "(null)")
  598. << "' to the IPv6 subnet id '" << host->getIPv6SubnetID()
  599. << "' for address/prefix " << it->second.getPrefix()
  600. << ": There's already reservation for this address/prefix");
  601. }
  602. hosts6_.insert(HostResrv6Tuple(it->second, host));
  603. }
  604. }
  605. ElementPtr
  606. CfgHosts::toElement() const {
  607. uint16_t family = CfgMgr::instance().getFamily();
  608. if (family == AF_INET) {
  609. return (toElement4());
  610. } else if (family == AF_INET6) {
  611. return (toElement6());
  612. } else {
  613. isc_throw(ToElementError, "CfgHosts::toElement: unknown "
  614. "address family: " << family);
  615. }
  616. }
  617. ElementPtr
  618. CfgHosts::toElement4() const {
  619. CfgHostsList result;
  620. // Iterate using arbitrary the index 0
  621. const HostContainerIndex0& idx = hosts_.get<0>();
  622. for (HostContainerIndex0::const_iterator host = idx.begin();
  623. host != idx.end(); ++host) {
  624. // Get the subnet ID
  625. SubnetID subnet_id = (*host)->getIPv4SubnetID();
  626. // Prepare the map
  627. ElementPtr map = Element::createMap();
  628. // Set the identifier
  629. Host::IdentifierType id_type = (*host)->getIdentifierType();
  630. if (id_type == Host::IDENT_HWADDR) {
  631. HWAddrPtr hwaddr = (*host)->getHWAddress();
  632. map->set("hw-address", Element::create(hwaddr->toText(false)));
  633. } else if (id_type == Host::IDENT_DUID) {
  634. DuidPtr duid = (*host)->getDuid();
  635. map->set("duid", Element::create(duid->toText()));
  636. } else if (id_type == Host::IDENT_CIRCUIT_ID) {
  637. const std::vector<uint8_t>& bin = (*host)->getIdentifier();
  638. std::string circuit_id = util::encode::encodeHex(bin);
  639. map->set("circuit-id", Element::create(circuit_id));
  640. } else if (id_type == Host::IDENT_CLIENT_ID) {
  641. const std::vector<uint8_t>& bin = (*host)->getIdentifier();
  642. std::string client_id = util::encode::encodeHex(bin);
  643. map->set("client-id", Element::create(client_id));
  644. } else if (id_type == Host::IDENT_FLEX) {
  645. const std::vector<uint8_t>& bin = (*host)->getIdentifier();
  646. std::string flex = util::encode::encodeHex(bin);
  647. map->set("flex-id", Element::create(flex));
  648. } else {
  649. isc_throw(ToElementError, "invalid identifier type: " << id_type);
  650. }
  651. // Set the reservation
  652. const IOAddress& address = (*host)->getIPv4Reservation();
  653. map->set("ip-address", Element::create(address.toText()));
  654. // Set the hostname
  655. const std::string& hostname = (*host)->getHostname();
  656. map->set("hostname", Element::create(hostname));
  657. // Set next-server
  658. const IOAddress& next_server = (*host)->getNextServer();
  659. map->set("next-server", Element::create(next_server.toText()));
  660. // Set server-hostname
  661. const std::string& server_hostname = (*host)->getServerHostname();
  662. map->set("server-hostname", Element::create(server_hostname));
  663. // Set boot-file-name
  664. const std::string& boot_file_name = (*host)->getBootFileName();
  665. map->set("boot-file-name", Element::create(boot_file_name));
  666. // Set client-classes
  667. const ClientClasses& cclasses = (*host)->getClientClasses4();
  668. ElementPtr classes = Element::createList();
  669. for (ClientClasses::const_iterator cclass = cclasses.cbegin();
  670. cclass != cclasses.end(); ++cclass) {
  671. classes->add(Element::create(*cclass));
  672. }
  673. map->set("client-classes", classes);
  674. // Set option-data
  675. ConstCfgOptionPtr opts = (*host)->getCfgOption4();
  676. map->set("option-data", opts->toElement());
  677. // Push the map on the list
  678. result.add(subnet_id, map);
  679. }
  680. return (result.externalize());
  681. }
  682. ElementPtr
  683. CfgHosts::toElement6() const {
  684. CfgHostsList result;
  685. // Iterate using arbitrary the index 0
  686. const HostContainerIndex0& idx = hosts_.get<0>();
  687. for (HostContainerIndex0::const_iterator host = idx.begin();
  688. host != idx.end(); ++host) {
  689. // Get the subnet ID
  690. SubnetID subnet_id = (*host)->getIPv6SubnetID();
  691. // Prepare the map
  692. ElementPtr map = Element::createMap();
  693. // Set the identifier
  694. Host::IdentifierType id_type = (*host)->getIdentifierType();
  695. if (id_type == Host::IDENT_HWADDR) {
  696. HWAddrPtr hwaddr = (*host)->getHWAddress();
  697. map->set("hw-address", Element::create(hwaddr->toText(false)));
  698. } else if (id_type == Host::IDENT_DUID) {
  699. DuidPtr duid = (*host)->getDuid();
  700. map->set("duid", Element::create(duid->toText()));
  701. } else if (id_type == Host::IDENT_CIRCUIT_ID) {
  702. isc_throw(ToElementError, "unexpected circuit-id DUID type");
  703. } else if (id_type == Host::IDENT_CLIENT_ID) {
  704. isc_throw(ToElementError, "unexpected client-id DUID type");
  705. } else if (id_type == Host::IDENT_FLEX) {
  706. const std::vector<uint8_t>& bin = (*host)->getIdentifier();
  707. std::string flex = util::encode::encodeHex(bin);
  708. map->set("flex-id", Element::create(flex));
  709. } else {
  710. isc_throw(ToElementError, "invalid DUID type: " << id_type);
  711. }
  712. // Set reservations (ip-addresses)
  713. IPv6ResrvRange na_resv =
  714. (*host)->getIPv6Reservations(IPv6Resrv::TYPE_NA);
  715. ElementPtr resvs = Element::createList();
  716. for (IPv6ResrvIterator resv = na_resv.first;
  717. resv != na_resv.second; ++resv) {
  718. resvs->add(Element::create(resv->second.toText()));
  719. }
  720. map->set("ip-addresses", resvs);
  721. // Set reservations (prefixes)
  722. IPv6ResrvRange pd_resv =
  723. (*host)->getIPv6Reservations(IPv6Resrv::TYPE_PD);
  724. resvs = Element::createList();
  725. for (IPv6ResrvIterator resv = pd_resv.first;
  726. resv != pd_resv.second; ++resv) {
  727. resvs->add(Element::create(resv->second.toText()));
  728. }
  729. map->set("prefixes", resvs);
  730. // Set the hostname
  731. const std::string& hostname = (*host)->getHostname();
  732. map->set("hostname", Element::create(hostname));
  733. // Set client-classes
  734. const ClientClasses& cclasses = (*host)->getClientClasses6();
  735. ElementPtr classes = Element::createList();
  736. for (ClientClasses::const_iterator cclass = cclasses.cbegin();
  737. cclass != cclasses.end(); ++cclass) {
  738. classes->add(Element::create(*cclass));
  739. }
  740. map->set("client-classes", classes);
  741. // Set option-data
  742. ConstCfgOptionPtr opts = (*host)->getCfgOption6();
  743. map->set("option-data", opts->toElement());
  744. // Push the map on the list
  745. result.add(subnet_id, map);
  746. }
  747. return (result.externalize());
  748. }
  749. } // end of namespace isc::dhcp
  750. } // end of namespace isc