Add files via upload
This commit is contained in:
parent
148523117b
commit
ebc0e05b8b
9 changed files with 753 additions and 60 deletions
200
base58.c
Normal file
200
base58.c
Normal file
|
@ -0,0 +1,200 @@
|
|||
/*
|
||||
* Copyright 2012-2014 Luke Dashjr
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the standard MIT license. See COPYING for more details.
|
||||
*/
|
||||
|
||||
#ifndef WIN32
|
||||
#include <arpa/inet.h>
|
||||
#else
|
||||
#include <winsock2.h>
|
||||
#endif
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "base58.h"
|
||||
|
||||
bool (*b58_sha256_impl)(void *, const void *, size_t) = NULL;
|
||||
|
||||
static const int8_t b58digits_map[] = {
|
||||
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
|
||||
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
|
||||
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
|
||||
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8,-1,-1,-1,-1,-1,-1,
|
||||
-1, 9,10,11,12,13,14,15, 16,-1,17,18,19,20,21,-1,
|
||||
22,23,24,25,26,27,28,29, 30,31,32,-1,-1,-1,-1,-1,
|
||||
-1,33,34,35,36,37,38,39, 40,41,42,43,-1,44,45,46,
|
||||
47,48,49,50,51,52,53,54, 55,56,57,-1,-1,-1,-1,-1,
|
||||
};
|
||||
|
||||
bool b58tobin(void *bin, size_t *binszp, const char *b58, size_t b58sz)
|
||||
{
|
||||
size_t binsz = *binszp;
|
||||
const unsigned char *b58u = (void*)b58;
|
||||
unsigned char *binu = bin;
|
||||
size_t outisz = (binsz + 3) / 4;
|
||||
uint32_t outi[outisz];
|
||||
uint64_t t;
|
||||
uint32_t c;
|
||||
size_t i, j;
|
||||
uint8_t bytesleft = binsz % 4;
|
||||
uint32_t zeromask = bytesleft ? (0xffffffff << (bytesleft * 8)) : 0;
|
||||
unsigned zerocount = 0;
|
||||
|
||||
if (!b58sz)
|
||||
b58sz = strlen(b58);
|
||||
|
||||
memset(outi, 0, outisz * sizeof(*outi));
|
||||
|
||||
// Leading zeros, just count
|
||||
for (i = 0; i < b58sz && b58u[i] == '1'; ++i)
|
||||
++zerocount;
|
||||
|
||||
for ( ; i < b58sz; ++i)
|
||||
{
|
||||
if (b58u[i] & 0x80)
|
||||
// High-bit set on invalid digit
|
||||
return false;
|
||||
if (b58digits_map[b58u[i]] == -1)
|
||||
// Invalid base58 digit
|
||||
return false;
|
||||
c = (unsigned)b58digits_map[b58u[i]];
|
||||
for (j = outisz; j--; )
|
||||
{
|
||||
t = ((uint64_t)outi[j]) * 58 + c;
|
||||
c = (t & 0x3f00000000) >> 32;
|
||||
outi[j] = t & 0xffffffff;
|
||||
}
|
||||
if (c)
|
||||
// Output number too big (carry to the next int32)
|
||||
return false;
|
||||
if (outi[0] & zeromask)
|
||||
// Output number too big (last int32 filled too far)
|
||||
return false;
|
||||
}
|
||||
|
||||
j = 0;
|
||||
switch (bytesleft) {
|
||||
case 3:
|
||||
*(binu++) = (outi[0] & 0xff0000) >> 16;
|
||||
case 2:
|
||||
*(binu++) = (outi[0] & 0xff00) >> 8;
|
||||
case 1:
|
||||
*(binu++) = (outi[0] & 0xff);
|
||||
++j;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
for (; j < outisz; ++j)
|
||||
{
|
||||
*(binu++) = (outi[j] >> 0x18) & 0xff;
|
||||
*(binu++) = (outi[j] >> 0x10) & 0xff;
|
||||
*(binu++) = (outi[j] >> 8) & 0xff;
|
||||
*(binu++) = (outi[j] >> 0) & 0xff;
|
||||
}
|
||||
|
||||
// Count canonical base58 byte count
|
||||
binu = bin;
|
||||
for (i = 0; i < binsz; ++i)
|
||||
{
|
||||
if (binu[i])
|
||||
break;
|
||||
--*binszp;
|
||||
}
|
||||
*binszp += zerocount;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static
|
||||
bool my_dblsha256(void *hash, const void *data, size_t datasz)
|
||||
{
|
||||
uint8_t buf[0x20];
|
||||
return b58_sha256_impl(buf, data, datasz) && b58_sha256_impl(hash, buf, sizeof(buf));
|
||||
}
|
||||
|
||||
int b58check(const void *bin, size_t binsz, const char *base58str, size_t b58sz)
|
||||
{
|
||||
unsigned char buf[32];
|
||||
const uint8_t *binc = bin;
|
||||
unsigned i;
|
||||
if (binsz < 4)
|
||||
return -4;
|
||||
if (!my_dblsha256(buf, bin, binsz - 4))
|
||||
return -2;
|
||||
if (memcmp(&binc[binsz - 4], buf, 4))
|
||||
return -1;
|
||||
|
||||
// Check number of zeros is correct AFTER verifying checksum (to avoid possibility of accessing base58str beyond the end)
|
||||
for (i = 0; binc[i] == '\0' && base58str[i] == '1'; ++i)
|
||||
{} // Just finding the end of zeros, nothing to do in loop
|
||||
if (binc[i] == '\0' || base58str[i] == '1')
|
||||
return -3;
|
||||
|
||||
return binc[0];
|
||||
}
|
||||
|
||||
static const char b58digits_ordered[] = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
||||
|
||||
bool b58enc(char *b58, size_t *b58sz, const void *data, size_t binsz)
|
||||
{
|
||||
const uint8_t *bin = data;
|
||||
int carry;
|
||||
ssize_t i, j, high, zcount = 0;
|
||||
size_t size;
|
||||
|
||||
while (zcount < binsz && !bin[zcount])
|
||||
++zcount;
|
||||
|
||||
size = (binsz - zcount) * 138 / 100 + 1;
|
||||
uint8_t buf[size];
|
||||
memset(buf, 0, size);
|
||||
|
||||
for (i = zcount, high = size - 1; i < binsz; ++i, high = j)
|
||||
{
|
||||
for (carry = bin[i], j = size - 1; (j > high) || carry; --j)
|
||||
{
|
||||
carry += 256 * buf[j];
|
||||
buf[j] = carry % 58;
|
||||
carry /= 58;
|
||||
}
|
||||
}
|
||||
|
||||
for (j = 0; j < size && !buf[j]; ++j);
|
||||
|
||||
if (*b58sz <= zcount + size - j)
|
||||
{
|
||||
*b58sz = zcount + size - j + 1;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (zcount)
|
||||
memset(b58, '1', zcount);
|
||||
for (i = zcount; j < size; ++i, ++j)
|
||||
b58[i] = b58digits_ordered[buf[j]];
|
||||
b58[i] = '\0';
|
||||
*b58sz = i + 1;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool b58check_enc(char *b58c, size_t *b58c_sz, uint8_t ver, const void *data, size_t datasz)
|
||||
{
|
||||
uint8_t buf[1 + datasz + 0x20];
|
||||
uint8_t *hash = &buf[1 + datasz];
|
||||
|
||||
buf[0] = ver;
|
||||
memcpy(&buf[1], data, datasz);
|
||||
if (!my_dblsha256(hash, buf, datasz + 1))
|
||||
{
|
||||
*b58c_sz = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
return b58enc(b58c, b58c_sz, buf, 1 + datasz + 4);
|
||||
}
|
24
base58.h
Normal file
24
base58.h
Normal file
|
@ -0,0 +1,24 @@
|
|||
#ifndef LIBBASE58_H
|
||||
#define LIBBASE58_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern bool (*b58_sha256_impl)(void *, const void *, size_t);
|
||||
|
||||
extern bool b58tobin(void *bin, size_t *binsz, const char *b58, size_t b58sz);
|
||||
extern int b58check(const void *bin, size_t binsz, const char *b58, size_t b58sz);
|
||||
|
||||
extern bool b58enc(char *b58, size_t *b58sz, const void *bin, size_t binsz);
|
||||
extern bool b58check_enc(char *b58c, size_t *b58c_sz, uint8_t ver, const void *data, size_t datasz);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
7
codecs.h
Normal file
7
codecs.h
Normal file
|
@ -0,0 +1,7 @@
|
|||
#ifndef CODECS
|
||||
#include "varhexutils.h"
|
||||
#include "varint.h"
|
||||
#include "protocols.h"
|
||||
#include <regex.h>
|
||||
|
||||
#endif
|
118
multiaddr.h
Normal file
118
multiaddr.h
Normal file
|
@ -0,0 +1,118 @@
|
|||
#ifndef MULTIADDR
|
||||
#define MULTIADDR
|
||||
#include "varhexutils.h"
|
||||
#include "codecs.h"
|
||||
#include "varint.h"
|
||||
#include "protocols.h"
|
||||
#include "protoutils.h"
|
||||
#include <string.h>
|
||||
int strpos(char *haystack, char *needle)
|
||||
{
|
||||
char *p = strstr(haystack, needle);
|
||||
if (p)
|
||||
{
|
||||
return p - haystack;
|
||||
}
|
||||
else
|
||||
{
|
||||
return -1; // Not found = -1.
|
||||
}
|
||||
}
|
||||
struct maddr
|
||||
{
|
||||
char string[100];
|
||||
uint8_t bytes[100];
|
||||
};
|
||||
struct maddr new_maddr_fb(uint8_t * byteaddress,int size)//Construct new address from bytes
|
||||
{
|
||||
struct maddr anewaddr;
|
||||
if(byteaddress!=NULL)
|
||||
{
|
||||
memcpy(anewaddr.bytes, byteaddress,size);
|
||||
if(bytes_to_string(anewaddr.string,byteaddress,size)==1)
|
||||
{
|
||||
return anewaddr;
|
||||
}
|
||||
}
|
||||
}
|
||||
struct maddr new_maddr_fs(char * straddress)//Construct new address from string
|
||||
{
|
||||
struct maddr anewaddr;
|
||||
strcpy(anewaddr.string, straddress);
|
||||
if(string_to_bytes(anewaddr.bytes,anewaddr.string,sizeof(anewaddr.string))==1)
|
||||
{
|
||||
return anewaddr;
|
||||
}
|
||||
}
|
||||
int m_encapsulate(struct maddr * result, char * string)
|
||||
{
|
||||
if(result!=NULL&&string!=NULL)
|
||||
{
|
||||
int success = 0;
|
||||
char pstr[50];
|
||||
bzero(pstr,50);
|
||||
strcpy(pstr,result->string);
|
||||
strcat(pstr,string+1);
|
||||
if(string_to_bytes(result->bytes,pstr,sizeof(pstr)))
|
||||
{
|
||||
printf("RESULT WOULD BE: %s\n", 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] = '/';
|
||||
}
|
||||
}
|
||||
strcpy(result->string,procstr);
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
#endif
|
30
proto-dat
30
proto-dat
|
@ -1,15 +1,15 @@
|
|||
hex-code dec-code size name
|
||||
84000000000000000000 4 32 ip4
|
||||
A9000000000000000000 41 128 ip6
|
||||
86000000000000000000 6 16 tcp
|
||||
91000000000000000000 17 16 udp
|
||||
A1000000000000000000 33 16 dccp
|
||||
84810000000000000000 132 16 sctp
|
||||
AD820000000000000000 301 0 udt
|
||||
AE820000000000000000 302 0 utp
|
||||
AA000000000000000000 42 -1 ipfs
|
||||
E0830000000000000000 480 0 http
|
||||
BB830000000000000000 443 0 https
|
||||
DD830000000000000000 477 0 ws
|
||||
BC830000000000000000 444 10 onion
|
||||
93820000000000000000 275 0 libp2p-webrtc-star
|
||||
hex-code dec-code size name
|
||||
04 4 32 ip4
|
||||
29 41 128 ip6
|
||||
06 6 16 tcp
|
||||
11 17 16 udp
|
||||
21 33 16 dccp
|
||||
84 132 16 sctp
|
||||
12D 301 0 udt
|
||||
12E 302 0 utp
|
||||
2A 42 -1 ipfs
|
||||
1E0 480 0 http
|
||||
1BB 443 0 https
|
||||
1DD 477 0 ws
|
||||
1BC 444 10 onion
|
||||
113 275 0 libp2p-webrtc-star
|
|
@ -21,7 +21,7 @@ int protocol_REMOVE_id(int remid)//Function to remove & shift back all data, sor
|
|||
|
||||
if(remid < CNT_PROTOCOLNUM && remid >= 0&&CNT_PROTOCOLNUM!=0) //Checking to see if remid actually exists.
|
||||
{
|
||||
for(int i=remid; i<CNT_PROTOCOLNUM; ++i) //While i < num of registered protocols
|
||||
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
|
||||
|
@ -49,6 +49,7 @@ int protocol_REMOVE_id(int remid)//Function to remove & shift back all data, sor
|
|||
void unload_protocols()
|
||||
{
|
||||
free(protocol_P);
|
||||
CNT_PROTOCOLNUM=0;
|
||||
}
|
||||
void load_protocols()
|
||||
{
|
||||
|
@ -128,6 +129,7 @@ struct protocol * proto_with_deccode(int proto_w_deccode) //Search for protocol
|
|||
return (protocol_P+i);
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
void pp() //Purely for debugging purposes, prints the entire loaded protocols.
|
||||
{
|
||||
|
|
348
protoutils.h
348
protoutils.h
|
@ -4,6 +4,11 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include <inttypes.h>
|
||||
#include <ctype.h>
|
||||
#include "base58.h"
|
||||
#include "varhexutils.h"
|
||||
#include "protocols.h"
|
||||
//IP2INT
|
||||
|
||||
//IPv4 VALIDATOR
|
||||
|
@ -12,6 +17,7 @@
|
|||
/* 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;
|
||||
|
@ -26,22 +32,23 @@ int is_valid_ipv4(char *ip_str)
|
|||
{
|
||||
int i, num, dots = 0;
|
||||
char *ptr;
|
||||
|
||||
int err=0;
|
||||
if (ip_str == NULL)
|
||||
return 0;
|
||||
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)
|
||||
return 0;
|
||||
err = 1;
|
||||
|
||||
while (ptr) {
|
||||
while (ptr)
|
||||
{
|
||||
|
||||
/* after parsing string, it must contain only digits */
|
||||
if (!valid_digit(ptr))
|
||||
return 0;
|
||||
err = 1;
|
||||
|
||||
num = atoi(ptr);
|
||||
|
||||
|
@ -52,13 +59,22 @@ int is_valid_ipv4(char *ip_str)
|
|||
if (ptr != NULL)
|
||||
++dots;
|
||||
} else
|
||||
return 0;
|
||||
err = 1;
|
||||
}
|
||||
|
||||
/* valid IP string must contain 3 dots */
|
||||
if (dots != 3)
|
||||
return 0;
|
||||
{
|
||||
err = 1;
|
||||
}
|
||||
if(err == 0)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -191,7 +207,6 @@ uint64_t ip2int(char * ipconvertint)
|
|||
int ipat4=0;
|
||||
char ip[16];
|
||||
strcpy(ip, ipconvertint);
|
||||
printf("ip: %s\n", ip);
|
||||
iproc = strtok (ip,".");
|
||||
for(int i=0; i<4;i++)
|
||||
{
|
||||
|
@ -225,10 +240,125 @@ uint64_t ip2int(char * ipconvertint)
|
|||
}
|
||||
iproc = strtok (NULL,".");
|
||||
}
|
||||
final_result = ((ipat1*pow(2,24))+(ipat2*pow(2,16))+(ipat3*pow(2,8))+1);
|
||||
final_result = ((ipat1*pow(2,24))+(ipat2*pow(2,16))+(ipat3*pow(2,8))+ipat4*1);
|
||||
return final_result;
|
||||
}
|
||||
char * address_string_to_bytes(struct protocol * xx, char * abc)
|
||||
char * int2ip(int inputintip)
|
||||
{
|
||||
uint32_t ipint = inputintip;
|
||||
static char xxx_int2ip_result[16] = "\0";
|
||||
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 * result, uint8_t * frombuf, size_t weesize)
|
||||
{
|
||||
char removedlines[weesize*2];
|
||||
bzero(removedlines,sizeof(removedlines));
|
||||
strcpy(removedlines, Var_To_Hex(frombuf));
|
||||
//printf("Bytes to Hex: %s\n",removedlines);
|
||||
load_protocols();
|
||||
int thelastpos=0;
|
||||
for(int i=0;i<sizeof(removedlines);i++)
|
||||
{
|
||||
if(thelastpos!=0)
|
||||
{
|
||||
i = thelastpos;
|
||||
thelastpos=0;
|
||||
}
|
||||
char protocol[2];
|
||||
bzero(protocol,2);
|
||||
protocol[0] = removedlines[i];
|
||||
protocol[1] = removedlines[i+1];
|
||||
if(proto_with_deccode(Hex_To_Int(protocol))!=NULL)
|
||||
{
|
||||
//printf("Cproto: %s - %lu\n",protocol,Hex_To_Int(protocol));
|
||||
struct protocol * bprotox;
|
||||
bprotox = proto_with_deccode(Hex_To_Int(protocol));
|
||||
//Getting the address!
|
||||
char the_wanted_address[50];
|
||||
int ffpos=0;
|
||||
int zxp=0;
|
||||
int addrsizesofar=0;
|
||||
bzero(the_wanted_address,50);
|
||||
char isitp[2] = "\0";
|
||||
bzero(isitp,2);
|
||||
for(int zx=i+2;zx<sizeof(removedlines);zx++)
|
||||
{
|
||||
//printf("ZX: %d\n",zx);
|
||||
if(zx%2==0)
|
||||
{
|
||||
isitp[0] = removedlines[zx];
|
||||
isitp[1] = removedlines[zx+1];
|
||||
struct protocol * izp;
|
||||
izp=proto_with_deccode(Hex_To_Int(isitp));
|
||||
if(izp)
|
||||
{
|
||||
|
||||
if(addrsizesofar == (bprotox->size/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++;
|
||||
}
|
||||
unload_protocols();
|
||||
strcat(result,"/");
|
||||
return 1;
|
||||
|
||||
}
|
||||
|
||||
|
||||
char * address_string_to_bytes(struct protocol * xx, char * abc,size_t getsznow)
|
||||
{
|
||||
static char astb__stringy[10] = "\0";
|
||||
int code = 0;
|
||||
|
@ -237,10 +367,13 @@ char * address_string_to_bytes(struct protocol * xx, char * abc)
|
|||
{
|
||||
case 4://IPv4
|
||||
{
|
||||
if(is_valid_ipv4(abc))
|
||||
char testip[16] = "\0";
|
||||
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
|
||||
|
@ -249,109 +382,270 @@ char * address_string_to_bytes(struct protocol * xx, char * abc)
|
|||
}
|
||||
break;
|
||||
}
|
||||
case 41://IPv6
|
||||
case 41://IPv6 Must be done
|
||||
{
|
||||
printf("IP6\n");
|
||||
return "ERR";
|
||||
break;
|
||||
}
|
||||
case 6: //Tcp
|
||||
{
|
||||
printf("TCP!\n");
|
||||
if(atoi(abc)<65536&&atoi(abc)>0)
|
||||
{
|
||||
static char himm_woot[4] = "\0";
|
||||
bzero(himm_woot, 4);
|
||||
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;
|
||||
}
|
||||
return himm_woot;
|
||||
}
|
||||
else
|
||||
{
|
||||
return "ERR";
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 17: //Udp
|
||||
{
|
||||
if(atoi(abc)<65536&&atoi(abc)>0)
|
||||
{
|
||||
static char himm_woot2[4] = "\0";
|
||||
bzero(himm_woot2, 4);
|
||||
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;
|
||||
}
|
||||
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 - !!!
|
||||
{
|
||||
//abc=address
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
uint8_t * string_to_bytes(char * strx, size_t strsize)
|
||||
int string_to_bytes(uint8_t * finalbytes,char * strx, size_t strsize)
|
||||
{
|
||||
static char xxx[40] = "\0";
|
||||
bzero(xxx,40);
|
||||
int sigmalf = 0;
|
||||
char * totpch;
|
||||
char totalwordstest[strsize];
|
||||
strcpy(totalwordstest, strx);
|
||||
int totalwords = 0;
|
||||
totpch = strtok(totalwordstest, "/");
|
||||
while(totpch != NULL)
|
||||
{
|
||||
totpch = strtok (NULL, "/");
|
||||
totalwords++;
|
||||
}
|
||||
int processedwords = 0;
|
||||
char processedsofar[500] = "\0";
|
||||
char str[strsize]; //This string will be bad later.
|
||||
nextproc:
|
||||
strcpy(str,strx);
|
||||
if(str[0] == '/')
|
||||
{
|
||||
char * pch;
|
||||
static char xxx[40] = "\0";
|
||||
printf ("Splitting string \"%s\" into tokens:\n",str);
|
||||
pch = strtok (str,"/");
|
||||
load_protocols();
|
||||
while (pch != NULL)
|
||||
for(int ax=0;ax<processedwords;ax++)
|
||||
{
|
||||
pch = strtok (NULL, "/");
|
||||
}
|
||||
while(pch != NULL)
|
||||
{
|
||||
if(proto_with_name(pch))
|
||||
{
|
||||
strcat(processedsofar, "/");
|
||||
strcat(processedsofar, pch);
|
||||
struct protocol * protx;
|
||||
//printf("PCH-P:%s\n",pch);
|
||||
protx = proto_with_name(pch);
|
||||
char cut[20]="\0";
|
||||
char cut[30]="\0";
|
||||
strcat(cut,Int_To_Hex(protx->deccode));
|
||||
cut[2] = '\0';
|
||||
strcat(xxx,cut);
|
||||
pch = strtok (NULL, "/");
|
||||
printf("ADDRESS: %s\n", pch);
|
||||
if(address_string_to_bytes(protx, pch) != "ERR")
|
||||
char finipbit[2] = "\0";
|
||||
finipbit[0] = cut[0];
|
||||
finipbit[1] = cut[1];
|
||||
finipbit[2] = '\0';
|
||||
//Address
|
||||
pch = strtok (NULL, "/"); // Move to supposed address
|
||||
strcat(processedsofar, "/");
|
||||
strcat(processedsofar, pch);
|
||||
//printf("PCH-A:%s\n",pch);
|
||||
char addr[60] = "\0";
|
||||
strcpy(addr, pch);
|
||||
//If both are ok:
|
||||
strcat(xxx,finipbit);
|
||||
if(address_string_to_bytes(protx, addr,sizeof(addr)))
|
||||
{
|
||||
if(address_string_to_bytes(protx, addr,sizeof(addr)) == "ERR")
|
||||
{
|
||||
sigmalf = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
strcat(xxx,address_string_to_bytes(protx, addr,sizeof(addr)));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
sigmalf = 1;
|
||||
}
|
||||
pch = strtok (NULL, "/"); // Next step.
|
||||
processedwords++;
|
||||
processedwords++;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
pch = strtok (NULL, "/");
|
||||
//This only gets triggered if the string has a wrong format
|
||||
//Or unknown protocol!
|
||||
printf("ERROR: MALFORMED STRING\n");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if(processedwords != totalwords)
|
||||
{
|
||||
goto nextproc;
|
||||
}
|
||||
unload_protocols();
|
||||
printf("S2B_RESULT: %s\n", xxx);
|
||||
if(sigmalf != 1)
|
||||
{
|
||||
//printf("S2B_RESULT: %s\n", xxx);
|
||||
//static uint8_t finalbytes[100] = {0};
|
||||
for(int i=0; i<100; i++)
|
||||
{
|
||||
finalbytes[i] = 0;
|
||||
}
|
||||
memcpy(finalbytes, Hex_To_Var(xxx), 100);
|
||||
int xtotbytes = 0;
|
||||
for(int i=0; i<100; i++)
|
||||
{
|
||||
if(finalbytes[i] != 0)
|
||||
{
|
||||
xtotbytes++;
|
||||
}
|
||||
}
|
||||
//printf("TOT BYTES: %d\n", xtotbytes);
|
||||
uint8_t finalproc[sizeof(finalbytes)/sizeof(finalbytes)[0]];
|
||||
for(int i=0;i<sizeof(finalproc); i++)
|
||||
{
|
||||
if(finalbytes[i] != 0)
|
||||
{
|
||||
finalproc[i] = '\0';
|
||||
}
|
||||
}
|
||||
//printf("FinalBytes: %s\n",finalbytes);
|
||||
//printf("Finaltest: %s",Var_To_Hex(finalbytes));
|
||||
return 1;//success
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("ERROR, MALFORMED STRING!\n");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("ERROR, Multiaddr needs to start with '/'\n");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
|
14
testing.c
Normal file
14
testing.c
Normal file
|
@ -0,0 +1,14 @@
|
|||
#include "multiaddr.h"
|
||||
//ADD TO PROTOUTILS.H
|
||||
|
||||
int main()
|
||||
{
|
||||
struct maddr a;
|
||||
a=new_maddr_fs("/ip4/192.168.1.1/");
|
||||
printf("s str:%s\n",a.string);
|
||||
//m_encapsulate("/tcp/8080");
|
||||
//m_decapsulate("tcp");
|
||||
//struct maddr b;
|
||||
//b=new_maddr_fb(a.bytes,sizeof(a.bytes));
|
||||
//printf("B STRING: %s\n",b.string);
|
||||
}
|
|
@ -52,45 +52,79 @@ uint32_t * Varint_To_Num_32(uint8_t TON32INPUT[10]) //VARINT TO UINT32_t
|
|||
//
|
||||
char * Int_To_Hex(uint64_t int2hex) //VAR[binformat] TO HEX
|
||||
{
|
||||
static char int2hex_result[10]="\0";
|
||||
static char int2hex_result[20]="\0";
|
||||
memset(int2hex_result,0,sizeof(int2hex_result));
|
||||
sprintf (int2hex_result, "%02lX", int2hex);
|
||||
return int2hex_result;
|
||||
}
|
||||
//
|
||||
char * Var_To_Hex(uint8_t TOHEXINPUT[10]) //VAR[binformat] TO HEX
|
||||
uint64_t Hex_To_Int(char *hex)
|
||||
{
|
||||
static char convert_resultz1[20]="\0";
|
||||
memset(convert_resultz1,0,sizeof(convert_resultz1));
|
||||
void convert(uint8_t buf[10])
|
||||
uint64_t val = 0;
|
||||
while (*hex)
|
||||
{
|
||||
char conv_proc[10]="\0";
|
||||
for(int i=0; i < 10; i++)
|
||||
// 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
|
||||
{
|
||||
if(TOHEXINPUT != NULL)
|
||||
{
|
||||
static char convert_resultz1[105]="\0";
|
||||
memset(convert_resultz1,0,sizeof(convert_resultz1));
|
||||
void convert(uint8_t * buf)
|
||||
{
|
||||
char conv_proc[100]="\0";
|
||||
for(int i=0; i < 100; i++)
|
||||
{
|
||||
sprintf (conv_proc, "%02X", buf[i]);
|
||||
//printf("%d:%d\n",i, buf[i]);
|
||||
strcat(convert_resultz1, conv_proc);
|
||||
if(buf[i]!=0)
|
||||
{
|
||||
sprintf (conv_proc, "%02X", buf[i]);
|
||||
//printf("%d:%d\n",i, buf[i]);
|
||||
strcat(convert_resultz1, conv_proc);
|
||||
}
|
||||
if(buf[i]==0 && buf[i+1]!=0)
|
||||
{
|
||||
sprintf (conv_proc, "%02X", buf[i]);
|
||||
//printf("%d:%d\n",i, buf[i]);
|
||||
strcat(convert_resultz1, conv_proc);
|
||||
}
|
||||
}
|
||||
}
|
||||
convert(TOHEXINPUT);
|
||||
return convert_resultz1;
|
||||
}
|
||||
}
|
||||
uint8_t * Hex_To_Var(char * Hexstr) //HEX TO VAR[BINFORMAT]
|
||||
{
|
||||
static uint8_t buffy_HEX[10] = {0};
|
||||
char codo[20] = "\0";
|
||||
static uint8_t buffy_HEX[105] = {0};
|
||||
for(int i=0;i<105;i++)
|
||||
{
|
||||
buffy_HEX[i] = 0;
|
||||
}
|
||||
char codo[105] = "\0";
|
||||
strcpy(codo, Hexstr);
|
||||
char code[3] = "\0";
|
||||
int x = 0;
|
||||
for(int i= 0;i<20;i++)
|
||||
int fori001=0;
|
||||
for(fori001=0;fori001<105;fori001++)
|
||||
{
|
||||
strncpy(&code[0],&codo[i],1);
|
||||
strncpy(&code[1],&codo[i+1],1);
|
||||
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;
|
||||
i++;
|
||||
//printf("%s - %lu\n",code,lu);
|
||||
fori001++;
|
||||
x++;
|
||||
}
|
||||
return buffy_HEX;
|
||||
|
|
Loading…
Reference in a new issue