ea75d7fd8ee40f1dd2e04013e8bcd987dd414fcc
[vpp.git] / extras / vom / vom / api_types.cpp
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include <vom/api_types.hpp>
17
18 namespace VOM {
19
20 vapi_enum_ip_neighbor_flags
21 to_api(const neighbour::flags_t& f)
22 {
23   vapi_enum_ip_neighbor_flags out = IP_API_NEIGHBOR_FLAG_NONE;
24
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);
31
32   return (out);
33 }
34
35 const neighbour::flags_t
36 from_api(vapi_enum_ip_neighbor_flags f)
37 {
38   neighbour::flags_t out = neighbour::flags_t::NONE;
39
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;
44
45   return out;
46 }
47
48 void
49 to_api(const boost::asio::ip::address_v4& a, vapi_type_ip4_address& v)
50 {
51   std::copy_n(std::begin(a.to_bytes()), a.to_bytes().size(), v);
52 }
53 void
54 to_api(const boost::asio::ip::address_v6& a, vapi_type_ip6_address& v)
55 {
56   std::copy_n(std::begin(a.to_bytes()), a.to_bytes().size(), v);
57 }
58
59 void
60 to_api(const ip_address_t& a, vapi_type_address& v)
61 {
62   if (a.is_v4()) {
63     v.af = ADDRESS_IP4;
64     memcpy(v.un.ip4, a.to_v4().to_bytes().data(), 4);
65   } else {
66     v.af = ADDRESS_IP6;
67     memcpy(v.un.ip6, a.to_v6().to_bytes().data(), 16);
68   }
69 }
70
71 void
72 to_api(const ip_address_t& a,
73        vapi_union_address_union& u,
74        vapi_enum_address_family& af)
75 {
76   if (a.is_v4()) {
77     af = ADDRESS_IP4;
78     memcpy(u.ip4, a.to_v4().to_bytes().data(), 4);
79   } else {
80     af = ADDRESS_IP6;
81     memcpy(u.ip6, a.to_v6().to_bytes().data(), 16);
82   }
83 }
84
85 boost::asio::ip::address_v6
86 from_api(const vapi_type_ip6_address& v)
87 {
88   std::array<uint8_t, 16> a;
89   std::copy(v, v + 16, std::begin(a));
90   boost::asio::ip::address_v6 v6(a);
91
92   return v6;
93 }
94
95 boost::asio::ip::address_v4
96 from_api(const vapi_type_ip4_address& v)
97 {
98   std::array<uint8_t, 4> a;
99   std::copy(v, v + 4, std::begin(a));
100   boost::asio::ip::address_v4 v4(a);
101
102   return v4;
103 }
104
105 ip_address_t
106 from_api(const vapi_type_address& v)
107 {
108   boost::asio::ip::address addr;
109
110   if (ADDRESS_IP6 == v.af) {
111     std::array<uint8_t, 16> a;
112     std::copy(v.un.ip6, v.un.ip6 + 16, std::begin(a));
113     boost::asio::ip::address_v6 v6(a);
114     addr = v6;
115   } else {
116     std::array<uint8_t, 4> a;
117     std::copy(v.un.ip6, v.un.ip6 + 4, std::begin(a));
118     boost::asio::ip::address_v4 v4(a);
119     addr = v4;
120   }
121
122   return addr;
123 }
124
125 ip_address_t
126 from_api(const vapi_union_address_union& u, vapi_enum_address_family af)
127 {
128   boost::asio::ip::address addr;
129
130   if (ADDRESS_IP6 == af) {
131     std::array<uint8_t, 16> a;
132     std::copy(u.ip6, u.ip6 + 16, std::begin(a));
133     boost::asio::ip::address_v6 v6(a);
134     addr = v6;
135   } else {
136     std::array<uint8_t, 4> a;
137     std::copy(u.ip6, u.ip6 + 4, std::begin(a));
138     boost::asio::ip::address_v4 v4(a);
139     addr = v4;
140   }
141
142   return addr;
143 }
144
145 void
146 to_api(const mac_address_t& a, vapi_type_mac_address& v)
147 {
148   std::copy(std::begin(a.bytes), std::end(a.bytes), v);
149 }
150
151 mac_address_t
152 from_api(const vapi_type_mac_address& v)
153 {
154   return mac_address_t(v);
155 }
156
157 route::prefix_t
158 from_api(const vapi_type_prefix& v)
159 {
160   return route::prefix_t(from_api(v.address), v.address_length);
161 }
162
163 vapi_type_prefix
164 to_api(const route::prefix_t& p)
165 {
166   vapi_type_prefix v;
167   to_api(p.address(), v.address);
168   v.address_length = p.mask_width();
169   return v;
170 }
171
172 route::mprefix_t
173 from_api(const vapi_type_mprefix& v)
174 {
175   return route::mprefix_t(from_api(v.src_address, v.af),
176                           from_api(v.grp_address, v.af), v.grp_address_length);
177 }
178
179 vapi_type_mprefix
180 to_api(const route::mprefix_t& p)
181 {
182   vapi_enum_address_family af;
183   vapi_type_mprefix v;
184   to_api(p.grp_address(), v.grp_address, af);
185   to_api(p.src_address(), v.src_address, af);
186   v.grp_address_length = p.mask_width();
187   v.af = af;
188   return v;
189 }
190 };
191
192 /*
193  * fd.io coding-style-patch-verification: ON
194  *
195  * Local Variables:
196  * eval: (c-set-style "mozilla")
197  * End:
198  */