FreeSWITCH API Documentation  1.7.0
Enumerations | Enumerator | Functions
NAT code
+ Collaboration diagram for NAT code:

Enumerations

enum  switch_nat_type_t { SWITCH_NAT_TYPE_NONE, SWITCH_NAT_TYPE_PMP, SWITCH_NAT_TYPE_UPNP }
 
enum  switch_nat_ip_proto_t { SWITCH_NAT_UDP, SWITCH_NAT_TCP }
 

Functions

const char * switch_nat_get_type (void)
 
void switch_nat_init (switch_memory_pool_t *pool, switch_bool_t mapping)
 Initilize the NAT Traversal System. More...
 
void switch_nat_late_init (void)
 Initilize the rest of the NAT Traversal System. More...
 
void switch_nat_shutdown (void)
 Shuts down the NAT Traversal System. More...
 
char * switch_nat_status (void)
 Returns a list of nat mappings and other status info. More...
 
void switch_nat_republish (void)
 Republishes the nap mappings. More...
 
void switch_nat_reinit (void)
 re-initializes NAT subsystem More...
 
void switch_nat_set_mapping (switch_bool_t mapping)
 Update the setting if port mapping will be created. More...
 
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)
 Maps a port through the NAT Traversal System. More...
 
switch_bool_t switch_nat_is_initialized (void)
 Has the NAT subsystem been initialized. More...
 
switch_status_t switch_nat_del_mapping (switch_port_t port, switch_nat_ip_proto_t proto)
 Deletes a NAT mapping. More...
 

Detailed Description

Enumeration Type Documentation

Enumerator
SWITCH_NAT_UDP 
SWITCH_NAT_TCP 

Definition at line 47 of file switch_nat.h.

Enumerator
SWITCH_NAT_TYPE_NONE 
SWITCH_NAT_TYPE_PMP 
SWITCH_NAT_TYPE_UPNP 

Definition at line 41 of file switch_nat.h.

Function Documentation

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 
)

Maps a port through the NAT Traversal System.

Parameters
portInternal port to map
protoProtocol
external_port[out] Mapped external port
stickymake the mapping permanent

Definition at line 619 of file switch_nat.c.

References switch_nat_add_mapping_internal(), and SWITCH_TRUE.

Referenced by switch_core_media_choose_port().

621 {
622  return switch_nat_add_mapping_internal(port, proto, external_port, sticky, SWITCH_TRUE);
623 }
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)
Definition: switch_nat.c:580
switch_status_t switch_nat_del_mapping ( switch_port_t  port,
switch_nat_ip_proto_t  proto 
)

Deletes a NAT mapping.

Parameters
protoProtocol

Definition at line 625 of file switch_nat.c.

References nat_globals_t::nat_type, switch_event_add_header(), switch_event_add_header_string(), switch_event_create, switch_event_fire, SWITCH_EVENT_NAT, switch_nat_del_mapping_pmp(), switch_nat_del_mapping_upnp(), SWITCH_NAT_TYPE_PMP, SWITCH_NAT_TYPE_UPNP, SWITCH_STACK_BOTTOM, SWITCH_STATUS_FALSE, and SWITCH_STATUS_SUCCESS.

Referenced by switch_core_media_deactivate_rtp().

