1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-23 02:35:57 +01:00

Dot11 now uses option_not_found. Sniffer catches malformed_packet rather than std::runtime_error while sniffing.

This commit is contained in:
Matias Fontanini
2013-04-19 14:06:59 -03:00
parent 75bd445bd3
commit 91bdcca577
7 changed files with 617 additions and 391 deletions

View File

@@ -57,8 +57,7 @@ subdir = .
DIST_COMMON = README $(am__configure_deps) $(libtins_include_HEADERS) \
$(srcdir)/Makefile.am $(srcdir)/Makefile.in \
$(srcdir)/libtins.pc.in $(top_srcdir)/configure AUTHORS THANKS \
TODO config.guess config.sub depcomp install-sh ltmain.sh \
missing
config.guess config.sub depcomp install-sh ltmain.sh missing
ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
am__aclocal_m4_deps = $(top_srcdir)/configure.ac
am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \

View File

@@ -1245,7 +1245,8 @@ namespace Tins {
* \brief Helper method to search for this PDU's rsn information
* option.
*
* Throws a std::runtime_error if the option has not been set.
* An option_not_found exception is thrown if the option has not
* been set.
*
* \return std::string containing the ssid.
*/
@@ -1254,7 +1255,8 @@ namespace Tins {
/**
* \brief Helper method to search for this PDU's ssid.
*
* Throws a std::runtime_error if the option has not been set.
* An option_not_found exception is thrown if the option has not
* been set.
*
* \return std::string containing the ssid.
*/
@@ -1263,7 +1265,8 @@ namespace Tins {
/**
* \brief Helper method to get the supported rates.
*
* Throws a std::runtime_error if the option has not been set.
* An option_not_found exception is thrown if the option has not
* been set.
*
* \return rates_type containing the supported rates.
*/
@@ -1272,7 +1275,8 @@ namespace Tins {
/**
* \brief Helper method to get the extended supported rates.
*
* Throws a std::runtime_error if the option has not been set.
* An option_not_found exception is thrown if the option has not
* been set.
*
* \return rates_type containing the extended supported rates.
*/
@@ -1281,7 +1285,8 @@ namespace Tins {
/**
* \brief Helper method to get the QOS capability.
*
* Throws a std::runtime_error if the option has not been set.
* An option_not_found exception is thrown if the option has not
* been set.
*
* \return uint8_t containing the QOS capability.
*/
@@ -1290,7 +1295,8 @@ namespace Tins {
/**
* \brief Helper method to get the power capability.
*
* Throws a std::runtime_error if the option has not been set.
* An option_not_found exception is thrown if the option has not
* been set.
*
* \return std::pair<uint8_t, uint8_t> containing the power capability.
*/
@@ -1299,7 +1305,8 @@ namespace Tins {
/**
* \brief Helper method to get the supported channels.
*
* Throws a std::runtime_error if the option has not been set.
* An option_not_found exception is thrown if the option has not
* been set.
*
* \return channels_type containing the power capability.
*/
@@ -1308,7 +1315,8 @@ namespace Tins {
/**
* \brief Helper method to get the request information.
*
* Throws a std::runtime_error if the option has not been set.
* An option_not_found exception is thrown if the option has not
* been set.
*
* \return request_info_type containing the request information.
*/
@@ -1317,7 +1325,8 @@ namespace Tins {
/**
* \brief Helper method to get the fh parameter set.
*
* Throws a std::runtime_error if the option has not been set.
* An option_not_found exception is thrown if the option has not
* been set.
*
* \return fh_params_set containing the fh parameter set.
*/
@@ -1326,7 +1335,8 @@ namespace Tins {
/**
* \brief Helper method to get the ds parameter set.
*
* Throws a std::runtime_error if the option has not been set.
* An option_not_found exception is thrown if the option has not
* been set.
*
* \return uint8_t containing the ds parameter set.
*/
@@ -1335,7 +1345,8 @@ namespace Tins {
/**
* \brief Helper method to get the ibss parameter set.
*
* Throws a std::runtime_error if the option has not been set.
* An option_not_found exception is thrown if the option has not
* been set.
*
* \return uint16_t containing the ibss parameter set.
*/
@@ -1344,7 +1355,8 @@ namespace Tins {
/**
* \brief Helper method to get the ibss dfs.
*
* Throws a std::runtime_error if the option has not been set.
* An option_not_found exception is thrown if the option has not
* been set.
*
* \return ibss_dfs_params containing the ibss dfs.
*/
@@ -1353,7 +1365,8 @@ namespace Tins {
/**
* \brief Helper method to get the country option.
*
* Throws a std::runtime_error if the option has not been set.
* An option_not_found exception is thrown if the option has not
* been set.
*
* \return country_params containing the country attributes.
*/
@@ -1362,7 +1375,8 @@ namespace Tins {
/**
* \brief Helper method to get the fh parameters option.
*
* Throws a std::runtime_error if the option has not been set.
* An option_not_found exception is thrown if the option has not
* been set.
*
* \return std::pair<uint8_t, uint8_t> containing the fh parameters.
*/
@@ -1371,7 +1385,8 @@ namespace Tins {
/**
* \brief Helper method to get the fh patterns option.
*
* Throws a std::runtime_error if the option has not been set.
* An option_not_found exception is thrown if the option has not
* been set.
*
* \return fh_pattern_type containing the fh patterns.
*/
@@ -1380,7 +1395,8 @@ namespace Tins {
/**
* \brief Helper method to get the power constraint option.
*
* Throws a std::runtime_error if the option has not been set.
* An option_not_found exception is thrown if the option has not
* been set.
*
* \return uint8_t containing the power constraint.
*/
@@ -1389,7 +1405,8 @@ namespace Tins {
/**
* \brief Helper method to get the channel switch option.
*
* Throws a std::runtime_error if the option has not been set.
* An option_not_found exception is thrown if the option has not
* been set.
*
* \return channel_switch_type containing the channel switch.
*/
@@ -1398,7 +1415,8 @@ namespace Tins {
/**
* \brief Helper method to get the quiet option.
*
* Throws a std::runtime_error if the option has not been set.
* An option_not_found exception is thrown if the option has not
* been set.
*
* \return quiet_type containing the quiet option value.
*/
@@ -1407,7 +1425,8 @@ namespace Tins {
/**
* \brief Helper method to get the tpc report option.
*
* Throws a std::runtime_error if the option has not been set.
* An option_not_found exception is thrown if the option has not
* been set.
*
* \return quiet_type containing the tpc report option value.
*/
@@ -1416,7 +1435,8 @@ namespace Tins {
/**
* \brief Helper method to get the erp information option.
*
* Throws a std::runtime_error if the option has not been set.
* An option_not_found exception is thrown if the option has not
* been set.
*
* \return quiet_type containing the erp information option value.
*/
@@ -1425,7 +1445,8 @@ namespace Tins {
/**
* \brief Helper method to get the bss load option.
*
* Throws a std::runtime_error if the option has not been set.
* An option_not_found exception is thrown if the option has not
* been set.
*
* \return quiet_type containing the bss load option value.
*/
@@ -1434,7 +1455,8 @@ namespace Tins {
/**
* \brief Helper method to get the tim option.
*
* Throws a std::runtime_error if the option has not been set.
* An option_not_found exception is thrown if the option has not
* been set.
*
* \return tim_type containing the tim option value.
*/
@@ -1443,7 +1465,8 @@ namespace Tins {
/**
* \brief Helper method to get the challenge text option.
*
* Throws a std::runtime_error if the option has not been set.
* An option_not_found exception is thrown if the option has not
* been set.
*
* \return std::string containing the challenge text option value.
*/

View File

@@ -36,8 +36,12 @@ namespace Tins {
/**
* \brief Exception thrown when an option is not found.
*/
class option_not_found : public std::exception {
class option_not_found : public std::runtime_error {
public:
option_not_found()
: std::runtime_error(std::string()) { }
// try to avoid allocations by doing this.
const char* what() const throw() {
return "Option not found";
}
@@ -49,7 +53,11 @@ public:
class malformed_packet : public std::runtime_error {
public:
malformed_packet()
: std::runtime_error("Malformed") { }
: std::runtime_error(std::string()) { }
const char* what() const throw() {
return "Option not found";
}
};
}

View File

@@ -44,6 +44,7 @@
#include "dot11.h"
#include "sll.h"
#include "cxxstd.h"
#include "exceptions.h"
namespace Tins {
/**
@@ -268,10 +269,10 @@ namespace Tins {
template<class Functor>
void Tins::BaseSniffer::callback_handler(u_char *args, const struct pcap_pkthdr *header, const u_char *packet) {
bool ret_val(false);
LoopData<Functor> *data = reinterpret_cast<LoopData<Functor>*>(args);
try {
std::auto_ptr<PDU> pdu;
LoopData<Functor> *data = reinterpret_cast<LoopData<Functor>*>(args);
bool ret_val(false);
if(data->iface_type == DLT_EN10MB)
ret_val = call_functor<Tins::EthernetII>(data, packet, header);
else if(data->iface_type == DLT_IEEE802_11_RADIO)
@@ -287,13 +288,12 @@ namespace Tins {
ret_val = call_functor<Tins::Loopback>(data, packet, header);
else if(data->iface_type == DLT_LINUX_SLL)
ret_val = call_functor<Tins::SLL>(data, packet, header);
if(!ret_val)
pcap_breakloop(data->handle);
}
catch(std::runtime_error&) {
catch(malformed_packet&) {
}
if(!ret_val)
pcap_breakloop(data->handle);
}
template<class T>

View File

@@ -617,42 +617,42 @@ void Dot11ManagementFrame::challenge_text(const std::string &text) {
RSNInformation Dot11ManagementFrame::rsn_information() {
const Dot11::option *option = search_option(RSN);
if(!option || option->data_size() < (sizeof(uint16_t) << 1) + sizeof(uint32_t))
throw std::runtime_error("RSN information not set");
throw option_not_found();
return RSNInformation(option->data_ptr(), option->data_size());
}
string Dot11ManagementFrame::ssid() const {
const Dot11::option *option = search_option(SSID);
if(!option || option->data_size() == 0)
throw std::runtime_error("SSID not set");
throw option_not_found();
return string((const char*)option->data_ptr(), option->data_size());
}
Dot11ManagementFrame::rates_type Dot11ManagementFrame::supported_rates() const {
const Dot11::option *option = search_option(SUPPORTED_RATES);
if(!option || option->data_size() == 0)
throw std::runtime_error("Supported rates not set");
throw option_not_found();
return deserialize_rates(option);
}
Dot11ManagementFrame::rates_type Dot11ManagementFrame::extended_supported_rates() const {
const Dot11::option *option = search_option(EXT_SUPPORTED_RATES);
if(!option || option->data_size() == 0)
throw std::runtime_error("Extended supported rates not set");
throw option_not_found();
return deserialize_rates(option);
}
uint8_t Dot11ManagementFrame::qos_capability() const {
const Dot11::option *option = search_option(QOS_CAPABILITY);
if(!option || option->data_size() != 1)
throw std::runtime_error("QOS capability not set");
throw option_not_found();
return *option->data_ptr();
}
std::pair<uint8_t, uint8_t> Dot11ManagementFrame::power_capability() const {
const Dot11::option *option = search_option(POWER_CAPABILITY);
if(!option || option->data_size() != 2)
throw std::runtime_error("Power capability not set");
throw option_not_found();
return std::make_pair(*option->data_ptr(), *(option->data_ptr() + 1));
}
@@ -660,7 +660,7 @@ Dot11ManagementFrame::channels_type Dot11ManagementFrame::supported_channels() c
const Dot11::option *option = search_option(SUPPORTED_CHANNELS);
// We need a multiple of two
if(!option || ((option->data_size() & 0x1) == 1))
throw std::runtime_error("Supported channels not set");
throw option_not_found();
channels_type output;
const uint8_t *ptr = option->data_ptr(), *end = ptr + option->data_size();
while(ptr != end) {
@@ -673,7 +673,7 @@ Dot11ManagementFrame::channels_type Dot11ManagementFrame::supported_channels() c
Dot11ManagementFrame::request_info_type Dot11ManagementFrame::request_information() const {
const Dot11::option *option = search_option(REQUEST_INFORMATION);
if(!option || option->data_size() == 0)
throw std::runtime_error("Request information not set");
throw option_not_found();
request_info_type output;
const uint8_t *ptr = option->data_ptr(), *end = ptr + option->data_size();
output.assign(ptr, end);
@@ -683,7 +683,7 @@ Dot11ManagementFrame::request_info_type Dot11ManagementFrame::request_informatio
Dot11ManagementFrame::fh_params_set Dot11ManagementFrame::fh_parameter_set() const {
const Dot11::option *option = search_option(FH_SET);
if(!option || option->data_size() != sizeof(fh_params_set))
throw std::runtime_error("FH parameters set not set");
throw option_not_found();
fh_params_set output = *reinterpret_cast<const fh_params_set*>(option->data_ptr());
output.dwell_time = Endian::le_to_host(output.dwell_time);
output.hop_set = output.hop_set;
@@ -695,21 +695,21 @@ Dot11ManagementFrame::fh_params_set Dot11ManagementFrame::fh_parameter_set() con
uint8_t Dot11ManagementFrame::ds_parameter_set() const {
const Dot11::option *option = search_option(DS_SET);
if(!option || option->data_size() != sizeof(uint8_t))
throw std::runtime_error("DS parameters set not set");
throw option_not_found();
return *option->data_ptr();
}
uint16_t Dot11ManagementFrame::ibss_parameter_set() const {
const Dot11::option *option = search_option(IBSS_SET);
if(!option || option->data_size() != sizeof(uint16_t))
throw std::runtime_error("IBSS parameters set not set");
throw option_not_found();
return Endian::le_to_host(*reinterpret_cast<const uint16_t*>(option->data_ptr()));
}
Dot11ManagementFrame::ibss_dfs_params Dot11ManagementFrame::ibss_dfs() const {
const Dot11::option *option = search_option(IBSS_DFS);
if(!option || option->data_size() < ibss_dfs_params::minimum_size)
throw std::runtime_error("IBSS DFS set not set");
throw option_not_found();
ibss_dfs_params output;
const uint8_t *ptr = option->data_ptr(), *end = ptr + option->data_size();
output.dfs_owner = ptr;
@@ -718,7 +718,7 @@ Dot11ManagementFrame::ibss_dfs_params Dot11ManagementFrame::ibss_dfs() const {
while(ptr != end) {
uint8_t first = *(ptr++);
if(ptr == end)
throw std::runtime_error("Malformed channel data");
throw option_not_found();
output.channel_map.push_back(std::make_pair(first, *(ptr++)));
}
return output;
@@ -727,7 +727,7 @@ Dot11ManagementFrame::ibss_dfs_params Dot11ManagementFrame::ibss_dfs() const {
Dot11ManagementFrame::country_params Dot11ManagementFrame::country() const {
const Dot11::option *option = search_option(COUNTRY);
if(!option || option->data_size() < country_params::minimum_size)
throw std::runtime_error("Country option not set");
throw option_not_found();
country_params output;
const uint8_t *ptr = option->data_ptr(), *end = ptr + option->data_size();
std::copy(ptr, ptr + 3, std::back_inserter(output.country));
@@ -738,14 +738,14 @@ Dot11ManagementFrame::country_params Dot11ManagementFrame::country() const {
output.max_transmit_power.push_back(*(ptr++));
}
if(ptr != end)
throw std::runtime_error("Malformed option");
throw option_not_found();
return output;
}
std::pair<uint8_t, uint8_t> Dot11ManagementFrame::fh_parameters() const {
const Dot11::option *option = search_option(HOPPING_PATTERN_PARAMS);
if(!option || option->data_size() != sizeof(uint8_t) * 2)
throw std::runtime_error("FH parameters option not set");
throw option_not_found();
const uint8_t *ptr = option->data_ptr();
uint8_t first = *(ptr++);
return std::make_pair(first, *ptr);
@@ -754,7 +754,7 @@ std::pair<uint8_t, uint8_t> Dot11ManagementFrame::fh_parameters() const {
Dot11ManagementFrame::fh_pattern_type Dot11ManagementFrame::fh_pattern_table() const {
const Dot11::option *option = search_option(HOPPING_PATTERN_TABLE);
if(!option || option->data_size() < fh_pattern_type::minimum_size)
throw std::runtime_error("FH pattern option not set");
throw option_not_found();
fh_pattern_type output;
const uint8_t *ptr = option->data_ptr(), *end = ptr + option->data_size();
@@ -770,14 +770,14 @@ Dot11ManagementFrame::fh_pattern_type Dot11ManagementFrame::fh_pattern_table() c
uint8_t Dot11ManagementFrame::power_constraint() const {
const Dot11::option *option = search_option(POWER_CONSTRAINT);
if(!option || option->data_size() != 1)
throw std::runtime_error("Power constraint option not set");
throw option_not_found();
return *option->data_ptr();
}
Dot11ManagementFrame::channel_switch_type Dot11ManagementFrame::channel_switch() const {
const Dot11::option *option = search_option(CHANNEL_SWITCH);
if(!option || option->data_size() != sizeof(uint8_t) * 3)
throw std::runtime_error("Channel switch option not set");
throw option_not_found();
const uint8_t *ptr = option->data_ptr();
channel_switch_type output;
output.switch_mode = *(ptr++);
@@ -789,7 +789,7 @@ Dot11ManagementFrame::channel_switch_type Dot11ManagementFrame::channel_switch()
Dot11ManagementFrame::quiet_type Dot11ManagementFrame::quiet() const {
const Dot11::option *option = search_option(QUIET);
if(!option || option->data_size() != (sizeof(uint8_t) * 2 + sizeof(uint16_t) * 2))
throw std::runtime_error("Quiet option not set");
throw option_not_found();
const uint8_t *ptr = option->data_ptr();
quiet_type output;
@@ -804,7 +804,7 @@ Dot11ManagementFrame::quiet_type Dot11ManagementFrame::quiet() const {
std::pair<uint8_t, uint8_t> Dot11ManagementFrame::tpc_report() const {
const Dot11::option *option = search_option(TPC_REPORT);
if(!option || option->data_size() != sizeof(uint8_t) * 2)
throw std::runtime_error("TPC Report option not set");
throw option_not_found();
const uint8_t *ptr = option->data_ptr();
uint8_t first = *(ptr++);
return std::make_pair(first, *ptr);
@@ -813,14 +813,14 @@ std::pair<uint8_t, uint8_t> Dot11ManagementFrame::tpc_report() const {
uint8_t Dot11ManagementFrame::erp_information() const {
const Dot11::option *option = search_option(ERP_INFORMATION);
if(!option || option->data_size() != sizeof(uint8_t))
throw std::runtime_error("ERP Information option not set");
throw option_not_found();
return *option->data_ptr();
}
Dot11ManagementFrame::bss_load_type Dot11ManagementFrame::bss_load() const {
const Dot11::option *option = search_option(BSS_LOAD);
if(!option || option->data_size() != sizeof(uint8_t) + 2 * sizeof(uint16_t))
throw std::runtime_error("BSS Load option not set");
throw option_not_found();
bss_load_type output;
const uint8_t *ptr = option->data_ptr();
@@ -833,7 +833,7 @@ Dot11ManagementFrame::bss_load_type Dot11ManagementFrame::bss_load() const {
Dot11ManagementFrame::tim_type Dot11ManagementFrame::tim() const {
const Dot11::option *option = search_option(TIM);
if(!option || option->data_size() < 4 * sizeof(uint8_t))
throw std::runtime_error("TIM option not set");
throw option_not_found();
const uint8_t *ptr = option->data_ptr(), *end = ptr + option->data_size();
tim_type output;
@@ -848,7 +848,7 @@ Dot11ManagementFrame::tim_type Dot11ManagementFrame::tim() const {
std::string Dot11ManagementFrame::challenge_text() const {
const Dot11::option *option = search_option(CHALLENGE_TEXT);
if(!option || option->data_size() == 0)
throw std::runtime_error("Challenge text option not set");
throw option_not_found();
return std::string(option->data_ptr(), option->data_ptr() + option->data_size());
}

View File

@@ -87,16 +87,19 @@ PtrPacket BaseSniffer::next_packet() {
const u_char *content = pcap_next(handle, &header);
// timestamp_ = header.ts;
if(content) {
if(iface_type == DLT_EN10MB)
ret = new EthernetII((const uint8_t*)content, header.caplen);
else if(iface_type == DLT_IEEE802_11_RADIO)
ret = new RadioTap((const uint8_t*)content, header.caplen);
else if(iface_type == DLT_IEEE802_11)
ret = Dot11::from_bytes((const uint8_t*)content, header.caplen);
else if(iface_type == DLT_LOOP)
ret = new Tins::Loopback((const uint8_t*)content, header.caplen);
else if(iface_type == DLT_LINUX_SLL)
ret = new Tins::SLL((const uint8_t*)content, header.caplen);
try {
if(iface_type == DLT_EN10MB)
ret = new EthernetII((const uint8_t*)content, header.caplen);
else if(iface_type == DLT_IEEE802_11_RADIO)
ret = new RadioTap((const uint8_t*)content, header.caplen);
else if(iface_type == DLT_IEEE802_11)
ret = Dot11::from_bytes((const uint8_t*)content, header.caplen);
else if(iface_type == DLT_LOOP)
ret = new Tins::Loopback((const uint8_t*)content, header.caplen);
else if(iface_type == DLT_LINUX_SLL)
ret = new Tins::SLL((const uint8_t*)content, header.caplen);
}
catch(malformed_packet&) {}
}
return PtrPacket(ret, header.ts);
}

File diff suppressed because it is too large Load Diff