ipaddr.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 <string.h>
  36. #include <stdlib.h>
  37. #include <misc/debug.h>
  38. #include <misc/byteorder.h>
  39. #include <misc/parse_number.h>
  40. #include <misc/find_char.h>
  41. #include <misc/print_macros.h>
  42. #include <misc/memref.h>
  43. struct ipv4_ifaddr {
  44. uint32_t addr;
  45. int prefix;
  46. };
  47. static int ipaddr_parse_ipv4_addr (MemRef name, uint32_t *out_addr);
  48. static int ipaddr_parse_ipv4_prefix (MemRef str, int *num);
  49. static int ipaddr_parse_ipv4_ifaddr (MemRef 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. #define IPADDR_PRINT_MAX 19
  55. static void ipaddr_print_addr (uint32_t addr, char *out);
  56. static void ipaddr_print_ifaddr (struct ipv4_ifaddr ifaddr, char *out);
  57. int ipaddr_parse_ipv4_addr (MemRef name, uint32_t *out_addr)
  58. {
  59. for (size_t i = 0; ; i++) {
  60. size_t j;
  61. for (j = 0; j < name.len && name.ptr[j] != '.'; j++);
  62. if ((j == name.len && i < 3) || (j < name.len && i == 3)) {
  63. return 0;
  64. }
  65. if (j < 1 || j > 3) {
  66. return 0;
  67. }
  68. uintmax_t d;
  69. if (!parse_unsigned_integer(MemRef_SubTo(name, j), &d)) {
  70. return 0;
  71. }
  72. if (d > 255) {
  73. return 0;
  74. }
  75. ((uint8_t *)out_addr)[i] = d;
  76. if (i == 3) {
  77. return 1;
  78. }
  79. name.ptr += j + 1;
  80. name.len -= j + 1;
  81. }
  82. }
  83. int ipaddr_parse_ipv4_prefix (MemRef str, int *num)
  84. {
  85. uintmax_t d;
  86. if (!parse_unsigned_integer(str, &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_ifaddr (MemRef str, struct ipv4_ifaddr *out)
  96. {
  97. size_t slash_pos;
  98. if (!MemRef_FindChar(str, '/', &slash_pos)) {
  99. return 0;
  100. }
  101. return (ipaddr_parse_ipv4_addr(MemRef_SubTo(str, slash_pos), &out->addr) &&
  102. ipaddr_parse_ipv4_prefix(MemRef_SubFrom(str, slash_pos + 1), &out->prefix));
  103. }
  104. int ipaddr_ipv4_ifaddr_from_addr_mask (uint32_t addr, uint32_t mask, struct ipv4_ifaddr *out)
  105. {
  106. int prefix;
  107. if (!ipaddr_ipv4_prefix_from_mask(mask, &prefix)) {
  108. return 0;
  109. }
  110. out->addr = addr;
  111. out->prefix = prefix;
  112. return 1;
  113. }
  114. uint32_t ipaddr_ipv4_mask_from_prefix (int prefix)
  115. {
  116. ASSERT(prefix >= 0)
  117. ASSERT(prefix <= 32)
  118. uint32_t t = 0;
  119. for (int i = 0; i < prefix; i++) {
  120. t |= 1 << (32 - i - 1);
  121. }
  122. return hton32(t);
  123. }
  124. int ipaddr_ipv4_prefix_from_mask (uint32_t mask, int *out_prefix)
  125. {
  126. uint32_t t = 0;
  127. int i;
  128. for (i = 0; i <= 32; i++) {
  129. if (ntoh32(mask) == t) {
  130. break;
  131. }
  132. if (i < 32) {
  133. t |= (1 << (32 - i - 1));
  134. }
  135. }
  136. if (!(i <= 32)) {
  137. return 0;
  138. }
  139. *out_prefix = i;
  140. return 1;
  141. }
  142. int ipaddr_ipv4_addrs_in_network (uint32_t addr1, uint32_t addr2, int netprefix)
  143. {
  144. ASSERT(netprefix >= 0)
  145. ASSERT(netprefix <= 32)
  146. uint32_t mask = ipaddr_ipv4_mask_from_prefix(netprefix);
  147. return !!((addr1 & mask) == (addr2 & mask));
  148. }
  149. void ipaddr_print_addr (uint32_t addr, char *out)
  150. {
  151. ASSERT(out)
  152. uint8_t *b = (uint8_t *)&addr;
  153. sprintf(out, "%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8,
  154. b[0], b[1], b[2], b[3]);
  155. }
  156. void ipaddr_print_ifaddr (struct ipv4_ifaddr ifaddr, char *out)
  157. {
  158. ASSERT(ifaddr.prefix >= 0)
  159. ASSERT(ifaddr.prefix <= 32)
  160. ASSERT(out)
  161. uint8_t *b = (uint8_t *)&ifaddr.addr;
  162. sprintf(out, "%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8"/%d",
  163. b[0], b[1], b[2], b[3], ifaddr.prefix);
  164. }
  165. #endif