ROHC compression/decompression library
Data Structures | Enumerations | Functions
ip.h File Reference

IP-agnostic packet. More...

#include "protocols/ipv4.h"
#include "protocols/ipv6.h"
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
Include dependency graph for ip.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  net_hdr
 
struct  ip_packet
 Defines an IP-agnostic packet that can handle an IPv4 or IPv6 packet. More...
 

Enumerations

enum  ip_header_pos_t { ROHC_IP_HDR_NONE = 0, ROHC_IP_HDR_FIRST = 1, ROHC_IP_HDR_SECOND = 2 }
 
enum  ip_version {
  IPV4 = 4, IPV6 = 6, IP_UNKNOWN = 0, IPV4_MALFORMED = 1,
  IPV6_MALFORMED = 2
}
 IP version. More...
 

Functions

static uint16_t swab16 (const uint16_t value)
 In-place change the byte order in a two-byte value. More...
 
static uint16_t from32to16 (const uint32_t x)
 
static uint16_t ip_fast_csum (const uint8_t *const iph, const size_t ihl)
 
void ip_create (struct ip_packet *const ip, const uint8_t *const packet, const size_t size)
 Create an IP packet from raw data. More...
 
void ip_get_inner_packet (const struct ip_packet *const outer, struct ip_packet *const inner)
 Get the inner IP packet (IP in IP) More...
 
const uint8_t * ip_get_raw_data (const struct ip_packet *const ip)
 Get the IP raw data (header + payload) More...
 
uint8_t * ip_get_next_header (const struct ip_packet *const ip, uint8_t *const type)
 Get the IP next header. More...
 
uint8_t * ip_get_next_layer (const struct ip_packet *const ip)
 Get the next header (but skip IP extensions) More...
 
uint8_t * ip_get_next_ext_from_ip (const struct ip_packet *const ip, uint8_t *const type)
 Get the next extension header of IPv6 packets from an IPv6 header. More...
 
uint8_t * ip_get_next_ext_from_ext (const uint8_t *const ext, uint8_t *const type)
 Get the next extension header of IPv6 packets from another extension. More...
 
unsigned int ip_get_totlen (const struct ip_packet *const ip)
 Get the total length of an IP packet. More...
 
unsigned int ip_get_hdrlen (const struct ip_packet *const ip)
 Get the length of an IP header. More...
 
bool ip_is_fragment (const struct ip_packet *const ip)
 Whether the IP packet is an IP fragment or not. More...
 
static ip_version ip_get_version (const struct ip_packet *const ip)
 Get the IP version of an IP packet. More...
 
uint8_t ip_get_protocol (const struct ip_packet *const ip)
 Get the protocol transported by an IP packet. More...
 
unsigned int ip_get_tos (const struct ip_packet *const ip)
 Get the IPv4 Type Of Service (TOS) or IPv6 Traffic Class (TC) of an IP packet. More...
 
unsigned int ip_get_ttl (const struct ip_packet *const ip)
 Get the IPv4 Time To Live (TTL) or IPv6 Hop Limit (HL) of an IP packet. More...
 
void ip_set_version (struct ip_packet *const ip, const ip_version value)
 Set the IP version of an IP packet. More...
 
void ip_set_protocol (struct ip_packet *const ip, const uint8_t value)
 Set the protocol transported by an IP packet. More...
 
void ip_set_tos (struct ip_packet *const ip, const uint8_t value)
 Set the IPv4 Type Of Service (TOS) or IPv6 Traffic Class (TC) of an IP packet. More...
 
void ip_set_ttl (struct ip_packet *const ip, const uint8_t value)
 Set the IPv4 Time To Live (TTL) or IPv6 Hop Limit (HL) of an IP packet. More...
 
void ip_set_saddr (struct ip_packet *const ip, const uint8_t *value)
 Set the Source Address of an IP packet. More...
 
void ip_set_daddr (struct ip_packet *const ip, const uint8_t *value)
 Set the Destination Address of an IP packet. More...
 
