summaryrefslogtreecommitdiff
path: root/ecp
diff options
context:
space:
mode:
authorUros Majstorovic <majstor@majstor.org>2024-05-14 22:59:12 +0200
committerUros Majstorovic <majstor@majstor.org>2024-05-14 22:59:12 +0200
commit5903b0819664f473d1279de57e8b00d60d76ae0e (patch)
tree40b5b7b9dc9bf5f2399e8720ff43d357dc19cf57 /ecp
parent7de54ce67127e010fde241089f262421818ea7be (diff)
dir protocol improvement
Diffstat (limited to 'ecp')
-rw-r--r--ecp/server/dir.c14
-rw-r--r--ecp/server/dir.h6
-rw-r--r--ecp/src/ecp/dir/dir.c62
-rw-r--r--ecp/src/ecp/dir/dir.h12
4 files changed, 64 insertions, 30 deletions
diff --git a/ecp/server/dir.c b/ecp/server/dir.c
index 149d14d..91d364f 100644
--- a/ecp/server/dir.c
+++ b/ecp/server/dir.c
@@ -182,10 +182,10 @@ ssize_t dir_send_shadow(ECPConnection *conn) {
pthread_mutex_lock(&node->mutex);
if (node->verified || node->ann_local) {
- if (count < MAX_DIR_ITEM_IN_MSG) {
+ if (count < ECP_MAX_DIR_ITEM_IN_MSG) {
size_t rv_ser;
- rv_ser = ecp_dir_item_serialize(&node->dir_item, _msg);
+ rv_ser = ecp_dir_serialize_item(&node->dir_item, _msg);
_msg += rv_ser;
_msg_size += rv_ser;
count++;
@@ -332,7 +332,7 @@ ssize_t dir_handle_shadow(ECPConnection *conn, unsigned char *msg, size_t msg_si
ECPDirItem dir_item;
size_t rv;
- rv = ecp_dir_item_parse(&dir_item, msg);
+ rv = ecp_dir_parse_item(&dir_item, msg);
msg += rv;
if (dir_item.region >= MAX_REGION) {
@@ -606,11 +606,11 @@ void dir_online_switch(ECPSocket *sock, int inc_serial) {
if (dir_online) {
size_t rv_ser;
- rv_ser = ecp_dir_item_serialize(&node->dir_item, msg[0]);
+ rv_ser = ecp_dir_serialize_item(&node->dir_item, msg[0]);
msg[0] += rv_ser;
count[0]++;
if (region) {
- rv_ser = ecp_dir_item_serialize(&node->dir_item, msg[region]);
+ rv_ser = ecp_dir_serialize_item(&node->dir_item, msg[region]);
msg[region] += rv_ser;
count[region]++;
}
@@ -642,13 +642,13 @@ void dir_online_switch(ECPSocket *sock, int inc_serial) {
if (node->conn) ecp_conn_set_uflags(node->conn, DIR_UFLAG_RECONNECT);
if (dir_online && verified) {
- if (count[0] == MAX_DIR_ITEM_IN_MSG) {
+ if (count[0] == ECP_MAX_DIR_ITEM_IN_MSG) {
dir_online->list[0].msg[msg_count[0]].count = count[0];
msg_count[0]++;
msg[0] = dir_online->list[0].msg[msg_count[0]].buffer;
count[0] = 0;
}
- if (region && count[region] == MAX_DIR_ITEM_IN_MSG) {
+ if (region && count[region] == ECP_MAX_DIR_ITEM_IN_MSG) {
dir_online->list[region].msg[msg_count[region]].count = count[region];
msg_count[region]++;
msg[region] = dir_online->list[region].msg[msg_count[region]].buffer;
diff --git a/ecp/server/dir.h b/ecp/server/dir.h
index 022010a..fee3a5e 100644
--- a/ecp/server/dir.h
+++ b/ecp/server/dir.h
@@ -1,7 +1,5 @@
-#define MAX_DIR_ITEM_IN_MSG ((ECP_MAX_PLD - ECP_SIZE_PLD((2 + sizeof(uint16_t)), ECP_MTYPE_DIR_REP)) / ECP_SIZE_DIR_ITEM)
-
#define MAX_DIR_MSG 10
-#define MAX_DIR_ITEM (MAX_DIR_MSG * MAX_DIR_ITEM_IN_MSG)
+#define MAX_DIR_ITEM (MAX_DIR_MSG * ECP_MAX_DIR_ITEM_IN_MSG)
#define MAX_DIR_ITEM_DIR 100
#define MAX_REGION 10
@@ -42,7 +40,7 @@ typedef struct DIRNode {
typedef struct DIRList {
struct {
uint8_t count;
- unsigned char buffer[MAX_DIR_ITEM_IN_MSG * ECP_SIZE_DIR_ITEM];
+ unsigned char buffer[ECP_MAX_DIR_ITEM_IN_MSG * ECP_SIZE_DIR_ITEM];
} msg[MAX_DIR_MSG];
uint8_t msg_count;
} DIRList;
diff --git a/ecp/src/ecp/dir/dir.c b/ecp/src/ecp/dir/dir.c
index e329054..6312eb7 100644
--- a/ecp/src/ecp/dir/dir.c
+++ b/ecp/src/ecp/dir/dir.c
@@ -7,15 +7,21 @@
#include "dir.h"
-int ecp_dir_item_eq(ECPDirItem *item1, ECPDirItem *item2) {
- if (item1->region != item2->region) return 0;
- if (item1->roles != item2->roles) return 0;
- if (!item1->node.key_perma.valid || !item1->node.key_perma.valid) return 0;
- if (memcmp(&item1->node.key_perma.public, &item2->node.key_perma.public, sizeof(item1->node.key_perma.public)) != 0) return 0;
- return ecp_tr_addr_eq(&item1->node.addr, &item2->node.addr);
+ssize_t ecp_dir_parse_msg(unsigned char *msg, size_t msg_size, unsigned char *count, unsigned char *region, uint16_t *serial) {
+ if (msg_size < 2 + sizeof(uint16_t)) return ECP_ERR_SIZE;
+
+ *count = msg[0];
+ *region = msg[1];
+ *serial = (uint16_t)msg[2] << 8;
+ *serial |= (uint16_t)msg[3];
+
+ if (msg_size < 2 + sizeof(uint16_t) + *count * ECP_SIZE_DIR_ITEM) return ECP_ERR_SIZE;
+ if (*count >= ECP_MAX_DIR_ITEM_IN_MSG) return ECP_ERR;
+
+ return 2 + sizeof(uint16_t);
}
-size_t ecp_dir_item_parse(ECPDirItem *item, unsigned char *buf) {
+size_t ecp_dir_parse_item(ECPDirItem *item, unsigned char *buf) {
ECPDHPub *key;
ecp_tr_addr_t *addr;
size_t rsize = 0;
@@ -48,7 +54,7 @@ size_t ecp_dir_item_parse(ECPDirItem *item, unsigned char *buf) {
return rsize;
}
-size_t ecp_dir_item_serialize(ECPDirItem *item, unsigned char *buf) {
+size_t ecp_dir_serialize_item(ECPDirItem *item, unsigned char *buf) {
ECPDHPub *key;
ecp_tr_addr_t *addr;
size_t rsize = 0;
@@ -77,12 +83,23 @@ size_t ecp_dir_item_serialize(ECPDirItem *item, unsigned char *buf) {
return rsize;
}
+int ecp_dir_item_eq(ECPDirItem *item1, ECPDirItem *item2) {
+ if (item1->region != item2->region) return 0;
+ if (item1->roles != item2->roles) return 0;
+ if (!item1->node.key_perma.valid || !item1->node.key_perma.valid) return 0;
+ if (memcmp(&item1->node.key_perma.public, &item2->node.key_perma.public, sizeof(item1->node.key_perma.public)) != 0) return 0;
+ return ecp_tr_addr_eq(&item1->node.addr, &item2->node.addr);
+}
+
int ecp_dir_handle_open(ECPConnection *conn, ECP2Buffer *b) {
+ unsigned char region;
ssize_t rv;
- if (ecp_conn_is_inb(conn)) return ECP_OK;
+ if (ecp_conn_is_inb(conn)) return ECP_ERR;
+
+ region = (unsigned char)ecp_conn_set_param(conn, NULL);
- rv = ecp_dir_send_req(conn);
+ rv = ecp_dir_send_req(conn, region);
if (rv < 0) return rv;
return ECP_OK;
@@ -91,22 +108,37 @@ int ecp_dir_handle_open(ECPConnection *conn, ECP2Buffer *b) {
static ssize_t _dir_send_req(ECPConnection *conn, ECPTimerItem *ti) {
ECPBuffer packet;
ECPBuffer payload;
- unsigned char pkt_buf[ECP_SIZE_PKT_BUF(ECP_SIZE_PLD(0, ECP_MTYPE_DIR_REQ), conn)];
- unsigned char pld_buf[ECP_SIZE_PLD_BUF(ECP_SIZE_PLD(0, ECP_MTYPE_DIR_REQ), conn)];
+ unsigned char pkt_buf[ECP_SIZE_PKT_BUF(ECP_SIZE_PLD(1, ECP_MTYPE_DIR_REQ), conn)];
+ unsigned char pld_buf[ECP_SIZE_PLD_BUF(ECP_SIZE_PLD(1, ECP_MTYPE_DIR_REQ), conn)];
+ unsigned char *msg;
+ unsigned char region;
packet.buffer = pkt_buf;
packet.size = sizeof(pkt_buf);
payload.buffer = pld_buf;
payload.size = sizeof(pld_buf);
+ region = (unsigned char)ecp_timer_get_param(ti);
+
ecp_pld_set_type(payload.buffer, payload.size, ECP_MTYPE_DIR_REQ);
- return ecp_pld_send_wtimer(conn, &packet, &payload, ECP_SIZE_PLD(0, ECP_MTYPE_DIR_REQ), 0, ti);
+ msg = ecp_pld_get_msg(payload.buffer, payload.size);
+ msg[0] = region;
+
+ return ecp_pld_send_wtimer(conn, &packet, &payload, ECP_SIZE_PLD(1, ECP_MTYPE_DIR_REQ), 0, ti);
}
-ssize_t ecp_dir_send_req(ECPConnection *conn) {
- return ecp_timer_send(conn, _dir_send_req, ECP_MTYPE_DIR_REP, ECP_SEND_TRIES, ECP_SEND_TIMEOUT);
+ssize_t ecp_dir_send_req(ECPConnection *conn, unsigned char region) {
+ return ecp_timer_send(conn, _dir_send_req, ECP_MTYPE_DIR_REP, ECP_SEND_TRIES, ECP_SEND_TIMEOUT, (void *)region);
}
void ecp_dir_conn_init(ECPConnection *conn, ECPSocket *sock) {
ecp_conn_init(conn, sock, ECP_CTYPE_DIR);
}
+
+int ecp_dir_request(ECPConnection *conn, ECPNode *node, unsigned char region) {
+ int rv;
+
+ ecp_conn_set_param(conn, (void *)region);
+ rv = ecp_conn_open(conn, node);
+ return rv;
+}
diff --git a/ecp/src/ecp/dir/dir.h b/ecp/src/ecp/dir/dir.h
index f88cf7c..5db33f1 100644
--- a/ecp/src/ecp/dir/dir.h
+++ b/ecp/src/ecp/dir/dir.h
@@ -1,3 +1,4 @@
+#define ECP_MAX_DIR_ITEM_IN_MSG ((ECP_MAX_PLD - ECP_SIZE_PLD((2 + sizeof(uint16_t)), ECP_MTYPE_DIR_REP)) / ECP_SIZE_DIR_ITEM)
#define ECP_SIZE_DIR_ITEM 40
#define ECP_MTYPE_DIR_REQ 0x00
@@ -14,10 +15,13 @@ typedef struct ECPDirItem {
uint8_t roles;
} ECPDirItem;
+ssize_t ecp_dir_parse_msg(unsigned char *msg, size_t msg_size, unsigned char *count, unsigned char *region, uint16_t *serial);
+size_t ecp_dir_parse_item(ECPDirItem *item, unsigned char *buf);
+size_t ecp_dir_serialize_item(ECPDirItem *item, unsigned char *buf);
int ecp_dir_item_eq(ECPDirItem *item1, ECPDirItem *item2);
-size_t ecp_dir_item_parse(ECPDirItem *item, unsigned char *buf);
-size_t ecp_dir_item_serialize(ECPDirItem *item, unsigned char *buf);
int ecp_dir_handle_open(ECPConnection *conn, ECP2Buffer *b);
-ssize_t ecp_dir_send_req(ECPConnection *conn);
-void ecp_dir_conn_init(ECPConnection *conn, ECPSocket *sock); \ No newline at end of file
+ssize_t ecp_dir_send_req(ECPConnection *conn, unsigned char region);
+
+void ecp_dir_conn_init(ECPConnection *conn, ECPSocket *sock);
+int ecp_dir_request(ECPConnection *conn, ECPNode *node, unsigned char region);