Merge pull request #3 from jmjatlanta/master

Various changes to align with other ipfs projects
This commit is contained in:
ken Code 2017-02-14 11:42:52 +01:00 committed by GitHub
commit b7f3d475e3
12 changed files with 1533 additions and 1324 deletions

View file

@ -1,6 +1,9 @@
CC = gcc
CFLAGS = -O0 -I include
DEBUG = true
ifdef DEBUG
CFLAGS += -g3
endif
@ -9,7 +12,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)

View file

@ -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);

View file

@ -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 */
#endif /* base58_h */

View file

@ -1,128 +1,56 @@
#ifndef MULTIADDR
#define MULTIADDR
#include <string.h>
#include "varhexutils.h"
//#include "codecs.h"
#include "varint.h"
#include "protocols.h"
#include "protoutils.h"
#include <string.h>
int strpos(char *haystack, char *needle)
/**
* 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><n byte addr>)+
<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:
(/<addr str code>/<addr str rep>)+
/ip4/<ipv4 str addr>/udp/<udp int port>
/ip6/<ipv6 str addr>/tcp/<tcp int port>
*/
struct MultiAddress
{
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];
// A MultiAddress represented as a string
char* string;
// A MultiAddress represented as an array of bytes
uint8_t* bytes;
size_t bsize;
};
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<sz;i++)
{
if(procstr[i] == '/')
{
procstr[i]=' ';
}
}
int pos=-1;
pos=strpos(procstr,src);
if(pos!=-1)
{
for(i=pos;i<sz;i++)
{
procstr[i] = '\0';
}
for(i=0;i<sz;i++)
{
if(procstr[i] == ' ')
{
procstr[i] = '/';
}
}
return 1;
}
else
{
return 0;
}
return 0;
}
else
{
return 0;
}
}
int strpos(char *haystack, char *needle);
struct MultiAddress* multiaddress_new_from_bytes(const uint8_t* byteaddress, int size); //Construct new address from bytes
struct MultiAddress* multiaddress_new_from_string(const char* straddress); //Construct new address from string
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);
#endif

View file

