/src/libtorrent/include/libtorrent/netlink.hpp
Line | Count | Source |
1 | | /* |
2 | | |
3 | | Copyright (c) 2016-2017, Alden Torres |
4 | | Copyright (c) 2016, Steven Siloti |
5 | | Copyright (c) 2017, 2019, Arvid Norberg |
6 | | Copyright (c) 2018, Eugene Shalygin |
7 | | All rights reserved. |
8 | | |
9 | | Redistribution and use in source and binary forms, with or without |
10 | | modification, are permitted provided that the following conditions |
11 | | are met: |
12 | | |
13 | | * Redistributions of source code must retain the above copyright |
14 | | notice, this list of conditions and the following disclaimer. |
15 | | * Redistributions in binary form must reproduce the above copyright |
16 | | notice, this list of conditions and the following disclaimer in |
17 | | the documentation and/or other materials provided with the distribution. |
18 | | * Neither the name of the author nor the names of its |
19 | | contributors may be used to endorse or promote products derived |
20 | | from this software without specific prior written permission. |
21 | | |
22 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
23 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
24 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
25 | | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
26 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
27 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
28 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
29 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
30 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
31 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
32 | | POSSIBILITY OF SUCH DAMAGE. |
33 | | |
34 | | */ |
35 | | |
36 | | #ifndef TORRENT_NETLINK_HPP |
37 | | #define TORRENT_NETLINK_HPP |
38 | | |
39 | | #include "libtorrent/config.hpp" |
40 | | |
41 | | #if TORRENT_USE_NETLINK |
42 | | |
43 | | #include <cstring> |
44 | | #include <cstdint> |
45 | | |
46 | | #include "libtorrent/aux_/disable_warnings_push.hpp" |
47 | | #include <linux/netlink.h> |
48 | | #include <linux/rtnetlink.h> |
49 | | #include <boost/asio/basic_raw_socket.hpp> |
50 | | #include "libtorrent/aux_/disable_warnings_pop.hpp" |
51 | | |
52 | | namespace libtorrent { |
53 | | |
54 | | template <typename Protocol> |
55 | | class basic_nl_endpoint |
56 | | { |
57 | | public: |
58 | | using protocol_type = Protocol; |
59 | | using data_type = boost::asio::detail::socket_addr_type; |
60 | | |
61 | 0 | basic_nl_endpoint() noexcept : basic_nl_endpoint(protocol_type(), 0, 0) {} |
62 | | |
63 | | basic_nl_endpoint(protocol_type netlink_family, std::uint32_t group, std::uint32_t pid = 0) |
64 | 0 | : m_proto(netlink_family) |
65 | 0 | { |
66 | 0 | std::memset(&m_sockaddr, 0, sizeof(sockaddr_nl)); |
67 | 0 | m_sockaddr.nl_family = AF_NETLINK; |
68 | 0 | m_sockaddr.nl_groups = group; |
69 | 0 | m_sockaddr.nl_pid = pid; |
70 | 0 | } |
71 | | |
72 | | basic_nl_endpoint(basic_nl_endpoint const& other) = default; |
73 | | basic_nl_endpoint(basic_nl_endpoint&& other) noexcept = default; |
74 | | |
75 | | basic_nl_endpoint& operator=(basic_nl_endpoint const& other) |
76 | | { |
77 | | m_sockaddr = other.m_sockaddr; |
78 | | return *this; |
79 | | } |
80 | | |
81 | | basic_nl_endpoint& operator=(basic_nl_endpoint&& other) noexcept |
82 | | { |
83 | | m_sockaddr = other.m_sockaddr; |
84 | | return *this; |
85 | | } |
86 | | |
87 | | protocol_type protocol() const |
88 | 0 | { |
89 | 0 | return m_proto; |
90 | 0 | } |
91 | | |
92 | | data_type* data() |
93 | | { |
94 | | return reinterpret_cast<data_type*>(&m_sockaddr); |
95 | | } |
96 | | |
97 | | const data_type* data() const |
98 | 0 | { |
99 | 0 | return reinterpret_cast<data_type const*>(&m_sockaddr); |
100 | 0 | } |
101 | | |
102 | | std::size_t size() const |
103 | 0 | { |
104 | 0 | return sizeof(m_sockaddr); |
105 | 0 | } |
106 | | |
107 | | std::size_t capacity() const |
108 | | { |
109 | | return sizeof(m_sockaddr); |
110 | | } |
111 | | |
112 | | // commented the comparison operators for now, until the |
113 | | // same operators are implemented for sockaddr_nl |
114 | | /* |
115 | | friend bool operator==(const basic_nl_endpoint<Protocol>& l |
116 | | , const basic_nl_endpoint<Protocol>& r) |
117 | | { |
118 | | return l.m_sockaddr == r.m_sockaddr; |
119 | | } |
120 | | |
121 | | friend bool operator!=(const basic_nl_endpoint<Protocol>& l |
122 | | , const basic_nl_endpoint<Protocol>& r) |
123 | | { |
124 | | return !(l.m_sockaddr == r.m_sockaddr); |
125 | | } |
126 | | |
127 | | friend bool operator<(const basic_nl_endpoint<Protocol>& l |
128 | | , const basic_nl_endpoint<Protocol>& r) |
129 | | { |
130 | | return l.m_sockaddr < r.m_sockaddr; |
131 | | } |
132 | | |
133 | | friend bool operator>(const basic_nl_endpoint<Protocol>& l |
134 | | , const basic_nl_endpoint<Protocol>& r) |
135 | | { |
136 | | return r.m_sockaddr < l.m_sockaddr; |
137 | | } |
138 | | |
139 | | friend bool operator<=(const basic_nl_endpoint<Protocol>& l |
140 | | , const basic_nl_endpoint<Protocol>& r) |
141 | | { |
142 | | return !(r < l); |
143 | | } |
144 | | |
145 | | friend bool operator>=(const basic_nl_endpoint<Protocol>& l |
146 | | , const basic_nl_endpoint<Protocol>& r) |
147 | | { |
148 | | return !(l < r); |
149 | | } |
150 | | */ |
151 | | |
152 | | private: |
153 | | protocol_type m_proto; |
154 | | sockaddr_nl m_sockaddr; |
155 | | }; |
156 | | |
157 | | class netlink |
158 | | { |
159 | | public: |
160 | | using endpoint = basic_nl_endpoint<netlink>; |
161 | | using socket = boost::asio::basic_raw_socket<netlink>; |
162 | | |
163 | 0 | netlink() : netlink(NETLINK_ROUTE) {} |
164 | | |
165 | | explicit netlink(int nl_family) |
166 | 0 | : m_nl_family(nl_family) |
167 | 0 | { |
168 | 0 | } |
169 | | |
170 | | int type() const |
171 | 0 | { |
172 | 0 | return SOCK_RAW; |
173 | 0 | } |
174 | | |
175 | | int protocol() const |
176 | 0 | { |
177 | 0 | return m_nl_family; |
178 | 0 | } |
179 | | |
180 | | int family() const |
181 | 0 | { |
182 | 0 | return AF_NETLINK; |
183 | 0 | } |
184 | | |
185 | | friend bool operator==(const netlink& l, const netlink& r) |
186 | 0 | { |
187 | 0 | return l.m_nl_family == r.m_nl_family; |
188 | 0 | } |
189 | | |
190 | | friend bool operator!=(const netlink& l, const netlink& r) |
191 | 0 | { |
192 | 0 | return l.m_nl_family != r.m_nl_family; |
193 | 0 | } |
194 | | |
195 | | private: |
196 | | int m_nl_family; |
197 | | }; |
198 | | |
199 | | } |
200 | | |
201 | | #endif // TORRENT_USE_NETLINK |
202 | | |
203 | | #endif |