paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Prefix structure. |
| 3 | * Copyright (C) 1998 Kunihiro Ishiguro |
| 4 | * |
| 5 | * This file is part of GNU Zebra. |
| 6 | * |
| 7 | * GNU Zebra is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of the GNU General Public License as published by the |
| 9 | * Free Software Foundation; either version 2, or (at your option) any |
| 10 | * later version. |
| 11 | * |
| 12 | * GNU Zebra is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with GNU Zebra; see the file COPYING. If not, write to the Free |
| 19 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 20 | * 02111-1307, USA. |
| 21 | */ |
| 22 | |
| 23 | #ifndef _ZEBRA_PREFIX_H |
| 24 | #define _ZEBRA_PREFIX_H |
| 25 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame^] | 26 | #include "sockunion.h" |
| 27 | |
gdt | 9d24baa | 2004-01-13 14:55:40 +0000 | [diff] [blame] | 28 | /* |
| 29 | * A struct prefix contains an address family, a prefix length, and an |
| 30 | * address. This can represent either a 'network prefix' as defined |
| 31 | * by CIDR, where the 'host bits' of the prefix are 0 |
| 32 | * (e.g. AF_INET:10.0.0.0/8), or an address and netmask |
| 33 | * (e.g. AF_INET:10.0.0.9/8), such as might be configured on an |
| 34 | * interface. |
| 35 | */ |
| 36 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 37 | /* IPv4 and IPv6 unified prefix structure. */ |
| 38 | struct prefix |
| 39 | { |
| 40 | u_char family; |
| 41 | u_char prefixlen; |
| 42 | union |
| 43 | { |
| 44 | u_char prefix; |
| 45 | struct in_addr prefix4; |
| 46 | #ifdef HAVE_IPV6 |
| 47 | struct in6_addr prefix6; |
| 48 | #endif /* HAVE_IPV6 */ |
| 49 | struct |
| 50 | { |
| 51 | struct in_addr id; |
| 52 | struct in_addr adv_router; |
| 53 | } lp; |
| 54 | u_char val[8]; |
| 55 | } u __attribute__ ((aligned (8))); |
| 56 | }; |
| 57 | |
| 58 | /* IPv4 prefix structure. */ |
| 59 | struct prefix_ipv4 |
| 60 | { |
| 61 | u_char family; |
| 62 | u_char prefixlen; |
| 63 | struct in_addr prefix __attribute__ ((aligned (8))); |
| 64 | }; |
| 65 | |
| 66 | /* IPv6 prefix structure. */ |
| 67 | #ifdef HAVE_IPV6 |
| 68 | struct prefix_ipv6 |
| 69 | { |
| 70 | u_char family; |
| 71 | u_char prefixlen; |
| 72 | struct in6_addr prefix __attribute__ ((aligned (8))); |
| 73 | }; |
| 74 | #endif /* HAVE_IPV6 */ |
| 75 | |
| 76 | struct prefix_ls |
| 77 | { |
| 78 | u_char family; |
| 79 | u_char prefixlen; |
| 80 | struct in_addr id __attribute__ ((aligned (8))); |
| 81 | struct in_addr adv_router; |
| 82 | }; |
| 83 | |
| 84 | /* Prefix for routing distinguisher. */ |
| 85 | struct prefix_rd |
| 86 | { |
| 87 | u_char family; |
| 88 | u_char prefixlen; |
| 89 | u_char val[8] __attribute__ ((aligned (8))); |
| 90 | }; |
| 91 | |
| 92 | #ifndef INET_ADDRSTRLEN |
| 93 | #define INET_ADDRSTRLEN 16 |
| 94 | #endif /* INET_ADDRSTRLEN */ |
| 95 | |
| 96 | #ifndef INET6_ADDRSTRLEN |
| 97 | #define INET6_ADDRSTRLEN 46 |
| 98 | #endif /* INET6_ADDRSTRLEN */ |
| 99 | |
| 100 | #ifndef INET6_BUFSIZ |
| 101 | #define INET6_BUFSIZ 51 |
| 102 | #endif /* INET6_BUFSIZ */ |
| 103 | |
| 104 | /* Max bit/byte length of IPv4 address. */ |
| 105 | #define IPV4_MAX_BYTELEN 4 |
| 106 | #define IPV4_MAX_BITLEN 32 |
| 107 | #define IPV4_MAX_PREFIXLEN 32 |
| 108 | #define IPV4_ADDR_CMP(D,S) memcmp ((D), (S), IPV4_MAX_BYTELEN) |
| 109 | #define IPV4_ADDR_SAME(D,S) (memcmp ((D), (S), IPV4_MAX_BYTELEN) == 0) |
| 110 | #define IPV4_ADDR_COPY(D,S) memcpy ((D), (S), IPV4_MAX_BYTELEN) |
| 111 | |
| 112 | #define IPV4_NET0(a) ((((u_int32_t) (a)) & 0xff000000) == 0x00000000) |
| 113 | #define IPV4_NET127(a) ((((u_int32_t) (a)) & 0xff000000) == 0x7f000000) |
| 114 | |
| 115 | /* Max bit/byte length of IPv6 address. */ |
| 116 | #define IPV6_MAX_BYTELEN 16 |
| 117 | #define IPV6_MAX_BITLEN 128 |
| 118 | #define IPV6_MAX_PREFIXLEN 128 |
| 119 | #define IPV6_ADDR_CMP(D,S) memcmp ((D), (S), IPV6_MAX_BYTELEN) |
| 120 | #define IPV6_ADDR_SAME(D,S) (memcmp ((D), (S), IPV6_MAX_BYTELEN) == 0) |
| 121 | #define IPV6_ADDR_COPY(D,S) memcpy ((D), (S), IPV6_MAX_BYTELEN) |
| 122 | |
| 123 | /* Count prefix size from mask length */ |
| 124 | #define PSIZE(a) (((a) + 7) / (8)) |
| 125 | |
| 126 | /* Prefix's family member. */ |
| 127 | #define PREFIX_FAMILY(p) ((p)->family) |
| 128 | |
| 129 | /* Prototypes. */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame^] | 130 | extern int afi2family (int); |
| 131 | extern int family2afi (int); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 132 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame^] | 133 | extern struct prefix *prefix_new (void); |
| 134 | extern void prefix_free (struct prefix *); |
| 135 | extern const char *prefix_family_str (const struct prefix *); |
| 136 | extern int prefix_blen (const struct prefix *); |
| 137 | extern int str2prefix (const char *, struct prefix *); |
| 138 | extern int prefix2str (const struct prefix *, char *, int); |
| 139 | extern int prefix_match (const struct prefix *, const struct prefix *); |
| 140 | extern int prefix_same (const struct prefix *, const struct prefix *); |
| 141 | extern int prefix_cmp (const struct prefix *, const struct prefix *); |
| 142 | extern void prefix_copy (struct prefix *dest, const struct prefix *src); |
| 143 | extern void apply_mask (struct prefix *); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 144 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame^] | 145 | extern struct prefix *sockunion2prefix (const union sockunion *dest, |
| 146 | const union sockunion *mask); |
| 147 | extern struct prefix *sockunion2hostprefix (const union sockunion *); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 148 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame^] | 149 | extern struct prefix_ipv4 *prefix_ipv4_new (void); |
| 150 | extern void prefix_ipv4_free (struct prefix_ipv4 *); |
| 151 | extern int str2prefix_ipv4 (const char *, struct prefix_ipv4 *); |
| 152 | extern void apply_mask_ipv4 (struct prefix_ipv4 *); |
hasso | b04c699 | 2004-10-04 19:10:31 +0000 | [diff] [blame] | 153 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame^] | 154 | extern int prefix_ipv4_any (const struct prefix_ipv4 *); |
| 155 | extern void apply_classful_mask_ipv4 (struct prefix_ipv4 *); |
hasso | b04c699 | 2004-10-04 19:10:31 +0000 | [diff] [blame] | 156 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame^] | 157 | extern u_char ip_masklen (struct in_addr); |
| 158 | extern void masklen2ip (int, struct in_addr *); |
hasso | 3fb9cd6 | 2004-10-19 19:44:43 +0000 | [diff] [blame] | 159 | /* returns the network portion of the host address */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame^] | 160 | extern in_addr_t ipv4_network_addr (in_addr_t hostaddr, int masklen); |
hasso | 3fb9cd6 | 2004-10-19 19:44:43 +0000 | [diff] [blame] | 161 | /* given the address of a host on a network and the network mask length, |
| 162 | * calculate the broadcast address for that network; |
| 163 | * special treatment for /31: returns the address of the other host |
| 164 | * on the network by flipping the host bit */ |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame^] | 165 | extern in_addr_t ipv4_broadcast_addr (in_addr_t hostaddr, int masklen); |
hasso | 3fb9cd6 | 2004-10-19 19:44:43 +0000 | [diff] [blame] | 166 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame^] | 167 | extern int netmask_str2prefix_str (const char *, const char *, char *); |
hasso | b04c699 | 2004-10-04 19:10:31 +0000 | [diff] [blame] | 168 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 169 | #ifdef HAVE_IPV6 |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame^] | 170 | extern struct prefix_ipv6 *prefix_ipv6_new (void); |
| 171 | extern void prefix_ipv6_free (struct prefix_ipv6 *); |
| 172 | extern int str2prefix_ipv6 (const char *, struct prefix_ipv6 *); |
| 173 | extern void apply_mask_ipv6 (struct prefix_ipv6 *); |
hasso | b04c699 | 2004-10-04 19:10:31 +0000 | [diff] [blame] | 174 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame^] | 175 | extern int ip6_masklen (struct in6_addr); |
| 176 | extern void masklen2ip6 (int, struct in6_addr *); |
hasso | b04c699 | 2004-10-04 19:10:31 +0000 | [diff] [blame] | 177 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame^] | 178 | extern void str2in6_addr (const char *, struct in6_addr *); |
| 179 | extern const char *inet6_ntoa (struct in6_addr); |
hasso | 5920990 | 2005-04-05 14:36:49 +0000 | [diff] [blame] | 180 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 181 | #endif /* HAVE_IPV6 */ |
| 182 | |
paul | 8cc4198 | 2005-05-06 21:25:49 +0000 | [diff] [blame^] | 183 | extern int all_digit (const char *); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 184 | |
| 185 | #endif /* _ZEBRA_PREFIX_H */ |