headers.hpp
Go to the documentation of this file.
1 #ifndef HEADERS_HPP
2 #define HEADERS_HPP
3 
4 #include <array>
5 #include <cstdint>
6 #include <cstring>
7 #include <sstream>
8 #include <string>
9 
10 #include <arpa/inet.h>
11 
12 namespace Headers {
13 
15 struct Ethernet {
16  std::array<uint8_t, 6> destMac;
17  std::array<uint8_t, 6> srcMac;
18  uint16_t ethertype;
19 
23  void *getPayload() {
24  return reinterpret_cast<void *>(
25  reinterpret_cast<uint8_t *>(this) + sizeof(struct Ethernet));
26  }
27 
28  static std::string addrToStr(std::array<uint8_t, 6> addr) {
29  std::stringstream str;
30  str << std::hex << static_cast<int>(addr[0]) << ":" << std::hex
31  << static_cast<int>(addr[1]) << ":" << std::hex << static_cast<int>(addr[2])
32  << ":" << std::hex << static_cast<int>(addr[3]) << ":" << std::hex
33  << static_cast<int>(addr[4]) << ":" << std::hex << static_cast<int>(addr[5]);
34  return str.str();
35  }
36 
37  std::string getSrcAddr() { return addrToStr(this->srcMac); }
38 
39  std::string getDstAddr() { return addrToStr(this->destMac); }
40 
44  uint16_t getEthertype() { return ntohs(this->ethertype); }
45 
46  static constexpr uint16_t ETHERTYPE_IPv4 = 0x0800;
47  static constexpr uint16_t ETHERTYPE_IPv6 = 0x86dd;
48 
52  void setEthertype(uint16_t type) { this->ethertype = htons(type); }
53 
54  void setSrcAddr(const std::array<uint8_t, 6> addr) {
55  memcpy(this->srcMac.data(), addr.data(), 6);
56  }
57 
58  void setDstAddr(std::array<uint8_t, 6> addr) {
59  memcpy(this->destMac.data(), addr.data(), 6);
60  }
61 
62 } __attribute__((packed));
63 
65 struct IPv4 {
66  uint8_t version_ihl;
67 
68  uint8_t version() const { return (version_ihl & 0xf0) >> 4; }
69  uint8_t ihl() const { return version_ihl & 0x0f; }
70 
73  void setVersion() {
74  version_ihl &= 0x0f;
75  version_ihl |= 0x40;
76  }
77 
81  void setIHL(uint8_t len) {
82  version_ihl &= 0xf0;
83  len &= 0x0f;
84  version_ihl |= len;
85  }
86 
87  uint8_t tos;
88  uint16_t total_length;
89 
93  void setPayloadLength(uint16_t len) { total_length = htons(ihl() * 4 + len); }
94 
98  uint16_t getPayloadLength() const { return ntohl(total_length) - ihl() * 4; }
99 
100  uint16_t id;
102 
103  uint16_t fragmentation() const { return flags_fragmentation & 0x1fff; }
104  uint16_t flags() const { return flags_fragmentation >> 18; }
105 
106  uint8_t ttl;
107  uint8_t proto;
108 
109  static constexpr uint8_t PROTO_ICMP = 1;
110  static constexpr uint8_t PROTO_TCP = 6;
111  static constexpr uint8_t PROTO_UDP = 17;
112 
115 
118 
121 
122  uint16_t checksum;
123  uint32_t srcIP;
124  uint32_t dstIP;
125 
129  void setSrcIP(uint32_t ip) { srcIP = htonl(ip); }
130 
134  void setDstIP(uint32_t ip) { dstIP = htonl(ip); }
135 
139  uint32_t getSrcIP() const { return ntohl(srcIP); }
140 
144  uint32_t getDstIP() const { return ntohl(dstIP); }
145 
149  void *getPayload() {
150  return reinterpret_cast<void *>(reinterpret_cast<uint8_t *>(this) + 4 * ihl());
151  }
152 
153  static std::string addrToStr(uint32_t addr) {
154  std::stringstream str;
155  str << (addr >> 24) << "." << ((addr >> 16) & 0xff) << "." << ((addr >> 8) & 0xff)
156  << "." << (addr & 0xff);
157  return str.str();
158  }
159 
160  std::string getSrcAddr() { return addrToStr(this->srcIP); }
161 
162  std::string getDstAddr() { return addrToStr(this->dstIP); }
163 
164  void setLength(uint16_t len) { this->total_length = htons(len); }
165 
166  uint16_t getLength() { return ntohs(this->total_length); }
167 
169  void calcChecksum() {
170  uint32_t result = 0;
171  uint16_t *hdr_cast = reinterpret_cast<uint16_t *>(this);
172 
173  this->checksum = 0;
174  for (uint8_t i = 0; i < (this->ihl() * 4); i++) {
175  result += ntohs(hdr_cast[i]);
176  if (result & (1 << 16)) {
177  result &= 0xffff;
178  result++;
179  }
180  }
181 
182  this->checksum = htons(~result);
183  };
184 
185 } __attribute__((packed));
186 
188 struct Tcp {
189  uint16_t srcPort;
190  uint16_t dstPort;
191  uint32_t seq;
192  uint32_t ack;
193  uint16_t offset_flags;
194  uint16_t window;
195  uint16_t checksum;
196  uint16_t urgend_ptr;
197 
201  void *getPayload() {
202  return reinterpret_cast<void *>(
203  reinterpret_cast<uint8_t *>(this) + sizeof(struct Tcp));
204  }
205 
206 } __attribute__((packed));
207 
209 struct Udp {
210  uint16_t srcPort;
211  uint16_t dstPort;
212  uint16_t len;
213  uint16_t checksum;
214 
218  void setSrcPort(uint16_t p) { srcPort = htons(p); }
219 
223  void setDstPort(uint16_t p) { dstPort = htons(p); }
224 
228  uint16_t getSrcPort() const { return ntohs(srcPort); }
229 
233  uint16_t getDstPort() const { return ntohs(dstPort); }
234 
238  uint16_t getPayloadLength() const { return ntohs(len) - sizeof(struct Udp); }
239 
243  void setPayloadLength(uint16_t length) { len = htons(length + sizeof(struct Udp)); }
244 
248  void *getPayload() {
249  return reinterpret_cast<void *>(
250  reinterpret_cast<uint8_t *>(this) + sizeof(struct Udp));
251  }
252 
253 } __attribute__((packed));
254 
256 struct Icmp {
257  uint8_t type;
258  uint8_t code;
259  uint16_t checksum;
260 } __attribute__((packed));
261 
262 } // namespace Headers
263 
264 #endif /* HEADERS_HPP */
uint16_t urgend_ptr
Urgent pointer.
Definition: headers.hpp:196
uint16_t checksum
header checksum
Definition: headers.hpp:122
uint16_t ethertype
Ethertype.
Definition: headers.hpp:18
void setSrcIP(uint32_t ip)
Set the source ip.
Definition: headers.hpp:129
static constexpr uint16_t ETHERTYPE_IPv4
Definition: headers.hpp:46
void setProtoUDP()
Set the protocol to UDP.
Definition: headers.hpp:120
Representation of the IPv4 header.
Definition: headers.hpp:65
uint32_t dstIP
destination IPv4 address
Definition: headers.hpp:124
void * getPayload()
Get the SDU.
Definition: headers.hpp:149
uint8_t ttl
Time to live.
Definition: headers.hpp:106
void setSrcPort(uint16_t p)
Set the source port.
Definition: headers.hpp:218
void setProtoICMP()
Set the protocol to ICMP.
Definition: headers.hpp:114
uint8_t tos
Type of Service.
Definition: headers.hpp:87
void setDstIP(uint32_t ip)
Set the destination ip.
Definition: headers.hpp:134
uint16_t getSrcPort() const
Get the source port.
Definition: headers.hpp:228
void setDstAddr(std::array< uint8_t, 6 > addr)
Definition: headers.hpp:58
uint16_t dstPort
Destination port.
Definition: headers.hpp:190
static constexpr uint16_t ETHERTYPE_IPv6
Definition: headers.hpp:47
Representation of the etherner header.
Definition: headers.hpp:15
void setProtoTCP()
Set the protocol to TCP.
Definition: headers.hpp:117
uint16_t getLength()
Definition: headers.hpp:166
uint32_t srcIP
source IPv4 address
Definition: headers.hpp:123
uint16_t flags_fragmentation
flags and fragmentation offset
Definition: headers.hpp:101
void setIHL(uint8_t len)
Sets the IP header length.
Definition: headers.hpp:81
std::string getSrcAddr()
Definition: headers.hpp:160
static constexpr uint8_t PROTO_TCP
Definition: headers.hpp:110
std::string getSrcAddr()
Definition: headers.hpp:37
void setDstPort(uint16_t p)
Set the destination port.
Definition: headers.hpp:223
uint16_t id
Identification.
Definition: headers.hpp:100
std::array< uint8_t, 6 > destMac
Destination MAC.
Definition: headers.hpp:16
uint16_t getDstPort() const
Get the destination port.
Definition: headers.hpp:233
uint16_t fragmentation() const
Definition: headers.hpp:103
void setSrcAddr(const std::array< uint8_t, 6 > addr)
Definition: headers.hpp:54
Representation of the UDP header.
Definition: headers.hpp:209
void * getPayload()
Get the SDU.
Definition: headers.hpp:248
uint16_t getEthertype()
Get the ethertype.
Definition: headers.hpp:44
uint8_t code
Code.
Definition: headers.hpp:258
uint16_t checksum
Checksum.
Definition: headers.hpp:259
uint32_t ack
Acknowledgement number.
Definition: headers.hpp:192
void setPayloadLength(uint16_t len)
Set the length of the L3-SDU.
Definition: headers.hpp:93
std::string getDstAddr()
Definition: headers.hpp:39
uint8_t type
Type.
Definition: headers.hpp:257
uint8_t ihl() const
Definition: headers.hpp:69
uint16_t srcPort
Source port.
Definition: headers.hpp:210
uint16_t getPayloadLength() const
Get the length of the L3-SDU.
Definition: headers.hpp:98
void calcChecksum()
Fill out the header checksum for this packet.
Definition: headers.hpp:169
uint8_t proto
next protocol
Definition: headers.hpp:107
static std::string addrToStr(std::array< uint8_t, 6 > addr)
Definition: headers.hpp:28
uint16_t total_length
L3-PDU length.
Definition: headers.hpp:88
static constexpr uint8_t PROTO_UDP
Definition: headers.hpp:111
uint32_t getDstIP() const
Get the destination IP.
Definition: headers.hpp:144
void setPayloadLength(uint16_t length)
Set the length of the L4-SDU (UDP payload)
Definition: headers.hpp:243
uint16_t dstPort
Destination port.
Definition: headers.hpp:211
uint8_t version_ihl
Version and IHL.
Definition: headers.hpp:66
void setEthertype(uint16_t type)
Set the ethertype.
Definition: headers.hpp:52
uint32_t seq
Sequence number.
Definition: headers.hpp:191
static std::string addrToStr(uint32_t addr)
Definition: headers.hpp:153
std::string getDstAddr()
Definition: headers.hpp:162
uint32_t getSrcIP() const
Get the source IP.
Definition: headers.hpp:139
uint16_t checksum
Checksum.
Definition: headers.hpp:195
uint16_t srcPort
Source port.
Definition: headers.hpp:189
uint16_t len
Length.
Definition: headers.hpp:212
Representation of the ICMP header.
Definition: headers.hpp:256
uint8_t version() const
Definition: headers.hpp:68
void setLength(uint16_t len)
Definition: headers.hpp:164
uint16_t checksum
Checksum.
Definition: headers.hpp:213
uint16_t getPayloadLength() const
Get the length of the L4-SDU (UDP payload)
Definition: headers.hpp:238
uint16_t offset_flags
Data offset and flags.
Definition: headers.hpp:193
std::array< uint8_t, 6 > srcMac
Source MAC.
Definition: headers.hpp:17
uint16_t flags() const
Definition: headers.hpp:104
void * getPayload()
Get the SDU.
Definition: headers.hpp:201
Representation of the TCP header.
Definition: headers.hpp:188
static constexpr uint8_t PROTO_ICMP
Definition: headers.hpp:109
void setVersion()
Set the version field to 4.
Definition: headers.hpp:73
void * getPayload()
Get the SDU.
Definition: headers.hpp:23
uint16_t window
Receive window.
Definition: headers.hpp:194