title: Should I write id to icmphdr id field when using icmp sockets?
body: <p>I'm writing simple program that sends icmp echo requests. I'm using ping sockets (to be able to send without suid).</p>
  
  <p>This is how i open the socket:</p>
  
  <pre><code>int fd=socket(PF_INET,SOCK_DGRAM,IPPROTO_ICMP);
  </code></pre>
  
  <p>And this is how i construct icmp datagram:</p>
  
  <pre><code>struct icmphdr req;
  req.type=8;
  req.code=0;
  req.checksum=0;
  req.un.echo.id=htons(12);
  req.un.echo.sequence=htons(1);
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>I found such description:
  <a href="http://lwn.net/Articles/443051/" rel="nofollow">LWN.net net: ipv4: add IPPROTO_ICMP</a></p>
  
  <blockquote>
    <p>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.</p>
  </blockquote>
  
  <p>It's not clear to me.
  So please, could you tell me, if I should fill this filed or not?</p>
  
  <p>UPDATE:
  Thanks to John's Zwinck anwer I realised that I posted wrong code example, it's correct now.</p>
  
  <p>Here is whole code and tcpdump output: <a href="http://pastebin.com/gDciA8sN" rel="nofollow">code and dump</a></p>
  
  <p>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.</p>