Compare commits

..

10 commits

Author SHA1 Message Date
Agorise
b1f7d607ee
Update LICENSE 2019-01-01 10:36:34 -06:00
Agorise
a271e08996
Update LICENSE 2018-06-17 16:31:04 +03:00
Agorise
028223e46a
Update LICENSE 2017-11-15 13:24:45 +02:00
Agorise
4d46d86514 Update LICENSE 2017-10-16 13:53:40 +03:00
jmjatlanta
4c457c1dd3 Better error reporting 2017-09-21 09:45:26 -05:00
jmjatlanta
3b178a142d Added helper method to compare ipfs address without IP address 2017-08-24 14:36:03 -05:00
John Jones
c2f2c0c2da Added a compare method 2017-07-26 07:36:45 -05:00
jmjatlanta
c434fbd160 Fixed warnings 2017-07-17 16:13:53 -05:00
John Jones
86d3cd422e Fixed memory leak and most compiler warnings 2017-07-17 15:11:08 -05:00
John Jones
58a6240af9 Fixed bug on odd byte ip addresses
IP addresses that when converted to an integer ended up as a string with
an odd number of bytes would result in a memory read into invalid
memory. Zero filled the text string representation of the bytes to avoid
this issue.
2017-07-13 18:29:02 -05:00
8 changed files with 130 additions and 27 deletions

View file

@ -1,6 +1,9 @@
MIT License
Copyright (c) 2017 BitShares Munich IVS
Copyright (c) 2019 AGORISE, LTD.
An International Business Company, Cyprus Reg# ΗΕ375959
Contains works from BitShares Munich IVS
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View file

@ -9,7 +9,7 @@
* @param binary_data_size the size of the results buffer
* @returns true(1) on success
*/
int multiaddr_encoding_base58_decode(const unsigned char* base58, size_t base58_size, unsigned char** binary_data, size_t *binary_data_size);
int multiaddr_encoding_base58_decode(const char* base58, size_t base58_size, unsigned char** binary_data, size_t *binary_data_size);
/**
* encode an array of bytes into a base58 string

View file

@ -87,4 +87,20 @@ int multiaddress_get_ip_port(const struct MultiAddress* in);
*/
char* multiaddress_get_peer_id(const struct MultiAddress* in);
/**
* Check to see how these two addresses compare
* @param a side A
* @param b side B
* @returns <0 if B > A; >0 if A > B; 0 if A == B
*/
int multiaddress_compare(const struct MultiAddress* a, const struct MultiAddress* b);
/**
* Check to see how these two addresses compare, ignoring IP address, only looking at ID hash
* @param a side A
* @param b side B
* @returns <0 if B > A; >0 if A > B; 0 if A == B
*/
int multiaddress_compare_id(const struct MultiAddress* a, const struct MultiAddress* b);
#endif

View file

@ -53,7 +53,7 @@ struct MultiAddress* multiaddress_new_from_bytes(const uint8_t* byteaddress, int
}
out->bsize = size;
memcpy(out->bytes, byteaddress, size);
if(!bytes_to_string(&out->string,byteaddress,size)==1)
if(!bytes_to_string(&out->string,byteaddress,size))
{
multiaddress_free(out);
return NULL;
@ -347,3 +347,51 @@ int multiaddress_decapsulate(struct MultiAddress * result, char * srci)
}
}
/**
* Check to see how these two addresses compare
* @param a side A
* @param b side B
* @returns <0 if B > A; >0 if A > B; 0 if A == B
*/
int multiaddress_compare(const struct MultiAddress* a, const struct MultiAddress* b) {
if (a == NULL && b == NULL)
return 0;
if (a == NULL && b != NULL)
return -1;
if (a != NULL && b == NULL)
return 1;
int total = b->bsize - a->bsize;
if (total != 0)
return total;
for(size_t i = 0; i < b->bsize; i++) {
total = b->bytes[i] - a->bytes[i];
if (total != 0)
return total;
}
return 0;
}
/**
* Check to see how these two addresses compare, ignoring IP address,
* only looking at the first /ipfs/ID hash
* @param a side A
* @param b side B
* @returns <0 if B > A; >0 if A > B; 0 if A == B
*/
int multiaddress_compare_id(const struct MultiAddress* a, const struct MultiAddress* b) {
char* a_id = multiaddress_get_peer_id(a);
char* b_id = multiaddress_get_peer_id(b);
if (a_id == NULL && b_id == NULL)
return 0;
if (a_id == NULL && b_id != NULL)
return -1;
if (a_id != NULL && b_id == NULL)
return 1;
int retVal = strcmp(a_id, b_id);
if (a_id != NULL)
free(a_id);
if (b_id != NULL)
free(b_id);
return retVal;
}

View file

