ROHC compression/decompression library
Functions
ip.c File Reference

IP-agnostic packet. More...

#include "ip.h"
#include "rohc_utils.h"
#include <string.h>
#include <assert.h>
Include dependency graph for ip.c:

Functions

int ip_create (struct ip_packet *const ip, const unsigned char *packet, const unsigned int size)
 Create an IP packet from raw data.
const unsigned char * ip_get_raw_data (const struct ip_packet *const ip)
 Get the IP raw data (header + payload)
int ip_get_inner_packet (const struct ip_packet *const outer, struct ip_packet *const inner)
 Get the inner IP packet (IP in IP)
unsigned char * ip_get_next_header (const struct ip_packet *const ip, uint8_t *const type)
 Get the IP next header.
unsigned char * ip_get_next_layer (const struct ip_packet *const ip)
 Get the next header (but skip IP extensions)
unsigned char * 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.
unsigned char * ip_get_next_ext_from_ext (const unsigned char *const ext, uint8_t *const type)
 Get the next extension header of IPv6 packets from another extension.
unsigned short ip_get_extension_size (const unsigned char *const ext)
 Get the size of an IPv6 extension.
unsigned short ip_get_total_extension_size (const struct ip_packet *const ip)
 Get the size of the extension list.
int ip_is_fragment (const struct ip_packet *const ip)
 Whether the IP packet is an IP fragment or not.
unsigned int ip_get_totlen (const struct ip_packet *const ip)
 Get the total length of an IP packet.
unsigned int ip_get_hdrlen (const struct ip_packet *const ip)
 Get the length of an IP header.
unsigned int ip_get_plen (const struct ip_packet *const ip)
 Get the length of an IPv4/IPv6 payload.
ip_version ip_get_version (const struct ip_packet *const ip)
 Get the IP version of an IP packet.
void ip_set_version (struct ip_packet *const ip, const ip_version value)
 Set the IP version of an IP packet.
unsigned int ip_get_protocol (const struct ip_packet *const ip)
 Get the protocol transported by an IP packet.
unsigned int ext_get_protocol (const unsigned char *const ext)
 Get the protocol transported by the last IPv6 extension.
void ip_set_protocol (struct ip_packet *const ip, const uint8_t value)
 Set the protocol transported by an IP packet.
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.
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.
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.
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.
void ip_set_saddr (struct ip_packet *const ip, const unsigned char *value)
 Set the Source Address of an IP packet.
void ip_set_daddr (struct ip_packet *const ip, const unsigned char *value)
 Set the Destination Address of an IP packet.
struct ipv4_hdripv4_get_header (const struct ip_packet *const ip)
 Get the IPv4 header.
uint16_t ipv4_get_id (const struct ip_packet *const ip)
 Get the IP-ID of an IPv4 packet.
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.
void ipv4_set_id (struct ip_packet *const ip, const int value)
 Set the IP-ID of an IPv4 packet.
int ipv4_get_df (const struct ip_packet *const ip)
 Get the Don't Fragment (DF) bit of an IPv4 packet.
void ipv4_set_df (struct ip_packet *const ip, const int value)
 Set the Don't Fragment (DF) bit of an IPv4 packet.
uint32_t ipv4_get_saddr (const struct ip_packet *const ip)
 Get the source address of an IPv4 packet.
uint32_t ipv4_get_daddr (const struct ip_packet *const ip)
 Get the destination address of an IPv4 packet.
struct ipv6_hdripv6_get_header (const struct ip_packet *const ip)
 Get the IPv6 header.
uint32_t ipv6_get_flow_label (const struct ip_packet *const ip)
 Get the flow label of an IPv6 packet.
void ipv6_set_flow_label (struct ip_packet *const ip, const uint32_t value)
 Set the flow label of an IPv6 packet.
struct ipv6_addripv6_get_saddr (const struct ip_packet *const ip)
 Get the source address of an IPv6 packet.
struct ipv6_addripv6_get_daddr (const struct ip_packet *const ip)
 Get the destination address of an IPv6 packet.
int get_ip_version (const unsigned char *const packet, const unsigned int size, ip_version *const version)

Detailed Description

IP-agnostic packet.

Author:
Didier Barvaux <didier.barvaux@toulouse.viveris.com>
Didier Barvaux <didier@barvaux.org>

Function Documentation

unsigned int ext_get_protocol ( const unsigned char *const  ext)

Get the protocol transported by the last IPv6 extension.

Parameters:
extThe first extension
Returns:
The protocol number that identify the protocol transported by the given IP extension

References ext_get_protocol(), IPV6_EXT_AUTH, IPV6_EXT_DESTINATION, IPV6_EXT_HOP_BY_HOP, IPV6_EXT_ROUTING, and protocol.

Referenced by ext_get_protocol(), and ip_get_protocol().

