ipaddr6.h 10 KB

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