Permalink
Browse files

Added publictopi6.c for calculating cjdns IP6 addresses from public k…

…ey, fixed bug in DevUrandomRandomSeed which was only taking sizeof(uintptr_t) bytes of random, made LinuxRandomUuidSysctlRandomSeed work on linux since KERN_RANDOM is an enum.
  • Loading branch information...
1 parent 892477d commit 1e3d50cc80bbdd1f41a5082528b87560498a6006 Caleb James DeLisle committed Feb 7, 2013
View
@@ -255,6 +255,13 @@ target_link_libraries(privatetopublic
util
crypto
)
+add_executable(publictoip6
+ publictoip6.c
+)
+target_link_libraries(publictoip6
+ util
+ cjdns-crypto-key
+)
#INSTALL(TARGETS cjdroute RUNTIME DESTINATION bin)
@@ -27,7 +27,7 @@
static int get(struct RandomSeed* randomSeed, uint64_t output[8])
{
- Bits_memset(output, 0, sizeof(output));
+ Bits_memset(output, 0, 64);
int fd = -1;
int tries = 0;
while ((fd = open("/dev/urandom", O_RDONLY, 0)) < 0) {
@@ -38,7 +38,7 @@ static int get(struct RandomSeed* randomSeed, uint64_t output[8])
}
tries = 0;
uint8_t* buff = (uint8_t*) output;
- int count = sizeof(output);
+ int count = 64;
while (count > 0) {
int r = read(fd, buff, count);
if (r < 1) {
@@ -52,7 +52,7 @@ static int get(struct RandomSeed* randomSeed, uint64_t output[8])
count -= r;
}
close(fd);
- if (count == 0 && !Bits_isZero(output, sizeof(output))) {
+ if (count == 0 && !Bits_isZero(output, 64)) {
return 0;
}
return -1;
@@ -20,7 +20,7 @@
#include <unistd.h>
#include <sys/sysctl.h>
-#if defined(KERN_RANDOM) && defined(RANDOM_UUID)
+#ifdef Linux
static int getUUID(uint64_t output[2])
{
int mib[] = { CTL_KERN, KERN_RANDOM, RANDOM_UUID };
View
@@ -0,0 +1,54 @@
+/* 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;
+}
+

0 comments on commit 1e3d50c

Please sign in to comment.