libtins  4.0
Classes | Functions | Variables
Tins::Utils Namespace Reference

Network utils namespace. More...

Classes

struct  InterfaceCollector
 
struct  is_pdu
 
struct  RadioTapFlags
 
class  RadioTapParser
 Allows parsing RadioTap options. More...
 
class  RadioTapWriter
 Writes RadioTap options into a buffer. More...
 
struct  Route6Entry
 
struct  RouteEntry
 

Functions

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. More...
 
TINS_API uint16_t sum_range (const uint8_t *start, const uint8_t *end)
 Computes the 16 bit sum of the input buffer. More...
 
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. More...
 
TINS_API uint32_t pseudoheader_checksum (IPv6Address source_ip, IPv6Address dest_ip, uint16_t len, uint16_t flag)
 Performs the pseudo header checksum used in TCP and UDP PDUs. More...
 
TINS_API uint32_t crc32 (const uint8_t *data, uint32_t data_size)
 Returns the 32 bit crc of the given buffer. More...
 
TINS_API uint16_t channel_to_mhz (uint16_t channel)
 Converts a channel number to its mhz representation. More...
 
TINS_API uint16_t mhz_to_channel (uint16_t mhz)
 Converts mhz units to the appropriate channel number. More...
 
TINS_API std::string to_string (PDU::PDUType pduType)
 Converts a PDUType to a string. More...
 
PDUdereference_until_pdu (PDU &pdu)
 
template<typename T >
Internals::enable_if<!is_pdu< T >::value, PDU & >::type dereference_until_pdu (T &value)
 Dereferences the parameter until a PDU is found. More...
 
TINS_API IPv4Address resolve_domain (const std::string &to_resolve)
 Resolves a domain name and returns its corresponding ip address. More...
 
TINS_API IPv6Address resolve_domain6 (const std::string &to_resolve)
 Resolves a domain name and returns its corresponding ip address. More...
 
TINS_API HWAddress< 6 > resolve_hwaddr (const NetworkInterface &iface, IPv4Address ip, PacketSender &sender)
 Resolves the hardware address for a given ip. More...
 
TINS_API HWAddress< 6 > resolve_hwaddr (IPv4Address ip, PacketSender &sender)
 Resolves the hardware address for a given ip. More...
 
template<typename ForwardIterator >
void route_entries (ForwardIterator output)
 Retrieves entries in the routing table. More...
 
template<typename ForwardIterator >
void route6_entries (ForwardIterator output)
 Retrieves entries in the routing table. More...
 
TINS_API std::vector< RouteEntryroute_entries ()
 Retrieves entries in the routing table. More...
 
TINS_API std::vector< Route6Entryroute6_entries ()
 Retrieves entries in the routing table. More...
 
TINS_API std::set< std::string > network_interfaces ()
 List all network interfaces. More...
 
TINS_API bool gateway_from_ip (IPv4Address ip, IPv4Address &gw_addr)
 Finds the gateway's IP address for the given IP address. More...
 
TINS_API bool gateway_from_ip (IPv6Address ip, IPv6Address &gw_addr)
 Finds the gateway's IP address for the given IP address. More...
 
template<size_t buffer_size, typename AddressType >
uint32_t generic_pseudoheader_checksum (const AddressType &source_ip, const AddressType &dest_ip, uint16_t len, uint16_t flag)
 
void align_buffer (const uint8_t *buffer_start, const uint8_t *&buffer, uint32_t size, size_t n)
 
uint32_t calculate_padding (uint32_t alignment, uint32_t offset)
 
uint32_t get_bit (uint32_t value)
 
IPv4Address resolve_domain (const string &to_resolve)
 
IPv6Address resolve_domain6 (const string &to_resolve)
 
bool from_hex (const string &str, uint32_t &result)
 
bool from_hex (const string &str, string &result)
 
void skip_line (istream &input)
 

Variables

TINS_BEGIN_PACK struct Tins::Utils::RadioTapFlags TINS_END_PACK
 

Detailed Description

Network utils namespace.

This namespace provides utils to convert between integer IP addresses and dotted notation strings, "net to host" integer conversions, interface listing, etc.

Function Documentation

uint16_t Tins::Utils::channel_to_mhz ( uint16_t  channel)

Converts a channel number to its mhz representation.

Parameters
channelThe channel number.
Returns
The channel's mhz representation.
uint32_t Tins::Utils::crc32 ( const uint8_t *  data,
uint32_t  data_size 
)

Returns the 32 bit crc of the given buffer.

Parameters
dataThe input buffer.
data_sizeThe size of the input buffer.
PDU& Tins::Utils::dereference_until_pdu ( PDU pdu)
inline

Returns the argument.

template<typename T >
Internals::enable_if<!is_pdu<T>::value, PDU&>::type Tins::Utils::dereference_until_pdu ( T &  value)
inline

Dereferences the parameter until a PDU is found.

This function dereferences the parameter until a PDU object is found. When it's found, it is returned.

Parameters
valueThe parameter to be dereferenced.
uint32_t Tins::Utils::do_checksum ( const uint8_t *  start,
const uint8_t *  end 
)

Does the 16 bits sum of all 2 bytes elements between start and end.

This is the checksum used by IP, UDP and TCP. If there's and odd number of bytes, the last one is padded and added to the checksum.

Parameters
startThe pointer to the start of the buffer.
endThe pointer to the end of the buffer(excluding the last element).
Returns
Returns the checksum between start and end (non inclusive) in network endian
bool Tins::Utils::gateway_from_ip ( IPv4Address  ip,
IPv4Address gw_addr 
)

Finds the gateway's IP address for the given IP address.

Parameters
ipThe IP address for which the default gateway will be searched.
gw_addrThis parameter will contain the gateway's IP address in case it is found.
Returns
bool indicating whether the lookup was successfull.
bool Tins::Utils::gateway_from_ip ( IPv6Address  ip,
IPv6Address gw_addr 
)

Finds the gateway's IP address for the given IP address.

Parameters
ipThe IP address for which the default gateway will be searched.
gw_addrThis parameter will contain the gateway's IP address in case it is found.
Returns
bool indicating whether the lookup was successfull.
uint16_t Tins::Utils::mhz_to_channel ( uint16_t  mhz)

Converts mhz units to the appropriate channel number.

Parameters
mhzThe mhz units to be converted.
Returns
The channel number.
set< string > Tins::Utils::network_interfaces ( )

List all network interfaces.

Returns a set of strings, each of them representing the name of a network interface. These names can be used as the input interface for Utils::interface_ip, Utils::interface_hwaddr, etc.

uint32_t Tins::Utils::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.

Parameters
source_ipThe source ip address.
dest_ipThe destination ip address.
lenThe length to be included in the pseudo header.
flagThe flag to use in the protocol field of the pseudo header.
Returns
The pseudo header checksum.
uint32_t Tins::Utils::pseudoheader_checksum ( IPv6Address  source_ip,
IPv6Address  dest_ip,
uint16_t  len,
uint16_t  flag 
)

Performs the pseudo header checksum used in TCP and UDP PDUs.

Parameters
source_ipThe source ip address.
dest_ipThe destination ip address.
lenThe length to be included in the pseudo header.
flagThe flag to use in the protocol field of the pseudo header.
Returns
The pseudo header checksum.
TINS_API IPv4Address Tins::Utils::resolve_domain ( const std::string &  to_resolve)

Resolves a domain name and returns its corresponding ip address.

If an ip address is given, its integer representation is returned. Otherwise, the domain name is resolved and its ip address is returned.

Parameters
to_resolveThe domain name/ip address to resolve.
TINS_API IPv6Address Tins::Utils::resolve_domain6 ( const std::string &  to_resolve)

Resolves a domain name and returns its corresponding ip address.

If an ip address is given, its integer representation is returned. Otherwise, the domain name is resolved and its ip address is returned.

Parameters
to_resolveThe domain name/ip address to resolve.
HWAddress< 6 > Tins::Utils::resolve_hwaddr ( const NetworkInterface iface,
IPv4Address  ip,
PacketSender sender 
)

Resolves the hardware address for a given ip.

If the address can't be resolved, a std::runtime_error exception is thrown.

Parameters
ifaceThe interface in which the packet will be sent.
ipThe ip to resolve, in integer format.
senderThe sender to use to send and receive the ARP requests.
Returns
HWAddress<6> containing the resolved hardware address.
HWAddress< 6 > Tins::Utils::resolve_hwaddr ( IPv4Address  ip,
PacketSender sender 
)

Resolves the hardware address for a given ip.

If the address can't be resolved, a std::runtime_error exception is thrown.

This method sends and receives the packet through PacketSender::default_interface.

Parameters
ipThe ip to resolve, in integer format.
senderThe sender to use to send and receive the ARP requests.
Returns
HWAddress<6> containing the resolved hardware address.
template<typename ForwardIterator >
void Tins::Utils::route6_entries ( ForwardIterator  output)

Retrieves entries in the routing table.

output ForwardIterator in which entries will be stored.

vector< Route6Entry > Tins::Utils::route6_entries ( )

Retrieves entries in the routing table.

Returns
a vector which contains all of the route entries.
template<typename ForwardIterator >
void Tins::Utils::route_entries ( ForwardIterator  output)

Retrieves entries in the routing table.

output ForwardIterator in which entries will be stored.

vector< RouteEntry > Tins::Utils::route_entries ( )

Retrieves entries in the routing table.

Returns
a vector which contains all of the route entries.
uint16_t Tins::Utils::sum_range ( const uint8_t *  start,
const uint8_t *  end 
)

Computes the 16 bit sum of the input buffer.

If there's and odd number of bytes in the buffer, the last one is padded and added to the checksum.

Parameters
startThe pointer to the start of the buffer.
endThe pointer to the end of the buffer(excluding the last element).
Returns
Returns the checksum between start and end (non inclusive) in network endian
string Tins::Utils::to_string ( PDU::PDUType  pduType)

Converts a PDUType to a string.

Parameters
pduTypeThe PDUType to be converted.
Returns
A string representation, for example "DOT11_QOS_DATA".