Folder renaming

This commit is contained in:
Jake Hillion 2021-05-13 15:48:24 +01:00
parent 2ac9955c72
commit 67a8a5aeb6
11 changed files with 222 additions and 5 deletions

View File

@ -0,0 +1,93 @@
#include...
struct Packet {
size_t len; uint8_t* data;
Packet(const uint8_t *input, size_t num_bytes) {
len = num_bytes; data = new uint8_t[len];
std::memcpy(data, input, len);
};
~Packet() { delete[] data; }
[[nodiscard]] std::string print() const {
std::stringstream out;
for (size_t i = 0; i < len; i++) {
int temp = data[i];
out << std::hex << temp << " ";
}
return out.str();
}
};
template <class T> class ThreadSafeQueue {
std::queue<T> _queue = std::queue<T>();
std::mutex _mutex; std::condition_variable _cond;
public:
ThreadSafeQueue() = default;
void push(T item) {
_mutex.lock(); _queue.push(item); _mutex.unlock();
_cond.notify_one();
}
T pop() {
while (true) {
std::unique_lock<std::mutex> unique(_mutex);
_cond.wait(unique);
if (!_queue.empty()) {
T out = _queue.front();
_queue.pop();
return out;
}
}
}
};
int tun_alloc(const char *dev, short flags) {
struct ifreq ifr{};
int fd, err;
if( (fd = open("/dev/net/tun" , O_RDWR)) < 0 ) {
perror("Opening /dev/net/tun");
return fd;
}
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = flags;
strncpy(ifr.ifr_name, dev, IFNAMSIZ);
if( (err = ioctl(fd, TUNSETIFF, (void *)&ifr)) < 0 ) {
perror("ioctl(TUNSETIFF)");
close(fd);
return err;
}
return fd;
}
std::mutex print_lock;
void consumer(const int index, ThreadSafeQueue<Packet*> *queue) {
std::cout << "thread " << index << "starting" << std::endl;
while (!stop) {
Packet *p = queue->pop();
print_lock.lock();
std::cout << "thread " << index << " received a packet with content `" << p->print() << "`" << std::endl;
print_lock.unlock();
delete p;
}
}
int main() {
int tun = tun_alloc("nc%d", IFF_TUN);
auto queue = new ThreadSafeQueue<Packet*>();
std::thread threads[10];
for (int i = 0; i < 10; i++) {
const int i_safe = i;
threads[i] = std::thread ([i_safe, queue]() {
consumer(i_safe, queue);
});
}
std::thread reader([tun, queue]() {
uint8_t buffer[1500];
while (true) {
int num_bytes = read(tun, &buffer, 1500);
if (num_bytes != 0) {
auto *packet = new Packet(buffer, num_bytes);
queue->push(packet);
}
}
});
}

View File

@ -0,0 +1,48 @@
package main
import (
"fmt"
"github.com/pkg/taptun"
"os"
"os/signal"
"syscall"
)
type Packet struct {
Data []byte
}
func main() {
tun, err := taptun.NewTun("nc%d")
if err != nil { panic(err) }
inboundPackets := make(chan Packet, 128)
go func() {
bufferSize := 1500
buffer := make([]byte, bufferSize)
for {
read, err := tun.Read(buffer)
if err != nil { panic(err) }
if read == 0 { panic("0 bytes read!") }
p := Packet{}
p.Data = make([]byte, read)
copy(p.Data, buffer)
inboundPackets <- p
}
}()
for i := 0; i < 10; i++ {
i := i
go func() {
for {
p := <-inboundPackets
fmt.Printf("Reader %d: %v\n", i, p)
}
}()
}
}

View File

