net_ipv6_mask2pref.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /****************************************************************************
  2. * net/utils/net_ipv6_mask2pref.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include "utils/utils.h"
  25. #ifdef CONFIG_NET_IPv6
  26. /****************************************************************************
  27. * Private Data
  28. ****************************************************************************/
  29. static const uint8_t g_nibblemap[16] =
  30. {
  31. 0, 0, 0, 0, 0, 0, 0, 0, /* 0: No bits, 1-7: Should not happen */
  32. 1, 1, 1, 1, /* 8: 1 bit, 9-b: Should not happen */
  33. 2, 2, 3, 4 /* c: 2 bits, d: Should not happen, e: 3 bits, f: 4 bits */
  34. };
  35. /****************************************************************************
  36. * Private Functions
  37. ****************************************************************************/
  38. /****************************************************************************
  39. * Name: net_msbits4
  40. *
  41. * Description:
  42. * Count the number of leading '1' bits in an 4-bit nibble
  43. *
  44. ****************************************************************************/
  45. static inline uint8_t net_msbits4(uint8_t nibble)
  46. {
  47. /* Return the number of leading ones: 0-4) */
  48. return g_nibblemap[nibble];
  49. }
  50. /****************************************************************************
  51. * Name: net_msbits8
  52. *
  53. * Description:
  54. * Count the number of leading '1' bits in an 8-bit byte
  55. *
  56. ****************************************************************************/
  57. static uint8_t net_msbits8(uint8_t byval)
  58. {
  59. uint8_t ones;
  60. /* Check the MS nibble */
  61. ones = net_msbits4(byval >> 4);
  62. if (ones == 4)
  63. {
  64. /* All ones, try the LS nibble */
  65. ones += net_msbits4(byval & 0x0f);
  66. }
  67. /* Return the number of leading ones (0-8) */
  68. return ones;
  69. }
  70. /****************************************************************************
  71. * Name: net_msbits16
  72. *
  73. * Description:
  74. * Count the number of leading '1' bits in a 16-bit half-workd
  75. *
  76. ****************************************************************************/
  77. static inline uint8_t net_msbits16(uint16_t hword)
  78. {
  79. uint8_t ones;
  80. /* Look at the MS byte of the 16-bit value */
  81. ones = net_msbits8((uint8_t)(hword >> 8));
  82. if (ones == 8)
  83. {
  84. /* All '1's, try the LS byte */
  85. ones += net_msbits8((uint8_t)(hword & 0xff));
  86. }
  87. /* Return the number of leading ones (0-15) */
  88. return ones;
  89. }
  90. /****************************************************************************
  91. * Public Functions
  92. ****************************************************************************/
  93. /****************************************************************************
  94. * Name: net_ipv6_mask2pref
  95. *
  96. * Description:
  97. * Convert a 128-bit netmask to a prefix length. The NuttX IPv6
  98. * networking uses 128-bit network masks internally. This function
  99. * converts the IPv6 netmask to a prefix length.
  100. *
  101. * The prefix length is the number of MS '1' bits on in the netmask.
  102. * This, of course, assumes that all MS bits are '1' and all LS bits are
  103. * '0' with no intermixed 1's and 0's. This function searches from the MS
  104. * bit until the first '0' is found (this does not necessary mean that
  105. * there might not be additional '1' bits following the firs '0', but that
  106. * will be a malformed netmask.
  107. *
  108. * Input Parameters:
  109. * mask Points to an IPv6 netmask in the form of uint16_t[8]
  110. *
  111. * Returned Value:
  112. * The prefix length, range 0-128 on success; This function will not
  113. * fail.
  114. *
  115. ****************************************************************************/
  116. uint8_t net_ipv6_mask2pref(FAR const uint16_t *mask)
  117. {
  118. uint8_t preflen;
  119. int i;
  120. /* Count the leading all '1' 16-bit groups */
  121. for (i = 0, preflen = 0; i < 8 && mask[i] == 0xffff; i++, preflen += 16);
  122. /* Now i either, (1) indexes past the end of the mask, or (2) is the index
  123. * to the first half-word that is not equal to 0xffff.
  124. */
  125. if (i < 8)
  126. {
  127. preflen += net_msbits16(ntohs(mask[i]));
  128. }
  129. /* Return the prefix length */
  130. return preflen;
  131. }
  132. #endif /* CONFIG_NET_IPv6 */