mirror of
https://github.com/oxen-io/lokinet
synced 2023-12-14 06:53:00 +01:00
more gutts
This commit is contained in:
parent
76d885d39e
commit
45cd73b1c2
|
@ -52,6 +52,96 @@ static bool INLINE bencode_write_version_entry(llarp_buffer_t* buff) {
|
|||
return llarp_buffer_writef(buff, "1:vi%de", LLARP_PROTO_VERSION);
|
||||
}
|
||||
|
||||
static bool INLINE bdecode_read_integer(struct llarp_buffer_t * buffer, int64_t * result)
|
||||
{
|
||||
size_t len;
|
||||
if(*buffer->cur != 'i')
|
||||
return false;
|
||||
|
||||
char numbuf[32];
|
||||
|
||||
len = llarp_buffer_read_until(buffer, 'e', numbuf, sizeof(numbuf)-1);
|
||||
if(!len)
|
||||
return false;
|
||||
|
||||
numbuf[len] = 0;
|
||||
*result = atol(numbuf);
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool INLINE bdecode_read_string(llarp_buffer_t * buffer, llarp_buffer_t * result)
|
||||
{
|
||||
size_t len, slen;
|
||||
char numbuf[10];
|
||||
|
||||
len = llarp_buffer_read_until(buffer, ':', numbuf, sizeof(numbuf)-1);
|
||||
if(!len)
|
||||
return false;
|
||||
|
||||
buffer->cur ++;
|
||||
numbuf[len] = 0;
|
||||
slen = atoi(numbuf);
|
||||
if(slen <= 0)
|
||||
return false;
|
||||
|
||||
len = llarp_buffer_size_left(buffer);
|
||||
if (len > slen)
|
||||
return false;
|
||||
|
||||
result->base = buffer->cur;
|
||||
buffer->cur += slen;
|
||||
result->sz = slen;
|
||||
buffer->cur ++;
|
||||
return true;
|
||||
}
|
||||
|
||||
struct dict_reader
|
||||
{
|
||||
llarp_buffer_t * buffer;
|
||||
void * user;
|
||||
/**
|
||||
* called when we got a key string, return true to continue iteration
|
||||
* called with null key on done iterating
|
||||
*/
|
||||
bool (*on_key)(struct dict_reader *, llarp_buffer_t *);
|
||||
};
|
||||
|
||||
static bool INLINE bdecode_read_dict(llarp_buffer_t * buff, struct dict_reader * r)
|
||||
{
|
||||
llarp_buffer_t strbuf;
|
||||
r->buffer = buff;
|
||||
while(llarp_buffer_size_left(buff) && *buff->cur != 'e')
|
||||
{
|
||||
if(bdecode_read_string(buff, &strbuf))
|
||||
{
|
||||
if(!r->on_key(r, &strbuf)) return false;
|
||||
}
|
||||
}
|
||||
|
||||
return *buff->cur == 'e' && r->on_key(r, 0);
|
||||
}
|
||||
|
||||
struct list_reader
|
||||
{
|
||||
llarp_buffer_t * buffer;
|
||||
void * user;
|
||||
bool (*on_item) (struct list_reader *, bool);
|
||||
};
|
||||
|
||||
static bool INLINE bdecode_read_list(llarp_buffer_t * buff, struct list_reader * r)
|
||||
{
|
||||
r->buffer = buff;
|
||||
if(*buff->cur != 'l')
|
||||
return false;
|
||||
|
||||
buff->cur ++;
|
||||
while(llarp_buffer_size_left(buff) && *buff->cur != 'e')
|
||||
{
|
||||
if(!r->on_item(r, true)) return false;
|
||||
}
|
||||
return *buff->cur == 'e' && r->on_item(r, false);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -25,6 +25,11 @@ bool llarp_buffer_writef(llarp_buffer_t *buff, const char *fmt, ...);
|
|||
bool llarp_buffer_readfile(llarp_buffer_t *buff, FILE *f,
|
||||
struct llarp_alloc *mem);
|
||||
|
||||
size_t llarp_buffer_read_until(llarp_buffer_t * buff, char delim, char * result, size_t resultlen);
|
||||
|
||||
bool llarp_buffer_eq(llarp_buffer_t buff, const char * data);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -4,6 +4,11 @@
|
|||
#include <llarp/mem.h>
|
||||
#include <llarp/string.h>
|
||||
|
||||
bool llarp_ai_bdecode(struct llarp_ai *ai, llarp_buffer_t *buff)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool llarp_ai_bencode(struct llarp_ai *ai, llarp_buffer_t *buff) {
|
||||
char ipbuff[128] = {0};
|
||||
const char *ipstr;
|
||||
|
@ -88,3 +93,8 @@ void llarp_ai_list_iterate(struct llarp_ai_list *l,
|
|||
cur = cur->next;
|
||||
};
|
||||
}
|
||||
|
||||
bool llarp_ai_list_bdecode(struct llarp_ai_list * l, llarp_buffer_t * buff)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -49,4 +49,35 @@ bool llarp_buffer_readfile(llarp_buffer_t* buff, FILE* f, llarp_alloc* mem) {
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t llarp_buffer_read_until(llarp_buffer_t * buff, char delim, char * result, size_t resultsize)
|
||||
{
|
||||
size_t read = 0;
|
||||
|
||||
while(*buff->cur != delim && resultsize && (buff->cur != buff->base + buff->sz))
|
||||
{
|
||||
*result = *buff->cur;
|
||||
buff->cur ++;
|
||||
result ++;
|
||||
resultsize --;
|
||||
read ++;
|
||||
}
|
||||
|
||||
if(llarp_buffer_size_left(buff))
|
||||
return read;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool llarp_buffer_eq(llarp_buffer_t buf, const char * str)
|
||||
{
|
||||
while(*str && buf.cur != (buf.base + buf.sz))
|
||||
{
|
||||
if(*buf.cur != *str) return false;
|
||||
buf.cur ++;
|
||||
str++;
|
||||
}
|
||||
return *str == 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -87,3 +87,9 @@ bool llarp_xi_bencode(struct llarp_xi *xi, llarp_buffer_t *buff) {
|
|||
|
||||
return bencode_end(buff);
|
||||
}
|
||||
|
||||
|
||||
bool llarp_xi_list_bdecode(struct llarp_xi_list * l, llarp_buffer_t * buff)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -7,6 +7,70 @@ void llarp_rc_free(struct llarp_rc *rc) {
|
|||
llarp_ai_list_free(&rc->addrs);
|
||||
}
|
||||
|
||||
|
||||
static bool llarp_rc_decode_dict(struct dict_reader * r, llarp_buffer_t * key)
|
||||
{
|
||||
int64_t v;
|
||||
llarp_buffer_t strbuf;
|
||||
struct llarp_rc * rc = r->user;
|
||||
|
||||
if(!key) return true;
|
||||
|
||||
if(llarp_buffer_eq(*key, "a"))
|
||||
{
|
||||
rc->addrs = llarp_ai_list_new();
|
||||
return llarp_ai_list_bdecode(rc->addrs, r->buffer);
|
||||
}
|
||||
|
||||
if(llarp_buffer_eq(*key, "k"))
|
||||
{
|
||||
if(!bdecode_read_string(r->buffer, &strbuf))
|
||||
return false;
|
||||
if(strbuf.sz != sizeof(llarp_pubkey_t))
|
||||
return false;
|
||||
memcpy(rc->pubkey, strbuf.base, sizeof(llarp_pubkey_t));
|
||||
return true;
|
||||
}
|
||||
|
||||
if(llarp_buffer_eq(*key, "v"))
|
||||
{
|
||||
if(!bdecode_read_integer(r->buffer, &v))
|
||||
return false;
|
||||
return v == LLARP_PROTO_VERSION;
|
||||
}
|
||||
|
||||
if(llarp_buffer_eq(*key, "x"))
|
||||
{
|
||||
rc->exits = llarp_xi_list_new();
|
||||
return llarp_xi_list_bdecode(rc->exits, r->buffer);
|
||||
}
|
||||
|
||||
if(llarp_buffer_eq(*key, "z"))
|
||||
{
|
||||
if(!bdecode_read_string(r->buffer, &strbuf))
|
||||
return false;
|
||||
if(strbuf.sz != sizeof(llarp_sig_t))
|
||||
return false;
|
||||
memcpy(rc->signature, strbuf.base, sizeof(llarp_sig_t));
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool llarp_rc_bdecode(struct llarp_rc * rc, llarp_buffer_t *buff) {
|
||||
struct dict_reader r = {
|
||||
.user = rc,
|
||||
.on_key = &llarp_rc_decode_dict
|
||||
};
|
||||
return bdecode_read_dict(buff, &r);
|
||||
}
|
||||
|
||||
bool llarp_rc_verify_sig(struct llarp_rc * rc)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool llarp_rc_bencode(struct llarp_rc *rc, llarp_buffer_t *buff) {
|
||||
/* write dict begin */
|
||||
if (!bencode_start_dict(buff)) return false;
|
||||
|
|
Loading…
Reference in a new issue