NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
icmp_socket类 参考final

ICMP Socket类 更多...

#include <icmp_socket.hpp>

类 icmp_socket 继承关系图:
[图例]

struct  ping_result
 Ping操作结果 更多...
struct  traceroute_hop
 Traceroute跳点信息 更多...

Public 类型

enum  icmp_type : uint8_t { ICMP_ECHO_REPLY = 0 , ICMP_ECHO_REQUEST = 8 , ICMP_TIME_EXCEEDED = 11 }
 ICMP消息类型常量 更多...
Public 类型 继承自 socket_base
using native_handle_type
 平台原生句柄类型

Public 成员函数

 icmp_socket ()=default
 默认构造函数
 icmp_socket (native_handle_type fd) noexcept
 从原生句柄构造
void open ()
 打开ICMP socket
ping_result ping (const ip_address &dest, milliseconds timeout, uint16_t sequence=0, const void *data=nullptr, size_t data_len=0)
 执行Ping操作
vector< traceroute_hoptraceroute (const ip_address &dest, int max_hops=30, milliseconds probe_timeout=milliseconds(1000), int probes_per_hop=3)
 执行Traceroute操作
Public 成员函数 继承自 socket_base
 socket_base ()
 默认构造函数
 socket_base (const native_handle_type fd) noexcept
 从原生句柄构造
 socket_base (socket_base &&other) noexcept
 移动构造函数
socket_baseoperator= (socket_base &&other) noexcept
 移动赋值运算符
virtual ~socket_base ()
 析构函数
NEFORCE_NODISCARD native_handle_type native_handle () const noexcept
 获取原生句柄
NEFORCE_NODISCARD bool is_open () const noexcept
 检查socket是否已打开
 operator bool () const noexcept
 布尔转换运算符
void open (int family, int type, int protocol)
 打开socket
virtual bool close () noexcept
 关闭socket
bool try_open (int family, int type, int protocol) noexcept
 尝试打开socket(不抛出异常)
bool set_nonblocking (bool enable) noexcept
 设置非阻塞模式
bool shutdown_send () noexcept
 关闭发送方向
bool shutdown_receive () noexcept
 关闭接收方向
bool shutdown_both () noexcept
 关闭双向通信
bool set_option (int level, int optname, const void *value, ::socklen_t len) noexcept
 设置socket选项
bool get_option (int level, int optname, void *optval, ::socklen_t *optlen) const noexcept
 获取socket选项
bool set_reuse_address (bool enable=true) noexcept
 设置地址重用(SO_REUSEADDR)
bool set_reuse_port (bool enable=true) noexcept
 设置端口重用(SO_REUSEPORT)
bool set_keep_alive (bool enable=true) noexcept
 设置TCP KeepAlive
bool set_tcp_nodelay (bool enable=true) noexcept
 设置TCP_NODELAY(禁用Nagle算法)
bool set_receive_buffer_size (int size) noexcept
 设置接收缓冲区大小
bool set_send_buffer_size (int size) noexcept
 设置发送缓冲区大小
bool set_send_timeout (milliseconds timeout) noexcept
 设置发送超时时间
bool set_receive_timeout (milliseconds timeout) noexcept
 设置接收超时时间
NEFORCE_NODISCARD optional< ip_addresslocal_endpoint () const
 获取本地端点地址
NEFORCE_NODISCARD optional< ip_addressremote_endpoint () const
 获取远程端点地址
void bind (const ip_address &endpoint)
 绑定socket到本地地址
void listen (int backlog)
 开始监听连接(TCP)
NEFORCE_NODISCARD native_handle_type release () noexcept
 释放socket所有权

额外继承的成员函数

静态 Public 属性 继承自 socket_base
static constexpr native_handle_type invalid_handle
 无效句柄常量
Protected 属性 继承自 socket_base
native_handle_type fd_ = invalid_handle
 Socket句柄

详细描述

ICMP Socket类

实现ICMP协议的原始套接字,支持Ping和Traceroute功能。

