Click
Loading...
Searching...
No Matches
iphelper.hh
1/*
2 * IPHelper.hh - Provides several methods that can be used by elements to manage IP packets
3 *
4 * Romain Gaillard, Tom Barbette
5 */
6
7#ifndef MIDDLEBOX_IPHELPER_HH
8#define MIDDLEBOX_IPHELPER_HH
9
10#include <click/config.h>
11#include <click/glue.hh>
12#include <clicknet/tcp.h>
13#include <clicknet/udp.h>
14
15CLICK_DECLS
16
23{
24public:
29 inline uint16_t packetTotalLength(Packet* packet) const;
30
35 inline uint16_t getIPHeaderOffset(Packet* packet) const;
36
41 inline void setPacketTotalLength(WritablePacket* packet, unsigned length) const;
42
47 inline uint32_t getDestinationAddress(Packet* packet) const;
48
53 inline uint32_t getSourceAddress(Packet* packet) const;
54
58 inline void computeIPChecksum(WritablePacket* packet) const;
59
60
64 inline void rewrite_ipport(WritablePacket* p, IPAddress ip, uint16_t port,
65 const int shift, bool is_tcp);
66};
67
68inline uint16_t IPHelper::packetTotalLength(Packet *packet) const
69{
70 const click_ip *iph = packet->ip_header();
71
72 return ntohs(iph->ip_len);
73}
74
75inline uint16_t IPHelper::getIPHeaderOffset(Packet *packet) const
76{
77 return (((const unsigned char *)packet->ip_header()) - packet->data());
78}
79
80inline void IPHelper::setPacketTotalLength(WritablePacket* packet, unsigned length) const
81{
82 click_ip *iph = packet->ip_header();
83 iph->ip_len = htons(length);
84}
85
86
88{
89 click_ip *iph = packet->ip_header();
90
91 //unsigned plen = ntohs(iph->ip_len) - (iph->ip_hl << 2);
92 unsigned hlen = iph->ip_hl << 2;
93
94 iph->ip_sum = 0;
95 iph->ip_sum = click_in_cksum((const unsigned char *)iph, hlen);
96}
97
98inline uint32_t IPHelper::getSourceAddress(Packet* packet) const
99{
100 const click_ip *iph = packet->ip_header();
101
102 return *(const uint32_t*)&iph->ip_src;
103}
104
105inline uint32_t IPHelper::getDestinationAddress(Packet* packet) const
106{
107 const click_ip *iph = packet->ip_header();
108
109 return *(const uint32_t*)&iph->ip_dst;
110}
111
112inline void
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;
119
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];
122 t_old_hw = old_hw;
123 old_hw += (old_hw >> 16);
124
125 memcpy(&xip[shift*2], &ip, 4);
126
127 new_hw = (uint32_t) xip[(shift*2) + 0] + xip[(shift*2) + 1];
128 t_new_hw = new_hw;
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);
132
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);
139
140 if (is_tcp)
141 click_update_in_cksum(&p->tcp_header()->th_sum, t_old_hw, t_new_hw);
142 else
143 click_update_in_cksum(&p->udp_header()->uh_sum, t_old_hw, t_new_hw);
144}
145
146CLICK_ENDDECLS
147#endif
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