| 1 | // Copyright (C) Microsoft Corporation. All rights reserved. |
| 2 | #include "NetlinkResponse.h" |
| 3 | #include "NetlinkError.h" |
| 4 | #include "Utils.h" |
| 5 | |
| 6 | NetlinkResponse::NetlinkResponse(std::vector<char>&& content) : m_data(std::move(content)) |
| 7 | { |
| 8 | } |
| 9 | |
| 10 | void NetlinkResponse::ThrowIfErrorFound() const |
| 11 | { |
| 12 | auto results = Messages<nlmsgerr>(NLMSG_ERROR); |
| 13 | |
| 14 | if (results.empty()) |
| 15 | { |
| 16 | return; |
| 17 | } |
| 18 | |
| 19 | for (const auto& e : results) |
| 20 | { |
| 21 | int result = e.Payload()->error; |
| 22 | |
| 23 | if (result != 0) |
| 24 | { |
| 25 | throw NetlinkError(result); |
| 26 | } |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | __u32 NetlinkResponse::Sequence() const |
| 31 | { |
| 32 | return reinterpret_cast<const nlmsghdr*>(m_data.data())->nlmsg_seq; |
| 33 | } |
| 34 | |
| 35 | bool NetlinkResponse::MultiMessage() const |
| 36 | { |
| 37 | size_t size = m_data.size(); |
| 38 | for (const auto* header = reinterpret_cast<const nlmsghdr*>(m_data.data()); NLMSG_OK(header, size); header = NLMSG_NEXT(header, size)) |
| 39 | { |
| 40 | if (header->nlmsg_flags & NLM_F_MULTI) |
| 41 | { |
| 42 | return true; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | NetlinkResponse::Titerator NetlinkResponse::Begin() const |
| 50 | { |
| 51 | return m_data.begin(); |
| 52 | } |
| 53 | |
| 54 | NetlinkResponse::Titerator NetlinkResponse::End() const |
| 55 | { |
| 56 | return m_data.end(); |
| 57 | } |
| 58 | |
| 59 | bool NetlinkResponse::Done() const |
| 60 | { |
| 61 | auto doneMessages = Messages<nlmsghdr>(NLMSG_DONE); |
| 62 | |
| 63 | return !doneMessages.empty(); |
| 64 | } |
| 65 | |
| 66 | std::ostream& operator<<(std::ostream& out, const NetlinkResponse& response) |
| 67 | { |
| 68 | const auto begin = &*response.Begin(); |
| 69 | const auto size = response.End() - response.Begin(); |
| 70 | |
| 71 | return utils::FormatBinary(out, begin, size); |
| 72 | } |