some refactor in ip change notifier source code (#1301)

some refactor in ip change notifier source code. using NETLINK_ROUTE (value 0) as the default nl_family
This commit is contained in:
Alden Torres 2016-11-08 03:44:43 -05:00 committed by Arvid Norberg
parent 9e5a2a7de4
commit 1a159666ff
3 changed files with 46 additions and 40 deletions

View File

@ -7,14 +7,14 @@ Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* 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.
* 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.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
@ -41,18 +41,23 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/io_service.hpp"
#if defined TORRENT_BUILD_SIMULATOR
// TODO: simulator support
#elif TORRENT_USE_NETLINK
#include "libtorrent/netlink.hpp"
#elif defined TORRENT_WINDOWS
#include "libtorrent/aux_/disable_warnings_push.hpp"
#include <boost/asio/windows/object_handle.hpp>
#include "libtorrent/aux_/disable_warnings_pop.hpp"
#endif
namespace libtorrent
{
struct ip_change_notifier : boost::noncopyable
struct ip_change_notifier
{
explicit ip_change_notifier(io_service& ios);
~ip_change_notifier();
ip_change_notifier(ip_change_notifier const&) = delete;
ip_change_notifier& operator=(ip_change_notifier const&) = delete;
// cb will be invoked when a change is detected in the
// system's IP addresses
@ -65,7 +70,7 @@ namespace libtorrent
, std::function<void(error_code const&)> cb);
#if defined TORRENT_BUILD_SIMULATOR
// TODO simulator support
// TODO: simulator support
#elif TORRENT_USE_NETLINK
netlink::socket m_socket;
std::array<char, 4096> m_buf;

View File

@ -7,14 +7,14 @@ Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* 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.
* 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.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
@ -39,9 +39,12 @@ POSSIBILITY OF SUCH DAMAGE.
#include <cstring>
#include <cstdint>
#include "libtorrent/aux_/disable_warnings_push.hpp"
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <boost/asio/basic_raw_socket.hpp>
#include "libtorrent/aux_/disable_warnings_pop.hpp"
namespace libtorrent
{
@ -52,13 +55,7 @@ namespace libtorrent
using protocol_type = Protocol;
using data_type = boost::asio::detail::socket_addr_type;
basic_nl_endpoint()
{
std::memset(&sockaddr, 0, sizeof(sockaddr_nl));
sockaddr.nl_family = AF_NETLINK;
sockaddr.nl_groups = 0;
sockaddr.nl_pid = 0;
}
basic_nl_endpoint() : basic_nl_endpoint(protocol_type(), 0, 0) {}
basic_nl_endpoint(protocol_type netlink_family, std::uint32_t group, ::pid_t pid = 0)
: proto(netlink_family)
@ -98,7 +95,7 @@ namespace libtorrent
const data_type* data() const
{
return (struct sockaddr*)&sockaddr;
return reinterpret_cast<data_type const*>(&sockaddr);
}
std::size_t size() const
@ -158,9 +155,7 @@ namespace libtorrent
using endpoint = basic_nl_endpoint<netlink>;
using socket = boost::asio::basic_raw_socket<netlink>;
netlink() : nl_family(0)
{
}
netlink() : netlink(NETLINK_ROUTE) {}
explicit netlink(int nl_family)
: nl_family(nl_family)
@ -198,6 +193,6 @@ namespace libtorrent
}
#endif
#endif // TORRENT_USE_NETLINK
#endif

View File

@ -7,14 +7,14 @@ Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* 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.
* 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.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
@ -33,7 +33,9 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/ip_notifier.hpp"
#if defined TORRENT_WINDOWS && !defined TORRENT_BUILD_SIMULATOR
#include "libtorrent/aux_/disable_warnings_push.hpp"
#include <iphlpapi.h>
#include "libtorrent/aux_/disable_warnings_pop.hpp"
#endif
using namespace std::placeholders;
@ -53,7 +55,11 @@ namespace libtorrent
TORRENT_UNUSED(ios);
#elif defined TORRENT_WINDOWS
if (!m_hnd.is_open())
#ifndef BOOST_NO_EXCEPTIONS
throw system_error(WSAGetLastError(), system_category());
#else
std::terminate();
#endif // BOOST_NO_EXCEPTIONS
m_ovl.hEvent = m_hnd.native_handle();
#elif !TORRENT_USE_NETLINK
TORRENT_UNUSED(ios);
@ -80,11 +86,11 @@ namespace libtorrent
DWORD err = NotifyAddrChange(&hnd, &m_ovl);
if (err == ERROR_IO_PENDING)
{
m_hnd.async_wait([this,cb](error_code const& ec) { on_notify(ec, 0, cb); });
m_hnd.async_wait([this, cb](error_code const& ec) { on_notify(ec, 0, cb); });
}
else
{
m_hnd.get_io_service().post([this,cb,err]()
m_hnd.get_io_service().post([this, cb, err]()
{ cb(error_code(err, system_category())); });
}
#else