mirror of
https://github.com/mfontanini/libtins
synced 2026-01-29 13:04:28 +01:00
Moved endianness change functions to endianness.h.
This commit is contained in:
@@ -27,7 +27,6 @@
|
||||
#include "ip.h"
|
||||
#include "ethernetII.h"
|
||||
#include "rawpdu.h"
|
||||
#include "utils.h"
|
||||
#include "constants.h"
|
||||
|
||||
|
||||
@@ -52,7 +51,7 @@ ARP::ARP(ipaddress_type target_ip, ipaddress_type sender_ip,
|
||||
}
|
||||
|
||||
ARP::ARP(const uint8_t *buffer, uint32_t total_sz)
|
||||
: PDU(Utils::host_to_be<uint16_t>(Constants::Ethernet::ARP))
|
||||
: PDU(Endian::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 +78,11 @@ void ARP::target_ip_addr(ipaddress_type new_tgt_ip_addr) {
|
||||
}
|
||||
|
||||
void ARP::hw_addr_format(uint16_t new_hw_addr_fmt) {
|
||||
this->_arp.ar_hrd = Utils::host_to_be(new_hw_addr_fmt);
|
||||
this->_arp.ar_hrd = Endian::host_to_be(new_hw_addr_fmt);
|
||||
}
|
||||
|
||||
void ARP::prot_addr_format(uint16_t new_prot_addr_fmt) {
|
||||
this->_arp.ar_pro = Utils::host_to_be(new_prot_addr_fmt);
|
||||
this->_arp.ar_pro = Endian::host_to_be(new_prot_addr_fmt);
|
||||
}
|
||||
|
||||
void ARP::hw_addr_length(uint8_t new_hw_addr_len) {
|
||||
@@ -95,7 +94,7 @@ void ARP::prot_addr_length(uint8_t new_prot_addr_len) {
|
||||
}
|
||||
|
||||
void ARP::opcode(Flags new_opcode) {
|
||||
this->_arp.ar_op = Utils::host_to_be<uint16_t>(new_opcode);
|
||||
this->_arp.ar_op = Endian::host_to_be<uint16_t>(new_opcode);
|
||||
}
|
||||
|
||||
uint32_t ARP::header_size() const {
|
||||
|
||||
@@ -63,15 +63,15 @@ void BootP::hops(uint8_t new_hops) {
|
||||
}
|
||||
|
||||
void BootP::xid(uint32_t new_xid) {
|
||||
_bootp.xid = Utils::host_to_be(new_xid);
|
||||
_bootp.xid = Endian::host_to_be(new_xid);
|
||||
}
|
||||
|
||||
void BootP::secs(uint16_t new_secs) {
|
||||
_bootp.secs = Utils::host_to_be(new_secs);
|
||||
_bootp.secs = Endian::host_to_be(new_secs);
|
||||
}
|
||||
|
||||
void BootP::padding(uint16_t new_padding) {
|
||||
_bootp.padding = Utils::host_to_be(new_padding);
|
||||
_bootp.padding = Endian::host_to_be(new_padding);
|
||||
}
|
||||
|
||||
void BootP::ciaddr(ipaddress_type new_ciaddr) {
|
||||
|
||||
19
src/dhcp.cpp
19
src/dhcp.cpp
@@ -22,7 +22,7 @@
|
||||
#include <stdexcept>
|
||||
#include <cstring>
|
||||
#include <cassert>
|
||||
#include "utils.h"
|
||||
#include "endianness.h"
|
||||
#include "dhcp.h"
|
||||
#include "ethernetII.h"
|
||||
|
||||
@@ -33,8 +33,7 @@ using std::runtime_error;
|
||||
namespace Tins {
|
||||
const uint32_t DHCP::MAX_DHCP_SIZE = 312;
|
||||
|
||||
/* Magic cookie: uint32_t.
|
||||
* end of options: 1 byte. */
|
||||
// Magic cookie: uint32_t.
|
||||
DHCP::DHCP() : _size(sizeof(uint32_t)) {
|
||||
opcode(BOOTREQUEST);
|
||||
htype(1); //ethernet
|
||||
@@ -47,7 +46,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::host_to_be<uint32_t>(0x63825363))
|
||||
if(total_sz < sizeof(uint32_t) || *(uint32_t*)buffer != Endian::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);
|
||||
@@ -115,7 +114,7 @@ bool DHCP::search_server_identifier(ipaddress_type *value) {
|
||||
}
|
||||
|
||||
bool DHCP::add_lease_time(uint32_t time) {
|
||||
time = Utils::host_to_be(time);
|
||||
time = Endian::host_to_be(time);
|
||||
return add_option(DHCP_LEASE_TIME, sizeof(uint32_t), (const uint8_t*)&time);
|
||||
}
|
||||
|
||||
@@ -124,7 +123,7 @@ bool DHCP::search_lease_time(uint32_t *value) {
|
||||
}
|
||||
|
||||
bool DHCP::add_renewal_time(uint32_t time) {
|
||||
time = Utils::host_to_be(time);
|
||||
time = Endian::host_to_be(time);
|
||||
return add_option(DHCP_RENEWAL_TIME, sizeof(uint32_t), (const uint8_t*)&time);
|
||||
}
|
||||
|
||||
@@ -192,7 +191,7 @@ bool DHCP::search_domain_name(std::string *value) {
|
||||
}
|
||||
|
||||
bool DHCP::add_rebind_time(uint32_t time) {
|
||||
time = Utils::host_to_be(time);
|
||||
time = Endian::host_to_be(time);
|
||||
return add_option(DHCP_REBINDING_TIME, sizeof(uint32_t), (uint8_t*)&time);
|
||||
}
|
||||
|
||||
@@ -220,7 +219,7 @@ void DHCP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *pa
|
||||
result.resize(_size);
|
||||
uint8_t *ptr = &result[0] + sizeof(uint32_t);
|
||||
// Magic cookie
|
||||
*((uint32_t*)&result[0]) = Utils::host_to_be<uint32_t>(0x63825363);
|
||||
*((uint32_t*)&result[0]) = Endian::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();
|
||||
@@ -256,7 +255,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::host_to_be(*value);
|
||||
*value = Endian::host_to_be(*value);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -265,7 +264,7 @@ bool DHCP::generic_search(Options opt, uint32_t *value) {
|
||||
bool DHCP::generic_search(Options opt, ipaddress_type *value) {
|
||||
uint32_t ip_int;
|
||||
if(generic_search(opt, &ip_int)) {
|
||||
*value = IPv4Address(Utils::host_to_be(ip_int));
|
||||
*value = IPv4Address(Endian::host_to_be(ip_int));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
52
src/dns.cpp
52
src/dns.cpp
@@ -101,8 +101,8 @@ const uint8_t *DNS::build_resource_list(list<ResourceRecord*> &lst, const uint8_
|
||||
// Probably convert to be this constant
|
||||
if((*ptr & 0xc0)) {
|
||||
uint16_t offset(*reinterpret_cast<const uint16_t*>(ptr));
|
||||
offset = Utils::be_to_host(offset) & 0x3fff;
|
||||
res.reset(new OffsetedResourceRecord(Utils::host_to_be(offset)));
|
||||
offset = Endian::be_to_host(offset) & 0x3fff;
|
||||
res.reset(new OffsetedResourceRecord(Endian::host_to_be(offset)));
|
||||
ptr += sizeof(uint16_t);
|
||||
}
|
||||
else {
|
||||
@@ -124,7 +124,7 @@ const uint8_t *DNS::build_resource_list(list<ResourceRecord*> &lst, const uint8_
|
||||
|
||||
// Store the option size.
|
||||
res->data.resize(
|
||||
Utils::be_to_host(*reinterpret_cast<const uint16_t*>(ptr))
|
||||
Endian::be_to_host(*reinterpret_cast<const uint16_t*>(ptr))
|
||||
);
|
||||
ptr += sizeof(uint16_t);
|
||||
if(ptr + res->data.size() > ptr_end)
|
||||
@@ -149,7 +149,7 @@ uint32_t DNS::header_size() const {
|
||||
}
|
||||
|
||||
void DNS::id(uint16_t new_id) {
|
||||
dns.id = Utils::host_to_be(new_id);
|
||||
dns.id = Endian::host_to_be(new_id);
|
||||
}
|
||||
|
||||
void DNS::type(QRType new_qr) {
|
||||
@@ -193,7 +193,7 @@ void DNS::rcode(uint8_t new_rcode) {
|
||||
}
|
||||
|
||||
bool DNS::contains_dname(uint16_t type) {
|
||||
type = Utils::be_to_host(type);
|
||||
type = Endian::be_to_host(type);
|
||||
return type == MX || type == CNAME ||
|
||||
type == PTR || type == NS;
|
||||
}
|
||||
@@ -204,11 +204,11 @@ void DNS::add_query(const string &name, QueryType type, QueryClass qclass) {
|
||||
|
||||
queries.push_back(
|
||||
Query(new_str,
|
||||
Utils::host_to_be<uint16_t>(type),
|
||||
Utils::host_to_be<uint16_t>(qclass))
|
||||
Endian::host_to_be<uint16_t>(type),
|
||||
Endian::host_to_be<uint16_t>(qclass))
|
||||
);
|
||||
extra_size += new_str.size() + 1 + (sizeof(uint16_t) << 1);
|
||||
dns.questions = Utils::host_to_be<uint16_t>(queries.size());
|
||||
dns.questions = Endian::host_to_be<uint16_t>(queries.size());
|
||||
}
|
||||
|
||||
void DNS::add_query(const Query &query) {
|
||||
@@ -220,9 +220,9 @@ 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, Utils::host_to_be((uint32_t)ip));
|
||||
ResourceRecord *res = make_record(name, type, qclass, ttl, Endian::host_to_be((uint32_t)ip));
|
||||
ans.push_back(res);
|
||||
dns.answers = Utils::host_to_be<uint16_t>(ans.size());
|
||||
dns.answers = Endian::host_to_be<uint16_t>(ans.size());
|
||||
}
|
||||
|
||||
void DNS::add_answer(const std::string &name, QueryType type, QueryClass qclass,
|
||||
@@ -231,31 +231,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::host_to_be<uint16_t>(ans.size());
|
||||
dns.answers = Endian::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::host_to_be<uint16_t>(ans.size());
|
||||
dns.answers = Endian::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::host_to_be<uint16_t>(arity.size());
|
||||
dns.authority = Endian::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::host_to_be<uint16_t>(addit.size());
|
||||
dns.additional = Endian::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::host_to_be(ip);
|
||||
ip = Endian::host_to_be(ip);
|
||||
return make_record(name, type, qclass, ttl, reinterpret_cast<uint8_t*>(&ip), sizeof(ip));
|
||||
}
|
||||
|
||||
@@ -269,12 +269,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::host_to_be(index), ptr, len);
|
||||
res = new OffsetedResourceRecord(Endian::host_to_be(index), ptr, len);
|
||||
else
|
||||
res = new NamedResourceRecord(nm, ptr, len);
|
||||
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);
|
||||
res->info.type = Endian::host_to_be<uint16_t>(type);
|
||||
res->info.qclass = Endian::host_to_be<uint16_t>(qclass);
|
||||
res->info.ttl = Endian::host_to_be(ttl);
|
||||
extra_size += res->size();
|
||||
return res;
|
||||
}
|
||||
@@ -389,7 +389,7 @@ uint32_t DNS::build_suffix_map(uint32_t index, const list<ResourceRecord*> &lst)
|
||||
index += sizeof(ResourceRecord::Info) + sizeof(uint16_t);
|
||||
uint32_t sz((*it)->data_size());
|
||||
const uint8_t *ptr = (*it)->data_pointer();
|
||||
if(Utils::be_to_host((*it)->info.type) == MX) {
|
||||
if(Endian::be_to_host((*it)->info.type) == MX) {
|
||||
ptr += 2;
|
||||
sz -= 2;
|
||||
index += 2;
|
||||
@@ -423,7 +423,7 @@ void DNS::compose_name(const uint8_t *ptr, uint32_t sz, std::string &out) {
|
||||
if(i && ptr[i])
|
||||
out.push_back('.');
|
||||
if((ptr[i] & 0xc0)) {
|
||||
uint16_t index = Utils::be_to_host(*((uint16_t*)(ptr + i)));
|
||||
uint16_t index = Endian::be_to_host(*((uint16_t*)(ptr + i)));
|
||||
index &= 0x3fff;
|
||||
SuffixMap::iterator it(suffixes.find(index));
|
||||
SuffixIndices::iterator suff_it(suffix_indices.find(index));
|
||||
@@ -477,15 +477,15 @@ void DNS::convert_resources(const ResourcesType &lst, std::list<Resource> &res)
|
||||
if(sz == 4)
|
||||
addr = IPv4Address(*(uint32_t*)ptr).to_string();
|
||||
else {
|
||||
if(Utils::be_to_host((*it)->info.type) == MX) {
|
||||
if(Endian::be_to_host((*it)->info.type) == MX) {
|
||||
ptr += 2;
|
||||
sz -= 2;
|
||||
}
|
||||
compose_name(ptr, sz, addr);
|
||||
}
|
||||
res.push_back(
|
||||
Resource(dname, addr, Utils::be_to_host((*it)->info.type),
|
||||
Utils::host_to_be((*it)->info.qclass), Utils::be_to_host((*it)->info.ttl)
|
||||
Resource(dname, addr, Endian::be_to_host((*it)->info.type),
|
||||
Endian::host_to_be((*it)->info.qclass), Endian::be_to_host((*it)->info.ttl)
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -496,7 +496,7 @@ DNS::queries_type DNS::dns_queries() const {
|
||||
for(std::list<Query>::const_iterator it(queries.begin()); it != queries.end(); ++it) {
|
||||
string dn;
|
||||
unparse_domain_name(it->name, dn);
|
||||
output.push_back(Query(dn, Utils::be_to_host(it->type), Utils::be_to_host(it->qclass)));
|
||||
output.push_back(Query(dn, Endian::be_to_host(it->type), Endian::be_to_host(it->qclass)));
|
||||
}
|
||||
return output;
|
||||
}
|
||||
@@ -529,7 +529,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::host_to_be(data.size());
|
||||
*((uint16_t*)buffer) = Endian::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();
|
||||
|
||||
@@ -34,7 +34,6 @@
|
||||
#include "rawpdu.h"
|
||||
#include "radiotap.h"
|
||||
#include "sniffer.h"
|
||||
#include "utils.h"
|
||||
#include "rsn_information.h"
|
||||
#include "snap.h"
|
||||
|
||||
@@ -146,7 +145,7 @@ void Dot11::order(small_uint<1> new_value) {
|
||||
}
|
||||
|
||||
void Dot11::duration_id(uint16_t new_duration_id) {
|
||||
this->_header.duration_id = Utils::host_to_le(new_duration_id);
|
||||
this->_header.duration_id = Endian::host_to_le(new_duration_id);
|
||||
}
|
||||
|
||||
void Dot11::addr1(const address_type &new_addr1) {
|
||||
@@ -167,8 +166,8 @@ bool Dot11::send(PacketSender* sender) {
|
||||
|
||||
memset(&addr, 0, sizeof(struct sockaddr_ll));
|
||||
|
||||
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_family = Endian::host_to_be<uint16_t>(PF_PACKET);
|
||||
addr.sll_protocol = Endian::host_to_be<uint16_t>(ETH_P_ALL);
|
||||
addr.sll_halen = 6;
|
||||
addr.sll_ifindex = _iface.id();
|
||||
memcpy(&(addr.sll_addr), _header.addr1, 6);
|
||||
@@ -316,7 +315,7 @@ void Dot11ManagementFrame::frag_num(uint8_t new_frag_num) {
|
||||
}
|
||||
|
||||
void Dot11ManagementFrame::seq_num(uint16_t new_seq_num) {
|
||||
this->_ext_header.seq_control.seq_number = Utils::host_to_le(new_seq_num);
|
||||
this->_ext_header.seq_control.seq_number = Endian::host_to_le(new_seq_num);
|
||||
}
|
||||
|
||||
void Dot11ManagementFrame::addr4(const address_type &new_addr4) {
|
||||
@@ -402,10 +401,10 @@ void Dot11ManagementFrame::edca_parameter_set(uint32_t ac_be, uint32_t ac_bk, ui
|
||||
buffer[0] = 0;
|
||||
buffer[1] = 0;
|
||||
uint32_t* ptr = (uint32_t*)(buffer + 2);
|
||||
*(ptr++) = Utils::host_to_le(ac_be);
|
||||
*(ptr++) = Utils::host_to_le(ac_bk);
|
||||
*(ptr++) = Utils::host_to_le(ac_vi);
|
||||
*(ptr++) = Utils::host_to_le(ac_vo);
|
||||
*(ptr++) = Endian::host_to_le(ac_be);
|
||||
*(ptr++) = Endian::host_to_le(ac_bk);
|
||||
*(ptr++) = Endian::host_to_le(ac_vi);
|
||||
*(ptr++) = Endian::host_to_le(ac_vo);
|
||||
add_tagged_option(EDCA, sizeof(buffer), buffer);
|
||||
}
|
||||
|
||||
@@ -418,7 +417,7 @@ void Dot11ManagementFrame::request_information(const request_info_type elements)
|
||||
}
|
||||
|
||||
void Dot11ManagementFrame::fh_parameter_set(fh_params_set fh_params) {
|
||||
fh_params.dwell_time = Utils::host_to_le(fh_params.dwell_time);
|
||||
fh_params.dwell_time = Endian::host_to_le(fh_params.dwell_time);
|
||||
fh_params.hop_set = fh_params.hop_set;
|
||||
fh_params.hop_pattern = fh_params.hop_pattern;
|
||||
fh_params.hop_index = fh_params.hop_index;
|
||||
@@ -433,13 +432,13 @@ void Dot11ManagementFrame::ds_parameter_set(uint8_t current_channel) {
|
||||
void Dot11ManagementFrame::cf_parameter_set(cf_params_set params) {
|
||||
params.cfp_count = params.cfp_count;
|
||||
params.cfp_period = params.cfp_period;
|
||||
params.cfp_max_duration = Utils::host_to_le(params.cfp_max_duration);
|
||||
params.cfp_dur_remaining = Utils::host_to_le(params.cfp_dur_remaining);
|
||||
params.cfp_max_duration = Endian::host_to_le(params.cfp_max_duration);
|
||||
params.cfp_dur_remaining = Endian::host_to_le(params.cfp_dur_remaining);
|
||||
add_tagged_option(CF_SET, sizeof(params), (uint8_t*)¶ms);
|
||||
}
|
||||
|
||||
void Dot11ManagementFrame::ibss_parameter_set(uint16_t atim_window) {
|
||||
atim_window = Utils::host_to_le(atim_window);
|
||||
atim_window = Endian::host_to_le(atim_window);
|
||||
add_tagged_option(IBSS_SET, 2, (uint8_t*)&atim_window);
|
||||
}
|
||||
|
||||
@@ -519,8 +518,8 @@ void Dot11ManagementFrame::quiet(const quiet_type &data) {
|
||||
|
||||
buffer[0] = data.quiet_count;
|
||||
buffer[1] = data.quiet_period;
|
||||
ptr_buffer[0] = Utils::host_to_le(data.quiet_duration);
|
||||
ptr_buffer[1] = Utils::host_to_le(data.quiet_offset);
|
||||
ptr_buffer[0] = Endian::host_to_le(data.quiet_duration);
|
||||
ptr_buffer[1] = Endian::host_to_le(data.quiet_offset);
|
||||
add_tagged_option(QUIET, sizeof(buffer), buffer);
|
||||
|
||||
}
|
||||
@@ -540,9 +539,9 @@ void Dot11ManagementFrame::erp_information(uint8_t value) {
|
||||
void Dot11ManagementFrame::bss_load(const bss_load_type &data) {
|
||||
uint8_t buffer[5];
|
||||
|
||||
*(uint16_t*)buffer = Utils::host_to_le(data.station_count);
|
||||
*(uint16_t*)buffer = Endian::host_to_le(data.station_count);
|
||||
buffer[2] = data.channel_utilization;
|
||||
*(uint16_t*)(buffer + 3) = Utils::host_to_le(data.available_capacity);
|
||||
*(uint16_t*)(buffer + 3) = Endian::host_to_le(data.available_capacity);
|
||||
add_tagged_option(BSS_LOAD, sizeof(buffer), buffer);
|
||||
}
|
||||
|
||||
@@ -640,7 +639,7 @@ Dot11ManagementFrame::fh_params_set Dot11ManagementFrame::fh_parameter_set() con
|
||||
if(!option || option->data_size() != sizeof(fh_params_set))
|
||||
throw std::runtime_error("FH parameters set not set");
|
||||
fh_params_set output = *reinterpret_cast<const fh_params_set*>(option->data_ptr());
|
||||
output.dwell_time = Utils::le_to_host(output.dwell_time);
|
||||
output.dwell_time = Endian::le_to_host(output.dwell_time);
|
||||
output.hop_set = output.hop_set;
|
||||
output.hop_pattern = output.hop_pattern;
|
||||
output.hop_index = output.hop_index;
|
||||
@@ -658,7 +657,7 @@ uint16_t Dot11ManagementFrame::ibss_parameter_set() const {
|
||||
const Dot11::Dot11Option *option = search_option(IBSS_SET);
|
||||
if(!option || option->data_size() != sizeof(uint16_t))
|
||||
throw std::runtime_error("IBSS parameters set not set");
|
||||
return Utils::le_to_host(*reinterpret_cast<const uint16_t*>(option->data_ptr()));
|
||||
return Endian::le_to_host(*reinterpret_cast<const uint16_t*>(option->data_ptr()));
|
||||
}
|
||||
|
||||
Dot11ManagementFrame::ibss_dfs_params Dot11ManagementFrame::ibss_dfs() const {
|
||||
@@ -751,8 +750,8 @@ Dot11ManagementFrame::quiet_type Dot11ManagementFrame::quiet() const {
|
||||
output.quiet_count = *(ptr++);
|
||||
output.quiet_period = *(ptr++);
|
||||
const uint16_t *ptr_16 = (const uint16_t*)ptr;
|
||||
output.quiet_duration = Utils::le_to_host(*(ptr_16++));
|
||||
output.quiet_offset = Utils::le_to_host(*ptr_16);
|
||||
output.quiet_duration = Endian::le_to_host(*(ptr_16++));
|
||||
output.quiet_offset = Endian::le_to_host(*ptr_16);
|
||||
return output;
|
||||
}
|
||||
|
||||
@@ -779,9 +778,9 @@ Dot11ManagementFrame::bss_load_type Dot11ManagementFrame::bss_load() const {
|
||||
bss_load_type output;
|
||||
|
||||
const uint8_t *ptr = option->data_ptr();
|
||||
output.station_count = Utils::le_to_host(*(uint16_t*)ptr);
|
||||
output.station_count = Endian::le_to_host(*(uint16_t*)ptr);
|
||||
output.channel_utilization = ptr[2];
|
||||
output.available_capacity = Utils::le_to_host(*(uint16_t*)(ptr + 3));
|
||||
output.available_capacity = Endian::le_to_host(*(uint16_t*)(ptr + 3));
|
||||
return output;
|
||||
}
|
||||
|
||||
@@ -832,11 +831,11 @@ Dot11Beacon::Dot11Beacon(const uint8_t *buffer, uint32_t total_sz)
|
||||
}
|
||||
|
||||
void Dot11Beacon::timestamp(uint64_t new_timestamp) {
|
||||
this->_body.timestamp = Utils::host_to_le(new_timestamp);
|
||||
this->_body.timestamp = Endian::host_to_le(new_timestamp);
|
||||
}
|
||||
|
||||
void Dot11Beacon::interval(uint16_t new_interval) {
|
||||
this->_body.interval = Utils::host_to_le(new_interval);
|
||||
this->_body.interval = Endian::host_to_le(new_interval);
|
||||
}
|
||||
|
||||
uint32_t Dot11Beacon::header_size() const {
|
||||
@@ -873,7 +872,7 @@ Dot11Disassoc::Dot11Disassoc(const uint8_t *buffer, uint32_t total_sz)
|
||||
}
|
||||
|
||||
void Dot11Disassoc::reason_code(uint16_t new_reason_code) {
|
||||
this->_body.reason_code = Utils::host_to_le(new_reason_code);
|
||||
this->_body.reason_code = Endian::host_to_le(new_reason_code);
|
||||
}
|
||||
|
||||
uint32_t Dot11Disassoc::header_size() const {
|
||||
@@ -910,7 +909,7 @@ Dot11AssocRequest::Dot11AssocRequest(const uint8_t *buffer, uint32_t total_sz) :
|
||||
}
|
||||
|
||||
void Dot11AssocRequest::listen_interval(uint16_t new_listen_interval) {
|
||||
this->_body.listen_interval = Utils::host_to_le(new_listen_interval);
|
||||
this->_body.listen_interval = Endian::host_to_le(new_listen_interval);
|
||||
}
|
||||
|
||||
uint32_t Dot11AssocRequest::header_size() const {
|
||||
@@ -949,11 +948,11 @@ Dot11AssocResponse::Dot11AssocResponse(const uint8_t *buffer, uint32_t total_sz)
|
||||
}
|
||||
|
||||
void Dot11AssocResponse::status_code(uint16_t new_status_code) {
|
||||
this->_body.status_code = Utils::host_to_le(new_status_code);
|
||||
this->_body.status_code = Endian::host_to_le(new_status_code);
|
||||
}
|
||||
|
||||
void Dot11AssocResponse::aid(uint16_t new_aid) {
|
||||
this->_body.aid = Utils::host_to_le(new_aid);
|
||||
this->_body.aid = Endian::host_to_le(new_aid);
|
||||
}
|
||||
|
||||
uint32_t Dot11AssocResponse::header_size() const {
|
||||
@@ -992,7 +991,7 @@ Dot11ReAssocRequest::Dot11ReAssocRequest(const uint8_t *buffer, uint32_t total_s
|
||||
}
|
||||
|
||||
void Dot11ReAssocRequest::listen_interval(uint16_t new_listen_interval) {
|
||||
this->_body.listen_interval = Utils::host_to_le(new_listen_interval);
|
||||
this->_body.listen_interval = Endian::host_to_le(new_listen_interval);
|
||||
}
|
||||
|
||||
void Dot11ReAssocRequest::current_ap(const address_type &new_current_ap) {
|
||||
@@ -1034,11 +1033,11 @@ Dot11ReAssocResponse::Dot11ReAssocResponse(const uint8_t *buffer, uint32_t total
|
||||
}
|
||||
|
||||
void Dot11ReAssocResponse::status_code(uint16_t new_status_code) {
|
||||
this->_body.status_code = Utils::host_to_le(new_status_code);
|
||||
this->_body.status_code = Endian::host_to_le(new_status_code);
|
||||
}
|
||||
|
||||
void Dot11ReAssocResponse::aid(uint16_t new_aid) {
|
||||
this->_body.aid = Utils::host_to_le(new_aid);
|
||||
this->_body.aid = Endian::host_to_le(new_aid);
|
||||
}
|
||||
|
||||
uint32_t Dot11ReAssocResponse::header_size() const {
|
||||
@@ -1078,15 +1077,15 @@ Dot11Authentication::Dot11Authentication(const uint8_t *buffer, uint32_t total_s
|
||||
}
|
||||
|
||||
void Dot11Authentication::auth_algorithm(uint16_t new_auth_algorithm) {
|
||||
this->_body.auth_algorithm = Utils::host_to_le(new_auth_algorithm);
|
||||
this->_body.auth_algorithm = Endian::host_to_le(new_auth_algorithm);
|
||||
}
|
||||
|
||||
void Dot11Authentication::auth_seq_number(uint16_t new_auth_seq_number) {
|
||||
this->_body.auth_seq_number = Utils::host_to_le(new_auth_seq_number);
|
||||
this->_body.auth_seq_number = Endian::host_to_le(new_auth_seq_number);
|
||||
}
|
||||
|
||||
void Dot11Authentication::status_code(uint16_t new_status_code) {
|
||||
this->_body.status_code = Utils::host_to_le(new_status_code);
|
||||
this->_body.status_code = Endian::host_to_le(new_status_code);
|
||||
}
|
||||
|
||||
uint32_t Dot11Authentication::header_size() const {
|
||||
@@ -1124,7 +1123,7 @@ Dot11Deauthentication::Dot11Deauthentication(const uint8_t *buffer, uint32_t tot
|
||||
}
|
||||
|
||||
void Dot11Deauthentication::reason_code(uint16_t new_reason_code) {
|
||||
this->_body.reason_code = Utils::host_to_le(new_reason_code);
|
||||
this->_body.reason_code = Endian::host_to_le(new_reason_code);
|
||||
}
|
||||
|
||||
uint32_t Dot11Deauthentication::header_size() const {
|
||||
@@ -1181,11 +1180,11 @@ Dot11ProbeResponse::Dot11ProbeResponse(const uint8_t *buffer, uint32_t total_sz)
|
||||
}
|
||||
|
||||
void Dot11ProbeResponse::timestamp(uint64_t new_timestamp) {
|
||||
this->_body.timestamp = Utils::host_to_le(new_timestamp);
|
||||
this->_body.timestamp = Endian::host_to_le(new_timestamp);
|
||||
}
|
||||
|
||||
void Dot11ProbeResponse::interval(uint16_t new_interval) {
|
||||
this->_body.interval = Utils::host_to_le(new_interval);
|
||||
this->_body.interval = Endian::host_to_le(new_interval);
|
||||
}
|
||||
|
||||
uint32_t Dot11ProbeResponse::header_size() const {
|
||||
@@ -1253,7 +1252,7 @@ void Dot11Data::frag_num(uint8_t new_frag_num) {
|
||||
}
|
||||
|
||||
void Dot11Data::seq_num(uint16_t new_seq_num) {
|
||||
_ext_header.seq_control.seq_number = Utils::host_to_le(new_seq_num);
|
||||
_ext_header.seq_control.seq_number = Endian::host_to_le(new_seq_num);
|
||||
}
|
||||
|
||||
void Dot11Data::addr4(const address_type &new_addr4) {
|
||||
@@ -1300,7 +1299,7 @@ Dot11QoSData::Dot11QoSData(const uint8_t *buffer, uint32_t total_sz)
|
||||
}
|
||||
|
||||
void Dot11QoSData::qos_control(uint16_t new_qos_control) {
|
||||
this->_qos_control = Utils::host_to_le(new_qos_control);
|
||||
this->_qos_control = Endian::host_to_le(new_qos_control);
|
||||
}
|
||||
|
||||
uint32_t Dot11QoSData::header_size() const {
|
||||
@@ -1467,12 +1466,12 @@ uint32_t Dot11BlockAckRequest::write_ext_header(uint8_t *buffer, uint32_t total_
|
||||
|
||||
void Dot11BlockAckRequest::bar_control(uint16_t bar) {
|
||||
//std::memcpy(&_bar_control, &bar, sizeof(bar));
|
||||
_bar_control.tid = Utils::host_to_le(bar);
|
||||
_bar_control.tid = Endian::host_to_le(bar);
|
||||
}
|
||||
|
||||
void Dot11BlockAckRequest::start_sequence(uint16_t seq) {
|
||||
//std::memcpy(&_start_sequence, &seq, sizeof(seq));
|
||||
_start_sequence.seq = Utils::host_to_le(seq);
|
||||
_start_sequence.seq = Endian::host_to_le(seq);
|
||||
}
|
||||
|
||||
void Dot11BlockAckRequest::fragment_number(uint8_t frag) {
|
||||
|
||||
@@ -114,11 +114,11 @@ RC4EAPOL::RC4EAPOL(const uint8_t *buffer, uint32_t total_sz)
|
||||
}
|
||||
|
||||
void RC4EAPOL::key_length(uint16_t new_key_length) {
|
||||
_header.key_length = Utils::host_to_be(new_key_length);
|
||||
_header.key_length = Endian::host_to_be(new_key_length);
|
||||
}
|
||||
|
||||
void RC4EAPOL::replay_counter(uint16_t new_replay_counter) {
|
||||
_header.replay_counter = Utils::host_to_be(new_replay_counter);
|
||||
_header.replay_counter = Endian::host_to_be(new_replay_counter);
|
||||
}
|
||||
|
||||
void RC4EAPOL::key_iv(const uint8_t *new_key_iv) {
|
||||
@@ -149,7 +149,7 @@ void RC4EAPOL::write_body(uint8_t *buffer, uint32_t total_sz) {
|
||||
uint32_t sz = sizeof(_header) + _key.size();
|
||||
assert(total_sz >= sz);
|
||||
if(_key.size())
|
||||
_header.key_length = Utils::host_to_be(_key.size());
|
||||
_header.key_length = Endian::host_to_be(_key.size());
|
||||
std::memcpy(buffer, &_header, sizeof(_header));
|
||||
buffer += sizeof(_header);
|
||||
std::copy(_key.begin(), _key.end(), buffer);
|
||||
@@ -183,11 +183,11 @@ void RSNEAPOL::RSNEAPOL::nonce(const uint8_t *new_nonce) {
|
||||
}
|
||||
|
||||
void RSNEAPOL::rsc(uint64_t new_rsc) {
|
||||
_header.rsc = Utils::host_to_be(new_rsc);
|
||||
_header.rsc = Endian::host_to_be(new_rsc);
|
||||
}
|
||||
|
||||
void RSNEAPOL::id(uint64_t new_id) {
|
||||
_header.id = Utils::host_to_be(new_id);
|
||||
_header.id = Endian::host_to_be(new_id);
|
||||
}
|
||||
|
||||
void RSNEAPOL::mic(const uint8_t *new_mic) {
|
||||
@@ -195,7 +195,7 @@ void RSNEAPOL::mic(const uint8_t *new_mic) {
|
||||
}
|
||||
|
||||
void RSNEAPOL::wpa_length(uint16_t new_wpa_length) {
|
||||
_header.wpa_length = Utils::host_to_be(new_wpa_length);
|
||||
_header.wpa_length = Endian::host_to_be(new_wpa_length);
|
||||
}
|
||||
|
||||
void RSNEAPOL::key(const key_type &new_key) {
|
||||
@@ -220,7 +220,7 @@ void RSNEAPOL::write_body(uint8_t *buffer, uint32_t total_sz) {
|
||||
assert(total_sz >= sz);
|
||||
if(_key.size()) {
|
||||
if(!_header.key_t) {
|
||||
_header.key_length = Utils::host_to_be<uint16_t>(32);
|
||||
_header.key_length = Endian::host_to_be<uint16_t>(32);
|
||||
wpa_length(_key.size());
|
||||
}
|
||||
else if(_key.size()) {
|
||||
|
||||
@@ -31,7 +31,6 @@
|
||||
#include "rawpdu.h"
|
||||
#include "ip.h"
|
||||
#include "arp.h"
|
||||
#include "utils.h"
|
||||
|
||||
namespace Tins {
|
||||
const EthernetII::address_type EthernetII::BROADCAST("ff:ff:ff:ff:ff:ff");
|
||||
@@ -88,7 +87,7 @@ void EthernetII::iface(const NetworkInterface& new_iface) {
|
||||
}
|
||||
|
||||
void EthernetII::payload_type(uint16_t new_payload_type) {
|
||||
this->_eth.payload_type = Utils::host_to_be(new_payload_type);
|
||||
this->_eth.payload_type = Endian::host_to_be(new_payload_type);
|
||||
}
|
||||
|
||||
uint32_t EthernetII::header_size() const {
|
||||
@@ -102,8 +101,8 @@ bool EthernetII::send(PacketSender* sender) {
|
||||
|
||||
memset(&addr, 0, sizeof(struct sockaddr_ll));
|
||||
|
||||
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_family = Endian::host_to_be<uint16_t>(PF_PACKET);
|
||||
addr.sll_protocol = Endian::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);
|
||||
@@ -139,7 +138,7 @@ void EthernetII::write_serialization(uint8_t *buffer, uint32_t total_sz, const P
|
||||
default:
|
||||
type = 0;
|
||||
}
|
||||
_eth.payload_type = Utils::host_to_be(type);
|
||||
_eth.payload_type = Endian::host_to_be(type);
|
||||
}
|
||||
memcpy(buffer, &_eth, sizeof(ethhdr));
|
||||
}
|
||||
@@ -148,8 +147,8 @@ PDU *EthernetII::recv_response(PacketSender *sender) {
|
||||
struct sockaddr_ll addr;
|
||||
memset(&addr, 0, sizeof(struct sockaddr_ll));
|
||||
|
||||
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_family = Endian::host_to_be<uint16_t>(PF_PACKET);
|
||||
addr.sll_protocol = Endian::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::host_to_be(new_check);
|
||||
_icmp.check = Endian::host_to_be(new_check);
|
||||
}
|
||||
|
||||
void Tins::ICMP::id(uint16_t new_id) {
|
||||
_icmp.un.echo.id = Utils::host_to_be(new_id);
|
||||
_icmp.un.echo.id = Endian::host_to_be(new_id);
|
||||
}
|
||||
|
||||
void Tins::ICMP::sequence(uint16_t new_seq) {
|
||||
_icmp.un.echo.sequence = Utils::host_to_be(new_seq);
|
||||
_icmp.un.echo.sequence = Endian::host_to_be(new_seq);
|
||||
}
|
||||
|
||||
void Tins::ICMP::gateway(uint32_t new_gw) {
|
||||
_icmp.un.gateway = Utils::host_to_be(new_gw);
|
||||
_icmp.un.gateway = Endian::host_to_be(new_gw);
|
||||
}
|
||||
|
||||
void Tins::ICMP::mtu(uint16_t new_mtu) {
|
||||
_icmp.un.frag.mtu = Utils::host_to_be(new_mtu);
|
||||
_icmp.un.frag.mtu = Endian::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::host_to_be<uint16_t>(~checksum);
|
||||
_icmp.check = Endian::host_to_be<uint16_t>(~checksum);
|
||||
}
|
||||
memcpy(buffer, &_icmp, sizeof(icmphdr));
|
||||
_icmp.check = 0;
|
||||
|
||||
@@ -29,7 +29,6 @@
|
||||
#endif
|
||||
#include "ieee802_3.h"
|
||||
#include "llc.h"
|
||||
#include "utils.h"
|
||||
|
||||
namespace Tins {
|
||||
const IEEE802_3::address_type IEEE802_3::BROADCAST("ff:ff:ff:ff:ff:ff");
|
||||
@@ -73,7 +72,7 @@ void IEEE802_3::iface(const NetworkInterface &new_iface) {
|
||||
}
|
||||
|
||||
void IEEE802_3::length(uint16_t new_length) {
|
||||
this->_eth.length = Utils::host_to_be(new_length);
|
||||
this->_eth.length = Endian::host_to_be(new_length);
|
||||
}
|
||||
|
||||
uint32_t IEEE802_3::header_size() const {
|
||||
@@ -85,8 +84,8 @@ bool IEEE802_3::send(PacketSender* sender) {
|
||||
|
||||
memset(&addr, 0, sizeof(struct sockaddr_ll));
|
||||
|
||||
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_family = Endian::host_to_be<uint16_t>(PF_PACKET);
|
||||
addr.sll_protocol = Endian::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 +109,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::host_to_be(size() - sizeof(_eth));
|
||||
_eth.length = Endian::host_to_be(size() - sizeof(_eth));
|
||||
|
||||
memcpy(buffer, &_eth, sizeof(ethhdr));
|
||||
|
||||
@@ -122,8 +121,8 @@ PDU *IEEE802_3::recv_response(PacketSender *sender) {
|
||||
struct sockaddr_ll addr;
|
||||
memset(&addr, 0, sizeof(struct sockaddr_ll));
|
||||
|
||||
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_family = Endian::host_to_be<uint16_t>(PF_PACKET);
|
||||
addr.sll_protocol = Endian::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));
|
||||
|
||||
12
src/ip.cpp
12
src/ip.cpp
@@ -146,15 +146,15 @@ void IP::tos(uint8_t new_tos) {
|
||||
}
|
||||
|
||||
void IP::tot_len(uint16_t new_tot_len) {
|
||||
_ip.tot_len = Utils::host_to_be(new_tot_len);
|
||||
_ip.tot_len = Endian::host_to_be(new_tot_len);
|
||||
}
|
||||
|
||||
void IP::id(uint16_t new_id) {
|
||||
_ip.id = Utils::host_to_be(new_id);
|
||||
_ip.id = Endian::host_to_be(new_id);
|
||||
}
|
||||
|
||||
void IP::frag_off(uint16_t new_frag_off) {
|
||||
_ip.frag_off = Utils::host_to_be(new_frag_off);
|
||||
_ip.frag_off = Endian::host_to_be(new_frag_off);
|
||||
}
|
||||
|
||||
void IP::ttl(uint8_t new_ttl) {
|
||||
@@ -166,7 +166,7 @@ void IP::protocol(uint8_t new_protocol) {
|
||||
}
|
||||
|
||||
void IP::check(uint16_t new_check) {
|
||||
_ip.check = Utils::host_to_be(new_check);
|
||||
_ip.check = Endian::host_to_be(new_check);
|
||||
}
|
||||
|
||||
|
||||
@@ -305,7 +305,7 @@ void IP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU* pare
|
||||
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::host_to_be<uint16_t>(~checksum);
|
||||
((iphdr*)buffer)->check = Endian::host_to_be<uint16_t>(~checksum);
|
||||
this->check(0);
|
||||
}
|
||||
}
|
||||
@@ -333,7 +333,7 @@ PDU *IP::clone_packet(const uint8_t *ptr, uint32_t total_sz) {
|
||||
if((child = PDU::clone_inner_pdu(ptr + sizeof(_ip), total_sz - sizeof(_ip))) == 0)
|
||||
return 0;
|
||||
}
|
||||
cloned = new IP(ptr, std::min(total_sz, (uint32_t)(Utils::be_to_host(ip_ptr->tot_len) * sizeof(uint32_t))));
|
||||
cloned = new IP(ptr, std::min(total_sz, (uint32_t)(Endian::be_to_host(ip_ptr->tot_len) * sizeof(uint32_t))));
|
||||
cloned->inner_pdu(child);
|
||||
return cloned;
|
||||
}
|
||||
|
||||
@@ -20,14 +20,15 @@
|
||||
*/
|
||||
|
||||
#include <stdexcept>
|
||||
#include <sstream>
|
||||
#include "ipaddress.h"
|
||||
#include "utils.h"
|
||||
#include "endianness.h"
|
||||
|
||||
using std::string;
|
||||
|
||||
namespace Tins{
|
||||
IPv4Address::IPv4Address(uint32_t ip)
|
||||
: ip_addr(Utils::be_to_host(ip)) {
|
||||
: ip_addr(Endian::be_to_host(ip)) {
|
||||
|
||||
}
|
||||
|
||||
@@ -41,7 +42,7 @@ IPv4Address::IPv4Address(const std::string &ip)
|
||||
}
|
||||
|
||||
IPv4Address::operator uint32_t() const {
|
||||
return Utils::host_to_be(ip_addr);
|
||||
return Endian::host_to_be(ip_addr);
|
||||
}
|
||||
|
||||
std::string IPv4Address::to_string() const {
|
||||
|
||||
@@ -27,7 +27,6 @@
|
||||
|
||||
#include "pdu.h"
|
||||
#include "llc.h"
|
||||
#include "utils.h"
|
||||
#include "rawpdu.h"
|
||||
|
||||
using std::list;
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
#endif
|
||||
#include "network_interface.h"
|
||||
#include "utils.h"
|
||||
#include "endianness.h"
|
||||
|
||||
/** \cond */
|
||||
struct InterfaceInfoCollector {
|
||||
@@ -41,7 +42,7 @@ struct InterfaceInfoCollector {
|
||||
: info(res), iface_id(id), iface_name(if_name), found(false) { }
|
||||
|
||||
bool operator() (struct ifaddrs *addr) {
|
||||
using Tins::Utils::host_to_be;
|
||||
using Tins::Endian::host_to_be;
|
||||
using Tins::IPv4Address;
|
||||
const struct sockaddr_ll* addr_ptr = ((struct sockaddr_ll*)addr->ifa_addr);
|
||||
|
||||
|
||||
@@ -37,7 +37,9 @@ Tins::RadioTap::RadioTap(const NetworkInterface &iface, PDU *child)
|
||||
init();
|
||||
}
|
||||
|
||||
Tins::RadioTap::RadioTap(const uint8_t *buffer, uint32_t total_sz) : PDU(0xff) {
|
||||
Tins::RadioTap::RadioTap(const uint8_t *buffer, uint32_t total_sz)
|
||||
: PDU(0xff)
|
||||
{
|
||||
static const std::string msg("Not enough size for an RadioTap header in the buffer.");
|
||||
if(total_sz < sizeof(_radio))
|
||||
throw std::runtime_error(msg);
|
||||
@@ -130,7 +132,7 @@ void Tins::RadioTap::length(uint8_t new_length) {
|
||||
}
|
||||
|
||||
void Tins::RadioTap::tsft(uint64_t new_tsft) {
|
||||
_tsft = new_tsft;
|
||||
_tsft = Endian::host_to_le(new_tsft);
|
||||
if(!_radio.tsft)
|
||||
_options_size += sizeof(_tsft);
|
||||
_radio.tsft = 1;
|
||||
@@ -151,8 +153,8 @@ void Tins::RadioTap::rate(uint8_t new_rate) {
|
||||
}
|
||||
|
||||
void Tins::RadioTap::channel(uint16_t new_freq, uint16_t new_type) {
|
||||
_channel_freq = new_freq;
|
||||
_channel_type = new_type;
|
||||
_channel_freq = Endian::host_to_le(new_freq);
|
||||
_channel_type = Endian::host_to_le(new_type);
|
||||
if(!_radio.channel)
|
||||
_options_size += sizeof(_channel_freq) + sizeof(_channel_type);
|
||||
_radio.channel = 1;
|
||||
@@ -172,7 +174,7 @@ void Tins::RadioTap::antenna(uint8_t new_antenna) {
|
||||
}
|
||||
|
||||
void Tins::RadioTap::rx_flag(uint16_t new_rx_flag) {
|
||||
_rx_flags = new_rx_flag;
|
||||
_rx_flags = Endian::host_to_le(new_rx_flag);
|
||||
if(!_radio.rx_flags)
|
||||
_options_size += sizeof(_rx_flags);
|
||||
_radio.rx_flags = 1;
|
||||
@@ -197,8 +199,8 @@ bool Tins::RadioTap::send(PacketSender* sender) {
|
||||
|
||||
memset(&addr, 0, sizeof(struct sockaddr_ll));
|
||||
|
||||
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_family = Endian::host_to_be<uint16_t>(PF_PACKET);
|
||||
addr.sll_protocol = Endian::host_to_be<uint16_t>(ETH_P_ALL);
|
||||
addr.sll_halen = 6;
|
||||
addr.sll_ifindex = _iface.id();
|
||||
|
||||
@@ -206,7 +208,6 @@ bool Tins::RadioTap::send(PacketSender* sender) {
|
||||
if(wlan) {
|
||||
Dot11::address_type dot11_addr(wlan->addr1());
|
||||
std::copy(dot11_addr.begin(), dot11_addr.end(), addr.sll_addr);
|
||||
//memcpy(&(addr.sll_addr), wlan->addr1(), 6);
|
||||
}
|
||||
|
||||
return sender->send_l2(this, (struct sockaddr*)&addr, (uint32_t)sizeof(addr));
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
|
||||
#include <stdexcept>
|
||||
#include "rsn_information.h"
|
||||
|
||||
namespace Tins {
|
||||
@@ -29,7 +29,7 @@ RSNInformation::RSNInformation() : _version(1), _capabilities(0) {
|
||||
|
||||
RSNInformation::RSNInformation(const uint8_t *buffer, uint32_t total_sz) {
|
||||
const char *err_msg = "Malformed RSN information structure";
|
||||
version(Utils::le_to_host(*(uint16_t*)buffer));
|
||||
version(Endian::le_to_host(*(uint16_t*)buffer));
|
||||
buffer += sizeof(uint16_t);
|
||||
group_suite((RSNInformation::CypherSuites)*(uint32_t*)buffer);
|
||||
buffer += sizeof(uint32_t);
|
||||
@@ -60,7 +60,7 @@ RSNInformation::RSNInformation(const uint8_t *buffer, uint32_t total_sz) {
|
||||
}
|
||||
if(total_sz < sizeof(uint16_t))
|
||||
throw std::runtime_error(err_msg);
|
||||
capabilities(Utils::le_to_host(*(uint16_t*)buffer));
|
||||
capabilities(Endian::le_to_host(*(uint16_t*)buffer));
|
||||
}
|
||||
|
||||
void RSNInformation::add_pairwise_cypher(CypherSuites cypher) {
|
||||
@@ -76,11 +76,11 @@ void RSNInformation::group_suite(CypherSuites group) {
|
||||
}
|
||||
|
||||
void RSNInformation::version(uint16_t ver) {
|
||||
_version = Utils::host_to_le(ver);
|
||||
_version = Endian::host_to_le(ver);
|
||||
}
|
||||
|
||||
void RSNInformation::capabilities(uint16_t cap) {
|
||||
_capabilities = Utils::host_to_le(cap);
|
||||
_capabilities = Endian::host_to_le(cap);
|
||||
}
|
||||
|
||||
RSNInformation::serialization_type RSNInformation::serialize() const {
|
||||
|
||||
68
src/snap.cpp
68
src/snap.cpp
@@ -35,7 +35,7 @@
|
||||
Tins::SNAP::SNAP(PDU *child) : PDU(0xff, child) {
|
||||
std::memset(&_snap, 0, sizeof(_snap));
|
||||
_snap.dsap = _snap.ssap = 0xaa;
|
||||
_snap.id = 3;
|
||||
_snap.control = 3;
|
||||
}
|
||||
|
||||
Tins::SNAP::SNAP(const uint8_t *buffer, uint32_t total_sz) : PDU(0xff) {
|
||||
@@ -44,43 +44,37 @@ 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(eth_type()) {
|
||||
case Tins::Constants::Ethernet::IP:
|
||||
inner_pdu(new Tins::IP(buffer, total_sz));
|
||||
break;
|
||||
case Tins::Constants::Ethernet::ARP:
|
||||
inner_pdu(new Tins::ARP(buffer, total_sz));
|
||||
break;
|
||||
case Tins::Constants::Ethernet::EAPOL:
|
||||
inner_pdu(Tins::EAPOL::from_bytes(buffer, total_sz));
|
||||
break;
|
||||
};
|
||||
if(total_sz) {
|
||||
switch(eth_type()) {
|
||||
case Tins::Constants::Ethernet::IP:
|
||||
inner_pdu(new Tins::IP(buffer, total_sz));
|
||||
break;
|
||||
case Tins::Constants::Ethernet::ARP:
|
||||
inner_pdu(new Tins::ARP(buffer, total_sz));
|
||||
break;
|
||||
case Tins::Constants::Ethernet::EAPOL:
|
||||
inner_pdu(Tins::EAPOL::from_bytes(buffer, total_sz));
|
||||
break;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Tins::SNAP::SNAP(const SNAP &other) : PDU(other) {
|
||||
copy_fields(&other);
|
||||
void Tins::SNAP::control(uint8_t new_control) {
|
||||
_snap.control = new_control;
|
||||
}
|
||||
|
||||
Tins::SNAP &Tins::SNAP::operator=(const SNAP &other) {
|
||||
copy_fields(&other);
|
||||
copy_inner_pdu(other);
|
||||
return *this;
|
||||
}
|
||||
|
||||
void Tins::SNAP::id(uint8_t new_id) {
|
||||
_snap.id = new_id;
|
||||
}
|
||||
|
||||
void Tins::SNAP::poll(uint8_t new_poll) {
|
||||
_snap.poll = new_poll;
|
||||
}
|
||||
|
||||
void Tins::SNAP::org_code(uint32_t new_org) {
|
||||
_snap.org_code = new_org;
|
||||
void Tins::SNAP::org_code(small_uint<24> new_org) {
|
||||
// little endian fix, it was the only way to make it work.
|
||||
// check on big endian?
|
||||
#ifdef TINS_IS_LITTLE_ENDIAN
|
||||
_snap.org_code = Endian::host_to_be<uint32_t>(new_org) >> 8;
|
||||
#else
|
||||
_snap.org_code = Endian::host_to_be<uint32_t>(new_org);
|
||||
#endif
|
||||
}
|
||||
|
||||
void Tins::SNAP::eth_type(uint16_t new_eth) {
|
||||
_snap.eth_type = Utils::host_to_be(new_eth);
|
||||
_snap.eth_type = Endian::host_to_be(new_eth);
|
||||
}
|
||||
|
||||
uint32_t Tins::SNAP::header_size() const {
|
||||
@@ -104,17 +98,7 @@ void Tins::SNAP::write_serialization(uint8_t *buffer, uint32_t total_sz, const P
|
||||
default:
|
||||
type = 0;
|
||||
}
|
||||
_snap.eth_type = Utils::host_to_be(type);
|
||||
_snap.eth_type = Endian::host_to_be(type);
|
||||
}
|
||||
std::memcpy(buffer, &_snap, sizeof(_snap));
|
||||
}
|
||||
|
||||
void Tins::SNAP::copy_fields(const SNAP *other) {
|
||||
std::memcpy(&_snap, &other->_snap, sizeof(_snap));
|
||||
}
|
||||
|
||||
Tins::PDU *Tins::SNAP::clone_pdu() const {
|
||||
SNAP *new_pdu = new SNAP();
|
||||
new_pdu->copy_fields(this);
|
||||
return new_pdu;
|
||||
}
|
||||
|
||||
28
src/tcp.cpp
28
src/tcp.cpp
@@ -89,31 +89,31 @@ TCP::TCP(const uint8_t *buffer, uint32_t total_sz)
|
||||
}
|
||||
|
||||
void TCP::dport(uint16_t new_dport) {
|
||||
_tcp.dport = Utils::host_to_be(new_dport);
|
||||
_tcp.dport = Endian::host_to_be(new_dport);
|
||||
}
|
||||
|
||||
void TCP::sport(uint16_t new_sport) {
|
||||
_tcp.sport = Utils::host_to_be(new_sport);
|
||||
_tcp.sport = Endian::host_to_be(new_sport);
|
||||
}
|
||||
|
||||
void TCP::seq(uint32_t new_seq) {
|
||||
_tcp.seq = Utils::host_to_be(new_seq);
|
||||
_tcp.seq = Endian::host_to_be(new_seq);
|
||||
}
|
||||
|
||||
void TCP::ack_seq(uint32_t new_ack_seq) {
|
||||
_tcp.ack_seq = Utils::host_to_be(new_ack_seq);
|
||||
_tcp.ack_seq = Endian::host_to_be(new_ack_seq);
|
||||
}
|
||||
|
||||
void TCP::window(uint16_t new_window) {
|
||||
_tcp.window = Utils::host_to_be(new_window);
|
||||
_tcp.window = Endian::host_to_be(new_window);
|
||||
}
|
||||
|
||||
void TCP::check(uint16_t new_check) {
|
||||
_tcp.check = Utils::host_to_be(new_check);
|
||||
_tcp.check = Endian::host_to_be(new_check);
|
||||
}
|
||||
|
||||
void TCP::urg_ptr(uint16_t new_urg_ptr) {
|
||||
_tcp.urg_ptr = Utils::host_to_be(new_urg_ptr);
|
||||
_tcp.urg_ptr = Endian::host_to_be(new_urg_ptr);
|
||||
}
|
||||
|
||||
void TCP::payload(uint8_t *new_payload, uint32_t new_payload_size) {
|
||||
@@ -125,14 +125,14 @@ void TCP::data_offset(small_uint<4> new_doff) {
|
||||
}
|
||||
|
||||
void TCP::add_mss_option(uint16_t value) {
|
||||
value = Utils::host_to_be(value);
|
||||
value = Endian::host_to_be(value);
|
||||
add_option(MSS, 2, (uint8_t*)&value);
|
||||
}
|
||||
|
||||
bool TCP::search_mss_option(uint16_t *value) {
|
||||
if(!generic_search(MSS, value))
|
||||
return false;
|
||||
*value = Utils::host_to_be(*value);
|
||||
*value = Endian::host_to_be(*value);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -158,7 +158,7 @@ void 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::host_to_be(*it);
|
||||
*(ptr++) = Endian::host_to_be(*it);
|
||||
}
|
||||
add_option(SACK, (uint8_t)(sizeof(uint32_t) * edges.size()), (const uint8_t*)value);
|
||||
delete[] value;
|
||||
@@ -171,13 +171,13 @@ bool 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::host_to_be(*(ptr++)));
|
||||
edges->push_back(Endian::host_to_be(*(ptr++)));
|
||||
return true;
|
||||
}
|
||||
|
||||
void TCP::add_timestamp_option(uint32_t value, uint32_t reply) {
|
||||
uint64_t buffer = (uint64_t(value) << 32) | reply;
|
||||
buffer = Utils::host_to_be(buffer);
|
||||
buffer = Endian::host_to_be(buffer);
|
||||
add_option(TSOPT, 8, (uint8_t*)&buffer);
|
||||
}
|
||||
|
||||
@@ -186,7 +186,7 @@ bool TCP::search_timestamp_option(uint32_t *value, uint32_t *reply) {
|
||||
if(!option || option->value.size() != (sizeof(uint32_t) << 1))
|
||||
return false;
|
||||
uint64_t buffer = *(const uint64_t*)&option->value[0];
|
||||
buffer = Utils::be_to_host(buffer);
|
||||
buffer = Endian::be_to_host(buffer);
|
||||
*value = (buffer >> 32) & 0xffffffff;
|
||||
*reply = buffer & 0xffffffff;
|
||||
return true;
|
||||
@@ -308,7 +308,7 @@ void TCP::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *par
|
||||
while (checksum >> 16)
|
||||
checksum = (checksum & 0xffff) + (checksum >> 16);
|
||||
|
||||
((tcphdr*)tcp_start)->check = Utils::host_to_be<uint16_t>(~checksum);
|
||||
((tcphdr*)tcp_start)->check = Endian::host_to_be<uint16_t>(~checksum);
|
||||
}
|
||||
_tcp.check = 0;
|
||||
}
|
||||
|
||||
13
src/udp.cpp
13
src/udp.cpp
@@ -24,6 +24,7 @@
|
||||
#include <cstring>
|
||||
#include "udp.h"
|
||||
#include "constants.h"
|
||||
#include "utils.h"
|
||||
#include "ip.h"
|
||||
#include "rawpdu.h"
|
||||
|
||||
@@ -47,20 +48,16 @@ Tins::UDP::UDP(const uint8_t *buffer, uint32_t total_sz)
|
||||
inner_pdu(new RawPDU(buffer + sizeof(udphdr), total_sz));
|
||||
}
|
||||
|
||||
void Tins::UDP::payload(uint8_t *new_payload, uint32_t new_payload_size) {
|
||||
inner_pdu(new RawPDU(new_payload, new_payload_size));
|
||||
}
|
||||
|
||||
void Tins::UDP::dport(uint16_t new_dport) {
|
||||
_udp.dport = Utils::host_to_be(new_dport);
|
||||
_udp.dport = Endian::host_to_be(new_dport);
|
||||
}
|
||||
|
||||
void Tins::UDP::sport(uint16_t new_sport) {
|
||||
_udp.sport = Utils::host_to_be(new_sport);
|
||||
_udp.sport = Endian::host_to_be(new_sport);
|
||||
}
|
||||
|
||||
void Tins::UDP::length(uint16_t new_len) {
|
||||
_udp.len = Utils::host_to_be(new_len);
|
||||
_udp.len = Endian::host_to_be(new_len);
|
||||
}
|
||||
|
||||
uint32_t Tins::UDP::header_size() const {
|
||||
@@ -78,7 +75,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::host_to_be<uint16_t>(~checksum);
|
||||
((udphdr*)buffer)->check = Endian::host_to_be<uint16_t>(~checksum);
|
||||
}
|
||||
_udp.check = 0;
|
||||
}
|
||||
|
||||
@@ -35,6 +35,7 @@
|
||||
#include "ip.h"
|
||||
#include "icmp.h"
|
||||
#include "arp.h"
|
||||
#include "endianness.h"
|
||||
|
||||
|
||||
using namespace std;
|
||||
@@ -163,14 +164,14 @@ uint32_t Utils::do_checksum(const uint8_t *start, const uint8_t *end) {
|
||||
padding = *(end - 1) << 8;
|
||||
}
|
||||
while(ptr < last)
|
||||
checksum += Utils::host_to_be(*(ptr++));
|
||||
checksum += Endian::host_to_be(*(ptr++));
|
||||
return checksum + padding;
|
||||
}
|
||||
|
||||
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::host_to_be<uint32_t>(source_ip),
|
||||
dest_ip_int = Utils::host_to_be<uint32_t>(dest_ip);
|
||||
uint32_t source_ip_int = Endian::host_to_be<uint32_t>(source_ip),
|
||||
dest_ip_int = Endian::host_to_be<uint32_t>(dest_ip);
|
||||
uint16_t *ptr = (uint16_t*)&source_ip_int;
|
||||
|
||||
checksum += (uint32_t)(*ptr) + (uint32_t)(*(ptr+1));
|
||||
|
||||
Reference in New Issue
Block a user