2 * Copyright (c) 2018 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <vom/api_types.hpp>
20 vapi_enum_ip_neighbor_flags
21 to_api(const neighbour::flags_t& f)
23 vapi_enum_ip_neighbor_flags out = IP_API_NEIGHBOR_FLAG_NONE;
25 if (f & neighbour::flags_t::STATIC)
26 out = static_cast<vapi_enum_ip_neighbor_flags>(out |
27 IP_API_NEIGHBOR_FLAG_STATIC);
28 if (f & neighbour::flags_t::NO_FIB_ENTRY)
29 out = static_cast<vapi_enum_ip_neighbor_flags>(
30 out | IP_API_NEIGHBOR_FLAG_NO_FIB_ENTRY);
35 const neighbour::flags_t
36 from_api(vapi_enum_ip_neighbor_flags f)
38 neighbour::flags_t out = neighbour::flags_t::NONE;
40 if (f & IP_API_NEIGHBOR_FLAG_STATIC)
41 out |= neighbour::flags_t::STATIC;
42 if (f & IP_API_NEIGHBOR_FLAG_NO_FIB_ENTRY)
43 out |= neighbour::flags_t::NO_FIB_ENTRY;
48 invalid_decode::invalid_decode(const std::string reason)
53 to_api(const boost::asio::ip::address_v4& a, vapi_type_ip4_address& v)
55 std::copy_n(std::begin(a.to_bytes()), a.to_bytes().size(), v);
58 to_api(const boost::asio::ip::address_v6& a, vapi_type_ip6_address& v)
60 std::copy_n(std::begin(a.to_bytes()), a.to_bytes().size(), v);
64 to_api(const ip_address_t& a, vapi_type_address& v)
68 memcpy(v.un.ip4, a.to_v4().to_bytes().data(), 4);
71 memcpy(v.un.ip6, a.to_v6().to_bytes().data(), 16);
76 to_api(const ip_address_t& a,
77 vapi_union_address_union& u,
78 vapi_enum_address_family& af)
82 memcpy(u.ip4, a.to_v4().to_bytes().data(), 4);
85 memcpy(u.ip6, a.to_v6().to_bytes().data(), 16);
90 to_api(const ip_address_t& a, vapi_union_address_union& u)
93 memcpy(u.ip4, a.to_v4().to_bytes().data(), 4);
95 memcpy(u.ip6, a.to_v6().to_bytes().data(), 16);
99 boost::asio::ip::address_v6
100 from_api(const vapi_type_ip6_address& v)
102 std::array<uint8_t, 16> a;
103 std::copy(v, v + 16, std::begin(a));
104 boost::asio::ip::address_v6 v6(a);
109 boost::asio::ip::address_v4
110 from_api(const vapi_type_ip4_address& v)
112 std::array<uint8_t, 4> a;
113 std::copy(v, v + 4, std::begin(a));
114 boost::asio::ip::address_v4 v4(a);
120 from_api(const vapi_type_address& v)
122 boost::asio::ip::address addr;
124 if (ADDRESS_IP6 == v.af) {
125 std::array<uint8_t, 16> a;
126 std::copy(v.un.ip6, v.un.ip6 + 16, std::begin(a));
127 boost::asio::ip::address_v6 v6(a);
130 std::array<uint8_t, 4> a;
131 std::copy(v.un.ip6, v.un.ip6 + 4, std::begin(a));
132 boost::asio::ip::address_v4 v4(a);
140 from_api(const vapi_union_address_union& u, vapi_enum_fib_path_nh_proto proto)
142 boost::asio::ip::address addr;
144 if (FIB_API_PATH_NH_PROTO_IP6 == proto) {
145 std::array<uint8_t, 16> a;
146 std::copy(u.ip6, u.ip6 + 16, std::begin(a));
147 boost::asio::ip::address_v6 v6(a);
149 } else if (FIB_API_PATH_NH_PROTO_IP4 == proto) {
150 std::array<uint8_t, 4> a;
151 std::copy(u.ip6, u.ip6 + 4, std::begin(a));
152 boost::asio::ip::address_v4 v4(a);
160 from_api(const vapi_union_address_union& u, vapi_enum_address_family af)
162 boost::asio::ip::address addr;
164 if (ADDRESS_IP6 == af) {
165 std::array<uint8_t, 16> a;
166 std::copy(u.ip6, u.ip6 + 16, std::begin(a));
167 boost::asio::ip::address_v6 v6(a);
170 std::array<uint8_t, 4> a;
171 std::copy(u.ip6, u.ip6 + 4, std::begin(a));
172 boost::asio::ip::address_v4 v4(a);
180 to_api(const mac_address_t& a, vapi_type_mac_address& v)
182 std::copy(std::begin(a.bytes), std::end(a.bytes), v);
186 from_api(const vapi_type_mac_address& v)
188 return mac_address_t(v);
192 from_api(const vapi_type_prefix& v)
194 return route::prefix_t(from_api(v.address), v.len);
198 to_api(const route::prefix_t& p)
201 to_api(p.address(), v.address);
202 v.len = p.mask_width();
207 from_api(const vapi_type_mprefix& v)
209 return route::mprefix_t(from_api(v.src_address, v.af),
210 from_api(v.grp_address, v.af),
211 v.grp_address_length);
215 to_api(const route::mprefix_t& p)
217 vapi_enum_address_family af;
219 to_api(p.grp_address(), v.grp_address, af);
220 to_api(p.src_address(), v.src_address, af);
221 v.grp_address_length = p.mask_width();
226 vapi_enum_fib_path_nh_proto
227 to_api(const nh_proto_t& p)
229 if (p == nh_proto_t::IPV4) {
230 return FIB_API_PATH_NH_PROTO_IP4;
231 } else if (p == nh_proto_t::IPV6) {
232 return FIB_API_PATH_NH_PROTO_IP6;
233 } else if (p == nh_proto_t::ETHERNET) {
234 return FIB_API_PATH_NH_PROTO_ETHERNET;
235 } else if (p == nh_proto_t::MPLS) {
236 return FIB_API_PATH_NH_PROTO_MPLS;
239 return FIB_API_PATH_NH_PROTO_IP4;
242 vapi_enum_address_family
243 to_api(const l3_proto_t p)
245 if (p == l3_proto_t::IPV6) {
252 from_api(vapi_enum_fib_path_nh_proto p)
255 case FIB_API_PATH_NH_PROTO_IP4:
256 return nh_proto_t::IPV4;
257 case FIB_API_PATH_NH_PROTO_IP6:
258 return nh_proto_t::IPV6;
259 case FIB_API_PATH_NH_PROTO_ETHERNET:
260 return nh_proto_t::ETHERNET;
261 case FIB_API_PATH_NH_PROTO_MPLS:
262 return nh_proto_t::MPLS;
263 case FIB_API_PATH_NH_PROTO_BIER:
267 return nh_proto_t::IPV4;
273 * fd.io coding-style-patch-verification: ON
276 * eval: (c-set-style "mozilla")