如何通过 boost::asio 从 send_to 获取特定的错误详细信息?
How do I get specific error details from send_to by boost::asio?
我想通过 boost::asio 原始套接字发送 TCP 消息。
我的数据包基于此来源:https://github.com/pfpacket/SYN-flood
我检查了 IP 和 TCP 数据包生成器的二进制输出,这些消息看起来有效。 TCP 的原始套接字函数看起来也不错。
45 10 00 28 00 00 40 00
40 06 A5 18 08 08 08 08
0B 0B 07 01 11 AD 15 B3
00 00 17 AA 00 00 00 00
50 02 10 00 3E BD 00 00
无论如何,如果我通过 send_to 函数将它发送到套接字,我会收到错误消息:
send_to: Ein ungültiges Argument wurde angegeben
英文翻译:"Used a non valid argument"
想不通了,哪里有问题。我需要有关相关问题的更多详细信息。
如何从 send_to() 接收更具体的错误消息?接下来我能做什么?
int main() {
this->port = "5555";
this->target = "11.11.7.1";
try {
boost::asio::io_service io_service;
raw_tcp::socket socket(io_service, raw_tcp::v4());
socket.set_option(boost::asio::ip::ip_hdrincl(true));
raw_tcp::resolver resolver(io_service);
raw_tcp::resolver::query query( this->target , boost::lexical_cast< std::string >( this->port ));
raw_tcp::endpoint destination = *resolver.resolve( query );
boost::asio::streambuf request_buffer;
std::ostream os(&request_buffer);
//creates the ipheader and tcp packet and forward it to buffer os
set_syn_segment(os);
socket.send_to(request_buffer.data(), destination);
} catch (std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
}
Asio 的错误检测和报告是一个相当薄的层,通常传播底层系统调用提供的所有信息。如果应用程序能够接收失败,Asio 将填充 boost::system::error_code
,例如用于异步操作或使用 error_code
参数重载的同步操作;否则它将抛出包含 error_code
的异常。 documentation 明确提到了异步操作,但同样的 "as if" 行为通常适用于同步操作:
Unless otherwise noted, when the behaviour of an asynchronous operation is defined "as if" implemented by a POSIX function, the handler will be invoked with a value of type error_code
that corresponds to the failure condition described by POSIX for that function, if any. Otherwise the handler will be invoked with an implementation-defined error_code
value that reflects the operating system error.
Asynchronous operations will not fail with an error condition that indicates interruption by a signal (POSIX EINTR
). Asynchronous operations will not fail with any error condition associated with non-blocking operations (POSIX EWOULDBLOCK
, EAGAIN
or EINPROGRESS
; Windows WSAEWOULDBLOCK
or WSAEINPROGRESS
).
要进一步调查错误,请考虑使用 BSD API mapping documentation to determine which OS calls are being made. One can then use the appropriate OS documentation to determine the conditions for which an error occurs and the values. The mapping between error codes and Boost.Asio error codes is located within asio/error.hpp
,但映射通常相当简单。
如sendto()
, a return value of EINVAL
may not provide insightful information. One could further diagnose the problem via attaching a debugger and examining the arguments for validity when Asio makes the system call to sendto()
.
我想通过 boost::asio 原始套接字发送 TCP 消息。 我的数据包基于此来源:https://github.com/pfpacket/SYN-flood
我检查了 IP 和 TCP 数据包生成器的二进制输出,这些消息看起来有效。 TCP 的原始套接字函数看起来也不错。
45 10 00 28 00 00 40 00
40 06 A5 18 08 08 08 08
0B 0B 07 01 11 AD 15 B3
00 00 17 AA 00 00 00 00
50 02 10 00 3E BD 00 00
无论如何,如果我通过 send_to 函数将它发送到套接字,我会收到错误消息:
send_to: Ein ungültiges Argument wurde angegeben
英文翻译:"Used a non valid argument"
想不通了,哪里有问题。我需要有关相关问题的更多详细信息。
如何从 send_to() 接收更具体的错误消息?接下来我能做什么?
int main() {
this->port = "5555";
this->target = "11.11.7.1";
try {
boost::asio::io_service io_service;
raw_tcp::socket socket(io_service, raw_tcp::v4());
socket.set_option(boost::asio::ip::ip_hdrincl(true));
raw_tcp::resolver resolver(io_service);
raw_tcp::resolver::query query( this->target , boost::lexical_cast< std::string >( this->port ));
raw_tcp::endpoint destination = *resolver.resolve( query );
boost::asio::streambuf request_buffer;
std::ostream os(&request_buffer);
//creates the ipheader and tcp packet and forward it to buffer os
set_syn_segment(os);
socket.send_to(request_buffer.data(), destination);
} catch (std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
}
Asio 的错误检测和报告是一个相当薄的层,通常传播底层系统调用提供的所有信息。如果应用程序能够接收失败,Asio 将填充 boost::system::error_code
,例如用于异步操作或使用 error_code
参数重载的同步操作;否则它将抛出包含 error_code
的异常。 documentation 明确提到了异步操作,但同样的 "as if" 行为通常适用于同步操作:
Unless otherwise noted, when the behaviour of an asynchronous operation is defined "as if" implemented by a POSIX function, the handler will be invoked with a value of type
error_code
that corresponds to the failure condition described by POSIX for that function, if any. Otherwise the handler will be invoked with an implementation-definederror_code
value that reflects the operating system error.Asynchronous operations will not fail with an error condition that indicates interruption by a signal (POSIX
EINTR
). Asynchronous operations will not fail with any error condition associated with non-blocking operations (POSIXEWOULDBLOCK
,EAGAIN
orEINPROGRESS
; WindowsWSAEWOULDBLOCK
orWSAEINPROGRESS
).
要进一步调查错误,请考虑使用 BSD API mapping documentation to determine which OS calls are being made. One can then use the appropriate OS documentation to determine the conditions for which an error occurs and the values. The mapping between error codes and Boost.Asio error codes is located within asio/error.hpp
,但映射通常相当简单。
如sendto()
, a return value of EINVAL
may not provide insightful information. One could further diagnose the problem via attaching a debugger and examining the arguments for validity when Asio makes the system call to sendto()
.