@ -172,7 +172,7 @@ int is_valid_ipv6(char *str)
{
str++;
if(ishexdigit(*str)||*str==0&&hncount<MAX_HEX_NUMBER_COUNT)
if(ishexdigit(*str) || (*str==0 && hncount < MAX_HEX_NUMBER_COUNT ))
{
packed=1;
hncount++;
@ -299,6 +299,7 @@ char * int2ip(int inputintip)
* @param results where to put the resultant string
* @param in_bytes the bytes to unserialize
* @param in_bytes_size the length of the bytes array
* @returns 0 on error, otherwise 1
*/
int bytes_to_string(char** buffer, const uint8_t* in_bytes, int in_bytes_size)
{
@ -314,7 +315,7 @@ int bytes_to_string(char** buffer, const uint8_t* in_bytes, int in_bytes_size)
// set up variables
load_protocols(&head);
memset(hex, 0, (in_bytes_size * 2) + 1);
char* tmp = Var_To_Hex(in_bytes, size);
char* tmp = (char*)Var_To_Hex((char*)in_bytes, size);
memcpy(hex, tmp, in_bytes_size * 2);
free(tmp);
pid[2] = 0;
@ -391,8 +392,8 @@ int bytes_to_string(char** buffer, const uint8_t* in_bytes, int in_bytes_size)
memcpy(IPFS_ADDR, &hex[lastpos], addrsize);
// convert the address from hex values to a binary array
size_t num_bytes = 0;
unsigned char* addrbuf = Hex_To_Var(IPFS_ADDR, &num_bytes);
size_t b58_size = strlen(IPFS_ADDR);
unsigned char* addrbuf = Hex_To_Var((char*)IPFS_ADDR, &num_bytes);
size_t b58_size = strlen((char*)IPFS_ADDR);
unsigned char b58[b58_size];
memset(b58, 0, b58_size);
unsigned char *ptr_b58 = b58;
@ -407,7 +408,7 @@ int bytes_to_string(char** buffer, const uint8_t* in_bytes, int in_bytes_size)
strcat(results, "/");
strcat(results, protocol->name);
strcat(results, "/");
strcat(results, b58);
strcat(results, (char*)b58);
}
}
strcat(results, "/");
@ -637,7 +638,7 @@ int string_to_bytes(uint8_t** finalbytes, size_t* realbbsize, const char* strx,
{
if(strx[0] != '/')
{
printf("Error, must start with '/'\n");
fprintf(stderr, "multiaddr:string_to_bytes: Error, must start with '/' : [%s].\n", strx);
return 0;
}
@ -684,7 +685,7 @@ int string_to_bytes(uint8_t** finalbytes, size_t* realbbsize, const char* strx,
{
char* s_to_b = NULL;
int s_to_b_size = 0;
if(address_string_to_bytes(protx, wp,strlen(wp), &s_to_b, &s_to_b_size) == "ERR")
if( strcmp(address_string_to_bytes(protx, wp,strlen(wp), &s_to_b, &s_to_b_size), "ERR") == 0)
{
malf = 1;
}

View file

@ -3,6 +3,27 @@
#include "multiaddr/multiaddr.h"
#include "multiaddr/varhexutils.h"
int test_new_like_libp2p() {
int retVal = 0;
char* ip = "10.211.55.2";
int port = 4001;
char str[strlen(ip) + 50];
sprintf(str, "/ip4/%s/tcp/%d/", ip, port);
struct MultiAddress* ma_string = multiaddress_new_from_string(str);
// convert to binary
struct MultiAddress* ma_binary = multiaddress_new_from_bytes(ma_string->bytes, ma_string->bsize);
if (strcmp(ma_string->string, ma_binary->string) != 0) {
fprintf(stderr, "%s does not equal %s\n", ma_string->string, ma_binary->string);
goto exit;
}
retVal = 1;
exit:
multiaddress_free(ma_string);
multiaddress_free(ma_binary);
return retVal;
}
int test_new_from_string() {
struct MultiAddress* a = multiaddress_new_from_string("/ip4/127.0.0.1/tcp/8080/");
printf("Number of Bytes: %lu, Bytes: ", a->bsize);
@ -20,7 +41,7 @@ int test_full() {
printf("INITIAL: %s\n",addrstr);
struct MultiAddress* a;
a= multiaddress_new_from_string(addrstr);
unsigned char* tmp = Var_To_Hex(a->bytes, a->bsize);
unsigned char* tmp = Var_To_Hex((char*)a->bytes, a->bsize);
printf("TEST BYTES: %s\n", tmp);
free(tmp);
@ -29,19 +50,19 @@ int test_full() {
printf("A STRING:%s\n",a->string);
multiaddress_encapsulate(a,"/udp/3333/");
printf("A STRING ENCAPSULATED:%s\n",a->string);
tmp = Var_To_Hex(a->bytes, a->bsize);
tmp = Var_To_Hex((char*)a->bytes, a->bsize);
printf("TEST BYTES: %s\n", tmp);
free(tmp);
multiaddress_decapsulate(a,"udp");
printf("A STRING DECAPSULATED UDP:%s\n",a->string);
tmp = Var_To_Hex(a->bytes, a->bsize);
tmp = Var_To_Hex((char*)a->bytes, a->bsize);
printf("TEST BYTES: %s\n", tmp);
free(tmp);
multiaddress_encapsulate(a,"/udp/3333/");
printf("A STRING ENCAPSULATED UDP: %s\n",a->string);
multiaddress_encapsulate(a,"/ipfs/QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG");
printf("A STRING ENCAPSULATED IPFS:%s\n",a->string);
tmp = Var_To_Hex(a->bytes, a->bsize);
tmp = Var_To_Hex((char*)a->bytes, a->bsize);
printf("TEST BYTES: %s\n", tmp);
free(tmp);
printf("TEST BYTE SIZE: %lu\n",a->bsize);
@ -132,7 +153,7 @@ int test_multiaddr_peer_id() {
// switch to bytes and back again to verify the peer id follows...
// 1. display the original bytes
result = Var_To_Hex(addr->bytes, addr->bsize);
result = (char*)Var_To_Hex((char*)addr->bytes, addr->bsize);
fprintf(stderr, "Original Bytes: %s\n", result);
free(result);
result = NULL;
@ -140,13 +161,13 @@ int test_multiaddr_peer_id() {
// make a new MultiAddress from bytes
bytes = malloc(addr->bsize);
memcpy(bytes, addr->bytes, addr->bsize);
addr2 = multiaddress_new_from_bytes(bytes, addr->bsize);
addr2 = multiaddress_new_from_bytes((unsigned char*)bytes, addr->bsize);
free(bytes);
bytes = NULL;
// 2. Display the resultant bytes
result = Var_To_Hex(addr2->bytes, addr2->bsize);
result = (char*)Var_To_Hex((char*)addr2->bytes, addr2->bsize);
fprintf(stderr, "New Bytes: %s\n", result);
free(result);
result = NULL;

View file

@ -10,7 +10,8 @@ const char* names[] = {
"test_multiaddr_utils",
"test_multiaddr_peer_id",
"test_multiaddr_get_peer_id",
"test_multiaddr_bytes"
"test_multiaddr_bytes",
"test_new_like_libp2p"
};
int (*funcs[])(void) = {
@ -21,7 +22,8 @@ int (*funcs[])(void) = {
test_multiaddr_utils,
test_multiaddr_peer_id,
test_multiaddr_get_peer_id,
test_multiaddr_bytes
test_multiaddr_bytes,
test_new_like_libp2p
};
int testit(const char* name, int (*func)(void)) {

View file

@ -51,14 +51,26 @@ uint32_t * Varint_To_Num_32(uint8_t TON32INPUT[60]) //VARINT TO UINT32_t
uvarint_decode32(TON32INPUT, 60, &varintdecode_032);
return &varintdecode_032;
}
//
/**
* Converts a 64 bit integer into a hex string
* @param int2hex the 64 bit integer
* @returns a hex representation as a string (leading zero if necessary)
*/
char * Int_To_Hex(uint64_t int2hex) //VAR[binformat] TO HEX
{
static char int2hex_result[800]="\0";
memset(int2hex_result,0,sizeof(int2hex_result));
sprintf (int2hex_result, "%02lX", int2hex);
return int2hex_result;
static char result[50];
memset(result, 0, 50);
sprintf(result, "%02lX", int2hex);
int slen = strlen(result);
if (slen % 2 != 0) {
for(int i = slen; i >= 0; --i) {
result[i+1] = result[i];
}
result[0] = '0';
}
return result;
}
uint64_t Hex_To_Int(char * hax)
{
char * hex = NULL;
@ -91,7 +103,7 @@ void vthconvert(const unsigned char* in, int in_size, unsigned char** out)
unsigned char *ptr = *out;
for (int i = 0; i < in_size; i++) {
sprintf(&ptr[i * 2], "%02x", in[i]);
sprintf((char*)&ptr[i * 2], "%02x", in[i]);
}
}
@ -170,7 +182,7 @@ void convert2(char * convert_result2, uint8_t * bufhx)
{
uint8_t * buf = NULL;
buf = bufhx;
char conv_proc[3]="\0";
char conv_proc[4]="\0";
conv_proc[3] = '\0';
bzero(conv_proc, 3);
int i;
@ -225,7 +237,7 @@ uint32_t HexVar_To_Num_32(char theHEXstring[]) //HEXIFIED VAR TO UINT32_T
char codo[800] = "\0";
bzero(codo,800);
strcpy(codo, theHEXstring);
char code[3] = "\0";
char code[4] = "\0";
bzero(code,3);
code[3] = '\0';
int x = 0;