mirror of
https://github.com/asterisk/asterisk.git
synced 2025-09-04 20:04:50 +00:00
Stasis: Convert network change events into network change stasis messages
(issue ASTERISK-21103) Review: https://reviewboard.asterisk.org/r/2490/ git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@387594 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
@@ -283,9 +283,9 @@ static char default_parkinglot[AST_MAX_CONTEXT];
|
||||
static char language[MAX_LANGUAGE] = "";
|
||||
static char regcontext[AST_MAX_CONTEXT] = "";
|
||||
|
||||
static struct ast_event_sub *network_change_event_subscription; /*!< subscription id for network change events */
|
||||
static struct stasis_subscription *network_change_sub; /*!< subscription id for network change events */
|
||||
static struct stasis_subscription *acl_change_sub; /*!< subscription id for ACL change events */
|
||||
static int network_change_event_sched_id = -1;
|
||||
static int network_change_sched_id = -1;
|
||||
|
||||
static int maxauthreq = 3;
|
||||
static int max_retries = 4;
|
||||
@@ -1254,7 +1254,7 @@ static void build_rand_pad(unsigned char *buf, ssize_t len);
|
||||
static int get_unused_callno(enum callno_type type, int validated, callno_entry *entry);
|
||||
static int replace_callno(const void *obj);
|
||||
static void sched_delay_remove(struct sockaddr_in *sin, callno_entry entry);
|
||||
static void network_change_event_cb(const struct ast_event *, void *);
|
||||
static void network_change_stasis_cb(void *data, struct stasis_subscription *sub, struct stasis_topic *topic, struct stasis_message *message);
|
||||
static void acl_change_stasis_cb(void *data, struct stasis_subscription *sub, struct stasis_topic *topic, struct stasis_message *message);
|
||||
|
||||
static struct ast_channel_tech iax2_tech = {
|
||||
@@ -1323,18 +1323,18 @@ static void mwi_event_cb(void *userdata, struct stasis_subscription *sub, struct
|
||||
* is time to send MWI, since it is only sent with a REGACK. */
|
||||
}
|
||||
|
||||
static void network_change_event_subscribe(void)
|
||||
static void network_change_stasis_subscribe(void)
|
||||
{
|
||||
if (!network_change_event_subscription) {
|
||||
network_change_event_subscription = ast_event_subscribe(AST_EVENT_NETWORK_CHANGE,
|
||||
network_change_event_cb, "IAX2 Network Change", NULL, AST_EVENT_IE_END);
|
||||
if (!network_change_sub) {
|
||||
network_change_sub = stasis_subscribe(ast_system_topic(),
|
||||
network_change_stasis_cb, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
static void network_change_event_unsubscribe(void)
|
||||
static void network_change_stasis_unsubscribe(void)
|
||||
{
|
||||
if (network_change_event_subscription) {
|
||||
network_change_event_subscription = ast_event_unsubscribe(network_change_event_subscription);
|
||||
if (network_change_sub) {
|
||||
network_change_sub = stasis_unsubscribe(network_change_sub);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1353,10 +1353,10 @@ static void acl_change_stasis_unsubscribe(void)
|
||||
}
|
||||
}
|
||||
|
||||
static int network_change_event_sched_cb(const void *data)
|
||||
static int network_change_sched_cb(const void *data)
|
||||
{
|
||||
struct iax2_registry *reg;
|
||||
network_change_event_sched_id = -1;
|
||||
network_change_sched_id = -1;
|
||||
AST_LIST_LOCK(®istrations);
|
||||
AST_LIST_TRAVERSE(®istrations, reg, entry) {
|
||||
iax2_do_register(reg);
|
||||
@@ -1366,13 +1366,18 @@ static int network_change_event_sched_cb(const void *data)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void network_change_event_cb(const struct ast_event *event, void *userdata)
|
||||
static void network_change_stasis_cb(void *data, struct stasis_subscription *sub,
|
||||
struct stasis_topic *topic, struct stasis_message *message)
|
||||
{
|
||||
ast_debug(1, "IAX, got a network change event, renewing all IAX registrations.\n");
|
||||
if (network_change_event_sched_id == -1) {
|
||||
network_change_event_sched_id = iax2_sched_add(sched, 1000, network_change_event_sched_cb, NULL);
|
||||
/* This callback is only concerned with network change messages from the system topic. */
|
||||
if (stasis_message_type(message) != ast_network_change_type()) {
|
||||
return;
|
||||
}
|
||||
|
||||
ast_verb(1, "IAX, got a network change message, renewing all IAX registrations.\n");
|
||||
if (network_change_sched_id == -1) {
|
||||
network_change_sched_id = iax2_sched_add(sched, 1000, network_change_sched_cb, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
static void acl_change_stasis_cb(void *data, struct stasis_subscription *sub,
|
||||
@@ -13442,9 +13447,9 @@ static int set_config(const char *config_file, int reload, int forced)
|
||||
}
|
||||
|
||||
if (subscribe_network_change) {
|
||||
network_change_event_subscribe();
|
||||
network_change_stasis_subscribe();
|
||||
} else {
|
||||
network_change_event_unsubscribe();
|
||||
network_change_stasis_unsubscribe();
|
||||
}
|
||||
|
||||
if (defaultsockfd < 0) {
|
||||
@@ -14287,7 +14292,7 @@ static int __unload_module(void)
|
||||
struct ast_context *con;
|
||||
int x;
|
||||
|
||||
network_change_event_unsubscribe();
|
||||
network_change_stasis_unsubscribe();
|
||||
acl_change_stasis_unsubscribe();
|
||||
|
||||
ast_manager_unregister("IAXpeers");
|
||||
@@ -14789,7 +14794,7 @@ static int load_module(void)
|
||||
|
||||
ast_realtime_require_field("iaxpeers", "name", RQ_CHAR, 10, "ipaddr", RQ_CHAR, 15, "port", RQ_UINTEGER2, 5, "regseconds", RQ_UINTEGER2, 6, SENTINEL);
|
||||
|
||||
network_change_event_subscribe();
|
||||
network_change_stasis_subscribe();
|
||||
|
||||
return AST_MODULE_LOAD_SUCCESS;
|
||||
}
|
||||
|
@@ -843,9 +843,9 @@ static int regobjs = 0; /*!< Registry objects */
|
||||
static struct ast_flags global_flags[3] = {{0}}; /*!< global SIP_ flags */
|
||||
static int global_t38_maxdatagram; /*!< global T.38 FaxMaxDatagram override */
|
||||
|
||||
static struct ast_event_sub *network_change_event_subscription; /*!< subscription id for network change events */
|
||||
static struct stasis_subscription *network_change_sub; /*!< subscription id for network change events */
|
||||
static struct stasis_subscription *acl_change_sub; /*!< subscription id for named ACL system change events */
|
||||
static int network_change_event_sched_id = -1;
|
||||
static int network_change_sched_id = -1;
|
||||
|
||||
static char used_context[AST_MAX_CONTEXT]; /*!< name of automatically created context for unloading */
|
||||
|
||||
@@ -1286,7 +1286,7 @@ static int sip_poke_peer(struct sip_peer *peer, int force);
|
||||
static void sip_poke_all_peers(void);
|
||||
static void sip_peer_hold(struct sip_pvt *p, int hold);
|
||||
static void mwi_event_cb(void *, struct stasis_subscription *, struct stasis_topic *, struct stasis_message *);
|
||||
static void network_change_event_cb(const struct ast_event *, void *);
|
||||
static void network_change_stasis_cb(void *data, struct stasis_subscription *sub, struct stasis_topic *topic, struct stasis_message *message);
|
||||
static void acl_change_stasis_cb(void *data, struct stasis_subscription *sub, struct stasis_topic *topic, struct stasis_message *message);
|
||||
static void sip_keepalive_all_peers(void);
|
||||
|
||||
@@ -16707,18 +16707,18 @@ static void mwi_event_cb(void *userdata, struct stasis_subscription *sub, struct
|
||||
}
|
||||
}
|
||||
|
||||
static void network_change_event_subscribe(void)
|
||||
static void network_change_stasis_subscribe(void)
|
||||
{
|
||||
if (!network_change_event_subscription) {
|
||||
network_change_event_subscription = ast_event_subscribe(AST_EVENT_NETWORK_CHANGE,
|
||||
network_change_event_cb, "SIP Network Change", NULL, AST_EVENT_IE_END);
|
||||
if (!network_change_sub) {
|
||||
network_change_sub = stasis_subscribe(ast_system_topic(),
|
||||
network_change_stasis_cb, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
static void network_change_event_unsubscribe(void)
|
||||
static void network_change_stasis_unsubscribe(void)
|
||||
{
|
||||
if (network_change_event_subscription) {
|
||||
network_change_event_subscription = ast_event_unsubscribe(network_change_event_subscription);
|
||||
if (network_change_sub) {
|
||||
network_change_sub = stasis_unsubscribe(network_change_sub);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16731,26 +16731,31 @@ static void acl_change_stasis_subscribe(void)
|
||||
|
||||
}
|
||||
|
||||
static void acl_change_event_unsubscribe(void)
|
||||
static void acl_change_event_stasis_unsubscribe(void)
|
||||
{
|
||||
if (acl_change_sub) {
|
||||
acl_change_sub = stasis_unsubscribe(acl_change_sub);
|
||||
}
|
||||
}
|
||||
|
||||
static int network_change_event_sched_cb(const void *data)
|
||||
static int network_change_sched_cb(const void *data)
|
||||
{
|
||||
network_change_event_sched_id = -1;
|
||||
network_change_sched_id = -1;
|
||||
sip_send_all_registers();
|
||||
sip_send_all_mwi_subscriptions();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void network_change_event_cb(const struct ast_event *event, void *userdata)
|
||||
static void network_change_stasis_cb(void *data, struct stasis_subscription *sub, struct stasis_topic *topic, struct stasis_message *message)
|
||||
{
|
||||
ast_debug(1, "SIP, got a network change event, renewing all SIP registrations.\n");
|
||||
if (network_change_event_sched_id == -1) {
|
||||
network_change_event_sched_id = ast_sched_add(sched, 1000, network_change_event_sched_cb, NULL);
|
||||
/* This callback is only concerned with network change messages from the system topic. */
|
||||
if (stasis_message_type(message) != ast_network_change_type()) {
|
||||
return;
|
||||
}
|
||||
|
||||
ast_verb(1, "SIP, got a network change message, renewing all SIP registrations.\n");
|
||||
if (network_change_sched_id == -1) {
|
||||
network_change_sched_id = ast_sched_add(sched, 1000, network_change_sched_cb, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32329,9 +32334,9 @@ static int reload_config(enum channelreloadreason reason)
|
||||
}
|
||||
|
||||
if (subscribe_network_change) {
|
||||
network_change_event_subscribe();
|
||||
network_change_stasis_subscribe();
|
||||
} else {
|
||||
network_change_event_unsubscribe();
|
||||
network_change_stasis_unsubscribe();
|
||||
}
|
||||
|
||||
if (global_t1 < global_t1min) {
|
||||
@@ -34819,7 +34824,7 @@ static int load_module(void)
|
||||
|
||||
|
||||
sip_register_tests();
|
||||
network_change_event_subscribe();
|
||||
network_change_stasis_subscribe();
|
||||
|
||||
ast_websocket_add_protocol("sip", sip_websocket_callback);
|
||||
|
||||
@@ -34839,8 +34844,8 @@ static int unload_module(void)
|
||||
|
||||
ast_websocket_remove_protocol("sip", sip_websocket_callback);
|
||||
|
||||
network_change_event_unsubscribe();
|
||||
acl_change_event_unsubscribe();
|
||||
network_change_stasis_unsubscribe();
|
||||
acl_change_event_stasis_unsubscribe();
|
||||
|
||||
ast_sched_dump(sched);
|
||||
|
||||
|
@@ -126,6 +126,31 @@ const char *ast_file_version_find(const char *file);
|
||||
|
||||
char *ast_complete_source_filename(const char *partial, int n);
|
||||
|
||||
/*!
|
||||
* \brief accessor for the system stasis topic
|
||||
* \since 12
|
||||
*
|
||||
* \retval NULL if the stasis topic hasn't been created or has been
|
||||
* deliberately disabled. Unless it is ran prior to system
|
||||
* initialization, this should never return NULL.
|
||||
* \retval a pointer to the System stasis topic
|
||||
*/
|
||||
struct stasis_topic *ast_system_topic(void);
|
||||
|
||||
/*!
|
||||
* \brief accessor for the network change stasis message type
|
||||
* \since 12
|
||||
*
|
||||
* \retval NULL if the message type hasn't been created or has been
|
||||
* deliberately disabled. Unless it is ran prior to system
|
||||
* initialization, this should never return NULL.
|
||||
* \retval a pointer to the network change stasis message type
|
||||
*
|
||||
* \note Messages of this type should always be issued on and expected from
|
||||
* the system stasis topic.
|
||||
*/
|
||||
struct stasis_message_type *ast_network_change_type(void);
|
||||
|
||||
/*!
|
||||
* \brief Register/unregister a source code file with the core.
|
||||
* \param file the source file name
|
||||
|
@@ -3560,6 +3560,26 @@ static void env_init(void)
|
||||
setenv("AST_VERSION", ast_get_version(), 1);
|
||||
}
|
||||
|
||||
static struct stasis_topic *system_topic;
|
||||
|
||||
static struct stasis_message_type *network_change_type;
|
||||
|
||||
struct stasis_topic *ast_system_topic(void)
|
||||
{
|
||||
return system_topic;
|
||||
}
|
||||
|
||||
struct stasis_message_type *ast_network_change_type(void)
|
||||
{
|
||||
return network_change_type;
|
||||
}
|
||||
|
||||
static void stasis_system_topic_init(void)
|
||||
{
|
||||
system_topic = stasis_topic_create("ast_system");
|
||||
network_change_type = stasis_message_type_create("network_change");
|
||||
}
|
||||
|
||||
static void print_intro_message(const char *runuser, const char *rungroup)
|
||||
{
|
||||
if (ast_opt_console || option_verbose || (ast_opt_remote && !ast_opt_exec)) {
|
||||
@@ -4127,6 +4147,7 @@ int main(int argc, char *argv[])
|
||||
printf("Stasis initialization failed.\n%s", term_quit());
|
||||
exit(1);
|
||||
}
|
||||
stasis_system_topic_init();
|
||||
|
||||
ast_makesocket();
|
||||
sigemptyset(&sigs);
|
||||
|
21
main/event.c
21
main/event.c
@@ -142,7 +142,6 @@ static int ast_event_cmp(void *obj, void *arg, int flags);
|
||||
static int ast_event_hash_mwi(const void *obj, const int flags);
|
||||
static int ast_event_hash_devstate(const void *obj, const int flags);
|
||||
static int ast_event_hash_devstate_change(const void *obj, const int flags);
|
||||
static int ast_event_hash_presence_state_change(const void *obj, const int flags);
|
||||
|
||||
#ifdef LOW_MEMORY
|
||||
#define NUM_CACHE_BUCKETS 17
|
||||
@@ -189,10 +188,6 @@ static struct cache_events ast_event_cache[AST_EVENT_TOTAL] = {
|
||||
.hash_fn = ast_event_hash_devstate_change,
|
||||
.cache_args = { AST_EVENT_IE_DEVICE, AST_EVENT_IE_EID, },
|
||||
},
|
||||
[AST_EVENT_PRESENCE_STATE] = {
|
||||
.hash_fn = ast_event_hash_presence_state_change,
|
||||
.cache_args = { AST_EVENT_IE_PRESENCE_STATE, },
|
||||
},
|
||||
};
|
||||
|
||||
/*!
|
||||
@@ -1614,22 +1609,6 @@ static int ast_event_hash_devstate_change(const void *obj, const int flags)
|
||||
return ast_str_hash(ast_event_get_ie_str(event, AST_EVENT_IE_DEVICE));
|
||||
}
|
||||
|
||||
/*!
|
||||
* \internal
|
||||
* \brief Hash function for AST_EVENT_PRESENCE_STATE
|
||||
*
|
||||
* \param[in] obj an ast_event
|
||||
* \param[in] flags unused
|
||||
*
|
||||
* \return hash value
|
||||
*/
|
||||
static int ast_event_hash_presence_state_change(const void *obj, const int flags)
|
||||
{
|
||||
const struct ast_event *event = obj;
|
||||
|
||||
return ast_str_hash(ast_event_get_ie_str(event, AST_EVENT_IE_PRESENCE_PROVIDER));
|
||||
}
|
||||
|
||||
static int ast_event_hash(const void *obj, const int flags)
|
||||
{
|
||||
const struct ast_event_ref *event_ref;
|
||||
|
@@ -32,7 +32,6 @@
|
||||
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
||||
|
||||
#include "asterisk/module.h"
|
||||
#include "asterisk/event.h"
|
||||
#include "asterisk/sched.h"
|
||||
#include "asterisk/config.h"
|
||||
#include "asterisk/stun.h"
|
||||
@@ -40,6 +39,9 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
||||
#include "asterisk/lock.h"
|
||||
#include "asterisk/acl.h"
|
||||
#include "asterisk/cli.h"
|
||||
#include "asterisk/stasis.h"
|
||||
#include "asterisk/json.h"
|
||||
#include "asterisk/astobj2.h"
|
||||
|
||||
#include <fcntl.h>
|
||||
|
||||
@@ -152,18 +154,27 @@ static int stun_monitor_request(const void *blarg)
|
||||
args.external_addr = answer;
|
||||
|
||||
if (args.external_addr_known) {
|
||||
struct ast_event *event;
|
||||
RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
|
||||
RAII_VAR(struct ast_json_payload *, json_payload, NULL, ao2_cleanup);
|
||||
RAII_VAR(struct ast_json *, json_object, ast_json_object_create(), ast_json_unref);
|
||||
|
||||
/*
|
||||
* The external address was already known, and has changed...
|
||||
* generate event.
|
||||
*/
|
||||
event = ast_event_new(AST_EVENT_NETWORK_CHANGE, AST_EVENT_IE_END);
|
||||
if (!event) {
|
||||
ast_log(LOG_ERROR, "Could not create AST_EVENT_NETWORK_CHANGE event.\n");
|
||||
} else if (ast_event_queue(event)) {
|
||||
ast_event_destroy(event);
|
||||
ast_log(LOG_ERROR, "Could not queue AST_EVENT_NETWORK_CHANGE event.\n");
|
||||
/* This json_object doesn't actually contain anything yet. We have to reference something
|
||||
* for stasis, and this is useful for if we want to ever add data for any reason. */
|
||||
if (!json_object) {
|
||||
goto publish_failure;
|
||||
}
|
||||
|
||||
if (!(json_payload = ast_json_payload_create(json_object))) {
|
||||
goto publish_failure;
|
||||
}
|
||||
|
||||
msg = stasis_message_create(ast_network_change_type(), json_payload);
|
||||
|
||||
publish_failure:
|
||||
if (msg) {
|
||||
stasis_publish(ast_system_topic(), msg);
|
||||
} else {
|
||||
ast_log(LOG_ERROR, "Failed to issue network change message.\n");
|
||||
}
|
||||
} else {
|
||||
/* this was the first external address we found, do not alert listeners
|
||||
|
Reference in New Issue
Block a user