VOM: IGMP only supports IPv4
[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 ip_address_t& a, vapi_type_address& v)
22 {
23   if (a.is_v4()) {
24     v.af = ADDRESS_IP4;
25     memcpy(v.un.ip4, a.to_v4().to_bytes().data(), 4);
26   } else {
27     v.af = ADDRESS_IP6;
28     memcpy(v.un.ip6, a.to_v6().to_bytes().data(), 16);
29   }
30 }
31 void
32 to_api(const boost::asio::ip::address& a, vapi_type_ip4_address& v)
33 {
34   memcpy(v, a.to_v4().to_bytes().data(), 4);
35 }
36
37 ip_address_t
38 from_api(const vapi_type_address& v)
39 {
40   boost::asio::ip::address addr;
41
42   if (ADDRESS_IP6 == v.af) {
43     std::array<uint8_t, 16> a;
44     std::copy(v.un.ip6, v.un.ip6 + 16, std::begin(a));
45     boost::asio::ip::address_v6 v6(a);
46     addr = v6;
47   } else {
48     std::array<uint8_t, 4> a;
49     std::copy(v.un.ip6, v.un.ip6 + 4, std::begin(a));
50     boost::asio::ip::address_v4 v4(a);
51     addr = v4;
52   }
53
54   return addr;
55 }
56
57 vapi_type_mac_address
58 to_api(const mac_address_t& a)
59 {
60   vapi_type_mac_address v;
61
62   std::copy(std::begin(a.bytes), std::end(a.bytes), v.bytes);
63
64   return (v);
65 }
66
67 mac_address_t
68 from_api(const vapi_type_mac_address& v)
69 {
70   return mac_address_t(v.bytes);
71 }
72
73 route::prefix_t
74 from_api(const vapi_type_prefix& v)
75 {
76   return route::prefix_t(from_api(v.address), v.address_length);
77 }
78
79 vapi_type_prefix
80 to_api(const route::prefix_t& p)
81 {
82   vapi_type_prefix v;
83   to_api(p.address(), v.address);
84   v.address_length = p.mask_width();
85   return v;
86 }
87 };
88
89 /*
90  * fd.io coding-style-patch-verification: ON
91  *
92  * Local Variables:
93  * eval: (c-set-style "mozilla")
94  * End:
95  */