34 #include <tins/small_uint.h> 35 #include <tins/endianness.h> 36 #include <tins/ip_address.h> 37 #include <tins/pdu_option.h> 38 #include <tins/macros.h> 39 #include <tins/cxxstd.h> 44 class OutputMemoryStream;
63 class TINS_API
IP :
public PDU {
127 #if TINS_IS_LITTLE_ENDIAN 131 #elif TINS_IS_BIG_ENDIAN 142 #if TINS_IS_LITTLE_ENDIAN 143 : number(0), op_class(0), copied(0) {}
145 : copied(0), op_class(0), number(0) {}
158 #if TINS_IS_LITTLE_ENDIAN 159 : number(value & 0x1f),
160 op_class((value >> 5) & 0x03),
161 copied((value >> 7) & 0x01) {}
162 #elif TINS_IS_BIG_ENDIAN 163 : copied((value >> 7) & 0x01),
164 op_class((value >> 5) & 0x03),
165 number(value & 0x1f) {}
176 #if TINS_IS_LITTLE_ENDIAN 177 : number(number), op_class(op_class), copied(copied) {}
179 : copied(copied), op_class(op_class), number(number) {}
186 return number == rhs.number && op_class == rhs.op_class && copied == rhs.copied;
199 uint16_t security, compartments;
200 uint16_t handling_restrictions;
205 uint16_t hand_res = 0,
207 : security(sec), compartments(comp),
208 handling_restrictions(hand_res), transmission_control(tcc) { }
217 typedef std::vector<address_type> routes_type;
223 : pointer(ptr), routes(rts) {}
254 static metadata extract_metadata(
const uint8_t *buffer, uint32_t total_sz);
266 IP(address_type ip_dst = address_type(),
267 address_type ip_src = address_type());
280 IP(
const uint8_t* buffer, uint32_t total_sz);
290 return this->header_.ihl;
308 return Endian::be_to_host(header_.tot_len);
316 uint16_t
id()
const {
317 return Endian::be_to_host(header_.id);
331 return Endian::be_to_host(header_.frag_off);
343 return Endian::be_to_host(header_.frag_off) & 0x1fff;
352 return static_cast<Flags>(Endian::be_to_host(header_.frag_off) >> 13);
370 return header_.protocol;
379 return Endian::be_to_host(header_.check);
388 return address_type(header_.saddr);
396 return address_type(header_.daddr);
404 return header_.version;
422 void tos(uint8_t new_tos);
429 void id(uint16_t new_id);
441 TINS_DEPRECATED(
void frag_off(uint16_t new_frag_off));
459 void flags(
Flags new_flags);
466 void ttl(uint8_t new_ttl);
484 void protocol(uint8_t new_protocol);
491 void src_addr(address_type ip);
498 void dst_addr(address_type ip);
515 void add_option(
const option& opt);
526 options_.push_back(std::move(opt));
537 template<
typename... Args>
539 options_.emplace_back(std::forward<Args>(args)...);
589 void lsrr(
const lsrr_type& data) {
590 add_route_option(131, data);
598 void ssrr(
const ssrr_type& data) {
599 add_route_option(137, data);
608 add_route_option(7, data);
616 void stream_identifier(uint16_t stream_id);
640 return search_route_option(131);
653 return search_route_option(137);
665 return search_route_option(7);
676 uint16_t stream_identifier()
const;
685 uint32_t header_size()
const;
699 bool matches_response(
const uint8_t* ptr, uint32_t total_sz)
const;
714 bool is_fragmented()
const;
728 return new IP(*
this);
731 static const uint8_t DEFAULT_TTL;
735 #if TINS_IS_LITTLE_ENDIAN 754 void tot_len(uint16_t new_tot_len);
756 void prepare_for_serialize();
757 uint32_t calculate_options_size()
const;
758 uint32_t pad_options_size(uint32_t size)
const;
759 void init_ip_fields();
760 void write_serialization(uint8_t* buffer, uint32_t total_sz);
761 void write_option(
const option& opt, Memory::OutputMemoryStream& stream);
764 void checksum(uint16_t new_check);
768 options_type options_;
option_identifier()
Default constructor.
Definition: ip.h:141
address_type src_addr() const
Getter for the source address field.
Definition: ip.h:387
Flags flags() const
Getter for the flags field.
Definition: ip.h:351
Class that represents an IP PDU.
Definition: ip.h:63
generic_route_option_type lsrr_type
Definition: ip.h:231
generic_route_option_type ssrr_type
Definition: ip.h:236
PDUType pdu_type() const
Getter for the PDU's type.
Definition: ip.h:720
small_uint< 4 > head_len() const
Getter for the header length field.
Definition: ip.h:289
PDUType
Enum which identifies each type of PDU.
Definition: pdu.h:127
void lsrr(const lsrr_type &data)
Adds a Loose Source and Record Route option.
Definition: ip.h:589
TINS_DEPRECATED(uint16_t frag_off() const)
Getter for the fragment offset field.
Definition: ip.h:330
PDUOption< option_identifier, IP > option
Definition: ip.h:193
lsrr_type lsrr() const
Searchs and returns a Loose Source and Record Route option.
Definition: ip.h:639
void ssrr(const ssrr_type &data)
Adds a Strict Source and Record Route option.
Definition: ip.h:598
uint16_t tot_len() const
Getter for the total length field.
Definition: ip.h:307
Sends packets through a network interface.
Definition: packet_sender.h:116
Represents a PDU option field.
Definition: pdu_option.h:201
const options_type & options() const
Getter for the IP options.
Definition: ip.h:411
IPv4Address address_type
Definition: ip.h:73
ssrr_type ssrr() const
Searchs and returns a Strict Source and Record Route option.
Definition: ip.h:652
IP * clone() const
Definition: ip.h:727
uint16_t id() const
Getter for the id field.
Definition: ip.h:316
OptionClass
Enum indicating the option's class.
Definition: ip.h:90
The Tins namespace.
Definition: address_range.h:38
std::vector< option > options_type
Definition: ip.h:246
uint8_t tos() const
Getter for the type of service field.
Definition: ip.h:298
bool operator==(const option_identifier &rhs) const
Equality operator.
Definition: ip.h:185
Abstraction of a network interface.
Definition: network_interface.h:47
uint8_t protocol() const
Getter for the protocol field.
Definition: ip.h:369
Abstraction of an IPv4 address.
Definition: ip_address.h:45
address_type dst_addr() const
Getter for the destination address field.
Definition: ip.h:395
small_uint< 4 > version() const
Getter for the version field.
Definition: ip.h:403
small_uint< 13 > fragment_offset() const
Getter for the fragment offset field.
Definition: ip.h:342
void add_option(option &&opt)
Adds an IP option.
Definition: ip.h:525
generic_route_option_type record_route_type
Definition: ip.h:241
The type used to represent an option's type.
Definition: ip.h:126
void record_route(const record_route_type &data)
Adds a Record Route option.
Definition: ip.h:607
OptionNumber
Enum indicating the option's id number.
Definition: ip.h:100
uint16_t checksum() const
Getter for the checksum field.
Definition: ip.h:378
Base class for protocol data units.
Definition: pdu.h:107
void add_option(Args &&...args)
Adds an IP option.
Definition: ip.h:538
uint8_t ttl() const
Getter for the time to live field.
Definition: ip.h:360
record_route_type record_route() const
Searchs and returns a Record Route option.
Definition: ip.h:664