The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Macros | Functions | Variables
inet.c File Reference

Functions for parsing, printing, masking and retrieving IP addresses. More...

#include <freeradius-devel/util/inet.h>
#include <freeradius-devel/util/misc.h>
#include <freeradius-devel/util/strerror.h>
#include <freeradius-devel/util/syserror.h>
#include <freeradius-devel/util/value.h>
#include <stdlib.h>
#include <ifaddrs.h>
#include <net/if_arp.h>
+ Include dependency graph for inet.c:

Go to the source code of this file.

Macros

#define AF_LINK   AF_PACKET
 

Functions

static struct in6_addr fr_in6addr_mask (struct in6_addr const *ipaddr, uint8_t prefix)
 Mask off a portion of an IPv6 address. More...
 
static struct in_addr fr_inaddr_mask (struct in_addr const *ipaddr, uint8_t prefix)
 Mask off a portion of an IPv4 address. More...
 
int fr_inet_hton (fr_ipaddr_t *out, int af, char const *hostname, bool fallback)
 Wrappers for IPv4/IPv6 host to IP address lookup. More...
 
char * fr_inet_ifid_ntop (char *out, size_t outlen, uint8_t const *ifid)
 Print an interface-id in standard colon notation. More...
 
uint8_tfr_inet_ifid_pton (uint8_t out[static 8], char const *ifid_str)
 Convert interface-id in colon notation to 8 byte binary form. More...
 
char const * fr_inet_ntoh (fr_ipaddr_t const *src, char *out, size_t outlen)
 Perform reverse resolution of an IP address. More...
 
char * fr_inet_ntop (char out[static FR_IPADDR_STRLEN], size_t outlen, fr_ipaddr_t const *addr)
 Print the address portion of a fr_ipaddr_t. More...
 
char * fr_inet_ntop_prefix (char out[static FR_IPADDR_PREFIX_STRLEN], size_t outlen, fr_ipaddr_t const *addr)
 Print a fr_ipaddr_t as a CIDR style network prefix. More...
 
int fr_inet_pton (fr_ipaddr_t *out, char const *value, ssize_t inlen, int af, bool resolve, bool mask)
 Simple wrapper to decide whether an IP value is v4 or v6 and call the appropriate parser. More...
 
int fr_inet_pton4 (fr_ipaddr_t *out, char const *value, ssize_t inlen, bool resolve, bool fallback, bool mask_bits)
 Parse an IPv4 address or IPv4 prefix in presentation format (and others) More...
 
int fr_inet_pton6 (fr_ipaddr_t *out, char const *value, ssize_t inlen, bool resolve, bool fallback, bool mask)
 Parse an IPv6 address or IPv6 prefix in presentation format (and others) More...
 
int fr_inet_pton_port (fr_ipaddr_t *out, uint16_t *port_out, char const *value, ssize_t inlen, int af, bool resolve, bool mask)
 Parses IPv4/6 address + port, to fr_ipaddr_t and integer (port) More...
 
int fr_interface_to_ethernet (char const *interface, fr_ethernet_t *ethernet)
 
int fr_interface_to_ipaddr (char const *interface, fr_ipaddr_t *ipaddr, int af, bool link_local)
 
int8_t fr_ipaddr_cmp (fr_ipaddr_t const *a, fr_ipaddr_t const *b)
 Compare two ip addresses. More...
 
int fr_ipaddr_from_ifname (UNUSED fr_ipaddr_t *out, UNUSED int af, char const *name)
 
int fr_ipaddr_from_sockaddr (fr_ipaddr_t *ipaddr, uint16_t *port, struct sockaddr_storage const *sa, socklen_t salen)
 Convert sockaddr to our internal ip address representation. More...
 
void fr_ipaddr_get_scope_id (fr_ipaddr_t *ipaddr)
 
int fr_ipaddr_is_inaddr_any (fr_ipaddr_t const *ipaddr)
 Determine if an address is the INADDR_ANY address for its address family. More...
 
int fr_ipaddr_is_multicast (fr_ipaddr_t const *ipaddr)
 Determine if an address is a multicast address. More...
 
int fr_ipaddr_is_prefix (fr_ipaddr_t const *ipaddr)
 Determine if an address is a prefix. More...
 
void fr_ipaddr_mask (fr_ipaddr_t *addr, uint8_t prefix)
 Zeroes out the host portion of an fr_ipaddr_t. More...
 
char * fr_ipaddr_to_interface (TALLOC_CTX *ctx, fr_ipaddr_t *ipaddr)
 
int fr_ipaddr_to_sockaddr (struct sockaddr_storage *sa, socklen_t *salen, fr_ipaddr_t const *ipaddr, uint16_t port)
 Convert our internal ip address representation to a sockaddr. More...
 
