More in line with existing code. Refactored naming.
This commit is contained in:
parent
78ce39b067
commit
bb488f7e82
7 changed files with 140 additions and 74 deletions
|
@ -7,24 +7,48 @@
|
|||
#include "protocols.h"
|
||||
#include "protoutils.h"
|
||||
|
||||
/**
|
||||
* Normally, addresses have been represented using string addresses, like:
|
||||
|
||||
tcp4://127.0.0.1:1234
|
||||
udp4://10.20.30.40:5060
|
||||
ws://1.2.3.4:5678
|
||||
tcp6://[1fff:0:a88:85a3::ac1f]:8001
|
||||
This isn't optimal. Instead, addresses should be formatted so:
|
||||
|
||||
Binary format:
|
||||
|
||||
(varint proto><n byte addr>)+
|
||||
<1 byte ipv4 code><4 byte ipv4 addr><1 byte udp code><2 byte udp port>
|
||||
<1 byte ipv6 code><16 byte ipv6 addr><1 byte tcp code><2 byte tcp port>
|
||||
|
||||
String format:
|
||||
|
||||
(/<addr str code>/<addr str rep>)+
|
||||
/ip4/<ipv4 str addr>/udp/<udp int port>
|
||||
/ip6/<ipv6 str addr>/tcp/<tcp int port>
|
||||
*/
|
||||
|
||||
struct MultiAddress
|
||||
{
|
||||
char string[800];
|
||||
uint8_t bytes[400];
|
||||
int bsize[1];
|
||||
// A MultiAddress represented as a string
|
||||
char* string;
|
||||
// A MultiAddress represented as an array of bytes
|
||||
uint8_t* bytes;
|
||||
size_t bsize;
|
||||
};
|
||||
|
||||
|
||||
int strpos(char *haystack, char *needle);
|
||||
|
||||
struct MultiAddress* new_maddr_fb(uint8_t * byteaddress,int size); //Construct new address from bytes
|
||||
struct MultiAddress* multiaddress_new_from_bytes(const uint8_t* byteaddress, int size); //Construct new address from bytes
|
||||
|
||||
struct MultiAddress* new_maddr_fs(char * straddress); //Construct new address from string
|
||||
struct MultiAddress* multiaddress_new_from_string(const char* straddress); //Construct new address from string
|
||||
|
||||
void maddr_free(struct MultiAddress* in);
|
||||
void multiaddress_free(struct MultiAddress* in);
|
||||
|
||||
int m_encapsulate(struct MultiAddress * result, char * string);
|
||||
int multiaddress_encapsulate(struct MultiAddress * result, char * string);
|
||||
|
||||
int m_decapsulate(struct MultiAddress * result, char * srci);
|
||||
int multiaddress_decapsulate(struct MultiAddress * result, char * srci);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -27,15 +27,15 @@ int ishexdigit(char ch);
|
|||
|
||||
int is_valid_ipv6(char *str);
|
||||
|
||||
uint64_t ip2int(char * ipconvertint);
|
||||
uint64_t ip2int(const char * ipconvertint);
|
||||
|
||||
char * int2ip(int inputintip);
|
||||
|
||||
//I didn't feel another address_bytes_to_string was necesarry sry guys
|
||||
int bytes_to_string(char * resultzx, uint8_t * catx,int xbsize);
|
||||
int bytes_to_string(char * resultzx, const uint8_t * catx,int xbsize);
|
||||
|
||||
char * address_string_to_bytes(struct protocol * xx, char * abc,size_t getsznow);
|
||||
char * address_string_to_bytes(struct protocol * xx, const char * abc, size_t getsznow);
|
||||
|
||||
int string_to_bytes(uint8_t * finalbytes,int * realbbsize,char * strx, size_t strsize);
|
||||
int string_to_bytes(uint8_t * finalbytes,size_t* realbbsize,char * strx, size_t strsize);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -31,7 +31,7 @@ uint64_t Hex_To_Int(char * hax);
|
|||
//
|
||||
void vthconvert(int size, char * crrz01, uint8_t * xbuf);
|
||||
|
||||
char * Var_To_Hex(int realsize, uint8_t * TOHEXINPUT); //VAR[binformat] TO HEX
|
||||
char * Var_To_Hex(int realsize, const uint8_t * TOHEXINPUT); //VAR[binformat] TO HEX
|
||||
|
||||
uint8_t * Hex_To_Var(char * Hexstr); //HEX TO VAR[BINFORMAT]
|
||||
|
||||
|
|
130
multiaddr.c
130
multiaddr.c
|
@ -18,71 +18,114 @@ int strpos(char *haystack, char *needle)
|
|||
}
|
||||
}
|
||||
|
||||
struct MultiAddress* new_maddr_fb(uint8_t* byteaddress,int size)//Construct new address from bytes
|
||||
/**
|
||||
* Construct a new MultiAddress struct
|
||||
* @returns an empty MultiAddress struct
|
||||
*/
|
||||
struct MultiAddress* multiaddress_new() {
|
||||
struct MultiAddress* out = (struct MultiAddress*)malloc(sizeof(struct MultiAddress));
|
||||
if (out != NULL) {
|
||||
out->bsize = 0;
|
||||
out->bytes = NULL;
|
||||
out->string = NULL;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* construct a new MultiAddress from bytes
|
||||
* @param byteaddress the byte array
|
||||
* @param size the size of the byte array
|
||||
* @returns a new MultiAddress struct filled in, or NULL on error
|
||||
*/
|
||||
struct MultiAddress* multiaddress_new_from_bytes(const uint8_t* byteaddress, int size)//Construct new address from bytes
|
||||
{
|
||||
struct MultiAddress* anewaddr2 = (struct MultiAddress*)malloc(sizeof(struct MultiAddress));
|
||||
if (anewaddr2 != NULL) {
|
||||
struct MultiAddress* out = multiaddress_new();
|
||||
if (out != NULL) {
|
||||
if(byteaddress!=NULL)
|
||||
{
|
||||
memcpy(anewaddr2->bytes, byteaddress, size);
|
||||
if(bytes_to_string(anewaddr2->string,byteaddress,size)==1)
|
||||
out->bytes = malloc(size);
|
||||
if (out->bytes == NULL) {
|
||||
multiaddress_free(out);
|
||||
return NULL;
|
||||
}
|
||||
memcpy(out->bytes, byteaddress, size);
|
||||
if(!bytes_to_string(out->string,byteaddress,size)==1)
|
||||
{
|
||||
return anewaddr2;
|
||||
multiaddress_free(out);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
return anewaddr2;
|
||||
return out;
|
||||
}
|
||||
struct MultiAddress* new_maddr_fs(char * straddress)//Construct new address from string
|
||||
|
||||
struct MultiAddress* multiaddress_new_from_string(const char* straddress)//Construct new address from string
|
||||
{
|
||||
struct MultiAddress* anewaddr = (struct MultiAddress*)malloc(sizeof(struct MultiAddress));
|
||||
if (anewaddr != NULL) {
|
||||
bzero(anewaddr->string, 800);
|
||||
strcpy(anewaddr->string, straddress);
|
||||
anewaddr->bsize[0] = 0;
|
||||
if(string_to_bytes(anewaddr->bytes,anewaddr->bsize,anewaddr->string,sizeof(anewaddr->string))==1)
|
||||
struct MultiAddress* out = multiaddress_new();
|
||||
if (out != NULL) {
|
||||
out->string = malloc(sizeof(straddress) + 1);
|
||||
if (out->string == NULL) {
|
||||
multiaddress_free(out);
|
||||
return NULL;
|
||||
}
|
||||
strcpy(out->string, straddress);
|
||||
|
||||
if (string_to_bytes(out->bytes, &out->bsize, out->string, sizeof(out->string)) == 0 )
|
||||
{
|
||||
int betta;
|
||||
//printf("BSIZE: %u\n", anewaddr.bsize[0]);
|
||||
for(betta=anewaddr->bsize[0];betta<400;betta++)
|
||||
{
|
||||
anewaddr->bytes[betta] = '\0';
|
||||
}
|
||||
return anewaddr;
|
||||
multiaddress_free(out);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
return anewaddr;
|
||||
return out;
|
||||
}
|
||||
|
||||
void maddr_free(struct MultiAddress* in) {
|
||||
free(in);
|
||||
void multiaddress_free(struct MultiAddress* in) {
|
||||
if (in != NULL) {
|
||||
if (in->bytes != NULL)
|
||||
free(in->bytes);
|
||||
if (in->string != NULL)
|
||||
free(in->string);
|
||||
free(in);
|
||||
in = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
int m_encapsulate(struct MultiAddress * result, char * string)
|
||||
/**
|
||||
* Put a string into the MultiAddress and recalculate the bytes
|
||||
* @param result the struct
|
||||
* @param string the new string
|
||||
*/
|
||||
int multiaddress_encapsulate(struct MultiAddress* result, char* string)
|
||||
{
|
||||
if(result!=NULL&&string!=NULL)
|
||||
if(result != NULL && string != NULL)
|
||||
{
|
||||
int success = 0;
|
||||
char pstr[800];
|
||||
bzero(pstr,800);
|
||||
strcpy(pstr,result->string);
|
||||
strcat(pstr,string+1);
|
||||
if(string_to_bytes(result->bytes,result->bsize,pstr,sizeof(pstr)))
|
||||
{
|
||||
strcpy(result->string,pstr);
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
// remove the old values
|
||||
if (result->bytes != NULL)
|
||||
free(result->bytes);
|
||||
result->bytes = NULL;
|
||||
result->bsize = 0;
|
||||
free(result->string);
|
||||
// insert the new values
|
||||
result->string = malloc(strlen(string) + 1);
|
||||
if (result->string == NULL) {
|
||||
multiaddress_free(result);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
strcpy(result->string, string);
|
||||
if(string_to_bytes(result->bytes, &result->bsize, result->string, sizeof(result->string)) == 0)
|
||||
{
|
||||
multiaddress_free(result);
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
int m_decapsulate(struct MultiAddress * result, char * srci)
|
||||
|
||||
// not sure what this does
|
||||
int multiaddress_decapsulate(struct MultiAddress * result, char * srci)
|
||||
{
|
||||
if(result!=NULL && srci!=NULL)
|
||||
{
|
||||
|
@ -92,6 +135,7 @@ int m_decapsulate(struct MultiAddress * result, char * srci)
|
|||
int sz=strlen(procstr);
|
||||
char * src = NULL;
|
||||
src=srci;
|
||||
// change slash to space
|
||||
for(i=0;i<sz;i++)
|
||||
{
|
||||
if(procstr[i] == '/')
|
||||
|
@ -103,10 +147,12 @@ int m_decapsulate(struct MultiAddress * result, char * srci)
|
|||
pos=strpos(procstr,src);
|
||||
if(pos!=-1)
|
||||
{
|
||||
// fill rest with 0s
|
||||
for(i=pos;i<sz;i++)
|
||||
{
|
||||
procstr[i] = '\0';
|
||||
}
|
||||
// replace space with slash
|
||||
for(i=0;i<sz;i++)
|
||||
{
|
||||
if(procstr[i] == ' ')
|
||||
|
|
10
protoutils.c
10
protoutils.c
|
@ -232,7 +232,7 @@ int is_valid_ipv6(char *str)
|
|||
|
||||
return(err==0);
|
||||
}
|
||||
uint64_t ip2int(char * ipconvertint)
|
||||
uint64_t ip2int(const char * ipconvertint)
|
||||
{
|
||||
uint64_t final_result =0;
|
||||
char * iproc;
|
||||
|
@ -291,7 +291,7 @@ char * int2ip(int inputintip)
|
|||
return xxx_int2ip_result;
|
||||
}
|
||||
//I didn't feel another address_bytes_to_string was necesarry sry guys
|
||||
int bytes_to_string(char * resultzx, uint8_t * catx,int xbsize)
|
||||
int bytes_to_string(char * resultzx, const uint8_t* catx,int xbsize)
|
||||
{
|
||||
bzero(resultzx,800);
|
||||
uint8_t * bytes = NULL;
|
||||
|
@ -430,7 +430,7 @@ int bytes_to_string(char * resultzx, uint8_t * catx,int xbsize)
|
|||
}
|
||||
//
|
||||
|
||||
char * address_string_to_bytes(struct protocol * xx, char * abc,size_t getsznow)
|
||||
char * address_string_to_bytes(struct protocol * xx, const char * abc,size_t getsznow)
|
||||
{
|
||||
static char astb__stringy[800] = "\0";
|
||||
bzero(astb__stringy,800);
|
||||
|
@ -562,7 +562,7 @@ char * address_string_to_bytes(struct protocol * xx, char * abc,size_t getsznow)
|
|||
|
||||
|
||||
char * x_data = NULL;
|
||||
x_data = abc;
|
||||
x_data = (char*)abc;
|
||||
size_t x_data_length = strlen(x_data);
|
||||
size_t result_buffer_length = multiaddr_encoding_base58_decode_size((unsigned char*)x_data);
|
||||
unsigned char result_buffer[result_buffer_length];
|
||||
|
@ -636,7 +636,7 @@ char * address_string_to_bytes(struct protocol * xx, char * abc,size_t getsznow)
|
|||
}
|
||||
}
|
||||
}
|
||||
int string_to_bytes(uint8_t * finalbytes,int * realbbsize,char * strx, size_t strsize)
|
||||
int string_to_bytes(uint8_t * finalbytes, size_t* realbbsize, char * strx, size_t strsize)
|
||||
{
|
||||
if(strx[0] != '/')
|
||||
{
|
||||
|
|
16
testing.c
16
testing.c
|
@ -8,25 +8,25 @@ int main()
|
|||
strcat(addrstr,"/ip4/192.168.1.1/tcp/8080/");
|
||||
printf("INITIAL: %s\n",addrstr);
|
||||
struct MultiAddress* a;
|
||||
a=new_maddr_fs(addrstr);
|
||||
printf("TEST BYTES: %s\n",Var_To_Hex(a->bsize[0], a->bytes));
|
||||
a= multiaddress_new_from_string(addrstr);
|
||||
printf("TEST BYTES: %s\n",Var_To_Hex(a->bsize, a->bytes));
|
||||
|
||||
//Remember, Decapsulation happens from right to left, never in reverse!
|
||||
|
||||
printf("A STRING:%s\n",a->string);
|
||||
m_encapsulate(a,"/ip4/192.31.200.1/udp/3333/");
|
||||
multiaddress_encapsulate(a,"/ip4/192.31.200.1/udp/3333/");
|
||||
printf("A STRING ENCAPSULATED:%s\n",a->string);
|
||||
|
||||
m_decapsulate(a,"udp");
|
||||
multiaddress_decapsulate(a,"udp");
|
||||
printf("A STRING DECAPSULATED UDP:%s\n",a->string);
|
||||
|
||||
m_encapsulate(a,"/tcp/8080");
|
||||
multiaddress_encapsulate(a,"/tcp/8080");
|
||||
printf("A STRING ENCAPSULATED TCP:%s\n",a->string);
|
||||
|
||||
struct MultiAddress* beta;
|
||||
beta=new_maddr_fb(a->bytes,a->bsize[0]);
|
||||
beta=multiaddress_new_from_bytes(a->bytes,a->bsize);
|
||||
printf("B STRING: %s\n",beta->string);
|
||||
|
||||
maddr_free(a);
|
||||
maddr_free(beta);
|
||||
multiaddress_free(a);
|
||||
multiaddress_free(beta);
|
||||
}
|
||||
|
|
|
@ -77,7 +77,7 @@ uint64_t Hex_To_Int(char * hax)
|
|||
return val;
|
||||
}
|
||||
//
|
||||
void vthconvert(int size, char * crrz01, uint8_t * xbuf)
|
||||
void vthconvert(int size, char * crrz01, const uint8_t * xbuf)
|
||||
{
|
||||
uint8_t buf[400];
|
||||
bzero(buf,400);
|
||||
|
@ -106,12 +106,8 @@ void vthconvert(int size, char * crrz01, uint8_t * xbuf)
|
|||
crrz1 = NULL;
|
||||
}
|
||||
}
|
||||
char * Var_To_Hex(int realsize, uint8_t * TOHEXINPUT) //VAR[binformat] TO HEX
|
||||
char * Var_To_Hex(int realsize, const uint8_t * TOHEXINPUT) //VAR[binformat] TO HEX
|
||||
{
|
||||
for(int ix=realsize;ix<400;ix++)
|
||||
{
|
||||
TOHEXINPUT[ix] = '\0';
|
||||
}
|
||||
if(TOHEXINPUT != NULL)
|
||||
{
|
||||
static char convert_resultz1[800]="\0";
|
||||
|
|
Loading…
Reference in a new issue