summaryrefslogtreecommitdiffstats
path: root/src/stun/stun_transaction.cpp
blob: 4d1d4ae387e77ed07751abb8e936267ee4c6cc16 (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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
/*
    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 <cstring>
#include "stun_transaction.h"
#include "events.h"
#include "log.h"
#include "phone.h"
#include "sys_settings.h"
#include "transaction_mgr.h"
#include "translator.h"
#include "util.h"
#include "audits/memman.h"

extern t_transaction_mgr	*transaction_mgr;
extern t_event_queue		*evq_trans_layer;
extern t_event_queue		*evq_trans_mgr;
extern t_event_queue		*evq_sender;
extern t_phone			*phone;


bool get_stun_binding(t_user *user_config, unsigned short src_port, unsigned long &mapped_ip,
	unsigned short &mapped_port, int &err_code, string &err_reason)
{
	list<t_ip_port> destinations = 
		user_config->get_stun_server().get_h_ip_srv("udp");
	
	if (destinations.empty()) {
		// Cannot resolve STUN server address.
		log_file->write_header("::get_stun_binding", LOG_NORMAL, LOG_CRITICAL);
		log_file->write_raw("Failed to resolve: ");
		log_file->write_raw(user_config->get_stun_server().encode());
		log_file->write_endl();
		log_file->write_raw("Return internal STUN bind error: 404 Not Found");
		log_file->write_endl();
		log_file->write_footer();
		
		err_code = 404;
		err_reason = "Not Found";
		return false;
	}
	
	int num_transmissions = 0;
	int wait_intval = DUR_STUN_START_INTVAL;

	t_socket_udp sock(src_port);
	sock.connect(destinations.front().ipaddr, destinations.front().port);
		
	// Build STUN request
	char buf[STUN_MAX_MESSAGE_SIZE + 1];
	StunMessage req_bind;
	StunAtrString stun_null_str;
	stun_null_str.sizeValue = 0;	
	stunBuildReqSimple(&req_bind, stun_null_str, false, false);	
	char req_msg[STUN_MAX_MESSAGE_SIZE];
	int req_msg_size = stunEncodeMessage(req_bind, req_msg, 
		STUN_MAX_MESSAGE_SIZE, stun_null_str, false);
		
	// Send STUN request and retransmit till a response is received.
	while (num_transmissions < STUN_MAX_TRANSMISSIONS) {
		bool ret;

		try {
			sock.send(req_msg, req_msg_size);
		}
		catch (int err) {
			// Socket error (probably ICMP error)
			// Failover to next destination
			log_file->write_report("Send failed. Failover to next destination.",
					"::get_stun_binding");	
						
			destinations.pop_front();
			if (destinations.empty()) {
				log_file->write_report("No next destination for failover.",
					"::get_stun_binding");
				break;
			}
			
			num_transmissions = 0;
			wait_intval = DUR_STUN_START_INTVAL;
			sock.connect(destinations.front().ipaddr, destinations.front().port);
			continue;
		}
		
		log_file->write_header("::get_stun_binding", LOG_STUN);
		log_file->write_raw("Send to: ");
		log_file->write_raw(h_ip2str(destinations.front().ipaddr));
		log_file->write_raw(":");
		log_file->write_raw(destinations.front().port);
		log_file->write_endl();
		log_file->write_raw(stunMsg2Str(req_bind));
		log_file->write_footer();
			
		try {
			ret = sock.select_read(wait_intval);
		}
		catch (int err) {
			// Socket error (probably ICMP error)
			// Failover to next destination
			log_file->write_report("Select failed. Failover to next destination.",
					"::get_stun_binding");	
						
			destinations.pop_front();
			if (destinations.empty()) {
				log_file->write_report("No next destination for failover.",
					"::get_stun_binding");
				break;
			}
			
			num_transmissions = 0;
			wait_intval = DUR_STUN_START_INTVAL;
			sock.connect(destinations.front().ipaddr, destinations.front().port);
			continue;
		}
			
		if (!ret) {
			// Time out
			num_transmissions++;
			if (wait_intval < DUR_STUN_MAX_INTVAL) {
				wait_intval *= 2;
			}
			continue;
		}
			
		// A message has been received
		int resp_msg_size;
		try {
			resp_msg_size = sock.recv(buf, STUN_MAX_MESSAGE_SIZE + 1);
		}
		catch (int err) {
			// Socket error (probably ICMP error)
			// Failover to next destination
			log_file->write_report("Recv failed. Failover to next destination.",
					"::get_stun_binding");	
						
			destinations.pop_front();
			if (destinations.empty()) {
				log_file->write_report("No next destination for failover.",
					"::get_stun_binding");
				break;
			}
			
			num_transmissions = 0;
			wait_intval = DUR_STUN_START_INTVAL;
			sock.connect(destinations.front().ipaddr, destinations.front().port);
			continue;
		}
			
		StunMessage resp_bind;
		
		if (!stunParseMessage(buf, resp_msg_size, resp_bind, false)) {
			log_file->write_report(
				"Received faulty STUN message", "::get_stun_binding", 
					LOG_STUN);
			num_transmissions++;
			if (wait_intval < DUR_STUN_MAX_INTVAL) {
				wait_intval *= 2;
			}
			continue;
		}
		
		log_file->write_header("::get_stun_binding", LOG_STUN);
		log_file->write_raw("Received from: ");
		log_file->write_raw(h_ip2str(destinations.front().ipaddr));
		log_file->write_raw(":");
		log_file->write_raw(destinations.front().port);
		log_file->write_endl();
		log_file->write_raw(stunMsg2Str(resp_bind));
		log_file->write_footer();
		
		// Check if id in msgHdr matches
		if (!stunEqualId(resp_bind, req_bind)) {
			num_transmissions++;
			if (wait_intval < DUR_STUN_MAX_INTVAL) {
				wait_intval *= 2;
			}
			continue;
		}
				
		if (resp_bind.msgHdr.msgType == BindResponseMsg && 
		    resp_bind.hasMappedAddress) {
		    	// Bind response received
			mapped_ip = resp_bind.mappedAddress.ipv4.addr;
			mapped_port = resp_bind.mappedAddress.ipv4.port;
			return true;
		}
			
		if (resp_bind.msgHdr.msgType == BindErrorResponseMsg &&
		    resp_bind.hasErrorCode) 
		{
			// Bind error received
			err_code = resp_bind.errorCode.errorClass * 100 +
				   resp_bind.errorCode.number;
			char s[STUN_MAX_STRING + 1];
			strncpy(s, resp_bind.errorCode.reason, STUN_MAX_STRING);
			s[STUN_MAX_STRING] = 0;
			err_reason = s;
			return false;
		}
			
		// A wrong response has been received.
		log_file->write_report(
			"Invalid STUN response received", "::get_stun_binding", 
				LOG_NORMAL);

		err_code = 500;
		err_reason = "Server Error";
		return false;
	}
		
	// Request timed out
	log_file->write_report("STUN request timeout", "::get_stun_binding", 
			LOG_NORMAL);
				
	err_code = 408;
	err_reason = "Request Timeout";
	return false;
}

bool stun_discover_nat(t_phone_user *pu, string &err_msg) {
	t_user *user_config = pu->get_user_profile();

	// By default enable STUN. If for some reason we cannot perform
	// NAT discovery, then enable STUN. It will not harm, but only
	// create non-needed STUN transactions if we are not behind a NAT.
	pu->use_stun = true;
	pu->use_nat_keepalive = true;

	list<t_ip_port> destinations = 
		user_config->get_stun_server().get_h_ip_srv("udp");

	if (destinations.empty()) {
		// Cannot resolve STUN server address.
		log_file->write_header("::main", LOG_NORMAL, LOG_CRITICAL);
		log_file->write_raw("Failed to resolve: ");
		log_file->write_raw(user_config->get_stun_server().encode());
		log_file->write_endl();
		log_file->write_footer();

		err_msg = TRANSLATE("Cannot resolve STUN server: %1");
		err_msg = replace_first(err_msg, "%1", user_config->get_stun_server().encode());
		return false;
	}

	while (!destinations.empty()) {
		StunAddress4 stun_ip4;
		stun_ip4.addr = destinations.front().ipaddr;
		stun_ip4.port = destinations.front().port;
		
		NatType nat_type = stunNatType(stun_ip4, false);
		log_file->write_header("::main");
		log_file->write_raw("STUN NAT type discovery for ");
		log_file->write_raw(user_config->get_profile_name());
		log_file->write_endl();
		log_file->write_raw("NAT type: ");
		log_file->write_raw(stunNatType2Str(nat_type));
		log_file->write_endl();
		log_file->write_footer();
		
		switch (nat_type) {
		case StunTypeOpen:
			// STUN is not needed.
			pu->use_stun = false;
			pu->use_nat_keepalive = false;
			return true;
		case StunTypeSymNat:
			err_msg = TRANSLATE("You are behind a symmetric NAT.\nSTUN will not work.\nConfigure a public IP address in the user profile\nand create the following static bindings (UDP) in your NAT.");
			err_msg += "\n\n";
			err_msg += TRANSLATE("public IP: %1 --> private IP: %2 (SIP signaling)");
			err_msg = replace_first(err_msg, "%1", int2str(sys_config->get_sip_port()));
			err_msg = replace_first(err_msg, "%2", int2str(sys_config->get_sip_port()));
			err_msg += "\n";
			err_msg += TRANSLATE("public IP: %1-%2 --> private IP: %3-%4 (RTP/RTCP)");
			err_msg = replace_first(err_msg, "%1", int2str(sys_config->get_rtp_port()));
			err_msg = replace_first(err_msg, "%2", int2str(sys_config->get_rtp_port() + 5));
			err_msg = replace_first(err_msg, "%3", int2str(sys_config->get_rtp_port()));
			err_msg = replace_first(err_msg, "%4", int2str(sys_config->get_rtp_port() + 5));
			
			pu->use_stun = false;
			pu->use_nat_keepalive = false;
			return false;
		case StunTypeSymFirewall:
			// STUN is not needed as we are on a pubic IP.
			// NAT keep alive is needed however to keep the firewall open.
			pu->use_stun = false;
			return true;
		case StunTypeBlocked:
			destinations.pop_front();
			
			// The code for NAT type discovery does not handle
			// ICMP errors. So if the conclusion is that the network
			// connection is blocked, it might be due to a down STUN
			// server. Try alternative destination if avaliable.
		
			if (destinations.empty()) {
				err_msg = TRANSLATE("Cannot reach the STUN server: %1");
				err_msg = replace_first(err_msg, "%1",
						user_config->get_stun_server().encode());
				err_msg += "\n\n";
				err_msg += TRANSLATE("If you are behind a firewall then you need to open the following UDP ports.");
				err_msg += "\n";
				err_msg += TRANSLATE("Port %1 (SIP signaling)");
				err_msg = replace_first(err_msg, "%1",
						int2str(sys_config->get_sip_port()));
				err_msg += "\n";
				err_msg += TRANSLATE("Ports %1-%2 (RTP/RTCP)");
				err_msg = replace_first(err_msg, "%1",
						int2str(sys_config->get_rtp_port()));
				err_msg = replace_first(err_msg, "%2",
						int2str(sys_config->get_rtp_port() + 5));
						
				return false;
			}
			
			log_file->write_report("Failover to next destination.",
				"::stun_discover_nat");			
			break;
		case StunTypeFailure:
			destinations.pop_front();
			log_file->write_report("Failover to next destination.",
				"::stun_discover_nat");
			break;
		default:
			// Use STUN.
			return true;
		}
	}

	err_msg = TRANSLATE("NAT type discovery via STUN failed.");	
	return false;
}


// Main function for STUN listener thread for media STUN requests.
void *stun_listen_main(void *arg) {
	char		buf[STUN_MAX_MESSAGE_SIZE + 1];
	int		data_size;
	
	t_socket_udp *sock = (t_socket_udp *)arg;
	
	while(true) {
		try {
			data_size = sock->recv(buf, STUN_MAX_MESSAGE_SIZE + 1);
		} catch (int err) {
			string msg("Failed to receive STUN response for media.\n");
			msg += get_error_str(err);
			log_file->write_report(msg, "::stun_listen_main",
				LOG_NORMAL, LOG_CRITICAL);
				
			// The request will timeout, no need to send a response now.
				
			return NULL;
		}
		
		StunMessage m;
		
		if (!stunParseMessage(buf, data_size, m, false)) {
			log_file->write_report("Faulty STUN message", "::stun_listen_main");
			continue;
		}
		
		log_file->write_header("::stun_listen_main", LOG_STUN);
		log_file->write_raw("Received: ");
		log_file->write_raw(stunMsg2Str(m));
		log_file->write_footer();
	
		evq_trans_mgr->push_stun_response(&m, 0, 0);
	}
}

//////////////////////////////////////////////
// Base STUN transaction
//////////////////////////////////////////////

t_mutex t_stun_transaction::mtx_class;
t_tid t_stun_transaction::next_id = 1;

t_stun_transaction::t_stun_transaction(t_user *user, StunMessage *r,
			   unsigned short _tuid, const list<t_ip_port> &dst) 
{
	mtx_class.lock();
	id = next_id++;
	if (next_id == 65535) next_id = 1;
	mtx_class.unlock();
	
	state = TS_NULL;
	request = new StunMessage(*r);
	MEMMAN_NEW(request);
	tuid = _tuid;
	
	dur_req_timeout = DUR_STUN_START_INTVAL;
	num_transmissions = 0;
	
	destinations = dst;
	
	user_config = user->copy();
}

t_stun_transaction::~t_stun_transaction() {
	MEMMAN_DELETE(request);
	delete request;
	MEMMAN_DELETE(user_config);
	delete user_config;
}

t_tid t_stun_transaction::get_id(void) const {
	return id;
}

t_trans_state t_stun_transaction::get_state(void) const {
	return state;
}

void t_stun_transaction::start_timer_req_timeout(void) {
	timer_req_timeout = transaction_mgr->start_stun_timer(dur_req_timeout,
		STUN_TMR_REQ_TIMEOUT, id);
		
	// RFC 3489 9.3
	// Double the retransmision interval till a maximum
	if (dur_req_timeout < DUR_STUN_MAX_INTVAL) {
		dur_req_timeout = 2 * dur_req_timeout;
	}
}

void t_stun_transaction::stop_timer_req_timeout(void) {
	if (timer_req_timeout) {
		transaction_mgr->stop_timer(timer_req_timeout);
		timer_req_timeout = 0;
	}
}

void t_stun_transaction::process_response(StunMessage *r) {
	stop_timer_req_timeout();
	evq_trans_layer->push_stun_response(r, tuid, id);
	state = TS_TERMINATED;
}

void t_stun_transaction::process_icmp(const t_icmp_msg &icmp) {
	stop_timer_req_timeout();
	
	log_file->write_report("Failover to next destination.",
				"t_stun_transaction::process_icmp");	
				
	destinations.pop_front();
	if (destinations.empty()) {
		log_file->write_report("No next destination for failover.",
				"t_stun_transaction::process_icmp");
						
		log_file->write_header("t_stun_transaction::process_icmp",
			LOG_NORMAL, LOG_INFO);
		log_file->write_raw("ICMP error received.\n\n");
		log_file->write_raw("Send internal: 500 Server Error\n");
		log_file->write_footer();	
	
		// No server could be reached, Notify the TU with 500 Server
		// Error.
		StunMessage *resp = stunBuildError(*request, 500, "Server Error");
		evq_trans_layer->push_stun_response(resp, tuid, id);
		MEMMAN_DELETE(resp);
		delete resp;
		
		state = TS_TERMINATED;
		return;
	}
	
	// Failover to next destination
	evq_sender->push_stun_request(user_config, request, TYPE_STUN_SIP, tuid, id,
		destinations.front().ipaddr, destinations.front().port);
	num_transmissions = 1;
	dur_req_timeout = DUR_STUN_START_INTVAL;
	start_timer_req_timeout();
}

void t_stun_transaction::timeout(t_stun_timer t) {
	// RFC 3489 9.3
	if (num_transmissions < STUN_MAX_TRANSMISSIONS) {
		retransmit();
		start_timer_req_timeout();
		return;
	}
	
	// Report timeout to TU
	StunMessage *timeout_resp;
	timeout_resp = stunBuildError(*request, 408, "Request Timeout");
	log_file->write_report("STUN request timeout", "t_stun_transaction::timeout", 
			LOG_NORMAL);
	
	evq_trans_layer->push_stun_response(timeout_resp, tuid, id);
	MEMMAN_DELETE(timeout_resp);
	delete timeout_resp;
	
	state = TS_TERMINATED;
}

bool t_stun_transaction::match(StunMessage *resp) const {
	return stunEqualId(*resp, *request);
}

// An ICMP error matches a transaction when the destination IP address/port
// of the packet that caused the ICMP error equals the destination 
// IP address/port of the transaction. Other information of the packet causing
// the ICMP error is not available.
// In theory when multiple transactions are open for the same destination, the
// wrong transaction may process the ICMP error. In practice this should rarely
// happen as the destination will be unreachable for all those transactions.
// If it happens a transaction gets aborted.
bool t_stun_transaction::match(const t_icmp_msg &icmp) const {
	return (destinations.front().ipaddr == icmp.ipaddr && 
	        destinations.front().port == icmp.port);
}

//////////////////////////////////////////////
// SIP STUN transaction
//////////////////////////////////////////////

void t_sip_stun_trans::retransmit(void) {
	// The SIP UDP sender will send out the STUN request.
	evq_sender->push_stun_request(user_config, request, TYPE_STUN_SIP, tuid, id,
		destinations.front().ipaddr, destinations.front().port);
	num_transmissions++;
}

t_sip_stun_trans::t_sip_stun_trans(t_user *user, StunMessage *r,
			unsigned short _tuid, const list<t_ip_port> &dst) :
		t_stun_transaction(user, r, _tuid, dst)
{
	// The SIP UDP sender will send out the STUN request.
	evq_sender->push_stun_request(user_config, request, TYPE_STUN_SIP, tuid, id,
		destinations.front().ipaddr, destinations.front().port);
	num_transmissions++;
	start_timer_req_timeout();	
	state = TS_PROCEEDING;
}

//////////////////////////////////////////////
// Media STUN transaction
//////////////////////////////////////////////

// TODO: this code is not used anymore. Remove?

void t_media_stun_trans::retransmit(void) {
	// Retransmit the STUN request
	StunAtrString stun_pass;
	stun_pass.sizeValue = 0;
	char m[STUN_MAX_MESSAGE_SIZE];
	int msg_size = stunEncodeMessage(*request, m, STUN_MAX_MESSAGE_SIZE, stun_pass, false);
	
	try {
		sock->sendto(destinations.front().ipaddr, destinations.front().port, 
			m, msg_size);
	} catch (int err) {
		string msg("Failed to send STUN request for media.\n");
		msg += get_error_str(err);
		log_file->write_report(msg, "::t_media_stun_trans::retransmit",
			LOG_NORMAL, LOG_CRITICAL);
			
		StunMessage *resp;
		resp = stunBuildError(*request, 500, "Could not send request");

		evq_trans_layer->push_stun_response(resp, tuid, id);
		MEMMAN_DELETE(resp);
		delete resp;
			
		return;
	}
	
	num_transmissions++;
}

t_media_stun_trans::t_media_stun_trans(t_user *user, StunMessage *r,
			 unsigned short _tuid, const list<t_ip_port> &dst, 
			 unsigned short src_port) :
		t_stun_transaction(user, r, _tuid, dst)
{
	thr_listen = NULL;
	
	try {
		sock = new t_socket_udp(src_port);
		MEMMAN_NEW(sock);
		sock->connect(destinations.front().ipaddr, destinations.front().port);
	} catch (int err) {
		string msg("Failed to create a UDP socket (STUN) on port ");
		msg += int2str(src_port);
		msg += "\n";
		msg += get_error_str(err);
		log_file->write_report(msg, "t_media_stun_trans::t_media_stun_trans", LOG_NORMAL, 
			LOG_CRITICAL);
		delete sock;
		sock = NULL;
		
		StunMessage *resp;
		resp = stunBuildError(*request, 500, "Could not create socket");

		evq_trans_layer->push_stun_response(resp, tuid, id);
		MEMMAN_DELETE(resp);
		delete resp;
		
		return;
	}
	
	// Send STUN request
	StunAtrString stun_pass;
	stun_pass.sizeValue = 0;
	char m[STUN_MAX_MESSAGE_SIZE];
	int msg_size = stunEncodeMessage(*r, m, STUN_MAX_MESSAGE_SIZE, stun_pass, false);
	
	try {
		sock->send(m, msg_size);
	} catch (int err) {
		string msg("Failed to send STUN request for media.\n");
		msg += get_error_str(err);
		log_file->write_report(msg, "::t_media_stun_trans::t_media_stun_trans",
			LOG_NORMAL, LOG_CRITICAL);

		StunMessage *resp;
		resp = stunBuildError(*request, 500, "Failed to send request");

		evq_trans_layer->push_stun_response(resp, tuid, id);
		MEMMAN_DELETE(resp);
		delete resp;
		
		return;
	}
	
	num_transmissions++;
	
	try {
		thr_listen = new t_thread(stun_listen_main, sock);
		MEMMAN_NEW(thr_listen);
	} catch (int) {
		log_file->write_report("Failed to create STUN listener thread.",
			"::t_media_stun_trans::t_media_stun_trans",
			LOG_NORMAL, LOG_CRITICAL);
		delete thr_listen;
		thr_listen = NULL;

		StunMessage *resp;
		resp = stunBuildError(*request, 500, "Failed to create STUN listen thread");

		evq_trans_layer->push_stun_response(resp, tuid, id);
		MEMMAN_DELETE(resp);
		delete resp;		
		
		return;
	}
	
	start_timer_req_timeout();
	state = TS_PROCEEDING;
}

t_media_stun_trans::~t_media_stun_trans() {
	if (sock) {
		MEMMAN_DELETE(sock);
		delete sock;
	}
	
	if (thr_listen) {
		thr_listen->cancel();
		thr_listen->join();
		MEMMAN_DELETE(thr_listen);
		delete thr_listen;
	}
}