34 #include "asterisk/module.h"
44 #define DEFAULT_MONITOR_REFRESH 30
72 if (0 <=
args.stun_sock) {
73 close(
args.stun_sock);
82 struct sockaddr_in answer;
83 static const struct sockaddr_in no_addr = { 0, };
86 if (!
args.monitor_enabled) {
87 goto monitor_request_cleanup;
90 if (
args.stun_sock < 0) {
94 if (!
args.server_hostname) {
96 goto monitor_request_cleanup;
100 memset(&stun_addr, 0,
sizeof(stun_addr));
101 stun_addr.
ss.ss_family = AF_INET;
105 args.server_hostname);
106 goto monitor_request_cleanup;
111 args.stun_sock = socket(AF_INET, SOCK_DGRAM, 0);
112 if (
args.stun_sock < 0) {
114 goto monitor_request_cleanup;
120 goto monitor_request_cleanup;
131 if (!
args.stun_poll_failed_gripe) {
132 args.stun_poll_failed_gripe = 1;
134 res < 0 ?
"failed" :
"got no response");
138 args.stun_poll_failed_gripe = 0;
139 if (memcmp(&no_addr, &answer,
sizeof(no_addr))
140 && memcmp(&
args.external_addr, &answer,
sizeof(
args.external_addr))) {
142 int newport = ntohs(answer.sin_port);
146 ntohs(
args.external_addr.sin_port), newaddr, newport);
148 args.external_addr = answer;
150 if (
args.external_addr_known) {
167 args.external_addr_known = 1;
172 monitor_request_cleanup:
175 res =
args.refresh * 1000;
192 args.monitor_enabled = 0;
194 args.server_hostname = NULL;
253 args.monitor_enabled = 0;
265 || 1 != sscanf(port_str,
"%30u", &port)) {
275 memset(&stun_addr, 0,
sizeof(stun_addr));
276 stun_addr.
ss.ss_family = AF_INET;
285 args.server_hostname = host_str;
286 args.stun_port = port;
289 args.monitor_enabled = 1;
314 args.stun_poll_failed_gripe = 0;
317 args.monitor_enabled = 0;
321 if (!strcasecmp(v->
name,
"stunaddr")) {
326 }
else if (!strcasecmp(v->
name,
"stunrefresh")) {
327 if ((sscanf(v->
value,
"%30u", &
args.refresh) != 1) || !
args.refresh) {
352 if (res < 0 || !
args.monitor_enabled) {
struct sockaddr_storage ss
Asterisk locking-related definitions:
Asterisk main include file. File version handling, generic pbx functions.
struct ast_sched_thread * ast_sched_thread_destroy(struct ast_sched_thread *st)
Destroy a scheduler and its thread.
#define DEFAULT_MONITOR_REFRESH
static int stun_monitor_request(const void *blarg)
struct ast_sched_thread * ast_sched_thread_create(void)
Create a scheduler with a dedicated thread.
int ast_sched_thread_add_variable(struct ast_sched_thread *st, int when, ast_sched_cb cb, const void *data, int variable)
Add a variable reschedule time scheduler entry.
static int __reload(int startup)
#define ast_set_flag(p, flag)
struct ast_variable * ast_variable_browse(const struct ast_config *config, const char *category)
Goes through variables.
Structure for variables, used for configurations and for channel variables.
Configuration File Parser.
#define ast_mutex_lock(a)
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Socket address structure.
struct ast_config * ast_config_load2(const char *filename, const char *who_asked, struct ast_flags flags)
Load a config file.
void ast_config_destroy(struct ast_config *config)
Destroys a config.
struct sockaddr_in external_addr
int ast_event_queue(struct ast_event *event)
Queue an event.
int ast_sockaddr_split_hostport(char *str, char **host, char **port, int flags)
Splits a string into its host and port components.
static const int STANDARD_STUN_PORT
int ast_stun_request(int s, struct sockaddr_in *dst, const char *username, struct sockaddr_in *answer)
Generic STUN request.
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Access Control of various sorts.
Scheduler Routines (derived from cheops)
static int load_config(int startup)
static int stun_start_monitor(void)
static struct sched_context * sched
#define ast_strdupa(s)
duplicate a string in memory from the stack
static int setup_stunaddr(const char *value)
#define ast_sockaddr_set_port(addr, port)
Sets the port number of a socket address.
static char * ast_sockaddr_stringify(const struct ast_sockaddr *addr)
Wrapper around ast_sockaddr_stringify_fmt() with default format.
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
static int load_module(void)
int ast_get_ip(struct ast_sockaddr *addr, const char *hostname)
Get the IP address given a hostname.
const char * ast_inet_ntoa(struct in_addr ia)
thread-safe replacement for inet_ntoa().
static const char stun_conf_file[]
static void stun_close_sock(void)
Structure used to handle boolean flags.
unsigned int monitor_enabled
const char * server_hostname
void ast_event_destroy(struct ast_event *event)
Destroy an event.
struct ast_event * ast_event_new(enum ast_event_type event_type,...)
Create a new event.
unsigned int stun_poll_failed_gripe
struct ast_variable * next
#define ast_mutex_init(pmutex)
static int unload_module(void)
#define CONFIG_STATUS_FILEINVALID
#define ast_mutex_destroy(a)
#define ASTERISK_GPL_KEY
The text the key() function should return.
int ast_connect(int sockfd, const struct ast_sockaddr *addr)
Wrapper around connect(2) that uses struct ast_sockaddr.
Structure for mutex and tracking information.
unsigned int external_addr_known
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
#define CONFIG_STATUS_FILEUNCHANGED
#define ast_mutex_unlock(a)
static void stun_stop_monitor(void)