mbgtools-lx  4.2.8
lan_util.h File Reference
#include <mbg_tgt.h>
#include <gpsdefs.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <ifaddrs.h>

Go to the source code of this file.

Macros

#define _ext   extern
 
#define MAC_SEP_CHAR   ':'
 
#define MAC_SEP_CHAR_ALT   '-'
 
#define MAX_IP4_BITS   ( 8 * (int) sizeof( IP4_ADDR ) )
 
#define IP4_MSB_MASK   ( 1UL << ( MAX_IP4_BITS - 1 ) )
 
#define MIN_IP4_CIDR_NETMASK_BITS   0
 
#define MAX_IP4_CIDR_NETMASK_BITS   MAX_IP4_BITS
 
#define IP6_MSB_MASK   ( 1UL << ( 8 - 1 ) )
 
#define MIN_IP6_CIDR_NETMASK_BITS   0
 
#define MAX_IP6_CIDR_NETMASK_BITS   IP6_ADDR_BITS
 
#define _ip4_addr_to_str(_s, _a)   snprint_ip4_addr( _s, sizeof( _s ), _a, NULL )
 
#define _mac_addr_to_str(_s, _a)   snprint_mac_addr( _s, sizeof( _s ), _a )
 

Functions

static __mbg_inline IP4_ADDR ip4_net_mask_from_cidr (int netmask_bits)
 Compute an IP4 net mask according to the number of CIDR netmask bits. More...
 
static __mbg_inline IP4_ADDR ip4_broad_addr_from_addr (const IP4_ADDR *p_addr, const IP4_ADDR *p_mask)
 Determine the broadcast address for an IP4 address plus net mask. More...
 
static __mbg_inline IP4_ADDR ip4_net_part_from_addr (const IP4_ADDR *p_addr, const IP4_ADDR *p_mask)
 Determine the network part of an IP4 address based on the net mask. More...
 
static __mbg_inline int ip4_net_part_matches (const IP4_ADDR *p_addr1, const IP4_ADDR *p_addr2, const IP4_ADDR *p_mask)
 Check if two IP4 addresses have the same network part. More...
 
int get_ip4_net_mask_bits (const IP4_ADDR *p_mask)
 Count the number of sequential bits in an IPv4 net mask. More...
 
size_t snprint_ip4_addr (char *s, size_t max_len, const IP4_ADDR *p_addr, const char *info)
 Print an IPv4 address to a dotted quad formatted string. More...
 
size_t snprint_ip4_cidr_addr (char *s, size_t max_len, const IP4_ADDR *p_addr, const IP4_ADDR *p_mask, const char *info)
 Print an IPv4 address plus net mask in CIDR notation to a string. More...
 
int str_to_ip4_addr (IP4_ADDR *p_addr, const char *s)
 Convert a string to an IP4_ADDR. More...
 
int cidr_str_to_ip4_addr_and_net_mask (IP4_ADDR *p_addr, IP4_ADDR *p_mask, const char *cidr_str)
 Convert a string in CIDR notation to an IP4_ADDR and net mask. More...
 
int get_ip6_net_mask_bits (const IP6_ADDR *p_mask)
 Count the number of sequential bits in an IPv6 net mask. More...
 
size_t snprint_ip6_addr (char *s, size_t max_len, const IP6_ADDR *p_addr, const char *info)
 Print an IPv6 address in optimized format to a string. More...
 
size_t snprint_ip6_cidr_addr (char *s, size_t max_len, const IP6_ADDR *p_addr, const IP6_ADDR *p_mask, const char *info)
 Print an IPv6 address plus net mask to string in CIDR notation. More...
 
size_t snprint_ip6_cidr_mask_addr (char *s, size_t max_len, const IP6_ADDR *p_addr, const int cidr_mask_bits, const char *info)
 Print an IPv6 address plus number of net mask bits to string in CIDR notation. More...
 
int str_to_ip6_addr (IP6_ADDR *p_addr, const char *s)
 Convert a string to an IP6_ADDR. More...
 
int cidr_str_to_ip6_addr_and_net_mask (IP6_ADDR *p_addr, IP6_ADDR *p_mask, const char *cidr_str)
 Convert a string in CIDR notation to an IP6_ADDR and net mask. More...
 
int cidr_str_to_ip6_addr_and_cidr_bits (IP6_ADDR *p_addr, int *p_cidr, const char *cidr_str)
 Convert a string in CIDR notation to an IP6_ADDR and net mask bits. More...
 
void ip6_net_mask_from_cidr (IP6_ADDR *p_mask, int netmask_bits)
 Compute an IPv6 net mask according to the number of CIDR netmask bits. More...
 
void ip6_net_part_from_addr (IP6_ADDR *p_net_part, const IP6_ADDR *p_addr, const IP6_ADDR *p_mask)
 Determine the network part of an IPv6 address based on the net mask. More...
 
size_t snprint_octets (char *s, size_t max_len, const uint8_t *octets, int num_octets, char sep, const char *info)
 Print a MAC ID or similar array of octets to a string. More...
 
