[英]ASIO UDP client never receiving messages
我正在嘗試使用獨立的ASIO / C ++庫編寫帶有用戶提供的解析器(只是一個帶有幾個IP地址的文本文件,可以用於查詢)的DNS解析器,但我每次嘗試都無法使接收器正常工作。 所有解析器似乎都沒有響應我正在發送的查詢( udp::receive_from
)。 但是,當我嘗試將相同的解析器文件與dnslib
這樣的外部庫dnslib
,它們的工作原理就像是魅力,因此問題出在我的代碼中。 這是我用來將數據發送到DNS服務器的代碼。
struct DNSPktHeader
{
uint16_t id{};
uint16_t bitfields{};
uint16_t qdcount{};
uint16_t ancount{};
uint16_t nscount{};
uint16_t arcount{};
};
// dnsname, for example is -> google.com
// dns_resolver is a list of udp::endpoint of IPv4 address on port 53.
// ip is the final result
// returns 0 on success and negative value on failure
int get_host_by_name( char const *dnsname, std::vector<udp::endpoint> const & dns_resolvers, OUT uint16_t* ip )
{
uint8_t netbuf[128]{};
char const *funcname = "get_host_by_name";
uint16_t const dns_id = rand() % 2345; // warning!!! Simply for testing purpose
DNSPktHeader dns_qry{};
dns_qry.id = dns_id;
dns_qry.qdcount = 1;
dns_qry.bitfields = 0x8; // set the RD field of the header to 1
// custom_htons sets the buffer pointed to by the second argument netbuf
// to the htons of the first argument
custom_htons( dns_qry.id, netbuf + 0 );
custom_htons( dns_qry.bitfields, netbuf + 2 );
custom_htons( dns_qry.qdcount, netbuf + 4 );
custom_htons( dns_qry.ancount, netbuf + 6 );
custom_htons( dns_qry.nscount, netbuf + 8 );
custom_htons( dns_qry.arcount, netbuf + 10 );
unsigned char* question_start = netbuf + sizeof( DNSPktHeader ) + 1;
// creates the DNS question segment into netbuf's specified starting index
int len = create_question_section( dnsname, (char**) &question_start, thisdns::dns_record_type::DNS_REC_A,
thisdns::dns_class::DNS_CLS_IN );
if( len < 0 ){
fmt::print( stderr, "{}: {} ({})\n", funcname, dnslib_errno_strings[DNSLIB_ERRNO_BADNAME - 1], dnsname );
return -EFAULT;
}
len += sizeof( DNSPktHeader );
fmt::print( stdout, "{}: Submitting DNS A-record query for domain name ({})\n", funcname, dnsname );
asio::error_code resolver_ec{};
udp::socket udp_socket{ DNSResolver::GetIOService() };
udp_socket.open( udp::v4() );
// set 5 seconds timeout on receive and reuse the address
udp_socket.set_option( asio::ip::udp::socket::reuse_address( true ) );
udp_socket.set_option( asio::detail::socket_option::integer<SOL_SOCKET, SO_RCVTIMEO>{ 5'000 } );
udp_socket.bind( udp::endpoint{ asio::ip::make_address( "127.0.0.1" ), 53 } );
std::size_t bytes_read = 0, retries = 1;
int const max_retries = 10;
asio::error_code receiver_err{};
uint8_t receive_buf[0x200]{};
udp::endpoint default_receiver{};
do{
udp::endpoint const & resolver_endpoint{ dns_resolvers[retries] };
int bytes_sent = udp_socket.send_to( asio::buffer( netbuf, len ), resolver_endpoint, 0, resolver_ec );
if( bytes_sent < len || resolver_ec ){
fmt::print( stderr, "{}: (found {}, expected {})\n", funcname, i, sizeof( DNSPktHeader ) );
return -EFAULT;
}
// ======== the problem ==============
bytes_read = udp_socket.receive_from( asio::buffer( receive_buf, sizeof( receive_buf ) ), default_receiver, 0,
receiver_err );
// bytes_read always return 0
if( receiver_err ){
fmt::print( stderr, "{}\n\n", receiver_err.message() );
}
} while( bytes_read == 0 && retries++ < max_retries );
//...
}
我已經盡力了,但顯然還不夠。 您能否看一下這個問題並幫助找出問題所在? 這是我第一次在任何實際項目中使用ASIO。
不知道這是否相關,但這是create_question_section
。
int create_question_section( const char *dnsname, char** buf, thisdns::dns_record_type type, thisdns::dns_class class_ )
{
char const *funcname = "create_question_section";
if( dnsname[0] == '\0' ){ // Blank DNS name?
fmt::print( stderr, "{}: Blank DNS name?\n", funcname );
return -EBADF;
}
uint8_t len{};
int index{};
int j{};
bool found = false;
do{
if( dnsname[index] != '.' ){
j = 1;
found = false;
do{
if( dnsname[index + j] == '.' || dnsname[index + j] == '\0' ){
len = j;
strncpy( *buf, (char*) &len, 1 );
++( *buf );
strncpy( *buf, (char*) dnsname + index, j );
( *buf ) += j;
found = true;
if( dnsname[index + j] != '\0' )
index += j + 1;
else
index += j;
} else{
j++;
}
} while( !found && j < 64 );
} else{
fmt::print( stderr, "{}: DNS addresses can't start with a dot!\n", funcname );
return -EBADF; // DNS addresses can't start with a dot!
}
} while( dnsname[index] );
uint8_t metadata_buf[5]{};
custom_htons( (uint16_t)type, metadata_buf + 1 );
custom_htons( (uint16_t)class_, metadata_buf + 3 );
strncpy( *buf, (char*) metadata_buf, sizeof(metadata_buf) );
return sizeof( metadata_buf ) + index + 1;
}
至少有兩個問題為什么它對您不起作用。 它們全都歸結為您發送的DNS數據包格式錯誤的事實。
這條線
unsigned char* question_start = netbuf + sizeof( DNSPktHeader ) + 1;
將指針設置到緩沖區比您想要的位置更遠的位置。 而不是從位置12(從0開始索引)開始,開始在位置13開始編碼的FQDN。這意味着DNS服務器看到一個零長度的域名和一些垃圾記錄類型和類,而忽略其余部分。 因此,它決定根本不響應您的查詢。 只要擺脫+1
。
另一個可能的問題是使用custom_htons()
對所有記錄進行編碼。 我不知道它是如何實現的,因此無法告訴您它是否正確運行。
此外,盡管不直接負責您觀察到的行為,但是對bind()
的以下調用將無效,除非您以root用戶身份運行二進制文件(或在Linux上具有適當的功能),因為您試圖綁定到特權端口
udp_socket.bind( udp::endpoint{ asio::ip::make_address( "127.0.0.1" ), 53 } );
此外,此dns_qry.bitfields = 0x8;
不會做你想要的。 應該是dns_qry.bitfields = 0x80;
。
選中此和此RFC ,以獲取有關如何形成有效DNS請求的參考。
重要說明:我強烈建議您不要將C ++與C混合使用。但是請選擇其中一個,因為您標記了C ++並使用Boost和libfmt,所以請堅持使用C ++。 用適當的C ++版本( static_cast
, reinterpret_cast
等)替換所有C樣式的強制轉換。 而不是使用C樣式的數組,請使用std::array
,不要使用strncpy
等。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.