diff options
Diffstat (limited to 'test/sip/user.c')
-rw-r--r-- | test/sip/user.c | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/test/sip/user.c b/test/sip/user.c new file mode 100644 index 0000000..99fb47d --- /dev/null +++ b/test/sip/user.c @@ -0,0 +1,73 @@ +/** + * @file sip/user.c Mock SIP server -- user handling + * + * Copyright (C) 2010 - 2016 Creytiv.com + */ +#include <re.h> +#include "sipsrv.h" + + +static void destructor(void *arg) +{ + struct user *usr = arg; + + hash_unlink(&usr->he); + mem_deref(usr->name); +} + + +int user_add(struct hash *ht, const char *username, const char *password, + const char *realm) +{ + struct user *usr; + int err; + + usr = mem_zalloc(sizeof(*usr), destructor); + if (!usr) { + err = ENOMEM; + goto out; + } + + err = str_dup(&usr->name, username); + if (err) { + goto out; + } + + err = md5_printf(usr->ha1, "%s:%s:%s", username, realm, password); + if (err) { + goto out; + } + + hash_append(ht, hash_joaat_str(username), &usr->he, usr); + + out: + if (err) { + mem_deref(usr); + } + + return err; +} + + +struct user *user_find(struct hash *ht, const struct pl *name) +{ + struct list *lst; + struct le *le; + + if (!ht || !name) + return NULL; + + lst = hash_list(ht, hash_joaat((uint8_t *)name->p, name->l)); + + for (le=list_head(lst); le; le=le->next) { + + struct user *usr = le->data; + + if (pl_strcmp(name, usr->name)) + continue; + + return usr; + } + + return NULL; +} |