ipaddr6.h 11 KB

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