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/dir.c | |
parent | 2a33ef2344145e12f1eea96214cdb317857321c1 (diff) |
updated directory service
Diffstat (limited to 'ecp/src/dir.c')
-rw-r--r-- | ecp/src/dir.c | 54 |
1 files changed, 52 insertions, 2 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; |