~ubuntu-branches/ubuntu/maverick/sflphone/maverick

« back to all changes in this revision

Viewing changes to sflphone-common/libs/pjproject/pjnath/include/pjnath/stun_sock.h

  • Committer: Bazaar Package Importer
  • Author(s): Francois Marier
  • Date: 2010-06-03 15:59:46 UTC
  • Revision ID: james.westby@ubuntu.com-20100603155946-ybe8d8o8zx8lp0m8
Tags: upstream-0.9.8.3
ImportĀ upstreamĀ versionĀ 0.9.8.3

Show diffs side-by-side

added added

removed removed

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