IPv4/IPv6 Primitives and Helpers

Overview

Miscellaneous defines and helper functions for IP addresses and IP protocols.

API Reference

group ip_4_6

IPv4/IPv6 primitives and helpers.

Defines

PF_UNSPEC

Unspecified protocol family.

PF_INET

IP protocol family version 4.

PF_INET6

IP protocol family version 6.

PF_PACKET

Packet family.

PF_CAN

Controller Area Network.

PF_NET_MGMT

Network management info.

AF_UNSPEC

Unspecified address family.

AF_INET

IP protocol family version 4.

AF_INET6

IP protocol family version 6.

AF_PACKET

Packet family.

AF_CAN

Controller Area Network.

AF_NET_MGMT

Network management info.

ntohs(x)

Convert 16-bit value from network to host byte order.

Return
Host byte order value.
Parameters
  • x: The network byte order value to convert.

ntohl(x)

Convert 32-bit value from network to host byte order.

Return
Host byte order value.
Parameters
  • x: The network byte order value to convert.

ntohll(x)

Convert 64-bit value from network to host byte order.

Return
Host byte order value.
Parameters
  • x: The network byte order value to convert.

htons(x)

Convert 16-bit value from host to network byte order.

Return
Network byte order value.
Parameters
  • x: The host byte order value to convert.

htonl(x)

Convert 32-bit value from host to network byte order.

Return
Network byte order value.
Parameters
  • x: The host byte order value to convert.

htonll(x)

Convert 64-bit value from host to network byte order.

Return
Network byte order value.
Parameters
  • x: The host byte order value to convert.

ALIGN_H(x)
ALIGN_D(x)
CMSG_FIRSTHDR(msghdr)
CMSG_NXTHDR(msghdr, cmsg)
CMSG_DATA(cmsg)
CMSG_SPACE(length)
CMSG_LEN(length)
INET_ADDRSTRLEN

Max length of the IPv4 address as a string. Defined by POSIX.

INET6_ADDRSTRLEN

Max length of the IPv6 address as a string. Takes into account possible mapped IPv4 addresses.

net_ipaddr_copy(dest, src)

Copy an IPv4 or IPv6 address.

Return
Destination address.
Parameters
  • dest: Destination IP address.
  • src: Source IP address.

Typedefs

typedef unsigned short int sa_family_t

Socket address family type

typedef size_t socklen_t

Length of a socket address

Enums

enum net_ip_protocol

Protocol numbers from IANA/BSD

Values:

IPPROTO_IP = 0

