47#define DEFAULT_MONITOR_REFRESH 30
48#define DEFAULT_RETRIES 3
77 if (0 <=
args.stun_sock) {
78 close(
args.stun_sock);
88 static const struct sockaddr_in no_addr = { 0, };
91 if (!
args.monitor_enabled) {
92 goto monitor_request_cleanup;
95 if (
args.stun_sock < 0) {
99 if (!
args.server_hostname) {
101 goto monitor_request_cleanup;
110 args.server_hostname);
111 goto monitor_request_cleanup;
116 args.stun_sock = socket(AF_INET, SOCK_DGRAM, 0);
117 if (
args.stun_sock < 0) {
119 goto monitor_request_cleanup;
125 goto monitor_request_cleanup;
136 if (!
args.stun_poll_failed_gripe) {
137 args.stun_poll_failed_gripe = 1;
139 res < 0 ?
"failed" :
"got no response");
143 args.stun_poll_failed_gripe = 0;
144 if (memcmp(&no_addr, &
answer,
sizeof(no_addr))
145 && memcmp(&
args.external_addr, &
answer,
sizeof(
args.external_addr))) {
147 int newport = ntohs(
answer.sin_port);
151 ntohs(
args.external_addr.sin_port), newaddr, newport);
155 if (
args.external_addr_known) {
161 goto publish_failure;
168 goto publish_failure;
172 goto publish_failure;
186 args.external_addr_known = 1;
191monitor_request_cleanup:
194 res =
args.refresh * 1000;
209 args.monitor_enabled = 0;
279 args.monitor_enabled = 0;
291 || 1 != sscanf(port_str,
"%30u", &port)) {
315 args.server_hostname = host_str;
316 args.stun_port = port;
319 args.monitor_enabled = 1;
344 args.stun_poll_failed_gripe = 0;
347 args.monitor_enabled = 0;
351 if (!strcasecmp(v->
name,
"stunaddr")) {
356 }
else if (!strcasecmp(v->
name,
"stunrefresh")) {
357 if ((sscanf(v->
value,
"%30u", &
args.refresh) != 1) || !
args.refresh) {
377#define DATALN "%-25s %-5u %-7u %-8d %-7s %-16s %-d\n"
378#define HEADER "%-25s %-5s %-7s %-8s %-7s %-16s %-s\n"
381 ast_cli(fd,
HEADER,
"Hostname",
"Port",
"Period",
"Retries",
"Status",
"ExternAddr",
"ExternPort");
383 if (
args.stun_poll_failed_gripe) {
385 }
else if (
args.external_addr_known) {
391 args.server_hostname,
397 ntohs(
args.external_addr.sin_port)
408 e->
command =
"stun show status";
410 "Usage: stun show status\n"
411 " List all known STUN servers and statuses.\n";
439 if (res < 0 || !
args.monitor_enabled) {
Access Control of various sorts.
int ast_get_ip(struct ast_sockaddr *addr, const char *hostname)
Get the IP address given a hostname.
Asterisk main include file. File version handling, generic pbx functions.
#define ast_strdup(str)
A wrapper for strdup()
#define ast_strdupa(s)
duplicate a string in memory from the stack
static int answer(void *data)
Standard Command Line Interface.
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
#define AST_CLI_DEFINE(fn, txt,...)
void ast_cli(int fd, const char *fmt,...)
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Configuration File Parser.
struct ast_config * ast_config_load2(const char *filename, const char *who_asked, struct ast_flags flags)
Load a config file.
#define CONFIG_STATUS_FILEUNCHANGED
#define CONFIG_STATUS_FILEINVALID
void ast_config_destroy(struct ast_config *cfg)
Destroys a config.
struct ast_variable * ast_variable_browse(const struct ast_config *config, const char *category_name)
@ CONFIG_FLAG_FILEUNCHANGED
Asterisk JSON abstraction layer.
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
struct ast_json * ast_json_object_create(void)
Create a new JSON object.
struct ast_json_payload * ast_json_payload_create(struct ast_json *json)
Create an ao2 object to pass json blobs as data payloads for stasis.
Asterisk locking-related definitions:
#define ast_mutex_init(pmutex)
#define ast_mutex_unlock(a)
#define ast_mutex_destroy(a)
#define ast_mutex_lock(a)
Asterisk module definitions.
@ AST_MODFLAG_GLOBAL_SYMBOLS
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
@ AST_MODPRI_CHANNEL_DEPEND
@ AST_MODULE_SUPPORT_CORE
#define ASTERISK_GPL_KEY
The text the key() function should return.
@ AST_MODULE_LOAD_SUCCESS
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
int ast_connect(int sockfd, const struct ast_sockaddr *addr)
Wrapper around connect(2) that uses struct ast_sockaddr.
static char * ast_sockaddr_stringify(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() with default format.
int ast_sockaddr_split_hostport(char *str, char **host, char **port, int flags)
Splits a string into its host and port components.
#define ast_sockaddr_set_port(addr, port)
Sets the port number of a socket address.
const char * ast_inet_ntoa(struct in_addr ia)
thread-safe replacement for inet_ntoa().
static void stun_stop_monitor(void)
#define DEFAULT_MONITOR_REFRESH
static int stun_start_monitor(void)
static void stun_close_sock(void)
static struct ast_sched_context * sched
struct sockaddr_in external_addr
static void _stun_show_status(int fd)
Execute stun show status command.
static int load_config(int startup)
static int setup_stunaddr(const char *value, int reload)
unsigned int stun_poll_failed_gripe
static int stun_monitor_request(const void *blarg)
called by scheduler to send STUN request
const char * server_hostname
static int __reload(int startup)
static char * handle_cli_stun_show_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static const char stun_conf_file[]
static int load_module(void)
static int unload_module(void)
static struct ast_cli_entry cli_stun[]
unsigned int external_addr_known
unsigned int monitor_enabled
Scheduler Routines (derived from cheops)
void ast_sched_context_destroy(struct ast_sched_context *c)
destroys a schedule context
int ast_sched_start_thread(struct ast_sched_context *con)
Start a thread for processing scheduler entries.
int ast_sched_add_variable(struct ast_sched_context *con, int when, ast_sched_cb callback, const void *data, int variable) attribute_warn_unused_result
Adds a scheduled event with rescheduling support.
struct ast_sched_context * ast_sched_context_create(void)
Create a scheduler context.
Stasis Message Bus API. See Stasis Message Bus API for detailed documentation.
struct stasis_message * stasis_message_create(struct stasis_message_type *type, void *data)
Create a new message.
void stasis_publish(struct stasis_topic *topic, struct stasis_message *message)
Publish a message to a topic's subscribers.
struct stasis_topic * ast_system_topic(void)
A Stasis Message Bus API topic which publishes messages regarding system changes.
struct stasis_message_type * ast_network_change_type(void)
A stasis_message_type for network changes.
static force_inline int attribute_pure ast_strlen_zero(const char *s)
descriptor for a cli entry.
Structure used to handle boolean flags.
Abstract JSON element (object, array, string, int, ...).
Structure for mutex and tracking information.
Socket address structure.
Structure for variables, used for configurations and for channel variables.
struct ast_variable * next
int ast_stun_request(int s, struct sockaddr_in *dst, const char *username, struct sockaddr_in *answer)
Generic STUN request.
static const int STANDARD_STUN_PORT
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
#define ast_set_flag(p, flag)