prefix.h 6.2 KB

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