blob: bbff6bf7b508de197dbdf9f9e7d05ace5ca16145 [file] [log] [blame]
Alexandre Lision8af73cb2013-12-10 14:11:20 -05001/* $Id$ */
2/*
3 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5 *
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.
10 *
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.
15 *
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
19 */
20#ifndef __PJNATH_TURN_SOCK_H__
21#define __PJNATH_TURN_SOCK_H__
22
23/**
24 * @file turn_sock.h
25 * @brief TURN relay using UDP client as transport protocol
26 */
27#include <pjnath/turn_session.h>
28#include <pj/sock_qos.h>
29
30
31PJ_BEGIN_DECL
32
33
34/* **************************************************************************/
35/**
36@addtogroup PJNATH_TURN_SOCK
37@{
38
39This is a ready to use object for relaying application data via a TURN server,
40by managing all the operations in \ref turn_op_sec.
41
42\section turnsock_using_sec Using TURN transport
43
44This object provides a thin wrapper to the \ref PJNATH_TURN_SESSION, hence the
45API is very much the same (apart from the obvious difference in the names).
46Please see \ref PJNATH_TURN_SESSION for the documentation on how to use the
47session.
48
49\section turnsock_samples_sec Samples
50
51The \ref turn_client_sample is a sample application to use the
52\ref PJNATH_TURN_SOCK.
53
54Also see <b>\ref samples_page</b> for other samples.
55
56 */
57
58
59/**
60 * Opaque declaration for TURN client.
61 */
62typedef struct pj_turn_sock pj_turn_sock;
63
64/**
65 * This structure contains callbacks that will be called by the TURN
66 * transport.
67 */
68typedef struct pj_turn_sock_cb
69{
70 /**
71 * Notification when incoming data has been received from the remote
72 * peer via the TURN server. The data reported in this callback will
73 * be the exact data as sent by the peer (e.g. the TURN encapsulation
74 * such as Data Indication or ChannelData will be removed before this
75 * function is called).
76 *
77 * @param turn_sock The TURN client transport.
78 * @param data The data as received from the peer.
79 * @param data_len Length of the data.
80 * @param peer_addr The peer address.
81 * @param addr_len The length of the peer address.
82 */
83 void (*on_rx_data)(pj_turn_sock *turn_sock,
84 void *pkt,
85 unsigned pkt_len,
86 const pj_sockaddr_t *peer_addr,
87 unsigned addr_len);
88
89 /**
90 * Notification when TURN session state has changed. Application should
91 * implement this callback to monitor the progress of the TURN session.
92 *
93 * @param turn_sock The TURN client transport.
94 * @param old_state Previous state.
95 * @param new_state Current state.
96 */
97 void (*on_state)(pj_turn_sock *turn_sock,
98 pj_turn_state_t old_state,
99 pj_turn_state_t new_state);
100
101} pj_turn_sock_cb;
102
103
104/**
105 * This structure describes options that can be specified when creating
106 * the TURN socket. Application should call #pj_turn_sock_cfg_default()
107 * to initialize this structure with its default values before using it.
108 */
109typedef struct pj_turn_sock_cfg
110{
111 /**
112 * The group lock to be used by the STUN socket. If NULL, the STUN socket
113 * will create one internally.
114 *
115 * Default: NULL
116 */
117 pj_grp_lock_t *grp_lock;
118
119 /**
120 * Packet buffer size.
121 *
122 * Default value is PJ_TURN_MAX_PKT_LEN.
123 */
124 unsigned max_pkt_size;
125
126 /**
127 * QoS traffic type to be set on this transport. When application wants
128 * to apply QoS tagging to the transport, it's preferable to set this
129 * field rather than \a qos_param fields since this is more portable.
130 *
131 * Default value is PJ_QOS_TYPE_BEST_EFFORT.
132 */
133 pj_qos_type qos_type;
134
135 /**
136 * Set the low level QoS parameters to the transport. This is a lower
137 * level operation than setting the \a qos_type field and may not be
138 * supported on all platforms.
139 *
140 * By default all settings in this structure are not set.
141 */
142 pj_qos_params qos_params;
143
144 /**
145 * Specify if STUN socket should ignore any errors when setting the QoS
146 * traffic type/parameters.
147 *
148 * Default: PJ_TRUE
149 */
150 pj_bool_t qos_ignore_error;
151
152 /**
153 * Specify the interface where the socket should be bound to. If the
154 * address is zero, socket will be bound to INADDR_ANY. If the address
155 * is non-zero, socket will be bound to this address only. If the port is
156 * set to zero, the socket will bind at any port (chosen by the OS).
157 */
158 pj_sockaddr bound_addr;
159
160 /**
161 * Specify the port range for TURN socket binding, relative to the start
162 * port number specified in \a bound_addr. Note that this setting is only
163 * applicable when the start port number is non zero.
164 *
165 * Default value is zero.
166 */
167 pj_uint16_t port_range;
168
169 /**
170 * Specify target value for socket receive buffer size. It will be
171 * applied using setsockopt(). When it fails to set the specified size,
172 * it will try with lower value until the highest possible has been
173 * successfully set.
174 *
175 * Default: 0 (OS default)
176 */
177 unsigned so_rcvbuf_size;
178
179 /**
180 * Specify target value for socket send buffer size. It will be
181 * applied using setsockopt(). When it fails to set the specified size,
182 * it will try with lower value until the highest possible has been
183 * successfully set.
184 *
185 * Default: 0 (OS default)
186 */
187 unsigned so_sndbuf_size;
188
189} pj_turn_sock_cfg;
190
191
192/**
193 * Initialize pj_turn_sock_cfg structure with default values.
194 */
195PJ_DECL(void) pj_turn_sock_cfg_default(pj_turn_sock_cfg *cfg);
196
197
198/**
199 * Create a TURN transport instance with the specified address family and
200 * connection type. Once TURN transport instance is created, application
201 * must call pj_turn_sock_alloc() to allocate a relay address in the TURN
202 * server.
203 *
204 * @param cfg The STUN configuration which contains among other
205 * things the ioqueue and timer heap instance for
206 * the operation of this transport.
207 * @param af Address family of the client connection. Currently
208 * pj_AF_INET() and pj_AF_INET6() are supported.
209 * @param conn_type Connection type to the TURN server. Both TCP and
210 * UDP are supported.
211 * @param cb Callback to receive events from the TURN transport.
212 * @param setting Optional settings to be specified to the transport.
213 * If this parameter is NULL, default values will be
214 * used.
215 * @param user_data Arbitrary application data to be associated with
216 * this transport.
217 * @param p_turn_sock Pointer to receive the created instance of the
218 * TURN transport.
219 *
220 * @return PJ_SUCCESS if the operation has been successful,
221 * or the appropriate error code on failure.
222 */
223PJ_DECL(pj_status_t) pj_turn_sock_create(pj_stun_config *cfg,
224 int af,
225 pj_turn_tp_type conn_type,
226 const pj_turn_sock_cb *cb,
227 const pj_turn_sock_cfg *setting,
228 void *user_data,
229 pj_turn_sock **p_turn_sock);
230
231/**
232 * Destroy the TURN transport instance. This will gracefully close the
233 * connection between the client and the TURN server. Although this
234 * function will return immediately, the TURN socket deletion may continue
235 * in the background and the application may still get state changes
236 * notifications from this transport.
237 *
238 * @param turn_sock The TURN transport instance.
239 */
240PJ_DECL(void) pj_turn_sock_destroy(pj_turn_sock *turn_sock);
241
242
243/**
244 * Associate a user data with this TURN transport. The user data may then
245 * be retrieved later with #pj_turn_sock_get_user_data().
246 *
247 * @param turn_sock The TURN transport instance.
248 * @param user_data Arbitrary data.
249 *
250 * @return PJ_SUCCESS if the operation has been successful,
251 * or the appropriate error code on failure.
252 */
253PJ_DECL(pj_status_t) pj_turn_sock_set_user_data(pj_turn_sock *turn_sock,
254 void *user_data);
255
256/**
257 * Retrieve the previously assigned user data associated with this TURN
258 * transport.
259 *
260 * @param turn_sock The TURN transport instance.
261 *
262 * @return The user/application data.
263 */
264PJ_DECL(void*) pj_turn_sock_get_user_data(pj_turn_sock *turn_sock);
265
266
267/**
268 * Get the group lock for this TURN transport.
269 *
270 * @param turn_sock The TURN transport instance.
271 *
272 * @return The group lock.
273 */
274PJ_DECL(pj_grp_lock_t *) pj_turn_sock_get_grp_lock(pj_turn_sock *turn_sock);
275
276
277/**
278 * Get the TURN transport info. The transport info contains, among other
279 * things, the allocated relay address.
280 *
281 * @param turn_sock The TURN transport instance.
282 * @param info Pointer to be filled with TURN transport info.
283 *
284 * @return PJ_SUCCESS if the operation has been successful,
285 * or the appropriate error code on failure.
286 */
287PJ_DECL(pj_status_t) pj_turn_sock_get_info(pj_turn_sock *turn_sock,
288 pj_turn_session_info *info);
289
290/**
291 * Acquire the internal mutex of the TURN transport. Application may need
292 * to call this function to synchronize access to other objects alongside
293 * the TURN transport, to avoid deadlock.
294 *
295 * @param turn_sock The TURN transport instance.
296 *
297 * @return PJ_SUCCESS if the operation has been successful,
298 * or the appropriate error code on failure.
299 */
300PJ_DECL(pj_status_t) pj_turn_sock_lock(pj_turn_sock *turn_sock);
301
302
303/**
304 * Release the internal mutex previously held with pj_turn_sock_lock().
305 *
306 * @param turn_sock The TURN transport instance.
307 *
308 * @return PJ_SUCCESS if the operation has been successful,
309 * or the appropriate error code on failure.
310 */
311PJ_DECL(pj_status_t) pj_turn_sock_unlock(pj_turn_sock *turn_sock);
312
313
314/**
315 * Set STUN message logging for this TURN session.
316 * See #pj_stun_session_set_log().
317 *
318 * @param turn_sock The TURN transport instance.
319 * @param flags Bitmask combination of #pj_stun_sess_msg_log_flag
320 */
321PJ_DECL(void) pj_turn_sock_set_log(pj_turn_sock *turn_sock,
322 unsigned flags);
323
324/**
325 * Configure the SOFTWARE name to be sent in all STUN requests by the
326 * TURN session.
327 *
328 * @param turn_sock The TURN transport instance.
329 * @param sw Software name string. If this argument is NULL or
330 * empty, the session will not include SOFTWARE attribute
331 * in STUN requests and responses.
332 *
333 * @return PJ_SUCCESS on success, or the appropriate error code.
334 */
335PJ_DECL(pj_status_t) pj_turn_sock_set_software_name(pj_turn_sock *turn_sock,
336 const pj_str_t *sw);
337
338
339/**
340 * Allocate a relay address/resource in the TURN server. This function
341 * will resolve the TURN server using DNS SRV (if desired) and send TURN
342 * \a Allocate request using the specified credential to allocate a relay
343 * address in the server. This function completes asynchronously, and
344 * application will be notified when the allocation process has been
345 * successful in the \a on_state() callback when the state is set to
346 * PJ_TURN_STATE_READY. If the allocation fails, the state will be set
347 * to PJ_TURN_STATE_DEALLOCATING or greater.
348 *
349 * @param turn_sock The TURN transport instance.
350 * @param domain The domain, hostname, or IP address of the TURN
351 * server. When this parameter contains domain name,
352 * the \a resolver parameter must be set to activate
353 * DNS SRV resolution.
354 * @param default_port The default TURN port number to use when DNS SRV
355 * resolution is not used. If DNS SRV resolution is
356 * used, the server port number will be set from the
357 * DNS SRV records.
358 * @param resolver If this parameter is not NULL, then the \a domain
359 * parameter will be first resolved with DNS SRV and
360 * then fallback to using DNS A/AAAA resolution when
361 * DNS SRV resolution fails. If this parameter is
362 * NULL, the \a domain parameter will be resolved as
363 * hostname.
364 * @param cred The STUN credential to be used for the TURN server.
365 * @param param Optional TURN allocation parameter.
366 *
367 * @return PJ_SUCCESS if the operation has been successfully
368 * queued, or the appropriate error code on failure.
369 * When this function returns PJ_SUCCESS, the final
370 * result of the allocation process will be notified
371 * to application in \a on_state() callback.
372 *
373 */
374PJ_DECL(pj_status_t) pj_turn_sock_alloc(pj_turn_sock *turn_sock,
375 const pj_str_t *domain,
376 int default_port,
377 pj_dns_resolver *resolver,
378 const pj_stun_auth_cred *cred,
379 const pj_turn_alloc_param *param);
380
381/**
382 * Create or renew permission in the TURN server for the specified peer IP
383 * addresses. Application must install permission for a particular (peer)
384 * IP address before it sends any data to that IP address, or otherwise
385 * the TURN server will drop the data.
386 *
387 * @param turn_sock The TURN transport instance.
388 * @param addr_cnt Number of IP addresses.
389 * @param addr Array of peer IP addresses. Only the address family
390 * and IP address portion of the socket address matter.
391 * @param options Specify 1 to let the TURN client session automatically
392 * renew the permission later when they are about to
393 * expire.
394 *
395 * @return PJ_SUCCESS if the operation has been successfully
396 * issued, or the appropriate error code. Note that
397 * the operation itself will complete asynchronously.
398 */
399PJ_DECL(pj_status_t) pj_turn_sock_set_perm(pj_turn_sock *turn_sock,
400 unsigned addr_cnt,
401 const pj_sockaddr addr[],
402 unsigned options);
403
404/**
405 * Send a data to the specified peer address via the TURN relay. This
406 * function will encapsulate the data as STUN Send Indication or TURN
407 * ChannelData packet and send the message to the TURN server. The TURN
408 * server then will send the data to the peer.
409 *
410 * The allocation (pj_turn_sock_alloc()) must have been successfully
411 * created before application can relay any data.
412 *
413 * @param turn_sock The TURN transport instance.
414 * @param pkt The data/packet to be sent to peer.
415 * @param pkt_len Length of the data.
416 * @param peer_addr The remote peer address (the ultimate destination
417 * of the data, and not the TURN server address).
418 * @param addr_len Length of the address.
419 *
420 * @return PJ_SUCCESS if the operation has been successful,
421 * or the appropriate error code on failure.
422 */
423PJ_DECL(pj_status_t) pj_turn_sock_sendto(pj_turn_sock *turn_sock,
424 const pj_uint8_t *pkt,
425 unsigned pkt_len,
426 const pj_sockaddr_t *peer_addr,
427 unsigned addr_len);
428
429/**
430 * Optionally establish channel binding for the specified a peer address.
431 * This function will assign a unique channel number for the peer address
432 * and request channel binding to the TURN server for this address. When
433 * a channel has been bound to a peer, the TURN transport and TURN server
434 * will exchange data using ChannelData encapsulation format, which has
435 * lower bandwidth overhead than Send Indication (the default format used
436 * when peer address is not bound to a channel).
437 *
438 * @param turn_sock The TURN transport instance.
439 * @param peer The remote peer address.
440 * @param addr_len Length of the address.
441 *
442 * @return PJ_SUCCESS if the operation has been successful,
443 * or the appropriate error code on failure.
444 */
445PJ_DECL(pj_status_t) pj_turn_sock_bind_channel(pj_turn_sock *turn_sock,
446 const pj_sockaddr_t *peer,
447 unsigned addr_len);
448
449
450/**
451 * @}
452 */
453
454
455PJ_END_DECL
456
457
458#endif /* __PJNATH_TURN_SOCK_H__ */
459