title: setting the maximum segment size in the tcp header
body: <p>I am putting together a port scanner as a learning exercise. My problem is I'm trying to set the maximum segment size option(MSS) in the TCP header. I had a look at tcp.h, but I'm  having trouble figuring out how to set it. I was hoping there would be an option like this:</p>
  
  <p><code>tcp_header-&gt;mss(32000);</code></p>
  
  <p>Something similar to the above was in tcp.h but not in the right struct. Admittedly, I'm still fairly new to reading struct definitions and I couldn't make much sense out of tcp.h so in the end I tried just tacking on the necessary bytes to the end of the TCP header:</p>
  
  <pre><code>struct tcphdr *CreateTcpHeader()
  {
      struct tcphdr *tcp_header;
  
      tcp_header = (struct tcphdr *)malloc(sizeof(struct tcphdr)+4*sizeof(int));
  
  
      tcp_header-&gt;source = htons(SRC_PORT);
      tcp_header-&gt;dest = htons(DST_PORT);
      tcp_header-&gt;seq = htonl(0);             
      tcp_header-&gt;ack_seq = htonl(0);         
      tcp_header-&gt;res1 = 0;
      tcp_header-&gt;doff = (sizeof(struct tcphdr))/4;
      tcp_header-&gt;syn = 1;
      tcp_header-&gt;window = htons(4096);
      tcp_header-&gt;check = 0; /* Will calculate the checksum with pseudo-header later */
      tcp_header-&gt;urg_ptr = 0;
  
  
      /*memcpy the mss data onto the end of the tcp header. */
      int mssCode = 2;
      int mssLength = 4;
      uint16_t mss = htonl(32000);
      int offset = sizeof(struct tcphdr);
      memcpy( (tcp_header+offset), &amp;mssCode, 1 );
      memcpy( (tcp_header+offset+1), &amp;mssLength, 1 );
      memcpy( (tcp_header+offset+2), &amp;mss, 2);
  
      return (tcp_header);
  }
  </code></pre>
  
  <p>But after I wrote it it was clear that is wasn't a real solution, plus it still doesn't work :P So is there a better way?</p>