diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/baresip.c | 13 | ||||
-rw-r--r-- | src/contact.c | 81 | ||||
-rw-r--r-- | src/ua.c | 8 |
3 files changed, 69 insertions, 33 deletions
diff --git a/src/baresip.c b/src/baresip.c index f70824c..72d13f1 100644 --- a/src/baresip.c +++ b/src/baresip.c @@ -14,6 +14,7 @@ */ static struct baresip { struct network *net; + struct contacts contacts; } baresip; @@ -35,12 +36,18 @@ int baresip_init(struct config *cfg, bool prefer_ipv6) return err; } + err = contact_init(&baresip.contacts); + if (err) + return err; + return 0; } void baresip_close(void) { + contact_close(&baresip.contacts); + baresip.net = mem_deref(baresip.net); } @@ -49,3 +56,9 @@ struct network *baresip_network(void) { return baresip.net; } + + +struct contacts *baresip_contacts(void) +{ + return &baresip.contacts; +} 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; @@ -302,7 +302,8 @@ static void call_event_handler(struct call *call, enum call_event ev, case CALL_EVENT_INCOMING: - if (contact_block_access(peeruri)) { + if (contact_block_access(baresip_contacts(), + peeruri)) { info("ua: blocked access: \"%s\"\n", peeruri); @@ -1336,10 +1337,6 @@ int ua_init(const char *software, bool udp, bool tcp, bool tls, play_init(); - err = contact_init(); - if (err) - return err; - uag.use_udp = udp; uag.use_tcp = tcp; uag.use_tls = tls; @@ -1395,7 +1392,6 @@ void ua_close(void) cmd_unregister(cmdv); play_close(); ui_reset(); - contact_close(); uag.evsock = mem_deref(uag.evsock); uag.sock = mem_deref(uag.sock); |