size_t snprint_ptp_clock_id (char *s, size_t max_len, const PTP_CLOCK_ID *p)
 Print a PTP_CLOCK_ID to a string. More...
 
size_t snprint_mac_addr (char *s, size_t max_len, const MBG_MAC_ADDR *p_mac_addr)
 Print a MAC address to a string. More...
 
int str_to_octets (uint8_t *octets, int num_octets, const char *s)
 Set a MAC ID or a similar array of octets from a string. More...
 
bool octets_are_all_zero (const uint8_t *octets, int num_octets)
 Check if an array of octets is all zero. More...
 
bool mac_addr_is_all_zero (const MBG_MAC_ADDR *p_addr)
 Check if a MAC address is all zero. More...
 
int do_siocg_ioctl (const char *if_name, int ioctl_code, struct ifreq *p_ifreq)
 Do a SIOCGxxx IOCTL call to read specific information from a LAN interface. More...
 
int get_port_intf_idx (const char *if_name, int *p_intf_idx)
 Retrieve the index of a specific network interface. More...
 
int get_port_mac_addr (const char *if_name, MBG_MAC_ADDR *p_mac_addr)
 Retrieve the MAC address of a network interface. More...
 
int check_port_link (const char *if_name)
 Check the link state of a network interface. More...
 
int check_port_status (const char *if_name, int *p_speed, int *p_duplex)
 Check the state of a network interface. More...
 
int get_port_ip4_addr (const char *if_name, IP4_ADDR *p_addr)
 Retrieve the IPv4 address of a network interface. More...
 
int get_port_ip4_netmask (const char *if_name, IP4_ADDR *p_addr)
 Retrieve the IPv4 net mask of a network interface. More...
 
int get_port_ip4_broad_addr (const char *if_name, IP4_ADDR *p_addr)
 Retrieve the IPv4 broadcast address of a network interface. More...
 
int get_ip4_gateway (IP4_ADDR *p_addr)
 Retrieve the IPv4 gateway (default route) More...
 
int get_port_ip4_addr_str (const char *if_name, char *p_addr_buf, int buf_size)
 Retrieve the IPv4 address of a network interface as string. More...
 
int get_port_ip4_netmask_str (const char *if_name, char *p_addr_buf, int buf_size)
 Retrieve the IPv4 net mask of a network interface as string. More...
 
int get_port_ip4_broad_addr_str (const char *if_name, char *p_addr_buf, int buf_size)
 Retrieve the IPv4 broadcast address of a network interface as string. More...
 
int get_port_ip4_settings (const char *if_name, IP4_SETTINGS *p)
 Retrieve the current IPv4 settings of a network interface. More...
 

Macro Definition Documentation

◆ _ext

#define _ext   extern

Definition at line 77 of file lan_util.h.

◆ _ip4_addr_to_str

#define _ip4_addr_to_str (   _s,
  _a 
)    snprint_ip4_addr( _s, sizeof( _s ), _a, NULL )

Definition at line 200 of file lan_util.h.

◆ _mac_addr_to_str

#define _mac_addr_to_str (   _s,
  _a 
)    snprint_mac_addr( _s, sizeof( _s ), _a )

Definition at line 203 of file lan_util.h.

◆ IP4_MSB_MASK

#define IP4_MSB_MASK   ( 1UL << ( MAX_IP4_BITS - 1 ) )

Definition at line 104 of file lan_util.h.

Referenced by get_ip4_net_mask_bits().

◆ IP6_MSB_MASK

#define IP6_MSB_MASK   ( 1UL << ( 8 - 1 ) )

Definition at line 110 of file lan_util.h.

Referenced by get_ip6_net_mask_bits().

◆ MAC_SEP_CHAR

#define MAC_SEP_CHAR   ':'

Definition at line 94 of file lan_util.h.

Referenced by set_ptp_cfg(), show_ptp_cfg(), snprint_mac_addr(), and str_to_octets().

◆ MAC_SEP_CHAR_ALT

#define MAC_SEP_CHAR_ALT   '-'

Definition at line 98 of file lan_util.h.

Referenced by show_ptp_state(), and str_to_octets().

◆ MAX_IP4_BITS

#define MAX_IP4_BITS   ( 8 * (int) sizeof( IP4_ADDR ) )

Definition at line 102 of file lan_util.h.

Referenced by get_ip4_net_mask_bits(), and ip4_net_mask_from_cidr().

◆ MAX_IP4_CIDR_NETMASK_BITS

#define MAX_IP4_CIDR_NETMASK_BITS   MAX_IP4_BITS

Definition at line 107 of file lan_util.h.

Referenced by cidr_str_to_ip4_addr_and_net_mask(), and snprint_ip4_cidr_addr().

◆ MAX_IP6_CIDR_NETMASK_BITS

#define MAX_IP6_CIDR_NETMASK_BITS   IP6_ADDR_BITS

◆ MIN_IP4_CIDR_NETMASK_BITS

#define MIN_IP4_CIDR_NETMASK_BITS   0

