AddrTools.c 6.5 KB

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