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.

PF_LOCAL

Inter-process communication

PF_UNIX

Inter-process communication

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.

AF_LOCAL

Inter-process communication

AF_UNIX

Inter-process communication

ntohs(x)

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

Parameters
  • x – The network byte order value to convert.

Returns

Host byte order value.

ntohl(x)

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

Parameters
  • x – The network byte order value to convert.

Returns

Host byte order value.

ntohll(x)

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

Parameters
  • x – The network byte order value to convert.

Returns

Host byte order value.

htons(x)

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

Parameters
  • x – The host byte order value to convert.

Returns

Network byte order value.

htonl(x)

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

Parameters
  • x – The host byte order value to convert.

Returns

Network byte order value.

htonll(x)

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

Parameters
  • x – The host byte order value to convert.

Returns

Network byte order value.

NET_IPV6_ADDR_SIZE
NET_IPV4_ADDR_SIZE
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_MAX_PRIORITIES
net_ipaddr_copy(dest, src)

Copy an IPv4 or IPv6 address.

Parameters
  • dest – Destination IP address.

  • src – Source IP address.

Returns

Destination 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:

enumerator IPPROTO_IP = 0

IP protocol (pseudo-val for setsockopt()

enumerator IPPROTO_ICMP = 1

ICMP protocol

enumerator IPPROTO_IGMP = 2

IGMP protocol

enumerator IPPROTO_IPIP = 4

IPIP tunnels

enumerator IPPROTO_TCP = 6

TCP protocol

enumerator IPPROTO_UDP = 17

UDP protocol

enumerator IPPROTO_IPV6 = 41

IPv6 protocol

enumerator IPPROTO_ICMPV6 = 58

ICMPv6 protocol

enumerator IPPROTO_RAW = 255

RAW IP packets

enum net_ip_protocol_secure

Protocol numbers for TLS protocols

Values:

enumerator IPPROTO_TLS_1_0 = 256

TLS 1.0 protocol

enumerator IPPROTO_TLS_1_1 = 257

TLS 1.1 protocol

enumerator IPPROTO_TLS_1_2 = 258

TLS 1.2 protocol

enumerator IPPROTO_DTLS_1_0 = 272

DTLS 1.0 protocol

enumerator IPPROTO_DTLS_1_2 = 273

DTLS 1.2 protocol

enum net_sock_type

Socket type

Values:

enumerator SOCK_STREAM = 1

Stream socket type

enumerator SOCK_DGRAM

Datagram socket type

enumerator SOCK_RAW

RAW socket type

enum net_ip_mtu

Values:

enumerator NET_IPV6_MTU = 1280

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

enumerator 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:

enumerator NET_PRIORITY_BK = 1

Background (lowest)

enumerator NET_PRIORITY_BE = 0

Best effort (default)

enumerator NET_PRIORITY_EE = 2

Excellent effort

enumerator NET_PRIORITY_CA = 3

Critical applications (highest)

enumerator NET_PRIORITY_VI = 4

Video, < 100 ms latency and jitter

enumerator NET_PRIORITY_VO = 5

Voice, < 10 ms latency and jitter

enumerator NET_PRIORITY_IC = 6

Internetwork control

enumerator NET_PRIORITY_NC = 7

Network control

enum net_addr_state

What is the current state of the network address

Values:

enumerator NET_ADDR_ANY_STATE = -1

Default (invalid) address type

enumerator NET_ADDR_TENTATIVE = 0

Tentative address

enumerator NET_ADDR_PREFERRED

Preferred address

enumerator NET_ADDR_DEPRECATED

Deprecated address

enum net_addr_type

How the network address is assigned to network interface

Values:

enumerator NET_ADDR_ANY = 0

Default value. This is not a valid value.

enumerator NET_ADDR_AUTOCONF

Auto configured address

enumerator NET_ADDR_DHCP

Address is from DHCP

enumerator NET_ADDR_MANUAL

Manually set address

enumerator NET_ADDR_OVERRIDABLE

Manually set address which is overridable by DHCP

Functions

static inline bool net_ipv6_is_addr_loopback(struct in6_addr *addr)

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

Parameters
  • addr – IPv6 address

Returns

True if address is a loopback address, False otherwise.

static inline bool net_ipv6_is_addr_mcast(const struct in6_addr *addr)

Check if the IPv6 address is a multicast address.

Parameters
  • addr – IPv6 address

Returns

True if address is multicast address, False otherwise.

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

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

Parameters
  • addr – IPv6 address

Returns

True if address was found, False otherwise.

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

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

Parameters
  • maddr – Multicast IPv6 address

Returns

True if address was found, False otherwise.

static inline bool net_ipv6_is_prefix(const uint8_t *addr1, const uint8_t *addr2, uint8_t length)

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

Parameters
  • addr1 – First IPv6 address.

  • addr2 – Second IPv6 address.

  • length – Prefix length (max length is 128).

Returns

True if IPv6 prefixes are the same, False otherwise.

static inline bool net_ipv4_is_addr_loopback(struct in_addr *addr)

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

Parameters
  • addr – IPv4 address

Returns

True if address is a loopback address, False otherwise.

static inline bool net_ipv4_is_addr_unspecified(const struct in_addr *addr)

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

Parameters
  • addr – IPv4 address.

Returns

True if the address is unspecified, false otherwise.

static inline bool net_ipv4_is_addr_mcast(const struct in_addr *addr)

Check if the IPv4 address is a multicast address.

Parameters
  • addr – IPv4 address

Returns

True if address is multicast address, False otherwise.

static inline bool net_ipv4_is_ll_addr(const struct in_addr *addr)

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

Parameters
  • addr – A valid pointer on an IPv4 address

Returns

True if it is, false otherwise.

static inline void net_ipv4_addr_copy_raw(uint8_t *dest, const uint8_t *src)

Copy an IPv4 address raw buffer.

Parameters
  • dest – Destination IP address.

  • src – Source IP address.

static inline void net_ipv6_addr_copy_raw(uint8_t *dest, const uint8_t *src)

Copy an IPv6 address raw buffer.

Parameters
  • dest – Destination IP address.

  • src – Source IP address.

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

Compare two IPv4 addresses.

Parameters
  • addr1 – Pointer to IPv4 address.

  • addr2 – Pointer to IPv4 address.

Returns

True if the addresses are the same, false otherwise.

static inline bool net_ipv4_addr_cmp_raw(const uint8_t *addr1, const uint8_t *addr2)

Compare two raw IPv4 address buffers.

Parameters
  • addr1 – Pointer to IPv4 address buffer.

  • addr2 – Pointer to IPv4 address buffer.

Returns

True if the addresses are the same, false otherwise.

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

Compare two IPv6 addresses.

Parameters
  • addr1 – Pointer to IPv6 address.

  • addr2 – Pointer to IPv6 address.

Returns

True if the addresses are the same, false otherwise.

static inline bool net_ipv6_addr_cmp_raw(const uint8_t *addr1, const uint8_t *addr2)

Compare two raw IPv6 address buffers.

Parameters
  • addr1 – Pointer to IPv6 address buffer.

  • addr2 – Pointer to IPv6 address buffer.

Returns

True if the addresses are the same, false otherwise.

static inline bool net_ipv6_is_ll_addr(const struct in6_addr *addr)

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

Parameters
  • addr – A valid pointer on an IPv6 address

Returns

True if it is, false otherwise.

static inline bool net_ipv6_is_ula_addr(const struct in6_addr *addr)

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

Parameters
  • addr – A valid pointer on an IPv6 address

Returns

True if it is, false otherwise.

const struct in6_addr *net_ipv6_unspecified_address(void)

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

Returns

Any IPv6 address.

const struct in_addr *net_ipv4_unspecified_address(void)

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

Returns

Any IPv4 address.

const struct in_addr *net_ipv4_broadcast_address(void)

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

Returns

Broadcast IPv4 address.

bool net_if_ipv4_addr_mask_cmp(struct net_if *iface, const struct in_addr *addr)
static inline 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.

Parameters
  • iface – A valid pointer on an interface

  • addr – IPv4 address

Returns

True if address is in same subnet, false otherwise.

bool net_if_ipv4_is_addr_bcast(struct net_if *iface, const struct in_addr *addr)
static inline 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.

Parameters
  • iface – Interface to use. Must be a valid pointer to an interface.

  • addr – IPv4 address

Returns

True if address is a broadcast address, false otherwise.

struct net_if_addr *net_if_ipv4_addr_lookup(const struct in_addr *addr, struct net_if **iface)
static inline 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.

Parameters
  • addr – A valid pointer on an IPv4 address

Returns

True if IPv4 address is found in one of the network interfaces, False otherwise.

static inline bool net_ipv6_is_addr_unspecified(const struct in6_addr *addr)

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

Parameters
  • addr – IPv6 address.

Returns

True if the address is unspecified, false otherwise.

static inline 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.

Parameters
  • addr – IPv6 address.

Returns

True if the address is solicited node address, false otherwise.

static inline 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::).

