Multiple fixes to code

This commit is contained in:
John Jones 2017-02-15 10:42:06 -05:00
parent 9a9c5ea1c3
commit 29a68c8f55
8 changed files with 157 additions and 134 deletions

View file

@ -8,28 +8,33 @@
#include <errno.h>
#include "varhexutils.h"
struct protocol
struct Protocol
{
char hexcode[21];
//char hexcode[21];
int deccode;
int size;
char name[30];
};
struct protocol *protocol_P; //Pointer for holding multiple structures
struct ProtocolListItem {
struct Protocol* current;
struct ProtocolListItem* next;
};
int protocol_REMOVE_id(int remid); //Function to remove & shift back all data, sort of like c++ vectors.
int protocol_REMOVE_id(struct ProtocolListItem* head, int remid); //Function to remove & shift back all data, sort of like c++ vectors.
void unload_protocols();
void unload_protocols(struct ProtocolListItem* head);
void load_protocols();
/**
* load the available protocols into the global protocol_P
* @returns True(1) on success, otherwise 0
*/
int load_protocols(struct ProtocolListItem** head);
struct protocol * proto_with_name(char proto_w_name[]); //Search for protocol with inputted name
struct Protocol * proto_with_name(const struct ProtocolListItem* head, const char* proto_w_name); //Search for protocol with inputted name
struct protocol * proto_with_deccode(int proto_w_deccode); //Search for protocol with inputted deccode
struct Protocol * proto_with_deccode(const struct ProtocolListItem* head, int proto_w_deccode); //Search for protocol with inputted deccode
void pp(); //Purely for debugging purposes, prints the entire loaded protocols.
void protocols_with_string(char * meee,int sizi); // NOT FINISHED, DO NOT USE!
void protocols_with_string(const struct ProtocolListItem* head, char * meee, int sizi); // NOT FINISHED, DO NOT USE!
#endif

View file

@ -32,10 +32,10 @@ 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, const 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, const 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,size_t* realbbsize,char * strx, size_t strsize);
int string_to_bytes(uint8_t** finalbytes,size_t* realbbsize, const char * strx, size_t strsize);
#endif

View file

@ -33,7 +33,13 @@ void vthconvert(int size, char * crrz01, uint8_t * xbuf);
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]
/**
* Turn a hex string into a byte array
* @param incoming a string of hex values
* @param num_bytes the size of the result
* @returns a pointer to the converted value
*/
unsigned char* Hex_To_Var(char * Hexstr, size_t* num_bytes);
//
void convert(char * convert_result, uint8_t * buf); //Both of them read them properly.

View file

