rtnetlink.cpp (b7d6a1a70a818783151eaa5092c1e3e2682dfb25) rtnetlink.cpp (9b2a20d3cfa1131521456b9ebfde7a7bb8b234bb)
1#include "rtnetlink.hpp"
2
3#include "netlink.hpp"
4#include "util.hpp"
5
6#include <linux/rtnetlink.h>
7
8namespace phosphor::network::netlink

--- 74 unchanged lines hidden (view full) ---

83 parseLinkInfo(ret, data);
84 break;
85 }
86 }
87 return ret;
88}
89
90template <typename Addr>
1#include "rtnetlink.hpp"
2
3#include "netlink.hpp"
4#include "util.hpp"
5
6#include <linux/rtnetlink.h>
7
8namespace phosphor::network::netlink

--- 74 unchanged lines hidden (view full) ---

83 parseLinkInfo(ret, data);
84 break;
85 }
86 }
87 return ret;
88}
89
90template <typename Addr>
91static std::optional<std::tuple<unsigned, InAddrAny>>
91static std::optional<std::tuple<unsigned, stdplus::InAnyAddr>>
92 parse(std::string_view msg)
93{
94 std::optional<unsigned> ifIdx;
92 parse(std::string_view msg)
93{
94 std::optional<unsigned> ifIdx;
95 std::optional<InAddrAny> gw;
95 std::optional<stdplus::InAnyAddr> gw;
96 while (!msg.empty())
97 {
98 auto [hdr, data] = extractRtAttr(msg);
99 switch (hdr.rta_type)
100 {
101 case RTA_OIF:
102 ifIdx.emplace(stdplus::raw::copyFromStrict<int>(data));
103 break;

--- 4 unchanged lines hidden (view full) ---

108 }
109 if (ifIdx && gw)
110 {
111 return std::make_tuple(*ifIdx, *gw);
112 }
113 return std::nullopt;
114}
115
96 while (!msg.empty())
97 {
98 auto [hdr, data] = extractRtAttr(msg);
99 switch (hdr.rta_type)
100 {
101 case RTA_OIF:
102 ifIdx.emplace(stdplus::raw::copyFromStrict<int>(data));
103 break;

--- 4 unchanged lines hidden (view full) ---

108 }
109 if (ifIdx && gw)
110 {
111 return std::make_tuple(*ifIdx, *gw);
112 }
113 return std::nullopt;
114}
115
116std::optional<std::tuple<unsigned, InAddrAny>>
116std::optional<std::tuple<unsigned, stdplus::InAnyAddr>>
117 gatewayFromRtm(std::string_view msg)
118{
119 const auto& rtm = extractRtData<rtmsg>(msg);
120 if (rtm.rtm_table != RT_TABLE_MAIN || rtm.rtm_dst_len != 0)
121 {
122 return std::nullopt;
123 }
124 switch (rtm.rtm_family)
125 {
126 case AF_INET:
117 gatewayFromRtm(std::string_view msg)
118{
119 const auto& rtm = extractRtData<rtmsg>(msg);
120 if (rtm.rtm_table != RT_TABLE_MAIN || rtm.rtm_dst_len != 0)
121 {
122 return std::nullopt;
123 }
124 switch (rtm.rtm_family)
125 {
126 case AF_INET:
127 return parse<in_addr>(msg);
127 return parse<stdplus::In4Addr>(msg);
128 case AF_INET6:
128 case AF_INET6:
129 return parse<in6_addr>(msg);
129 return parse<stdplus::In6Addr>(msg);
130 }
131 return std::nullopt;
132}
133
134AddressInfo addrFromRtm(std::string_view msg)
135{
136 const auto& ifa = extractRtData<ifaddrmsg>(msg);
137
130 }
131 return std::nullopt;
132}
133
134AddressInfo addrFromRtm(std::string_view msg)
135{
136 const auto& ifa = extractRtData<ifaddrmsg>(msg);
137
138 AddressInfo ret;
139 ret.ifidx = ifa.ifa_index;
140 ret.flags = ifa.ifa_flags;
141 ret.scope = ifa.ifa_scope;
142 std::optional<InAddrAny> addr;
138 uint32_t flags = ifa.ifa_flags;
139 std::optional<stdplus::InAnyAddr> addr;
143 while (!msg.empty())
144 {
145 auto [hdr, data] = extractRtAttr(msg);
146 if (hdr.rta_type == IFA_ADDRESS)
147 {
148 addr.emplace(addrFromBuf(ifa.ifa_family, data));
149 }
150 else if (hdr.rta_type == IFA_FLAGS)
151 {
140 while (!msg.empty())
141 {
142 auto [hdr, data] = extractRtAttr(msg);
143 if (hdr.rta_type == IFA_ADDRESS)
144 {
145 addr.emplace(addrFromBuf(ifa.ifa_family, data));
146 }
147 else if (hdr.rta_type == IFA_FLAGS)
148 {
152 ret.flags = stdplus::raw::copyFromStrict<uint32_t>(data);
149 flags = stdplus::raw::copyFromStrict<uint32_t>(data);
153 }
154 }
155 if (!addr)
156 {
157 throw std::runtime_error("Missing address");
158 }
150 }
151 }
152 if (!addr)
153 {
154 throw std::runtime_error("Missing address");
155 }
159 ret.ifaddr = {*addr, ifa.ifa_prefixlen};
160 return ret;
156 return AddressInfo{.ifidx = ifa.ifa_index,
157 .ifaddr = stdplus::SubnetAny{*addr, ifa.ifa_prefixlen},
158 .scope = ifa.ifa_scope,
159 .flags = flags};
161}
162
163NeighborInfo neighFromRtm(std::string_view msg)
164{
165 const auto& ndm = netlink::extractRtData<ndmsg>(msg);
166
167 NeighborInfo ret;
168 ret.ifidx = ndm.ndm_ifindex;

--- 17 unchanged lines hidden ---
160}
161
162NeighborInfo neighFromRtm(std::string_view msg)
163{
164 const auto& ndm = netlink::extractRtData<ndmsg>(msg);
165
166 NeighborInfo ret;
167 ret.ifidx = ndm.ndm_ifindex;

--- 17 unchanged lines hidden ---