Parse roughly IPv6 header

This commit is contained in:
Théophile Bastian 2020-06-05 16:14:17 +02:00
parent 55ada8abc1
commit 8b75660bbc
3 changed files with 31 additions and 1 deletions

View file

@ -2,7 +2,7 @@ CXX=g++
CXXFLAGS=-O2 -g -Wall -Wextra -std=c++17
CXXLIBS=
OBJS=UdpVpn.o VpnPeer.o TunDevice.o util.o main.o
OBJS=UdpVpn.o VpnPeer.o TunDevice.o ip_header.o util.o main.o
TARGET=congestvpn
all: $(TARGET)

19
ip_header.cpp Normal file
View file

@ -0,0 +1,19 @@
#include <cstring>
#include "ip_header.hpp"
#include "util.hpp"
bool parse_ipv6_header(const char* data, size_t len, IPv6Header& header) {
if(len < 40) { // header too short
kdebugf("Bad v6 header -- too short.\n");
return false;
}
if((data[0] & 0xf0) != 0x60) { // bad version number
kdebugf("Bad v6 header -- bad version number (byte = %x).\n", data[0]);
return false;
}
memcpy(&(header.source), data + 8, 16);
memcpy(&(header.dest), data + 24, 16);
return true;
}

11
ip_header.hpp Normal file
View file

@ -0,0 +1,11 @@
#pragma once
#include <netinet/in.h>
struct IPv6Header {
in6_addr source;
in6_addr dest;
};
/** Parse an IPv6 header, filling `header`. Returns `true` on success. */
bool parse_ipv6_header(const char* data, size_t len, IPv6Header& header);