@@ -43,16 +43,15 @@ namespace tcp {
4343class Packet : public PacketIP4 {
4444public:
4545
46- inline Header& tcp_header () const
46+ Header& tcp_header () const
4747 { return *(Header*) ip_data_ptr (); }
4848
4949 // ! initializes to a default, empty TCP packet, given
5050 // ! a valid MTU-sized buffer
5151 void init ()
5252 {
53-
5453 PacketIP4::init (Protocol::TCP);
55- Byte * ipdata = ip_data_ptr ();
54+ auto * ipdata = ip_data_ptr ();
5655
5756 // clear TCP header
5857 ((uint32_t *) ipdata)[3 ] = 0 ;
@@ -69,99 +68,99 @@ class Packet : public PacketIP4 {
6968 }
7069
7170 // GETTERS
72- inline port_t src_port () const
71+ port_t src_port () const
7372 { return ntohs (tcp_header ().source_port ); }
7473
75- inline port_t dst_port () const
74+ port_t dst_port () const
7675 { return ntohs (tcp_header ().destination_port ); }
7776
78- inline seq_t seq () const
77+ seq_t seq () const
7978 { return ntohl (tcp_header ().seq_nr ); }
8079
81- inline seq_t ack () const
80+ seq_t ack () const
8281 { return ntohl (tcp_header ().ack_nr ); }
8382
84- inline uint16_t win () const
83+ uint16_t win () const
8584 { return ntohs (tcp_header ().window_size ); }
8685
8786 uint16_t tcp_checksum () const noexcept
8887 { return tcp_header ().checksum ; }
8988
90- inline Socket source () const
89+ Socket source () const
9190 { return Socket{ip_src (), src_port ()}; }
9291
93- inline Socket destination () const
92+ Socket destination () const
9493 { return Socket{ip_dst (), dst_port ()}; }
9594
96- inline seq_t end () const
95+ seq_t end () const
9796 { return seq () + tcp_data_length (); }
9897
9998 // SETTERS
100- inline Packet& set_src_port (port_t p) {
99+ Packet& set_src_port (port_t p) {
101100 tcp_header ().source_port = htons (p);
102101 return *this ;
103102 }
104103
105- inline Packet& set_dst_port (port_t p) {
104+ Packet& set_dst_port (port_t p) {
106105 tcp_header ().destination_port = htons (p);
107106 return *this ;
108107 }
109108
110- inline Packet& set_seq (seq_t n) {
109+ Packet& set_seq (seq_t n) {
111110 tcp_header ().seq_nr = htonl (n);
112111 return *this ;
113112 }
114113
115- inline Packet& set_ack (seq_t n) {
114+ Packet& set_ack (seq_t n) {
116115 tcp_header ().ack_nr = htonl (n);
117116 return *this ;
118117 }
119118
120- inline Packet& set_win (uint16_t size) {
119+ Packet& set_win (uint16_t size) {
121120 tcp_header ().window_size = htons (size);
122121 return *this ;
123122 }
124123
125- inline Packet& set_checksum (uint16_t checksum) {
124+ Packet& set_checksum (uint16_t checksum) {
126125 tcp_header ().checksum = checksum;
127126 return *this ;
128127 }
129128
130- inline Packet& set_source (const Socket& src) {
129+ Packet& set_source (const Socket& src) {
131130 set_ip_src (src.address ()); // PacketIP4::set_src
132131 set_src_port (src.port ());
133132 return *this ;
134133 }
135134
136- inline Packet& set_destination (const Socket& dest) {
135+ Packet& set_destination (const Socket& dest) {
137136 set_ip_dst (dest.address ()); // PacketIP4::set_dst
138137 set_dst_port (dest.port ());
139138 return *this ;
140139 }
141140
142141 // / FLAGS / CONTROL BITS ///
143142
144- inline Packet& set_flag (Flag f) {
143+ Packet& set_flag (Flag f) {
145144 tcp_header ().offset_flags .whole |= htons (f);
146145 return *this ;
147146 }
148147
149- inline Packet& set_flags (uint16_t f) {
148+ Packet& set_flags (uint16_t f) {
150149 tcp_header ().offset_flags .whole |= htons (f);
151150 return *this ;
152151 }
153152
154- inline Packet& clear_flag (Flag f) {
153+ Packet& clear_flag (Flag f) {
155154 tcp_header ().offset_flags .whole &= ~ htons (f);
156155 return *this ;
157156 }
158157
159- inline Packet& clear_flags () {
158+ Packet& clear_flags () {
160159 tcp_header ().offset_flags .whole &= 0x00ff ;
161160 return *this ;
162161 }
163162
164- inline bool isset (Flag f) const
163+ bool isset (Flag f) const
165164 { return ntohs (tcp_header ().offset_flags .whole ) & f; }
166165
167166 // TCP::Flag flags() const { return (htons(tcp_header().offset_flags.whole) << 8) & 0xFF; }
@@ -170,33 +169,33 @@ class Packet : public PacketIP4 {
170169 // / OFFSET, OPTIONS, DATA ///
171170
172171 // Get the raw tcp offset, in quadruples
173- inline uint8_t offset () const
174- { return ( uint8_t )( tcp_header ().offset_flags .offset_reserved >> 4 ) ; }
172+ int offset () const
173+ { return tcp_header ().offset_flags .offset_reserved >> 4 ; }
175174
176175 // Set raw TCP offset in quadruples
177- inline void set_offset (uint8_t offset)
178- { tcp_header ().offset_flags .offset_reserved = (offset << 4 ) ; }
176+ void set_offset (int offset)
177+ { tcp_header ().offset_flags .offset_reserved = (offset & 0xF ) << 4 ; }
179178
180179 // The actual TCP header size (including options).
181- inline uint8_t tcp_header_length () const
180+ auto tcp_header_length () const
182181 { return offset () * 4 ; }
183182
184183 // The total length of the TCP segment (TCP header + data)
185184 uint16_t tcp_length () const
186185 { return tcp_header_length () + tcp_data_length (); }
187186
188187 // Where data starts
189- inline Byte* tcp_data ()
188+ Byte* tcp_data ()
190189 { return ip_data_ptr () + tcp_header_length (); }
191190
192- inline const Byte* tcp_data () const
191+ const Byte* tcp_data () const
193192 { return ip_data_ptr () + tcp_header_length (); }
194193
195194 // Length of data in packet when header has been accounted for
196- inline uint16_t tcp_data_length () const
195+ uint16_t tcp_data_length () const
197196 { return ip_data_length () - tcp_header_length (); }
198197
199- inline bool has_tcp_data () const
198+ bool has_tcp_data () const
200199 { return tcp_data_length () > 0 ; }
201200
202201 /* *
@@ -232,7 +231,11 @@ class Packet : public PacketIP4 {
232231 }
233232
234233 // update offset
235- set_offset (offset () + round_up (opt.length + Padding, 4 ));
234+ auto newoffset = offset () + round_up (opt.length + Padding, 4 );
235+ if (UNLIKELY (newoffset > 0xF )) {
236+ throw std::runtime_error (" Too many TCP options" );
237+ }
238+ set_offset (newoffset);
236239
237240 set_length (); // update
238241 }
@@ -255,29 +258,35 @@ class Packet : public PacketIP4 {
255258 new (addr) T (args...);
256259
257260 // update offset
258- set_offset ( offset () + (sizeof (T) / 4 ) );
261+ auto newoffset = offset () + (sizeof (T) / 4 );
259262
263+ set_offset (newoffset);
264+ if (UNLIKELY (newoffset > 0xF )) {
265+ throw std::runtime_error (" Too many TCP options" );
266+ }
260267 set_length (); // update
261268 }
262269
263- inline void clear_options () {
270+ void clear_options () {
264271 // clear existing options
265- // move data (if any) (??)
272+ if (UNLIKELY (tcp_data_length () > 0 )) {
273+ throw std::runtime_error (" Can't clear options on TCP packet with data" );
274+ }
266275 set_offset (5 );
267276 set_length (); // update
268277 }
269278
270279 // Options
271- inline uint8_t * tcp_options ()
280+ uint8_t * tcp_options ()
272281 { return (uint8_t *) tcp_header ().options ; }
273282
274- inline const uint8_t * tcp_options () const
283+ const uint8_t * tcp_options () const
275284 { return (const uint8_t *) tcp_header ().options ; }
276285
277- inline uint8_t tcp_options_length () const
286+ int tcp_options_length () const
278287 { return tcp_header_length () - sizeof (Header); }
279288
280- inline bool has_tcp_options () const
289+ bool has_tcp_options () const
281290 { return tcp_options_length () > 0 ; }
282291
283292
0 commit comments