@ -50,7 +50,7 @@ struct MultiAddress* multiaddress_new_from_bytes(const uint8_t* byteaddress, int
return NULL;
}
memcpy(out->bytes, byteaddress, size);
if(!bytes_to_string(out->string,byteaddress,size)==1)
if(!bytes_to_string(&out->string,byteaddress,size)==1)
{
multiaddress_free(out);
return NULL;
@ -64,14 +64,14 @@ struct MultiAddress* multiaddress_new_from_string(const char* straddress)//Const
{
struct MultiAddress* out = multiaddress_new();
if (out != NULL) {
out->string = malloc(sizeof(straddress) + 1);
out->string = malloc(strlen(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 )
if (string_to_bytes(&out->bytes, &out->bsize, out->string, strlen(out->string)) == 0 )
{
multiaddress_free(out);
return NULL;
@ -138,7 +138,7 @@ int multiaddress_encapsulate(struct MultiAddress* result, char* string)
return 0;
}
strcpy(result->string, string);
if(string_to_bytes(result->bytes, &result->bsize, result->string, sizeof(result->string)) == 0)
if(string_to_bytes(&result->bytes, &result->bsize, result->string, sizeof(result->string)) == 0)
{
multiaddress_free(result);
return 0;

View file

@ -7,23 +7,20 @@
#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.
/*
int protocol_REMOVE_id(struct ProtocolListItem* protocol_P, 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; i<CNT_PROTOCOLNUM-1; ++i) //While i < num of registered protocols //Needs to be tested that -1 is for valgrind debugging
{
strcpy((protocol_P+i)->hexcode, (protocol_P+i+1)->hexcode); //shift memory to the user we want to remove.
//strcpy((protocol_P+i)->hexcode, (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));
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/
@ -41,13 +38,52 @@ int protocol_REMOVE_id(int remid)//Function to remove & shift back all data, sor
return 0;
}
}
void unload_protocols()
*/
void unload_protocols(struct ProtocolListItem* head)
{
free(protocol_P);
CNT_PROTOCOLNUM=0;
struct ProtocolListItem* current = head;
while (current != NULL) {
struct ProtocolListItem* next = current->next;
free(current->current);
free(current);
current = next;
}
}
void load_protocols()
/**
* load the available protocols into the global protocol_P
* @returns True(1) on success, otherwise 0
*/
int load_protocols(struct ProtocolListItem** head)
{
unload_protocols(*head);
int num_protocols = 14;
int dec_code[] = {4, 41, 6, 17, 33, 132, 301, 302, 42, 480, 443, 477, 444, 275};
int size[] = {32, 128, 16, 16, 16, 16, 0, 0, -1, 0, 0, 0, 10, 0 };
char* name[] = { "ip4", "ip6", "tcp", "udp", "dccp", "sctp", "udt", "utp", "ipfs", "http", "https", "ws", "onion", "libp2p-webrtc-star" };
struct ProtocolListItem* last = NULL;
for(int i = 0; i < num_protocols; i++) {
struct ProtocolListItem* current_item = (struct ProtocolListItem*)malloc(sizeof(struct ProtocolListItem));
current_item->current = (struct Protocol*)malloc(sizeof(struct Protocol));
current_item->next = NULL;
current_item->current->deccode = dec_code[i];
strcpy(current_item->current->name, name[i]);
current_item->current->size = size[i];
if (*head == NULL) {
*head = current_item;
} else {
last->next = current_item;
}
last = current_item;
}
return 1;
}
/*
void load_protocols_from_file(struct Protocol** in)
{
struct Protocol* protocol_P = *in;
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.
@ -62,13 +98,13 @@ void load_protocols()
//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));
protocol_P = (struct Protocol*) malloc (sizeof(struct Protocol));
}
else //Reallocate memory to fit one more protocol
else //Reallocate memory to fit one more Protocol
{
protocol_P = (struct protocol*) realloc(protocol_P, (CNT_PROTOCOLNUM+1) * sizeof(struct 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
//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
@ -96,53 +132,42 @@ void load_protocols()
}
}
fclose(FPROC_POINT);
protocol_REMOVE_id(0);
protocol_REMOVE_id(protocol_P, 0);
}
else
{
perror("Fatal Error:");
}
}
struct protocol * proto_with_name(char proto_w_name[]) //Search for protocol with inputted name
{
*/
for(int i=0; i<CNT_PROTOCOLNUM; i++)
struct Protocol* proto_with_name(const struct ProtocolListItem* head, const char* proto_w_name) //Search for Protocol with inputted name
{
const struct ProtocolListItem* current = head;
while(current != NULL)
{
if(strcmp(proto_w_name, (protocol_P+i)->name) == 0)
{
return (protocol_P+i);
if (strcmp(proto_w_name, current->current->name) == 0) {
return current->current;
}
current = current->next;
}
return NULL;
}
struct protocol * proto_with_deccode(int proto_w_deccode) //Search for protocol with inputted deccode
struct Protocol* proto_with_deccode(const struct ProtocolListItem* head, int proto_w_deccode) //Search for Protocol with inputted deccode
{
for(int i=0; i<CNT_PROTOCOLNUM; i++)
const struct ProtocolListItem* current = head;
while(current != NULL)
{
if((protocol_P+i)->deccode == proto_w_deccode)
{
return (protocol_P+i);
if (current->current->deccode == proto_w_deccode) {
return current->current;
}
current = current->next;
}
return NULL;
}
void pp() //Purely for debugging purposes, prints the entire loaded protocols.
{
for(int i=0;i<CNT_PROTOCOLNUM;i++)
{
if(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!
void protocols_with_string(const struct ProtocolListItem* head, char* meee, int sizi) // NOT FINISHED, DO NOT USE!
{
int finalsize = 0;

View file

@ -291,13 +291,16 @@ char * int2ip(int inputintip)
return xxx_int2ip_result;
}
//I didn't feel another address_bytes_to_string was necesarry sry guys
int bytes_to_string(char * resultzx, const uint8_t* catx,int xbsize)
int bytes_to_string(char** buffer, const uint8_t* catx, int xbsize)
{
bzero(resultzx,800);
*buffer = malloc(800);
char* resultzx = *buffer;
bzero(resultzx, 800);
uint8_t * bytes = NULL;
int size = 0;
size = xbsize;
load_protocols();
struct ProtocolListItem* head = NULL;
load_protocols(&head);
char hex[xbsize*2];
bzero(hex,xbsize*2);
strcat(hex,Var_To_Hex(size, catx));
@ -309,21 +312,17 @@ int bytes_to_string(char * resultzx, const uint8_t* catx,int xbsize)
//Stage 1 ID:
if(lastpos!=0)
{
lastpos+1;
lastpos++;
}
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)))
if(proto_with_deccode(head, Hex_To_Int(pid)))
{
//////////Stage 2: Address
struct protocol * PID;
struct Protocol * PID;
PID = NULL;
PID = proto_with_deccode(Hex_To_Int(pid));
PID = proto_with_deccode(head, Hex_To_Int(pid));
if(strcmp(PID->name,"ipfs")!=0)
{
lastpos = lastpos+2;
@ -403,9 +402,8 @@ int bytes_to_string(char * resultzx, const uint8_t* catx,int xbsize)
//printf("\nIPFS_ADDR[%d] = %c\n\n",IPFS_PARSE,hex[i]);
IPFS_PARSE++;
}
unsigned char addrbuf[strlen(IPFS_ADDR)/2];
bzero(addrbuf,strlen(IPFS_ADDR)/2);
memcpy(addrbuf,Hex_To_Var(IPFS_ADDR),sizeof(addrbuf));
size_t num_bytes = 0;
unsigned char* addrbuf = Hex_To_Var(IPFS_ADDR, &num_bytes);
size_t rezbuflen = strlen(IPFS_ADDR);
unsigned char rezultat[rezbuflen];
bzero(rezultat,rezbuflen);
@ -413,9 +411,11 @@ int bytes_to_string(char * resultzx, const uint8_t* catx,int xbsize)
pointyaddr = rezultat;
int returnstatus = 0;
returnstatus = multiaddr_encoding_base58_encode(addrbuf, sizeof(addrbuf), &pointyaddr, &rezbuflen);
free(addrbuf);
if(returnstatus == 0)
{
printf("\nERROR!!!!!\n");
unload_protocols(head);
return 0;
}
strcat(resultzx, "/");
@ -425,12 +425,13 @@ int bytes_to_string(char * resultzx, const uint8_t* catx,int xbsize)
}
}
strcat(resultzx, "/");
unload_protocols();
unload_protocols(head);
return 1;
}
//
char * address_string_to_bytes(struct protocol * xx, const char * abc,size_t getsznow)
char * address_string_to_bytes(struct Protocol * xx, const char * abc,size_t getsznow)
{
static char astb__stringy[800] = "\0";
bzero(astb__stringy,800);
@ -644,48 +645,43 @@ char * address_string_to_bytes(struct protocol * xx, const char * abc,size_t get
* @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)
int string_to_bytes(uint8_t** finalbytes, size_t* realbbsize, const char* strx, size_t strsize)
{
if(strx[0] != '/')
{
printf("Error, must start with '/'\n");
return 0;
}
//Getting total words
int totalwords = 0;
char* totp;
char totalwordstest[strsize + 1];
bzero(totalwordstest,strsize + 1);
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[strsize + 1];//We do not want to harm the initial string.
bzero(pstring,strsize + 1);
strcat(pstring,strx);
// copy input so as to not harm it
char pstring[strsize + 1];
strcpy(pstring,strx);
// load up the list of protocols
struct ProtocolListItem* head = NULL;
load_protocols(&head);
//Starting to extract words and process them:
char * wp;
char * end;
wp=strtok_r(pstring,"/",&end);
load_protocols();
struct protocol * protx;
struct Protocol * protx;
while(wp)
{
if(firstorsecond==1)//This is the Protocol
{
if(proto_with_name(wp))
if(proto_with_name(head, wp))
{
protx=proto_with_name(wp);
protx=proto_with_name(head, wp);
//printf("PROTOCOL: %s\n",protx->name);
strcat(processed, Int_To_Hex(protx->deccode));
firstorsecond=2;//Since the next word will be an address
@ -716,8 +712,7 @@ int string_to_bytes(uint8_t * finalbytes, size_t* realbbsize, char * strx, size_
wp=strtok_r(NULL,"/",&end);
}
protx=NULL;
unload_protocols();
//printf("Processed contains: %s \n",processed);
unload_protocols(head);
if(malf==1)
{
@ -725,17 +720,7 @@ int string_to_bytes(uint8_t * finalbytes, size_t* realbbsize, char * strx, size_
}
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]++;
}
}
*finalbytes = Hex_To_Var(processed, realbbsize);
return 1;
}
}

View file

@ -4,8 +4,7 @@
int main()
{
char addrstr[100];
bzero(addrstr,100);
strcat(addrstr,"/ip4/192.168.1.1/tcp/8080/");
strcpy(addrstr,"/ip4/192.168.1.1/tcp/8080/");
printf("INITIAL: %s\n",addrstr);
struct MultiAddress* a;
a= multiaddress_new_from_string(addrstr);
@ -14,7 +13,7 @@ int main()
//Remember, Decapsulation happens from right to left, never in reverse!
printf("A STRING:%s\n",a->string);
multiaddress_encapsulate(a,"/ip4/192.31.200.1/udp/3333/");
multiaddress_encapsulate(a,"/ip4/192.131.200.111/udp/3333/");
printf("A STRING ENCAPSULATED:%s\n",a->string);
multiaddress_decapsulate(a,"udp");
@ -24,7 +23,7 @@ int main()
printf("A STRING ENCAPSULATED TCP:%s\n",a->string);
struct MultiAddress* beta;
beta=multiaddress_new_from_bytes(a->bytes,a->bsize);
beta = multiaddress_new_from_bytes(a->bytes,a->bsize);
printf("B STRING: %s\n",beta->string);
multiaddress_free(a);

View file

@ -116,32 +116,35 @@ char * Var_To_Hex(int realsize, const uint8_t * TOHEXINPUT) //VAR[binformat] TO
return convert_resultz1;
}
}
uint8_t * Hex_To_Var(char * Hexstr) //HEX TO VAR[BINFORMAT]
/**
* Turn a hex string into a byte array
* @param incoming a string of hex values
* @param num_bytes the size of the result
* @returns a pointer to the converted value
*/
unsigned char* Hex_To_Var(const char* incoming, size_t* num_bytes) //HEX TO VAR[BINFORMAT]
{
static uint8_t buffy_HEX[400] = {0};
bzero(buffy_HEX,400);
int i;
char codo[800] = "\0";
bzero(codo,800);
strcpy(codo, Hexstr);
// the return value
unsigned char* retVal = NULL;
int incoming_size = strlen(incoming);
*num_bytes = incoming_size / 2;
retVal = (unsigned char*)malloc(*num_bytes);
char code[3];
bzero(code,3);
code[3]='\0';
int x = 0;
int fori001=0;
for(fori001=0;fori001<800;fori001++)
int i=0;
for(i=0; i < incoming_size; i += 2)
{
strncpy(&code[0],&codo[fori001],1);
strncpy(&code[1],&codo[fori001+1],1);
code[0] = incoming[i];
code[1] = incoming[i+1];
char *ck = NULL;
uint64_t lu = 0;
lu=strtoul(code, &ck, 16);
buffy_HEX[x] = lu;
//printf("%s - %lu\n",code,lu);
fori001++;
x++;
retVal[i] = lu;
i++;
}
return buffy_HEX;
return retVal;
}
//
void convert(char * convert_result, uint8_t * buf) //Both of them read them properly.