forked from cjdelisle/cjdns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublictoip6.c
54 lines (47 loc) · 1.8 KB
/
publictoip6.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/* vim: set expandtab ts=4 sw=4: */
/*
* You may redistribute this program and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "crypto/Key.h"
#include "util/AddrTools.h"
static int usage(char* appName)
{
printf("Usage: %s <public key>\n"
"\n"
"Get a cjdns IPv6 address from a public key.\n"
"The key should be in Base32 and end in '.k'.\n",
appName);
return 0;
}
int main(int argc, char** argv)
{
if (argc < 2) {
return usage(argv[0]);
}
uint8_t keyBytes[32];
uint8_t ip6Bytes[16];
String key = { .bytes = argv[1], .len = strlen(argv[1]) };
int ret = Key_parse(&key, keyBytes, ip6Bytes);
switch (ret) {
case Key_parse_TOO_SHORT: fprintf(stderr, "ERROR: Key_parse_TOO_SHORT\n"); return 1;
case Key_parse_MALFORMED: fprintf(stderr, "ERROR: Key_parse_MALFORMED\n"); return 1;
case Key_parse_DECODE_FAILED: fprintf(stderr, "ERROR: Key_parse_DECODE_FAILED\n"); return 1;
case Key_parse_INVALID: fprintf(stderr, "ERROR: Key_parse_INVALID\n"); return 1;
case 0: break;
default: fprintf(stderr, "ERROR: unknown error [%d]\n", ret); return 1;
}
uint8_t output[40] = {0};
AddrTools_printIp(output, ip6Bytes);
printf("%s\n", output);
return 0;
}