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-09-17 18:59:12 +00:00
|
|
|
#ifdef _WIN32
|
2018-08-18 03:19:00 +00:00
|
|
|
#include <winsock2.h>
|
2018-08-23 18:02:02 +00:00
|
|
|
#endif
|
2018-09-17 20:18:11 +00:00
|
|
|
struct ip_header
|
2018-09-17 18:59:12 +00:00
|
|
|
{
|
|
|
|
#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-16 22:36:15 +00:00
|
|
|
#endif
|
2018-09-17 18:59:12 +00: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 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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-09-17 20:18:11 +00:00
|
|
|
ip_header*
|
2018-08-17 19:49:58 +00:00
|
|
|
Header()
|
|
|
|
{
|
2018-09-17 20:18:11 +00:00
|
|
|
return (ip_header*)&buf[0];
|
2018-08-17 19:49:58 +00:00
|
|
|
}
|
|
|
|
|
2018-09-17 20:18:11 +00:00
|
|
|
const ip_header*
|
2018-08-17 19:49:58 +00:00
|
|
|
Header() const
|
|
|
|
{
|
2018-09-17 20:18:11 +00:00
|
|
|
return (ip_header*)&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-09-16 12:31:14 +00:00
|
|
|
return ntohl(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-09-16 12:31:14 +00:00
|
|
|
return ntohl(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-13 16:41:53 +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-13 16:41:53 +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
|