1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-27 04:11:35 +01:00

Code cleanup and use same syntax on the entire project

Initial code cleanup

More code cleanup

Cleanup more code

Cleanup Dot11 code

Fix OSX build issue

Cleanup examples

Fix ref and pointer declaration syntax

Fix braces
This commit is contained in:
Matias Fontanini
2016-01-02 08:17:59 -08:00
parent f5a82b1a17
commit d84f10cf08
177 changed files with 13203 additions and 12272 deletions

View File

@@ -59,24 +59,23 @@ using Tins::Memory::InputMemoryStream;
using Tins::Memory::OutputMemoryStream;
namespace Tins {
const Dot11::address_type Dot11::BROADCAST = "ff:ff:ff:ff:ff:ff";
Dot11::Dot11(const address_type &dst_hw_addr)
: _header(), _options_size(0)
{
Dot11::Dot11(const address_type& dst_hw_addr)
: header_(), options_size_(0) {
addr1(dst_hw_addr);
}
Dot11::Dot11(const ieee80211_header *header_ptr)
{
Dot11::Dot11(const dot11_header* header_ptr)
: header_(), options_size_(0) {
}
Dot11::Dot11(const uint8_t *buffer, uint32_t total_sz)
: _options_size(0)
{
Dot11::Dot11(const uint8_t* buffer, uint32_t total_sz)
: options_size_(0) {
InputMemoryStream stream(buffer, total_sz);
stream.read(_header);
stream.read(header_);
}
void Dot11::parse_tagged_parameters(InputMemoryStream& stream) {
@@ -93,104 +92,104 @@ void Dot11::parse_tagged_parameters(InputMemoryStream& stream) {
}
}
void Dot11::add_tagged_option(OptionTypes opt, uint8_t len, const uint8_t *val) {
void Dot11::add_tagged_option(OptionTypes opt, uint8_t len, const uint8_t* val) {
uint32_t opt_size = len + sizeof(uint8_t) * 2;
_options.push_back(option((uint8_t)opt, val, val + len));
_options_size += opt_size;
options_.push_back(option((uint8_t)opt, val, val + len));
options_size_ += opt_size;
}
void Dot11::internal_add_option(const option &opt) {
_options_size += static_cast<uint32_t>(opt.data_size() + sizeof(uint8_t) * 2);
void Dot11::internal_add_option(const option& opt) {
options_size_ += static_cast<uint32_t>(opt.data_size() + sizeof(uint8_t) * 2);
}
bool Dot11::remove_option(OptionTypes type) {
options_type::iterator iter = search_option_iterator(type);
if (iter == _options.end()) {
if (iter == options_.end()) {
return false;
}
_options_size -= static_cast<uint32_t>(iter->data_size() + sizeof(uint8_t) * 2);
_options.erase(iter);
options_size_ -= static_cast<uint32_t>(iter->data_size() + sizeof(uint8_t) * 2);
options_.erase(iter);
return true;
}
void Dot11::add_option(const option &opt) {
void Dot11::add_option(const option& opt) {
internal_add_option(opt);
_options.push_back(opt);
options_.push_back(opt);
}
const Dot11::option *Dot11::search_option(OptionTypes type) const {
const Dot11::option* Dot11::search_option(OptionTypes type) const {
// Search for the iterator. If we found something, return it, otherwise return nullptr.
options_type::const_iterator iter = search_option_iterator(type);
return (iter != _options.end()) ? &*iter : 0;
return (iter != options_.end()) ? &*iter : 0;
}
Dot11::options_type::const_iterator Dot11::search_option_iterator(OptionTypes type) const {
Internals::option_type_equality_comparator<option> comparator(static_cast<uint8_t>(type));
return find_if(_options.begin(), _options.end(), comparator);
return find_if(options_.begin(), options_.end(), comparator);
}
Dot11::options_type::iterator Dot11::search_option_iterator(OptionTypes type) {
Internals::option_type_equality_comparator<option> comparator(static_cast<uint8_t>(type));
return find_if(_options.begin(), _options.end(), comparator);
return find_if(options_.begin(), options_.end(), comparator);
}
void Dot11::protocol(small_uint<2> new_proto) {
this->_header.control.protocol = new_proto;
header_.control.protocol = new_proto;
}
void Dot11::type(small_uint<2> new_type) {
this->_header.control.type = new_type;
header_.control.type = new_type;
}
void Dot11::subtype(small_uint<4> new_subtype) {
this->_header.control.subtype = new_subtype;
header_.control.subtype = new_subtype;
}
void Dot11::to_ds(small_uint<1> new_value) {
this->_header.control.to_ds = (new_value)? 1 : 0;
header_.control.to_ds = (new_value)? 1 : 0;
}
void Dot11::from_ds(small_uint<1> new_value) {
this->_header.control.from_ds = (new_value)? 1 : 0;
header_.control.from_ds = (new_value)? 1 : 0;
}
void Dot11::more_frag(small_uint<1> new_value) {
this->_header.control.more_frag = (new_value)? 1 : 0;
header_.control.more_frag = (new_value)? 1 : 0;
}
void Dot11::retry(small_uint<1> new_value) {
this->_header.control.retry = (new_value)? 1 : 0;
header_.control.retry = (new_value)? 1 : 0;
}
void Dot11::power_mgmt(small_uint<1> new_value) {
this->_header.control.power_mgmt = (new_value)? 1 : 0;
header_.control.power_mgmt = (new_value)? 1 : 0;
}
void Dot11::wep(small_uint<1> new_value) {
this->_header.control.wep = (new_value)? 1 : 0;
header_.control.wep = (new_value)? 1 : 0;
}
void Dot11::order(small_uint<1> new_value) {
this->_header.control.order = (new_value)? 1 : 0;
header_.control.order = (new_value)? 1 : 0;
}
void Dot11::duration_id(uint16_t new_duration_id) {
this->_header.duration_id = Endian::host_to_le(new_duration_id);
header_.duration_id = Endian::host_to_le(new_duration_id);
}
void Dot11::addr1(const address_type &new_addr1) {
std::copy(new_addr1.begin(), new_addr1.end(), _header.addr1);
void Dot11::addr1(const address_type& new_addr1) {
new_addr1.copy(header_.addr1);
}
uint32_t Dot11::header_size() const {
uint32_t sz = sizeof(ieee80211_header) + _options_size;
return sz;
return sizeof(header_) + options_size_;
}
#ifndef _WIN32
void Dot11::send(PacketSender &sender, const NetworkInterface &iface) {
if(!iface)
void Dot11::send(PacketSender& sender, const NetworkInterface& iface) {
if (!iface) {
throw invalid_interface();
}
#if !defined(BSD) && !defined(__FreeBSD_kernel__)
sockaddr_ll addr;
@@ -201,7 +200,7 @@ void Dot11::send(PacketSender &sender, const NetworkInterface &iface) {
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);
memcpy(&(addr.sll_addr), header_.addr1, 6);
sender.send_l2(*this, (struct sockaddr*)&addr, (uint32_t)sizeof(addr));
#else
sender.send_l2(*this, 0, 0, iface);
@@ -209,74 +208,84 @@ void Dot11::send(PacketSender &sender, const NetworkInterface &iface) {
}
#endif // _WIN32
void Dot11::write_serialization(uint8_t *buffer, uint32_t total_sz, const PDU *parent) {
void Dot11::write_serialization(uint8_t* buffer, uint32_t total_sz, const PDU* parent) {
OutputMemoryStream stream(buffer, total_sz);
stream.write(_header);
stream.write(header_);
write_ext_header(stream);
write_fixed_parameters(stream);
for (std::list<option>::const_iterator it = _options.begin(); it != _options.end(); ++it) {
for (std::list<option>::const_iterator it = options_.begin(); it != options_.end(); ++it) {
stream.write<uint8_t>(it->option());
stream.write<uint8_t>(it->length_field());
stream.write(it->data_ptr(), it->data_size());
}
}
Dot11 *Dot11::from_bytes(const uint8_t *buffer, uint32_t total_sz) {
Dot11* 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.
// This should be sizeof(ieee80211_header::control), but gcc 4.2 complains
if(total_sz < 2)
// This should be sizeof(dot11_header::control), but gcc 4.2 complains
if (total_sz < 2) {
throw malformed_packet();
const ieee80211_header *hdr = (const ieee80211_header*)buffer;
Dot11 *ret = 0;
if(hdr->control.type == MANAGEMENT) {
if(hdr->control.subtype == BEACON)
ret = new Dot11Beacon(buffer, total_sz);
else if(hdr->control.subtype == DISASSOC)
ret = new Dot11Disassoc(buffer, total_sz);
else if(hdr->control.subtype == ASSOC_REQ)
ret = new Dot11AssocRequest(buffer, total_sz);
else if(hdr->control.subtype == ASSOC_RESP)
ret = new Dot11AssocResponse(buffer, total_sz);
else if(hdr->control.subtype == REASSOC_REQ)
ret = new Dot11ReAssocRequest(buffer, total_sz);
else if(hdr->control.subtype == REASSOC_RESP)
ret = new Dot11ReAssocResponse(buffer, total_sz);
else if(hdr->control.subtype == AUTH)
ret = new Dot11Authentication(buffer, total_sz);
else if(hdr->control.subtype == DEAUTH)
ret = new Dot11Deauthentication(buffer, total_sz);
else if(hdr->control.subtype == PROBE_REQ)
ret = new Dot11ProbeRequest(buffer, total_sz);
else if(hdr->control.subtype == PROBE_RESP)
ret = new Dot11ProbeResponse(buffer, total_sz);
}
else if(hdr->control.type == DATA){
if(hdr->control.subtype <= 4)
ret = new Dot11Data(buffer, total_sz);
else
ret = new Dot11QoSData(buffer, total_sz);
const dot11_header* hdr = (const dot11_header*)buffer;
if (hdr->control.type == MANAGEMENT) {
switch (hdr->control.subtype) {
case BEACON:
return new Dot11Beacon(buffer, total_sz);
case DISASSOC:
return new Dot11Disassoc(buffer, total_sz);
case ASSOC_REQ:
return new Dot11AssocRequest(buffer, total_sz);
case ASSOC_RESP:
return new Dot11AssocResponse(buffer, total_sz);
case REASSOC_REQ:
return new Dot11ReAssocRequest(buffer, total_sz);
case REASSOC_RESP:
return new Dot11ReAssocResponse(buffer, total_sz);
case AUTH:
return new Dot11Authentication(buffer, total_sz);
case DEAUTH:
return new Dot11Deauthentication(buffer, total_sz);
case PROBE_REQ:
return new Dot11ProbeRequest(buffer, total_sz);
case PROBE_RESP:
return new Dot11ProbeResponse(buffer, total_sz);
default:
break;
};
}
else if(hdr->control.type == CONTROL){
if(hdr->control.subtype == ACK)
ret = new Dot11Ack(buffer, total_sz);
else if(hdr->control.subtype == CF_END)
ret = new Dot11CFEnd(buffer, total_sz);
else if(hdr->control.subtype == CF_END_ACK)
ret = new Dot11EndCFAck(buffer, total_sz);
else if(hdr->control.subtype == PS)
ret = new Dot11PSPoll(buffer, total_sz);
else if(hdr->control.subtype == RTS)
ret = new Dot11RTS(buffer, total_sz);
else if(hdr->control.subtype == BLOCK_ACK)
ret = new Dot11BlockAck(buffer, total_sz);
else if(hdr->control.subtype == BLOCK_ACK_REQ)
ret = new Dot11BlockAckRequest(buffer, total_sz);
else if (hdr->control.type == DATA) {
if (hdr->control.subtype <= 4) {
return new Dot11Data(buffer, total_sz);
}
else {
return new Dot11QoSData(buffer, total_sz);
}
}
if(ret == 0)
ret = new Dot11(buffer, total_sz);
return ret;
else if (hdr->control.type == CONTROL) {
switch (hdr->control.subtype) {
case ACK:
return new Dot11Ack(buffer, total_sz);
case CF_END:
return new Dot11CFEnd(buffer, total_sz);
case CF_END_ACK:
return new Dot11EndCFAck(buffer, total_sz);
case PS:
return new Dot11PSPoll(buffer, total_sz);
case RTS:
return new Dot11RTS(buffer, total_sz);
case BLOCK_ACK:
return new Dot11BlockAck(buffer, total_sz);
case BLOCK_ACK_REQ:
return new Dot11BlockAckRequest(buffer, total_sz);
default:
break;
};
}
// Fallback to just building a dot11
return new Dot11(buffer, total_sz);
}
} // namespace Tins
} // Tins
#endif // HAVE_DOT11