626 {
628  switch_event_t *event = NULL;
629 
630  switch (nat_globals.nat_type) {
631  case SWITCH_NAT_TYPE_PMP:
632  status = switch_nat_del_mapping_pmp(port, proto);
633  break;
635  status = switch_nat_del_mapping_upnp(port, proto);
636  break;
637  default:
638  break;
639  }
640 
641  if (status == SWITCH_STATUS_SUCCESS) {
644  switch_event_add_header(event, SWITCH_STACK_BOTTOM, "port", "%d", port);
645  switch_event_add_header(event, SWITCH_STACK_BOTTOM, "proto", "%d", proto);
646  switch_event_fire(&event);
647  }
648 
649  return status;
650 }
#define switch_event_fire(event)
Fire an event filling in most of the arguements with obvious values.
Definition: switch_event.h:412
switch_status_t switch_event_add_header(switch_event_t *event, switch_stack_t stack, const char *header_name, const char *fmt,...) PRINTF_FUNCTION(4
Add a header to an event.
Representation of an event.
Definition: switch_event.h:80
switch_status_t switch_event_add_header_string(switch_event_t *event, switch_stack_t stack, const char *header_name, const char *data)
Add a string header to an event.
static switch_status_t switch_nat_del_mapping_upnp(switch_port_t port, switch_nat_ip_proto_t proto)
Definition: switch_nat.c:553
switch_nat_type_t nat_type
Definition: switch_nat.c:47
static switch_status_t switch_nat_del_mapping_pmp(switch_port_t port, switch_nat_ip_proto_t proto)
Definition: switch_nat.c:516
switch_status_t
Common return values.
#define switch_event_create(event, id)
Create a new event assuming it will not be custom event and therefore hiding the unused parameters...
Definition: switch_event.h:383
static nat_globals_t nat_globals
Definition: switch_nat.c:57
const char* switch_nat_get_type ( void  )

Definition at line 575 of file switch_nat.c.

References nat_globals_t::nat_type_str.

576 {
577  return nat_globals.nat_type_str;
578 }
char nat_type_str[5]
Definition: switch_nat.c:48
static nat_globals_t nat_globals
Definition: switch_nat.c:57
void switch_nat_init ( switch_memory_pool_t pool,
switch_bool_t  mapping 
)

Initilize the NAT Traversal System.

Parameters
poolthe memory pool to use for long term allocations
Note
Generally called by the core_init

Definition at line 399 of file switch_nat.c.

References nat_globals_t::descURL, first_init, init_pmp(), init_upnp(), initialized, nat_globals_t::mapping, memset(), nat_thread_p, nat_globals_t::nat_type, nat_globals_t::nat_type_str, pool, nat_globals_perm_t::pool, nat_globals_t::pub_addr, nat_globals_t::pvt_addr, SWITCH_CHANNEL_LOG, switch_core_set_variable(), SWITCH_FALSE, switch_find_local_ip(), SWITCH_LOG_DEBUG, SWITCH_LOG_INFO, switch_log_printf(), switch_nat_thread_start(), SWITCH_NAT_TYPE_PMP, switch_safe_free, SWITCH_TRUE, and nat_globals_t::urls.

Referenced by switch_core_init(), and switch_nat_reinit().

400 {
401  /* try free dynamic data structures prior to resetting to 0 */
402  FreeUPNPUrls(&nat_globals.urls);
404 
405  memset(&nat_globals, 0, sizeof(nat_globals));
406 
407  if (first_init) {
410  }
411 
412  nat_globals.mapping = mapping;
413 
415 
416 
417  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Scanning for NAT\n");
418 
419  init_pmp();
420 
421  if (!nat_globals.nat_type) {
422  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Checking for UPnP\n");
423  init_upnp();
424  }
425 
426  if (nat_globals.nat_type) {
427  switch_core_set_variable("nat_public_addr", nat_globals.pub_addr);
428  switch_core_set_variable("nat_private_addr", nat_globals.pvt_addr);
429  switch_core_set_variable("nat_type", nat_globals.nat_type == SWITCH_NAT_TYPE_PMP ? "pmp" : "upnp");
430  strncpy(nat_globals.nat_type_str, nat_globals.nat_type == SWITCH_NAT_TYPE_PMP ? "pmp" : "upnp", sizeof(nat_globals.nat_type_str) - 1);
431  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "NAT detected type: %s, ExtIP: '%s'\n",
433 
434  if (!nat_thread_p) {
436  }
437  } else {
438  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "No PMP or UPnP NAT devices detected!\n");
439  }
442 }
#define SWITCH_CHANNEL_LOG
struct UPNPUrls urls
Definition: switch_nat.c:49
char nat_type_str[5]
Definition: switch_nat.c:48
switch_memory_pool_t * pool
static switch_bool_t first_init
Definition: switch_nat.c:68
void switch_core_set_variable(_In_z_ const char *varname, _In_opt_z_ const char *value)
Add a global variable to the core.
switch_bool_t mapping
Definition: switch_nat.c:54
void switch_nat_thread_start(void)
Definition: switch_nat.c:357
switch_memory_pool_t * pool
Definition: switch_nat.c:60
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:789
char * descURL
Definition: switch_nat.c:51
switch_nat_type_t nat_type
Definition: switch_nat.c:47
char pub_addr[IP_LEN]
Definition: switch_nat.c:52
static nat_globals_perm_t nat_globals_perm
Definition: switch_nat.c:66
static int init_pmp(void)
Definition: switch_nat.c:205
switch_thread_t * nat_thread_p
Definition: switch_nat.c:355
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
switch_status_t switch_find_local_ip(_Out_opt_bytecapcount_(len) char *buf, _In_ int len, _In_opt_ int *mask, _In_ int family)
find local ip of the box
static switch_bool_t initialized
Definition: switch_nat.c:69
static int init_upnp(void)
Definition: switch_nat.c:86
static nat_globals_t nat_globals
Definition: switch_nat.c:57
memset(buf, 0, buflen)
char pvt_addr[IP_LEN]
Definition: switch_nat.c:53
switch_bool_t switch_nat_is_initialized ( void  )

