Asterisk - The Open Source Telephony Project
18.5.0
|
STUN Support. More...
#include "asterisk.h"
#include "asterisk/logger.h"
#include "asterisk/logger_category.h"
#include "asterisk/_private.h"
#include "asterisk/stun.h"
#include "asterisk/cli.h"
#include "asterisk/utils.h"
#include "asterisk/channel.h"
Go to the source code of this file.
Data Structures | |
struct | stun_addr |
struct | stun_attr |
struct | stun_header |
struct | stun_state |
here we store credentials extracted from a message More... | |
struct | stun_trans_id |
STUN support code. More... | |
Macros | |
#define | STUN_BINDERR 0x0111 |
#define | STUN_BINDREQ 0x0001 |
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. More... | |
#define | STUN_BINDRESP 0x0101 |
#define | STUN_CHANGE_REQUEST 0x0003 |
#define | STUN_CHANGED_ADDRESS 0x0005 |
#define | STUN_ERROR_CODE 0x0009 |
#define | STUN_MAPPED_ADDRESS 0x0001 |
Basic attribute types in stun messages. Messages can also contain custom attributes (codes above 0x7fff) More... | |
#define | STUN_MESSAGE_INTEGRITY 0x0008 |
#define | STUN_PASSWORD 0x0007 |
#define | STUN_REFLECTED_FROM 0x000b |
#define | STUN_RESPONSE_ADDRESS 0x0002 |
#define | STUN_SECERR 0x0112 |
#define | STUN_SECREQ 0x0002 |
#define | STUN_SECRESP 0x0102 |
#define | STUN_SOURCE_ADDRESS 0x0004 |
#define | STUN_UNKNOWN_ATTRIBUTES 0x000a |
#define | STUN_USERNAME 0x0006 |
Functions | |
static void | append_attr_address (struct stun_attr **attr, int attrval, struct sockaddr_in *sin, int *len, int *left) |
append an address to an STUN message More... | |
static void | append_attr_string (struct stun_attr **attr, int attrval, const char *s, int *len, int *left) |
append a string to an STUN message More... | |
uintmax_t | ast_debug_category_stun_id (void) |
uintmax_t | ast_debug_category_stun_packet_id (void) |
int | ast_stun_handle_packet (int s, struct sockaddr_in *src, unsigned char *data, size_t len, stun_cb_f *stun_cb, void *arg) |
handle an incoming STUN message. More... | |
void | ast_stun_init (void) |
Initialize the STUN system in Asterisk. More... | |
int | ast_stun_request (int s, struct sockaddr_in *dst, const char *username, struct sockaddr_in *answer) |
Generic STUN request. More... | |
static char * | handle_cli_stun_set_debug (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
static const char * | stun_attr2str (int msg) |
helper function to print attribute names More... | |
static int | stun_get_mapped (struct stun_attr *attr, void *arg) |
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. More... | |
static int | stun_id_cmp (stun_trans_id *left, stun_trans_id *right) |
static const char * | stun_msg2str (int msg) |
helper function to print message names More... | |
static int | stun_process_attr (struct stun_state *state, struct stun_attr *attr) |
static void | stun_req_id (struct stun_header *req) |
helper function to generate a random request id More... | |
static int | stun_send (int s, struct sockaddr_in *dst, struct stun_header *resp) |
wrapper to send an STUN message More... | |
static void | stun_shutdown (void) |
Variables | |
static struct ast_cli_entry | cli_stun [] |
static uintmax_t | debug_category_stun_id |
static uintmax_t | debug_category_stun_packet_id |
#define STUN_BINDERR 0x0111 |
Definition at line 103 of file stun.c.
Referenced by ast_stun_request(), and stun_msg2str().
#define STUN_BINDREQ 0x0001 |
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.
Definition at line 101 of file stun.c.
Referenced by ast_stun_handle_packet(), ast_stun_request(), and stun_msg2str().
#define STUN_BINDRESP 0x0101 |
Definition at line 102 of file stun.c.
Referenced by ast_stun_handle_packet(), ast_stun_request(), and stun_msg2str().
#define STUN_CHANGE_REQUEST 0x0003 |
Definition at line 113 of file stun.c.
Referenced by stun_attr2str().
#define STUN_CHANGED_ADDRESS 0x0005 |
Definition at line 115 of file stun.c.
Referenced by stun_attr2str().
#define STUN_ERROR_CODE 0x0009 |
Definition at line 119 of file stun.c.
Referenced by stun_attr2str().
#define STUN_MAPPED_ADDRESS 0x0001 |
Basic attribute types in stun messages. Messages can also contain custom attributes (codes above 0x7fff)
Definition at line 111 of file stun.c.
Referenced by ast_stun_handle_packet(), stun_attr2str(), and stun_get_mapped().
#define STUN_MESSAGE_INTEGRITY 0x0008 |
Definition at line 118 of file stun.c.
Referenced by stun_attr2str().
#define STUN_PASSWORD 0x0007 |
Definition at line 117 of file stun.c.
Referenced by stun_attr2str(), and stun_process_attr().
#define STUN_REFLECTED_FROM 0x000b |
Definition at line 121 of file stun.c.
Referenced by stun_attr2str().
#define STUN_RESPONSE_ADDRESS 0x0002 |
Definition at line 112 of file stun.c.
Referenced by stun_attr2str().
#define STUN_SECERR 0x0112 |
Definition at line 106 of file stun.c.
Referenced by stun_msg2str().
#define STUN_SECREQ 0x0002 |
Definition at line 104 of file stun.c.
Referenced by stun_msg2str().
#define STUN_SECRESP 0x0102 |
Definition at line 105 of file stun.c.
Referenced by stun_msg2str().
#define STUN_SOURCE_ADDRESS 0x0004 |
Definition at line 114 of file stun.c.
Referenced by stun_attr2str().
#define STUN_UNKNOWN_ATTRIBUTES 0x000a |
Definition at line 120 of file stun.c.
Referenced by stun_attr2str().
#define STUN_USERNAME 0x0006 |
Definition at line 116 of file stun.c.
Referenced by ast_stun_handle_packet(), ast_stun_request(), stun_attr2str(), and stun_process_attr().
|
static |
append an address to an STUN message
Definition at line 220 of file stun.c.
References stun_addr::addr, stun_addr::family, stun_addr::port, and stun_addr::unused.
Referenced by ast_stun_handle_packet().
|
static |
append a string to an STUN message
Definition at line 203 of file stun.c.
Referenced by ast_stun_handle_packet(), and ast_stun_request().
uintmax_t ast_debug_category_stun_id | ( | void | ) |
uintmax_t ast_debug_category_stun_packet_id | ( | void | ) |
int ast_stun_handle_packet | ( | int | s, |
struct sockaddr_in * | src, | ||
unsigned char * | data, | ||
size_t | len, | ||
stun_cb_f * | stun_cb, | ||
void * | arg | ||
) |
handle an incoming STUN message.
s | Socket to send any response to. |
src | Address where packet came from. |
data | STUN packet buffer to process. |
len | Length of packet |
stun_cb | If not NULL, callback for each STUN attribute. |
arg | Arg to pass to callback. |
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.
AST_STUN_ACCEPT | if responed to a STUN request |
AST_STUN_IGNORE | |
-1 | on error |
Definition at line 268 of file stun.c.
References append_attr_address(), append_attr_string(), ast_debug_stun, ast_debug_stun_packet_is_allowed, AST_STUN_ACCEPT, AST_STUN_IGNORE, ast_stun_request(), ast_verbose(), stun_attr::attr, stun_header::id, stun_header::ies, stun_attr::len, stun_header::msglen, stun_header::msgtype, NULL, stun_attr2str(), STUN_BINDREQ, STUN_BINDRESP, STUN_MAPPED_ADDRESS, stun_msg2str(), stun_process_attr(), stun_send(), STUN_USERNAME, and stun_state::username.
Referenced by ast_rtcp_read(), ast_rtp_read(), and ast_stun_request().
void ast_stun_init | ( | void | ) |
Initialize the STUN system in Asterisk.
Provided by stun.c
Definition at line 543 of file stun.c.
References ast_cli_register_multiple, ast_debug_category_register(), AST_LOG_CATEGORY_STUN, AST_LOG_CATEGORY_STUN_PACKET, ast_register_cleanup(), and stun_shutdown().
Referenced by asterisk_daemon().
int ast_stun_request | ( | int | s, |
struct sockaddr_in * | dst, | ||
const char * | username, | ||
struct sockaddr_in * | answer | ||
) |
Generic STUN request.
s | The socket used to send the request. |
dst | If non null, the address of the STUN server. Only needed if the socket is not bound or connected. |
username | If non null, add the username in the request. |
answer | If non null, the function waits for a response and puts here the externally visible address. |
Send a generic STUN request to the server specified, possibly waiting for a reply and filling the answer parameter with the externally visible address. Note that in this case the request will be blocking.
0 | on success. |
<0 | on error. |
>0 | on timeout. |
Definition at line 384 of file stun.c.
References append_attr_string(), ast_debug_stun, ast_poll, ast_remaining_ms(), ast_stun_handle_packet(), ast_tvnow(), stun_attr::attr, errno, stun_header::id, stun_header::ies, if(), stun_header::msglen, stun_header::msgtype, STUN_BINDERR, STUN_BINDREQ, STUN_BINDRESP, stun_get_mapped(), stun_id_cmp(), stun_req_id(), stun_send(), and STUN_USERNAME.
Referenced by ast_rtp_stun_request(), ast_stun_handle_packet(), rtp_add_candidates_to_ice(), and stun_monitor_request().
|
static |
Definition at line 488 of file stun.c.
References ast_cli_args::argc, ast_cli_entry::args, ast_cli_args::argv, ast_cli(), ast_debug_category_set_sublevel(), ast_debug_stun_packet_is_allowed, AST_LOG_CATEGORY_DISABLED, AST_LOG_CATEGORY_ENABLED, AST_LOG_CATEGORY_STUN_PACKET, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_cli_args::fd, NULL, and ast_cli_entry::usage.
|
static |
helper function to print attribute names
Definition at line 144 of file stun.c.
References STUN_CHANGE_REQUEST, STUN_CHANGED_ADDRESS, STUN_ERROR_CODE, STUN_MAPPED_ADDRESS, STUN_MESSAGE_INTEGRITY, STUN_PASSWORD, STUN_REFLECTED_FROM, STUN_RESPONSE_ADDRESS, STUN_SOURCE_ADDRESS, STUN_UNKNOWN_ATTRIBUTES, and STUN_USERNAME.
Referenced by ast_stun_handle_packet(), and stun_process_attr().
|
static |
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.
Definition at line 372 of file stun.c.
References stun_addr::addr, stun_attr::attr, stun_attr::len, stun_addr::port, and STUN_MAPPED_ADDRESS.
Referenced by ast_stun_request().
|
static |
Definition at line 255 of file stun.c.
Referenced by ast_stun_request().
|
static |
helper function to print message names
Definition at line 124 of file stun.c.
References STUN_BINDERR, STUN_BINDREQ, STUN_BINDRESP, STUN_SECERR, STUN_SECREQ, and STUN_SECRESP.
Referenced by ast_stun_handle_packet().
|
static |
Definition at line 179 of file stun.c.
References ast_debug_stun_packet_is_allowed, ast_verbose(), stun_attr::attr, stun_attr::len, stun_state::password, stun_attr2str(), STUN_PASSWORD, STUN_USERNAME, stun_state::username, and stun_attr::value.
Referenced by ast_stun_handle_packet().
|
static |
helper function to generate a random request id
Definition at line 261 of file stun.c.
References ast_random(), stun_trans_id::id, and stun_header::id.
Referenced by ast_stun_request().
|
static |
wrapper to send an STUN message
Definition at line 239 of file stun.c.
References stun_header::msglen.
Referenced by ast_stun_handle_packet(), and ast_stun_request().
|
static |
Definition at line 534 of file stun.c.
References ast_cli_unregister_multiple(), ast_debug_category_unregister(), AST_LOG_CATEGORY_STUN, and AST_LOG_CATEGORY_STUN_PACKET.
Referenced by ast_stun_init().
|
static |
|
static |
Definition at line 520 of file stun.c.
Referenced by ast_debug_category_stun_id().
|
static |
Definition at line 527 of file stun.c.
Referenced by ast_debug_category_stun_packet_id().