PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/release/src/router/zebra/lib/prefix.h

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