NAT code
[Core Library]

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 }

Enumerator

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


Enumeration Type Documentation

enum switch_nat_ip_proto_t

Enumerator:
SWITCH_NAT_UDP 
SWITCH_NAT_TCP 

Definition at line 47 of file switch_nat.h.

00047              {
00048         SWITCH_NAT_UDP,
00049         SWITCH_NAT_TCP
00050 } switch_nat_ip_proto_t;

enum switch_nat_type_t

Enumerator:
SWITCH_NAT_TYPE_NONE 
SWITCH_NAT_TYPE_PMP 
SWITCH_NAT_TYPE_UPNP 

Definition at line 41 of file switch_nat.h.


Enumerator Documentation

SWITCH_NAT_TCP

Referenced by switch_nat_add_mapping_pmp(), switch_nat_add_mapping_upnp(), switch_nat_del_mapping_pmp(), and switch_nat_del_mapping_upnp().

SWITCH_NAT_TYPE_PMP

Referenced by get_pmp_pubaddr(), init_nat_monitor(), switch_nat_add_mapping_internal(), switch_nat_del_mapping(), switch_nat_init(), and switch_nat_status().

SWITCH_NAT_TYPE_UPNP

Referenced by init_nat_monitor(), init_upnp(), switch_nat_add_mapping_internal(), switch_nat_del_mapping(), switch_nat_multicast_runtime(), and switch_nat_status().


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:
port Internal port to map
proto Protocol
external_port [out] Mapped external port
sticky make the mapping permanent

Definition at line 621 of file switch_nat.c.

References switch_nat_add_mapping_internal(), and SWITCH_TRUE.

00623 {
00624         return switch_nat_add_mapping_internal(port, proto, external_port, sticky, SWITCH_TRUE);
00625 }

switch_status_t switch_nat_del_mapping ( switch_port_t  port,
switch_nat_ip_proto_t  proto 
)

Deletes a NAT mapping.

Parameters:
proto Protocol

Definition at line 627 of file switch_nat.c.

References nat_globals, 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.

00628 {
00629         switch_status_t status = SWITCH_STATUS_FALSE;
00630         switch_event_t *event = NULL;
00631 
00632         switch (nat_globals.nat_type) {
00633         case SWITCH_NAT_TYPE_PMP:
00634                 status = switch_nat_del_mapping_pmp(port, proto);
00635                 break;
00636         case SWITCH_NAT_TYPE_UPNP:
00637                 status = switch_nat_del_mapping_upnp(port, proto);
00638                 break;
00639         default:
00640                 break;
00641         }
00642 
00643         if (status == SWITCH_STATUS_SUCCESS) {
00644                 switch_event_create(&event, SWITCH_EVENT_NAT);
00645                 switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "op", "del");
00646                 switch_event_add_header(event, SWITCH_STACK_BOTTOM, "port", "%d", port);
00647                 switch_event_add_header(event, SWITCH_STACK_BOTTOM, "proto", "%d", proto);
00648                 switch_event_fire(&event);
00649         }
00650 
00651         return status;
00652 }

const char* switch_nat_get_type ( void   ) 

Definition at line 578 of file switch_nat.c.

References nat_globals, and nat_globals_t::nat_type_str.

00579 {
00580         return nat_globals.nat_type_str;
00581 }

void switch_nat_init ( switch_memory_pool_t pool,
switch_bool_t  mapping 
)

Initilize the NAT Traversal System.

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

Definition at line 402 of file switch_nat.c.

References nat_globals_t::descURL, first_init, init_pmp(), init_upnp(), initialized, nat_globals, nat_globals_perm, nat_thread_p, 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().

00403 {
00404         /* try free dynamic data structures prior to resetting to 0 */
00405         FreeUPNPUrls(&nat_globals.urls);
00406         switch_safe_free(nat_globals.descURL);
00407 
00408         memset(&nat_globals, 0, sizeof(nat_globals));
00409 
00410         if (first_init) {
00411                 memset(&nat_globals_perm, 0, sizeof(nat_globals_perm));
00412                 nat_globals_perm.pool = pool;
00413         }
00414 
00415         nat_globals.mapping = mapping;
00416 
00417         switch_find_local_ip(nat_globals.pvt_addr, sizeof(nat_globals.pvt_addr), NULL, AF_INET);
00418 
00419 
00420         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Scanning for NAT\n");
00421 
00422         init_pmp();
00423 
00424         if (!nat_globals.nat_type) {
00425                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Checking for UPnP\n");
00426                 init_upnp();
00427         }
00428 
00429         if (nat_globals.nat_type) {
00430                 switch_core_set_variable("nat_public_addr", nat_globals.pub_addr);
00431                 switch_core_set_variable("nat_private_addr", nat_globals.pvt_addr);
00432                 switch_core_set_variable("nat_type", nat_globals.nat_type == SWITCH_NAT_TYPE_PMP ? "pmp" : "upnp");
00433                 strncpy(nat_globals.nat_type_str, nat_globals.nat_type == SWITCH_NAT_TYPE_PMP ? "pmp" : "upnp", sizeof(nat_globals.nat_type_str) - 1);
00434                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "NAT detected type: %s, ExtIP: '%s'\n",
00435                                                   nat_globals.nat_type == SWITCH_NAT_TYPE_PMP ? "pmp" : "upnp", nat_globals.pub_addr);
00436 
00437                 if (!nat_thread_p) {
00438                         switch_nat_thread_start();
00439                 }
00440         } else {
00441                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "No PMP or UPnP NAT devices detected!\n");
00442         }
00443         first_init = SWITCH_FALSE;
00444         initialized = SWITCH_TRUE;
00445 }

