ipaddr6.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 <string.h>
  36. #include <stdio.h>
  37. #include <inttypes.h>
  38. #include <misc/debug.h>
  39. // from /etc/iproute2/rt_scopes
  40. #define IPADDR6_SCOPE_GLOBAL 0
  41. #define IPADDR6_SCOPE_HOST 254
  42. #define IPADDR6_SCOPE_LINK 253
  43. #define IPADDR6_SCOPE_SITE 200
  44. struct ipv6_ifaddr {
  45. uint8_t addr[16];
  46. int prefix;
  47. int scope;
  48. };
  49. #define IPADDR6_PRINT_MAX 46
  50. static void ipaddr6_print_addr (const uint8_t *addr, char *out_buf);
  51. void ipaddr6_print_addr (const uint8_t *addr, char *out_buf)
  52. {
  53. int largest_start = 0;
  54. int largest_len = 0;
  55. int current_start = 0;
  56. int current_len = 0;
  57. for (int i = 0; i < 8; i++) {
  58. if (addr[2 * i] == 0 && addr[2 * i + 1] == 0) {
  59. current_len++;
  60. if (current_len > largest_len) {
  61. largest_start = current_start;
  62. largest_len = current_len;
  63. }
  64. } else {
  65. current_start = i + 1;
  66. current_len = 0;
  67. }
  68. }
  69. if (largest_len > 1) {
  70. for (int i = 0; i < largest_start; i++) {
  71. uint16_t block = ((uint16_t)addr[2 * i] << 8) | addr[2 * i + 1];
  72. out_buf += sprintf(out_buf, "%"PRIx16":", block);
  73. }
  74. if (largest_start == 0) {
  75. out_buf += sprintf(out_buf, ":");
  76. }
  77. for (int i = largest_start + largest_len; i < 8; i++) {
  78. uint16_t block = ((uint16_t)addr[2 * i] << 8) | addr[2 * i + 1];
  79. out_buf += sprintf(out_buf, ":%"PRIx16, block);
  80. }
  81. if (largest_start + largest_len == 8) {
  82. out_buf += sprintf(out_buf, ":");
  83. }
  84. } else {
  85. const char *prefix = "";
  86. for (int i = 0; i < 8; i++) {
  87. uint16_t block = ((uint16_t)addr[2 * i] << 8) | addr[2 * i + 1];
  88. out_buf += sprintf(out_buf, "%s%"PRIx16, prefix, block);
  89. prefix = ":";
  90. }
  91. }
  92. }
  93. #endif