Compare commits
No commits in common. "b1f7d607eef3928d7153e87c2b551e0c713588df" and "7c6bd1e36b1e9e210080851b8cc0dd74af340123" have entirely different histories.
b1f7d607ee
...
7c6bd1e36b
8 changed files with 27 additions and 130 deletions
5
LICENSE
5
LICENSE
|
@ -1,9 +1,6 @@
|
||||||
MIT License
|
MIT License
|
||||||
|
|
||||||
Copyright (c) 2019 AGORISE, LTD.
|
Copyright (c) 2017 BitShares Munich IVS
|
||||||
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
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
* @param binary_data_size the size of the results buffer
|
* @param binary_data_size the size of the results buffer
|
||||||
* @returns true(1) on success
|
* @returns true(1) on success
|
||||||
*/
|
*/
|
||||||
int multiaddr_encoding_base58_decode(const char* base58, size_t base58_size, unsigned char** binary_data, size_t *binary_data_size);
|
int multiaddr_encoding_base58_decode(const unsigned char* base58, size_t base58_size, unsigned char** binary_data, size_t *binary_data_size);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* encode an array of bytes into a base58 string
|
* encode an array of bytes into a base58 string
|
||||||
|
|
|
@ -87,20 +87,4 @@ int multiaddress_get_ip_port(const struct MultiAddress* in);
|
||||||
*/
|
*/
|
||||||
char* multiaddress_get_peer_id(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
|
#endif
|
||||||
|
|
50
multiaddr.c
50
multiaddr.c
|
@ -53,7 +53,7 @@ struct MultiAddress* multiaddress_new_from_bytes(const uint8_t* byteaddress, int
|
||||||
}
|
}
|
||||||
out->bsize = size;
|
out->bsize = size;
|
||||||
memcpy(out->bytes, byteaddress, size);
|
memcpy(out->bytes, byteaddress, size);
|
||||||
if(!bytes_to_string(&out->string,byteaddress,size))
|
if(!bytes_to_string(&out->string,byteaddress,size)==1)
|
||||||
{
|
{
|
||||||
multiaddress_free(out);
|
multiaddress_free(out);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -347,51 +347,3 @@ 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
15
protoutils.c
15
protoutils.c
|
@ -172,7 +172,7 @@ int is_valid_ipv6(char *str)
|
||||||
{
|
{
|
||||||
str++;
|
str++;
|
||||||
|
|
||||||
if(ishexdigit(*str) || (*str==0 && hncount < MAX_HEX_NUMBER_COUNT ))
|
if(ishexdigit(*str)||*str==0&&hncount<MAX_HEX_NUMBER_COUNT)
|
||||||
{
|
{
|
||||||
packed=1;
|
packed=1;
|
||||||
hncount++;
|
hncount++;
|
||||||
|
@ -299,7 +299,6 @@ char * int2ip(int inputintip)
|
||||||
* @param results where to put the resultant string
|
* @param results where to put the resultant string
|
||||||
* @param in_bytes the bytes to unserialize
|
* @param in_bytes the bytes to unserialize
|
||||||
* @param in_bytes_size the length of the bytes array
|
* @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)
|
int bytes_to_string(char** buffer, const uint8_t* in_bytes, int in_bytes_size)
|
||||||
{
|
{
|
||||||
|
@ -315,7 +314,7 @@ int bytes_to_string(char** buffer, const uint8_t* in_bytes, int in_bytes_size)
|
||||||
// set up variables
|
// set up variables
|
||||||
load_protocols(&head);
|
load_protocols(&head);
|
||||||
memset(hex, 0, (in_bytes_size * 2) + 1);
|
memset(hex, 0, (in_bytes_size * 2) + 1);
|
||||||
char* tmp = (char*)Var_To_Hex((char*)in_bytes, size);
|
char* tmp = Var_To_Hex(in_bytes, size);
|
||||||
memcpy(hex, tmp, in_bytes_size * 2);
|
memcpy(hex, tmp, in_bytes_size * 2);
|
||||||
free(tmp);
|
free(tmp);
|
||||||
pid[2] = 0;
|
pid[2] = 0;
|
||||||
|
@ -392,8 +391,8 @@ int bytes_to_string(char** buffer, const uint8_t* in_bytes, int in_bytes_size)
|
||||||
memcpy(IPFS_ADDR, &hex[lastpos], addrsize);
|
memcpy(IPFS_ADDR, &hex[lastpos], addrsize);
|
||||||
// convert the address from hex values to a binary array
|
// convert the address from hex values to a binary array
|
||||||
size_t num_bytes = 0;
|
size_t num_bytes = 0;
|
||||||
unsigned char* addrbuf = Hex_To_Var((char*)IPFS_ADDR, &num_bytes);
|
unsigned char* addrbuf = Hex_To_Var(IPFS_ADDR, &num_bytes);
|
||||||
size_t b58_size = strlen((char*)IPFS_ADDR);
|
size_t b58_size = strlen(IPFS_ADDR);
|
||||||
unsigned char b58[b58_size];
|
unsigned char b58[b58_size];
|
||||||
memset(b58, 0, b58_size);
|
memset(b58, 0, b58_size);
|
||||||
unsigned char *ptr_b58 = b58;
|
unsigned char *ptr_b58 = b58;
|
||||||
|
@ -408,7 +407,7 @@ int bytes_to_string(char** buffer, const uint8_t* in_bytes, int in_bytes_size)
|
||||||
strcat(results, "/");
|
strcat(results, "/");
|
||||||
strcat(results, protocol->name);
|
strcat(results, protocol->name);
|
||||||
strcat(results, "/");
|
strcat(results, "/");
|
||||||
strcat(results, (char*)b58);
|
strcat(results, b58);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
strcat(results, "/");
|
strcat(results, "/");
|
||||||
|
@ -638,7 +637,7 @@ int string_to_bytes(uint8_t** finalbytes, size_t* realbbsize, const char* strx,
|
||||||
{
|
{
|
||||||
if(strx[0] != '/')
|
if(strx[0] != '/')
|
||||||
{
|
{
|
||||||
fprintf(stderr, "multiaddr:string_to_bytes: Error, must start with '/' : [%s].\n", strx);
|
printf("Error, must start with '/'\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -685,7 +684,7 @@ int string_to_bytes(uint8_t** finalbytes, size_t* realbbsize, const char* strx,
|
||||||
{
|
{
|
||||||
char* s_to_b = NULL;
|
char* s_to_b = NULL;
|
||||||
int s_to_b_size = 0;
|
int s_to_b_size = 0;
|
||||||
if( strcmp(address_string_to_bytes(protx, wp,strlen(wp), &s_to_b, &s_to_b_size), "ERR") == 0)
|
if(address_string_to_bytes(protx, wp,strlen(wp), &s_to_b, &s_to_b_size) == "ERR")
|
||||||
{
|
{
|
||||||
malf = 1;
|
malf = 1;
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,27 +3,6 @@
|
||||||
#include "multiaddr/multiaddr.h"
|
#include "multiaddr/multiaddr.h"
|
||||||
#include "multiaddr/varhexutils.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() {
|
int test_new_from_string() {
|
||||||
struct MultiAddress* a = multiaddress_new_from_string("/ip4/127.0.0.1/tcp/8080/");
|
struct MultiAddress* a = multiaddress_new_from_string("/ip4/127.0.0.1/tcp/8080/");
|
||||||
printf("Number of Bytes: %lu, Bytes: ", a->bsize);
|
printf("Number of Bytes: %lu, Bytes: ", a->bsize);
|
||||||
|
@ -41,7 +20,7 @@ int test_full() {
|
||||||
printf("INITIAL: %s\n",addrstr);
|
printf("INITIAL: %s\n",addrstr);
|
||||||
struct MultiAddress* a;
|
struct MultiAddress* a;
|
||||||
a= multiaddress_new_from_string(addrstr);
|
a= multiaddress_new_from_string(addrstr);
|
||||||
unsigned char* tmp = Var_To_Hex((char*)a->bytes, a->bsize);
|
unsigned char* tmp = Var_To_Hex(a->bytes, a->bsize);
|
||||||
printf("TEST BYTES: %s\n", tmp);
|
printf("TEST BYTES: %s\n", tmp);
|
||||||
free(tmp);
|
free(tmp);
|
||||||
|
|
||||||
|
@ -50,19 +29,19 @@ int test_full() {
|
||||||
printf("A STRING:%s\n",a->string);
|
printf("A STRING:%s\n",a->string);
|
||||||
multiaddress_encapsulate(a,"/udp/3333/");
|
multiaddress_encapsulate(a,"/udp/3333/");
|
||||||
printf("A STRING ENCAPSULATED:%s\n",a->string);
|
printf("A STRING ENCAPSULATED:%s\n",a->string);
|
||||||
tmp = Var_To_Hex((char*)a->bytes, a->bsize);
|
tmp = Var_To_Hex(a->bytes, a->bsize);
|
||||||
printf("TEST BYTES: %s\n", tmp);
|
printf("TEST BYTES: %s\n", tmp);
|
||||||
free(tmp);
|
free(tmp);
|
||||||
multiaddress_decapsulate(a,"udp");
|
multiaddress_decapsulate(a,"udp");
|
||||||
printf("A STRING DECAPSULATED UDP:%s\n",a->string);
|
printf("A STRING DECAPSULATED UDP:%s\n",a->string);
|
||||||
tmp = Var_To_Hex((char*)a->bytes, a->bsize);
|
tmp = Var_To_Hex(a->bytes, a->bsize);
|
||||||
printf("TEST BYTES: %s\n", tmp);
|
printf("TEST BYTES: %s\n", tmp);
|
||||||
free(tmp);
|
free(tmp);
|
||||||
multiaddress_encapsulate(a,"/udp/3333/");
|
multiaddress_encapsulate(a,"/udp/3333/");
|
||||||
printf("A STRING ENCAPSULATED UDP: %s\n",a->string);
|
printf("A STRING ENCAPSULATED UDP: %s\n",a->string);
|
||||||
multiaddress_encapsulate(a,"/ipfs/QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG");
|
multiaddress_encapsulate(a,"/ipfs/QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG");
|
||||||
printf("A STRING ENCAPSULATED IPFS:%s\n",a->string);
|
printf("A STRING ENCAPSULATED IPFS:%s\n",a->string);
|
||||||
tmp = Var_To_Hex((char*)a->bytes, a->bsize);
|
tmp = Var_To_Hex(a->bytes, a->bsize);
|
||||||
printf("TEST BYTES: %s\n", tmp);
|
printf("TEST BYTES: %s\n", tmp);
|
||||||
free(tmp);
|
free(tmp);
|
||||||
printf("TEST BYTE SIZE: %lu\n",a->bsize);
|
printf("TEST BYTE SIZE: %lu\n",a->bsize);
|
||||||
|
@ -153,7 +132,7 @@ int test_multiaddr_peer_id() {
|
||||||
// switch to bytes and back again to verify the peer id follows...
|
// switch to bytes and back again to verify the peer id follows...
|
||||||
|
|
||||||
// 1. display the original bytes
|
// 1. display the original bytes
|
||||||
result = (char*)Var_To_Hex((char*)addr->bytes, addr->bsize);
|
result = Var_To_Hex(addr->bytes, addr->bsize);
|
||||||
fprintf(stderr, "Original Bytes: %s\n", result);
|
fprintf(stderr, "Original Bytes: %s\n", result);
|
||||||
free(result);
|
free(result);
|
||||||
result = NULL;
|
result = NULL;
|
||||||
|
@ -161,13 +140,13 @@ int test_multiaddr_peer_id() {
|
||||||
// make a new MultiAddress from bytes
|
// make a new MultiAddress from bytes
|
||||||
bytes = malloc(addr->bsize);
|
bytes = malloc(addr->bsize);
|
||||||
memcpy(bytes, addr->bytes, addr->bsize);
|
memcpy(bytes, addr->bytes, addr->bsize);
|
||||||
addr2 = multiaddress_new_from_bytes((unsigned char*)bytes, addr->bsize);
|
addr2 = multiaddress_new_from_bytes(bytes, addr->bsize);
|
||||||
|
|
||||||
free(bytes);
|
free(bytes);
|
||||||
bytes = NULL;
|
bytes = NULL;
|
||||||
|
|
||||||
// 2. Display the resultant bytes
|
// 2. Display the resultant bytes
|
||||||
result = (char*)Var_To_Hex((char*)addr2->bytes, addr2->bsize);
|
result = Var_To_Hex(addr2->bytes, addr2->bsize);
|
||||||
fprintf(stderr, "New Bytes: %s\n", result);
|
fprintf(stderr, "New Bytes: %s\n", result);
|
||||||
free(result);
|
free(result);
|
||||||
result = NULL;
|
result = NULL;
|
||||||
|
|
|
@ -10,8 +10,7 @@ const char* names[] = {
|
||||||
"test_multiaddr_utils",
|
"test_multiaddr_utils",
|
||||||
"test_multiaddr_peer_id",
|
"test_multiaddr_peer_id",
|
||||||
"test_multiaddr_get_peer_id",
|
"test_multiaddr_get_peer_id",
|
||||||
"test_multiaddr_bytes",
|
"test_multiaddr_bytes"
|
||||||
"test_new_like_libp2p"
|
|
||||||
};
|
};
|
||||||
|
|
||||||
int (*funcs[])(void) = {
|
int (*funcs[])(void) = {
|
||||||
|
@ -22,8 +21,7 @@ int (*funcs[])(void) = {
|
||||||
test_multiaddr_utils,
|
test_multiaddr_utils,
|
||||||
test_multiaddr_peer_id,
|
test_multiaddr_peer_id,
|
||||||
test_multiaddr_get_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)) {
|
int testit(const char* name, int (*func)(void)) {
|
||||||
|
|
|
@ -51,26 +51,14 @@ uint32_t * Varint_To_Num_32(uint8_t TON32INPUT[60]) //VARINT TO UINT32_t
|
||||||
uvarint_decode32(TON32INPUT, 60, &varintdecode_032);
|
uvarint_decode32(TON32INPUT, 60, &varintdecode_032);
|
||||||
return &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
|
char * Int_To_Hex(uint64_t int2hex) //VAR[binformat] TO HEX
|
||||||
{
|
{
|
||||||
static char result[50];
|
static char int2hex_result[800]="\0";
|
||||||
memset(result, 0, 50);
|
memset(int2hex_result,0,sizeof(int2hex_result));
|
||||||
sprintf(result, "%02lX", int2hex);
|
sprintf (int2hex_result, "%02lX", int2hex);
|
||||||
int slen = strlen(result);
|
return int2hex_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)
|
uint64_t Hex_To_Int(char * hax)
|
||||||
{
|
{
|
||||||
char * hex = NULL;
|
char * hex = NULL;
|
||||||
|
@ -103,7 +91,7 @@ void vthconvert(const unsigned char* in, int in_size, unsigned char** out)
|
||||||
unsigned char *ptr = *out;
|
unsigned char *ptr = *out;
|
||||||
|
|
||||||
for (int i = 0; i < in_size; i++) {
|
for (int i = 0; i < in_size; i++) {
|
||||||
sprintf((char*)&ptr[i * 2], "%02x", in[i]);
|
sprintf(&ptr[i * 2], "%02x", in[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -182,7 +170,7 @@ void convert2(char * convert_result2, uint8_t * bufhx)
|
||||||
{
|
{
|
||||||
uint8_t * buf = NULL;
|
uint8_t * buf = NULL;
|
||||||
buf = bufhx;
|
buf = bufhx;
|
||||||
char conv_proc[4]="\0";
|
char conv_proc[3]="\0";
|
||||||
conv_proc[3] = '\0';
|
conv_proc[3] = '\0';
|
||||||
bzero(conv_proc, 3);
|
bzero(conv_proc, 3);
|
||||||
int i;
|
int i;
|
||||||
|
@ -237,7 +225,7 @@ uint32_t HexVar_To_Num_32(char theHEXstring[]) //HEXIFIED VAR TO UINT32_T
|
||||||
char codo[800] = "\0";
|
char codo[800] = "\0";
|
||||||
bzero(codo,800);
|
bzero(codo,800);
|
||||||
strcpy(codo, theHEXstring);
|
strcpy(codo, theHEXstring);
|
||||||
char code[4] = "\0";
|
char code[3] = "\0";
|
||||||
bzero(code,3);
|
bzero(code,3);
|
||||||
code[3] = '\0';
|
code[3] = '\0';
|
||||||
int x = 0;
|
int x = 0;
|
||||||
|
|
Loading…
Reference in a new issue