net_ipv6_mask2pref.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /****************************************************************************
  2. * net/utils/net_ipv6_mask2pref.c
  3. *
  4. * Copyright (C) 2016 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <stdint.h>
  40. #include <arpa/inet.h>
  41. #include "utils/utils.h"
  42. #ifdef CONFIG_NET_IPv6
  43. /****************************************************************************
  44. * Private Data
  45. ****************************************************************************/
  46. static const uint8_t g_nibblemap[16] =
  47. {
  48. 0, 0, 0, 0, 0, 0, 0, 0, /* 0: No bits, 1-7: Should not happen */
  49. 1, 1, 1, 1, /* 8: 1 bit, 9-b: Should not happen */
  50. 2, 2, 3, 4 /* c: 2 bits, d: Should not happen, e: 3 bits, f: 4 bits */
  51. };
  52. /****************************************************************************
  53. * Private Functions
  54. ****************************************************************************/
  55. /****************************************************************************
  56. * Name: net_msbits4
  57. *
  58. * Description:
  59. * Count the number of leading '1' bits in an 4-bit nibble
  60. *
  61. ****************************************************************************/
  62. static inline uint8_t net_msbits4(uint8_t nibble)
  63. {
  64. /* Return the number of leading zeroes: 0-4) */
  65. return g_nibblemap[nibble];
  66. }
  67. /****************************************************************************
  68. * Name: net_msbits8
  69. *
  70. * Description:
  71. * Count the number of leading '1' bits in an 8-bit byte
  72. *
  73. ****************************************************************************/
  74. static uint8_t net_msbits8(uint8_t byval)
  75. {
  76. uint8_t ones;
  77. /* Check the MS nibble */
  78. ones = net_msbits4(byval >> 4);
  79. if (ones == 4)
  80. {
  81. /* All ones, try the LS nibble */
  82. ones += net_msbits4(byval & 0x0f);
  83. }
  84. /* Return the number of leading ones (0-8) */
  85. return ones;
  86. }
  87. /****************************************************************************
  88. * Name: net_msbits16
  89. *
  90. * Description:
  91. * Count the number of leading '1' bits in a 16-bit half-workd
  92. *
  93. ****************************************************************************/
  94. static inline uint8_t net_msbits16(uint16_t hword)
  95. {
  96. uint8_t ones;
  97. /* Look at the MS byte of the 16-bit value */
  98. ones = net_msbits8((uint8_t)(hword >> 8));
  99. if (ones == 8)
  100. {
  101. /* All '1's, try the LS byte */
  102. ones += net_msbits8((uint8_t)(hword & 0xff));
  103. }
  104. /* Return the number of leading ones (0-15) */
  105. return ones;
  106. }
  107. /****************************************************************************
  108. * Public Functions
  109. ****************************************************************************/
  110. /****************************************************************************
  111. * Name: net_ipv6_mask2pref
  112. *
  113. * Description:
  114. * Convert a 128-bit netmask to a prefix length. The Nuttx IPv6
  115. * networking uses 128-bit network masks internally. This function
  116. * converts the IPv6 netmask to a prefix length.
  117. *
  118. * The prefix length is the number of MS '1' bits on in the netmask.
  119. * This, of course, assumes that all MS bits are '1' and all LS bits are
  120. * '0' with no intermixed 1's and 0's. This function searches from the MS
  121. * bit until the first '0' is found (this does not necessary mean that
  122. * there might not be additional '1' bits following the firs '0', but that
  123. * will be a malformed netmask.
  124. *
  125. * Input Parameters:
  126. * mask Points to an IPv6 netmask in the form of uint16_t[8]
  127. *
  128. * Returned Value:
  129. * The prefix length, range 0-128 on success; This function will not
  130. * fail.
  131. *
  132. ****************************************************************************/
  133. uint8_t net_ipv6_mask2pref(FAR const uint16_t *mask)
  134. {
  135. uint8_t preflen;
  136. int i;
  137. /* Count the leading all '1' 16-bit groups */
  138. for (i = 0, preflen = 0; i < 8 && mask[i] == 0xffff; i++, preflen += 16);
  139. /* Now i either, (1) indexes past the end of the mask, or (2) is the index
  140. * to the first half-word that is not equal to 0xffff.
  141. */
  142. if (i < 8)
  143. {
  144. preflen += net_msbits16(ntohs(mask[i]));
  145. }
  146. /* Return the prefix length */
  147. return preflen;
  148. }
  149. #endif /* CONFIG_NET_IPv6 */