Network Component  Version 7.11.0
MDK Middleware for IPv4 and IPv6 Networking
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Conversion Functions

Convert network addresses from ASCII to binary and vice versa. More...

Functions

const char * netIP_ntoa (int16_t addr_type, const uint8_t *ip_addr, char *string_buf, uint32_t buf_len)
 Convert IP address from binary to text form. [thread-safe]. More...
 
bool netIP_aton (const char *addr_string, int16_t addr_type, uint8_t *ip_addr)
 Convert IP address from text to binary form. [thread-safe]. More...
 
const char * netMAC_ntoa (const uint8_t *mac_addr, char *string_buf, uint32_t buf_len)
 Convert MAC address from binary to text form. [thread-safe]. More...
 
bool netMAC_aton (const char *mac_string, uint8_t *mac_addr)
 Convert MAC address from text to binary form. [thread-safe]. More...
 

Description

Convert network addresses from ASCII to binary and vice versa.

The address conversion functions are used for conversion of IP or MAC addresses from binary to ASCII strings and vice versa.

Function Documentation

bool netIP_aton ( const char *  addr_string,
int16_t  addr_type,
uint8_t *  ip_addr 
)

Convert IP address from text to binary form. [thread-safe].

Parameters
[in]addr_stringnetwork address string:
  • in dotted-decimal IPv4 notation.
  • in compressed colon-hexadecimal IPv6 notation.
[in]addr_typenetwork address type:
  • NET_ADDR_IP4 = IPv4 address.
  • NET_ADDR_IP6 = IPv6 address.
[out]ip_addrIPv6 or IPv6 address in binary form.
Returns
  • true = Conversion successful.
  • false = Conversion failed.

The function netIP_aton converts an ASCII to a network address.

The argument addr_string specifies the ASCII representation of the IP address.

The argument addr_type specifies if the address to be converted is IPv4 or IPv6.

The argument ip_addr points to the buffer that stores the converted IP address.

Code Example

uint8_t ip_addr[NET_ADDR_IP6_LEN];
netIP_aton ("fec0::1", NET_ADDR_IP6, &ip_addr[0]);
const char * netIP_ntoa ( int16_t  addr_type,
const uint8_t *  ip_addr,
char *  string_buf,
uint32_t  buf_len 
)

Convert IP address from binary to text form. [thread-safe].

Parameters
[in]addr_typenetwork address type:
  • NET_ADDR_IP4 = IPv4 address.
  • NET_ADDR_IP6 = IPv6 address.
[in]ip_addrIPv4 or IPv6 address in binary form.
[out]string_bufbuffer to store converted IP address to.
[in]buf_lenlength of a string buffer, at least:
  • 16 characters for IPv4 address.
  • 40 characters for IPv6 address.
Returns
pointer to string_buf with null-terminated IP address string.
  • NULL in case of parameter error.

The function netIP_ntoa converts a network address to ASCII.

The argument addr_type specifies if the address to be converted is IPv4 or IPv6.

The argument ip_addr points to the buffer containing the IP address to be converted.

Code Example

uint8_t ip_addr [NET_ADDR_IP6_LEN];
char ip_ascii[40];
netIP_ntoa (NET_ADDR_IP6, &ip_addr[0], ip_ascii, sizeof (ip_ascii));
printf ("IP address: %s\n", ip_ascii);
bool netMAC_aton ( const char *  mac_string,
uint8_t *  mac_addr 
)

Convert MAC address from text to binary form. [thread-safe].

Parameters
[in]mac_stringaddress string in hyphen MAC-address notation.
[out]mac_addrMAC address in binary form.
Returns
  • true = Conversion successful.
  • false = Conversion failed.

The function netMAC_aton converts an ASCII string to a MAC address.

The argument mac_string specifies the ASCII representation of the MAC address.

The argument mac_addr is a pointer to the buffer that stores the converted MAC address.

Code Example

uint8_t mac [NET_ADDR_ETH_LEN];
netMAC_aton ("1E-30-6C-A2-45-5E", &mac[0]);
const char * netMAC_ntoa ( const uint8_t *  mac_addr,
char *  string_buf,
uint32_t  buf_len 
)

Convert MAC address from binary to text form. [thread-safe].

Parameters
[in]mac_addrMAC address in binary form.
[out]string_bufbuffer to store converted MAC address to.
[in]buf_lenlength of a string buffer, at least 18 characters.
Returns
pointer to string_buf with null-terminated MAC address string.
  • NULL in case of parameter error.

The function netMAC_ntoa converts a MAC address to ASCII.

The argument mac_addr specifies the MAC address to be converted.

Code Example

uint8_t mac [NET_ADDR_ETH_LEN];
char mac_ascii[18];
netMAC_ntoa (&mac[0], mac_ascii, sizeof (mac_ascii));
printf ("MAC address: %s\n", mac_ascii);