The FreeRADIUS server $Id: f3670dba8951ca10eb4948feb3dc3db9423a334f $
Loading...
Searching...
No Matches
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 */
25RCSIDH(inet_h, "$Id: 63a1ff43879ebb9c3ff1019aee0c4de8f36d9e02 $")
26
27#include <freeradius-devel/build.h>
28#include <freeradius-devel/missing.h>
29#include <freeradius-devel/util/misc.h>
30#include <freeradius-devel/util/talloc.h>
31
32#include <arpa/inet.h>
33#include <net/if.h> /* SIOCGIFADDR et al */
34#include <netinet/in.h> /* in6?_addr */
35
36#ifdef __cplusplus
37extern "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 */
45typedef 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 */
54typedef 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 */
63typedef 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.
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
85extern 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
95extern bool fr_reverse_lookups; /* do IP -> hostname lookups? */
96extern bool fr_hostname_lookups; /* do hostname -> IP lookups? */
97
98/*
99 * We want to be able to have a separate "filename" socket, which
100 * is not a unix socket.
101 *
102 * On some systems, AF_FILE is equivalent to AF_LOCAL. So we use
103 * FILENAME, and use a "_FR_" qualifier. too.
104 */
105#define AF_FR_FILENAME (INT_MIN)
106
107/*
108 * A virtual server.
109 */
110#define AF_FR_VIRTUAL_SERVER (INT_MIN + 1)
111
112/*
113 * Utility functions
114 */
115int fr_ipaddr_is_inaddr_any(fr_ipaddr_t const *ipaddr);
116int fr_ipaddr_is_multicast(fr_ipaddr_t const *ipaddr);
117int fr_ipaddr_is_prefix(fr_ipaddr_t const *ipaddr);
118
119/*
120 * IP address masking
121 */
122void fr_ipaddr_mask(fr_ipaddr_t *addr, uint8_t prefix);
123
124/*
125 * Presentation to network, and network to presentation conversion
126 */
127int fr_inet_hton(fr_ipaddr_t *out, int af, char const *hostname, bool fallback);
128
129char const *fr_inet_ntoh(fr_ipaddr_t const *src, char *out, size_t outlen);
130
131int fr_inet_pton4(fr_ipaddr_t *out, char const *value, ssize_t inlen, bool resolve, bool fallback, bool mask);
132
133int fr_inet_pton6(fr_ipaddr_t *out, char const *value, ssize_t inlen, bool resolve, bool fallback, bool mask);
134
135int fr_inet_pton(fr_ipaddr_t *out, char const *value, ssize_t inlen, int af, bool resolve, bool mask);
136
137int fr_inet_pton_port(fr_ipaddr_t *out, uint16_t *port_out, char const *value,
138 ssize_t inlen, int af, bool resolve, bool mask);
139
140char *fr_inet_ntop(char out[static FR_IPADDR_STRLEN], size_t outlen, fr_ipaddr_t const *addr);
141
142char *fr_inet_ntop_prefix(char out[static FR_IPADDR_PREFIX_STRLEN], size_t outlen, fr_ipaddr_t const *addr);
143
144char *fr_inet_ifid_ntop(char *out, size_t outlen, uint8_t const *ifid);
145
146uint8_t *fr_inet_ifid_pton(uint8_t out[static 8], char const *ifid_str);
147
148/*
149 * ifindex and if_name resolution
150 */
151int fr_ipaddr_from_ifname(fr_ipaddr_t *out, int af, char const *name);
152
153#ifdef WITH_IFINDEX_NAME_RESOLUTION
154char *fr_ifname_from_ifindex(char out[static IFNAMSIZ], int ifindex);
155#endif
156
157#ifdef WITH_IFINDEX_IPADDR_RESOLUTION
158int fr_ipaddr_from_ifindex(fr_ipaddr_t *out, int fd, int af, int ifindex);
159#endif
160
161char *fr_ipaddr_to_interface(TALLOC_CTX *ctx, fr_ipaddr_t *ipaddr);
163int fr_interface_to_ipaddr(char const *interface, fr_ipaddr_t *ipaddr, int af, bool link_local);
164
165int fr_interface_to_ethernet(char const *interface, fr_ethernet_t *ethernet);
166
167/*
168 * Comparison
169 */
171
172int8_t fr_sockaddr_cmp(struct sockaddr_storage const *a, struct sockaddr_storage const *b);
173
174/*
175 * Sockaddr conversion functions
176 */
177int fr_ipaddr_to_sockaddr(struct sockaddr_storage *sa, socklen_t *salen,
178 fr_ipaddr_t const *ipaddr, uint16_t port);
179
181 struct sockaddr_storage const *sa, socklen_t salen);
182
183#ifdef __cplusplus
184}
185#endif
#define RCSIDH(h, id)
Definition build.h:561
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:1580
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:126
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:1493
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:356
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:1080
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:632
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:255
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:1448
int8_t fr_sockaddr_cmp(struct sockaddr_storage const *a, struct sockaddr_storage const *b)
Definition inet.c:1665
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:783
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:944
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
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:1025
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)
char * fr_ipaddr_to_interface(TALLOC_CTX *ctx, fr_ipaddr_t *ipaddr)
Definition inet.c:1537
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:1399
void fr_ipaddr_mask(fr_ipaddr_t *addr, uint8_t prefix)
Zeroes out the host portion of an fr_ipaddr_t.
Definition inet.c:218
int fr_interface_to_ethernet(char const *interface, fr_ethernet_t *ethernet)
Definition inet.c:1625
fr_cmp_ret_t fr_ipaddr_cmp(fr_ipaddr_t const *a, fr_ipaddr_t const *b)
Compare two ip addresses.
Definition inet.c:1353
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:1106
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:1120
int fr_ipaddr_from_ifname(fr_ipaddr_t *out, int af, char const *name)
#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
Struct to represent an ethernet address.
Definition inet.h:45
Struct to represent an interface id.
Definition inet.h:54
IPv4/6 prefix.
unsigned short uint16_t
unsigned int uint32_t
long int ssize_t
unsigned char uint8_t
fr_cmp_ret_t
Result of an ordering comparison.
Definition misc.h:50
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:1030
static size_t char ** out
Definition value.h:1030