static size_t ipv4_get_hdrlen (const struct ip_packet *const ip)
 
const struct ipv4_hdripv4_get_header (const struct ip_packet *const ip)
 Get the IPv4 header. More...
 
uint16_t ipv4_get_id (const struct ip_packet *const ip)
 Get the IP-ID of an IPv4 packet. More...
 
uint16_t ipv4_get_id_nbo (const struct ip_packet *const ip, const unsigned int nbo)
 Get the IP-ID of an IPv4 packet in Network Byte Order. More...
 
int ipv4_get_df (const struct ip_packet *const ip)
 Get the Don't Fragment (DF) bit of an IPv4 packet. More...
 
uint32_t ipv4_get_saddr (const struct ip_packet *const ip)
 Get the source address of an IPv4 packet. More...
 
uint32_t ipv4_get_daddr (const struct ip_packet *const ip)
 Get the destination address of an IPv4 packet. More...
 
void ipv4_set_id (struct ip_packet *const ip, const int value)
 Set the IP-ID of an IPv4 packet. More...
 
void ipv4_set_df (struct ip_packet *const ip, const int value)
 Set the Don't Fragment (DF) bit of an IPv4 packet. More...
 
static size_t ipv6_get_hdrlen (const struct ip_packet *const ip)
 
const struct ipv6_hdripv6_get_header (const struct ip_packet *const ip)
 Get the IPv6 header. More...
 
uint32_t ip_get_flow_label (const struct ip_packet *const ip)
 Get the flow label of an IPv6 packet. More...
 
const struct ipv6_addripv6_get_saddr (const struct ip_packet *const ip)
 Get the source address of an IPv6 packet. More...
 
const struct ipv6_addripv6_get_daddr (const struct ip_packet *const ip)
 Get the destination address of an IPv6 packet. More...
 
void ip_set_flow_label (struct ip_packet *const ip, const uint32_t value)
 Set the flow label of an IPv6 packet. More...
 
unsigned short ip_get_extension_size (const uint8_t *const ext)
 Get the size of an IPv6 extension. More...
 
unsigned short ip_get_total_extension_size (const struct ip_packet *const ip)
 Get the size of the extension list. More...
 

Detailed Description

IP-agnostic packet.

Author
Didier Barvaux didie.nosp@m.r.ba.nosp@m.rvaux.nosp@m.@tou.nosp@m.louse.nosp@m..viv.nosp@m.eris..nosp@m.com

Enumeration Type Documentation

◆ ip_header_pos_t

The selected IP header

Enumerator
ROHC_IP_HDR_NONE 

No IP header selected

ROHC_IP_HDR_FIRST 

The first IP header is selected

ROHC_IP_HDR_SECOND 

The second IP header is selected

◆ ip_version

enum ip_version

IP version.

Enumerator
IPV4 

IP version 4.

IPV6 

IP version 6.

IP_UNKNOWN 

not IP

IPV4_MALFORMED 

IP version 4 (malformed)

IPV6_MALFORMED 

IP version 6 (malformed)

Function Documentation

◆ from32to16()

static uint16_t from32to16 ( const uint32_t  x)
inlinestatic

◆ ip_create()

void ip_create ( struct ip_packet *const  ip,
const uint8_t *const  packet,
const size_t  size 
)

Create an IP packet from raw data.

Parameters
ipOUT: The IP packet to create
packetThe IP packet data
sizeThe length of the IP packet data

◆ ip_fast_csum()

static uint16_t ip_fast_csum ( const uint8_t *const  iph,
const size_t  ihl 
)
inlinestatic

This is a version of ip_compute_csum() optimized for IP headers, which always checksum on 4 octet boundaries.

◆ ip_get_extension_size()

unsigned short ip_get_extension_size ( const uint8_t *const  ext)

Get the size of an IPv6 extension.

Parameters
extThe extension
Returns
The size of the extension

◆ ip_get_flow_label()

uint32_t ip_get_flow_label ( const struct ip_packet *const  ip)

