7#ifndef MIDDLEBOX_IPHELPER_HH
8#define MIDDLEBOX_IPHELPER_HH
10#include <click/config.h>
11#include <click/glue.hh>
12#include <clicknet/tcp.h>
13#include <clicknet/udp.h>
65 const int shift,
bool is_tcp);
70 const click_ip *iph = packet->
ip_header();
72 return ntohs(iph->ip_len);
77 return (((
const unsigned char *)packet->
ip_header()) - packet->
data());
82 click_ip *iph = packet->ip_header();
83 iph->ip_len = htons(length);
89 click_ip *iph = packet->ip_header();
92 unsigned hlen = iph->ip_hl << 2;
95 iph->ip_sum = click_in_cksum((
const unsigned char *)iph, hlen);
100 const click_ip *iph = packet->
ip_header();
102 return *(
const uint32_t*)&iph->ip_src;
107 const click_ip *iph = packet->
ip_header();
109 return *(
const uint32_t*)&iph->ip_dst;
114 const int shift,
bool is_tcp) {
115 assert(p->network_header());
116 assert(p->transport_header());
117 uint32_t old_hw, t_old_hw;
118 uint32_t new_hw, t_new_hw;
120 uint16_t *xip =
reinterpret_cast<uint16_t *
>(&p->ip_header()->ip_src);
121 old_hw = (uint32_t) xip[(shift * 2) + 0] + xip[(shift*2) + 1];
123 old_hw += (old_hw >> 16);
125 memcpy(&xip[shift*2], &ip, 4);
127 new_hw = (uint32_t) xip[(shift*2) + 0] + xip[(shift*2) + 1];
129 new_hw += (new_hw >> 16);
130 click_ip *iph = p->ip_header();
131 click_update_in_cksum(&iph->ip_sum, old_hw, new_hw);
133 uint16_t *xport =
reinterpret_cast<uint16_t *
>(&p->tcp_header()->th_sport);
134 t_old_hw += (uint32_t) xport[shift + 0];
135 t_old_hw += (t_old_hw >> 16);
136 xport[shift + 0] = port;
137 t_new_hw += (uint32_t) xport[shift + 0];
138 t_new_hw += (t_new_hw >> 16);
141 click_update_in_cksum(&p->tcp_header()->th_sum, t_old_hw, t_new_hw);
143 click_update_in_cksum(&p->udp_header()->uh_sum, t_old_hw, t_new_hw);
An IPv4 address.
Definition ipaddress.hh:16
This class provides several methods that can be used by elements that inherits from it in order to ma...
Definition iphelper.hh:23
void rewrite_ipport(WritablePacket *p, IPAddress ip, uint16_t port, const int shift, bool is_tcp)
Definition iphelper.hh:113
uint16_t getIPHeaderOffset(Packet *packet) const
Return the offset in the packet at which the IP header starts.
Definition iphelper.hh:75
void setPacketTotalLength(WritablePacket *packet, unsigned length) const
Set the length of the IP packet in the header.
Definition iphelper.hh:80
uint16_t packetTotalLength(Packet *packet) const
Return the length of the IP packet, obtained from the header.
Definition iphelper.hh:68
void computeIPChecksum(WritablePacket *packet) const
Recompute the IP checksum of the packet and set it in the IP header.
Definition iphelper.hh:87
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
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
A network packet believed not to be shared.
Definition packet.hh:1004