Definition at line 106 of file lan_util.h.

Referenced by cidr_str_to_ip4_addr_and_net_mask(), and snprint_ip4_cidr_addr().

◆ MIN_IP6_CIDR_NETMASK_BITS

#define MIN_IP6_CIDR_NETMASK_BITS   0

Function Documentation

◆ check_port_link()

int check_port_link ( const char *  if_name)

Check the link state of a network interface.

Parameters
[in]if_nameName of the interface
Returns
1 if link detected on port, 0 if no link detected on port, one of the MBG_ERROR_CODES in case of an error

Definition at line 1267 of file lan_util.c.

References do_siocg_ioctl(), MBG_ERR_NOT_SUPP_ON_OS, and mbg_rc_is_success.

Referenced by get_port_ip4_settings().

◆ check_port_status()

int check_port_status ( const char *  if_name,
int *  p_speed,
int *  p_duplex 
)

Check the state of a network interface.

Parameters
[in]if_nameName of the interface to check
[out]p_speedOptional pointer to a variable to take up the link speed, may be NULL
[out]p_duplexOptional pointer to a variable to take up the duplex state, may be NULL
Returns
1 if link detected on port, 0 if no link detected on port, one of the MBG_ERROR_CODES in case of an error

Definition at line 1302 of file lan_util.c.

References do_siocg_ioctl(), MBG_ERR_NOT_SUPP_ON_OS, and mbg_rc_is_success.

◆ cidr_str_to_ip4_addr_and_net_mask()

int cidr_str_to_ip4_addr_and_net_mask ( IP4_ADDR p_addr,
IP4_ADDR p_mask,
const char *  cidr_str 
)

Convert a string in CIDR notation to an IP4_ADDR and net mask.

If output parameters are specified as NULL then this function can be used to check if the CIDR string is formally correct.

Parameters
[out]p_addrPointer to an IP4_ADDR variable to be filled with the IPv4 address, or NULL
[out]p_maskPointer to an IP4_ADDR variable to be filled with the IPv4 net mask, or NULL
[in]cidr_strThe string to be converted, in CIDR format, e.g. "172.16.3.250/24"
Returns
A number >= 0 (MBG_SUCCESS) according to the number of characters evaluated from the input string, or one of the MBG_ERROR_CODES on error, specifically MBG_ERR_PARM_FMT if an invalid number or character was found in the string.
See also
snprint_ip4_addr
snprint_ip4_cidr_addr
str_to_ip4_addr

Definition at line 349 of file lan_util.c.

References ip4_net_mask_from_cidr(), MAX_IP4_CIDR_NETMASK_BITS, MBG_ERR_PARM_FMT, MBG_ERR_RANGE, mbg_rc_is_error, MIN_IP4_CIDR_NETMASK_BITS, and str_to_ip4_addr().

◆ cidr_str_to_ip6_addr_and_cidr_bits()

int cidr_str_to_ip6_addr_and_cidr_bits ( IP6_ADDR p_addr,
int *  p_cidr,
const char *  cidr_str 
)

Convert a string in CIDR notation to an IP6_ADDR and net mask bits.

If output parameters are specified as NULL then this function can be used to check if the CIDR string is formally correct.

Parameters
[out]p_addrPointer to an IP6_ADDR variable for the IPv6 address, or NULL
[out]p_cidrPointer to an int variable for the net mask bits, or NULL
[in]cidr_strThe string to be converted, in CIDR format, e.g. "2001:0DB8:0:CD30::/64"
Returns
A number >= 0 (MBG_SUCCESS) according to the number of characters evaluated from the input string, or one of the MBG_ERROR_CODES on error, specifically MBG_ERR_PARM_FMT if an invalid number or character was found in the string.
See also
snprint_ip6_addr
snprint_ip6_cidr_addr
str_to_ip6_addr

Definition at line 859 of file lan_util.c.

References MAX_IP6_CIDR_NETMASK_BITS, MBG_ERR_PARM_FMT, MBG_ERR_RANGE, mbg_rc_is_error, MIN_IP6_CIDR_NETMASK_BITS, and str_to_ip6_addr().

Referenced by cidr_str_to_ip6_addr_and_net_mask().

◆ cidr_str_to_ip6_addr_and_net_mask()

int cidr_str_to_ip6_addr_and_net_mask ( IP6_ADDR p_addr,
IP6_ADDR p_mask,
const char *  cidr_str 
)

Convert a string in CIDR notation to an IP6_ADDR and net mask.

If output parameters are specified as NULL then this function can be used to check if the CIDR string is formally correct.

Parameters
[out]p_addrPointer to an IP6_ADDR variable to be filled up with the IPv6 address, or NULL
[out]p_maskPointer to an IP6_ADDR variable to be filled up with the net mask bits, or NULL
[in]cidr_strThe string to be converted, in CIDR format, e.g. "2001:0DB8:0:CD30::/64"
Returns
A number >= 0 (MBG_SUCCESS) according to the number of characters evaluated from the input string, or one of the MBG_ERROR_CODES on error, specifically MBG_ERR_PARM_FMT if an invalid number or character was found in the string.
See also
snprint_ip4_addr
snprint_ip4_cidr_addr
str_to_ip4_addr

