~ubuntu-branches/ubuntu/trusty/sflphone/trusty

« back to all changes in this revision

Viewing changes to daemon/libs/pjproject-2.1.0/pjnath/include/pjnath/stun_sock.h

  • Committer: Package Import Robot
  • Author(s): Mark Purcell
  • Date: 2014-01-28 18:23:36 UTC
  • mfrom: (4.3.4 sid)
  • Revision ID: package-import@ubuntu.com-20140128182336-jrsv0k9u6cawc068
Tags: 1.3.0-1
* New upstream release 
  - Fixes "New Upstream Release" (Closes: #735846)
  - Fixes "Ringtone does not stop" (Closes: #727164)
  - Fixes "[sflphone-kde] crash on startup" (Closes: #718178)
  - Fixes "sflphone GUI crashes when call is hung up" (Closes: #736583)
* Build-Depends: ensure GnuTLS 2.6
  - libucommon-dev (>= 6.0.7-1.1), libccrtp-dev (>= 2.0.6-3)
  - Fixes "FTBFS Build-Depends libgnutls{26,28}-dev" (Closes: #722040)
* Fix "boost 1.49 is going away" unversioned Build-Depends: (Closes: #736746)
* Add Build-Depends: libsndfile-dev, nepomuk-core-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: stun_sock.h 4360 2013-02-21 11:26:35Z bennylp $ */
 
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_STUN_SOCK_H__
 
21
#define __PJNATH_STUN_SOCK_H__
 
22
 
 
23
/**
 
24
 * @file stun_sock.h
 
25
 * @brief STUN aware socket transport
 
26
 */
 
27
#include <pjnath/stun_config.h>
 
28
#include <pjlib-util/resolver.h>
 
29
#include <pj/ioqueue.h>
 
30
#include <pj/lock.h>
 
31
#include <pj/sock.h>
 
32
#include <pj/sock_qos.h>
 
33
 
 
34
 
 
35
PJ_BEGIN_DECL
 
36
 
 
37
 
 
38
/**
 
39
 * @addtogroup PJNATH_STUN_SOCK
 
40
 * @{
 
41
 *
 
42
 * The STUN transport provides asynchronous UDP like socket transport
 
43
 * with the additional STUN capability. It has the following features:
 
44
 *
 
45
 *  - API to send and receive UDP packets
 
46
 *
 
47
 *  - multiplex STUN and non-STUN incoming packets and distinguish between
 
48
 *    STUN responses that belong to internal requests with application data
 
49
 *    (the application data may be STUN packets as well)
 
50
 *
 
51
 *  - DNS SRV resolution to the STUN server (if wanted), along with fallback
 
52
 *    to DNS A resolution if SRV record is not found.
 
53
 *
 
54
 *  - STUN keep-alive maintenance, and handle changes to the mapped address
 
55
 *    (when the NAT binding changes)
 
56
 *
 
57
 */
 
58
 
 
59
/**
 
60
 * Opaque type to represent a STUN transport.
 
61
 */
 
62
typedef struct pj_stun_sock pj_stun_sock;
 
63
 
 
64
/**
 
65
 * Types of operation being reported in \a on_status() callback of
 
66
 * pj_stun_sock_cb. Application may retrieve the string representation
 
67
 * of these constants with pj_stun_sock_op_name().
 
68
 */
 
69
typedef enum pj_stun_sock_op
 
70
{
 
71
    /**
 
72
     * Asynchronous DNS resolution.
 
73
     */
 
74
    PJ_STUN_SOCK_DNS_OP         = 1,
 
75
 
 
76
    /**
 
77
     * Initial STUN Binding request.
 
78
     */
 
79
    PJ_STUN_SOCK_BINDING_OP,
 
80
 
 
81
    /**
 
82
     * Subsequent STUN Binding request for keeping the binding
 
83
     * alive.
 
84
     */
 
85
    PJ_STUN_SOCK_KEEP_ALIVE_OP,
 
86
 
 
87
    /**
 
88
     * IP address change notification from the keep-alive operation.
 
89
     */
 
90
    PJ_STUN_SOCK_MAPPED_ADDR_CHANGE
 
91
 
 
92
 
 
93
} pj_stun_sock_op;
 
94
 
 
95
 
 
96
/**
 
97
 * This structure contains callbacks that will be called by the STUN
 
98
 * transport to notify application about various events.
 
99
 */
 
100
typedef struct pj_stun_sock_cb
 
101
{
 
102
    /**
 
103
     * Notification when incoming packet has been received.
 
104
     *
 
105
     * @param stun_sock The STUN transport.
 
106
     * @param data      The packet.
 
107
     * @param data_len  Length of the packet.
 
108
     * @param src_addr  The source address of the packet.
 
109
     * @param addr_len  The length of the source address.
 
110
     *
 
111
     * @return          Application should normally return PJ_TRUE to let
 
112
     *                  the STUN transport continue its operation. However
 
113
     *                  it must return PJ_FALSE if it has destroyed the
 
114
     *                  STUN transport in this callback.
 
115
     */
 
116
    pj_bool_t (*on_rx_data)(pj_stun_sock *stun_sock,
 
117
                            void *pkt,
 
118
                            unsigned pkt_len,
 
119
                            const pj_sockaddr_t *src_addr,
 
120
                            unsigned addr_len);
 
121
 
 
122
    /**
 
123
     * Notifification when asynchronous send operation has completed.
 
124
     *
 
125
     * @param stun_sock The STUN transport.
 
126
     * @param send_key  The send operation key that was given in
 
127
     *                  #pj_stun_sock_sendto().
 
128
     * @param sent      If value is positive non-zero it indicates the
 
129
     *                  number of data sent. When the value is negative,
 
130
     *                  it contains the error code which can be retrieved
 
131
     *                  by negating the value (i.e. status=-sent).
 
132
     *
 
133
     * @return          Application should normally return PJ_TRUE to let
 
134
     *                  the STUN transport continue its operation. However
 
135
     *                  it must return PJ_FALSE if it has destroyed the
 
136
     *                  STUN transport in this callback.
 
137
     */
 
138
    pj_bool_t (*on_data_sent)(pj_stun_sock *stun_sock,
 
139
                              pj_ioqueue_op_key_t *send_key,
 
140
                              pj_ssize_t sent);
 
141
 
 
142
    /**
 
143
     * Notification when the status of the STUN transport has changed. This
 
144
     * callback may be called for the following conditions:
 
145
     *  - the first time the publicly mapped address has been resolved from
 
146
     *    the STUN server, this callback will be called with \a op argument
 
147
     *    set to PJ_STUN_SOCK_BINDING_OP \a status  argument set to 
 
148
     *    PJ_SUCCESS.
 
149
     *  - anytime when the transport has detected that the publicly mapped
 
150
     *    address has changed, this callback will be called with \a op
 
151
     *    argument set to PJ_STUN_SOCK_KEEP_ALIVE_OP and \a status
 
152
     *    argument set to PJ_SUCCESS. On this case and the case above,
 
153
     *    application will get the resolved public address in the
 
154
     *    #pj_stun_sock_info structure.
 
155
     *  - for any terminal error (such as STUN time-out, DNS resolution
 
156
     *    failure, or keep-alive failure), this callback will be called 
 
157
     *    with the \a status argument set to non-PJ_SUCCESS.
 
158
     *
 
159
     * @param stun_sock The STUN transport.
 
160
     * @param op        The operation that triggers the callback.
 
161
     * @param status    The status.
 
162
     *
 
163
     * @return          Must return PJ_FALSE if it has destroyed the
 
164
     *                  STUN transport in this callback. Application should
 
165
     *                  normally destroy the socket and return PJ_FALSE
 
166
     *                  upon encountering terminal error, otherwise it
 
167
     *                  should return PJ_TRUE to let the STUN socket operation
 
168
     *                  continues.
 
169
     */
 
170
    pj_bool_t   (*on_status)(pj_stun_sock *stun_sock, 
 
171
                             pj_stun_sock_op op,
 
172
                             pj_status_t status);
 
173
 
 
174
} pj_stun_sock_cb;
 
175
 
 
176
 
 
177
/**
 
178
 * This structure contains information about the STUN transport. Application
 
179
 * may query this information by calling #pj_stun_sock_get_info().
 
180
 */
 
181
typedef struct pj_stun_sock_info
 
182
{
 
183
    /**
 
184
     * The bound address of the socket.
 
185
     */
 
186
    pj_sockaddr     bound_addr;
 
187
 
 
188
    /**
 
189
     * IP address of the STUN server.
 
190
     */
 
191
    pj_sockaddr     srv_addr;
 
192
 
 
193
    /**
 
194
     * The publicly mapped address. It may contain zero address when the
 
195
     * mapped address has not been resolved. Application may query whether
 
196
     * this field contains valid address with pj_sockaddr_has_addr().
 
197
     */
 
198
    pj_sockaddr     mapped_addr;
 
199
 
 
200
    /**
 
201
     * Number of interface address aliases. The interface address aliases
 
202
     * are list of all interface addresses in this host.
 
203
     */
 
204
    unsigned        alias_cnt;
 
205
 
 
206
    /**
 
207
     * Array of interface address aliases.
 
208
     */
 
209
    pj_sockaddr     aliases[PJ_ICE_ST_MAX_CAND];
 
210
 
 
211
} pj_stun_sock_info;
 
212
 
 
213
 
 
214
/**
 
215
 * This describe the settings to be given to the STUN transport during its
 
216
 * creation. Application should initialize this structure by calling
 
217
 * #pj_stun_sock_cfg_default().
 
218
 */
 
219
typedef struct pj_stun_sock_cfg
 
220
{
 
221
    /**
 
222
     * The group lock to be used by the STUN socket. If NULL, the STUN socket
 
223
     * will create one internally.
 
224
     *
 
225
     * Default: NULL
 
226
     */
 
227
    pj_grp_lock_t *grp_lock;
 
228
 
 
229
    /**
 
230
     * Packet buffer size.
 
231
     *
 
232
     * Default value is PJ_STUN_SOCK_PKT_LEN.
 
233
     */
 
234
    unsigned max_pkt_size;
 
235
 
 
236
    /**
 
237
     * Specify the number of simultaneous asynchronous read operations to
 
238
     * be invoked to the ioqueue. Having more than one read operations will
 
239
     * increase performance on multiprocessor systems since the application
 
240
     * will be able to process more than one incoming packets simultaneously.
 
241
     * Default value is 1.
 
242
     */
 
243
    unsigned async_cnt;
 
244
 
 
245
    /**
 
246
     * Specify the interface where the socket should be bound to. If the
 
247
     * address is zero, socket will be bound to INADDR_ANY. If the address
 
248
     * is non-zero, socket will be bound to this address only, and the
 
249
     * transport will have only one address alias (the \a alias_cnt field
 
250
     * in #pj_stun_sock_info structure. If the port is set to zero, the
 
251
     * socket will bind at any port (chosen by the OS).
 
252
     */
 
253
    pj_sockaddr bound_addr;
 
254
 
 
255
    /**
 
256
     * Specify the port range for STUN socket binding, relative to the start
 
257
     * port number specified in \a bound_addr. Note that this setting is only
 
258
     * applicable when the start port number is non zero.
 
259
     *
 
260
     * Default value is zero.
 
261
     */
 
262
    pj_uint16_t port_range;
 
263
 
 
264
    /**
 
265
     * Specify the STUN keep-alive duration, in seconds. The STUN transport
 
266
     * does keep-alive by sending STUN Binding request to the STUN server. 
 
267
     * If this value is zero, the PJ_STUN_KEEP_ALIVE_SEC value will be used.
 
268
     * If the value is negative, it will disable STUN keep-alive.
 
269
     */
 
270
    int ka_interval;
 
271
 
 
272
    /**
 
273
     * QoS traffic type to be set on this transport. When application wants
 
274
     * to apply QoS tagging to the transport, it's preferable to set this
 
275
     * field rather than \a qos_param fields since this is more portable.
 
276
     *
 
277
     * Default value is PJ_QOS_TYPE_BEST_EFFORT.
 
278
     */
 
279
    pj_qos_type qos_type;
 
280
 
 
281
    /**
 
282
     * Set the low level QoS parameters to the transport. This is a lower
 
283
     * level operation than setting the \a qos_type field and may not be
 
284
     * supported on all platforms.
 
285
     *
 
286
     * By default all settings in this structure are disabled.
 
287
     */
 
288
    pj_qos_params qos_params;
 
289
 
 
290
    /**
 
291
     * Specify if STUN socket should ignore any errors when setting the QoS
 
292
     * traffic type/parameters.
 
293
     *
 
294
     * Default: PJ_TRUE
 
295
     */
 
296
    pj_bool_t qos_ignore_error;
 
297
 
 
298
} pj_stun_sock_cfg;
 
299
 
 
300
 
 
301
 
 
302
/**
 
303
 * Retrieve the name representing the specified operation.
 
304
 */
 
305
PJ_DECL(const char*) pj_stun_sock_op_name(pj_stun_sock_op op);
 
306
 
 
307
 
 
308
/**
 
309
 * Initialize the STUN transport setting with its default values.
 
310
 *
 
311
 * @param cfg   The STUN transport config.
 
312
 */
 
313
PJ_DECL(void) pj_stun_sock_cfg_default(pj_stun_sock_cfg *cfg);
 
314
 
 
315
 
 
316
/**
 
317
 * Create the STUN transport using the specified configuration. Once
 
318
 * the STUN transport has been create, application should call
 
319
 * #pj_stun_sock_start() to start the transport.
 
320
 *
 
321
 * @param stun_cfg      The STUN configuration which contains among other
 
322
 *                      things the ioqueue and timer heap instance for
 
323
 *                      the operation of this transport.
 
324
 * @param af            Address family of socket. Currently pj_AF_INET()
 
325
 *                      and pj_AF_INET6() are supported. 
 
326
 * @param name          Optional name to be given to this transport to
 
327
 *                      assist debugging.
 
328
 * @param cb            Callback to receive events/data from the transport.
 
329
 * @param cfg           Optional transport settings.
 
330
 * @param user_data     Arbitrary application data to be associated with
 
331
 *                      this transport.
 
332
 * @param p_sock        Pointer to receive the created transport instance.
 
333
 *
 
334
 * @return              PJ_SUCCESS if the operation has been successful,
 
335
 *                      or the appropriate error code on failure.
 
336
 */
 
337
PJ_DECL(pj_status_t) pj_stun_sock_create(pj_stun_config *stun_cfg,
 
338
                                         const char *name,
 
339
                                         int af,
 
340
                                         const pj_stun_sock_cb *cb,
 
341
                                         const pj_stun_sock_cfg *cfg,
 
342
                                         void *user_data,
 
343
                                         pj_stun_sock **p_sock);
 
344
 
 
345
 
 
346
/**
 
347
 * Start the STUN transport. This will start the DNS SRV resolution for
 
348
 * the STUN server (if desired), and once the server is resolved, STUN
 
349
 * Binding request will be sent to resolve the publicly mapped address.
 
350
 * Once the initial STUN Binding response is received, the keep-alive
 
351
 * timer will be started.
 
352
 *
 
353
 * @param stun_sock     The STUN transport instance.
 
354
 * @param domain        The domain, hostname, or IP address of the TURN
 
355
 *                      server. When this parameter contains domain name,
 
356
 *                      the \a resolver parameter must be set to activate
 
357
 *                      DNS SRV resolution.
 
358
 * @param default_port  The default STUN port number to use when DNS SRV
 
359
 *                      resolution is not used. If DNS SRV resolution is
 
360
 *                      used, the server port number will be set from the
 
361
 *                      DNS SRV records. The recommended value for this
 
362
 *                      parameter is PJ_STUN_PORT.
 
363
 * @param resolver      If this parameter is not NULL, then the \a domain
 
364
 *                      parameter will be first resolved with DNS SRV and
 
365
 *                      then fallback to using DNS A/AAAA resolution when
 
366
 *                      DNS SRV resolution fails. If this parameter is
 
367
 *                      NULL, the \a domain parameter will be resolved as
 
368
 *                      hostname.
 
369
 *
 
370
 * @return              PJ_SUCCESS if the operation has been successfully
 
371
 *                      queued, or the appropriate error code on failure.
 
372
 *                      When this function returns PJ_SUCCESS, the final
 
373
 *                      result of the allocation process will be notified
 
374
 *                      to application in \a on_state() callback.
 
375
 */
 
376
PJ_DECL(pj_status_t) pj_stun_sock_start(pj_stun_sock *stun_sock,
 
377
                                        const pj_str_t *domain,
 
378
                                        pj_uint16_t default_port,
 
379
                                        pj_dns_resolver *resolver);
 
380
 
 
381
/**
 
382
 * Destroy the STUN transport.
 
383
 *
 
384
 * @param sock          The STUN transport socket.
 
385
 *
 
386
 * @return              PJ_SUCCESS if the operation has been successful,
 
387
 *                      or the appropriate error code on failure.
 
388
 */
 
389
PJ_DECL(pj_status_t) pj_stun_sock_destroy(pj_stun_sock *sock);
 
390
 
 
391
 
 
392
/**
 
393
 * Associate a user data with this STUN transport. The user data may then
 
394
 * be retrieved later with #pj_stun_sock_get_user_data().
 
395
 *
 
396
 * @param stun_sock     The STUN transport instance.
 
397
 * @param user_data     Arbitrary data.
 
398
 *
 
399
 * @return              PJ_SUCCESS if the operation has been successful,
 
400
 *                      or the appropriate error code on failure.
 
401
 */
 
402
PJ_DECL(pj_status_t) pj_stun_sock_set_user_data(pj_stun_sock *stun_sock,
 
403
                                                void *user_data);
 
404
 
 
405
/**
 
406
 * Retrieve the previously assigned user data associated with this STUN
 
407
 * transport.
 
408
 *
 
409
 * @param stun_sock     The STUN transport instance.
 
410
 *
 
411
 * @return              The user/application data.
 
412
 */
 
413
PJ_DECL(void*) pj_stun_sock_get_user_data(pj_stun_sock *stun_sock);
 
414
 
 
415
 
 
416
/**
 
417
 * Get the STUN transport info. The transport info contains, among other
 
418
 * things, the allocated relay address.
 
419
 *
 
420
 * @param stun_sock     The STUN transport instance.
 
421
 * @param info          Pointer to be filled with STUN transport info.
 
422
 *
 
423
 * @return              PJ_SUCCESS if the operation has been successful,
 
424
 *                      or the appropriate error code on failure.
 
425
 */
 
426
PJ_DECL(pj_status_t) pj_stun_sock_get_info(pj_stun_sock *stun_sock,
 
427
                                           pj_stun_sock_info *info);
 
428
 
 
429
 
 
430
/**
 
431
 * Send a data to the specified address. This function may complete
 
432
 * asynchronously and in this case \a on_data_sent() will be called.
 
433
 *
 
434
 * @param stun_sock     The STUN transport instance.
 
435
 * @param send_key      Optional send key for sending the packet down to
 
436
 *                      the ioqueue. This value will be given back to
 
437
 *                      \a on_data_sent() callback
 
438
 * @param pkt           The data/packet to be sent to peer.
 
439
 * @param pkt_len       Length of the data.
 
440
 * @param flag          pj_ioqueue_sendto() flag.
 
441
 * @param dst_addr      The remote address.
 
442
 * @param addr_len      Length of the address.
 
443
 *
 
444
 * @return              PJ_SUCCESS if data has been sent immediately, or
 
445
 *                      PJ_EPENDING if data cannot be sent immediately. In
 
446
 *                      this case the \a on_data_sent() callback will be
 
447
 *                      called when data is actually sent. Any other return
 
448
 *                      value indicates error condition.
 
449
 */ 
 
450
PJ_DECL(pj_status_t) pj_stun_sock_sendto(pj_stun_sock *stun_sock,
 
451
                                         pj_ioqueue_op_key_t *send_key,
 
452
                                         const void *pkt,
 
453
                                         unsigned pkt_len,
 
454
                                         unsigned flag,
 
455
                                         const pj_sockaddr_t *dst_addr,
 
456
                                         unsigned addr_len);
 
457
 
 
458
/**
 
459
 * @}
 
460
 */
 
461
 
 
462
 
 
463
PJ_END_DECL
 
464
 
 
465
 
 
466
#endif  /* __PJNATH_STUN_SOCK_H__ */
 
467