The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
inet.h
Go to the documentation of this file.
1 #pragma once
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software Foundation,
15  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16  */
17 
18 /** Structures and functions for parsing, printing, masking and retrieving IP addresses
19  *
20  * @file src/lib/util/inet.h
21  *
22  * @author Arran Cudbard-Bell (a.cudbardb@freeradius.org)
23  * @copyright 2015 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
24  */
25 RCSIDH(inet_h, "$Id: 01d922415ad0b7d42f9cc1999bf95cc5e16c6056 $")
26 
27 #include <freeradius-devel/build.h>
28 #include <freeradius-devel/missing.h>
29 #include <freeradius-devel/util/talloc.h>
30 
31 #include <arpa/inet.h>
32 #include <net/if.h> /* SIOCGIFADDR et al */
33 #include <netinet/in.h> /* in6?_addr */
34 #include <stdbool.h>
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 /** Struct to represent an ethernet address
41  *
42  * This is needed to represent ethernet addresses correctly in
43  * _Generic macros.
44  */
45 typedef struct {
46  uint8_t addr[6]; //!< Ethernet address.
48 
49 /** Struct to represent an interface id
50  *
51  * This is needed to represent an interface id correctly in
52  * _Generic macros.
53  */
54 typedef struct {
55  uint8_t addr[8]; //!< Interface ID.
56 } fr_ifid_t;
57 
58 /** IPv4/6 prefix
59  *
60  * Abstraction around the standard in_addr/in6_addr structures to
61  * support address family agnostic functions.
62  */
63 typedef struct {
64  int af; //!< Address family.
65  union {
66  struct in_addr v4; //!< IPv4 address.
67  struct in6_addr v6; //!< IPv6 address.
68  } addr;
69  uint8_t prefix; //!< Prefix length - Between 0-32 for IPv4 and 0-128 for IPv6.
70  uint32_t scope_id; //!< A host may have multiple link-local interfaces
71  //!< the scope ID allows the application to specify which of
72  //!< those interfaces the IP applies to. A special scope_id
73  //!< of zero means that any interface of a given scope can
74  //!< be used.
75 } fr_ipaddr_t;
76 
77 # if defined(SIOCGIFADDR) && (defined(SIOCGIFNAME) || defined(HAVE_IF_INDEXTONAME))
78 # define WITH_IFINDEX_RESOLUTION 1
79 # endif
80 
81 # if defined(SIOCGIFNAME) || defined(HAVE_IF_INDEXTONAME)
82 # define WITH_IFINDEX_NAME_RESOLUTION 1
83 # endif
84 
85 extern struct in6_addr fr_inet_link_local6;
86 
87 /** Like INET6_ADDRSTRLEN but includes space for the textual Zone ID
88  */
89 #define FR_IPADDR_STRLEN (INET6_ADDRSTRLEN + 1 + IFNAMSIZ)
90 
91 /** Like FR_IPADDR_STRLEN but with space for a prefix
92  */
93 #define FR_IPADDR_PREFIX_STRLEN (FR_IPADDR_STRLEN + 1 + 4)
94 
95 extern bool fr_reverse_lookups; /* do IP -> hostname lookups? */
96 extern bool fr_hostname_lookups; /* do hostname -> IP lookups? */
97 
98 /*
99  * Utility functions
100  */
101 int fr_ipaddr_is_inaddr_any(fr_ipaddr_t const *ipaddr);
102 int fr_ipaddr_is_multicast(fr_ipaddr_t const *ipaddr);
103 int fr_ipaddr_is_prefix(fr_ipaddr_t const *ipaddr);
104 
105 /*
106  * IP address masking
107  */
108 void fr_ipaddr_mask(fr_ipaddr_t *addr, uint8_t prefix);
109 
110 /*
111  * Presentation to network, and network to presentation conversion
112  */
113 int fr_inet_hton(fr_ipaddr_t *out, int af, char const *hostname, bool fallback);
114 
115 char const *fr_inet_ntoh(fr_ipaddr_t const *src, char *out, size_t outlen);
116 
117 int fr_inet_pton4(fr_ipaddr_t *out, char const *value, ssize_t inlen, bool resolve, bool fallback, bool mask);
118 
119 int fr_inet_pton6(fr_ipaddr_t *out, char const *value, ssize_t inlen, bool resolve, bool fallback, bool mask);
120 
121 int fr_inet_pton(fr_ipaddr_t *out, char const *value, ssize_t inlen, int af, bool resolve, bool mask);
122 
123 int fr_inet_pton_port(fr_ipaddr_t *out, uint16_t *port_out, char const *value,
124  ssize_t inlen, int af, bool resolve, bool mask);
125 
126 char *fr_inet_ntop(char out[static FR_IPADDR_STRLEN], size_t outlen, fr_ipaddr_t const *addr);
127 
128 char *fr_inet_ntop_prefix(char out[static FR_IPADDR_PREFIX_STRLEN], size_t outlen, fr_ipaddr_t const *addr);
129 
130 char *fr_inet_ifid_ntop(char *out, size_t outlen, uint8_t const *ifid);
131 
132 uint8_t *fr_inet_ifid_pton(uint8_t out[static 8], char const *ifid_str);
133 
134 /*
135  * ifindex and if_name resolution
136  */
137 int fr_ipaddr_from_ifname(fr_ipaddr_t *out, int af, char const *name);
138 
139 #ifdef WITH_IFINDEX_NAME_RESOLUTION
140 char *fr_ifname_from_ifindex(char out[static IFNAMSIZ], int ifindex);
141 #endif
142 
143 #ifdef WITH_IFINDEX_IPADDR_RESOLUTION
144 int fr_ipaddr_from_ifindex(fr_ipaddr_t *out, int fd, int af, int ifindex);
145 #endif
146 
147 char *fr_ipaddr_to_interface(TALLOC_CTX *ctx, fr_ipaddr_t *ipaddr);
148 void fr_ipaddr_get_scope_id(fr_ipaddr_t *ipaddr);
149 int fr_interface_to_ipaddr(char const *interface, fr_ipaddr_t *ipaddr, int af, bool link_local);
150 
151 int fr_interface_to_ethernet(char const *interface, fr_ethernet_t *ethernet);
152 /*
153  * Comparison
154  */
155 int8_t fr_ipaddr_cmp(fr_ipaddr_t const *a, fr_ipaddr_t const *b);
156 
157 /*
158  * Sockaddr conversion functions
159  */
160 int fr_ipaddr_to_sockaddr(struct sockaddr_storage *sa, socklen_t *salen,
161  fr_ipaddr_t const *ipaddr, uint16_t port);
162 
163 int fr_ipaddr_from_sockaddr(fr_ipaddr_t *ipaddr, uint16_t *port,
164  struct sockaddr_storage const *sa, socklen_t salen);
165 
166 #ifdef __cplusplus
167 }
168 #endif
#define RCSIDH(h, id)
Definition: build.h:445
Test enumeration values.
Definition: dict_test.h:92
int fr_interface_to_ipaddr(char const *interface, fr_ipaddr_t *ipaddr, int af, bool link_local)
Definition: inet.c:1559
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:1085
uint8_t prefix
Prefix length - Between 0-32 for IPv4 and 0-128 for IPv6.
Definition: inet.h:69
#define FR_IPADDR_STRLEN
Like INET6_ADDRSTRLEN but includes space for the textual Zone ID.
Definition: inet.h:89
int fr_ipaddr_is_prefix(fr_ipaddr_t const *ipaddr)
Determine if an address is a prefix.
Definition: inet.c:125
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.
Definition: inet.c:1099
int fr_ipaddr_is_multicast(fr_ipaddr_t const *ipaddr)
Determine if an address is a multicast address.
Definition: inet.c:94
void fr_ipaddr_get_scope_id(fr_ipaddr_t *ipaddr)
Definition: inet.c:1472
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:615
struct in6_addr fr_inet_link_local6
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:240
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.
Definition: inet.c:1059
int af
Address family.
Definition: inet.h:64
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.
Definition: inet.c:1427
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:341
uint32_t scope_id
A host may have multiple link-local interfaces the scope ID allows the application to specify which o...
Definition: inet.h:70
bool fr_hostname_lookups
hostname -> IP lookups?
Definition: inet.c:52
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:764
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:923
int fr_ipaddr_is_inaddr_any(fr_ipaddr_t const *ipaddr)
Determine if an address is the INADDR_ANY address for its address family.
Definition: inet.c:62
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: merged_model.c:275
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.
Definition: inet.c:1378
int8_t fr_ipaddr_cmp(fr_ipaddr_t const *a, fr_ipaddr_t const *b)
Compare two ip addresses.
Definition: inet.c:1332
void fr_ipaddr_mask(fr_ipaddr_t *addr, uint8_t prefix)
Zeroes out the host portion of an fr_ipaddr_t.
Definition: inet.c:203
int fr_interface_to_ethernet(char const *interface, fr_ethernet_t *ethernet)
Definition: inet.c:1604
int fr_ipaddr_from_ifname(fr_ipaddr_t *out, int af, char const *name)
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.
Definition: inet.c:1004
#define FR_IPADDR_PREFIX_STRLEN
Like FR_IPADDR_STRLEN but with space for a prefix.
Definition: inet.h:93
bool fr_reverse_lookups
IP -> hostname lookups?
Definition: inet.c:51
char * fr_ipaddr_to_interface(TALLOC_CTX *ctx, fr_ipaddr_t *ipaddr)
Definition: inet.c:1516
Struct to represent an ethernet address.
Definition: inet.h:45
Struct to represent an interface id.
Definition: inet.h:54
IPv4/6 prefix.
Definition: merged_model.c:272
unsigned short uint16_t
Definition: merged_model.c:31
unsigned int uint32_t
Definition: merged_model.c:33
long int ssize_t
Definition: merged_model.c:24
unsigned char uint8_t
Definition: merged_model.c:30
static char const * hostname(char *buf, size_t buflen, uint32_t ipaddr)
Definition: radwho.c:133
static uint32_t mask
Definition: rbmonkey.c:39
static char const * name
static size_t char fr_sbuff_t size_t inlen
Definition: value.h:984
static size_t char ** out
Definition: value.h:984