Definition at line 823 of file lan_util.c.

References cidr_str_to_ip6_addr_and_cidr_bits(), ip6_net_mask_from_cidr(), and mbg_rc_is_error.

◆ do_siocg_ioctl()

int do_siocg_ioctl ( const char *  if_name,
int  ioctl_code,
struct ifreq *  p_ifreq 
)

Do a SIOCGxxx IOCTL call to read specific information from a LAN interface.

Parameters
[in]if_nameName of the interface
[in]ioctl_codeOne of the predefined system SIOCGxxx IOCTL codes
[out]p_ifreqPointer to a request buffer
Returns
One of the MBG_RETURN_CODES

Definition at line 1155 of file lan_util.c.

References MBG_ERR_PARM_FMT, mbg_get_last_socket_error(), MBG_SUCCESS, and strncpy_safe().

Referenced by check_port_link(), check_port_status(), get_port_intf_idx(), get_port_mac_addr(), and get_specific_port_ip4_addr().

◆ get_ip4_gateway()

int get_ip4_gateway ( IP4_ADDR p_addr)

Retrieve the IPv4 gateway (default route)

Parameters
[out]p_addrPointer to address field to be filled up
Returns
One of the MBG_RETURN_CODES On error, *p_addr is set to all 0.

Definition at line 1679 of file lan_util.c.

References route_info::dstAddr, route_info::gateWay, route_info::ifName, MBG_ERR_NOT_SUPP_ON_OS, mbg_get_last_socket_error(), mbg_rc_is_error, mbg_strerror(), MBG_SUCCESS, nl_parse_routes(), nl_read(), snprintf_safe(), and route_info::srcAddr.

Referenced by get_port_ip4_settings().

◆ get_ip4_net_mask_bits()

int get_ip4_net_mask_bits ( const IP4_ADDR p_mask)

Count the number of sequential bits in an IPv4 net mask.

Counting starts from MSB, i.e. for 0xC0 and 0xC1 the results are both 2 since only the 2 MSBs are sequentially set.

Parameters
[in]p_maskThe IPv4 net mask to be evaluated
Returns
The number of sequential MSB bits set in *p_mask
See also
ip4_net_mask_from_cidr

Definition at line 183 of file lan_util.c.

References IP4_MSB_MASK, and MAX_IP4_BITS.

Referenced by mbg_get_all_net_cfg_info(), mbg_get_all_net_status_info(), and snprint_ip4_cidr_addr().

◆ get_ip6_net_mask_bits()

int get_ip6_net_mask_bits ( const IP6_ADDR p_mask)

Count the number of sequential bits in an IPv6 net mask.

Counting starts from MSB, i.e. for 0xC0 and 0xC1 the results are both 2 since only the 2 MSBs are sequentially set.

Parameters
[in]p_maskThe IPv6 net mask to be evaluated
Returns
The number of sequential MSB bits set in *p_mask
See also
ip6_net_mask_from_cidr

Definition at line 417 of file lan_util.c.

References IP6_ADDR::b, IP6_ADDR_BYTES, IP6_MSB_MASK, and MAX_IP6_CIDR_NETMASK_BITS.

Referenced by snprint_ip6_cidr_addr().

◆ get_port_intf_idx()

int get_port_intf_idx ( const char *  if_name,
int *  p_intf_idx 
)

Retrieve the index of a specific network interface.

Parameters
[in]if_nameName of the interface
[out]p_intf_idxPointer to a variable to be filled up
Returns
One of the MBG_RETURN_CODES. On error, *p_intf_idx is set to -1.

Definition at line 1197 of file lan_util.c.

References do_siocg_ioctl(), MBG_ERR_NOT_SUPP_ON_OS, and mbg_rc_is_success.

◆ get_port_ip4_addr()

int get_port_ip4_addr ( const char *  if_name,
IP4_ADDR p_addr 
)

Retrieve the IPv4 address of a network interface.

Parameters
[in]if_nameName of the interface
[out]p_addrPointer to address field to be filled up
Returns
One of the MBG_RETURN_CODES On error, *p_addr is set to all 0.
See also
get_port_ip4_settings
get_port_ip4_addr_str
get_port_ip4_netmask
get_port_ip4_netmask_str
get_port_ip4_broad_addr
get_port_ip4_broad_addr_str
get_specific_port_ip4_addr

Definition at line 1404 of file lan_util.c.

References get_specific_port_ip4_addr().

Referenced by get_port_ip4_addr_str(), and get_port_ip4_settings().

◆ get_port_ip4_addr_str()

int get_port_ip4_addr_str ( const char *  if_name,
char *  p_addr_buf,
int  buf_size 
)

Retrieve the IPv4 address of a network interface as string.

