mirror of
https://github.com/hufrea/byedpi.git
synced 2024-11-21 14:35:22 +03:00
serialize directly from address
This commit is contained in:
parent
11a43db9f7
commit
ccbbf55fe9
1 changed files with 18 additions and 70 deletions
88
extend.c
88
extend.c
|
@ -23,6 +23,9 @@
|
|||
#include "desync.h"
|
||||
#include "packets.h"
|
||||
|
||||
#define KEY_SIZE sizeof(uint16_t) + \
|
||||
sizeof(sa_family_t) + sizeof(struct sockaddr_in6)
|
||||
|
||||
|
||||
int set_timeout(int fd, unsigned int s)
|
||||
{
|
||||
|
@ -44,18 +47,9 @@ int set_timeout(int fd, unsigned int s)
|
|||
return 0;
|
||||
}
|
||||
|
||||
struct key_struct {
|
||||
uint16_t port;
|
||||
// to detect correct union in raw
|
||||
sa_family_t family;
|
||||
union {
|
||||
struct in_addr i4;
|
||||
struct in6_addr i6;
|
||||
};
|
||||
};
|
||||
|
||||
int serialize_key_struct(const struct key_struct* const in, uint8_t* const out, const size_t out_len){
|
||||
|
||||
int serialize_addr(const struct sockaddr_ina *dst, uint8_t* const out, const size_t out_len)
|
||||
{
|
||||
// Not a function to return on error directly
|
||||
#define serialize(raw, field, len, counter){ \
|
||||
const size_t size = sizeof(field); \
|
||||
|
@ -64,86 +58,40 @@ int serialize_key_struct(const struct key_struct* const in, uint8_t* const out,
|
|||
counter += size; \
|
||||
}else return (counter + size); \
|
||||
}
|
||||
|
||||
// call order is important
|
||||
size_t c = 0;
|
||||
serialize(out, in->port, out_len, c);
|
||||
serialize(out, in->family, out_len, c);
|
||||
serialize(out, dst->in.sin_port, out_len, c);
|
||||
serialize(out, dst->sa.sa_family, out_len, c);
|
||||
|
||||
if(in->family == AF_INET){
|
||||
serialize(out, in->i4, out_len, c);
|
||||
}else{
|
||||
serialize(out, in->i6, out_len, c);
|
||||
if (dst->sa.sa_family == AF_INET) {
|
||||
serialize(out, dst->in.sin_addr, out_len, c);
|
||||
} else {
|
||||
serialize(out, dst->in6.sin6_addr, out_len, c);
|
||||
}
|
||||
#undef serialize
|
||||
|
||||
return 0;
|
||||
return (int )c;
|
||||
}
|
||||
|
||||
int deserialize_key_struct(const uint8_t* const in, const size_t in_len, struct key_struct* const out){
|
||||
|
||||
#define deserialize(raw, field, len, counter){ \
|
||||
const size_t size = sizeof(field); \
|
||||
if((counter + size) <= len){ \
|
||||
memcpy(&(field), raw + counter, size); \
|
||||
counter += size; \
|
||||
}else return (counter + size); \
|
||||
}
|
||||
|
||||
// call order is important
|
||||
size_t c = 0;
|
||||
deserialize(in, out->port, in_len, c);
|
||||
deserialize(in, out->family, in_len, c);
|
||||
|
||||
if(out->family == AF_INET){
|
||||
deserialize(in, out->i4, in_len, c);
|
||||
}else if(out->family == AF_INET6) {
|
||||
deserialize(in, out->i6, in_len, c);
|
||||
}else{
|
||||
// incorrect family
|
||||
return c;
|
||||
}
|
||||
#undef deserialize
|
||||
|
||||
return 0;
|
||||
}
|
||||
int mode_add_get(struct sockaddr_ina *dst, int m)
|
||||
{
|
||||
// m < 0: get, m > 0: set, m == 0: delete
|
||||
assert(m >= -1 && m < params.dp_count);
|
||||
|
||||
struct key_struct key = {0};
|
||||
|
||||
key.port = dst->in.sin_port;
|
||||
key.family = dst->sa.sa_family;
|
||||
|
||||
int len = sizeof(key.port) + sizeof(key.family);
|
||||
time_t t = 0;
|
||||
struct elem *val = 0;
|
||||
|
||||
if (key.family == AF_INET) {
|
||||
len += sizeof(dst->in.sin_addr);
|
||||
key.i4 = dst->in.sin_addr;
|
||||
}
|
||||
else {
|
||||
len += sizeof(dst->in6.sin6_addr);
|
||||
key.i6 = dst->in6.sin6_addr;
|
||||
}
|
||||
|
||||
// Arrays can't have padding
|
||||
uint8_t key_raw[sizeof(key) + 1] = {0};
|
||||
if(serialize_key_struct(&key, key_raw, sizeof(key_raw))){
|
||||
uint8_t key[KEY_SIZE] = { 0 };
|
||||
int len = serialize_addr(dst, key, sizeof(key));
|
||||
if (len < 0) {
|
||||
return -1;
|
||||
}
|
||||
if (m < 0) {
|
||||
val = mem_get(params.mempool, (char*)key_raw, len);
|
||||
val = mem_get(params.mempool, (char *)key, len);
|
||||
if (!val) {
|
||||
return -1;
|
||||
}
|
||||
/*
|
||||
if(deserialize_key_struct((const uint8_t*) val->data, val->len, &key))
|
||||
return -1;
|
||||
*/
|
||||
time(&t);
|
||||
if (t > val->time + params.cache_ttl) {
|
||||
LOG(LOG_S, "time=%jd, now=%jd, ignore\n", (intmax_t)val->time, (intmax_t)t);
|
||||
|
@ -155,14 +103,14 @@ int mode_add_get(struct sockaddr_ina *dst, int m)
|
|||
|
||||
if (m == 0) {
|
||||
LOG(LOG_S, "delete ip: %s\n", ADDR_STR);
|
||||
mem_delete(params.mempool, (char*)key_raw, len);
|
||||
mem_delete(params.mempool, (char *)key, len);
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
LOG(LOG_S, "save ip: %s, m=%d\n", ADDR_STR, m);
|
||||
time(&t);
|
||||
|
||||
val = mem_add(params.mempool, (char*)key_raw, len);
|
||||
val = mem_add(params.mempool, (char *)key, len);
|
||||
if (!val) {
|
||||
uniperror("mem_add");
|
||||
return -1;
|
||||
|
|
Loading…
Reference in a new issue