Use IP and MAC API types for neighbors
[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 void
21 to_api(const boost::asio::ip::address_v4& a, vapi_type_ip4_address& v)
22 {
23   std::copy_n(std::begin(a.to_bytes()), a.to_bytes().size(), v);
24 }
25 void
26 to_api(const boost::asio::ip::address_v6& a, vapi_type_ip6_address& v)
27 {
28   std::copy_n(std::begin(a.to_bytes()), a.to_bytes().size(), v);
29 }
30
31 void
32 to_api(const ip_address_t& a, vapi_type_address& v)
33 {
34   if (a.is_v4()) {
35     v.af = ADDRESS_IP4;
36     memcpy(v.un.ip4, a.to_v4().to_bytes().data(), 4);
37   } else {
38     v.af = ADDRESS_IP6;
39     memcpy(v.un.ip6, a.to_v6().to_bytes().data(), 16);
40   }
41 }
42
43 void
44 to_api(const ip_address_t& a,
45        vapi_union_address_union& u,
46        vapi_enum_address_family& af)
47 {
48   if (a.is_v4()) {
49     af = ADDRESS_IP4;
50     memcpy(u.ip4, a.to_v4().to_bytes().data(), 4);
51   } else {
52     af = ADDRESS_IP6;
53     memcpy(u.ip6, a.to_v6().to_bytes().data(), 16);
54   }
55 }
56
57 boost::asio::ip::address_v6
58 from_api(const vapi_type_ip6_address& v)
59 {
60   std::array<uint8_t, 16> a;
61   std::copy(v, v + 16, std::begin(a));
62   boost::asio::ip::address_v6 v6(a);
63
64   return v6;
65 }
66
67 boost::asio::ip::address_v4
68 from_api(const vapi_type_ip4_address& v)
69 {
70   std::array<uint8_t, 4> a;
71   std::copy(v, v + 4, std::begin(a));
72   boost::asio::ip::address_v4 v4(a);
73
74   return v4;
75 }
76
77 ip_address_t
78 from_api(const vapi_type_address& v)
79 {
80   boost::asio::ip::address addr;
81
82   if (ADDRESS_IP6 == v.af) {
83     std::array<uint8_t, 16> a;
84     std::copy(v.un.ip6, v.un.ip6 + 16, std::begin(a));
85     boost::asio::ip::address_v6 v6(a);
86     addr = v6;
87   } else {
88     std::array<uint8_t, 4> a;
89     std::copy(v.un.ip6, v.un.ip6 + 4, std::begin(a));
90     boost::asio::ip::address_v4 v4(a);
91     addr = v4;
92   }
93
94   return addr;
95 }
96
97 ip_address_t
98 from_api(const vapi_union_address_union& u, vapi_enum_address_family af)
99 {
100   boost::asio::ip::address addr;
101
102   if (ADDRESS_IP6 == af) {
103     std::array<uint8_t, 16> a;
104     std::copy(u.ip6, u.ip6 + 16, std::begin(a));
105     boost::asio::ip::address_v6 v6(a);
106     addr = v6;
107   } else {
108     std::array<uint8_t, 4> a;
109     std::copy(u.ip6, u.ip6 + 4, std::begin(a));
110     boost::asio::ip::address_v4 v4(a);
111     addr = v4;
112   }
113
114   return addr;
115 }
116
117 void
118 to_api(const mac_address_t& a, vapi_type_mac_address& v)
119 {
120   std::copy(std::begin(a.bytes), std::end(a.bytes), v);
121 }
122
123 mac_address_t
124 from_api(const vapi_type_mac_address& v)
125 {
126   return mac_address_t(v);
127 }
128
129 route::prefix_t
130 from_api(const vapi_type_prefix& v)
131 {
132   return route::prefix_t(from_api(v.address), v.address_length);
133 }
134
135 vapi_type_prefix
136 to_api(const route::prefix_t& p)
137 {
138   vapi_type_prefix v;
139   to_api(p.address(), v.address);
140   v.address_length = p.mask_width();
141   return v;
142 }
143
144 route::mprefix_t
145 from_api(const vapi_type_mprefix& v)
146 {
147   return route::mprefix_t(from_api(v.src_address, v.af),
148                           from_api(v.grp_address, v.af), v.grp_address_length);
149 }
150
151 vapi_type_mprefix
152 to_api(const route::mprefix_t& p)
153 {
154   vapi_enum_address_family af;
155   vapi_type_mprefix v;
156   to_api(p.grp_address(), v.grp_address, af);
157   to_api(p.src_address(), v.src_address, af);
158   v.grp_address_length = p.mask_width();
159   v.af = af;
160   return v;
161 }
162 };
163
164 /*
165  * fd.io coding-style-patch-verification: ON
166  *
167  * Local Variables:
168  * eval: (c-set-style "mozilla")
169  * End:
170  */