Parameters
  • addr – IPv6 address

  • scope – Scope to check

Returns

True if the address is in given scope multicast address, false otherwise.

static inline bool net_ipv6_is_same_mcast_scope(const struct in6_addr *addr_1, const struct in6_addr *addr_2)

Check if the IPv6 addresses have the same multicast scope (FFyx::).

Parameters
  • addr_1 – IPv6 address 1

  • addr_2 – IPv6 address 2

Returns

True if both addresses have same multicast scope, false otherwise.

static inline bool net_ipv6_is_addr_mcast_global(const struct in6_addr *addr)

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

Parameters
  • addr – IPv6 address.

Returns

True if the address is global multicast address, false otherwise.

static inline bool net_ipv6_is_addr_mcast_iface(const struct in6_addr *addr)

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

Parameters
  • addr – IPv6 address.

Returns

True if the address is a interface scope multicast address, false otherwise.

Check if the IPv6 address is a link local scope multicast address (FFx2::).

Parameters
  • addr – IPv6 address.

Returns

True if the address is a link local scope multicast address, false otherwise.

static inline bool net_ipv6_is_addr_mcast_mesh(const struct in6_addr *addr)

Check if the IPv6 address is a mesh-local scope multicast address (FFx3::).

Parameters
  • addr – IPv6 address.

