From d57e026fbf889e436a9b6f9bf6844725d0e642eb Mon Sep 17 00:00:00 2001 From: John Jones Date: Mon, 13 Feb 2017 13:25:43 -0500 Subject: [PATCH 1/5] Moved functionality from headers to c files so that we can compile --- Makefile | 2 +- base58.c | 8 +- include/multiaddr/base58.h | 10 +- include/multiaddr/multiaddr.h | 130 +----- include/multiaddr/protocols.h | 205 +-------- include/multiaddr/protoutils.h | 729 +------------------------------ include/multiaddr/varhexutils.h | 270 ++---------- multiaddr.c | 130 ++++++ protocols.c | 246 +++++++++-- protoutils.c | 736 ++++++++++++++++++++++++++++++++ testing.c | 27 +- varhexutils.c | 252 +++++++++++ 12 files changed, 1426 insertions(+), 1319 deletions(-) create mode 100644 multiaddr.c create mode 100644 protoutils.c create mode 100644 varhexutils.c diff --git a/Makefile b/Makefile index 1677f21..85f726c 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ LFLAGS = -lm DEPS = include/multiaddr/base58.h include/multiaddr/endian.h include/multiaddr/multiaddr.h \ include/multiaddr/protocols.h include/multiaddr/protoutils.h include/multiaddr/varhexutils.h \ include/multiaddr/varint.h -OBJS = base58.o varint.o +OBJS = base58.o varint.o varhexutils.o protoutils.o protocols.o multiaddr.o %.o: %.c $(DEPS) $(CC) -c -o $@ $< $(CFLAGS) diff --git a/base58.c b/base58.c index 1b17041..b5baf39 100644 --- a/base58.c +++ b/base58.c @@ -31,7 +31,7 @@ static const int8_t b58digits_map[] = { * @param binszp the size of the results buffer * @returns true(1) on success */ -int libp2p_crypto_encoding_base58_decode(const char* b58, size_t base58_size, unsigned char** bin, size_t* binszp) +int multiaddr_encoding_base58_decode(const char* b58, size_t base58_size, unsigned char** bin, size_t* binszp) { size_t binsz = *binszp; const unsigned char* b58u = (const void*)b58; @@ -125,7 +125,7 @@ int libp2p_crypto_encoding_base58_decode(const char* b58, size_t base58_size, un * @returns true(1) on success */ //int libp2p_crypto_encoding_base58_encode(const unsigned char* binary_data, size_t binary_data_size, unsigned char* base58, size_t* base58_size) -int libp2p_crypto_encoding_base58_encode(const unsigned char* data, size_t binsz, unsigned char** b58, size_t* b58sz) +int multiaddr_encoding_base58_encode(const unsigned char* data, size_t binsz, unsigned char** b58, size_t* b58sz) { const uint8_t* bin = data; int carry; @@ -175,7 +175,7 @@ int libp2p_crypto_encoding_base58_encode(const unsigned char* data, size_t binsz * @param base58_string the string * @returns the size in bytes had the string been decoded */ -size_t libp2p_crypto_encoding_base58_decode_size(const unsigned char* base58_string) { +size_t multiaddr_encoding_base58_decode_size(const unsigned char* base58_string) { size_t string_length = strlen((char*)base58_string); size_t decoded_length = 0; size_t radix = strlen(b58digits_ordered); @@ -190,7 +190,7 @@ size_t libp2p_crypto_encoding_base58_decode_size(const unsigned char* base58_str * @param base58_string the string * @returns the maximum size in bytes had the string been decoded */ -size_t libp2p_crypto_encoding_base58_decode_max_size(const unsigned char* base58_string) { +size_t multiaddr_encoding_base58_decode_max_size(const unsigned char* base58_string) { size_t string_length = strlen((char*)base58_string); size_t decoded_length = 0; size_t radix = strlen(b58digits_ordered); diff --git a/include/multiaddr/base58.h b/include/multiaddr/base58.h index 421a5a4..874a631 100644 --- a/include/multiaddr/base58.h +++ b/include/multiaddr/base58.h @@ -17,7 +17,7 @@ * @param binary_data_size the size of the results buffer * @returns true(1) on success */ -int libp2p_crypto_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 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 @@ -27,21 +27,21 @@ int libp2p_crypto_encoding_base58_decode(const unsigned char* base58, size_t bas * @param base58_size the size of the results buffer * @returns true(1) on success */ -int libp2p_crypto_encoding_base58_encode(const unsigned char* binary_data, size_t binary_data_size, unsigned char** base58, size_t* base58_size); +int multiaddr_encoding_base58_encode(const unsigned char* binary_data, size_t binary_data_size, unsigned char** base58, size_t* base58_size); /*** * calculate the size of the binary results based on an incoming base58 string with no initial padding * @param base58_string the string * @returns the size in bytes had the string been decoded */ -size_t libp2p_crypto_encoding_base58_decode_size(const unsigned char* base58_string); +size_t multiaddr_encoding_base58_decode_size(const unsigned char* base58_string); /** * calculate the max length in bytes of an encoding of n source bits * @param base58_string the string * @returns the maximum size in bytes had the string been decoded */ -size_t libp2p_crypto_encoding_base58_decode_max_size(const unsigned char* base58_string); +size_t multiaddr_encoding_base58_decode_max_size(const unsigned char* base58_string); -#endif /* base58_h */ \ No newline at end of file +#endif /* base58_h */ diff --git a/include/multiaddr/multiaddr.h b/include/multiaddr/multiaddr.h index f902ffc..9b04a3b 100644 --- a/include/multiaddr/multiaddr.h +++ b/include/multiaddr/multiaddr.h @@ -1,128 +1,30 @@ #ifndef MULTIADDR #define MULTIADDR +#include + #include "varhexutils.h" -//#include "codecs.h" #include "varint.h" #include "protocols.h" #include "protoutils.h" -#include -int strpos(char *haystack, char *needle) -{ - char *p = strstr(haystack, needle); - if (p) - { - return p - haystack; - } - else - { - return -1; // Not found = -1. - } -} + struct maddr { char string[800]; uint8_t bytes[400]; int bsize[1]; }; -struct maddr new_maddr_fb(uint8_t * byteaddress,int size)//Construct new address from bytes -{ - struct maddr anewaddr2; - if(byteaddress!=NULL) - { - memcpy(anewaddr2.bytes, byteaddress,size); - if(bytes_to_string(anewaddr2.string,byteaddress,size)==1) - { - return anewaddr2; - } - } - return anewaddr2; -} -struct maddr new_maddr_fs(char * straddress)//Construct new address from string -{ - struct maddr anewaddr; - 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) - { - int betta; - //printf("BSIZE: %u\n", anewaddr.bsize[0]); - for(betta=anewaddr.bsize[0];betta<400;betta++) - { - anewaddr.bytes[betta] = '\0'; - } - return anewaddr; - } - return anewaddr; -} -int m_encapsulate(struct maddr * result, char * string) -{ - 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 - { - return 0; - } - } - else - { - return 0; - } -} -int m_decapsulate(struct maddr * result, char * srci) -{ - if(result!=NULL && srci!=NULL) - { - char * procstr = NULL; - procstr = result->string; - int i=0; - int sz=strlen(procstr); - char * src = NULL; - src=srci; - for(i=0;i #include #include "varhexutils.h" -int CNT_PROTOCOLNUM=0; + struct protocol { char hexcode[21]; @@ -15,196 +15,21 @@ struct protocol int size; char name[30]; }; + struct protocol *protocol_P; //Pointer for holding multiple structures -int protocol_REMOVE_id(int remid)//Function to remove & shift back all data, sort of like c++ vectors. -{ - if(remid < CNT_PROTOCOLNUM && remid >= 0&&CNT_PROTOCOLNUM!=0) //Checking to see if remid actually exists. - { - for(int i=remid; ihexcode, (protocol_P+i+1)->hexcode); //shift memory to the user we want to remove. - (protocol_P+i)->deccode = (protocol_P+i+1)->deccode; //Same as above - (protocol_P+i)->size = (protocol_P+i+1)->size; //Same as above - strcpy((protocol_P+i)->name, (protocol_P+i+1)->name); //Same as above - }//Overwriting user done. Time to get rid of that extra memory. - protocol_P = (struct protocol*) realloc(protocol_P, (CNT_PROTOCOLNUM-1) * sizeof(struct protocol)); - //Memory erased, - CNT_PROTOCOLNUM--; //Since the record no longer exists, we should decrease the ammount of users. - return 1; //Purely for error checking, in case someone ever wants it/ - } //1 = Success - else - { - if(CNT_PROTOCOLNUM == 0) - { - perror("ERROR: 0 PROTOCOLS... Did you load protocols?"); - } - else - { - perror("ERROR: No such protocol!"); - } - return 0; - } -} -void unload_protocols() -{ - free(protocol_P); - CNT_PROTOCOLNUM=0; -} -void load_protocols() -{ - FILE *FPROC_POINT; //File pointer. - FPROC_POINT = fopen("proto-dat", "r");//Opening proto-dat Or protocols.csv, I just formatted it to my liking. - if(FPROC_POINT != NULL) //While pointer is not null. - { - char W_BUFF[20] = "\0";//Char array to parse file. - for(int i=0; fscanf(FPROC_POINT, "%s", W_BUFF) != EOF; i++) // Scanning file and incrementing for processing. - { - switch(i) - { - case 0: //First word - HEXCODE - { - //ADD MEMORY FOR NEW PROTOCOL - if(CNT_PROTOCOLNUM==0) //If there are no registered protocols yet, allocate memory to pointer. - { - protocol_P = (struct protocol*) malloc (sizeof(struct protocol)); - } - else //Reallocate memory to fit one more protocol - { - protocol_P = (struct protocol*) realloc(protocol_P, (CNT_PROTOCOLNUM+1) * sizeof(struct protocol)); - } - strcpy((protocol_P+CNT_PROTOCOLNUM)->hexcode, W_BUFF); //Copy word to structure at hexcode A hexcode is a string so we keep it as such - break; - } - case 1://Second word - DECCODE - { - (protocol_P+CNT_PROTOCOLNUM)->deccode= atoi(W_BUFF); //Copy word to structure at deccode after converting it to int. - break; - } - case 2://Third word - SIZE - { - (protocol_P+CNT_PROTOCOLNUM)->size= atoi(W_BUFF); //Copy word to structure at size after converting it to int. - break; - } - case 3://Fourth word - NAME - { - strcpy((protocol_P+CNT_PROTOCOLNUM)->name, W_BUFF); //Copy word to structure at name // String - i=-1; - CNT_PROTOCOLNUM++; - break; - } - default: - { - printf("HOUSTON WE HAVE A BIG PROBLEM!!!!\nPROTOCOLS.H-REACHED DEFAULT CASE IN READING FILE!\nREPORT TO SYSTEMS ADMIN!\n"); - break; - } - } - } - fclose(FPROC_POINT); - protocol_REMOVE_id(0); - } - else - { - perror("Fatal Error:"); - } -} -struct protocol * proto_with_name(char proto_w_name[]) //Search for protocol with inputted name -{ +int protocol_REMOVE_id(int remid); //Function to remove & shift back all data, sort of like c++ vectors. - for(int i=0; iname) == 0) - { - return (protocol_P+i); - } - } - return NULL; -} -struct protocol * proto_with_deccode(int proto_w_deccode) //Search for protocol with inputted deccode -{ - for(int i=0; ideccode == proto_w_deccode) - { - return (protocol_P+i); - } - } - return NULL; -} -void pp() //Purely for debugging purposes, prints the entire loaded protocols. -{ - for(int i=0;i=9) - {printf("=========== ~%d~ ===========\n", i+1);} - else - {printf("=========== ~0%d~ ===========\n", i+1);} - printf(">> HEX-CODE: %s\n", (protocol_P+i)->hexcode); - printf(">> DEC-CODE: %d\n", (protocol_P+i)->deccode); - printf(">> SIZE: %d\n", (protocol_P+i)->size); - printf(">> NAME: %s\n", (protocol_P+i)->name); - } - printf("----------------------------\n"); - printf("TOTAL PROTOCOLS: %d\n",CNT_PROTOCOLNUM); -} -void protocols_with_string(char * meee,int sizi) // NOT FINISHED, DO NOT USE! -{ - int finalsize = 0; +void unload_protocols(); - if(!isalnum(meee[sizi-1]) && !isalnum(meee[sizi-1])) - { - //Everything is alright, it's nul terminated!; - finalsize = sizi; - } - else - { - //Well houston we have a problem. - finalsize = sizi+2; - } - char mestring[finalsize]; - strcpy(mestring, meee); - if(sizi!=finalsize) - { - strcpy(mestring,"\0"); - } - - char * words[50] = { NULL }; - int atword = 0; - int mem = 0; - for(int i=0; i -#include -#include -#include -#include -#include -#include "base58.h" -#include "varhexutils.h" -#include "protocols.h" + ////////////////////////////////////////////////////////// -char ASCII2bits(char ch) { - if (ch >= '0' && ch <= '9') { - return (ch - '0'); - } else if (ch >= 'a' && ch <= 'z') { - return (ch - 'a') + 10; - } else if (ch >= 'A' && ch <= 'Z') { - return (ch - 'A') + 10; - } - return 0; // fail -} +char ASCII2bits(char ch); -void hex2bin (char *dst, char *src, int len) -{ - while (len--) { - *dst = ASCII2bits(*src++) << 4; // higher bits - *dst++ |= ASCII2bits(*src++); // lower bits - } -} +void hex2bin (char *dst, char *src, int len); -char bits2ASCII(char b) { - if (b >= 0 && b < 10) { - return (b + '0'); - } else if (b >= 10 && b <= 15) { - return (b - 10 + 'a'); - } - return 0; // fail -} +char bits2ASCII(char b); + +void bin2hex (char *dst, char *src, int len); -void bin2hex (char *dst, char *src, int len) -{ - while (len--) { - *dst++ = bits2ASCII((*src >> 4) & 0xf); // higher bits - *dst++ = bits2ASCII(*src++ & 0xf); // lower bits - } - *dst = '\0'; -} ////////////////////////////////////////////////////////// //IPv4 VALIDATOR #define DELIM "." /* return 1 if string contain only digits, else return 0 */ -int valid_digit(char *ip_str) -{ - int err = 0; - while (*ip_str) { - if (*ip_str >= '0' && *ip_str <= '9') - ++ip_str; - else - return 0; - } - return 1; -} - -/* return 1 if IP string is valid, else return 0 */ -int is_valid_ipv4(char *ip_str) -{ - int i, num, dots = 0; - char *ptr; - int err=0; - if (ip_str == NULL) - err = 1; - - // See following link for strtok() - // http://pubs.opengroup.org/onlinepubs/009695399/functions/strtok_r.html - ptr = strtok(ip_str, DELIM); - - if (ptr == NULL) - err = 1; - - while (ptr) - { - - /* after parsing string, it must contain only digits */ - if (!valid_digit(ptr)) - err = 1; - - num = atoi(ptr); - - /* check for valid IP */ - if (num >= 0 && num <= 255) { - /* parse remaining string */ - ptr = strtok(NULL, DELIM); - if (ptr != NULL) - ++dots; - } else - err = 1; - } - - /* valid IP string must contain 3 dots */ - if (dots != 3) - { - err = 1; - } - if(err == 0) - { - return 1; - } - else - { - return 0; - } -} +int valid_digit(char *ip_str); +/* return 1 if IP string is valid, else return 0 */ +int is_valid_ipv4(char *ip_str); //////////////IPv6 Validator #define MAX_HEX_NUMBER_COUNT 8 -int ishexdigit(char ch) -{ - if((ch>='0'&&ch<='9')||(ch>='a'&&ch<='f')||(ch>='A'&&ch<='F')) - return(1); - return(0); -} +int ishexdigit(char ch); -int is_valid_ipv6(char *str) -{ - int hdcount=0; - int hncount=0; - int err=0; - int packed=0; +int is_valid_ipv6(char *str); - if(*str==':') - { - str++; - if(*str!=':') - return(0); - else - { - packed=1; - hncount=1; - str++; +uint64_t ip2int(char * ipconvertint); - if(*str==0) - return(1); - } - } +char * int2ip(int inputintip); - if(ishexdigit(*str)==0) - { - return(0); - } - - hdcount=1; - hncount=1; - str++; - - while(err==0&&*str!=0) - { - if(*str==':') - { - str++; - if(*str==':') - { - if(packed==1) - err=1; - else - { - str++; - - if(ishexdigit(*str)||*str==0&&hncount> 8*3) % 256; - uint32_t ipint1 = (ipint >> 8*2) % 256; - uint32_t ipint2 = (ipint >> 8*1) % 256; - uint32_t ipint3 = (ipint >> 8*0) % 256; - sprintf(xxx_int2ip_result, "%d.%d.%d.%d", ipint0,ipint1,ipint2,ipint3); - 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) -{ - bzero(resultzx,800); - uint8_t * bytes = NULL; - int size = 0; - size = xbsize; - load_protocols(); - char hex[xbsize*2]; - bzero(hex,xbsize*2); - strcat(hex,Var_To_Hex(size, catx)); - //Positioning for memory jump: - int lastpos = 0; - char pid[3]; - //Process Hex String - NAX: - //Stage 1 ID: - if(lastpos!=0) - { - lastpos+1; - } - pid[0] = hex[lastpos]; - pid[1] = hex[lastpos+1]; - pid[2] = '\0'; - if(lastpos == 0) - { - load_protocols(); - } - if(proto_with_deccode(Hex_To_Int(pid))) - { -//////////Stage 2: Address - struct protocol * PID; - PID = NULL; - PID = proto_with_deccode(Hex_To_Int(pid)); - if(strcmp(PID->name,"ipfs")!=0) - { - lastpos = lastpos+2; - char address[(PID->size/4)+1]; - bzero(address,(PID->size/4)+1); - address[(PID->size/4)]='\0'; - int x=0; - //printf("\nHEX TO DECODE: %s\n",hex); - for(int i = lastpos;i<(PID->size/4)+lastpos;i++) - { - address[x] = hex[i]; - //printf("HEX[%d]=%c\n",i,hex[i]); - x++; - } - //////////Stage 3 Process it back to string - //printf("Protocol: %s\n", PID->name); - //printf("Address : %s\n", address); - lastpos= lastpos+(PID->size/4); - //printf("lastpos: %d",lastpos); - - //////////Address: - //Keeping Valgrind happy - char name[30]; - bzero(name,30); - strcpy(name, PID->name); - // - strcat(resultzx, "/"); - strcat(resultzx, name); - strcat(resultzx, "/"); - if(strcmp(name, "ip4")==0) - { - strcat(resultzx,int2ip(Hex_To_Int(address))); - } - else if(strcmp(name, "tcp")==0) - { - char a[5]; - sprintf(a,"%lu",Hex_To_Int(address)); - strcat(resultzx,a); - } - else if(strcmp(name, "udp")==0) - { - char a[5]; - sprintf(a,"%lu",Hex_To_Int(address)); - strcat(resultzx,a); - } - //printf("Address(hex):%s\n",address); - //printf("TESTING: %s\n",resultzx); - /////////////Done processing this, move to next if there is more. - if(lastposname); - strcat(resultzx, "/"); - strcat(resultzx, rezultat); - } - } - strcat(resultzx, "/"); - unload_protocols(); - -} -// +int bytes_to_string(char * resultzx, uint8_t * catx,int xbsize); -char * address_string_to_bytes(struct protocol * xx, char * abc,size_t getsznow) -{ - static char astb__stringy[800] = "\0"; - bzero(astb__stringy,800); - int code = 0; - code = xx->deccode; - switch(code) - { - case 4://IPv4 - { - char testip[16] = "\0"; - bzero(testip,16); - strcpy(testip,abc); - if(is_valid_ipv4(testip)==1) - { - uint64_t iip = ip2int(abc); - strcpy(astb__stringy,Int_To_Hex(iip)); - xx = NULL; - return astb__stringy; - } - else - { - return "ERR"; - } - break; - } - case 41://IPv6 Must be done - { - return "ERR"; - break; - } - case 6: //Tcp - { - if(atoi(abc)<65536&&atoi(abc)>0) - { - static char himm_woot[5] = "\0"; - bzero(himm_woot, 5); - strcpy(himm_woot, Int_To_Hex(atoi(abc))); - if(himm_woot[2] == '\0') - {//manual switch - char swap0='0'; - char swap1='0'; - char swap2=himm_woot[0]; - char swap3=himm_woot[1]; - himm_woot[0] = swap0; - himm_woot[1] = swap1; - himm_woot[2] = swap2; - himm_woot[3] = swap3; - } - else if(himm_woot[3] == '\0') - { - char swap0='0'; - char swap1=himm_woot[0]; - char swap2=himm_woot[1]; - char swap3=himm_woot[2]; - himm_woot[0] = swap0; - himm_woot[1] = swap1; - himm_woot[2] = swap2; - himm_woot[3] = swap3; - } - himm_woot[4]='\0'; - return himm_woot; - } - else - { - return "ERR"; - } - break; - } - case 17: //Udp - { - if(atoi(abc)<65536&&atoi(abc)>0) - { - static char himm_woot2[5] = "\0"; - bzero(himm_woot2, 5); - strcpy(himm_woot2, Int_To_Hex(atoi(abc))); - if(himm_woot2[2] == '\0') - {//Manual Switch2be - char swap0='0'; - char swap1='0'; - char swap2=himm_woot2[0]; - char swap3=himm_woot2[1]; - himm_woot2[0] = swap0; - himm_woot2[1] = swap1; - himm_woot2[2] = swap2; - himm_woot2[3] = swap3; - } - else if(himm_woot2[3] == '\0') - {//Manual switch - char swap0='0'; - char swap1=himm_woot2[0]; - char swap2=himm_woot2[1]; - char swap3=himm_woot2[2]; - himm_woot2[0] = swap0; - himm_woot2[1] = swap1; - himm_woot2[2] = swap2; - himm_woot2[3] = swap3; - } - himm_woot2[4]='\0'; - return himm_woot2; - } - else - { - return "ERR"; - } - break; - } - case 33://dccp - { - return "ERR"; - break; - } - case 132://sctp - { - return "ERR"; - break; - } - case 301://udt - { - return "ERR"; - break; - } - case 302://utp - { - return "ERR"; - break; - } - case 42://IPFS - !!! - { - +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); - char * x_data = NULL; - x_data = abc; - size_t x_data_length = strlen(x_data); - size_t result_buffer_length = libp2p_crypto_encoding_base58_decode_max_size(x_data); - unsigned char result_buffer[result_buffer_length]; - unsigned char* ptr_to_result = result_buffer; - memset(result_buffer, 0, result_buffer_length); - // now get the decoded address - int return_value = libp2p_crypto_encoding_base58_decode(x_data, x_data_length, &ptr_to_result, &result_buffer_length); - if (return_value == 0) - { - return "ERR"; - } - // throw everything in a hex string so we can debug the results - static char returning_result[300]; - bzero(returning_result,300); - char ADDR_ENCODED[300]; - bzero(ADDR_ENCODED,300); - int ilen = 0; - bzero(returning_result,300); - for(int i = 0; i < result_buffer_length; i++) - { - // get the char so we can see it in the debugger - unsigned char c = ptr_to_result[i]; - char miu[3]; - bzero(miu, 3); - miu[3] = '\0'; - sprintf(miu,"%02x", c); - - strcat(ADDR_ENCODED, miu); - } - ilen = strlen(ADDR_ENCODED); - char prefixed[3]; - strcpy(prefixed,Num_To_HexVar_32(ilen)); - prefixed[3] = '\0'; - strcat(returning_result, prefixed); - strcat(returning_result, ADDR_ENCODED); - //printf("ADDRESS: %s\nSIZEADDR: %d\n",ADDR_ENCODED,ilen); - //printf("NOW DECODED VARINT: %d", HexVar_To_Num_32(prefixed)); - return returning_result; - break; - } - case 480://http - { - return "ERR"; - break; - } - case 443://https - { - return "ERR"; - break; - } - case 477://ws - { - return "ERR"; - break; - } - case 444://onion - { - return "ERR"; - break; - } - case 275://libp2p-webrtc-star - { - return "ERR"; - break; - } - default: - { - printf("NO SUCH PROTOCOL!\n"); - return "ERR"; - break; - } - } -} -int string_to_bytes(uint8_t * finalbytes,int * realbbsize,char * strx, size_t strsize) -{ - if(strx[0] != '/') - { - printf("Error, must start with '/'\n"); - return 0; - } - char xxx[800]; - bzero(xxx,800); - //Getting total words - int totalwords = 0; - char * totp; - char totalwordstest[strsize]; - bzero(totalwordstest,strsize); - strcat(totalwordstest, strx); - totp = strtok(totalwordstest, "/"); - while(totp != NULL) - { - totp = strtok (NULL, "/"); - totalwords++; - } - //Initializing variables to store our processed HEX in: - int malf=0; //In case something goes wrong this will be 1. - char processed[800];//HEX CONTAINER - bzero(processed,800); - //Now Setting up variables for calculating which is the first - //and second word: - int firstorsecond = 1; //1=Protocol && 2 = Address - char pstring[800];//We do not want to harm the initial string. - bzero(pstring,800); - strcat(pstring,strx); - //Starting to extract words and process them: - char * wp; - char * end; - wp=strtok_r(pstring,"/",&end); - load_protocols(); - struct protocol * protx; - while(wp) - { - if(firstorsecond==1)//This is the Protocol - { - if(proto_with_name(wp)) - { - protx=proto_with_name(wp); - //printf("PROTOCOL: %s\n",protx->name); - strcat(processed, Int_To_Hex(protx->deccode)); - firstorsecond=2;//Since the next word will be an address - } - else - { - printf("\nNo such protocol!\n\n"); - malf=1; - break; - } - } - else//This is the address - { - //printf("ADDRESS: %s\n",wp); - if(address_string_to_bytes(protx, wp,strlen(wp)) == "ERR") - { - malf = 1; - //printf("\n\nTRIGGERED!!!!!!!!!!!!!!!!!!!!!!!\n\n"); - } - else - { - strcat(processed,address_string_to_bytes(protx, wp,strlen(wp))); - //printf("Addressinbytes: %s\n",address_string_to_bytes(protx, wp,strlen(wp))); - } - protx=NULL;//Since right now it doesn't need that assignment anymore. - firstorsecond=1;//Since the next word will be an protocol - } - wp=strtok_r(NULL,"/",&end); - } - protx=NULL; - unload_protocols(); - //printf("Processed contains: %s \n",processed); - - if(malf==1) - { - return 0; - } - else - { - bzero(finalbytes,400); - //printf("XXX: %s\n",xxx); - memcpy(finalbytes, Hex_To_Var(processed), 400); - realbbsize[0] = 0; - for(int i=0;i<400;i++) - { - if(finalbytes[i]) - { - realbbsize[0]++; - } - } - return 1; - } -} #endif diff --git a/include/multiaddr/varhexutils.h b/include/multiaddr/varhexutils.h index 18a08ec..da1b2b9 100644 --- a/include/multiaddr/varhexutils.h +++ b/include/multiaddr/varhexutils.h @@ -13,239 +13,39 @@ memcpy(encbe, htobe32(ebex32)); return encbe; }*/ -int8_t Var_Bytes_Count(uint8_t * countbytesofthis) -{ - static int8_t xrzk_bytescnt = 0; - for(int8_t i=0; i<10; i++) - { - if(countbytesofthis[i] != 0) - { - xrzk_bytescnt++; - } - } - return xrzk_bytescnt; -} -uint8_t * Num_To_Varint_64(uint64_t TOV64INPUT) //UINT64_T TO VARINT -{ - static uint8_t buffy_001[60] = {0}; - uvarint_encode64(TOV64INPUT, buffy_001, 60); - return buffy_001; -} -uint8_t * Num_To_Varint_32(uint32_t TOV32INPUT) // UINT32_T TO VARINT -{ - static uint8_t buffy_032[60] = {0}; - uvarint_encode32(TOV32INPUT, buffy_032, 60); - return buffy_032; -} -uint64_t * Varint_To_Num_64(uint8_t TON64INPUT[60]) //VARINT TO UINT64_t -{ - static uint64_t varintdecode_001 = 0; - uvarint_decode64(TON64INPUT, 60, &varintdecode_001); - return &varintdecode_001; -} -uint32_t * Varint_To_Num_32(uint8_t TON32INPUT[60]) //VARINT TO UINT32_t -{ - static uint32_t varintdecode_032 = 0; - uvarint_decode32(TON32INPUT, 60, &varintdecode_032); - return &varintdecode_032; -} -// -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; -} -uint64_t Hex_To_Int(char * hax) -{ - char * hex = NULL; - hex=hax; - uint64_t val = 0; - while (*hex) - { - // get current character then increment - uint8_t byte = *hex++; - // transform hex character to the 4bit equivalent number, using the ascii table indexes - if (byte >= '0' && byte <= '9') byte = byte - '0'; - else if (byte >= 'a' && byte <='f') byte = byte - 'a' + 10; - else if (byte >= 'A' && byte <='F') byte = byte - 'A' + 10; - // shift 4 to make space for new digit, and add the 4 bits of the new digit - val = (val << 4) | (byte & 0xF); - } - return val; -} -// -void vthconvert(int size, char * crrz01, uint8_t * xbuf) -{ - uint8_t buf[400]; - bzero(buf,400); - - //fixing the buf - for(int cz=0; cz +#include "multiaddr/varhexutils.h" +#include "multiaddr/varint.h" +#include "multiaddr/protocols.h" +#include "multiaddr/protoutils.h" +#include "multiaddr/multiaddr.h" + +int strpos(char *haystack, char *needle) +{ + char *p = strstr(haystack, needle); + if (p) + { + return p - haystack; + } + else + { + return -1; // Not found = -1. + } +} + +struct maddr* new_maddr_fb(uint8_t* byteaddress,int size)//Construct new address from bytes +{ + struct maddr* anewaddr2 = (struct maddr*)malloc(sizeof(struct maddr)); + if (anewaddr2 != NULL) { + if(byteaddress!=NULL) + { + memcpy(anewaddr2->bytes, byteaddress, size); + if(bytes_to_string(anewaddr2->string,byteaddress,size)==1) + { + return anewaddr2; + } + } + } + return anewaddr2; +} +struct maddr* new_maddr_fs(char * straddress)//Construct new address from string +{ + struct maddr* anewaddr = (struct maddr*)malloc(sizeof(struct maddr)); + 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) + { + int betta; + //printf("BSIZE: %u\n", anewaddr.bsize[0]); + for(betta=anewaddr->bsize[0];betta<400;betta++) + { + anewaddr->bytes[betta] = '\0'; + } + return anewaddr; + } + } + return anewaddr; +} + +void maddr_free(struct maddr* in) { + free(in); +} + +int m_encapsulate(struct maddr * result, char * string) +{ + 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 + { + return 0; + } + } + else + { + return 0; + } +} +int m_decapsulate(struct maddr * result, char * srci) +{ + if(result!=NULL && srci!=NULL) + { + char * procstr = NULL; + procstr = result->string; + int i=0; + int sz=strlen(procstr); + char * src = NULL; + src=srci; + for(i=0;i +#include +#include +#include +#include + +#include "multiaddr/protocols.h" +#include "multiaddr/varhexutils.h" + +int CNT_PROTOCOLNUM=0; + +struct protocol *protocol_P; //Pointer for holding multiple structures + +int protocol_REMOVE_id(int remid)//Function to remove & shift back all data, sort of like c++ vectors. { - load_protocols(); - pp(); - printf("The returned protocol is: %s\nSIZE: %d\n", proto_with_name("onion")->name, proto_with_name("onion")->size); - printf("The returned protocol is: %s\nSIZE: %d\n", proto_with_deccode(444)->name, proto_with_deccode(444)->size); - for(int i=0; i<14; i++) + + if(remid < CNT_PROTOCOLNUM && remid >= 0&&CNT_PROTOCOLNUM!=0) //Checking to see if remid actually exists. { - uint64_t extract = (protocol_P+i)->deccode; - char exhexco[20] = "\0"; - strcpy(exhexco, Num_To_HexVar_64(extract)); - uint64_t decimalval = HexVar_To_Num_64(exhexco); - if(i<9){printf("PROTOCOL 0%d HAS HEXCODE: %s DECIMAL: %"PRIu64" \n", i+1, exhexco, decimalval);} - else{printf("PROTOCOL %d HAS HEXCODE: %s DECIMAL: %"PRIu64" \n", i+1, exhexco, decimalval);}; + for(int i=remid; ihexcode, (protocol_P+i+1)->hexcode); //shift memory to the user we want to remove. + (protocol_P+i)->deccode = (protocol_P+i+1)->deccode; //Same as above + (protocol_P+i)->size = (protocol_P+i+1)->size; //Same as above + strcpy((protocol_P+i)->name, (protocol_P+i+1)->name); //Same as above + }//Overwriting user done. Time to get rid of that extra memory. + protocol_P = (struct protocol*) realloc(protocol_P, (CNT_PROTOCOLNUM-1) * sizeof(struct protocol)); + //Memory erased, + CNT_PROTOCOLNUM--; //Since the record no longer exists, we should decrease the ammount of users. + return 1; //Purely for error checking, in case someone ever wants it/ + } //1 = Success + else + { + if(CNT_PROTOCOLNUM == 0) + { + perror("ERROR: 0 PROTOCOLS... Did you load protocols?"); + } + else + { + perror("ERROR: No such protocol!"); + } + return 0; } +} +void unload_protocols() +{ free(protocol_P); - printf("1337 in Hexvar_64: %s\n", Num_To_HexVar_64(1337)); - char HEXSTR[20] = "B90A0000000000000000"; //\0 since it's not defined in a source string - uint64_t result = HexVar_To_Num_64(HEXSTR); - result = HexVar_To_Num_64(HEXSTR); - printf("Hexvar_To_Num_64: %"PRIu64"\n", result); - uint8_t Numinvar[10] = {0}; - memcpy(Numinvar, Num_To_Varint_64(1337), 10); - printf("Binary form unreadable obviously : %s\n", Numinvar); - uint64_t Varinnum = 0; - memcpy(&Varinnum, Varint_To_Num_64(Numinvar), sizeof(Varint_To_Num_64(Numinvar))); - printf("Number form now readable again:%"PRIu64"\n", Varinnum); - char converted2hex[20] = "\0"; - strcpy(converted2hex, Var_To_Hex(Numinvar)); - uint8_t converted2bin[10] = {0}; - memcpy(converted2bin, Hex_To_Var(converted2hex), 10); - printf("Encoding previous binary to hex now: %s\n", converted2hex); - printf("Encoding previous hex to binary now: %s\n", converted2bin); - //Series of bytes test - int8_t bcounter = 0; - bcounter = Var_Bytes_Count(Numinvar); - printf("Bytes_Count of Numinvar(1337): %d\n", bcounter); - char int2hex[20] = "\0"; - strcat(int2hex, Int_To_Hex(1337)); - printf("INT2HEX: %s\n", int2hex); - /*TESTING ENDIAN // Aparently won't be needed. - printf("Testing Endian:\n"); - uint32_t val32 = 1337; - printf("val32 = %d swapped val32 = %d\n",val32, htole32(val32)); - printf("val32 = 0x%08x swapped val32 = 0x%08x\n\n",val32, htole32(val32)); - */ - return 0; -} \ No newline at end of file + CNT_PROTOCOLNUM=0; +} +void load_protocols() +{ + FILE *FPROC_POINT; //File pointer. + FPROC_POINT = fopen("proto-dat", "r");//Opening proto-dat Or protocols.csv, I just formatted it to my liking. + if(FPROC_POINT != NULL) //While pointer is not null. + { + char W_BUFF[20] = "\0";//Char array to parse file. + for(int i=0; fscanf(FPROC_POINT, "%s", W_BUFF) != EOF; i++) // Scanning file and incrementing for processing. + { + switch(i) + { + case 0: //First word - HEXCODE + { + //ADD MEMORY FOR NEW PROTOCOL + if(CNT_PROTOCOLNUM==0) //If there are no registered protocols yet, allocate memory to pointer. + { + protocol_P = (struct protocol*) malloc (sizeof(struct protocol)); + } + else //Reallocate memory to fit one more protocol + { + protocol_P = (struct protocol*) realloc(protocol_P, (CNT_PROTOCOLNUM+1) * sizeof(struct protocol)); + } + strcpy((protocol_P+CNT_PROTOCOLNUM)->hexcode, W_BUFF); //Copy word to structure at hexcode A hexcode is a string so we keep it as such + break; + } + case 1://Second word - DECCODE + { + (protocol_P+CNT_PROTOCOLNUM)->deccode= atoi(W_BUFF); //Copy word to structure at deccode after converting it to int. + break; + } + case 2://Third word - SIZE + { + (protocol_P+CNT_PROTOCOLNUM)->size= atoi(W_BUFF); //Copy word to structure at size after converting it to int. + break; + } + case 3://Fourth word - NAME + { + strcpy((protocol_P+CNT_PROTOCOLNUM)->name, W_BUFF); //Copy word to structure at name // String + i=-1; + CNT_PROTOCOLNUM++; + break; + } + default: + { + printf("HOUSTON WE HAVE A BIG PROBLEM!!!!\nPROTOCOLS.H-REACHED DEFAULT CASE IN READING FILE!\nREPORT TO SYSTEMS ADMIN!\n"); + break; + } + } + } + fclose(FPROC_POINT); + protocol_REMOVE_id(0); + } + else + { + perror("Fatal Error:"); + } +} +struct protocol * proto_with_name(char proto_w_name[]) //Search for protocol with inputted name +{ + + for(int i=0; iname) == 0) + { + return (protocol_P+i); + } + } + return NULL; +} +struct protocol * proto_with_deccode(int proto_w_deccode) //Search for protocol with inputted deccode +{ + for(int i=0; ideccode == proto_w_deccode) + { + return (protocol_P+i); + } + } + return NULL; +} +void pp() //Purely for debugging purposes, prints the entire loaded protocols. +{ + for(int i=0;i=9) + {printf("=========== ~%d~ ===========\n", i+1);} + else + {printf("=========== ~0%d~ ===========\n", i+1);} + printf(">> HEX-CODE: %s\n", (protocol_P+i)->hexcode); + printf(">> DEC-CODE: %d\n", (protocol_P+i)->deccode); + printf(">> SIZE: %d\n", (protocol_P+i)->size); + printf(">> NAME: %s\n", (protocol_P+i)->name); + } + printf("----------------------------\n"); + printf("TOTAL PROTOCOLS: %d\n",CNT_PROTOCOLNUM); +} +void protocols_with_string(char * meee,int sizi) // NOT FINISHED, DO NOT USE! +{ + int finalsize = 0; + + if(!isalnum(meee[sizi-1]) && !isalnum(meee[sizi-1])) + { + //Everything is alright, it's nul terminated!; + finalsize = sizi; + } + else + { + //Well houston we have a problem. + finalsize = sizi+2; + } + char mestring[finalsize]; + strcpy(mestring, meee); + if(sizi!=finalsize) + { + strcpy(mestring,"\0"); + } + + char * words[50] = { NULL }; + int atword = 0; + int mem = 0; + for(int i=0; i +#include +#include +#include +#include +#include +#include "multiaddr/base58.h" +#include "multiaddr/varhexutils.h" +#include "multiaddr/protocols.h" +#include "multiaddr/protoutils.h" + +////////////////////////////////////////////////////////// +char ASCII2bits(char ch) { + if (ch >= '0' && ch <= '9') { + return (ch - '0'); + } else if (ch >= 'a' && ch <= 'z') { + return (ch - 'a') + 10; + } else if (ch >= 'A' && ch <= 'Z') { + return (ch - 'A') + 10; + } + return 0; // fail +} + +void hex2bin (char *dst, char *src, int len) +{ + while (len--) { + *dst = ASCII2bits(*src++) << 4; // higher bits + *dst++ |= ASCII2bits(*src++); // lower bits + } +} + +char bits2ASCII(char b) { + if (b >= 0 && b < 10) { + return (b + '0'); + } else if (b >= 10 && b <= 15) { + return (b - 10 + 'a'); + } + return 0; // fail +} + +void bin2hex (char *dst, char *src, int len) +{ + while (len--) { + *dst++ = bits2ASCII((*src >> 4) & 0xf); // higher bits + *dst++ = bits2ASCII(*src++ & 0xf); // lower bits + } + *dst = '\0'; +} +////////////////////////////////////////////////////////// +//IPv4 VALIDATOR +#define DELIM "." + +/* return 1 if string contain only digits, else return 0 */ +int valid_digit(char *ip_str) +{ + int err = 0; + while (*ip_str) { + if (*ip_str >= '0' && *ip_str <= '9') + ++ip_str; + else + return 0; + } + return 1; +} + +/* return 1 if IP string is valid, else return 0 */ +int is_valid_ipv4(char *ip_str) +{ + int i, num, dots = 0; + char *ptr; + int err=0; + if (ip_str == NULL) + err = 1; + + // See following link for strtok() + // http://pubs.opengroup.org/onlinepubs/009695399/functions/strtok_r.html + ptr = strtok(ip_str, DELIM); + + if (ptr == NULL) + err = 1; + + while (ptr) + { + + /* after parsing string, it must contain only digits */ + if (!valid_digit(ptr)) + err = 1; + + num = atoi(ptr); + + /* check for valid IP */ + if (num >= 0 && num <= 255) { + /* parse remaining string */ + ptr = strtok(NULL, DELIM); + if (ptr != NULL) + ++dots; + } else + err = 1; + } + + /* valid IP string must contain 3 dots */ + if (dots != 3) + { + err = 1; + } + if(err == 0) + { + return 1; + } + else + { + return 0; + } +} + + +//////////////IPv6 Validator +#define MAX_HEX_NUMBER_COUNT 8 + +int ishexdigit(char ch) +{ + if((ch>='0'&&ch<='9')||(ch>='a'&&ch<='f')||(ch>='A'&&ch<='F')) + return(1); + return(0); +} + +int is_valid_ipv6(char *str) +{ + int hdcount=0; + int hncount=0; + int err=0; + int packed=0; + + if(*str==':') + { + str++; + if(*str!=':') + return(0); + else + { + packed=1; + hncount=1; + str++; + + if(*str==0) + return(1); + } + } + + if(ishexdigit(*str)==0) + { + return(0); + } + + hdcount=1; + hncount=1; + str++; + + while(err==0&&*str!=0) + { + if(*str==':') + { + str++; + if(*str==':') + { + if(packed==1) + err=1; + else + { + str++; + + if(ishexdigit(*str)||*str==0&&hncount> 8*3) % 256; + uint32_t ipint1 = (ipint >> 8*2) % 256; + uint32_t ipint2 = (ipint >> 8*1) % 256; + uint32_t ipint3 = (ipint >> 8*0) % 256; + sprintf(xxx_int2ip_result, "%d.%d.%d.%d", ipint0,ipint1,ipint2,ipint3); + 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) +{ + bzero(resultzx,800); + uint8_t * bytes = NULL; + int size = 0; + size = xbsize; + load_protocols(); + char hex[xbsize*2]; + bzero(hex,xbsize*2); + strcat(hex,Var_To_Hex(size, catx)); + //Positioning for memory jump: + int lastpos = 0; + char pid[3]; + //Process Hex String + NAX: + //Stage 1 ID: + if(lastpos!=0) + { + lastpos+1; + } + pid[0] = hex[lastpos]; + pid[1] = hex[lastpos+1]; + pid[2] = '\0'; + if(lastpos == 0) + { + load_protocols(); + } + if(proto_with_deccode(Hex_To_Int(pid))) + { +//////////Stage 2: Address + struct protocol * PID; + PID = NULL; + PID = proto_with_deccode(Hex_To_Int(pid)); + if(strcmp(PID->name,"ipfs")!=0) + { + lastpos = lastpos+2; + char address[(PID->size/4)+1]; + bzero(address,(PID->size/4)+1); + address[(PID->size/4)]='\0'; + int x=0; + //printf("\nHEX TO DECODE: %s\n",hex); + for(int i = lastpos;i<(PID->size/4)+lastpos;i++) + { + address[x] = hex[i]; + //printf("HEX[%d]=%c\n",i,hex[i]); + x++; + } + //////////Stage 3 Process it back to string + //printf("Protocol: %s\n", PID->name); + //printf("Address : %s\n", address); + lastpos= lastpos+(PID->size/4); + //printf("lastpos: %d",lastpos); + + //////////Address: + //Keeping Valgrind happy + char name[30]; + bzero(name,30); + strcpy(name, PID->name); + // + strcat(resultzx, "/"); + strcat(resultzx, name); + strcat(resultzx, "/"); + if(strcmp(name, "ip4")==0) + { + strcat(resultzx,int2ip(Hex_To_Int(address))); + } + else if(strcmp(name, "tcp")==0) + { + char a[5]; + sprintf(a,"%lu",Hex_To_Int(address)); + strcat(resultzx,a); + } + else if(strcmp(name, "udp")==0) + { + char a[5]; + sprintf(a,"%lu",Hex_To_Int(address)); + strcat(resultzx,a); + } + //printf("Address(hex):%s\n",address); + //printf("TESTING: %s\n",resultzx); + /////////////Done processing this, move to next if there is more. + if(lastposname); + strcat(resultzx, "/"); + strcat(resultzx, rezultat); + } + } + strcat(resultzx, "/"); + unload_protocols(); + +} +// + +char * address_string_to_bytes(struct protocol * xx, char * abc,size_t getsznow) +{ + static char astb__stringy[800] = "\0"; + bzero(astb__stringy,800); + int code = 0; + code = xx->deccode; + switch(code) + { + case 4://IPv4 + { + char testip[16] = "\0"; + bzero(testip,16); + strcpy(testip,abc); + if(is_valid_ipv4(testip)==1) + { + uint64_t iip = ip2int(abc); + strcpy(astb__stringy,Int_To_Hex(iip)); + xx = NULL; + return astb__stringy; + } + else + { + return "ERR"; + } + break; + } + case 41://IPv6 Must be done + { + return "ERR"; + break; + } + case 6: //Tcp + { + if(atoi(abc)<65536&&atoi(abc)>0) + { + static char himm_woot[5] = "\0"; + bzero(himm_woot, 5); + strcpy(himm_woot, Int_To_Hex(atoi(abc))); + if(himm_woot[2] == '\0') + {//manual switch + char swap0='0'; + char swap1='0'; + char swap2=himm_woot[0]; + char swap3=himm_woot[1]; + himm_woot[0] = swap0; + himm_woot[1] = swap1; + himm_woot[2] = swap2; + himm_woot[3] = swap3; + } + else if(himm_woot[3] == '\0') + { + char swap0='0'; + char swap1=himm_woot[0]; + char swap2=himm_woot[1]; + char swap3=himm_woot[2]; + himm_woot[0] = swap0; + himm_woot[1] = swap1; + himm_woot[2] = swap2; + himm_woot[3] = swap3; + } + himm_woot[4]='\0'; + return himm_woot; + } + else + { + return "ERR"; + } + break; + } + case 17: //Udp + { + if(atoi(abc)<65536&&atoi(abc)>0) + { + static char himm_woot2[5] = "\0"; + bzero(himm_woot2, 5); + strcpy(himm_woot2, Int_To_Hex(atoi(abc))); + if(himm_woot2[2] == '\0') + {//Manual Switch2be + char swap0='0'; + char swap1='0'; + char swap2=himm_woot2[0]; + char swap3=himm_woot2[1]; + himm_woot2[0] = swap0; + himm_woot2[1] = swap1; + himm_woot2[2] = swap2; + himm_woot2[3] = swap3; + } + else if(himm_woot2[3] == '\0') + {//Manual switch + char swap0='0'; + char swap1=himm_woot2[0]; + char swap2=himm_woot2[1]; + char swap3=himm_woot2[2]; + himm_woot2[0] = swap0; + himm_woot2[1] = swap1; + himm_woot2[2] = swap2; + himm_woot2[3] = swap3; + } + himm_woot2[4]='\0'; + return himm_woot2; + } + else + { + return "ERR"; + } + break; + } + case 33://dccp + { + return "ERR"; + break; + } + case 132://sctp + { + return "ERR"; + break; + } + case 301://udt + { + return "ERR"; + break; + } + case 302://utp + { + return "ERR"; + break; + } + case 42://IPFS - !!! + { + + + char * x_data = NULL; + x_data = 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]; + unsigned char* ptr_to_result = result_buffer; + memset(result_buffer, 0, result_buffer_length); + // now get the decoded address + int return_value = multiaddr_encoding_base58_decode(x_data, x_data_length, &ptr_to_result, &result_buffer_length); + if (return_value == 0) + { + return "ERR"; + } + // throw everything in a hex string so we can debug the results + static char returning_result[300]; + bzero(returning_result,300); + char ADDR_ENCODED[300]; + bzero(ADDR_ENCODED,300); + int ilen = 0; + bzero(returning_result,300); + for(int i = 0; i < result_buffer_length; i++) + { + // get the char so we can see it in the debugger + unsigned char c = ptr_to_result[i]; + char miu[3]; + bzero(miu, 3); + miu[3] = '\0'; + sprintf(miu,"%02x", c); + + strcat(ADDR_ENCODED, miu); + } + ilen = strlen(ADDR_ENCODED); + char prefixed[3]; + strcpy(prefixed,Num_To_HexVar_32(ilen)); + prefixed[3] = '\0'; + strcat(returning_result, prefixed); + strcat(returning_result, ADDR_ENCODED); + //printf("ADDRESS: %s\nSIZEADDR: %d\n",ADDR_ENCODED,ilen); + //printf("NOW DECODED VARINT: %d", HexVar_To_Num_32(prefixed)); + return returning_result; + break; + } + case 480://http + { + return "ERR"; + break; + } + case 443://https + { + return "ERR"; + break; + } + case 477://ws + { + return "ERR"; + break; + } + case 444://onion + { + return "ERR"; + break; + } + case 275://libp2p-webrtc-star + { + return "ERR"; + break; + } + default: + { + printf("NO SUCH PROTOCOL!\n"); + return "ERR"; + break; + } + } +} +int string_to_bytes(uint8_t * finalbytes,int * realbbsize,char * strx, size_t strsize) +{ + if(strx[0] != '/') + { + printf("Error, must start with '/'\n"); + return 0; + } + char xxx[800]; + bzero(xxx,800); + //Getting total words + int totalwords = 0; + char * totp; + char totalwordstest[strsize]; + bzero(totalwordstest,strsize); + strcat(totalwordstest, strx); + totp = strtok(totalwordstest, "/"); + while(totp != NULL) + { + totp = strtok (NULL, "/"); + totalwords++; + } + //Initializing variables to store our processed HEX in: + int malf=0; //In case something goes wrong this will be 1. + char processed[800];//HEX CONTAINER + bzero(processed,800); + //Now Setting up variables for calculating which is the first + //and second word: + int firstorsecond = 1; //1=Protocol && 2 = Address + char pstring[800];//We do not want to harm the initial string. + bzero(pstring,800); + strcat(pstring,strx); + //Starting to extract words and process them: + char * wp; + char * end; + wp=strtok_r(pstring,"/",&end); + load_protocols(); + struct protocol * protx; + while(wp) + { + if(firstorsecond==1)//This is the Protocol + { + if(proto_with_name(wp)) + { + protx=proto_with_name(wp); + //printf("PROTOCOL: %s\n",protx->name); + strcat(processed, Int_To_Hex(protx->deccode)); + firstorsecond=2;//Since the next word will be an address + } + else + { + printf("\nNo such protocol!\n\n"); + malf=1; + break; + } + } + else//This is the address + { + //printf("ADDRESS: %s\n",wp); + if(address_string_to_bytes(protx, wp,strlen(wp)) == "ERR") + { + malf = 1; + //printf("\n\nTRIGGERED!!!!!!!!!!!!!!!!!!!!!!!\n\n"); + } + else + { + strcat(processed,address_string_to_bytes(protx, wp,strlen(wp))); + //printf("Addressinbytes: %s\n",address_string_to_bytes(protx, wp,strlen(wp))); + } + protx=NULL;//Since right now it doesn't need that assignment anymore. + firstorsecond=1;//Since the next word will be an protocol + } + wp=strtok_r(NULL,"/",&end); + } + protx=NULL; + unload_protocols(); + //printf("Processed contains: %s \n",processed); + + if(malf==1) + { + return 0; + } + else + { + bzero(finalbytes,400); + //printf("XXX: %s\n",xxx); + memcpy(finalbytes, Hex_To_Var(processed), 400); + realbbsize[0] = 0; + for(int i=0;i<400;i++) + { + if(finalbytes[i]) + { + realbbsize[0]++; + } + } + return 1; + } +} + diff --git a/testing.c b/testing.c index 31c962c..25e5774 100644 --- a/testing.c +++ b/testing.c @@ -7,23 +7,26 @@ int main() bzero(addrstr,100); strcat(addrstr,"/ip4/192.168.1.1/tcp/8080/"); printf("INITIAL: %s\n",addrstr); - struct maddr a; + struct maddr* a; a=new_maddr_fs(addrstr); - printf("TEST BYTES: %s\n",Var_To_Hex(a.bsize[0], a.bytes)); + printf("TEST BYTES: %s\n",Var_To_Hex(a->bsize[0], 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/"); - printf("A STRING ENCAPSULATED:%s\n",a.string); + printf("A STRING:%s\n",a->string); + m_encapsulate(a,"/ip4/192.31.200.1/udp/3333/"); + printf("A STRING ENCAPSULATED:%s\n",a->string); - m_decapsulate(&a,"udp"); - printf("A STRING DECAPSULATED UDP:%s\n",a.string); + m_decapsulate(a,"udp"); + printf("A STRING DECAPSULATED UDP:%s\n",a->string); - m_encapsulate(&a,"/tcp/8080"); - printf("A STRING ENCAPSULATED TCP:%s\n",a.string); + m_encapsulate(a,"/tcp/8080"); + printf("A STRING ENCAPSULATED TCP:%s\n",a->string); - struct maddr beta; - beta=new_maddr_fb(a.bytes,a.bsize[0]); - printf("B STRING: %s\n",beta.string); + struct maddr* beta; + beta=new_maddr_fb(a->bytes,a->bsize[0]); + printf("B STRING: %s\n",beta->string); + + maddr_free(a); + maddr_free(beta); } diff --git a/varhexutils.c b/varhexutils.c new file mode 100644 index 0000000..68db6ed --- /dev/null +++ b/varhexutils.c @@ -0,0 +1,252 @@ +#ifndef VARHEXUTILS +#define VARHEXUTILS + +#include +#include +#include +#include +#include "multiaddr/varint.h" +#include "multiaddr/varhexutils.h" + +/*uint8_t * encode_big_endian_32(uint32_t ebex32) +{ + uint8_t encbe[10] = {0}; + memcpy(encbe, htobe32(ebex32)); + return encbe; +}*/ +int8_t Var_Bytes_Count(uint8_t * countbytesofthis) +{ + static int8_t xrzk_bytescnt = 0; + for(int8_t i=0; i<10; i++) + { + if(countbytesofthis[i] != 0) + { + xrzk_bytescnt++; + } + } + return xrzk_bytescnt; +} +uint8_t * Num_To_Varint_64(uint64_t TOV64INPUT) //UINT64_T TO VARINT +{ + static uint8_t buffy_001[60] = {0}; + uvarint_encode64(TOV64INPUT, buffy_001, 60); + return buffy_001; +} +uint8_t * Num_To_Varint_32(uint32_t TOV32INPUT) // UINT32_T TO VARINT +{ + static uint8_t buffy_032[60] = {0}; + uvarint_encode32(TOV32INPUT, buffy_032, 60); + return buffy_032; +} +uint64_t * Varint_To_Num_64(uint8_t TON64INPUT[60]) //VARINT TO UINT64_t +{ + static uint64_t varintdecode_001 = 0; + uvarint_decode64(TON64INPUT, 60, &varintdecode_001); + return &varintdecode_001; +} +uint32_t * Varint_To_Num_32(uint8_t TON32INPUT[60]) //VARINT TO UINT32_t +{ + static uint32_t varintdecode_032 = 0; + uvarint_decode32(TON32INPUT, 60, &varintdecode_032); + return &varintdecode_032; +} +// +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; +} +uint64_t Hex_To_Int(char * hax) +{ + char * hex = NULL; + hex=hax; + uint64_t val = 0; + while (*hex) + { + // get current character then increment + uint8_t byte = *hex++; + // transform hex character to the 4bit equivalent number, using the ascii table indexes + if (byte >= '0' && byte <= '9') byte = byte - '0'; + else if (byte >= 'a' && byte <='f') byte = byte - 'a' + 10; + else if (byte >= 'A' && byte <='F') byte = byte - 'A' + 10; + // shift 4 to make space for new digit, and add the 4 bits of the new digit + val = (val << 4) | (byte & 0xF); + } + return val; +} +// +void vthconvert(int size, char * crrz01, uint8_t * xbuf) +{ + uint8_t buf[400]; + bzero(buf,400); + + //fixing the buf + for(int cz=0; cz Date: Mon, 13 Feb 2017 13:30:22 -0500 Subject: [PATCH 2/5] renamed struct maddr to MultiAddress --- include/multiaddr/multiaddr.h | 12 ++++++------ multiaddr.c | 14 +++++++------- testing.c | 4 ++-- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/include/multiaddr/multiaddr.h b/include/multiaddr/multiaddr.h index 9b04a3b..72c5659 100644 --- a/include/multiaddr/multiaddr.h +++ b/include/multiaddr/multiaddr.h @@ -7,7 +7,7 @@ #include "protocols.h" #include "protoutils.h" -struct maddr +struct MultiAddress { char string[800]; uint8_t bytes[400]; @@ -17,14 +17,14 @@ struct maddr int strpos(char *haystack, char *needle); -struct maddr* new_maddr_fb(uint8_t * byteaddress,int size); //Construct new address from bytes +struct MultiAddress* new_maddr_fb(uint8_t * byteaddress,int size); //Construct new address from bytes -struct maddr* new_maddr_fs(char * straddress); //Construct new address from string +struct MultiAddress* new_maddr_fs(char * straddress); //Construct new address from string -void maddr_free(struct maddr* in); +void maddr_free(struct MultiAddress* in); -int m_encapsulate(struct maddr * result, char * string); +int m_encapsulate(struct MultiAddress * result, char * string); -int m_decapsulate(struct maddr * result, char * srci); +int m_decapsulate(struct MultiAddress * result, char * srci); #endif diff --git a/multiaddr.c b/multiaddr.c index a5798c2..4d27f17 100644 --- a/multiaddr.c +++ b/multiaddr.c @@ -18,9 +18,9 @@ int strpos(char *haystack, char *needle) } } -struct maddr* new_maddr_fb(uint8_t* byteaddress,int size)//Construct new address from bytes +struct MultiAddress* new_maddr_fb(uint8_t* byteaddress,int size)//Construct new address from bytes { - struct maddr* anewaddr2 = (struct maddr*)malloc(sizeof(struct maddr)); + struct MultiAddress* anewaddr2 = (struct MultiAddress*)malloc(sizeof(struct MultiAddress)); if (anewaddr2 != NULL) { if(byteaddress!=NULL) { @@ -33,9 +33,9 @@ struct maddr* new_maddr_fb(uint8_t* byteaddress,int size)//Construct new address } return anewaddr2; } -struct maddr* new_maddr_fs(char * straddress)//Construct new address from string +struct MultiAddress* new_maddr_fs(char * straddress)//Construct new address from string { - struct maddr* anewaddr = (struct maddr*)malloc(sizeof(struct maddr)); + struct MultiAddress* anewaddr = (struct MultiAddress*)malloc(sizeof(struct MultiAddress)); if (anewaddr != NULL) { bzero(anewaddr->string, 800); strcpy(anewaddr->string, straddress); @@ -54,11 +54,11 @@ struct maddr* new_maddr_fs(char * straddress)//Construct new address from string return anewaddr; } -void maddr_free(struct maddr* in) { +void maddr_free(struct MultiAddress* in) { free(in); } -int m_encapsulate(struct maddr * result, char * string) +int m_encapsulate(struct MultiAddress * result, char * string) { if(result!=NULL&&string!=NULL) { @@ -82,7 +82,7 @@ int m_encapsulate(struct maddr * result, char * string) return 0; } } -int m_decapsulate(struct maddr * result, char * srci) +int m_decapsulate(struct MultiAddress * result, char * srci) { if(result!=NULL && srci!=NULL) { diff --git a/testing.c b/testing.c index 25e5774..40b9112 100644 --- a/testing.c +++ b/testing.c @@ -7,7 +7,7 @@ int main() bzero(addrstr,100); strcat(addrstr,"/ip4/192.168.1.1/tcp/8080/"); printf("INITIAL: %s\n",addrstr); - struct maddr* a; + struct MultiAddress* a; a=new_maddr_fs(addrstr); printf("TEST BYTES: %s\n",Var_To_Hex(a->bsize[0], a->bytes)); @@ -23,7 +23,7 @@ int main() m_encapsulate(a,"/tcp/8080"); printf("A STRING ENCAPSULATED TCP:%s\n",a->string); - struct maddr* beta; + struct MultiAddress* beta; beta=new_maddr_fb(a->bytes,a->bsize[0]); printf("B STRING: %s\n",beta->string); From bb488f7e820d650c1606aaccbe5e92eb02bec32b Mon Sep 17 00:00:00 2001 From: John Jones Date: Mon, 13 Feb 2017 14:12:32 -0500 Subject: [PATCH 3/5] 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"; From cc8ff45cc13220b7c8897d3eb9f1cb161fd0ff25 Mon Sep 17 00:00:00 2001 From: John Jones Date: Mon, 13 Feb 2017 14:28:06 -0500 Subject: [PATCH 4/5] Added a method to copy MultiAddresses --- include/multiaddr/multiaddr.h | 2 ++ multiaddr.c | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/include/multiaddr/multiaddr.h b/include/multiaddr/multiaddr.h index 9c00544..04dee0d 100644 --- a/include/multiaddr/multiaddr.h +++ b/include/multiaddr/multiaddr.h @@ -47,6 +47,8 @@ struct MultiAddress* multiaddress_new_from_string(const char* straddress); //Con void multiaddress_free(struct MultiAddress* in); +int multiaddress_copy(const struct MultiAddress* source, struct MultiAddress* destination); + int multiaddress_encapsulate(struct MultiAddress * result, char * string); int multiaddress_decapsulate(struct MultiAddress * result, char * srci); diff --git a/multiaddr.c b/multiaddr.c index bf861a6..463e113 100644 --- a/multiaddr.c +++ b/multiaddr.c @@ -91,6 +91,31 @@ void multiaddress_free(struct MultiAddress* in) { } } +/** + * Copy a multiaddress from one memory location to another + * @param in the source + * @param out the destination. NOTE: memory for out should be preallocated + * @returns true(1) on success, otherwise false(0) + */ +int multiaddress_copy(const struct MultiAddress* in, struct MultiAddress* out) { + if (in != NULL && out != NULL) { + // memory allocation + out->bytes = malloc(in->bsize); + if (out->bytes != NULL) { + out->string = malloc(strlen(in->string) + 1); + if (out->string != NULL) { + // copy + out->bsize = in->bsize; + memcpy(out->bytes, in->bytes, out->bsize); + strcpy(out->string, in->string); + return 1; + } // string allocated + free(out->bytes); + } // bytes allocated + } // good parameters + return 0; +} + /** * Put a string into the MultiAddress and recalculate the bytes * @param result the struct From 9a9c5ea1c324e2aaeaba06fc17f47e929e550d71 Mon Sep 17 00:00:00 2001 From: John Jones Date: Mon, 13 Feb 2017 17:49:20 -0500 Subject: [PATCH 5/5] More cleanup --- Makefile | 3 +++ protoutils.c | 20 +++++++++++++------- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 85f726c..f85000d 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,9 @@ CC = gcc CFLAGS = -O0 -I include +DEBUG = true + + ifdef DEBUG CFLAGS += -g3 endif diff --git a/protoutils.c b/protoutils.c index 3a4b79f..217ae3f 100644 --- a/protoutils.c +++ b/protoutils.c @@ -636,6 +636,14 @@ char * address_string_to_bytes(struct protocol * xx, const char * abc,size_t get } } } + +/** + * convert a string address into bytes + * @param finalbytes the destination + * @param realbbsize the ultimate size of the destination + * @param strx the incoming string + * @param strsize the string length + */ int string_to_bytes(uint8_t * finalbytes, size_t* realbbsize, char * strx, size_t strsize) { if(strx[0] != '/') @@ -643,13 +651,11 @@ int string_to_bytes(uint8_t * finalbytes, size_t* realbbsize, char * strx, size_ printf("Error, must start with '/'\n"); return 0; } - char xxx[800]; - bzero(xxx,800); //Getting total words int totalwords = 0; - char * totp; - char totalwordstest[strsize]; - bzero(totalwordstest,strsize); + char* totp; + char totalwordstest[strsize + 1]; + bzero(totalwordstest,strsize + 1); strcat(totalwordstest, strx); totp = strtok(totalwordstest, "/"); while(totp != NULL) @@ -664,8 +670,8 @@ int string_to_bytes(uint8_t * finalbytes, size_t* realbbsize, char * strx, size_ //Now Setting up variables for calculating which is the first //and second word: int firstorsecond = 1; //1=Protocol && 2 = Address - char pstring[800];//We do not want to harm the initial string. - bzero(pstring,800); + char pstring[strsize + 1];//We do not want to harm the initial string. + bzero(pstring,strsize + 1); strcat(pstring,strx); //Starting to extract words and process them: char * wp;