summaryrefslogtreecommitdiff
path: root/pjnath/src/pjturn-srv/server.c
blob: c9fc40cf569fe3ebc348e22b70bc1e7af1dface7 (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
/* $Id$ */
/* 
 * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
 *
 * 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 "turn.h"

#define MAX_CLIENTS		32
#define MAX_PEERS_PER_CLIENT	8
#define MAX_HANDLES		(MAX_CLIENTS*MAX_PEERS_PER_CLIENT+MAX_LISTENERS)
#define MAX_TIMER		(MAX_HANDLES * 2)
#define MIN_PORT		49152
#define MAX_PORT		65535
#define MAX_LISTENERS		16
#define MAX_THREADS		2

#define MAX_CLIENT_BANDWIDTH	128  /* In Kbps */
#define DEFA_CLIENT_BANDWIDTH	64

#define MIN_LIFETIME		32
#define MAX_LIFETIME		600
#define DEF_LIFETIME		300


/* Prototypes */
static pj_status_t on_tx_stun_msg( pj_stun_session *sess,
				   const void *pkt,
				   pj_size_t pkt_size,
				   const pj_sockaddr_t *dst_addr,
				   unsigned addr_len);
static pj_status_t on_rx_stun_request(pj_stun_session *sess,
				      const pj_uint8_t *pkt,
				      unsigned pkt_len,
				      const pj_stun_msg *msg,
				      const pj_sockaddr_t *src_addr,
				      unsigned src_addr_len);

/*
 * Get transport type name.
 */
PJ_DEF(const char*) pjturn_tp_type_name(int tp_type)
{
    /* Must be 3 characters long! */
    if (tp_type == PJTURN_TP_UDP)
	return "UDP";
    else if (tp_type == PJTURN_TP_TCP)
	return "TCP";
    else
	return "???";
}

/*
 * Create server.
 */
PJ_DEF(pj_status_t) pjturn_srv_create( pj_pool_factory *pf,
				       pjturn_srv **p_srv)
{
    pj_pool_t *pool;
    pjturn_srv *srv;
    pj_status_t status;

    PJ_ASSERT_RETURN(pf && p_srv, PJ_EINVAL);

    /* Create server and init core settings */
    pool = pj_pool_create(pf, "srv%p", 1000, 1000, NULL);
    srv = PJ_POOL_ZALLOC_T(pool, pjturn_srv);
    srv->core.obj_name = pool->obj_name;
    srv->core.pf = pf;
    srv->core.pool = pool;
    
    status = pj_ioqueue_create(pool, MAX_HANDLES, &srv->core.ioqueue);
    if (status != PJ_SUCCESS)
	goto on_error;

    status = pj_timer_heap_create(pool, MAX_TIMER, &srv->core.timer_heap);
    if (status != PJ_SUCCESS)
	goto on_error;

    srv->core.listener = pj_pool_calloc(pool, MAX_LISTENERS, 
					sizeof(srv->core.listener[0]));
    srv->core.stun_sess = pj_pool_calloc(pool, MAX_LISTENERS,
					 (sizeof(srv->core.stun_sess[0])));

    srv->core.thread_cnt = MAX_THREADS;
    srv->core.thread = pj_pool_calloc(pool, srv->core.thread_cnt, 
				      sizeof(pj_thread_t*));

    status = pj_lock_create_recursive_mutex(pool, "srv%p", &srv->core.lock);
    if (status != PJ_SUCCESS)
	goto on_error;

    /* Create hash tables */
    srv->tables.alloc = pj_hash_create(pool, MAX_CLIENTS);
    srv->tables.res = pj_hash_create(pool, MAX_CLIENTS);

    /* Init ports settings */
    srv->ports.min_udp = srv->ports.next_udp = MIN_PORT;
    srv->ports.max_tcp = MAX_PORT;
    srv->ports.min_tcp = srv->ports.next_tcp = MIN_PORT;
    srv->ports.max_tcp = MAX_PORT;

    /* Init STUN config */
    pj_stun_config_init(&srv->core.stun_cfg, pf, 0, srv->core.ioqueue,
		        srv->core.timer_heap);

    *p_srv = srv;
    return PJ_SUCCESS;

on_error:
    pjturn_srv_destroy(srv);
    return status;
}

/** 
 * Create server.
 */
PJ_DEF(pj_status_t) pjturn_srv_destroy(pjturn_srv *srv)
{
    return PJ_SUCCESS;
}

/** 
 * Add listener.
 */
PJ_DEF(pj_status_t) pjturn_srv_add_listener(pjturn_srv *srv,
					    pjturn_listener *lis)
{
    pj_stun_session_cb sess_cb;
    unsigned index;
    pj_stun_session *sess;
    pj_status_t status;

    PJ_ASSERT_RETURN(srv && lis, PJ_EINVAL);
    PJ_ASSERT_RETURN(srv->core.lis_cnt < MAX_LISTENERS, PJ_ETOOMANY);

    /* Add to array */
    index = srv->core.lis_cnt;
    srv->core.listener[index] = lis;
    lis->server = srv;

    /* Create STUN session to handle new allocation */
    pj_bzero(&sess_cb, sizeof(sess_cb));
    sess_cb.on_rx_request = &on_rx_stun_request;
    sess_cb.on_send_msg = &on_tx_stun_msg;

    status = pj_stun_session_create(&srv->core.stun_cfg, "lis%p", &sess_cb,
				    PJ_FALSE, &sess);
    if (status != PJ_SUCCESS) {
	srv->core.listener[index] = NULL;
	return status;
    }

    pj_stun_session_set_user_data(sess, lis);

    srv->core.stun_sess[index] = sess;
    lis->id = index;
    srv->core.lis_cnt++;

    return PJ_SUCCESS;
}

/**
 * Register an allocation.
 */
PJ_DEF(pj_status_t) pjturn_srv_register_allocation(pjturn_srv *srv,
						   pjturn_allocation *alloc)
{
    /* Add to hash tables */
    pj_lock_acquire(srv->core.lock);
    pj_hash_set(alloc->pool, srv->tables.alloc,
		&alloc->hkey, sizeof(alloc->hkey), 0, alloc);
    pj_hash_set(alloc->pool, srv->tables.res,
		&alloc->relay.hkey, sizeof(alloc->relay.hkey), 0,
		&alloc->relay);
    pj_lock_release(srv->core.lock);

    return PJ_SUCCESS;
}

/**
 * Unregister an allocation.
 */
PJ_DEF(pj_status_t) pjturn_srv_unregister_allocation(pjturn_srv *srv,
						     pjturn_allocation *alloc)
{
    /* Unregister from hash tables */
    pj_lock_acquire(srv->core.lock);
    pj_hash_set(alloc->pool, srv->tables.alloc,
		&alloc->hkey, sizeof(alloc->hkey), 0, NULL);
    pj_hash_set(alloc->pool, srv->tables.res,
		&alloc->relay.hkey, sizeof(alloc->relay.hkey), 0, NULL);
    pj_lock_release(srv->core.lock);

    return PJ_SUCCESS;
}


/* Callback from our own STUN session to send packet */
static pj_status_t on_tx_stun_msg( pj_stun_session *sess,
				   const void *pkt,
				   pj_size_t pkt_size,
				   const pj_sockaddr_t *dst_addr,
				   unsigned addr_len)
{
    pjturn_listener *listener;
    
    listener = (pjturn_listener*) pj_stun_session_get_user_data(sess);

    PJ_ASSERT_RETURN(listener!=NULL, PJ_EINVALIDOP);

    return pjturn_listener_sendto(listener, pkt, pkt_size, 0, 
				  dst_addr, addr_len);
}

/* Create and send error response */
static pj_status_t respond_error(pj_stun_session *sess, const pj_stun_msg *req,
				 pj_bool_t cache, int code, const char *errmsg,
				 const pj_sockaddr_t *dst_addr, 
				 unsigned addr_len)
{
    pj_status_t status;
    pj_str_t reason;
    pj_stun_tx_data *tdata;

    status = pj_stun_session_create_res(sess, req, 
				        code, (errmsg?pj_cstr(&reason,errmsg):NULL), 
					&tdata);
    if (status != PJ_SUCCESS)
	return status;

    status = pj_stun_session_send_msg(sess, cache, dst_addr,  addr_len, tdata);
    return status;

}

/* Parse ALLOCATE request */
static pj_status_t parse_allocate_req(pjturn_allocation_req *cfg,
				      pjturn_listener *listener,
				      pj_stun_session *sess,
				      const pj_stun_msg *req,
				      const pj_sockaddr_t *src_addr,
				      unsigned src_addr_len)
{
    pj_stun_bandwidth_attr *attr_bw;
    pj_stun_req_transport_attr *attr_req_tp;
    pj_stun_req_ip_attr *attr_req_ip;
    pj_stun_req_port_props_attr *attr_rpp;
    pj_stun_lifetime_attr *attr_lifetime;

    pj_bzero(cfg, sizeof(*cfg));

    /* Get BANDWIDTH attribute, if any. */
    attr_bw = (pj_stun_uint_attr*)
	      pj_stun_msg_find_attr(req, PJ_STUN_ATTR_BANDWIDTH, 0);
    if (attr_bw) {
	cfg->bandwidth = attr_bw->value;
    } else {
	cfg->bandwidth = DEFA_CLIENT_BANDWIDTH;
    }

    /* Check if we can satisfy the bandwidth */
    if (cfg->bandwidth > MAX_CLIENT_BANDWIDTH) {
	respond_error(sess, req, PJ_FALSE, 
		      PJ_STUN_SC_ALLOCATION_QUOTA_REACHED, 
		      "Invalid bandwidth", src_addr, src_addr_len);
	return -1;
    }

    /* Get REQUESTED-TRANSPORT attribute, is any */
    attr_req_tp = (pj_stun_uint_attr*)
	          pj_stun_msg_find_attr(req, PJ_STUN_ATTR_REQ_TRANSPORT, 0);
    if (attr_req_tp) {
	cfg->tp_type = PJ_STUN_GET_RT_PROTO(attr_req_tp->value);
    } else {
	cfg->tp_type = listener->tp_type;
    }

    /* Can only support UDP for now */
    if (cfg->tp_type != PJTURN_TP_UDP) {
	respond_error(sess, req, PJ_FALSE, 
		      PJ_STUN_SC_UNSUPP_TRANSPORT_PROTO, 
		      NULL, src_addr, src_addr_len);
	return -1;
    }

    /* Get REQUESTED-IP attribute, if any */
    attr_req_ip = (pj_stun_sockaddr_attr*)
	          pj_stun_msg_find_attr(req, PJ_STUN_ATTR_REQ_IP, 0);
    if (attr_req_ip) {
	pj_sockaddr_print(&attr_req_ip->sockaddr, cfg->addr, 
			  sizeof(cfg->addr), 0);
    }

    /* Get REQUESTED-PORT-PROPS attribute, if any */
    attr_rpp = (pj_stun_uint_attr*)
	       pj_stun_msg_find_attr(req, PJ_STUN_ATTR_REQ_PORT_PROPS, 0);
    if (attr_rpp) {
	cfg->rpp_bits = PJ_STUN_GET_RPP_BITS(attr_rpp->value);
	cfg->rpp_port = PJ_STUN_GET_RPP_PORT(attr_rpp->value);
    } else {
	cfg->rpp_bits = 0;
	cfg->rpp_port = 0;
    }

    /* Get LIFETIME attribute */
    attr_lifetime = (pj_stun_uint_attr*)
	            pj_stun_msg_find_attr(req, PJ_STUN_ATTR_LIFETIME, 0);
    if (attr_lifetime) {
	cfg->lifetime = attr_lifetime->value;
	if (cfg->lifetime < MIN_LIFETIME || cfg->lifetime > MAX_LIFETIME) {
	    respond_error(sess, req, PJ_FALSE, 
			  PJ_STUN_SC_BAD_REQUEST, 
			  "Invalid LIFETIME value", src_addr, 
			  src_addr_len);
	    return -1;
	}
    } else {
	cfg->lifetime = DEF_LIFETIME;
    }

    return PJ_SUCCESS;
}

/* Callback from our own STUN session when incoming request arrives */
static pj_status_t on_rx_stun_request(pj_stun_session *sess,
				      const pj_uint8_t *pkt,
				      unsigned pkt_len,
				      const pj_stun_msg *msg,
				      const pj_sockaddr_t *src_addr,
				      unsigned src_addr_len)
{
    pjturn_listener *listener;
    pjturn_srv *srv;
    pjturn_allocation_req req;
    pjturn_allocation *alloc;
    pj_stun_tx_data *tdata;
    pj_status_t status;

    listener = (pjturn_listener*) pj_stun_session_get_user_data(sess);
    srv = listener->server;

    /* Handle strayed REFRESH request */
    if (msg->hdr.type == PJ_STUN_REFRESH_REQUEST) {
	return respond_error(sess, msg, PJ_FALSE, 
			     PJ_STUN_SC_ALLOCATION_MISMATCH,
			     NULL, src_addr, src_addr_len);
    }

    /* Respond any other requests with Bad Request response */
    if (msg->hdr.type != PJ_STUN_ALLOCATE_REQUEST) {
	return respond_error(sess, msg, PJ_FALSE, PJ_STUN_SC_BAD_REQUEST,
			     NULL, src_addr, src_addr_len);
    }

    /* We have ALLOCATE request here, and it's authenticated. Parse the
     * request.
     */
    status = parse_allocate_req(&req, listener, sess, msg, src_addr, 
				src_addr_len);
    if (status != PJ_SUCCESS)
	return status;

    /* Create new allocation. The relay resource will be allocated
     * in this function.
     */
    status = pjturn_allocation_create(listener, src_addr, src_addr_len,
				      msg, &req, &alloc);
    if (status != PJ_SUCCESS) {
	char errmsg[PJ_ERR_MSG_SIZE];

	pj_strerror(status, errmsg, sizeof(errmsg));
	return respond_error(sess, msg, PJ_FALSE, PJ_STUN_SC_SERVER_ERROR,
			     errmsg, src_addr, src_addr_len);
    }

    /* Respond the original ALLOCATE request */
    status = pj_stun_session_create_res(srv->core.stun_sess[listener->id],
					msg, 0, NULL, &tdata);
    if (status != PJ_SUCCESS) {
	char errmsg[PJ_ERR_MSG_SIZE];

	pjturn_allocation_destroy(alloc);

	pj_strerror(status, errmsg, sizeof(errmsg));
	return respond_error(sess, msg, PJ_FALSE, PJ_STUN_SC_SERVER_ERROR,
			     errmsg, src_addr, src_addr_len);
    }

    /* Add RELAYED-ADDRESS attribute */
    pj_stun_msg_add_sockaddr_attr(tdata->pool, tdata->msg,
				  PJ_STUN_ATTR_RELAY_ADDR, PJ_TRUE,
				  &alloc->relay.hkey.addr,
				  pj_sockaddr_get_len(&alloc->relay.hkey.addr));

    /* Add LIFETIME. */
    pj_stun_msg_add_uint_attr(tdata->pool, tdata->msg,
			      PJ_STUN_ATTR_LIFETIME, 
			      (unsigned)alloc->relay.lifetime);

    /* Add BANDWIDTH */
    pj_stun_msg_add_uint_attr(tdata->pool, tdata->msg,
			      PJ_STUN_ATTR_BANDWIDTH,
			      alloc->bandwidth);

    /* Add RESERVATION-TOKEN */
    PJ_TODO(ADD_RESERVATION_TOKEN);

    /* Add XOR-MAPPED-ADDRESS */
    pj_stun_msg_add_sockaddr_attr(tdata->pool, tdata->msg,
				  PJ_STUN_ATTR_XOR_MAPPED_ADDR, PJ_TRUE,
				  &alloc->hkey.clt_addr,
				  pj_sockaddr_get_len(&alloc->hkey.clt_addr));
    
    /* Send the response */
    pj_stun_session_send_msg(srv->core.stun_sess[listener->id], PJ_TRUE,
			     src_addr, src_addr_len, tdata);

    /* Done. */
    return PJ_SUCCESS;
}


/* Handle packet from new client address. */
static void handle_new_client( pjturn_srv *srv, 
			       pjturn_pkt *pkt)
{
    unsigned options, lis_id;
    pj_status_t status;

    /* Check that this is a STUN message */
    options = PJ_STUN_CHECK_PACKET;
    if (pkt->listener->tp_type == PJTURN_TP_UDP)
	options |= PJ_STUN_IS_DATAGRAM;

    status = pj_stun_msg_check(pkt->pkt, pkt->len, options);
    if (status != PJ_SUCCESS) {
	char errmsg[PJ_ERR_MSG_SIZE];
	char ip[PJ_INET6_ADDRSTRLEN+10];

	pj_strerror(status, errmsg, sizeof(errmsg));
	PJ_LOG(5,(srv->core.obj_name, 
	          "Non STUN packet from %s is dropped: %s",
		  pj_sockaddr_print(&pkt->src.clt_addr, ip, sizeof(ip), 3),
		  errmsg));
	return;
    }

    lis_id = pkt->listener->id;

    /* Hand over processing to STUN session */
    options &= ~PJ_STUN_CHECK_PACKET;
    status = pj_stun_session_on_rx_pkt(srv->core.stun_sess[lis_id], pkt->pkt, 
				       pkt->len, options, NULL,
				       &pkt->src.clt_addr, 
				       pkt->src_addr_len);
    if (status != PJ_SUCCESS) {
	char errmsg[PJ_ERR_MSG_SIZE];
	char ip[PJ_INET6_ADDRSTRLEN+10];

	pj_strerror(status, errmsg, sizeof(errmsg));
	PJ_LOG(5,(srv->core.obj_name, 
	          "Error processing STUN packet from %s: %s",
		  pj_sockaddr_print(&pkt->src.clt_addr, ip, sizeof(ip), 3),
		  errmsg));
	return;
    }
}


/*
 * This callback is called by UDP listener on incoming packet.
 */
PJ_DEF(void) pjturn_srv_on_rx_pkt( pjturn_srv *srv, 
				   pjturn_pkt *pkt)
{
    pjturn_allocation *alloc;

    /* Get TURN allocation from the source address */
    pj_lock_acquire(srv->core.lock);
    alloc = pj_hash_get(srv->tables.alloc, &pkt->src, sizeof(pkt->src), NULL);
    pj_lock_release(srv->core.lock);

    /* If allocation is found, just hand over the packet to the
     * allocation.
     */
    if (alloc) {
	pjturn_allocation_on_rx_client_pkt(alloc, pkt);
    } else {
	/* Otherwise this is a new client */
	handle_new_client(srv, pkt);
    }
}