static int ip_octet_from_str (uint32_t *out, char const *str)
 Parse a single octet of an IPv4 address string. More...
 
static int ip_prefix_addr_from_str (struct in_addr *out, char const *str)
 Parses the network portion of an IPv4 prefix into an in_addr. More...
 

Variables

bool fr_hostname_lookups = true
 hostname -> IP lookups? More...
 
bool fr_reverse_lookups = false
 IP -> hostname lookups? More...
 

Detailed Description

Functions for parsing, printing, masking and retrieving IP addresses.

Author
Arran Cudbard-Bell (a.cud.nosp@m.bard.nosp@m.b@fre.nosp@m.erad.nosp@m.ius.o.nosp@m.rg)

Definition in file inet.c.

Macro Definition Documentation

◆ AF_LINK

#define AF_LINK   AF_PACKET

Definition at line 1615 of file inet.c.

Function Documentation

◆ fr_in6addr_mask()

static struct in6_addr fr_in6addr_mask ( struct in6_addr const *  ipaddr,
uint8_t  prefix 
)
static

Mask off a portion of an IPv6 address.

Parameters
ipaddrto mask.
prefixNumber of contiguous bits to mask.
Returns
an ipv6 address with the host portion zeroed out.

Definition at line 125 of file inet.c.

+ Here is the caller graph for this function:

◆ fr_inaddr_mask()

static struct in_addr fr_inaddr_mask ( struct in_addr const *  ipaddr,
uint8_t  prefix 
)
static

Mask off a portion of an IPv4 address.

Parameters
ipaddrto mask.
prefixNumber of contiguous bits to mask.
Returns
an ipv4 address with the host portion zeroed out.

Definition at line 125 of file inet.c.

+ Here is the caller graph for this function:

◆ fr_inet_hton()

int fr_inet_hton ( fr_ipaddr_t out,
int  af,
char const *  hostname,
bool  fallback 
)

Wrappers for IPv4/IPv6 host to IP address lookup.

This function returns only one IP address, of the specified address family, or the first address (of whatever family), if AF_UNSPEC is used.

If fallback is specified and af is AF_INET, but not AF_INET records were found and a record for AF_INET6 exists that record will be returned.

If fallback is specified and af is AF_INET6, and a record with AF_INET4 exists that record will be returned inserted.

Parameters
[out]outWhere to write result.
[in]afTo search for in preference.
[in]hostnameto search for.
[in]fallbackto the other address family, if no records matching af, found.
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 254 of file inet.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_inet_ifid_ntop()

char* fr_inet_ifid_ntop ( char *  out,
size_t  outlen,
uint8_t const *  ifid 
)

Print an interface-id in standard colon notation.

Parameters
[out]outWhere to write the resulting interface-id string.
[in]outlenof output buffer.
[in]ifidto print.
Returns
a pointer to out.

Definition at line 1099 of file inet.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_inet_ifid_pton()

uint8_t* fr_inet_ifid_pton ( uint8_t  out[static 8],
char const *  ifid_str 
)

Convert interface-id in colon notation to 8 byte binary form.

Parameters
[out]outWhere to write the binary interface-id.
[in]ifid_strto parse.
Returns
a pointer to out.

Definition at line 1113 of file inet.c.

+ Here is the caller graph for this function:

◆ fr_inet_ntoh()

char const* fr_inet_ntoh ( fr_ipaddr_t const *  src,
char *  out,
size_t  outlen 
)

Perform reverse resolution of an IP address.

Attempt to resolve an IP address to a DNS record (if dns lookups are enabled).

Parameters
[in]srcaddress to resolve.
[out]outWhere to write the resulting hostname.
[in]outlenlength of the output buffer.

Definition at line 355 of file inet.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_inet_ntop()

char* fr_inet_ntop ( char  out[static FR_IPADDR_STRLEN],
size_t  outlen,
fr_ipaddr_t const *  addr 
)

Print the address portion of a fr_ipaddr_t.

Note
Includes the textual scope_id name (eth0, en0 etc...) if supported.
Parameters
[out]outWhere to write the resulting IP string. Should be at least FR_IPADDR_STRLEN bytes.
[in]outlenof output buffer.
[in]addrto convert to presentation format.
Returns
  • NULL on error (use fr_syserror(errno)).
  • a pointer to out on success.

Definition at line 1018 of file inet.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_inet_ntop_prefix()

char* fr_inet_ntop_prefix ( char  out[static FR_IPADDR_PREFIX_STRLEN],
size_t  outlen,
fr_ipaddr_t const *  addr 
)

Print a fr_ipaddr_t as a CIDR style network prefix.

