ipaddr.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /**
  2. * @file ipaddr.h
  3. * @author Ambroz Bizjak <[email protected]>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * IP address parsing functions.
  32. */
  33. #ifndef BADVPN_MISC_IPADDR_H
  34. #define BADVPN_MISC_IPADDR_H
  35. #include <stdint.h>
  36. #include <string.h>
  37. #include <stdlib.h>
  38. #include <misc/debug.h>
  39. #include <misc/byteorder.h>
  40. #include <misc/parse_number.h>
  41. #include <misc/find_char.h>
  42. struct ipv4_ifaddr {
  43. uint32_t addr;
  44. int prefix;
  45. };
  46. static int ipaddr_parse_ipv4_addr_bin (const char *name, size_t name_len, uint32_t *out_addr);
  47. static int ipaddr_parse_ipv4_addr (const char *name, uint32_t *out_addr);
  48. static int ipaddr_parse_ipv4_prefix_bin (const char *str, size_t str_len, int *num);
  49. static int ipaddr_parse_ipv4_prefix (const char *str, int *num);
  50. static int ipaddr_parse_ipv4_ifaddr_bin (const char *str, size_t str_len, struct ipv4_ifaddr *out);
  51. static int ipaddr_parse_ipv4_ifaddr (const char *str, struct ipv4_ifaddr *out);
  52. static int ipaddr_ipv4_ifaddr_from_addr_mask (uint32_t addr, uint32_t mask, struct ipv4_ifaddr *out);
  53. static uint32_t ipaddr_ipv4_mask_from_prefix (int prefix);
  54. static int ipaddr_ipv4_prefix_from_mask (uint32_t mask, int *out_prefix);
  55. static int ipaddr_ipv4_addrs_in_network (uint32_t addr1, uint32_t addr2, int netprefix);
  56. int ipaddr_parse_ipv4_addr_bin (const char *name, size_t name_len, uint32_t *out_addr)
  57. {
  58. for (size_t i = 0; ; i++) {
  59. size_t j;
  60. for (j = 0; j < name_len && name[j] != '.'; j++);
  61. if ((j == name_len && i < 3) || (j < name_len && i == 3)) {
  62. return 0;
  63. }
  64. if (j < 1 || j > 3) {
  65. return 0;
  66. }
  67. uintmax_t d;
  68. if (!parse_unsigned_integer_bin(name, j, &d)) {
  69. return 0;
  70. }
  71. if (d > 255) {
  72. return 0;
  73. }
  74. ((uint8_t *)out_addr)[i] = d;
  75. if (i == 3) {
  76. return 1;
  77. }
  78. name += j + 1;
  79. name_len -= j + 1;
  80. }
  81. }
  82. int ipaddr_parse_ipv4_addr (const char *name, uint32_t *out_addr)
  83. {
  84. return ipaddr_parse_ipv4_addr_bin(name, strlen(name), out_addr);
  85. }
  86. int ipaddr_parse_ipv4_prefix_bin (const char *str, size_t str_len, int *num)
  87. {
  88. uintmax_t d;
  89. if (!parse_unsigned_integer_bin(str, str_len, &d)) {
  90. return 0;
  91. }
  92. if (d > 32) {
  93. return 0;
  94. }
  95. *num = d;
  96. return 1;
  97. }
  98. int ipaddr_parse_ipv4_prefix (const char *str, int *num)
  99. {
  100. return ipaddr_parse_ipv4_prefix_bin(str, strlen(str), num);
  101. }
  102. int ipaddr_parse_ipv4_ifaddr_bin (const char *str, size_t str_len, struct ipv4_ifaddr *out)
  103. {
  104. size_t slash_pos;
  105. if (!b_find_char_bin(str, str_len, '/', &slash_pos)) {
  106. return 0;
  107. }
  108. return (ipaddr_parse_ipv4_addr_bin(str, slash_pos, &out->addr) &&
  109. ipaddr_parse_ipv4_prefix_bin(str + slash_pos + 1, str_len - slash_pos - 1, &out->prefix));
  110. }
  111. int ipaddr_parse_ipv4_ifaddr (const char *str, struct ipv4_ifaddr *out)
  112. {
  113. return ipaddr_parse_ipv4_ifaddr_bin(str, strlen(str), out);
  114. }
  115. int ipaddr_ipv4_ifaddr_from_addr_mask (uint32_t addr, uint32_t mask, struct ipv4_ifaddr *out)
  116. {
  117. int prefix;
  118. if (!ipaddr_ipv4_prefix_from_mask(mask, &prefix)) {
  119. return 0;
  120. }
  121. out->addr = addr;
  122. out->prefix = prefix;
  123. return 1;
  124. }
  125. uint32_t ipaddr_ipv4_mask_from_prefix (int prefix)
  126. {
  127. ASSERT(prefix >= 0)
  128. ASSERT(prefix <= 32)
  129. uint32_t t = 0;
  130. for (int i = 0; i < prefix; i++) {
  131. t |= 1 << (32 - i - 1);
  132. }
  133. return hton32(t);
  134. }
  135. int ipaddr_ipv4_prefix_from_mask (uint32_t mask, int *out_prefix)
  136. {
  137. uint32_t t = 0;
  138. int i;
  139. for (i = 0; i <= 32; i++) {
  140. if (ntoh32(mask) == t) {
  141. break;
  142. }
  143. if (i < 32) {
  144. t |= (1 << (32 - i - 1));
  145. }
  146. }
  147. if (!(i <= 32)) {
  148. return 0;
  149. }
  150. *out_prefix = i;
  151. return 1;
  152. }
  153. int ipaddr_ipv4_addrs_in_network (uint32_t addr1, uint32_t addr2, int netprefix)
  154. {
  155. ASSERT(netprefix >= 0)
  156. ASSERT(netprefix <= 32)
  157. uint32_t mask = ipaddr_ipv4_mask_from_prefix(netprefix);
  158. return !!((addr1 & mask) == (addr2 & mask));
  159. }
  160. #endif