You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lokinet/include/llarp/ip.hpp

120 lines
2.0 KiB
C++

#ifndef LLARP_IP_HPP
#define LLARP_IP_HPP
#include <llarp/buffer.h>
#include <llarp/time.h>
#include <llarp/net.hpp>
6 years ago
#ifdef _WIN32
#include <winsock2.h>
6 years ago
#endif
6 years ago
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
6 years ago
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;
6 years ago
llarp_time_t timestamp;
size_t sz;
byte_t buf[MaxSize];
llarp_buffer_t
Buffer();
6 years ago
bool
Load(llarp_buffer_t buf);
struct GetTime
{
llarp_time_t
operator()(const IPv4Packet& pkt) const
{
return pkt.timestamp;
}
};
struct PutTime
{
void
operator()(IPv4Packet& pkt) const
{
pkt.timestamp = llarp_time_now_ms();
}
};
struct CompareOrder
{
bool
operator()(const IPv4Packet& left, const IPv4Packet& right)
{
return left.timestamp < right.timestamp;
}
};
iphdr*
Header()
{
6 years ago
return (iphdr*)&buf[0];
}
const iphdr*
Header() const
{
6 years ago
return (iphdr*)&buf[0];
}
6 years ago
uint32_t
src()
{
6 years ago
return ntohl(Header()->saddr);
}
6 years ago
uint32_t
dst()
{
6 years ago
return ntohl(Header()->daddr);
}
6 years ago
void
src(uint32_t ip)
{
Header()->saddr = htonl(ip);
}
6 years ago
void
dst(uint32_t ip)
{
Header()->daddr = htonl(ip);
}
6 years ago
// update ip packet checksum
void
UpdateChecksum();
};
} // namespace net
} // namespace llarp
#endif