switch_bool_t switch_nat_is_initialized ( void   ) 

Has the NAT subsystem been initialized.

Definition at line 738 of file switch_nat.c.

References initialized.

00739 {
00740         return initialized;
00741 }

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 710 of file switch_nat.c.

References nat_globals_perm, 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().

00711 {
00712         if (nat_globals_perm.running == 1) {
00713                 switch_scheduler_add_task(switch_epoch_time_now(NULL) + NAT_REFRESH_INTERVAL, switch_nat_republish_sched, "nat_republish", "core", 0, NULL,
00714                                                                   SSHF_OWN_THREAD);
00715         }
00716 }

void switch_nat_reinit ( void   ) 

re-initializes NAT subsystem

Definition at line 217 of file switch_nat.c.

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

Referenced by switch_nat_multicast_runtime().

void switch_nat_republish ( void   ) 

Republishes the nap mappings.

Definition at line 654 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().

00655 {
00656         switch_xml_t natxml = NULL;
00657         switch_xml_t row = NULL;
00658         switch_xml_t child = NULL;
00659         switch_stream_handle_t stream = { 0 };
00660         SWITCH_STANDARD_STREAM(stream);
00661 
00662         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "Refreshing nat maps\n");
00663 
00664         switch_api_execute("show", "nat_map as xml", NULL, &stream);
00665 
00666         if (!(natxml = switch_xml_parse_str_dup(stream.data))) {
00667                 switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unable to parse XML: %s\n", (char *) stream.data);
00668                 switch_safe_free(stream.data);
00669                 return;
00670         }
00671 
00672         /* iterate the xml and publish the mappings */
00673         row = switch_xml_find_child(natxml, "row", "row_id", "1");
00674         while (row != NULL) {
00675                 char *sport = NULL;
00676                 char *sproto = NULL;
00677                 switch_port_t port;
00678                 switch_nat_ip_proto_t proto;
00679 
00680                 if ((child = switch_xml_child(row, "port"))) {
00681                         sport = child->txt;
00682                 }
00683                 if ((child = switch_xml_child(row, "proto_num"))) {
00684                         sproto = child->txt;
00685                 }
00686 
00687                 if (sport && sproto) {
00688                         port = (switch_port_t) (atoi(sport));
00689                         proto = (switch_nat_ip_proto_t) (atoi(sproto));
00690                         switch_nat_add_mapping_internal(port, proto, NULL, SWITCH_FALSE, SWITCH_FALSE);
00691                 } else {
00692                         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unable to parse port/proto info: XML: %s\n", (char *) stream.data);
00693                 }
00694 
00695                 row = switch_xml_next(row);
00696         }
00697 
00698         switch_safe_free(stream.data);
00699         switch_xml_free(natxml);
00700 }

void switch_nat_set_mapping ( switch_bool_t  mapping  ) 

Update the setting if port mapping will be created.

Definition at line 212 of file switch_nat.c.

References nat_globals_t::mapping, and nat_globals.

00213 {
00214         nat_globals.mapping = mapping;
00215 }

void switch_nat_shutdown ( void   ) 

Shuts down the NAT Traversal System.

Definition at line 743 of file switch_nat.c.

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

Referenced by switch_core_destroy().

00744 {
00745         switch_nat_thread_stop();
00746         FreeUPNPUrls(&nat_globals.urls);
00747         switch_safe_free(nat_globals.descURL);
00748 }

char* switch_nat_status ( void   ) 

Returns a list of nat mappings and other status info.

Note:
caller must free the string

Definition at line 718 of file switch_nat.c.

References switch_stream_handle::data, nat_globals_t::mapping, nat_globals, 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.

00719 {
00720         switch_stream_handle_t stream = { 0 };
00721         SWITCH_STANDARD_STREAM(stream);
00722 
00723         stream.write_function(&stream, "Nat Type: %s, ExtIP: %s\n",
00724                                                   (nat_globals.nat_type == SWITCH_NAT_TYPE_UPNP) ? "UPNP" : (nat_globals.nat_type == SWITCH_NAT_TYPE_PMP ? "NAT-PMP" : "UNKNOWN"),
00725                                                   nat_globals.pub_addr);
00726 
00727         if (nat_globals.mapping) {
00728                 stream.write_function(&stream, "NAT port mapping enabled.\n");
00729         } else {
00730                 stream.write_function(&stream, "NAT port mapping disabled.\n");
00731         }
00732 
00733         switch_api_execute("show", "nat_map", NULL, &stream);
00734 
00735         return stream.data;                     /* caller frees */
00736 }


Generated on Wed May 16 04:00:19 2012 for FreeSWITCH API Documentation by  doxygen 1.4.7