Fixed multiaddress_get_peer_id to return allocated memory

Returing in place causes problems with continuations i.e. ending
slashes. Therefore it is better to return allocated memory.
This commit is contained in:
John Jones 2017-04-17 11:56:36 -05:00
parent f0961a247f
commit 603ed1d72f
5 changed files with 61 additions and 16 deletions

View file

@ -1,11 +1,3 @@
//
// base58.h
// libp2p_xcode
//
// Created by John Jones on 11/7/16.
// Copyright © 2016 JMJAtlanta. All rights reserved.
//
#ifndef base58_h
#define base58_h
#include "varint.h"

View file

@ -83,7 +83,7 @@ int multiaddress_get_ip_port(const struct MultiAddress* in);
/**
* Pulls the peer ID out of a multiaddress struct
* @param in the MultiAddress
* @returns a pointer to the peer id string, or NULL
* @returns the peer id string, or NULL NOTE: This allocates memory that needs to be freed
*/
char* multiaddress_get_peer_id(const struct MultiAddress* in);

View file

@ -159,10 +159,28 @@ int multiaddress_get_ip_port(const struct MultiAddress* in) {
}
char* multiaddress_get_peer_id(const struct MultiAddress* in) {
char* ptr = strstr(in->string, "/ipfs/");
if (ptr == NULL)
return NULL;
return &ptr[6];
char* result = NULL;
int str_len = 0;
char* slash = NULL;
char* ptr = NULL;
ptr = strstr(in->string, "/ipfs/");
if (ptr != NULL && ptr[6] != 0) {
ptr += 6;
str_len = strlen(ptr);
slash = strchr(ptr, '/');
if (slash != NULL) {
str_len = slash - ptr;
}
if (str_len > 0) {
result = malloc(str_len + 1);
if (result != NULL) {
memset(result, 0, str_len);
memcpy(result, ptr, str_len);
}
}
}
return result;
}
void multiaddress_free(struct MultiAddress* in) {

View file

@ -114,7 +114,7 @@ int test_multiaddr_peer_id() {
char full_string[255];
char* result = NULL;
char* bytes = NULL;
int retVal = 0;
int retVal = 0, port = 0;
struct MultiAddress *addr = NULL, *addr2 = NULL;
sprintf(full_string, "/ip4/127.0.0.1/tcp/4001/ipfs/%s/", orig_address);
@ -123,9 +123,10 @@ int test_multiaddr_peer_id() {
result = multiaddress_get_peer_id(addr);
if (result == NULL || strncmp(result, orig_address, strlen(orig_address)) != 0)
if (result == NULL || strcmp(result, orig_address) != 0)
goto exit;
free(result);
result = NULL;
// switch to bytes and back again to verify the peer id follows...
@ -155,12 +156,20 @@ int test_multiaddr_peer_id() {
goto exit;
}
int port = multiaddress_get_ip_port(addr2);
port = multiaddress_get_ip_port(addr2);
if (port != 4001) {
fprintf(stderr, "Original string had port 4001, but now reporting %d\n", port);
goto exit;
}
result = multiaddress_get_peer_id(addr2);
if (strcmp(result, orig_address) != 0) {
fprintf(stderr, "New peer id %s does not match %s", result, orig_address);
goto exit;
}
free(result);
result = NULL;
retVal = 1;
exit:
if (addr != NULL)
@ -174,6 +183,30 @@ int test_multiaddr_peer_id() {
return retVal;
}
int test_multiaddr_get_peer_id() {
char* orig_address = "QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG";
char full_string[255];
char* result = NULL;
int retVal = 0;
struct MultiAddress *addr = NULL;
sprintf(full_string, "/ip4/127.0.0.1/tcp/4001/ipfs/%s/", orig_address);
addr = multiaddress_new_from_string(full_string);
result = multiaddress_get_peer_id(addr);
if (result == NULL || strcmp(result, orig_address) != 0)
goto exit;
retVal = 1;
exit:
multiaddress_free(addr);
free(result);
result = NULL;
return retVal;
}
int test_multiaddr_bytes() {
int retVal = 0;
char* orig_address = "/ip4/127.0.0.1/tcp/4001/";

View file

@ -9,6 +9,7 @@ const char* names[] = {
"test_int_to_hex",
"test_multiaddr_utils",
"test_multiaddr_peer_id",
"test_multiaddr_get_peer_id",
"test_multiaddr_bytes"
};
@ -19,6 +20,7 @@ int (*funcs[])(void) = {
test_int_to_hex,
test_multiaddr_utils,
test_multiaddr_peer_id,
test_multiaddr_get_peer_id,
test_multiaddr_bytes
};