ipv6_parse.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**
  2. * Copyright (c) 2015 - 2019, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include <stddef.h>
  41. #include <stdbool.h>
  42. #include <string.h>
  43. #include <stdlib.h>
  44. #include <stdio.h>
  45. #include "ipv6_parse.h"
  46. #include "nrf_error.h"
  47. static uint16_t ascii_to_hex(char * p_str)
  48. {
  49. uint16_t res = 0;
  50. sscanf(p_str, "%hx", &res);
  51. return res;
  52. }
  53. static void reverse_string(char * p_str)
  54. {
  55. uint32_t str_len = strlen(p_str);
  56. for (uint32_t i = 0, j = str_len - 1; i < j; i++, j--)
  57. {
  58. char tmp = p_str[i];
  59. p_str[i] = p_str[j];
  60. p_str[j] = tmp;
  61. }
  62. }
  63. uint32_t ipv6_parse_addr(uint8_t * p_addr, const char * p_uri, uint8_t uri_len)
  64. {
  65. bool is_compressed = false;
  66. uint8_t block_1_len = uri_len;
  67. const char * compressed_position = strstr(&p_uri[0], "::");
  68. if (compressed_position != NULL)
  69. {
  70. is_compressed = true;
  71. block_1_len = compressed_position - &p_uri[0];
  72. }
  73. // Parse block 1.
  74. uint8_t block_1_end = block_1_len;
  75. char sub_addr_buf[5] = {'\0',};
  76. uint8_t char_pos = 0;
  77. uint8_t sub_addr_count_block_1 = 0;
  78. for (uint8_t i = 0; i < block_1_end; i++)
  79. {
  80. if (p_uri[i] != ':')
  81. {
  82. sub_addr_buf[char_pos++] = p_uri[i];
  83. }
  84. else
  85. {
  86. // we have read all number bytes and hit a delimiter. Save the sub address.
  87. uint16_t value = ascii_to_hex(sub_addr_buf);
  88. p_addr[sub_addr_count_block_1++] = ((value & 0xFF00) >> 8);
  89. p_addr[sub_addr_count_block_1++] = ((value & 0x00FF));
  90. char_pos = 0;
  91. memset(sub_addr_buf, '\0', 5);
  92. }
  93. // if we are at the end of block 1, save the last sub address.
  94. if ((i + 1) == block_1_end)
  95. {
  96. uint16_t value = ascii_to_hex(sub_addr_buf);
  97. p_addr[sub_addr_count_block_1++] = ((value & 0xFF00) >> 8);
  98. p_addr[sub_addr_count_block_1++] = ((value & 0x00FF));
  99. char_pos = 0;
  100. memset(sub_addr_buf, '\0', 5);
  101. }
  102. }
  103. if (is_compressed == true)
  104. {
  105. // NOTE: sub_addr_buf must be cleared in previous loop.
  106. // lets parse backwards for second block.
  107. uint8_t block_2_start = block_1_end + 2; // skip the '::' delimiter.
  108. uint8_t block_2_len = uri_len - (block_1_len + 2);
  109. uint8_t block_2_end = block_2_start + block_2_len;
  110. uint8_t sub_addr_count_block_2 = 0;
  111. uint8_t sub_addr_index = 15;
  112. for (uint8_t i = block_2_end - 1; i > block_2_start - 1; i--)
  113. {
  114. if (p_uri[i] != ':')
  115. {
  116. sub_addr_buf[char_pos++] = p_uri[i];
  117. }
  118. else
  119. {
  120. // we have read all number bytes and hit a delimiter. Save the sub address.
  121. reverse_string(sub_addr_buf);
  122. uint16_t value = ascii_to_hex(sub_addr_buf);
  123. p_addr[sub_addr_index--] = ((value & 0x00FF));
  124. p_addr[sub_addr_index--] = ((value & 0xFF00) >> 8);
  125. sub_addr_count_block_2 += 2;
  126. char_pos = 0;
  127. memset(sub_addr_buf, '\0', 5);
  128. }
  129. // if we are at the end of block 1, save the last sub address.
  130. if (i == block_2_start)
  131. {
  132. reverse_string(sub_addr_buf);
  133. uint16_t value = ascii_to_hex(sub_addr_buf);
  134. p_addr[sub_addr_index--] = ((value & 0x00FF));
  135. p_addr[sub_addr_index--] = ((value & 0xFF00) >> 8);
  136. sub_addr_count_block_2 += 2;
  137. char_pos = 0;
  138. memset(sub_addr_buf, '\0', 5);
  139. }
  140. }
  141. for (uint8_t i = sub_addr_count_block_1; i < (16 - sub_addr_count_block_2); i++)
  142. {
  143. p_addr[i] = 0x00;
  144. }
  145. }
  146. return NRF_SUCCESS;
  147. }