1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-30 21:44:26 +01:00

Modified some PacketSender and BaseSniffer functions to take references instead of pointers.

This commit is contained in:
Matias Fontanini
2012-09-11 08:51:25 -03:00
parent b977697367
commit 7cbd32361a
21 changed files with 93 additions and 110 deletions

View File

@@ -377,7 +377,7 @@ namespace Tins {
/**
* \sa PDU::send()
*/
bool send(PacketSender* sender);
bool send(PacketSender &sender);
/**
* \brief Adds a new option to this Dot11 PDU.

View File

@@ -147,7 +147,7 @@ namespace Tins {
/**
* \sa PDU::send()
*/
bool send(PacketSender* sender);
bool send(PacketSender &sender);
/** \brief Check wether ptr points to a valid response for this PDU.
*
@@ -162,7 +162,7 @@ namespace Tins {
* \sa PDU::recv_response
* \param sender The packet sender which will receive the packet.
*/
PDU *recv_response(PacketSender *sender);
PDU *recv_response(PacketSender &sender);
/**
* \brief Getter for the PDU's type.

View File

@@ -145,7 +145,7 @@ namespace Tins {
/**
* \sa PDU::send()
*/
bool send(PacketSender* sender);
bool send(PacketSender &sender);
/** \brief Check wether ptr points to a valid response for this PDU.
*
@@ -160,7 +160,7 @@ namespace Tins {
* \sa PDU::recv_response
* \param sender The packet sender which will receive the packet.
*/
PDU *recv_response(PacketSender *sender);
PDU *recv_response(PacketSender &sender);
/**
* \brief Getter for the PDU's type.

View File

@@ -510,7 +510,7 @@ namespace Tins {
/**
* \sa PDU::send()
*/
bool send(PacketSender* sender);
bool send(PacketSender &sender);
/**
* \brief Check wether ptr points to a valid response for this PDU.
@@ -527,7 +527,7 @@ namespace Tins {
* \sa PDU::recv_response
* \param sender The packet sender which will receive the packet.
*/
PDU *recv_response(PacketSender *sender);
PDU *recv_response(PacketSender &sender);
/**
* \brief Getter for the PDU's type.

View File

@@ -96,7 +96,7 @@ namespace Tins {
* \param pdu The PDU to send.
* \return Returns true if the PDU is sent successfully, false otherwise.
*/
bool send(PDU* pdu);
bool send(PDU &pdu);
/** \brief Sends a PDU and waits for its response.
*
@@ -108,7 +108,7 @@ namespace Tins {
* \param pdu The PDU to send.
* \return Returns the response PDU, 0 if not response was received.
*/
PDU *send_recv(PDU *pdu);
PDU *send_recv(PDU &pdu);
/** \brief Receives a layer 2 PDU response to a previously sent PDU.
*
@@ -120,7 +120,7 @@ namespace Tins {
* \param len_addr The sockaddr struct length.
* \return Returns the response PDU. If no response is received, then 0 is returned.
*/
PDU *recv_l2(PDU *pdu, struct sockaddr *link_addr, uint32_t len_addr);
PDU *recv_l2(PDU &pdu, struct sockaddr *link_addr, uint32_t len_addr);
/** \brief Sends a level 2 PDU.
*
@@ -132,7 +132,7 @@ namespace Tins {
* \param len_addr The sockaddr struct length.
* \return Returns true if the PDU was successfully sent, false otherwise.
*/
bool send_l2(PDU *pdu, struct sockaddr* link_addr, uint32_t len_addr);
bool send_l2(PDU &pdu, struct sockaddr* link_addr, uint32_t len_addr);
/** \brief Receives a layer 3 PDU response to a previously sent PDU.
*
@@ -145,7 +145,7 @@ namespace Tins {
* \param type The socket protocol type.
* \return Returns the response PDU. If no response is received, then 0 is returned.
*/
PDU *recv_l3(PDU *pdu, struct sockaddr *link_addr, uint32_t len_addr, SocketType type);
PDU *recv_l3(PDU &pdu, struct sockaddr *link_addr, uint32_t len_addr, SocketType type);
/** \brief Sends a level 3 PDU.
*
@@ -158,7 +158,7 @@ namespace Tins {
* \param type The socket protocol type.
* \return Returns true if the PDU was successfully sent, false otherwise.
*/
bool send_l3(PDU *pdu, struct sockaddr *link_addr, uint32_t len_addr, SocketType type);
bool send_l3(PDU &pdu, struct sockaddr *link_addr, uint32_t len_addr, SocketType type);
private:
static const int INVALID_RAW_SOCKET;
@@ -167,7 +167,7 @@ namespace Tins {
int find_type(SocketType type);
int timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y);
PDU *recv_match_loop(int sock, PDU *pdu, struct sockaddr* link_addr, uint32_t addrlen);
PDU *recv_match_loop(int sock, PDU &pdu, struct sockaddr* link_addr, uint32_t addrlen);
std::vector<int> _sockets;
SocketTypeMap _types;

View File

@@ -228,14 +228,14 @@ namespace Tins {
* those methods.
* \param sender The PacketSender which will send the packet.
*/
virtual bool send(PacketSender *sender);
virtual bool send(PacketSender &sender);
/** \brief Receives a matching response for this packet.
*
* This method should act as a proxy for PacketSender::recv_lX methods.
* \param sender The packet sender which will receive the packet.
*/
virtual PDU *recv_response(PacketSender *sender);
virtual PDU *recv_response(PacketSender &sender);
/** \brief Check wether ptr points to a valid response for this PDU.
*

View File

@@ -117,7 +117,7 @@ namespace Tins {
/**
* \sa PDU::send()
*/
bool send(PacketSender* sender);
bool send(PacketSender &sender);
/**
* \brief Setter for the version field.

View File

@@ -69,12 +69,12 @@ namespace Tins {
* The callback object must implement an operator with the
* following(or compatible) signature:
*
* bool operator()(PDU*);
* bool operator()(PDU&);
*
* This operator will be called using the sniffed packets
* as arguments. The callback object <b>must not</b> delete the
* PDU parameter. You can modify it as you wish, though. Calling
* PDU methods like PDU::release_inner_pdu is perfectly valid.
* as arguments. You can modify the PDU argument as you wish.
* Calling PDU methods like PDU::release_inner_pdu is perfectly
* valid.
*
* Note that the Functor object will be copied using its copy
* constructor, so that object should be some kind of proxy to
@@ -128,6 +128,9 @@ namespace Tins {
BaseSniffer(const BaseSniffer&);
BaseSniffer &operator=(const BaseSniffer&);
template<class ConcretePDU, class Functor>
static bool call_functor(LoopData<Functor> *data, const u_char *packet, size_t len);
bool compile_set_filter(const std::string &filter, bpf_program &prog);
template<class Functor>
@@ -185,16 +188,26 @@ namespace Tins {
pcap_loop(handle, max_packets, &BaseSniffer::callback_handler<Functor>, (u_char*)&data);
}
template<class ConcretePDU, class Functor>
bool Tins::BaseSniffer::call_functor(LoopData<Functor> *data, const u_char *packet, size_t len) {
ConcretePDU some_pdu((const uint8_t*)packet, len);
return data->c_handler(some_pdu);
}
template<class Functor>
void Tins::BaseSniffer::callback_handler(u_char *args, const struct pcap_pkthdr *header, const u_char *packet) {
try {
std::auto_ptr<PDU> pdu;
LoopData<Functor> *data = reinterpret_cast<LoopData<Functor>*>(args);
if(data->wired)
pdu.reset(new Tins::EthernetII((const uint8_t*)packet, header->caplen));
bool ret_val(false);
/*if(data->wired)
ret_val = data->c_handler(Tins::EthernetII((const uint8_t*)packet, header->caplen));
else
pdu.reset(new Tins::RadioTap((const uint8_t*)packet, header->caplen));
bool ret_val = data->c_handler(pdu.get());
pdu.reset(new Tins::RadioTap((const uint8_t*)packet, header->caplen));*/
if(data->wired)
ret_val = call_functor<Tins::EthernetII>(data, packet, header->caplen);
else
ret_val = call_functor<Tins::RadioTap>(data, packet, header->caplen);
if(!ret_val)
pcap_breakloop(data->handle);
}
@@ -207,12 +220,12 @@ namespace Tins {
class HandlerProxy {
public:
typedef T* ptr_type;
typedef bool (T::*fun_type)(PDU*) ;
typedef bool (T::*fun_type)(PDU&) ;
HandlerProxy(ptr_type ptr, fun_type function)
: object(ptr), fun(function) {}
bool operator()(PDU *pdu) {
bool operator()(PDU &pdu) {
return (object->*fun)(pdu);
}
private:

View File

@@ -217,7 +217,7 @@ private:
template<typename DataFunctor, typename EndFunctor>
struct proxy_caller {
bool callback(PDU *pdu) {
bool callback(PDU &pdu) {
return stream->callback(pdu, data_fun, end_fun);
}
@@ -227,7 +227,7 @@ private:
};
template<typename DataFunctor, typename EndFunctor>
bool callback(PDU *pdu, const DataFunctor &fun, const EndFunctor &end_fun);
bool callback(PDU &pdu, const DataFunctor &fun, const EndFunctor &end_fun);
sessions_type sessions;
uint64_t last_identifier;
@@ -241,9 +241,9 @@ void TCPStreamFollower::follow_streams(BaseSniffer &sniffer, DataFunctor data_fu
}
template<typename DataFunctor, typename EndFunctor>
bool TCPStreamFollower::callback(PDU *pdu, const DataFunctor &data_fun, const EndFunctor &end_fun) {
IP *ip = pdu->find_pdu<IP>();
TCP *tcp = pdu->find_pdu<TCP>();
bool TCPStreamFollower::callback(PDU &pdu, const DataFunctor &data_fun, const EndFunctor &end_fun) {
IP *ip = pdu.find_pdu<IP>();
TCP *tcp = pdu.find_pdu<TCP>();
if(ip && tcp) {
TCPStream::StreamInfo info = {
ip->src_addr(), ip->dst_addr(),

View File

@@ -79,23 +79,6 @@ namespace Tins {
* \param to_resolve The domain name/ip address to resolve.
*/
IPv4Address resolve_ip(const std::string &to_resolve);
/**
* \brief Pings an ip address.
*
* This function pings an IP address and returns the ICMP response.
* If no response is received, 0 is returned
*
* \param ip The IP address to ping.
* \param sender The PacketSender that will send the ping request.
* \param ip_src The source IP address that will be used in the packet.
* If 0, or no parameter is provided, then that IP address is looked
* up using Utils::interface_ip.
*
* \return PDU * containing either 0 if no response was received,
* or the ICMP response otherwise.
*/
PDU *ping_address(IPv4Address ip, PacketSender *sender, IPv4Address ip_src = 0);
/** \brief Resolves the hardware address for a given ip.
*
@@ -107,7 +90,7 @@ namespace Tins {
* false otherwise.
*/
bool resolve_hwaddr(const NetworkInterface &iface, IPv4Address ip,
HWAddress<6> *address, PacketSender *sender);
HWAddress<6> *address, PacketSender &sender);
/** \brief List all network interfaces.
*