Parameters
[in]if_nameName of the interface
[out]p_addr_bufPointer to the string buffer to be filled up
[in]buf_sizesize of the string buffer
Returns
One of the MBG_RETURN_CODES On error, a string according to "0.0.0.0" is generated.
See also
get_port_ip4_settings
get_port_ip4_addr
get_port_ip4_netmask
get_port_ip4_netmask_str
get_port_ip4_broad_addr
get_port_ip4_broad_addr_str
get_specific_port_ip4_addr

Definition at line 1826 of file lan_util.c.

References get_port_ip4_addr(), and snprint_ip4_addr().

◆ get_port_ip4_broad_addr()

int get_port_ip4_broad_addr ( const char *  if_name,
IP4_ADDR p_addr 
)

Retrieve the IPv4 broadcast address of a network interface.

Parameters
[in]if_nameName of the interface
[out]p_addrPointer to address field to be filled up
Returns
One of the MBG_RETURN_CODES On error, *p_addr is set to all 0.
See also
get_port_ip4_settings
get_port_ip4_addr
get_port_ip4_addr_str
get_port_ip4_netmask
get_port_ip4_netmask_str
get_port_ip4_broad_addr_str
get_specific_port_ip4_addr

Definition at line 1456 of file lan_util.c.

References get_specific_port_ip4_addr().

Referenced by get_port_ip4_broad_addr_str(), and get_port_ip4_settings().

◆ get_port_ip4_broad_addr_str()

int get_port_ip4_broad_addr_str ( const char *  if_name,
char *  p_addr_buf,
int  buf_size 
)

Retrieve the IPv4 broadcast address of a network interface as string.

Parameters
[in]if_nameName of the interface
[out]p_addr_bufPointer to the string buffer to be filled up
[in]buf_sizesize of the string buffer
Returns
One of the MBG_RETURN_CODES On error, a string according to "0.0.0.0" is generated.
See also
get_port_ip4_settings
get_port_ip4_addr
get_port_ip4_addr_str
get_port_ip4_netmask
get_port_ip4_netmask_str
get_port_ip4_broad_addr
get_specific_port_ip4_addr

Definition at line 1892 of file lan_util.c.

References get_port_ip4_broad_addr(), and snprint_ip4_addr().

◆ get_port_ip4_netmask()

int get_port_ip4_netmask ( const char *  if_name,
IP4_ADDR p_addr 
)

Retrieve the IPv4 net mask of a network interface.

Parameters
[in]if_nameName of the interface
[out]p_addrPointer to address field to be filled up
Returns
One of the MBG_RETURN_CODES On error, *p_addr is set to all 0.
See also
get_port_ip4_settings
get_port_ip4_addr
get_port_ip4_addr_str
get_port_ip4_netmask_str
get_port_ip4_broad_addr
get_port_ip4_broad_addr_str
get_specific_port_ip4_addr

Definition at line 1430 of file lan_util.c.

References get_specific_port_ip4_addr().

Referenced by get_port_ip4_netmask_str(), and get_port_ip4_settings().

◆ get_port_ip4_netmask_str()

int get_port_ip4_netmask_str ( const char *  if_name,
char *  p_addr_buf,
int  buf_size 
)

Retrieve the IPv4 net mask of a network interface as string.

Parameters
[in]if_nameName of the interface
[out]p_addr_bufPointer to the string buffer to be filled up
[in]buf_sizesize of the string buffer
Returns
One of the MBG_RETURN_CODES On error, a string according to "0.0.0.0" is generated.
See also
get_port_ip4_settings
get_port_ip4_addr
get_port_ip4_addr_str
get_port_ip4_netmask
get_port_ip4_broad_addr
get_port_ip4_broad_addr_str
get_specific_port_ip4_addr

Definition at line 1859 of file lan_util.c.

References get_port_ip4_netmask(), and snprint_ip4_addr().

◆ get_port_ip4_settings()

◆ get_port_mac_addr()

int get_port_mac_addr ( const char *  if_name,
MBG_MAC_ADDR p_mac_addr 
)

Retrieve the MAC address of a network interface.

Parameters
[in]if_nameName of the interface
[out]p_mac_addrPointer to the MAC address buffer to be filled up
Returns
One of the MBG_RETURN_CODES On error, the MAC address is set to all 0

Definition at line 1232 of file lan_util.c.

References do_siocg_ioctl(), MBG_ERR_NOT_SUPP_ON_OS, and mbg_rc_is_success.

◆ ip4_broad_addr_from_addr()

static __mbg_inline IP4_ADDR ip4_broad_addr_from_addr ( const IP4_ADDR p_addr,
const IP4_ADDR p_mask 
)
static

Determine the broadcast address for an IP4 address plus net mask.

E.g. IP 0xAC1003FA, net mask 0xFFFFFF00 yields broadcast addr 0xAC1003FF. In dotted quad notation, IP 172.16.3.250 with net mask 255.255.255.0 result in broadcast address 172.16.3.255.

Parameters
p_addrThe full IP4 address
p_maskThe IP4 net mask
Returns
The determined IP4 broadcast address

Definition at line 151 of file lan_util.h.

