diff options
author | Uros Majstorovic <majstor@majstor.org> | 2021-09-01 03:47:19 +0200 |
---|---|---|
committer | Uros Majstorovic <majstor@majstor.org> | 2021-09-01 03:47:19 +0200 |
commit | b98a39ddae6a9cd002465bd46c19a1cb9285174b (patch) | |
tree | 1d18ab7cd34b2f07b4fbb313f845111d4d09b847 /ecp/src | |
parent | 2a33ef2344145e12f1eea96214cdb317857321c1 (diff) |
updated directory service
Diffstat (limited to 'ecp/src')
-rw-r--r-- | ecp/src/dir.c | 54 | ||||
-rw-r--r-- | ecp/src/dir.h | 7 | ||||
-rw-r--r-- | ecp/src/dir_srv.c | 52 |
3 files changed, 66 insertions, 47 deletions
diff --git a/ecp/src/dir.c b/ecp/src/dir.c index 81f564e..8bc7a94 100644 --- a/ecp/src/dir.c +++ b/ecp/src/dir.c @@ -3,7 +3,57 @@ #include "dir.h" -void ecp_dir_parse_item(unsigned char *buf, ECPDirItem *item) { +static int dir_update(ECPDirList *list, ECPDirItem *item) { + int i; + + for (i=0; i<list->count; i++) { + if (memcmp(ecp_cr_dh_pub_get_buf(&list->item[i].node.public), ecp_cr_dh_pub_get_buf(&item->node.public), ECP_ECDH_SIZE_KEY) == 0) { + return ECP_OK; + } + } + + if (list->count == ECP_MAX_DIR_ITEM) return ECP_ERR_SIZE; + + list->item[list->count] = *item; + list->count++; + + return ECP_OK; +} + +ssize_t ecp_dir_parse(ECPDirList *list, unsigned char *buf, size_t buf_size) { + ECPDirItem item; + size_t size; + int rv; + + size = buf_size; + while (size >= ECP_SIZE_DIR_ITEM) { + ecp_dir_item_parse(&item, buf); + + rv = dir_update(list, &item); + if (rv) return rv; + + buf += ECP_SIZE_DIR_ITEM; + size -= ECP_SIZE_DIR_ITEM; + }; + + return buf_size - size; +} + +int ecp_dir_serialize(ECPDirList *list, unsigned char *buf, size_t buf_size) { + int i; + + for (i=0; i<list->count; i++) { + if (buf_size < ECP_SIZE_DIR_ITEM) return ECP_ERR_SIZE; + + ecp_dir_item_serialize(&list->item[i], buf); + buf += ECP_SIZE_DIR_ITEM; + buf_size -= ECP_SIZE_DIR_ITEM; + } + + return ECP_OK; +} + +void ecp_dir_item_parse(ECPDirItem *item, unsigned char *buf) { ecp_cr_dh_pub_from_buf(&item->node.public, buf); buf += ECP_ECDH_SIZE_KEY; @@ -21,7 +71,7 @@ void ecp_dir_parse_item(unsigned char *buf, ECPDirItem *item) { buf += sizeof(uint16_t); } -void ecp_dir_serialize_item(unsigned char *buf, ECPDirItem *item) { +void ecp_dir_item_serialize(ECPDirItem *item, unsigned char *buf) { ecp_cr_dh_pub_to_buf(buf, &item->node.public); buf += ECP_ECDH_SIZE_KEY; diff --git a/ecp/src/dir.h b/ecp/src/dir.h index ae8daa9..2fcc50f 100644 --- a/ecp/src/dir.h +++ b/ecp/src/dir.h @@ -11,5 +11,8 @@ typedef struct ECPDirList { uint16_t count; } ECPDirList; -void ecp_dir_parse_item(unsigned char *buf, ECPDirItem *item); -void ecp_dir_serialize_item(unsigned char *buf, ECPDirItem *item); +ssize_t ecp_dir_parse(ECPDirList *list, unsigned char *buf, size_t buf_size); +int ecp_dir_serialize(ECPDirList *list, unsigned char *buf, size_t buf_size); + +void ecp_dir_item_parse(ECPDirItem *item, unsigned char *buf); +void ecp_dir_item_serialize(ECPDirItem *item, unsigned char *buf); diff --git a/ecp/src/dir_srv.c b/ecp/src/dir_srv.c index 7513c4a..f5d1893 100644 --- a/ecp/src/dir_srv.c +++ b/ecp/src/dir_srv.c @@ -12,45 +12,16 @@ int ecp_dir_init(ECPContext *ctx, ECPDirList *dir_online, ECPDirList *dir_shadow return ECP_OK; } -int ecp_dir_update(ECPDirList *list, ECPDirItem *item) { - int i; - - for (i=0; i<list->count; i++) { - if (memcmp(ecp_cr_dh_pub_get_buf(&list->item[i].node.public), ecp_cr_dh_pub_get_buf(&item->node.public), ECP_ECDH_SIZE_KEY) == 0) { - return ECP_OK; - } - } - - if (list->count == ECP_MAX_DIR_ITEM) return ECP_ERR_SIZE; - - list->item[list->count] = *item; - list->count++; - - return ECP_OK; -} - ssize_t ecp_dir_handle_update(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size, ECP2Buffer *b) { ECPContext *ctx = conn->sock->ctx; ECPDirList *dir_shadow = ctx->dir_shadow; - ECPDirItem item; - size_t _size; - _size = size; if (mtype == ECP_MTYPE_DIR_REQ) { - int rv; - - while (_size >= ECP_SIZE_DIR_ITEM) { - ecp_dir_parse_item(msg, &item); - - rv = ecp_dir_update(dir_shadow, &item); - if (rv) return rv; - - msg += ECP_SIZE_DIR_ITEM; - _size -= ECP_SIZE_DIR_ITEM; - }; + return ecp_dir_parse(dir_shadow, msg, size); + } else { + return ECP_ERR; } - return size - _size; } int ecp_dir_handle_req(ECPSocket *sock, ECPNetAddr *addr, ECPConnection *parent, unsigned char *msg, size_t msg_size, ECPPktMeta *pkt_meta, ECP2Buffer *bufs, ECPConnection **_conn) { @@ -58,23 +29,18 @@ int ecp_dir_handle_req(ECPSocket *sock, ECPNetAddr *addr, ECPConnection *parent, ECPBuffer *packet = bufs->packet; ECPBuffer *payload = bufs->payload; ECPDirList *dir_online = ctx->dir_online; - ssize_t rv; - int i; + ssize_t _rv; + int rv; ecp_pld_set_type(payload->buffer, payload->size, ECP_MTYPE_DIR_REP); msg = ecp_pld_get_buf(payload->buffer, payload->size); msg_size = payload->size - (msg - payload->buffer); - for (i=0; i<dir_online->count; i++) { - if (msg_size < ECP_SIZE_DIR_ITEM) return ECP_ERR_SIZE; - - ecp_dir_serialize_item(msg, &dir_online->item[i]); - msg += ECP_SIZE_DIR_ITEM; - msg_size -= ECP_SIZE_DIR_ITEM; - } + rv = ecp_dir_serialize(dir_online, msg, msg_size); + if (rv) return rv; - rv = ecp_pld_send_tr(sock, addr, parent, packet, pkt_meta, payload, ECP_SIZE_PLD(i * ECP_SIZE_DIR_ITEM, ECP_MTYPE_DIR_REP), 0); - if (rv < 0) return rv; + _rv = ecp_pld_send_tr(sock, addr, parent, packet, pkt_meta, payload, ECP_SIZE_PLD(dir_online->count * ECP_SIZE_DIR_ITEM, ECP_MTYPE_DIR_REP), 0); + if (_rv < 0) return _rv; return ECP_OK; } |