blob: 5884640a441c4e5c86481fa4f71f599c1b967dea [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
131int prefix2str (struct prefix *, char *, int);
132int str2prefix (char *, struct prefix *);
133struct prefix *prefix_new ();
134void prefix_free (struct prefix *p);
135
136struct prefix_ipv4 *prefix_ipv4_new ();
137void prefix_ipv4_free ();
138int str2prefix_ipv4 (char *, struct prefix_ipv4 *);
139void apply_mask_ipv4 (struct prefix_ipv4 *);
140int prefix_blen (struct prefix *);
141u_char ip_masklen (struct in_addr);
142int prefix_ipv4_any (struct prefix_ipv4 *);
143void masklen2ip (int, struct in_addr *);
144void apply_classful_mask_ipv4 (struct prefix_ipv4 *);
145
146char *prefix_family_str (struct prefix *p);
147struct prefix *sockunion2prefix ();
148struct prefix *sockunion2hostprefix ();
149
150#ifdef HAVE_IPV6
151struct prefix_ipv6 *prefix_ipv6_new ();
152void prefix_ipv6_free ();
153struct prefix *str2routev6 (char *);
154int str2prefix_ipv6 (char *str, struct prefix_ipv6 *p);
155void apply_mask_ipv6 (struct prefix_ipv6 *p);
156void str2in6_addr (char *str, struct in6_addr *addr);
157void masklen2ip6 (int masklen, struct in6_addr *netmask);
158int ip6_masklen (struct in6_addr netmask);
159#endif /* HAVE_IPV6 */
160
161void apply_mask (struct prefix *);
162int prefix_match (struct prefix *n, struct prefix *p);
163int prefix_same (struct prefix *, struct prefix *);
164int prefix_cmp (struct prefix *, struct prefix *);
165void prefix_copy (struct prefix *, struct prefix *);
166
167int all_digit (char *);
168int netmask_str2prefix_str (char *, char *, char *);
169
170#endif /* _ZEBRA_PREFIX_H */