ipaddr.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * @file ipaddr.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * IP address parsing functions.
  25. */
  26. #ifndef BADVPN_MISC_IPADDR_H
  27. #define BADVPN_MISC_IPADDR_H
  28. #include <stdint.h>
  29. #include <string.h>
  30. #include <stdlib.h>
  31. #include <misc/byteorder.h>
  32. struct ipv4_ifaddr {
  33. uint32_t addr;
  34. int prefix;
  35. };
  36. static int ipaddr_parse_ipv4_addr_bin (char *name, size_t name_len, uint32_t *out_addr);
  37. static int ipaddr_parse_ipv4_addr (char *name, uint32_t *out_addr);
  38. static int ipaddr_parse_ipv4_prefix (char *str, int *num);
  39. static int ipaddr_parse_ipv4_ifaddr (char *str, struct ipv4_ifaddr *out);
  40. static int ipaddr_ipv4_ifaddr_from_addr_mask (uint32_t addr, uint32_t mask, struct ipv4_ifaddr *out);
  41. static int ipaddr_char_is_digit (char c)
  42. {
  43. return (c >= '0' && c <= '9');
  44. }
  45. static int ipaddr_parse_number (char *data, size_t data_len, int *out)
  46. {
  47. if (data_len == 0) {
  48. return 0;
  49. }
  50. for (size_t i = 0; i < data_len; i++) {
  51. if (!ipaddr_char_is_digit(data[i])) {
  52. return 0;
  53. }
  54. }
  55. char data2[data_len + 1];
  56. memcpy(data2, data, data_len);
  57. data2[data_len] = '\0';
  58. *out = atoi(data2);
  59. return 1;
  60. }
  61. int ipaddr_parse_ipv4_addr_bin (char *name, size_t name_len, uint32_t *out_addr)
  62. {
  63. for (size_t i = 0; ; i++) {
  64. size_t j;
  65. for (j = 0; j < name_len && name[j] != '.'; j++) {
  66. if (!ipaddr_char_is_digit(name[j])) {
  67. return 0;
  68. }
  69. }
  70. if ((j == name_len && i < 3) || (j < name_len && i == 3)) {
  71. return 0;
  72. }
  73. if (j < 1 || j > 3) {
  74. return 0;
  75. }
  76. int d;
  77. if (!ipaddr_parse_number(name, j, &d)) {
  78. return 0;
  79. }
  80. if (d > 255) {
  81. return 0;
  82. }
  83. ((uint8_t *)out_addr)[i] = d;
  84. if (i == 3) {
  85. return 1;
  86. }
  87. name += j + 1;
  88. name_len -= j + 1;
  89. }
  90. }
  91. int ipaddr_parse_ipv4_addr (char *name, uint32_t *out_addr)
  92. {
  93. return ipaddr_parse_ipv4_addr_bin(name, strlen(name), out_addr);
  94. }
  95. int ipaddr_parse_ipv4_prefix (char *str, int *num)
  96. {
  97. if (strlen(str) > 2 || !ipaddr_parse_number(str, strlen(str), num)) {
  98. return 0;
  99. }
  100. if (*num > 32) {
  101. return 0;
  102. }
  103. return 1;
  104. }
  105. int ipaddr_parse_ipv4_ifaddr (char *str, struct ipv4_ifaddr *out)
  106. {
  107. char *slash = strstr(str, "/");
  108. if (!slash) {
  109. return 0;
  110. }
  111. if (!ipaddr_parse_ipv4_addr_bin(str, (slash - str), &out->addr)) {
  112. return 0;
  113. }
  114. char *prefix = slash + 1;
  115. size_t prefix_len = strlen(prefix);
  116. if (prefix_len > 2) {
  117. return 0;
  118. }
  119. if (!ipaddr_parse_number(prefix, prefix_len, &out->prefix)) {
  120. return 0;
  121. }
  122. if (out->prefix > 32) {
  123. return 0;
  124. }
  125. return 1;
  126. }
  127. int ipaddr_ipv4_ifaddr_from_addr_mask (uint32_t addr, uint32_t mask, struct ipv4_ifaddr *out)
  128. {
  129. // check mask
  130. uint32_t t = 0;
  131. int i;
  132. for (i = 0; i <= 32; i++) {
  133. if (ntoh32(mask) == t) {
  134. break;
  135. }
  136. if (i < 32) {
  137. t |= (1 << (32 - i - 1));
  138. }
  139. }
  140. if (!(i <= 32)) {
  141. return 0;
  142. }
  143. out->addr = addr;
  144. out->prefix = i;
  145. return 1;
  146. }
  147. #endif