/ src / nodereply.hpp
nodereply.hpp
 1  // SPDX-FileCopyrightText: Copyright (C) 2024-2025 Marek Küthe <m.k@mk16.de>
 2  //
 3  // SPDX-License-Identifier: GPL-3.0-or-later
 4  
 5  #ifndef NODEREPLY_HPP
 6  #define NODEREPLY_HPP
 7  
 8  #include <iomanip>
 9  #include <ostream>
10  #include <string>
11  #include <stdexcept>
12  #include <cstdint>
13  #include <tins/tins.h>
14  
15  namespace crazytrace
16  {
17      enum class NodeReplyType
18      {
19          NOREPLY, /* No reply is to be sent. */
20          ICMP_ECHO_REPLY, /* An ICMP ECHO REPLY packet is sent in response. */
21          ICMP_TIME_EXCEEDED_ICMP_ECHO_REQUEST, /* An ICMP TIME EXCEEDED packet is
22                                                   sent in response to an ICMP
23                                                   ECHO_REQUEST packet. */
24          ICMP_PORT_UNREACHABLE, /* An ICMP PORT UNREACHABLE packet is sent in
25                                    response. */
26          ICMP_TIME_EXCEEDED_UDP, /* An ICMP TIME EXCEEDED packet is sent in
27                                     response to an UDP packet. */
28          ICMP_NDP /* A neighbor advertisement is sent. */
29      };
30  
31      class NodeReply
32      {
33          public:
34              explicit NodeReply(NodeReplyType type);
35              explicit NodeReply(NodeReplyType type,
36                                 Tins::HWAddress<6> destination_mac,
37                                 Tins::IPv6Address destination_address,
38                                 Tins::HWAddress<6> source_mac,
39                                 Tins::IPv6Address source_address);
40  
41              void set_hoplimit(uint8_t hoplimit);
42              void icmp_echo_reply(uint16_t icmp_identifier,
43                                   uint16_t icmp_sequence,
44                                   const Tins::RawPDU::payload_type& payload);
45              void udp_response(const Tins::RawPDU::payload_type& payload,
46                                uint16_t udp_dport,
47                                uint16_t udp_sport);
48              void packet_reassembly(
49                  Tins::IPv6Address original_destination_address);
50  
51              [[nodiscard]] std::string to_packet() const;
52              [[nodiscard]] NodeReplyType get_type() const noexcept;
53  
54              bool operator==(const NodeReply& other) const;
55              friend std::ostream& operator<<(std::ostream& os,
56                                              const NodeReply& nodereply);
57  
58          private:
59              NodeReplyType _type;
60              Tins::HWAddress<6> _destination_mac;
61              Tins::IPv6Address _destination_address;
62              Tins::HWAddress<6> _source_mac;
63              Tins::IPv6Address _source_address; /* Also used for ICMP_NDP */
64  
65              uint8_t _hoplimit;
66  
67              /* ICMP ECHO REPLY */
68              uint16_t _icmp_identifier;
69              uint16_t _icmp_sequence;
70  
71              /* ICMP_PORT_UNREACHABLE */
72              Tins::RawPDU::payload_type /* aka std::vector<uint8_t> */ _payload;
73              uint16_t _udp_dport;
74              uint16_t _udp_sport;
75  
76              /* ICMP TIME EXCEEDED */
77              Tins::IPv6Address _original_destination_address;
78      };
79  
80      std::ostream& operator<<(std::ostream& os, const NodeReply& nodereply);
81  } // namespace crazytrace
82  
83  #endif