int get_ip_version ( const unsigned char *const  packet,
const unsigned int  size,
ip_version *const  version 
)

Private functions used by the IP module: (please do not use directly)

References IP_UNKNOWN, IPV4, and IPV6.

Referenced by compute_crc_dynamic(), compute_crc_static(), and ip_create().

int ip_create ( struct ip_packet *const  ip,
const unsigned char *  packet,
const unsigned int  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
Returns:
Whether the IP packet was successfully created or not

References ip_packet::data, get_ip_version(), ip_packet::header, ip_get_hdrlen(), ip_get_totlen(), IP_UNKNOWN, IPV4, IPV4_MALFORMED, IPV6, IPV6_MALFORMED, ip_packet::size, ip_packet::v4, ip_packet::v6, version, and ip_packet::version.

Referenced by ip_get_inner_packet(), and rohc_compress2().

unsigned short ip_get_extension_size ( const unsigned char *const  ext)

Get the size of an IPv6 extension.

Parameters:
extThe extension
Returns:
The size of the extension

Referenced by c_init_header_info(), and ip_get_total_extension_size().

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

References ip_packet::header, ipv4_hdr::ihl, IPV4, IPV6, len, ip_packet::v4, and ip_packet::version.

Referenced by c_generic_check_profile(), c_generic_encode(), and ip_create().

int 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
Returns:
Whether the inner IP header is successfully created or not

References ip_create(), ip_get_next_layer(), and ip_get_plen().

Referenced by c_generic_create(), c_generic_encode(), c_udp_check_context(), and rohc_compress2().

unsigned char* ip_get_next_ext_from_ext ( const unsigned char *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

References IPV6_EXT_AUTH, IPV6_EXT_DESTINATION, IPV6_EXT_HOP_BY_HOP, and IPV6_EXT_ROUTING.

Referenced by ip_get_total_extension_size().

unsigned char* 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

References ip_get_next_header(), IP_UNKNOWN, IPV6, IPV6_EXT_AUTH, IPV6_EXT_DESTINATION, IPV6_EXT_HOP_BY_HOP, IPV6_EXT_ROUTING, and ip_packet::version.

Referenced by ip_get_total_extension_size().

unsigned char* 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

References ip_packet::data, ip_packet::header, IPV4, IPV6, ipv4_hdr::protocol, ip_packet::v4, ip_packet::v6, and ip_packet::version.

Referenced by ip_get_next_ext_from_ip(), and ip_get_next_layer().

unsigned char* 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

References ip_get_next_header(), IPV6, IPV6_EXT_AUTH, IPV6_EXT_DESTINATION, IPV6_EXT_HOP_BY_HOP, IPV6_EXT_ROUTING, and ip_packet::version.

Referenced by c_esp_check_profile(), c_generic_encode(), c_udp_check_context(), c_udp_check_profile(), ip_get_inner_packet(), and rtp_header_flags_and_fields().

unsigned int ip_get_plen ( const struct ip_packet *const  ip)

Get the length of an IPv4/IPv6 payload.

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

Parameters:
ipThe IPv4/IPv6 packet to analyze
Returns:
The length of the IPv4/IPv6 payload if successful, 0 otherwise

References ip_packet::header, ipv4_hdr::ihl, ip_get_total_extension_size(), IPV4, IPV6, len, rohc_ntoh16(), ipv4_hdr::tot_len, ip_packet::v4, ip_packet::v6, and ip_packet::version.

Referenced by c_esp_check_profile(), c_udp_check_profile(), and ip_get_inner_packet().

unsigned int 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

References ip_packet::data, ext_get_protocol(), ip_packet::header, IPV4, IPV6, IPV6_EXT_AUTH, IPV6_EXT_DESTINATION, IPV6_EXT_HOP_BY_HOP, IPV6_EXT_ROUTING, ipv4_hdr::protocol, protocol, ip_packet::v4, ip_packet::v6, and ip_packet::version.

Referenced by c_generic_create(), c_generic_encode(), c_udp_check_context(), changed_fields(), code_ipv4_static_part(), code_ipv6_static_part(), header_fields(), and rohc_compress2().

const unsigned char* 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)

References ip_packet::data.

Referenced by code_UO0_packet(), code_UO1_packet(), code_UO2_packet(), and rohc_compress2().

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

References ip_packet::header, IPV4, IPV6, IPV6_GET_TC, ipv4_hdr::tos, tos, ip_packet::v4, ip_packet::v6, and ip_packet::version.

Referenced by changed_fields(), code_ipv4_dynamic_part(), code_ipv6_dynamic_part(), and header_fields().

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

References ip_get_extension_size(), ip_get_next_ext_from_ext(), and ip_get_next_ext_from_ip().

Referenced by c_generic_encode(), and ip_get_plen().

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

References ip_packet::header, IPV4, IPV6, len, rohc_ntoh16(), ip_packet::size, ipv4_hdr::tot_len, ip_packet::v4, and ip_packet::version.

Referenced by ip_create(), and rohc_compress2().

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

References ip_packet::header, IPV4, IPV6, ipv4_hdr::ttl, ttl, ip_packet::v4, ip_packet::v6, and ip_packet::version.

Referenced by changed_fields(), code_ipv4_dynamic_part(), code_ipv6_dynamic_part(), and header_fields().

ip_version ip_get_version ( const struct ip_packet *const  ip)

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

References ip_packet::version.

Referenced by c_generic_check_profile(), c_generic_encode(), c_init_header_info(), c_udp_check_context(), changed_dynamic_one_hdr(), changed_fields(), code_EXT3_packet(), code_generic_dynamic_part(), code_generic_static_part(), code_IR_packet(), and code_uo_remainder().

int 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 if successful, 0 otherwise

References ipv4_hdr::frag_off, ip_packet::header, IP_DF, IPV4, IPV6, rohc_ntoh16(), ip_packet::v4, and ip_packet::version.

Referenced by c_generic_check_profile().

void ip_set_daddr ( struct ip_packet *const  ip,
const unsigned char *  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

References ipv4_hdr::daddr, ip_packet::header, ipv6_hdr::ip6_dst, IPV4, IPV6, ip_packet::v4, ip_packet::v6, and ip_packet::version.

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

References ip_packet::header, IPV4, IPV6, ipv4_hdr::protocol, ip_packet::v4, ip_packet::v6, and ip_packet::version.

void ip_set_saddr ( struct ip_packet *const  ip,
const unsigned char *  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

References ip_packet::header, ipv6_hdr::ip6_src, IPV4, IPV6, ipv4_hdr::saddr, ip_packet::v4, ip_packet::v6, and ip_packet::version.

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

References ip_packet::header, IPV4, IPV6, IPV6_SET_TC, ipv4_hdr::tos, ip_packet::v4, ip_packet::v6, and ip_packet::version.

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

References ip_packet::header, IPV4, IPV6, ipv4_hdr::ttl, ip_packet::v4, ip_packet::v6, and ip_packet::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

References ip_packet::version.

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

References ipv4_hdr::daddr, ip_packet::header, IPV4, ip_packet::v4, and ip_packet::version.

Referenced by c_generic_check_profile(), c_udp_check_context(), and code_ipv4_static_part().

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

References ip_packet::header, IPV4, IPV4_GET_DF, ip_packet::v4, and ip_packet::version.

Referenced by changed_dynamic_one_hdr(), code_ipv4_dynamic_part(), and header_flags().

struct ipv4_hdr* ipv4_get_header ( const struct ip_packet *const  ip) [read]

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

References ip_packet::header, IPV4, ip_packet::v4, and ip_packet::version.

Referenced by c_generic_encode().

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

References IPV4, ipv4_get_id_nbo(), and ip_packet::version.

Referenced by c_generic_encode(), and code_uo_remainder().

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

References ip_packet::header, ipv4_hdr::id, id, IPV4, ip_packet::v4, and ip_packet::version.

Referenced by code_ipv4_dynamic_part(), and ipv4_get_id().

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

References ip_packet::header, IPV4, ipv4_hdr::saddr, ip_packet::v4, and ip_packet::version.

Referenced by c_generic_check_profile(), c_udp_check_context(), and code_ipv4_static_part().

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

References ip_packet::header, IPV4, IPV4_SET_DF, ip_packet::v4, and ip_packet::version.

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

References ip_packet::header, ipv4_hdr::id, IPV4, ip_packet::v4, and ip_packet::version.

struct ipv6_addr* ipv6_get_daddr ( const struct ip_packet *const  ip) [read]

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

References ip_packet::header, ipv6_hdr::ip6_dst, IPV6, ip_packet::v6, and ip_packet::version.

Referenced by c_generic_check_profile(), c_udp_check_context(), and code_ipv6_static_part().

uint32_t ipv6_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

References ip_packet::header, IPV6, IPV6_GET_FLOW_LABEL, ip_packet::v6, and ip_packet::version.

Referenced by c_udp_check_context(), and code_ipv6_static_part().

struct ipv6_hdr* ipv6_get_header ( const struct ip_packet *const  ip) [read]

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

References ip_packet::header, IPV6, ip_packet::v6, and ip_packet::version.

Referenced by c_generic_encode().

struct ipv6_addr* ipv6_get_saddr ( const struct ip_packet *const  ip) [read]

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

References ip_packet::header, ipv6_hdr::ip6_src, IPV6, ip_packet::v6, and ip_packet::version.

Referenced by c_generic_check_profile(), c_udp_check_context(), and code_ipv6_static_part().

void ipv6_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

References ip_packet::header, IPV6, IPV6_SET_FLOW_LABEL, ip_packet::v6, and ip_packet::version.