mirror of
https://github.com/mfontanini/libtins
synced 2026-01-23 02:35:57 +01:00
Added IP pdu class. It's compiling so far.
This commit is contained in:
23
Makefile.in
Normal file
23
Makefile.in
Normal file
@@ -0,0 +1,23 @@
|
||||
CXX=@CXX@
|
||||
CFLAGS=-c -Wall @CFLAGS@ -DTINS_VERSION=@PACKAGE_VERSION@
|
||||
LDFLAGS=
|
||||
SOURCES=$(wildcard src/*.cpp)
|
||||
|
||||
OBJECTS=$(SOURCES:.cpp=.o)
|
||||
INCLUDE = -Iinclude/
|
||||
EXECUTABLE=client
|
||||
|
||||
all: $(SOURCES) $(EXECUTABLE)
|
||||
|
||||
compile: $(OBJECTS)
|
||||
|
||||
recompile: clean all
|
||||
|
||||
$(EXECUTABLE): $(OBJECTS)
|
||||
$(CXX) $(OBJECTS) $(LDFLAGS) -o $@
|
||||
|
||||
.cpp.o:
|
||||
$(CXX) $(CFLAGS) $(INCLUDE) $< -o $@
|
||||
|
||||
clean:
|
||||
rm $(OBJECTS) $(EXECUTABLE)
|
||||
15
configure.ac
Normal file
15
configure.ac
Normal file
@@ -0,0 +1,15 @@
|
||||
AC_INIT(myconfig, 0.1)
|
||||
|
||||
AC_PROG_CXX()
|
||||
AC_LANG(C++)
|
||||
|
||||
if test -n "$debug"
|
||||
then
|
||||
CFLAGS="-DDEBUG -g"
|
||||
else
|
||||
CFLAGS="-O3"
|
||||
fi
|
||||
|
||||
AC_CHECK_HEADERS([])
|
||||
AC_SUBST(CFLAGS)
|
||||
AC_OUTPUT(Makefile)
|
||||
70
include/ip.h
Normal file
70
include/ip.h
Normal file
@@ -0,0 +1,70 @@
|
||||
#ifndef __IP_H
|
||||
#define __IP_H
|
||||
|
||||
#ifndef WIN32
|
||||
#include <endian.h>
|
||||
#endif
|
||||
#include <string>
|
||||
#include <stdint.h>
|
||||
#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; }
|
||||
inline uint16_t frag_off() const { return _ip.frag_off; }
|
||||
inline uint8_t ttl() const { return _ip.ttl; }
|
||||
inline uint8_t protocol() const { return _ip.protocol; }
|
||||
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);
|
||||
void frag_off(uint16_t new_frag_off);
|
||||
void ttl(uint8_t new_ttl);
|
||||
void protocol(uint8_t new_protocol);
|
||||
void check(uint16_t new_check);
|
||||
void source_address(const std::string &ip);
|
||||
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();
|
||||
void write_serialization(uint8_t *buffer, uint32_t total_sz);
|
||||
private:
|
||||
struct iphdr {
|
||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
unsigned int ihl:4;
|
||||
unsigned int version:4;
|
||||
#elif __BYTE_ORDER == __BIG_ENDIAN
|
||||
unsigned int version:4;
|
||||
unsigned int ihl:4;
|
||||
#else
|
||||
# error "Endian is not LE nor BE..."
|
||||
#endif
|
||||
uint8_t tos;
|
||||
uint16_t tot_len;
|
||||
uint16_t id;
|
||||
uint16_t frag_off;
|
||||
uint8_t ttl;
|
||||
uint8_t protocol;
|
||||
uint16_t check;
|
||||
uint32_t saddr;
|
||||
uint32_t daddr;
|
||||
/*The options start here. */
|
||||
} __attribute__((packed));
|
||||
|
||||
iphdr _ip;
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -4,37 +4,40 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
class PDU {
|
||||
public:
|
||||
PDU(uint32_t pdu_flag, PDU *next_pdu = 0);
|
||||
virtual ~PDU();
|
||||
namespace Tins {
|
||||
|
||||
/* This PDU's header size only. */
|
||||
virtual uint32_t header_size() const = 0;
|
||||
/* This PDU's trailer size only. Defaults to 0. */
|
||||
virtual uint32_t trailer_size() const { return 0; }
|
||||
|
||||
/* The size of the whole chain of PDUs, including this one. */
|
||||
uint32_t size() const;
|
||||
|
||||
inline const PDU *inner_pdu() const { return _inner_pdu; }
|
||||
|
||||
/* 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();
|
||||
|
||||
/* 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:
|
||||
uint32_t _pdu_flag;
|
||||
PDU *_inner_pdu;
|
||||
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. */
|
||||
virtual uint32_t trailer_size() const { return 0; }
|
||||
/* The size of the whole chain of PDUs, including this one. */
|
||||
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();
|
||||
|
||||
/* 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:
|
||||
uint32_t _flag;
|
||||
PDU *_inner_pdu;
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
16
include/utils.h
Normal file
16
include/utils.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef __UTILS_H
|
||||
#define __UTILS_H
|
||||
|
||||
|
||||
#include <string>
|
||||
#include <stdint.h>
|
||||
|
||||
namespace Tins {
|
||||
/* Utils namespace. */
|
||||
namespace Utils {
|
||||
uint32_t ip_to_int(const std::string &ip);
|
||||
std::string ip_to_string(uint32_t ip);
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
||||
77
src/ip.cpp
Normal file
77
src/ip.cpp
Normal file
@@ -0,0 +1,77 @@
|
||||
#include <cstring>
|
||||
#include <cassert>
|
||||
#ifndef WIN32
|
||||
#include <netinet/in.h>
|
||||
#endif
|
||||
#include "ip.h"
|
||||
#include "utils.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
Tins::IP::IP(const string &ip_dst, const string &ip_src) : PDU(IPPROTO_IP) {
|
||||
memset(&_ip, 0, sizeof(iphdr));
|
||||
if(ip_dst.size())
|
||||
_ip.daddr = Utils::ip_to_int(ip_dst);
|
||||
if(ip_src.size())
|
||||
_ip.saddr = Utils::ip_to_int(ip_src);
|
||||
}
|
||||
|
||||
Tins::IP::IP(uint32_t ip_dst, uint32_t ip_src) : PDU(IPPROTO_IP) {
|
||||
memset(&_ip, 0, sizeof(iphdr));
|
||||
_ip.daddr = ip_dst;
|
||||
_ip.saddr = ip_src;
|
||||
}
|
||||
|
||||
void Tins::IP::tos(uint8_t new_tos) {
|
||||
_ip.tos = new_tos;
|
||||
}
|
||||
|
||||
void Tins::IP::tot_len(uint16_t new_tot_len) {
|
||||
_ip.tot_len = new_tot_len;
|
||||
}
|
||||
|
||||
void Tins::IP::id(uint16_t new_id) {
|
||||
_ip.id = new_id;
|
||||
}
|
||||
|
||||
void Tins::IP::frag_off(uint16_t new_frag_off) {
|
||||
_ip.frag_off = new_frag_off;
|
||||
}
|
||||
|
||||
void Tins::IP::ttl(uint8_t new_ttl) {
|
||||
_ip.ttl = new_ttl;
|
||||
}
|
||||
|
||||
void Tins::IP::protocol(uint8_t new_protocol) {
|
||||
_ip.protocol = new_protocol;
|
||||
}
|
||||
|
||||
void Tins::IP::check(uint16_t new_check) {
|
||||
_ip.check = new_check;
|
||||
}
|
||||
|
||||
void Tins::IP::source_address(const string &ip) {
|
||||
_ip.saddr = Utils::ip_to_int(ip);
|
||||
}
|
||||
|
||||
void Tins::IP::source_address(uint32_t ip) {
|
||||
_ip.saddr = ip;
|
||||
}
|
||||
|
||||
void Tins::IP::dest_address(const string &ip) {
|
||||
_ip.daddr = Utils::ip_to_int(ip);
|
||||
}
|
||||
|
||||
void Tins::IP::dest_address(uint32_t ip) {
|
||||
_ip.daddr = ip;
|
||||
}
|
||||
|
||||
uint32_t Tins::IP::header_size() {
|
||||
return sizeof(iphdr);
|
||||
}
|
||||
|
||||
void Tins::IP::write_serialization(uint8_t *buffer, uint32_t total_sz) {
|
||||
assert(total_sz >= sizeof(iphdr));
|
||||
memcpy(buffer, &_ip, sizeof(iphdr));
|
||||
}
|
||||
27
src/pdu.cpp
27
src/pdu.cpp
@@ -1,18 +1,17 @@
|
||||
#include <cassert.h>
|
||||
#include <cassert>
|
||||
#include "pdu.h"
|
||||
|
||||
|
||||
PDU::PDU(uint32_t pdu_flag, , PDU *next_pdu) : _pdu_flag(pdu_flag), _inner_pdu(next_pdu) {
|
||||
Tins::PDU::PDU(uint32_t flag, PDU *next_pdu) : _flag(flag), _inner_pdu(next_pdu) {
|
||||
|
||||
}
|
||||
|
||||
PDU::~PDU() {
|
||||
Tins::PDU::~PDU() {
|
||||
delete _inner_pdu;
|
||||
}
|
||||
|
||||
uint32_t PDU::size() const {
|
||||
uint32_t Tins::PDU::size() const {
|
||||
uint32_t sz = header_size() + trailer_size();
|
||||
PDU *ptr(_inner_pdu);
|
||||
const PDU *ptr(_inner_pdu);
|
||||
while(ptr) {
|
||||
sz += ptr->header_size() + trailer_size();
|
||||
ptr = ptr->inner_pdu();
|
||||
@@ -20,22 +19,28 @@ uint32_t PDU::size() const {
|
||||
return sz;
|
||||
}
|
||||
|
||||
void PDU::inner_pdu(PDU *next_pdu) {
|
||||
void Tins::PDU::flag(uint32_t new_flag) {
|
||||
_flag = new_flag;
|
||||
}
|
||||
|
||||
void Tins::PDU::inner_pdu(PDU *next_pdu) {
|
||||
delete _inner_pdu;
|
||||
_inner_pdu = next_pdu;
|
||||
}
|
||||
|
||||
uint8_t *PDU::serialize() {
|
||||
uint8_t *Tins::PDU::serialize() {
|
||||
uint32_t sz(size());
|
||||
uint8_t *buffer = new uint8_t[sz];
|
||||
|
||||
serialize(buffer, sz);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void PDU::serialize(uint8_t *buffer, uint32_t total_sz) {
|
||||
void Tins::PDU::serialize(uint8_t *buffer, uint32_t total_sz) {
|
||||
uint32_t sz = header_size() + trailer_size();
|
||||
write_serialization(buffer, total_sz);
|
||||
/* Must not happen... */
|
||||
std::assert(total_sz >= sz);
|
||||
assert(total_sz >= sz);
|
||||
if(_inner_pdu)
|
||||
_inner_pdu->serialize(buffer + header_size(), total_sz - sz);
|
||||
}
|
||||
|
||||
|
||||
38
src/utils.cpp
Normal file
38
src/utils.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
#include <stdexcept>
|
||||
#include <sstream>
|
||||
#include "utils.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
uint32_t Tins::Utils::ip_to_int(const string &ip) {
|
||||
uint32_t result(0), i(0), end, bytes_found(0);
|
||||
while(i < ip.size() && bytes_found < 4) {
|
||||
uint8_t this_byte(0);
|
||||
end = i + 3;
|
||||
while(i < ip.size() && i < end && ip[i] != '.') {
|
||||
if(ip[i] < '0' || ip[i] > '9')
|
||||
throw std::runtime_error("Non-digit character found in ip");
|
||||
this_byte = (this_byte * 10) + (ip[i] - '0');
|
||||
i++;
|
||||
}
|
||||
result = (result << 8) | this_byte;
|
||||
bytes_found++;
|
||||
if(bytes_found < 4 && i < ip.size() && ip[i] == '.')
|
||||
i++;
|
||||
}
|
||||
if(bytes_found < 4 || (i < ip.size() && bytes_found == 4))
|
||||
throw std::runtime_error("Invalid ip address");
|
||||
return result;
|
||||
}
|
||||
|
||||
string Tins::Utils::ip_to_string(uint32_t ip) {
|
||||
ostringstream oss;
|
||||
int mask(24);
|
||||
while(mask >=0) {
|
||||
oss << ((ip >> mask) & 0xff);
|
||||
if(mask)
|
||||
oss << '.';
|
||||
mask -= 8;
|
||||
}
|
||||
return oss.str();
|
||||
}
|
||||
Reference in New Issue
Block a user