summaryrefslogtreecommitdiffstats
path: root/src/transaction_layer.cpp
blob: d2982617cb6f09096ed672b5cab2d4c57adf2cd0 (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
/*
    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, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <assert.h>
#include <iostream>
#include "events.h"
#include "transaction_layer.h"
#include "userintf.h"
#include "util.h"
#include "audits/memman.h"

extern t_event_queue	*evq_trans_mgr;
extern t_event_queue	*evq_trans_layer;
extern bool		end_app;

void t_transaction_layer::recvd_response(t_response *r, t_tuid tuid,
		t_tid tid)
{
	lock();

	switch(r->get_class()) {
	case R_1XX:
		recvd_provisional(r, tuid, tid);
		break;
	case R_2XX:
		recvd_success(r, tuid, tid);
		break;
	case R_3XX:
		recvd_redirect(r, tuid, tid);
		break;
	case R_4XX:
		recvd_client_error(r, tuid, tid);
		break;
	case R_5XX:
		recvd_server_error(r, tuid, tid);
		break;
	case R_6XX:
		recvd_global_error(r, tuid, tid);
		break;
	default:
		assert(false);
		break;
	}
	
	post_process_response(r, tuid, tid);

	unlock();
}

void t_transaction_layer::recvd_request(t_request *r, t_tid tid,
		t_tid tid_cancel_target)
{
	bool fatal;
	string reason;
	t_response *resp;

	lock();

	// Return a 400 response if the SIP headers are wrong
	if (!r->is_valid(fatal, reason)) {
		resp = r->create_response(R_400_BAD_REQUEST, reason);
		send_response(resp, 0, tid);
		MEMMAN_DELETE(resp);
		delete resp;
		unlock();
		return;
	}

	// Return a 400 response if the SIP body contained a parse error
	if (r->body && r->body->invalid) {
		resp = r->create_response(R_400_BAD_REQUEST, "Invalid SIP body.");
		send_response(resp, 0, tid);
		MEMMAN_DELETE(resp);
		delete resp;
		unlock();
		return;
	}
	
	// If a message exceeded the maximum message size, than the body
	// is not parsed by the listener.
	if (r->hdr_content_length.is_populated() &&
	    r->hdr_content_length.length > 0 &&
	    !r->body)
	{
		resp = r->create_response(R_513_MESSAGE_TOO_LARGE);
		send_response(resp, 0, tid);
		MEMMAN_DELETE(resp);
		delete resp;
		unlock();
		return;
	}
	
	// RFC 3261 8.2.3
	// Return a 415 response if content encoding is not supported
	if (r->body && r->hdr_content_encoding.is_populated()) {
		for (list<t_coding>::iterator it = r->hdr_content_encoding.coding_list.begin();
		     it != r->hdr_content_encoding.coding_list.end(); ++it)
		{
			if (!CONTENT_ENCODING_SUPPORTED(it->content_coding)) {
				resp = r->create_response(R_415_UNSUPPORTED_MEDIA_TYPE);
				SET_HDR_ACCEPT_ENCODING(resp->hdr_accept_encoding);
				send_response(resp, 0, tid);
				MEMMAN_DELETE(resp);
				delete resp;
				unlock();
				return;
			}		
		}
	}

	// Check if URI scheme is supported
	if (r->uri.get_scheme() != "sip") {
		resp = r->create_response(R_416_UNSUPPORTED_URI_SCHEME);
		send_response(resp, 0, tid);
		MEMMAN_DELETE(resp);
		delete resp;
		unlock();
		return;
	}

	switch(r->method) {
	case INVITE:
		recvd_invite(r, tid);
		break;
	case ACK:
		recvd_ack(r, tid);
		break;
	case CANCEL:
		recvd_cancel(r, tid, tid_cancel_target);
		break;
	case BYE:
		recvd_bye(r, tid);
		break;
	case OPTIONS:
		recvd_options(r, tid);
		break;
	case REGISTER:
		recvd_register(r, tid);
		break;
	case PRACK:
		recvd_prack(r, tid);
		break;
	case SUBSCRIBE:
		recvd_subscribe(r, tid);
		break;
	case NOTIFY:
		recvd_notify(r, tid);
		break;
	case REFER:
		recvd_refer(r, tid);
		break;
	case INFO:
		recvd_info(r, tid);
		break;
	case MESSAGE:
		recvd_message(r, tid);
		break;
	default:
		resp = r->create_response(R_501_NOT_IMPLEMENTED);
		send_response(resp, 0, tid);
		MEMMAN_DELETE(resp);
		delete resp;
		break;
	}
	
	post_process_request(r, tid, tid_cancel_target);

	unlock();
}

void t_transaction_layer::recvd_async_response(t_event_async_response *event) {
	lock();
	
	switch (event->get_response_type()) {
	case t_event_async_response::RESP_REFER_PERMISSION:
		recvd_refer_permission(event->get_bool_response());
		break;
	default:
		// Ignore other responses
		break;
	}
	
	unlock();
}

void t_transaction_layer::send_request(t_user *user_config, t_request *r, t_tuid tuid) {
	evq_trans_mgr->push_user(user_config, (t_sip_message *)r, tuid, 0);
}

void t_transaction_layer::send_request(t_user *user_config, StunMessage *r, t_tuid tuid) {
	// The transaction manager will determine the destination IP and port,
	// so they can be left to zero in the event.
	evq_trans_mgr->push_stun_request(user_config, r, TYPE_STUN_SIP, tuid, 0, 0, 0);
}

void t_transaction_layer::send_response(t_response *r, t_tuid tuid,
		t_tid tid)
{
	evq_trans_mgr->push_user((t_sip_message *)r, tuid, tid);
}

void t_transaction_layer::run(void) {
	t_event			*event;
	t_event_user		*ev_user;
	t_event_timeout		*ev_timeout;
	t_event_failure		*ev_failure;
	t_event_stun_response	*ev_stun_resp;
	t_event_async_response	*ev_async_resp;
	t_event_broken_connection *ev_broken_connection;
	t_sip_message		*msg;
	StunMessage		*stun_msg;
	t_tid			tid;
	t_tid			tid_cancel;
	t_tuid			tuid;

	bool quit = false;
	while (!quit) {
		event = evq_trans_layer->pop();

		switch (event->get_type()) {
		case EV_USER:
			ev_user = (t_event_user *)event;
			tid = ev_user->get_tid();
			tuid = ev_user->get_tuid();
			tid_cancel = ev_user->get_tid_cancel_target();
			msg = ev_user->get_msg();

			switch(msg->get_type()) {
			case MSG_REQUEST:
				recvd_request((t_request *)msg, tid,
						tid_cancel);
				break;
			case MSG_RESPONSE:
				recvd_response((t_response *)msg, tuid, tid);
				break;
			default:
				assert(false);
				break;
			}

			break;
		case EV_TIMEOUT:
			ev_timeout = dynamic_cast<t_event_timeout *>(event);
			handle_event_timeout(ev_timeout);
			break;
		case EV_FAILURE:
			ev_failure = (t_event_failure *)event;
			tid = ev_failure->get_tid();
			lock();
			failure(ev_failure->get_failure(), tid);
			unlock();
			break;
		case EV_STUN_RESPONSE:
			ev_stun_resp = (t_event_stun_response *)event;
			tid = ev_stun_resp->get_tid();
			tuid = ev_stun_resp->get_tuid();
			stun_msg = ev_stun_resp->get_msg();
			recvd_stun_resp(stun_msg, tuid, tid);
			break;
		case EV_ASYNC_RESPONSE:
			ev_async_resp = dynamic_cast<t_event_async_response *>(event);
			recvd_async_response(ev_async_resp);
			break;
		case EV_BROKEN_CONNECTION:
			ev_broken_connection = dynamic_cast<t_event_broken_connection *>(event);
			handle_broken_connection(ev_broken_connection);
			break;
		case EV_QUIT:
			quit = true;
			break;
		default:
			// other types of event are not expected
			assert(false);
			break;
		}

		MEMMAN_DELETE(event);
		delete event;
	}
}

void t_transaction_layer::lock(void) const {
	// Prohibited threads may not lock the transaction layer
	assert(!is_prohibited_thread());

	// The user interface and transaction layer threads both call
	// functions on the transaction layer. By locking the UI mutex
	// first, a deadlock can never occur as the UI also takes the
	// UI lock first and then the transaction layer lock.
	// During shutdown of Twinkle the GUI has exited already and
	// a lock on an exited QApplication causes a segmentation fault.
	// Therefore the lock on the UI should not be taken during shutdown.
	if (!end_app) ui->lock();
	tl_mutex.lock();
}

void t_transaction_layer::unlock(void) const {
	tl_mutex.unlock();
	if (!end_app) ui->unlock();
}