diff options
author | Alfred E. Heggestad <aeh@db.org> | 2016-07-24 18:00:40 +0200 |
---|---|---|
committer | Alfred E. Heggestad <aeh@db.org> | 2016-07-24 18:00:40 +0200 |
commit | 713f55a1f9958222f85238bde40482b566cf040e (patch) | |
tree | 7ca0800a14cec6f9cbe6794a3bdcea9b42001db7 /src/contact.c | |
parent | 0a987e0a95b3473f8bc5e70455c92232d9990114 (diff) |
contact: make contacts re-entrant
Diffstat (limited to 'src/contact.c')
-rw-r--r-- | src/contact.c | 81 |
1 files changed, 54 insertions, 27 deletions
diff --git a/src/contact.c b/src/contact.c index e8e6df6..1fe308b 100644 --- a/src/contact.c +++ b/src/contact.c @@ -3,6 +3,7 @@ * * Copyright (C) 2010 Creytiv.com */ +#include <string.h> #include <re.h> #include <baresip.h> @@ -22,9 +23,6 @@ struct contact { enum access access; }; -static struct list cl; -static struct hash *cht; - static void destructor(void *arg) { @@ -39,18 +37,20 @@ static void destructor(void *arg) /** * Add a contact * + * @param contacts Contacts container * @param contactp Pointer to allocated contact (optional) * @param addr Contact in SIP address format * * @return 0 if success, otherwise errorcode */ -int contact_add(struct contact **contactp, const struct pl *addr) +int contact_add(struct contacts *contacts, + struct contact **contactp, const struct pl *addr) { struct contact *c; struct pl pl; int err; - if (!cht) + if (!contacts) return EINVAL; c = mem_zalloc(sizeof(*c), destructor); @@ -89,8 +89,8 @@ int contact_add(struct contact **contactp, const struct pl *addr) c->status = PRESENCE_UNKNOWN; - list_append(&cl, &c->le, c); - hash_append(cht, hash_joaat_pl(&c->addr.auri), &c->he, c); + list_append(&contacts->cl, &c->le, c); + hash_append(contacts->cht, hash_joaat_pl(&c->addr.auri), &c->he, c); out: if (err) @@ -131,11 +131,16 @@ const char *contact_str(const struct contact *c) /** * Get the list of contacts * + * @param contacts Contacts container + * * @return List of contacts */ -struct list *contact_list(void) +struct list *contact_list(const struct contacts *contacts) { - return &cl; + if (!contacts) + return NULL; + + return (struct list *)&contacts->cl; } @@ -168,17 +173,21 @@ const char *contact_presence_str(enum presence_status status) } -int contacts_print(struct re_printf *pf, void *unused) +int contacts_print(struct re_printf *pf, const struct contacts *contacts) { + const struct list *lst; struct le *le; int err; - (void)unused; + if (!contacts) + return 0; + + lst = contact_list(contacts); err = re_hprintf(pf, "\n--- Contacts: (%u) ---\n", - list_count(contact_list())); + list_count(lst)); - for (le = list_head(contact_list()); le && !err; le = le->next) { + for (le = list_head(lst); le && !err; le = le->next) { const struct contact *c = le->data; const struct sip_addr *addr = &c->addr; @@ -196,27 +205,40 @@ int contacts_print(struct re_printf *pf, void *unused) /** * Initialise the contacts sub-system * + * @param contacts Contacts container + * * @return 0 if success, otherwise errorcode */ -int contact_init(void) +int contact_init(struct contacts *contacts) { int err = 0; - if (!cht) - err = hash_alloc(&cht, 32); + if (!contacts) + return EINVAL; + + memset(contacts, 0, sizeof(*contacts)); + + list_init(&contacts->cl); + + err = hash_alloc(&contacts->cht, 32); return err; } /** + * @param contacts Contacts container + * * Close the contacts sub-system */ -void contact_close(void) +void contact_close(struct contacts *contacts) { - hash_clear(cht); - cht = mem_deref(cht); - list_flush(&cl); + if (!contacts) + return; + + hash_clear(contacts->cht); + contacts->cht = mem_deref(contacts->cht); + list_flush(&contacts->cl); } @@ -231,13 +253,17 @@ static bool find_handler(struct le *le, void *arg) /** * Lookup a SIP uri in all registered contacts * - * @param uri SIP uri to lookup + * @param contacts Contacts container + * @param uri SIP uri to lookup * * @return Matching contact if found, otherwise NULL */ -struct contact *contact_find(const char *uri) +struct contact *contact_find(const struct contacts *contacts, const char *uri) { - return list_ledata(hash_lookup(cht, hash_joaat_str(uri), + if (!contacts) + return NULL; + + return list_ledata(hash_lookup(contacts->cht, hash_joaat_str(uri), find_handler, (void *)uri)); } @@ -248,19 +274,20 @@ struct contact *contact_find(const char *uri) * - Matching uri has first presedence * - Global <sip:*@*> uri has second presedence * - * @param uri SIP uri to check for access + * @param contacts Contacts container + * @param uri SIP uri to check for access * * @return True if blocked, false if allowed */ -bool contact_block_access(const char *uri) +bool contact_block_access(const struct contacts *contacts, const char *uri) { struct contact *c; - c = contact_find(uri); + c = contact_find(contacts, uri); if (c && c->access != ACCESS_UNKNOWN) return c->access == ACCESS_BLOCK; - c = contact_find("sip:*@*"); + c = contact_find(contacts, "sip:*@*"); if (c && c->access != ACCESS_UNKNOWN) return c->access == ACCESS_BLOCK; |