From 483957077e212aea9ac97a30bb96cdb96efc0f0d Mon Sep 17 00:00:00 2001 From: xethyrion Date: Tue, 8 Nov 2016 13:02:45 +0200 Subject: [PATCH] Update --- multiaddr.h | 29 ++++--- protocols.c | 50 +++++++++++ protoutils.h | 234 ++++++++++++++++++++++++-------------------------- varhexutils.h | 205 ++++++++++++++++++++++++------------------- 4 files changed, 296 insertions(+), 222 deletions(-) create mode 100644 protocols.c diff --git a/multiaddr.h b/multiaddr.h index aa6278c..d0e0b64 100644 --- a/multiaddr.h +++ b/multiaddr.h @@ -20,27 +20,36 @@ int strpos(char *haystack, char *needle) } struct maddr { - char string[100]; + char string[200]; uint8_t bytes[100]; + int bsize[1]; }; struct maddr new_maddr_fb(uint8_t * byteaddress,int size)//Construct new address from bytes { - struct maddr anewaddr; + struct maddr anewaddr2; if(byteaddress!=NULL) { - memcpy(anewaddr.bytes, byteaddress,size); - if(bytes_to_string(anewaddr.string,byteaddress,size)==1) + memcpy(anewaddr2.bytes, byteaddress,size); + if(bytes_to_string(anewaddr2.string,byteaddress,size)==1) { - return anewaddr; + return anewaddr2; } } } struct maddr new_maddr_fs(char * straddress)//Construct new address from string { struct maddr anewaddr; + bzero(anewaddr.string, 100); strcpy(anewaddr.string, straddress); - if(string_to_bytes(anewaddr.bytes,anewaddr.string,sizeof(anewaddr.string))==1) + 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++) + { + anewaddr.bytes[betta] = '\0'; + } return anewaddr; } } @@ -49,13 +58,11 @@ int m_encapsulate(struct maddr * result, char * string) if(result!=NULL&&string!=NULL) { int success = 0; - char pstr[50]; - bzero(pstr,50); + char pstr[100]; strcpy(pstr,result->string); strcat(pstr,string+1); - if(string_to_bytes(result->bytes,pstr,sizeof(pstr))) + if(string_to_bytes(result->bytes,result->bsize,pstr,sizeof(pstr))) { - printf("RESULT WOULD BE: %s\n", pstr); strcpy(result->string,pstr); return 1; } @@ -101,7 +108,6 @@ int m_decapsulate(struct maddr * result, char * srci) procstr[i] = '/'; } } - strcpy(result->string,procstr); return 1; } else @@ -115,4 +121,5 @@ int m_decapsulate(struct maddr * result, char * srci) return 0; } } + #endif \ No newline at end of file diff --git a/protocols.c b/protocols.c new file mode 100644 index 0000000..1ad1968 --- /dev/null +++ b/protocols.c @@ -0,0 +1,50 @@ +#include "protocols.h" +#include "codecs.h" +int main() //This won't exist, it's here for my own testing purposes. +{ + 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++) + { + 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);}; + } + 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 diff --git a/protoutils.h b/protoutils.h index 7b78831..02d6864 100644 --- a/protoutils.h +++ b/protoutils.h @@ -247,6 +247,7 @@ char * int2ip(int inputintip) { uint32_t ipint = inputintip; static char xxx_int2ip_result[16] = "\0"; + bzero(xxx_int2ip_result,16); uint32_t ipint0 = (ipint >> 8*3) % 256; uint32_t ipint1 = (ipint >> 8*2) % 256; uint32_t ipint2 = (ipint >> 8*1) % 256; @@ -255,112 +256,100 @@ 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 * result, uint8_t * frombuf, size_t weesize) +int bytes_to_string(char * resultzx, uint8_t * catx,int xbsize) { - char removedlines[weesize*2]; - bzero(removedlines,sizeof(removedlines)); - strcpy(removedlines, Var_To_Hex(frombuf)); - //printf("Bytes to Hex: %s\n",removedlines); + bzero(resultzx,200); + uint8_t * bytes = NULL; + int size = 0; + size = xbsize; load_protocols(); - int thelastpos=0; - for(int i=0;isize/4)) - { - printf("%d -- %d\n", addrsizesofar, (bprotox->size/4)); - the_wanted_address[zxp]='\0'; - thelastpos=zx; - printf("THELASTPOS: %d ",thelastpos); - break; - } - else if(addrsizesofar>(bprotox->size/4)) - { - printf("MALFORMED STRING!!!\n"); - return 0; - } - - } - //printf("RL[%d]: %c\n",zx,removedlines[zx]); - //printf("RL[%d]: %c\n",zx,removedlines[zx+1]); - } - addrsizesofar++; - the_wanted_address[zxp] = removedlines[zx]; - zxp++; - } - - printf("And the address:%s\n", the_wanted_address); - printf("With size: %d\n", addrsizesofar); - //printf("%d < %lu\n", i,sizeof(removedlines)); - strcat(result, "/"); - strcat(result,bprotox->name); - strcat(result, "/"); - if(strcmp(bprotox->name,"ip4")==0) - { - strcat(result,int2ip(Hex_To_Int(the_wanted_address))); - } - else if(strcmp(bprotox->name,"tcp")==0) - { - char a[5]; - sprintf(a,"%lu",Hex_To_Int(the_wanted_address)); - strcat(result,a); - } - else if(strcmp(bprotox->name,"udp")==0) - { - char a[5]; - sprintf(a,"%lu",Hex_To_Int(the_wanted_address)); - strcat(result,a); - } - else - { - return 0; - } - } - i++; + 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)); + 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(lastposdeccode; switch(code) @@ -368,6 +357,7 @@ char * address_string_to_bytes(struct protocol * xx, char * abc,size_t getsznow) case 4://IPv4 { char testip[16] = "\0"; + bzero(testip,16); strcpy(testip,abc); if(is_valid_ipv4(testip)==1) { @@ -483,7 +473,7 @@ char * address_string_to_bytes(struct protocol * xx, char * abc,size_t getsznow) } case 42://IPFS - !!! { - //abc=address + break; } case 480://http @@ -519,14 +509,15 @@ char * address_string_to_bytes(struct protocol * xx, char * abc,size_t getsznow) } } } -int string_to_bytes(uint8_t * finalbytes,char * strx, size_t strsize) +int string_to_bytes(uint8_t * finalbytes,int * realbbsize,char * strx, size_t strsize) { - static char xxx[40] = "\0"; - bzero(xxx,40); + static char xxx[200] = "\0"; + bzero(xxx,200); int sigmalf = 0; char * totpch; char totalwordstest[strsize]; - strcpy(totalwordstest, strx); + bzero(totalwordstest,strsize); + strcat(totalwordstest, strx); int totalwords = 0; totpch = strtok(totalwordstest, "/"); while(totpch != NULL) @@ -535,8 +526,10 @@ int string_to_bytes(uint8_t * finalbytes,char * strx, size_t strsize) totalwords++; } int processedwords = 0; - char processedsofar[500] = "\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] == '/') @@ -557,10 +550,12 @@ int string_to_bytes(uint8_t * finalbytes,char * strx, size_t strsize) struct protocol * protx; //printf("PCH-P:%s\n",pch); protx = proto_with_name(pch); - char cut[30]="\0"; + char cut[3]="\0"; + bzero(cut,3); strcat(cut,Int_To_Hex(protx->deccode)); cut[2] = '\0'; - char finipbit[2] = "\0"; + char finipbit[3] = "\0"; + bzero(finipbit,3); finipbit[0] = cut[0]; finipbit[1] = cut[1]; finipbit[2] = '\0'; @@ -570,7 +565,8 @@ int string_to_bytes(uint8_t * finalbytes,char * strx, size_t strsize) strcat(processedsofar, pch); //printf("PCH-A:%s\n",pch); char addr[60] = "\0"; - strcpy(addr, pch); + bzero(addr,60); + strcat(addr, pch); //If both are ok: strcat(xxx,finipbit); if(address_string_to_bytes(protx, addr,sizeof(addr))) @@ -610,30 +606,20 @@ int string_to_bytes(uint8_t * finalbytes,char * strx, size_t strsize) { //printf("S2B_RESULT: %s\n", xxx); //static uint8_t finalbytes[100] = {0}; - for(int i=0; i<100; i++) - { - finalbytes[i] = 0; - } + bzero(finalbytes,100); + //printf("XXX: %s\n",xxx); memcpy(finalbytes, Hex_To_Var(xxx), 100); - int xtotbytes = 0; - for(int i=0; i<100; i++) + realbbsize[0] = 0; + for(int i=0;i<100;i++) { - if(finalbytes[i] != 0) + if(finalbytes[i]) { - xtotbytes++; + realbbsize[0]++; } } - //printf("TOT BYTES: %d\n", xtotbytes); - uint8_t finalproc[sizeof(finalbytes)/sizeof(finalbytes)[0]]; - for(int i=0;i= '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; + // 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; } // -char * Var_To_Hex(uint8_t * TOHEXINPUT) //VAR[binformat] TO HEX +void vthconvert(int size, char * crrz01, uint8_t * xbuf) { - if(TOHEXINPUT != NULL) + uint8_t buf[100]; + bzero(buf,100); + + //fixing the buf + for(int cz=0; cz