inet_address.h 897 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #ifndef _IPYML_INET_ADDRESS_H
  2. #define _IPYML_INET_ADDRESS_H
  3. #include "yaml.h"
  4. #include <arpa/inet.h>
  5. #include <cassert>
  6. #include <cstring>
  7. #include <libmnl/libmnl.h>
  8. #include <linux/netlink.h>
  9. #include <netinet/in.h>
  10. #include <ostream>
  11. class InetAddress : public YamlObject {
  12. unsigned char bytes[sizeof(in_addr_t)];
  13. public:
  14. InetAddress &operator=(const nlattr *attr) {
  15. assert(mnl_attr_validate(attr, MNL_TYPE_BINARY) == 0);
  16. assert(mnl_attr_get_payload_len(attr) == sizeof(bytes));
  17. memcpy(this->bytes, mnl_attr_get_payload(attr), sizeof(bytes));
  18. return *this;
  19. }
  20. void format(char *dst, socklen_t size) const {
  21. inet_ntop(AF_INET, bytes, dst, size);
  22. }
  23. void write_yaml(std::ostream &stream,
  24. const yaml_indent_level_t indent_level = 0) const {
  25. char str[INET_ADDRSTRLEN];
  26. format(str, sizeof(str));
  27. stream << str << '\n';
  28. }
  29. };
  30. #endif