Get the flow label of an IPv6 packet.

The function does not handle ip_packet whose ip_packet::version is not IPV6.

Parameters
ipThe IPv6 packet to analyze
Returns
The flow label of the given IPv6 packet

◆ ip_get_hdrlen()

unsigned int ip_get_hdrlen ( const struct ip_packet *const  ip)

Get the length of an IP header.

The function does not handle ip_packet whose ip_packet::version is IP_UNKNOWN.

Parameters
ipThe IP packet to analyze
Returns
The length of the IP header if successful, 0 otherwise

◆ ip_get_inner_packet()

void ip_get_inner_packet ( const struct ip_packet *const  outer,
struct ip_packet *const  inner 
)

Get the inner IP packet (IP in IP)

The function does not handle ip_packet whose ip_packet::version is IP_UNKNOWN.

Parameters
outerThe outer IP packet to analyze
innerThe inner IP packet to create

◆ ip_get_next_ext_from_ext()

uint8_t* ip_get_next_ext_from_ext ( const uint8_t *const  ext,
uint8_t *const  type 
)

Get the next extension header of IPv6 packets from another extension.

Parameters
extThe extension to analyse
typeOUT: The type of the next header If there is no next header the value must be ignored
Returns
The next extension header, NULL if there is no more extension

◆ ip_get_next_ext_from_ip()

uint8_t* ip_get_next_ext_from_ip ( const struct ip_packet *const  ip,
uint8_t *const  type 
)

Get the next extension header of IPv6 packets from an IPv6 header.

The function does not handle ip_packet whose ip_packet::version is IP_UNKNOWN.

Parameters
ipThe IP packet to analyze
typeOUT: The type of the next header If there is no next header the value must be ignored
Returns
The next extension header, NULL if there is no extension

◆ ip_get_next_header()

uint8_t* ip_get_next_header ( const struct ip_packet *const  ip,
uint8_t *const  type 
)

Get the IP next header.

The function does not handle ip_packet whose ip_packet::version is IP_UNKNOWN.

Parameters
ipThe IP packet to analyze
typeOUT: The type of the next header
Returns
The next header if successful, NULL otherwise

◆ ip_get_next_layer()

uint8_t* ip_get_next_layer ( const struct ip_packet *const  ip)

Get the next header (but skip IP extensions)

The function does not handle ip_packet whose ip_packet::version is IP_UNKNOWN.

Parameters
ipThe IP packet to analyze
Returns
The next header that is not an IP extension if there is one, NULL if there is none

◆ ip_get_protocol()

uint8_t ip_get_protocol ( const struct ip_packet *const  ip)

Get the protocol transported by an IP packet.

The protocol returned is the one transported by the last known IP extension header if any is found.

The function handles ip_packet whose ip_packet::version is IP_UNKNOWN. It always returns the special value 0.

Parameters
ipThe IP packet to analyze
Returns
The protocol number that identify the protocol transported by the given IP packet, 0 if the packet is not IPv4 nor IPv6

◆ ip_get_raw_data()

const uint8_t* ip_get_raw_data ( const struct ip_packet *const  ip)

Get the IP raw data (header + payload)

The function handles ip_packet whose ip_packet::version is IP_UNKNOWN.

Parameters
ipThe IP packet to analyze
Returns
The IP raw data (header + payload)

◆ ip_get_tos()

unsigned int ip_get_tos ( const struct ip_packet *const  ip)

Get the IPv4 Type Of Service (TOS) or IPv6 Traffic Class (TC) of an IP packet.

The function does not handle ip_packet whose ip_packet::version is IP_UNKNOWN.

Parameters
ipThe IP packet to analyze
Returns
The TOS or TC value if successful, 0 otherwise

◆ ip_get_total_extension_size()

unsigned short ip_get_total_extension_size ( const struct ip_packet *const  ip)

Get the size of the extension list.

The function does not handle ip_packet whose ip_packet::version is IP_UNKNOWN.

Parameters
ipThe packet to analyse
Returns
The size of extension list