Referenced by mbg_save_all_net_cfg_info().

◆ ip4_net_mask_from_cidr()

static __mbg_inline IP4_ADDR ip4_net_mask_from_cidr ( int  netmask_bits)
static

Compute an IP4 net mask according to the number of CIDR netmask bits.

E.g. the 24 bits mentioned in "172.16.3.250/24" result in 0xFFFFFF00, corresponding to 255.255.255.0 in dotted quad notation.

Parameters
netmask_bitsNumber of netmask bits from CIDR notation
Returns
The IPv4 net mask
See also
get_ip4_net_mask_bits()

Definition at line 130 of file lan_util.h.

References MAX_IP4_BITS.

Referenced by cidr_str_to_ip4_addr_and_net_mask(), and mbg_save_all_net_cfg_info().

◆ ip4_net_part_from_addr()

static __mbg_inline IP4_ADDR ip4_net_part_from_addr ( const IP4_ADDR p_addr,
const IP4_ADDR p_mask 
)
static

Determine the network part of an IP4 address based on the net mask.

E.g. IP 0xAC1003FA, net mask 0xFFFFFF00 yields network part 0xAC100300. In dotted quad notation, IP 172.16.3.250 with net mask 255.255.255.0 results in network part 172.16.3.0.

Parameters
p_addrThe full IP4 address
p_maskThe IP4 net mask
Returns
The network part of the IP4 address

Definition at line 172 of file lan_util.h.

Referenced by ip4_net_part_matches().

◆ ip4_net_part_matches()

static __mbg_inline int ip4_net_part_matches ( const IP4_ADDR p_addr1,
const IP4_ADDR p_addr2,
const IP4_ADDR p_mask 
)
static

Check if two IP4 addresses have the same network part.

Parameters
p_addr1The first IP4 address to check
p_addr2The second IP4 address to check
p_maskThe IP4 net mask
Returns
true, if the network parts are matching

Definition at line 190 of file lan_util.h.

References ip4_net_part_from_addr().

◆ ip6_net_mask_from_cidr()

void ip6_net_mask_from_cidr ( IP6_ADDR p_mask,
int  netmask_bits 
)

Compute an IPv6 net mask according to the number of CIDR netmask bits.

E.g. the 64 bits mentioned in "2001:0DB8:0:CD30::/64" result in 2^64, corresponding to FFFF:FFFF:FFFF:FFFF:: in IPv6 notation.

Parameters
[out]p_maskPointer to an IP6_ADDR variable for the IPv6 netmask
[in]netmask_bitsNumber of netmask bits from CIDR notation
See also
get_ip6_net_mask_bits

Definition at line 918 of file lan_util.c.

References IP6_ADDR::b, IP6_ADDR_BITS, and IP6_ADDR_BYTES.

Referenced by cidr_str_to_ip6_addr_and_net_mask(), and snprint_ip6_cidr_mask_addr().

◆ ip6_net_part_from_addr()

void ip6_net_part_from_addr ( IP6_ADDR p_net_part,
const IP6_ADDR p_addr,
const IP6_ADDR p_mask 
)

Determine the network part of an IPv6 address based on the net mask.

E.g. IP "2001:0DB8:0:CD30::", net mask "FFFF:FFFF::" yields network part "2001:0DB8::".

Parameters
[out]p_net_partThe extracted network part of the IPv6 address
[in]p_addrThe IPv6 address to be evaluated
[in]p_maskThe associated IPv6 net mask

Definition at line 961 of file lan_util.c.

References IP6_ADDR::b, and IP6_ADDR_BYTES.

◆ mac_addr_is_all_zero()

bool mac_addr_is_all_zero ( const MBG_MAC_ADDR p_addr)

Check if a MAC address is all zero.

Parameters
[in]p_addrPointer to a MAC address to be checked
Returns
true if all bytes of the MAC address are 0, else false
See also
octets_are_all_zero

Definition at line 1135 of file lan_util.c.

References MBG_MAC_ADDR::b, and octets_are_all_zero().

◆ octets_are_all_zero()

bool octets_are_all_zero ( const uint8_t octets,
int  num_octets 
)

Check if an array of octets is all zero.

Parameters
[in]octetsPointer to the array of octets
[in]num_octetsNumber of octets
Returns
true if all bytes are 0, else false
See also
snprint_octets
snprint_mac_addr
str_to_octets

Definition at line 1110 of file lan_util.c.

Referenced by mac_addr_is_all_zero().

◆ snprint_ip4_addr()

size_t snprint_ip4_addr ( char *  s,
size_t  max_len,
const IP4_ADDR p_addr,
const char *  info 
)

Print an IPv4 address to a dotted quad formatted string.

Parameters
[out]sThe string buffer to be filled
[in]max_lenSize of the output buffer for 0-terminated string
[in]p_addrThe IPv4 address to be evaluated
[in]infoAn optional string which is prepended to the string, or NULL
Returns
The overall number of characters printed to the string
See also
snprint_ip4_cidr_addr
str_to_ip4_addr
cidr_str_to_ip4_addr_and_net_mask

