host.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. // Copyright (C) 2014-2016 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/host.h>
  8. #include <util/encode/hex.h>
  9. #include <util/strutil.h>
  10. #include <exceptions/exceptions.h>
  11. #include <sstream>
  12. namespace isc {
  13. namespace dhcp {
  14. IPv6Resrv::IPv6Resrv(const Type& type,
  15. const asiolink::IOAddress& prefix,
  16. const uint8_t prefix_len)
  17. : type_(type), prefix_(asiolink::IOAddress("::")), prefix_len_(128) {
  18. // Validate and set the actual values.
  19. set(type, prefix, prefix_len);
  20. }
  21. void
  22. IPv6Resrv::set(const Type& type, const asiolink::IOAddress& prefix,
  23. const uint8_t prefix_len) {
  24. if (!prefix.isV6() || prefix.isV6Multicast()) {
  25. isc_throw(isc::BadValue, "invalid prefix '" << prefix
  26. << "' for new IPv6 reservation");
  27. } else if (prefix_len > 128) {
  28. isc_throw(isc::BadValue, "invalid prefix length '"
  29. << static_cast<int>(prefix_len)
  30. << "' for new IPv6 reservation");
  31. } else if ((type == TYPE_NA) && (prefix_len != 128)) {
  32. isc_throw(isc::BadValue, "invalid prefix length '"
  33. << static_cast<int>(prefix_len)
  34. << "' for reserved IPv6 address, expected 128");
  35. }
  36. type_ = type;
  37. prefix_ = prefix;
  38. prefix_len_ = prefix_len;
  39. }
  40. std::string
  41. IPv6Resrv::toText() const {
  42. std::ostringstream s;
  43. s << prefix_;
  44. // For PD, append prefix length.
  45. if (getType() == TYPE_PD) {
  46. s << "/" << static_cast<int>(prefix_len_);
  47. }
  48. return (s.str());
  49. }
  50. bool
  51. IPv6Resrv::operator==(const IPv6Resrv& other) const {
  52. return (type_ == other.type_ &&
  53. prefix_ == other.prefix_ &&
  54. prefix_len_ == other.prefix_len_);
  55. }
  56. bool
  57. IPv6Resrv::operator!=(const IPv6Resrv& other) const {
  58. return (!operator==(other));
  59. }
  60. Host::Host(const uint8_t* identifier, const size_t identifier_len,
  61. const IdentifierType& identifier_type,
  62. const SubnetID ipv4_subnet_id, const SubnetID ipv6_subnet_id,
  63. const asiolink::IOAddress& ipv4_reservation,
  64. const std::string& hostname,
  65. const std::string& dhcp4_client_classes,
  66. const std::string& dhcp6_client_classes,
  67. const asiolink::IOAddress& next_server,
  68. const std::string& server_host_name,
  69. const std::string& boot_file_name)
  70. : identifier_type_(identifier_type),
  71. identifier_value_(), ipv4_subnet_id_(ipv4_subnet_id),
  72. ipv6_subnet_id_(ipv6_subnet_id),
  73. ipv4_reservation_(asiolink::IOAddress::IPV4_ZERO_ADDRESS()),
  74. hostname_(hostname), dhcp4_client_classes_(dhcp4_client_classes),
  75. dhcp6_client_classes_(dhcp6_client_classes),
  76. next_server_(asiolink::IOAddress::IPV4_ZERO_ADDRESS()),
  77. server_host_name_(server_host_name), boot_file_name_(boot_file_name),
  78. host_id_(0), cfg_option4_(new CfgOption()), cfg_option6_(new CfgOption()) {
  79. // Initialize host identifier.
  80. setIdentifier(identifier, identifier_len, identifier_type);
  81. if (!ipv4_reservation.isV4Zero()) {
  82. // Validate and set IPv4 address reservation.
  83. setIPv4Reservation(ipv4_reservation);
  84. }
  85. if (!next_server.isV4Zero()) {
  86. // Validate and set next server address.
  87. setNextServer(next_server);
  88. }
  89. }
  90. Host::Host(const std::string& identifier, const std::string& identifier_name,
  91. const SubnetID ipv4_subnet_id, const SubnetID ipv6_subnet_id,
  92. const asiolink::IOAddress& ipv4_reservation,
  93. const std::string& hostname,
  94. const std::string& dhcp4_client_classes,
  95. const std::string& dhcp6_client_classes,
  96. const asiolink::IOAddress& next_server,
  97. const std::string& server_host_name,
  98. const std::string& boot_file_name)
  99. : identifier_type_(IDENT_HWADDR),
  100. identifier_value_(), ipv4_subnet_id_(ipv4_subnet_id),
  101. ipv6_subnet_id_(ipv6_subnet_id),
  102. ipv4_reservation_(asiolink::IOAddress::IPV4_ZERO_ADDRESS()),
  103. hostname_(hostname), dhcp4_client_classes_(dhcp4_client_classes),
  104. dhcp6_client_classes_(dhcp6_client_classes),
  105. next_server_(asiolink::IOAddress::IPV4_ZERO_ADDRESS()),
  106. server_host_name_(server_host_name), boot_file_name_(boot_file_name),
  107. host_id_(0), cfg_option4_(new CfgOption()), cfg_option6_(new CfgOption()) {
  108. // Initialize host identifier.
  109. setIdentifier(identifier, identifier_name);
  110. if (!ipv4_reservation.isV4Zero()) {
  111. // Validate and set IPv4 address reservation.
  112. setIPv4Reservation(ipv4_reservation);
  113. }
  114. if (!next_server.isV4Zero()) {
  115. // Validate and set next server address.
  116. setNextServer(next_server);
  117. }
  118. }
  119. const std::vector<uint8_t>&
  120. Host::getIdentifier() const {
  121. return (identifier_value_);
  122. }
  123. Host::IdentifierType
  124. Host::getIdentifierType() const {
  125. return (identifier_type_);
  126. }
  127. Host::IdentifierType
  128. Host::getIdentifierType(const std::string& identifier_name) {
  129. if (identifier_name == "hw-address") {
  130. return (IDENT_HWADDR);
  131. } else if (identifier_name == "duid") {
  132. return (IDENT_DUID);
  133. } else if (identifier_name == "circuit-id") {
  134. return (IDENT_CIRCUIT_ID);
  135. } else if (identifier_name == "client-id") {
  136. return (IDENT_CLIENT_ID);
  137. } else {
  138. isc_throw(isc::BadValue, "invalid client identifier type '"
  139. << identifier_name << "'");
  140. }
  141. }
  142. HWAddrPtr
  143. Host::getHWAddress() const {
  144. return ((identifier_type_ == IDENT_HWADDR) ?
  145. HWAddrPtr(new HWAddr(identifier_value_, HTYPE_ETHER)) : HWAddrPtr());
  146. }
  147. DuidPtr
  148. Host::getDuid() const {
  149. return ((identifier_type_ == IDENT_DUID) ?
  150. DuidPtr(new DUID(identifier_value_)) : DuidPtr());
  151. }
  152. std::string
  153. Host::getIdentifierAsText() const {
  154. return (getIdentifierAsText(identifier_type_, &identifier_value_[0],
  155. identifier_value_.size()));
  156. }
  157. std::string
  158. Host::getIdentifierAsText(const IdentifierType& type, const uint8_t* value,
  159. const size_t length) {
  160. // Convert identifier into <type>=<value> form.
  161. std::ostringstream s;
  162. switch (type) {
  163. case IDENT_HWADDR:
  164. s << "hwaddr";
  165. break;
  166. case IDENT_DUID:
  167. s << "duid";
  168. break;
  169. case IDENT_CIRCUIT_ID:
  170. s << "circuit-id";
  171. break;
  172. case IDENT_CLIENT_ID:
  173. s << "client-id";
  174. break;
  175. default:
  176. // This should never happen actually, unless we add new identifier
  177. // and forget to add a case for it above.
  178. s << "(invalid-type)";
  179. }
  180. std::vector<uint8_t> vec(value, value + length);
  181. s << "=" << (length > 0 ? util::encode::encodeHex(vec) : "(null)");
  182. return (s.str());
  183. }
  184. std::string
  185. Host::getIdentifierName(const IdentifierType& type) {
  186. switch (type) {
  187. case Host::IDENT_HWADDR:
  188. return ("hw-address");
  189. case Host::IDENT_DUID:
  190. return ("duid");
  191. case Host::IDENT_CIRCUIT_ID:
  192. return ("circuit-id");
  193. case Host::IDENT_CLIENT_ID:
  194. return ("client-id");
  195. default:
  196. ;
  197. }
  198. return ("(unknown)");
  199. }
  200. void
  201. Host::setIdentifier(const uint8_t* identifier, const size_t len,
  202. const IdentifierType& type) {
  203. if (len < 1) {
  204. isc_throw(BadValue, "invalid client identifier length 0");
  205. }
  206. identifier_type_ = type;
  207. identifier_value_.assign(identifier, identifier + len);
  208. }
  209. void
  210. Host::setIdentifier(const std::string& identifier, const std::string& name) {
  211. // Empty identifier is not allowed.
  212. if (identifier.empty()) {
  213. isc_throw(isc::BadValue, "empty host identifier used");
  214. }
  215. // Set identifier type.
  216. identifier_type_ = getIdentifierType(name);
  217. // Idetifier value can either be specified as string of hexadecimal
  218. // digits or a string in quotes. The latter is copied to a vector excluding
  219. // quote characters.
  220. // Try to convert the values in quotes into a vector of ASCII codes.
  221. // If the identifier lacks opening and closing quote, this will return
  222. // an empty value, in which case we'll try to decode it as a string of
  223. // hexadecimal digits.
  224. try {
  225. std::vector<uint8_t> binary = util::str::quotedStringToBinary(identifier);
  226. if (binary.empty()) {
  227. util::str::decodeFormattedHexString(identifier, binary);
  228. }
  229. // Successfully decoded the identifier, so let's use it.
  230. identifier_value_.swap(binary);
  231. } catch (...) {
  232. // The string doesn't match any known pattern, so we have to
  233. // report an error at this point.
  234. isc_throw(isc::BadValue, "invalid host identifier value '"
  235. << identifier << "'");
  236. }
  237. }
  238. void
  239. Host::setIPv4Reservation(const asiolink::IOAddress& address) {
  240. if (!address.isV4()) {
  241. isc_throw(isc::BadValue, "address '" << address << "' is not a valid"
  242. " IPv4 address");
  243. } else if (address.isV4Zero() || address.isV4Bcast()) {
  244. isc_throw(isc::BadValue, "must not make reservation for the '"
  245. << address << "' address");
  246. }
  247. ipv4_reservation_ = address;
  248. }
  249. void
  250. Host::removeIPv4Reservation() {
  251. ipv4_reservation_ = asiolink::IOAddress::IPV4_ZERO_ADDRESS();
  252. }
  253. void
  254. Host::addReservation(const IPv6Resrv& reservation) {
  255. // Check if it is not duplicating existing reservation.
  256. if (hasReservation(reservation)) {
  257. isc_throw(isc::InvalidOperation, "failed on attempt to add a duplicated"
  258. " host reservation for " << reservation.toText());
  259. }
  260. // Add it.
  261. ipv6_reservations_.insert(IPv6ResrvTuple(reservation.getType(),
  262. reservation));
  263. }
  264. IPv6ResrvRange
  265. Host::getIPv6Reservations(const IPv6Resrv::Type& type) const {
  266. return (ipv6_reservations_.equal_range(type));
  267. }
  268. IPv6ResrvRange
  269. Host::getIPv6Reservations() const {
  270. return (IPv6ResrvRange(ipv6_reservations_.begin(),
  271. ipv6_reservations_.end()));
  272. }
  273. bool
  274. Host::hasIPv6Reservation() const {
  275. return (!ipv6_reservations_.empty());
  276. }
  277. bool
  278. Host::hasReservation(const IPv6Resrv& reservation) const {
  279. IPv6ResrvRange reservations = getIPv6Reservations(reservation.getType());
  280. if (std::distance(reservations.first, reservations.second) > 0) {
  281. for (IPv6ResrvIterator it = reservations.first;
  282. it != reservations.second; ++it) {
  283. if (it->second == reservation) {
  284. return (true);
  285. }
  286. }
  287. }
  288. // No matching reservations found.
  289. return (false);
  290. }
  291. void
  292. Host::addClientClass4(const std::string& class_name) {
  293. addClientClassInternal(dhcp4_client_classes_, class_name);
  294. }
  295. void
  296. Host::addClientClass6(const std::string& class_name) {
  297. addClientClassInternal(dhcp6_client_classes_, class_name);
  298. }
  299. void
  300. Host::addClientClassInternal(ClientClasses& classes,
  301. const std::string& class_name) {
  302. std::string trimmed = util::str::trim(class_name);
  303. if (!trimmed.empty()) {
  304. classes.insert(ClientClass(trimmed));
  305. }
  306. }
  307. void
  308. Host::setNextServer(const asiolink::IOAddress& next_server) {
  309. if (!next_server.isV4()) {
  310. isc_throw(isc::BadValue, "next server address '" << next_server
  311. << "' is not a valid IPv4 address");
  312. } else if (next_server.isV4Zero() || next_server.isV4Bcast()) {
  313. isc_throw(isc::BadValue, "invalid next server address '"
  314. << next_server << "'");
  315. }
  316. next_server_ = next_server;
  317. }
  318. std::string
  319. Host::toText() const {
  320. std::ostringstream s;
  321. // Add HW address or DUID.
  322. s << getIdentifierAsText();
  323. // Add IPv4 subnet id if exists (non-zero).
  324. if (ipv4_subnet_id_) {
  325. s << " ipv4_subnet_id=" << ipv4_subnet_id_;
  326. }
  327. // Add IPv6 subnet id if exists (non-zero).
  328. if (ipv6_subnet_id_) {
  329. s << " ipv6_subnet_id=" << ipv6_subnet_id_;
  330. }
  331. // Add hostname.
  332. s << " hostname=" << (hostname_.empty() ? "(empty)" : hostname_);
  333. // Add IPv4 reservation.
  334. s << " ipv4_reservation=" << (ipv4_reservation_.isV4Zero() ? "(no)" :
  335. ipv4_reservation_.toText());
  336. // Add next server.
  337. s << " siaddr=" << (next_server_.isV4Zero() ? "(no)" :
  338. next_server_.toText());
  339. // Add server host name.
  340. s << " sname=" << (server_host_name_.empty() ? "(empty)" : server_host_name_);
  341. // Add boot file name.
  342. s << " file=" << (boot_file_name_.empty() ? "(empty)" : boot_file_name_);
  343. if (ipv6_reservations_.empty()) {
  344. s << " ipv6_reservations=(none)";
  345. } else {
  346. // Add all IPv6 reservations.
  347. for (IPv6ResrvIterator resrv = ipv6_reservations_.begin();
  348. resrv != ipv6_reservations_.end(); ++resrv) {
  349. s << " ipv6_reservation"
  350. << std::distance(ipv6_reservations_.begin(), resrv)
  351. << "=" << resrv->second.toText();
  352. }
  353. }
  354. // Add DHCPv4 client classes.
  355. for (ClientClasses::const_iterator cclass = dhcp4_client_classes_.begin();
  356. cclass != dhcp4_client_classes_.end(); ++cclass) {
  357. s << " dhcp4_class"
  358. << std::distance(dhcp4_client_classes_.begin(), cclass)
  359. << "=" << *cclass;
  360. }
  361. // Add DHCPv6 client classes.
  362. for (ClientClasses::const_iterator cclass = dhcp6_client_classes_.begin();
  363. cclass != dhcp6_client_classes_.end(); ++cclass) {
  364. s << " dhcp6_class"
  365. << std::distance(dhcp6_client_classes_.begin(), cclass)
  366. << "=" << *cclass;
  367. }
  368. return (s.str());
  369. }
  370. } // end of namespace isc::dhcp
  371. } // end of namespace isc