ipaddr.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 <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. struct ipv4_ifaddr {
  42. uint32_t addr;
  43. int prefix;
  44. };
  45. static int ipaddr_parse_ipv4_addr_bin (char *name, size_t name_len, uint32_t *out_addr);
  46. static int ipaddr_parse_ipv4_addr (char *name, uint32_t *out_addr);
  47. static int ipaddr_parse_ipv4_prefix_bin (char *str, size_t str_len, int *num);
  48. static int ipaddr_parse_ipv4_prefix (char *str, int *num);
  49. static int ipaddr_parse_ipv4_ifaddr (char *str, struct ipv4_ifaddr *out);
  50. static int ipaddr_ipv4_ifaddr_from_addr_mask (uint32_t addr, uint32_t mask, struct ipv4_ifaddr *out);
  51. static uint32_t ipaddr_ipv4_mask_from_prefix (int prefix);
  52. static int ipaddr_ipv4_prefix_from_mask (uint32_t mask, int *out_prefix);
  53. static int ipaddr_ipv4_addrs_in_network (uint32_t addr1, uint32_t addr2, int netprefix);
  54. int ipaddr_parse_ipv4_addr_bin (char *name, size_t name_len, uint32_t *out_addr)
  55. {
  56. for (size_t i = 0; ; i++) {
  57. size_t j;
  58. for (j = 0; j < name_len && name[j] != '.'; j++);
  59. if ((j == name_len && i < 3) || (j < name_len && i == 3)) {
  60. return 0;
  61. }
  62. if (j < 1 || j > 3) {
  63. return 0;
  64. }
  65. uintmax_t d;
  66. if (!parse_unsigned_integer_bin(name, j, &d)) {
  67. return 0;
  68. }
  69. if (d > 255) {
  70. return 0;
  71. }
  72. ((uint8_t *)out_addr)[i] = d;
  73. if (i == 3) {
  74. return 1;
  75. }
  76. name += j + 1;
  77. name_len -= j + 1;
  78. }
  79. }
  80. int ipaddr_parse_ipv4_addr (char *name, uint32_t *out_addr)
  81. {
  82. return ipaddr_parse_ipv4_addr_bin(name, strlen(name), out_addr);
  83. }
  84. int ipaddr_parse_ipv4_prefix_bin (char *str, size_t str_len, int *num)
  85. {
  86. uintmax_t d;
  87. if (!parse_unsigned_integer_bin(str, str_len, &d)) {
  88. return 0;
  89. }
  90. if (d > 32) {
  91. return 0;
  92. }
  93. *num = d;
  94. return 1;
  95. }
  96. int ipaddr_parse_ipv4_prefix (char *str, int *num)
  97. {
  98. return ipaddr_parse_ipv4_prefix_bin(str, strlen(str), num);
  99. }
  100. int ipaddr_parse_ipv4_ifaddr (char *str, struct ipv4_ifaddr *out)
  101. {
  102. char *slash = strstr(str, "/");
  103. if (!slash) {
  104. return 0;
  105. }
  106. if (!ipaddr_parse_ipv4_addr_bin(str, (slash - str), &out->addr)) {
  107. return 0;
  108. }
  109. if (!ipaddr_parse_ipv4_prefix(slash + 1, &out->prefix)) {
  110. return 0;
  111. }
  112. return 1;
  113. }
  114. int ipaddr_ipv4_ifaddr_from_addr_mask (uint32_t addr, uint32_t mask, struct ipv4_ifaddr *out)
  115. {
  116. int prefix;
  117. if (!ipaddr_ipv4_prefix_from_mask(mask, &prefix)) {
  118. return 0;
  119. }
  120. out->addr = addr;
  121. out->prefix = prefix;
  122. return 1;
  123. }
  124. uint32_t ipaddr_ipv4_mask_from_prefix (int prefix)
  125. {
  126. ASSERT(prefix >= 0)
  127. ASSERT(prefix <= 32)
  128. uint32_t t = 0;
  129. for (int i = 0; i < prefix; i++) {
  130. t |= 1 << (32 - i - 1);
  131. }
  132. return hton32(t);
  133. }
  134. int ipaddr_ipv4_prefix_from_mask (uint32_t mask, int *out_prefix)
  135. {
  136. uint32_t t = 0;
  137. int i;
  138. for (i = 0; i <= 32; i++) {
  139. if (ntoh32(mask) == t) {
  140. break;
  141. }
  142. if (i < 32) {
  143. t |= (1 << (32 - i - 1));
  144. }
  145. }
  146. if (!(i <= 32)) {
  147. return 0;
  148. }
  149. *out_prefix = i;
  150. return 1;
  151. }
  152. int ipaddr_ipv4_addrs_in_network (uint32_t addr1, uint32_t addr2, int netprefix)
  153. {
  154. ASSERT(netprefix >= 0)
  155. ASSERT(netprefix <= 32)
  156. uint32_t mask = ipaddr_ipv4_mask_from_prefix(netprefix);
  157. return !!((addr1 & mask) == (addr2 & mask));
  158. }
  159. #endif