Add missing <array> inclusion, which is needed for modern libstd++
[tunudptotcp] / main.cpp
index c116e8994a27b53e6d0d3ec10a147b2704b26b73..7c7cbed15fc503faf126d2f1cccafe06ca9acfb8 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -13,6 +13,7 @@
 #include <linux/if_tun.h>
 #include <assert.h>
 
+#include <array>
 #include <atomic>
 #include <chrono>
 #include <thread>
@@ -186,8 +187,9 @@ static std::atomic<uint32_t> highest_recvd_seq(0), cur_seq(0);
 static std::atomic<uint16_t> local_port(0);
 static uint16_t remote_port;
 static bool are_server;
+static uint64_t timestamps_magic;
 
-static void build_tcp_header(unsigned char* buf, uint32_t len, int syn, int synack, in_addr_t src_addr, in_addr_t dest_addr) {
+static int build_tcp_header(unsigned char* buf, uint32_t len, int syn, int synack, in_addr_t src_addr, in_addr_t dest_addr) {
        buf[0 ] = local_port >> 8;         // src port
        buf[1 ] = local_port;              // src port
        buf[2 ] = remote_port >> 8;        // dst port
@@ -205,8 +207,8 @@ static void build_tcp_header(unsigned char* buf, uint32_t len, int syn, int syna
        buf[10] = their_seq >> (8 * 1);    // ACK
        buf[11] = their_seq >> (8 * 0);    // ACK
 
-       bool longpkt = syn || synack;
-       buf[12] = (longpkt ? 6 : 5) << 4;  // data offset
+       unsigned char hdrlen = syn ? 36 : (synack ? 24 : 20);
+       buf[12] = (hdrlen/4) << 4;  // data offset
        if (syn)
                buf[13] = 1 << 1;              // SYN
        else if (synack)
@@ -223,16 +225,25 @@ static void build_tcp_header(unsigned char* buf, uint32_t len, int syn, int syna
        buf[18] = 0x00;                    // URG Pointer
        buf[19] = 0x00;                    // URG Pointer
 
-       if (longpkt) {
+       if (syn || synack) {
        buf[20] = 0x01;                    // NOP
        buf[21] = 0x03;                    // Window Scale
        buf[22] = 0x03;                    // Window Scale Option Length
        buf[23] = 0x0e;                    // 1GB Window Size (0xffff << 0x0e)
        }
+       if (syn) {
+       buf[24] = 0x01;                    // NOP
+       buf[25] = 0x01;                    // NOP
+       buf[26] = 8;                       // Timestamp
+       buf[27] = 10;                      // Timestamp Option Length
+       memcpy(buf + 28, &timestamps_magic, 8);
+       }
 
-       uint16_t checksum = tcp_checksum(buf, len + 20 + (longpkt ? 4 : 0), src_addr, dest_addr);
+       uint16_t checksum = tcp_checksum(buf, len + hdrlen, src_addr, dest_addr);
        buf[16] = checksum;                // Checksum
        buf[17] = checksum >> 8;           // Checksum
+
+       return hdrlen;
 }
 
 static void build_ip_header(unsigned char* buf, struct pkt_hdr hdr, const in_addr_t& src_addr, const in_addr_t& dest_addr) {
@@ -294,7 +305,7 @@ static int fdr;
 static int fd[TUN_IF_COUNT];
 static struct sockaddr_in dest;
 static in_addr_t src, tun_src, tun_dest;
-static uint64_t tcp_init_magic;
+static uint32_t starting_ack;
 
 #define PENDING_MESSAGES_BUFF_SIZE (0x800)
 #define THREAD_POLL_SLEEP_MICS 250
@@ -363,16 +374,31 @@ static void tcp_to_tun_queue_process() {
                bool ack = tcp_buf[13] & (1 << 4);
 
                if (are_server && syn && !ack) {
-                       // We're a server and just got a client
-                       if (tcp_buf[4 ] != uint8_t(tcp_init_magic >> (7 * 8)) ||
-                           tcp_buf[5 ] != uint8_t(tcp_init_magic >> (6 * 8)) ||
-                           tcp_buf[6 ] != uint8_t(tcp_init_magic >> (5 * 8)) ||
-                           tcp_buf[7 ] != uint8_t(tcp_init_magic >> (4 * 8)) ||
-                           tcp_buf[8 ] != uint8_t(tcp_init_magic >> (3 * 8)) ||
-                           tcp_buf[9 ] != uint8_t(tcp_init_magic >> (2 * 8)) ||
-                           tcp_buf[10] != uint8_t(tcp_init_magic >> (1 * 8)) ||
-                           tcp_buf[11] != uint8_t(tcp_init_magic >> (0 * 8)))
+                       uint32_t expected_ack = htobe32(starting_ack);
+                       if (memcmp(tcp_buf + 8, &expected_ack, 4))
                                continue;
+                       if (nread < 36 + header_size) { continue; }
+                       // We're a server and just got a client...walk options until we find timestamps
+                       const unsigned char* opt_buf = tcp_buf + 20;
+                       bool found_magic = false;
+                       while (!found_magic && opt_buf < buf + nread) {
+                               switch (*opt_buf) {
+                                       case 1: opt_buf += 1; break;
+                                       case 2: opt_buf += 4; break;
+                                       case 3: opt_buf += 3; break;
+                                       // SACK should never appear
+                                       case 8:
+                                               if (opt_buf + 10 <= buf + nread) {
+                                                       if (!memcmp(opt_buf + 2, &timestamps_magic, 8)) {
+                                                               found_magic = true;
+                                                               break;
+                                                       }
+                                               }
+                                               // Fall through
+                                       default: opt_buf = buf + nread;
+                               }
+                       }
+                       if (!found_magic) continue;
 
                        fprintf(stderr, "Got SYN, sending SYNACK\n");
                        remote_port = (tcp_buf[0] << 8) | tcp_buf[1];
@@ -399,9 +425,9 @@ static void tcp_to_tun_queue_process() {
                        last_ack_recv = std::chrono::steady_clock::now();
 
                if (are_server && syn && !ack) {
-                       build_tcp_header(tcp_buf, 0, 0, 1, src, dest.sin_addr.s_addr);
+                       int len = build_tcp_header(tcp_buf, 0, 0, 1, src, dest.sin_addr.s_addr);
 
-                       ssize_t res = sendto(fdr, tcp_buf, 20 + 4, 0, (struct sockaddr*)&dest, sizeof(dest));
+                       ssize_t res = sendto(fdr, tcp_buf, len, 0, (struct sockaddr*)&dest, sizeof(dest));
                        if (res < 0) {
                                int err = errno;
                                fprintf(stderr, "Failed to send SYNACK with err %d (%s)\n", err, strerror(err));
@@ -511,10 +537,9 @@ int do_init() {
                local_port = local_port_tmp;
        }
 
-       uint32_t starting_ack = 0, starting_seq = 0;
-       memcpy(&starting_ack, &tcp_init_magic, 4);
-       memcpy(&starting_seq, ((const unsigned char*)&tcp_init_magic) + 4, 4);
        highest_recvd_seq = starting_ack;
+       uint32_t starting_seq;
+       assert(getrandom(&starting_seq, sizeof(starting_seq), 0) == sizeof(starting_seq));
        cur_seq = starting_seq;
 
        if (!pause_tun_read_reinit_tcp) { // Not doing a re-init
@@ -526,8 +551,8 @@ int do_init() {
 
        unsigned char buf[1500];
        if (!are_server) {
-               build_tcp_header(buf, 0, 1, 0, src, dest.sin_addr.s_addr);
-               ssize_t res = sendto(fdr, buf, 20 + 4, 0, (struct sockaddr*)&dest, sizeof(dest));
+               int len = build_tcp_header(buf, 0, 1, 0, src, dest.sin_addr.s_addr);
+               ssize_t res = sendto(fdr, buf, len, 0, (struct sockaddr*)&dest, sizeof(dest));
                if (res < 0) {
                        int err = errno;
                        fprintf(stderr, "Failed to send initial SYN with err %d (%s)\n", err, strerror(err));
@@ -544,8 +569,8 @@ int do_init() {
        if (!are_server) {
                fprintf(stderr, "Got SYNACK, sending ACK and starting tun listen\n");
 
-               build_tcp_header(buf, 0, 0, 0, src, dest.sin_addr.s_addr);
-               ssize_t res = sendto(fdr, buf, 20, 0, (struct sockaddr*)&dest, sizeof(dest));
+               int len = build_tcp_header(buf, 0, 0, 0, src, dest.sin_addr.s_addr);
+               ssize_t res = sendto(fdr, buf, len, 0, (struct sockaddr*)&dest, sizeof(dest));
                if (res < 0) {
                        int err = errno;
                        fprintf(stderr, "Failed to send initial ACK with err %d (%s)\n", err, strerror(err));
@@ -601,7 +626,9 @@ int main(int argc, char* argv[]) {
                remote_port = atoi(argv[4]);
        }
 
-       tcp_init_magic = atoll(argv[5]);
+       uint64_t tcp_init_magic = atoll(argv[5]);
+       starting_ack = tcp_init_magic >> 32;
+       timestamps_magic = htobe64(tcp_init_magic);
 
        src = inet_addr(argv[7]);