Definition at line 217 of file lan_util.c.

References BYTE_0, BYTE_1, BYTE_2, BYTE_3, and snprintf_safe().

Referenced by get_port_ip4_addr_str(), get_port_ip4_broad_addr_str(), get_port_ip4_netmask_str(), set_lan_intf(), set_ptp_cfg(), show_lan_intf(), show_lan_intf_state(), and snprint_ip4_cidr_addr().

◆ snprint_ip4_cidr_addr()

size_t snprint_ip4_cidr_addr ( char *  s,
size_t  max_len,
const IP4_ADDR p_addr,
const IP4_ADDR p_mask,
const char *  info 
)

Print an IPv4 address plus net mask in CIDR notation to a string.

The printed CIDR string is something like "172.16.3.250/24"

Parameters
[out]sThe string buffer to be filled
[in]max_lenSize of the output buffer for 0-terminated string
[in]p_addrThe IPv4 address to be evaluated
[in]p_maskThe associated IPv4 net mask
[in]infoAn optional string which is prepended to the string, or NULL
Returns
The overall number of characters printed to the string
See also
snprint_ip4_addr
str_to_ip4_addr
cidr_str_to_ip4_addr_and_net_mask

Definition at line 255 of file lan_util.c.

References get_ip4_net_mask_bits(), MAX_IP4_CIDR_NETMASK_BITS, MIN_IP4_CIDR_NETMASK_BITS, snprint_ip4_addr(), and snprintf_safe().

◆ snprint_ip6_addr()

size_t snprint_ip6_addr ( char *  s,
size_t  max_len,
const IP6_ADDR p_addr,
const char *  info 
)

Print an IPv6 address in optimized format to a string.

Parameters
[out]sThe string buffer to be filled
[in]max_lenSize of the output buffer for 0-terminated string
[in]p_addrThe IPv6 address to be evaluated
[in]infoAn optional string which is prepended to the string, or NULL
Returns
The overall number of characters printed to the string
See also
snprint_ip6_cidr_addr
snprint_ip6_cidr_mask_addr
str_to_ip6_addr
cidr_str_to_ip6_addr_and_cidr_bits
cidr_str_to_ip6_addr_and_net_mask

Definition at line 463 of file lan_util.c.

References IP6_ADDR::b, IP6_ADDR_BYTES, MBG_ERR_INV_PARM, MBG_ERR_OVERFLOW, NUM_WORDS, and snprintf_safe().

Referenced by snprint_ip6_cidr_addr(), and snprint_ip6_cidr_mask_addr().

◆ snprint_ip6_cidr_addr()

size_t snprint_ip6_cidr_addr ( char *  s,
size_t  max_len,
const IP6_ADDR p_addr,
const IP6_ADDR p_mask,
const char *  info 
)

Print an IPv6 address plus net mask to string in CIDR notation.

The printed CIDR string is something like "2001:0DB8:0:CD30:EF45::/64"

Parameters
[out]sThe string buffer to be filled
[in]max_lenSize of the output buffer for 0-terminated string
[in]p_addrThe IPv6 address to be evaluated
[in]p_maskThe associated IPv6 net mask
[in]infoAn optional string which is prepended to the string, or NULL
Returns
The overall number of characters printed to the string
See also
snprint_ip6_addr
snprint_ip6_cidr_mask_addr
str_to_ip6_addr
cidr_str_to_ip6_addr_and_cidr_bits
cidr_str_to_ip6_addr_and_net_mask

Definition at line 592 of file lan_util.c.

References get_ip6_net_mask_bits(), MAX_IP6_CIDR_NETMASK_BITS, MIN_IP6_CIDR_NETMASK_BITS, snprint_ip6_addr(), and snprintf_safe().

◆ snprint_ip6_cidr_mask_addr()

size_t snprint_ip6_cidr_mask_addr ( char *  s,
size_t  max_len,
const IP6_ADDR p_addr,
const int  cidr_mask_bits,
const char *  info 
)

Print an IPv6 address plus number of net mask bits to string in CIDR notation.

The printed CIDR string is something like "2001:0DB8:0:CD30:EF45::/64"

Parameters
[out]sThe string buffer to be filled
[in]max_lenSize of the output buffer for 0-terminated string
[in]p_addrThe IPv6 address to be evaluated
[in]cidr_mask_bitsThe CIDR number of bits specifying the IPv6 net mask
[in]infoAn optional string which is prepended to the string, or NULL
Returns
The overall number of characters printed to the string
See also
snprint_ip6_addr
snprint_ip6_cidr_addr
str_to_ip6_addr
cidr_str_to_ip6_addr_and_cidr_bits
cidr_str_to_ip6_addr_and_net_mask

Definition at line 629 of file lan_util.c.

References ip6_net_mask_from_cidr(), MAX_IP6_CIDR_NETMASK_BITS, MIN_IP6_CIDR_NETMASK_BITS, snprint_ip6_addr(), and snprintf_safe().

◆ snprint_mac_addr()

