title: Raw socket programming - Why does printf() affect the packet sending?
body: <p>Ok, it is a very weird problem. I was trying to create a raw socket ICMP packet to spoof the ping request.</p>
  
  <pre><code>int s;
  s = socket(PF_INET, SOCK_RAW, IPPROTO_RAW);
  </code></pre>
  
  <p>And then</p>
  
  <pre><code>int one; // I should initialize it as 1, but I didn't. 
  const int *val = &amp;one;
  setsockopt(s, IPPROTO_IP, IP_HDRINCL, val, sizeof(one));
  ....
  </code></pre>
  
  <p>It turns out that since I didn't initialize one as 1, the spoofed client cannot receive the ping reply. However, when I add a</p>
  
  <p><code>unsigned char *ch = (unsigned char *)spoof;</code> </p>
  
  <p>just before the</p>
  
  <p><code>close(s);</code>,</p>
  
  <p>it turns out that the spoofed client can receive the ping reply. 
  Why is that?</p>