1
0
Fork 0

In addrbook_t name was renamed to alias. addrbook_get() to addrbook_get_by_pk(). A func addrbook_get_by_alias() was added.

This commit is contained in:
Alexander Andreev 2024-03-30 03:55:25 +04:00
parent 098e5c4faa
commit 8b67936227
Signed by: Arav
GPG Key ID: 25969B23DCB5CA34
2 changed files with 21 additions and 9 deletions

View File

@ -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;
}

View File

@ -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_ */