◆ ip_get_totlen()

unsigned int ip_get_totlen ( const struct ip_packet *const  ip)

Get the total length of an IP packet.

The function handles ip_packet whose ip_packet::version is IP_UNKNOWN.

Parameters
ipThe IP packet to analyze
Returns
The total length of the IP packet

◆ ip_get_ttl()

unsigned int ip_get_ttl ( const struct ip_packet *const  ip)

Get the IPv4 Time To Live (TTL) or IPv6 Hop Limit (HL) of an IP packet.

The function does not handle ip_packet whose ip_packet::version is IP_UNKNOWN.

Parameters
ipThe IP packet to analyze
Returns
The TTL or HL value if successful, 0 otherwise

◆ ip_get_version()

static ip_version ip_get_version ( const struct ip_packet *const  ip)
inlinestatic

Get the IP version of an IP packet.

The function handles ip_packet whose ip_packet::version is IP_UNKNOWN.

Parameters
ipThe IP packet to analyze
Returns
The version of the IP packet

◆ ip_is_fragment()

bool ip_is_fragment ( const struct ip_packet *const  ip)

Whether the IP packet is an IP fragment or not.

The IP packet is a fragment if the MF (More Fragments) bit is set or the Fragment Offset field is non-zero.

The function does not handle ip_packet whose ip_packet::version is IP_UNKNOWN.

Parameters
ipThe IP packet to analyze
Returns
Whether the IP packet is an IP fragment or not

◆ ip_set_daddr()

void ip_set_daddr ( struct ip_packet *const  ip,
const uint8_t *  value 
)

Set the Destination Address of an IP packet.

The function does not handle ip_packet whose ip_packet::version is IP_UNKNOWN.

Parameters
ipThe IP packet to modify
valueThe IP address value

◆ ip_set_flow_label()

void ip_set_flow_label ( struct ip_packet *const  ip,
const uint32_t  value 
)

Set the flow label of an IPv6 packet.

The function does not handle ip_packet whose ip_packet::version is not IPV6.

Parameters
ipThe IPv6 packet to modify
valueThe flow label value

◆ ip_set_protocol()

void ip_set_protocol ( struct ip_packet *const  ip,
const uint8_t  value 
)

Set the protocol transported by an IP packet.

The function does not handle ip_packet whose ip_packet::version is IP_UNKNOWN.

Parameters
ipThe IP packet to modify
valueThe protocol value

◆ ip_set_saddr()

void ip_set_saddr ( struct ip_packet *const  ip,
const uint8_t *  value 
)

Set the Source Address of an IP packet.

The function does not handle ip_packet whose ip_packet::version is IP_UNKNOWN.

Parameters
ipThe IP packet to modify
valueThe IP address value

◆ ip_set_tos()

void ip_set_tos ( struct ip_packet *const  ip,
const uint8_t  value 
)

Set the IPv4 Type Of Service (TOS) or IPv6 Traffic Class (TC) of an IP packet.

The function does not handle ip_packet whose ip_packet::version is IP_UNKNOWN.

Parameters
ipThe IP packet to modify
valueThe TOS/TC value

◆ ip_set_ttl()

void ip_set_ttl ( struct ip_packet *const  ip,
const uint8_t  value 
)

Set the IPv4 Time To Live (TTL) or IPv6 Hop Limit (HL) of an IP packet.

The function does not handle ip_packet whose ip_packet::version is IP_UNKNOWN.

Parameters
ipThe IP packet to modify
valueThe TTL/HL value

◆ ip_set_version()

void ip_set_version ( struct ip_packet *const  ip,
const ip_version  value 
)

Set the IP version of an IP packet.

Parameters
ipThe IP packet to modify
valueThe version value

◆ ipv4_get_daddr()

uint32_t ipv4_get_daddr ( const struct ip_packet *const  ip)

Get the destination address of an IPv4 packet.

The function does not handle ip_packet whose ip_packet::version is not IPV4.

Parameters
ipThe IPv4 packet to analyze
Returns
The source address of the given IPv4 packet

