Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1+ */
2 : #pragma once
3 :
4 : #include <netinet/in.h>
5 : #include <stddef.h>
6 : #include <sys/socket.h>
7 :
8 : #include "hash-funcs.h"
9 : #include "macro.h"
10 : #include "util.h"
11 :
12 : union in_addr_union {
13 : struct in_addr in;
14 : struct in6_addr in6;
15 : };
16 :
17 : struct in_addr_data {
18 : int family;
19 : union in_addr_union address;
20 : };
21 :
22 : bool in4_addr_is_null(const struct in_addr *a);
23 : int in_addr_is_null(int family, const union in_addr_union *u);
24 :
25 : int in_addr_is_multicast(int family, const union in_addr_union *u);
26 :
27 : bool in4_addr_is_link_local(const struct in_addr *a);
28 : int in_addr_is_link_local(int family, const union in_addr_union *u);
29 :
30 : bool in4_addr_is_localhost(const struct in_addr *a);
31 : int in_addr_is_localhost(int family, const union in_addr_union *u);
32 :
33 : bool in4_addr_is_non_local(const struct in_addr *a);
34 :
35 : bool in4_addr_equal(const struct in_addr *a, const struct in_addr *b);
36 : int in_addr_equal(int family, const union in_addr_union *a, const union in_addr_union *b);
37 : int in_addr_prefix_intersect(int family, const union in_addr_union *a, unsigned aprefixlen, const union in_addr_union *b, unsigned bprefixlen);
38 : int in_addr_prefix_next(int family, union in_addr_union *u, unsigned prefixlen);
39 : int in_addr_random_prefix(int family, union in_addr_union *u, unsigned prefixlen_fixed_part, unsigned prefixlen);
40 : int in_addr_to_string(int family, const union in_addr_union *u, char **ret);
41 : int in_addr_prefix_to_string(int family, const union in_addr_union *u, unsigned prefixlen, char **ret);
42 : int in_addr_ifindex_to_string(int family, const union in_addr_union *u, int ifindex, char **ret);
43 : int in_addr_from_string(int family, const char *s, union in_addr_union *ret);
44 : int in_addr_from_string_auto(const char *s, int *ret_family, union in_addr_union *ret);
45 : int in_addr_ifindex_from_string_auto(const char *s, int *family, union in_addr_union *ret, int *ifindex);
46 : unsigned char in4_addr_netmask_to_prefixlen(const struct in_addr *addr);
47 : struct in_addr* in4_addr_prefixlen_to_netmask(struct in_addr *addr, unsigned char prefixlen);
48 : int in4_addr_default_prefixlen(const struct in_addr *addr, unsigned char *prefixlen);
49 : int in4_addr_default_subnet_mask(const struct in_addr *addr, struct in_addr *mask);
50 : int in_addr_mask(int family, union in_addr_union *addr, unsigned char prefixlen);
51 : int in_addr_prefix_covers(int family, const union in_addr_union *prefix, unsigned char prefixlen, const union in_addr_union *address);
52 : int in_addr_parse_prefixlen(int family, const char *p, unsigned char *ret);
53 : int in_addr_prefix_from_string(const char *p, int family, union in_addr_union *ret_prefix, unsigned char *ret_prefixlen);
54 :
55 : typedef enum InAddrPrefixLenMode {
56 : PREFIXLEN_FULL, /* Default to prefixlen of address size, 32 for IPv4 or 128 for IPv6, if not specified. */
57 : PREFIXLEN_REFUSE, /* Fail with -ENOANO if prefixlen is not specified. */
58 : PREFIXLEN_LEGACY, /* Default to legacy default prefixlen calculation from address if not specified. */
59 : } InAddrPrefixLenMode;
60 :
61 : int in_addr_prefix_from_string_auto_internal(const char *p, InAddrPrefixLenMode mode, int *ret_family, union in_addr_union *ret_prefix, unsigned char *ret_prefixlen);
62 29 : static inline int in_addr_prefix_from_string_auto(const char *p, int *ret_family, union in_addr_union *ret_prefix, unsigned char *ret_prefixlen) {
63 29 : return in_addr_prefix_from_string_auto_internal(p, PREFIXLEN_FULL, ret_family, ret_prefix, ret_prefixlen);
64 : }
65 :
66 263 : static inline size_t FAMILY_ADDRESS_SIZE(int family) {
67 263 : assert(IN_SET(family, AF_INET, AF_INET6));
68 263 : return family == AF_INET6 ? 16 : 4;
69 : }
70 :
71 : /* Workaround for clang, explicitly specify the maximum-size element here.
72 : * See also oss-fuzz#11344. */
73 : #define IN_ADDR_NULL ((union in_addr_union) { .in6 = {} })
74 :
75 : extern const struct hash_ops in_addr_data_hash_ops;
76 : extern const struct hash_ops in6_addr_hash_ops;
|