From 079f48458e024a87c4ce6336a6f88d50a18d4f25 Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Wed, 8 Jun 2011 11:09:03 -0500 Subject: [PATCH] add check_sync to sofia cli (like flush_inbound_reg without the unreg) --- src/mod/endpoints/mod_sofia/mod_sofia.c | 15 +++++ src/mod/endpoints/mod_sofia/mod_sofia.h | 2 + src/mod/endpoints/mod_sofia/sofia_reg.c | 86 +++++++++++++++++++++++++ 3 files changed, 103 insertions(+) diff --git a/src/mod/endpoints/mod_sofia/mod_sofia.c b/src/mod/endpoints/mod_sofia/mod_sofia.c index 31d437d7b0..69f7b24f30 100644 --- a/src/mod/endpoints/mod_sofia/mod_sofia.c +++ b/src/mod/endpoints/mod_sofia/mod_sofia.c @@ -3273,6 +3273,19 @@ static switch_status_t cmd_profile(char **argv, int argc, switch_stream_handle_t goto done; } + if (!strcasecmp(argv[1], "check_sync")) { + if (argc > 2) { + sofia_reg_check_call_id(profile, argv[2]); + stream->write_function(stream, "+OK syncing all registrations matching specified call_id\n"); + } else { + sofia_reg_check_sync(profile); + stream->write_function(stream, "+OK syncing all registrations\n"); + } + + goto done; + } + + if (!strcasecmp(argv[1], "flush_inbound_reg")) { int reboot = 0; @@ -3853,6 +3866,7 @@ SWITCH_STANDARD_API(sofia_function) " watchdog \n\n" "sofia profile [start | stop | restart | rescan]\n" " flush_inbound_reg [ | <[user]@domain>] [reboot]\n" + " check_sync [ | <[user]@domain>]\n" " [register | unregister] [ | all]\n" " killgw \n" " [stun-auto-disable | stun-enabled] [true | false]]\n" @@ -5158,6 +5172,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_sofia_load) switch_console_set_complete("add sofia profile ::sofia::list_profiles restart"); switch_console_set_complete("add sofia profile ::sofia::list_profiles flush_inbound_reg"); + switch_console_set_complete("add sofia profile ::sofia::list_profiles check_sync"); switch_console_set_complete("add sofia profile ::sofia::list_profiles register ::sofia::list_profile_gateway"); switch_console_set_complete("add sofia profile ::sofia::list_profiles unregister ::sofia::list_profile_gateway"); switch_console_set_complete("add sofia profile ::sofia::list_profiles killgw ::sofia::list_profile_gateway"); diff --git a/src/mod/endpoints/mod_sofia/mod_sofia.h b/src/mod/endpoints/mod_sofia/mod_sofia.h index 4b41189a02..ed57e81e1e 100644 --- a/src/mod/endpoints/mod_sofia/mod_sofia.h +++ b/src/mod/endpoints/mod_sofia/mod_sofia.h @@ -983,6 +983,8 @@ void sofia_glue_tech_patch_sdp(private_object_t *tech_pvt); switch_status_t sofia_glue_tech_proxy_remote_addr(private_object_t *tech_pvt); void sofia_presence_event_thread_start(void); void sofia_reg_expire_call_id(sofia_profile_t *profile, const char *call_id, int reboot); +void sofia_reg_check_call_id(sofia_profile_t *profile, const char *call_id); +void sofia_reg_check_sync(sofia_profile_t *profile); switch_status_t sofia_glue_tech_choose_video_port(private_object_t *tech_pvt, int force); switch_status_t sofia_glue_tech_set_video_codec(private_object_t *tech_pvt, int force); char *sofia_glue_get_register_host(const char *uri); diff --git a/src/mod/endpoints/mod_sofia/sofia_reg.c b/src/mod/endpoints/mod_sofia/sofia_reg.c index fa837eb830..df02436ecd 100644 --- a/src/mod/endpoints/mod_sofia/sofia_reg.c +++ b/src/mod/endpoints/mod_sofia/sofia_reg.c @@ -733,6 +733,92 @@ void sofia_reg_check_expire(sofia_profile_t *profile, time_t now, int reboot) } + +int sofia_reg_check_callback(void *pArg, int argc, char **argv, char **columnNames) +{ + sofia_profile_t *profile = (sofia_profile_t *) pArg; + + sofia_reg_send_reboot(profile, argv[1], argv[2], argv[3], argv[7], argv[11]); + + return 0; +} + +void sofia_reg_check_call_id(sofia_profile_t *profile, const char *call_id) +{ + char *sql = NULL; + char *sqlextra = NULL; + char *dup = strdup(call_id); + char *host = NULL, *user = NULL; + + switch_assert(dup); + + if ((host = strchr(dup, '@'))) { + *host++ = '\0'; + user = dup; + } else { + host = dup; + } + + if (!host) { + host = "none"; + } + + if (zstr(user)) { + sqlextra = switch_mprintf(" or (sip_host='%q')", host); + } else { + sqlextra = switch_mprintf(" or (sip_user='%q' and sip_host='%q')", user, host); + } + + sql = switch_mprintf("select call_id,sip_user,sip_host,contact,status,rpid,expires" + ",user_agent,server_user,server_host,profile_name,network_ip" + " from sip_registrations where call_id='%q' %s", call_id, sqlextra); + + switch_mutex_lock(profile->ireg_mutex); + sofia_glue_execute_sql_callback(profile, NULL, sql, sofia_reg_check_callback, profile); + switch_mutex_unlock(profile->ireg_mutex); + + switch_safe_free(sql); + switch_safe_free(sqlextra); + switch_safe_free(dup); + +} + +void sofia_reg_check_sync(sofia_profile_t *profile) +{ + char sql[1024]; + + switch_mutex_lock(profile->ireg_mutex); + + switch_snprintf(sql, sizeof(sql), "select call_id,sip_user,sip_host,contact,status,rpid,expires" + ",user_agent,server_user,server_host,profile_name,network_ip" + " from sip_registrations where expires > 0"); + + + sofia_glue_execute_sql_callback(profile, NULL, sql, sofia_reg_del_callback, profile); + switch_snprintf(sql, sizeof(sql), "delete from sip_registrations where expires > 0 and hostname='%s'", mod_sofia_globals.hostname); + sofia_glue_actually_execute_sql(profile, sql, NULL); + + + switch_snprintf(sql, sizeof(sql), "delete from sip_presence where expires > 0 and hostname='%s'", mod_sofia_globals.hostname); + sofia_glue_actually_execute_sql(profile, sql, NULL); + + switch_snprintf(sql, sizeof(sql), "delete from sip_authentication where expires > 0 and hostname='%s'", mod_sofia_globals.hostname); + sofia_glue_actually_execute_sql(profile, sql, NULL); + + switch_snprintf(sql, sizeof(sql), "select sub_to_user,sub_to_host,call_id from sip_subscriptions where expires >= -1 and hostname='%s'", + mod_sofia_globals.hostname); + sofia_glue_execute_sql_callback(profile, NULL, sql, sofia_sub_del_callback, profile); + + switch_snprintf(sql, sizeof(sql), "delete from sip_subscriptions where expires >= -1 and hostname='%s'", mod_sofia_globals.hostname); + sofia_glue_actually_execute_sql(profile, sql, NULL); + + switch_snprintf(sql, sizeof(sql), "delete from sip_dialogs where expires >= -1 and hostname='%s'", mod_sofia_globals.hostname); + sofia_glue_actually_execute_sql(profile, sql, NULL); + + switch_mutex_unlock(profile->ireg_mutex); + +} + char *sofia_reg_find_reg_url(sofia_profile_t *profile, const char *user, const char *host, char *val, switch_size_t len) { struct callback_t cbt = { 0 };