hwaddr.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright (C) 2013-2014 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. #ifndef HWADDR_H
  15. #define HWADDR_H
  16. #include <vector>
  17. #include <stdint.h>
  18. #include <stddef.h>
  19. #include <dhcp/dhcp4.h>
  20. #include <boost/shared_ptr.hpp>
  21. namespace isc {
  22. namespace dhcp {
  23. /// @brief Hardware type that represents information from DHCPv4 packet
  24. struct HWAddr {
  25. public:
  26. /// @brief Size of an ethernet hardware address.
  27. static const size_t ETHERNET_HWADDR_LEN = 6;
  28. /// @brief Maximum size of a hardware address.
  29. static const size_t MAX_HWADDR_LEN = 20;
  30. /// @brief default constructor
  31. HWAddr();
  32. /// @brief constructor, based on C-style pointer and length
  33. /// @param hwaddr pointer to hardware address
  34. /// @param len length of the address pointed by hwaddr
  35. /// @param htype hardware type
  36. HWAddr(const uint8_t* hwaddr, size_t len, uint8_t htype);
  37. /// @brief constructor, based on C++ vector<uint8_t>
  38. /// @param hwaddr const reference to hardware address
  39. /// @param htype hardware type
  40. HWAddr(const std::vector<uint8_t>& hwaddr, uint8_t htype);
  41. // Vector that keeps the actual hardware address
  42. std::vector<uint8_t> hwaddr_;
  43. // Hardware type
  44. uint8_t htype_;
  45. /// @brief Returns textual representation of a hardware address
  46. /// (e.g. 00:01:02:03:04:05)
  47. ///
  48. /// @param include_htype Boolean value which controls whether the hardware
  49. /// type is included in the returned string (true), or not (false).
  50. ///
  51. /// @return Hardware address in the textual format.
  52. std::string toText(bool include_htype = true) const;
  53. /// @brief Creates instance of the hardware address from textual format.
  54. ///
  55. /// This function parses HW address specified as text and creates the
  56. /// corresponding @c HWAddr instance. The hexadecimal digits representing
  57. /// individual bytes of the hardware address should be separated with
  58. /// colons. Typically, two digits per byte are used. However, this function
  59. /// allows for 1 digit per HW address byte. In this case, the digit is
  60. /// prepended with '0' during conversion to binary value.
  61. ///
  62. /// This function can be used to perform a reverse operation to the
  63. /// @c HWAddr::toText(false).
  64. ///
  65. /// The instance created by this function sets HTYPE_ETHER as a hardware
  66. /// type.
  67. ///
  68. /// @param text HW address in the textual format.
  69. ///
  70. /// @return Instance of the HW address created from text.
  71. static HWAddr fromText(const std::string& text);
  72. /// @brief Compares two hardware addresses for equality
  73. bool operator==(const HWAddr& other) const;
  74. /// @brief Compares two hardware addresses for inequality
  75. bool operator!=(const HWAddr& other) const;
  76. };
  77. /// @brief Shared pointer to a hardware address structure
  78. typedef boost::shared_ptr<HWAddr> HWAddrPtr;
  79. }; // end of isc::dhcp namespace
  80. }; // end of isc namespace
  81. #endif // HWADDR_H