summaryrefslogtreecommitdiffstats
path: root/src/abstract_dialog.cpp
blob: 597fdd9b337470944188e1f7abf830073908edb4 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
    Copyright (C) 2005-2009  Michel de Boer <michel@twinklephone.com>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

#include <cstdlib>
#include <assert.h>
#include <iostream>
#include "abstract_dialog.h"
#include "log.h"
#include "phone.h"
#include "phone_user.h"
#include "util.h"
#include "userintf.h"
#include "audits/memman.h"

extern string		user_host;
extern t_phone		*phone;

// Private
void t_abstract_dialog::remove_client_request(t_client_request **cr) {
	if ((*cr)->dec_ref_count() == 0) {
		MEMMAN_DELETE(*cr);
		delete *cr;
	}

	*cr = NULL;
}

// Create a request within a dialog
// RFC 3261 12.2.1.1
t_request *t_abstract_dialog::create_request(t_method m) {
	t_user *user_config = phone_user->get_user_profile();
	
	t_request *r = new t_request(m);
	MEMMAN_NEW(r);

	// To header
	r->hdr_to.set_uri(remote_uri);
	r->hdr_to.set_display(remote_display);
	r->hdr_to.set_tag(remote_tag);

	// From header
	r->hdr_from.set_uri(local_uri);
	r->hdr_from.set_display(local_display);
	r->hdr_from.set_tag(local_tag);

	// Call-ID header
	r->hdr_call_id.set_call_id(call_id);

	// CSeq header
	r->hdr_cseq.set_method(m);
	r->hdr_cseq.set_seqnr(++local_seqnr);

	// Set Max-Forwards header
	r->hdr_max_forwards.set_max_forwards(MAX_FORWARDS);

	// User-Agent
	SET_HDR_USER_AGENT(r->hdr_user_agent);

	// RFC 3261 12.2.1.1
	// Request URI and Route header
	r->set_route(remote_target_uri, route_set);
        
        // Caculate destination set. A DNS request can result in multiple
        // IP address. In failover scenario's the request must be sent to
        // the next IP address in the list. As the request will be copied
        // in various places, the destination set must be calculated now.
        // In previous version the DNS request was done by the transaction
        // manager. This is too late as the transaction manager gets a copy
        // of the request. The destination set should be set in the copy
        // kept by the dialog.
        r->calc_destinations(*user_config);
        
        // The Via header can only be created after the destinations
        // are calculated, because the destination deterimines which
        // local IP address should be used.
        
        // Via header
        unsigned long local_ip = r->get_local_ip();
	t_via via(USER_HOST(user_config, h_ip2str(local_ip)), PUBLIC_SIP_PORT(user_config));
	r->hdr_via.add_via(via);

	return r;
}

void t_abstract_dialog::create_route_set(t_response *r) {
	// Originally the check was this:
	// if (route_set.empty() && r->hdr_record_route.is_populated())
	// This prevented the route set from being altered between a 18X response
	// and a 2XX response. This is allowed per RFC 3261 13.2.2.4
	if (r->hdr_record_route.is_populated())
	{
		route_set = r->hdr_record_route.route_list;
		route_set.reverse();
	} else {
		route_set.clear();
	}
}

void t_abstract_dialog::create_remote_target(t_response *r) {
	if (r->hdr_contact.is_populated()) {
		remote_target_uri = r->hdr_contact.contact_list.front().uri;
		remote_target_display = r->hdr_contact.contact_list.front().display;
	}
}

void t_abstract_dialog::resend_request(t_client_request *cr) {
	t_user *user_config = phone_user->get_user_profile();
	t_request *req = cr->get_request();

	// A new sequence number must be assigned
	req->hdr_cseq.set_seqnr(++local_seqnr);

	// Create a new via-header. Otherwise the
	// request will be seen as a retransmission
	unsigned long local_ip = req->get_local_ip();
	req->hdr_via.via_list.clear();
	t_via via(USER_HOST(user_config, h_ip2str(local_ip)), PUBLIC_SIP_PORT(user_config));
	req->hdr_via.add_via(via);

	cr->renew(0);
	send_request(req, cr->get_tuid());
}

bool t_abstract_dialog::resend_request_auth(t_client_request *cr, t_response *resp) {
	t_user *user_config = phone_user->get_user_profile();
	t_request *req = cr->get_request();

	// Add authorization header, increment CSeq and create new branch id
	if (phone->authorize(user_config, req, resp)) {
		resend_request(cr);
		return true;
	}

	return false;
}

bool t_abstract_dialog::redirect_request(t_client_request *cr, t_response *resp,
		t_contact_param &contact) 
{
	t_user *user_config = phone_user->get_user_profile();
	
	// If the response is a 3XX response then add redirection contacts
	if (resp->get_class() == R_3XX  && resp->hdr_contact.is_populated()) {
		cr->redirector.add_contacts(
					resp->hdr_contact.contact_list);
	}

	// Get next destination
	if (!cr->redirector.get_next_contact(contact)) {
		// There is no next destination
		return false;
	}

	t_request *req = cr->get_request();

	// Ask user for permission to redirect if indicated by user config
	if (user_config->get_ask_user_to_redirect()) {
		if(!ui->cb_ask_user_to_redirect_request(user_config,
				contact.uri, contact.display, resp->hdr_cseq.method)) 
		{
			// User did not permit to redirect
			return false;
		}
	}

	// Change the request URI to the new URI.
	// As the URI changes the destination set must be recalculated
	req->uri = contact.uri;
	req->calc_destinations(*user_config);

	resend_request(cr);

	return true;
}

bool t_abstract_dialog::failover_request(t_client_request *cr) {
	log_file->write_report("Failover to next destination.",
				"t_abstract_dialog::failover_request");
	
	t_request *req = cr->get_request();
	
	// Get next destination
	if (!req->next_destination()) {
		log_file->write_report("No next destination for failover.",
				"t_abstract_dialog::failover_request");
		return false;
	}
	
	resend_request(cr);
	return true;
}


////////////
// Public
////////////

t_abstract_dialog::t_abstract_dialog(t_phone_user *pu) :
	t_id_object()
{
	assert(pu);
	phone_user = pu;
	call_id_owner = false;
	
	local_seqnr = 0;
	remote_seqnr = 0;
	remote_seqnr_set = false;
	
	local_resp_nr = 0;
	remote_resp_nr = 0;
	
	remote_ip_port.clear();
	
	log_file->write_header("t_abstract_dialog::t_abstract_dialog", LOG_NORMAL, LOG_DEBUG);
	log_file->write_raw("Created dialog, id=");
	log_file->write_raw(get_object_id());
	log_file->write_endl();
	log_file->write_footer();
}

t_abstract_dialog::~t_abstract_dialog() {
	log_file->write_header("t_abstract_dialog::~t_abstract_dialog", LOG_NORMAL, LOG_DEBUG);
	log_file->write_raw("Destroy dialog, id=");
	log_file->write_raw(get_object_id());
	log_file->write_endl();
	log_file->write_footer();
}

t_user *t_abstract_dialog::get_user(void) const {
	return phone_user->get_user_profile();
}

void t_abstract_dialog::recvd_response(t_response *r, t_tuid tuid, t_tid tid) {
	// The source address and port of a message may be 0 when the
	// message was sent internally.
	if (!r->src_ip_port.is_null()) {
		remote_ip_port = r->src_ip_port;
	}
}

void t_abstract_dialog::recvd_request(t_request *r, t_tuid tuid, t_tid tid) {
	// The source address and port of a message may be 0 when the
	// message was sent internally.
	if (!r->src_ip_port.is_null()) {
		remote_ip_port = r->src_ip_port;
	}
}

bool t_abstract_dialog::match_response(t_response *r, t_tuid tuid) {
	return (call_id == r->hdr_call_id.call_id &&
		local_tag == r->hdr_from.tag &&
		(remote_tag.size() == 0 || remote_tag == r->hdr_to.tag));
}

bool t_abstract_dialog::match_request(t_request *r) {
	return match(r->hdr_call_id.call_id, r->hdr_to.tag, r->hdr_from.tag);
}

bool t_abstract_dialog::match_partial_request(t_request *r) {
	return (r->hdr_call_id.call_id == call_id &&
	        r->hdr_to.tag == local_tag);
}

bool t_abstract_dialog::match(const string &_call_id, const string &to_tag, 
		const string &from_tag) const
{
	return (call_id == _call_id &&
		local_tag == to_tag &&
		remote_tag == from_tag);
}

t_url t_abstract_dialog::get_remote_target_uri(void) const {
	return remote_target_uri;
}

string t_abstract_dialog::get_remote_target_display(void) const {
	return remote_target_display;
}

t_url t_abstract_dialog::get_remote_uri(void) const {
	return remote_uri;
}

string t_abstract_dialog::get_remote_display(void) const {
	return remote_display;
}

t_ip_port t_abstract_dialog::get_remote_ip_port(void) const {
	return remote_ip_port;
}

string t_abstract_dialog::get_call_id(void) const {
	return call_id;
}

string t_abstract_dialog::get_local_tag(void) const {
	return local_tag;
}

string t_abstract_dialog::get_remote_tag(void) const {
	return remote_tag;
}

bool t_abstract_dialog::remote_extension_supported(const string &extension) const {
	return (remote_extensions.find(extension) != remote_extensions.end());
}

bool t_abstract_dialog::is_call_id_owner(void) const {
	return call_id_owner;
}