@ -7,7 +7,7 @@
#include <ctype.h>
#include <errno.h>
#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; 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.
(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; i<CNT_PROTOCOLNUM; i++)
{
if(strcmp(proto_w_name, (protocol_P+i)->name) == 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; i<CNT_PROTOCOLNUM; i++)
{
if((protocol_P+i)->deccode == 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<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!
{
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<sizeof(mestring)-2; i++)
{
if(mestring[i] == '/')
{
printf("NEW WORD!\n");
atword++;
int currentsize = 0;
for(int j = i+1; mestring[j] != '/' && j < sizeof(mestring)-2; j++)
{
currentsize++;
}
char haay[20];
int lesbo = 0;
for(int x = i+1; x<sizeof(mestring)-2; x++)
{
if(mestring[x] == '/')
{
break;
}
haay[lesbo] = mestring[x];
lesbo++;
}
words[atword-1] = (char *) malloc(currentsize+2);
strcpy(words[atword-1], haay);
bzero(haay,20);
}
}
printf("Result:%s\n", words[0]);
for(int mm=0; mm < 50; mm++)
{
if(words[mm])
{
free(words[mm]);
}
}
}
#endif
void load_protocols();
struct protocol * proto_with_name(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
void pp(); //Purely for debugging purposes, prints the entire loaded protocols.
void protocols_with_string(char * meee,int sizi); // NOT FINISHED, DO NOT USE!
#endif

View file

@ -1,736 +1,41 @@
#ifndef PROTOUTILS
#define PROTOUTILS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <inttypes.h>
#include <ctype.h>
#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(const 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<MAX_HEX_NUMBER_COUNT)
{
packed=1;
hncount++;
if(ishexdigit(*str))
{
if(hncount==MAX_HEX_NUMBER_COUNT)
{
err=1;
} else
{
hdcount=1;
hncount++;
str++;
}
}
} else
{
err=1;
}
}
}
else
{
if(!ishexdigit(*str))
{
err=1;
} else
{
if(hncount==MAX_HEX_NUMBER_COUNT)
{
err=1;
} else
{
hdcount=1;
hncount++;
str++;
}
}
}
}
else
{
if(ishexdigit(*str))
{
if(hdcount==4)
err=1;
else
{
hdcount++;
str++;
}
} else
err=1;
}
}
if(hncount<MAX_HEX_NUMBER_COUNT&&packed==0)
err=1;
return(err==0);
}
uint64_t ip2int(char * ipconvertint)
{
uint64_t final_result =0;
char * iproc;
int ipat1=0;
int ipat2=0;
int ipat3=0;
int ipat4=0;
char ip[16];
strcpy(ip, ipconvertint);
iproc = strtok (ip,".");
for(int i=0; i<4;i++)
{
switch(i)
{
case 0:
{
ipat1 = atoi(iproc);
break;
}
case 1:
{
ipat2 = atoi(iproc);
break;
}
case 2:
{
ipat3 = atoi(iproc);
break;
}
case 3:
{
ipat4 = atoi(iproc);
break;
}
default:
{
printf("Somebody misplaced an int\n");
break;
}
}
iproc = strtok (NULL,".");
}
final_result = ((ipat1*pow(2,24))+(ipat2*pow(2,16))+(ipat3*pow(2,8))+ipat4*1);
return final_result;
}
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;
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(lastpos<size*2)
{
goto NAX;
}
}
else//IPFS CASE
{
lastpos = lastpos + 4;
//FETCHING SIZE OF ADDRESS
char prefixedvarint[3];
bzero(prefixedvarint,3);
int pvi;
pvi=0;
for(int i=lastpos-2;i<lastpos;i++)
{
prefixedvarint[pvi] = hex[i];
pvi++;
}
int addrsize;
addrsize = HexVar_To_Num_32(prefixedvarint);
unsigned char IPFS_ADDR[addrsize+1];
bzero(IPFS_ADDR,addrsize+1);
int IPFS_PARSE;
IPFS_PARSE = 0;
for(int i = lastpos;i<lastpos+addrsize;i++)
{
IPFS_ADDR[IPFS_PARSE] = hex[i];
//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 rezbuflen = strlen(IPFS_ADDR);
unsigned char rezultat[rezbuflen];
bzero(rezultat,rezbuflen);
unsigned char * pointyaddr = NULL;
pointyaddr = rezultat;
int returnstatus = 0;
returnstatus = libp2p_crypto_encoding_base58_encode(addrbuf, sizeof(addrbuf), &pointyaddr, &rezbuflen);
if(returnstatus == 0)
{
printf("\nERROR!!!!!\n");
return 0;
}
strcat(resultzx, "/");
strcat(resultzx, PID->name);
strcat(resultzx, "/");
strcat(resultzx, rezultat);
}
}
strcat(resultzx, "/");
unload_protocols();
}
//
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)
{
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, const char * abc, size_t getsznow);
int string_to_bytes(uint8_t * finalbytes,size_t* 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

View file

@ -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<size;cz++)
{
buf[cz] = xbuf[cz];
}
//
if(crrz01!=NULL)
{
char * crrz1 = NULL;
crrz1 = crrz01;
char conv_proc[800]="\0";
int i;
for(i=0; i < (size*2); i++)
{
if(buf[i]!='\0')
{
sprintf (conv_proc, "%02X", buf[i]);
//printf("%d:%d\n",i, buf[i]);
strcat(crrz1, conv_proc);
}
}
crrz1 = NULL;
}
}
char * Var_To_Hex(int realsize, 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";
bzero(convert_resultz1,800);
vthconvert(realsize, convert_resultz1, TOHEXINPUT);
return convert_resultz1;
}
}
uint8_t * Hex_To_Var(char * Hexstr) //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);
char code[3];
bzero(code,3);
code[3]='\0';
int x = 0;
int fori001=0;
for(fori001=0;fori001<800;fori001++)
{
strncpy(&code[0],&codo[fori001],1);
strncpy(&code[1],&codo[fori001+1],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++;
}
return buffy_HEX;
}
//
void convert(char * convert_result, uint8_t * buf) //Both of them read them properly.
{
char conv_proc[800]="\0";
bzero(conv_proc,800);
int i;
for(i=0; i < 10; i++)
{
sprintf (conv_proc, "%02X", buf[i]);
//printf("%d:%d\n",i, buf[i]);
strcat(convert_result, conv_proc);
}
}
char * Num_To_HexVar_64(uint64_t TOHVINPUT) //UINT64 TO HEXIFIED VAR
{ //Code to varint - py
static char convert_result[800]="\0";//Note that the hex resulted from this will differ from py
bzero(convert_result,800);
memset(convert_result,0,sizeof(convert_result));//But if you make sure the string is always 20 chars in size
uint8_t buf[400] = {0};
bzero(buf,400);
uvarint_encode64(TOHVINPUT, buf, 800);
convert(convert_result,buf);
return convert_result;
}
void convert2(char * convert_result2, uint8_t * bufhx)
{
uint8_t * buf = NULL;
buf = bufhx;
char conv_proc[3]="\0";
conv_proc[3] = '\0';
bzero(conv_proc, 3);
int i;
for(i=0; i == 0; i++)
{
sprintf (conv_proc, "%02X", buf[i]);
//printf("aaaaaaaaaaah%d:%d\n",i, buf[i]);
strcat(convert_result2, conv_proc);
}
buf = NULL;
}
char * Num_To_HexVar_32(uint32_t TOHVINPUT) //UINT32 TO HEXIFIED VAR
{ //Code to varint - py
static char convert_result2[3]="\0";
bzero(convert_result2,3);
convert_result2[2] = '\0';
memset(convert_result2,0,sizeof(convert_result2));
uint8_t buf[1] = {0};
bzero(buf,1);
uvarint_encode32(TOHVINPUT, buf, 1);
convert2(convert_result2,buf);
return convert_result2;
}
int8_t Var_Bytes_Count(uint8_t * countbytesofthis);
uint64_t HexVar_To_Num_64(char * theHEXstring) //HEXIFIED VAR TO UINT64_T
{ //Varint to code - py
uint8_t buffy[400] = {0};
char codo[800] = "\0";
bzero(codo,800);
strcpy(codo, theHEXstring);
char code[3] = "\0";
int x = 0;
for(int i= 0;i<399;i++)
{
strncpy(&code[0],&codo[i],1);
strncpy(&code[1],&codo[i+1],1);
char *ck = NULL;
uint64_t lu = 0;
lu=strtoul(code, &ck, 16);
buffy[x] = lu;
i++;
x++;
}
static uint64_t decoded;
uvarint_decode64 (buffy, 400, &decoded);
return decoded;
}
uint32_t HexVar_To_Num_32(char theHEXstring[]) //HEXIFIED VAR TO UINT32_T
{ //Varint to code py
uint8_t buffy[400] = {0};
bzero(buffy,400);
char codo[800] = "\0";
bzero(codo,800);
strcpy(codo, theHEXstring);
char code[3] = "\0";
bzero(code,3);
code[3] = '\0';
int x = 0;
for(int i= 0;i<399;i++)
{
strncpy(&code[0],&codo[i],1);
strncpy(&code[1],&codo[i+1],1);
char *ck = NULL;
uint32_t lu = {0};
lu=strtoul(code, &ck, 16);
buffy[x] = lu;
i++;
x++;
}
static uint32_t decoded;
uvarint_decode32 (buffy, 10, &decoded);
return decoded;
}
#endif
uint8_t * Num_To_Varint_64(uint64_t TOV64INPUT); //UINT64_T TO VARINT
uint8_t * Num_To_Varint_32(uint32_t TOV32INPUT); // UINT32_T TO VARINT
uint64_t * Varint_To_Num_64(uint8_t TON64INPUT[60]); //VARINT TO UINT64_t
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
uint64_t Hex_To_Int(char * hax);
//
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]
//
void convert(char * convert_result, uint8_t * buf); //Both of them read them properly.
char * Num_To_HexVar_64(uint64_t TOHVINPUT); //UINT64 TO HEXIFIED VAR
void convert2(char * convert_result2, uint8_t * bufhx);
char * Num_To_HexVar_32(uint32_t TOHVINPUT); //UINT32 TO HEXIFIED VAR
uint64_t HexVar_To_Num_64(char * theHEXstring); //HEXIFIED VAR TO UINT64_T
uint32_t HexVar_To_Num_32(char theHEXstring[]); //HEXIFIED VAR TO UINT32_T
#endif

