Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
on:
push:
branches:
- "**"
jobs:
ubuntu-test:
name: test on all operating systems
runs-on: ${{ matrix.os }}
defaults:
run:
shell: bash
strategy:
matrix:
os: [ubuntu-latest]
steps:
- uses: actions/checkout@v5
- uses: actions/setup-python@v6
with:
python-version: '3.13'
- name: install tools
run: |
sudo apt-get update
sudo apt-get install -y g++ cmake valgrind libprotobuf-dev protobuf-compiler lcov
- name: compile
run: |
mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Debug -DENABLE_COVERAGE=ON
cmake --build .
- name: run tests
run: |
cd build
cmake --build . --target coverage
- name: run memory tests
run: |
cd build
cmake --build . --target memcheck
68 changes: 66 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,70 @@
cmake_minimum_required(VERSION 3.28.0)
PROJECT(vNet)
SET(CMAKE_CXX_FLAGS "-g -Wall -Werror -std=c++11")
SET(CMAKE_CXX_FLAGS "-g -Wall -Werror -std=c++17")

option(ENABLE_COVERAGE "Enable code coverage instrumentation (requires gcov/lcov)" OFF)

find_program(VALGRIND_EXECUTABLE NAMES valgrind)

if (ENABLE_COVERAGE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage -O0")

set(MEMORYCHECK_COMMAND "${VALGRIND_EXECUTABLE}")
set(MEMORYCHECK_COMMAND_OPTIONS "--tool=memcheck --leak-check=full --track-fds=yes --error-exitcode=1")

add_custom_target(
memcheck

COMMAND ctest -T memcheck
COMMAND python3 ${CMAKE_SOURCE_DIR}/tests/lookup_wrong_fds.py ${CMAKE_CURRENT_BINARY_DIR}/Testing/Temporary
)
add_custom_target(
coverage

COMMAND lcov --directory . --zerocounters --rc branch_coverage=1
COMMAND ctest

COMMAND lcov --capture --directory . --output-file coverage.info --ignore-errors mismatch --rc branch_coverage=1
COMMAND lcov --remove coverage.info '/usr/*' '*test*' --output-file coverage_filtered.info --rc branch_coverage=1

COMMAND genhtml coverage_filtered.info --output-directory html_report --rc branch_coverage=1
)
endif()

#############################################################
###################### PROTOCOL BUFFER ######################
#############################################################

ADD_SUBDIRECTORY(proto)
include_directories(${ProtobufIncludePath})
include_directories(${ProtobufIncludePath})

#############################################################
######################## VIRTUAL NET ########################
#############################################################

SET(VNET_SOURCE_FILES
src/protocol/header.cpp
src/protocol/types.cpp

src/netqueue/element.cpp
src/netqueue/netqueue.cpp
src/netqueue/handler.cpp)

add_library(vnet STATIC ${VNET_SOURCE_FILES})
target_link_libraries(vnet PRIVATE gcov)
target_include_directories(vnet PUBLIC include)

#############################################################
######################## GOOGLE TEST ########################
#############################################################

include(FetchContent)

FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/refs/tags/v1.17.0.zip
)
FetchContent_MakeAvailable(googletest)

enable_testing()
add_subdirectory(tests)
6 changes: 6 additions & 0 deletions include/vnet/const.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

#pragma once

namespace vnet {
const int NET_BUFFER_SIZE = 66000;
};
23 changes: 23 additions & 0 deletions include/vnet/netqueue/element.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

#pragma once

#include <cstdint>
#include <cstddef>

#include "vnet/const.hpp"
#include "vnet/netqueue/fsm.hpp"
#include <cstdlib>

namespace vnet::netqueue {
struct NetworkElement {
FiniteStateMachine state;

int fd;
void *ptr;

size_t net_buffer_used;
uint8_t net_buffer[NET_BUFFER_SIZE];

NetworkElement (int fd, void *ptr, FiniteStateMachine state);
};
}
30 changes: 30 additions & 0 deletions include/vnet/netqueue/fsm.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

#pragma once

namespace vnet::netqueue {
/**
* This finite state machine represents the state in which the file descriptor
* is currently. In particular, there are two distinct finite state machines
* depending on the type of socket.
*
* Anything in TUN_* represents a device that operates as a queue of packets,
* under which reading yields the entire IP packet directly and on which you
* should only write a entire IP packet.
*
* Anything in SCK_* represents a device that follows the specific protobuf
* format in the specifications. In particular, it contains first a 6 byte
* header containing the payload size and payload type.
*/
enum FiniteStateMachine {
TUN_RECEIVE,
TUN_READY,
TUN_ERROR,
TUN_EOF,

SCK_HEADER,
SCK_PAYLOAD,
SCK_READY,
SCK_ERROR,
SCK_EOF
};
};
54 changes: 54 additions & 0 deletions include/vnet/netqueue/handler.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

#pragma once

#include "vnet/netqueue/element.hpp"
#include "vnet/protocol/header.hpp"

namespace vnet::netqueue {
enum close_reason {
CLOSE_ERROR,
CLOSE_EPOLL,
CLOSE_HANGUP
};
struct close_data {
NetworkElement* net_element;

void* ptr_data;
int fd;

close_reason reason;
};
struct socket_data {
NetworkElement* net_element;

void* ptr_data;
int fd;

protocol::PacketType packet_type;
unsigned char* packet_buffer;
size_t payload_size;

unsigned char* full_buffer;
size_t full_buffer_size;
};
struct tun_data {
NetworkElement* net_element;

void* ptr_data;
int fd;

unsigned char* ip_buffer;
size_t ip_buffer_size;
};

void doNothingOnClose (close_data close_content);
void doNothingOnSocketReady (socket_data data);
void doNothingOnTunReady (tun_data data);

struct NetworkQueueHandler {
void (*onClose) (close_data close_content) = &doNothingOnClose;
void (*onSocketReady) (socket_data data) = &doNothingOnSocketReady;
void (*onTunReady) (tun_data data) = &doNothingOnTunReady;
};

}
Loading
Loading