2018-08-17 21:49:58 +02:00
|
|
|
#ifndef LLARP_IP_HPP
|
|
|
|
#define LLARP_IP_HPP
|
|
|
|
#include <llarp/buffer.h>
|
|
|
|
#include <llarp/time.h>
|
2018-08-24 18:07:17 +02:00
|
|
|
#include <llarp/net.hpp>
|
2018-09-17 20:59:12 +02:00
|
|
|
#ifdef _WIN32
|
2018-08-18 05:19:00 +02:00
|
|
|
#include <winsock2.h>
|
2018-08-23 20:02:02 +02:00
|
|
|
#endif
|
2018-09-17 20:59:12 +02:00
|
|
|
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 "Please fix <bits/endian.h>"
|
2018-08-17 00:36:15 +02:00
|
|
|
#endif
|
2018-09-17 20:59:12 +02:00
|
|
|
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;
|
|
|
|
};
|
2018-08-17 21:49:58 +02:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace net
|
|
|
|
{
|
|
|
|
struct IPv4Packet
|
|
|
|
{
|
|
|
|
static constexpr size_t MaxSize = 1500;
|
2018-08-22 17:52:10 +02:00
|
|
|
llarp_time_t timestamp;
|
|
|
|
size_t sz;
|
|
|
|
byte_t buf[MaxSize];
|
|
|
|
|
|
|
|
llarp_buffer_t
|
|
|
|
Buffer();
|
2018-08-20 21:12:12 +02:00
|
|
|
|
|
|
|
bool
|
|
|
|
Load(llarp_buffer_t buf);
|
2018-08-17 21:49:58 +02:00
|
|
|
|
|
|
|
struct GetTime
|
|
|
|
{
|
|
|
|
llarp_time_t
|
2018-08-31 16:41:04 +02:00
|
|
|
operator()(const IPv4Packet& pkt) const
|
2018-08-17 21:49:58 +02:00
|
|
|
{
|
2018-08-31 16:41:04 +02:00
|
|
|
return pkt.timestamp;
|
2018-08-17 21:49:58 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct PutTime
|
|
|
|
{
|
|
|
|
void
|
2018-08-31 16:41:04 +02:00
|
|
|
operator()(IPv4Packet& pkt) const
|
2018-08-17 21:49:58 +02:00
|
|
|
{
|
2018-08-31 16:41:04 +02:00
|
|
|
pkt.timestamp = llarp_time_now_ms();
|
2018-08-17 21:49:58 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CompareOrder
|
|
|
|
{
|
|
|
|
bool
|
2018-08-31 16:41:04 +02:00
|
|
|
operator()(const IPv4Packet& left, const IPv4Packet& right)
|
2018-08-17 21:49:58 +02:00
|
|
|
{
|
2018-08-31 16:41:04 +02:00
|
|
|
return left.timestamp < right.timestamp;
|
2018-08-17 21:49:58 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
iphdr*
|
|
|
|
Header()
|
|
|
|
{
|
2018-08-22 17:52:10 +02:00
|
|
|
return (iphdr*)&buf[0];
|
2018-08-17 21:49:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const iphdr*
|
|
|
|
Header() const
|
|
|
|
{
|
2018-08-22 17:52:10 +02:00
|
|
|
return (iphdr*)&buf[0];
|
2018-08-17 21:49:58 +02:00
|
|
|
}
|
|
|
|
|
2018-08-20 21:12:12 +02:00
|
|
|
uint32_t
|
2018-08-17 21:49:58 +02:00
|
|
|
src()
|
|
|
|
{
|
2018-09-16 14:31:14 +02:00
|
|
|
return ntohl(Header()->saddr);
|
2018-08-17 21:49:58 +02:00
|
|
|
}
|
|
|
|
|
2018-08-20 21:12:12 +02:00
|
|
|
uint32_t
|
2018-08-17 21:49:58 +02:00
|
|
|
dst()
|
|
|
|
{
|
2018-09-16 14:31:14 +02:00
|
|
|
return ntohl(Header()->daddr);
|
2018-08-17 21:49:58 +02:00
|
|
|
}
|
|
|
|
|
2018-08-20 21:12:12 +02:00
|
|
|
void
|
|
|
|
src(uint32_t ip)
|
2018-08-17 21:49:58 +02:00
|
|
|
{
|
2018-09-13 18:41:53 +02:00
|
|
|
Header()->saddr = htonl(ip);
|
2018-08-17 21:49:58 +02:00
|
|
|
}
|
|
|
|
|
2018-08-20 21:12:12 +02:00
|
|
|
void
|
|
|
|
dst(uint32_t ip)
|
2018-08-17 21:49:58 +02:00
|
|
|
{
|
2018-09-13 18:41:53 +02:00
|
|
|
Header()->daddr = htonl(ip);
|
2018-08-17 21:49:58 +02:00
|
|
|
}
|
|
|
|
|
2018-08-20 21:12:12 +02:00
|
|
|
// update ip packet checksum
|
|
|
|
void
|
|
|
|
UpdateChecksum();
|
2018-08-17 21:49:58 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace net
|
|
|
|
} // namespace llarp
|
|
|
|
|
|
|
|
#endif
|