【发布时间】:2013-04-28 21:33:26
【问题描述】:
我编写了一个 ping 程序,将 syn 数据包发送到目标 IP 地址 1.1.1.1 端口 0。我使用了 wireshark,我看到返回的数据包带有 RST/ACK 标志。但问题是当我使用 select() 尝试读取套接字时,select() 总是返回 0。所以我不知道如何调试程序。我确定我每次都重置 fd_set 读取集。有什么我错过的吗? 我发现 1.1.1.1 的回复 在 0.0003 秒内返回 使用 wireshark 我设置的 timeout 是 1 秒(1000 毫秒) 这是代码
void readloop() {
....
while(nsent < 4) {
send_v4();
wait_for_reply(1000); // wait for 1 second;
}
}
int wait_for_reply(long wait_time) {
....
result = recving_time(...);
if(result < 0) // because time out
return 0;
....
}
int recving_time(...) {
......
fd_set readset;
select_again:
set timeout value to structure *to*
FD_ZERO(&readset);
FD_SET(sockfd, &readset);
readable = select(sockfd+1, &readset, NULL, NULL, &to);
#ifdef DEBUG
fprintf(stderr, "readable is %d\n",readable); // **Why readable always be 0 although packets returned.**
#endif
if(readable < 0) {
if(errno == EINTR)
goto select_again;
else {
perror("select() error");
exit(1);
}
if(readable == 0) {
return -1;
}
......
}
我不确定这里是否有足够的代码来理解我要说的内容。如果你能帮助我,我将非常感激
【问题讨论】:
-
你实际上不能使用端口 0 -- 如果你绑定到端口 0,内核会为你分配一个未使用的端口,如果内核看到数据包发送到一个不存在的端口(例如为 0),它将以 RST/ACK 响应,告诉发送它的人没有这样的端口...
-
@ChrisDodd 其实我要写的程序类似命令: hping3 -S 1.1.1.1 hping send syn packet to port 0. and get rst/ack reply to determine the host is在线