blob: e4f17ab02ab048763b8f19409b62fabdd0cb0f87 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
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
gdt9d24baa2004-01-13 14:55:40 +000026/*
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
paul718e3742002-12-13 20:15:29 +000035/* IPv4 and IPv6 unified prefix structure. */
36struct 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. */
57struct 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
66struct 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
74struct 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. */
83struct 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. */
128int afi2family (int);
129int family2afi (int);
130
paul718e3742002-12-13 20:15:29 +0000131struct prefix *prefix_new ();
hassob04c6992004-10-04 19:10:31 +0000132void prefix_free (struct prefix *);
133const char *prefix_family_str (const struct prefix *);
134int prefix_blen (const struct prefix *);
135int str2prefix (const char *, struct prefix *);
136int prefix2str (const struct prefix *, char *, int);
137int prefix_match (const struct prefix *, const struct prefix *);
138int prefix_same (const struct prefix *, const struct prefix *);
139int prefix_cmp (const struct prefix *, const struct prefix *);
140void prefix_copy (struct prefix *dest, const struct prefix *src);
141void apply_mask (struct prefix *);
paul718e3742002-12-13 20:15:29 +0000142
paul718e3742002-12-13 20:15:29 +0000143struct prefix *sockunion2prefix ();
144struct prefix *sockunion2hostprefix ();
145
hassob04c6992004-10-04 19:10:31 +0000146struct prefix_ipv4 *prefix_ipv4_new ();
147void prefix_ipv4_free (struct prefix_ipv4 *);
148int str2prefix_ipv4 (const char *, struct prefix_ipv4 *);
149void apply_mask_ipv4 (struct prefix_ipv4 *);
150
151int prefix_ipv4_any (const struct prefix_ipv4 *);
152void apply_classful_mask_ipv4 (struct prefix_ipv4 *);
153
154u_char ip_masklen (struct in_addr);
155void masklen2ip (int, struct in_addr *);
156int netmask_str2prefix_str (const char *, const char *, char *);
157
paul718e3742002-12-13 20:15:29 +0000158#ifdef HAVE_IPV6
159struct prefix_ipv6 *prefix_ipv6_new ();
hassob04c6992004-10-04 19:10:31 +0000160void prefix_ipv6_free (struct prefix_ipv6 *);
161int str2prefix_ipv6 (const char *, struct prefix_ipv6 *);
162void apply_mask_ipv6 (struct prefix_ipv6 *);
163
164int ip6_masklen (struct in6_addr);
165void masklen2ip6 (int, struct in6_addr *);
166
167void str2in6_addr (const char *, struct in6_addr *);
paul718e3742002-12-13 20:15:29 +0000168#endif /* HAVE_IPV6 */
169
hassob04c6992004-10-04 19:10:31 +0000170int all_digit (const char *);
paul718e3742002-12-13 20:15:29 +0000171
172#endif /* _ZEBRA_PREFIX_H */