0

I am making simple traceroute program in C. The responses are coming but now I want to check for responses sent from this particular program. My problem is I am getting different values of ID from ICMP headers from those I set from routers on the path. The ID matches only in response from destination. I am setting id in ICMP header for PID.

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));

The way I do is increase ttl by one after every sent package. Here is the sending code:

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)
); 

Where address is the destination ip. From my understanding in reply in ICMP header the id should be the same as in sent package - PID of my program, but it is 0 for every router on the path and the id = PID only in from the destination router. Here is the code receiving packages and printing the id's:

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); 

and this is example response I get from running my program for 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
Ko Su
  • 1
  • 3

1 Answers1

0

I have found the issue, posting this answer if anyone finds the same problem.

https://en.wikipedia.org/wiki/Internet_Control_Message_Protocol#Control_messages

The problem was the id was in other place in case the rospone exceeded time limit.

if (resp->type == ICMP_TIMXCEED){
    icmpHeader = (void*)icmpHeader + 8 + ipHeaderLen;
}
Ko Su
  • 1
  • 3