hardware_address.h 973 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #ifndef _IPYML_HARDWARE_ADDRESS_H
  2. #define _IPYML_HARDWARE_ADDRESS_H
  3. #include "yaml.h"
  4. #include <cassert>
  5. #include <cstring>
  6. #include <iomanip>
  7. #include <libmnl/libmnl.h>
  8. #include <linux/if.h>
  9. #include <linux/netlink.h>
  10. #include <ostream>
  11. #include <sstream>
  12. #include <string>
  13. class HardwareAddress : public YamlObject {
  14. uint8_t bytes[IFHWADDRLEN];
  15. public:
  16. HardwareAddress &operator=(const nlattr *attr) {
  17. assert(mnl_attr_get_payload_len(attr) == IFHWADDRLEN);
  18. memcpy(this->bytes, mnl_attr_get_payload(attr), IFHWADDRLEN);
  19. return *this;
  20. }
  21. std::string format() const {
  22. std::stringstream ss;
  23. ss << std::hex << std::setfill('0');
  24. for (int i = 0; i < IFHWADDRLEN; i++) {
  25. if (i != 0) {
  26. ss << ':';
  27. }
  28. ss << std::setw(2) << (int)bytes[i];
  29. }
  30. return ss.str();
  31. }
  32. void write_yaml(std::ostream &stream,
  33. const yaml_indent_level_t indent_level = 0) const {
  34. stream << format();
  35. }
  36. };
  37. #endif