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 | |
gdt | 9d24baa | 2004-01-13 14:55:40 +0000 | [diff] [blame] | 26 | /* |
| 27 | * A struct prefix contains an address family, a prefix length, and an |
| 28 | * address. This can represent either a 'network prefix' as defined |
| 29 | * by CIDR, where the 'host bits' of the prefix are 0 |
| 30 | * (e.g. AF_INET:10.0.0.0/8), or an address and netmask |
| 31 | * (e.g. AF_INET:10.0.0.9/8), such as might be configured on an |
| 32 | * interface. |
| 33 | */ |
| 34 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 35 | /* IPv4 and IPv6 unified prefix structure. */ |
| 36 | struct prefix |
| 37 | { |
| 38 | u_char family; |
| 39 | u_char prefixlen; |
| 40 | union |
| 41 | { |
| 42 | u_char prefix; |
| 43 | struct in_addr prefix4; |
| 44 | #ifdef HAVE_IPV6 |
| 45 | struct in6_addr prefix6; |
| 46 | #endif /* HAVE_IPV6 */ |
| 47 | struct |
| 48 | { |
| 49 | struct in_addr id; |
| 50 | struct in_addr adv_router; |
| 51 | } lp; |
| 52 | u_char val[8]; |
| 53 | } u __attribute__ ((aligned (8))); |
| 54 | }; |
| 55 | |
| 56 | /* IPv4 prefix structure. */ |
| 57 | struct prefix_ipv4 |
| 58 | { |
| 59 | u_char family; |
| 60 | u_char prefixlen; |
| 61 | struct in_addr prefix __attribute__ ((aligned (8))); |
| 62 | }; |
| 63 | |
| 64 | /* IPv6 prefix structure. */ |
| 65 | #ifdef HAVE_IPV6 |
| 66 | struct prefix_ipv6 |
| 67 | { |
| 68 | u_char family; |
| 69 | u_char prefixlen; |
| 70 | struct in6_addr prefix __attribute__ ((aligned (8))); |
| 71 | }; |
| 72 | #endif /* HAVE_IPV6 */ |
| 73 | |
| 74 | struct prefix_ls |
| 75 | { |
| 76 | u_char family; |
| 77 | u_char prefixlen; |
| 78 | struct in_addr id __attribute__ ((aligned (8))); |
| 79 | struct in_addr adv_router; |
| 80 | }; |
| 81 | |
| 82 | /* Prefix for routing distinguisher. */ |
| 83 | struct prefix_rd |
| 84 | { |
| 85 | u_char family; |
| 86 | u_char prefixlen; |
| 87 | u_char val[8] __attribute__ ((aligned (8))); |
| 88 | }; |
| 89 | |
| 90 | #ifndef INET_ADDRSTRLEN |
| 91 | #define INET_ADDRSTRLEN 16 |
| 92 | #endif /* INET_ADDRSTRLEN */ |
| 93 | |
| 94 | #ifndef INET6_ADDRSTRLEN |
| 95 | #define INET6_ADDRSTRLEN 46 |
| 96 | #endif /* INET6_ADDRSTRLEN */ |
| 97 | |
| 98 | #ifndef INET6_BUFSIZ |
| 99 | #define INET6_BUFSIZ 51 |
| 100 | #endif /* INET6_BUFSIZ */ |
| 101 | |
| 102 | /* Max bit/byte length of IPv4 address. */ |
| 103 | #define IPV4_MAX_BYTELEN 4 |
| 104 | #define IPV4_MAX_BITLEN 32 |
| 105 | #define IPV4_MAX_PREFIXLEN 32 |
| 106 | #define IPV4_ADDR_CMP(D,S) memcmp ((D), (S), IPV4_MAX_BYTELEN) |
| 107 | #define IPV4_ADDR_SAME(D,S) (memcmp ((D), (S), IPV4_MAX_BYTELEN) == 0) |
| 108 | #define IPV4_ADDR_COPY(D,S) memcpy ((D), (S), IPV4_MAX_BYTELEN) |
| 109 | |
| 110 | #define IPV4_NET0(a) ((((u_int32_t) (a)) & 0xff000000) == 0x00000000) |
| 111 | #define IPV4_NET127(a) ((((u_int32_t) (a)) & 0xff000000) == 0x7f000000) |
| 112 | |
| 113 | /* Max bit/byte length of IPv6 address. */ |
| 114 | #define IPV6_MAX_BYTELEN 16 |
| 115 | #define IPV6_MAX_BITLEN 128 |
| 116 | #define IPV6_MAX_PREFIXLEN 128 |
| 117 | #define IPV6_ADDR_CMP(D,S) memcmp ((D), (S), IPV6_MAX_BYTELEN) |
| 118 | #define IPV6_ADDR_SAME(D,S) (memcmp ((D), (S), IPV6_MAX_BYTELEN) == 0) |
| 119 | #define IPV6_ADDR_COPY(D,S) memcpy ((D), (S), IPV6_MAX_BYTELEN) |
| 120 | |
| 121 | /* Count prefix size from mask length */ |
| 122 | #define PSIZE(a) (((a) + 7) / (8)) |
| 123 | |
| 124 | /* Prefix's family member. */ |
| 125 | #define PREFIX_FAMILY(p) ((p)->family) |
| 126 | |
| 127 | /* Prototypes. */ |
| 128 | int afi2family (int); |
| 129 | int family2afi (int); |
| 130 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 131 | struct prefix *prefix_new (); |
hasso | b04c699 | 2004-10-04 19:10:31 +0000 | [diff] [blame] | 132 | void prefix_free (struct prefix *); |
| 133 | const char *prefix_family_str (const struct prefix *); |
| 134 | int prefix_blen (const struct prefix *); |
| 135 | int str2prefix (const char *, struct prefix *); |
| 136 | int prefix2str (const struct prefix *, char *, int); |
| 137 | int prefix_match (const struct prefix *, const struct prefix *); |
| 138 | int prefix_same (const struct prefix *, const struct prefix *); |
| 139 | int prefix_cmp (const struct prefix *, const struct prefix *); |
| 140 | void prefix_copy (struct prefix *dest, const struct prefix *src); |
| 141 | void apply_mask (struct prefix *); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 142 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 143 | struct prefix *sockunion2prefix (); |
| 144 | struct prefix *sockunion2hostprefix (); |
| 145 | |
hasso | b04c699 | 2004-10-04 19:10:31 +0000 | [diff] [blame] | 146 | struct prefix_ipv4 *prefix_ipv4_new (); |
| 147 | void prefix_ipv4_free (struct prefix_ipv4 *); |
| 148 | int str2prefix_ipv4 (const char *, struct prefix_ipv4 *); |
| 149 | void apply_mask_ipv4 (struct prefix_ipv4 *); |
| 150 | |
| 151 | int prefix_ipv4_any (const struct prefix_ipv4 *); |
| 152 | void apply_classful_mask_ipv4 (struct prefix_ipv4 *); |
| 153 | |
| 154 | u_char ip_masklen (struct in_addr); |
| 155 | void masklen2ip (int, struct in_addr *); |
hasso | 3fb9cd6 | 2004-10-19 19:44:43 +0000 | [diff] [blame] | 156 | /* returns the network portion of the host address */ |
| 157 | in_addr_t ipv4_network_addr (in_addr_t hostaddr, int masklen); |
| 158 | /* given the address of a host on a network and the network mask length, |
| 159 | * calculate the broadcast address for that network; |
| 160 | * special treatment for /31: returns the address of the other host |
| 161 | * on the network by flipping the host bit */ |
| 162 | in_addr_t ipv4_broadcast_addr (in_addr_t hostaddr, int masklen); |
| 163 | |
hasso | b04c699 | 2004-10-04 19:10:31 +0000 | [diff] [blame] | 164 | int netmask_str2prefix_str (const char *, const char *, char *); |
| 165 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 166 | #ifdef HAVE_IPV6 |
| 167 | struct prefix_ipv6 *prefix_ipv6_new (); |
hasso | b04c699 | 2004-10-04 19:10:31 +0000 | [diff] [blame] | 168 | void prefix_ipv6_free (struct prefix_ipv6 *); |
| 169 | int str2prefix_ipv6 (const char *, struct prefix_ipv6 *); |
| 170 | void apply_mask_ipv6 (struct prefix_ipv6 *); |
| 171 | |
| 172 | int ip6_masklen (struct in6_addr); |
| 173 | void masklen2ip6 (int, struct in6_addr *); |
| 174 | |
| 175 | void str2in6_addr (const char *, struct in6_addr *); |
hasso | 3a2ce6a | 2005-04-08 01:30:51 +0000 | [diff] [blame^] | 176 | const char *inet6_ntoa (struct in6_addr); |
hasso | 5920990 | 2005-04-05 14:36:49 +0000 | [diff] [blame] | 177 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 178 | #endif /* HAVE_IPV6 */ |
| 179 | |
hasso | b04c699 | 2004-10-04 19:10:31 +0000 | [diff] [blame] | 180 | int all_digit (const char *); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 181 | |
| 182 | #endif /* _ZEBRA_PREFIX_H */ |