1
/* $Id: turn.h 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
#ifndef __PJ_TURN_SRV_TURN_H__
21
#define __PJ_TURN_SRV_TURN_H__
26
typedef struct pj_turn_relay_res pj_turn_relay_res;
27
typedef struct pj_turn_listener pj_turn_listener;
28
typedef struct pj_turn_transport pj_turn_transport;
29
typedef struct pj_turn_permission pj_turn_permission;
30
typedef struct pj_turn_allocation pj_turn_allocation;
31
typedef struct pj_turn_srv pj_turn_srv;
32
typedef struct pj_turn_pkt pj_turn_pkt;
35
#define PJ_TURN_INVALID_LIS_ID ((unsigned)-1)
38
* Get transport type name string.
40
PJ_DECL(const char*) pj_turn_tp_type_name(int tp_type);
43
* This structure describes TURN relay resource. An allocation allocates
44
* one relay resource, and optionally it may reserve another resource.
46
struct pj_turn_relay_res
50
/** Transport type. */
53
/** Transport/relay address */
57
/** Allocation who requested or reserved this resource. */
58
pj_turn_allocation *allocation;
60
/** Username used in credential */
63
/** Realm used in credential. */
66
/** Lifetime, in seconds. */
69
/** Relay/allocation expiration time */
72
/** Timeout timer entry */
77
/** Transport/relay socket */
80
/** Transport/relay ioqueue */
81
pj_ioqueue_key_t *key;
83
/** Read operation key. */
84
pj_ioqueue_op_key_t read_key;
86
/** The incoming packet buffer */
87
char rx_pkt[PJ_TURN_MAX_PKT_LEN];
89
/** Source address of the packet. */
92
/** Source address length */
95
/** The outgoing packet buffer. This must be 3wbit aligned. */
96
char tx_pkt[PJ_TURN_MAX_PKT_LEN+4];
101
/****************************************************************************/
103
* TURN Allocation API
107
* This structure describes key to lookup TURN allocations in the
108
* allocation hash table.
110
typedef struct pj_turn_allocation_key
112
int tp_type; /**< Transport type. */
113
pj_sockaddr clt_addr; /**< Client's address. */
114
} pj_turn_allocation_key;
118
* This structure describes TURN pj_turn_allocation session.
120
struct pj_turn_allocation
122
/** Hash table key to identify client. */
123
pj_turn_allocation_key hkey;
125
/** Pool for this allocation. */
128
/** Object name for logging identification */
131
/** Client info (IP address and port) */
137
/** Server instance. */
140
/** Transport to send/receive packets to/from client. */
141
pj_turn_transport *transport;
143
/** The relay resource for this allocation. */
144
pj_turn_relay_res relay;
146
/** Relay resource reserved by this allocation, if any */
147
pj_turn_relay_res *resv;
149
/** Requested bandwidth */
152
/** STUN session for this client */
153
pj_stun_session *sess;
155
/** Credential for this STUN session. */
156
pj_stun_auth_cred cred;
158
/** Peer hash table (keyed by peer address) */
159
pj_hash_table_t *peer_table;
161
/** Channel hash table (keyed by channel number) */
162
pj_hash_table_t *ch_table;
167
* This structure describes the hash table key to lookup TURN
170
typedef struct pj_turn_permission_key
173
pj_sockaddr peer_addr;
175
} pj_turn_permission_key;
179
* This structure describes TURN pj_turn_permission or channel.
181
struct pj_turn_permission
183
/** Hash table key */
184
pj_turn_permission_key hkey;
186
/** TURN allocation that owns this permission/channel */
187
pj_turn_allocation *allocation;
189
/** Optional channel number, or PJ_TURN_INVALID_CHANNEL if channel number
190
* is not requested for this permission.
194
/** Permission expiration time. */
199
* Create new allocation.
201
PJ_DECL(pj_status_t) pj_turn_allocation_create(pj_turn_transport *transport,
202
const pj_sockaddr_t *src_addr,
203
unsigned src_addr_len,
204
const pj_stun_rx_data *rdata,
205
pj_stun_session *srv_sess,
206
pj_turn_allocation **p_alloc);
208
* Destroy allocation.
210
PJ_DECL(void) pj_turn_allocation_destroy(pj_turn_allocation *alloc);
214
* Handle incoming packet from client.
216
PJ_DECL(void) pj_turn_allocation_on_rx_client_pkt(pj_turn_allocation *alloc,
220
* Handle transport closure.
222
PJ_DECL(void) pj_turn_allocation_on_transport_closed(pj_turn_allocation *alloc,
223
pj_turn_transport *tp);
225
/****************************************************************************/
231
* This structure describes TURN listener socket. A TURN listener socket
232
* listens for incoming connections from clients.
234
struct pj_turn_listener
236
/** Object name/identification */
239
/** Slightly longer info about this listener */
242
/** TURN server instance. */
245
/** Listener index in the server */
248
/** Pool for this listener. */
251
/** Transport type. */
254
/** Bound address of this listener. */
263
/** Destroy handler */
264
pj_status_t (*destroy)(pj_turn_listener*);
269
* This structure describes TURN transport socket which is used to send and
270
* receive packets towards client.
272
struct pj_turn_transport
274
/** Object name/identification */
277
/** Slightly longer info about this listener */
280
/** Listener instance */
281
pj_turn_listener *listener;
283
/** Sendto handler */
284
pj_status_t (*sendto)(pj_turn_transport *tp,
288
const pj_sockaddr_t *addr,
291
/** Addref handler */
292
void (*add_ref)(pj_turn_transport *tp,
293
pj_turn_allocation *alloc);
295
/** Decref handler */
296
void (*dec_ref)(pj_turn_transport *tp,
297
pj_turn_allocation *alloc);
303
* An incoming packet.
307
/** Pool for this packet */
310
/** Transport where the packet was received. */
311
pj_turn_transport *transport;
313
/** Packet buffer (must be 32bit aligned). */
314
pj_uint8_t pkt[PJ_TURN_MAX_PKT_LEN];
316
/** Size of the packet */
322
/** Source transport type and source address. */
323
pj_turn_allocation_key src;
325
/** Source address length. */
331
* Create a UDP listener on the specified port.
333
PJ_DECL(pj_status_t) pj_turn_listener_create_udp(pj_turn_srv *srv,
335
const pj_str_t *bound_addr,
337
unsigned concurrency_cnt,
339
pj_turn_listener **p_lis);
342
* Create a TCP listener on the specified port.
344
PJ_DECL(pj_status_t) pj_turn_listener_create_tcp(pj_turn_srv *srv,
346
const pj_str_t *bound_addr,
348
unsigned concurrency_cnt,
350
pj_turn_listener **p_lis);
355
PJ_DECL(pj_status_t) pj_turn_listener_destroy(pj_turn_listener *listener);
359
* Add a reference to a transport.
361
PJ_DECL(void) pj_turn_transport_add_ref(pj_turn_transport *transport,
362
pj_turn_allocation *alloc);
366
* Decrement transport reference counter.
368
PJ_DECL(void) pj_turn_transport_dec_ref(pj_turn_transport *transport,
369
pj_turn_allocation *alloc);
373
/****************************************************************************/
378
* This structure describes TURN pj_turn_srv instance.
390
/** Pool for this server instance. */
393
/** Global Ioqueue */
394
pj_ioqueue_t *ioqueue;
399
/** Global timer heap instance. */
400
pj_timer_heap_t *timer_heap;
402
/** Number of listeners */
405
/** Array of listeners. */
406
pj_turn_listener **listener;
408
/** STUN session to handle initial Allocate request. */
409
pj_stun_session *stun_sess;
411
/** Number of worker threads. */
414
/** Array of worker threads. */
415
pj_thread_t **thread;
417
/** Thread quit signal */
421
pj_stun_config stun_cfg;
423
/** STUN auth credential. */
424
pj_stun_auth_cred cred;
426
/** Thread local ID for storing credential */
427
long tls_key, tls_data;
434
/** Allocations hash table, indexed by transport type and
437
pj_hash_table_t *alloc;
439
/** Relay resource hash table, indexed by transport type and
442
pj_hash_table_t *res;
446
/** Ports settings */
448
/** Minimum UDP port number. */
451
/** Maximum UDP port number. */
454
/** Next UDP port number. */
455
pj_uint16_t next_udp;
458
/** Minimum TCP port number. */
461
/** Maximum TCP port number. */
464
/** Next TCP port number. */
465
pj_uint16_t next_tcp;
474
PJ_DECL(pj_status_t) pj_turn_srv_create(pj_pool_factory *pf,
475
pj_turn_srv **p_srv);
480
PJ_DECL(pj_status_t) pj_turn_srv_destroy(pj_turn_srv *srv);
485
PJ_DECL(pj_status_t) pj_turn_srv_add_listener(pj_turn_srv *srv,
486
pj_turn_listener *lis);
489
* Register an allocation.
491
PJ_DECL(pj_status_t) pj_turn_srv_register_allocation(pj_turn_srv *srv,
492
pj_turn_allocation *alloc);
495
* Unregister an allocation.
497
PJ_DECL(pj_status_t) pj_turn_srv_unregister_allocation(pj_turn_srv *srv,
498
pj_turn_allocation *alloc);
501
* This callback is called by UDP listener on incoming packet.
503
PJ_DECL(void) pj_turn_srv_on_rx_pkt(pj_turn_srv *srv,
507
#endif /* __PJ_TURN_SRV_TURN_H__ */