109 lines
2.6 KiB
C++
109 lines
2.6 KiB
C++
/**
|
||
* @brief 无需root权限发送icmp包,从boost/asio/ip/icmp.hpp修改而来
|
||
* @author yikenan
|
||
*/
|
||
|
||
# pragma once
|
||
|
||
#include <boost/asio/detail/config.hpp>
|
||
#include <boost/asio/detail/socket_types.hpp>
|
||
#include <boost/asio/basic_raw_socket.hpp>
|
||
#include <boost/asio/ip/basic_endpoint.hpp>
|
||
#include <boost/asio/ip/basic_resolver.hpp>
|
||
#include <boost/asio/ip/basic_resolver_iterator.hpp>
|
||
#include <boost/asio/ip/basic_resolver_query.hpp>
|
||
|
||
#include <boost/asio/detail/push_options.hpp>
|
||
|
||
namespace boost
|
||
{
|
||
namespace asio
|
||
{
|
||
namespace ip
|
||
{
|
||
|
||
/**
|
||
* @brief 无需root权限发送icmp包,从boost/asio/ip/icmp.hpp修改而来
|
||
*/
|
||
class icmp_nonroot
|
||
{
|
||
public:
|
||
/// The type of a ICMP endpoint.
|
||
typedef basic_endpoint <icmp_nonroot> endpoint;
|
||
|
||
/// Construct to represent the IPv4 ICMP protocol.
|
||
static icmp_nonroot v4()
|
||
{
|
||
return icmp_nonroot( BOOST_ASIO_OS_DEF( IPPROTO_ICMP ),
|
||
BOOST_ASIO_OS_DEF( AF_INET ) );
|
||
}
|
||
|
||
/// Construct to represent the IPv6 ICMP protocol.
|
||
static icmp_nonroot v6()
|
||
{
|
||
return icmp_nonroot( BOOST_ASIO_OS_DEF( IPPROTO_ICMPV6 ),
|
||
BOOST_ASIO_OS_DEF( AF_INET6 ) );
|
||
}
|
||
|
||
/// Obtain an identifier for the type of the protocol.
|
||
int type() const
|
||
{
|
||
//< yikenan
|
||
#ifndef OS_WINDOWS
|
||
return BOOST_ASIO_OS_DEF( SOCK_DGRAM );
|
||
#else
|
||
return BOOST_ASIO_OS_DEF(SOCK_RAW);
|
||
#endif
|
||
}
|
||
|
||
/// Obtain an identifier for the protocol.
|
||
int protocol() const
|
||
{
|
||
return protocol_;
|
||
}
|
||
|
||
/// Obtain an identifier for the protocol family.
|
||
int family() const
|
||
{
|
||
return family_;
|
||
}
|
||
|
||
/// The ICMP socket type.
|
||
//< yikenan
|
||
//typedef basic_raw_socket <icmp_nonroot> socket;
|
||
typedef basic_datagram_socket <icmp_nonroot> socket;
|
||
|
||
/// The ICMP resolver type.
|
||
typedef basic_resolver <icmp_nonroot> resolver;
|
||
|
||
/// Compare two protocols for equality.
|
||
friend bool operator==( const icmp_nonroot &p1, const icmp_nonroot &p2 )
|
||
{
|
||
return p1.protocol_ == p2.protocol_ && p1.family_ == p2.family_;
|
||
}
|
||
|
||
/// Compare two protocols for inequality.
|
||
friend bool operator!=( const icmp_nonroot &p1, const icmp_nonroot &p2 )
|
||
{
|
||
return p1.protocol_ != p2.protocol_ || p1.family_ != p2.family_;
|
||
}
|
||
|
||
private:
|
||
// Construct with a specific family.
|
||
explicit icmp_nonroot( int protocol_id, int protocol_family )
|
||
: protocol_( protocol_id ),
|
||
family_( protocol_family )
|
||
{
|
||
}
|
||
|
||
int protocol_;
|
||
int family_;
|
||
};
|
||
|
||
} // namespace ip
|
||
} // namespace asio
|
||
} // namespace boost
|
||
|
||
#include <boost/asio/detail/pop_options.hpp>
|
||
|