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

Use custom exceptions everywhere

This commit is contained in:
Matias Fontanini
2017-05-13 10:59:15 -07:00
parent 1a83801722
commit 1ef5be352a
14 changed files with 37 additions and 37 deletions

View File

@@ -30,9 +30,9 @@
#ifndef TINS_ADDRESS_RANGE
#define TINS_ADDRESS_RANGE
#include <stdexcept>
#include <iterator>
#include "endianness.h"
#include "exceptions.h"
#include "detail/address_helpers.h"
namespace Tins {
@@ -198,7 +198,7 @@ public:
AddressRange(const address_type& first, const address_type& last, bool only_hosts = false)
: first_(first), last_(last), only_hosts_(only_hosts){
if (last_ < first_) {
throw std::runtime_error("Invalid address range");
throw exception_base("Invalid address range");
}
}

View File

@@ -100,6 +100,14 @@ public:
invalid_address() : exception_base("Invalid address") { }
};
/**
* \brief Exception thrown when a PDU option is set using an incorrect value
*/
class invalid_option_value : public exception_base {
public:
invalid_option_value() : exception_base("Invalid option value") { }
};
/**
* \brief Exception thrown when a field is not present in frame.
*/
@@ -204,6 +212,10 @@ public:
pcap_error(const char* message) : exception_base(message) {
}
pcap_error(const std::string& message) : exception_base(message) {
}
};
/**

View File

@@ -33,7 +33,6 @@
#include "memory_helpers.h"
using std::vector;
using std::runtime_error;
using std::memcpy;
using std::equal;
@@ -448,7 +447,7 @@ void DHCPv6::server_id(const duid_type& value) {
DHCPv6::duid_llt DHCPv6::duid_llt::from_bytes(const uint8_t* buffer, uint32_t total_sz) {
// at least one byte for lladdress
if (total_sz < sizeof(uint16_t) + sizeof(uint32_t) + 1) {
throw runtime_error("Not enough size for a DUID_LLT identifier");
throw malformed_option();
}
InputMemoryStream stream(buffer, total_sz);
duid_llt output;
@@ -470,7 +469,7 @@ PDU::serialization_type DHCPv6::duid_llt::serialize() const {
DHCPv6::duid_en DHCPv6::duid_en::from_bytes(const uint8_t* buffer, uint32_t total_sz) {
// at least one byte for identifier
if (total_sz < sizeof(uint32_t) + 1) {
throw runtime_error("Not enough size for a DUID_en identifier");
throw malformed_option();
}
InputMemoryStream stream(buffer, total_sz);
duid_en output;
@@ -490,7 +489,7 @@ PDU::serialization_type DHCPv6::duid_en::serialize() const {
DHCPv6::duid_ll DHCPv6::duid_ll::from_bytes(const uint8_t* buffer, uint32_t total_sz) {
// at least one byte for lladdress
if (total_sz < sizeof(uint16_t) + 1) {
throw runtime_error("Not enough size for a DUID_en identifier");
throw malformed_option();
}
InputMemoryStream stream(buffer, total_sz);
duid_ll output;

View File

@@ -38,7 +38,6 @@ using std::string;
using std::copy;
using std::vector;
using std::back_inserter;
using std::runtime_error;
using std::pair;
using std::make_pair;
@@ -239,10 +238,10 @@ void Dot11ManagementFrame::ibss_dfs(const ibss_dfs_params& params) {
void Dot11ManagementFrame::country(const country_params& params) {
if ((params.first_channel.size() != params.number_channels.size()) ||
(params.number_channels.size() != params.max_transmit_power.size())) {
throw runtime_error("The length of the lists are distinct");
throw invalid_option_value();
}
if (params.country.size() != 3) {
throw runtime_error("Invalid country identifier length");
throw invalid_option_value();
}
size_t sz = sizeof(uint8_t) * 3 * params.first_channel.size() + params.country.size();
// Use 1 byte padding at the end if the length is odd.

View File

@@ -120,7 +120,7 @@ void EthernetII::send(PacketSender& sender, const NetworkInterface& iface) {
sender.send_l2(*this, 0, 0, iface);
#elif defined(_WIN32)
// On Windows we can only send l2 PDUs using pcap_sendpacket
throw std::runtime_error("LIBTINS_USE_PCAP_SENDPACKET is not enabled");
throw feature_disabled();
#else
// Default GNU/Linux behaviour
struct sockaddr_ll addr;

View File

@@ -83,7 +83,7 @@ void string_to_hw_address(const string& hw_addr, uint8_t* output, size_t output_
break;
}
else {
throw std::runtime_error("Invalid byte found");
throw invalid_address();
}
i++;
}
@@ -94,7 +94,7 @@ void string_to_hw_address(const string& hw_addr, uint8_t* output, size_t output_
i++;
}
else {
throw std::runtime_error("Invalid separator");
throw invalid_address();
}
}
}

View File

@@ -34,8 +34,6 @@
#include "mpls.h"
#include "utils/checksum_utils.h"
using std::runtime_error;
using Tins::Memory::InputMemoryStream;
using Tins::Memory::OutputMemoryStream;

View File

@@ -185,13 +185,13 @@ pcap_t* PacketSender::make_pcap_handle(const NetworkInterface& iface) const {
char error[PCAP_ERRBUF_SIZE];
pcap_t* handle = pcap_create(TINS_PREFIX_INTERFACE(iface.name()).c_str(), error);
if (!handle) {
throw runtime_error("Error opening pcap handle: " + string(error));
throw pcap_error("Error opening pcap handle: " + string(error));
}
if (pcap_set_promisc(handle, 1) < 0) {
throw runtime_error("Failed to set pcap handle promisc mode: " + string(pcap_geterr(handle)));
throw pcap_error("Failed to set pcap handle promisc mode: " + string(pcap_geterr(handle)));
}
if (pcap_activate(handle) < 0) {
throw runtime_error("Failed to activate pcap handle: " + string(pcap_geterr(handle)));
throw pcap_error("Failed to activate pcap handle: " + string(pcap_geterr(handle)));
}
return handle;
}
@@ -362,8 +362,7 @@ void PacketSender::send_l2(PDU& pdu,
pcap_t* handle = pcap_handles_[iface];
const int buf_size = static_cast<int>(buffer.size());
if (pcap_sendpacket(handle, (u_char*)&buffer[0], buf_size) != 0) {
throw runtime_error("Failed to send packet: " +
string(pcap_geterr(handle)));
throw pcap_error("Failed to send packet: " + string(pcap_geterr(handle)));
}
#else // TINS_HAVE_PACKET_SENDER_PCAP_SENDPACKET
int sock = get_ether_socket(iface);
@@ -402,7 +401,7 @@ PDU* PacketSender::recv_l3(PDU& pdu,
vector<int> sockets(1, sockets_[type]);
if (type == IP_TCP_SOCKET || type == IP_UDP_SOCKET) {
#ifdef BSD
throw runtime_error("Receiving L3 packets not supported on this platform");
throw feature_disabled();
#endif
open_l3_socket(ICMP_SOCKET);
sockets.push_back(sockets_[ICMP_SOCKET]);

View File

@@ -46,7 +46,6 @@
#include "detail/pdu_helpers.h"
using std::string;
using std::runtime_error;
namespace Tins {
@@ -237,7 +236,7 @@ Sniffer::Sniffer(const string& device, const SnifferConfiguration& configuration
char error[PCAP_ERRBUF_SIZE];
pcap_t* phandle = pcap_create(TINS_PREFIX_INTERFACE(device).c_str(), error);
if (!phandle) {
throw runtime_error(error);
throw pcap_error(error);
}
set_pcap_handle(phandle);
@@ -250,7 +249,7 @@ Sniffer::Sniffer(const string& device, const SnifferConfiguration& configuration
// Configure the sniffer's attributes prior to activation.
configuration.configure_sniffer_pre_activation(*this);
// Finally, activate the pcap. In case of error throw runtime_error
// Finally, activate the pcap. In case of error, throw
if (pcap_activate(get_pcap_handle()) < 0) {
throw pcap_error(pcap_geterr(get_pcap_handle()));
}
@@ -273,7 +272,7 @@ Sniffer::Sniffer(const string& device,
char error[PCAP_ERRBUF_SIZE];
pcap_t* phandle = pcap_create(TINS_PREFIX_INTERFACE(device).c_str(), error);
if (!phandle) {
throw runtime_error(error);
throw pcap_error(error);
}
set_pcap_handle(phandle);
@@ -286,7 +285,7 @@ Sniffer::Sniffer(const string& device,
// Configure the sniffer's attributes prior to activation.
configuration.configure_sniffer_pre_activation(*this);
// Finally, activate the pcap. In case of error throw runtime_error
// Finally, activate the pcap. In case of error, throw
if (pcap_activate(get_pcap_handle()) < 0) {
throw pcap_error(pcap_geterr(get_pcap_handle()));
}
@@ -307,7 +306,7 @@ Sniffer::Sniffer(const string& device,
char error[PCAP_ERRBUF_SIZE];
pcap_t* phandle = pcap_create(TINS_PREFIX_INTERFACE(device).c_str(), error);
if (!phandle) {
throw runtime_error(error);
throw pcap_error(error);
}
set_pcap_handle(phandle);
@@ -320,7 +319,7 @@ Sniffer::Sniffer(const string& device,
// Configure the sniffer's attributes prior to activation.
configuration.configure_sniffer_pre_activation(*this);
// Finally, activate the pcap. In case of error throw runtime_error
// Finally, activate the pcap. In case of error, throw
if (pcap_activate(get_pcap_handle()) < 0) {
throw pcap_error(pcap_geterr(get_pcap_handle()));
}

View File

@@ -45,7 +45,6 @@
using std::make_pair;
using std::bind;
using std::pair;
using std::runtime_error;
using std::numeric_limits;
using Tins::Memory::OutputMemoryStream;

View File

@@ -45,10 +45,8 @@
using std::make_pair;
using std::bind;
using std::pair;
using std::runtime_error;
using std::numeric_limits;
namespace Tins {
namespace TCPIP {

View File

@@ -45,7 +45,6 @@
using std::make_pair;
using std::bind;
using std::pair;
using std::runtime_error;
using std::numeric_limits;
using std::chrono::system_clock;
using std::chrono::minutes;

View File

@@ -59,7 +59,6 @@
#include "detail/smart_ptr.h"
using std::string;
using std::runtime_error;
/** \cond */
@@ -72,7 +71,7 @@ addrinfo* resolve_domain(const string& to_resolve, int family) {
return result;
}
else {
throw runtime_error("Could not resolve address");
throw Tins::exception_base("Could not resolve address");
}
}
@@ -121,7 +120,7 @@ HWAddress<6> resolve_hwaddr(const NetworkInterface& iface,
}
}
#endif
throw runtime_error("Could not resolve hardware address");
throw exception_base("Could not resolve hardware address");
}
HWAddress<6> resolve_hwaddr(IPv4Address ip, PacketSender& sender) {

View File

@@ -63,7 +63,6 @@ using std::string;
using std::set;
using std::ifstream;
using std::istream;
using std::runtime_error;
namespace Tins {
namespace Utils {
@@ -147,12 +146,12 @@ vector<char> query_route_table(int family) {
mib[4] = NET_RT_DUMP;
mib[5] = 0;
if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
throw runtime_error("sysctl failed");
throw exception_base("sysctl failed");
}
buf.resize(len);
if (sysctl(mib, 6, &buf[0], &len, NULL, 0) < 0) {
throw runtime_error("sysctl failed");
throw exception_base("sysctl failed");
}
return buf;