1
0

AddrTools.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "util/AddrTools.h"
  16. #include "util/Bits.h"
  17. #include "util/Endian.h"
  18. #include "util/Hex.h"
  19. #include "util/platform/Sockaddr.h"
  20. #include <stdint.h>
  21. /** Takes the path in host byte order. */
  22. void AddrTools_printPath(uint8_t out[20], uint64_t path)
  23. {
  24. uint64_t path_be = Endian_hostToBigEndian64(path);
  25. uint8_t bytes[16];
  26. Hex_encode(bytes, 16, (uint8_t*) &path_be, 8);
  27. out[ 0] = bytes[ 0];
  28. out[ 1] = bytes[ 1];
  29. out[ 2] = bytes[ 2];
  30. out[ 3] = bytes[ 3];
  31. out[ 4] = '.';
  32. out[ 5] = bytes[ 4];
  33. out[ 6] = bytes[ 5];
  34. out[ 7] = bytes[ 6];
  35. out[ 8] = bytes[ 7];
  36. out[ 9] = '.';
  37. out[10] = bytes[ 8];
  38. out[11] = bytes[ 9];
  39. out[12] = bytes[10];
  40. out[13] = bytes[11];
  41. out[14] = '.';
  42. out[15] = bytes[12];
  43. out[16] = bytes[13];
  44. out[17] = bytes[14];
  45. out[18] = bytes[15];
  46. out[19] = '\0';
  47. }
  48. /**
  49. * Parse out a path.
  50. *
  51. * @param out a pointer to a number which will be set to the path in HOST BYTE ORDER.
  52. * @param netAddr a string representation of the path such as "0000.1111.2222.3333" in Big Endian.
  53. * @return 0 if successful, -1 if the netAddr is malformed.
  54. */
  55. int AddrTools_parsePath(uint64_t* out, const uint8_t netAddr[20])
  56. {
  57. if (netAddr[4] != '.' || netAddr[9] != '.' || netAddr[14] != '.' || netAddr[19] != '\0') {
  58. return -1;
  59. }
  60. uint8_t hex[16] = {
  61. netAddr[ 0],
  62. netAddr[ 1],
  63. netAddr[ 2],
  64. netAddr[ 3],
  65. netAddr[ 5],
  66. netAddr[ 6],
  67. netAddr[ 7],
  68. netAddr[ 8],
  69. netAddr[10],
  70. netAddr[11],
  71. netAddr[12],
  72. netAddr[13],
  73. netAddr[15],
  74. netAddr[16],
  75. netAddr[17],
  76. netAddr[18]
  77. };
  78. uint8_t numberBytes[8];
  79. if (Hex_decode(numberBytes, 8, hex, 16) != 8) {
  80. return -1;
  81. }
  82. uint64_t out_be;
  83. Bits_memcpy(&out_be, numberBytes, 8);
  84. *out = Endian_bigEndianToHost64(out_be);
  85. return 0;
  86. }
  87. void AddrTools_printIp(uint8_t output[40], const uint8_t binIp[16])
  88. {
  89. uint8_t hex[32];
  90. Hex_encode(hex, 32, binIp, 16);
  91. output[ 0] = hex[ 0];
  92. output[ 1] = hex[ 1];
  93. output[ 2] = hex[ 2];
  94. output[ 3] = hex[ 3];
  95. output[ 4] = ':';
  96. output[ 5] = hex[ 4];
  97. output[ 6] = hex[ 5];
  98. output[ 7] = hex[ 6];
  99. output[ 8] = hex[ 7];
  100. output[ 9] = ':';
  101. output[10] = hex[ 8];
  102. output[11] = hex[ 9];
  103. output[12] = hex[10];
  104. output[13] = hex[11];
  105. output[14] = ':';
  106. output[15] = hex[12];
  107. output[16] = hex[13];
  108. output[17] = hex[14];
  109. output[18] = hex[15];
  110. output[19] = ':';
  111. output[20] = hex[16];
  112. output[21] = hex[17];
  113. output[22] = hex[18];
  114. output[23] = hex[19];
  115. output[24] = ':';
  116. output[25] = hex[20];
  117. output[26] = hex[21];
  118. output[27] = hex[22];
  119. output[28] = hex[23];
  120. output[29] = ':';
  121. output[30] = hex[24];
  122. output[31] = hex[25];
  123. output[32] = hex[26];
  124. output[33] = hex[27];
  125. output[34] = ':';
  126. output[35] = hex[28];
  127. output[36] = hex[29];
  128. output[37] = hex[30];
  129. output[38] = hex[31];
  130. output[39] = '\0';
  131. }
  132. void AddrTools_printShortIp(uint8_t output[40], const uint8_t binIp[16])
  133. {
  134. /* The chances of hitting :0:0: and breaking
  135. * RFC5952 are 1 in (1 / (2^16))^2 * 6.
  136. * E. Siler
  137. */
  138. char *p = output;
  139. int i = 0;
  140. for (; i < 16;) {
  141. if ((size_t)p != (size_t)output) {
  142. *p++= ':';
  143. }
  144. if (binIp[i] > 0x0F) {
  145. Hex_encode(p, 2, &binIp[i++], 1);
  146. p += 2;
  147. } else if (binIp[i] > 0x00) {
  148. *p++ = Hex_encodeLowNibble(binIp[i++]);
  149. } else {
  150. ++i;
  151. if (binIp[i] > 0x0F) {
  152. Hex_encode(p, 2, &binIp[i++], 1);
  153. p += 2;
  154. } else {
  155. *p++ = Hex_encodeLowNibble(binIp[i++]);
  156. }
  157. continue;
  158. }
  159. Hex_encode(p, 2, &binIp[i++], 1);
  160. p += 2;
  161. }
  162. *p = '\0';
  163. Assert_true((size_t)p <= ((size_t)output + 40));
  164. Assert_true(i <= 16);
  165. }
  166. /**
  167. * Parse out an address.
  168. *
  169. * @param out a pointer to a byte array which will be set to the bytes of the ipv6 address.
  170. * @param hexAddr a string representation of the ipv6 address such as:
  171. * "fc4f:630d:e499:8f5b:c49f:6e6b:01ae:3120".
  172. * @return 0 if successful, -1 if the hexAddr is malformed.
  173. */
  174. int AddrTools_parseIp(uint8_t out[16], const uint8_t* hexAddr)
  175. {
  176. struct Sockaddr_storage ss;
  177. if (Sockaddr_parse((const char*) hexAddr, &ss)
  178. || Sockaddr_getFamily(&ss.addr) != Sockaddr_AF_INET6)
  179. {
  180. return -1;
  181. }
  182. uint8_t* addr = NULL;
  183. Sockaddr_getAddress(&ss.addr, &addr);
  184. Bits_memcpy(out, addr, 16);
  185. return 0;
  186. }
  187. /**
  188. * Parse out an ethernet MAC address.
  189. *
  190. * @param out a pointer to a byte array which will be set to the bytes of the MAC address.
  191. * @param hexAddr a string representation of an ethernet MAC address such as:
  192. * "00:11:22:33:44:55"
  193. * @return 0 if successful, -1 if the hexAddr is malformed.
  194. */
  195. int AddrTools_parseMac(uint8_t out[6], const uint8_t hexAddr[17])
  196. {
  197. for (int i = 2; i < 15; i += 3) {
  198. if (hexAddr[i] != ':') {
  199. return -1;
  200. }
  201. }
  202. uint8_t hex[12];
  203. int j = 0;
  204. for (int i = 0; i < 18; i++) {
  205. hex[j++] = hexAddr[i++];
  206. hex[j++] = hexAddr[i++];
  207. }
  208. if (Hex_decode(out, 6, hex, 12) != 6) {
  209. return -1;
  210. }
  211. return 0;
  212. }
  213. void AddrTools_printMac(uint8_t output[18], const uint8_t binMac[6])
  214. {
  215. uint8_t hex[12];
  216. Hex_encode(hex, 12, binMac, 6);
  217. output[ 0] = hex[ 0];
  218. output[ 1] = hex[ 1];
  219. output[ 2] = ':';
  220. output[ 3] = hex[ 2];
  221. output[ 4] = hex[ 3];
  222. output[ 5] = ':';
  223. output[ 6] = hex[ 4];
  224. output[ 7] = hex[ 5];
  225. output[ 8] = ':';
  226. output[ 9] = hex[ 6];
  227. output[10] = hex[ 7];
  228. output[11] = ':';
  229. output[12] = hex[ 8];
  230. output[13] = hex[ 9];
  231. output[14] = ':';
  232. output[15] = hex[10];
  233. output[16] = hex[11];
  234. output[17] = '\0';
  235. }