diff --git a/A1_LanguageSamples/LanguageSamples/Samples/main.cpp b/A1_LanguageSamples/LanguageSamples/Samples/main.cpp new file mode 100644 index 0000000..8c225c8 --- /dev/null +++ b/A1_LanguageSamples/LanguageSamples/Samples/main.cpp @@ -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 ThreadSafeQueue { + std::queue _queue = std::queue(); + 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 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 *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(); + 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); + } + } + }); +} diff --git a/A1_LanguageSamples/LanguageSamples/Samples/main.go b/A1_LanguageSamples/LanguageSamples/Samples/main.go new file mode 100644 index 0000000..7e040ea --- /dev/null +++ b/A1_LanguageSamples/LanguageSamples/Samples/main.go @@ -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) + } + }() + } +} diff --git a/A1_LanguageSamples/LanguageSamples/Samples/main.rs b/A1_LanguageSamples/LanguageSamples/Samples/main.rs new file mode 100644 index 0000000..b5f03d8 --- /dev/null +++ b/A1_LanguageSamples/LanguageSamples/Samples/main.rs @@ -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(); + } +} diff --git a/A1_LanguageSamples/LanguageSamples/languagesamples.tex b/A1_LanguageSamples/LanguageSamples/languagesamples.tex new file mode 100644 index 0000000..36abe8c --- /dev/null +++ b/A1_LanguageSamples/LanguageSamples/languagesamples.tex @@ -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} diff --git a/A1_LanguageSamples/languagesamples.tex b/A1_LanguageSamples/languagesamples.tex index 36abe8c..8c1236b 100644 --- a/A1_LanguageSamples/languagesamples.tex +++ b/A1_LanguageSamples/languagesamples.tex @@ -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} diff --git a/A3_OutboundGraphs/OutboundGraphs/Figs/graphs/bidirectional-outbound.png b/A3_OutboundGraphs/OutboundGraphs/Figs/graphs/bidirectional-outbound.png new file mode 100644 index 0000000..c45f520 Binary files /dev/null and b/A3_OutboundGraphs/OutboundGraphs/Figs/graphs/bidirectional-outbound.png differ diff --git a/A3_OutboundGraphs/OutboundGraphs/outboundgraphs.tex b/A3_OutboundGraphs/OutboundGraphs/outboundgraphs.tex new file mode 100644 index 0000000..7f9fdf4 --- /dev/null +++ b/A3_OutboundGraphs/OutboundGraphs/outboundgraphs.tex @@ -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. diff --git a/A4_ProjectProposal/Proposal/project-proposal.pdf b/A4_ProjectProposal/Proposal/project-proposal.pdf new file mode 100644 index 0000000..63bd141 Binary files /dev/null and b/A4_ProjectProposal/Proposal/project-proposal.pdf differ diff --git a/A4_ProjectProposal/proposal.tex b/A4_ProjectProposal/Proposal/proposal.tex similarity index 100% rename from A4_ProjectProposal/proposal.tex rename to A4_ProjectProposal/Proposal/proposal.tex diff --git a/A4_ProjectProposal/projectproposal.tex b/A4_ProjectProposal/projectproposal.tex new file mode 100644 index 0000000..b0ba21e --- /dev/null +++ b/A4_ProjectProposal/projectproposal.tex @@ -0,0 +1,6 @@ +% ************************** Proposal ************************** + +\chapter{Project Proposal} +\label{appendix:project-proposal} + +\includepdf[pages=-]{A4_ProjectProposal/project-proposal.pdf} diff --git a/thesis.tex b/thesis.tex index 6436f91..68d97d6 100644 --- a/thesis.tex +++ b/thesis.tex @@ -177,7 +177,7 @@ \include{A1_LanguageSamples/languagesamples} \include{A3_OutboundGraphs/outboundgraphs} -\include{A4_Proposal/proposal} +\include{A4_ProjectProposal/projectproposal} \end{appendices}