publictoip6.c 1.9 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 <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. int main(int argc, char** argv)
  29. {
  30. if (argc < 2) {
  31. return usage(argv[0]);
  32. }
  33. uint8_t keyBytes[32];
  34. uint8_t ip6Bytes[16];
  35. String key = { .bytes = argv[1], .len = CString_strlen(argv[1]) };
  36. int ret = Key_parse(&key, keyBytes, ip6Bytes);
  37. switch (ret) {
  38. case Key_parse_TOO_SHORT: fprintf(stderr, "ERROR: Key_parse_TOO_SHORT\n"); return 1;
  39. case Key_parse_MALFORMED: fprintf(stderr, "ERROR: Key_parse_MALFORMED\n"); return 1;
  40. case Key_parse_DECODE_FAILED: fprintf(stderr, "ERROR: Key_parse_DECODE_FAILED\n"); return 1;
  41. case Key_parse_INVALID: fprintf(stderr, "ERROR: Key_parse_INVALID\n"); return 1;
  42. case 0: break;
  43. default: fprintf(stderr, "ERROR: unknown error [%d]\n", ret); return 1;
  44. }
  45. uint8_t output[40] = {0};
  46. AddrTools_printShortIp(output, ip6Bytes);
  47. printf("%s\n", output);
  48. return 0;
  49. }