7#ifndef MIDDLEBOX_TCPHELPER_HH
8#define MIDDLEBOX_TCPHELPER_HH
10#include <click/config.h>
11#include <click/glue.hh>
12#include <clicknet/tcp.h>
13#include <clicknet/ip.h>
15#include <click/ipflowid.hh>
20#include <click/dpdkdevice.hh>
27#if RTE_VERSION >= RTE_VERSION_NUM(22,07,0,0)
28#define PKT_TX_TCP_CKSUM RTE_MBUF_F_TX_TCP_CKSUM
29#define PKT_TX_IP_CKSUM RTE_MBUF_F_TX_IP_CKSUM
30#define PKT_TX_IPV4 RTE_MBUF_F_TX_IPV4
57 uint8_t flag = TH_RST;
78 tcp_seq_t seq, tcp_seq_t ack, uint16_t winSize, uint8_t flags,
79 uint32_t contentSize = 0)
const;
251 static int iterateOptions(
Packet *packet, std::function<
bool(uint8_t,
void*)> fnt);
253 static void printPacket(
Packet* p);
256inline void TCPHelper::printPacket(
Packet* p) {
288 click_ip *iph = packet->ip_header();
289 click_tcp *tcph = packet->tcp_header();
291 unsigned plen = ntohs(iph->ip_len) - (iph->ip_hl << 2);
293 unsigned csum = click_in_cksum((
unsigned char *)tcph, plen);
294 tcph->th_sum = click_in_cksum_pseudohdr(csum, iph, plen);
299 click_ip *iph = packet->ip_header();
300 click_tcp *tcph = packet->tcp_header();
305 if (!DPDKDevice::is_dpdk_buffer(packet))
307 click_chatter(
"Not a DPDK buffer. For max performance, arrange TCP element to always work on DPDK buffers");
316# if !CLICK_PACKET_USE_DPDK
317 rte_mbuf* mbuf = (
struct rte_mbuf *) packet->destructor_argument();
319 rte_mbuf* mbuf = (
struct rte_mbuf *) packet;
323 mbuf->l4_len = tcph->th_off << 2;
324 mbuf->ol_flags |= PKT_TX_TCP_CKSUM | PKT_TX_IP_CKSUM | PKT_TX_IPV4;
325 tcph->th_sum = rte_ipv4_phdr_cksum((
const struct rte_ipv4_hdr *)iph, mbuf->ol_flags);
331 click_tcp *tcph = packet->tcp_header();
333 tcph->th_seq = htonl(seq);
340 return ntohl(tcph->th_seq);
347 return ntohl(tcph->th_ack);
352 click_tcp *tcph = packet->tcp_header();
354 tcph->th_ack = htonl(ack);
361 return ntohs(tcph->th_win);
366 click_tcp *tcph = packet->tcp_header();
368 tcph->th_win = htons(winSize);
373 const click_ip *iph = packet->
ip_header();
374 unsigned iph_len = iph->ip_hl << 2;
375 uint16_t ip_len = ntohs(iph->ip_len);
378 unsigned tcp_offset = tcph->th_off << 2;
380 return ip_len - iph_len - tcp_offset;
385 click_tcp *tcph = packet->tcp_header();
388 unsigned tcph_len = tcph->th_off << 2;
389 return (
unsigned char*)packet->transport_header() + tcph_len;
397 unsigned tcph_len = tcph->th_off << 2;
402 uint32_t length)
const
404 click_tcp *tcph = packet->tcp_header();
407 unsigned tcph_len = tcph->th_off << 2;
409 unsigned char* payloadPtr = (
unsigned char*)packet->transport_header() + tcph_len;
410 memcpy(payloadPtr, payload, length);
418 unsigned tcph_len = tcph->th_off << 2;
429 return ntohs(tcph->th_sport);
436 return ntohs(tcph->th_dport);
441 return packet->
ip_header()->ip_p == IP_PROTO_TCP;
467 uint8_t flags = tcph->th_flags;
479 return tcph->th_flags;
485 uint8_t flags = tcph->th_flags;
492 if(flags == TH_ACK || (or_fin && flags == (TH_ACK | TH_FIN)))
This class provides several methods that can be used by elements that inherits from it in order to ma...
Definition iphelper.hh:23
uint32_t getSourceAddress(Packet *packet) const
Return the IP source address of the packet.
Definition iphelper.hh:98
uint32_t getDestinationAddress(Packet *packet) const
Return the IP destination address of the packet.
Definition iphelper.hh:105
A network packet.
Definition packet.hh:68
uint32_t mac_header_length() const
Return the MAC header length.
Definition packet.hh:2556
const click_tcp * tcp_header() const
Return the packet's transport header pointer as TCP.
Definition packet.hh:1553
const click_ip * ip_header() const
Return the packet's network header pointer as IPv4.
Definition packet.hh:1523
const unsigned char * data() const
Return the packet's data pointer.
Definition packet.hh:1207
const unsigned char * transport_header() const
Return the packet's transport header pointer.
Definition packet.hh:1495
const Timestamp & timestamp_anno() const
Return the timestamp annotation.
Definition packet.hh:1596
uint32_t network_header_length() const
Return the network header length.
Definition packet.hh:2577
A string of characters.
Definition string.hh:22
const char * c_str() const
Null-terminate the string.
Definition string.hh:519
This class provides several methods that can be used by elements that inherits from it to manage TCP ...
Definition tcphelper.hh:44
static bool isAck(Packet *packet)
Indicate whether a packet is an ACK packet.
Definition tcphelper.hh:459
static bool isTCP(Packet *packet)
Indicate whether a packet is a TCP packet.
Definition tcphelper.hh:439
tcp_seq_t getNextSequenceNumber(Packet *packet) const
Return the sequence number of the packet that will be received after the given one.
Definition tcphelper.hh:272
unsigned char * getPayload(WritablePacket *packet) const
Return the payload of a TCP packet.
Definition tcphelper.hh:383
void setAckNumber(WritablePacket *packet, tcp_seq_t ack) const
Set the ack number of a TCP packet.
Definition tcphelper.hh:350
static tcp_seq_t getAckNumber(Packet *packet)
Return the ack number of a packet.
Definition tcphelper.hh:343
uint16_t getPayloadOffset(Packet *packet) const
Return the offset of the payload in a TCP packet.
Definition tcphelper.hh:413
static bool checkFlag(Packet *packet, uint8_t flag)
Check if a given flag is set in a packet.
Definition tcphelper.hh:464
static bool isSyn(Packet *packet)
Indicate whether a packet is a SYN packet.
Definition tcphelper.hh:444
void setSequenceNumber(WritablePacket *packet, tcp_seq_t seq) const
Set the sequence number of a TCP packet.
Definition tcphelper.hh:329
static tcp_seq_t getSequenceNumber(Packet *packet)
Return the sequence number of a packet.
Definition tcphelper.hh:336
uint8_t getFlags(Packet *packet) const
Return the flags of a TCP packet.
Definition tcphelper.hh:476
uint16_t getWindowSize(Packet *packet) const
Return the window size set in the header of a packet.
Definition tcphelper.hh:357
uint16_t getSourcePort(Packet *packet) const
Return the source port of a packet.
Definition tcphelper.hh:425
static void resetTCPChecksum(WritablePacket *packet)
Reset the TCP checksum of a packet and set it in its header.
Definition tcphelper.hh:297
static bool isRst(Packet *packet)
Indicate whether a packet is a RST packet.
Definition tcphelper.hh:454
uint16_t getDestinationPort(Packet *packet) const
Return the destination port of a packet.
Definition tcphelper.hh:432
static unsigned getPayloadLength(Packet *packet)
Return the length of the TCP payload of a packet.
Definition tcphelper.hh:371
void setWindowSize(WritablePacket *packet, uint16_t winSize) const
Set the window size in the TCP header of a packet.
Definition tcphelper.hh:364
WritablePacket * forgePacket(uint32_t saddr, uint32_t daddr, uint16_t sport, uint16_t dport, tcp_seq_t seq, tcp_seq_t ack, uint16_t winSize, uint8_t flags, uint32_t contentSize=0) const
Create a TCP packet.
Definition tcphelper.cc:33
static bool isJustAnAck(Packet *packet, const bool or_fin=false)
Indicate whether the packet is just an ACK without any additional information.
Definition tcphelper.hh:482
void setPayload(WritablePacket *packet, const unsigned char *payload, uint32_t length) const
Set the payload of a TCP packet.
Definition tcphelper.hh:401
const unsigned char * getPayloadConst(Packet *packet) const
Return the payload of a TCP packet.
Definition tcphelper.hh:392
static void computeTCPChecksum(WritablePacket *packet)
Compute the TCP checksum of a packet and set it in its header.
Definition tcphelper.hh:286
static bool isFin(Packet *packet)
Indicate whether a packet is a FIN packet.
Definition tcphelper.hh:449
String unparse() const
Unparse this timestamp into a String.
Definition timestamp.cc:240
A network packet believed not to be shared.
Definition packet.hh:1004