c-multiaddr/multiaddr.h

125 lines
2.1 KiB
C
Raw Normal View History

2016-11-06 01:57:36 +00:00
#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
{
2016-11-08 11:02:45 +00:00
char string[200];
2016-11-06 01:57:36 +00:00
uint8_t bytes[100];
2016-11-08 11:02:45 +00:00
int bsize[1];
2016-11-06 01:57:36 +00:00
};
struct maddr new_maddr_fb(uint8_t * byteaddress,int size)//Construct new address from bytes
{
2016-11-08 11:02:45 +00:00
struct maddr anewaddr2;
2016-11-06 01:57:36 +00:00
if(byteaddress!=NULL)
{
2016-11-08 11:02:45 +00:00
memcpy(anewaddr2.bytes, byteaddress,size);
if(bytes_to_string(anewaddr2.string,byteaddress,size)==1)
2016-11-06 01:57:36 +00:00
{
2016-11-08 11:02:45 +00:00
return anewaddr2;
2016-11-06 01:57:36 +00:00
}
}
}
struct maddr new_maddr_fs(char * straddress)//Construct new address from string
{
struct maddr anewaddr;
2016-11-08 11:02:45 +00:00
bzero(anewaddr.string, 100);
2016-11-06 01:57:36 +00:00
strcpy(anewaddr.string, straddress);
2016-11-08 11:02:45 +00:00
anewaddr.bsize[0] = 0;
if(string_to_bytes(anewaddr.bytes,anewaddr.bsize,anewaddr.string,sizeof(anewaddr.string))==1)
2016-11-06 01:57:36 +00:00
{
2016-11-08 11:02:45 +00:00
int betta;
//printf("BSIZE: %u\n", anewaddr.bsize[0]);
for(betta=anewaddr.bsize[0];betta<100;betta++)
{
anewaddr.bytes[betta] = '\0';
}
2016-11-06 01:57:36 +00:00
return anewaddr;
}
}
int m_encapsulate(struct maddr * result, char * string)
{
if(result!=NULL&&string!=NULL)
{
int success = 0;
2016-11-08 11:02:45 +00:00
char pstr[100];
2016-11-06 01:57:36 +00:00
strcpy(pstr,result->string);
strcat(pstr,string+1);
2016-11-08 11:02:45 +00:00
if(string_to_bytes(result->bytes,result->bsize,pstr,sizeof(pstr)))
2016-11-06 01:57:36 +00:00
{
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;
}
}
2016-11-08 11:02:45 +00:00
2016-11-06 01:57:36 +00:00
#endif