Click
Loading...
Searching...
No Matches
ipelement.hh
1/*
2 * ipelement.hh - Provides several methods that can be used by elements to manage IP packets
3 *
4 * Romain Gaillard.
5 */
6
7#ifndef MIDDLEBOX_IPELEMENT_HH
8#define MIDDLEBOX_IPELEMENT_HH
9
10#include <click/config.h>
11#include <click/glue.hh>
12#include <click/element.hh>
13
14CLICK_DECLS
15
22{
23public:
28 inline uint16_t packetTotalLength(Packet* packet) const;
29
34 inline uint16_t getIPHeaderOffset(Packet* packet) const;
35
40 inline void setPacketTotalLength(WritablePacket* packet, unsigned length) const;
41
46 inline const uint32_t getDestinationAddress(Packet* packet) const;
47
52 inline const uint32_t getSourceAddress(Packet* packet) const;
53
57 inline void computeIPChecksum(WritablePacket* packet) const;
58};
59
60inline uint16_t IPElement::packetTotalLength(Packet *packet) const
61{
62 const click_ip *iph = packet->ip_header();
63
64 return ntohs(iph->ip_len);
65}
66
67inline uint16_t IPElement::getIPHeaderOffset(Packet *packet) const
68{
69 return (((const unsigned char *)packet->ip_header()) - packet->data());
70}
71
72inline void IPElement::setPacketTotalLength(WritablePacket* packet, unsigned length) const
73{
74 click_ip *iph = packet->ip_header();
75 iph->ip_len = htons(length);
76}
77
78
80{
81 click_ip *iph = packet->ip_header();
82
83 //unsigned plen = ntohs(iph->ip_len) - (iph->ip_hl << 2);
84 unsigned hlen = iph->ip_hl << 2;
85
86 iph->ip_sum = 0;
87 iph->ip_sum = click_in_cksum((const unsigned char *)iph, hlen);
88}
89
90inline const uint32_t IPElement::getSourceAddress(Packet* packet) const
91{
92 const click_ip *iph = packet->ip_header();
93
94 return *(const uint32_t*)&iph->ip_src;
95}
96
97inline const uint32_t IPElement::getDestinationAddress(Packet* packet) const
98{
99 const click_ip *iph = packet->ip_header();
100
101 return *(const uint32_t*)&iph->ip_dst;
102}
103
104CLICK_ENDDECLS
105#endif
This class provides several methods that can be used by elements that inherits from it in order to ma...
Definition ipelement.hh:22
void computeIPChecksum(WritablePacket *packet) const
Recompute the IP checksum of the packet and set it in the IP header.
Definition ipelement.hh:79
uint16_t packetTotalLength(Packet *packet) const
Return the length of the IP packet, obtained from the header.
Definition ipelement.hh:60
const uint32_t getSourceAddress(Packet *packet) const
Return the IP source address of the packet.
Definition ipelement.hh:90
void setPacketTotalLength(WritablePacket *packet, unsigned length) const
Set the length of the IP packet in the header.
Definition ipelement.hh:72
uint16_t getIPHeaderOffset(Packet *packet) const
Return the offset in the packet at which the IP header starts.
Definition ipelement.hh:67
const uint32_t getDestinationAddress(Packet *packet) const
Return the IP destination address of the packet.
Definition ipelement.hh:97
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
Click's Element class.