publictoip6.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 "crypto/Key.h"
  16. #include "util/AddrTools.h"
  17. #include <stdio.h>
  18. static int usage(char* appName)
  19. {
  20. printf("Usage: %s <public key>\n"
  21. "\n"
  22. "Get a cjdns IPv6 address from a public key.\n"
  23. "The key should be in Base32 and end in '.k'.\n",
  24. appName);
  25. return 0;
  26. }
  27. int main(int argc, char** argv)
  28. {
  29. if (argc < 2) {
  30. return usage(argv[0]);
  31. }
  32. uint8_t keyBytes[32];
  33. uint8_t ip6Bytes[16];
  34. String key = { .bytes = argv[1], .len = strlen(argv[1]) };
  35. int ret = Key_parse(&key, keyBytes, ip6Bytes);
  36. switch (ret) {
  37. case Key_parse_TOO_SHORT: fprintf(stderr, "ERROR: Key_parse_TOO_SHORT\n"); return 1;
  38. case Key_parse_MALFORMED: fprintf(stderr, "ERROR: Key_parse_MALFORMED\n"); return 1;
  39. case Key_parse_DECODE_FAILED: fprintf(stderr, "ERROR: Key_parse_DECODE_FAILED\n"); return 1;
  40. case Key_parse_INVALID: fprintf(stderr, "ERROR: Key_parse_INVALID\n"); return 1;
  41. case 0: break;
  42. default: fprintf(stderr, "ERROR: unknown error [%d]\n", ret); return 1;
  43. }
  44. uint8_t output[40] = {0};
  45. AddrTools_printIp(output, ip6Bytes);
  46. printf("%s\n", output);
  47. return 0;
  48. }