publictoip6.c 2.0 KB

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