@ -0,0 +1,34 @@
use std::thread;
use tun_tap::{Iface, Mode};
#[derive(Debug)]
struct Packet {
data: [u8; 1504],
}
fn main() {
let (mut tx, rx) = spmc::channel();
let iface = Iface::new("nc%d", Mode::Tun).expect("failed to create TUN device");
let mut buffer = vec![0; 1504];
for i in 0..10 {
let rx = rx.clone();
thread::spawn(move || {
let packet: Packet = rx.recv().unwrap();
println!("Thread {}: {:?}", i, packet);
});
}
for _ in 0..500 {
iface.recv(&mut buffer).unwrap();
let mut packet = Packet{ data: [0; 1504] };
for i in 0..1504 {
packet.data[i] = buffer[i];
}
tx.send(packet).unwrap();
}
}

View File

@ -0,0 +1,28 @@
%!TEX root = ../thesis.tex
% ********************** Thesis Appendix A - Language Samples *************************
\chapter{Language Samples}
\label{appendix:language-samples}
\begin{figure}
\inputminted[firstline=1,lastline=48]{cpp}{LanguageSamples/Samples/main.cpp}
\caption{A sample script written in C++ to collect packets from a TUN interface and print them from multiple threads}
\label{fig:cpp-tun-sample}
\end{figure}
\begin{figure}
\ContinuedFloat
\inputminted[firstline=49]{cpp}{LanguageSamples/Samples/main.cpp}
\end{figure}
\begin{figure}
\inputminted{rust}{LanguageSamples/Samples/main.rs}
\caption{A sample script written in Rust to collect packets from a TUN interface and print them from multiple threads}
\label{fig:rust-tun-sample}
\end{figure}
\begin{figure}
\inputminted{go}{LanguageSamples/Samples/main.go}
\caption{A sample script written in Go to collect packets from a TUN interface and print them from multiple threads}
\label{fig:go-tun-sample}
\end{figure}

View File

@ -5,24 +5,24 @@
\label{appendix:language-samples}
\begin{figure}
\inputminted[firstline=1,lastline=48]{cpp}{LanguageSamples/Samples/main.cpp}
\inputminted[firstline=1,lastline=48]{cpp}{A1_LanguageSamples/Samples/main.cpp}
\caption{A sample script written in C++ to collect packets from a TUN interface and print them from multiple threads}
\label{fig:cpp-tun-sample}
\end{figure}
\begin{figure}
\ContinuedFloat
\inputminted[firstline=49]{cpp}{LanguageSamples/Samples/main.cpp}
\inputminted[firstline=49]{cpp}{A1_LanguageSamples/Samples/main.cpp}
\end{figure}
\begin{figure}
\inputminted{rust}{LanguageSamples/Samples/main.rs}
\inputminted{rust}{A1_LanguageSamples/Samples/main.rs}
\caption{A sample script written in Rust to collect packets from a TUN interface and print them from multiple threads}
\label{fig:rust-tun-sample}
\end{figure}
\begin{figure}
\inputminted{go}{LanguageSamples/Samples/main.go}
\inputminted{go}{A1_LanguageSamples/Samples/main.go}
\caption{A sample script written in Go to collect packets from a TUN interface and print them from multiple threads}
\label{fig:go-tun-sample}
\end{figure}

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.6 KiB

View File

@ -0,0 +1,8 @@
%!TEX root = ../thesis.tex
% ********************** Thesis Appendix B - Outbound Graphs *************************
\chapter{Outbound Graphs}
\label{appendix:outbound-graphs}
The graphs shown in the evaluation section are Inbound to the Client (unless otherwise specified).
This appendix contains the same tests but Outbound from the client.

Binary file not shown.

View File

@ -0,0 +1,6 @@
% ************************** Proposal **************************
\chapter{Project Proposal}
\label{appendix:project-proposal}
\includepdf[pages=-]{A4_ProjectProposal/project-proposal.pdf}

View File

@ -177,7 +177,7 @@
\include{A1_LanguageSamples/languagesamples}
\include{A3_OutboundGraphs/outboundgraphs}
\include{A4_Proposal/proposal}
\include{A4_ProjectProposal/projectproposal}
\end{appendices}