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 | |
| 26 | /* IPv4 and IPv6 unified prefix structure. */ |
| 27 | struct prefix |
| 28 | { |
| 29 | u_char family; |
| 30 | u_char prefixlen; |
| 31 | union |
| 32 | { |
| 33 | u_char prefix; |
| 34 | struct in_addr prefix4; |
| 35 | #ifdef HAVE_IPV6 |
| 36 | struct in6_addr prefix6; |
| 37 | #endif /* HAVE_IPV6 */ |
| 38 | struct |
| 39 | { |
| 40 | struct in_addr id; |
| 41 | struct in_addr adv_router; |
| 42 | } lp; |
| 43 | u_char val[8]; |
| 44 | } u __attribute__ ((aligned (8))); |
| 45 | }; |
| 46 | |
| 47 | /* IPv4 prefix structure. */ |
| 48 | struct prefix_ipv4 |
| 49 | { |
| 50 | u_char family; |
| 51 | u_char prefixlen; |
| 52 | struct in_addr prefix __attribute__ ((aligned (8))); |
| 53 | }; |
| 54 | |
| 55 | /* IPv6 prefix structure. */ |
| 56 | #ifdef HAVE_IPV6 |
| 57 | struct prefix_ipv6 |
| 58 | { |
| 59 | u_char family; |
| 60 | u_char prefixlen; |
| 61 | struct in6_addr prefix __attribute__ ((aligned (8))); |
| 62 | }; |
| 63 | #endif /* HAVE_IPV6 */ |
| 64 | |
| 65 | struct prefix_ls |
| 66 | { |
| 67 | u_char family; |
| 68 | u_char prefixlen; |
| 69 | struct in_addr id __attribute__ ((aligned (8))); |
| 70 | struct in_addr adv_router; |
| 71 | }; |
| 72 | |
| 73 | /* Prefix for routing distinguisher. */ |
| 74 | struct prefix_rd |
| 75 | { |
| 76 | u_char family; |
| 77 | u_char prefixlen; |
| 78 | u_char val[8] __attribute__ ((aligned (8))); |
| 79 | }; |
| 80 | |
| 81 | #ifndef INET_ADDRSTRLEN |
| 82 | #define INET_ADDRSTRLEN 16 |
| 83 | #endif /* INET_ADDRSTRLEN */ |
| 84 | |
| 85 | #ifndef INET6_ADDRSTRLEN |
| 86 | #define INET6_ADDRSTRLEN 46 |
| 87 | #endif /* INET6_ADDRSTRLEN */ |
| 88 | |
| 89 | #ifndef INET6_BUFSIZ |
| 90 | #define INET6_BUFSIZ 51 |
| 91 | #endif /* INET6_BUFSIZ */ |
| 92 | |
| 93 | /* Max bit/byte length of IPv4 address. */ |
| 94 | #define IPV4_MAX_BYTELEN 4 |
| 95 | #define IPV4_MAX_BITLEN 32 |
| 96 | #define IPV4_MAX_PREFIXLEN 32 |
| 97 | #define IPV4_ADDR_CMP(D,S) memcmp ((D), (S), IPV4_MAX_BYTELEN) |
| 98 | #define IPV4_ADDR_SAME(D,S) (memcmp ((D), (S), IPV4_MAX_BYTELEN) == 0) |
| 99 | #define IPV4_ADDR_COPY(D,S) memcpy ((D), (S), IPV4_MAX_BYTELEN) |
| 100 | |
| 101 | #define IPV4_NET0(a) ((((u_int32_t) (a)) & 0xff000000) == 0x00000000) |
| 102 | #define IPV4_NET127(a) ((((u_int32_t) (a)) & 0xff000000) == 0x7f000000) |
| 103 | |
| 104 | /* Max bit/byte length of IPv6 address. */ |
| 105 | #define IPV6_MAX_BYTELEN 16 |
| 106 | #define IPV6_MAX_BITLEN 128 |
| 107 | #define IPV6_MAX_PREFIXLEN 128 |
| 108 | #define IPV6_ADDR_CMP(D,S) memcmp ((D), (S), IPV6_MAX_BYTELEN) |
| 109 | #define IPV6_ADDR_SAME(D,S) (memcmp ((D), (S), IPV6_MAX_BYTELEN) == 0) |
| 110 | #define IPV6_ADDR_COPY(D,S) memcpy ((D), (S), IPV6_MAX_BYTELEN) |
| 111 | |
| 112 | /* Count prefix size from mask length */ |
| 113 | #define PSIZE(a) (((a) + 7) / (8)) |
| 114 | |
| 115 | /* Prefix's family member. */ |
| 116 | #define PREFIX_FAMILY(p) ((p)->family) |
| 117 | |
| 118 | /* Prototypes. */ |
| 119 | int afi2family (int); |
| 120 | int family2afi (int); |
| 121 | |
| 122 | int prefix2str (struct prefix *, char *, int); |
| 123 | int str2prefix (char *, struct prefix *); |
| 124 | struct prefix *prefix_new (); |
| 125 | void prefix_free (struct prefix *p); |
| 126 | |
| 127 | struct prefix_ipv4 *prefix_ipv4_new (); |
| 128 | void prefix_ipv4_free (); |
| 129 | int str2prefix_ipv4 (char *, struct prefix_ipv4 *); |
| 130 | void apply_mask_ipv4 (struct prefix_ipv4 *); |
| 131 | int prefix_blen (struct prefix *); |
| 132 | u_char ip_masklen (struct in_addr); |
| 133 | int prefix_ipv4_any (struct prefix_ipv4 *); |
| 134 | void masklen2ip (int, struct in_addr *); |
| 135 | void apply_classful_mask_ipv4 (struct prefix_ipv4 *); |
| 136 | |
| 137 | char *prefix_family_str (struct prefix *p); |
| 138 | struct prefix *sockunion2prefix (); |
| 139 | struct prefix *sockunion2hostprefix (); |
| 140 | |
| 141 | #ifdef HAVE_IPV6 |
| 142 | struct prefix_ipv6 *prefix_ipv6_new (); |
| 143 | void prefix_ipv6_free (); |
| 144 | struct prefix *str2routev6 (char *); |
| 145 | int str2prefix_ipv6 (char *str, struct prefix_ipv6 *p); |
| 146 | void apply_mask_ipv6 (struct prefix_ipv6 *p); |
| 147 | void str2in6_addr (char *str, struct in6_addr *addr); |
| 148 | void masklen2ip6 (int masklen, struct in6_addr *netmask); |
| 149 | int ip6_masklen (struct in6_addr netmask); |
| 150 | #endif /* HAVE_IPV6 */ |
| 151 | |
| 152 | void apply_mask (struct prefix *); |
| 153 | int prefix_match (struct prefix *n, struct prefix *p); |
| 154 | int prefix_same (struct prefix *, struct prefix *); |
| 155 | int prefix_cmp (struct prefix *, struct prefix *); |
| 156 | void prefix_copy (struct prefix *, struct prefix *); |
| 157 | |
| 158 | int all_digit (char *); |
| 159 | int netmask_str2prefix_str (char *, char *, char *); |
| 160 | |
| 161 | #endif /* _ZEBRA_PREFIX_H */ |