1
1
Fork 0
mirror of https://github.com/oxen-io/lokinet synced 2023-12-14 06:53:00 +01:00
lokinet/include/llarp/ip.hpp

120 lines
2 KiB
C++
Raw Normal View History

#ifndef LLARP_IP_HPP
#define LLARP_IP_HPP
#include <llarp/buffer.h>
#include <llarp/time.h>
#include <llarp/net.hpp>
2018-09-17 20:59:12 +02:00
#ifdef _WIN32
#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>"
#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;
};
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);
struct GetTime
{
llarp_time_t
2018-08-31 16:41:04 +02:00
operator()(const IPv4Packet& pkt) const
{
2018-08-31 16:41:04 +02:00
return pkt.timestamp;
}
};
struct PutTime
{
void
2018-08-31 16:41:04 +02:00
operator()(IPv4Packet& pkt) const
{
2018-08-31 16:41:04 +02:00
pkt.timestamp = llarp_time_now_ms();
}
};
struct CompareOrder
{
bool
2018-08-31 16:41:04 +02:00
operator()(const IPv4Packet& left, const IPv4Packet& right)
{
2018-08-31 16:41:04 +02:00
return left.timestamp < right.timestamp;
}
};
iphdr*
Header()
{
2018-08-22 17:52:10 +02:00
return (iphdr*)&buf[0];
}
const iphdr*
Header() const
{
2018-08-22 17:52:10 +02:00
return (iphdr*)&buf[0];
}
2018-08-20 21:12:12 +02:00
uint32_t
src()
{
2018-09-16 14:31:14 +02:00
return ntohl(Header()->saddr);
}
2018-08-20 21:12:12 +02:00
uint32_t
dst()
{
2018-09-16 14:31:14 +02:00
return ntohl(Header()->daddr);
}
2018-08-20 21:12:12 +02:00
void
src(uint32_t ip)
{
Header()->saddr = htonl(ip);
}
2018-08-20 21:12:12 +02:00
void
dst(uint32_t ip)
{
Header()->daddr = htonl(ip);
}
2018-08-20 21:12:12 +02:00
// update ip packet checksum
void
UpdateChecksum();
};
} // namespace net
} // namespace llarp
#endif