Parameters
[out]outWhere to write the resulting prefix string. Should be at least FR_IPADDR_PREFIX_STRLEN bytes.
[in]outlenof output buffer.
[in]addrto convert to presentation format.
Returns
  • NULL on error (use fr_syserror(errno)).
  • a pointer to out on success.

Definition at line 1073 of file inet.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_inet_pton()

int fr_inet_pton ( fr_ipaddr_t out,
char const *  value,
ssize_t  inlen,
int  af,
bool  resolve,
bool  mask 
)

Simple wrapper to decide whether an IP value is v4 or v6 and call the appropriate parser.

Parameters
[out]outWhere to write the ip address value.
[in]valueto parse.
[in]inlenLength of value, if value is \0 terminated inlen may be -1.
[in]resolveIf true and value doesn't look like an IP address, try and resolve value as a hostname.
[in]afIf the address type is not obvious from the format, and resolve is true, the DNS record (A or AAAA) we require. Also controls which parser we pass the address to if we have no idea what it is.
  • AF_UNSPEC - Use the server default IP family.
  • AF_INET - Treat value as an IPv4 address.
  • AF_INET6 - Treat value as in IPv6 address.
[in]maskIf true, set address bits to zero.
Returns
  • 0 if ip address was parsed successfully.
  • -1 on failure.

Definition at line 778 of file inet.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_inet_pton4()

int fr_inet_pton4 ( fr_ipaddr_t out,
char const *  value,
ssize_t  inlen,
bool  resolve,
bool  fallback,
bool  mask_bits 
)

Parse an IPv4 address or IPv4 prefix in presentation format (and others)

Parameters
[out]outWhere to write the ip address value.
[in]valueto parse, may be:
  • dotted quad [+ prefix]
  • integer
  • octal number
  • '*' (INADDR_ANY)
  • FQDN if resolve is true.
[in]inlenLength of value, if value is \0 terminated inlen may be -1.
[in]resolveIf true and value doesn't look like an IP address, try and resolve value as a hostname.
[in]fallbackto IPv6 resolution if no A records can be found.
[in]mask_bitsIf true, set address bits to zero.
Returns
  • 0 if ip address was parsed successfully.
  • -1 on failure.

Definition at line 480 of file inet.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_inet_pton6()

int fr_inet_pton6 ( fr_ipaddr_t out,
char const *  value,
ssize_t  inlen,
bool  resolve,
bool  fallback,
bool  mask 
)

Parse an IPv6 address or IPv6 prefix in presentation format (and others)

Parameters
[out]outWhere to write the ip address value.
[in]valueto parse, may be:
  • IPv6 hexits [+ prefix].
  • '*' wildcard.
  • FQDN if resolve is true.
[in]inlenLength of value, if value is \0 terminated inlen may be -1.
[in]resolveIf true and value doesn't look like an IP address, try and resolve value as a hostname.
[in]fallbackto IPv4 resolution if no AAAA records can be found.
[in]maskIf true, set address bits to zero.
Returns
  • 0 if ip address was parsed successfully.
  • -1 on failure.

Definition at line 629 of file inet.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_inet_pton_port()

int fr_inet_pton_port ( fr_ipaddr_t out,
uint16_t port_out,
char const *  value,
ssize_t  inlen,
int  af,
bool  resolve,
bool  mask 
)

Parses IPv4/6 address + port, to fr_ipaddr_t and integer (port)

Parameters
[out]outWhere to write the ip address value.
[out]port_outWhere to write the port (0 if no port found).
[in]valueto parse.
[in]inlenLength of value, if value is \0 terminated inlen may be -1.
[in]resolveIf true and value doesn't look like an IP address, try and resolve value as a hostname.
[in]afIf the address type is not obvious from the format, and resolve is true, the DNS record (A or AAAA) we require. Also controls which parser we pass the address to if we have no idea what it is.
  • AF_UNSPEC - Use the server default IP family.
  • AF_INET - Treat value as an IPv4 address.
  • AF_INET6 - Treat value as in IPv6 address.
[in]maskIf true, set address bits to zero.
Returns
  • 0 if ip address was parsed successfully.
  • -1 on failure.

Definition at line 937 of file inet.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_interface_to_ethernet()

int fr_interface_to_ethernet ( char const *  interface,
fr_ethernet_t ethernet 
)

Definition at line 1618 of file inet.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_interface_to_ipaddr()

int fr_interface_to_ipaddr ( char const *  interface,
fr_ipaddr_t ipaddr,
int  af,
bool  link_local 
)

Definition at line 1573 of file inet.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_ipaddr_cmp()

int8_t fr_ipaddr_cmp ( fr_ipaddr_t const *  a,
fr_ipaddr_t const *  b 
)

Compare two ip addresses.

Parameters
[in]aFirst ip to compare.
[in]bSecond ip to compare.
Returns
  • 1 if a > b
  • 0 if a == b
  • -1 if a < b
  • -2 on error.

