From bb488f7e820d650c1606aaccbe5e92eb02bec32b Mon Sep 17 00:00:00 2001 From: John Jones Date: Mon, 13 Feb 2017 14:12:32 -0500 Subject: [PATCH] More in line with existing code. Refactored naming. --- include/multiaddr/multiaddr.h | 40 ++++++++-- include/multiaddr/protoutils.h | 8 +- include/multiaddr/varhexutils.h | 2 +- multiaddr.c | 130 +++++++++++++++++++++----------- protoutils.c | 10 +-- testing.c | 16 ++-- varhexutils.c | 8 +- 7 files changed, 140 insertions(+), 74 deletions(-) diff --git a/include/multiaddr/multiaddr.h b/include/multiaddr/multiaddr.h index 72c5659..9c00544 100644 --- a/include/multiaddr/multiaddr.h +++ b/include/multiaddr/multiaddr.h @@ -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>)+ + <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: + + (//)+ + /ip4//udp/ + /ip6//tcp/ + */ + 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 diff --git a/include/multiaddr/protoutils.h b/include/multiaddr/protoutils.h index f374c10..b0bd860 100644 --- a/include/multiaddr/protoutils.h +++ b/include/multiaddr/protoutils.h @@ -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 diff --git a/include/multiaddr/varhexutils.h b/include/multiaddr/varhexutils.h index da1b2b9..01695cc 100644 --- a/include/multiaddr/varhexutils.h +++ b/include/multiaddr/varhexutils.h @@ -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] diff --git a/multiaddr.c b/multiaddr.c index 4d27f17..bf861a6 100644 --- a/multiaddr.c +++ b/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;ibsize[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); } diff --git a/varhexutils.c b/varhexutils.c index 68db6ed..de0d741 100644 --- a/varhexutils.c +++ b/varhexutils.c @@ -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";