2016-06-13 90 views
0

我想通過boost :: asio原始套接字發送TCP消息。 我的數據包基於來源:https://github.com/pfpacket/SYN-flood如何通過boost :: asio從send_to獲取特定的錯誤細節?

我檢查了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

英語翻譯:「使用非有效的論據」

我沒有更多的想法,哪裏出現問題。我需要更多有關相關問題的細節。

如何從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; 
    } 
} 

回答

0

短耳的錯誤檢測和報告是一個相當薄的層,通常傳播的它已被底層系統調用提供的所有信息。在失敗時,如果應用程序能夠接收它,Asio將填充boost::system::error_code,例如異步操作或帶error_code參數的同步操作超載;否則會拋出一個包含error_code的異常。該documentation明確提到異步操作,但同樣「好像」行爲通常是適用於同步操作:

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確定正在作出何種操作系統調用。然後可以使用相應的OS文檔來確定發生錯誤的條件和值。錯誤代碼和Boost.Asio錯誤代碼之間的映射位於asio/error.hpp之內,但映射通常相當簡單。

sendto()的情況下,返回值EINVAL可能不提供有見地的信息。當Asio製造system call to sendto()時,可以通過附加調試器並檢查有效性的參數來進一步診斷問題。

相關問題