From 00b53a91ea31c2758ecf6eac600915f1b828101f Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Fri, 29 Apr 2011 10:24:50 -0500 Subject: [PATCH] FS-3277 --resolve There are actually really only 2 places where it needs the real hostname, the hostname command and the header on the events. I changed everything to the switchname because there were some more sql stmts etc in the core. --- conf/autoload_configs/switch.conf.xml | 8 +++ src/include/private/switch_core_pvt.h | 1 + src/include/switch_core.h | 1 + .../mod_cidlookup/mod_cidlookup.c | 3 - .../applications/mod_commands/mod_commands.c | 15 ++-- src/mod/applications/mod_curl/mod_curl.c | 3 - src/mod/applications/mod_db/mod_db.c | 2 +- .../mod_directory/mod_directory.c | 4 +- src/mod/applications/mod_fifo/mod_fifo.c | 2 +- src/mod/applications/mod_redis/mod_redis.c | 10 +-- src/mod/endpoints/mod_sofia/mod_sofia.c | 3 +- .../mod_event_multicast/mod_event_multicast.c | 4 +- src/mod/event_handlers/mod_snmp/subagent.c | 7 +- src/mod/xml_int/mod_xml_curl/mod_xml_curl.c | 2 +- src/switch_console.c | 45 ++++++------ src/switch_core.c | 10 +++ src/switch_core_sqldb.c | 70 +++++++++---------- src/switch_event.c | 5 +- 18 files changed, 102 insertions(+), 93 deletions(-) diff --git a/conf/autoload_configs/switch.conf.xml b/conf/autoload_configs/switch.conf.xml index d2500a6463..3cba428e45 100644 --- a/conf/autoload_configs/switch.conf.xml +++ b/conf/autoload_configs/switch.conf.xml @@ -24,6 +24,14 @@ + + + diff --git a/src/include/private/switch_core_pvt.h b/src/include/private/switch_core_pvt.h index 0ef64d1079..23c5272a1f 100644 --- a/src/include/private/switch_core_pvt.h +++ b/src/include/private/switch_core_pvt.h @@ -248,6 +248,7 @@ struct switch_runtime { int max_sql_buffer_len; switch_dbtype_t odbc_dbtype; char hostname[256]; + char *switchname; int multiple_registrations; uint32_t max_db_handles; uint32_t db_handle_timeout; diff --git a/src/include/switch_core.h b/src/include/switch_core.h index 5a2b8992b2..88e8152f2a 100644 --- a/src/include/switch_core.h +++ b/src/include/switch_core.h @@ -762,6 +762,7 @@ SWITCH_DECLARE(char *) switch_core_get_variable(_In_z_ const char *varname); SWITCH_DECLARE(char *) switch_core_get_variable_dup(_In_z_ const char *varname); SWITCH_DECLARE(char *) switch_core_get_variable_pdup(_In_z_ const char *varname, switch_memory_pool_t *pool); SWITCH_DECLARE(const char *) switch_core_get_hostname(void); +SWITCH_DECLARE(const char *) switch_core_get_switchname(void); /*! \brief Add a global variable to the core diff --git a/src/mod/applications/mod_cidlookup/mod_cidlookup.c b/src/mod/applications/mod_cidlookup/mod_cidlookup.c index 06bf615ce5..64e5fc7520 100755 --- a/src/mod/applications/mod_cidlookup/mod_cidlookup.c +++ b/src/mod/applications/mod_cidlookup/mod_cidlookup.c @@ -366,7 +366,6 @@ static long do_lookup_url(switch_memory_pool_t *pool, switch_event_t *event, cha switch_time_t time_diff = 0; CURL *curl_handle = NULL; long httpRes = 0; - char hostname[256] = ""; struct http_data http_data; @@ -375,8 +374,6 @@ static long do_lookup_url(switch_memory_pool_t *pool, switch_event_t *event, cha http_data.max_bytes = 10240; SWITCH_STANDARD_STREAM(http_data.stream); - gethostname(hostname, sizeof(hostname)); - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG10, "url: %s\n", query); curl_handle = curl_easy_init(); diff --git a/src/mod/applications/mod_commands/mod_commands.c b/src/mod/applications/mod_commands/mod_commands.c index 96ef58299e..d7d67223e4 100644 --- a/src/mod/applications/mod_commands/mod_commands.c +++ b/src/mod/applications/mod_commands/mod_commands.c @@ -249,9 +249,13 @@ SWITCH_STANDARD_API(banner_function) SWITCH_STANDARD_API(hostname_api_function) { - char hostname[256] = ""; - gethostname(hostname, sizeof(hostname)); - stream->write_function(stream, "%s", hostname); + stream->write_function(stream, "%s", switch_core_get_hostname()); + return SWITCH_STATUS_SUCCESS; +} + +SWITCH_STANDARD_API(switchname_api_function) +{ + stream->write_function(stream, "%s", switch_core_get_switchname()); return SWITCH_STATUS_SUCCESS; } @@ -3862,9 +3866,7 @@ SWITCH_STANDARD_API(show_function) char *command = NULL, *as = NULL; switch_core_flag_t cflags = switch_core_flags(); switch_status_t status = SWITCH_STATUS_SUCCESS; - char hostname[256] = ""; - gethostname(hostname, sizeof(hostname)); - + const char *hostname = switch_core_get_switchname(); if (!(cflags & SCF_USE_SQL)) { stream->write_function(stream, "-ERR SQL DISABLED NO DATA AVAILABLE!\n"); @@ -5150,6 +5152,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_commands_load) SWITCH_ADD_API(commands_api_interface, "help", "Show help for all the api commands", help_function, ""); SWITCH_ADD_API(commands_api_interface, "host_lookup", "host_lookup", host_lookup_function, ""); SWITCH_ADD_API(commands_api_interface, "hostname", "Returns the system hostname", hostname_api_function, ""); + SWITCH_ADD_API(commands_api_interface, "switchname", "Returns the switch name", switchname_api_function, ""); SWITCH_ADD_API(commands_api_interface, "hupall", "hupall", hupall_api_function, " [ ]"); SWITCH_ADD_API(commands_api_interface, "in_group", "determine if a user is in a group", in_group_function, "[@] "); SWITCH_ADD_API(commands_api_interface, "is_lan_addr", "see if an ip is a lan addr", lan_addr_function, ""); diff --git a/src/mod/applications/mod_curl/mod_curl.c b/src/mod/applications/mod_curl/mod_curl.c index 633e4c80e2..adcfe453de 100644 --- a/src/mod/applications/mod_curl/mod_curl.c +++ b/src/mod/applications/mod_curl/mod_curl.c @@ -104,7 +104,6 @@ static http_data_t *do_lookup_url(switch_memory_pool_t *pool, const char *url, c CURL *curl_handle = NULL; long httpRes = 0; - char hostname[256] = ""; http_data_t *http_data = NULL; @@ -115,8 +114,6 @@ static http_data_t *do_lookup_url(switch_memory_pool_t *pool, const char *url, c http_data->max_bytes = 64000; SWITCH_STANDARD_STREAM(http_data->stream); - gethostname(hostname, sizeof(hostname)); - if (!method) { method = "get"; } diff --git a/src/mod/applications/mod_db/mod_db.c b/src/mod/applications/mod_db/mod_db.c index e611d036ea..165e02e949 100644 --- a/src/mod/applications/mod_db/mod_db.c +++ b/src/mod/applications/mod_db/mod_db.c @@ -598,7 +598,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_db_load) switch_limit_interface_t *limit_interface; memset(&globals, 0, sizeof(&globals)); - gethostname(globals.hostname, sizeof(globals.hostname)); + strncpy(globals.hostname, switch_core_get_switchname(), sizeof(globals.hostname)); globals.pool = pool; diff --git a/src/mod/applications/mod_directory/mod_directory.c b/src/mod/applications/mod_directory/mod_directory.c index 0127363064..b4c7d94e78 100644 --- a/src/mod/applications/mod_directory/mod_directory.c +++ b/src/mod/applications/mod_directory/mod_directory.c @@ -67,7 +67,7 @@ static switch_xml_config_int_options_t config_int_ht_0 = { SWITCH_TRUE, 0 }; static struct { switch_hash_t *profile_hash; - char hostname[256]; + const char *hostname; int integer; int debug; char *dbname; @@ -949,7 +949,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_directory_load) /* connect my internal structure to the blank pointer passed to me */ *module_interface = switch_loadable_module_create_module_interface(pool, modname); - gethostname(globals.hostname, sizeof(globals.hostname)); + globals.hostname = switch_core_get_switchname(); globals.dbname = switch_core_sprintf(pool, "directory"); diff --git a/src/mod/applications/mod_fifo/mod_fifo.c b/src/mod/applications/mod_fifo/mod_fifo.c index a0a1f9df56..1984406eff 100644 --- a/src/mod/applications/mod_fifo/mod_fifo.c +++ b/src/mod/applications/mod_fifo/mod_fifo.c @@ -3885,7 +3885,7 @@ static switch_status_t load_config(int reload, int del_all) switch_cache_db_handle_t *dbh = NULL; fifo_node_t *node; - gethostname(globals.hostname, sizeof(globals.hostname)); + strncpy(globals.hostname, switch_core_get_switchname(), sizeof(globals.hostname)); if (!(xml = switch_xml_open_cfg(cf, &cfg, NULL))) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Open of %s failed\n", cf); diff --git a/src/mod/applications/mod_redis/mod_redis.c b/src/mod/applications/mod_redis/mod_redis.c index ebf2a9e324..36376dd069 100755 --- a/src/mod/applications/mod_redis/mod_redis.c +++ b/src/mod/applications/mod_redis/mod_redis.c @@ -89,7 +89,7 @@ SWITCH_LIMIT_INCR(limit_incr_redis) } /* Get the keys for redis server */ - uuid_rediskey = switch_core_session_sprintf(session,"%s_%s_%s", switch_core_get_hostname(), realm, resource); + uuid_rediskey = switch_core_session_sprintf(session,"%s_%s_%s", switch_core_get_switchname(), realm, resource); rediskey = switch_core_session_sprintf(session, "%s_%s", realm, resource); if ((pvt = switch_channel_get_private(channel, "limit_redis"))) { @@ -179,7 +179,7 @@ SWITCH_LIMIT_RELEASE(limit_release_redis) switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Couldn't decrement value corresponding to %s\n", (char *)p_key); switch_goto_status(SWITCH_STATUS_FALSE, end); } - p_uuid_key = switch_core_session_sprintf(session, "%s_%s", switch_core_get_hostname(), (char *)p_key); + p_uuid_key = switch_core_session_sprintf(session, "%s_%s", switch_core_get_switchname(), (char *)p_key); if (credis_decr(redis,p_uuid_key,&uuid_val) != 0) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Couldn't decrement value corresponding to %s\n", p_uuid_key); switch_goto_status(SWITCH_STATUS_FALSE, end); @@ -193,7 +193,7 @@ SWITCH_LIMIT_RELEASE(limit_release_redis) } else { rediskey = switch_core_session_sprintf(session, "%s_%s", realm, resource); - uuid_rediskey = switch_core_session_sprintf(session, "%s_%s_%s", switch_core_get_hostname(), realm, resource); + uuid_rediskey = switch_core_session_sprintf(session, "%s_%s_%s", switch_core_get_switchname(), realm, resource); switch_core_hash_delete(pvt->hash, (const char *) rediskey); if (credis_decr(redis, rediskey, &val) != 0) { @@ -249,13 +249,13 @@ SWITCH_LIMIT_RESET(limit_reset_redis) { REDIS redis; if (redis_factory(&redis) == SWITCH_STATUS_SUCCESS) { - char *rediskey = switch_mprintf("%s_*", switch_core_get_hostname()); + char *rediskey = switch_mprintf("%s_*", switch_core_get_switchname()); int dec = 0, val = 0, keyc; char *uuids[2000]; if ((keyc = credis_keys(redis, rediskey, uuids, switch_arraylen(uuids))) > 0) { int i = 0; - int hostnamelen = strlen(switch_core_get_hostname())+1; + int hostnamelen = strlen(switch_core_get_switchname())+1; for (i = 0; i < keyc && uuids[i]; i++){ const char *key = uuids[i] + hostnamelen; diff --git a/src/mod/endpoints/mod_sofia/mod_sofia.c b/src/mod/endpoints/mod_sofia/mod_sofia.c index e18c0ea3c0..c1431c28e9 100644 --- a/src/mod/endpoints/mod_sofia/mod_sofia.c +++ b/src/mod/endpoints/mod_sofia/mod_sofia.c @@ -4956,7 +4956,8 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_sofia_load) switch_find_local_ip(mod_sofia_globals.guess_ip, sizeof(mod_sofia_globals.guess_ip), &mod_sofia_globals.guess_mask, AF_INET); in.s_addr = mod_sofia_globals.guess_mask; switch_set_string(mod_sofia_globals.guess_mask_str, inet_ntoa(in)); - gethostname(mod_sofia_globals.hostname, sizeof(mod_sofia_globals.hostname)); + + strcpy(mod_sofia_globals.hostname, switch_core_get_switchname()); switch_core_hash_init(&mod_sofia_globals.profile_hash, mod_sofia_globals.pool); diff --git a/src/mod/event_handlers/mod_event_multicast/mod_event_multicast.c b/src/mod/event_handlers/mod_event_multicast/mod_event_multicast.c index 5c4d11e807..2c0b927c0f 100644 --- a/src/mod/event_handlers/mod_event_multicast/mod_event_multicast.c +++ b/src/mod/event_handlers/mod_event_multicast/mod_event_multicast.c @@ -51,7 +51,6 @@ static struct { char *address; char *bindings; uint32_t key_count; - char hostname[80]; switch_port_t port; switch_sockaddr_t *addr; switch_socket_t *udp_socket; @@ -282,7 +281,7 @@ static void event_handler(switch_event_t *event) case SWITCH_EVENT_LOG: return; default: - switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Multicast-Sender", globals.hostname); + switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Multicast-Sender", switch_core_get_switchname()); if (switch_event_serialize(event, &packet, SWITCH_TRUE) == SWITCH_STATUS_SUCCESS) { size_t len; char *buf; @@ -377,7 +376,6 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_event_multicast_load) switch_core_hash_init(&globals.event_hash, module_pool); switch_core_hash_init(&globals.peer_hash, module_pool); - gethostname(globals.hostname, sizeof(globals.hostname)); globals.key_count = 0; if (load_config() != SWITCH_STATUS_SUCCESS) { diff --git a/src/mod/event_handlers/mod_snmp/subagent.c b/src/mod/event_handlers/mod_snmp/subagent.c index 8d58667687..69f135d6bd 100644 --- a/src/mod/event_handlers/mod_snmp/subagent.c +++ b/src/mod/event_handlers/mod_snmp/subagent.c @@ -140,7 +140,7 @@ int channelList_load(netsnmp_cache *cache, void *vmagic) idx = 1; - sprintf(sql, "SELECT * FROM channels WHERE hostname='%s' ORDER BY created_epoch", switch_core_get_hostname()); + sprintf(sql, "SELECT * FROM channels WHERE hostname='%s' ORDER BY created_epoch", switch_core_get_switchname()); switch_cache_db_execute_sql_callback(dbh, sql, channelList_callback, NULL, NULL); switch_cache_db_release_db_handle(&dbh); @@ -237,14 +237,13 @@ int handle_systemStats(netsnmp_mib_handler *handler, netsnmp_handler_registratio case SS_CURRENT_CALLS: { switch_cache_db_handle_t *dbh; - char sql[1024] = "", hostname[256] = ""; + char sql[1024] = ""; if (switch_core_db_handle(&dbh) != SWITCH_STATUS_SUCCESS) { return SNMP_ERR_GENERR; } - gethostname(hostname, sizeof(hostname)); - sprintf(sql, "SELECT COUNT(*) FROM calls WHERE hostname='%s'", hostname); + sprintf(sql, "SELECT COUNT(*) FROM calls WHERE hostname='%s'", switch_core_get_switchname()); switch_cache_db_execute_sql_callback(dbh, sql, sql_count_callback, &int_val, NULL); snmp_set_var_typed_integer(requests->requestvb, ASN_GAUGE, int_val); switch_cache_db_release_db_handle(&dbh); diff --git a/src/mod/xml_int/mod_xml_curl/mod_xml_curl.c b/src/mod/xml_int/mod_xml_curl/mod_xml_curl.c index bc143d7e48..6aa899babb 100644 --- a/src/mod/xml_int/mod_xml_curl/mod_xml_curl.c +++ b/src/mod/xml_int/mod_xml_curl/mod_xml_curl.c @@ -155,7 +155,7 @@ static switch_xml_t xml_url_fetch(const char *section, const char *tag_name, con char *uri = NULL; char *dynamic_url = NULL; - gethostname(hostname, sizeof(hostname)); + strncpy(hostname, switch_core_get_switchname(), sizeof(hostname)); if (!binding) { return NULL; diff --git a/src/switch_console.c b/src/switch_console.c index a73bb58e89..d902af69e7 100644 --- a/src/switch_console.c +++ b/src/switch_console.c @@ -443,11 +443,8 @@ SWITCH_DECLARE(void) switch_console_printf(switch_text_channel_t channel, const fflush(handle); } -static char hostname[256] = ""; static int32_t running = 1; - - struct helper { int len; int hits; @@ -643,9 +640,9 @@ SWITCH_DECLARE_NONSTD(switch_status_t) switch_console_list_uuid(const char *line if (!zstr(cursor)) { sql = switch_mprintf("select distinct uuid from channels where uuid like '%q%%' and hostname='%q' order by uuid", - cursor, switch_core_get_hostname()); + cursor, switch_core_get_switchname()); } else { - sql = switch_mprintf("select distinct uuid from channels where hostname='%q' order by uuid", switch_core_get_hostname()); + sql = switch_mprintf("select distinct uuid from channels where hostname='%q' order by uuid", switch_core_get_switchname()); } switch_cache_db_execute_sql_callback(db, sql, uuid_callback, &h, &errmsg); @@ -764,7 +761,7 @@ SWITCH_DECLARE(unsigned char) switch_console_complete(const char *line, const ch if (h.words == 0) { sql = switch_mprintf("select distinct name from interfaces where type='api' and name like '%q%%' and hostname='%q' order by name", - buf, switch_core_get_hostname()); + buf, switch_core_get_switchname()); } if (sql) { @@ -792,7 +789,7 @@ SWITCH_DECLARE(unsigned char) switch_console_complete(const char *line, const ch if (h.words == 0) { stream.write_function(&stream, "select distinct a1 from complete where " "a1 not in (select name from interfaces where hostname='%s') %s ", - switch_core_get_hostname(), argc ? "and" : ""); + switch_core_get_switchname(), argc ? "and" : ""); } else { if (switch_cache_db_get_type(db) == SCDB_TYPE_CORE_DB) { stream.write_function(&stream, "select distinct a%d,'%q','%q' from complete where ", h.words + 1, switch_str_nil(dup), switch_str_nil(lp)); @@ -821,7 +818,7 @@ SWITCH_DECLARE(unsigned char) switch_console_complete(const char *line, const ch } } - stream.write_function(&stream, " and hostname='%s' order by a%d", switch_core_get_hostname(), h.words + 1); + stream.write_function(&stream, " and hostname='%s' order by a%d", switch_core_get_switchname(), h.words + 1); switch_cache_db_execute_sql_callback(db, stream.data, comp_callback, &h, &errmsg); @@ -1001,7 +998,7 @@ static unsigned char console_f12key(EditLine * el, int ch) char *prompt(EditLine * e) { if (*prompt_str == '\0') { - switch_snprintf(prompt_str, sizeof(prompt_str), "freeswitch@%s> ", hostname); + switch_snprintf(prompt_str, sizeof(prompt_str), "freeswitch@%s> ", switch_core_get_switchname()); } return prompt_str; @@ -1067,8 +1064,6 @@ SWITCH_DECLARE(void) switch_console_loop(void) switch_threadattr_t *thd_attr = NULL; switch_memory_pool_t *pool; - gethostname(hostname, sizeof(hostname)); - if (switch_core_new_memory_pool(&pool) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Pool Failure\n"); return; @@ -1508,9 +1503,9 @@ SWITCH_DECLARE(void) switch_console_loop(void) /* Load/Init the config first */ console_xml_config(); - gethostname(hostname, sizeof(hostname)); + #ifdef _MSC_VER - sprintf(cmd, "\nfreeswitch@%s> ", hostname); + sprintf(cmd, "\nfreeswitch@%s> ", switch_core_get_switchname()); console_bufferInput(0, 0, cmd, PROMPT_OP); memset(cmd, 0, sizeof(cmd)); #endif @@ -1531,7 +1526,7 @@ SWITCH_DECLARE(void) switch_console_loop(void) } if (activity) { - switch_log_printf(SWITCH_CHANNEL_LOG_CLEAN, SWITCH_LOG_CONSOLE, "\nfreeswitch@%s> ", hostname); + switch_log_printf(SWITCH_CHANNEL_LOG_CLEAN, SWITCH_LOG_CONSOLE, "\nfreeswitch@%s> ", switch_core_get_switchname()); } #ifdef _MSC_VER activity = 0; @@ -1799,7 +1794,7 @@ SWITCH_DECLARE(switch_status_t) switch_console_set_complete(const char *string) } } } - mystream.write_function(&mystream, " '%s')", switch_core_get_hostname()); + mystream.write_function(&mystream, " '%s')", switch_core_get_switchname()); switch_cache_db_persistant_execute(db, mystream.data, 5); status = SWITCH_STATUS_SUCCESS; } else if (!strcasecmp(argv[0], "add")) { @@ -1815,7 +1810,7 @@ SWITCH_DECLARE(switch_status_t) switch_console_set_complete(const char *string) } } } - mystream.write_function(&mystream, " '%s')", switch_core_get_hostname()); + mystream.write_function(&mystream, " '%s')", switch_core_get_switchname()); switch_cache_db_persistant_execute(db, mystream.data, 5); status = SWITCH_STATUS_SUCCESS; @@ -1832,7 +1827,7 @@ SWITCH_DECLARE(switch_status_t) switch_console_set_complete(const char *string) mystream.write_function(&mystream, "a%d = '%w'%w", x + 1, switch_str_nil(argv[x + 1]), x == argc - 2 ? "" : " and "); } } - mystream.write_function(&mystream, " and hostname='%s'", switch_core_get_hostname()); + mystream.write_function(&mystream, " and hostname='%s'", switch_core_get_switchname()); switch_cache_db_persistant_execute(db, mystream.data, 1); } status = SWITCH_STATUS_SUCCESS; @@ -1868,38 +1863,38 @@ SWITCH_DECLARE(switch_status_t) switch_console_set_alias(const char *string) } if (!strcasecmp(argv[0], "stickyadd") && argc == 3) { - sql = switch_mprintf("delete from aliases where alias='%q' and hostname='%q'", argv[1], switch_core_get_hostname()); + sql = switch_mprintf("delete from aliases where alias='%q' and hostname='%q'", argv[1], switch_core_get_switchname()); switch_cache_db_persistant_execute(db, sql, 5); switch_safe_free(sql); if (switch_cache_db_get_type(db) == SCDB_TYPE_CORE_DB) { sql = switch_mprintf("insert into aliases (sticky, alias, command, hostname) values (1, '%q','%q','%q')", - argv[1], argv[2], switch_core_get_hostname()); + argv[1], argv[2], switch_core_get_switchname()); } else { sql = switch_mprintf("insert into aliases (sticky, alias, command, hostname) values (1, '%w','%w','%w')", - argv[1], argv[2], switch_core_get_hostname()); + argv[1], argv[2], switch_core_get_switchname()); } switch_cache_db_persistant_execute(db, sql, 5); status = SWITCH_STATUS_SUCCESS; } else if (!strcasecmp(argv[0], "add") && argc == 3) { - sql = switch_mprintf("delete from aliases where alias='%q' and hostname='%q'", argv[1], switch_core_get_hostname()); + sql = switch_mprintf("delete from aliases where alias='%q' and hostname='%q'", argv[1], switch_core_get_switchname()); switch_cache_db_persistant_execute(db, sql, 5); switch_safe_free(sql); if (switch_cache_db_get_type(db) == SCDB_TYPE_CORE_DB) { sql = switch_mprintf("insert into aliases (sticky, alias, command, hostname) values (0, '%q','%q','%q')", - argv[1], argv[2], switch_core_get_hostname()); + argv[1], argv[2], switch_core_get_switchname()); } else { sql = switch_mprintf("insert into aliases (sticky, alias, command, hostname) values (0, '%w','%w','%w')", - argv[1], argv[2], switch_core_get_hostname()); + argv[1], argv[2], switch_core_get_switchname()); } switch_cache_db_persistant_execute(db, sql, 5); status = SWITCH_STATUS_SUCCESS; } else if (!strcasecmp(argv[0], "del") && argc == 2) { char *what = argv[1]; if (!strcasecmp(what, "*")) { - sql = switch_mprintf("delete from aliases where hostname='%q'", switch_core_get_hostname()); + sql = switch_mprintf("delete from aliases where hostname='%q'", switch_core_get_switchname()); switch_cache_db_persistant_execute(db, sql, 1); } else { - sql = switch_mprintf("delete from aliases where alias='%q' and hostname='%q'", argv[1], switch_core_get_hostname()); + sql = switch_mprintf("delete from aliases where alias='%q' and hostname='%q'", argv[1], switch_core_get_switchname()); switch_cache_db_persistant_execute(db, sql, 5); } status = SWITCH_STATUS_SUCCESS; diff --git a/src/switch_core.c b/src/switch_core.c index 740b0629b5..b80ca810eb 100644 --- a/src/switch_core.c +++ b/src/switch_core.c @@ -265,6 +265,13 @@ SWITCH_DECLARE(const char *) switch_core_get_hostname(void) return runtime.hostname; } +SWITCH_DECLARE(const char *) switch_core_get_switchname(void) +{ + if (!zstr(runtime.switchname)) return runtime.switchname; + return runtime.hostname; +} + + SWITCH_DECLARE(char *) switch_core_get_variable(const char *varname) { char *val; @@ -1716,6 +1723,9 @@ static void switch_load_core_config(const char *file) } else if (!strcasecmp(var, "rtp-enable-zrtp")) { switch_core_set_variable("zrtp_enabled", val); #endif + } else if (!strcasecmp(var, "switchname") && !zstr(val)) { + runtime.switchname = switch_core_strdup(runtime.memory_pool, val); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Set switchname to %s\n", runtime.switchname); } } } diff --git a/src/switch_core_sqldb.c b/src/switch_core_sqldb.c index 106c7b0599..686c5d24de 100644 --- a/src/switch_core_sqldb.c +++ b/src/switch_core_sqldb.c @@ -1141,7 +1141,7 @@ static void core_event_handler(switch_event_t *event) new_sql() = switch_mprintf("insert into tasks values(%q,'%q','%q',%q, '%q')", id, switch_event_get_header_nil(event, "task-desc"), - switch_event_get_header_nil(event, "task-group"), manager ? manager : "0", switch_core_get_hostname() + switch_event_get_header_nil(event, "task-group"), manager ? manager : "0", switch_core_get_switchname() ); } } @@ -1149,7 +1149,7 @@ static void core_event_handler(switch_event_t *event) case SWITCH_EVENT_DEL_SCHEDULE: case SWITCH_EVENT_EXE_SCHEDULE: new_sql() = switch_mprintf("delete from tasks where task_id=%q and hostname='%q'", - switch_event_get_header_nil(event, "task-id"), switch_core_get_hostname()); + switch_event_get_header_nil(event, "task-id"), switch_core_get_switchname()); break; case SWITCH_EVENT_RE_SCHEDULE: { @@ -1160,7 +1160,7 @@ static void core_event_handler(switch_event_t *event) new_sql() = switch_mprintf("update tasks set task_desc='%q',task_group='%q', task_sql_manager=%q where task_id=%q and hostname='%q'", switch_event_get_header_nil(event, "task-desc"), switch_event_get_header_nil(event, "task-group"), manager ? manager : "0", id, - switch_core_get_hostname()); + switch_core_get_switchname()); } } break; @@ -1170,10 +1170,10 @@ static void core_event_handler(switch_event_t *event) if (uuid) { new_sql() = switch_mprintf("delete from channels where uuid='%q' and hostname='%q'", - uuid, switch_core_get_hostname()); + uuid, switch_core_get_switchname()); new_sql() = switch_mprintf("delete from calls where (caller_uuid='%q' or callee_uuid='%q') and hostname='%q'", - uuid, uuid, switch_core_get_hostname()); + uuid, uuid, switch_core_get_switchname()); } } @@ -1185,12 +1185,12 @@ static void core_event_handler(switch_event_t *event) "update calls set callee_uuid='%q' where callee_uuid='%q' and hostname='%q'", switch_event_get_header_nil(event, "unique-id"), switch_event_get_header_nil(event, "old-unique-id"), - switch_core_get_hostname(), + switch_core_get_switchname(), switch_event_get_header_nil(event, "unique-id"), switch_event_get_header_nil(event, "old-unique-id"), - switch_core_get_hostname(), + switch_core_get_switchname(), switch_event_get_header_nil(event, "unique-id"), - switch_event_get_header_nil(event, "old-unique-id"), switch_core_get_hostname() + switch_event_get_header_nil(event, "old-unique-id"), switch_core_get_switchname() ); break; } @@ -1205,7 +1205,7 @@ static void core_event_handler(switch_event_t *event) switch_event_get_header_nil(event, "channel-state"), switch_event_get_header_nil(event, "channel-call-state"), switch_event_get_header_nil(event, "caller-dialplan"), - switch_event_get_header_nil(event, "caller-context"), switch_core_get_hostname() + switch_event_get_header_nil(event, "caller-context"), switch_core_get_switchname() ); break; case SWITCH_EVENT_CODEC: @@ -1218,7 +1218,7 @@ static void core_event_handler(switch_event_t *event) switch_event_get_header_nil(event, "channel-write-codec-name"), switch_event_get_header_nil(event, "channel-write-codec-rate"), switch_event_get_header_nil(event, "channel-write-codec-bit-rate"), - switch_event_get_header_nil(event, "unique-id"), switch_core_get_hostname()); + switch_event_get_header_nil(event, "unique-id"), switch_core_get_switchname()); break; case SWITCH_EVENT_CHANNEL_HOLD: case SWITCH_EVENT_CHANNEL_UNHOLD: @@ -1230,7 +1230,7 @@ static void core_event_handler(switch_event_t *event) switch_event_get_header_nil(event, "application-data"), switch_event_get_header_nil(event, "channel-presence-id"), switch_event_get_header_nil(event, "channel-presence-data"), - switch_event_get_header_nil(event, "unique-id"), switch_core_get_hostname() + switch_event_get_header_nil(event, "unique-id"), switch_core_get_switchname() ); } @@ -1245,7 +1245,7 @@ static void core_event_handler(switch_event_t *event) switch_event_get_header_nil(event, "channel-presence-data"), switch_event_get_header_nil(event, "channel-call-uuid"), extra_cols, - switch_event_get_header_nil(event, "unique-id"), switch_core_get_hostname()); + switch_event_get_header_nil(event, "unique-id"), switch_core_get_switchname()); free(extra_cols); } else { new_sql() = switch_mprintf("update channels set " @@ -1253,7 +1253,7 @@ static void core_event_handler(switch_event_t *event) switch_event_get_header_nil(event, "channel-presence-id"), switch_event_get_header_nil(event, "channel-presence-data"), switch_event_get_header_nil(event, "channel-call-uuid"), - switch_event_get_header_nil(event, "unique-id"), switch_core_get_hostname()); + switch_event_get_header_nil(event, "unique-id"), switch_core_get_switchname()); } } @@ -1288,7 +1288,7 @@ static void core_event_handler(switch_event_t *event) switch_str_nil(name), switch_str_nil(number), switch_event_get_header_nil(event, "direction"), - switch_event_get_header_nil(event, "unique-id"), switch_core_get_hostname()); + switch_event_get_header_nil(event, "unique-id"), switch_core_get_switchname()); name = switch_event_get_header(event, "callee-name"); number = switch_event_get_header(event, "callee-number"); @@ -1305,7 +1305,7 @@ static void core_event_handler(switch_event_t *event) { new_sql() = switch_mprintf("update channels set callstate='%q' where uuid='%q' and hostname='%q'", switch_event_get_header_nil(event, "channel-call-state"), - switch_event_get_header_nil(event, "unique-id"), switch_core_get_hostname()); + switch_event_get_header_nil(event, "unique-id"), switch_core_get_switchname()); } break; @@ -1337,7 +1337,7 @@ static void core_event_handler(switch_event_t *event) switch_event_get_header_nil(event, "channel-presence-id"), switch_event_get_header_nil(event, "channel-presence-data"), extra_cols, - switch_event_get_header_nil(event, "unique-id"), switch_core_get_hostname()); + switch_event_get_header_nil(event, "unique-id"), switch_core_get_switchname()); free(extra_cols); } else { new_sql() = switch_mprintf("update channels set state='%s',cid_name='%q',cid_num='%q'," @@ -1352,13 +1352,13 @@ static void core_event_handler(switch_event_t *event) switch_event_get_header_nil(event, "caller-context"), switch_event_get_header_nil(event, "channel-presence-id"), switch_event_get_header_nil(event, "channel-presence-data"), - switch_event_get_header_nil(event, "unique-id"), switch_core_get_hostname()); + switch_event_get_header_nil(event, "unique-id"), switch_core_get_switchname()); } break; default: new_sql() = switch_mprintf("update channels set state='%s' where uuid='%s' and hostname='%q'", switch_event_get_header_nil(event, "channel-state"), - switch_event_get_header_nil(event, "unique-id"), switch_core_get_hostname()); + switch_event_get_header_nil(event, "unique-id"), switch_core_get_switchname()); break; } @@ -1384,7 +1384,7 @@ static void core_event_handler(switch_event_t *event) new_sql() = switch_mprintf("update channels set call_uuid='%q' where uuid='%s' and hostname='%q'", switch_event_get_header_nil(event, "channel-call-uuid"), - switch_event_get_header_nil(event, "unique-id"), switch_core_get_hostname()); + switch_event_get_header_nil(event, "unique-id"), switch_core_get_switchname()); if (runtime.odbc_dbtype == DBTYPE_DEFAULT) { func_name = "function"; @@ -1411,7 +1411,7 @@ static void core_event_handler(switch_event_t *event) callee_cid_num, switch_event_get_header_nil(event, "Other-Leg-destination-number"), switch_event_get_header_nil(event, "Other-Leg-channel-name"), - switch_event_get_header_nil(event, "Other-Leg-unique-id"), switch_core_get_hostname() + switch_event_get_header_nil(event, "Other-Leg-unique-id"), switch_core_get_switchname() ); } break; @@ -1420,14 +1420,14 @@ static void core_event_handler(switch_event_t *event) char *uuid = switch_event_get_header_nil(event, "caller-unique-id"); new_sql() = switch_mprintf("delete from calls where (caller_uuid='%q' or callee_uuid='%q') and hostname='%q'", - uuid, uuid, switch_core_get_hostname()); + uuid, uuid, switch_core_get_switchname()); break; } case SWITCH_EVENT_SHUTDOWN: new_sql() = switch_mprintf("delete from channels where hostname='%q';" "delete from interfaces where hostname='%q';" "delete from calls where hostname='%q'", - switch_core_get_hostname(), switch_core_get_hostname(), switch_core_get_hostname() + switch_core_get_switchname(), switch_core_get_switchname(), switch_core_get_switchname() ); break; case SWITCH_EVENT_LOG: @@ -1445,7 +1445,7 @@ static void core_event_handler(switch_event_t *event) switch_mprintf ("insert into interfaces (type,name,description,syntax,ikey,filename,hostname) values('%q','%q','%q','%q','%q','%q','%q')", type, name, switch_str_nil(description), switch_str_nil(syntax), switch_str_nil(key), switch_str_nil(filename), - switch_core_get_hostname() + switch_core_get_switchname() ); } break; @@ -1456,7 +1456,7 @@ static void core_event_handler(switch_event_t *event) const char *name = switch_event_get_header_nil(event, "name"); if (!zstr(type) && !zstr(name)) { new_sql() = switch_mprintf("delete from interfaces where type='%q' and name='%q' and hostname='%q'", type, name, - switch_core_get_hostname()); + switch_core_get_switchname()); } break; } @@ -1468,7 +1468,7 @@ static void core_event_handler(switch_event_t *event) break; } new_sql() = switch_mprintf("update channels set secure='%s' where uuid='%s' and hostname='%q'", - type, switch_event_get_header_nil(event, "caller-unique-id"), switch_core_get_hostname() + type, switch_event_get_header_nil(event, "caller-unique-id"), switch_core_get_switchname() ); break; } @@ -1479,12 +1479,12 @@ static void core_event_handler(switch_event_t *event) if (!strcmp("add", op)) { new_sql() = switch_mprintf("insert into nat (port, proto, sticky, hostname) values (%s, %s, %d,'%q')", switch_event_get_header_nil(event, "port"), - switch_event_get_header_nil(event, "proto"), sticky, switch_core_get_hostname() + switch_event_get_header_nil(event, "proto"), sticky, switch_core_get_switchname() ); } else if (!strcmp("del", op)) { new_sql() = switch_mprintf("delete from nat where port=%s and proto=%s and hostname='%q'", switch_event_get_header_nil(event, "port"), - switch_event_get_header_nil(event, "proto"), switch_core_get_hostname()); + switch_event_get_header_nil(event, "proto"), switch_core_get_switchname()); } else if (!strcmp("status", op)) { /* call show nat api */ } else if (!strcmp("status_response", op)) { @@ -1650,10 +1650,10 @@ SWITCH_DECLARE(switch_status_t) switch_core_add_registration(const char *user, c if (runtime.multiple_registrations) { sql = switch_mprintf("delete from registrations where hostname='%q' and (url='%q' or token='%q')", - switch_core_get_hostname(), url, switch_str_nil(token)); + switch_core_get_switchname(), url, switch_str_nil(token)); } else { sql = switch_mprintf("delete from registrations where reg_user='%q' and realm='%q' and hostname='%q'", - user, realm, switch_core_get_hostname()); + user, realm, switch_core_get_switchname()); } switch_queue_push(sql_manager.sql_queue[0], sql); @@ -1668,7 +1668,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_add_registration(const char *user, c switch_str_nil(network_ip), switch_str_nil(network_port), switch_str_nil(network_proto), - switch_core_get_hostname() + switch_core_get_switchname() ); @@ -1687,9 +1687,9 @@ SWITCH_DECLARE(switch_status_t) switch_core_del_registration(const char *user, c } if (!zstr(token) && runtime.multiple_registrations) { - sql = switch_mprintf("delete from registrations where reg_user='%q' and realm='%q' and hostname='%q' and token='%q'", user, realm, switch_core_get_hostname(), token); + sql = switch_mprintf("delete from registrations where reg_user='%q' and realm='%q' and hostname='%q' and token='%q'", user, realm, switch_core_get_switchname(), token); } else { - sql = switch_mprintf("delete from registrations where reg_user='%q' and realm='%q' and hostname='%q'", user, realm, switch_core_get_hostname()); + sql = switch_mprintf("delete from registrations where reg_user='%q' and realm='%q' and hostname='%q'", user, realm, switch_core_get_switchname()); } switch_queue_push(sql_manager.sql_queue[0], sql); @@ -1710,9 +1710,9 @@ SWITCH_DECLARE(switch_status_t) switch_core_expire_registration(int force) now = switch_epoch_time_now(NULL); if (force) { - sql = switch_mprintf("delete from registrations where hostname='%q'", switch_core_get_hostname()); + sql = switch_mprintf("delete from registrations where hostname='%q'", switch_core_get_switchname()); } else { - sql = switch_mprintf("delete from registrations where expires > 0 and expires <= %ld and hostname='%q'", now, switch_core_get_hostname()); + sql = switch_mprintf("delete from registrations where expires > 0 and expires <= %ld and hostname='%q'", now, switch_core_get_switchname()); } switch_queue_push(sql_manager.sql_queue[0], sql); @@ -1767,7 +1767,7 @@ switch_status_t switch_core_sqldb_start(switch_memory_pool_t *pool, switch_bool_ char sql[512] = ""; char *tables[] = { "channels", "calls", "interfaces", "tasks", NULL }; int i; - const char *hostname = switch_core_get_hostname(); + const char *hostname = switch_core_get_switchname(); for (i = 0; tables[i]; i++) { switch_snprintf(sql, sizeof(sql), "delete from %s where hostname='%s'", tables[i], hostname); diff --git a/src/switch_event.c b/src/switch_event.c index 9816834d37..8c48b61f7c 100644 --- a/src/switch_event.c +++ b/src/switch_event.c @@ -66,7 +66,6 @@ struct switch_event_subclass { #define MAX_DISPATCH_VAL 20 static unsigned int MAX_DISPATCH = MAX_DISPATCH_VAL; static unsigned int SOFT_MAX_DISPATCH = 0; -static char hostname[128] = ""; static char guess_ip_v4[80] = ""; static char guess_ip_v6[80] = ""; static switch_event_node_t *EVENT_NODES[SWITCH_EVENT_ALL + 1] = { NULL }; @@ -647,7 +646,6 @@ SWITCH_DECLARE(switch_status_t) switch_event_init(switch_memory_pool_t *pool) switch_mutex_unlock(EVENT_QUEUE_MUTEX); switch_threadattr_create(&thd_attr, pool); - gethostname(hostname, sizeof(hostname)); switch_find_local_ip(guess_ip_v4, sizeof(guess_ip_v4), NULL, AF_INET); switch_find_local_ip(guess_ip_v6, sizeof(guess_ip_v6), NULL, AF_INET6); @@ -1359,7 +1357,8 @@ SWITCH_DECLARE(void) switch_event_prep_for_delivery_detailed(const char *file, c switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Event-Name", switch_event_name(event->event_id)); switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Core-UUID", switch_core_get_uuid()); - switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "FreeSWITCH-Hostname", hostname); + switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "FreeSWITCH-Hostname", switch_core_get_hostname()); + switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "FreeSWITCH-Switchname", switch_core_get_switchname()); switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "FreeSWITCH-IPv4", guess_ip_v4); switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "FreeSWITCH-IPv6", guess_ip_v6);