Definition at line 1346 of file inet.c.

+ Here is the caller graph for this function:

◆ fr_ipaddr_from_ifname()

int fr_ipaddr_from_ifname ( UNUSED fr_ipaddr_t out,
UNUSED int  af,
char const *  name 
)

Definition at line 1209 of file inet.c.

◆ fr_ipaddr_from_sockaddr()

int fr_ipaddr_from_sockaddr ( fr_ipaddr_t ipaddr,
uint16_t port,
struct sockaddr_storage const *  sa,
socklen_t  salen 
)

Convert sockaddr to our internal ip address representation.

Parameters
[out]ipaddrWhere to write the ipaddr.
[out]portWhere to write the port.
[in]sastruct to convert.
[in]salenLength of the sockaddr struct.
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 1441 of file inet.c.

+ Here is the caller graph for this function:

◆ fr_ipaddr_get_scope_id()

void fr_ipaddr_get_scope_id ( fr_ipaddr_t ipaddr)

Definition at line 1486 of file inet.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_ipaddr_is_inaddr_any()

int fr_ipaddr_is_inaddr_any ( fr_ipaddr_t const *  ipaddr)

Determine if an address is the INADDR_ANY address for its address family.

Parameters
ipaddrto check.
Returns
  • 0 if it's not.
  • 1 if it is.
  • -1 on error.

Definition at line 62 of file inet.c.

+ Here is the caller graph for this function:

◆ fr_ipaddr_is_multicast()

int fr_ipaddr_is_multicast ( fr_ipaddr_t const *  ipaddr)

Determine if an address is a multicast address.

Parameters
ipaddrto check.
Returns
  • 0 if it's not.
  • 1 if it is.
  • -1 on error.

Definition at line 94 of file inet.c.

+ Here is the caller graph for this function:

◆ fr_ipaddr_is_prefix()

int fr_ipaddr_is_prefix ( fr_ipaddr_t const *  ipaddr)

Determine if an address is a prefix.

Parameters
ipaddrto check.
Returns
  • 0 if it's not.
  • 1 if it is.
  • -1 on error.

Definition at line 125 of file inet.c.

+ Here is the caller graph for this function:

◆ fr_ipaddr_mask()

void fr_ipaddr_mask ( fr_ipaddr_t addr,
uint8_t  prefix 
)

Zeroes out the host portion of an fr_ipaddr_t.

Parameters
[in,out]addrto mask
[in]prefixLength of the network portion.

Definition at line 217 of file inet.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_ipaddr_to_interface()

char* fr_ipaddr_to_interface ( TALLOC_CTX *  ctx,
fr_ipaddr_t ipaddr 
)

Definition at line 1530 of file inet.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ fr_ipaddr_to_sockaddr()

int fr_ipaddr_to_sockaddr ( struct sockaddr_storage *  sa,
socklen_t *  salen,
fr_ipaddr_t const *  ipaddr,
uint16_t  port 
)

Convert our internal ip address representation to a sockaddr.

Parameters
[out]sawhere to write out the sockaddr, must be large enough to hold sizeof(s6).
[out]salenLength of the sockaddr struct.
[in]ipaddrIP address to convert.
[in]portPort to convert.
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 1392 of file inet.c.

+ Here is the caller graph for this function:

◆ ip_octet_from_str()

static int ip_octet_from_str ( uint32_t out,
char const *  str 
)
static

Parse a single octet of an IPv4 address string.

Parameters
[out]outWhere to write integer.
[in]strto parse.
Returns
  • >= 0 on success (number of bytes parsed of in).
  • < 0 on error.

Definition at line 387 of file inet.c.

+ Here is the caller graph for this function:

◆ ip_prefix_addr_from_str()

static int ip_prefix_addr_from_str ( struct in_addr *  out,
char const *  str 
)
static

Parses the network portion of an IPv4 prefix into an in_addr.

Note
output is in network order.

Parses address strings in dotted quad notation. Unlike inet_pton allows octets to be omitted, in which case their value is considered to be 0. Unlike inet_aton treats integers as representing the highest octet of an IPv4 address, and limits them to 255.

Examples of acceptable strings:

  • 192.168.0.0
  • 192.168.0.0/24
  • 192.168/16
  • 192
  • 192/8
Parameters
[out]outWhere to write parsed address.
[in]strto parse.
Returns
  • >= 0 on success (number of bytes parsed of in).
  • < 0 on error.

Definition at line 430 of file inet.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ fr_hostname_lookups

bool fr_hostname_lookups = true

hostname -> IP lookups?

Definition at line 52 of file inet.c.

◆ fr_reverse_lookups

bool fr_reverse_lookups = false

IP -> hostname lookups?

Definition at line 51 of file inet.c.