mbgtools-lx  4.2.8
lan_util.c File Reference
#include <lan_util.h>
#include <words.h>
#include <str_util.h>
#include <mbgerror.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <linux/types.h>
#include <linux/sockios.h>
#include <linux/ethtool.h>
#include <linux/rtnetlink.h>
#include <unistd.h>
#include <syslog.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <netinet/in.h>

Go to the source code of this file.

Data Structures

struct  route_info
 

Macros

#define _LAN_UTIL
 
#define MAX_IP4_ADDR_STR_SIZE   16
 
#define NUM_WORDS   ( (int) ( sizeof( IP6_ADDR_STR ) / sizeof( uint16_t ) ) )
 

Typedefs

typedef uint8_t u8
 
typedef uint16_t u16
 
typedef uint32_t u32
 
typedef uint64_t u64
 

Functions

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...
 
static int get_specific_port_ip4_addr (const char *if_name, IP4_ADDR *p_addr, int sigioc_code)
 Retrieve some IPv4 address-like info from 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...
 
static int nl_read (int sock_fd, char *buf_ptr, size_t buf_size, int seq_num, int pid)
 Read a requested message from the netlink socket. More...
 
static int nl_parse_routes (struct nlmsghdr *nl_hdr, struct route_info *rt_info)
 
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

◆ _LAN_UTIL

#define _LAN_UTIL

Definition at line 75 of file lan_util.c.

◆ MAX_IP4_ADDR_STR_SIZE

#define MAX_IP4_ADDR_STR_SIZE   16

Definition at line 153 of file lan_util.c.

◆ NUM_WORDS

#define NUM_WORDS   ( (int) ( sizeof( IP6_ADDR_STR ) / sizeof( uint16_t ) ) )

Referenced by snprint_ip6_addr().

Typedef Documentation

◆ u16

typedef uint16_t u16

Definition at line 99 of file lan_util.c.

◆ u32

typedef uint32_t u32

Definition at line 100 of file lan_util.c.

◆ u64

typedef uint64_t u64

Definition at line 101 of file lan_util.c.

◆ u8

typedef uint8_t u8

Definition at line 98 of file lan_util.c.

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.

◆ get_specific_port_ip4_addr()

static int get_specific_port_ip4_addr ( const char *  if_name,
IP4_ADDR p_addr,
int  sigioc_code 
)
static

Retrieve some IPv4 address-like info from a network interface.

Parameters
[in]if_nameName of the interface
[out]p_addrPointer to address field to be filled up
[in]sigioc_codeThe IOCTL code associated with the address
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
get_port_ip4_broad_addr_str

Definition at line 1361 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_addr(), get_port_ip4_broad_addr(), and get_port_ip4_netmask().

◆ 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().

◆ nl_parse_routes()

static int nl_parse_routes ( struct nlmsghdr *  nl_hdr,
struct route_info rt_info 
)
static

◆ nl_read()

static int nl_read ( int  sock_fd,
char *  buf_ptr,
size_t  buf_size,
int  seq_num,
int  pid 
)
static

Read a requested message from the netlink socket.

Returns
Message length (>= 0) on success, or of the MBG_ERROR_CODES

Definition at line 1472 of file lan_util.c.

References BYTE_OF, MBG_ERR_DATA_FMT, mbg_get_last_socket_error(), mbg_strerror(), and snprintf_safe().

Referenced by get_ip4_gateway().

◆ 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

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().