ipaddr.h 4.9 KB

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