2016-11-07 13:40:09 +01:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2016, Steven Siloti
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
2016-11-08 09:44:43 +01:00
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
2016-11-07 13:40:09 +01:00
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TORRENT_NETLINK_HPP
|
|
|
|
#define TORRENT_NETLINK_HPP
|
|
|
|
|
|
|
|
#include "libtorrent/config.hpp"
|
|
|
|
|
|
|
|
#if TORRENT_USE_NETLINK
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <cstdint>
|
2016-11-08 09:44:43 +01:00
|
|
|
|
|
|
|
#include "libtorrent/aux_/disable_warnings_push.hpp"
|
2016-11-07 13:40:09 +01:00
|
|
|
#include <linux/netlink.h>
|
|
|
|
#include <linux/rtnetlink.h>
|
|
|
|
#include <boost/asio/basic_raw_socket.hpp>
|
2016-11-08 09:44:43 +01:00
|
|
|
#include "libtorrent/aux_/disable_warnings_pop.hpp"
|
2016-11-07 13:40:09 +01:00
|
|
|
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace libtorrent {
|
|
|
|
|
2016-11-07 13:40:09 +01:00
|
|
|
template <typename Protocol>
|
|
|
|
class basic_nl_endpoint
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using protocol_type = Protocol;
|
|
|
|
using data_type = boost::asio::detail::socket_addr_type;
|
|
|
|
|
2017-05-28 13:14:58 +02:00
|
|
|
basic_nl_endpoint() noexcept : basic_nl_endpoint(protocol_type(), 0, 0) {}
|
2016-11-07 13:40:09 +01:00
|
|
|
|
2017-03-18 13:49:19 +01:00
|
|
|
basic_nl_endpoint(protocol_type netlink_family, std::uint32_t group, std::uint32_t pid = 0)
|
2016-11-14 01:46:22 +01:00
|
|
|
: m_proto(netlink_family)
|
2016-11-07 13:40:09 +01:00
|
|
|
{
|
2016-11-14 01:46:22 +01:00
|
|
|
std::memset(&m_sockaddr, 0, sizeof(sockaddr_nl));
|
|
|
|
m_sockaddr.nl_family = AF_NETLINK;
|
|
|
|
m_sockaddr.nl_groups = group;
|
|
|
|
m_sockaddr.nl_pid = pid;
|
2016-11-07 13:40:09 +01:00
|
|
|
}
|
|
|
|
|
2017-05-28 13:14:58 +02:00
|
|
|
basic_nl_endpoint(basic_nl_endpoint const& other) = default;
|
|
|
|
basic_nl_endpoint(basic_nl_endpoint&& other) noexcept = default;
|
2016-11-07 13:40:09 +01:00
|
|
|
|
2017-05-28 13:14:58 +02:00
|
|
|
basic_nl_endpoint& operator=(basic_nl_endpoint const& other)
|
2016-11-07 13:40:09 +01:00
|
|
|
{
|
2016-11-14 01:46:22 +01:00
|
|
|
m_sockaddr = other.m_sockaddr;
|
2016-11-07 13:40:09 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-05-28 13:14:58 +02:00
|
|
|
basic_nl_endpoint& operator=(basic_nl_endpoint&& other) noexcept
|
2016-11-07 13:40:09 +01:00
|
|
|
{
|
2016-11-14 01:46:22 +01:00
|
|
|
m_sockaddr = other.m_sockaddr;
|
2016-11-07 13:40:09 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
protocol_type protocol() const
|
|
|
|
{
|
2016-11-14 01:46:22 +01:00
|
|
|
return m_proto;
|
2016-11-07 13:40:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
data_type* data()
|
|
|
|
{
|
2016-11-14 01:46:22 +01:00
|
|
|
return &m_sockaddr;
|
2016-11-07 13:40:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const data_type* data() const
|
|
|
|
{
|
2016-11-14 01:46:22 +01:00
|
|
|
return reinterpret_cast<data_type const*>(&m_sockaddr);
|
2016-11-07 13:40:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t size() const
|
|
|
|
{
|
2016-11-14 01:46:22 +01:00
|
|
|
return sizeof(m_sockaddr);
|
2016-11-07 13:40:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t capacity() const
|
|
|
|
{
|
2016-11-14 01:46:22 +01:00
|
|
|
return sizeof(m_sockaddr);
|
2016-11-07 13:40:09 +01:00
|
|
|
}
|
|
|
|
|
2017-06-02 14:29:41 +02:00
|
|
|
// commented the comparison operators for now, until the
|
|
|
|
// same operators are implemented for sockaddr_nl
|
|
|
|
/*
|
2016-11-07 13:40:09 +01:00
|
|
|
friend bool operator==(const basic_nl_endpoint<Protocol>& l
|
|
|
|
, const basic_nl_endpoint<Protocol>& r)
|
|
|
|
{
|
2016-11-14 01:46:22 +01:00
|
|
|
return l.m_sockaddr == r.m_sockaddr;
|
2016-11-07 13:40:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
friend bool operator!=(const basic_nl_endpoint<Protocol>& l
|
|
|
|
, const basic_nl_endpoint<Protocol>& r)
|
|
|
|
{
|
2016-11-14 01:46:22 +01:00
|
|
|
return !(l.m_sockaddr == r.m_sockaddr);
|
2016-11-07 13:40:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
friend bool operator<(const basic_nl_endpoint<Protocol>& l
|
|
|
|
, const basic_nl_endpoint<Protocol>& r)
|
|
|
|
{
|
2016-11-14 01:46:22 +01:00
|
|
|
return l.m_sockaddr < r.m_sockaddr;
|
2016-11-07 13:40:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
friend bool operator>(const basic_nl_endpoint<Protocol>& l
|
|
|
|
, const basic_nl_endpoint<Protocol>& r)
|
|
|
|
{
|
2016-11-14 01:46:22 +01:00
|
|
|
return r.m_sockaddr < l.m_sockaddr;
|
2016-11-07 13:40:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
friend bool operator<=(const basic_nl_endpoint<Protocol>& l
|
|
|
|
, const basic_nl_endpoint<Protocol>& r)
|
|
|
|
{
|
|
|
|
return !(r < l);
|
|
|
|
}
|
|
|
|
|
|
|
|
friend bool operator>=(const basic_nl_endpoint<Protocol>& l
|
|
|
|
, const basic_nl_endpoint<Protocol>& r)
|
|
|
|
{
|
|
|
|
return !(l < r);
|
|
|
|
}
|
2017-06-02 14:29:41 +02:00
|
|
|
*/
|
2016-11-07 13:40:09 +01:00
|
|
|
|
|
|
|
private:
|
2016-11-14 01:46:22 +01:00
|
|
|
protocol_type m_proto;
|
|
|
|
sockaddr_nl m_sockaddr;
|
2016-11-07 13:40:09 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class netlink
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using endpoint = basic_nl_endpoint<netlink>;
|
|
|
|
using socket = boost::asio::basic_raw_socket<netlink>;
|
|
|
|
|
2016-11-08 09:44:43 +01:00
|
|
|
netlink() : netlink(NETLINK_ROUTE) {}
|
2016-11-07 13:40:09 +01:00
|
|
|
|
|
|
|
explicit netlink(int nl_family)
|
2016-11-14 01:46:22 +01:00
|
|
|
: m_nl_family(nl_family)
|
2016-11-07 13:40:09 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int type() const
|
|
|
|
{
|
|
|
|
return SOCK_RAW;
|
|
|
|
}
|
|
|
|
|
|
|
|
int protocol() const
|
|
|
|
{
|
2016-11-14 01:46:22 +01:00
|
|
|
return m_nl_family;
|
2016-11-07 13:40:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int family() const
|
|
|
|
{
|
|
|
|
return AF_NETLINK;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend bool operator==(const netlink& l, const netlink& r)
|
|
|
|
{
|
2016-11-14 01:46:22 +01:00
|
|
|
return l.m_nl_family == r.m_nl_family;
|
2016-11-07 13:40:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
friend bool operator!=(const netlink& l, const netlink& r)
|
|
|
|
{
|
2016-11-14 01:46:22 +01:00
|
|
|
return l.m_nl_family != r.m_nl_family;
|
2016-11-07 13:40:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-11-14 01:46:22 +01:00
|
|
|
int m_nl_family;
|
2016-11-07 13:40:09 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-11-08 09:44:43 +01:00
|
|
|
#endif // TORRENT_USE_NETLINK
|
2016-11-07 13:40:09 +01:00
|
|
|
|
|
|
|
#endif
|