| 1 | // Copyright (C) Microsoft Corporation. All rights reserved. |
| 2 | #pragma once |
| 3 | |
| 4 | #include "NetlinkResponse.h" |
| 5 | #include "NetlinkParseException.h" |
| 6 | |
| 7 | template <typename T> |
| 8 | std::vector<NetlinkMessage<T>> NetlinkResponse::Messages(int type) const |
| 9 | { |
| 10 | std::vector<NetlinkMessage<T>> messages; |
| 11 | |
| 12 | size_t size = m_data.size(); |
| 13 | for (const auto* header = reinterpret_cast<const nlmsghdr*>(m_data.data()); NLMSG_OK(header, size); header = NLMSG_NEXT(header, size)) |
| 14 | { |
| 15 | if (header->nlmsg_type == type) |
| 16 | { |
| 17 | auto begin = m_data.begin() + (reinterpret_cast<const char*>(header) - m_data.data()); |
| 18 | auto end = begin + header->nlmsg_len; |
| 19 | messages.emplace_back(*this, m_data.begin(), begin, end); |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | if (size != 0) |
| 24 | { |
| 25 | throw NetlinkParseException(*this, std::format("Netlink message is truncated. Missing bytes: {}", size)); |
| 26 | } |
| 27 | |
| 28 | return messages; |
| 29 | } |