libtins  3.4
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
tcp.h
1 /*
2  * Copyright (c) 2016, Matias Fontanini
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 are
7  * 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
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * 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
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 #ifndef TINS_TCP_H
31 #define TINS_TCP_H
32 
33 
34 #include <list>
35 #include <vector>
36 #include <stdint.h>
37 #include <stdexcept>
38 #include <utility>
39 #include "pdu.h"
40 #include "macros.h"
41 #include "endianness.h"
42 #include "small_uint.h"
43 #include "pdu_option.h"
44 #include "cxxstd.h"
45 
46 namespace Tins {
47 namespace Memory {
48 class OutputMemoryStream;
49 } // Memory
50 
79 class TINS_API TCP : public PDU {
80 public:
84  static const PDU::PDUType pdu_flag = PDU::TCP;
85 
91  enum Flags {
92  FIN = 1,
93  SYN = 2,
94  RST = 4,
95  PSH = 8,
96  ACK = 16,
97  URG = 32,
98  ECE = 64,
99  CWR = 128
100  };
101 
107  enum OptionTypes {
108  EOL = 0,
109  NOP = 1,
110  MSS = 2,
111  WSCALE = 3,
112  SACK_OK = 4,
113  SACK = 5,
114  TSOPT = 8,
115  ALTCHK = 14
116  };
117 
122  CHK_TCP,
123  CHK_8FLETCHER,
124  CHK_16FLETCHER
125  };
126 
131 
135  typedef std::list<option> options_type;
136 
140  typedef std::vector<uint32_t> sack_type;
141 
148  static metadata extract_metadata(const uint8_t *buffer, uint32_t total_sz);
149 
159  TCP(uint16_t dport = 0, uint16_t sport = 0);
160 
173  TCP(const uint8_t* buffer, uint32_t total_sz);
174 
180  uint16_t dport() const {
181  return Endian::be_to_host(header_.dport);
182  }
183 
189  uint16_t sport() const {
190  return Endian::be_to_host(header_.sport);
191  }
192 
198  uint32_t seq() const {
199  return Endian::be_to_host(header_.seq);
200  }
201 
207  uint32_t ack_seq() const {
208  return Endian::be_to_host(header_.ack_seq);
209  }
210 
216  uint16_t window() const {
217  return Endian::be_to_host(header_.window);
218  }
219 
225  uint16_t checksum() const {
226  return Endian::be_to_host(header_.check);
227  }
228 
234  uint16_t urg_ptr() const {
235  return Endian::be_to_host(header_.urg_ptr);
236  }
237 
244  return this->header_.doff;
245  }
246 
252  const options_type& options() const {
253  return options_;
254  }
255 
279  small_uint<1> get_flag(Flags tcp_flag) const;
280 
298  small_uint<12> flags() const;
299 
300  /* Setters */
301 
307  void dport(uint16_t new_dport);
308 
314  void sport(uint16_t new_sport);
315 
321  void seq(uint32_t new_seq);
322 
328  void ack_seq(uint32_t new_ack_seq);
329 
335  void window(uint16_t new_window);
336 
342  void urg_ptr(uint16_t new_urg_ptr);
343 
349  void data_offset(small_uint<4> new_doff);
350 
351  // Options
352 
358  void mss(uint16_t value);
359 
365  uint16_t mss() const;
366 
372  void winscale(uint8_t value);
373 
379  uint8_t winscale() const;
380 
384  void sack_permitted();
385 
390  bool has_sack_permitted() const;
391 
397  void sack(const sack_type& edges);
398 
404  sack_type sack() const;
405 
412  void timestamp(uint32_t value, uint32_t reply);
413 
420  std::pair<uint32_t, uint32_t> timestamp() const;
421 
427  void altchecksum(AltChecksums value);
428 
434  AltChecksums altchecksum() const;
435 
442  void set_flag(Flags tcp_flag, small_uint<1> value);
443 
462  void flags(small_uint<12> value);
463 
464 
470  void add_option(const option& opt);
471 
472  #if TINS_IS_CXX11
473 
480  void add_option(option &&opt) {
481  internal_add_option(opt);
482  options_.push_back(std::move(opt));
483  }
484 
493  template <typename... Args>
494  void add_option(Args&&... args) {
495  options_.emplace_back(std::forward<Args>(args)...);
496  internal_add_option(options_.back());
497  }
498  #endif
499 
509  bool remove_option(OptionTypes type);
510 
519  uint32_t header_size() const;
520 
528  bool matches_response(const uint8_t* ptr, uint32_t total_sz) const;
529 
535  PDUType pdu_type() const {
536  return pdu_flag;
537  }
538 
544  const option* search_option(OptionTypes type) const;
545 
549  TCP* clone() const {
550  return new TCP(*this);
551  }
552 private:
553  #if TINS_IS_LITTLE_ENDIAN
554  TINS_BEGIN_PACK
555  struct flags_type {
556  uint8_t fin:1,
557  syn:1,
558  rst:1,
559  psh:1,
560  ack:1,
561  urg:1,
562  ece:1,
563  cwr:1;
564  } TINS_END_PACK;
565  #else
566  TINS_BEGIN_PACK
567  struct flags_type {
568  uint8_t cwr:1,
569  ece:1,
570  urg:1,
571  ack:1,
572  psh:1,
573  rst:1,
574  syn:1,
575  fin:1;
576  } TINS_END_PACK;
577  #endif
578 
579  TINS_BEGIN_PACK
580  struct tcp_header {
581  uint16_t sport;
582  uint16_t dport;
583  uint32_t seq;
584  uint32_t ack_seq;
585  #if TINS_IS_LITTLE_ENDIAN
586  uint8_t res1:4,
587  doff:4;
588  #else
589  uint8_t doff:4,
590  res1:4;
591  #endif
592  union {
593  flags_type flags;
594  uint8_t flags_8;
595  };
596  uint16_t window;
597  uint16_t check;
598  uint16_t urg_ptr;
599  } TINS_END_PACK;
600 
601  static const uint16_t DEFAULT_WINDOW;
602 
603  template <typename T>
604  T generic_search(OptionTypes opt_type) const {
605  const option* opt = search_option(opt_type);
606  if (!opt) {
607  throw option_not_found();
608  }
609  return opt->to<T>();
610  }
611 
612  void internal_add_option(const option& option);
613  void write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU* parent);
614  void checksum(uint16_t new_check);
615  void update_options_size();
616  options_type::const_iterator search_option_iterator(OptionTypes type) const;
617  options_type::iterator search_option_iterator(OptionTypes type);
618 
619  void write_option(const option& opt, Memory::OutputMemoryStream& stream);
620 
621  tcp_header header_;
622  uint16_t options_size_, total_options_size_;
623  options_type options_;
624 };
625 
626 } // Tins
627 
628 #endif // TINS_TCP_H
uint16_t sport() const
Getter for the source port field.
Definition: tcp.h:189
Represents a TCP PDU.
Definition: tcp.h:79
std::list< option > options_type
Definition: tcp.h:135
OptionTypes
TCP options enum.
Definition: tcp.h:107
PDUType
Enum which identifies each type of PDU.
Definition: pdu.h:128
uint32_t seq() const
Getter for the sequence number field.
Definition: tcp.h:198
small_uint< 4 > data_offset() const
Getter for the data offset field.
Definition: tcp.h:243
uint16_t checksum() const
Getter for the checksum field.
Definition: tcp.h:225
Represents a PDU option field.
Definition: pdu_option.h:320
void add_option(option &&opt)
Adds a TCP option.
Definition: tcp.h:480
PDUOption< uint8_t, TCP > option
Definition: tcp.h:130
TCP * clone() const
Definition: tcp.h:549
std::vector< uint32_t > sack_type
Definition: tcp.h:140
void add_option(Args &&...args)
Adds a TCP option using the provided arguments.
Definition: tcp.h:494
uint32_t ack_seq() const
Getter for the acknowledge number field.
Definition: tcp.h:207
Type used to store a PDU header's data.
Definition: pdu.h:195
uint16_t urg_ptr() const
Getter for the urgent pointer field.
Definition: tcp.h:234
Flags
TCP flags enum.
Definition: tcp.h:91
uint16_t dport() const
Getter for the destination port field.
Definition: tcp.h:180
const options_type & options() const
Getter for the option list.
Definition: tcp.h:252
uint16_t window() const
Getter for the window size field.
Definition: tcp.h:216
AltChecksums
Alternate checksum enum.
Definition: tcp.h:121
Base class for protocol data units.
Definition: pdu.h:108
PDUType pdu_type() const
Getter for the PDU's type.
Definition: tcp.h:535