ipaddr6.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /**
  2. * @file ipaddr6.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. * IPv6 address parsing functions.
  32. */
  33. #ifndef BADVPN_MISC_IPADDR6_H
  34. #define BADVPN_MISC_IPADDR6_H
  35. #include <stdio.h>
  36. #include <inttypes.h>
  37. #include <string.h>
  38. #include <stdlib.h>
  39. #include <limits.h>
  40. #include <misc/debug.h>
  41. #include <misc/byteorder.h>
  42. #include <misc/parse_number.h>
  43. #include <misc/find_char.h>
  44. struct ipv6_addr {
  45. uint8_t bytes[16];
  46. };
  47. struct ipv6_ifaddr {
  48. struct ipv6_addr addr;
  49. int prefix;
  50. };
  51. static int ipaddr6_parse_ipv6_addr_bin (const char *name, size_t name_len, struct ipv6_addr *out_addr);
  52. static int ipaddr6_parse_ipv6_addr (const char *name, struct ipv6_addr *out_addr);
  53. static int ipaddr6_parse_ipv6_prefix_bin (const char *str, size_t str_len, int *out_num);
  54. static int ipaddr6_parse_ipv6_prefix (const char *str, int *out_num);
  55. static int ipaddr6_parse_ipv6_ifaddr_bin (const char *str, size_t str_len, struct ipv6_ifaddr *out);
  56. static int ipaddr6_parse_ipv6_ifaddr (const char *str, struct ipv6_ifaddr *out);
  57. static int ipaddr6_ipv6_ifaddr_from_addr_mask (struct ipv6_addr addr, struct ipv6_addr mask, struct ipv6_ifaddr *out);
  58. static void ipaddr6_ipv6_mask_from_prefix (int prefix, struct ipv6_addr *out_mask);
  59. static int ipaddr6_ipv6_prefix_from_mask (struct ipv6_addr mask, int *out_prefix);
  60. static int ipaddr6_ipv6_addrs_in_network (struct ipv6_addr addr1, struct ipv6_addr addr2, int netprefix);
  61. #define IPADDR6_PRINT_MAX 46
  62. static void ipaddr6_print_addr (struct ipv6_addr addr, char *out_buf);
  63. int ipaddr6_parse_ipv6_addr_bin (const char *name, size_t name_len, struct ipv6_addr *out_addr)
  64. {
  65. int num_blocks = 0;
  66. int compress_pos = -1;
  67. uint16_t block = 0;
  68. int empty = 1;
  69. size_t i = 0;
  70. while (i < name_len) {
  71. if (name[i] == '.') {
  72. goto ipv4_ending;
  73. } else if (name[i] == ':') {
  74. int is_double = (i + 1 < name_len && name[i + 1] == ':');
  75. if (i > 0) {
  76. if (empty || num_blocks == 7) {
  77. return 0;
  78. }
  79. out_addr->bytes[2 * num_blocks + 0] = block >> 8;
  80. out_addr->bytes[2 * num_blocks + 1] = block & 0xFF;
  81. num_blocks++;
  82. block = 0;
  83. empty = 1;
  84. }
  85. else if (!is_double) {
  86. return 0;
  87. }
  88. if (is_double) {
  89. if (compress_pos != -1) {
  90. return 0;
  91. }
  92. compress_pos = num_blocks;
  93. }
  94. i += 1 + is_double;
  95. } else {
  96. int digit = decode_hex_digit(name[i]);
  97. if (digit < 0) {
  98. return 0;
  99. }
  100. if (block > UINT16_MAX / 16) {
  101. return 0;
  102. }
  103. block *= 16;
  104. if (digit > UINT16_MAX - block) {
  105. return 0;
  106. }
  107. block += digit;
  108. empty = 0;
  109. i += 1;
  110. }
  111. }
  112. if (!empty) {
  113. out_addr->bytes[2 * num_blocks + 0] = block >> 8;
  114. out_addr->bytes[2 * num_blocks + 1] = block & 0xFF;
  115. num_blocks++;
  116. }
  117. else if (num_blocks != compress_pos) {
  118. return 0;
  119. }
  120. ipv4_done:
  121. if (compress_pos == -1) {
  122. if (num_blocks != 8) {
  123. return 0;
  124. }
  125. compress_pos = 0;
  126. }
  127. int num_rear = num_blocks - compress_pos;
  128. memmove(out_addr->bytes + 2 * (8 - num_rear), out_addr->bytes + 2 * compress_pos, 2 * num_rear);
  129. memset(out_addr->bytes + 2 * compress_pos, 0, 2 * (8 - num_rear - compress_pos));
  130. return 1;
  131. ipv4_ending:
  132. if (empty || (num_blocks == 0 && compress_pos == -1)) {
  133. return 0;
  134. }
  135. while (name[i - 1] != ':') {
  136. i--;
  137. }
  138. uint8_t bytes[4];
  139. int cur_byte = 0;
  140. uint8_t byte = 0;
  141. empty = 1;
  142. while (i < name_len) {
  143. if (name[i] == '.') {
  144. if (empty || cur_byte == 3) {
  145. return 0;
  146. }
  147. bytes[cur_byte] = byte;
  148. cur_byte++;
  149. byte = 0;
  150. empty = 1;
  151. } else {
  152. if (!empty && byte == 0) {
  153. return 0;
  154. }
  155. int digit = decode_decimal_digit(name[i]);
  156. if (digit < 0) {
  157. return 0;
  158. }
  159. if (byte > UINT8_MAX / 10) {
  160. return 0;
  161. }
  162. byte *= 10;
  163. if (digit > UINT8_MAX - byte) {
  164. return 0;
  165. }
  166. byte += digit;
  167. empty = 0;
  168. }
  169. i++;
  170. }
  171. if (cur_byte != 3 || empty) {
  172. return 0;
  173. }
  174. bytes[cur_byte] = byte;
  175. if (8 - num_blocks < 2) {
  176. return 0;
  177. }
  178. memcpy(out_addr->bytes + 2 * num_blocks, bytes, 4);
  179. num_blocks += 2;
  180. goto ipv4_done;
  181. }
  182. int ipaddr6_parse_ipv6_addr (const char *name, struct ipv6_addr *out_addr)
  183. {
  184. return ipaddr6_parse_ipv6_addr_bin(name, strlen(name), out_addr);
  185. }
  186. int ipaddr6_parse_ipv6_prefix_bin (const char *str, size_t str_len, int *out_num)
  187. {
  188. uintmax_t d;
  189. if (!parse_unsigned_integer_bin(str, str_len, &d)) {
  190. return 0;
  191. }
  192. if (d > 128) {
  193. return 0;
  194. }
  195. *out_num = d;
  196. return 1;
  197. }
  198. int ipaddr6_parse_ipv6_prefix (const char *str, int *out_num)
  199. {
  200. return ipaddr6_parse_ipv6_prefix_bin(str, strlen(str), out_num);
  201. }
  202. int ipaddr6_parse_ipv6_ifaddr_bin (const char *str, size_t str_len, struct ipv6_ifaddr *out)
  203. {
  204. size_t slash_pos;
  205. if (!b_find_char_bin(str, str_len, '/', &slash_pos)) {
  206. return 0;
  207. }
  208. return (ipaddr6_parse_ipv6_addr_bin(str, slash_pos, &out->addr) &&
  209. ipaddr6_parse_ipv6_prefix_bin(str + slash_pos + 1, str_len - slash_pos - 1, &out->prefix));
  210. }
  211. int ipaddr6_parse_ipv6_ifaddr (const char *str, struct ipv6_ifaddr *out)
  212. {
  213. return ipaddr6_parse_ipv6_ifaddr_bin(str, strlen(str), out);
  214. }
  215. int ipaddr6_ipv6_ifaddr_from_addr_mask (struct ipv6_addr addr, struct ipv6_addr mask, struct ipv6_ifaddr *out)
  216. {
  217. int prefix;
  218. if (!ipaddr6_ipv6_prefix_from_mask(mask, &prefix)) {
  219. return 0;
  220. }
  221. out->addr = addr;
  222. out->prefix = prefix;
  223. return 1;
  224. }
  225. void ipaddr6_ipv6_mask_from_prefix (int prefix, struct ipv6_addr *out_mask)
  226. {
  227. ASSERT(prefix >= 0)
  228. ASSERT(prefix <= 128)
  229. int quot = prefix / 8;
  230. int rem = prefix % 8;
  231. memset(out_mask->bytes, UINT8_MAX, quot);
  232. memset(out_mask->bytes + quot, 0, 16 - quot);
  233. for (int i = 0; i < rem; i++) {
  234. out_mask->bytes[quot] |= (uint8_t)1 << (8 - i - 1);
  235. }
  236. }
  237. int ipaddr6_ipv6_prefix_from_mask (struct ipv6_addr mask, int *out_prefix)
  238. {
  239. int prefix = 0;
  240. int i = 0;
  241. while (i < 16 && mask.bytes[i] == UINT8_MAX) {
  242. prefix += 8;
  243. i++;
  244. }
  245. if (i < 16) {
  246. uint8_t t = 0;
  247. int j;
  248. for (j = 0; j <= 8; j++) {
  249. if (mask.bytes[i] == t) {
  250. break;
  251. }
  252. if (j < 8) {
  253. t |= ((uint8_t)1 << (8 - j - 1));
  254. }
  255. }
  256. if (!(j <= 8)) {
  257. return 0;
  258. }
  259. prefix += j;
  260. i++;
  261. while (i < 16) {
  262. if (mask.bytes[i] != 0) {
  263. return 0;
  264. }
  265. i++;
  266. }
  267. }
  268. *out_prefix = prefix;
  269. return 1;
  270. }
  271. int ipaddr6_ipv6_addrs_in_network (struct ipv6_addr addr1, struct ipv6_addr addr2, int netprefix)
  272. {
  273. ASSERT(netprefix >= 0)
  274. ASSERT(netprefix <= 128)
  275. int quot = netprefix / 8;
  276. int rem = netprefix % 8;
  277. if (memcmp(addr1.bytes, addr2.bytes, quot)) {
  278. return 0;
  279. }
  280. if (rem == 0) {
  281. return 1;
  282. }
  283. uint8_t t = 0;
  284. for (int i = 0; i < rem; i++) {
  285. t |= (uint8_t)1 << (8 - i - 1);
  286. }
  287. return ((addr1.bytes[quot] & t) == (addr2.bytes[quot] & t));
  288. }
  289. void ipaddr6_print_addr (struct ipv6_addr addr, char *out_buf)
  290. {
  291. int largest_start = 0;
  292. int largest_len = 0;
  293. int current_start = 0;
  294. int current_len = 0;
  295. for (int i = 0; i < 8; i++) {
  296. if (addr.bytes[2 * i] == 0 && addr.bytes[2 * i + 1] == 0) {
  297. current_len++;
  298. if (current_len > largest_len) {
  299. largest_start = current_start;
  300. largest_len = current_len;
  301. }
  302. } else {
  303. current_start = i + 1;
  304. current_len = 0;
  305. }
  306. }
  307. if (largest_len > 1) {
  308. for (int i = 0; i < largest_start; i++) {
  309. uint16_t block = ((uint16_t)addr.bytes[2 * i] << 8) | addr.bytes[2 * i + 1];
  310. out_buf += sprintf(out_buf, "%"PRIx16":", block);
  311. }
  312. if (largest_start == 0) {
  313. out_buf += sprintf(out_buf, ":");
  314. }
  315. for (int i = largest_start + largest_len; i < 8; i++) {
  316. uint16_t block = ((uint16_t)addr.bytes[2 * i] << 8) | addr.bytes[2 * i + 1];
  317. out_buf += sprintf(out_buf, ":%"PRIx16, block);
  318. }
  319. if (largest_start + largest_len == 8) {
  320. out_buf += sprintf(out_buf, ":");
  321. }
  322. } else {
  323. const char *prefix = "";
  324. for (int i = 0; i < 8; i++) {
  325. uint16_t block = ((uint16_t)addr.bytes[2 * i] << 8) | addr.bytes[2 * i + 1];
  326. out_buf += sprintf(out_buf, "%s%"PRIx16, prefix, block);
  327. prefix = ":";
  328. }
  329. }
  330. }
  331. #endif