From 5bc0d88bf1a8d18cf504a207717a1794bb46be8c Mon Sep 17 00:00:00 2001 From: xethyrion Date: Tue, 8 Nov 2016 18:01:10 +0200 Subject: [PATCH] Added more space for proxies Fixed a bug in protoutils.h->string_to_bytes where an extra NULL caused memory errors. --- multiaddr.h | 11 +-- protoutils.h | 183 ++++++++++++++++++++++---------------------------- varhexutils.h | 58 ++++++++-------- 3 files changed, 114 insertions(+), 138 deletions(-) diff --git a/multiaddr.h b/multiaddr.h index d0e0b64..3380194 100644 --- a/multiaddr.h +++ b/multiaddr.h @@ -20,8 +20,8 @@ int strpos(char *haystack, char *needle) } struct maddr { - char string[200]; - uint8_t bytes[100]; + 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 @@ -39,14 +39,14 @@ struct maddr new_maddr_fb(uint8_t * byteaddress,int size)//Construct new address struct maddr new_maddr_fs(char * straddress)//Construct new address from string { struct maddr anewaddr; - bzero(anewaddr.string, 100); + 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<100;betta++) + for(betta=anewaddr.bsize[0];betta<400;betta++) { anewaddr.bytes[betta] = '\0'; } @@ -58,7 +58,8 @@ int m_encapsulate(struct maddr * result, char * string) if(result!=NULL&&string!=NULL) { int success = 0; - char pstr[100]; + 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))) diff --git a/protoutils.h b/protoutils.h index 02d6864..ae026db 100644 --- a/protoutils.h +++ b/protoutils.h @@ -258,7 +258,7 @@ 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) { - bzero(resultzx,200); + bzero(resultzx,800); uint8_t * bytes = NULL; int size = 0; size = xbsize; @@ -348,8 +348,8 @@ 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[200] = "\0"; - bzero(astb__stringy,200); + static char astb__stringy[800] = "\0"; + bzero(astb__stringy,800); int code = 0; code = xx->deccode; switch(code) @@ -381,8 +381,8 @@ char * address_string_to_bytes(struct protocol * xx, char * abc,size_t getsznow) { if(atoi(abc)<65536&&atoi(abc)>0) { - static char himm_woot[4] = "\0"; - bzero(himm_woot, 4); + 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 @@ -406,6 +406,7 @@ char * address_string_to_bytes(struct protocol * xx, char * abc,size_t getsznow) himm_woot[2] = swap2; himm_woot[3] = swap3; } + himm_woot[4]='\0'; return himm_woot; } else @@ -418,8 +419,8 @@ char * address_string_to_bytes(struct protocol * xx, char * abc,size_t getsznow) { if(atoi(abc)<65536&&atoi(abc)>0) { - static char himm_woot2[4] = "\0"; - bzero(himm_woot2, 4); + 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 @@ -443,6 +444,7 @@ char * address_string_to_bytes(struct protocol * xx, char * abc,size_t getsznow) himm_woot2[2] = swap2; himm_woot2[3] = swap3; } + himm_woot2[4]='\0'; return himm_woot2; } else @@ -474,6 +476,7 @@ char * address_string_to_bytes(struct protocol * xx, char * abc,size_t getsznow) case 42://IPFS - !!! { + return "ERR"; break; } case 480://http @@ -511,127 +514,99 @@ 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) { - static char xxx[200] = "\0"; - bzero(xxx,200); - int sigmalf = 0; - char * totpch; + 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); - int totalwords = 0; - totpch = strtok(totalwordstest, "/"); - while(totpch != NULL) + totp = strtok(totalwordstest, "/"); + while(totp != NULL) { - totpch = strtok (NULL, "/"); + totp = strtok (NULL, "/"); totalwords++; } - int processedwords = 0; - char processedsofar[100]; - bzero(processedsofar,100); - char str[strsize]; //This string will be bad later. - bzero(str,strsize); - nextproc: - strcpy(str,strx); - if(str[0] == '/') + //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) { - char * pch; - pch = strtok (str,"/"); - load_protocols(); - for(int ax=0;axdeccode)); - cut[2] = '\0'; - char finipbit[3] = "\0"; - bzero(finipbit,3); - finipbit[0] = cut[0]; - finipbit[1] = cut[1]; - finipbit[2] = '\0'; - //Address - pch = strtok (NULL, "/"); // Move to supposed address - strcat(processedsofar, "/"); - strcat(processedsofar, pch); - //printf("PCH-A:%s\n",pch); - char addr[60] = "\0"; - bzero(addr,60); - strcat(addr, pch); - //If both are ok: - strcat(xxx,finipbit); - if(address_string_to_bytes(protx, addr,sizeof(addr))) - { - if(address_string_to_bytes(protx, addr,sizeof(addr)) == "ERR") - { - sigmalf = 1; - } - else - { - strcat(xxx,address_string_to_bytes(protx, addr,sizeof(addr))); - } - } - else - { - sigmalf = 1; - } - pch = strtok (NULL, "/"); // Next step. - processedwords++; - processedwords++; - + 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 { - //This only gets triggered if the string has a wrong format - //Or unknown protocol! - printf("ERROR: MALFORMED STRING\n"); - return 0; + printf("\nNo such protocol!\n\n"); + malf=1; + break; } } - if(processedwords != totalwords) + else//This is the address { - goto nextproc; + //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 } - unload_protocols(); - if(sigmalf != 1) - { - //printf("S2B_RESULT: %s\n", xxx); - //static uint8_t finalbytes[100] = {0}; - bzero(finalbytes,100); + 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(xxx), 100); + memcpy(finalbytes, Hex_To_Var(processed), 400); realbbsize[0] = 0; - for(int i=0;i<100;i++) + for(int i=0;i<400;i++) { if(finalbytes[i]) { realbbsize[0]++; } } - //printf("FB2XXX: %s\nWith size: %u\n",Var_To_Hex(finalbytes), realbbsize[0]); - //realbsize = realsize; - //printf("REALbBSZIE: %d", realbbsize[0]); - return 1;//success - } - else - { - printf("ERROR, MALFORMED STRING!\n"); - return 0; - } - } - else - { - printf("ERROR, Multiaddr needs to start with '/'\n"); - return 0; + return 1; } } #endif diff --git a/varhexutils.h b/varhexutils.h index 7331656..18a08ec 100644 --- a/varhexutils.h +++ b/varhexutils.h @@ -52,7 +52,7 @@ uint32_t * Varint_To_Num_32(uint8_t TON32INPUT[60]) //VARINT TO UINT32_t // char * Int_To_Hex(uint64_t int2hex) //VAR[binformat] TO HEX { - static char int2hex_result[200]="\0"; + static char int2hex_result[800]="\0"; memset(int2hex_result,0,sizeof(int2hex_result)); sprintf (int2hex_result, "%02lX", int2hex); return int2hex_result; @@ -78,8 +78,8 @@ uint64_t Hex_To_Int(char * hax) // void vthconvert(int size, char * crrz01, uint8_t * xbuf) { - uint8_t buf[100]; - bzero(buf,100); + uint8_t buf[400]; + bzero(buf,400); //fixing the buf for(int cz=0; cz