Returns

True if the address is a mesh-local scope multicast address, false otherwise.

static inline bool net_ipv6_is_addr_mcast_site(const struct in6_addr *addr)

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

Parameters
  • addr – IPv6 address.

Returns

True if the address is a site scope multicast address, false otherwise.

static inline bool net_ipv6_is_addr_mcast_org(const struct in6_addr *addr)

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

Parameters
  • addr – IPv6 address.

Returns

True if the address is an organization scope multicast address, false otherwise.

static inline 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.

Parameters
  • addr – IPv6 address.

  • group – Group id IPv6 address, the values must be in network byte order

Returns

True if the IPv6 multicast address belongs to given multicast group, false otherwise.

static inline bool net_ipv6_is_addr_mcast_all_nodes_group(const struct in6_addr *addr)

Check if the IPv6 address belongs to the all nodes multicast group.

Parameters
  • addr – IPv6 address

Returns

True if the IPv6 multicast address belongs to the all nodes multicast group, false otherwise

static inline bool net_ipv6_is_addr_mcast_iface_all_nodes(const struct in6_addr *addr)

Check if the IPv6 address is a interface scope all nodes multicast address (FF01::1).

Parameters
  • addr – IPv6 address.

Returns

True if the address is a interface scope all nodes multicast address, false otherwise.

Check if the IPv6 address is a link local scope all nodes multicast address (FF02::1).

Parameters
  • addr – IPv6 address.

Returns

True if the address is a link local scope all nodes multicast address, false otherwise.

