ipaddr.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /**
  2. * @file ipaddr.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  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 <inttypes.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. #define IPADDR_PRINT_MAX 19
  57. static void ipaddr_print_addr (uint32_t addr, char *out);
  58. static void ipaddr_print_ifaddr (struct ipv4_ifaddr ifaddr, char *out);
  59. int ipaddr_parse_ipv4_addr_bin (const char *name, size_t name_len, uint32_t *out_addr)
  60. {
  61. for (size_t i = 0; ; i++) {
  62. size_t j;
  63. for (j = 0; j < name_len && name[j] != '.'; j++);
  64. if ((j == name_len && i < 3) || (j < name_len && i == 3)) {
  65. return 0;
  66. }
  67. if (j < 1 || j > 3) {
  68. return 0;
  69. }
  70. uintmax_t d;
  71. if (!parse_unsigned_integer_bin(name, j, &d)) {
  72. return 0;
  73. }
  74. if (d > 255) {
  75. return 0;
  76. }
  77. ((uint8_t *)out_addr)[i] = d;
  78. if (i == 3) {
  79. return 1;
  80. }
  81. name += j + 1;
  82. name_len -= j + 1;
  83. }
  84. }
  85. int ipaddr_parse_ipv4_addr (const char *name, uint32_t *out_addr)
  86. {
  87. return ipaddr_parse_ipv4_addr_bin(name, strlen(name), out_addr);
  88. }
  89. int ipaddr_parse_ipv4_prefix_bin (const char *str, size_t str_len, int *num)
  90. {
  91. uintmax_t d;
  92. if (!parse_unsigned_integer_bin(str, str_len, &d)) {
  93. return 0;
  94. }
  95. if (d > 32) {
  96. return 0;
  97. }
  98. *num = d;
  99. return 1;
  100. }
  101. int ipaddr_parse_ipv4_prefix (const char *str, int *num)
  102. {
  103. return ipaddr_parse_ipv4_prefix_bin(str, strlen(str), num);
  104. }
  105. int ipaddr_parse_ipv4_ifaddr_bin (const char *str, size_t str_len, struct ipv4_ifaddr *out)
  106. {
  107. size_t slash_pos;
  108. if (!b_find_char_bin(str, str_len, '/', &slash_pos)) {
  109. return 0;
  110. }
  111. return (ipaddr_parse_ipv4_addr_bin(str, slash_pos, &out->addr) &&
  112. ipaddr_parse_ipv4_prefix_bin(str + slash_pos + 1, str_len - slash_pos - 1, &out->prefix));
  113. }
  114. int ipaddr_parse_ipv4_ifaddr (const char *str, struct ipv4_ifaddr *out)
  115. {
  116. return ipaddr_parse_ipv4_ifaddr_bin(str, strlen(str), out);
  117. }
  118. int ipaddr_ipv4_ifaddr_from_addr_mask (uint32_t addr, uint32_t mask, struct ipv4_ifaddr *out)
  119. {
  120. int prefix;
  121. if (!ipaddr_ipv4_prefix_from_mask(mask, &prefix)) {
  122. return 0;
  123. }
  124. out->addr = addr;
  125. out->prefix = prefix;
  126. return 1;
  127. }
  128. uint32_t ipaddr_ipv4_mask_from_prefix (int prefix)
  129. {
  130. ASSERT(prefix >= 0)
  131. ASSERT(prefix <= 32)
  132. uint32_t t = 0;
  133. for (int i = 0; i < prefix; i++) {
  134. t |= 1 << (32 - i - 1);
  135. }
  136. return hton32(t);
  137. }
  138. int ipaddr_ipv4_prefix_from_mask (uint32_t mask, int *out_prefix)
  139. {
  140. uint32_t t = 0;
  141. int i;
  142. for (i = 0; i <= 32; i++) {
  143. if (ntoh32(mask) == t) {
  144. break;
  145. }
  146. if (i < 32) {
  147. t |= (1 << (32 - i - 1));
  148. }
  149. }
  150. if (!(i <= 32)) {
  151. return 0;
  152. }
  153. *out_prefix = i;
  154. return 1;
  155. }
  156. int ipaddr_ipv4_addrs_in_network (uint32_t addr1, uint32_t addr2, int netprefix)
  157. {
  158. ASSERT(netprefix >= 0)
  159. ASSERT(netprefix <= 32)
  160. uint32_t mask = ipaddr_ipv4_mask_from_prefix(netprefix);
  161. return !!((addr1 & mask) == (addr2 & mask));
  162. }
  163. void ipaddr_print_addr (uint32_t addr, char *out)
  164. {
  165. ASSERT(out)
  166. uint8_t *b = (uint8_t *)&addr;
  167. sprintf(out, "%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8,
  168. b[0], b[1], b[2], b[3]);
  169. }
  170. void ipaddr_print_ifaddr (struct ipv4_ifaddr ifaddr, char *out)
  171. {
  172. ASSERT(ifaddr.prefix >= 0)
  173. ASSERT(ifaddr.prefix <= 32)
  174. ASSERT(out)
  175. uint8_t *b = (uint8_t *)&ifaddr.addr;
  176. sprintf(out, "%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"/%d",
  177. b[0], b[1], b[2], b[3], ifaddr.prefix);
  178. }
  179. #endif