1 /**
2  * @file sip/aor.c Mock SIP server -- SIP Address of Record
3  *
4  * Copyright (C) 2010 - 2016 Creytiv.com
5  */
6 #include <string.h>
7 #include <re.h>
8 #include "sipsrv.h"
9 
10 
destructor(void * arg)11 static void destructor(void *arg)
12 {
13 	struct aor *aor = arg;
14 
15 	list_flush(&aor->locl);
16 	hash_unlink(&aor->he);
17 	mem_deref(aor->uri);
18 }
19 
20 
uri_canon(char ** curip,const struct uri * uri)21 static int uri_canon(char **curip, const struct uri *uri)
22 {
23 	if (pl_isset(&uri->user))
24 		return re_sdprintf(curip, "%r:%H@%r",
25 				   &uri->scheme,
26 				   uri_user_unescape, &uri->user,
27 				   &uri->host);
28 	else
29 		return re_sdprintf(curip, "%r:%r",
30 				   &uri->scheme,
31 				   &uri->host);
32 }
33 
34 
aor_create(struct sip_server * srv,struct aor ** aorp,const struct uri * uri)35 int aor_create(struct sip_server *srv, struct aor **aorp,
36 	       const struct uri *uri)
37 {
38 	struct aor *aor;
39 	int err;
40 
41 	if (!aorp || !uri)
42 		return EINVAL;
43 
44 	aor = mem_zalloc(sizeof(*aor), destructor);
45 	if (!aor)
46 		return ENOMEM;
47 
48 	err = uri_canon(&aor->uri, uri);
49 	if (err)
50 		goto out;
51 
52 	hash_append(srv->ht_aor, hash_joaat_str_ci(aor->uri), &aor->he, aor);
53 
54  out:
55 	if (err)
56 		mem_deref(aor);
57 	else
58 		*aorp = aor;
59 
60 	return err;
61 }
62 
63 
aor_find(struct sip_server * srv,struct aor ** aorp,const struct uri * uri)64 int aor_find(struct sip_server *srv, struct aor **aorp, const struct uri *uri)
65 {
66 	struct list *lst;
67 	struct aor *aor = NULL;
68 	struct le *le;
69 	char *curi;
70 	int err;
71 
72 	if (!uri)
73 		return EINVAL;
74 
75 	err = uri_canon(&curi, uri);
76 	if (err)
77 		return err;
78 
79 	lst = hash_list(srv->ht_aor, hash_joaat_str_ci(curi));
80 
81 	for (le=list_head(lst); le; le=le->next) {
82 
83 		aor = le->data;
84 
85 		if (!str_casecmp(curi, aor->uri))
86 			break;
87 	}
88 
89 	mem_deref(curi);
90 
91 	if (!le)
92 		return ENOENT;
93 
94 	if (aorp)
95 		*aorp = aor;
96 
97 	return 0;
98 }
99