1
0

inet6_address.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #ifndef _IPYML_INET6_ADDRESS_H
  2. #define _IPYML_INET6_ADDRESS_H
  3. #include "yaml.h"
  4. #include <arpa/inet.h> // inet_ntop, socklen_t
  5. #include <bits/socket.h> // AF_*, socklen_t
  6. #include <cassert> // assert
  7. #include <cstring> // memcpy
  8. #include <libmnl/libmnl.h> // mnl_attr_*
  9. #include <linux/netlink.h> // struct nlattr
  10. #include <netinet/in.h> // INET6_ADDRSTRLEN, struct in6_addr
  11. #include <ostream> // std::ostream
  12. /*
  13. class may not have any virtual member due to:
  14. > error: union member ‘Address::<anonymous union>::inet6_addr’
  15. > with non-trivial ‘constexpr Inet6Address::Inet6Address(Inet6Address&&)’
  16. */
  17. class Inet6Address { // : public YamlObject {
  18. unsigned char bytes[sizeof(in6_addr)];
  19. public:
  20. Inet6Address &operator=(const nlattr *attr) {
  21. assert(mnl_attr_validate(attr, MNL_TYPE_BINARY) == 0);
  22. assert(mnl_attr_get_payload_len(attr) == sizeof(bytes));
  23. memcpy(this->bytes, mnl_attr_get_payload(attr), sizeof(bytes));
  24. return *this;
  25. }
  26. void format(char *dst, socklen_t size) const {
  27. inet_ntop(AF_INET6, bytes, dst, size);
  28. }
  29. std::string format() const {
  30. char str[INET6_ADDRSTRLEN];
  31. format(str, sizeof(str));
  32. return std::string(str);
  33. }
  34. void write_yaml(std::ostream &stream,
  35. const yaml_indent_level_t indent_level = 0) const {
  36. stream << format() << '\n';
  37. }
  38. };
  39. #endif