Compare commits
10 commits
7c6bd1e36b
...
b1f7d607ee
Author | SHA1 | Date | |
---|---|---|---|
|
b1f7d607ee | ||
|
a271e08996 | ||
|
028223e46a | ||
|
4d46d86514 | ||
|
4c457c1dd3 | ||
|
3b178a142d | ||
|
c2f2c0c2da | ||
|
c434fbd160 | ||
|
86d3cd422e | ||
|
58a6240af9 |
8 changed files with 130 additions and 27 deletions
5
LICENSE
5
LICENSE
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
50
multiaddr.c
50
multiaddr.c
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
15
protoutils.c
15
protoutils.c
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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)) {
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue