summaryrefslogtreecommitdiff
path: root/test/sip/location.c
blob: be2bd6696cb5feae579947e0d3de118bd89b2525 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
 * @file sip/location.c Mock SIP server -- location handling
 *
 * Copyright (C) 2010 - 2016 Creytiv.com
 */
#include <time.h>
#include <re.h>
#include "sipsrv.h"


struct loctmp {
	struct sa src;
	struct uri duri;
	char *uri;
	char *callid;
	uint32_t expires;
	uint32_t cseq;
	double q;
};


static void destructor_loctmp(void *arg)
{
	struct loctmp *tmp = arg;

	mem_deref(tmp->uri);
	mem_deref(tmp->callid);
}


static void destructor_location(void *arg)
{
	struct location *loc = arg;

	list_unlink(&loc->le);
	mem_deref(loc->uri);
	mem_deref(loc->callid);
	mem_deref(loc->tmp);
}


static bool cmp_handler(struct le *le, void *arg)
{
	struct location *loc = le->data;

	return uri_cmp(&loc->duri, arg);
}


int location_update(struct list *locl, const struct sip_msg *msg,
		    const struct sip_addr *contact, uint32_t expires)
{
	struct location *loc, *loc_new = NULL;
	struct loctmp *tmp;
	struct pl pl;
	int err;

	if (!locl || !msg || !contact)
		return EINVAL;

	loc = list_ledata(list_apply(locl, true, cmp_handler,
				     (void *)&contact->uri));
	if (!loc) {
		if (expires == 0)
			return 0;

		loc = loc_new = mem_zalloc(sizeof(*loc), destructor_location);
		if (!loc)
			return ENOMEM;

		list_append(locl, &loc->le, loc);
	}
	else {
		if (!pl_strcmp(&msg->callid, loc->callid) &&
		    msg->cseq.num <= loc->cseq)
			return EPROTO;

		if (expires == 0) {
			loc->rm = true;
			return 0;
		}
	}

	tmp = mem_zalloc(sizeof(*tmp), destructor_loctmp);
	if (!tmp) {
		err = ENOMEM;
		goto out;
	}

	err = pl_strdup(&tmp->uri, &contact->auri);
	if (err)
		goto out;

	pl_set_str(&pl, tmp->uri);

	if (uri_decode(&tmp->duri, &pl)) {
		err = EBADMSG;
		goto out;
	}

	err = pl_strdup(&tmp->callid, &msg->callid);
	if (err)
		goto out;


	if (!msg_param_decode(&contact->params, "q", &pl))
		tmp->q = pl_float(&pl);
	else
		tmp->q = 1;

	tmp->cseq    = msg->cseq.num;
	tmp->expires = expires;
	tmp->src     = msg->src;

 out:
	if (err) {
		mem_deref(loc_new);
		mem_deref(tmp);
	}
	else {
		mem_deref(loc->tmp);
		loc->tmp = tmp;
	}

	return err;
}


void location_commit(struct list *locl)
{
	time_t now = time(NULL);
	struct le *le;

	if (!locl)
		return;

	for (le=locl->head; le; ) {

		struct location *loc = le->data;

		le = le->next;

		if (loc->rm) {
			list_unlink(&loc->le);
			mem_deref(loc);
		}
		else if (loc->tmp) {

			mem_deref(loc->uri);
			mem_deref(loc->callid);

			loc->uri       = mem_ref(loc->tmp->uri);
			loc->callid    = mem_ref(loc->tmp->callid);
			loc->duri      = loc->tmp->duri;
			loc->cseq      = loc->tmp->cseq;
			loc->expires   = loc->tmp->expires + now;
			loc->src       = loc->tmp->src;
			loc->q         = loc->tmp->q;

			loc->tmp = mem_deref(loc->tmp);
		}
	}
}


void location_rollback(struct list *locl)
{
	struct le *le;

	if (!locl)
		return;

	for (le=locl->head; le; ) {

		struct location *loc = le->data;

		le = le->next;

		if (!loc->uri) {
			list_unlink(&loc->le);
			mem_deref(loc);
		}
		else {
			loc->tmp = mem_deref(loc->tmp);
			loc->rm  = false;
		}
	}
}