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

Documented PacketSender. Added Utils::resolve_hwaddr, it's not working yet.

This commit is contained in:
Matias Fontanini
2011-08-16 13:48:49 -03:00
parent 7bf8bd6ee7
commit 0989027f3d
4 changed files with 114 additions and 21 deletions

View File

@@ -46,6 +46,8 @@ namespace Tins {
public:
static const uint32_t DEFAULT_TIMEOUT;
/** \brief Flags to indicate the socket type.
*/
enum SocketType {
ETHER_SOCKET,
IP_SOCKET,
@@ -56,30 +58,105 @@ namespace Tins {
/**
* \brief Constructor for PacketSender objects.
*
* \param recv_timeout The timeout which will be used when receiving responses.
*/
PacketSender(uint32_t recv_timeout = DEFAULT_TIMEOUT);
~PacketSender();
/**
* \brief
*
/** \brief Opens a layer y socket.
*
* \return Returns true if the socket was open successfully, false otherwise.
*/
bool open_l2_socket();
/** \brief Opens a layer 3 socket, using the corresponding protocol
* for the given flag.
*
* \param type The type of socket which will be used to pick the protocol flag
* for this socket.
* \return Returns true if the socket was open successfully, false otherwise.
*/
bool open_l3_socket(SocketType type);
/** \brief Closes the socket associated with the given flag.
*
* \param flag
* \return Returns true if the socket was closed successfully, false otherwise.
*/
bool close_socket(uint32_t flag);
/** \brief Sends a PDU.
*
* This method is used to send PDUs. It opens the required socket(if it's not open yet).
*
* \param pdu The PDU to send.
* \return Returns true if the PDU is sent successfully, false otherwise.
*/
bool send(PDU* pdu);
/** \brief Sends a PDU and waits for its response.
*
* This method is used to send PDUs and receive their response.
* It opens the required socket(if it's not open yet). This can be used
* to expect responses for ICMP, ARP, and such packets that are normally
* answered by the host that receives the packet.
*
* \param pdu The PDU to send.
* \return Returns the response PDU, 0 if not response was received.
*/
PDU *send_recv(PDU *pdu);
PDU *recv_l2(PDU *pdu, struct sockaddr *link_addr, uint32_t len_link_addr);
/** \brief Receives a layer 2 PDU response to a previously sent PDU.
*
* This PacketSender will receive data from a raw socket, open using the corresponding flag,
* according to the given type of protocol, until a match for the given PDU is received.
*
* \param pdu The PDU which will try to match the responses.
* \param link_addr The sockaddr struct which will be used to receive the PDU.
* \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);
bool send_l2(PDU *pdu, struct sockaddr* link_addr, uint32_t len_link_addr);
/** \brief Sends a level 2 PDU.
*
* This method sends a layer 2 PDU, using a raw socket, open using the corresponding flag,
* according to the given type of protocol.
*
* \param pdu The PDU to send.
* \param link_addr The sockaddr struct which will be used to send the PDU.
* \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);
PDU *recv_l3(PDU *pdu, struct sockaddr *link_addr, uint32_t len_link_addr, SocketType type);
/** \brief Receives a layer 3 PDU response to a previously sent PDU.
*
* This PacketSender will receive data from a raw socket, open using the corresponding flag,
* according to the given type of protocol, until a match for the given PDU is received.
*
* \param pdu The PDU which will try to match the responses.
* \param link_addr The sockaddr struct which will be used to receive the PDU.
* \param len_addr The sockaddr struct length.
* \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);
bool send_l3(PDU *pdu, struct sockaddr *link_addr, uint32_t len_link_addr, SocketType type);
/** \brief Sends a level 3 PDU.
*
* This method sends a layer 3 PDU, using a raw socket, open using the corresponding flag,
* according to the given type of protocol.
*
* \param pdu The PDU to send.
* \param link_addr The sockaddr struct which will be used to send the PDU.
* \param len_addr The sockaddr struct length.
* \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);
private:
static const int INVALID_RAW_SOCKET;

View File

@@ -77,6 +77,16 @@ namespace Tins {
*/
uint32_t resolve_ip(const std::string &to_resolve);
/** \brief Resolves the hardware address for a given ip.
*
* \param ip The ip to resolve, in integer format.
* \param buffer The buffer in which the host's hardware address will be stored.
* \param sender The sender to use to send and receive the ARP requests.(optional)
* \return Returns true if the hardware address was resolved successfully,
* false otherwise.
*/
bool resolve_hwaddr(uint32_t ip, uint8_t *buffer, PacketSender *sender = 0);
/** \brief List all network interfaces.
*
* Returns a set of strings, each of them representing the name

View File

@@ -33,10 +33,9 @@
#include <string.h>
#include <ctime>
#include "packetsender.h"
#include "utils.h" //borrar
const int Tins::PacketSender::INVALID_RAW_SOCKET = -10;
const int Tins::PacketSender::INVALID_RAW_SOCKET = -1;
const uint32_t Tins::PacketSender::DEFAULT_TIMEOUT = 2;
Tins::PacketSender::PacketSender(uint32_t recv_timeout) : _sockets(SOCKETS_END, INVALID_RAW_SOCKET), _timeout(recv_timeout) {
@@ -44,17 +43,20 @@ Tins::PacketSender::PacketSender(uint32_t recv_timeout) : _sockets(SOCKETS_END,
_types[ICMP_SOCKET] = IPPROTO_ICMP;
}
bool Tins::PacketSender::open_l2_socket() {
Tins::PacketSender::~PacketSender() {
for(unsigned i(0); i < _sockets.size(); ++i) {
if(_sockets[i] != INVALID_RAW_SOCKET)
::close(sockets[i]);
}
}
bool Tins::PacketSender::open_l2_socket() {
if (_sockets[ETHER_SOCKET] != INVALID_RAW_SOCKET)
return true;
int sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if (sock == -1)
return false;
_sockets[ETHER_SOCKET] = sock;
return true;
}
@@ -94,7 +96,7 @@ Tins::PDU *Tins::PacketSender::send_recv(PDU *pdu) {
return pdu->recv_response(this);
}
bool Tins::PacketSender::send_l2(PDU *pdu, struct sockaddr* link_addr, uint32_t len_link_addr) {
bool Tins::PacketSender::send_l2(PDU *pdu, struct sockaddr* link_addr, uint32_t len_addr) {
if(!open_l2_socket())
return false;
@@ -102,25 +104,25 @@ bool Tins::PacketSender::send_l2(PDU *pdu, struct sockaddr* link_addr, uint32_t
uint32_t sz;
int sock = _sockets[ETHER_SOCKET];
uint8_t *buffer = pdu->serialize(sz);
bool ret_val = (sendto(sock, buffer, sz, 0, link_addr, len_link_addr) != -1);
bool ret_val = (sendto(sock, buffer, sz, 0, link_addr, len_addr) != -1);
delete[] buffer;
return ret_val;
}
Tins::PDU *Tins::PacketSender::recv_l2(PDU *pdu, struct sockaddr *link_addr, uint32_t len_link_addr) {
Tins::PDU *Tins::PacketSender::recv_l2(PDU *pdu, struct sockaddr *link_addr, uint32_t len_addr) {
if(!open_l2_socket())
return 0;
return recv_match_loop(_sockets[ETHER_SOCKET], pdu, link_addr, len_link_addr);
return recv_match_loop(_sockets[ETHER_SOCKET], pdu, link_addr, len_addr);
}
Tins::PDU *Tins::PacketSender::recv_l3(PDU *pdu, struct sockaddr* link_addr, uint32_t len_link_addr, SocketType type) {
Tins::PDU *Tins::PacketSender::recv_l3(PDU *pdu, struct sockaddr* link_addr, uint32_t len_addr, SocketType type) {
if(!open_l3_socket(type))
return 0;
return recv_match_loop(_sockets[type], pdu, link_addr, len_link_addr);
return recv_match_loop(_sockets[type], pdu, link_addr, len_addr);
}
bool Tins::PacketSender::send_l3(PDU *pdu, struct sockaddr* link_addr, uint32_t len_link_addr, SocketType type) {
bool Tins::PacketSender::send_l3(PDU *pdu, struct sockaddr* link_addr, uint32_t len_addr, SocketType type) {
bool ret_val = true;
if(!open_l3_socket(type))
ret_val = false;
@@ -128,7 +130,7 @@ bool Tins::PacketSender::send_l3(PDU *pdu, struct sockaddr* link_addr, uint32_t
uint32_t sz;
int sock = _sockets[type];
uint8_t *buffer = pdu->serialize(sz);
ret_val = (sendto(sock, buffer, sz, 0, link_addr, len_link_addr) != -1);
ret_val = (sendto(sock, buffer, sz, 0, link_addr, len_addr) != -1);
delete[] buffer;
}
return ret_val;

View File

@@ -155,6 +155,10 @@ uint32_t Tins::Utils::resolve_ip(const string &to_resolve) {
return ((struct in_addr**)data->h_addr_list)[0]->s_addr;
}
bool Tins::Utils::resolve_hwaddr(uint32_t ip, uint8_t *buffer, PacketSender *sender) {
}
set<string> Tins::Utils::network_interfaces() {
InterfaceCollector collector;
generic_iface_loop(collector);