lokinet/include/llarp/ip.hpp

120 lines
2.0 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 18:59:12 +00:00
#ifdef _WIN32
#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>"
#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;
};
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);
struct GetTime
{
llarp_time_t
2018-08-31 14:41:04 +00:00
operator()(const IPv4Packet& pkt) const
{
2018-08-31 14:41:04 +00:00
return pkt.timestamp;
}
};
struct PutTime
{
void
2018-08-31 14:41:04 +00:00
operator()(IPv4Packet& pkt) const
{
2018-08-31 14:41:04 +00:00
pkt.timestamp = llarp_time_now_ms();
}
};
struct CompareOrder
{
bool
2018-08-31 14:41:04 +00:00
operator()(const IPv4Packet& left, const IPv4Packet& right)
{
2018-08-31 14:41:04 +00:00
return left.timestamp < right.timestamp;
}
};
2018-09-17 20:18:11 +00:00
ip_header*
Header()
{
2018-09-17 20:18:11 +00:00
return (ip_header*)&buf[0];
}
2018-09-17 20:18:11 +00:00
const ip_header*
Header() const
{
2018-09-17 20:18:11 +00:00
return (ip_header*)&buf[0];
}
2018-08-20 19:12:12 +00:00
uint32_t
src()
{
2018-09-16 12:31:14 +00:00
return ntohl(Header()->saddr);
}
2018-08-20 19:12:12 +00:00
uint32_t
dst()
{
2018-09-16 12:31:14 +00:00
return ntohl(Header()->daddr);
}
2018-08-20 19:12:12 +00:00
void
src(uint32_t ip)
{
Header()->saddr = htonl(ip);
}
2018-08-20 19:12:12 +00:00
void
dst(uint32_t ip)
{
Header()->daddr = htonl(ip);
}
2018-08-20 19:12:12 +00:00
// update ip packet checksum
void
UpdateChecksum();
};
} // namespace net
} // namespace llarp
#endif