mirror of https://github.com/oxen-io/lokinet
* more tun stuff
* use std::unique_ptr where bare pointers aren't absolutely requiredpull/13/head
parent
913fb1d88a
commit
1a1f93c171
@ -0,0 +1,100 @@
|
|||||||
|
#ifndef LLARP_IP_HPP
|
||||||
|
#define LLARP_IP_HPP
|
||||||
|
#include <llarp/buffer.h>
|
||||||
|
#include <llarp/time.h>
|
||||||
|
#include <netinet/ip.h>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
namespace llarp
|
||||||
|
{
|
||||||
|
namespace net
|
||||||
|
{
|
||||||
|
struct IPv4Packet
|
||||||
|
{
|
||||||
|
static constexpr size_t MaxSize = 1500;
|
||||||
|
llarp_time_t timestamp;
|
||||||
|
size_t sz;
|
||||||
|
byte_t buf[MaxSize];
|
||||||
|
|
||||||
|
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 std::unique_ptr< IPv4Packet >& left,
|
||||||
|
const std::unique_ptr< IPv4Packet >& right)
|
||||||
|
{
|
||||||
|
return left->timestamp < right->timestamp;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
iphdr*
|
||||||
|
Header()
|
||||||
|
{
|
||||||
|
return (iphdr*)buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
const iphdr*
|
||||||
|
Header() const
|
||||||
|
{
|
||||||
|
return (iphdr*)buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t&
|
||||||
|
src()
|
||||||
|
{
|
||||||
|
return Header()->saddr;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t&
|
||||||
|
dst()
|
||||||
|
{
|
||||||
|
return Header()->daddr;
|
||||||
|
}
|
||||||
|
|
||||||
|
const uint32_t&
|
||||||
|
src() const
|
||||||
|
{
|
||||||
|
return Header()->saddr;
|
||||||
|
}
|
||||||
|
|
||||||
|
const uint32_t&
|
||||||
|
dst() const
|
||||||
|
{
|
||||||
|
return Header()->daddr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// put the payload of an ip packet
|
||||||
|
/// recalculate all fields
|
||||||
|
/// return true on success
|
||||||
|
/// return false if the payload doesn't fit
|
||||||
|
bool
|
||||||
|
PutPayload(llarp_buffer_t buf);
|
||||||
|
};
|
||||||
|
|
||||||
|
/// parse an ipv4 packet
|
||||||
|
/// returns nullptr if invalid data
|
||||||
|
/// copies buffer into return value
|
||||||
|
std::unique_ptr< IPv4Packet >
|
||||||
|
ParseIPv4Packet(const void* buf, size_t sz);
|
||||||
|
|
||||||
|
} // namespace net
|
||||||
|
} // namespace llarp
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,18 @@
|
|||||||
|
#include <llarp/endian.h>
|
||||||
|
#include <llarp/ip.hpp>
|
||||||
|
|
||||||
|
namespace llarp
|
||||||
|
{
|
||||||
|
namespace net
|
||||||
|
{
|
||||||
|
std::unique_ptr< IPv4Packet >
|
||||||
|
ParseIPv4Packet(const void* buf, size_t sz)
|
||||||
|
{
|
||||||
|
if(sz < 16 || sz > IPv4Packet::MaxSize)
|
||||||
|
return nullptr;
|
||||||
|
IPv4Packet* pkt = new IPv4Packet();
|
||||||
|
memcpy(pkt->buf, buf, sz);
|
||||||
|
return std::unique_ptr< IPv4Packet >(pkt);
|
||||||
|
}
|
||||||
|
} // namespace net
|
||||||
|
} // namespace llarp
|
Loading…
Reference in New Issue