mirror of
https://github.com/mfontanini/libtins
synced 2026-01-23 02:35:57 +01:00
Fixed some bugs in Dot11 and DNS. Most of the code uses host_to_be and be_to_host.
This commit is contained in:
@@ -41,3 +41,4 @@ uninstall:
|
||||
rm -r /usr/include/tins/
|
||||
rm /usr/lib/$(EXECUTABLE)
|
||||
|
||||
-include depends.d
|
||||
|
||||
@@ -77,7 +77,7 @@ namespace Tins {
|
||||
* \brief Getter for the length field.
|
||||
* \return The length field.
|
||||
*/
|
||||
uint16_t length() const { return Utils::net_to_host_s(_header.length); }
|
||||
uint16_t length() const { return Utils::be_to_host(_header.length); }
|
||||
|
||||
/**
|
||||
* \brief Getter for the type field.
|
||||
@@ -103,7 +103,7 @@ namespace Tins {
|
||||
* \brief Sets the length field.
|
||||
* \param new_length The new length to be set.
|
||||
*/
|
||||
void length(uint8_t new_length);
|
||||
void length(uint16_t new_length);
|
||||
|
||||
/**
|
||||
* \brief Sets the type field.
|
||||
@@ -209,13 +209,13 @@ namespace Tins {
|
||||
* \brief Getter for the key length field.
|
||||
* \return The key length field.
|
||||
*/
|
||||
uint16_t key_length() const { return Utils::net_to_host_s(_header.key_length); }
|
||||
uint16_t key_length() const { return Utils::be_to_host(_header.key_length); }
|
||||
|
||||
/**
|
||||
* \brief Getter for the replay counter field.
|
||||
* \return The replay counter field.
|
||||
*/
|
||||
uint64_t replay_counter() const { return Utils::net_to_host_ll(_header.replay_counter); }
|
||||
uint64_t replay_counter() const { return Utils::be_to_host(_header.replay_counter); }
|
||||
|
||||
/**
|
||||
* \brief Getter for the key IV field.
|
||||
@@ -389,13 +389,13 @@ namespace Tins {
|
||||
* \brief Getter for the key length field.
|
||||
* \return The key length field.
|
||||
*/
|
||||
uint16_t key_length() const { return Utils::net_to_host_s(_header.key_length); }
|
||||
uint16_t key_length() const { return Utils::be_to_host(_header.key_length); }
|
||||
|
||||
/**
|
||||
* \brief Getter for the replay counter field.
|
||||
* \return The replay counter field.
|
||||
*/
|
||||
uint64_t replay_counter() const { return Utils::net_to_host_ll(_header.replay_counter); }
|
||||
uint64_t replay_counter() const { return Utils::be_to_host(_header.replay_counter); }
|
||||
|
||||
/**
|
||||
* \brief Getter for the key IV field.
|
||||
@@ -413,13 +413,13 @@ namespace Tins {
|
||||
* \brief Getter for the rsc field.
|
||||
* \return The rsc field.
|
||||
*/
|
||||
uint64_t rsc() const { return Utils::net_to_host_ll(_header.rsc); }
|
||||
uint64_t rsc() const { return Utils::be_to_host(_header.rsc); }
|
||||
|
||||
/**
|
||||
* \brief Getter for the id field.
|
||||
* \return The id field.
|
||||
*/
|
||||
uint64_t id() const { return Utils::net_to_host_ll(_header.id); }
|
||||
uint64_t id() const { return Utils::be_to_host(_header.id); }
|
||||
|
||||
/**
|
||||
* \brief Getter for the mic field.
|
||||
@@ -431,7 +431,7 @@ namespace Tins {
|
||||
* \brief Getter for the wpa length field.
|
||||
* \return The wpa length field.
|
||||
*/
|
||||
uint16_t wpa_length() const { return Utils::net_to_host_s(_header.wpa_length); }
|
||||
uint16_t wpa_length() const { return Utils::be_to_host(_header.wpa_length); }
|
||||
|
||||
/**
|
||||
* \brief Getter for the key field.
|
||||
|
||||
@@ -109,7 +109,7 @@ namespace Tins {
|
||||
* \brief Getter for the payload_type
|
||||
* \return The payload type.
|
||||
*/
|
||||
uint16_t payload_type() const { return Utils::net_to_host_s(_eth.payload_type); };
|
||||
uint16_t payload_type() const { return Utils::be_to_host(_eth.payload_type); };
|
||||
|
||||
/* Setters */
|
||||
|
||||
|
||||
@@ -102,7 +102,7 @@ namespace Tins {
|
||||
* \brief Getter for the length field.
|
||||
* \return The length field value.
|
||||
*/
|
||||
uint16_t length() const { return Utils::net_to_host_s(_eth.length); };
|
||||
uint16_t length() const { return Utils::be_to_host(_eth.length); };
|
||||
|
||||
/* Setters */
|
||||
|
||||
|
||||
@@ -100,37 +100,37 @@ namespace Tins {
|
||||
* \brief Getter for the dsap field.
|
||||
* \return The dsap field.
|
||||
*/
|
||||
inline uint8_t dsap() const { return _snap.dsap; }
|
||||
uint8_t dsap() const { return _snap.dsap; }
|
||||
|
||||
/**
|
||||
* \brief Getter for the ssap field.
|
||||
* \return The ssap field.
|
||||
*/
|
||||
inline uint8_t ssap() const { return _snap.ssap; }
|
||||
uint8_t ssap() const { return _snap.ssap; }
|
||||
|
||||
/**
|
||||
* \brief Getter for the id field.
|
||||
* \return The id field.
|
||||
*/
|
||||
inline uint8_t id() const { return _snap.id; }
|
||||
uint8_t id() const { return _snap.id; }
|
||||
|
||||
/**
|
||||
* \brief Getter for the poll field.
|
||||
* \return The poll field.
|
||||
*/
|
||||
inline uint8_t poll() const { return _snap.poll; }
|
||||
uint8_t poll() const { return _snap.poll; }
|
||||
|
||||
/**
|
||||
* \brief Getter for the org code field.
|
||||
* \return The org code field.
|
||||
*/
|
||||
inline uint32_t org_code() const { return _snap.org_code; }
|
||||
uint32_t org_code() const { return _snap.org_code; }
|
||||
|
||||
/**
|
||||
* \brief Getter for the eth type field.
|
||||
* \return The eth field.
|
||||
*/
|
||||
inline uint16_t eth_type() const { return Utils::net_to_host_s(_snap.eth_type); }
|
||||
uint16_t eth_type() const { return Utils::be_to_host(_snap.eth_type); }
|
||||
|
||||
/**
|
||||
* \brief Returns the SNAP frame's header length.
|
||||
|
||||
@@ -93,7 +93,7 @@ namespace Tins {
|
||||
*
|
||||
* \param to_resolve The domain name/ip address to resolve.
|
||||
*/
|
||||
uint32_t resolve_ip(const std::string &to_resolve);
|
||||
IPv4Address resolve_ip(const std::string &to_resolve);
|
||||
|
||||
/**
|
||||
* \brief Pings an ip address.
|
||||
@@ -190,7 +190,7 @@ namespace Tins {
|
||||
* \param data The data to convert.
|
||||
*/
|
||||
template<typename T>
|
||||
inline T to_be(T data) {
|
||||
inline T host_to_be(T data) {
|
||||
return change_endian(data);
|
||||
}
|
||||
|
||||
@@ -202,7 +202,7 @@ namespace Tins {
|
||||
* \param data The data to convert.
|
||||
*/
|
||||
template<typename T>
|
||||
inline T to_le(T data) {
|
||||
inline T host_to_le(T data) {
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -232,7 +232,7 @@ namespace Tins {
|
||||
* \param data The data to convert.
|
||||
*/
|
||||
template<typename T>
|
||||
inline T to_be(T data) {
|
||||
inline T host_to_be(T data) {
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -244,7 +244,7 @@ namespace Tins {
|
||||
* \param data The data to convert.
|
||||
*/
|
||||
template<typename T>
|
||||
inline T to_le(T data) {
|
||||
inline T host_to_le(T data) {
|
||||
return change_endian(data);
|
||||
}
|
||||
|
||||
@@ -371,11 +371,11 @@ void Tins::Utils::route_entries(ForwardIterator output) {
|
||||
for(unsigned i(0); i < 5; ++i)
|
||||
input >> mask;
|
||||
from_hex(destination, dummy);
|
||||
entry.destination = net_to_host_l(dummy);
|
||||
entry.destination = be_to_host(dummy);
|
||||
from_hex(mask, dummy);
|
||||
entry.mask = net_to_host_l(dummy);
|
||||
entry.mask = be_to_host(dummy);
|
||||
from_hex(gw, dummy);
|
||||
entry.gateway = net_to_host_l(dummy);
|
||||
entry.gateway = be_to_host(dummy);
|
||||
skip_line(input);
|
||||
*output = entry;
|
||||
++output;
|
||||
|
||||
@@ -52,7 +52,7 @@ ARP::ARP(IPv4Address target_ip, IPv4Address sender_ip,
|
||||
}
|
||||
|
||||
ARP::ARP(const uint8_t *buffer, uint32_t total_sz)
|
||||
: PDU(Utils::to_be<uint16_t>(Constants::Ethernet::ARP))
|
||||
: PDU(Utils::host_to_be<uint16_t>(Constants::Ethernet::ARP))
|
||||
{
|
||||
if(total_sz < sizeof(arphdr))
|
||||
throw runtime_error("Not enough size for an ARP header in the buffer.");
|
||||
@@ -79,11 +79,11 @@ void ARP::target_ip_addr(IPv4Address new_tgt_ip_addr) {
|
||||
}
|
||||
|
||||
void ARP::hw_addr_format(uint16_t new_hw_addr_fmt) {
|
||||
this->_arp.ar_hrd = Utils::to_be(new_hw_addr_fmt);
|
||||
this->_arp.ar_hrd = Utils::host_to_be(new_hw_addr_fmt);
|
||||
}
|
||||
|
||||
void ARP::prot_addr_format(uint16_t new_prot_addr_fmt) {
|
||||
this->_arp.ar_pro = Utils::to_be(new_prot_addr_fmt);
|
||||
this->_arp.ar_pro = Utils::host_to_be(new_prot_addr_fmt);
|
||||
}
|
||||
|
||||
void ARP::hw_addr_length(uint8_t new_hw_addr_len) {
|
||||
@@ -95,7 +95,7 @@ void ARP::prot_addr_length(uint8_t new_prot_addr_len) {
|
||||
}
|
||||
|
||||
void ARP::opcode(Flags new_opcode) {
|
||||
this->_arp.ar_op = Utils::to_be<uint16_t>(new_opcode);
|
||||
this->_arp.ar_op = Utils::host_to_be<uint16_t>(new_opcode);
|
||||
}
|
||||
|
||||
uint32_t ARP::header_size() const {
|
||||
|
||||
@@ -81,15 +81,15 @@ void BootP::hops(uint8_t new_hops) {
|
||||
}
|
||||
|
||||
void BootP::xid(uint32_t new_xid) {
|
||||
_bootp.xid = Utils::to_be(new_xid);
|
||||
_bootp.xid = Utils::host_to_be(new_xid);
|
||||
}
|
||||
|
||||
void BootP::secs(uint16_t new_secs) {
|
||||
_bootp.secs = Utils::to_be(new_secs);
|
||||
_bootp.secs = Utils::host_to_be(new_secs);
|
||||
}
|
||||
|
||||
void BootP::padding(uint16_t new_padding) {
|
||||
_bootp.padding = Utils::to_be(new_padding);
|
||||
_bootp.padding = Utils::host_to_be(new_padding);
|
||||
}
|
||||
|
||||
void BootP::ciaddr(IPv4Address new_ciaddr) {
|
||||
|
||||
24
src/dhcp.cpp
24
src/dhcp.cpp
@@ -47,7 +47,7 @@ DHCP::DHCP(const uint8_t *buffer, uint32_t total_sz)
|
||||
buffer += BootP::header_size() - vend_size();
|
||||
total_sz -= BootP::header_size() - vend_size();
|
||||
uint8_t args[2] = {0};
|
||||
if(total_sz < sizeof(uint32_t) || *(uint32_t*)buffer != Utils::to_be<uint32_t>(0x63825363))
|
||||
if(total_sz < sizeof(uint32_t) || *(uint32_t*)buffer != Utils::host_to_be<uint32_t>(0x63825363))
|
||||
throw std::runtime_error("Not enough size for a DHCP header in the buffer.");
|
||||
buffer += sizeof(uint32_t);
|
||||
total_sz -= sizeof(uint32_t);
|
||||
@@ -106,7 +106,7 @@ bool DHCP::search_type_option(uint8_t *value) {
|
||||
}
|
||||
|
||||
bool DHCP::add_server_identifier(uint32_t ip) {
|
||||
ip = Utils::to_be(ip);
|
||||
ip = Utils::host_to_be(ip);
|
||||
return add_option(DHCP_SERVER_IDENTIFIER, sizeof(uint32_t), (const uint8_t*)&ip);
|
||||
}
|
||||
|
||||
@@ -115,7 +115,7 @@ bool DHCP::search_server_identifier(uint32_t *value) {
|
||||
}
|
||||
|
||||
bool DHCP::add_lease_time(uint32_t time) {
|
||||
time = Utils::to_be(time);
|
||||
time = Utils::host_to_be(time);
|
||||
return add_option(DHCP_LEASE_TIME, sizeof(uint32_t), (const uint8_t*)&time);
|
||||
}
|
||||
|
||||
@@ -124,7 +124,7 @@ bool DHCP::search_lease_time(uint32_t *value) {
|
||||
}
|
||||
|
||||
bool DHCP::add_renewal_time(uint32_t time) {
|
||||
time = Utils::to_be(time);
|
||||
time = Utils::host_to_be(time);
|
||||
return add_option(DHCP_RENEWAL_TIME, sizeof(uint32_t), (const uint8_t*)&time);
|
||||
}
|
||||
|
||||
@@ -133,7 +133,7 @@ bool DHCP::search_renewal_time(uint32_t *value) {
|
||||
}
|
||||
|
||||
bool DHCP::add_subnet_mask(uint32_t mask) {
|
||||
mask = Utils::to_be(mask);
|
||||
mask = Utils::host_to_be(mask);
|
||||
return add_option(SUBNET_MASK, sizeof(uint32_t), (const uint8_t*)&mask);
|
||||
}
|
||||
|
||||
@@ -166,7 +166,7 @@ bool DHCP::search_dns_option(std::list<uint32_t> *dns) {
|
||||
}
|
||||
|
||||
bool DHCP::add_broadcast_option(uint32_t addr) {
|
||||
addr = Utils::to_be(addr);
|
||||
addr = Utils::host_to_be(addr);
|
||||
return add_option(BROADCAST_ADDRESS, sizeof(uint32_t), (uint8_t*)&addr);
|
||||
}
|
||||
|
||||
@@ -175,7 +175,7 @@ bool DHCP::search_broadcast_option(uint32_t *value) {
|
||||
}
|
||||
|
||||
bool DHCP::add_requested_ip_option(uint32_t addr) {
|
||||
addr = Utils::to_be(addr);
|
||||
addr = Utils::host_to_be(addr);
|
||||
return add_option(DHCP_REQUESTED_ADDRESS, sizeof(uint32_t), (uint8_t*)&addr);
|
||||
}
|
||||
|
||||
@@ -192,7 +192,7 @@ bool DHCP::search_domain_name(std::string *value) {
|
||||
}
|
||||
|
||||
bool DHCP::add_rebind_time(uint32_t time) {
|
||||
time = Utils::to_be(time);
|
||||
time = Utils::host_to_be(time);
|
||||
return add_option(DHCP_REBINDING_TIME, sizeof(uint32_t), (uint8_t*)&time);
|
||||
}
|
||||
|
||||
@@ -204,7 +204,7 @@ uint8_t *DHCP::serialize_list(const list<uint32_t> &int_list, uint32_t &sz) {
|
||||
uint8_t *buffer = new uint8_t[int_list.size() * sizeof(uint32_t)];
|
||||
uint32_t *ptr = (uint32_t*)buffer;
|
||||
for(list<uint32_t>::const_iterator it = int_list.begin(); it != int_list.end(); ++it)
|
||||
*(ptr++) = Utils::to_be(*it);
|
||||
*(ptr++) = Utils::host_to_be(*it);
|
||||
sz = sizeof(uint32_t) * int_list.size();
|
||||
return buffer;
|
||||
}
|
||||
@@ -220,7 +220,7 @@ void DHCP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *pa
|
||||
result = new uint8_t[_size];
|
||||
uint8_t *ptr = result + sizeof(uint32_t);
|
||||
// Magic cookie
|
||||
*((uint32_t*)result) = Utils::to_be<uint32_t>(0x63825363);
|
||||
*((uint32_t*)result) = Utils::host_to_be<uint32_t>(0x63825363);
|
||||
for(options_type::const_iterator it = _options.begin(); it != _options.end(); ++it) {
|
||||
*(ptr++) = it->option;
|
||||
*(ptr++) = it->value.size();
|
||||
@@ -251,7 +251,7 @@ bool DHCP::generic_search(Options opt, std::list<uint32_t> *container) {
|
||||
if((len % sizeof(uint32_t)) != 0)
|
||||
return false;
|
||||
while(len) {
|
||||
container->push_back(Utils::to_be(*(ptr++)));
|
||||
container->push_back(Utils::host_to_be(*(ptr++)));
|
||||
len -= sizeof(uint32_t);
|
||||
}
|
||||
return true;
|
||||
@@ -267,7 +267,7 @@ bool DHCP::generic_search(Options opt, std::string *str) {
|
||||
|
||||
bool DHCP::generic_search(Options opt, uint32_t *value) {
|
||||
if(generic_search<uint32_t>(opt, value)) {
|
||||
*value = Utils::to_be(*value);
|
||||
*value = Utils::host_to_be(*value);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
46
src/dns.cpp
46
src/dns.cpp
@@ -46,7 +46,7 @@ DNS::DNS(const uint8_t *buffer, uint32_t total_sz) : PDU(255), extra_size(0) {
|
||||
while(ptr < end && *ptr)
|
||||
ptr++;
|
||||
Query query;
|
||||
if((ptr + (sizeof(uint16_t) << 1)) >= end)
|
||||
if((ptr + (sizeof(uint16_t) * 2)) >= end)
|
||||
throw std::runtime_error("Not enough size for a given query.");
|
||||
query.name = string(buffer, ptr);
|
||||
ptr++;
|
||||
@@ -101,7 +101,7 @@ const uint8_t *DNS::build_resource_list(list<ResourceRecord*> &lst, const uint8_
|
||||
if((*ptr & 0xc0)) {
|
||||
uint16_t offset(*reinterpret_cast<const uint16_t*>(ptr));
|
||||
offset = Utils::be_to_host(offset) & 0x3fff;
|
||||
res.reset(new OffsetedResourceRecord(Utils::to_be(offset)));
|
||||
res.reset(new OffsetedResourceRecord(Utils::host_to_be(offset)));
|
||||
ptr += sizeof(uint16_t);
|
||||
}
|
||||
else {
|
||||
@@ -110,9 +110,9 @@ const uint8_t *DNS::build_resource_list(list<ResourceRecord*> &lst, const uint8_
|
||||
str_end++;
|
||||
if(str_end == end)
|
||||
throw std::runtime_error("Not enough size for a resource domain name.");
|
||||
str_end++;
|
||||
//str_end++;
|
||||
res.reset(new NamedResourceRecord(string(ptr, str_end)));
|
||||
ptr = str_end;
|
||||
ptr = ++str_end;
|
||||
}
|
||||
if(ptr + sizeof(res->info) > ptr_end)
|
||||
throw std::runtime_error("Not enough size for a resource info.");
|
||||
@@ -123,15 +123,17 @@ const uint8_t *DNS::build_resource_list(list<ResourceRecord*> &lst, const uint8_
|
||||
|
||||
// Store the option size.
|
||||
res->data.resize(
|
||||
Utils::to_be(*reinterpret_cast<const uint16_t*>(ptr))
|
||||
Utils::host_to_be(*reinterpret_cast<const uint16_t*>(ptr))
|
||||
);
|
||||
ptr += sizeof(uint16_t);
|
||||
if(ptr + res->data.size() > ptr_end)
|
||||
throw std::runtime_error("Not enough size for resource data");
|
||||
if(contains_dname(res->info.type))
|
||||
std::copy(ptr, ptr + res->data.size(), res->data.begin());
|
||||
else if(res->data.size() == sizeof(uint32_t))
|
||||
*(uint32_t*)&res->data[0] = Utils::host_to_be(*(uint32_t*)ptr);
|
||||
else
|
||||
*(uint32_t*)&res->data[0] = Utils::to_be(*(uint32_t*)ptr);
|
||||
throw std::runtime_error("Not enough size for resource data");
|
||||
|
||||
ptr += res->data.size();
|
||||
extra_size += ptr - this_opt_start;
|
||||
@@ -146,7 +148,7 @@ uint32_t DNS::header_size() const {
|
||||
}
|
||||
|
||||
void DNS::id(uint16_t new_id) {
|
||||
dns.id = Utils::to_be(new_id);
|
||||
dns.id = Utils::host_to_be(new_id);
|
||||
}
|
||||
|
||||
void DNS::type(QRType new_qr) {
|
||||
@@ -201,11 +203,11 @@ void DNS::add_query(const string &name, QueryType type, QueryClass qclass) {
|
||||
|
||||
queries.push_back(
|
||||
Query(new_str,
|
||||
Utils::to_be<uint16_t>(type),
|
||||
Utils::to_be<uint16_t>(qclass))
|
||||
Utils::host_to_be<uint16_t>(type),
|
||||
Utils::host_to_be<uint16_t>(qclass))
|
||||
);
|
||||
extra_size += new_str.size() + 1 + (sizeof(uint16_t) << 1);
|
||||
dns.questions = Utils::to_be<uint16_t>(queries.size());
|
||||
dns.questions = Utils::host_to_be<uint16_t>(queries.size());
|
||||
}
|
||||
|
||||
void DNS::add_query(const Query &query) {
|
||||
@@ -219,7 +221,7 @@ void DNS::add_query(const Query &query) {
|
||||
void DNS::add_answer(const string &name, QueryType type, QueryClass qclass, uint32_t ttl, IPv4Address ip) {
|
||||
ResourceRecord *res = make_record(name, type, qclass, ttl, (uint32_t)ip);
|
||||
ans.push_back(res);
|
||||
dns.answers = Utils::to_be<uint16_t>(ans.size());
|
||||
dns.answers = Utils::host_to_be<uint16_t>(ans.size());
|
||||
}
|
||||
|
||||
void DNS::add_answer(const std::string &name, QueryType type, QueryClass qclass,
|
||||
@@ -228,31 +230,31 @@ void DNS::add_answer(const std::string &name, QueryType type, QueryClass qclass,
|
||||
parse_domain_name(dname, new_str);
|
||||
ResourceRecord *res = make_record(name, type, qclass, ttl, new_str);
|
||||
ans.push_back(res);
|
||||
dns.answers = Utils::to_be<uint16_t>(ans.size());
|
||||
dns.answers = Utils::host_to_be<uint16_t>(ans.size());
|
||||
}
|
||||
|
||||
void DNS::add_answer(const std::string &name, QueryType type, QueryClass qclass,
|
||||
uint32_t ttl, const uint8_t *data, uint32_t sz) {
|
||||
ResourceRecord *res = make_record(name, type, qclass, ttl, data, sz);
|
||||
ans.push_back(res);
|
||||
dns.answers = Utils::to_be<uint16_t>(ans.size());
|
||||
dns.answers = Utils::host_to_be<uint16_t>(ans.size());
|
||||
}
|
||||
|
||||
void DNS::add_authority(const string &name, QueryType type,
|
||||
QueryClass qclass, uint32_t ttl, const uint8_t *data, uint32_t sz) {
|
||||
ResourceRecord *res = make_record(name, type, qclass, ttl, data, sz);
|
||||
arity.push_back(res);
|
||||
dns.authority = Utils::to_be<uint16_t>(arity.size());
|
||||
dns.authority = Utils::host_to_be<uint16_t>(arity.size());
|
||||
}
|
||||
|
||||
void DNS::add_additional(const string &name, QueryType type, QueryClass qclass, uint32_t ttl, uint32_t ip) {
|
||||
ResourceRecord *res = make_record(name, type, qclass, ttl, ip);
|
||||
addit.push_back(res);
|
||||
dns.additional = Utils::to_be<uint16_t>(addit.size());
|
||||
dns.additional = Utils::host_to_be<uint16_t>(addit.size());
|
||||
}
|
||||
|
||||
DNS::ResourceRecord *DNS::make_record(const std::string &name, QueryType type, QueryClass qclass, uint32_t ttl, uint32_t ip) {
|
||||
ip = Utils::to_be(ip);
|
||||
ip = Utils::host_to_be(ip);
|
||||
return make_record(name, type, qclass, ttl, reinterpret_cast<uint8_t*>(&ip), sizeof(ip));
|
||||
}
|
||||
|
||||
@@ -266,12 +268,12 @@ DNS::ResourceRecord *DNS::make_record(const std::string &name, QueryType type, Q
|
||||
uint16_t index = find_domain_name(nm);
|
||||
ResourceRecord *res;
|
||||
if(index)
|
||||
res = new OffsetedResourceRecord(Utils::to_be(index), ptr, len);
|
||||
res = new OffsetedResourceRecord(Utils::host_to_be(index), ptr, len);
|
||||
else
|
||||
res = new NamedResourceRecord(nm, ptr, len);
|
||||
res->info.type = Utils::to_be<uint16_t>(type);
|
||||
res->info.qclass = Utils::to_be<uint16_t>(qclass);
|
||||
res->info.ttl = Utils::to_be(ttl);
|
||||
res->info.type = Utils::host_to_be<uint16_t>(type);
|
||||
res->info.qclass = Utils::host_to_be<uint16_t>(qclass);
|
||||
res->info.ttl = Utils::host_to_be(ttl);
|
||||
extra_size += res->size();
|
||||
return res;
|
||||
}
|
||||
@@ -482,7 +484,7 @@ void DNS::convert_resources(const ResourcesType &lst, std::list<Resource> &res)
|
||||
}
|
||||
res.push_back(
|
||||
Resource(dname, addr, Utils::be_to_host((*it)->info.type),
|
||||
Utils::to_be((*it)->info.qclass), Utils::be_to_host((*it)->info.ttl))
|
||||
Utils::host_to_be((*it)->info.qclass), Utils::be_to_host((*it)->info.ttl))
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -525,7 +527,7 @@ uint32_t DNS::ResourceRecord::write(uint8_t *buffer) const {
|
||||
buffer += sz;
|
||||
std::memcpy(buffer, &info, sizeof(info));
|
||||
buffer += sizeof(info);
|
||||
*((uint16_t*)buffer) = Utils::to_be(data.size());
|
||||
*((uint16_t*)buffer) = Utils::host_to_be(data.size());
|
||||
buffer += sizeof(uint16_t);
|
||||
std::copy(data.begin(), data.end(), buffer);
|
||||
return sz + sizeof(info) + sizeof(uint16_t) + data.size();
|
||||
|
||||
@@ -37,7 +37,11 @@
|
||||
#include "utils.h"
|
||||
#include "snap.h"
|
||||
|
||||
using namespace std;
|
||||
using std::pair;
|
||||
using std::vector;
|
||||
using std::string;
|
||||
using std::list;
|
||||
using std::runtime_error;
|
||||
|
||||
namespace Tins {
|
||||
const Dot11::address_type Dot11::BROADCAST = "ff:ff:ff:ff:ff:ff";
|
||||
@@ -67,12 +71,15 @@ Dot11::Dot11(const ieee80211_header *header_ptr)
|
||||
Dot11::Dot11(const uint8_t *buffer, uint32_t total_sz)
|
||||
: PDU(ETHERTYPE_IP), _options_size(0)
|
||||
{
|
||||
if(total_sz < sizeof(_header.control))
|
||||
throw std::runtime_error("Not enough size for an IEEE 802.11 header in the buffer.");
|
||||
/*if(total_sz < sizeof(_header.control))
|
||||
throw runtime_error("Not enough size for an IEEE 802.11 header in the buffer.");
|
||||
uint32_t sz = std::min((uint32_t)sizeof(_header), total_sz);
|
||||
std::memcpy(&_header, buffer, sz);
|
||||
buffer += sz;
|
||||
total_sz -= sz;
|
||||
total_sz -= sz;*/
|
||||
if(total_sz < sizeof(_header))
|
||||
throw runtime_error("Not enough size for an IEEE 802.11 header in the buffer.");
|
||||
std::memcpy(&_header, buffer, sizeof(_header));
|
||||
}
|
||||
|
||||
Dot11::Dot11(const Dot11 &other) : PDU(other) {
|
||||
@@ -221,12 +228,12 @@ void Dot11::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *p
|
||||
PDU *Dot11::from_bytes(const uint8_t *buffer, uint32_t total_sz) {
|
||||
// We only need the control field, the length of the PDU will depend on the flags set.
|
||||
if(total_sz < sizeof(ieee80211_header::control))
|
||||
throw std::runtime_error("Not enough size for a IEEE 802.11 header in the buffer.");
|
||||
throw runtime_error("Not enough size for a IEEE 802.11 header in the buffer.");
|
||||
const ieee80211_header *hdr = (const ieee80211_header*)buffer;
|
||||
PDU *ret = 0;
|
||||
if(hdr->control.type == MANAGEMENT && hdr->control.subtype == 8) {
|
||||
if(total_sz < sizeof(_header))
|
||||
throw std::runtime_error("Not enough size for an IEEE 802.11 header in the buffer.");
|
||||
throw runtime_error("Not enough size for an IEEE 802.11 header in the buffer.");
|
||||
ret = new Dot11Beacon(buffer, total_sz);
|
||||
}
|
||||
else if(hdr->control.type == DATA){
|
||||
@@ -266,15 +273,20 @@ void Dot11::copy_80211_fields(const Dot11 *other) {
|
||||
|
||||
/* Dot11ManagementFrame */
|
||||
|
||||
Dot11ManagementFrame::Dot11ManagementFrame(const uint8_t *buffer, uint32_t total_sz) : Dot11(buffer, total_sz) {
|
||||
Dot11ManagementFrame::Dot11ManagementFrame(const uint8_t *buffer, uint32_t total_sz)
|
||||
: Dot11(buffer, total_sz)
|
||||
{
|
||||
buffer += sizeof(ieee80211_header);
|
||||
total_sz -= sizeof(ieee80211_header);
|
||||
if(total_sz < sizeof(_ext_header))
|
||||
throw std::runtime_error("Not enough size for an IEEE 802.11 header in the buffer.");
|
||||
throw runtime_error("Not enough size for an IEEE 802.11 header in the buffer.");
|
||||
std::memcpy(&_ext_header, buffer, sizeof(_ext_header));
|
||||
if(from_ds() && to_ds())
|
||||
_addr4 = buffer + sizeof(_ext_header);
|
||||
total_sz -= sizeof(_ext_header);
|
||||
if(from_ds() && to_ds() && total_sz >= _addr4.size())
|
||||
//std::memcpy(_addr4, buffer + sizeof(_ext_header), sizeof(_addr4));
|
||||
_addr4 = buffer + sizeof(_ext_header);
|
||||
else
|
||||
throw runtime_error("Not enough size for an IEEE 802.11 header in the buffer.");
|
||||
}
|
||||
|
||||
Dot11ManagementFrame::Dot11ManagementFrame(
|
||||
@@ -626,7 +638,7 @@ Dot11Beacon::Dot11Beacon(const uint8_t *buffer, uint32_t total_sz)
|
||||
buffer += sz;
|
||||
total_sz -= sz;
|
||||
if(total_sz < sizeof(_body))
|
||||
throw std::runtime_error("Not enough size for a IEEE 802.11 beacon header in the buffer.");
|
||||
throw runtime_error("Not enough size for a IEEE 802.11 beacon header in the buffer.");
|
||||
memcpy(&_body, buffer, sizeof(_body));
|
||||
buffer += sizeof(_body);
|
||||
total_sz -= sizeof(_body);
|
||||
@@ -830,12 +842,13 @@ Dot11Disassoc::Dot11Disassoc(const NetworkInterface& iface,
|
||||
memset(&_body, 0, sizeof(_body));
|
||||
}
|
||||
|
||||
Dot11Disassoc::Dot11Disassoc(const uint8_t *buffer, uint32_t total_sz) {
|
||||
Dot11Disassoc::Dot11Disassoc(const uint8_t *buffer, uint32_t total_sz)
|
||||
: Dot11ManagementFrame(buffer, total_sz) {
|
||||
uint32_t sz = management_frame_size();
|
||||
buffer += sz;
|
||||
total_sz -= sz;
|
||||
if(total_sz < sizeof(_body))
|
||||
throw std::runtime_error("Not enough size for a IEEE 802.11 disassociation header in the buffer.");
|
||||
throw runtime_error("Not enough size for a IEEE 802.11 disassociation header in the buffer.");
|
||||
memcpy(&_body, buffer, sizeof(_body));
|
||||
buffer += sizeof(_body);
|
||||
total_sz -= sizeof(_body);
|
||||
@@ -888,7 +901,7 @@ Dot11AssocRequest::Dot11AssocRequest(const uint8_t *buffer, uint32_t total_sz) :
|
||||
buffer += sz;
|
||||
total_sz -= sz;
|
||||
if(total_sz < sizeof(_body))
|
||||
throw std::runtime_error("Not enough size for an IEEE 802.11 association request header in the buffer.");
|
||||
throw runtime_error("Not enough size for an IEEE 802.11 association request header in the buffer.");
|
||||
memcpy(&_body, buffer, sizeof(_body));
|
||||
buffer += sizeof(_body);
|
||||
total_sz -= sizeof(_body);
|
||||
@@ -971,7 +984,7 @@ Dot11AssocResponse::Dot11AssocResponse(const uint8_t *buffer, uint32_t total_sz)
|
||||
buffer += sz;
|
||||
total_sz -= sz;
|
||||
if(total_sz < sizeof(_body))
|
||||
throw std::runtime_error("Not enough size for an IEEE 802.11 association response header in the buffer.");
|
||||
throw runtime_error("Not enough size for an IEEE 802.11 association response header in the buffer.");
|
||||
memcpy(&_body, buffer, sizeof(_body));
|
||||
buffer += sizeof(_body);
|
||||
total_sz -= sizeof(_body);
|
||||
@@ -1042,7 +1055,7 @@ Dot11ReAssocRequest::Dot11ReAssocRequest(const uint8_t *buffer, uint32_t total_s
|
||||
buffer += sz;
|
||||
total_sz -= sz;
|
||||
if(total_sz < sizeof(_body))
|
||||
throw std::runtime_error("Not enough size for an IEEE 802.11 reassociation request header in the buffer.");
|
||||
throw runtime_error("Not enough size for an IEEE 802.11 reassociation request header in the buffer.");
|
||||
memcpy(&_body, buffer, sizeof(_body));
|
||||
buffer += sizeof(_body);
|
||||
total_sz -= sizeof(_body);
|
||||
@@ -1128,7 +1141,7 @@ Dot11ReAssocResponse::Dot11ReAssocResponse(const uint8_t *buffer, uint32_t total
|
||||
buffer += sz;
|
||||
total_sz -= sz;
|
||||
if(total_sz < sizeof(_body))
|
||||
throw std::runtime_error("Not enough size for an IEEE 802.11 reassociation response header in the buffer.");
|
||||
throw runtime_error("Not enough size for an IEEE 802.11 reassociation response header in the buffer.");
|
||||
memcpy(&_body, buffer, sizeof(_body));
|
||||
buffer += sizeof(_body);
|
||||
total_sz -= sizeof(_body);
|
||||
@@ -1244,7 +1257,7 @@ Dot11ProbeResponse::Dot11ProbeResponse(const uint8_t *buffer, uint32_t total_sz)
|
||||
buffer += sz;
|
||||
total_sz -= sz;
|
||||
if(total_sz < sizeof(_body))
|
||||
throw std::runtime_error("Not enough size for an IEEE 802.11 probe response header in the buffer.");
|
||||
throw runtime_error("Not enough size for an IEEE 802.11 probe response header in the buffer.");
|
||||
memcpy(&_body, buffer, sizeof(_body));
|
||||
buffer += sizeof(_body);
|
||||
total_sz -= sizeof(_body);
|
||||
@@ -1393,7 +1406,7 @@ Dot11Authentication::Dot11Authentication(const uint8_t *buffer, uint32_t total_s
|
||||
buffer += sz;
|
||||
total_sz -= sz;
|
||||
if(total_sz < sizeof(_body))
|
||||
throw std::runtime_error("Not enough size for an IEEE 802.11 authentication header in the buffer.");
|
||||
throw runtime_error("Not enough size for an IEEE 802.11 authentication header in the buffer.");
|
||||
memcpy(&_body, buffer, sizeof(_body));
|
||||
buffer += sizeof(_body);
|
||||
total_sz -= sizeof(_body);
|
||||
@@ -1459,7 +1472,7 @@ Dot11Deauthentication::Dot11Deauthentication(const uint8_t *buffer, uint32_t tot
|
||||
buffer += sz;
|
||||
total_sz -= sz;
|
||||
if(total_sz < sizeof(_body))
|
||||
throw std::runtime_error("Not enough size for a IEEE 802.11 deauthentication header in the buffer.");
|
||||
throw runtime_error("Not enough size for a IEEE 802.11 deauthentication header in the buffer.");
|
||||
memcpy(&_body, buffer, sizeof(_body));
|
||||
buffer += sizeof(_body);
|
||||
total_sz -= sizeof(_body);
|
||||
@@ -1497,13 +1510,13 @@ Dot11Data::Dot11Data(const uint8_t *buffer, uint32_t total_sz)
|
||||
buffer += sz;
|
||||
total_sz -= sz;
|
||||
if(total_sz < sizeof(_ext_header))
|
||||
throw std::runtime_error("Not enough size for an IEEE 802.11 data header in the buffer.");
|
||||
throw runtime_error("Not enough size for an IEEE 802.11 data header in the buffer.");
|
||||
std::memcpy(&_ext_header, buffer, sizeof(_ext_header));
|
||||
buffer += sizeof(_ext_header);
|
||||
total_sz -= sizeof(_ext_header);
|
||||
if(from_ds() && to_ds()) {
|
||||
if(total_sz < sizeof(_addr4))
|
||||
throw std::runtime_error("Not enough size for an IEEE 802.11 data header in the buffer.");
|
||||
throw runtime_error("Not enough size for an IEEE 802.11 data header in the buffer.");
|
||||
std::memcpy(&_addr4, buffer, sizeof(_addr4));
|
||||
buffer += sizeof(_addr4);
|
||||
total_sz -= sizeof(_addr4);
|
||||
@@ -1600,12 +1613,14 @@ Dot11QoSData::Dot11QoSData(const NetworkInterface &iface,
|
||||
}
|
||||
|
||||
Dot11QoSData::Dot11QoSData(const uint8_t *buffer, uint32_t total_sz)
|
||||
: Dot11Data(buffer, std::min(data_frame_size(), total_sz)) {
|
||||
// Am I breaking something? :S
|
||||
//: Dot11Data(buffer, std::min(data_frame_size(), total_sz)) {
|
||||
: Dot11Data(buffer, total_sz) {
|
||||
uint32_t sz = data_frame_size();
|
||||
buffer += sz;
|
||||
total_sz -= sz;
|
||||
if(total_sz < sizeof(this->_qos_control))
|
||||
throw std::runtime_error("Not enough size for an IEEE 802.11 data header in the buffer.");
|
||||
throw runtime_error("Not enough size for an IEEE 802.11 data header in the buffer.");
|
||||
this->_qos_control = *(uint16_t*)buffer;
|
||||
total_sz -= sizeof(uint16_t);
|
||||
buffer += sizeof(uint16_t);
|
||||
@@ -1688,7 +1703,7 @@ Dot11ControlTA::Dot11ControlTA(const uint8_t *buffer, uint32_t total_sz) : Dot11
|
||||
buffer += sizeof(ieee80211_header);
|
||||
total_sz -= sizeof(ieee80211_header);
|
||||
if(total_sz < sizeof(_taddr))
|
||||
throw std::runtime_error("Not enough size for an IEEE 802.11 RTS frame in the buffer.");
|
||||
throw runtime_error("Not enough size for an IEEE 802.11 RTS frame in the buffer.");
|
||||
std::memcpy(_taddr, buffer, sizeof(_taddr));
|
||||
}
|
||||
|
||||
@@ -1859,7 +1874,7 @@ Dot11BlockAckRequest::Dot11BlockAckRequest(const uint8_t *buffer, uint32_t total
|
||||
buffer += padding;
|
||||
total_sz -= padding;
|
||||
if(total_sz < sizeof(_bar_control) + sizeof(_start_sequence))
|
||||
throw std::runtime_error("Not enough size for an IEEE 802.11 Block Ack frame in the buffer.");
|
||||
throw runtime_error("Not enough size for an IEEE 802.11 Block Ack frame in the buffer.");
|
||||
std::memcpy(&_bar_control, buffer, sizeof(_bar_control));
|
||||
buffer += sizeof(_bar_control);
|
||||
std::memcpy(&_start_sequence, buffer, sizeof(_start_sequence));
|
||||
@@ -1920,7 +1935,7 @@ Dot11BlockAck::Dot11BlockAck(const uint8_t *buffer, uint32_t total_sz) : Dot11Co
|
||||
buffer += padding;
|
||||
total_sz -= padding;
|
||||
if(total_sz < sizeof(_bitmap) + sizeof(_bar_control) + sizeof(_start_sequence))
|
||||
throw std::runtime_error("Not enough size for an IEEE 802.11 Block Ack frame in the buffer.");
|
||||
throw runtime_error("Not enough size for an IEEE 802.11 Block Ack frame in the buffer.");
|
||||
std::memcpy(&_bar_control, buffer, sizeof(_bar_control));
|
||||
buffer += sizeof(_bar_control);
|
||||
std::memcpy(&_start_sequence, buffer, sizeof(_start_sequence));
|
||||
|
||||
@@ -67,8 +67,8 @@ void Tins::EAPOL::packet_type(uint8_t new_ptype) {
|
||||
_header.packet_type = new_ptype;
|
||||
}
|
||||
|
||||
void Tins::EAPOL::length(uint8_t new_length) {
|
||||
_header.length = Utils::net_to_host_s(new_length);
|
||||
void Tins::EAPOL::length(uint16_t new_length) {
|
||||
_header.length = new_length;
|
||||
}
|
||||
|
||||
void Tins::EAPOL::type(uint8_t new_type) {
|
||||
@@ -127,11 +127,11 @@ Tins::RC4EAPOL::~RC4EAPOL() {
|
||||
}
|
||||
|
||||
void Tins::RC4EAPOL::key_length(uint16_t new_key_length) {
|
||||
_header.key_length = Utils::net_to_host_s(new_key_length);
|
||||
_header.key_length = Utils::host_to_be(new_key_length);
|
||||
}
|
||||
|
||||
void Tins::RC4EAPOL::replay_counter(uint16_t new_replay_counter) {
|
||||
_header.replay_counter = Utils::net_to_host_s(new_replay_counter);
|
||||
_header.replay_counter = Utils::host_to_be(new_replay_counter);
|
||||
}
|
||||
|
||||
void Tins::RC4EAPOL::key_iv(const uint8_t *new_key_iv) {
|
||||
@@ -165,7 +165,7 @@ void Tins::RC4EAPOL::write_body(uint8_t *buffer, uint32_t total_sz) {
|
||||
uint32_t sz = sizeof(_header) + _key_size;
|
||||
assert(total_sz >= sz);
|
||||
if(_key)
|
||||
_header.key_length = Utils::net_to_host_s(_key_size);
|
||||
_header.key_length = Utils::host_to_be(_key_size);
|
||||
std::memcpy(buffer, &_header, sizeof(_header));
|
||||
buffer += sizeof(_header);
|
||||
if(_key)
|
||||
@@ -233,11 +233,11 @@ void Tins::RSNEAPOL::RSNEAPOL::nonce(const uint8_t *new_nonce) {
|
||||
}
|
||||
|
||||
void Tins::RSNEAPOL::rsc(uint64_t new_rsc) {
|
||||
_header.rsc = Utils::net_to_host_ll(new_rsc);
|
||||
_header.rsc = Utils::host_to_be(new_rsc);
|
||||
}
|
||||
|
||||
void Tins::RSNEAPOL::id(uint64_t new_id) {
|
||||
_header.id = Utils::net_to_host_ll(new_id);
|
||||
_header.id = Utils::host_to_be(new_id);
|
||||
}
|
||||
|
||||
void Tins::RSNEAPOL::mic(const uint8_t *new_mic) {
|
||||
@@ -245,7 +245,7 @@ void Tins::RSNEAPOL::mic(const uint8_t *new_mic) {
|
||||
}
|
||||
|
||||
void Tins::RSNEAPOL::wpa_length(uint16_t new_wpa_length) {
|
||||
_header.wpa_length = Utils::net_to_host_s(new_wpa_length);
|
||||
_header.wpa_length = Utils::host_to_be(new_wpa_length);
|
||||
}
|
||||
|
||||
void Tins::RSNEAPOL::key(const uint8_t *new_key, uint32_t sz) {
|
||||
@@ -273,7 +273,7 @@ void Tins::RSNEAPOL::write_body(uint8_t *buffer, uint32_t total_sz) {
|
||||
assert(total_sz >= sz);
|
||||
if(_key) {
|
||||
if(!_header.key_type) {
|
||||
_header.key_length = Utils::net_to_host_s(32);
|
||||
_header.key_length = Utils::host_to_be<uint16_t>(32);
|
||||
wpa_length(_key_size);
|
||||
}
|
||||
else if(_key_size) {
|
||||
|
||||
@@ -100,8 +100,8 @@ bool EthernetII::send(PacketSender* sender) {
|
||||
|
||||
memset(&addr, 0, sizeof(struct sockaddr_ll));
|
||||
|
||||
addr.sll_family = Utils::net_to_host_s(PF_PACKET);
|
||||
addr.sll_protocol = Utils::net_to_host_s(ETH_P_ALL);
|
||||
addr.sll_family = Utils::host_to_be<uint16_t>(PF_PACKET);
|
||||
addr.sll_protocol = Utils::host_to_be<uint16_t>(ETH_P_ALL);
|
||||
addr.sll_halen = ADDR_SIZE;
|
||||
addr.sll_ifindex = _iface.id();
|
||||
memcpy(&(addr.sll_addr), _eth.dst_mac, ADDR_SIZE);
|
||||
@@ -146,8 +146,8 @@ PDU *EthernetII::recv_response(PacketSender *sender) {
|
||||
struct sockaddr_ll addr;
|
||||
memset(&addr, 0, sizeof(struct sockaddr_ll));
|
||||
|
||||
addr.sll_family = Utils::net_to_host_s(PF_PACKET);
|
||||
addr.sll_protocol = Utils::net_to_host_s(ETH_P_ALL);
|
||||
addr.sll_family = Utils::host_to_be<uint16_t>(PF_PACKET);
|
||||
addr.sll_protocol = Utils::host_to_be<uint16_t>(ETH_P_ALL);
|
||||
addr.sll_halen = ADDR_SIZE;
|
||||
addr.sll_ifindex = _iface.id();
|
||||
memcpy(&(addr.sll_addr), _eth.dst_mac, ADDR_SIZE);
|
||||
|
||||
12
src/icmp.cpp
12
src/icmp.cpp
@@ -66,23 +66,23 @@ void Tins::ICMP::type(Flags new_type) {
|
||||
}
|
||||
|
||||
void Tins::ICMP::check(uint16_t new_check) {
|
||||
_icmp.check = Utils::to_be(new_check);
|
||||
_icmp.check = Utils::host_to_be(new_check);
|
||||
}
|
||||
|
||||
void Tins::ICMP::id(uint16_t new_id) {
|
||||
_icmp.un.echo.id = Utils::to_be(new_id);
|
||||
_icmp.un.echo.id = Utils::host_to_be(new_id);
|
||||
}
|
||||
|
||||
void Tins::ICMP::sequence(uint16_t new_seq) {
|
||||
_icmp.un.echo.sequence = Utils::to_be(new_seq);
|
||||
_icmp.un.echo.sequence = Utils::host_to_be(new_seq);
|
||||
}
|
||||
|
||||
void Tins::ICMP::gateway(uint32_t new_gw) {
|
||||
_icmp.un.gateway = Utils::to_be(new_gw);
|
||||
_icmp.un.gateway = Utils::host_to_be(new_gw);
|
||||
}
|
||||
|
||||
void Tins::ICMP::mtu(uint16_t new_mtu) {
|
||||
_icmp.un.frag.mtu = Utils::to_be(new_mtu);
|
||||
_icmp.un.frag.mtu = Utils::host_to_be(new_mtu);
|
||||
}
|
||||
|
||||
void Tins::ICMP::pointer(uint8_t new_pointer) {
|
||||
@@ -171,7 +171,7 @@ void Tins::ICMP::write_serialization(uint8_t *buffer, uint32_t total_sz, const P
|
||||
Utils::do_checksum((uint8_t*)&_icmp, ((uint8_t*)&_icmp) + sizeof(icmphdr));
|
||||
while (checksum >> 16)
|
||||
checksum = (checksum & 0xffff) + (checksum >> 16);
|
||||
_icmp.check = Utils::to_be<uint16_t>(~checksum);
|
||||
_icmp.check = Utils::host_to_be<uint16_t>(~checksum);
|
||||
}
|
||||
memcpy(buffer, &_icmp, sizeof(icmphdr));
|
||||
_icmp.check = 0;
|
||||
|
||||
@@ -73,7 +73,7 @@ void IEEE802_3::iface(const NetworkInterface &new_iface) {
|
||||
}
|
||||
|
||||
void IEEE802_3::length(uint16_t new_length) {
|
||||
this->_eth.length = Utils::net_to_host_s(new_length);
|
||||
this->_eth.length = Utils::host_to_be(new_length);
|
||||
}
|
||||
|
||||
uint32_t IEEE802_3::header_size() const {
|
||||
@@ -85,8 +85,8 @@ bool IEEE802_3::send(PacketSender* sender) {
|
||||
|
||||
memset(&addr, 0, sizeof(struct sockaddr_ll));
|
||||
|
||||
addr.sll_family = Utils::net_to_host_s(PF_PACKET);
|
||||
addr.sll_protocol = Utils::net_to_host_s(ETH_P_ALL);
|
||||
addr.sll_family = Utils::host_to_be<uint16_t>(PF_PACKET);
|
||||
addr.sll_protocol = Utils::host_to_be<uint16_t>(ETH_P_ALL);
|
||||
addr.sll_halen = address_type::address_size;
|
||||
addr.sll_ifindex = _iface.id();
|
||||
memcpy(&(addr.sll_addr), _eth.dst_mac, sizeof(_eth.dst_mac));
|
||||
@@ -110,7 +110,7 @@ void IEEE802_3::write_serialization(uint8_t *buffer, uint32_t total_sz, const PD
|
||||
assert(total_sz >= my_sz);
|
||||
|
||||
if (set_length)
|
||||
_eth.length = Utils::net_to_host_s(size() - sizeof(_eth));
|
||||
_eth.length = Utils::host_to_be(size() - sizeof(_eth));
|
||||
|
||||
memcpy(buffer, &_eth, sizeof(ethhdr));
|
||||
|
||||
@@ -122,8 +122,8 @@ PDU *IEEE802_3::recv_response(PacketSender *sender) {
|
||||
struct sockaddr_ll addr;
|
||||
memset(&addr, 0, sizeof(struct sockaddr_ll));
|
||||
|
||||
addr.sll_family = Utils::net_to_host_s(PF_PACKET);
|
||||
addr.sll_protocol = Utils::net_to_host_s(ETH_P_802_3);
|
||||
addr.sll_family = Utils::host_to_be<uint16_t>(PF_PACKET);
|
||||
addr.sll_protocol = Utils::host_to_be<uint16_t>(ETH_P_802_3);
|
||||
addr.sll_halen = address_type::address_size;
|
||||
addr.sll_ifindex = _iface.id();
|
||||
memcpy(&(addr.sll_addr), _eth.dst_mac, sizeof(_eth.dst_mac));
|
||||
|
||||
10
src/ip.cpp
10
src/ip.cpp
@@ -145,15 +145,15 @@ void Tins::IP::tos(uint8_t new_tos) {
|
||||
}
|
||||
|
||||
void Tins::IP::tot_len(uint16_t new_tot_len) {
|
||||
_ip.tot_len = Utils::to_be(new_tot_len);
|
||||
_ip.tot_len = Utils::host_to_be(new_tot_len);
|
||||
}
|
||||
|
||||
void Tins::IP::id(uint16_t new_id) {
|
||||
_ip.id = Utils::to_be(new_id);
|
||||
_ip.id = Utils::host_to_be(new_id);
|
||||
}
|
||||
|
||||
void Tins::IP::frag_off(uint16_t new_frag_off) {
|
||||
_ip.frag_off = Utils::to_be(new_frag_off);
|
||||
_ip.frag_off = Utils::host_to_be(new_frag_off);
|
||||
}
|
||||
|
||||
void Tins::IP::ttl(uint8_t new_ttl) {
|
||||
@@ -165,7 +165,7 @@ void Tins::IP::protocol(uint8_t new_protocol) {
|
||||
}
|
||||
|
||||
void Tins::IP::check(uint16_t new_check) {
|
||||
_ip.check = Utils::to_be(new_check);
|
||||
_ip.check = Utils::host_to_be(new_check);
|
||||
}
|
||||
|
||||
|
||||
@@ -304,7 +304,7 @@ void Tins::IP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU
|
||||
uint32_t checksum = Utils::do_checksum(buffer, buffer + sizeof(_ip) + _padded_options_size);
|
||||
while (checksum >> 16)
|
||||
checksum = (checksum & 0xffff) + (checksum >> 16);
|
||||
((iphdr*)buffer)->check = Utils::to_be<uint16_t>(~checksum);
|
||||
((iphdr*)buffer)->check = Utils::host_to_be<uint16_t>(~checksum);
|
||||
this->check(0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ Tins::SNAP::SNAP(const uint8_t *buffer, uint32_t total_sz) : PDU(0xff) {
|
||||
std::memcpy(&_snap, buffer, sizeof(_snap));
|
||||
buffer += sizeof(_snap);
|
||||
total_sz -= sizeof(_snap);
|
||||
switch(Utils::net_to_host_s(_snap.eth_type)) {
|
||||
switch(eth_type()) {
|
||||
case Tins::Constants::Ethernet::IP:
|
||||
inner_pdu(new Tins::IP(buffer, total_sz));
|
||||
break;
|
||||
@@ -61,7 +61,7 @@ Tins::SNAP::SNAP(const SNAP &other) : PDU(other) {
|
||||
copy_fields(&other);
|
||||
}
|
||||
|
||||
Tins::SNAP &Tins::SNAP::operator= (const SNAP &other) {
|
||||
Tins::SNAP &Tins::SNAP::operator=(const SNAP &other) {
|
||||
copy_fields(&other);
|
||||
copy_inner_pdu(other);
|
||||
return *this;
|
||||
|
||||
30
src/tcp.cpp
30
src/tcp.cpp
@@ -88,31 +88,31 @@ Tins::TCP::TCP(const uint8_t *buffer, uint32_t total_sz)
|
||||
}
|
||||
|
||||
void Tins::TCP::dport(uint16_t new_dport) {
|
||||
_tcp.dport = Utils::to_be(new_dport);
|
||||
_tcp.dport = Utils::host_to_be(new_dport);
|
||||
}
|
||||
|
||||
void Tins::TCP::sport(uint16_t new_sport) {
|
||||
_tcp.sport = Utils::to_be(new_sport);
|
||||
_tcp.sport = Utils::host_to_be(new_sport);
|
||||
}
|
||||
|
||||
void Tins::TCP::seq(uint32_t new_seq) {
|
||||
_tcp.seq = Utils::to_be(new_seq);
|
||||
_tcp.seq = Utils::host_to_be(new_seq);
|
||||
}
|
||||
|
||||
void Tins::TCP::ack_seq(uint32_t new_ack_seq) {
|
||||
_tcp.ack_seq = Utils::to_be(new_ack_seq);
|
||||
_tcp.ack_seq = Utils::host_to_be(new_ack_seq);
|
||||
}
|
||||
|
||||
void Tins::TCP::window(uint16_t new_window) {
|
||||
_tcp.window = Utils::to_be(new_window);
|
||||
_tcp.window = Utils::host_to_be(new_window);
|
||||
}
|
||||
|
||||
void Tins::TCP::check(uint16_t new_check) {
|
||||
_tcp.check = Utils::to_be(new_check);
|
||||
_tcp.check = Utils::host_to_be(new_check);
|
||||
}
|
||||
|
||||
void Tins::TCP::urg_ptr(uint16_t new_urg_ptr) {
|
||||
_tcp.urg_ptr = Utils::to_be(new_urg_ptr);
|
||||
_tcp.urg_ptr = Utils::host_to_be(new_urg_ptr);
|
||||
}
|
||||
|
||||
void Tins::TCP::payload(uint8_t *new_payload, uint32_t new_payload_size) {
|
||||
@@ -124,14 +124,14 @@ void Tins::TCP::data_offset(uint8_t new_doff) {
|
||||
}
|
||||
|
||||
void Tins::TCP::add_mss_option(uint16_t value) {
|
||||
value = Utils::to_be(value);
|
||||
value = Utils::host_to_be(value);
|
||||
add_option(MSS, 2, (uint8_t*)&value);
|
||||
}
|
||||
|
||||
bool Tins::TCP::search_mss_option(uint16_t *value) {
|
||||
if(!generic_search(MSS, value))
|
||||
return false;
|
||||
*value = Utils::to_be(*value);
|
||||
*value = Utils::host_to_be(*value);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -157,7 +157,7 @@ void Tins::TCP::add_sack_option(const std::list<uint32_t> &edges) {
|
||||
value = new uint32_t[edges.size()];
|
||||
uint32_t *ptr = value;
|
||||
for(std::list<uint32_t>::const_iterator it = edges.begin(); it != edges.end(); ++it)
|
||||
*(ptr++) = Utils::to_be(*it);
|
||||
*(ptr++) = Utils::host_to_be(*it);
|
||||
}
|
||||
add_option(SACK, (uint8_t)(sizeof(uint32_t) * edges.size()), (const uint8_t*)value);
|
||||
delete[] value;
|
||||
@@ -170,12 +170,12 @@ bool Tins::TCP::search_sack_option(std::list<uint32_t> *edges) {
|
||||
const uint32_t *ptr = (const uint32_t*)&option->value[0];
|
||||
const uint32_t *end = ptr + (option->value.size() / sizeof(uint32_t));
|
||||
while(ptr < end)
|
||||
edges->push_back(Utils::to_be(*(ptr++)));
|
||||
edges->push_back(Utils::host_to_be(*(ptr++)));
|
||||
return true;
|
||||
}
|
||||
|
||||
void Tins::TCP::add_timestamp_option(uint32_t value, uint32_t reply) {
|
||||
uint64_t buffer = ((uint64_t)Utils::to_be(reply) << 32) | Utils::to_be(value);
|
||||
uint64_t buffer = ((uint64_t)Utils::host_to_be(reply) << 32) | Utils::host_to_be(value);
|
||||
add_option(TSOPT, 8, (uint8_t*)&buffer);
|
||||
}
|
||||
|
||||
@@ -184,8 +184,8 @@ bool Tins::TCP::search_timestamp_option(uint32_t *value, uint32_t *reply) {
|
||||
if(!option || option->value.size() != (sizeof(uint32_t) << 1))
|
||||
return false;
|
||||
const uint32_t *ptr = (const uint32_t*)&option->value[0];
|
||||
*value = Utils::to_be(*(ptr++));
|
||||
*reply = Utils::to_be(*(ptr));
|
||||
*value = Utils::host_to_be(*(ptr++));
|
||||
*reply = Utils::host_to_be(*(ptr));
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -304,7 +304,7 @@ void Tins::TCP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PD
|
||||
while (checksum >> 16)
|
||||
checksum = (checksum & 0xffff) + (checksum >> 16);
|
||||
|
||||
((tcphdr*)tcp_start)->check = Utils::to_be<uint16_t>(~checksum);
|
||||
((tcphdr*)tcp_start)->check = Utils::host_to_be<uint16_t>(~checksum);
|
||||
}
|
||||
_tcp.check = 0;
|
||||
}
|
||||
|
||||
@@ -52,15 +52,15 @@ void Tins::UDP::payload(uint8_t *new_payload, uint32_t new_payload_size) {
|
||||
}
|
||||
|
||||
void Tins::UDP::dport(uint16_t new_dport) {
|
||||
_udp.dport = Utils::to_be(new_dport);
|
||||
_udp.dport = Utils::host_to_be(new_dport);
|
||||
}
|
||||
|
||||
void Tins::UDP::sport(uint16_t new_sport) {
|
||||
_udp.sport = Utils::to_be(new_sport);
|
||||
_udp.sport = Utils::host_to_be(new_sport);
|
||||
}
|
||||
|
||||
void Tins::UDP::length(uint16_t new_len) {
|
||||
_udp.len = Utils::to_be(new_len);
|
||||
_udp.len = Utils::host_to_be(new_len);
|
||||
}
|
||||
|
||||
uint32_t Tins::UDP::header_size() const {
|
||||
@@ -78,7 +78,7 @@ void Tins::UDP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PD
|
||||
Utils::do_checksum(buffer, buffer + total_sz);
|
||||
while (checksum >> 16)
|
||||
checksum = (checksum & 0xffff)+(checksum >> 16);
|
||||
((udphdr*)buffer)->check = Utils::to_be<uint16_t>(~checksum);
|
||||
((udphdr*)buffer)->check = Utils::host_to_be<uint16_t>(~checksum);
|
||||
}
|
||||
_udp.check = 0;
|
||||
}
|
||||
|
||||
@@ -66,7 +66,9 @@ struct IPv4Collector {
|
||||
}
|
||||
};
|
||||
|
||||
bool Tins::Utils::Internals::from_hex(const string &str, uint32_t &result) {
|
||||
namespace Tins {
|
||||
|
||||
bool Utils::Internals::from_hex(const string &str, uint32_t &result) {
|
||||
unsigned i(0);
|
||||
result = 0;
|
||||
while(i < str.size()) {
|
||||
@@ -83,7 +85,7 @@ bool Tins::Utils::Internals::from_hex(const string &str, uint32_t &result) {
|
||||
return true;
|
||||
}
|
||||
|
||||
void Tins::Utils::Internals::skip_line(istream &input) {
|
||||
void Utils::Internals::skip_line(istream &input) {
|
||||
int c = 0;
|
||||
while(c != '\n' && input)
|
||||
c = input.get();
|
||||
@@ -91,7 +93,7 @@ void Tins::Utils::Internals::skip_line(istream &input) {
|
||||
|
||||
/** \endcond */
|
||||
|
||||
uint32_t Tins::Utils::ip_to_int(const string &ip) throw (std::runtime_error) {
|
||||
uint32_t Utils::ip_to_int(const string &ip) throw (std::runtime_error) {
|
||||
uint32_t result(0), i(0), end, bytes_found(0);
|
||||
while(i < ip.size() && bytes_found < 4) {
|
||||
uint16_t this_byte(0);
|
||||
@@ -115,7 +117,7 @@ uint32_t Tins::Utils::ip_to_int(const string &ip) throw (std::runtime_error) {
|
||||
return result;
|
||||
}
|
||||
|
||||
string Tins::Utils::ip_to_string(uint32_t ip) {
|
||||
string Utils::ip_to_string(uint32_t ip) {
|
||||
ostringstream oss;
|
||||
int mask(24);
|
||||
while(mask >=0) {
|
||||
@@ -127,14 +129,14 @@ string Tins::Utils::ip_to_string(uint32_t ip) {
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
uint32_t Tins::Utils::resolve_ip(const string &to_resolve) {
|
||||
IPv4Address Utils::resolve_ip(const string &to_resolve) {
|
||||
struct hostent *data = gethostbyname(to_resolve.c_str());
|
||||
if(!data)
|
||||
throw std::runtime_error("Could not resolve IP");
|
||||
return Utils::net_to_host_l(((struct in_addr**)data->h_addr_list)[0]->s_addr);
|
||||
return be_to_host(((struct in_addr**)data->h_addr_list)[0]->s_addr);
|
||||
}
|
||||
|
||||
Tins::PDU *Tins::Utils::ping_address(IPv4Address ip, PacketSender *sender, IPv4Address ip_src) {
|
||||
PDU *Utils::ping_address(IPv4Address ip, PacketSender *sender, IPv4Address ip_src) {
|
||||
if(!ip_src) {
|
||||
try {
|
||||
NetworkInterface iface(ip);
|
||||
@@ -148,7 +150,7 @@ Tins::PDU *Tins::Utils::ping_address(IPv4Address ip, PacketSender *sender, IPv4A
|
||||
return sender->send_recv(&ip_packet);
|
||||
}
|
||||
|
||||
bool Tins::Utils::resolve_hwaddr(const NetworkInterface &iface, IPv4Address ip,
|
||||
bool Utils::resolve_hwaddr(const NetworkInterface &iface, IPv4Address ip,
|
||||
HWAddress<6> *address, PacketSender *sender)
|
||||
{
|
||||
IPv4Address my_ip;
|
||||
@@ -165,7 +167,7 @@ bool Tins::Utils::resolve_hwaddr(const NetworkInterface &iface, IPv4Address ip,
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Tins::Utils::gateway_from_ip(IPv4Address ip, IPv4Address &gw_addr) {
|
||||
bool Utils::gateway_from_ip(IPv4Address ip, IPv4Address &gw_addr) {
|
||||
typedef std::vector<RouteEntry> entries_type;
|
||||
entries_type entries;
|
||||
uint32_t ip_int = ip;
|
||||
@@ -179,17 +181,17 @@ bool Tins::Utils::gateway_from_ip(IPv4Address ip, IPv4Address &gw_addr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
set<string> Tins::Utils::network_interfaces() {
|
||||
set<string> Utils::network_interfaces() {
|
||||
InterfaceCollector collector;
|
||||
generic_iface_loop(collector);
|
||||
return collector.ifaces;
|
||||
}
|
||||
|
||||
uint16_t Tins::Utils::channel_to_mhz(uint16_t channel) {
|
||||
uint16_t Utils::channel_to_mhz(uint16_t channel) {
|
||||
return 2407 + (channel * 5);
|
||||
}
|
||||
|
||||
uint32_t Tins::Utils::do_checksum(const uint8_t *start, const uint8_t *end) {
|
||||
uint32_t Utils::do_checksum(const uint8_t *start, const uint8_t *end) {
|
||||
uint32_t checksum(0);
|
||||
uint16_t *ptr = (uint16_t*)start, *last = (uint16_t*)end, padding(0);
|
||||
if(((end - start) & 1) == 1) {
|
||||
@@ -197,14 +199,14 @@ uint32_t Tins::Utils::do_checksum(const uint8_t *start, const uint8_t *end) {
|
||||
padding = *(end - 1) << 8;
|
||||
}
|
||||
while(ptr < last)
|
||||
checksum += Utils::net_to_host_s(*(ptr++));
|
||||
checksum += Utils::host_to_be(*(ptr++));
|
||||
return checksum + padding;
|
||||
}
|
||||
|
||||
uint32_t Tins::Utils::pseudoheader_checksum(IPv4Address source_ip, IPv4Address dest_ip, uint32_t len, uint32_t flag) {
|
||||
uint32_t Utils::pseudoheader_checksum(IPv4Address source_ip, IPv4Address dest_ip, uint32_t len, uint32_t flag) {
|
||||
uint32_t checksum(0);
|
||||
uint32_t source_ip_int = Utils::to_be<uint32_t>(source_ip),
|
||||
dest_ip_int = Utils::to_be<uint32_t>(dest_ip);
|
||||
uint32_t source_ip_int = Utils::host_to_be<uint32_t>(source_ip),
|
||||
dest_ip_int = Utils::host_to_be<uint32_t>(dest_ip);
|
||||
uint16_t *ptr = (uint16_t*)&source_ip_int;
|
||||
|
||||
checksum += (uint32_t)(*ptr) + (uint32_t)(*(ptr+1));
|
||||
@@ -214,7 +216,7 @@ uint32_t Tins::Utils::pseudoheader_checksum(IPv4Address source_ip, IPv4Address d
|
||||
return checksum;
|
||||
}
|
||||
|
||||
uint32_t Tins::Utils::crc32(const uint8_t* data, uint32_t data_size) {
|
||||
uint32_t Utils::crc32(const uint8_t* data, uint32_t data_size) {
|
||||
uint32_t i, crc = 0;
|
||||
static uint32_t crc_table[] = {
|
||||
0x4DBDF21C, 0x500AE278, 0x76D3D2D4, 0x6B64C2B0,
|
||||
@@ -230,3 +232,4 @@ uint32_t Tins::Utils::crc32(const uint8_t* data, uint32_t data_size) {
|
||||
|
||||
return crc;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,3 +31,5 @@ $(EXECUTABLE): $(OBJECTS)
|
||||
|
||||
clean:
|
||||
rm $(OBJECTS) $(EXECUTABLE)
|
||||
|
||||
-include depends.d
|
||||
|
||||
@@ -67,25 +67,6 @@ src/dns.o: src/dns.cpp ../include/dns.h ../include/pdu.h \
|
||||
|
||||
../include/network_interface.h:
|
||||
|
||||
../include/utils.h:
|
||||
src/dot11.o: src/dot11.cpp ../include/dot11.h ../include/pdu.h \
|
||||
../include/packetsender.h ../include/utils.h ../include/ipaddress.h \
|
||||
../include/hwaddress.h ../include/network_interface.h ../include/utils.h
|
||||
|
||||
../include/dot11.h:
|
||||
|
||||
../include/pdu.h:
|
||||
|
||||
../include/packetsender.h:
|
||||
|
||||
../include/utils.h:
|
||||
|
||||
../include/ipaddress.h:
|
||||
|
||||
../include/hwaddress.h:
|
||||
|
||||
../include/network_interface.h:
|
||||
|
||||
../include/utils.h:
|
||||
src/ethernetII_test.o: src/ethernetII_test.cpp ../include/ethernetII.h \
|
||||
../include/pdu.h ../include/packetsender.h ../include/utils.h \
|
||||
|
||||
@@ -82,7 +82,7 @@ TEST_F(UtilsTest, IpToString) {
|
||||
|
||||
|
||||
TEST_F(UtilsTest, ResolveIp) {
|
||||
uint32_t localhost_ip = Utils::ip_to_int("127.0.0.1");
|
||||
IPv4Address localhost_ip("127.0.0.1");
|
||||
|
||||
EXPECT_EQ(Utils::resolve_ip("localhost"), localhost_ip);
|
||||
EXPECT_THROW(Utils::resolve_ip("www.qwertyuiopasdfg.com.ar.edu.gov"), std::runtime_error);
|
||||
|
||||
Reference in New Issue
Block a user