IP protocol (pseudo-val for setsockopt()

IPPROTO_ICMP = 1

ICMP protocol

IPPROTO_TCP = 6

TCP protocol

IPPROTO_UDP = 17

UDP protocol

IPPROTO_IPV6 = 41

IPv6 protocol

IPPROTO_ICMPV6 = 58

ICMPv6 protocol

IPPROTO_RAW = 255

RAW IP packets

enum net_ip_protocol_secure

Protocol numbers for TLS protocols

Values:

IPPROTO_TLS_1_0 = 256

TLS 1.0 protocol

IPPROTO_TLS_1_1 = 257

TLS 1.1 protocol

IPPROTO_TLS_1_2 = 258

TLS 1.2 protocol

IPPROTO_DTLS_1_0 = 272

DTLS 1.0 protocol

IPPROTO_DTLS_1_2 = 273

DTLS 1.2 protocol

enum net_sock_type

Socket type

Values:

SOCK_STREAM = 1

Stream socket type

SOCK_DGRAM

Datagram socket type

SOCK_RAW

RAW socket type

enum net_ip_mtu

Values:

NET_IPV6_MTU = 1280

IPv6 MTU length. We must be able to receive this size IPv6 packet without fragmentation.

NET_IPV4_MTU = 576

IPv4 MTU length. We must be able to receive this size IPv4 packet without fragmentation.

enum net_priority

Network packet priority settings described in IEEE 802.1Q Annex I.1

Values:

NET_PRIORITY_BK = 1

Background (lowest)

NET_PRIORITY_BE = 0

Best effort (default)

NET_PRIORITY_EE = 2

Excellent effort

NET_PRIORITY_CA = 3

Critical applications (highest)

NET_PRIORITY_VI = 4

Video, < 100 ms latency and jitter

NET_PRIORITY_VO = 5

Voice, < 10 ms latency and jitter

NET_PRIORITY_IC = 6

Internetwork control

NET_PRIORITY_NC = 7

Network control

enum net_addr_state

What is the current state of the network address

Values:

NET_ADDR_ANY_STATE = -1

Default (invalid) address type

NET_ADDR_TENTATIVE = 0

Tentative address

NET_ADDR_PREFERRED

Preferred address

NET_ADDR_DEPRECATED

Deprecated address

enum net_addr_type

How the network address is assigned to network interface

Values:

NET_ADDR_ANY = 0

Default value. This is not a valid value.

NET_ADDR_AUTOCONF

Auto configured address

NET_ADDR_DHCP

Address is from DHCP

NET_ADDR_MANUAL

Manually set address

NET_ADDR_OVERRIDABLE

Manually set address which is overridable by DHCP

Functions

static bool net_ipv6_is_addr_loopback(struct in6_addr *addr)

Check if the IPv6 address is a loopback address (::1).

Return
True if address is a loopback address, False otherwise.
Parameters
  • addr: IPv6 address

static bool net_ipv6_is_addr_mcast(const struct in6_addr *addr)

Check if the IPv6 address is a multicast address.

Return
True if address is multicast address, False otherwise.
Parameters
  • addr: IPv6 address

struct net_if_addr *net_if_ipv6_addr_lookup(const struct in6_addr *addr, struct net_if **iface)
static bool net_ipv6_is_my_addr(struct in6_addr *addr)

Check if IPv6 address is found in one of the network interfaces.

Return
True if address was found, False otherwise.
Parameters
  • addr: IPv6 address

struct net_if_mcast_addr *net_if_ipv6_maddr_lookup(const struct in6_addr *addr, struct net_if **iface)
static bool net_ipv6_is_my_maddr(struct in6_addr *maddr)

Check if IPv6 multicast address is found in one of the network interfaces.

Return
True if address was found, False otherwise.
Parameters
  • maddr: Multicast IPv6 address

static bool net_ipv6_is_prefix(const u8_t *addr1, const u8_t *addr2, u8_t length)

Check if two IPv6 addresses are same when compared after prefix mask.

Return
True if IPv6 prefixes are the same, False otherwise.
Parameters
  • addr1: First IPv6 address.
  • addr2: Second IPv6 address.
  • length: Prefix length (max length is 128).

static bool net_ipv4_is_addr_loopback(struct in_addr *addr)

Check if the IPv4 address is a loopback address (127.0.0.0/8).

Return
True if address is a loopback address, False otherwise.
Parameters
  • addr: IPv4 address

static bool net_ipv4_is_addr_unspecified(const struct in_addr *addr)

Check if the IPv4 address is unspecified (all bits zero)

Return
True if the address is unspecified, false otherwise.
Parameters
  • addr: IPv4 address.

static bool net_ipv4_is_addr_mcast(const struct in_addr *addr)

Check if the IPv4 address is a multicast address.

Return
True if address is multicast address, False otherwise.
Parameters
  • addr: IPv4 address

static bool net_ipv4_is_ll_addr(const struct in_addr *addr)

Check if the given IPv4 address is a link local address.

Return
True if it is, false otherwise.
Parameters
  • addr: A valid pointer on an IPv4 address

static bool net_ipv4_addr_cmp(const struct in_addr *addr1, const struct in_addr *addr2)

Compare two IPv4 addresses.

Return
True if the addresses are the same, false otherwise.
Parameters
  • addr1: Pointer to IPv4 address.
  • addr2: Pointer to IPv4 address.

static bool net_ipv6_addr_cmp(const struct in6_addr *addr1, const struct in6_addr *addr2)

Compare two IPv6 addresses.

Return
True if the addresses are the same, false otherwise.
Parameters
  • addr1: Pointer to IPv6 address.
  • addr2: Pointer to IPv6 address.

static bool net_ipv6_is_ll_addr(const struct in6_addr *addr)

Check if the given IPv6 address is a link local address.

Return
True if it is, false otherwise.
Parameters
  • addr: A valid pointer on an IPv6 address

const struct in6_addr *net_ipv6_unspecified_address(void)

Return pointer to any (all bits zeros) IPv6 address.

Return
Any IPv6 address.

const struct in_addr *net_ipv4_unspecified_address(void)

Return pointer to any (all bits zeros) IPv4 address.

Return
Any IPv4 address.

const struct in_addr *net_ipv4_broadcast_address(void)

Return pointer to broadcast (all bits ones) IPv4 address.

Return
Broadcast IPv4 address.

bool net_if_ipv4_addr_mask_cmp(struct net_if *iface, const struct in_addr *addr)
static bool net_ipv4_addr_mask_cmp(struct net_if *iface, const struct in_addr *addr)

Check if the given address belongs to same subnet that has been configured for the interface.

Return
True if address is in same subnet, false otherwise.
Parameters
  • iface: A valid pointer on an interface
  • addr: IPv4 address

bool net_if_ipv4_is_addr_bcast(struct net_if *iface, const struct in_addr *addr)
static bool net_ipv4_is_addr_bcast(struct net_if *iface, const struct in_addr *addr)

Check if the given IPv4 address is a broadcast address.

Return
True if address is a broadcast address, false otherwise.
Parameters
  • iface: Interface to use. Must be a valid pointer to an interface.
  • addr: IPv4 address

struct net_if_addr *net_if_ipv4_addr_lookup(const struct in_addr *addr, struct net_if **iface)
static bool net_ipv4_is_my_addr(const struct in_addr *addr)

Check if the IPv4 address is assigned to any network interface in the system.

Return
True if IPv4 address is found in one of the network interfaces, False otherwise.
Parameters
  • addr: A valid pointer on an IPv4 address

static bool net_ipv6_is_addr_unspecified(const struct in6_addr *addr)

Check if the IPv6 address is unspecified (all bits zero)

Return
True if the address is unspecified, false otherwise.
Parameters
  • addr: IPv6 address.

static bool net_ipv6_is_addr_solicited_node(const struct in6_addr *addr)

Check if the IPv6 address is solicited node multicast address FF02:0:0:0:0:1:FFXX:XXXX defined in RFC 3513.

Return
True if the address is solicited node address, false otherwise.
Parameters
  • addr: IPv6 address.

static bool net_ipv6_is_addr_mcast_scope(const struct in6_addr *addr, int scope)

Check if the IPv6 address is a given scope multicast address (FFyx::).

Return
True if the address is in given scope multicast address, false otherwise.
Parameters
  • addr: IPv6 address
  • scope: Scope to check

static bool net_ipv6_is_addr_mcast_global(const struct in6_addr *addr)

Check if the IPv6 address is a global multicast address (FFxE::/16).

Return
True if the address is global multicast address, false otherwise.
Parameters
  • addr: IPv6 address.

static bool net_ipv6_is_addr_mcast_iface(const struct in6_addr *addr)

Check if the IPv6 address is a interface scope multicast address (FFx1::).

Return
True if the address is a interface scope multicast address, false otherwise.
Parameters
  • addr: IPv6 address.

static bool net_ipv6_is_addr_mcast_site(const struct in6_addr *addr)

Check if the IPv6 address is a site scope multicast address (FFx5::).

Return
True if the address is a site scope multicast address, false otherwise.
Parameters
  • addr: IPv6 address.

static bool net_ipv6_is_addr_mcast_org(const struct in6_addr *addr)

Check if the IPv6 address is an organization scope multicast address (FFx8::).

Return
True if the address is an organization scope multicast address, false otherwise.
Parameters
  • addr: IPv6 address.

static bool net_ipv6_is_addr_mcast_group(const struct in6_addr *addr, const struct in6_addr *group)

Check if the IPv6 address belongs to certain multicast group.

Return
True if the IPv6 multicast address belongs to given multicast group, false otherwise.
Parameters
  • addr: IPv6 address.
  • group: Group id IPv6 address, the values must be in network byte order

static void net_ipv6_addr_create_solicited_node(const struct in6_addr *src, struct in6_addr *dst)

Create solicited node IPv6 multicast address FF02:0:0:0:0:1:FFXX:XXXX defined in RFC 3513.

Parameters
  • src: IPv6 address.
  • dst: IPv6 address.

static void net_ipv6_addr_create(struct in6_addr *addr, u16_t addr0, u16_t addr1, u16_t addr2, u16_t addr3, u16_t addr4, u16_t addr5, u16_t addr6, u16_t addr7)

Construct an IPv6 address from eight 16-bit words.

Parameters
  • addr: IPv6 address
  • addr0: 16-bit word which is part of the address
  • addr1: 16-bit word which is part of the address
  • addr2: 16-bit word which is part of the address
  • addr3: 16-bit word which is part of the address
  • addr4: 16-bit word which is part of the address
  • addr5: 16-bit word which is part of the address
  • addr6: 16-bit word which is part of the address
  • addr7: 16-bit word which is part of the address

static void net_ipv6_addr_create_ll_allnodes_mcast(struct in6_addr *addr)

Create link local allnodes multicast IPv6 address.

Parameters
  • addr: IPv6 address

static void net_ipv6_addr_create_iid(struct in6_addr *addr, struct net_linkaddr *lladdr)

Create IPv6 address interface identifier.

Parameters
  • addr: IPv6 address
  • lladdr: Link local address

static bool net_ipv6_addr_based_on_ll(const struct in6_addr *addr, const struct net_linkaddr *lladdr)

Check if given address is based on link layer address.

Return
True if it is, False otherwise

static struct sockaddr_in6 *net_sin6(const struct sockaddr *addr)

Get sockaddr_in6 from sockaddr. This is a helper so that the code calling this function can be made shorter.

Return
Pointer to IPv6 socket address
Parameters
  • addr: Socket address

static struct sockaddr_in *net_sin(const struct sockaddr *addr)

Get sockaddr_in from sockaddr. This is a helper so that the code calling this function can be made shorter.

Return
Pointer to IPv4 socket address
Parameters
  • addr: Socket address

static struct sockaddr_in6_ptr *net_sin6_ptr(const struct sockaddr_ptr *addr)

Get sockaddr_in6_ptr from sockaddr_ptr. This is a helper so that the code calling this function can be made shorter.

Return
Pointer to IPv6 socket address
Parameters
  • addr: Socket address

static struct sockaddr_in_ptr *net_sin_ptr(const struct sockaddr_ptr *addr)

Get sockaddr_in_ptr from sockaddr_ptr. This is a helper so that the code calling this function can be made shorter.

Return
Pointer to IPv4 socket address
Parameters
  • addr: Socket address

static struct sockaddr_ll_ptr *net_sll_ptr(const struct sockaddr_ptr *addr)

Get sockaddr_ll_ptr from sockaddr_ptr. This is a helper so that the code calling this function can be made shorter.

Return
Pointer to linklayer socket address
Parameters
  • addr: Socket address

static struct sockaddr_can_ptr *net_can_ptr(const struct sockaddr_ptr *addr)

Get sockaddr_can_ptr from sockaddr_ptr. This is a helper so that the code needing this functionality can be made shorter.

Return
Pointer to CAN socket address
Parameters
  • addr: Socket address

int net_addr_pton(sa_family_t family, const char *src, void *dst)

Convert a string to IP address.

Note
This function doesn’t do precise error checking, do not use for untrusted strings.
Return
0 if ok, < 0 if error
Parameters
  • family: IP address family (AF_INET or AF_INET6)
  • src: IP address in a null terminated string
  • dst: Pointer to struct in_addr if family is AF_INET or pointer to struct in6_addr if family is AF_INET6

char *net_addr_ntop(sa_family_t family, const void *src, char *dst, size_t size)

Convert IP address to string form.

Return
dst pointer if ok, NULL if error
Parameters
  • family: IP address family (AF_INET or AF_INET6)
  • src: Pointer to struct in_addr if family is AF_INET or pointer to struct in6_addr if family is AF_INET6
  • dst: Buffer for IP address as a null terminated string
  • size: Number of bytes available in the buffer

bool net_ipaddr_parse(const char *str, size_t str_len, struct sockaddr *addr)

Parse a string that contains either IPv4 or IPv6 address and optional port, and store the information in user supplied sockaddr struct.

Syntax of the IP address string: 192.0.2.1:80 192.0.2.42

[2001:db8::2] 2001:db::42 Note that the str_len parameter is used to restrict the amount of characters that are checked. If the string does not contain port number, then the port number in sockaddr is not modified.

Return
True if parsing could be done, false otherwise.
Parameters
  • str: String that contains the IP address.
  • str_len: Length of the string to be parsed.
  • addr: Pointer to user supplied struct sockaddr.

static s32_t net_tcp_seq_cmp(u32_t seq1, u32_t seq2)

Compare TCP sequence numbers.

This function compares TCP sequence numbers, accounting for wraparound effects.

Return
< 0 if seq1 < seq2, 0 if seq1 == seq2, > 0 if seq > seq2
Parameters
  • seq1: First sequence number
  • seq2: Seconds sequence number

static bool net_tcp_seq_greater(u32_t seq1, u32_t seq2)

Check that one TCP sequence number is greater.

This is convenience function on top of net_tcp_seq_cmp().

Return
True if seq > seq2
Parameters
  • seq1: First sequence number
  • seq2: Seconds sequence number

int net_bytes_from_str(u8_t *buf, int buf_len, const char *src)

Convert a string of hex values to array of bytes.

The syntax of the string is “ab:02:98:fa:42:01”

Return
0 if ok, <0 if error
Parameters
  • buf: Pointer to memory where the bytes are written.
  • buf_len: Length of the memory area.
  • src: String of bytes.

int net_tx_priority2tc(enum net_priority prio)

Convert Tx network packet priority to traffic class so we can place the packet into correct Tx queue.

Return
Tx traffic class that handles that priority network traffic.
Parameters
  • prio: Network priority

int net_rx_priority2tc(enum net_priority prio)

Convert Rx network packet priority to traffic class so we can place the packet into correct Rx queue.

Return
Rx traffic class that handles that priority network traffic.
Parameters
  • prio: Network priority

static enum net_priority net_vlan2priority(u8_t priority)

Convert network packet VLAN priority to network packet priority so we can place the packet into correct queue.

Return
Network priority
Parameters
  • priority: VLAN priority

static u8_t net_priority2vlan(enum net_priority priority)

Convert network packet priority to network packet VLAN priority.

Return
VLAN priority (PCP)
Parameters
  • priority: Packet priority

const char *net_family2str(sa_family_t family)

Return network address family value as a string. This is only usable for debugging.

Return
Network address family as a string, or NULL if family is unknown.
Parameters
  • family: Network address family code

struct in6_addr
#include <net_ip.h>

IPv6 address struct

struct in_addr
#include <net_ip.h>

IPv4 address struct

struct sockaddr_in6
#include <net_ip.h>

Socket address struct for IPv6.

struct sockaddr_in
#include <net_ip.h>

Socket address struct for IPv4.

struct sockaddr_ll
#include <net_ip.h>

Socket address struct for packet socket.

struct sockaddr
#include <net_ip.h>

Generic sockaddr struct. Must be cast to proper type.

struct net_tuple
#include <net_ip.h>

IPv6/IPv4 network connection tuple