2018-08-17 19:49:58 +00:00
|
|
|
#ifndef LLARP_IP_HPP
|
|
|
|
#define LLARP_IP_HPP
|
|
|
|
#include <llarp/buffer.h>
|
|
|
|
#include <llarp/time.h>
|
2018-08-24 16:07:17 +00:00
|
|
|
#include <llarp/net.hpp>
|
2018-08-18 03:19:00 +00:00
|
|
|
#ifndef _WIN32
|
2018-08-16 22:36:15 +00:00
|
|
|
#include <netinet/in.h>
|
2018-08-17 19:49:58 +00:00
|
|
|
#include <netinet/ip.h>
|
2018-08-18 03:19:00 +00:00
|
|
|
#else
|
|
|
|
#include <winsock2.h>
|
2018-08-20 10:52:47 +00:00
|
|
|
// Apparently this does not seem to be located _anywhere_ in the windows sdk???
|
|
|
|
// -despair86
|
2018-08-18 03:19:00 +00:00
|
|
|
typedef struct ip_hdr
|
|
|
|
{
|
2018-08-20 10:52:47 +00:00
|
|
|
unsigned char
|
|
|
|
ip_header_len : 4; // 4-bit header length (in 32-bit words) normally=5
|
|
|
|
// (Means 20 Bytes may be 24 also)
|
2018-09-07 20:56:30 +00:00
|
|
|
unsigned char ip_version : 4; // 4-bit IPv4 version
|
2018-08-20 10:52:47 +00:00
|
|
|
unsigned char ip_tos; // IP type of service
|
|
|
|
unsigned short ip_total_length; // Total length
|
|
|
|
unsigned short ip_id; // Unique identifier
|
2018-08-18 03:19:00 +00:00
|
|
|
|
2018-08-20 10:52:47 +00:00
|
|
|
unsigned char ip_frag_offset : 5; // Fragment offset field
|
2018-08-18 03:19:00 +00:00
|
|
|
|
2018-08-20 10:52:47 +00:00
|
|
|
unsigned char ip_more_fragment : 1;
|
|
|
|
unsigned char ip_dont_fragment : 1;
|
|
|
|
unsigned char ip_reserved_zero : 1;
|
2018-08-18 03:19:00 +00:00
|
|
|
|
2018-08-20 10:52:47 +00:00
|
|
|
unsigned char ip_frag_offset1; // fragment offset
|
2018-08-18 03:19:00 +00:00
|
|
|
|
2018-08-20 10:52:47 +00:00
|
|
|
unsigned char ip_ttl; // Time to live
|
|
|
|
unsigned char ip_protocol; // Protocol(TCP,UDP etc)
|
|
|
|
unsigned short ip_checksum; // IP checksum
|
|
|
|
unsigned int ip_srcaddr; // Source address
|
|
|
|
unsigned int ip_destaddr; // Source address
|
2018-08-18 03:19:00 +00:00
|
|
|
} IPV4_HDR;
|
|
|
|
#define iphdr IPV4_HDR
|
2018-08-21 18:31:42 +00:00
|
|
|
#define saddr ip_srcaddr
|
|
|
|
#define daddr ip_destaddr
|
2018-08-21 18:33:27 +00:00
|
|
|
#define check ip_checksum
|
2018-08-21 13:02:05 +00:00
|
|
|
#define ihl ip_header_len
|
2018-08-18 03:19:00 +00:00
|
|
|
#endif
|
2018-08-17 19:49:58 +00:00
|
|
|
#include <memory>
|
2018-08-18 03:19:00 +00:00
|
|
|
#if !defined(__linux__) && !defined(_WIN32)
|
2018-08-16 22:36:15 +00:00
|
|
|
#define iphdr ip
|
|
|
|
#define saddr ip_src.s_addr
|
|
|
|
#define daddr ip_dst.s_addr
|
2018-08-23 18:02:02 +00:00
|
|
|
#define ip_version ip_v
|
|
|
|
#define check ip_sum
|
|
|
|
#define ihl ip_hl
|
|
|
|
#endif
|
|
|
|
#if defined(__linux__)
|
|
|
|
#define ip_version version
|
2018-08-16 22:36:15 +00:00
|
|
|
#endif
|
2018-08-17 19:49:58 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace net
|
|
|
|
{
|
|
|
|
struct IPv4Packet
|
|
|
|
{
|
|
|
|
static constexpr size_t MaxSize = 1500;
|
2018-08-22 15:52:10 +00:00
|
|
|
llarp_time_t timestamp;
|
|
|
|
size_t sz;
|
|
|
|
byte_t buf[MaxSize];
|
|
|
|
|
|
|
|
llarp_buffer_t
|
|
|
|
Buffer();
|
2018-08-20 19:12:12 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
Load(llarp_buffer_t buf);
|
2018-08-17 19:49:58 +00:00
|
|
|
|
|
|
|
struct GetTime
|
|
|
|
{
|
|
|
|
llarp_time_t
|
2018-08-31 14:41:04 +00:00
|
|
|
operator()(const IPv4Packet& pkt) const
|
2018-08-17 19:49:58 +00:00
|
|
|
{
|
2018-08-31 14:41:04 +00:00
|
|
|
return pkt.timestamp;
|
2018-08-17 19:49:58 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct PutTime
|
|
|
|
{
|
|
|
|
void
|
2018-08-31 14:41:04 +00:00
|
|
|
operator()(IPv4Packet& pkt) const
|
2018-08-17 19:49:58 +00:00
|
|
|
{
|
2018-08-31 14:41:04 +00:00
|
|
|
pkt.timestamp = llarp_time_now_ms();
|
2018-08-17 19:49:58 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CompareOrder
|
|
|
|
{
|
|
|
|
bool
|
2018-08-31 14:41:04 +00:00
|
|
|
operator()(const IPv4Packet& left, const IPv4Packet& right)
|
2018-08-17 19:49:58 +00:00
|
|
|
{
|
2018-08-31 14:41:04 +00:00
|
|
|
return left.timestamp < right.timestamp;
|
2018-08-17 19:49:58 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
iphdr*
|
|
|
|
Header()
|
|
|
|
{
|
2018-08-22 15:52:10 +00:00
|
|
|
return (iphdr*)&buf[0];
|
2018-08-17 19:49:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const iphdr*
|
|
|
|
Header() const
|
|
|
|
{
|
2018-08-22 15:52:10 +00:00
|
|
|
return (iphdr*)&buf[0];
|
2018-08-17 19:49:58 +00:00
|
|
|
}
|
|
|
|
|
2018-08-20 19:12:12 +00:00
|
|
|
uint32_t
|
2018-08-17 19:49:58 +00:00
|
|
|
src()
|
|
|
|
{
|
2018-08-22 15:52:10 +00:00
|
|
|
return Header()->saddr;
|
2018-08-17 19:49:58 +00:00
|
|
|
}
|
|
|
|
|
2018-08-20 19:12:12 +00:00
|
|
|
uint32_t
|
2018-08-17 19:49:58 +00:00
|
|
|
dst()
|
|
|
|
{
|
2018-08-22 15:52:10 +00:00
|
|
|
return Header()->daddr;
|
2018-08-17 19:49:58 +00:00
|
|
|
}
|
|
|
|
|
2018-08-20 19:12:12 +00:00
|
|
|
void
|
|
|
|
src(uint32_t ip)
|
2018-08-17 19:49:58 +00:00
|
|
|
{
|
2018-09-10 19:31:29 +00:00
|
|
|
Header()->saddr = htonl(ip);
|
2018-08-17 19:49:58 +00:00
|
|
|
}
|
|
|
|
|
2018-08-20 19:12:12 +00:00
|
|
|
void
|
|
|
|
dst(uint32_t ip)
|
2018-08-17 19:49:58 +00:00
|
|
|
{
|
2018-09-10 19:31:29 +00:00
|
|
|
Header()->daddr = htonl(ip);
|
2018-08-17 19:49:58 +00:00
|
|
|
}
|
|
|
|
|
2018-08-20 19:12:12 +00:00
|
|
|
// update ip packet checksum
|
|
|
|
void
|
|
|
|
UpdateChecksum();
|
2018-08-17 19:49:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace net
|
|
|
|
} // namespace llarp
|
|
|
|
|
|
|
|
#endif
|