I'm writing simple program that sends icmp echo requests. I'm using ping sockets (to be able to send without suid).
This is how i open the socket:
int fd=socket(PF_INET,SOCK_DGRAM,IPPROTO_ICMP);
And this is how i construct icmp datagram:
struct icmphdr req;
req.type=8;
req.code=0;
req.checksum=0;
req.un.echo.id=htons(12);
req.un.echo.sequence=htons(1);
I'm sending the packet with sendto() and it works well, I mean the target host receives the request, sends response and i am able to receive this response in my program.
However, I'm not sure the meaning of un.echo.id field. In many samples that are on the net this field is filled with something like rand() etc. But it is overwritten later, I can clearly see it in tcpdump.
I found such description: LWN.net net: ipv4: add IPPROTO_ICMP
Message identifiers (octets 4-5 of ICMP header) are interpreted as local ports. Addresses are stored in struct sockaddr_in. No port numbers are reserved for privileged processes, port 0 is reserved for API ("let the kernel pick a free number"). There is no notion of remote ports, remote port numbers provided by the user (e.g. in connect()) are ignored.
It's not clear to me. So please, could you tell me, if I should fill this filed or not?
UPDATE: Thanks to John's Zwinck anwer I realised that I posted wrong code example, it's correct now.
Here is whole code and tcpdump output: code and dump
It illustrates my question, in the code id is set to 12 (just a random number, tried many others) but icmp headers in dump have id set to 4.