ipaddr.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 (char *name, size_t name_len, uint32_t *out_addr);
  37. static int ipaddr_parse_ipv4_ifaddr (char *str, struct ipv4_ifaddr *out);
  38. static int ipaddr_ipv4_ifaddr_from_addr_mask (uint32_t addr, uint32_t mask, struct ipv4_ifaddr *out);
  39. static int ipaddr_char_is_digit (char c)
  40. {
  41. return (c >= '0' && c <= '9');
  42. }
  43. static int ipaddr_parse_number (char *data, size_t data_len, int *out)
  44. {
  45. if (data_len == 0) {
  46. return 0;
  47. }
  48. for (size_t i = 0; i < data_len; i++) {
  49. if (!ipaddr_char_is_digit(data[i])) {
  50. return 0;
  51. }
  52. }
  53. char data2[data_len + 1];
  54. memcpy(data2, data, data_len);
  55. data2[data_len] = '\0';
  56. *out = atoi(data2);
  57. return 1;
  58. }
  59. int ipaddr_parse_ipv4_addr (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 (!ipaddr_char_is_digit(name[j])) {
  65. return 0;
  66. }
  67. }
  68. if ((j == name_len && i < 3) || (j < name_len && i == 3)) {
  69. return 0;
  70. }
  71. if (j < 1 || j > 3) {
  72. return 0;
  73. }
  74. int d;
  75. if (!ipaddr_parse_number(name, j, &d)) {
  76. return 0;
  77. }
  78. if (d > 255) {
  79. return 0;
  80. }
  81. ((uint8_t *)out_addr)[i] = d;
  82. if (i == 3) {
  83. return 1;
  84. }
  85. name += j + 1;
  86. name_len -= j + 1;
  87. }
  88. }
  89. int ipaddr_parse_ipv4_ifaddr (char *str, struct ipv4_ifaddr *out)
  90. {
  91. char *slash = strstr(str, "/");
  92. if (!slash) {
  93. return 0;
  94. }
  95. if (!ipaddr_parse_ipv4_addr(str, (slash - str), &out->addr)) {
  96. return 0;
  97. }
  98. char *prefix = slash + 1;
  99. size_t prefix_len = strlen(prefix);
  100. if (prefix_len > 2) {
  101. return 0;
  102. }
  103. if (!ipaddr_parse_number(prefix, prefix_len, &out->prefix)) {
  104. return 0;
  105. }
  106. if (out->prefix > 32) {
  107. return 0;
  108. }
  109. return 1;
  110. }
  111. int ipaddr_ipv4_ifaddr_from_addr_mask (uint32_t addr, uint32_t mask, struct ipv4_ifaddr *out)
  112. {
  113. // check mask
  114. uint32_t t = 0;
  115. int i;
  116. for (i = 0; i <= 32; i++) {
  117. if (ntoh32(mask) == t) {
  118. break;
  119. }
  120. if (i < 32) {
  121. t |= (1 << (32 - i - 1));
  122. }
  123. }
  124. if (!(i <= 32)) {
  125. return 0;
  126. }
  127. out->addr = addr;
  128. out->prefix = i;
  129. return 1;
  130. }
  131. #endif