IPv4/IPv6 Primitives and Helpers¶
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
-
PF_LTE
¶ Specific to LTE.
-
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
-
AF_LTE
¶ Specific to LTE.
-
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:
-
enumerator
IPPROTO_IP
¶ IP protocol (pseudo-val for setsockopt()
-
enumerator
IPPROTO_ICMP
¶ ICMP protocol
-
enumerator
IPPROTO_TCP
¶ TCP protocol
-
enumerator
IPPROTO_UDP
¶ UDP protocol
-
enumerator
IPPROTO_IPV6
¶ IPv6 protocol
-
enumerator
IPPROTO_ICMPV6
¶ ICMPv6 protocol
-
enumerator
IPPROTO_RAW
¶ RAW IP packets
-
enumerator
-
enum
net_ip_protocol_secure
¶ Protocol numbers for TLS protocols
Values:
-
enumerator
IPPROTO_TLS_1_0
¶ TLS 1.0 protocol
-
enumerator
IPPROTO_TLS_1_1
¶ TLS 1.1 protocol
-
enumerator
IPPROTO_TLS_1_2
¶ TLS 1.2 protocol
-
enumerator
IPPROTO_DTLS_1_0
¶ DTLS 1.0 protocol
-
enumerator
IPPROTO_DTLS_1_2
¶ DTLS 1.2 protocol
-
enumerator
-
enum
net_sock_type
¶ Socket type
Values:
-
enumerator
SOCK_STREAM
¶ Stream socket type
-
enumerator
SOCK_DGRAM
¶ Datagram socket type
-
enumerator
SOCK_RAW
¶ RAW socket type
-
enumerator
SOCK_MGMT
¶ Management socket type
-
enumerator
-
enum
net_ip_mtu
¶ Values:
-
enumerator
NET_IPV6_MTU
¶ IPv6 MTU length. We must be able to receive this size IPv6 packet without fragmentation.
-
enumerator
NET_IPV4_MTU
¶ IPv4 MTU length. We must be able to receive this size IPv4 packet without fragmentation.
-
enumerator
-
enum
net_priority
¶ Network packet priority settings described in IEEE 802.1Q Annex I.1
Values:
-
enumerator
NET_PRIORITY_BK
¶ Background (lowest)
-
enumerator
NET_PRIORITY_BE
¶ Best effort (default)
-
enumerator
NET_PRIORITY_EE
¶ Excellent effort
-
enumerator
NET_PRIORITY_CA
¶ Critical applications (highest)
-
enumerator
NET_PRIORITY_VI
¶ Video, < 100 ms latency and jitter
-
enumerator
NET_PRIORITY_VO
¶ Voice, < 10 ms latency and jitter
-
enumerator
NET_PRIORITY_IC
¶ Internetwork control
-
enumerator
NET_PRIORITY_NC
¶ Network control
-
enumerator
-
enum
net_addr_state
¶ What is the current state of the network address
Values:
-
enumerator
NET_ADDR_ANY_STATE
¶ Default (invalid) address type
-
enumerator
NET_ADDR_TENTATIVE
¶ Tentative address
-
enumerator
NET_ADDR_PREFERRED
¶ Preferred address
-
enumerator
NET_ADDR_DEPRECATED
¶ Deprecated address
-
enumerator
-
enum
net_addr_type
¶ How the network address is assigned to network interface
Values:
-
enumerator
NET_ADDR_ANY
¶ 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
-
enumerator
Functions
-
static inline 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 inline 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 inline 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 inline 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 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.
- 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 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).
- Return
True if address is a loopback address, False otherwise.
- Parameters
addr
: IPv4 address
-
static inline 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 inline 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 inline 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 inline 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 inline 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 inline 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.
-
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.
- Return
True if address is in same subnet, false otherwise.
- Parameters
iface
: A valid pointer on an interfaceaddr
: IPv4 address
-
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.
- 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 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.
- 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 inline 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 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.
- Return
True if the address is solicited node address, false otherwise.
- Parameters
addr
: IPv6 address.
-
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::).
- Return
True if the address is in given scope multicast address, false otherwise.
- Parameters
addr
: IPv6 addressscope
: Scope to check
-
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::).
- Return
True if both addresses have same multicast scope, false otherwise.
- Parameters
addr_1
: IPv6 address 1addr_2
: IPv6 address 2
-
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).
- Return
True if the address is global multicast address, false otherwise.
- Parameters
addr
: IPv6 address.
-
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::).
- Return
True if the address is a interface scope multicast address, false otherwise.
- Parameters
addr
: IPv6 address.
-
static inline bool
net_ipv6_is_addr_mcast_link
(const struct in6_addr *addr)¶ Check if the IPv6 address is a link local scope multicast address (FFx2::).
- Return
True if the address is a link local scope multicast address, false otherwise.
- Parameters
addr
: IPv6 address.
-
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::).
- Return
True if the address is a mesh-local scope multicast address, false otherwise.
- Parameters
addr
: IPv6 address.
-
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::).
- Return
True if the address is a site scope multicast address, false otherwise.
- Parameters
addr
: IPv6 address.
-
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::).
- Return
True if the address is an organization scope multicast address, false otherwise.
- Parameters
addr
: IPv6 address.
-
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.
- 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 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.
- Return
True if the IPv6 multicast address belongs to the all nodes multicast group, false otherwise
- Parameters
addr
: IPv6 address
-
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).
- Return
True if the address is a interface scope all nodes multicast address, false otherwise.
- Parameters
addr
: IPv6 address.
-
static inline bool
net_ipv6_is_addr_mcast_link_all_nodes
(const struct in6_addr *addr)¶ Check if the IPv6 address is a link local scope all nodes multicast address (FF02::1).
- Return
True if the address is a link local scope all nodes multicast address, false otherwise.
- Parameters
addr
: IPv6 address.
-
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 addressaddr0
: 16-bit word which is part of the addressaddr1
: 16-bit word which is part of the addressaddr2
: 16-bit word which is part of the addressaddr3
: 16-bit word which is part of the addressaddr4
: 16-bit word which is part of the addressaddr5
: 16-bit word which is part of the addressaddr6
: 16-bit word which is part of the addressaddr7
: 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 addresslladdr
: 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.
- Return
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.
- Return
Pointer to IPv6 socket address
- Parameters
addr
: 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.
- Return
Pointer to IPv4 socket address
- Parameters
addr
: 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.
- Return
Pointer to IPv6 socket address
- Parameters
addr
: 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.
- Return
Pointer to IPv4 socket address
- Parameters
addr
: 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.
- Return
Pointer to linklayer socket address
- Parameters
addr
: 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.
- 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
-
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
-
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 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.
- Return
< 0 if seq1 < seq2, 0 if seq1 == seq2, > 0 if seq > seq2
- Parameters
seq1
: First sequence numberseq2
: Seconds sequence number
-
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().
- Return
True if seq > seq2
- Parameters
seq1
: First sequence numberseq2
: Seconds sequence number
-
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”
- 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 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.
- Return
Network priority
- Parameters
priority
: VLAN priority
-
static inline uint8_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_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
-