1 | /*
|
---|
2 | * Copyright (c) 2009 Lukas Mejdrech
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup tcp
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /** @file
|
---|
34 | * TCP header definition.
|
---|
35 | * Based on the RFC~793.
|
---|
36 | */
|
---|
37 |
|
---|
38 | #ifndef __NET_TCP_HEADER_H__
|
---|
39 | #define __NET_TCP_HEADER_H__
|
---|
40 |
|
---|
41 | #include <sys/types.h>
|
---|
42 |
|
---|
43 | /** TCP header size in bytes.
|
---|
44 | */
|
---|
45 | #define TCP_HEADER_SIZE sizeof( tcp_header_t )
|
---|
46 |
|
---|
47 | /** Returns the actual TCP header length in bytes.
|
---|
48 | * @param[in] header The TCP packet header.
|
---|
49 | */
|
---|
50 | #define TCP_HEADER_LENGTH( header ) (( header )->header_length * 4u )
|
---|
51 |
|
---|
52 | /** Returns the TCP header length.
|
---|
53 | * @param[in] length The TCP header length in bytes.
|
---|
54 | */
|
---|
55 | #define TCP_COMPUTE_HEADER_LENGTH( length ) (( uint8_t ) (( length ) / 4u ))
|
---|
56 |
|
---|
57 | /** Type definition of the transmission datagram header.
|
---|
58 | * @see tcp_header
|
---|
59 | */
|
---|
60 | typedef struct tcp_header tcp_header_t;
|
---|
61 |
|
---|
62 | /** Type definition of the transmission datagram header pointer.
|
---|
63 | * @see tcp_header
|
---|
64 | */
|
---|
65 | typedef tcp_header_t * tcp_header_ref;
|
---|
66 |
|
---|
67 | /** Type definition of the transmission datagram header option.
|
---|
68 | * @see tcp_option
|
---|
69 | */
|
---|
70 | typedef struct tcp_option tcp_option_t;
|
---|
71 |
|
---|
72 | /** Type definition of the transmission datagram header option pointer.
|
---|
73 | * @see tcp_option
|
---|
74 | */
|
---|
75 | typedef tcp_option_t * tcp_option_ref;
|
---|
76 |
|
---|
77 | /** Type definition of the Maximum segment size TCP option.
|
---|
78 | * @see ...
|
---|
79 | */
|
---|
80 | typedef struct tcp_max_segment_size_option tcp_max_segment_size_option_t;
|
---|
81 |
|
---|
82 | /** Type definition of the Maximum segment size TCP option pointer.
|
---|
83 | * @see tcp_max_segment_size_option
|
---|
84 | */
|
---|
85 | typedef tcp_max_segment_size_option_t * tcp_max_segment_size_option_ref;
|
---|
86 |
|
---|
87 | /** Transmission datagram header.
|
---|
88 | */
|
---|
89 | struct tcp_header{
|
---|
90 | /** The source port number.
|
---|
91 | */
|
---|
92 | uint16_t source_port;
|
---|
93 | /** The destination port number.
|
---|
94 | */
|
---|
95 | uint16_t destination_port;
|
---|
96 | /** The sequence number of the first data octet in this segment (except when SYN is present).
|
---|
97 | * If SYN is present the sequence number is the initial sequence number (ISN) and the first data octet is ISN+1.
|
---|
98 | */
|
---|
99 | uint32_t sequence_number;
|
---|
100 | /** If the ACK control bit is set this field contains the value of the next sequence number the sender of the segment is expecting to receive.
|
---|
101 | * Once a~connection is established this is always sent.
|
---|
102 | * @see acknowledge
|
---|
103 | */
|
---|
104 | uint32_t acknowledgement_number;
|
---|
105 | #ifdef ARCH_IS_BIG_ENDIAN
|
---|
106 | /** The number of 32~bit words in the TCP Header.
|
---|
107 | * This indicates where the data begins.
|
---|
108 | * The TCP header (even one including options) is an integral number of 32~bits long.
|
---|
109 | */
|
---|
110 | uint8_t header_length:4;
|
---|
111 | /** Four bits reserved for future use.
|
---|
112 | * Must be zero.
|
---|
113 | */
|
---|
114 | uint8_t reserved1:4;
|
---|
115 | #else
|
---|
116 | /** Four bits reserved for future use.
|
---|
117 | * Must be zero.
|
---|
118 | */
|
---|
119 | uint8_t reserved1:4;
|
---|
120 | /** The number of 32~bit words in the TCP Header.
|
---|
121 | * This indicates where the data begins.
|
---|
122 | * The TCP header (even one including options) is an integral number of 32~bits long.
|
---|
123 | */
|
---|
124 | uint8_t header_length:4;
|
---|
125 | #endif
|
---|
126 | #ifdef ARCH_IS_BIG_ENDIAN
|
---|
127 | /** Two bits reserved for future use.
|
---|
128 | * Must be zero.
|
---|
129 | */
|
---|
130 | uint8_t reserved2:2;
|
---|
131 | /** Urgent Pointer field significant.
|
---|
132 | * @see tcp_header:urgent_pointer
|
---|
133 | */
|
---|
134 | uint8_t urgent:1;
|
---|
135 | /** Acknowledgment field significant
|
---|
136 | * @see tcp_header:acknowledgement_number
|
---|
137 | */
|
---|
138 | uint8_t acknowledge:1;
|
---|
139 | /** Push function.
|
---|
140 | */
|
---|
141 | uint8_t push:1;
|
---|
142 | /** Reset the connection.
|
---|
143 | */
|
---|
144 | uint8_t reset:1;
|
---|
145 | /** Synchronize the sequence numbers.
|
---|
146 | */
|
---|
147 | uint8_t synchronize:1;
|
---|
148 | /** No more data from the sender.
|
---|
149 | */
|
---|
150 | uint8_t finalize:1;
|
---|
151 | #else
|
---|
152 | /** No more data from the sender.
|
---|
153 | */
|
---|
154 | uint8_t finalize:1;
|
---|
155 | /** Synchronize the sequence numbers.
|
---|
156 | */
|
---|
157 | uint8_t synchronize:1;
|
---|
158 | /** Reset the connection.
|
---|
159 | */
|
---|
160 | uint8_t reset:1;
|
---|
161 | /** Push function.
|
---|
162 | */
|
---|
163 | uint8_t push:1;
|
---|
164 | /** Acknowledgment field significant.
|
---|
165 | * @see tcp_header:acknowledgement_number
|
---|
166 | */
|
---|
167 | uint8_t acknowledge:1;
|
---|
168 | /** Urgent Pointer field significant.
|
---|
169 | * @see tcp_header:urgent_pointer
|
---|
170 | */
|
---|
171 | uint8_t urgent:1;
|
---|
172 | /** Two bits reserved for future use.
|
---|
173 | * Must be zero.
|
---|
174 | */
|
---|
175 | uint8_t reserved2:2;
|
---|
176 | #endif
|
---|
177 | /** The number of data octets beginning with the one indicated in the acknowledgment field which the sender of this segment is willing to accept.
|
---|
178 | * @see tcp_header:acknowledge
|
---|
179 | */
|
---|
180 | uint16_t window;
|
---|
181 | /** The checksum field is the 16~bit one's complement of the one's complement sum of all 16~bit words in the header and text.
|
---|
182 | * If a~segment contains an odd number of header and text octets to be checksummed, the last octet is padded on the right with zeros to form a~16~bit word for checksum purposes.
|
---|
183 | * The pad is not transmitted as part of the segment.
|
---|
184 | * While computing the checksum, the checksum field itself is replaced with zeros.
|
---|
185 | * The checksum also coves a~pseudo header conceptually.
|
---|
186 | * The pseudo header conceptually prefixed to the TCP header contains the source address, the destination address, the protocol, and the TCP length.
|
---|
187 | * This information gives protection against misrouted datagrams.
|
---|
188 | * If the computed checksum is zero, it is transmitted as all ones (the equivalent in one's complement arithmetic).
|
---|
189 | */
|
---|
190 | uint16_t checksum;
|
---|
191 | /** This field communicates the current value of the urgent pointer as a~positive offset from the sequence number in this segment.
|
---|
192 | * The urgent pointer points to the sequence number of the octet following the urgent data.
|
---|
193 | * This field is only be interpreted in segments with the URG control bit set.
|
---|
194 | * @see tcp_header:urgent
|
---|
195 | */
|
---|
196 | uint16_t urgent_pointer;
|
---|
197 | } __attribute__ ((packed));
|
---|
198 |
|
---|
199 | /** Transmission datagram header option.
|
---|
200 | */
|
---|
201 | struct tcp_option{
|
---|
202 | /** Option type.
|
---|
203 | */
|
---|
204 | uint8_t type;
|
---|
205 | /** Option length.
|
---|
206 | */
|
---|
207 | uint8_t length;
|
---|
208 | };
|
---|
209 |
|
---|
210 | /** Maximum segment size TCP option.
|
---|
211 | */
|
---|
212 | struct tcp_max_segment_size_option{
|
---|
213 | /** TCP option.
|
---|
214 | * @see TCPOPT_MAX_SEGMENT_SIZE
|
---|
215 | * @see TCPOPT_MAX_SEGMENT_SIZE_LENGTH
|
---|
216 | */
|
---|
217 | tcp_option_t option;
|
---|
218 | /** Maximum segment size in bytes.
|
---|
219 | */
|
---|
220 | uint16_t max_segment_size;
|
---|
221 | } __attribute__ ((packed));
|
---|
222 |
|
---|
223 | #endif
|
---|
224 |
|
---|
225 | /** @}
|
---|
226 | */
|
---|