mirror of
https://github.com/mfontanini/libtins
synced 2026-01-29 13:04:28 +01:00
Added IPv4Address class.
This commit is contained in:
@@ -26,6 +26,7 @@
|
||||
|
||||
#include <string>
|
||||
#include "pdu.h"
|
||||
#include "ipaddress.h"
|
||||
#include "utils.h"
|
||||
|
||||
namespace Tins {
|
||||
@@ -50,7 +51,8 @@ namespace Tins {
|
||||
* ARP requests and replies can be constructed easily using
|
||||
* ARP::make_arp_request/reply static functions.
|
||||
*/
|
||||
ARP(uint32_t target_ip = 0, uint32_t sender_ip = 0, const uint8_t *target_hw = 0, const uint8_t *sender_hw = 0);
|
||||
ARP(IPv4Address target_ip = 0, IPv4Address sender_ip = 0,
|
||||
const uint8_t *target_hw = 0, const uint8_t *sender_hw = 0);
|
||||
|
||||
/**
|
||||
* \brief Constructor which creates an ARP object from a buffer and adds all identifiable
|
||||
@@ -80,7 +82,7 @@ namespace Tins {
|
||||
*
|
||||
* \return Returns the sender's IP address in an uint32_t.
|
||||
*/
|
||||
inline const uint32_t sender_ip_addr() { return Utils::net_to_host_l(this->_arp.ar_sip); }
|
||||
inline const IPv4Address sender_ip_addr() { return Utils::net_to_host_l(this->_arp.ar_sip); }
|
||||
|
||||
/**
|
||||
* \brief Getter for the target's hardware address.
|
||||
@@ -94,7 +96,7 @@ namespace Tins {
|
||||
*
|
||||
* \return Returns the target's IP address in an uint32_t.
|
||||
*/
|
||||
inline const uint32_t target_ip_addr() { return Utils::net_to_host_l(this->_arp.ar_tip); }
|
||||
inline const IPv4Address target_ip_addr() { return Utils::net_to_host_l(this->_arp.ar_tip); }
|
||||
|
||||
/**
|
||||
* \brief Getter for the hardware address format.
|
||||
@@ -148,16 +150,9 @@ namespace Tins {
|
||||
/**
|
||||
* \brief Setter for the sender's IP address.
|
||||
*
|
||||
* \param new_snd_ip_addr uint32_t containing the new sender's IP address.
|
||||
* \param new_snd_ip_addr IPv4Address containing the new sender's IP address.
|
||||
*/
|
||||
void sender_ip_addr(uint32_t new_snd_ip_addr);
|
||||
|
||||
/**
|
||||
* \brief Setter for the sender's IP address.
|
||||
*
|
||||
* \param new_snd_ip_addr string containing the new sender's IP address or hostname.
|
||||
*/
|
||||
void sender_ip_addr(const std::string& new_snd_ip_addr);
|
||||
void sender_ip_addr(IPv4Address new_snd_ip_addr);
|
||||
|
||||
/**
|
||||
* \brief Setter for the target's hardware address.
|
||||
@@ -169,16 +164,9 @@ namespace Tins {
|
||||
/**
|
||||
* \brief Setter for the target's IP address.
|
||||
*
|
||||
* \param new_tgt_ip_addr uint32_t containing the new target's IP address.
|
||||
* \param new_tgt_ip_addr IPv4Address containing the new target's IP address.
|
||||
*/
|
||||
void target_ip_addr(uint32_t new_tgt_ip_addr);
|
||||
|
||||
/**
|
||||
* \brief Setter for the target's IP address.
|
||||
*
|
||||
* \param new_tgt_ip_addr string containing the new target's IP address or hostname.
|
||||
*/
|
||||
void target_ip_addr(const std::string& new_tgt_ip_addr);
|
||||
void target_ip_addr(IPv4Address new_tgt_ip_addr);
|
||||
|
||||
/**
|
||||
* \brief Setter for the hardware address format.
|
||||
@@ -221,20 +209,6 @@ namespace Tins {
|
||||
*/
|
||||
PDUType pdu_type() const { return PDU::ARP; }
|
||||
|
||||
/**
|
||||
* \brief Creates an ARP Request within a Layer 2 PDU using strings for target and sender.
|
||||
*
|
||||
* Creates an ARP Request PDU and embeds it within a Layer 2 PDU ready to be
|
||||
* sent. The target and sender's protocol address are given using strings.
|
||||
*
|
||||
* \param iface string with the interface from where to send the ARP.
|
||||
* \param target string with the target's IP or hostname.
|
||||
* \param sender string with the sender's IP or hostname.
|
||||
* \param hw_snd uint8_t array of 6 bytes containing the sender's hardware address.
|
||||
* \return Returns a PDU* to the new Layer 2 PDU containing the ARP Request.
|
||||
*/
|
||||
static PDU* make_arp_request(const std::string& iface, const std::string &target, const std::string &sender, const uint8_t* hw_snd = 0);
|
||||
|
||||
/**
|
||||
* \brief Creates an ARP Request within a Layer 2 PDU using uint32_t for target and sender.
|
||||
*
|
||||
@@ -242,27 +216,13 @@ namespace Tins {
|
||||
* sent. The target and sender's protocol address are given using uint32_t.
|
||||
*
|
||||
* \param iface string with the interface from where to send the ARP.
|
||||
* \param target uint32_t with the target's IP.
|
||||
* \param sender uint32_t with the sender's IP.
|
||||
* \param target IPv4Address with the target's IP.
|
||||
* \param sender IPv4Address with the sender's IP.
|
||||
* \param hw_snd uint8_t array of 6 bytes containing the sender's hardware address.
|
||||
* \return Returns a PDU* to the new Layer 2 PDU containing the ARP Request.
|
||||
*/
|
||||
static PDU* make_arp_request(const std::string& iface, uint32_t target, uint32_t sender, const uint8_t* hw_snd = 0);
|
||||
|
||||
/**
|
||||
* \brief Creates an ARP Reply within a Layer 2 PDU using strings for target and sender.
|
||||
*
|
||||
* Creates an ARP Reply PDU and embeds it within a Layer 2 PDU ready to be
|
||||
* sent. The target and sender's protocol address are given using strings.
|
||||
*
|
||||
* \param iface string with the interface from where to send the ARP.
|
||||
* \param target string with the target's IP or hostname.
|
||||
* \param sender string with the sender's IP or hostname.
|
||||
* \param hw_tgt uint8_t array of 6 bytes containing the target's hardware address.
|
||||
* \param hw_snd uint8_t array of 6 bytes containing the sender's hardware address.
|
||||
* \return Returns a PDU* to the new Layer 2 PDU containing the ARP Replay.
|
||||
*/
|
||||
static PDU* make_arp_reply(const std::string& iface, const std::string &target, const std::string &sender, const uint8_t* hw_tgt, const uint8_t* hw_snd);
|
||||
static PDU* make_arp_request(const std::string& iface, IPv4Address target,
|
||||
IPv4Address sender, const uint8_t* hw_snd = 0);
|
||||
|
||||
/**
|
||||
* \brief Creates an ARP Reply within a Layer 2 PDU using uint32_t for target and sender.
|
||||
@@ -271,13 +231,14 @@ namespace Tins {
|
||||
* sent. The target and sender's protocol address are given using uint32_t.
|
||||
*
|
||||
* \param iface string with the interface from where to send the ARP.
|
||||
* \param target uint32_t with the target's IP.
|
||||
* \param sender uint32_t with the sender's IP.
|
||||
* \param target IPv4Address with the target's IP.
|
||||
* \param sender IPv4Address with the sender's IP.
|
||||
* \param hw_tgt uint8_t array of 6 bytes containing the target's hardware address.
|
||||
* \param hw_snd uint8_t array of 6 bytes containing the sender's hardware address.
|
||||
* \return Returns a PDU* to the new Layer 2 PDU containing the ARP Replay.
|
||||
*/
|
||||
static PDU* make_arp_reply(const std::string& iface, uint32_t target, uint32_t sender, const uint8_t* hw_tgt, const uint8_t* hw_snd);
|
||||
static PDU* make_arp_reply(const std::string& iface, IPv4Address target,
|
||||
IPv4Address sender, const uint8_t* hw_tgt, const uint8_t* hw_snd);
|
||||
|
||||
/**
|
||||
* \brief Converts the current ARP PDU to an ARP Request.
|
||||
|
||||
44
include/ip.h
44
include/ip.h
@@ -19,8 +19,8 @@
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef __IP_H
|
||||
#define __IP_H
|
||||
#ifndef TINS_IP_H
|
||||
#define TINS_IP_H
|
||||
|
||||
#ifndef WIN32
|
||||
#include <endian.h>
|
||||
@@ -29,6 +29,7 @@
|
||||
#include <utility>
|
||||
#include <list>
|
||||
#include "pdu.h"
|
||||
#include "ipaddress.h"
|
||||
#include "utils.h"
|
||||
|
||||
namespace Tins {
|
||||
@@ -123,18 +124,6 @@ namespace Tins {
|
||||
*/
|
||||
IP();
|
||||
|
||||
/**
|
||||
* \brief Constructor for building the IP PDU taking strings as ip addresses.
|
||||
*
|
||||
* Constructor that builds an IP using strings as addresses. They
|
||||
* can be hostnames or IPs.
|
||||
*
|
||||
* \param ip_dst string containing the destination hostname(optional).
|
||||
* \param ip_src string containing the source hostname(optional).
|
||||
* \param child pointer to a PDU which will be set as the inner_pdu for the packet being constructed(optional).
|
||||
*/
|
||||
IP(const std::string &ip_dst, const std::string &ip_src = "", PDU *child = 0);
|
||||
|
||||
/**
|
||||
* \brief Constructor for building the IP PDU taking integer as ip addresses.
|
||||
*
|
||||
@@ -145,7 +134,7 @@ namespace Tins {
|
||||
* \param ip_src The source ip address(optional).
|
||||
* \param child pointer to a PDU which will be set as the inner_pdu for the packet being constructed(optional).
|
||||
*/
|
||||
IP(uint32_t ip_dst, uint32_t ip_src = 0, PDU *child = 0);
|
||||
IP(IPv4Address ip_dst, IPv4Address ip_src = 0, PDU *child = 0);
|
||||
|
||||
/**
|
||||
* \brief Copy constructor.
|
||||
@@ -236,12 +225,12 @@ namespace Tins {
|
||||
*
|
||||
* \return The source address for this IP PDU.
|
||||
*/
|
||||
inline uint32_t src_addr() const { return Utils::net_to_host_l(_ip.saddr); }
|
||||
inline IPv4Address src_addr() const { return Utils::net_to_host_l(_ip.saddr); }
|
||||
|
||||
/** \brief Getter for the destination address field.
|
||||
* \return The destination address for this IP PDU.
|
||||
*/
|
||||
inline uint32_t dst_addr() const { return Utils::net_to_host_l(_ip.daddr); }
|
||||
inline IPv4Address dst_addr() const { return Utils::net_to_host_l(_ip.daddr); }
|
||||
|
||||
/** \brief Getter for the version field.
|
||||
* \return The version for this IP PDU.
|
||||
@@ -306,33 +295,19 @@ namespace Tins {
|
||||
*/
|
||||
void check(uint16_t new_check);
|
||||
|
||||
/**
|
||||
* \brief Setter for the source address field.
|
||||
*
|
||||
* \param ip The ip address in dotted string notation.
|
||||
*/
|
||||
void src_addr(const std::string &ip);
|
||||
|
||||
/**
|
||||
* \brief Setter for the source address field.
|
||||
*
|
||||
* \param ip The ip address in integer notation.
|
||||
*/
|
||||
void src_addr(uint32_t ip);
|
||||
|
||||
/**
|
||||
* \brief Setter for the destination address field.
|
||||
*
|
||||
* \param ip The ip address in dotted string notation.
|
||||
*/
|
||||
void dst_addr(const std::string &ip);
|
||||
void src_addr(IPv4Address ip);
|
||||
|
||||
/**
|
||||
* \brief Setter for the destination address field.
|
||||
*
|
||||
* \param ip The ip address in integer notation.
|
||||
*/
|
||||
void dst_addr(uint32_t ip);
|
||||
void dst_addr(IPv4Address ip);
|
||||
|
||||
/**
|
||||
* \brief Setter for the version field.
|
||||
@@ -341,7 +316,6 @@ namespace Tins {
|
||||
*/
|
||||
void version(uint8_t ver);
|
||||
|
||||
|
||||
/**
|
||||
* \brief Sets an IP option.
|
||||
*
|
||||
@@ -470,4 +444,4 @@ namespace Tins {
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif // TINS_IP_H
|
||||
|
||||
49
include/ipaddress.h
Normal file
49
include/ipaddress.h
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* libtins is a net packet wrapper library for crafting and
|
||||
* interpreting sniffed packets.
|
||||
*
|
||||
* Copyright (C) 2011 Nasel
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef TINS_IPADDRESS_H
|
||||
#define TINS_IPADDRESS_H
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <stdint.h>
|
||||
#include "utils.h"
|
||||
|
||||
namespace Tins {
|
||||
class IPv4Address {
|
||||
public:
|
||||
IPv4Address(uint32_t ip = 0) : ip_addr(ip) {}
|
||||
IPv4Address(const std::string &ip) : ip_addr(Utils::ip_to_int(ip)) {}
|
||||
|
||||
operator uint32_t() const { return Utils::net_to_host_l(ip_addr); }
|
||||
operator std::string() const { return Utils::ip_to_string(ip_addr); }
|
||||
|
||||
friend std::ostream &operator<<(std::ostream &output, const IPv4Address &addr) {
|
||||
return output << (std::string)addr;
|
||||
}
|
||||
private:
|
||||
uint32_t ip_addr;
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif // TINS_IPADDRESS_H
|
||||
@@ -240,6 +240,8 @@ namespace Tins {
|
||||
return control_field.super.recv_seq_num;
|
||||
case UNNUMBERED:
|
||||
return 0;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -255,6 +257,8 @@ namespace Tins {
|
||||
return control_field.info.poll_final_bit;
|
||||
case SUPERVISORY:
|
||||
return control_field.super.poll_final_bit;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user