summaryrefslogtreecommitdiff
path: root/pjnath/src/pjturn-srv/turn.h
blob: 2fe9f9d4f4affb76dc491a82c0d81234d561f313 (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
/* $Id: turn.h 3553 2011-05-05 06:14:19Z nanang $ */
/* 
 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
 * Copyright (C) 2003-2008 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 
 */
#ifndef __PJ_TURN_SRV_TURN_H__
#define __PJ_TURN_SRV_TURN_H__

#include <pjlib.h>
#include <pjnath.h>

typedef struct pj_turn_relay_res    pj_turn_relay_res;
typedef struct pj_turn_listener	    pj_turn_listener;
typedef struct pj_turn_transport    pj_turn_transport;
typedef struct pj_turn_permission   pj_turn_permission;
typedef struct pj_turn_allocation   pj_turn_allocation;
typedef struct pj_turn_srv	    pj_turn_srv;
typedef struct pj_turn_pkt	    pj_turn_pkt;


#define PJ_TURN_INVALID_LIS_ID	    ((unsigned)-1)

/** 
 * Get transport type name string.
 */
PJ_DECL(const char*) pj_turn_tp_type_name(int tp_type);

/**
 * This structure describes TURN relay resource. An allocation allocates
 * one relay resource, and optionally it may reserve another resource.
 */
struct pj_turn_relay_res
{
    /** Hash table key */
    struct {
	/** Transport type. */
	int		    tp_type;

	/** Transport/relay address */
	pj_sockaddr	    addr;
    } hkey;

    /** Allocation who requested or reserved this resource. */
    pj_turn_allocation *allocation;

    /** Username used in credential */
    pj_str_t	    user;

    /** Realm used in credential. */
    pj_str_t	    realm;

    /** Lifetime, in seconds. */
    unsigned	    lifetime;

    /** Relay/allocation expiration time */
    pj_time_val	    expiry;

    /** Timeout timer entry */
    pj_timer_entry  timer;

    /** Transport. */
    struct {
	/** Transport/relay socket */
	pj_sock_t	    sock;

	/** Transport/relay ioqueue */
	pj_ioqueue_key_t    *key;

	/** Read operation key. */
	pj_ioqueue_op_key_t read_key;

	/** The incoming packet buffer */
	char		    rx_pkt[PJ_TURN_MAX_PKT_LEN];

	/** Source address of the packet. */
	pj_sockaddr	    src_addr;

	/** Source address length */
	int		    src_addr_len;

	/** The outgoing packet buffer. This must be 3wbit aligned. */
	char		    tx_pkt[PJ_TURN_MAX_PKT_LEN+4];
    } tp;
};


/****************************************************************************/
/*
 * TURN Allocation API
 */

/**
 * This structure describes key to lookup TURN allocations in the
 * allocation hash table.
 */
typedef struct pj_turn_allocation_key
{
    int		    tp_type;	/**< Transport type.	    */
    pj_sockaddr	    clt_addr;	/**< Client's address.	    */
} pj_turn_allocation_key;


/**
 * This structure describes TURN pj_turn_allocation session.
 */
struct pj_turn_allocation
{
    /** Hash table key to identify client. */
    pj_turn_allocation_key hkey;

    /** Pool for this allocation. */
    pj_pool_t		*pool;

    /** Object name for logging identification */
    char		*obj_name;

    /** Client info (IP address and port) */
    char		info[80];

    /** Mutex */
    pj_lock_t		*lock;

    /** Server instance. */
    pj_turn_srv		*server;

    /** Transport to send/receive packets to/from client. */
    pj_turn_transport	*transport;

    /** The relay resource for this allocation. */
    pj_turn_relay_res	relay;

    /** Relay resource reserved by this allocation, if any */
    pj_turn_relay_res	*resv;

    /** Requested bandwidth */
    unsigned		bandwidth;

    /** STUN session for this client */
    pj_stun_session	*sess;

    /** Credential for this STUN session. */
    pj_stun_auth_cred	 cred;

    /** Peer hash table (keyed by peer address) */
    pj_hash_table_t	*peer_table;

    /** Channel hash table (keyed by channel number) */
    pj_hash_table_t	*ch_table;
};


/**
 * This structure describes the hash table key to lookup TURN
 * permission.
 */
typedef struct pj_turn_permission_key
{
    /** Peer address. */
    pj_sockaddr		peer_addr;

} pj_turn_permission_key;


/**
 * This structure describes TURN pj_turn_permission or channel.
 */
struct pj_turn_permission
{
    /** Hash table key */
    pj_turn_permission_key hkey;

    /** TURN allocation that owns this permission/channel */
    pj_turn_allocation	*allocation;

    /** Optional channel number, or PJ_TURN_INVALID_CHANNEL if channel number
     *  is not requested for this permission. 
     */
    pj_uint16_t		channel;

    /** Permission expiration time. */
    pj_time_val		expiry;
};

/**
 * Create new allocation.
 */
PJ_DECL(pj_status_t) pj_turn_allocation_create(pj_turn_transport *transport,
					       const pj_sockaddr_t *src_addr,
					       unsigned src_addr_len,
					       const pj_stun_rx_data *rdata,
					       pj_stun_session *srv_sess,
					       pj_turn_allocation **p_alloc);
/**
 * Destroy allocation.
 */
PJ_DECL(void) pj_turn_allocation_destroy(pj_turn_allocation *alloc);


/**
 * Handle incoming packet from client.
 */
PJ_DECL(void) pj_turn_allocation_on_rx_client_pkt(pj_turn_allocation *alloc,
						  pj_turn_pkt *pkt);

/**
 * Handle transport closure.
 */
PJ_DECL(void) pj_turn_allocation_on_transport_closed(pj_turn_allocation *alloc,
						     pj_turn_transport *tp);

/****************************************************************************/
/*
 * TURN Listener API
 */

/**
 * This structure describes TURN listener socket. A TURN listener socket
 * listens for incoming connections from clients.
 */
struct pj_turn_listener
{
    /** Object name/identification */
    char		*obj_name;

    /** Slightly longer info about this listener */
    char		info[80];

    /** TURN server instance. */
    pj_turn_srv		*server;

    /** Listener index in the server */
    unsigned		id;

    /** Pool for this listener. */
    pj_pool_t	       *pool;

    /** Transport type. */
    int			tp_type;

    /** Bound address of this listener. */
    pj_sockaddr		addr;

    /** Socket. */
    pj_sock_t		sock;

    /** Flags. */
    unsigned		flags;

    /** Destroy handler */
    pj_status_t		(*destroy)(pj_turn_listener*);
};


/**
 * This structure describes TURN transport socket which is used to send and
 * receive packets towards client.
 */
struct pj_turn_transport
{
    /** Object name/identification */
    char		*obj_name;

    /** Slightly longer info about this listener */
    char		*info;

    /** Listener instance */
    pj_turn_listener	*listener;

    /** Sendto handler */
    pj_status_t		(*sendto)(pj_turn_transport *tp,
				  const void *packet,
				  pj_size_t size,
				  unsigned flag,
				  const pj_sockaddr_t *addr,
				  int addr_len);

    /** Addref handler */
    void		(*add_ref)(pj_turn_transport *tp,
				   pj_turn_allocation *alloc);

    /** Decref handler */
    void		(*dec_ref)(pj_turn_transport *tp,
				   pj_turn_allocation *alloc);

};


/**
 * An incoming packet.
 */
struct pj_turn_pkt
{
    /** Pool for this packet */
    pj_pool_t		    *pool;

    /** Transport where the packet was received. */
    pj_turn_transport	    *transport;

    /** Packet buffer (must be 32bit aligned). */
    pj_uint8_t		    pkt[PJ_TURN_MAX_PKT_LEN];

    /** Size of the packet */
    pj_size_t		    len;

    /** Arrival time. */
    pj_time_val		    rx_time;

    /** Source transport type and source address. */
    pj_turn_allocation_key   src;

    /** Source address length. */
    int			    src_addr_len;
};


/**
 * Create a UDP listener on the specified port.
 */
PJ_DECL(pj_status_t) pj_turn_listener_create_udp(pj_turn_srv *srv,
						 int af,
					         const pj_str_t *bound_addr,
					         unsigned port,
						 unsigned concurrency_cnt,
						 unsigned flags,
						 pj_turn_listener **p_lis);

/**
 * Create a TCP listener on the specified port.
 */
PJ_DECL(pj_status_t) pj_turn_listener_create_tcp(pj_turn_srv *srv,
						 int af,
					         const pj_str_t *bound_addr,
					         unsigned port,
						 unsigned concurrency_cnt,
						 unsigned flags,
						 pj_turn_listener **p_lis);

/**
 * Destroy listener.
 */
PJ_DECL(pj_status_t) pj_turn_listener_destroy(pj_turn_listener *listener);


/**
 * Add a reference to a transport.
 */
PJ_DECL(void) pj_turn_transport_add_ref(pj_turn_transport *transport,
					pj_turn_allocation *alloc);


/**
 * Decrement transport reference counter.
 */
PJ_DECL(void) pj_turn_transport_dec_ref(pj_turn_transport *transport,
					pj_turn_allocation *alloc);



/****************************************************************************/
/*
 * TURN Server API
 */
/**
 * This structure describes TURN pj_turn_srv instance.
 */
struct pj_turn_srv
{
    /** Object name */
    char	*obj_name;

    /** Core settings */
    struct {
	/** Pool factory */
	pj_pool_factory *pf;

	/** Pool for this server instance. */
	pj_pool_t       *pool;

	/** Global Ioqueue */
	pj_ioqueue_t    *ioqueue;

	/** Mutex */
	pj_lock_t	*lock;

	/** Global timer heap instance. */
	pj_timer_heap_t *timer_heap;

	/** Number of listeners */
	unsigned         lis_cnt;

	/** Array of listeners. */
	pj_turn_listener **listener;

	/** STUN session to handle initial Allocate request. */
	pj_stun_session	*stun_sess;

	/** Number of worker threads. */
	unsigned        thread_cnt;

	/** Array of worker threads. */
	pj_thread_t     **thread;

	/** Thread quit signal */
	pj_bool_t	quit;

	/** STUN config. */
	pj_stun_config	 stun_cfg;

	/** STUN auth credential. */
	pj_stun_auth_cred cred;

	/** Thread local ID for storing credential */
	long		 tls_key, tls_data;

    } core;

    
    /** Hash tables */
    struct {
	/** Allocations hash table, indexed by transport type and
	 *  client address. 
	 */
	pj_hash_table_t *alloc;

	/** Relay resource hash table, indexed by transport type and
	 *  relay address. 
	 */
	pj_hash_table_t *res;

    } tables;

    /** Ports settings */
    struct {
	/** Minimum UDP port number. */
	pj_uint16_t	    min_udp;

	/** Maximum UDP port number. */
	pj_uint16_t	    max_udp;

	/** Next UDP port number. */
	pj_uint16_t	    next_udp;


	/** Minimum TCP port number. */
	pj_uint16_t	    min_tcp;

	/** Maximum TCP port number. */
	pj_uint16_t	    max_tcp;

	/** Next TCP port number. */
	pj_uint16_t	    next_tcp;

    } ports;
};


/** 
 * Create server.
 */
PJ_DECL(pj_status_t) pj_turn_srv_create(pj_pool_factory *pf,
				        pj_turn_srv **p_srv);

/** 
 * Destroy server.
 */
PJ_DECL(pj_status_t) pj_turn_srv_destroy(pj_turn_srv *srv);

/** 
 * Add listener.
 */
PJ_DECL(pj_status_t) pj_turn_srv_add_listener(pj_turn_srv *srv,
					      pj_turn_listener *lis);

/**
 * Register an allocation.
 */
PJ_DECL(pj_status_t) pj_turn_srv_register_allocation(pj_turn_srv *srv,
						     pj_turn_allocation *alloc);

/**
 * Unregister an allocation.
 */
PJ_DECL(pj_status_t) pj_turn_srv_unregister_allocation(pj_turn_srv *srv,
						       pj_turn_allocation *alloc);

/**
 * This callback is called by UDP listener on incoming packet.
 */
PJ_DECL(void) pj_turn_srv_on_rx_pkt(pj_turn_srv *srv, 
				    pj_turn_pkt *pkt);


#endif	/* __PJ_TURN_SRV_TURN_H__ */