CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches
Network Addresses

Functions

bool netAddrFromStr (NetAddr *addr, strref str)
 
bool netAddrToStr (string *str, const NetAddr *addr)
 

Detailed Description

Network address functions for IPv4 and IPv6.

Function Documentation

◆ netAddrFromStr()

bool netAddrFromStr ( NetAddr addr,
strref  str 
)

Parse an IPv4 or IPv6 address string

Accepts a dotted-quad IPv4 literal ("192.168.1.1") or an IPv6 literal ("::1", "fe80::1%eth0"), including "::" compression, an embedded IPv4 tail ("::ffff:10.0.0.1"), and a zone suffix – numeric ("%3") or an interface name resolved through the OS. No port suffix is accepted; the port field is left 0, set addr->port separately.

Parameters
addrOutput address structure
strAddress string to parse
Returns
true if the string was a valid address, false otherwise

Example:

NetAddr addr;
netAddrFromStr(&addr, _SL("127.0.0.1"));
addr.port = 8080;
bool netAddrFromStr(NetAddr *addr, strref str)
#define _SL(s)
Inline ASCII string literal with compile-time embedded length (STR_LEN8). Content must be < 200 bytes...
Definition strliteral.h:207
uint16 port
Port number.
Definition net_shared.h:290

◆ netAddrToStr()

bool netAddrToStr ( string *  str,
const NetAddr addr 
)

Format an address as a string

Writes the host portion only (dotted-quad for IPv4, RFC 5952 compressed form for IPv6, with a numeric zone suffix if the scope is nonzero) – the port is not included.

Parameters
strOutput string; overwritten with the formatted address
addrAddress to format
Returns
true if the address was formatted, false if its type was invalid

Example:

string s = 0;
netAddrToStr(&s, &addr);
bool netAddrToStr(string *str, const NetAddr *addr)