Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <code>struct tcphdr</code> in tcp.h defines the mandatory part of the TCP header. (Look at the <a href="http://en.wikipedia.org/wiki/Transmission_Control_Protocol#TCP_segment_structure" rel="noreferrer">TCP header</a> and you can match the definitions in <code>struct tcphdr</code> to your the actual bits to appear in the header.) Structs in C have a constant size, but TCP allows optional data. The header length field (<code>doff</code> in the structure) is the total length of the header, including options, so you'll need to add one word to account for the MSS option:</p> <pre><code>tcp_header-&gt;doff = (sizeof(struct tcphdr))/4 + 1; </code></pre> <p>Let's define a structure for the MSS option:</p> <pre><code>struct tcp_option_mss { uint8_t kind; /* 2 */ uint8_t len; /* 4 */ uint16_t mss; } __attribute__((packed)); </code></pre> <p>Now you can populate the structure, in the right order:</p> <pre><code>/*memcpy the mss data onto the end of the tcp header. */ struct tcp_option_mss mss; mss.kind = 2; mss.len = 4; mss.mss = htons(32000); </code></pre> <p>Let's go one step further and define a single structure for your packet, to let the compiler help us out:</p> <pre><code>struct tcphdr_mss { struct tcphdr tcp_header; struct tcp_option_mss mss; }; </code></pre> <p>(You may need to add an end-of-option-list option at the end, and nop options to pad the option list to 8 bytes.)</p> <p>Now we can put all the pieces together:</p> <pre><code>struct tcphdr *CreateTcpHeader() { struct tcphdr_mss *tcp_header; tcp_header = malloc(sizeof(struct tcphdr_mss)); tcp_header-&gt;tcp_header.source = htons(SRC_PORT); tcp_header-&gt;tcp_header.dest = htons(DST_PORT); tcp_header-&gt;tcp_header.seq = htonl(0); tcp_header-&gt;tcp_header.ack_seq = htonl(0); tcp_header-&gt;tcp_header.res1 = 0; tcp_header-&gt;tcp_header.doff = (sizeof(struct tcphdr_mss))/4; tcp_header-&gt;tcp_header.syn = 1; tcp_header-&gt;tcp_header.window = htons(4096); tcp_header-&gt;tcp_header.check = 0; /* Will calculate the checksum with pseudo-header later */ tcp_header-&gt;tcp_header.urg_ptr = 0; tcp_header-&gt;mss.kind = 2; tcp_header-&gt;mss.len = 2; tcp_header-&gt;mss.mss = htons(32000); return (tcp_header); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload