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: 625a631b98d85965e22de1a6c29f4d89c8e01a8a $")
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
35#ifdef __cplusplus
36extern "C" {
37#endif
38
39/** Struct to represent an ethernet address
40 *
41 * This is needed to represent ethernet addresses correctly in
42 * _Generic macros.
43 */
44typedef struct {
45 uint8_t addr[6]; //!< Ethernet address.
47
48/** Struct to represent an interface id
49 *
50 * This is needed to represent an interface id correctly in
51 * _Generic macros.
52 */
53typedef struct {
54 uint8_t addr[8]; //!< Interface ID.
55} fr_ifid_t;
56
57/** IPv4/6 prefix
58 *
59 * Abstraction around the standard in_addr/in6_addr structures to
60 * support address family agnostic functions.
61 */
62typedef struct {
63 int af; //!< Address family.
64 union {
65 struct in_addr v4; //!< IPv4 address.
66 struct in6_addr v6; //!< IPv6 address.
67 } addr;
68 uint8_t prefix; //!< Prefix length - Between 0-32 for IPv4 and 0-128 for IPv6.
69 uint32_t scope_id; //!< A host may have multiple link-local interfaces
70 //!< the scope ID allows the application to specify which of
71 //!< those interfaces the IP applies to. A special scope_id
72 //!< of zero means that any interface of a given scope can
73 //!< be used.
75
76# if defined(SIOCGIFADDR) && (defined(SIOCGIFNAME) || defined(HAVE_IF_INDEXTONAME))
77# define WITH_IFINDEX_RESOLUTION 1
78# endif
79
80# if defined(SIOCGIFNAME) || defined(HAVE_IF_INDEXTONAME)
81# define WITH_IFINDEX_NAME_RESOLUTION 1
82# endif
83
84extern struct in6_addr fr_inet_link_local6;
85
86/** Like INET6_ADDRSTRLEN but includes space for the textual Zone ID
87 */
88#define FR_IPADDR_STRLEN (INET6_ADDRSTRLEN + 1 + IFNAMSIZ)
89
90/** Like FR_IPADDR_STRLEN but with space for a prefix
91 */
92#define FR_IPADDR_PREFIX_STRLEN (FR_IPADDR_STRLEN + 1 + 4)
93
94extern bool fr_reverse_lookups; /* do IP -> hostname lookups? */
95extern bool fr_hostname_lookups; /* do hostname -> IP lookups? */
96
97/*
98 * We want to be able to have a separate "filename" socket, which
99 * is not a unix socket.
100 *
101 * On some systems, AF_FILE is equivalent to AF_LOCAL. So we use
102 * FILENAME, and use a "_FR_" qualifier. too.
103 */
104#define AF_FR_FILENAME (INT_MIN)
105
106/*
107 * A virtual server.
108 */
109#define AF_FR_VIRTUAL_SERVER (INT_MIN + 1)
110
111/*
112 * Utility functions
113 */
114int fr_ipaddr_is_inaddr_any(fr_ipaddr_t const *ipaddr);
115int fr_ipaddr_is_multicast(fr_ipaddr_t const *ipaddr);
116int fr_ipaddr_is_prefix(fr_ipaddr_t const *ipaddr);
117
118/*
119 * IP address masking
120 */
121void fr_ipaddr_mask(fr_ipaddr_t *addr, uint8_t prefix);
122
123/*
124 * Presentation to network, and network to presentation conversion
125 */
126int fr_inet_hton(fr_ipaddr_t *out, int af, char const *hostname, bool fallback);
127
128char const *fr_inet_ntoh(fr_ipaddr_t const *src, char *out, size_t outlen);
129
130int fr_inet_pton4(fr_ipaddr_t *out, char const *value, ssize_t inlen, bool resolve, bool fallback, bool mask);
131
132int fr_inet_pton6(fr_ipaddr_t *out, char const *value, ssize_t inlen, bool resolve, bool fallback, bool mask);
133
134int fr_inet_pton(fr_ipaddr_t *out, char const *value, ssize_t inlen, int af, bool resolve, bool mask);
135
136int fr_inet_pton_port(fr_ipaddr_t *out, uint16_t *port_out, char const *value,
137 ssize_t inlen, int af, bool resolve, bool mask);
138
139char *fr_inet_ntop(char out[static FR_IPADDR_STRLEN], size_t outlen, fr_ipaddr_t const *addr);
140
141char *fr_inet_ntop_prefix(char out[static FR_IPADDR_PREFIX_STRLEN], size_t outlen, fr_ipaddr_t const *addr);
142
143char *fr_inet_ifid_ntop(char *out, size_t outlen, uint8_t const *ifid);
144
145uint8_t *fr_inet_ifid_pton(uint8_t out[static 8], char const *ifid_str);
146
147/*
148 * ifindex and if_name resolution
149 */
150int fr_ipaddr_from_ifname(fr_ipaddr_t *out, int af, char const *name);
151
152#ifdef WITH_IFINDEX_NAME_RESOLUTION
153char *fr_ifname_from_ifindex(char out[static IFNAMSIZ], int ifindex);
154#endif
155
156#ifdef WITH_IFINDEX_IPADDR_RESOLUTION
157int fr_ipaddr_from_ifindex(fr_ipaddr_t *out, int fd, int af, int ifindex);
158#endif
159
160char *fr_ipaddr_to_interface(TALLOC_CTX *ctx, fr_ipaddr_t *ipaddr);
162int fr_interface_to_ipaddr(char const *interface, fr_ipaddr_t *ipaddr, int af, bool link_local);
163
164int fr_interface_to_ethernet(char const *interface, fr_ethernet_t *ethernet);
165
166/*
167 * Comparison
168 */
169int8_t fr_ipaddr_cmp(fr_ipaddr_t const *a, fr_ipaddr_t const *b);
170
171int8_t fr_sockaddr_cmp(struct sockaddr_storage const *a, struct sockaddr_storage const *b);
172
173/*
174 * Sockaddr conversion functions
175 */
176int fr_ipaddr_to_sockaddr(struct sockaddr_storage *sa, socklen_t *salen,
177 fr_ipaddr_t const *ipaddr, uint16_t port);
178
180 struct sockaddr_storage const *sa, socklen_t salen);
181
182#ifdef __cplusplus
183}
184#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:68
#define FR_IPADDR_STRLEN
Like INET6_ADDRSTRLEN but includes space for the textual Zone ID.
Definition inet.h:88
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:63
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:69
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
int8_t fr_ipaddr_cmp(fr_ipaddr_t const *a, fr_ipaddr_t const *b)
Compare two ip addresses.
Definition inet.c:1353
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
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:92
bool fr_reverse_lookups
IP -> hostname lookups?
Definition inet.c:51
Struct to represent an ethernet address.
Definition inet.h:44
Struct to represent an interface id.
Definition inet.h:53
IPv4/6 prefix.
unsigned short uint16_t
unsigned int uint32_t
long int ssize_t
unsigned char uint8_t
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