From 3b301f823ad3993d25d92fdd3905bdb9b2409487 Mon Sep 17 00:00:00 2001 From: Jose Marcial Vieira Bisneto Date: Thu, 19 Jan 2017 20:45:24 -0300 Subject: [PATCH 1/2] Initial implementation of record/record --- Makefile | 4 ++- include/libp2p/record/record.h | 7 ++++ record/Makefile | 14 ++++++++ record/record.c | 58 ++++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 include/libp2p/record/record.h create mode 100644 record/Makefile create mode 100644 record/record.c diff --git a/Makefile b/Makefile index 2373033..87b5aad 100644 --- a/Makefile +++ b/Makefile @@ -2,13 +2,14 @@ DEBUG = true export DEBUG -OBJS = crypto/*.o crypto/encoding/*.o thirdparty/mbedtls/*.o hashmap/hashmap.o +OBJS = crypto/*.o crypto/encoding/*.o thirdparty/mbedtls/*.o hashmap/hashmap.o record/*.o compile: cd crypto; make all; cd thirdparty; make all; cd hashmap; make all; + cd record; make all; ar rcs libp2p.a $(OBJS) test: compile @@ -21,5 +22,6 @@ clean: cd hashmap; make clean; cd thirdparty; make clean cd test; make clean; + cd record; make clean; rm -rf libp2p.a diff --git a/include/libp2p/record/record.h b/include/libp2p/record/record.h new file mode 100644 index 0000000..0a36b85 --- /dev/null +++ b/include/libp2p/record/record.h @@ -0,0 +1,7 @@ +#ifndef LIBP2P_RECORD_H + #define LIBP2P_RECORD_H + + #define RECORD_BUFSIZE 1024 + + int libp2p_record_make_put_record (char* record, struct RsaPrivateKey* sk, char* key, char* value, size_t vlen, int sign); +#endif // LIBP2P_RECORD_H diff --git a/record/Makefile b/record/Makefile new file mode 100644 index 0000000..f72c847 --- /dev/null +++ b/record/Makefile @@ -0,0 +1,14 @@ +CC = gcc +CFLAGS = -O0 -I../include -I../../c-protobuf -I../../c-multihash/include -g3 +LFLAGS = +DEPS = +OBJS = record.o + +%.o: %.c $(DEPS) + $(CC) -c -o $@ $< $(CFLAGS) + + +all: $(OBJS) + +clean: + rm -f $(OBJS) diff --git a/record/record.c b/record/record.c new file mode 100644 index 0000000..1d81bac --- /dev/null +++ b/record/record.c @@ -0,0 +1,58 @@ +#include +#include + +#include "libp2p/crypto/rsa.h" +#include "libp2p/record/record.h" +#include "protobuf.h" +#include "mh/hashes.h" +#include "mh/multihash.h" + +// libp2p_record_make_put_record creates and signs a dht record for the given key/value pair +int libp2p_record_make_put_record (char* record, struct RsaPrivateKey* sk, char* key, char* value, size_t vlen, int sign) +{ + char *pkh; + int pkh_len; + size_t len = 0, l; + + record = malloc(RECORD_BUFSIZE); + + if (record) { + memset (record, '\0', len); + if (!protobuf_encode_string (1, WIRETYPE_LENGTH_DELIMITED, key, record, RECORD_BUFSIZE, &l)) { + free (record); + return -1; + } + len += l; + if (!protobuf_encode_length_delimited (2, WIRETYPE_LENGTH_DELIMITED, value, vlen, record+len, RECORD_BUFSIZE-len, &l)) { + free (record); + return -1; + } + len += l; + pkh_len = mh_new_length(MH_H_SHA2_256, sk->public_key_length); + pkh = malloc(pkh_len); + if (!pkh) { + free (record); + return -1; + } + if (mh_new(pkh, MH_H_SHA2_256, sk->public_key_der, sk->public_key_length)) { + free (pkh); + free (record); + return -1; + } + if (!protobuf_encode_length_delimited (3, WIRETYPE_LENGTH_DELIMITED, pkh, pkh_len, record+len, RECORD_BUFSIZE-len, &l)) { + free (pkh); + free (record); + return -1; + } + len += l; + if (sign) { + //TODO: missing signature function at libp2p-crypto ? + //sign(sk, signature, record, len); + //proto encode signature. + free (pkh); + free (record); + return -1; // not implemented. + } + } + return 0; // sucess. +} From e3fc5f640953ef23eb28ccffd086c05e2e7947c2 Mon Sep 17 00:00:00 2001 From: Jose Marcial Vieira Bisneto Date: Thu, 19 Jan 2017 21:31:46 -0300 Subject: [PATCH 2/2] Fixed record pointer, should be pointer of pointer so can return allocated memory. --- include/libp2p/record/record.h | 2 +- record/record.c | 39 +++++++++++++++++++++------------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/include/libp2p/record/record.h b/include/libp2p/record/record.h index 0a36b85..b29003f 100644 --- a/include/libp2p/record/record.h +++ b/include/libp2p/record/record.h @@ -3,5 +3,5 @@ #define RECORD_BUFSIZE 1024 - int libp2p_record_make_put_record (char* record, struct RsaPrivateKey* sk, char* key, char* value, size_t vlen, int sign); + int libp2p_record_make_put_record (char** record, size_t *rec_size, struct RsaPrivateKey* sk, char* key, char* value, size_t vlen, int sign); #endif // LIBP2P_RECORD_H diff --git a/record/record.c b/record/record.c index 1d81bac..3cd8af2 100644 --- a/record/record.c +++ b/record/record.c @@ -8,51 +8,60 @@ #include "mh/multihash.h" // libp2p_record_make_put_record creates and signs a dht record for the given key/value pair -int libp2p_record_make_put_record (char* record, struct RsaPrivateKey* sk, char* key, char* value, size_t vlen, int sign) +int libp2p_record_make_put_record (char** record, size_t *rec_size, struct RsaPrivateKey* sk, char* key, char* value, size_t vlen, int sign) { - char *pkh; + char *pkh, *p; int pkh_len; size_t len = 0, l; - record = malloc(RECORD_BUFSIZE); + *record = NULL; *rec_size = 0; + p = malloc(RECORD_BUFSIZE); - if (record) { - memset (record, '\0', len); - if (!protobuf_encode_string (1, WIRETYPE_LENGTH_DELIMITED, key, record, RECORD_BUFSIZE, &l)) { - free (record); + if (p) { + memset (p, '\0', len); + if (!protobuf_encode_string (1, WIRETYPE_LENGTH_DELIMITED, key, p, RECORD_BUFSIZE, &l)) { + free (p); return -1; } len += l; - if (!protobuf_encode_length_delimited (2, WIRETYPE_LENGTH_DELIMITED, value, vlen, record+len, RECORD_BUFSIZE-len, &l)) { - free (record); + if (!protobuf_encode_length_delimited (2, WIRETYPE_LENGTH_DELIMITED, value, vlen, p+len, RECORD_BUFSIZE-len, &l)) { + free (p); return -1; } len += l; pkh_len = mh_new_length(MH_H_SHA2_256, sk->public_key_length); pkh = malloc(pkh_len); if (!pkh) { - free (record); + free (p); return -1; } if (mh_new(pkh, MH_H_SHA2_256, sk->public_key_der, sk->public_key_length)) { free (pkh); - free (record); + free (p); return -1; } - if (!protobuf_encode_length_delimited (3, WIRETYPE_LENGTH_DELIMITED, pkh, pkh_len, record+len, RECORD_BUFSIZE-len, &l)) { + if (!protobuf_encode_length_delimited (3, WIRETYPE_LENGTH_DELIMITED, pkh, pkh_len, p+len, RECORD_BUFSIZE-len, &l)) { free (pkh); - free (record); + free (p); return -1; } + free (pkh); len += l; if (sign) { //TODO: missing signature function at libp2p-crypto ? - //sign(sk, signature, record, len); + //sign(sk, signature, p, len); //proto encode signature. free (pkh); - free (record); + free (p); return -1; // not implemented. } } + *record = realloc(p, len); // Reduces memory used for just what is needed. + if (*record) { + *rec_size = len; + } else { + free (p); + return -1; + } return 0; // sucess. }