libtins  4.0
udp.h
1 /*
2  * Copyright (c) 2017, 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_UDP_H
31 #define TINS_UDP_H
32 
33 #include <tins/macros.h>
34 #include <tins/pdu.h>
35 #include <tins/endianness.h>
36 
37 namespace Tins {
38 
63 class TINS_API UDP : public PDU {
64 public:
68  static const PDU::PDUType pdu_flag = PDU::UDP;
69 
76  static metadata extract_metadata(const uint8_t *buffer, uint32_t total_sz);
77 
87  UDP(uint16_t dport = 0, uint16_t sport = 0);
88 
100  UDP(const uint8_t* buffer, uint32_t total_sz);
101 
106  uint16_t dport() const {
107  return Endian::be_to_host(header_.dport);
108  }
109 
114  uint16_t sport() const {
115  return Endian::be_to_host(header_.sport);
116  }
117 
122  uint16_t length() const {
123  return Endian::be_to_host(header_.len);
124  }
125 
130  uint16_t checksum() const {
131  return Endian::be_to_host(header_.check);
132  }
133 
138  void dport(uint16_t new_dport);
139 
145  void sport(uint16_t new_sport);
146 
151  void length(uint16_t new_len);
152 
163  bool matches_response(const uint8_t* ptr, uint32_t total_sz) const;
164 
171  uint32_t header_size() const;
172 
177  PDUType pdu_type() const { return PDU::UDP; }
178 
182  UDP* clone() const {
183  return new UDP(*this);
184  }
185 private:
186  TINS_BEGIN_PACK
187  struct udp_header {
188  uint16_t sport;
189  uint16_t dport;
190  uint16_t len;
191  uint16_t check;
192  } TINS_END_PACK;
193 
194  void write_serialization(uint8_t* buffer, uint32_t total_sz);
195 
196  udp_header header_;
197 };
198 
199 } // Tins
200 
201 #endif // TINS_UDP_H
uint16_t checksum() const
Getter for the checksum of the datagram.
Definition: udp.h:130
PDUType
Enum which identifies each type of PDU.
Definition: pdu.h:127
PDUType pdu_type() const
Getter for the PDU&#39;s type.
Definition: udp.h:177
uint16_t sport() const
Getter for the source port.
Definition: udp.h:114
Represents an UDP PDU.
Definition: udp.h:63
The Tins namespace.
Definition: address_range.h:38
UDP * clone() const
Definition: udp.h:182
Type used to store a PDU header&#39;s data.
Definition: pdu.h:194
uint16_t length() const
Getter for the length of the datagram.
Definition: udp.h:122
uint16_t dport() const
Getter for the destination port.
Definition: udp.h:106
Base class for protocol data units.
Definition: pdu.h:107