◆ ipv4_get_df()

int ipv4_get_df ( const struct ip_packet *const  ip)

Get the Don't Fragment (DF) bit of an IPv4 packet.

The function does not handle ip_packet whose ip_packet::version is not IPV4.

Parameters
ipThe IP packet to analyze
Returns
The DF bit

◆ ipv4_get_hdrlen()

static size_t ipv4_get_hdrlen ( const struct ip_packet *const  ip)
inlinestatic

◆ ipv4_get_header()

const struct ipv4_hdr* ipv4_get_header ( const struct ip_packet *const  ip)

Get the IPv4 header.

The function does not handle ip_packet whose ip_packet::version is not IPV4.

Parameters
ipThe IP packet to analyze
Returns
The IP header

◆ ipv4_get_id()

uint16_t ipv4_get_id ( const struct ip_packet *const  ip)

Get the IP-ID of an IPv4 packet.

The IP-ID value is returned as-is (ie. not automatically converted to the host byte order).

The function does not handle ip_packet whose ip_packet::version is not IPV4.

Parameters
ipThe IP packet to analyze
Returns
The IP-ID

◆ ipv4_get_id_nbo()

uint16_t ipv4_get_id_nbo ( const struct ip_packet *const  ip,
const unsigned int  nbo 
)

Get the IP-ID of an IPv4 packet in Network Byte Order.

The function does not handle ip_packet whose ip_packet::version is not IPV4.

Parameters
ipThe IP packet to analyze
nboThe NBO flag (if RND = 1, use NBO = 1)
Returns
The IP-ID

◆ ipv4_get_saddr()

uint32_t ipv4_get_saddr ( const struct ip_packet *const  ip)

Get the source address of an IPv4 packet.

The function does not handle ip_packet whose ip_packet::version is not IPV4.

Parameters
ipThe IPv4 packet to analyze
Returns
The source address of the given IPv4 packet

◆ ipv4_set_df()

void ipv4_set_df ( struct ip_packet *const  ip,
const int  value 
)

Set the Don't Fragment (DF) bit of an IPv4 packet.

The function does not handle ip_packet whose ip_packet::version is not IPV4.

Parameters
ipThe IP packet to modify
valueThe value of the DF bit

◆ ipv4_set_id()

void ipv4_set_id ( struct ip_packet *const  ip,
const int  value 
)

Set the IP-ID of an IPv4 packet.

The IP-ID value is set as-is (ie. not automatically converted to the host byte order).

The function does not handle ip_packet whose ip_packet::version is not IPV4.

Parameters
ipThe IP packet to modify
valueThe IP-ID value

◆ ipv6_get_daddr()

const struct ipv6_addr* ipv6_get_daddr ( const struct ip_packet *const  ip)

Get the destination address of an IPv6 packet.

The function does not handle ip_packet whose ip_packet::version is not IPV6.

Parameters
ipThe IPv6 packet to analyze
Returns
The source address of the given IPv6 packet

◆ ipv6_get_hdrlen()

static size_t ipv6_get_hdrlen ( const struct ip_packet *const  ip)
inlinestatic

◆ ipv6_get_header()

const struct ipv6_hdr* ipv6_get_header ( const struct ip_packet *const  ip)

Get the IPv6 header.

The function does not handle ip_packet whose ip_packet::version is not IPV6.

Parameters
ipThe IP packet to analyze
Returns
The IP header if IPv6

◆ ipv6_get_saddr()

const struct ipv6_addr* ipv6_get_saddr ( const struct ip_packet *const  ip)

Get the source address of an IPv6 packet.

The function does not handle ip_packet whose ip_packet::version is not IPV6.

Parameters
ipThe IPv6 packet to analyze
Returns
The source address of the given IPv6 packet

◆ swab16()

static uint16_t swab16 ( const uint16_t  value)
inlinestatic

In-place change the byte order in a two-byte value.

Parameters
valueThe two-byte value to modify
Returns
The same value with the byte order changed