1
votes

I use this "struct iphdr* ip_reply" to read received packet with this function :

recvfrom(sockfd, buffer, sizeof(struct iphdr) + sizeof(struct icmphdr), 0, (struct sockaddr *)&connection, &addrlen))

ip_reply = (struct iphdr*) buffer; // casting => read ip header from received packet

this is the struct parameter :

      struct iphdr {  #if defined(__LITTLE_ENDIAN_BITFIELD)
     __u8    ihl:4,
            version:4;  #elif defined (__BIG_ENDIAN_BITFIELD)
     __u8    version:4,
             ihl:4;

   __u8    tos;
   __be16  tot_len;
   __be16  id;
   __be16  frag_off;
   __u8    ttl;
   __u8    protocol;
   __sum16 check;
   __be32  saddr;
   __be32  daddr;
    /*The options start here. */  };

how can I display "saddr" "daddr" with printf?

2

2 Answers

0
votes

There is a group of function in libc that deals with those transformations. In your case, you can use the function: inet_ntoa(). Basically, it get an address in network binary format (network byte order) and retrieve an IPv4 address, using dot notation, into a string.

If you are using a unix SO, I suggest you to look at man pages to see the correct usage.

1
votes

Use the correct function: ntohl()

This functions is meant to be used to translate Network TO Host (that's why its name starts with ntoh) and you don't have to care about the endianess of your system: the C library will translate it for your, if necessary.