Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C中的跟踪路由,错误的ICMP id';她回答道_C_Sockets_Ip_Icmp_Traceroute - Fatal编程技术网

C中的跟踪路由,错误的ICMP id';她回答道

C中的跟踪路由,错误的ICMP id';她回答道,c,sockets,ip,icmp,traceroute,C,Sockets,Ip,Icmp,Traceroute,我正在用C语言编写一个简单的traceroute程序。响应即将到来,但现在我想检查从这个特定程序发送的响应。我的问题是,我从ICMP头中获取的ID值与从路径上的路由器设置的ID值不同。ID仅在来自目标的响应中匹配。我正在为PID设置ICMP标头中的id struct icmp header; header.icmp_type = ICMP_ECHO; header.icmp_code = 0; header.icmp_hun.ih_idseq.icd_id = getpid(); header.

我正在用C语言编写一个简单的traceroute程序。响应即将到来,但现在我想检查从这个特定程序发送的响应。我的问题是,我从ICMP头中获取的ID值与从路径上的路由器设置的ID值不同。ID仅在来自目标的响应中匹配。我正在为PID设置ICMP标头中的id

struct icmp header;
header.icmp_type = ICMP_ECHO;
header.icmp_code = 0;
header.icmp_hun.ih_idseq.icd_id = getpid();
header.icmp_hun.ih_idseq.icd_seq = seq;
header.icmp_cksum = 0;
header.icmp_cksum = compute_icmp_checksum((u_int16_t*)&header, sizeof(header));
我的方法是在每个发送包之后增加一个ttl。以下是发送代码:

struct sockaddr_in recipent;
bzero (&recipent, sizeof(recipent));
recipent.sin_family = AF_INET;
inet_pton(AF_INET, address, &recipent.sin_addr);

ssize_t bytes_sent = sendto(
  sockfd,
  &header,
  sizeof(header),
  0,
  (struct sockaddr*)&recipent,
  sizeof(recipent)
); 
其中
地址
是目标ip。 据我在ICMP报头回复中的理解,id应与我程序的发送包-PID中的id相同,但路径上的每个路由器的id为0,并且
id=PID
仅从目标路由器输入。以下是接收包裹和打印id的代码:

ssize_t packet_len = recvfrom (sockfd, buffer, 4096, 0, (struct sockaddr*)&sender, &sender_len);

char sender_ip_str[20]; 
inet_ntop(AF_INET, &(sender.sin_addr), sender_ip_str, sizeof(sender_ip_str));

struct ip*          ip_header = (struct ip*) buffer;
ssize_t             ip_header_len = 4 * ip_header->ip_hl;
u_int8_t*           icmp_packet = buffer + ip_header_len;
struct icmp*        icmp_header = (struct icmp*) icmp_packet;

printf("icd_id: %d, icd_seq: %d ---\n", icmp_header->icmp_hun.ih_idseq.icd_id, icmp_header->icmp_hun.ih_idseq.icd_seq); 
这是我运行
address=8.8.8.8

1936  <---- PID

icd_id: 0, icd_seq: 0
192.168.1.1 0.13ms

icd_id: 0, icd_seq: 0
83.1.5.1 0.09ms

icd_id: 0, icd_seq: 0
80.50.18.65 0.05ms

icd_id: 0, icd_seq: 0
193.251.248.153 0.16ms

icd_id: 0, icd_seq: 0
193.251.255.168 0.21ms

icd_id: 0, icd_seq: 0
108.170.231.245 0.12ms

icd_id: 4352, icd_seq: 0
142.250.46.245 0.18ms

icd_id: 1936, icd_seq: 49629
8.8.8.8 0.16ms

1936我发现了这个问题,如果有人发现同样的问题,我会发布这个答案

问题是身份证在其他地方,以防rospone超过时间限制

if (resp->type == ICMP_TIMXCEED){
    icmpHeader = (void*)icmpHeader + 8 + ipHeaderLen;
}