Has the NAT subsystem been initialized.

Definition at line 736 of file switch_nat.c.

References initialized.

737 {
738  return initialized;
739 }
static switch_bool_t initialized
Definition: switch_nat.c:69
void switch_nat_late_init ( void  )

Initilize the rest of the NAT Traversal System.

Note
nat_init is called prior to some other modules being loaded. This method allows us to init the rest of the NAT system.

Definition at line 708 of file switch_nat.c.

References NAT_REFRESH_INTERVAL, nat_globals_perm_t::running, SSHF_OWN_THREAD, switch_epoch_time_now(), and switch_scheduler_add_task().

Referenced by switch_core_init().

709 {
710  if (nat_globals_perm.running == 1) {
711  switch_scheduler_add_task(switch_epoch_time_now(NULL) + NAT_REFRESH_INTERVAL, switch_nat_republish_sched, "nat_republish", "core", 0, NULL,
713  }
714 }
uint32_t switch_scheduler_add_task(time_t task_runtime, switch_scheduler_func_t func, const char *desc, const char *group, uint32_t cmd_id, void *cmd_arg, switch_scheduler_flag_t flags)
Schedule a task in the future.
#define NAT_REFRESH_INTERVAL
Definition: switch_nat.c:44
static nat_globals_perm_t nat_globals_perm
Definition: switch_nat.c:66
time_t switch_epoch_time_now(time_t *t)
Get the current epoch time.
Definition: switch_time.c:321
void switch_nat_reinit ( void  )

re-initializes NAT subsystem

Definition at line 215 of file switch_nat.c.

References nat_globals_t::mapping, nat_globals_perm_t::pool, and switch_nat_init().

Referenced by switch_nat_multicast_runtime().

216 {
218 }
switch_bool_t mapping
Definition: switch_nat.c:54
void switch_nat_init(switch_memory_pool_t *pool, switch_bool_t mapping)
Initilize the NAT Traversal System.
Definition: switch_nat.c:399
switch_memory_pool_t * pool
Definition: switch_nat.c:60
static nat_globals_perm_t nat_globals_perm
Definition: switch_nat.c:66
static nat_globals_t nat_globals
Definition: switch_nat.c:57
void switch_nat_republish ( void  )

Republishes the nap mappings.

Definition at line 652 of file switch_nat.c.

References switch_stream_handle::data, switch_api_execute(), SWITCH_CHANNEL_LOG, SWITCH_FALSE, SWITCH_LOG_DEBUG1, SWITCH_LOG_ERROR, switch_log_printf(), switch_nat_add_mapping_internal(), switch_safe_free, SWITCH_STANDARD_STREAM, switch_xml_child(), switch_xml_find_child(), switch_xml_free(), switch_xml_next, switch_xml_parse_str_dup, and switch_xml::txt.

Referenced by switch_nat_multicast_runtime(), and SWITCH_STANDARD_SCHED_FUNC().

653 {
654  switch_xml_t natxml = NULL;
655  switch_xml_t row = NULL;
656  switch_xml_t child = NULL;
657  switch_stream_handle_t stream = { 0 };
658  SWITCH_STANDARD_STREAM(stream);
659 
660  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "Refreshing nat maps\n");
661 
662  switch_api_execute("show", "nat_map as xml", NULL, &stream);
663 
664  if (!(natxml = switch_xml_parse_str_dup(stream.data))) {
665  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unable to parse XML: %s\n", (char *) stream.data);
666  switch_safe_free(stream.data);
667  return;
668  }
669 
670  /* iterate the xml and publish the mappings */
671  row = switch_xml_find_child(natxml, "row", "row_id", "1");
672  while (row != NULL) {
673  char *sport = NULL;
674  char *sproto = NULL;
675  switch_port_t port;
676  switch_nat_ip_proto_t proto;
677 
678  if ((child = switch_xml_child(row, "port"))) {
679  sport = child->txt;
680  }
681  if ((child = switch_xml_child(row, "proto_num"))) {
682  sproto = child->txt;
683  }
684 
685  if (sport && sproto) {
686  port = (switch_port_t) (atoi(sport));
687  proto = (switch_nat_ip_proto_t) (atoi(sproto));
689  } else {
690  switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unable to parse port/proto info: XML: %s\n", (char *) stream.data);
691  }
692 
693  row = switch_xml_next(row);
694  }
695 
696  switch_safe_free(stream.data);
697  switch_xml_free(natxml);
698 }
void switch_xml_free(_In_opt_ switch_xml_t xml)
frees the memory allocated for an switch_xml structure
#define SWITCH_CHANNEL_LOG
switch_xml_t switch_xml_find_child(_In_ switch_xml_t node, _In_z_ const char *childname, _In_opt_z_ const char *attrname, _In_opt_z_ const char *value)
find a child tag in a node called 'childname' with an attribute 'attrname' which equals 'value' ...
#define switch_xml_next(xml)
returns the next tag of the same name in the same section and depth or NULL \ if not found ...
Definition: switch_xml.h:170
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)
Definition: switch_nat.c:580
switch_status_t switch_api_execute(const char *cmd, const char *arg, switch_core_session_t *session, switch_stream_handle_t *stream)
Execute a registered API command.
A representation of an XML tree.
Definition: switch_xml.h:76
char * txt
Definition: switch_xml.h:82
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:789
uint16_t switch_port_t
#define SWITCH_STANDARD_STREAM(s)
#define switch_xml_parse_str_dup(x)
Parses a string into a switch_xml_t.
Definition: switch_xml.h:117
switch_xml_t switch_xml_child(_In_ switch_xml_t xml, _In_z_ const char *name)
returns the first child tag (one level deeper) with the given name or NULL \ if not found ...
void switch_log_printf(_In_ switch_text_channel_t channel, _In_z_ const char *file, _In_z_ const char *func, _In_ int line, _In_opt_z_ const char *userdata, _In_ switch_log_level_t level, _In_z_ _Printf_format_string_ const char *fmt,...) PRINTF_FUNCTION(7
Write log data to the logging engine.
switch_nat_ip_proto_t
Definition: switch_nat.h:47
void switch_nat_set_mapping ( switch_bool_t  mapping)

Update the setting if port mapping will be created.

Definition at line 210 of file switch_nat.c.

References nat_globals_t::mapping.

211 {
212  nat_globals.mapping = mapping;
213 }
switch_bool_t mapping
Definition: switch_nat.c:54
static nat_globals_t nat_globals
Definition: switch_nat.c:57
void switch_nat_shutdown ( void  )

Shuts down the NAT Traversal System.

Definition at line 741 of file switch_nat.c.

References nat_globals_t::descURL, switch_nat_thread_stop(), switch_safe_free, and nat_globals_t::urls.

Referenced by switch_core_destroy().

742 {
744  FreeUPNPUrls(&nat_globals.urls);
746 }
struct UPNPUrls urls
Definition: switch_nat.c:49
void switch_nat_thread_stop(void)
Definition: switch_nat.c:371
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:789
char * descURL
Definition: switch_nat.c:51
static nat_globals_t nat_globals
Definition: switch_nat.c:57
char* switch_nat_status ( void  )

Returns a list of nat mappings and other status info.

Note
caller must free the string

Definition at line 716 of file switch_nat.c.

References switch_stream_handle::data, nat_globals_t::mapping, nat_globals_t::nat_type, nat_globals_t::pub_addr, switch_api_execute(), SWITCH_NAT_TYPE_PMP, SWITCH_NAT_TYPE_UPNP, SWITCH_STANDARD_STREAM, and switch_stream_handle::write_function.

717 {
718  switch_stream_handle_t stream = { 0 };
719  SWITCH_STANDARD_STREAM(stream);
720 
721  stream.write_function(&stream, "Nat Type: %s, ExtIP: %s\n",
722  (nat_globals.nat_type == SWITCH_NAT_TYPE_UPNP) ? "UPNP" : (nat_globals.nat_type == SWITCH_NAT_TYPE_PMP ? "NAT-PMP" : "UNKNOWN"),
724 
725  if (nat_globals.mapping) {
726  stream.write_function(&stream, "NAT port mapping enabled.\n");
727  } else {
728  stream.write_function(&stream, "NAT port mapping disabled.\n");
729  }
730 
731  switch_api_execute("show", "nat_map", NULL, &stream);
732 
733  return stream.data; /* caller frees */
734 }
switch_status_t switch_api_execute(const char *cmd, const char *arg, switch_core_session_t *session, switch_stream_handle_t *stream)
Execute a registered API command.
switch_bool_t mapping
Definition: switch_nat.c:54
#define SWITCH_STANDARD_STREAM(s)
switch_nat_type_t nat_type
Definition: switch_nat.c:47
char pub_addr[IP_LEN]
Definition: switch_nat.c:52
switch_stream_handle_write_function_t write_function
static nat_globals_t nat_globals
Definition: switch_nat.c:57