diff options
author | Uros Majstorovic <majstor@majstor.org> | 2024-05-14 22:59:12 +0200 |
---|---|---|
committer | Uros Majstorovic <majstor@majstor.org> | 2024-05-14 22:59:12 +0200 |
commit | 5903b0819664f473d1279de57e8b00d60d76ae0e (patch) | |
tree | 40b5b7b9dc9bf5f2399e8720ff43d357dc19cf57 /ecp/src | |
parent | 7de54ce67127e010fde241089f262421818ea7be (diff) |
dir protocol improvement
Diffstat (limited to 'ecp/src')
-rw-r--r-- | ecp/src/ecp/dir/dir.c | 62 | ||||
-rw-r--r-- | ecp/src/ecp/dir/dir.h | 12 |
2 files changed, 55 insertions, 19 deletions
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); |