mirror of
https://github.com/mfontanini/libtins
synced 2026-01-29 13:04:28 +01:00
Updated IP, PDU and PacketSender so as to send forged ip packets successfully
This commit is contained in:
15
include/ip.h
15
include/ip.h
@@ -9,12 +9,12 @@
|
||||
#include "pdu.h"
|
||||
|
||||
namespace Tins {
|
||||
|
||||
|
||||
class IP : public PDU {
|
||||
public:
|
||||
IP(const std::string &ip_dst = "", const std::string &ip_src = "");
|
||||
IP(uint32_t ip_dst = 0, uint32_t ip_src = 0);
|
||||
|
||||
|
||||
inline uint8_t tos() const { return _ip.tos; }
|
||||
inline uint16_t tot_len() const { return _ip.tot_len; }
|
||||
inline uint16_t id() const { return _ip.id; }
|
||||
@@ -24,7 +24,7 @@ namespace Tins {
|
||||
inline uint16_t check() const { return _ip.check; }
|
||||
inline uint32_t source_address() const { return _ip.saddr; }
|
||||
inline uint32_t dest_address() const { return _ip.daddr; }
|
||||
|
||||
|
||||
void tos(uint8_t new_tos);
|
||||
void tot_len(uint16_t new_tot_len);
|
||||
void id(uint16_t new_id);
|
||||
@@ -36,9 +36,10 @@ namespace Tins {
|
||||
void source_address(uint32_t ip);
|
||||
void dest_address(const std::string &ip);
|
||||
void dest_address(uint32_t ip);
|
||||
|
||||
|
||||
/* Virtual methods */
|
||||
uint32_t header_size() const;
|
||||
bool send(PacketSender* sender);
|
||||
private:
|
||||
struct iphdr {
|
||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
@@ -48,7 +49,7 @@ namespace Tins {
|
||||
unsigned int version:4;
|
||||
unsigned int ihl:4;
|
||||
#else
|
||||
# error "Endian is not LE nor BE..."
|
||||
# error "Endian is not LE nor BE..."
|
||||
#endif
|
||||
uint8_t tos;
|
||||
uint16_t tot_len;
|
||||
@@ -61,10 +62,10 @@ namespace Tins {
|
||||
uint32_t daddr;
|
||||
/*The options start here. */
|
||||
} __attribute__((packed));
|
||||
|
||||
|
||||
void init_ip_fields();
|
||||
void write_serialization(uint8_t *buffer, uint32_t total_sz);
|
||||
|
||||
|
||||
iphdr _ip;
|
||||
};
|
||||
};
|
||||
|
||||
@@ -7,21 +7,25 @@
|
||||
#include "pdu.h"
|
||||
|
||||
namespace Tins {
|
||||
class PDU;
|
||||
|
||||
class PacketSender {
|
||||
public:
|
||||
/* Opens a socket, using flag as protocol family.
|
||||
/* Opens a socket, using flag as protocol family.
|
||||
* Return true if it was possible to open it(or it was already open),
|
||||
* false otherwise. */
|
||||
bool open_socket(uint32_t flag);
|
||||
|
||||
bool open_l3_socket();
|
||||
|
||||
bool close_socket(uint32_t flag);
|
||||
|
||||
bool send(PDU *pdu);
|
||||
|
||||
bool send(PDU* pdu);
|
||||
|
||||
bool send_l3(PDU *pdu, const struct sockaddr* link_addr, uint32_t len_link_addr);
|
||||
private:
|
||||
typedef std::map<uint32_t, int> SocketMap;
|
||||
bool write(int sock, uint8_t *buffer, uint32_t size);
|
||||
|
||||
|
||||
static const uint32_t IP_SOCKET;
|
||||
|
||||
SocketMap _sockets;
|
||||
};
|
||||
};
|
||||
|
||||
@@ -3,14 +3,17 @@
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
#include "packetsender.h"
|
||||
|
||||
namespace Tins {
|
||||
|
||||
|
||||
class PacketSender;
|
||||
|
||||
class PDU {
|
||||
public:
|
||||
PDU(uint32_t flag, PDU *next_pdu = 0);
|
||||
virtual ~PDU();
|
||||
|
||||
|
||||
/* This PDU's header size only. */
|
||||
virtual uint32_t header_size() const = 0;
|
||||
/* This PDU's trailer size only. Defaults to 0. */
|
||||
@@ -19,19 +22,22 @@ namespace Tins {
|
||||
inline uint32_t size() const;
|
||||
inline uint32_t flag() const { return _flag; }
|
||||
inline const PDU *inner_pdu() const { return _inner_pdu; }
|
||||
|
||||
|
||||
void flag(uint32_t new_flag);
|
||||
/* When setting a new inner_pdu, the instance takes
|
||||
* ownership of the object, therefore deleting it when
|
||||
* it's no longer required. */
|
||||
void inner_pdu(PDU *next_pdu);
|
||||
|
||||
|
||||
/* Serializes the whole chain of PDU's, including this one. */
|
||||
uint8_t *serialize(uint32_t &sz);
|
||||
|
||||
|
||||
/* */
|
||||
virtual bool send(PacketSender* sender) {return false;}
|
||||
protected:
|
||||
/* Serialize this PDU storing the result in buffer. */
|
||||
void serialize(uint8_t *buffer, uint32_t total_sz);
|
||||
protected:
|
||||
|
||||
/* Each PDU's own implementation of serialization. */
|
||||
virtual void write_serialization(uint8_t *buffer, uint32_t total_sz) = 0;
|
||||
private:
|
||||
|
||||
@@ -10,6 +10,14 @@ namespace Tins {
|
||||
namespace Utils {
|
||||
uint32_t ip_to_int(const std::string &ip);
|
||||
std::string ip_to_string(uint32_t ip);
|
||||
|
||||
inline uint32_t ntohl(uint32_t address) {
|
||||
return (((address & 0xff000000) >> 24) |
|
||||
((address & 0x00ff0000) >> 8) |
|
||||
((address & 0x0000ff00) << 8) |
|
||||
((address & 0x000000ff) << 24));
|
||||
}
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user