prefix.h 5.3 KB

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