From 8e81c01b2de70c5e7e1a73d25ebbece0a95bd2c8 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sat, 7 Feb 2015 01:28:57 +0100 Subject: [PATCH] Make Reorderer into a Protocol. --- greprotocol.cpp | 8 ++++---- greprotocol.h | 4 ++-- protocol.h | 2 +- reorderer.cpp | 8 ++++---- reorderer.h | 10 ++++++---- tunprotocol.cpp | 4 ++-- tunprotocol.h | 2 +- 7 files changed, 20 insertions(+), 18 deletions(-) diff --git a/greprotocol.cpp b/greprotocol.cpp index b826a52..4a63415 100644 --- a/greprotocol.cpp +++ b/greprotocol.cpp @@ -44,7 +44,7 @@ GREProtocol::GREProtocol(const in6_addr &src, const in6_addr &dst) } } -void GREProtocol::send_packet(uint16_t proto, const string &data) +void GREProtocol::send_packet(uint16_t proto, const string &data, int incoming_seq) { char buf[4096]; gre_header *gre = (gre_header *)buf; @@ -55,7 +55,7 @@ void GREProtocol::send_packet(uint16_t proto, const string &data) gre->protocol_type = htons(proto); char *ptr = buf + sizeof(*gre); - int seq_be = htonl(seq++); + int seq_be = htonl(seq++); // Ignore incoming_seq. memcpy(ptr, &seq_be, sizeof(seq_be)); ptr += sizeof(seq_be); @@ -72,7 +72,7 @@ int GREProtocol::fd() const return sock; } -void GREProtocol::read_packet(Reorderer *sender) +void GREProtocol::read_packet(Protocol *sender) { struct sockaddr_storage addr; socklen_t addrlen = sizeof(addr); @@ -107,6 +107,6 @@ void GREProtocol::read_packet(Reorderer *sender) //printf("gre packet: proto=%x\n", ntohs(gre->protocol_type)); - sender->handle_packet(ntohs(gre->protocol_type), string(ptr, buf + ret), seq); + sender->send_packet(ntohs(gre->protocol_type), string(ptr, buf + ret), seq); } diff --git a/greprotocol.h b/greprotocol.h index 6930f20..6215534 100644 --- a/greprotocol.h +++ b/greprotocol.h @@ -12,9 +12,9 @@ class Reorderer; class GREProtocol : public Protocol { public: GREProtocol(const in6_addr &myaddr, const in6_addr &dst); - virtual void send_packet(uint16_t proto, const std::string &data); + virtual void send_packet(uint16_t proto, const std::string &data, int incoming_seq); virtual int fd() const; - void read_packet(Reorderer* sender); + void read_packet(Protocol* sender); private: int seq; diff --git a/protocol.h b/protocol.h index a6d3309..8b06f0a 100644 --- a/protocol.h +++ b/protocol.h @@ -6,7 +6,7 @@ class Protocol { public: - virtual void send_packet(uint16_t proto, const std::string &data) = 0; + virtual void send_packet(uint16_t proto, const std::string &data, int incoming_seq) = 0; virtual int fd() const = 0; }; diff --git a/reorderer.cpp b/reorderer.cpp index fe3b7b6..b6ac6a9 100644 --- a/reorderer.cpp +++ b/reorderer.cpp @@ -12,7 +12,7 @@ Reorderer::Reorderer(Protocol* sender) { } -void Reorderer::handle_packet(uint16_t proto, const string& data, int seq) +void Reorderer::send_packet(uint16_t proto, const string& data, int seq) { bool silence = false; if (packet_buffer.size() >= PACKET_BUFFER_SIZE) { @@ -43,7 +43,8 @@ void Reorderer::handle_packet(uint16_t proto, const string& data, int seq) // printf("seq=%d\n", front_seq); //} const string &data = packet_buffer.top().data; - send_packet(packet_buffer.top().proto, data, silence); + check_ts_discontinuity(packet_buffer.top().proto, data, silence); + sender->send_packet(proto, data, seq); packet_buffer.pop(); last_seq = front_seq; if (!silence && !packet_buffer.empty()) { @@ -53,7 +54,7 @@ void Reorderer::handle_packet(uint16_t proto, const string& data, int seq) } } -void Reorderer::send_packet(uint16_t proto, const string &data, bool silence) +void Reorderer::check_ts_discontinuity(uint16_t proto, const string &data, bool silence) { if (data.size() == 1344) { for (int i = 0; i < 7; ++i) { @@ -74,6 +75,5 @@ void Reorderer::send_packet(uint16_t proto, const string &data, bool silence) } } } - sender->send_packet(proto, data); } diff --git a/reorderer.h b/reorderer.h index 476093f..393e327 100644 --- a/reorderer.h +++ b/reorderer.h @@ -1,6 +1,7 @@ #ifndef _REORDERER_H #define _REORDERER_H 1 +#include #include #include @@ -9,7 +10,7 @@ #include #include -class Protocol; +#include "protocol.h" struct GREPacket { int seq; @@ -21,13 +22,14 @@ struct GREPacket { } }; -class Reorderer { +class Reorderer : public Protocol { public: Reorderer(Protocol* sender); - void handle_packet(uint16_t proto, const std::string& data, int seq); + void send_packet(uint16_t proto, const std::string& data, int seq); + virtual int fd() const { assert(false); } private: - void send_packet(uint16_t proto, const std::string &data, bool silence); + void check_ts_discontinuity(uint16_t proto, const std::string &data, bool silence); Protocol* sender; int last_seq; diff --git a/tunprotocol.cpp b/tunprotocol.cpp index b8b63a3..cc10651 100644 --- a/tunprotocol.cpp +++ b/tunprotocol.cpp @@ -41,7 +41,7 @@ TUNProtocol::TUNProtocol(const char *devname) : tunfd(tun_open(devname)) { } -void TUNProtocol::send_packet(uint16_t proto, const string &data) +void TUNProtocol::send_packet(uint16_t proto, const string &data, int incoming_seq) { char buf[4096]; @@ -88,6 +88,6 @@ void TUNProtocol::read_packet(Protocol *sender) ptr += 2; //fprintf(stderr, "tun packet: flags=%x proto=%x len=%d\n", // flags, proto, ret - 4); - sender->send_packet(proto, string(ptr, buf + ret)); + sender->send_packet(proto, string(ptr, buf + ret), -1); } diff --git a/tunprotocol.h b/tunprotocol.h index cfe3b46..9af2e78 100644 --- a/tunprotocol.h +++ b/tunprotocol.h @@ -9,7 +9,7 @@ class TUNProtocol : public Protocol { public: TUNProtocol(const char *devname); - virtual void send_packet(uint16_t proto, const std::string &data); + virtual void send_packet(uint16_t proto, const std::string &data, int incoming_seq); virtual int fd() const; void read_packet(Protocol* sender); -- 2.39.2