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

@@ -67,73 +67,82 @@
#include "internals.h"
using std::string;
using std::ostringstream;
using std::max;
using std::make_pair;
using std::vector;
using std::runtime_error;
namespace Tins {
const int PacketSender::INVALID_RAW_SOCKET = -1;
const uint32_t PacketSender::DEFAULT_TIMEOUT = 2;
#ifndef _WIN32
typedef int socket_type;
const char *make_error_string() {
const char* make_error_string() {
return strerror(errno);
}
#else
typedef SOCKET socket_type;
// fixme
const char *make_error_string() {
const char* make_error_string() {
return "error";
}
#endif
PacketSender::PacketSender(const NetworkInterface &iface, uint32_t recv_timeout,
uint32_t usec)
: _sockets(SOCKETS_END, INVALID_RAW_SOCKET),
PacketSender::PacketSender(const NetworkInterface& iface,
uint32_t recv_timeout,
uint32_t usec)
: sockets_(SOCKETS_END, INVALID_RAW_SOCKET),
#if !defined(BSD) && !defined(_WIN32) && !defined(__FreeBSD_kernel__)
_ether_socket(INVALID_RAW_SOCKET),
ether_socket_(INVALID_RAW_SOCKET),
#endif
_timeout(recv_timeout), _timeout_usec(usec), default_iface(iface)
{
_types[IP_TCP_SOCKET] = IPPROTO_TCP;
_types[IP_UDP_SOCKET] = IPPROTO_UDP;
_types[IP_RAW_SOCKET] = IPPROTO_RAW;
_types[IPV6_SOCKET] = IPPROTO_RAW;
_types[ICMP_SOCKET] = IPPROTO_ICMP;
_timeout(recv_timeout), timeout_usec_(usec), default_iface_(iface) {
types_[IP_TCP_SOCKET] = IPPROTO_TCP;
types_[IP_UDP_SOCKET] = IPPROTO_UDP;
types_[IP_RAW_SOCKET] = IPPROTO_RAW;
types_[IPV6_SOCKET] = IPPROTO_RAW;
types_[ICMP_SOCKET] = IPPROTO_ICMP;
}
PacketSender::~PacketSender() {
for(unsigned i(0); i < _sockets.size(); ++i) {
if(_sockets[i] != INVALID_RAW_SOCKET)
#ifndef _WIN32
::close(_sockets[i]);
#else
::closesocket(_sockets[i]);
#endif
for (unsigned i(0); i < sockets_.size(); ++i) {
if (sockets_[i] != INVALID_RAW_SOCKET) {
#ifndef _WIN32
::close(sockets_[i]);
#else
::closesocket(sockets_[i]);
#endif
}
}
#if defined(BSD) || defined(__FreeBSD_kernel__)
for(BSDEtherSockets::iterator it = _ether_socket.begin(); it != _ether_socket.end(); ++it)
for (BSDEtherSockets::iterator it = ether_socket_.begin();
it != ether_socket_.end(); ++it) {
::close(it->second);
}
#elif !defined(_WIN32)
if(_ether_socket != INVALID_RAW_SOCKET)
::close(_ether_socket);
if (ether_socket_ != INVALID_RAW_SOCKET) {
::close(ether_socket_);
}
#endif
#ifdef HAVE_PACKET_SENDER_PCAP_SENDPACKET
for (PcapHandleMap::iterator it = pcap_handles.begin(); it != pcap_handles.end(); ++it) {
for (PcapHandleMap::iterator it = pcap_handles_.begin(); it != pcap_handles_.end(); ++it) {
pcap_close(it->second);
}
pcap_handles.clear();
pcap_handles_.clear();
#endif // HAVE_PACKET_SENDER_PCAP_SENDPACKET
}
void PacketSender::default_interface(const NetworkInterface &iface) {
default_iface = iface;
void PacketSender::default_interface(const NetworkInterface& iface) {
default_iface_ = iface;
}
const NetworkInterface& PacketSender::default_interface() const {
return default_iface;
return default_iface_;
}
#if !defined(_WIN32) || defined(HAVE_PACKET_SENDER_PCAP_SENDPACKET)
@@ -141,19 +150,20 @@ const NetworkInterface& PacketSender::default_interface() const {
#ifndef _WIN32
bool PacketSender::ether_socket_initialized(const NetworkInterface& iface) const {
#if defined(BSD) || defined(__FreeBSD_kernel__)
return _ether_socket.count(iface.id());
return ether_socket_.count(iface.id());
#else
return _ether_socket != INVALID_RAW_SOCKET;
return ether_socket_ != INVALID_RAW_SOCKET;
#endif
}
int PacketSender::get_ether_socket(const NetworkInterface& iface) {
if(!ether_socket_initialized(iface))
int PacketSender::getether_socket_(const NetworkInterface& iface) {
if (!ether_socket_initialized(iface)) {
open_l2_socket(iface);
}
#if defined(BSD) || defined(__FreeBSD_kernel__)
return _ether_socket[iface.id()];
return ether_socket_[iface.id()];
#else
return _ether_socket;
return ether_socket_;
#endif
}
#endif // _WIN32
@@ -161,6 +171,8 @@ int PacketSender::get_ether_socket(const NetworkInterface& iface) {
#ifdef HAVE_PACKET_SENDER_PCAP_SENDPACKET
pcap_t* PacketSender::make_pcap_handle(const NetworkInterface& iface) const {
// This is an ugly fix to make interface names look like what
// libpcap expects on Windows
#ifdef _WIN32
#define TINS_PREFIX_INTERFACE(x) ("\\Device\\NPF_" + x)
#else // _WIN32
@@ -185,41 +197,45 @@ pcap_t* PacketSender::make_pcap_handle(const NetworkInterface& iface) const {
void PacketSender::open_l2_socket(const NetworkInterface& iface) {
#ifdef HAVE_PACKET_SENDER_PCAP_SENDPACKET
if (pcap_handles.count(iface) == 0) {
pcap_handles.insert(std::make_pair(iface, make_pcap_handle(iface)));
if (pcap_handles_.count(iface) == 0) {
pcap_handles_.insert(make_pair(iface, make_pcap_handle(iface)));
}
#elif defined(BSD) || defined(__FreeBSD_kernel__)
int sock = -1;
// At some point, there should be an available device
for (int i = 0; sock == -1;i++) {
std::ostringstream oss;
ostringstream oss;
oss << "/dev/bpf" << i;
sock = open(oss.str().c_str(), O_RDWR);
}
if(sock == -1)
if (sock == -1) {
throw socket_open_error(make_error_string());
}
struct ifreq ifr;
strncpy(ifr.ifr_name, iface.name().c_str(), sizeof(ifr.ifr_name) - 1);
if(ioctl(sock, BIOCSETIF, (caddr_t)&ifr) < 0) {
if (ioctl(sock, BIOCSETIF, (caddr_t)&ifr) < 0) {
::close(sock);
throw socket_open_error(make_error_string());
}
// Use immediate mode
u_int value = 1;
if(ioctl(sock, BIOCIMMEDIATE, &value) < 0)
if (ioctl(sock, BIOCIMMEDIATE, &value) < 0) {
throw socket_open_error(make_error_string());
}
// Get the buffer size
if(ioctl(sock, BIOCGBLEN, &buffer_size) < 0)
if (ioctl(sock, BIOCGBLEN, &buffer_size_) < 0) {
throw socket_open_error(make_error_string());
_ether_socket[iface.id()] = sock;
}
ether_socket_[iface.id()] = sock;
#else
if (_ether_socket == INVALID_RAW_SOCKET) {
_ether_socket = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if (ether_socket_ == INVALID_RAW_SOCKET) {
ether_socket_ = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if (_ether_socket == -1)
if (ether_socket_ == -1) {
throw socket_open_error(make_error_string());
}
}
#endif
}
@@ -227,13 +243,15 @@ void PacketSender::open_l2_socket(const NetworkInterface& iface) {
void PacketSender::open_l3_socket(SocketType type) {
int socktype = find_type(type);
if(socktype == -1)
if (socktype == -1) {
throw invalid_socket_type();
if(_sockets[type] == INVALID_RAW_SOCKET) {
}
if (sockets_[type] == INVALID_RAW_SOCKET) {
socket_type sockfd;
sockfd = socket((type == IPV6_SOCKET) ? AF_INET6 : AF_INET, SOCK_RAW, socktype);
if (sockfd < 0)
if (sockfd < 0) {
throw socket_open_error(make_error_string());
}
const int on = 1;
#ifndef _WIN32
@@ -243,93 +261,109 @@ void PacketSender::open_l3_socket(SocketType type) {
#endif
setsockopt(sockfd, IPPROTO_IP, IP_HDRINCL,(option_ptr)&on,sizeof(on));
_sockets[type] = static_cast<int>(sockfd);
sockets_[type] = static_cast<int>(sockfd);
}
}
void PacketSender::close_socket(SocketType type, const NetworkInterface &iface) {
if(type == ETHER_SOCKET) {
void PacketSender::close_socket(SocketType type, const NetworkInterface& iface) {
if (type == ETHER_SOCKET) {
#if defined(BSD) || defined(__FreeBSD_kernel__)
BSDEtherSockets::iterator it = _ether_socket.find(iface.id());
if(it == _ether_socket.end())
BSDEtherSockets::iterator it = ether_socket_.find(iface.id());
if (it == ether_socket_.end()) {
throw invalid_socket_type();
if(::close(it->second) == -1)
}
if (::close(it->second) == -1) {
throw socket_close_error(make_error_string());
_ether_socket.erase(it);
}
ether_socket_.erase(it);
#elif !defined(_WIN32)
if(_ether_socket == INVALID_RAW_SOCKET)
if (ether_socket_ == INVALID_RAW_SOCKET) {
throw invalid_socket_type();
if(::close(_ether_socket) == -1)
}
if (::close(ether_socket_) == -1) {
throw socket_close_error(make_error_string());
_ether_socket = INVALID_RAW_SOCKET;
}
ether_socket_ = INVALID_RAW_SOCKET;
#endif
}
else {
if(type >= SOCKETS_END || _sockets[type] == INVALID_RAW_SOCKET)
if (type >= SOCKETS_END || sockets_[type] == INVALID_RAW_SOCKET) {
throw invalid_socket_type();
}
#ifndef _WIN32
if(close(_sockets[type]) == -1)
if (close(sockets_[type]) == -1) {
throw socket_close_error(make_error_string());
}
#else
closesocket(_sockets[type]);
closesocket(sockets_[type]);
#endif
_sockets[type] = INVALID_RAW_SOCKET;
sockets_[type] = INVALID_RAW_SOCKET;
}
}
void PacketSender::send(PDU &pdu) {
pdu.send(*this, default_iface);
void PacketSender::send(PDU& pdu) {
pdu.send(*this, default_iface_);
}
void PacketSender::send(PDU &pdu, const NetworkInterface &iface) {
if (pdu.matches_flag(PDU::ETHERNET_II))
void PacketSender::send(PDU& pdu, const NetworkInterface& iface) {
if (pdu.matches_flag(PDU::ETHERNET_II)) {
send<Tins::EthernetII>(pdu, iface);
}
#ifdef HAVE_DOT11
else if (pdu.matches_flag(PDU::DOT11))
else if (pdu.matches_flag(PDU::DOT11)) {
send<Tins::Dot11>(pdu, iface);
else if (pdu.matches_flag(PDU::RADIOTAP))
}
else if (pdu.matches_flag(PDU::RADIOTAP)) {
send<Tins::RadioTap>(pdu, iface);
}
#endif // HAVE_DOT11
else if (pdu.matches_flag(PDU::IEEE802_3))
else if (pdu.matches_flag(PDU::IEEE802_3)) {
send<Tins::IEEE802_3>(pdu, iface);
else send(pdu);
}
else {
send(pdu);
}
}
PDU *PacketSender::send_recv(PDU &pdu) {
return send_recv(pdu, default_iface);
PDU* PacketSender::send_recv(PDU& pdu) {
return send_recv(pdu, default_iface_);
}
PDU *PacketSender::send_recv(PDU &pdu, const NetworkInterface &iface) {
PDU* PacketSender::send_recv(PDU& pdu, const NetworkInterface& iface) {
try {
pdu.send(*this, iface);
}
catch(runtime_error&) {
catch (runtime_error&) {
return 0;
}
return pdu.recv_response(*this, iface);
}
#if !defined(_WIN32) || defined(HAVE_PACKET_SENDER_PCAP_SENDPACKET)
void PacketSender::send_l2(PDU &pdu, struct sockaddr* link_addr,
uint32_t len_addr, const NetworkInterface &iface)
{
void PacketSender::send_l2(PDU& pdu,
struct sockaddr* link_addr,
uint32_t len_addr,
const NetworkInterface& iface) {
PDU::serialization_type buffer = pdu.serialize();
#ifdef HAVE_PACKET_SENDER_PCAP_SENDPACKET
open_l2_socket(iface);
pcap_t* handle = pcap_handles[iface];
if (pcap_sendpacket(handle, (u_char*)&buffer[0], static_cast<int>(buffer.size())) != 0) {
throw runtime_error("Failed to send packet: " + string(pcap_geterr(handle)));
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)));
}
#else // HAVE_PACKET_SENDER_PCAP_SENDPACKET
int sock = get_ether_socket(iface);
if(!buffer.empty()) {
int sock = getether_socket_(iface);
if (!buffer.empty()) {
#if defined(BSD) || defined(__FreeBSD_kernel__)
if(::write(sock, &buffer[0], buffer.size()) == -1)
if (::write(sock, &buffer[0], buffer.size()) == -1) {
#else
if(::sendto(sock, &buffer[0], buffer.size(), 0, link_addr, len_addr) == -1)
if (::sendto(sock, &buffer[0], buffer.size(), 0, link_addr, len_addr) == -1) {
#endif
throw socket_write_error(make_error_string());
}
}
#endif // HAVE_PACKET_SENDER_PCAP_SENDPACKET
}
@@ -337,37 +371,49 @@ void PacketSender::send_l2(PDU &pdu, struct sockaddr* link_addr,
#endif // !_WIN32 || HAVE_PACKET_SENDER_PCAP_SENDPACKET
#ifndef _WIN32
PDU *PacketSender::recv_l2(PDU &pdu, struct sockaddr *link_addr,
uint32_t len_addr, const NetworkInterface &iface)
{
int sock = get_ether_socket(iface);
std::vector<int> sockets(1, sock);
PDU* PacketSender::recv_l2(PDU& pdu,
struct sockaddr* link_addr,
uint32_t len_addr,
const NetworkInterface& iface) {
int sock = getether_socket_(iface);
vector<int> sockets(1, sock);
return recv_match_loop(sockets, pdu, link_addr, len_addr);
}
#endif // _WIN32
PDU *PacketSender::recv_l3(PDU &pdu, struct sockaddr* link_addr, uint32_t len_addr, SocketType type) {
PDU* PacketSender::recv_l3(PDU& pdu,
struct sockaddr* link_addr,
uint32_t len_addr,
SocketType type) {
open_l3_socket(type);
std::vector<int> sockets(1, _sockets[type]);
if(type == IP_TCP_SOCKET || type == IP_UDP_SOCKET) {
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");
#endif
open_l3_socket(ICMP_SOCKET);
sockets.push_back(_sockets[ICMP_SOCKET]);
sockets.push_back(sockets_[ICMP_SOCKET]);
}
return recv_match_loop(sockets, pdu, link_addr, len_addr);
}
void PacketSender::send_l3(PDU &pdu, struct sockaddr* link_addr, uint32_t len_addr, SocketType type) {
void PacketSender::send_l3(PDU& pdu,
struct sockaddr* link_addr,
uint32_t len_addr,
SocketType type) {
open_l3_socket(type);
int sock = _sockets[type];
int sock = sockets_[type];
PDU::serialization_type buffer = pdu.serialize();
if(sendto(sock, (const char*)&buffer[0], static_cast<int>(buffer.size()), 0, link_addr, len_addr) == -1)
const int buf_size = static_cast<int>(buffer.size());
if (sendto(sock, (const char*)&buffer[0], buf_size, 0, link_addr, len_addr) == -1) {
throw socket_write_error(make_error_string());
}
}
PDU *PacketSender::recv_match_loop(const std::vector<int>& sockets, PDU &pdu, struct sockaddr* link_addr, uint32_t addrlen) {
PDU* PacketSender::recv_match_loop(const vector<int>& sockets,
PDU& pdu,
struct sockaddr* link_addr,
uint32_t addrlen) {
#ifdef _WIN32
typedef int socket_len_type;
typedef int recvfrom_ret_type;
@@ -379,9 +425,9 @@ PDU *PacketSender::recv_match_loop(const std::vector<int>& sockets, PDU &pdu, st
struct timeval timeout, end_time;
int read;
#if defined(BSD) || defined(__FreeBSD_kernel__)
// On *BSD, we need to allocate a buffer using the given size.
std::vector<uint8_t> actual_buffer(buffer_size);
uint8_t *buffer = &actual_buffer[0];
// On* BSD, we need to allocate a buffer using the given size.
vector<uint8_t> actual_buffer(buffer_size_);
uint8_t* buffer = &actual_buffer[0];
#else
uint8_t buffer[2048];
const int buffer_size = 2048;
@@ -389,28 +435,29 @@ PDU *PacketSender::recv_match_loop(const std::vector<int>& sockets, PDU &pdu, st
timeout.tv_sec = _timeout;
end_time.tv_sec = static_cast<long>(time(0) + _timeout);
end_time.tv_usec = timeout.tv_usec = _timeout_usec;
while(true) {
end_time.tv_usec = timeout.tv_usec = timeout_usec_;
while (true) {
FD_ZERO(&readfds);
int max_fd = 0;
for(std::vector<int>::const_iterator it = sockets.begin(); it != sockets.end(); ++it) {
for (vector<int>::const_iterator it = sockets.begin(); it != sockets.end(); ++it) {
FD_SET(*it, &readfds);
max_fd = std::max(max_fd, *it);
max_fd = max(max_fd, *it);
}
if((read = select(max_fd + 1, &readfds, 0, 0, &timeout)) == -1)
if ((read = select(max_fd + 1, &readfds, 0, 0, &timeout)) == -1) {
return 0;
if(read > 0) {
for(std::vector<int>::const_iterator it = sockets.begin(); it != sockets.end(); ++it) {
if(FD_ISSET(*it, &readfds)) {
}
if (read > 0) {
for (vector<int>::const_iterator it = sockets.begin(); it != sockets.end(); ++it) {
if (FD_ISSET(*it, &readfds)) {
recvfrom_ret_type size;
#if defined(BSD) || defined(__FreeBSD_kernel__)
size = ::read(*it, buffer, buffer_size);
size = ::read(*it, buffer, buffer_size_);
const uint8_t* ptr = buffer;
// We might see more than one packet
while(ptr < (buffer + size)) {
while (ptr < (buffer + size)) {
const bpf_hdr* bpf_header = reinterpret_cast<const bpf_hdr*>(ptr);
const uint8_t *pkt_start = ptr + bpf_header->bh_hdrlen;
if(pdu.matches_response(pkt_start, bpf_header->bh_caplen)) {
const uint8_t* pkt_start = ptr + bpf_header->bh_hdrlen;
if (pdu.matches_response(pkt_start, bpf_header->bh_caplen)) {
return Internals::pdu_from_flag(pdu.pdu_type(), pkt_start, bpf_header->bh_caplen);
}
ptr += BPF_WORDALIGN(bpf_header->bh_hdrlen + bpf_header->bh_caplen);
@@ -418,7 +465,7 @@ PDU *PacketSender::recv_match_loop(const std::vector<int>& sockets, PDU &pdu, st
#else
socket_len_type length = addrlen;
size = ::recvfrom(*it, (char*)buffer, buffer_size, 0, link_addr, &length);
if(pdu.matches_response(buffer, size)) {
if (pdu.matches_response(buffer, size)) {
return Internals::pdu_from_flag(pdu.pdu_type(), buffer, size);
}
#endif
@@ -431,15 +478,16 @@ PDU *PacketSender::recv_match_loop(const std::vector<int>& sockets, PDU &pdu, st
#else
gettimeofday(&this_time, 0);
#endif // _WIN32
if(timeval_subtract(&diff, &end_time, &this_time))
if (timeval_subtract(&diff, &end_time, &this_time)) {
return 0;
}
timeout.tv_sec = diff.tv_sec;
timeout.tv_usec = diff.tv_usec;
}
return 0;
}
int PacketSender::timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y) {
int PacketSender::timeval_subtract (struct timeval* result, struct timeval* x, struct timeval* y) {
/* Perform the carry for the later subtraction by updating y. */
if (x->tv_usec < y->tv_usec) {
int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
@@ -463,10 +511,13 @@ int PacketSender::timeval_subtract (struct timeval *result, struct timeval *x, s
}
int PacketSender::find_type(SocketType type) {
SocketTypeMap::iterator it = _types.find(type);
if(it == _types.end())
SocketTypeMap::iterator it = types_.find(type);
if (it == types_.end()) {
return -1;
else
}
else {
return it->second;
}
}
}
} // Tins