201
multiaddr.c Normal file
View file

@ -0,0 +1,201 @@
#include <string.h>
#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.
}
}
/**
* 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* out = multiaddress_new();
if (out != NULL) {
if(byteaddress!=NULL)
{
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)
{
multiaddress_free(out);
return NULL;
}
}
}
return out;
}
struct MultiAddress* multiaddress_new_from_string(const char* straddress)//Construct new address from string
{
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 )
{
multiaddress_free(out);
return NULL;
}
}
return out;
}
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;
}
}
/**
* 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
* @param string the new string
*/
int multiaddress_encapsulate(struct MultiAddress* result, char* string)
{
if(result != NULL && string != NULL)
{
// 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;
}
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;
}
// not sure what this does
int multiaddress_decapsulate(struct MultiAddress * 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;
// change slash to space
for(i=0;i<sz;i++)
{
if(procstr[i] == '/')
{
procstr[i]=' ';
}
}
int pos=-1;
pos=strpos(procstr,src);
if(pos!=-1)
{
// fill rest with 0s
for(i=pos;i<sz;i++)
{
procstr[i] = '\0';
}
// replace space with slash
for(i=0;i<sz;i++)
{
if(procstr[i] == ' ')
{
procstr[i] = '/';
}
}
return 1;
}
else
{
return 0;
}
return 0;
}
else
{
return 0;
}
}

