libtins  3.4
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
utils.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_UTILS_H
31 #define TINS_UTILS_H
32 
33 #include "macros.h"
34 #include <string>
35 #include <set>
36 #include <vector>
37 #include <stdint.h>
38 #include "ip_address.h"
39 #include "ipv6_address.h"
40 #include "internals.h"
41 
42 // Fix for Windows interface define on combaseapi.h
43 #undef interface
44 
45 namespace Tins {
46 
47 class NetworkInterface;
48 class PacketSender;
49 class PDU;
50 class IPv6Address;
51 template <size_t n, typename Storage>
52 class HWAddress;
53 
61 namespace Utils {
62 
66 struct RouteEntry {
70  std::string interface;
71 
76 
81 
86 
90  int metric;
91 };
92 
96 struct Route6Entry {
100  std::string interface;
101 
106 
111 
116 
120  int metric;
121 };
122 
131 TINS_API IPv4Address resolve_domain(const std::string& to_resolve);
132 
141 TINS_API IPv6Address resolve_domain6(const std::string& to_resolve);
142 
154 TINS_API HWAddress<6> resolve_hwaddr(const NetworkInterface& iface,
155  IPv4Address ip,
156  PacketSender& sender);
157 
172 
179 TINS_API std::set<std::string> network_interfaces();
180 
192 TINS_API bool gateway_from_ip(IPv4Address ip, IPv4Address& gw_addr);
193 
194 
200 template<typename ForwardIterator>
201 void route_entries(ForwardIterator output);
202 
208 TINS_API std::vector<RouteEntry> route_entries();
209 
215 TINS_API std::vector<Route6Entry> route6_entries();
216 
222 TINS_API uint32_t crc32(const uint8_t* data, uint32_t data_size);
223 
229 TINS_API uint16_t channel_to_mhz(uint16_t channel);
230 
236 TINS_API uint16_t mhz_to_channel(uint16_t mhz);
237 
243 TINS_API std::string to_string(PDU::PDUType pduType);
244 
255 TINS_API uint32_t do_checksum(const uint8_t* start, const uint8_t* end);
256 
267 TINS_API uint16_t sum_range(const uint8_t* start, const uint8_t* end);
268 
277 TINS_API uint32_t pseudoheader_checksum(IPv4Address source_ip,
278  IPv4Address dest_ip,
279  uint16_t len,
280  uint16_t flag);
281 
290 TINS_API uint32_t pseudoheader_checksum(IPv6Address source_ip,
291  IPv6Address dest_ip,
292  uint16_t len,
293  uint16_t flag);
294 
295 template <typename T>
296 struct is_pdu {
297  template <typename U>
298  static char test(typename U::PDUType*);
299 
300  template <typename U>
301  static long test(...);
302 
303  static const bool value = sizeof(test<T>(0)) == 1;
304 };
305 
310  return pdu;
311 }
312 
321 template<typename T>
322 inline typename Internals::enable_if<!is_pdu<T>::value, PDU&>::type
324  return dereference_until_pdu(*value);
325 }
326 
327 } // Utils
328 } // Tins
329 
330 template<typename ForwardIterator>
331 void Tins::Utils::route_entries(ForwardIterator output) {
332  std::vector<RouteEntry> entries = route_entries();
333  for (size_t i = 0; i < entries.size(); ++i) {
334  *output = entries[i];
335  ++output;
336  }
337 }
338 
339 #endif // TINS_UTILS_H
TINS_API uint16_t mhz_to_channel(uint16_t mhz)
Converts mhz units to the appropriate channel number.
Definition: utils.cpp:472
IPv6Address destination
Definition: utils.h:105
PDUType
Enum which identifies each type of PDU.
Definition: pdu.h:128
IPv4Address destination
Definition: utils.h:75
TINS_API uint32_t do_checksum(const uint8_t *start, const uint8_t *end)
Does the 16 bits sum of all 2 bytes elements between start and end.
Definition: utils.cpp:538
int metric
Definition: utils.h:120
Sends packets through a network interface.
Definition: packet_sender.h:117
PDU & dereference_until_pdu(PDU &pdu)
Definition: utils.h:309
Definition: utils.h:66
Definition: utils.h:296
std::string interface
Definition: utils.h:100
TINS_API uint16_t sum_range(const uint8_t *start, const uint8_t *end)
Computes the 16 bit sum of the input buffer.
Definition: udp.cpp:83
IPv6Address gateway
Definition: utils.h:115
std::string interface
Definition: utils.h:70
TINS_API HWAddress< 6 > resolve_hwaddr(const NetworkInterface &iface, IPv4Address ip, PacketSender &sender)
Resolves the hardware address for a given ip.
TINS_API bool gateway_from_ip(IPv4Address ip, IPv4Address &gw_addr)
Finds the gateway's IP address for the given IP address.
TINS_API uint16_t channel_to_mhz(uint16_t channel)
Converts a channel number to its mhz representation.
Definition: utils.cpp:468
TINS_API uint32_t crc32(const uint8_t *data, uint32_t data_size)
Returns the 32 bit crc of the given buffer.
Definition: utils.cpp:605
TINS_API std::string to_string(PDU::PDUType pduType)
Converts a PDUType to a string.
TINS_API std::set< std::string > network_interfaces()
List all network interfaces.
Definition: utils.cpp:453
Abstraction of a network interface.
Definition: network_interface.h:47
IPv4Address mask
Definition: utils.h:85
TINS_API IPv4Address resolve_domain(const std::string &to_resolve)
Resolves a domain name and returns its corresponding ip address.
IPv6Address mask
Definition: utils.h:110
Abstraction of an IPv4 address.
Definition: ip_address.h:44
IPv4Address gateway
Definition: utils.h:80
Definition: ipv6_address.h:44
Definition: utils.h:96
Base class for protocol data units.
Definition: pdu.h:108
TINS_API IPv6Address resolve_domain6(const std::string &to_resolve)
Resolves a domain name and returns its corresponding ip address.
TINS_API std::vector< Route6Entry > route6_entries()
Retrieves entries in the routing table.
Definition: utils.cpp:384
TINS_API uint32_t pseudoheader_checksum(IPv4Address source_ip, IPv4Address dest_ip, uint16_t len, uint16_t flag)
Performs the pseudo header checksum used in TCP and UDP PDUs.
int metric
Definition: utils.h:90
void route_entries(ForwardIterator output)
Retrieves entries in the routing table.
Definition: utils.h:331