Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Un)StripTCPHeader #456

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions elements/tcpudp/striptcpheader.cc
Original file line number Diff line number Diff line change
@@ -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 <click/config.h>
#include <click/args.hh>
#include <click/error.hh>
#include <click/glue.hh>
#include <click/packet_anno.hh>
#include <clicknet/tcp.h>
#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)
43 changes: 43 additions & 0 deletions elements/tcpudp/striptcpheader.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// -*- mode: c++; c-basic-offset: 4 -*-
#ifndef CLICK_STRIPTCPHEADER_HH
#define CLICK_STRIPTCPHEADER_HH
#include <click/element.hh>
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
43 changes: 43 additions & 0 deletions elements/tcpudp/unstriptcpheader.cc
Original file line number Diff line number Diff line change
@@ -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 <click/config.h>
#include "unstriptcpheader.hh"
#include <clicknet/ip.h>
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)
34 changes: 34 additions & 0 deletions elements/tcpudp/unstriptcpheader.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef CLICK_UNSTRIPTCPHEADER_HH
#define CLICK_UNSTRIPTCPHEADER_HH
#include <click/element.hh>
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
39 changes: 39 additions & 0 deletions test/tcpudp/StripTCPHeader-01.testie
Original file line number Diff line number Diff line change
@@ -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