mirror of
https://github.com/signalwire/freeswitch.git
synced 2025-03-15 05:08:26 +00:00
LOOK OUT BELOW... (FSCORE-381)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@14055 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
parent
2b8a04f1d7
commit
b6363dc294
@ -125,6 +125,7 @@ static const char *EVENT_NAMES[] = {
|
||||
"SEND_INFO",
|
||||
"RECV_INFO",
|
||||
"CALL_SECURE",
|
||||
"NAT",
|
||||
"ALL"
|
||||
};
|
||||
|
||||
|
@ -113,6 +113,7 @@ typedef enum {
|
||||
ESL_EVENT_SEND_INFO,
|
||||
ESL_EVENT_RECV_INFO,
|
||||
ESL_EVENT_CALL_SECURE,
|
||||
ESL_EVENT_NAT,
|
||||
ESL_EVENT_ALL
|
||||
} esl_event_types_t;
|
||||
|
||||
|
@ -64,13 +64,30 @@ SWITCH_DECLARE(void) switch_nat_init(switch_memory_pool_t *pool);
|
||||
*/
|
||||
SWITCH_DECLARE(void) switch_nat_shutdown(void);
|
||||
|
||||
/*!
|
||||
\brief Returns a list of nat mappings and other status info
|
||||
\note caller must free the string
|
||||
*/
|
||||
SWITCH_DECLARE(char *) switch_nat_status(void);
|
||||
|
||||
/*!
|
||||
\brief Republishes the nap mappings
|
||||
*/
|
||||
SWITCH_DECLARE(void) switch_nat_republish(void);
|
||||
|
||||
/*!
|
||||
\brief re-initializes NAT subsystem
|
||||
*/
|
||||
SWITCH_DECLARE(void) switch_nat_reinit(void);
|
||||
|
||||
/*!
|
||||
\brief Maps a port through the NAT Traversal System
|
||||
\param port Internal port to map
|
||||
\param proto Protocol
|
||||
\param external_port [out] Mapped external port
|
||||
\param sticky make the mapping permanent
|
||||
*/
|
||||
SWITCH_DECLARE(switch_status_t) switch_nat_add_mapping(switch_port_t port, switch_nat_ip_proto_t proto, switch_port_t *external_port);
|
||||
SWITCH_DECLARE(switch_status_t) switch_nat_add_mapping(switch_port_t port, switch_nat_ip_proto_t proto, switch_port_t *external_port, switch_bool_t sticky);
|
||||
/*!
|
||||
\brief Deletes a NAT mapping
|
||||
\param proto Protocol
|
||||
|
@ -1202,6 +1202,7 @@ typedef uint32_t switch_io_flag_t;
|
||||
SWITCH_EVENT_NOTIFY - Notification
|
||||
SWITCH_EVENT_SEND_MESSAGE - Message
|
||||
SWITCH_EVENT_RECV_MESSAGE - Message
|
||||
SWITCH_EVENT_NAT - NAT Management (new/del/status)
|
||||
SWITCH_EVENT_ALL - All events at once
|
||||
</pre>
|
||||
|
||||
@ -1274,6 +1275,7 @@ typedef enum {
|
||||
SWITCH_EVENT_SEND_INFO,
|
||||
SWITCH_EVENT_RECV_INFO,
|
||||
SWITCH_EVENT_CALL_SECURE,
|
||||
SWITCH_EVENT_NAT,
|
||||
SWITCH_EVENT_ALL
|
||||
} switch_event_types_t;
|
||||
|
||||
|
@ -31,6 +31,7 @@
|
||||
* Bret McDanel <trixter AT 0xdecafbad.com>
|
||||
* Cesar Cepeda <cesar@auronix.com>
|
||||
* Massimo Cetra <devel@navynet.it>
|
||||
* Rupa Schomaker <rupa@rupa.com>
|
||||
*
|
||||
*
|
||||
* mod_commands.c -- Misc. Command Module
|
||||
@ -47,9 +48,11 @@ SWITCH_MODULE_DEFINITION(mod_commands, mod_commands_load, mod_commands_shutdown,
|
||||
SWITCH_STANDARD_API(nat_map_function)
|
||||
{
|
||||
int argc;
|
||||
char *mydata = NULL, *argv[4];
|
||||
char *mydata = NULL, *argv[5];
|
||||
switch_nat_ip_proto_t proto = SWITCH_NAT_UDP;
|
||||
switch_port_t external_port = 0;
|
||||
char *tmp = NULL;
|
||||
switch_bool_t sticky = SWITCH_FALSE;
|
||||
|
||||
if (!cmd) {
|
||||
goto error;
|
||||
@ -60,6 +63,27 @@ SWITCH_STANDARD_API(nat_map_function)
|
||||
|
||||
argc = switch_separate_string(mydata, ' ', argv, (sizeof(argv) / sizeof(argv[0])));
|
||||
|
||||
if (argc < 1) {
|
||||
goto error;
|
||||
}
|
||||
if (argv[0] && switch_stristr("status", argv[0])) {
|
||||
tmp = switch_nat_status();
|
||||
stream->write_function(stream, tmp);
|
||||
switch_safe_free(tmp);
|
||||
goto ok;
|
||||
} else if (argv[0] && switch_stristr("republish", argv[0])) {
|
||||
switch_nat_republish();
|
||||
stream->write_function(stream, "true");
|
||||
goto ok;
|
||||
} else if (argv[0] && switch_stristr("reinit", argv[0])) {
|
||||
switch_nat_reinit();
|
||||
stream->write_function(stream, "true");
|
||||
tmp = switch_nat_status();
|
||||
stream->write_function(stream, tmp);
|
||||
switch_safe_free(tmp);
|
||||
goto ok;
|
||||
}
|
||||
|
||||
if (argc < 3) {
|
||||
goto error;
|
||||
}
|
||||
@ -70,8 +94,12 @@ SWITCH_STANDARD_API(nat_map_function)
|
||||
proto = SWITCH_NAT_UDP;
|
||||
}
|
||||
|
||||
if (argv[3] && switch_stristr("sticky", argv[3])) {
|
||||
sticky = SWITCH_TRUE;
|
||||
}
|
||||
|
||||
if (argv[0] && switch_stristr("add", argv[0])) {
|
||||
if (switch_nat_add_mapping((switch_port_t)atoi(argv[1]), proto, &external_port) == SWITCH_STATUS_SUCCESS) {
|
||||
if (switch_nat_add_mapping((switch_port_t)atoi(argv[1]), proto, &external_port, sticky) == SWITCH_STATUS_SUCCESS) {
|
||||
stream->write_function(stream, "%d", (int)external_port);
|
||||
goto ok;
|
||||
}
|
||||
@ -2700,7 +2728,7 @@ SWITCH_STANDARD_API(alias_function)
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
#define SHOW_SYNTAX "codec|endpoint|application|api|dialplan|file|timer|calls [count]|channels [count]|distinct_channels|aliases|complete|chat|endpoint|management|modules|say|interfaces|interface_types"
|
||||
#define SHOW_SYNTAX "codec|endpoint|application|api|dialplan|file|timer|calls [count]|channels [count]|distinct_channels|aliases|complete|chat|endpoint|management|modules|nat_map|say|interfaces|interface_types"
|
||||
SWITCH_STANDARD_API(show_function)
|
||||
{
|
||||
char sql[1024];
|
||||
@ -2844,6 +2872,18 @@ SWITCH_STANDARD_API(show_function)
|
||||
} else {
|
||||
switch_snprintf(sql, sizeof(sql) - 1, "select name, syntax, description, key from interfaces where type = 'api' order by name");
|
||||
}
|
||||
} else if (!strcasecmp(command, "nat_map")) {
|
||||
switch_snprintf(sql, sizeof(sql) - 1,
|
||||
"SELECT port, "
|
||||
" CASE proto "
|
||||
" WHEN 0 THEN 'udp' "
|
||||
" WHEN 1 THEN 'tcp' "
|
||||
" ELSE 'unknown' "
|
||||
" END AS proto, "
|
||||
" proto AS proto_num, "
|
||||
" sticky "
|
||||
" FROM nat ORDER BY port, proto"
|
||||
);
|
||||
} else {
|
||||
stream->write_function(stream, "-USAGE: %s\n", SHOW_SYNTAX);
|
||||
goto end;
|
||||
@ -2900,6 +2940,7 @@ SWITCH_STANDARD_API(show_function)
|
||||
|
||||
switch_xml_set_attr(switch_xml_set_flag(holder.xml, SWITCH_XML_DUP), strdup("row_count"), strdup(count));
|
||||
xmlstr = switch_xml_toxml(holder.xml, SWITCH_FALSE);
|
||||
switch_xml_free(holder.xml);
|
||||
|
||||
if (xmlstr) {
|
||||
holder.stream->write_function(holder.stream, "%s", xmlstr);
|
||||
@ -3537,7 +3578,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_commands_load)
|
||||
SWITCH_ADD_API(commands_api_interface, "stun", "stun", stun_function, "<stun_server>[:port]");
|
||||
SWITCH_ADD_API(commands_api_interface, "system", "Execute a system command", system_function, SYSTEM_SYNTAX);
|
||||
SWITCH_ADD_API(commands_api_interface, "time_test", "time_test", time_test_function, "<mss>");
|
||||
SWITCH_ADD_API(commands_api_interface, "nat_map", "nat_map", nat_map_function, "[add|del] <port> [tcp|udp]");
|
||||
SWITCH_ADD_API(commands_api_interface, "nat_map", "nat_map", nat_map_function, "[status|republish|reinit] | [add|del] <port> [tcp|udp] [static]");
|
||||
|
||||
/* indicate that the module should continue to be loaded */
|
||||
return SWITCH_STATUS_NOUNLOAD;
|
||||
|
@ -578,6 +578,34 @@ static void roster_event_handler(switch_event_t *event)
|
||||
|
||||
}
|
||||
|
||||
static void ipchanged_event_handler(switch_event_t *event)
|
||||
{
|
||||
const char *cond = switch_event_get_header(event, "condition");
|
||||
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "EVENT_TRAP: IP change detected\n");
|
||||
|
||||
if (cond && !strcmp(cond, "network-address-change")) {
|
||||
const char *old_ip4 = switch_event_get_header_nil(event, "network-address-previous-v4");
|
||||
const char *new_ip4 = switch_event_get_header_nil(event, "network-address-change-v4");
|
||||
switch_hash_index_t *hi;
|
||||
void *val;
|
||||
char *tmp;
|
||||
mdl_profile_t *profile;
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "IP change detected [%s]->[%s]\n", old_ip4, new_ip4);
|
||||
if (globals.profile_hash) {
|
||||
for (hi = switch_hash_first(NULL, globals.profile_hash); hi; hi = switch_hash_next(hi)) {
|
||||
switch_hash_this(hi, NULL, NULL, &val);
|
||||
profile = (mdl_profile_t *) val;
|
||||
if (!strcmp(profile->extip, old_ip4)) {
|
||||
tmp = profile->extip;
|
||||
profile->extip = strdup(new_ip4);
|
||||
switch_safe_free(tmp);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int so_callback(void *pArg, int argc, char **argv, char **columnNames)
|
||||
{
|
||||
mdl_profile_t *profile = (mdl_profile_t *) pArg;
|
||||
@ -845,7 +873,7 @@ static int activate_rtp(struct private_object *tech_pvt)
|
||||
if(globals.auto_nat && tech_pvt->profile->local_network &&
|
||||
!switch_check_network_list_ip(tech_pvt->remote_ip, tech_pvt->profile->local_network)) {
|
||||
switch_port_t external_port = 0;
|
||||
switch_nat_add_mapping((switch_port_t)tech_pvt->local_port, SWITCH_NAT_UDP, &external_port);
|
||||
switch_nat_add_mapping((switch_port_t)tech_pvt->local_port, SWITCH_NAT_UDP, &external_port, SWITCH_FALSE);
|
||||
tech_pvt->local_port = external_port;
|
||||
}
|
||||
|
||||
@ -1800,6 +1828,12 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_dingaling_load)
|
||||
return SWITCH_STATUS_GENERR;
|
||||
}
|
||||
|
||||
if (switch_event_bind(modname, SWITCH_EVENT_TRAP, SWITCH_EVENT_SUBCLASS_ANY, ipchanged_event_handler, NULL) != SWITCH_STATUS_SUCCESS) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't bind!\n");
|
||||
return SWITCH_STATUS_GENERR;
|
||||
}
|
||||
|
||||
|
||||
/* connect my internal structure to the blank pointer passed to me */
|
||||
*module_interface = switch_loadable_module_create_module_interface(pool, modname);
|
||||
dingaling_endpoint_interface = switch_loadable_module_create_interface(*module_interface, SWITCH_ENDPOINT_INTERFACE);
|
||||
|
@ -3157,6 +3157,8 @@ static void general_event_handler(switch_event_t *event)
|
||||
{
|
||||
const char *cond = switch_event_get_header(event, "condition");
|
||||
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "EVENT_TRAP: IP change detected\n");
|
||||
|
||||
if (cond && !strcmp(cond, "network-address-change") && mod_sofia_globals.auto_restart) {
|
||||
const char *old_ip4 = switch_event_get_header_nil(event, "network-address-previous-v4");
|
||||
const char *new_ip4 = switch_event_get_header_nil(event, "network-address-change-v4");
|
||||
|
@ -766,13 +766,13 @@ void *SWITCH_THREAD_FUNC sofia_profile_thread_run(switch_thread_t *thread, void
|
||||
);
|
||||
|
||||
if (sofia_test_pflag(profile, PFLAG_AUTO_NAT) && switch_core_get_variable("nat_type")) {
|
||||
if (switch_nat_add_mapping(profile->sip_port, SWITCH_NAT_UDP, NULL) == SWITCH_STATUS_SUCCESS) {
|
||||
if (switch_nat_add_mapping(profile->sip_port, SWITCH_NAT_UDP, NULL, SWITCH_FALSE) == SWITCH_STATUS_SUCCESS) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Created UDP nat mapping for %s port %d\n", profile->name, profile->sip_port);
|
||||
}
|
||||
if (switch_nat_add_mapping(profile->sip_port, SWITCH_NAT_TCP, NULL) == SWITCH_STATUS_SUCCESS) {
|
||||
if (switch_nat_add_mapping(profile->sip_port, SWITCH_NAT_TCP, NULL, SWITCH_FALSE) == SWITCH_STATUS_SUCCESS) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Created TCP nat mapping for %s port %d\n", profile->name, profile->sip_port);
|
||||
}
|
||||
if(sofia_test_pflag(profile, PFLAG_TLS) && switch_nat_add_mapping(profile->tls_sip_port, SWITCH_NAT_TCP, NULL) == SWITCH_STATUS_SUCCESS) {
|
||||
if(sofia_test_pflag(profile, PFLAG_TLS) && switch_nat_add_mapping(profile->tls_sip_port, SWITCH_NAT_TCP, NULL, SWITCH_FALSE) == SWITCH_STATUS_SUCCESS) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Created TCP/TLS nat mapping for %s port %d\n", profile->name, profile->tls_sip_port);
|
||||
}
|
||||
}
|
||||
|
@ -672,7 +672,7 @@ switch_status_t sofia_glue_tech_choose_port(private_object_t *tech_pvt, int forc
|
||||
|
||||
if (tech_pvt->profile->extrtpip && sofia_glue_check_nat(tech_pvt->profile, tech_pvt->remote_ip)) {
|
||||
tech_pvt->adv_sdp_audio_ip = switch_core_session_strdup(tech_pvt->session, tech_pvt->profile->extrtpip);
|
||||
switch_nat_add_mapping((switch_port_t)sdp_port, SWITCH_NAT_UDP, &external_port);
|
||||
switch_nat_add_mapping((switch_port_t)sdp_port, SWITCH_NAT_UDP, &external_port, SWITCH_FALSE);
|
||||
} else {
|
||||
tech_pvt->adv_sdp_audio_ip = switch_core_session_strdup(tech_pvt->session, ip);
|
||||
}
|
||||
@ -719,7 +719,7 @@ switch_status_t sofia_glue_tech_choose_video_port(private_object_t *tech_pvt, in
|
||||
}
|
||||
|
||||
if (sofia_glue_check_nat(tech_pvt->profile, tech_pvt->remote_ip)) {
|
||||
switch_nat_add_mapping((switch_port_t)sdp_port, SWITCH_NAT_UDP, &external_port);
|
||||
switch_nat_add_mapping((switch_port_t)sdp_port, SWITCH_NAT_UDP, &external_port, SWITCH_FALSE);
|
||||
}
|
||||
|
||||
tech_pvt->adv_sdp_video_port = external_port != 0 ? external_port : sdp_port;
|
||||
|
@ -2291,7 +2291,7 @@ SWITCH_MODULE_RUNTIME_FUNCTION(mod_event_socket_runtime)
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Socket up listening on %s:%u\n", prefs.ip, prefs.port);
|
||||
|
||||
if (prefs.nat_map) {
|
||||
switch_nat_add_mapping(prefs.port, SWITCH_NAT_TCP, NULL);
|
||||
switch_nat_add_mapping(prefs.port, SWITCH_NAT_TCP, NULL, SWITCH_FALSE);
|
||||
}
|
||||
|
||||
break;
|
||||
|
@ -402,6 +402,28 @@ static void core_event_handler(switch_event_t *event)
|
||||
switch_event_get_header_nil(event, "caller-unique-id"));
|
||||
break;
|
||||
}
|
||||
case SWITCH_EVENT_NAT:
|
||||
{
|
||||
const char *op = switch_event_get_header_nil(event, "op");
|
||||
switch_bool_t sticky = switch_true(switch_event_get_header_nil(event, "sticky"));
|
||||
if (!strcmp("add", op)) {
|
||||
sql = switch_mprintf("insert into nat (port, proto, sticky) values (%s, %s, %d)",
|
||||
switch_event_get_header_nil(event, "port"),
|
||||
switch_event_get_header_nil(event, "proto"),
|
||||
sticky);
|
||||
} else if (!strcmp("del", op)) {
|
||||
sql = switch_mprintf("delete from nat where port=%s and proto=%s",
|
||||
switch_event_get_header_nil(event, "port"),
|
||||
switch_event_get_header_nil(event, "proto"));
|
||||
} else if (!strcmp("status", op)) {
|
||||
/* call show nat api */
|
||||
} else if (!strcmp("status_response", op)) {
|
||||
/* ignore */
|
||||
} else {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unknown op for SWITCH_EVENT_NAT: %s\n", op);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@ -506,6 +528,12 @@ void switch_core_sqldb_start(switch_memory_pool_t *pool)
|
||||
" task_group VARCHAR(255),\n"
|
||||
" task_sql_manager INTEGER(8)\n"
|
||||
");\n";
|
||||
char create_nat_sql[] =
|
||||
"CREATE TABLE nat (\n"
|
||||
" sticky INTEGER,\n"
|
||||
" port INTEGER,\n"
|
||||
" proto INTEGER\n"
|
||||
");\n";
|
||||
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Opening DB\n");
|
||||
switch_core_db_exec(sql_manager.db, "drop table channels", NULL, NULL, NULL);
|
||||
@ -519,8 +547,10 @@ void switch_core_sqldb_start(switch_memory_pool_t *pool)
|
||||
|
||||
switch_core_db_test_reactive(sql_manager.db, "select sticky from complete", "DROP TABLE complete", create_complete_sql);
|
||||
switch_core_db_test_reactive(sql_manager.db, "select sticky from aliases", "DROP TABLE aliases", create_alias_sql);
|
||||
switch_core_db_test_reactive(sql_manager.db, "select sticky from nat", "DROP TABLE nat", create_nat_sql);
|
||||
switch_core_db_exec(sql_manager.db, "delete from complete where sticky=0", NULL, NULL, NULL);
|
||||
switch_core_db_exec(sql_manager.db, "delete from aliases where sticky=0", NULL, NULL, NULL);
|
||||
switch_core_db_exec(sql_manager.db, "delete from nat where sticky=0", NULL, NULL, NULL);
|
||||
switch_core_db_exec(sql_manager.db, "create index if not exists alias1 on aliases (alias)", NULL, NULL, NULL);
|
||||
switch_core_db_exec(sql_manager.db, "create index if not exists complete1 on complete (a1)", NULL, NULL, NULL);
|
||||
switch_core_db_exec(sql_manager.db, "create index if not exists complete2 on complete (a2)", NULL, NULL, NULL);
|
||||
@ -532,6 +562,7 @@ void switch_core_sqldb_start(switch_memory_pool_t *pool)
|
||||
switch_core_db_exec(sql_manager.db, "create index if not exists complete8 on complete (a8)", NULL, NULL, NULL);
|
||||
switch_core_db_exec(sql_manager.db, "create index if not exists complete9 on complete (a9)", NULL, NULL, NULL);
|
||||
switch_core_db_exec(sql_manager.db, "create index if not exists complete10 on complete (a10)", NULL, NULL, NULL);
|
||||
switch_core_db_exec(sql_manager.db, "create unique index if not exists nat_map_port_proto on nat (port,proto)", NULL, NULL, NULL);
|
||||
switch_core_db_exec(sql_manager.db, create_channels_sql, NULL, NULL, NULL);
|
||||
switch_core_db_exec(sql_manager.db, create_calls_sql, NULL, NULL, NULL);
|
||||
switch_core_db_exec(sql_manager.db, create_interfaces_sql, NULL, NULL, NULL);
|
||||
|
@ -179,6 +179,7 @@ static char *EVENT_NAMES[] = {
|
||||
"SEND_INFO",
|
||||
"RECV_INFO",
|
||||
"CALL_SECURE",
|
||||
"NAT",
|
||||
"ALL"
|
||||
};
|
||||
|
||||
|
345
src/switch_nat.c
345
src/switch_nat.c
@ -25,6 +25,7 @@
|
||||
*
|
||||
* Anthony Minessale II <anthm@freeswitch.org>
|
||||
* Brian K. West <brian@freeswitch.org>
|
||||
* Rupa Schomaker <rupa@rupa.com>
|
||||
*
|
||||
*
|
||||
* switch_nat.c NAT Traversal via NAT-PMP or uPNP
|
||||
@ -38,18 +39,44 @@
|
||||
#include "../libs/miniupnpc/upnperrors.h"
|
||||
#include "../libs/libnatpmp/natpmp.h"
|
||||
|
||||
#define MULTICAST_BUFFSIZE 65536
|
||||
|
||||
typedef struct {
|
||||
switch_memory_pool_t *pool;
|
||||
switch_nat_type_t nat_type;
|
||||
struct UPNPUrls urls;
|
||||
struct IGDdatas data;
|
||||
char *descURL;
|
||||
char pub_addr[16];
|
||||
char pvt_addr[16];
|
||||
|
||||
} nat_globals_t;
|
||||
|
||||
static nat_globals_t nat_globals;
|
||||
|
||||
typedef struct {
|
||||
switch_memory_pool_t *pool;
|
||||
int running;
|
||||
switch_sockaddr_t *maddress;
|
||||
switch_socket_t *msocket;
|
||||
} nat_globals_perm_t;
|
||||
|
||||
static nat_globals_perm_t nat_globals_perm;
|
||||
|
||||
static switch_bool_t first_init = SWITCH_TRUE;
|
||||
|
||||
static switch_status_t get_upnp_pubaddr(char *pub_addr)
|
||||
{
|
||||
if (UPNP_GetExternalIPAddress(nat_globals.urls.controlURL,
|
||||
nat_globals.data.servicetype,
|
||||
pub_addr) == UPNPCOMMAND_SUCCESS) {
|
||||
if (!strcmp(pub_addr, "0.0.0.0") || switch_strlen_zero(pub_addr)) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,
|
||||
"uPNP Device (url: %s) returned an invalid external address of '%s'. Disabling uPNP\n", nat_globals.urls.controlURL, pub_addr);
|
||||
return SWITCH_STATUS_GENERR;
|
||||
}
|
||||
}
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
static int init_upnp (void)
|
||||
{
|
||||
struct UPNPDev *devlist;
|
||||
@ -58,7 +85,6 @@ static int init_upnp (void)
|
||||
int descXMLsize = 0;
|
||||
const char *multicastif = 0;
|
||||
const char *minissdpdpath = 0;
|
||||
int r = -2;
|
||||
|
||||
memset(&nat_globals.urls, 0, sizeof(struct UPNPUrls));
|
||||
memset(&nat_globals.data, 0, sizeof(struct IGDdatas));
|
||||
@ -79,6 +105,8 @@ static int init_upnp (void)
|
||||
|
||||
descXML = miniwget(dev->descURL, &descXMLsize);
|
||||
|
||||
nat_globals.descURL = strdup(dev->descURL);
|
||||
|
||||
if (descXML) {
|
||||
parserootdesc (descXML, descXMLsize, &nat_globals.data);
|
||||
free (descXML); descXML = 0;
|
||||
@ -88,16 +116,7 @@ static int init_upnp (void)
|
||||
freeUPNPDevlist(devlist);
|
||||
}
|
||||
|
||||
if ((r = UPNP_GetExternalIPAddress(nat_globals.urls.controlURL,
|
||||
nat_globals.data.servicetype,
|
||||
nat_globals.pub_addr)) == UPNPCOMMAND_SUCCESS) {
|
||||
|
||||
if (!strcmp(nat_globals.pub_addr, "0.0.0.0")) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR,
|
||||
"uPNP Device (url: %s) returned an invalid external address of 0.0.0.0. Disabling uPNP\n", nat_globals.urls.controlURL);
|
||||
return -2;
|
||||
}
|
||||
|
||||
if (get_upnp_pubaddr(nat_globals.pub_addr) == SWITCH_STATUS_SUCCESS) {
|
||||
nat_globals.nat_type = SWITCH_NAT_TYPE_UPNP;
|
||||
return 0;
|
||||
}
|
||||
@ -105,7 +124,7 @@ static int init_upnp (void)
|
||||
return -2;
|
||||
}
|
||||
|
||||
static int init_pmp(void)
|
||||
static int get_pmp_pubaddr(char *pub_addr)
|
||||
{
|
||||
int r = 0, i = 0, max = 5;
|
||||
natpmpresp_t response;
|
||||
@ -138,7 +157,7 @@ static int init_pmp(void)
|
||||
}
|
||||
|
||||
pubaddr = inet_ntoa(response.pnu.publicaddress.addr);
|
||||
switch_set_string(nat_globals.pub_addr, pubaddr);
|
||||
switch_set_string(pub_addr, pubaddr);
|
||||
nat_globals.nat_type = SWITCH_NAT_TYPE_PMP;
|
||||
|
||||
closenatpmp(&natpmp);
|
||||
@ -148,10 +167,198 @@ static int init_pmp(void)
|
||||
return r;
|
||||
}
|
||||
|
||||
static int init_pmp(void)
|
||||
{
|
||||
return get_pmp_pubaddr(nat_globals.pub_addr);
|
||||
}
|
||||
|
||||
SWITCH_DECLARE(void) switch_nat_reinit(void)
|
||||
{
|
||||
switch_nat_init(nat_globals_perm.pool);
|
||||
}
|
||||
|
||||
switch_status_t init_nat_monitor(switch_memory_pool_t *pool)
|
||||
{
|
||||
char *addr = NULL;
|
||||
switch_port_t port = 0;
|
||||
|
||||
if (nat_globals.nat_type == SWITCH_NAT_TYPE_UPNP) {
|
||||
addr = "239.255.255.250";
|
||||
port = 1900;
|
||||
} else if (nat_globals.nat_type == SWITCH_NAT_TYPE_PMP) {
|
||||
addr = "224.0.0.1";
|
||||
port = 5350;
|
||||
}
|
||||
|
||||
if (switch_sockaddr_info_get(&nat_globals_perm.maddress, addr, SWITCH_UNSPEC, port, 0, pool) != SWITCH_STATUS_SUCCESS) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot find address\n");
|
||||
return SWITCH_STATUS_TERM;
|
||||
}
|
||||
|
||||
if (switch_socket_create(&nat_globals_perm.msocket, AF_INET, SOCK_DGRAM, 0, pool) != SWITCH_STATUS_SUCCESS) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Socket Error\n");
|
||||
return SWITCH_STATUS_TERM;
|
||||
}
|
||||
|
||||
if (switch_socket_opt_set(nat_globals_perm.msocket, SWITCH_SO_REUSEADDR, 1) != SWITCH_STATUS_SUCCESS) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Socket Option Error\n");
|
||||
switch_socket_close(nat_globals_perm.msocket);
|
||||
return SWITCH_STATUS_TERM;
|
||||
}
|
||||
|
||||
if (switch_mcast_join(nat_globals_perm.msocket, nat_globals_perm.maddress, NULL, NULL) != SWITCH_STATUS_SUCCESS) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Multicast Error\n");
|
||||
switch_socket_close(nat_globals_perm.msocket);
|
||||
return SWITCH_STATUS_TERM;
|
||||
}
|
||||
|
||||
if (switch_socket_bind(nat_globals_perm.msocket, nat_globals_perm.maddress) != SWITCH_STATUS_SUCCESS) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Bind Error\n");
|
||||
switch_socket_close(nat_globals_perm.msocket);
|
||||
return SWITCH_STATUS_TERM;
|
||||
}
|
||||
|
||||
switch_socket_opt_set(nat_globals_perm.msocket, SWITCH_SO_NONBLOCK, TRUE);
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "NAT thread configured\n");
|
||||
return SWITCH_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
static void *SWITCH_THREAD_FUNC switch_nat_multicast_runtime(switch_thread_t *thread, void *obj)
|
||||
{
|
||||
char *buf = NULL;
|
||||
char newip[16];
|
||||
switch_event_t *event = NULL;
|
||||
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "NAT thread started\n");
|
||||
|
||||
buf = (char *) malloc(MULTICAST_BUFFSIZE);
|
||||
switch_assert(buf);
|
||||
nat_globals_perm.running = 1;
|
||||
|
||||
while (nat_globals_perm.running == 1) {
|
||||
size_t len = MULTICAST_BUFFSIZE;
|
||||
switch_status_t status;
|
||||
switch_bool_t do_repub = SWITCH_FALSE;
|
||||
memset(buf, 0, len);
|
||||
|
||||
status = switch_socket_recvfrom(nat_globals_perm.maddress, nat_globals_perm.msocket, 0, buf, &len);
|
||||
|
||||
if (!len) {
|
||||
if (SWITCH_STATUS_IS_BREAK(status)) {
|
||||
switch_yield(5000000);
|
||||
continue;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (nat_globals.nat_type == SWITCH_NAT_TYPE_UPNP) {
|
||||
/* look for our desc URL and servicetype in the packet */
|
||||
if (strstr(buf, nat_globals.descURL) && strstr(buf, nat_globals.data.servicetype)) {
|
||||
if (strstr(buf, "NTS: ssdp:alive")) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "got UPnP keep alive packet: \n%s\n", buf);
|
||||
/* did pub ip change */
|
||||
if (get_upnp_pubaddr(newip) != SWITCH_STATUS_SUCCESS) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Unable to get current pubaddr after receiving UPnP keep alive packet.\n");
|
||||
}
|
||||
} else if (strstr(buf, "NTS: ssdp:byebye")) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "got UPnP signoff packet. Your NAT gateway is probably going offline.\n");
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "got UPnP signoff packet: \n%s\n", buf);
|
||||
} else {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "got UNKNOWN UPnP keep alive packet: \n%s\n", buf);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
/* got some data in NAT-PMP mode, treat any data as a republish event */
|
||||
if (get_pmp_pubaddr(newip) < 0) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Unable to get current pubaddr after receiving UPnP keep alive packet.\n");
|
||||
}
|
||||
}
|
||||
|
||||
if ((strlen(newip) > 0) && strcmp(newip, "0.0.0.0") && strcmp(newip, nat_globals.pub_addr)) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Public IP changed from '%s' to '%s'.\n", nat_globals.pub_addr, newip);
|
||||
do_repub = SWITCH_TRUE;
|
||||
|
||||
switch_event_create(&event, SWITCH_EVENT_TRAP);
|
||||
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "condition", "network-address-change");
|
||||
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "network-address-previous-v4", nat_globals.pub_addr);
|
||||
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "network-address-change-v4", newip);
|
||||
switch_event_fire(&event);
|
||||
|
||||
switch_set_string(nat_globals.pub_addr, newip);
|
||||
switch_nat_reinit();
|
||||
}
|
||||
|
||||
if (do_repub) {
|
||||
switch_nat_republish();
|
||||
}
|
||||
}
|
||||
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "NAT thread ending\n");
|
||||
nat_globals_perm.running = 0;
|
||||
|
||||
switch_safe_free(buf);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
switch_thread_t *nat_thread_p = NULL;
|
||||
|
||||
SWITCH_DECLARE(void) switch_nat_thread_start(void)
|
||||
{
|
||||
|
||||
switch_threadattr_t *thd_attr;
|
||||
|
||||
if (init_nat_monitor(nat_globals_perm.pool) != SWITCH_STATUS_SUCCESS) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unable to initialize NAT thread\n");
|
||||
return;
|
||||
}
|
||||
|
||||
switch_threadattr_create(&thd_attr, nat_globals_perm.pool);
|
||||
switch_threadattr_detach_set(thd_attr, 1);
|
||||
switch_thread_create(&nat_thread_p, thd_attr, switch_nat_multicast_runtime, NULL, nat_globals_perm.pool);
|
||||
}
|
||||
|
||||
SWITCH_DECLARE(void) switch_nat_thread_stop(void)
|
||||
{
|
||||
/* don't do anything if no thread ptr */
|
||||
if (!nat_thread_p ) {
|
||||
return;
|
||||
}
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Stopping NAT Task Thread\n");
|
||||
if (nat_globals_perm.running == 1) {
|
||||
int sanity = 0;
|
||||
switch_status_t st;
|
||||
|
||||
nat_globals_perm.running = -1;
|
||||
|
||||
switch_thread_join(&st, nat_thread_p);
|
||||
|
||||
while (nat_globals_perm.running) {
|
||||
switch_yield(1000000); /* can take up to 5s for the thread to terminate, so wait for 10 */
|
||||
if (++sanity > 10) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Timed out waiting for NAT Task Thread to stop\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
nat_thread_p = NULL;
|
||||
}
|
||||
|
||||
|
||||
SWITCH_DECLARE(void) switch_nat_init(switch_memory_pool_t *pool)
|
||||
{
|
||||
/* try free dynamic data structures prior to resetting to 0 */
|
||||
FreeUPNPUrls(&nat_globals.urls);
|
||||
switch_safe_free(nat_globals.descURL);
|
||||
|
||||
memset(&nat_globals, 0, sizeof(nat_globals));
|
||||
nat_globals.pool = pool;
|
||||
|
||||
if (first_init) {
|
||||
memset(&nat_globals_perm, 0, sizeof(nat_globals_perm));
|
||||
nat_globals_perm.pool = pool;
|
||||
}
|
||||
|
||||
switch_find_local_ip(nat_globals.pvt_addr, sizeof(nat_globals.pvt_addr), NULL, AF_INET);
|
||||
|
||||
@ -169,10 +376,15 @@ SWITCH_DECLARE(void) switch_nat_init(switch_memory_pool_t *pool)
|
||||
switch_core_set_variable("nat_public_addr", nat_globals.pub_addr);
|
||||
switch_core_set_variable("nat_private_addr", nat_globals.pvt_addr);
|
||||
switch_core_set_variable("nat_type", nat_globals.nat_type == SWITCH_NAT_TYPE_PMP ? "pmp" : "upnp");
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "NAT detected type: %s\n", nat_globals.nat_type == SWITCH_NAT_TYPE_PMP ? "pmp" : "upnp");
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "NAT detected type: %s, ExtIP: '%s'\n", nat_globals.nat_type == SWITCH_NAT_TYPE_PMP ? "pmp" : "upnp", nat_globals.pub_addr);
|
||||
|
||||
if (!nat_thread_p) {
|
||||
switch_nat_thread_start();
|
||||
}
|
||||
} else {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "No PMP or UPnP NAT detected!\n");
|
||||
}
|
||||
first_init = SWITCH_FALSE;
|
||||
}
|
||||
|
||||
static switch_status_t switch_nat_add_mapping_pmp(switch_port_t port, switch_nat_ip_proto_t proto, switch_port_t *external_port)
|
||||
@ -310,9 +522,11 @@ static switch_status_t switch_nat_del_mapping_upnp(switch_port_t port, switch_na
|
||||
return status;
|
||||
}
|
||||
|
||||
SWITCH_DECLARE(switch_status_t) switch_nat_add_mapping(switch_port_t port, switch_nat_ip_proto_t proto, switch_port_t *external_port)
|
||||
SWITCH_DECLARE(switch_status_t) switch_nat_add_mapping_internal(switch_port_t port, switch_nat_ip_proto_t proto, switch_port_t *external_port, switch_bool_t sticky, switch_bool_t publish)
|
||||
{
|
||||
switch_status_t status = SWITCH_STATUS_FALSE;
|
||||
switch_event_t *event = NULL;
|
||||
char key[1024] = "";
|
||||
|
||||
switch (nat_globals.nat_type) {
|
||||
case SWITCH_NAT_TYPE_PMP:
|
||||
@ -329,12 +543,30 @@ SWITCH_DECLARE(switch_status_t) switch_nat_add_mapping(switch_port_t port, switc
|
||||
break;
|
||||
}
|
||||
|
||||
if (publish && status == SWITCH_STATUS_SUCCESS) {
|
||||
switch_event_create(&event, SWITCH_EVENT_NAT);
|
||||
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "op", "add");
|
||||
switch_snprintf(key, sizeof(key), "%d", port);
|
||||
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "port", key);
|
||||
switch_snprintf(key, sizeof(key), "%d", proto);
|
||||
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "proto", key);
|
||||
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "sticky", (sticky ? "true" : "false"));
|
||||
switch_event_fire(&event);
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
SWITCH_DECLARE(switch_status_t) switch_nat_add_mapping(switch_port_t port, switch_nat_ip_proto_t proto, switch_port_t *external_port, switch_bool_t sticky)
|
||||
{
|
||||
return switch_nat_add_mapping_internal(port, proto, external_port, sticky, SWITCH_TRUE);
|
||||
}
|
||||
|
||||
SWITCH_DECLARE(switch_status_t) switch_nat_del_mapping(switch_port_t port, switch_nat_ip_proto_t proto)
|
||||
{
|
||||
switch_status_t status = SWITCH_STATUS_FALSE;
|
||||
switch_event_t *event = NULL;
|
||||
char key[1024] = "";
|
||||
|
||||
switch (nat_globals.nat_type) {
|
||||
case SWITCH_NAT_TYPE_PMP:
|
||||
@ -347,12 +579,87 @@ SWITCH_DECLARE(switch_status_t) switch_nat_del_mapping(switch_port_t port, switc
|
||||
break;
|
||||
}
|
||||
|
||||
if (status == SWITCH_STATUS_SUCCESS) {
|
||||
switch_event_create(&event, SWITCH_EVENT_NAT);
|
||||
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "op", "del");
|
||||
switch_snprintf(key, sizeof(key), "%d", port);
|
||||
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "port", key);
|
||||
switch_snprintf(key, sizeof(key), "%d", proto);
|
||||
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "proto", key);
|
||||
switch_event_fire(&event);
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
SWITCH_DECLARE(void) switch_nat_republish(void)
|
||||
{
|
||||
switch_xml_t natxml = NULL;
|
||||
switch_xml_t row = NULL;
|
||||
switch_xml_t child = NULL;
|
||||
switch_stream_handle_t stream = { 0 };
|
||||
SWITCH_STANDARD_STREAM(stream);
|
||||
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Refreshing nat maps\n");
|
||||
|
||||
switch_api_execute("show", "nat_map as xml", NULL, &stream);
|
||||
|
||||
if (!(natxml = switch_xml_parse_str_dup(stream.data))) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unable to parse XML: %s\n", (char *) stream.data);
|
||||
switch_safe_free(stream.data);
|
||||
return;
|
||||
}
|
||||
|
||||
/* iterate the xml and publish the mappings */
|
||||
row = switch_xml_find_child(natxml, "row", "row_id", "1");
|
||||
while (row != NULL) {
|
||||
char *sport = NULL;
|
||||
char *sproto = NULL;
|
||||
switch_port_t port;
|
||||
switch_nat_ip_proto_t proto;
|
||||
|
||||
if ((child = switch_xml_child(row, "port"))) {
|
||||
sport = child->txt;
|
||||
}
|
||||
if ((child = switch_xml_child(row, "proto_num"))) {
|
||||
sproto = child->txt;
|
||||
}
|
||||
|
||||
if (sport && sproto) {
|
||||
port = (switch_port_t)(atoi(sport));
|
||||
proto = (switch_nat_ip_proto_t)(atoi(sproto));
|
||||
switch_nat_add_mapping_internal(port, proto, NULL, SWITCH_FALSE, SWITCH_FALSE);
|
||||
} else {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unable to parse port/proto info: XML: %s\n", (char *) stream.data);
|
||||
}
|
||||
|
||||
row = switch_xml_next(row);
|
||||
}
|
||||
|
||||
switch_safe_free(stream.data);
|
||||
switch_xml_free(natxml);
|
||||
}
|
||||
|
||||
SWITCH_DECLARE(char *) switch_nat_status(void)
|
||||
{
|
||||
switch_stream_handle_t stream = { 0 };
|
||||
SWITCH_STANDARD_STREAM(stream);
|
||||
|
||||
stream.write_function(&stream, "Nat Type: %s, ExtIP: %s\n",
|
||||
(nat_globals.nat_type == SWITCH_NAT_TYPE_UPNP) ? "UPNP" : (nat_globals.nat_type == SWITCH_NAT_TYPE_PMP ? "NAT-PMP" : "UNKNOWN"),
|
||||
nat_globals.pub_addr);
|
||||
|
||||
switch_api_execute("show", "nat_map", NULL, &stream);
|
||||
|
||||
return stream.data; /* caller frees */
|
||||
}
|
||||
|
||||
|
||||
SWITCH_DECLARE(void) switch_nat_shutdown(void)
|
||||
{
|
||||
|
||||
switch_nat_thread_stop();
|
||||
FreeUPNPUrls(&nat_globals.urls);
|
||||
switch_safe_free(nat_globals.descURL);
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user