24 #ifndef NDN_CXX_NET_NETLINK_MESSAGE_HPP 25 #define NDN_CXX_NET_NETLINK_MESSAGE_HPP 31 #ifndef NDN_CXX_HAVE_NETLINK 32 #error "This file should not be included ..." 35 #include <linux/netlink.h> 36 #include <linux/rtnetlink.h> 42 #include <boost/asio/ip/address.hpp> 73 return NLA_ALIGN(attr->nla_len);
80 return RTA_ALIGN(attr->rta_len);
91 return attr->nla_type & NLA_TYPE_MASK;
98 return attr->rta_type;
106 inline const uint8_t*
109 return reinterpret_cast<const uint8_t*
>(attr) + NLA_HDRLEN;
113 inline const uint8_t*
116 return reinterpret_cast<const uint8_t*
>(RTA_DATA(const_cast<rtattr*>(attr)));
127 return attr->nla_len - NLA_HDRLEN;
134 return RTA_PAYLOAD(attr);
145 : m_msg(reinterpret_cast<const nlmsghdr*>(buf))
148 BOOST_ASSERT(buf !=
nullptr);
166 return NLMSG_OK(m_msg, m_length);
175 auto thisLen = NLMSG_ALIGN(m_msg->nlmsg_len);
176 return NetlinkMessage{
reinterpret_cast<const uint8_t*
>(m_msg) + thisLen, m_length - thisLen};
185 if (m_msg->nlmsg_len < NLMSG_LENGTH(
sizeof(T)))
188 return reinterpret_cast<const T*
>(NLMSG_DATA(const_cast<nlmsghdr*>(m_msg)));
191 template<
typename AttributeT,
typename PayloadT>
197 auto begin =
reinterpret_cast<const uint8_t*
>(p) + NLMSG_ALIGN(
sizeof(PayloadT));
198 auto length = NLMSG_PAYLOAD(m_msg,
sizeof(PayloadT));
203 const nlmsghdr* m_msg;
212 struct AttrValueTypeTag {};
218 for (; isAttrValid(begin, length); begin = getNextAttr(begin, length)) {
226 return m_attrs.size();
233 auto it = m_attrs.find(attrType);
234 if (it == m_attrs.end())
238 getAttributeValueLength(it->second),
239 AttrValueTypeTag<U>{});
244 isAttrValid(
const T* attr,
size_t nBytesRemaining) noexcept
246 return attr !=
nullptr &&
247 nBytesRemaining >=
sizeof(T) &&
253 getNextAttr(
const T* attr,
size_t& nBytesRemaining) noexcept
256 if (len > nBytesRemaining)
259 nBytesRemaining -= len;
260 return reinterpret_cast<const T*
>(
reinterpret_cast<const uint8_t*
>(attr) + len);
263 template<
typename Integral>
265 convertAttrValue(
const uint8_t* val,
size_t len, AttrValueTypeTag<Integral>)
267 if (len <
sizeof(Integral))
271 std::memcpy(&i, val,
sizeof(Integral));
275 static optional<std::string>
276 convertAttrValue(
const uint8_t* val,
size_t len, AttrValueTypeTag<std::string>)
278 auto str =
reinterpret_cast<const char*
>(val);
279 if (::strnlen(str, len) >= len)
282 return std::string(str);
285 static optional<ethernet::Address>
286 convertAttrValue(
const uint8_t* val,
size_t len, AttrValueTypeTag<ethernet::Address>)
294 template<
typename IpAddress>
298 convertAttrValue(
const uint8_t* val,
size_t len, AttrValueTypeTag<IpAddress>)
300 typename IpAddress::bytes_type bytes;
301 if (len < bytes.size())
304 std::copy_n(val, bytes.size(), bytes.begin());
305 return IpAddress(bytes);
309 std::map<uint16_t, const T*> m_attrs;
315 #endif // NDN_CXX_NET_NETLINK_MESSAGE_HPP const T * getPayload() const noexcept
Copyright (c) 2011-2015 Regents of the University of California.
constexpr size_t getAttributeValueLength(const T *attr)
NetlinkMessageAttributes(const T *begin, size_t length) noexcept
const size_t ADDR_LEN
Octets in one Ethernet address.
constexpr uint16_t getAttributeType(const T *attr)
NetlinkMessage getNext() const noexcept
size_t size() const noexcept
Common includes and macros used throughout the library.
const nlmsghdr & operator*() const noexcept
optional< U > getAttributeByType(uint16_t attrType) const
constexpr size_t getAttributeLength(const T *attr)
bool isValid() const noexcept
represents an Ethernet hardware address
constexpr size_t getAttributeLengthAligned(const T *attr)
NetlinkMessage(const uint8_t *buf, size_t buflen) noexcept
const uint8_t * getAttributeValue(const T *attr)
const nullopt_t nullopt((nullopt_t::init()))
const nlmsghdr * operator->() const noexcept
NetlinkMessageAttributes< AttributeT > getAttributes(const PayloadT *p) const noexcept