size_t snprint_mac_addr ( char *  s,
size_t  max_len,
const MBG_MAC_ADDR p_mac_addr 
)

Print a MAC address to a string.

Parameters
[out]sThe string buffer to be filled
[in]max_lenMaximum length of the string, i.e. size of the buffer
[in]p_mac_addrThe MAC address to be printed
Returns
The overall number of characters printed to the string
See also
snprint_octets
str_to_octets
octets_are_all_zero

Definition at line 1049 of file lan_util.c.

References MBG_MAC_ADDR::b, MAC_SEP_CHAR, and snprint_octets().

Referenced by show_lan_intf_state().

◆ snprint_octets()

size_t snprint_octets ( char *  s,
size_t  max_len,
const uint8_t octets,
int  num_octets,
char  sep,
const char *  info 
)

Print a MAC ID or similar array of octets to a string.

Parameters
[out]sThe string buffer to be filled
[in]max_lenMaximum length of the string, i.e. size of the buffer
[in]octetsAn array of octets
[in]num_octetsThe number of octets to be printed from the array
[in]sepThe separator printed between the bytes, or 0
[in]infoAn optional string which is prepended to the output, or NULL
Returns
The overall number of characters printed to the string
See also
snprint_mac_addr
str_to_octets
octets_are_all_zero

Definition at line 990 of file lan_util.c.

References snprintf_safe().

Referenced by set_ptp_cfg(), show_ptp_cfg(), show_ptp_state(), snprint_mac_addr(), and snprint_ptp_clock_id().

◆ snprint_ptp_clock_id()

size_t snprint_ptp_clock_id ( char *  s,
size_t  max_len,
const PTP_CLOCK_ID p 
)

Print a PTP_CLOCK_ID to a string.

Parameters
[out]sThe string buffer to be filled
[in]max_lenMaximum length of the string, i.e. size of the buffer
[in]pThe PTP_CLOCK_ID to be printed
Returns
The overall number of characters printed to the string
See also
snprint_octets
Parameters
[out]sThe string buffer to be filled
[in]max_lenMaximum length of the string, i.e. size of the buffer
[in]pThe PTP_CLOCK_ID to be printed
Returns
The overall number of characters printed to the string
See also
snprint_octets

Definition at line 1027 of file lan_util.c.

References PTP_CLOCK_ID::b, and snprint_octets().

◆ str_to_ip4_addr()

int str_to_ip4_addr ( IP4_ADDR p_addr,
const char *  s 
)

Convert a string to an IP4_ADDR.

If output parameter is specified as NULL then this function can be used to check if the IPv4 address string is formally correct.

Parameters
[out]p_addrPointer to an IP4_ADDR variable to be filled, or NULL
[in]sAn IPv4 address string to be converted
Returns
A number >= 0 (MBG_SUCCESS) according to the number of characters evaluated from the input string, or one of the MBG_ERROR_CODES on error, specifically MBG_ERR_PARM_FMT if an invalid number or character was found in the string.
See also
snprint_ip4_addr
snprint_ip4_cidr_addr
cidr_str_to_ip4_addr_and_net_mask

Definition at line 291 of file lan_util.c.

References MBG_ERR_PARM_FMT.

Referenced by cidr_str_to_ip4_addr_and_net_mask(), ip4_check_parm(), and set_ptp_cfg().

◆ str_to_ip6_addr()

int str_to_ip6_addr ( IP6_ADDR p_addr,
const char *  s 
)

Convert a string to an IP6_ADDR.

If the output parameter is specified as NULL then this function can be used to check if the string is formally correct.

On success IP6_ADDR variable contains the IPv6 address in little endian byte order.

Parameters
[out]p_addrPointer to the IP6_ADDR variable, or NULL
[in]sA string containing an IPv6 address
Returns
A number >= 0 (MBG_SUCCESS) according to the number of characters evaluated from the input string, or one of the MBG_ERROR_CODES on error, specifically MBG_ERR_PARM_FMT if an invalid number or character was found in the string.
See also
snprint_ip6_addr
snprint_ip6_cidr_addr
snprint_ip6_cidr_mask_addr
str_to_ip6_addr
cidr_str_to_ip6_addr_and_cidr_bits
cidr_str_to_ip6_addr_and_net_mask

Definition at line 673 of file lan_util.c.

References IP6_ADDR::b, and MBG_ERR_PARM_FMT.

Referenced by cidr_str_to_ip6_addr_and_cidr_bits().

◆ str_to_octets()

int str_to_octets ( uint8_t octets,
int  num_octets,
const char *  s 
)

Set a MAC ID or a similar array of octets from a string.

Parameters
[out]octetsAn array of octets to be set up
[in]num_octetsThe number of octets which can be stored
[in]sThe string to be converted
Returns
The overall number of octets decoded from the string
See also
snprint_octets
snprint_mac_addr
octets_are_all_zero

Definition at line 1071 of file lan_util.c.

References MAC_SEP_CHAR, and MAC_SEP_CHAR_ALT.

Referenced by set_ptp_cfg().