mirror of https://github.com/asterisk/asterisk
This API provides a generic way for multiple RTP stacks to be integrated into Asterisk. Right now there is only one present, res_rtp_asterisk, which is the existing Asterisk RTP stack. Functionality wise this commit performs the same as previously. API documentation can be viewed in the rtp_engine.h header file. Review: http://reviewboard.digium.com/r/209/ git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@186078 65c4cc65-6c06-0410-ace0-fbb531ad65f3certified/1.8.6
parent
08971ce205
commit
63de834395
File diff suppressed because it is too large
Load Diff
@ -1,416 +0,0 @@
|
||||
/*
|
||||
* Asterisk -- An open source telephony toolkit.
|
||||
*
|
||||
* Copyright (C) 1999 - 2006, Digium, Inc.
|
||||
*
|
||||
* Mark Spencer <markster@digium.com>
|
||||
*
|
||||
* See http://www.asterisk.org for more information about
|
||||
* the Asterisk project. Please do not directly contact
|
||||
* any of the maintainers of this project for assistance;
|
||||
* the project provides a web site, mailing lists and IRC
|
||||
* channels for your use.
|
||||
*
|
||||
* This program is free software, distributed under the terms of
|
||||
* the GNU General Public License Version 2. See the LICENSE file
|
||||
* at the top of the source tree.
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \file rtp.h
|
||||
* \brief Supports RTP and RTCP with Symmetric RTP support for NAT traversal.
|
||||
*
|
||||
* RTP is defined in RFC 3550.
|
||||
*/
|
||||
|
||||
#ifndef _ASTERISK_RTP_H
|
||||
#define _ASTERISK_RTP_H
|
||||
|
||||
#include "asterisk/network.h"
|
||||
|
||||
#include "asterisk/frame.h"
|
||||
#include "asterisk/io.h"
|
||||
#include "asterisk/sched.h"
|
||||
#include "asterisk/channel.h"
|
||||
#include "asterisk/linkedlists.h"
|
||||
|
||||
#if defined(__cplusplus) || defined(c_plusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Codes for RTP-specific data - not defined by our AST_FORMAT codes */
|
||||
/*! DTMF (RFC2833) */
|
||||
#define AST_RTP_DTMF (1 << 0)
|
||||
/*! 'Comfort Noise' (RFC3389) */
|
||||
#define AST_RTP_CN (1 << 1)
|
||||
/*! DTMF (Cisco Proprietary) */
|
||||
#define AST_RTP_CISCO_DTMF (1 << 2)
|
||||
/*! Maximum RTP-specific code */
|
||||
#define AST_RTP_MAX AST_RTP_CISCO_DTMF
|
||||
|
||||
/*! Maxmum number of payload defintions for a RTP session */
|
||||
#define MAX_RTP_PT 256
|
||||
|
||||
/*! T.140 Redundancy Maxium number of generations */
|
||||
#define RED_MAX_GENERATION 5
|
||||
|
||||
#define FLAG_3389_WARNING (1 << 0)
|
||||
|
||||
enum ast_rtp_options {
|
||||
AST_RTP_OPT_G726_NONSTANDARD = (1 << 0),
|
||||
};
|
||||
|
||||
enum ast_rtp_get_result {
|
||||
/*! Failed to find the RTP structure */
|
||||
AST_RTP_GET_FAILED = 0,
|
||||
/*! RTP structure exists but true native bridge can not occur so try partial */
|
||||
AST_RTP_TRY_PARTIAL,
|
||||
/*! RTP structure exists and native bridge can occur */
|
||||
AST_RTP_TRY_NATIVE,
|
||||
};
|
||||
|
||||
/*! \brief Variables used in ast_rtcp_get function */
|
||||
enum ast_rtp_qos_vars {
|
||||
AST_RTP_TXCOUNT,
|
||||
AST_RTP_RXCOUNT,
|
||||
AST_RTP_TXJITTER,
|
||||
AST_RTP_RXJITTER,
|
||||
AST_RTP_RXPLOSS,
|
||||
AST_RTP_TXPLOSS,
|
||||
AST_RTP_RTT
|
||||
};
|
||||
|
||||
struct ast_rtp;
|
||||
/*! T.140 Redundancy structure*/
|
||||
struct rtp_red;
|
||||
|
||||
/*! \brief The value of each payload format mapping: */
|
||||
struct rtpPayloadType {
|
||||
int isAstFormat; /*!< whether the following code is an AST_FORMAT */
|
||||
int code;
|
||||
};
|
||||
|
||||
/*! \brief This is the structure that binds a channel (SIP/Jingle/H.323) to the RTP subsystem
|
||||
*/
|
||||
struct ast_rtp_protocol {
|
||||
/*! Get RTP struct, or NULL if unwilling to transfer */
|
||||
enum ast_rtp_get_result (* const get_rtp_info)(struct ast_channel *chan, struct ast_rtp **rtp);
|
||||
/*! Get RTP struct, or NULL if unwilling to transfer */
|
||||
enum ast_rtp_get_result (* const get_vrtp_info)(struct ast_channel *chan, struct ast_rtp **rtp);
|
||||
/*! Get RTP struct, or NULL if unwilling to transfer */
|
||||
enum ast_rtp_get_result (* const get_trtp_info)(struct ast_channel *chan, struct ast_rtp **rtp);
|
||||
/*! Set RTP peer */
|
||||
int (* const set_rtp_peer)(struct ast_channel *chan, struct ast_rtp *peer, struct ast_rtp *vpeer, struct ast_rtp *tpeer, int codecs, int nat_active);
|
||||
int (* const get_codec)(struct ast_channel *chan);
|
||||
const char * const type;
|
||||
AST_LIST_ENTRY(ast_rtp_protocol) list;
|
||||
};
|
||||
|
||||
enum ast_rtp_quality_type {
|
||||
RTPQOS_SUMMARY = 0,
|
||||
RTPQOS_JITTER,
|
||||
RTPQOS_LOSS,
|
||||
RTPQOS_RTT
|
||||
};
|
||||
|
||||
/*! \brief RTCP quality report storage */
|
||||
struct ast_rtp_quality {
|
||||
unsigned int local_ssrc; /*!< Our SSRC */
|
||||
unsigned int local_lostpackets; /*!< Our lost packets */
|
||||
double local_jitter; /*!< Our calculated jitter */
|
||||
unsigned int local_count; /*!< Number of received packets */
|
||||
unsigned int remote_ssrc; /*!< Their SSRC */
|
||||
unsigned int remote_lostpackets; /*!< Their lost packets */
|
||||
double remote_jitter; /*!< Their reported jitter */
|
||||
unsigned int remote_count; /*!< Number of transmitted packets */
|
||||
double rtt; /*!< Round trip time */
|
||||
};
|
||||
|
||||
/*! RTP callback structure */
|
||||
typedef int (*ast_rtp_callback)(struct ast_rtp *rtp, struct ast_frame *f, void *data);
|
||||
|
||||
/*!
|
||||
* \brief Get the amount of space required to hold an RTP session
|
||||
* \return number of bytes required
|
||||
*/
|
||||
size_t ast_rtp_alloc_size(void);
|
||||
|
||||
/*!
|
||||
* \brief Initializate a RTP session.
|
||||
*
|
||||
* \param sched
|
||||
* \param io
|
||||
* \param rtcpenable
|
||||
* \param callbackmode
|
||||
* \return A representation (structure) of an RTP session.
|
||||
*/
|
||||
struct ast_rtp *ast_rtp_new(struct sched_context *sched, struct io_context *io, int rtcpenable, int callbackmode);
|
||||
|
||||
/*!
|
||||
* \brief Initializate a RTP session using an in_addr structure.
|
||||
*
|
||||
* This fuction gets called by ast_rtp_new().
|
||||
*
|
||||
* \param sched
|
||||
* \param io
|
||||
* \param rtcpenable
|
||||
* \param callbackmode
|
||||
* \param in
|
||||
* \return A representation (structure) of an RTP session.
|
||||
*/
|
||||
struct ast_rtp *ast_rtp_new_with_bindaddr(struct sched_context *sched, struct io_context *io, int rtcpenable, int callbackmode, struct in_addr in);
|
||||
|
||||
void ast_rtp_set_peer(struct ast_rtp *rtp, struct sockaddr_in *them);
|
||||
|
||||
/* Copies from rtp to them and returns 1 if there was a change or 0 if it was already the same */
|
||||
int ast_rtp_get_peer(struct ast_rtp *rtp, struct sockaddr_in *them);
|
||||
|
||||
void ast_rtp_get_us(struct ast_rtp *rtp, struct sockaddr_in *us);
|
||||
|
||||
struct ast_rtp *ast_rtp_get_bridged(struct ast_rtp *rtp);
|
||||
|
||||
/*! Destroy RTP session */
|
||||
void ast_rtp_destroy(struct ast_rtp *rtp);
|
||||
|
||||
void ast_rtp_reset(struct ast_rtp *rtp);
|
||||
|
||||
/*! Stop RTP session, do not destroy structure */
|
||||
void ast_rtp_stop(struct ast_rtp *rtp);
|
||||
|
||||
void ast_rtp_set_callback(struct ast_rtp *rtp, ast_rtp_callback callback);
|
||||
|
||||
void ast_rtp_set_data(struct ast_rtp *rtp, void *data);
|
||||
|
||||
int ast_rtp_write(struct ast_rtp *rtp, struct ast_frame *f);
|
||||
|
||||
struct ast_frame *ast_rtp_read(struct ast_rtp *rtp);
|
||||
|
||||
struct ast_frame *ast_rtcp_read(struct ast_rtp *rtp);
|
||||
|
||||
int ast_rtp_fd(struct ast_rtp *rtp);
|
||||
|
||||
int ast_rtcp_fd(struct ast_rtp *rtp);
|
||||
|
||||
int ast_rtp_senddigit_begin(struct ast_rtp *rtp, char digit);
|
||||
|
||||
int ast_rtp_senddigit_end(struct ast_rtp *rtp, char digit);
|
||||
|
||||
int ast_rtp_sendcng(struct ast_rtp *rtp, int level);
|
||||
|
||||
int ast_rtp_setqos(struct ast_rtp *rtp, int tos, int cos, char *desc);
|
||||
|
||||
void ast_rtp_new_source(struct ast_rtp *rtp);
|
||||
|
||||
/*! \brief Setting RTP payload types from lines in a SDP description: */
|
||||
void ast_rtp_pt_clear(struct ast_rtp* rtp);
|
||||
/*! \brief Set payload types to defaults */
|
||||
void ast_rtp_pt_default(struct ast_rtp* rtp);
|
||||
|
||||
/*! \brief Copy payload types between RTP structures */
|
||||
void ast_rtp_pt_copy(struct ast_rtp *dest, struct ast_rtp *src);
|
||||
|
||||
/*! \brief Activate payload type */
|
||||
void ast_rtp_set_m_type(struct ast_rtp* rtp, int pt);
|
||||
|
||||
/*! \brief clear payload type */
|
||||
void ast_rtp_unset_m_type(struct ast_rtp* rtp, int pt);
|
||||
|
||||
/*! \brief Set payload type to a known MIME media type for a codec
|
||||
*
|
||||
* \param rtp RTP structure to modify
|
||||
* \param pt Payload type entry to modify
|
||||
* \param mimeType top-level MIME type of media stream (typically "audio", "video", "text", etc.)
|
||||
* \param mimeSubtype MIME subtype of media stream (typically a codec name)
|
||||
* \param options Zero or more flags from the ast_rtp_options enum
|
||||
*
|
||||
* This function 'fills in' an entry in the list of possible formats for
|
||||
* a media stream associated with an RTP structure.
|
||||
*
|
||||
* \retval 0 on success
|
||||
* \retval -1 if the payload type is out of range
|
||||
* \retval -2 if the mimeType/mimeSubtype combination was not found
|
||||
*/
|
||||
int ast_rtp_set_rtpmap_type(struct ast_rtp* rtp, int pt,
|
||||
char *mimeType, char *mimeSubtype,
|
||||
enum ast_rtp_options options);
|
||||
|
||||
/*! \brief Set payload type to a known MIME media type for a codec with a specific sample rate
|
||||
*
|
||||
* \param rtp RTP structure to modify
|
||||
* \param pt Payload type entry to modify
|
||||
* \param mimeType top-level MIME type of media stream (typically "audio", "video", "text", etc.)
|
||||
* \param mimeSubtype MIME subtype of media stream (typically a codec name)
|
||||
* \param options Zero or more flags from the ast_rtp_options enum
|
||||
* \param sample_rate The sample rate of the media stream
|
||||
*
|
||||
* This function 'fills in' an entry in the list of possible formats for
|
||||
* a media stream associated with an RTP structure.
|
||||
*
|
||||
* \retval 0 on success
|
||||
* \retval -1 if the payload type is out of range
|
||||
* \retval -2 if the mimeType/mimeSubtype combination was not found
|
||||
*/
|
||||
int ast_rtp_set_rtpmap_type_rate(struct ast_rtp* rtp, int pt,
|
||||
char *mimeType, char *mimeSubtype,
|
||||
enum ast_rtp_options options,
|
||||
unsigned int sample_rate);
|
||||
|
||||
/*! \brief Mapping between RTP payload format codes and Asterisk codes: */
|
||||
struct rtpPayloadType ast_rtp_lookup_pt(struct ast_rtp* rtp, int pt);
|
||||
int ast_rtp_lookup_code(struct ast_rtp* rtp, int isAstFormat, int code);
|
||||
|
||||
void ast_rtp_get_current_formats(struct ast_rtp* rtp,
|
||||
int* astFormats, int* nonAstFormats);
|
||||
|
||||
/*! \brief Mapping an Asterisk code into a MIME subtype (string): */
|
||||
const char *ast_rtp_lookup_mime_subtype(int isAstFormat, int code,
|
||||
enum ast_rtp_options options);
|
||||
|
||||
/*! \brief Get the sample rate associated with known RTP payload types
|
||||
*
|
||||
* \param isAstFormat True if the value in the 'code' parameter is an AST_FORMAT value
|
||||
* \param code Format code, either from AST_FORMAT list or from AST_RTP list
|
||||
*
|
||||
* \return the sample rate if the format was found, zero if it was not found
|
||||
*/
|
||||
unsigned int ast_rtp_lookup_sample_rate(int isAstFormat, int code);
|
||||
|
||||
/*! \brief Build a string of MIME subtype names from a capability list */
|
||||
char *ast_rtp_lookup_mime_multiple(char *buf, size_t size, const int capability,
|
||||
const int isAstFormat, enum ast_rtp_options options);
|
||||
|
||||
void ast_rtp_setnat(struct ast_rtp *rtp, int nat);
|
||||
|
||||
int ast_rtp_getnat(struct ast_rtp *rtp);
|
||||
|
||||
/*! \brief Indicate whether this RTP session is carrying DTMF or not */
|
||||
void ast_rtp_setdtmf(struct ast_rtp *rtp, int dtmf);
|
||||
|
||||
/*! \brief Compensate for devices that send RFC2833 packets all at once */
|
||||
void ast_rtp_setdtmfcompensate(struct ast_rtp *rtp, int compensate);
|
||||
|
||||
/*! \brief Enable STUN capability */
|
||||
void ast_rtp_setstun(struct ast_rtp *rtp, int stun_enable);
|
||||
|
||||
/*! \brief Generic STUN request
|
||||
* send a generic stun request to the server specified.
|
||||
* \param s the socket used to send the request
|
||||
* \param dst the address of the STUN server
|
||||
* \param username if non null, add the username in the request
|
||||
* \param answer if non null, the function waits for a response and
|
||||
* puts here the externally visible address.
|
||||
* \return 0 on success, other values on error.
|
||||
* The interface it may change in the future.
|
||||
*/
|
||||
int ast_stun_request(int s, struct sockaddr_in *dst,
|
||||
const char *username, struct sockaddr_in *answer);
|
||||
|
||||
/*! \brief Send STUN request for an RTP socket
|
||||
* Deprecated, this is just a wrapper for ast_rtp_stun_request()
|
||||
*/
|
||||
void ast_rtp_stun_request(struct ast_rtp *rtp, struct sockaddr_in *suggestion, const char *username);
|
||||
|
||||
/*! \brief The RTP bridge.
|
||||
\arg \ref AstRTPbridge
|
||||
*/
|
||||
int ast_rtp_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc, int timeoutms);
|
||||
|
||||
/*! \brief Register an RTP channel client */
|
||||
int ast_rtp_proto_register(struct ast_rtp_protocol *proto);
|
||||
|
||||
/*! \brief Unregister an RTP channel client */
|
||||
void ast_rtp_proto_unregister(struct ast_rtp_protocol *proto);
|
||||
|
||||
int ast_rtp_make_compatible(struct ast_channel *dest, struct ast_channel *src, int media);
|
||||
|
||||
/*! \brief If possible, create an early bridge directly between the devices without
|
||||
having to send a re-invite later */
|
||||
int ast_rtp_early_bridge(struct ast_channel *c0, struct ast_channel *c1);
|
||||
|
||||
/*! \brief Get QOS stats on a RTP channel
|
||||
* \since 1.6.1
|
||||
*/
|
||||
int ast_rtp_get_qos(struct ast_rtp *rtp, const char *qos, char *buf, unsigned int buflen);
|
||||
|
||||
/*! \brief Return RTP and RTCP QoS values
|
||||
* \since 1.6.1
|
||||
*/
|
||||
unsigned int ast_rtp_get_qosvalue(struct ast_rtp *rtp, enum ast_rtp_qos_vars value);
|
||||
|
||||
/*! \brief Set RTPAUDIOQOS(...) variables on a channel when it is being hung up
|
||||
* \since 1.6.1
|
||||
*/
|
||||
void ast_rtp_set_vars(struct ast_channel *chan, struct ast_rtp *rtp);
|
||||
|
||||
/*! \brief Return RTCP quality string
|
||||
*
|
||||
* \param rtp An rtp structure to get qos information about.
|
||||
*
|
||||
* \param qual An (optional) rtp quality structure that will be
|
||||
* filled with the quality information described in
|
||||
* the ast_rtp_quality structure. This structure is
|
||||
* not dependent on any qtype, so a call for any
|
||||
* type of information would yield the same results
|
||||
* because ast_rtp_quality is not a data type
|
||||
* specific to any qos type.
|
||||
*
|
||||
* \param qtype The quality type you'd like, default should be
|
||||
* RTPQOS_SUMMARY which returns basic information
|
||||
* about the call. The return from RTPQOS_SUMMARY
|
||||
* is basically ast_rtp_quality in a string. The
|
||||
* other types are RTPQOS_JITTER, RTPQOS_LOSS and
|
||||
* RTPQOS_RTT which will return more specific
|
||||
* statistics.
|
||||
* \version 1.6.1 added qtype parameter
|
||||
*/
|
||||
char *ast_rtp_get_quality(struct ast_rtp *rtp, struct ast_rtp_quality *qual, enum ast_rtp_quality_type qtype);
|
||||
/*! \brief Send an H.261 fast update request. Some devices need this rather than the XML message in SIP */
|
||||
int ast_rtcp_send_h261fur(void *data);
|
||||
|
||||
void ast_rtp_init(void); /*! Initialize RTP subsystem */
|
||||
int ast_rtp_reload(void); /*! reload rtp configuration */
|
||||
void ast_rtp_new_init(struct ast_rtp *rtp);
|
||||
|
||||
/*! \brief Set codec preference */
|
||||
void ast_rtp_codec_setpref(struct ast_rtp *rtp, struct ast_codec_pref *prefs);
|
||||
|
||||
/*! \brief Get codec preference */
|
||||
struct ast_codec_pref *ast_rtp_codec_getpref(struct ast_rtp *rtp);
|
||||
|
||||
/*! \brief get format from predefined dynamic payload format */
|
||||
int ast_rtp_codec_getformat(int pt);
|
||||
|
||||
/*! \brief Set rtp timeout */
|
||||
void ast_rtp_set_rtptimeout(struct ast_rtp *rtp, int timeout);
|
||||
/*! \brief Set rtp hold timeout */
|
||||
void ast_rtp_set_rtpholdtimeout(struct ast_rtp *rtp, int timeout);
|
||||
/*! \brief set RTP keepalive interval */
|
||||
void ast_rtp_set_rtpkeepalive(struct ast_rtp *rtp, int period);
|
||||
/*! \brief Get RTP keepalive interval */
|
||||
int ast_rtp_get_rtpkeepalive(struct ast_rtp *rtp);
|
||||
/*! \brief Get rtp hold timeout */
|
||||
int ast_rtp_get_rtpholdtimeout(struct ast_rtp *rtp);
|
||||
/*! \brief Get rtp timeout */
|
||||
int ast_rtp_get_rtptimeout(struct ast_rtp *rtp);
|
||||
/* \brief Put RTP timeout timers on hold during another transaction, like T.38 */
|
||||
void ast_rtp_set_rtptimers_onhold(struct ast_rtp *rtp);
|
||||
|
||||
/*! \brief Initalize t.140 redudancy
|
||||
* \param ti time between each t140red frame is sent
|
||||
* \param red_pt payloadtype for RTP packet
|
||||
* \param pt payloadtype numbers for each generation including primary data
|
||||
* \param num_gen number of redundant generations, primary data excluded
|
||||
* \since 1.6.1
|
||||
*/
|
||||
int ast_rtp_red_init(struct ast_rtp *rtp, int ti, int *pt, int num_gen);
|
||||
|
||||
/*! \brief Buffer t.140 data */
|
||||
void ast_red_buffer_t140(struct ast_rtp *rtp, struct ast_frame *f);
|
||||
|
||||
|
||||
|
||||
#if defined(__cplusplus) || defined(c_plusplus)
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _ASTERISK_RTP_H */
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Asterisk -- An open source telephony toolkit.
|
||||
*
|
||||
* Copyright (C) 1999 - 2008, Digium, Inc.
|
||||
*
|
||||
* Mark Spencer <markster@digium.com>
|
||||
*
|
||||
* See http://www.asterisk.org for more information about
|
||||
* the Asterisk project. Please do not directly contact
|
||||
* any of the maintainers of this project for assistance;
|
||||
* the project provides a web site, mailing lists and IRC
|
||||
* channels for your use.
|
||||
*
|
||||
* This program is free software, distributed under the terms of
|
||||
* the GNU General Public License Version 2. See the LICENSE file
|
||||
* at the top of the source tree.
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \file stun.h
|
||||
* \brief STUN support.
|
||||
*
|
||||
* STUN is defined in RFC 3489.
|
||||
*/
|
||||
|
||||
#ifndef _ASTERISK_STUN_H
|
||||
#define _ASTERISK_STUN_H
|
||||
|
||||
#include "asterisk/network.h"
|
||||
|
||||
#if defined(__cplusplus) || defined(c_plusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
enum ast_stun_result {
|
||||
AST_STUN_IGNORE = 0,
|
||||
AST_STUN_ACCEPT,
|
||||
};
|
||||
|
||||
struct stun_attr;
|
||||
|
||||
/*! \brief Generic STUN request
|
||||
* send a generic stun request to the server specified.
|
||||
* \param s the socket used to send the request
|
||||
* \param dst the address of the STUN server
|
||||
* \param username if non null, add the username in the request
|
||||
* \param answer if non null, the function waits for a response and
|
||||
* puts here the externally visible address.
|
||||
* \return 0 on success, other values on error.
|
||||
* The interface it may change in the future.
|
||||
*/
|
||||
int ast_stun_request(int s, struct sockaddr_in *dst, const char *username, struct sockaddr_in *answer);
|
||||
|
||||
/*! \brief callback type to be invoked on stun responses. */
|
||||
typedef int (stun_cb_f)(struct stun_attr *attr, void *arg);
|
||||
|
||||
/*! \brief handle an incoming STUN message.
|
||||
*
|
||||
* Do some basic sanity checks on packet size and content,
|
||||
* try to extract a bit of information, and possibly reply.
|
||||
* At the moment this only processes BIND requests, and returns
|
||||
* the externally visible address of the request.
|
||||
* If a callback is specified, invoke it with the attribute.
|
||||
*/
|
||||
int ast_stun_handle_packet(int s, struct sockaddr_in *src, unsigned char *data, size_t len, stun_cb_f *stun_cb, void *arg);
|
||||
|
||||
#if defined(__cplusplus) || defined(c_plusplus)
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _ASTERISK_STUN_H */
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,475 @@
|
||||
/*
|
||||
* Asterisk -- An open source telephony toolkit.
|
||||
*
|
||||
* Copyright (C) 1999 - 2008, Digium, Inc.
|
||||
*
|
||||
* Mark Spencer <markster@digium.com>
|
||||
*
|
||||
* See http://www.asterisk.org for more information about
|
||||
* the Asterisk project. Please do not directly contact
|
||||
* any of the maintainers of this project for assistance;
|
||||
* the project provides a web site, mailing lists and IRC
|
||||
* channels for your use.
|
||||
*
|
||||
* This program is free software, distributed under the terms of
|
||||
* the GNU General Public License Version 2. See the LICENSE file
|
||||
* at the top of the source tree.
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \file
|
||||
*
|
||||
* \brief STUN Support
|
||||
*
|
||||
* \author Mark Spencer <markster@digium.com>
|
||||
*
|
||||
* \note STUN is defined in RFC 3489.
|
||||
*/
|
||||
|
||||
#include "asterisk.h"
|
||||
|
||||
ASTERISK_FILE_VERSION(__FILE__, "$Revision: 124370 $")
|
||||
|
||||
#include "asterisk/_private.h"
|
||||
#include "asterisk/stun.h"
|
||||
#include "asterisk/cli.h"
|
||||
#include "asterisk/utils.h"
|
||||
#include "asterisk/channel.h"
|
||||
|
||||
static int stundebug; /*!< Are we debugging stun? */
|
||||
|
||||
/*!
|
||||
* \brief STUN support code
|
||||
*
|
||||
* This code provides some support for doing STUN transactions.
|
||||
* Eventually it should be moved elsewhere as other protocols
|
||||
* than RTP can benefit from it - e.g. SIP.
|
||||
* STUN is described in RFC3489 and it is based on the exchange
|
||||
* of UDP packets between a client and one or more servers to
|
||||
* determine the externally visible address (and port) of the client
|
||||
* once it has gone through the NAT boxes that connect it to the
|
||||
* outside.
|
||||
* The simplest request packet is just the header defined in
|
||||
* struct stun_header, and from the response we may just look at
|
||||
* one attribute, STUN_MAPPED_ADDRESS, that we find in the response.
|
||||
* By doing more transactions with different server addresses we
|
||||
* may determine more about the behaviour of the NAT boxes, of
|
||||
* course - the details are in the RFC.
|
||||
*
|
||||
* All STUN packets start with a simple header made of a type,
|
||||
* length (excluding the header) and a 16-byte random transaction id.
|
||||
* Following the header we may have zero or more attributes, each
|
||||
* structured as a type, length and a value (whose format depends
|
||||
* on the type, but often contains addresses).
|
||||
* Of course all fields are in network format.
|
||||
*/
|
||||
|
||||
typedef struct { unsigned int id[4]; } __attribute__((packed)) stun_trans_id;
|
||||
|
||||
struct stun_header {
|
||||
unsigned short msgtype;
|
||||
unsigned short msglen;
|
||||
stun_trans_id id;
|
||||
unsigned char ies[0];
|
||||
} __attribute__((packed));
|
||||
|
||||
struct stun_attr {
|
||||
unsigned short attr;
|
||||
unsigned short len;
|
||||
unsigned char value[0];
|
||||
} __attribute__((packed));
|
||||
|
||||
/*
|
||||
* The format normally used for addresses carried by STUN messages.
|
||||
*/
|
||||
struct stun_addr {
|
||||
unsigned char unused;
|
||||
unsigned char family;
|
||||
unsigned short port;
|
||||
unsigned int addr;
|
||||
} __attribute__((packed));
|
||||
|
||||
/*! \brief STUN message types
|
||||
* 'BIND' refers to transactions used to determine the externally
|
||||
* visible addresses. 'SEC' refers to transactions used to establish
|
||||
* a session key for subsequent requests.
|
||||
* 'SEC' functionality is not supported here.
|
||||
*/
|
||||
|
||||
#define STUN_BINDREQ 0x0001
|
||||
#define STUN_BINDRESP 0x0101
|
||||
#define STUN_BINDERR 0x0111
|
||||
#define STUN_SECREQ 0x0002
|
||||
#define STUN_SECRESP 0x0102
|
||||
#define STUN_SECERR 0x0112
|
||||
|
||||
/*! \brief Basic attribute types in stun messages.
|
||||
* Messages can also contain custom attributes (codes above 0x7fff)
|
||||
*/
|
||||
#define STUN_MAPPED_ADDRESS 0x0001
|
||||
#define STUN_RESPONSE_ADDRESS 0x0002
|
||||
#define STUN_CHANGE_REQUEST 0x0003
|
||||
#define STUN_SOURCE_ADDRESS 0x0004
|
||||
#define STUN_CHANGED_ADDRESS 0x0005
|
||||
#define STUN_USERNAME 0x0006
|
||||
#define STUN_PASSWORD 0x0007
|
||||
#define STUN_MESSAGE_INTEGRITY 0x0008
|
||||
#define STUN_ERROR_CODE 0x0009
|
||||
#define STUN_UNKNOWN_ATTRIBUTES 0x000a
|
||||
#define STUN_REFLECTED_FROM 0x000b
|
||||
|
||||
/*! \brief helper function to print message names */
|
||||
static const char *stun_msg2str(int msg)
|
||||
{
|
||||
switch (msg) {
|
||||
case STUN_BINDREQ:
|
||||
return "Binding Request";
|
||||
case STUN_BINDRESP:
|
||||
return "Binding Response";
|
||||
case STUN_BINDERR:
|
||||
return "Binding Error Response";
|
||||
case STUN_SECREQ:
|
||||
return "Shared Secret Request";
|
||||
case STUN_SECRESP:
|
||||
return "Shared Secret Response";
|
||||
case STUN_SECERR:
|
||||
return "Shared Secret Error Response";
|
||||
}
|
||||
return "Non-RFC3489 Message";
|
||||
}
|
||||
|
||||
/*! \brief helper function to print attribute names */
|
||||
static const char *stun_attr2str(int msg)
|
||||
{
|
||||
switch (msg) {
|
||||
case STUN_MAPPED_ADDRESS:
|
||||
return "Mapped Address";
|
||||
case STUN_RESPONSE_ADDRESS:
|
||||
return "Response Address";
|
||||
case STUN_CHANGE_REQUEST:
|
||||
return "Change Request";
|
||||
case STUN_SOURCE_ADDRESS:
|
||||
return "Source Address";
|
||||
case STUN_CHANGED_ADDRESS:
|
||||
return "Changed Address";
|
||||
case STUN_USERNAME:
|
||||
return "Username";
|
||||
case STUN_PASSWORD:
|
||||
return "Password";
|
||||
case STUN_MESSAGE_INTEGRITY:
|
||||
return "Message Integrity";
|
||||
case STUN_ERROR_CODE:
|
||||
return "Error Code";
|
||||
case STUN_UNKNOWN_ATTRIBUTES:
|
||||
return "Unknown Attributes";
|
||||
case STUN_REFLECTED_FROM:
|
||||
return "Reflected From";
|
||||
}
|
||||
return "Non-RFC3489 Attribute";
|
||||
}
|
||||
|
||||
/*! \brief here we store credentials extracted from a message */
|
||||
struct stun_state {
|
||||
const char *username;
|
||||
const char *password;
|
||||
};
|
||||
|
||||
static int stun_process_attr(struct stun_state *state, struct stun_attr *attr)
|
||||
{
|
||||
if (stundebug)
|
||||
ast_verbose("Found STUN Attribute %s (%04x), length %d\n",
|
||||
stun_attr2str(ntohs(attr->attr)), ntohs(attr->attr), ntohs(attr->len));
|
||||
switch (ntohs(attr->attr)) {
|
||||
case STUN_USERNAME:
|
||||
state->username = (const char *) (attr->value);
|
||||
break;
|
||||
case STUN_PASSWORD:
|
||||
state->password = (const char *) (attr->value);
|
||||
break;
|
||||
default:
|
||||
if (stundebug)
|
||||
ast_verbose("Ignoring STUN attribute %s (%04x), length %d\n",
|
||||
stun_attr2str(ntohs(attr->attr)), ntohs(attr->attr), ntohs(attr->len));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*! \brief append a string to an STUN message */
|
||||
static void append_attr_string(struct stun_attr **attr, int attrval, const char *s, int *len, int *left)
|
||||
{
|
||||
int size = sizeof(**attr) + strlen(s);
|
||||
if (*left > size) {
|
||||
(*attr)->attr = htons(attrval);
|
||||
(*attr)->len = htons(strlen(s));
|
||||
memcpy((*attr)->value, s, strlen(s));
|
||||
(*attr) = (struct stun_attr *)((*attr)->value + strlen(s));
|
||||
*len += size;
|
||||
*left -= size;
|
||||
}
|
||||
}
|
||||
|
||||
/*! \brief append an address to an STUN message */
|
||||
static void append_attr_address(struct stun_attr **attr, int attrval, struct sockaddr_in *sin, int *len, int *left)
|
||||
{
|
||||
int size = sizeof(**attr) + 8;
|
||||
struct stun_addr *addr;
|
||||
if (*left > size) {
|
||||
(*attr)->attr = htons(attrval);
|
||||
(*attr)->len = htons(8);
|
||||
addr = (struct stun_addr *)((*attr)->value);
|
||||
addr->unused = 0;
|
||||
addr->family = 0x01;
|
||||
addr->port = sin->sin_port;
|
||||
addr->addr = sin->sin_addr.s_addr;
|
||||
(*attr) = (struct stun_attr *)((*attr)->value + 8);
|
||||
*len += size;
|
||||
*left -= size;
|
||||
}
|
||||
}
|
||||
|
||||
/*! \brief wrapper to send an STUN message */
|
||||
static int stun_send(int s, struct sockaddr_in *dst, struct stun_header *resp)
|
||||
{
|
||||
return sendto(s, resp, ntohs(resp->msglen) + sizeof(*resp), 0,
|
||||
(struct sockaddr *)dst, sizeof(*dst));
|
||||
}
|
||||
|
||||
/*! \brief helper function to generate a random request id */
|
||||
static void stun_req_id(struct stun_header *req)
|
||||
{
|
||||
int x;
|
||||
for (x = 0; x < 4; x++)
|
||||
req->id.id[x] = ast_random();
|
||||
}
|
||||
|
||||
/*! \brief handle an incoming STUN message.
|
||||
*
|
||||
* Do some basic sanity checks on packet size and content,
|
||||
* try to extract a bit of information, and possibly reply.
|
||||
* At the moment this only processes BIND requests, and returns
|
||||
* the externally visible address of the request.
|
||||
* If a callback is specified, invoke it with the attribute.
|
||||
*/
|
||||
int ast_stun_handle_packet(int s, struct sockaddr_in *src, unsigned char *data, size_t len, stun_cb_f *stun_cb, void *arg)
|
||||
{
|
||||
struct stun_header *hdr = (struct stun_header *)data;
|
||||
struct stun_attr *attr;
|
||||
struct stun_state st;
|
||||
int ret = AST_STUN_IGNORE;
|
||||
int x;
|
||||
|
||||
/* On entry, 'len' is the length of the udp payload. After the
|
||||
* initial checks it becomes the size of unprocessed options,
|
||||
* while 'data' is advanced accordingly.
|
||||
*/
|
||||
if (len < sizeof(struct stun_header)) {
|
||||
ast_debug(1, "Runt STUN packet (only %d, wanting at least %d)\n", (int) len, (int) sizeof(struct stun_header));
|
||||
return -1;
|
||||
}
|
||||
len -= sizeof(struct stun_header);
|
||||
data += sizeof(struct stun_header);
|
||||
x = ntohs(hdr->msglen); /* len as advertised in the message */
|
||||
if (stundebug)
|
||||
ast_verbose("STUN Packet, msg %s (%04x), length: %d\n", stun_msg2str(ntohs(hdr->msgtype)), ntohs(hdr->msgtype), x);
|
||||
if (x > len) {
|
||||
ast_debug(1, "Scrambled STUN packet length (got %d, expecting %d)\n", x, (int)len);
|
||||
} else
|
||||
len = x;
|
||||
memset(&st, 0, sizeof(st));
|
||||
while (len) {
|
||||
if (len < sizeof(struct stun_attr)) {
|
||||
ast_debug(1, "Runt Attribute (got %d, expecting %d)\n", (int)len, (int) sizeof(struct stun_attr));
|
||||
break;
|
||||
}
|
||||
attr = (struct stun_attr *)data;
|
||||
/* compute total attribute length */
|
||||
x = ntohs(attr->len) + sizeof(struct stun_attr);
|
||||
if (x > len) {
|
||||
ast_debug(1, "Inconsistent Attribute (length %d exceeds remaining msg len %d)\n", x, (int)len);
|
||||
break;
|
||||
}
|
||||
if (stun_cb)
|
||||
stun_cb(attr, arg);
|
||||
if (stun_process_attr(&st, attr)) {
|
||||
ast_debug(1, "Failed to handle attribute %s (%04x)\n", stun_attr2str(ntohs(attr->attr)), ntohs(attr->attr));
|
||||
break;
|
||||
}
|
||||
/* Clear attribute id: in case previous entry was a string,
|
||||
* this will act as the terminator for the string.
|
||||
*/
|
||||
attr->attr = 0;
|
||||
data += x;
|
||||
len -= x;
|
||||
}
|
||||
/* Null terminate any string.
|
||||
* XXX NOTE, we write past the size of the buffer passed by the
|
||||
* caller, so this is potentially dangerous. The only thing that
|
||||
* saves us is that usually we read the incoming message in a
|
||||
* much larger buffer in the struct ast_rtp
|
||||
*/
|
||||
*data = '\0';
|
||||
|
||||
/* Now prepare to generate a reply, which at the moment is done
|
||||
* only for properly formed (len == 0) STUN_BINDREQ messages.
|
||||
*/
|
||||
if (len == 0) {
|
||||
unsigned char respdata[1024];
|
||||
struct stun_header *resp = (struct stun_header *)respdata;
|
||||
int resplen = 0; /* len excluding header */
|
||||
int respleft = sizeof(respdata) - sizeof(struct stun_header);
|
||||
|
||||
resp->id = hdr->id;
|
||||
resp->msgtype = 0;
|
||||
resp->msglen = 0;
|
||||
attr = (struct stun_attr *)resp->ies;
|
||||
switch (ntohs(hdr->msgtype)) {
|
||||
case STUN_BINDREQ:
|
||||
if (stundebug)
|
||||
ast_verbose("STUN Bind Request, username: %s\n",
|
||||
st.username ? st.username : "<none>");
|
||||
if (st.username)
|
||||
append_attr_string(&attr, STUN_USERNAME, st.username, &resplen, &respleft);
|
||||
append_attr_address(&attr, STUN_MAPPED_ADDRESS, src, &resplen, &respleft);
|
||||
resp->msglen = htons(resplen);
|
||||
resp->msgtype = htons(STUN_BINDRESP);
|
||||
stun_send(s, src, resp);
|
||||
ret = AST_STUN_ACCEPT;
|
||||
break;
|
||||
default:
|
||||
if (stundebug)
|
||||
ast_verbose("Dunno what to do with STUN message %04x (%s)\n", ntohs(hdr->msgtype), stun_msg2str(ntohs(hdr->msgtype)));
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*! \brief Extract the STUN_MAPPED_ADDRESS from the stun response.
|
||||
* This is used as a callback for stun_handle_response
|
||||
* when called from ast_stun_request.
|
||||
*/
|
||||
static int stun_get_mapped(struct stun_attr *attr, void *arg)
|
||||
{
|
||||
struct stun_addr *addr = (struct stun_addr *)(attr + 1);
|
||||
struct sockaddr_in *sa = (struct sockaddr_in *)arg;
|
||||
|
||||
if (ntohs(attr->attr) != STUN_MAPPED_ADDRESS || ntohs(attr->len) != 8)
|
||||
return 1; /* not us. */
|
||||
sa->sin_port = addr->port;
|
||||
sa->sin_addr.s_addr = addr->addr;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*! \brief Generic STUN request
|
||||
* Send a generic stun request to the server specified,
|
||||
* possibly waiting for a reply and filling the 'reply' field with
|
||||
* the externally visible address. Note that in this case the request
|
||||
* will be blocking.
|
||||
* (Note, the interface may change slightly in the future).
|
||||
*
|
||||
* \param s the socket used to send the request
|
||||
* \param dst the address of the STUN server
|
||||
* \param username if non null, add the username in the request
|
||||
* \param answer if non null, the function waits for a response and
|
||||
* puts here the externally visible address.
|
||||
* \return 0 on success, other values on error.
|
||||
*/
|
||||
int ast_stun_request(int s, struct sockaddr_in *dst,
|
||||
const char *username, struct sockaddr_in *answer)
|
||||
{
|
||||
struct stun_header *req;
|
||||
unsigned char reqdata[1024];
|
||||
int reqlen, reqleft;
|
||||
struct stun_attr *attr;
|
||||
int res = 0;
|
||||
int retry;
|
||||
|
||||
req = (struct stun_header *)reqdata;
|
||||
stun_req_id(req);
|
||||
reqlen = 0;
|
||||
reqleft = sizeof(reqdata) - sizeof(struct stun_header);
|
||||
req->msgtype = 0;
|
||||
req->msglen = 0;
|
||||
attr = (struct stun_attr *)req->ies;
|
||||
if (username)
|
||||
append_attr_string(&attr, STUN_USERNAME, username, &reqlen, &reqleft);
|
||||
req->msglen = htons(reqlen);
|
||||
req->msgtype = htons(STUN_BINDREQ);
|
||||
for (retry = 0; retry < 3; retry++) { /* XXX make retries configurable */
|
||||
/* send request, possibly wait for reply */
|
||||
unsigned char reply_buf[1024];
|
||||
fd_set rfds;
|
||||
struct timeval to = { 3, 0 }; /* timeout, make it configurable */
|
||||
struct sockaddr_in src;
|
||||
socklen_t srclen;
|
||||
|
||||
res = stun_send(s, dst, req);
|
||||
if (res < 0) {
|
||||
ast_log(LOG_WARNING, "ast_stun_request send #%d failed error %d, retry\n",
|
||||
retry, res);
|
||||
continue;
|
||||
}
|
||||
if (answer == NULL)
|
||||
break;
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(s, &rfds);
|
||||
res = ast_select(s + 1, &rfds, NULL, NULL, &to);
|
||||
if (res <= 0) /* timeout or error */
|
||||
continue;
|
||||
memset(&src, 0, sizeof(src));
|
||||
srclen = sizeof(src);
|
||||
/* XXX pass -1 in the size, because stun_handle_packet might
|
||||
* write past the end of the buffer.
|
||||
*/
|
||||
res = recvfrom(s, reply_buf, sizeof(reply_buf) - 1,
|
||||
0, (struct sockaddr *)&src, &srclen);
|
||||
if (res < 0) {
|
||||
ast_log(LOG_WARNING, "ast_stun_request recvfrom #%d failed error %d, retry\n",
|
||||
retry, res);
|
||||
continue;
|
||||
}
|
||||
memset(answer, 0, sizeof(struct sockaddr_in));
|
||||
ast_stun_handle_packet(s, &src, reply_buf, res,
|
||||
stun_get_mapped, answer);
|
||||
res = 0; /* signal regular exit */
|
||||
break;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
static char *handle_cli_stun_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
|
||||
{
|
||||
switch (cmd) {
|
||||
case CLI_INIT:
|
||||
e->command = "stun set debug {on|off}";
|
||||
e->usage =
|
||||
"Usage: stun set debug {on|off}\n"
|
||||
" Enable/Disable STUN (Simple Traversal of UDP through NATs)\n"
|
||||
" debugging\n";
|
||||
return NULL;
|
||||
case CLI_GENERATE:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (a->argc != e->args)
|
||||
return CLI_SHOWUSAGE;
|
||||
|
||||
if (!strncasecmp(a->argv[e->args-1], "on", 2))
|
||||
stundebug = 1;
|
||||
else if (!strncasecmp(a->argv[e->args-1], "off", 3))
|
||||
stundebug = 0;
|
||||
else
|
||||
return CLI_SHOWUSAGE;
|
||||
|
||||
ast_cli(a->fd, "STUN Debugging %s\n", stundebug ? "Enabled" : "Disabled");
|
||||
return CLI_SUCCESS;
|
||||
}
|
||||
|
||||
static struct ast_cli_entry cli_stun[] = {
|
||||
AST_CLI_DEFINE(handle_cli_stun_set_debug, "Enable/Disable STUN debugging"),
|
||||
};
|
||||
|
||||
/*! \brief Initialize the STUN system in Asterisk */
|
||||
void ast_stun_init(void)
|
||||
{
|
||||
ast_cli_register_multiple(cli_stun, sizeof(cli_stun) / sizeof(struct ast_cli_entry));
|
||||
}
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue