我正在通过原始套接字发送 dns 数据包(A 型)。我从我的默认 DNS 服务器得到了很好的结果问题是我的代码卡在“recvfrom”中,我可以看到传出的请求数据包和通过 wireshark 的传入答案(没有错误)。我尝试了各种操作,例如使用不同的套接字来传出和传入数据包,尝试“recv”……但似乎没有任何效果。我将不胜感激。
int main() {
char hostname[100];
//Get the DNS servers from the resolv.conf file
get_dns_servers();
struct sockaddr_in sin;
// dest
sin.sin_family = AF_INET;
sin.sin_port = htons (53);
sin.sin_addr.s_addr = inet_addr (dns_servers[0]);
//Get the hostname from the terminal
cout << "\nEnter Hostname to Poison : ";
scanf("%s" , hostname);
//Create a raw socket of type IPPROTO
int s = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
if(s == -1)
{
//socket creation failed, may be because of non-root privileges
perror("Failed to create raw socket");
exit(1);
}
dns_packet *pak = new dns_packet("192.168.203.128",(uint16_t) 50115,(in_addr_t) sin.sin_addr.s_addr, (in_port_t) sin.sin_port, (int) 3333, hostname);
//Send the packet
if (sendto (s, pak->datagram, pak->iph->tot_len , 0, (struct sockaddr *) &sin, sizeof (sin)) < 0)
{
perror("sendto failed");
}
//Data send successfully
else
{
printf ("Packet Send. Length : %d \n" , pak->iph->tot_len);
}
//Receive the answer
int i = sizeof(sin);
unsigned char buf[65536];
cout << "\nReceiving answer..." << endl;
if(recvfrom (s,(char*)buf , sizeof(buf) , 0 , (struct sockaddr*)&sin , (socklen_t*)&i ) < 0)
{
perror("recvfrom failed");
}
read_answer(buf);
return 0;
}