summaryrefslogtreecommitdiff
path: root/test/sip/aor.c
blob: 2d7e0d3bf9a6911c8ba6ddbdef41dfd3ce3b3842 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
 * @file sip/aor.c Mock SIP server -- SIP Address of Record
 *
 * Copyright (C) 2010 - 2016 Creytiv.com
 */
#include <string.h>
#include <re.h>
#include "sipsrv.h"


static void destructor(void *arg)
{
	struct aor *aor = arg;

	list_flush(&aor->locl);
	hash_unlink(&aor->he);
	mem_deref(aor->uri);
}


static int uri_canon(char **curip, const struct uri *uri)
{
	if (pl_isset(&uri->user))
		return re_sdprintf(curip, "%r:%H@%r",
				   &uri->scheme,
				   uri_user_unescape, &uri->user,
				   &uri->host);
	else
		return re_sdprintf(curip, "%r:%r",
				   &uri->scheme,
				   &uri->host);
}


int aor_create(struct sip_server *srv, struct aor **aorp,
	       const struct uri *uri)
{
	struct aor *aor;
	int err;

	if (!aorp || !uri)
		return EINVAL;

	aor = mem_zalloc(sizeof(*aor), destructor);
	if (!aor)
		return ENOMEM;

	err = uri_canon(&aor->uri, uri);
	if (err)
		goto out;

	hash_append(srv->ht_aor, hash_joaat_str_ci(aor->uri), &aor->he, aor);

 out:
	if (err)
		mem_deref(aor);
	else
		*aorp = aor;

	return err;
}


int aor_find(struct sip_server *srv, struct aor **aorp, const struct uri *uri)
{
	struct list *lst;
	struct aor *aor = NULL;
	struct le *le;
	char *curi;
	int err;

	if (!uri)
		return EINVAL;

	err = uri_canon(&curi, uri);
	if (err)
		return err;

	lst = hash_list(srv->ht_aor, hash_joaat_str_ci(curi));

	for (le=list_head(lst); le; le=le->next) {

		aor = le->data;

		if (!str_casecmp(curi, aor->uri))
			break;
	}

	mem_deref(curi);

	if (!le)
		return ENOENT;

	if (aorp)
		*aorp = aor;

	return 0;
}