All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
inet.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2, or (at your option)
5  * any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 #ifndef _FR_INET_H
17 #define _FR_INET_H
18 /**
19  * $Id: 875d39fc4536d78f1c461fc584c3efc06ba2940b $
20  *
21  * @file include/inet.h
22  * @brief Structures and functions for parsing, printing, masking and retrieving IP addresses.
23  *
24  * @author Arran Cudbard-Bell <a.cudbardb@freeradius.org>
25  * @copyright 2015 Arran Cudbard-Bell <a.cudbardb@freeradius.org>
26  */
27 RCSIDH(inet_h, "$Id: 875d39fc4536d78f1c461fc584c3efc06ba2940b $")
28 
29 #include <net/if.h>
30 #include <arpa/inet.h>
31 #include <stdbool.h>
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 /** IPv4/6 prefix
37  *
38  * Abstraction around the standard in_addr/in6_addr structures to
39  * support address family agnostic functions.
40  */
41 typedef struct fr_ipaddr_t {
42  int af; //!< Address family.
43  union {
44  struct in_addr ip4addr; //!< IPv4 address.
45  struct in6_addr ip6addr; //!< IPv6 address.
46  } ipaddr;
47  uint8_t prefix; //!< Prefix length - Between 0-32 for IPv4 and 0-128 for IPv6.
48  uint32_t zone_id; //!< A host may have multiple link-local interfaces
49  //!< the scope ID allows the application to specify which of
50  //!< those interfaces the IP applies to. A special scope_id
51  //!< of zero means that any interface of a given scope can
52  //!< be used.
53 } fr_ipaddr_t;
54 
55 # if defined(SIOCGIFADDR) && (defined(SIOCGIFNAME) || defined(HAVE_IF_INDEXTONAME))
56 # define WITH_IFINDEX_RESOLUTION 1
57 # endif
58 
59 # if defined(SIOCGIFNAME) || defined(HAVE_IF_INDEXTONAME)
60 # define WITH_IFINDEX_NAME_RESOLUTION 1
61 # endif
62 
63 extern struct in6_addr fr_inet_link_local6;
64 
65 /** Like INET6_ADDRSTRLEN but includes space for the textual Zone ID
66  */
67 #define FR_IPADDR_STRLEN (INET6_ADDRSTRLEN + 1 + IFNAMSIZ)
68 
69 /** Like FR_IPADDR_STRLEN but with space for a prefix
70  */
71 #define FR_IPADDR_PREFIX_STRLEN (FR_IPADDR_STRLEN + 1 + 3)
72 
73 /*
74  * IP address masking
75  */
76 void fr_ipaddr_mask(fr_ipaddr_t *addr, uint8_t prefix);
77 
78 /*
79  * Presentation to network, and network to presentation conversion
80  */
81 int fr_inet_hton(fr_ipaddr_t *out, int af, char const *hostname, bool fallback);
82 
83 char const *fr_inet_ntoh(fr_ipaddr_t const *src, char *out, size_t outlen);
84 
85 int fr_inet_pton4(fr_ipaddr_t *out, char const *value, ssize_t inlen, bool resolve, bool fallback, bool mask);
86 
87 int fr_inet_pton6(fr_ipaddr_t *out, char const *value, ssize_t inlen, bool resolve, bool fallback, bool mask);
88 
89 int fr_inet_pton(fr_ipaddr_t *out, char const *value, ssize_t inlen, int af, bool resolve, bool mask);
90 
91 int fr_inet_pton_port(fr_ipaddr_t *out, uint16_t *port_out, char const *value,
92  ssize_t inlen, int af, bool resolve, bool mask);
93 
94 char *fr_inet_ntop(char out[FR_IPADDR_STRLEN], size_t outlen, fr_ipaddr_t *addr);
95 
96 char *fr_inet_ntop_prefix(char out[FR_IPADDR_PREFIX_STRLEN], size_t outlen, fr_ipaddr_t *addr);
97 
98 char *fr_inet_ifid_ntop(char *out, size_t outlen, uint8_t const *ifid);
99 
100 uint8_t *fr_inet_ifid_pton(uint8_t out[8], char const *ifid_str);
101 
102 /*
103  * if_index and if_name resolution
104  */
105 int fr_ipaddr_from_ifname(fr_ipaddr_t *out, int af, char const *name);
106 
107 #ifdef WITH_IFINDEX_NAME_RESOLUTION
108 char *fr_ifname_from_ifindex(char out[IFNAMSIZ], int if_index);
109 #endif
110 
111 #ifdef WITH_IFINDEX_IPADDR_RESOLUTION
112 int fr_ipaddr_from_ifindex(fr_ipaddr_t *out, int fd, int af, int if_index);
113 #endif
114 
115 /*
116  * Comparison
117  */
118 int fr_ipaddr_cmp(fr_ipaddr_t const *a, fr_ipaddr_t const *b);
119 
120 /*
121  * Sockaddr conversion functions
122  */
123 int fr_ipaddr_to_sockaddr(fr_ipaddr_t const *ipaddr, uint16_t port,
124  struct sockaddr_storage *sa, socklen_t *salen);
125 
126 int fr_ipaddr_from_sockaddr(struct sockaddr_storage const *sa, socklen_t salen,
127  fr_ipaddr_t *ipaddr, uint16_t *port);
128 
129 #ifdef __cplusplus
130 }
131 #endif
132 #endif /* _FR_INET_H */
#define RCSIDH(h, id)
Definition: build.h:136
int fr_ipaddr_from_sockaddr(struct sockaddr_storage const *sa, socklen_t salen, fr_ipaddr_t *ipaddr, uint16_t *port)
Definition: inet.c:1095
int fr_inet_hton(fr_ipaddr_t *out, int af, char const *hostname, bool fallback)
Wrappers for IPv4/IPv6 host to IP address lookup.
Definition: inet.c:127
uint8_t prefix
Prefix length - Between 0-32 for IPv4 and 0-128 for IPv6.
Definition: inet.h:47
static char const * name
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)
Definition: inet.c:632
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.
Definition: inet.c:762
static char const * hostname(char *buf, size_t buflen, uint32_t ipaddr)
Definition: radwho.c:149
#define FR_IPADDR_STRLEN
Like INET6_ADDRSTRLEN but includes space for the textual Zone ID.
Definition: inet.h:67
char * fr_inet_ifid_ntop(char *out, size_t outlen, uint8_t const *ifid)
Print an interface-id in standard colon notation.
Definition: inet.c:788
int af
Address family.
Definition: inet.h:42
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...
Definition: inet.c:564
static uint32_t mask
Definition: rbmonkey.c:75
union fr_ipaddr_t::@1 ipaddr
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)
Definition: inet.c:348
struct fr_ipaddr_t fr_ipaddr_t
IPv4/6 prefix.
void fr_ipaddr_mask(fr_ipaddr_t *addr, uint8_t prefix)
Zeroes out the host portion of an fr_ipaddr_t.
Definition: inet.c:90
static char const * prefix
Definition: mainconfig.c:81
struct in6_addr fr_inet_link_local6
uint32_t zone_id
A host may have multiple link-local interfaces the scope ID allows the application to specify which o...
Definition: inet.h:48
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.
Definition: inet.c:802
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.
Definition: inet.c:713
int fr_ipaddr_from_ifname(fr_ipaddr_t *out, int af, char const *name)
IPv4/6 prefix.
Definition: inet.h:41
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)
Definition: inet.c:465
char const * fr_inet_ntoh(fr_ipaddr_t const *src, char *out, size_t outlen)
Perform reverse resolution of an IP address.
Definition: inet.c:226
int fr_ipaddr_cmp(fr_ipaddr_t const *a, fr_ipaddr_t const *b)
Compare two ip addresses.
Definition: inet.c:1026
#define FR_IPADDR_PREFIX_STRLEN
Like FR_IPADDR_STRLEN but with space for a prefix.
Definition: inet.h:71
int fr_ipaddr_to_sockaddr(fr_ipaddr_t const *ipaddr, uint16_t port, struct sockaddr_storage *sa, socklen_t *salen)
Definition: inet.c:1057