summaryrefslogtreecommitdiff
path: root/pjnath/src/pjturn-srv/turn.h
blob: 39a17fae05a8ecbabea5601e52359d3d5916772a (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
/* $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 
 */
#ifndef __PJTURN_SRV_TURN_H__
#define __PJTURN_SRV_TURN_H__

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

typedef struct pjturn_relay_res	    pjturn_relay_res;
typedef struct pjturn_listener	    pjturn_listener;
typedef struct pjturn_permission    pjturn_permission;
typedef struct pjturn_allocation    pjturn_allocation;
typedef struct pjturn_srv	    pjturn_srv;
typedef struct pjturn_pkt	    pjturn_pkt;


#define PJTURN_INVALID_CHANNEL	    0xFFFF
#define PJTURN_NO_TIMEOUT	    ((long)0x7FFFFFFF)
#define PJTURN_MAX_PKT_LEN	    3000

/** Transport types */
enum {
    PJTURN_TP_UDP = 16,	    /**< UDP.	*/
    PJTURN_TP_TCP = 6	    /**< TCP.	*/
};


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

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

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

    /** Mutex */
    pj_lock_t	    *lock;

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

    /** Time when this resource times out */
    pj_time_val	    timeout;

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

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

    /** Transport/relay socket */
    pj_sock_t	    sock;
};


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

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


/**
 * Allocation request.
 */
typedef struct pjturn_allocation_req
{
    /** Requested transport */
    unsigned		tp_type;

    /** Requested IP */
    pj_sockaddr		addr;

    /** Requested bandwidth */
    unsigned		bandwidth;

    /** Lifetime. */
    unsigned		lifetime;

    /** A bits */
    unsigned		rpp_bits;

    /** Requested port */
    unsigned		rpp_port;

} pjturn_allocation_req;


/**
 * This structure describes TURN pjturn_allocation session.
 */
struct pjturn_allocation
{
    /** Hash table key to identify client. */
    pjturn_allocation_key key;

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

    /** Mutex */
    pj_lock_t		*lock;

    /** TURN listener. */
    pjturn_listener	*listener;

    /** Client socket, if connection to client is using TCP. */
    pj_sock_t		clt_sock;

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

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

};


/**
 * This structure describes TURN pjturn_permission or channel.
 */
struct pjturn_permission
{
    /** Hash table key */
    struct {
	/** Transport type. */
	pj_uint16_t		tp_type;

	/** Transport socket. If TCP is used, the value will be the actual
	 *  TCP socket. If UDP is used, the value will be the relay address
	 */
	pj_sock_t		sock;

	/** Peer address. */
	pj_sockaddr		peer_addr;
    } key;

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

    /** Mutex */
    pj_lock_t		*lock;

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

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

    /** Permission timeout. */
    pj_time_val		timeout;
};

/**
 * Handle incoming packet.
 */
PJ_DECL(void) pjturn_allocation_on_rx_pkt(pjturn_allocation *alloc,
					  pjturn_pkt *pkt);


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

/**
 * This structure describes TURN listener socket. A TURN listener socket
 * listens for incoming connections from clients.
 */
struct pjturn_listener
{
    /** TURN server instance. */
    pjturn_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;

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

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


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

    /** Listener that owns this. */
    pjturn_listener	    *listener;

    /** Packet buffer. */
    pj_uint8_t		    pkt[PJTURN_MAX_PKT_LEN];

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

    /** Arrival time. */
    pj_time_val		    rx_time;

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

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


/**
 * Create a new listener on the specified port.
 */
PJ_DECL(pj_status_t) pjturn_listener_create_udp(pjturn_srv *srv,
						int af,
					        const pj_str_t *bound_addr,
					        unsigned port,
						unsigned concurrency_cnt,
						unsigned flags,
						pjturn_listener **p_listener);

/**
 * Send packet with this listener.
 */
PJ_DECL(pj_status_t) pjturn_listener_sendto(pjturn_listener *listener,
					    const void *packet,
					    pj_size_t size,
					    unsigned flag,
					    const pj_sockaddr_t *addr,
					    int addr_len);

/**
 * Destroy listener.
 */
PJ_DECL(pj_status_t) pjturn_listener_destroy(pjturn_listener *listener);


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

	/** 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. */
	pjturn_listener **listener;

	/** Array of STUN sessions, one for each listeners. */
	pj_stun_session	**stun_sess;

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

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

	/** STUN config. */
	pj_stun_config	 stun_cfg;


    } 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;

	/** Permission hash table, indexed by transport type, socket handle,
	 *  and peer address.
	 */
	pj_hash_table_t *peer;

    } 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) pjturn_srv_create(pj_pool_factory *pf,
				       pjturn_srv **p_srv);

/** 
 * Destroy server.
 */
PJ_DECL(pj_status_t) pjturn_srv_destroy(pjturn_srv *srv);

/** 
 * Add listener.
 */
PJ_DECL(pj_status_t) pjturn_srv_add_listener(pjturn_srv *srv,
					     pjturn_listener *lis);

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


#endif	/* __PJTURN_SRV_TURN_H__ */