static inline 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 inline void net_ipv6_addr_create(struct in6_addr *addr, uint16_t addr0, uint16_t addr1, uint16_t addr2, uint16_t addr3, uint16_t addr4, uint16_t addr5, uint16_t addr6, uint16_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 inline void net_ipv6_addr_create_ll_allnodes_mcast(struct in6_addr *addr)

Create link local allnodes multicast IPv6 address.

Parameters
  • addr – IPv6 address

static inline void net_ipv6_addr_create_ll_allrouters_mcast(struct in6_addr *addr)

Create link local allrouters multicast IPv6 address.

Parameters
  • addr – IPv6 address

static inline 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 inline 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.

Returns

True if it is, False otherwise

static inline 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.

Parameters
  • addr – Socket address

Returns

Pointer to IPv6 socket address

static inline 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.

Parameters
  • addr – Socket address

Returns

Pointer to IPv4 socket address

static inline 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.

Parameters
  • addr – Socket address

Returns

Pointer to IPv6 socket address

static inline 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.

Parameters
  • addr – Socket address

Returns

Pointer to IPv4 socket address

static inline 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.

Parameters
  • addr – Socket address

Returns

Pointer to linklayer socket address

static inline 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.

Parameters
  • addr – Socket address

Returns

Pointer to CAN 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.

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

Returns

0 if ok, < 0 if error

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

Convert IP address to string form.

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

Returns

dst pointer if ok, NULL if error

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.

Parameters
  • str – String that contains the IP address.

  • str_len – Length of the string to be parsed.

  • addr – Pointer to user supplied struct sockaddr.

Returns

True if parsing could be done, false otherwise.

static inline int32_t net_tcp_seq_cmp(uint32_t seq1, uint32_t seq2)

Compare TCP sequence numbers.

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

Parameters
  • seq1 – First sequence number

  • seq2 – Seconds sequence number

Returns

< 0 if seq1 < seq2, 0 if seq1 == seq2, > 0 if seq > seq2

static inline bool net_tcp_seq_greater(uint32_t seq1, uint32_t seq2)

Check that one TCP sequence number is greater.

This is convenience function on top of net_tcp_seq_cmp().

Parameters
  • seq1 – First sequence number

  • seq2 – Seconds sequence number

Returns

True if seq > seq2

int net_bytes_from_str(uint8_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”

Parameters
  • buf – Pointer to memory where the bytes are written.

  • buf_len – Length of the memory area.

  • src – String of bytes.

Returns

0 if ok, <0 if error

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.

Parameters
  • prio – Network priority

Returns

Tx traffic class that handles that priority network traffic.

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.

Parameters
  • prio – Network priority

Returns

Rx traffic class that handles that priority network traffic.

static inline enum net_priority net_vlan2priority(uint8_t priority)

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

Parameters
  • priority – VLAN priority

Returns

Network priority

static inline uint8_t net_priority2vlan(enum net_priority priority)

Convert network packet priority to network packet VLAN priority.

Parameters
  • priority – Packet priority

Returns

VLAN priority (PCP)

const char *net_family2str(sa_family_t family)

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

Parameters
  • family – Network address family code

Returns

Network address family as a string, or NULL if family is unknown.

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_in6_ptr
#include <net_ip.h>
struct sockaddr_in
#include <net_ip.h>

Socket address struct for IPv4.

struct sockaddr_in_ptr
#include <net_ip.h>
struct sockaddr_ll
#include <net_ip.h>

Socket address struct for packet socket.

struct sockaddr_ll_ptr
#include <net_ip.h>
struct sockaddr_can_ptr
#include <net_ip.h>
struct iovec
#include <net_ip.h>
struct msghdr
#include <net_ip.h>
struct cmsghdr
#include <net_ip.h>
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

Public Members

struct net_addr *remote_addr

IPv6/IPv4 remote address

struct net_addr *local_addr

IPv6/IPv4 local address

uint16_t remote_port

UDP/TCP remote port

uint16_t local_port

UDP/TCP local port

enum net_ip_protocol ip_proto

IP protocol