diff --git a/include/tins/address_range.h b/include/tins/address_range.h index 89a753a..2093637 100644 --- a/include/tins/address_range.h +++ b/include/tins/address_range.h @@ -30,9 +30,9 @@ #ifndef TINS_ADDRESS_RANGE #define TINS_ADDRESS_RANGE -#include #include #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"); } } diff --git a/include/tins/exceptions.h b/include/tins/exceptions.h index 0b6405a..cd841ca 100644 --- a/include/tins/exceptions.h +++ b/include/tins/exceptions.h @@ -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) { + + } }; /** diff --git a/src/dhcpv6.cpp b/src/dhcpv6.cpp index 6d6285e..4107920 100644 --- a/src/dhcpv6.cpp +++ b/src/dhcpv6.cpp @@ -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; diff --git a/src/dot11/dot11_mgmt.cpp b/src/dot11/dot11_mgmt.cpp index 9c42ff9..aa9acb1 100644 --- a/src/dot11/dot11_mgmt.cpp +++ b/src/dot11/dot11_mgmt.cpp @@ -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. diff --git a/src/ethernetII.cpp b/src/ethernetII.cpp index 0c1021c..e09a36a 100644 --- a/src/ethernetII.cpp +++ b/src/ethernetII.cpp @@ -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; diff --git a/src/hw_address.cpp b/src/hw_address.cpp index 08bb42d..e373b4f 100644 --- a/src/hw_address.cpp +++ b/src/hw_address.cpp @@ -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(); } } } diff --git a/src/icmp_extension.cpp b/src/icmp_extension.cpp index 4169088..dfa9620 100644 --- a/src/icmp_extension.cpp +++ b/src/icmp_extension.cpp @@ -34,8 +34,6 @@ #include "mpls.h" #include "utils/checksum_utils.h" -using std::runtime_error; - using Tins::Memory::InputMemoryStream; using Tins::Memory::OutputMemoryStream; diff --git a/src/packet_sender.cpp b/src/packet_sender.cpp index 58fafd1..2a9e320 100644 --- a/src/packet_sender.cpp +++ b/src/packet_sender.cpp @@ -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(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 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]); diff --git a/src/sniffer.cpp b/src/sniffer.cpp index 3d385e8..e393a1d 100644 --- a/src/sniffer.cpp +++ b/src/sniffer.cpp @@ -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())); } diff --git a/src/tcp_ip/flow.cpp b/src/tcp_ip/flow.cpp index e6d96b8..7a6a888 100644 --- a/src/tcp_ip/flow.cpp +++ b/src/tcp_ip/flow.cpp @@ -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; diff --git a/src/tcp_ip/stream.cpp b/src/tcp_ip/stream.cpp index 7bb4bbd..2fb2ff0 100644 --- a/src/tcp_ip/stream.cpp +++ b/src/tcp_ip/stream.cpp @@ -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 { diff --git a/src/tcp_ip/stream_follower.cpp b/src/tcp_ip/stream_follower.cpp index d0c3528..d0250ac 100644 --- a/src/tcp_ip/stream_follower.cpp +++ b/src/tcp_ip/stream_follower.cpp @@ -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; diff --git a/src/utils/resolve_utils.cpp b/src/utils/resolve_utils.cpp index 5039c0c..b620c12 100644 --- a/src/utils/resolve_utils.cpp +++ b/src/utils/resolve_utils.cpp @@ -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) { diff --git a/src/utils/routing_utils.cpp b/src/utils/routing_utils.cpp index f16b9bd..35860ae 100644 --- a/src/utils/routing_utils.cpp +++ b/src/utils/routing_utils.cpp @@ -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 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;