From 8b67936227a9ece9eab9dd9d47f22b348fa5dcc0 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Sat, 30 Mar 2024 03:55:25 +0400 Subject: [PATCH] In addrbook_t name was renamed to alias. addrbook_get() to addrbook_get_by_pk(). A func addrbook_get_by_alias() was added. --- src/addrbook.c | 23 +++++++++++++++++------ src/addrbook.h | 7 ++++--- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/addrbook.c b/src/addrbook.c index 0f6bdee..a52858e 100644 --- a/src/addrbook.c +++ b/src/addrbook.c @@ -8,10 +8,10 @@ #define ADDRBOOK_DELIM_COUNT 4 char *addrbook_entry_to_string(const addrbook_entry_t *const ent) { - size_t str_len = CRYPT_PKEY_HEXLEN + ent->name_len + ent->last_addr_len + size_t str_len = CRYPT_PKEY_HEXLEN + ent->alias_len + ent->last_addr_len + ent->last_port_len + ADDRBOOK_DELIM_COUNT; char *str = (char *)malloc(str_len * sizeof(char)); - sprintf(str, "%.128s\t%.*s\t%.*s\t%.*s\n", ent->pubk, ent->name_len, ent->name, + sprintf(str, "%.128s\t%.*s\t%.*s\t%.*s\n", ent->pubk, ent->alias_len, ent->alias, ent->last_addr_len, ent->last_addr, ent->last_port_len, ent->last_port); return str; } @@ -23,8 +23,8 @@ int addrbook_entry_from_string(addrbook_entry_t *const ent, char * str) { memcpy(ent->pubk, tok, CRYPT_PKEY_HEXLEN); tok = strtok(NULL, "\t\n"); - ent->name = strdup(tok); - ent->name_len = strlen(ent->name); + ent->alias = strdup(tok); + ent->alias_len = strlen(ent->alias); tok = strtok(NULL, "\t\n"); ent->last_addr = strdup(tok); @@ -94,7 +94,7 @@ int addrbook_add(addrbook_t *const ab, const addrbook_entry_t *const ent) { ab->entries = (addrbook_entry_t *)malloc(sizeof(addrbook_entry_t)); ab->entries_cap = 1; ab->entries_len = 0; - } else if (addrbook_get(ab, ent->pubk) != NULL) + } else if (addrbook_get_by_pk(ab, ent->pubk) != NULL) return -1; if (ab->entries_len == ab->entries_cap) @@ -105,7 +105,7 @@ int addrbook_add(addrbook_t *const ab, const addrbook_entry_t *const ent) { return 0; } -addrbook_entry_t *addrbook_get(const addrbook_t *const ab, const char pkey[CRYPT_PKEY_HEXLEN]) { +addrbook_entry_t *addrbook_get_by_pk(const addrbook_t *const ab, const char pkey[CRYPT_PKEY_HEXLEN]) { if (ab->entries == NULL) return NULL; @@ -115,3 +115,14 @@ addrbook_entry_t *addrbook_get(const addrbook_t *const ab, const char pkey[CRYPT return NULL; } + +addrbook_entry_t *addrbook_get_by_alias(const addrbook_t *const ab, const char *const alias) { + if (ab->entries == NULL) + return NULL; + + for (size_t i = 0; i < ab->entries_len; ++i) + if (strncmp(ab->entries[i].alias, alias, CRYPT_PKEY_HEXLEN) == 0) + return &ab->entries[i]; + + return NULL; +} diff --git a/src/addrbook.h b/src/addrbook.h index 8d0ec9b..cfcf14e 100644 --- a/src/addrbook.h +++ b/src/addrbook.h @@ -9,10 +9,10 @@ typedef struct addrbook_entry_t { char pubk[CRYPT_PKEY_HEXLEN]; - char *name; + char *alias; char *last_addr; char *last_port; - unsigned char name_len; + unsigned char alias_len; unsigned char last_addr_len; unsigned char last_port_len; } addrbook_entry_t; @@ -32,6 +32,7 @@ int addrbook_store(addrbook_t *ab, FILE *out); void addrbook_destroy(addrbook_t *const ab); int addrbook_add(addrbook_t *const ab, const addrbook_entry_t *const ent); -addrbook_entry_t *addrbook_get(const addrbook_t *const ab, const char pkey[CRYPT_PKEY_HEXLEN]); +addrbook_entry_t *addrbook_get_by_pk(const addrbook_t *const ab, const char pkey[CRYPT_PKEY_HEXLEN]); +addrbook_entry_t *addrbook_get_by_alias(const addrbook_t *const ab, const char *const alias); #endif /* _ADDRBOOK_H_ */