From e05d4c40e4aa0942061e2cd7c29b1ae7b6a457a8 Mon Sep 17 00:00:00 2001 From: Tom Barbette Date: Tue, 21 Apr 2020 16:06:48 +0200 Subject: [PATCH] (Un)StripTCPHeader Which do what we expect it does --- elements/tcpudp/striptcpheader.cc | 43 ++++++++++++++++++++++++++++ elements/tcpudp/striptcpheader.hh | 43 ++++++++++++++++++++++++++++ elements/tcpudp/unstriptcpheader.cc | 43 ++++++++++++++++++++++++++++ elements/tcpudp/unstriptcpheader.hh | 34 ++++++++++++++++++++++ test/tcpudp/StripTCPHeader-01.testie | 39 +++++++++++++++++++++++++ 5 files changed, 202 insertions(+) create mode 100644 elements/tcpudp/striptcpheader.cc create mode 100644 elements/tcpudp/striptcpheader.hh create mode 100644 elements/tcpudp/unstriptcpheader.cc create mode 100644 elements/tcpudp/unstriptcpheader.hh create mode 100644 test/tcpudp/StripTCPHeader-01.testie diff --git a/elements/tcpudp/striptcpheader.cc b/elements/tcpudp/striptcpheader.cc new file mode 100644 index 0000000000..8c065903bd --- /dev/null +++ b/elements/tcpudp/striptcpheader.cc @@ -0,0 +1,43 @@ +// -*- mode: c++; c-basic-offset: 4 -*- +/* + * striptcpheader.{cc,hh} -- element strips TCP header from front of packet + * Tom Barbette + * + * Copyright (c) 2020 KTH Royal Institute of Technology + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, subject to the conditions + * listed in the Click LICENSE file. These conditions include: you must + * preserve this copyright notice, and you cannot mention the copyright + * holders in advertising related to the Software without their permission. + * The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This + * notice is a summary of the Click LICENSE file; the license in that file is + * legally binding. + */ + +#include +#include +#include +#include +#include +#include +#include "striptcpheader.hh" +CLICK_DECLS + +StripTCPHeader::StripTCPHeader() +{ +} + +Packet * +StripTCPHeader::simple_action(Packet *p) +{ + const click_tcp *th = p->tcp_header(); + unsigned n = th->th_off << 2; + p->pull(n); + return p; +} + +CLICK_ENDDECLS +EXPORT_ELEMENT(StripTCPHeader) +ELEMENT_MT_SAFE(StripTCPHeader) diff --git a/elements/tcpudp/striptcpheader.hh b/elements/tcpudp/striptcpheader.hh new file mode 100644 index 0000000000..087b71be77 --- /dev/null +++ b/elements/tcpudp/striptcpheader.hh @@ -0,0 +1,43 @@ +// -*- mode: c++; c-basic-offset: 4 -*- +#ifndef CLICK_STRIPTCPHEADER_HH +#define CLICK_STRIPTCPHEADER_HH +#include +CLICK_DECLS + +/* + * =c + * StripTCPHeader() + * + * =s tcp + * Strip the TCP header from the front of packets + * + * =d + * Removes all bytes from the beginning of the packet up to the end of the TCP + * header. + * + * =e + * Use this to get rid of all headers up to the end of the TCP layer, check if + * the first bytes are "GET", and + * set back the pointer to the beginning of the IP layer: + * + * StripTCPHeader() + * -> c :: Classifier(0/474552,-) + * -> UnstripIPHeader() + * + * + * =a Strip, StripIPHeader, UnstripTCPHeader + */ + +class StripTCPHeader : public Element { public: + + StripTCPHeader() CLICK_COLD; + + const char *class_name() const { return "StripTCPHeader"; } + const char *port_count() const { return PORTS_1_1; } + + Packet *simple_action(Packet *); + +}; + +CLICK_ENDDECLS +#endif diff --git a/elements/tcpudp/unstriptcpheader.cc b/elements/tcpudp/unstriptcpheader.cc new file mode 100644 index 0000000000..b43bca6660 --- /dev/null +++ b/elements/tcpudp/unstriptcpheader.cc @@ -0,0 +1,43 @@ +/* + * unstriptcpheader.{cc,hh} -- put TCP header back based on annotation + * Tom Barbette, based on Benjie Chen's UnstripIPHeader + * + * Copyright (c) 2020 KTH Royal Institute of Technology + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, subject to the conditions + * listed in the Click LICENSE file. These conditions include: you must + * preserve this copyright notice, and you cannot mention the copyright + * holders in advertising related to the Software without their permission. + * The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This + * notice is a summary of the Click LICENSE file; the license in that file is + * legally binding. + */ + +#include +#include "unstriptcpheader.hh" +#include +CLICK_DECLS + +UnstripTCPHeader::UnstripTCPHeader() +{ +} + +UnstripTCPHeader::~UnstripTCPHeader() +{ +} + +Packet * +UnstripTCPHeader::simple_action(Packet *p) +{ + assert(p->tcp_header()); + ptrdiff_t offset = (unsigned char*)p->tcp_header() - p->data(); + if (offset < 0) + p = p->push(-offset); // should never create a new packet + return p; +} + +CLICK_ENDDECLS +EXPORT_ELEMENT(UnstripTCPHeader) +ELEMENT_MT_SAFE(UnstripTCPHeader) diff --git a/elements/tcpudp/unstriptcpheader.hh b/elements/tcpudp/unstriptcpheader.hh new file mode 100644 index 0000000000..1849ad52cd --- /dev/null +++ b/elements/tcpudp/unstriptcpheader.hh @@ -0,0 +1,34 @@ +#ifndef CLICK_UNSTRIPTCPHEADER_HH +#define CLICK_UNSTRIPTCPHEADER_HH +#include +CLICK_DECLS + +/* + * =c + * UnstripTCPHeader() + * + * =s tcp + * + * restores outermost TCP header + * =d + * + * Put outermost TCP header back onto a stripped packet, based on the TCP + * Header annotation from MarkTCPHeader or CheckTCPHeader. If TCP header + * already on,forwards packet unmodified. + * + * =a StripTCPHeader, CheckIPHeader, MarkIPHeader, StripIPHeader */ + +class UnstripTCPHeader : public Element { public: + + UnstripTCPHeader() CLICK_COLD; + ~UnstripTCPHeader() CLICK_COLD; + + const char *class_name() const { return "UnstripTCPHeader"; } + const char *port_count() const { return PORTS_1_1; } + + Packet *simple_action(Packet *); + +}; + +CLICK_ENDDECLS +#endif diff --git a/test/tcpudp/StripTCPHeader-01.testie b/test/tcpudp/StripTCPHeader-01.testie new file mode 100644 index 0000000000..56c4969081 --- /dev/null +++ b/test/tcpudp/StripTCPHeader-01.testie @@ -0,0 +1,39 @@ +%info + +TCPRewriter and FTPPortMapper sequence number translation, even for SACK. + +%script +$VALGRIND click -e " +FromIPSummaryDump(IN1, STOP true, CHECKSUM true) + -> CheckIPHeader(VERBOSE true) + -> CheckTCPHeader(VERBOSE true) + -> StripIPHeader + -> StripTCPHeader + -> Print(PAY) + -> StoreData(OFFSET 0, DATA "ABC") + -> UnstripIPHeader() + -> Print(FULLTCP) + -> ToIPSummaryDump(OUT1, FIELDS src sport dst dport proto tcp_seq tcp_ack payload tcp_opt) +" + +%file IN1 +!data src sport dst dport proto tcp_seq tcp_ack payload tcp_opt +200.200.200.200 30 2.0.0.2 21 T 0 0 "XYZ" . +200.200.200.200 30 2.0.0.2 21 T 30 0 "XYZ" . +2.0.0.2 21 1.0.0.1 1024 T 0 0 "XYZ" sack1-10;sack1-18;sack18-20 + +%expect OUT1 +!IPSummaryDump 1.3 +!data ip_src sport ip_dst dport ip_proto tcp_seq tcp_ack payload tcp_opt +200.200.200.200 30 2.0.0.2 21 T 0 0 "ABC" . +200.200.200.200 30 2.0.0.2 21 T 30 0 "ABC" . +2.0.0.2 21 1.0.0.1 1024 T 0 0 "ABC" sack1-10;sack1-18;sack18-20 + + +%expect stderr +PAY: 3 | 58595a +FULLTCP: 43 | 4500002b 00000000 6406c33a c8c8c8c8 02000002 001e0015 +PAY: 3 | 58595a +FULLTCP: 43 | 4500002b 00000000 6406c33a c8c8c8c8 02000002 001e0015 +PAY: 3 | 58595a +FULLTCP: 75 | 4500004b 00000000 640653ab 02000002 01000001 00150400