View file

@ -1,50 +1,204 @@
#include "protocols.h"
#include "codecs.h"
int main() //This won't exist, it's here for my own testing purposes.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#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; 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.
(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;
}
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; i<CNT_PROTOCOLNUM; i++)
{
if(strcmp(proto_w_name, (protocol_P+i)->name) == 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; i<CNT_PROTOCOLNUM; i++)
{
if((protocol_P+i)->deccode == 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<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!
{
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<sizeof(mestring)-2; i++)
{
if(mestring[i] == '/')
{
printf("NEW WORD!\n");
atword++;
int currentsize = 0;
for(int j = i+1; mestring[j] != '/' && j < sizeof(mestring)-2; j++)
{
currentsize++;
}
char haay[20];
int lesbo = 0;
for(int x = i+1; x<sizeof(mestring)-2; x++)
{
if(mestring[x] == '/')
{
break;
}
haay[lesbo] = mestring[x];
lesbo++;
}
words[atword-1] = (char *) malloc(currentsize+2);
strcpy(words[atword-1], haay);
bzero(haay,20);
}
}
printf("Result:%s\n", words[0]);
for(int mm=0; mm < 50; mm++)
{
if(words[mm])
{
free(words[mm]);
}
}
}

742
protoutils.c Normal file
View file

@ -0,0 +1,742 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <inttypes.h>
#include <ctype.h>
#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<MAX_HEX_NUMBER_COUNT)
{
packed=1;
hncount++;
if(ishexdigit(*str))
{
if(hncount==MAX_HEX_NUMBER_COUNT)
{
err=1;
} else
{
hdcount=1;
hncount++;
str++;
}
}
} else
{
err=1;
}
}
}
else
{
if(!ishexdigit(*str))
{
err=1;
} else
{
if(hncount==MAX_HEX_NUMBER_COUNT)
{
err=1;
} else
{
hdcount=1;
hncount++;
str++;
}
}
}
}
else
{
if(ishexdigit(*str))
{
if(hdcount==4)
err=1;
else
{
hdcount++;
str++;
}
} else
err=1;
}
}
if(hncount<MAX_HEX_NUMBER_COUNT&&packed==0)
err=1;
return(err==0);
}
uint64_t ip2int(const char * ipconvertint)
{
uint64_t final_result =0;
char * iproc;
int ipat1=0;
int ipat2=0;
int ipat3=0;
int ipat4=0;
char ip[16];
strcpy(ip, ipconvertint);
iproc = strtok (ip,".");
for(int i=0; i<4;i++)
{
switch(i)
{
case 0:
{
ipat1 = atoi(iproc);
break;
}
case 1:
{
ipat2 = atoi(iproc);
break;
}
case 2:
{
ipat3 = atoi(iproc);
break;
}
case 3:
{
ipat4 = atoi(iproc);
break;
}
default:
{
printf("Somebody misplaced an int\n");
break;
}
}
iproc = strtok (NULL,".");
}
final_result = ((ipat1*pow(2,24))+(ipat2*pow(2,16))+(ipat3*pow(2,8))+ipat4*1);
return final_result;
}
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;
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, const 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(lastpos<size*2)
{
goto NAX;
}
}
else//IPFS CASE
{
lastpos = lastpos + 4;
//FETCHING SIZE OF ADDRESS
char prefixedvarint[3];
bzero(prefixedvarint,3);
int pvi;
pvi=0;
for(int i=lastpos-2;i<lastpos;i++)
{
prefixedvarint[pvi] = hex[i];
pvi++;
}
int addrsize;
addrsize = HexVar_To_Num_32(prefixedvarint);
unsigned char IPFS_ADDR[addrsize+1];
bzero(IPFS_ADDR,addrsize+1);
int IPFS_PARSE;
IPFS_PARSE = 0;
for(int i = lastpos;i<lastpos+addrsize;i++)
{
IPFS_ADDR[IPFS_PARSE] = hex[i];
//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 rezbuflen = strlen(IPFS_ADDR);
unsigned char rezultat[rezbuflen];
bzero(rezultat,rezbuflen);
unsigned char * pointyaddr = NULL;
pointyaddr = rezultat;
int returnstatus = 0;
returnstatus = multiaddr_encoding_base58_encode(addrbuf, sizeof(addrbuf), &pointyaddr, &rezbuflen);
if(returnstatus == 0)
{
printf("\nERROR!!!!!\n");
return 0;
}
strcat(resultzx, "/");
strcat(resultzx, PID->name);
strcat(resultzx, "/");
strcat(resultzx, rezultat);
}
}
strcat(resultzx, "/");
unload_protocols();
}
//
char * address_string_to_bytes(struct protocol * xx, const 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 = (char*)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;
}
}
}
/**
* 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] != '/')
{
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);
//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;
}
}

View file

@ -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;
a=new_maddr_fs(addrstr);
printf("TEST BYTES: %s\n",Var_To_Hex(a.bsize[0], a.bytes));
struct MultiAddress* a;
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/");
printf("A STRING ENCAPSULATED:%s\n",a.string);
printf("A STRING:%s\n",a->string);
multiaddress_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);
multiaddress_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);
multiaddress_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 MultiAddress* beta;
beta=multiaddress_new_from_bytes(a->bytes,a->bsize);
printf("B STRING: %s\n",beta->string);
multiaddress_free(a);
multiaddress_free(beta);
}

248
varhexutils.c Normal file
View file

@ -0,0 +1,248 @@
#ifndef VARHEXUTILS
#define VARHEXUTILS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <inttypes.h>
#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, const uint8_t * xbuf)
{
uint8_t buf[400];
bzero(buf,400);
//fixing the buf
for(int cz=0; cz<size;cz++)
{
buf[cz] = xbuf[cz];
}
//
if(crrz01!=NULL)
{
char * crrz1 = NULL;
crrz1 = crrz01;
char conv_proc[800]="\0";
int i;
for(i=0; i < (size*2); i++)
{
if(buf[i]!='\0')
{
sprintf (conv_proc, "%02X", buf[i]);
//printf("%d:%d\n",i, buf[i]);
strcat(crrz1, conv_proc);
}
}
crrz1 = NULL;
}
}
char * Var_To_Hex(int realsize, const uint8_t * TOHEXINPUT) //VAR[binformat] TO HEX
{
if(TOHEXINPUT != NULL)
{
static char convert_resultz1[800]="\0";
bzero(convert_resultz1,800);
vthconvert(realsize, convert_resultz1, TOHEXINPUT);
return convert_resultz1;
}
}
uint8_t * Hex_To_Var(char * Hexstr) //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);
char code[3];
bzero(code,3);
code[3]='\0';
int x = 0;
int fori001=0;
for(fori001=0;fori001<800;fori001++)
{
strncpy(&code[0],&codo[fori001],1);
strncpy(&code[1],&codo[fori001+1],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++;
}
return buffy_HEX;
}
//
void convert(char * convert_result, uint8_t * buf) //Both of them read them properly.
{
char conv_proc[800]="\0";
bzero(conv_proc,800);
int i;
for(i=0; i < 10; i++)
{
sprintf (conv_proc, "%02X", buf[i]);
//printf("%d:%d\n",i, buf[i]);
strcat(convert_result, conv_proc);
}
}
char * Num_To_HexVar_64(uint64_t TOHVINPUT) //UINT64 TO HEXIFIED VAR
{ //Code to varint - py
static char convert_result[800]="\0";//Note that the hex resulted from this will differ from py
bzero(convert_result,800);
memset(convert_result,0,sizeof(convert_result));//But if you make sure the string is always 20 chars in size
uint8_t buf[400] = {0};
bzero(buf,400);
uvarint_encode64(TOHVINPUT, buf, 800);
convert(convert_result,buf);
return convert_result;
}
void convert2(char * convert_result2, uint8_t * bufhx)
{
uint8_t * buf = NULL;
buf = bufhx;
char conv_proc[3]="\0";
conv_proc[3] = '\0';
bzero(conv_proc, 3);
int i;
for(i=0; i == 0; i++)
{
sprintf (conv_proc, "%02X", buf[i]);
//printf("aaaaaaaaaaah%d:%d\n",i, buf[i]);
strcat(convert_result2, conv_proc);
}
buf = NULL;
}
char * Num_To_HexVar_32(uint32_t TOHVINPUT) //UINT32 TO HEXIFIED VAR
{ //Code to varint - py
static char convert_result2[3]="\0";
bzero(convert_result2,3);
convert_result2[2] = '\0';
memset(convert_result2,0,sizeof(convert_result2));
uint8_t buf[1] = {0};
bzero(buf,1);
uvarint_encode32(TOHVINPUT, buf, 1);
convert2(convert_result2,buf);
return convert_result2;
}
uint64_t HexVar_To_Num_64(char * theHEXstring) //HEXIFIED VAR TO UINT64_T
{ //Varint to code - py
uint8_t buffy[400] = {0};
char codo[800] = "\0";
bzero(codo,800);
strcpy(codo, theHEXstring);
char code[3] = "\0";
int x = 0;
for(int i= 0;i<399;i++)
{
strncpy(&code[0],&codo[i],1);
strncpy(&code[1],&codo[i+1],1);
char *ck = NULL;
uint64_t lu = 0;
lu=strtoul(code, &ck, 16);
buffy[x] = lu;
i++;
x++;
}
static uint64_t decoded;
uvarint_decode64 (buffy, 400, &decoded);
return decoded;
}
uint32_t HexVar_To_Num_32(char theHEXstring[]) //HEXIFIED VAR TO UINT32_T
{ //Varint to code py
uint8_t buffy[400] = {0};
bzero(buffy,400);
char codo[800] = "\0";
bzero(codo,800);
strcpy(codo, theHEXstring);
char code[3] = "\0";
bzero(code,3);
code[3] = '\0';
int x = 0;
for(int i= 0;i<399;i++)
{
strncpy(&code[0],&codo[i],1);
strncpy(&code[1],&codo[i+1],1);
char *ck = NULL;
uint32_t lu = {0};
lu=strtoul(code, &ck, 16);
buffy[x] = lu;
i++;
x++;
}
static uint32_t decoded;
uvarint_decode32 (buffy, 10, &decoded);
return decoded;
}
#endif