libtins  3.4
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
icmp.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_ICMP_H
31 #define TINS_ICMP_H
32 
33 // Windows likes to define macros with not-so-common-names, which break
34 // this code
35 #ifdef _WIN32
36  #ifdef TIMESTAMP_REQUEST
37  #undef TIMESTAMP_REQUEST
38  #endif // TIMESTAMP_REQUEST
39 
40  #ifdef TIMESTAMP_REPLY
41  #undef TIMESTAMP_REPLY
42  #endif // TIMESTAMP_REPLY
43 #endif // _WIN32
44 
45 #include "macros.h"
46 #include "pdu.h"
47 #include "endianness.h"
48 #include "ip_address.h"
49 #include "icmp_extension.h"
50 
51 namespace Tins {
52 namespace Memory {
53 
54 class InputMemoryStream;
55 
56 } // Memory
57 
65 class TINS_API ICMP : public PDU {
66 public:
70  static const PDU::PDUType pdu_flag = PDU::ICMP;
71 
76 
79  enum Flags {
80  ECHO_REPLY = 0,
81  DEST_UNREACHABLE = 3,
82  SOURCE_QUENCH = 4,
83  REDIRECT = 5,
84  ECHO_REQUEST = 8,
85  TIME_EXCEEDED = 11,
86  PARAM_PROBLEM = 12,
87  TIMESTAMP_REQUEST = 13,
88  TIMESTAMP_REPLY = 14,
89  INFO_REQUEST = 15,
90  INFO_REPLY = 16,
91  ADDRESS_MASK_REQUEST = 17,
92  ADDRESS_MASK_REPLY = 18
93  };
94 
101  static metadata extract_metadata(const uint8_t *buffer, uint32_t total_sz);
102 
109  ICMP(Flags flag = ECHO_REQUEST);
110 
122  ICMP(const uint8_t* buffer, uint32_t total_sz);
123 
129  void code(uint8_t new_code);
130 
135  void type(Flags type);
136 
142  void id(uint16_t new_id);
143 
149  void sequence(uint16_t new_seq);
150 
156  void gateway(address_type new_gw);
157 
163  void mtu(uint16_t new_mtu);
164 
170  void pointer(uint8_t new_pointer);
171 
177  void original_timestamp(uint32_t new_timestamp);
178 
184  void receive_timestamp(uint32_t new_timestamp);
185 
191  void transmit_timestamp(uint32_t new_timestamp);
192 
198  void address_mask(address_type new_mask);
199 
206  void set_echo_request(uint16_t id, uint16_t seq);
207 
214  void set_echo_reply(uint16_t id, uint16_t seq);
215 
222  void set_info_request(uint16_t id, uint16_t seq);
223 
230  void set_info_reply(uint16_t id, uint16_t seq);
231 
235  void set_dest_unreachable();
236 
244  void set_time_exceeded(bool ttl_exceeded = true);
245 
254  void set_param_problem(bool set_pointer = false, uint8_t bad_octet = 0);
255 
259  void set_source_quench();
260 
268  void set_redirect(uint8_t icode, address_type address);
269 
275  Flags type() const {
276  return (Flags)header_.type;
277  }
278 
284  uint8_t code() const {
285  return header_.code;
286  }
287 
293  uint16_t checksum() const {
294  return Endian::be_to_host(header_.check);
295  }
296 
302  uint16_t id() const {
303  return Endian::be_to_host(header_.un.echo.id);
304  }
305 
311  uint16_t sequence() const {
312  return Endian::be_to_host(header_.un.echo.sequence);
313  }
314 
321  return address_type(Endian::be_to_host(header_.un.gateway));
322  }
323 
329  uint8_t pointer() const {
330  return header_.un.rfc4884.pointer;
331  }
332 
338  uint8_t length() const {
339  return header_.un.rfc4884.length;
340  }
341 
347  uint16_t mtu() const {
348  return Endian::be_to_host(header_.un.frag.mtu);
349  }
350 
356  uint32_t original_timestamp() const {
357  return Endian::be_to_host(orig_timestamp_or_address_mask_);
358  }
359 
365  uint32_t receive_timestamp() const {
366  return Endian::be_to_host(recv_timestamp_);
367  }
368 
374  uint32_t transmit_timestamp() const {
375  return Endian::be_to_host(trans_timestamp_);
376  }
377 
384  return address_type(Endian::be_to_host(orig_timestamp_or_address_mask_));
385  }
386 
395  uint32_t header_size() const;
396 
404  uint32_t trailer_size() const;
405 
413  bool matches_response(const uint8_t* ptr, uint32_t total_sz) const;
414 
421  return extensions_;
422  }
423 
430  return extensions_;
431  }
432 
436  bool has_extensions() const {
437  return !extensions_.extensions().empty();
438  }
439 
455  void use_length_field(bool value);
456 
462  PDUType pdu_type() const {
463  return pdu_flag;
464  }
465 
469  ICMP* clone() const {
470  return new ICMP(*this);
471  }
472 private:
473  TINS_BEGIN_PACK
474  struct icmp_header {
475  uint8_t type;
476  uint8_t code;
477  uint16_t check;
478  union {
479  struct {
480  uint16_t id;
481  uint16_t sequence;
482  } echo;
483  uint32_t gateway;
484  struct {
485  uint16_t unused;
486  uint16_t mtu;
487  } frag;
488  struct {
489  uint8_t pointer;
490  uint8_t length;
491  uint16_t unused;
492  } rfc4884;
493  } un;
494  } TINS_END_PACK;
495 
496  void checksum(uint16_t new_check);
497 
503  void write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU* parent);
504 
505  uint32_t get_adjusted_inner_pdu_size() const;
506  void try_parse_extensions(Memory::InputMemoryStream& stream);
507  bool are_extensions_allowed() const;
508 
509  icmp_header header_;
510  uint32_t orig_timestamp_or_address_mask_;
511  uint32_t recv_timestamp_;
512  uint32_t trans_timestamp_;
513  ICMPExtensionsStructure extensions_;
514 };
515 
516 } // Tins
517 
518 #endif // TINS_ICMP_H
uint8_t code() const
Getter for the ICMP code flag.
Definition: icmp.h:284
uint8_t pointer() const
Getter for the pointer field.
Definition: icmp.h:329
Class that represents an ICMP PDU.
Definition: icmp.h:65
ICMPExtensionsStructure & extensions()
Getter for the extensions field.
Definition: icmp.h:429
Flags
ICMP flags.
Definition: icmp.h:79
IPv4Address address_type
Definition: icmp.h:75
uint16_t id() const
Getter for the echo id.
Definition: icmp.h:302
PDUType
Enum which identifies each type of PDU.
Definition: pdu.h:128
ICMP * clone() const
Definition: icmp.h:469
address_type address_mask() const
Getter for the address mask field.
Definition: icmp.h:383
const ICMPExtensionsStructure & extensions() const
Getter for the extensions field.
Definition: icmp.h:420
bool has_extensions() const
Indicates whether this object contains ICMP extensions.
Definition: icmp.h:436
uint32_t receive_timestamp() const
Getter for the receive timestamp field.
Definition: icmp.h:365
PDUType pdu_type() const
Getter for the PDU's type.
Definition: icmp.h:462
uint32_t transmit_timestamp() const
Getter for the transmit timestamp field.
Definition: icmp.h:374
uint16_t checksum() const
Getter for the checksum field.
Definition: icmp.h:293
uint8_t length() const
Getter for the length field.
Definition: icmp.h:338
Flags type() const
Getter for the ICMP type flag.
Definition: icmp.h:275
address_type gateway() const
Getter for the gateway field.
Definition: icmp.h:320
uint16_t mtu() const
Getter for the mtu field.
Definition: icmp.h:347
Abstraction of an IPv4 address.
Definition: ip_address.h:44
uint16_t sequence() const
Getter for the echo sequence number.
Definition: icmp.h:311
Class that represents an ICMP extensions structure.
Definition: icmp_extension.h:162
Base class for protocol data units.
Definition: pdu.h:108
uint32_t original_timestamp() const
Getter for the original timestamp field.
Definition: icmp.h:356