主要功能:

  • ICMP Echo请求/回复(Ping)
  • 网络路径追踪(Traceroute)
  • ICMP超时消息处理
  • RTT(往返时间)测量
  • TTL设置和读取

使用示例:

// Ping示例
sock.open(AF_INET);
auto dest = ip_address::parse("8.8.8.8");
if (dest) {
auto result = sock.ping(*dest, milliseconds(2000));
if (result.success) {
println("Reply from ", result.destination,
": bytes=", result.reply_size,
" time=", result.rtt.count(), "ms",
" TTL=", result.reply_ttl);
} else {
println("Request timed out");
}
}
// Traceroute示例
auto hops = sock.traceroute(*dest, 30, milliseconds(1000), 3);
for (size_t i = 0; i < hops.size(); ++i) {
const auto& hop = hops[i];
print(i + 1, ". ");
if (hop.address.is_valid()) {
print(hop.address);
for (int j = 0; j < 3; ++j) {
if (hop.rtt[j].count() >= 0) {
print(" ", hop.rtt[j].count(), "ms");
} else {
print(" *");
}
}
} else {
println("* * *");
}
if (hop.reached) break;
}
ping_result ping(const ip_address &dest, milliseconds timeout, uint16_t sequence=0, const void *data=nullptr, size_t data_len=0)
执行Ping操作
vector< traceroute_hop > traceroute(const ip_address &dest, int max_hops=30, milliseconds probe_timeout=milliseconds(1000), int probes_per_hop=3)
执行Traceroute操作
void open()
打开ICMP socket
icmp_socket()=default
默认构造函数
static NEFORCE_NODISCARD optional< ip_address > parse(const string &host, ports port=ports{}) noexcept
从字符串解析IP地址
void print(Args &&... args)
打印多个值
void println(Args &&... args)
打印多个值并换行
duration< int64_t, milli > milliseconds
毫秒持续时间
注解
在Linux上需要root权限才能创建原始ICMP套接字。 Windows可能需要管理员权限。

在文件 icmp_socket.hpp102 行定义.

成员枚举类型说明

◆ icmp_type

ICMP消息类型常量

枚举值
ICMP_ECHO_REPLY 

Echo Reply(Ping响应)

ICMP_ECHO_REQUEST 

Echo Request(Ping请求)

ICMP_TIME_EXCEEDED 

Time Exceeded(TTL超时)

在文件 icmp_socket.hpp108 行定义.

构造及析构函数说明

◆ icmp_socket()

icmp_socket::icmp_socket ( native_handle_type fd)
inlineexplicitnoexcept

从原生句柄构造

参数
fd原生socket句柄

在文件 icmp_socket.hpp153 行定义.

成员函数说明

◆ open()

void icmp_socket::open ( )

打开ICMP socket

异常
socket_exception创建失败时抛出

创建原始ICMP套接字,需要适当权限。

◆ ping()

ping_result icmp_socket::ping ( const ip_address & dest,
milliseconds timeout,
uint16_t sequence = 0,
const void * data = nullptr,
size_t data_len = 0 )

执行Ping操作

参数
dest目标IPv4地址
timeout超时时间
sequence序列号(默认0)
data附加数据(可选)
data_len附加数据长度
返回
Ping结果

发送ICMP Echo请求并等待响应,测量RTT。

引用了 data().

◆ traceroute()

vector< traceroute_hop > icmp_socket::traceroute ( const ip_address & dest,
int max_hops = 30,
milliseconds probe_timeout = milliseconds(1000),
int probes_per_hop = 3 )

执行Traceroute操作

参数
dest目标IPv4地址
max_hops最大跳数(默认30)
probe_timeout每跳探测超时时间(默认1000ms)
probes_per_hop每跳探测次数(默认3)
返回
跳点信息列表

通过逐步增加TTL值探测网络路径, 收集每个中间路由器的IP地址和响应时间。


该类的文档由以下文件生成: