1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-27 20:24:26 +01:00

Started fixing endianess issues.

This commit is contained in:
Matias Fontanini
2012-08-15 12:04:13 -03:00
parent 68ab7b094a
commit 892bc0ecd3
18 changed files with 301 additions and 142 deletions

View File

@@ -86,7 +86,7 @@ namespace Tins {
*
* \return Returns the sender's IP address in an uint32_t.
*/
IPv4Address sender_ip_addr() const { return Utils::net_to_host_l(_arp.ar_sip); }
IPv4Address sender_ip_addr() const { return Utils::be_to_host(_arp.ar_sip); }
/**
* \brief Getter for the target's hardware address.
@@ -100,21 +100,21 @@ namespace Tins {
*
* \return Returns the target's IP address in an uint32_t.
*/
IPv4Address target_ip_addr() const { return Utils::net_to_host_l(_arp.ar_tip); }
IPv4Address target_ip_addr() const { return Utils::be_to_host(_arp.ar_tip); }
/**
* \brief Getter for the hardware address format.
*
* \return Returns the hardware address' format in an uint16_t.
*/
uint16_t hw_addr_format() const { return Utils::net_to_host_s(_arp.ar_hrd); }
uint16_t hw_addr_format() const { return Utils::be_to_host(_arp.ar_hrd); }
/**
* \brief Getter for the protocol address format.
*
* \return Returns the protocol address' format in an uint16_t.
*/
uint16_t prot_addr_format() const { return Utils::net_to_host_s(_arp.ar_pro); }
uint16_t prot_addr_format() const { return Utils::be_to_host(_arp.ar_pro); }
/**
* \brief Getter for the hardware address length.
@@ -135,7 +135,7 @@ namespace Tins {
*
* \return Returns the ARP opcode in an uint16_t.
*/
uint16_t opcode() const { return Utils::net_to_host_s(_arp.ar_op); }
uint16_t opcode() const { return Utils::be_to_host(_arp.ar_op); }
/** \brief Getter for the header size.
* \return Returns the ARP header size.

View File

@@ -116,48 +116,47 @@ namespace Tins {
* \brief Getter for the xid field.
* \return The xid field for this BootP PDU.
*/
uint32_t xid() const { return Utils::net_to_host_l(_bootp.xid); }
uint32_t xid() const { return Utils::be_to_host(_bootp.xid); }
/**
* \brief Getter for the secs field.
* \return The secs field for this BootP PDU.
*/
uint16_t secs() const { return Utils::net_to_host_s(_bootp.secs); }
uint16_t secs() const { return Utils::be_to_host(_bootp.secs); }
/** \brief Getter for the padding field.
* \return The padding field for this BootP PDU.
*/
uint16_t padding() const { return Utils::net_to_host_s(_bootp.padding); }
uint16_t padding() const { return Utils::be_to_host(_bootp.padding); }
/**
* \brief Getter for the ciaddr field.
* \return The ciaddr field for this BootP PDU.
*/
IPv4Address ciaddr() const { return Utils::net_to_host_l(_bootp.ciaddr); }
IPv4Address ciaddr() const { return Utils::be_to_host(_bootp.ciaddr); }
/**
* \brief Getter for the yiaddr field.
* \return The yiaddr field for this BootP PDU.
*/
IPv4Address yiaddr() const { return Utils::net_to_host_l(_bootp.yiaddr); }
IPv4Address yiaddr() const { return Utils::be_to_host(_bootp.yiaddr); }
/**
* \brief Getter for the siaddr field.
* \return The siaddr field for this BootP PDU.
*/
IPv4Address siaddr() const { return Utils::net_to_host_l(_bootp.siaddr); }
IPv4Address siaddr() const { return Utils::be_to_host(_bootp.siaddr); }
/**
* \brief Getter for the giaddr field.
* \return The giaddr field for this BootP PDU.
*/
IPv4Address giaddr() const { return Utils::net_to_host_l(_bootp.giaddr); }
IPv4Address giaddr() const { return Utils::be_to_host(_bootp.giaddr); }
/**
* \brief Getter for the chaddr field.
* \return The chddr field for this BootP PDU.
*/
//const uint8_t *chaddr() const { return _bootp.chaddr; }
chaddr_type chaddr() const { return _bootp.chaddr; }
/**

View File

@@ -133,6 +133,9 @@ namespace Tins {
dname(nm), addr(ad), type(t), qclass(c), ttl(tt) {}
};
typedef std::list<Query> queries_type;
typedef std::list<Resource> resources_type;
/**
* \brief Default constructor.
*
@@ -172,7 +175,7 @@ namespace Tins {
*
* \return uint16_t containing the value of the id field.
*/
uint16_t id() const { return Utils::net_to_host_s(dns.id); }
uint16_t id() const { return Utils::be_to_host(dns.id); }
/**
* \brief Setter for the query response field.
@@ -255,28 +258,28 @@ namespace Tins {
*
* \return uint16_t containing the value of the questions field.
*/
uint16_t questions() const { return Utils::net_to_host_s(dns.questions); }
uint16_t questions() const { return Utils::be_to_host(dns.questions); }
/**
* \brief Setter for the answers field.
*
* \return uint16_t containing the value of the answers field.
*/
uint16_t answers() const { return Utils::net_to_host_s(dns.answers); }
uint16_t answers() const { return Utils::be_to_host(dns.answers); }
/**
* \brief Setter for the authority field.
*
* \return uint16_t containing the value of the authority field.
*/
uint16_t authority() const { return Utils::net_to_host_s(dns.authority); }
uint16_t authority() const { return Utils::be_to_host(dns.authority); }
/**
* \brief Setter for the additional field.
*
* \return uint16_t containing the value of the additional field.
*/
uint16_t additional() const { return Utils::net_to_host_s(dns.additional); }
uint16_t additional() const { return Utils::be_to_host(dns.additional); }
/**
* \brief Getter for the PDU's type.
@@ -401,7 +404,7 @@ namespace Tins {
* \param ip The ip address of the resolved name.
*/
void add_answer(const std::string &name, QueryType type, QueryClass qclass,
uint32_t ttl, uint32_t ip);
uint32_t ttl, IPv4Address ip);
/**
* \brief Add a query response.
@@ -459,14 +462,14 @@ namespace Tins {
* \return std::list<Query> containing the queries in this
* record.
*/
std::list<Query> dns_queries() const;
queries_type dns_queries() const;
/**
* \brief Getter for this PDU's DNS answers
* \return std::list<Resource> containing the answers in this
* record.
*/
std::list<Resource> dns_answers();
resources_type dns_answers();
/**
* \sa PDU::clone_pdu

View File

@@ -230,28 +230,28 @@ namespace Tins {
*
* \return Returns the checksum as an unit16_t.
*/
uint16_t check() const { return Utils::net_to_host_s(this->_icmp.check); }
uint16_t check() const { return Utils::be_to_host(this->_icmp.check); }
/**
* \brief Getter for the echo id.
*
* \return Returns the echo id.
*/
uint16_t id() const { return Utils::net_to_host_s(_icmp.un.echo.id); }
uint16_t id() const { return Utils::be_to_host(_icmp.un.echo.id); }
/**
* \brief Getter for the echo sequence number.
*
* \return Returns the echo sequence number.
*/
uint16_t sequence() const { return Utils::net_to_host_s(_icmp.un.echo.sequence); }
uint16_t sequence() const { return Utils::be_to_host(_icmp.un.echo.sequence); }
/**
* \brief Getter for the gateway field.
*
* \return Returns the gateways in an unit32_t.
*/
uint32_t gateway() const { return Utils::net_to_host_l(this->_icmp.un.gateway); }
uint32_t gateway() const { return Utils::be_to_host(this->_icmp.un.gateway); }
/**
* \brief Getter for the pointer field.
@@ -265,7 +265,7 @@ namespace Tins {
*
* \return Returns the mtu value in an uint16_t.
*/
uint16_t mtu() const { return Utils::net_to_host_s(this->_icmp.un.frag.mtu); }
uint16_t mtu() const { return Utils::be_to_host(this->_icmp.un.frag.mtu); }
/**
* \brief Returns the header size.

View File

@@ -170,21 +170,21 @@ namespace Tins {
*
* \return The total length of this IP PDU.
*/
uint16_t tot_len() const { return Utils::net_to_host_s(_ip.tot_len); }
uint16_t tot_len() const { return Utils::be_to_host(_ip.tot_len); }
/**
* \brief Getter for the id field.
*
* \return The id for this IP PDU.
*/
uint16_t id() const { return Utils::net_to_host_s(_ip.id); }
uint16_t id() const { return Utils::be_to_host(_ip.id); }
/**
* \brief Getter for the fragment offset field.
*
* \return The fragment offset for this IP PDU.
*/
uint16_t frag_off() const { return Utils::net_to_host_s(_ip.frag_off); }
uint16_t frag_off() const { return Utils::be_to_host(_ip.frag_off); }
/**
* \brief Getter for the time to live field.
@@ -205,20 +205,20 @@ namespace Tins {
*
* \return The checksum for this IP PDU.
*/
uint16_t check() const { return Utils::net_to_host_s(_ip.check); }
uint16_t check() const { return Utils::be_to_host(_ip.check); }
/**
* \brief Getter for the source address field.
*
* \return The source address for this IP PDU.
*/
IPv4Address src_addr() const { return Utils::net_to_host_l(_ip.saddr); }
IPv4Address src_addr() const { return Utils::be_to_host(_ip.saddr); }
/**
* \brief Getter for the destination address field.
* \return The destination address for this IP PDU.
*/
IPv4Address dst_addr() const { return Utils::net_to_host_l(_ip.daddr); }
IPv4Address dst_addr() const { return Utils::be_to_host(_ip.daddr); }
/**
* \brief Getter for the version field.

View File

@@ -141,49 +141,49 @@ namespace Tins {
*
* \return The destination port in an uint16_t.
*/
uint16_t dport() const { return Utils::net_to_host_s(_tcp.dport); }
uint16_t dport() const { return Utils::be_to_host(_tcp.dport); }
/**
* \brief Getter for the source port field.
*
* \return The source port in an uint16_t.
*/
uint16_t sport() const { return Utils::net_to_host_s(_tcp.sport); }
uint16_t sport() const { return Utils::be_to_host(_tcp.sport); }
/**
* \brief Getter for the sequence number field.
*
* \return The sequence number in an uint32_t.
*/
uint32_t seq() const { return Utils::net_to_host_l(_tcp.seq); }
uint32_t seq() const { return Utils::be_to_host(_tcp.seq); }
/**
* \brief Getter for the acknowledge number field.
*
* \return The acknowledge number in an uint32_t.
*/
uint32_t ack_seq() const { return Utils::net_to_host_l(_tcp.ack_seq); }
uint32_t ack_seq() const { return Utils::be_to_host(_tcp.ack_seq); }
/**
* \brief Getter for the window size field.
*
* \return The window size in an uint32_t.
*/
uint16_t window() const { return Utils::net_to_host_s(_tcp.window); }
uint16_t window() const { return Utils::be_to_host(_tcp.window); }
/**
* \brief Getter for the checksum field.
*
* \return The checksum field in an uint16_t.
*/
uint16_t check() const { return Utils::net_to_host_s(_tcp.check); }
uint16_t check() const { return Utils::be_to_host(_tcp.check); }
/**
* \brief Getter for the urgent pointer field.
*
* \return The urgent pointer in an uint16_t.
*/
uint16_t urg_ptr() const { return Utils::net_to_host_s(_tcp.urg_ptr); }
uint16_t urg_ptr() const { return Utils::be_to_host(_tcp.urg_ptr); }
/**
* \brief Getter for the data offset field.

View File

@@ -63,19 +63,19 @@ namespace Tins {
* \brief Getter for the destination port.
* \return The datagram's destination port.
*/
uint16_t dport() const { return Utils::net_to_host_s(_udp.dport); }
uint16_t dport() const { return Utils::be_to_host(_udp.dport); }
/**
* \brief Getter for the source port.
* \return The datagram's source port.
*/
uint16_t sport() const { return Utils::net_to_host_s(_udp.sport); }
uint16_t sport() const { return Utils::be_to_host(_udp.sport); }
/**
* \brief Getter for the length of the datagram.
* \return The length of the datagram.
*/
uint16_t length() const { return Utils::net_to_host_s(_udp.len); }
uint16_t length() const { return Utils::be_to_host(_udp.len); }
/**
* \brief Set the destination port.

View File

@@ -26,6 +26,7 @@
#ifndef WIN32
#include <ifaddrs.h>
#include <endian.h>
#endif
#include <string>
#include <set>
@@ -153,6 +154,120 @@ namespace Tins {
template<class ForwardIterator>
void route_entries(ForwardIterator output);
/**
* \brief Changes a 16-bit integral value's endianess.
*
* \param data The data to convert.
*/
inline uint16_t change_endian(uint16_t data) {
return ((data & 0xff00) >> 8) | ((data & 0x00ff) << 8);
}
/**
* \brief Changes a 32-bit integral value's endianess.
*
* \param data The data to convert.
*/
inline uint32_t change_endian(uint32_t data) {
return (((data & 0xff000000) >> 24) | ((data & 0x00ff0000) >> 8) |
((data & 0x0000ff00) << 8) | ((data & 0x000000ff) << 24));
}
/**
* \brief Changes a 64-bit integral value's endianess.
*
* \param data The data to convert.
*/
inline uint64_t change_endian(uint64_t data) {
return (((uint64_t)(change_endian((uint32_t)((data << 32) >> 32))) << 32) |
(change_endian(((uint32_t)(data >> 32)))));
}
#if __BYTE_ORDER == __LITTLE_ENDIAN
/**
* \brief Convert any integral type to big endian.
*
* \param data The data to convert.
*/
template<typename T>
inline T to_be(T data) {
return change_endian(data);
}
/**
* \brief Convert any integral type to little endian.
*
* On little endian platforms, the parameter is simply returned.
*
* \param data The data to convert.
*/
template<typename T>
inline T to_le(T data) {
return data;
}
/**
* \brief Convert any big endian value to the host's endianess.
*
* \param data The data to convert.
*/
template<typename T>
inline T be_to_host(T data) {
return change_endian(data);
}
/**
* \brief Convert any little endian value to the host's endianess.
*
* \param data The data to convert.
*/
template<typename T>
inline T le_to_host(T data) {
return data;
}
#elif __BYTE_ORDER == __BIG_ENDIAN
/**
* \brief Convert any integral type to big endian.
*
* \param data The data to convert.
*/
template<typename T>
inline T to_be(T data) {
return data;
}
/**
* \brief Convert any integral type to little endian.
*
* On little endian platforms, the parameter is simply returned.
*
* \param data The data to convert.
*/
template<typename T>
inline T to_le(T data) {
return change_endian(data);
}
/**
* \brief Convert any big endian value to the host's endianess.
*
* \param data The data to convert.
*/
template<typename T>
inline T be_to_host(T data) {
return data;
}
/**
* \brief Convert any little endian value to the host's endianess.
*
* \param data The data to convert.
*/
template<typename T>
inline T le_to_host(T data) {
return change_endian(data);
}
#endif
/** \brief Convert 16 bit integer into network byte order.
*
@@ -215,7 +330,7 @@ namespace Tins {
* \param flag The flag to use in the protocol field of the pseudo header.
* \return The pseudo header checksum.
*/
uint32_t pseudoheader_checksum(uint32_t source_ip, uint32_t dest_ip, uint32_t len, uint32_t flag);
uint32_t pseudoheader_checksum(IPv4Address source_ip, IPv4Address dest_ip, uint32_t len, uint32_t flag);
/** \brief Generic function to iterate through interface and collect
* data.