Fixed record pointer, should be pointer of pointer so can return allocated memory.

This commit is contained in:
Jose Marcial Vieira Bisneto 2017-01-19 21:31:46 -03:00
parent 3b301f823a
commit e3fc5f6409
2 changed files with 25 additions and 16 deletions

View file

@ -3,5 +3,5 @@
#define RECORD_BUFSIZE 1024 #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 #endif // LIBP2P_RECORD_H

View file

@ -8,51 +8,60 @@
#include "mh/multihash.h" #include "mh/multihash.h"
// libp2p_record_make_put_record creates and signs a dht record for the given key/value pair // 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; int pkh_len;
size_t len = 0, l; size_t len = 0, l;
record = malloc(RECORD_BUFSIZE); *record = NULL; *rec_size = 0;
p = malloc(RECORD_BUFSIZE);
if (record) { if (p) {
memset (record, '\0', len); memset (p, '\0', len);
if (!protobuf_encode_string (1, WIRETYPE_LENGTH_DELIMITED, key, record, RECORD_BUFSIZE, &l)) { if (!protobuf_encode_string (1, WIRETYPE_LENGTH_DELIMITED, key, p, RECORD_BUFSIZE, &l)) {
free (record); free (p);
return -1; return -1;
} }
len += l; len += l;
if (!protobuf_encode_length_delimited (2, WIRETYPE_LENGTH_DELIMITED, value, vlen, record+len, RECORD_BUFSIZE-len, &l)) { if (!protobuf_encode_length_delimited (2, WIRETYPE_LENGTH_DELIMITED, value, vlen, p+len, RECORD_BUFSIZE-len, &l)) {
free (record); free (p);
return -1; return -1;
} }
len += l; len += l;
pkh_len = mh_new_length(MH_H_SHA2_256, sk->public_key_length); pkh_len = mh_new_length(MH_H_SHA2_256, sk->public_key_length);
pkh = malloc(pkh_len); pkh = malloc(pkh_len);
if (!pkh) { if (!pkh) {
free (record); free (p);
return -1; return -1;
} }
if (mh_new(pkh, MH_H_SHA2_256, sk->public_key_der, sk->public_key_length)) { if (mh_new(pkh, MH_H_SHA2_256, sk->public_key_der, sk->public_key_length)) {
free (pkh); free (pkh);
free (record); free (p);
return -1; 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 (pkh);
free (record); free (p);
return -1; return -1;
} }
free (pkh);
len += l; len += l;
if (sign) { if (sign) {
//TODO: missing signature function at libp2p-crypto ? //TODO: missing signature function at libp2p-crypto ?
//sign(sk, signature, record, len); //sign(sk, signature, p, len);
//proto encode signature. //proto encode signature.
free (pkh); free (pkh);
free (record); free (p);
return -1; // not implemented. 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. return 0; // sucess.
} }