All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Data Structures | Macros | Typedefs | Functions | Variables
inet.h File Reference

Structures and functions for parsing, printing, masking and retrieving IP addresses. More...

#include <net/if.h>
#include <arpa/inet.h>
#include <stdbool.h>
+ Include dependency graph for inet.h:

Go to the source code of this file.

Data Structures

struct  fr_ipaddr_t
 IPv4/6 prefix. More...
 
union  fr_ipaddr_t.ipaddr
 

Macros

#define FR_IPADDR_PREFIX_STRLEN   (FR_IPADDR_STRLEN + 1 + 3)
 Like FR_IPADDR_STRLEN but with space for a prefix. More...
 
#define FR_IPADDR_STRLEN   (INET6_ADDRSTRLEN + 1 + IFNAMSIZ)
 Like INET6_ADDRSTRLEN but includes space for the textual Zone ID. More...
 

Typedefs

typedef struct fr_ipaddr_t fr_ipaddr_t
 IPv4/6 prefix. More...
 

Functions

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_t * fr_inet_ifid_pton (uint8_t out[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[FR_IPADDR_STRLEN], size_t outlen, fr_ipaddr_t *addr)
 Print the address portion of a fr_ipaddr_t. More...
 
char * fr_inet_ntop_prefix (char out[FR_IPADDR_PREFIX_STRLEN], size_t outlen, fr_ipaddr_t *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)
 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_ipaddr_cmp (fr_ipaddr_t const *a, fr_ipaddr_t const *b)
 Compare two ip addresses. More...
 
int fr_ipaddr_from_ifname (fr_ipaddr_t *out, int af, char const *name)
 
int fr_ipaddr_from_sockaddr (struct sockaddr_storage const *sa, socklen_t salen, fr_ipaddr_t *ipaddr, uint16_t *port)
 
void fr_ipaddr_mask (fr_ipaddr_t *addr, uint8_t prefix)
 Zeroes out the host portion of an fr_ipaddr_t. More...
 
int fr_ipaddr_to_sockaddr (fr_ipaddr_t const *ipaddr, uint16_t port, struct sockaddr_storage *sa, socklen_t *salen)
 

Variables

struct in6_addr fr_inet_link_local6
 

Detailed Description

Structures and functions for parsing, printing, masking and retrieving IP addresses.

Id:
875d39fc4536d78f1c461fc584c3efc06ba2940b
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.h.


Data Structure Documentation

struct fr_ipaddr_t

IPv4/6 prefix.

Abstraction around the standard in_addr/in6_addr structures to support address family agnostic functions.

Definition at line 41 of file inet.h.

Data Fields
int af Address family.
union fr_ipaddr_t ipaddr
uint8_t prefix Prefix length - Between 0-32 for IPv4 and 0-128 for IPv6.
uint32_t zone_id A host may have multiple link-local interfaces the scope ID allows the application to specify which of those interfaces the IP applies to.

A special scope_id of zero means that any interface of a given scope can be used.

union fr_ipaddr_t.ipaddr

Definition at line 43 of file inet.h.

Data Fields
struct in_addr ip4addr IPv4 address.
struct in6_addr ip6addr IPv6 address.

Macro Definition Documentation

#define FR_IPADDR_PREFIX_STRLEN   (FR_IPADDR_STRLEN + 1 + 3)

Like FR_IPADDR_STRLEN but with space for a prefix.

Definition at line 71 of file inet.h.

#define FR_IPADDR_STRLEN   (INET6_ADDRSTRLEN + 1 + IFNAMSIZ)

Like INET6_ADDRSTRLEN but includes space for the textual Zone ID.

Definition at line 67 of file inet.h.

Typedef Documentation

typedef struct fr_ipaddr_t fr_ipaddr_t

IPv4/6 prefix.

Abstraction around the standard in_addr/in6_addr structures to support address family agnostic functions.

Function Documentation

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 inseted.

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 127 of file inet.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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 788 of file inet.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

uint8_t* fr_inet_ifid_pton ( uint8_t  out[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 802 of file inet.c.

+ Here is the caller graph for this function:

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 226 of file inet.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Print the address portion of a fr_ipaddr_t.

Note
Includes the textual zone_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 713 of file inet.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

char* fr_inet_ntop_prefix ( char  out[FR_IPADDR_PREFIX_STRLEN],
size_t  outlen,
fr_ipaddr_t 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 762 of file inet.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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.
[in]maskIf true, set address bits to zero.
Returns
  • 0 if ip address was parsed successfully.
  • -1 on failure.

Definition at line 564 of file inet.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 348 of file inet.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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
outWhere to write the ip address value.
valueto parse.
inlenLength of value, if value is \0 terminated inlen may be -1.
resolveIf true and value doesn't look like an IP address, try and resolve value as a hostname.
fallbackto IPv4 resolution if no AAAA records can be found.
maskIf true, set address bits to zero.
Returns
  • 0 if ip address was parsed successfully.
  • -1 on failure.

Definition at line 465 of file inet.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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]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.
[in]resolveIf true and value doesn't look like an IP address, try and resolve value as a hostname.
[in]maskIf true, set address bits to zero.

Definition at line 632 of file inet.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Compare two ip addresses.

Definition at line 1026 of file inet.c.

+ Here is the caller graph for this function:

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

+ Here is the caller graph for this function:

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

Definition at line 1095 of file inet.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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 90 of file inet.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 1057 of file inet.c.

+ Here is the caller graph for this function:

Variable Documentation

struct in6_addr fr_inet_link_local6