try calcuating tcp checksums right

try tracking convo tags correctly
pull/15/head
Jeff Becker 6 years ago
parent 8d6e7df94d
commit 0c1e22650e
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -43,9 +43,9 @@ namespace llarp
bool bool
SetupNetworking(); SetupNetworking();
/// overrides Endpoint /// extends Endpoint
/// handle inbound traffic /// handle inbound traffic
void bool
HandleDataMessage(const PathID_t& src, service::ProtocolMessage* msg); HandleDataMessage(const PathID_t& src, service::ProtocolMessage* msg);
#ifndef _MINGW32_NO_THREADS #ifndef _MINGW32_NO_THREADS

@ -105,11 +105,8 @@ namespace llarp
bool bool
ForgetPathToService(const Address& remote); ForgetPathToService(const Address& remote);
virtual void virtual bool
HandleDataMessage(const PathID_t&, ProtocolMessage* msg) HandleDataMessage(const PathID_t&, ProtocolMessage* msg);
{
// override me in subclass
}
/// ensure that we know a router, looks up if it doesn't /// ensure that we know a router, looks up if it doesn't
void void

@ -14,7 +14,7 @@ namespace llarp
struct ProtocolMessage; struct ProtocolMessage;
struct IDataHandler struct IDataHandler
{ {
virtual void virtual bool
HandleDataMessage(const PathID_t&, ProtocolMessage* msg) = 0; HandleDataMessage(const PathID_t&, ProtocolMessage* msg) = 0;
virtual bool virtual bool

@ -196,15 +196,13 @@ namespace llarp
}); });
} }
void bool
TunEndpoint::HandleDataMessage(const PathID_t &src, TunEndpoint::HandleDataMessage(const PathID_t &src,
service::ProtocolMessage *msg) service::ProtocolMessage *msg)
{ {
PutIntroFor(msg->tag, msg->introReply); if(!Endpoint::HandleDataMessage(src, msg))
EnsureReplyPath(msg->sender); return false;
service::Address addr; uint32_t themIP = ObtainIPForAddr(msg->sender.Addr());
msg->sender.CalculateAddress(addr.data());
uint32_t themIP = ObtainIPForAddr(addr);
uint32_t usIP = m_OurIP; uint32_t usIP = m_OurIP;
auto buf = llarp::Buffer(msg->payload); auto buf = llarp::Buffer(msg->payload);
if(m_NetworkToUserPktQueue.EmplaceIf( if(m_NetworkToUserPktQueue.EmplaceIf(
@ -223,6 +221,7 @@ namespace llarp
" bytes from ", inet_ntoa({htonl(themIP)})); " bytes from ", inet_ntoa({htonl(themIP)}));
else else
llarp::LogWarn(Name(), " dropped packet"); llarp::LogWarn(Name(), " dropped packet");
return true;
} }
uint32_t uint32_t

@ -4,6 +4,7 @@
#include "llarp/buffer.hpp" #include "llarp/buffer.hpp"
#include "mem.hpp" #include "mem.hpp"
#include <netinet/in.h> #include <netinet/in.h>
#include <llarp/endian.h>
#include <map> #include <map>
namespace llarp namespace llarp
@ -28,17 +29,9 @@ namespace llarp
return llarp::InitBuffer(buf, sz); return llarp::InitBuffer(buf, sz);
} }
/// first map entry is bytes offset to checksum relative to end of ip header
/// second map entry is offset from beginning of packet for checksum
static std::map< byte_t, std::pair< uint16_t, uint16_t > >
protoChecksumOffsets = {{IPPROTO_TCP, {16, 12}},
{IPPROTO_ICMP, {2, 0}},
{IPPROTO_UDP, {6, 0}}};
static uint16_t static uint16_t
ipchksum(const byte_t *buf, size_t sz) ipchksum(const byte_t *buf, size_t sz, uint32_t sum = 0)
{ {
uint32_t sum = 0;
while(sz > 1) while(sz > 1)
{ {
sum += *(const uint16_t *)buf; sum += *(const uint16_t *)buf;
@ -54,6 +47,24 @@ namespace llarp
return ~sum; return ~sum;
} }
static std::map<
byte_t, std::function< void(const ip_header *, byte_t *, size_t) > >
protoCheckSummer = {
{IPPROTO_ICMP,
[](const ip_header *hdr, byte_t *buf, size_t sz) {
auto len = hdr->ihl * 4;
uint16_t *check = (uint16_t *)buf + len + 2;
*check = 0;
*check = ipchksum(buf, sz);
}},
{IPPROTO_TCP, [](const ip_header *hdr, byte_t *pkt, size_t sz) {
auto len = hdr->ihl * 4;
uint16_t *check = (uint16_t *)pkt + 28 + len;
*check = 0;
*check =
ipchksum(pkt, sz - len,
hdr->saddr + hdr->daddr + IPPROTO_TCP + (sz - len));
}}};
void void
IPv4Packet::UpdateChecksum() IPv4Packet::UpdateChecksum()
{ {
@ -62,15 +73,11 @@ namespace llarp
auto len = hdr->ihl * 4; auto len = hdr->ihl * 4;
hdr->check = ipchksum(buf, len); hdr->check = ipchksum(buf, len);
auto proto = hdr->protocol; auto proto = hdr->protocol;
auto itr = protoChecksumOffsets.find(proto); auto itr = protoCheckSummer.find(proto);
if(itr != protoChecksumOffsets.end()) if(itr != protoCheckSummer.end())
{ {
auto offset = itr->second.second; itr->second(hdr, buf, sz);
uint16_t *check = (uint16_t *)(buf + len + itr->second.first);
*check = 0;
*check = ipchksum(buf + offset, sz - offset);
} }
} }
} // namespace net } // namespace net
} // namespace llarp } // namespace llarp

@ -698,6 +698,18 @@ namespace llarp
return true; return true;
} }
bool
Endpoint::HandleDataMessage(const PathID_t& src, ProtocolMessage* msg)
{
msg->sender.UpdateAddr();
auto path = GetPathByID(src);
if(!path)
return false;
PutIntroFor(msg->tag, path->intro);
EnsureReplyPath(msg->sender);
return true;
}
bool bool
Endpoint::HandleHiddenServiceFrame(const ProtocolFrame* frame) Endpoint::HandleHiddenServiceFrame(const ProtocolFrame* frame)
{ {

Loading…
Cancel
Save