📄 nat_api.c
字号:
************************************************************************/
STATUS natPassThruListDelete(char *address, char *mask)
{
IP_ADDRESS addr, msk;
NAT_PASSTHRU_PAIR *p_pair;
addr = ntohl(inet_addr(address));
msk = ntohl(inet_addr(mask));
p_pair = (NAT_PASSTHRU_PAIR *) lstFirst(&nat.passthru_list);
while (p_pair != NULL)
{
if ((p_pair->address & p_pair->mask) == (addr & msk))
{
lstDelete (&nat.passthru_list, (NODE*) p_pair);
free (p_pair);
return (OK);
}
p_pair = (NAT_PASSTHRU_PAIR *) lstNext((NODE*) p_pair);
}
printf("Can't find match in the pass through list.\n");
return(ERROR);
}
/************************************************************************
Description:
Show the list of address/mask pairs in the pass through list.
Outbound packets sent to these addresses will not be translated
by NAT.
************************************************************************/
STATUS natPassThruListShow()
{
NAT_PASSTHRU_PAIR *p_pair;
struct in_addr iaddr;
char address[INET_ADDR_LEN];
char mask[INET_ADDR_LEN];
printf("NAT Pass Through List\n");
printf("=====================\n");
printf("Address\t\t\tMask\n");
p_pair = (NAT_PASSTHRU_PAIR *) lstFirst(&nat.passthru_list);
while (p_pair != NULL)
{
if (p_pair->address != 0)
{
iaddr.s_addr = htonl(p_pair->address);
inet_ntoa_b (iaddr, address);
iaddr.s_addr = htonl(p_pair->mask);
inet_ntoa_b (iaddr, mask);
printf ("%s\t\t%s\n", address, mask);
}
p_pair = (NAT_PASSTHRU_PAIR *) lstNext((NODE*) p_pair);
}
return (OK);
}
#endif
/*************************************************************************
Description:
Get the translated global address of the given local address.
*************************************************************************/
/******************************************************************************
*
* natGetGlobalAddr - display global (external) address of specified local host
*
* Use this routine to display the external address that binds to the specified
* host in the private (or local) network realm. Although it can be called in
* NAPT mode, this information is more useful in Basic NAT.
*
* RETURNS
*
* OK (success), or ERROR (failure).
*
*/
STATUS natGetGlobalAddr
(
char *localAddr /* Local address of host whose global address is sought. */
)
{
IP_TRANSLATION_ENTRY* ip_entry;
IP_ADDRESS addr;
struct in_addr iaddr;
char address[INET_ADDR_LEN];
if(nat.single_global_address_enabled == TRUE) /* NAPT */
{
iaddr.s_addr = htonl(nat.global_address);
inet_ntoa_b(iaddr, address);
printf("Global address = %s\n", address);
return(OK);
}
/* Basic NAT */
ip_entry = (IP_TRANSLATION_ENTRY *) DLL_FIRST(
(DL_LIST *) &nat.natg.ip_translation_list);
addr = ntohl(inet_addr(localAddr));
while (ip_entry != NULL)
{
if (ip_entry->sa_local_address == addr)
{
iaddr.s_addr = htonl(ip_entry->sa_global_address);
inet_ntoa_b(iaddr, address);
printf("Global address = %s\n", address);
return(OK);
}
ip_entry = (IP_TRANSLATION_ENTRY *) DLL_NEXT(
(DL_NODE *) ip_entry);
}
printf("Global address not found\n");
return(ERROR);
}
/******************************************************************************
Description:
Add a TCP static port-based translation entry (NAPT only).
Call registerStaticEntryToTranslationList to create a new entry in the
NAT's translation list and bind list for this static entry.
******************************************************************************/
STATUS natStaticDelete(char *servername)
{
int index;
struct in_addr ip;
char ipAddr[INET_ADDR_LEN];
for (index=0; index < MAXIMUM_NUMBER_OF_TCP_STATIC_ENTRIES; index++)
{
if(nat.tcp_static_entries[index].local_address == 0)
continue;
else if(!strcmp(nat.tcp_static_entries[index].name,servername))
{
ip.s_addr=htonl(nat.tcp_static_entries[index].local_address );
inet_ntoa_b(ip,ipAddr);
natTcpStaticDelete(ipAddr,nat.tcp_static_entries[index].local_port_number ,
nat.tcp_static_entries[index].global_port_number);
memset(nat.tcp_static_entries[index].name,0,32);
return (OK);
}
}
for (index=0; index < MAXIMUM_NUMBER_OF_UDP_STATIC_ENTRIES; index++)
{
if(nat.udp_static_entries[index].local_address == 0)
continue;
else if(!strcmp(nat.udp_static_entries[index].name,servername))
{
ip.s_addr=htonl(nat.udp_static_entries[index].local_address );
inet_ntoa_b(ip,ipAddr);
natUdpStaticDelete(ipAddr,nat.udp_static_entries[index].local_port_number ,
nat.udp_static_entries[index].global_port_number);
memset(nat.udp_static_entries[index].name,0,32);
return (OK);
}
}
return (ERROR);
}
/******************************************************************************
*
* natTcpStaticAdd - add a TCP static port-based translation entry
*
* Use this routine to add a TCP static port-based translation entry (NAPT
* only). The <localAddr> must be in the standard IP string format, for
* example, "10.1.10.110". This routine can be called by a software agent
* (for example, an SNMP agent, or a NAT ALG) or invoked from the Tornado
* WindSh window.
*
* 'Note:' This routine applies in NAPT mode only.
*
* RETURNS
*
* OK (success), or ERROR (failure).
*
*/
STATUS natTcpStaticAdd
(
char *strName,
char *localAddr, /* Expects the local address. */
u_short localPort, /* Expects the local port number. */
u_short globalPort /* Expects the global port number. */
)
{
u_long localAddress;
int index;
if (nat.single_global_address_enabled == FALSE) /* basic NAT */
{
return(ERROR);
}
localAddress = ntohl(inet_addr(localAddr));
/* Only allow localAddress of 0 when global address is also 0
* (they will get updated later)
*/
if ( (localAddress == 0) && (nat.global_address != 0) )
{
return (ERROR);
}
for (index=0; index < MAXIMUM_NUMBER_OF_TCP_STATIC_ENTRIES; index++)
{
if ( (nat.tcp_static_entries[index].local_address == 0)
&& (nat.tcp_static_entries[index].local_port_number == 0)
&& (nat.tcp_static_entries[index].global_port_number == 0)
)
{
nat.tcp_static_entries[index].local_address = localAddress;
nat.tcp_static_entries[index].local_port_number = localPort;
nat.tcp_static_entries[index].global_port_number = globalPort;
strncpy(nat.tcp_static_entries[index].name,strName,32);
if (registerStaticEntryToTranslationList (
&(nat.tcp_static_entries[index]), IPPROTO_TCP) == ERROR)
{
return (ERROR);
}
break;
}
else
{
if (nat.tcp_static_entries[index].local_address == localAddress &&
nat.tcp_static_entries[index].local_port_number == localPort &&
nat.tcp_static_entries[index].global_port_number == globalPort)
{
break;
}
}
}
return (OK);
}
/******************************************************************************
Description:
Delete a TCP static port-based translation entry (NAPT only).
Look for the matching entry in the NAT's TCP translation list. If found,
delete the entry from the list and from the bind list.
******************************************************************************/
/******************************************************************************
*
* natTcpStaticDelete - delete a TCP static port-based translation entry
*
* Use this routine to delete a TCP static port-based translation entry (NAPT
* only). The <localAddr> must be in the standard IP string format, for
* example "10.1.10.110". This routine can be called by a software agent (for
* example, an SNMP agent, or a NAT ALG) or invoked from the Tornado WindSh
* window. If the input parameters do not match the list of existing static
* TCP entries, it displays "No match found in the TCP static entries" and
* returns ERROR.
*
* 'Note:' This routine applies to NAPT mode only.
*
* RETURNS
*
* OK (success), or ERROR (failure).
*
*/
STATUS natTcpStaticDelete
(
char *localAddr, /* local IP address */
u_short localPort, /* number of local port */
u_short globalPort /* number of global port */
)
{
u_long localAddress;
NAT_BIND_INFO* bind_info=NULL;
unsigned short state=0;
int index;
NAT_CURRENCY_TRANSLATION_ENTRY *tcpTranslationEntry=NULL;
if (nat.single_global_address_enabled == FALSE) /* basic NAT */
{
return(ERROR);
}
localAddress = ntohl(inet_addr(localAddr));
/*Delete All static Entrys*/
if((!localAddress)&&(!localPort)&&(!globalPort))
{
for (index=0; index < MAXIMUM_NUMBER_OF_TCP_STATIC_ENTRIES; index++)
{
semTake(natentrylock, WAIT_FOREVER);
tcpTranslationEntry = match_ALG_port_entry(nat.tcp_static_entries[index].global_port_number,
nat.tcp_static_entries[index].local_address,
nat.tcp_static_entries[index].local_port_number);
nat.tcp_static_entries[index].local_address = 0;
nat.tcp_static_entries[index].local_port_number = 0;
nat.tcp_static_entries[index].global_port_number = 0;
if (tcpTranslationEntry == NULL)
{
semGive(natentrylock);
return(ERROR);
}
delete_sequence_entry_list (&tcpTranslationEntry->local_sequence_delta_list);
delete_sequence_entry_list (&tcpTranslationEntry->global_sequence_delta_list);
semGive(natentrylock);
if(natFreeBind ((u_long)&nat, 0, tcpTranslationEntry->bind_id)!=NAT_OK)
{
if(tcpTranslationEntry->bind_id)
{
bind_info=(NAT_BIND_INFO*)tcpTranslationEntry->bind_id;
if(bind_info)
{
free(bind_info);
bind_info=NULL;
}
}
}
semTake(natentrylock, WAIT_FOREVER);
natDelete_entrys(tcpTranslationEntry);
semGive(natentrylock);
state++;
}
if(state)
return (OK);
else
return(ERROR);
}
else if((!localPort)&&(!globalPort)) /*Delete Specifical IP Entrys*/
{
for (index=0; index < MAXIMUM_NUMBER_OF_TCP_STATIC_ENTRIES; index++)
{
if (nat.tcp_static_entries[index].local_address == localAddress)
{
semTake(natentrylock, WAIT_FOREVER);
tcpTranslationEntry = match_ALG_port_entry(nat.tcp_static_entries[index].global_port_number,
localAddress,nat.tcp_static_entries[index].local_port_number);
nat.tcp_static_entries[index].local_address = 0;
nat.tcp_static_entries[index].local_port_number = 0;
nat.tcp_static_entries[index].global_port_number = 0;
if (tcpTranslationEntry == NULL)
{
semGive(natentrylock);
return(ERROR);
}
delete_sequence_entry_list (&tcpTranslationEntry->local_sequence_delta_list);
delete_sequence_entry_list (&tcpTranslationEntry->global_sequence_delta_list);
semGive(natentrylock);
if(natFreeBind ((u_long)&nat, 0, tcpTranslationEntry->bind_id)!=NAT_OK)
{
if(tcpTranslationEntry->bind_id)
{
bind_info=(NAT_BIND_INFO*)tcpTranslationEntry->bind_id;
if(bind_info)
{
free(bind_info);
bind_info=NULL;
}
}
}
semTake(natentrylock, WAIT_FOREVER);
natDelete_entrys(tcpTranslationEntry);
semGive(natentrylock);
state++;
}
}
if(state)
return (OK);
else
return(ERROR);
}
else
{
for (index=0; index < MAXIMUM_NUMBER_OF_TCP_STATIC_ENTRIES; index++)
{
if (nat.tcp_static_entries[index].local_address == localAddress &&
nat.tcp_static_entries[index].local_port_number == localPort &&
nat.tcp_static_entries[index].global_port_number == globalPort)
{
nat.tcp_static_entries[index].local_address = 0;
nat.tcp_static_entries[index].local_port_number = 0;
nat.tcp_static_entries[index].global_port_number = 0;
semTake(natentrylock, WAIT_FOREVER);
tcpTranslationEntry = match_ALG_port_entry (globalPort, localAddress, localPort);
if (tcpTranslationEntry == NULL)
{
semGive(natentrylock);
return(ERROR);
}
delete_sequence_entry_list (&tcpTranslationEntry->local_sequence_delta_list);
delete_sequence_entry_list (&tcpTranslationEntry->global_sequence_delta_list);
semGive(natentrylock);
if(natFreeBind ((u_long)&nat, 0, tcpTranslationEntry->bind_id)!=NAT_OK)
{
if(tcpTranslationEntry->bind_id)
{
bind_info=(NAT_BIND_INFO*)tcpTranslationEntry->bind_id;
if(bind_info)
{
free(bind_info);
bind_info=NULL;
}
}
}
semTake(natentrylock, WAIT_FOREVER);
natDelete_entrys(tcpTranslationEntry);
semGive(natentrylock);
return (OK);
}
}
}
return (ERROR);
}
/******************************************************************************
Description:
Add a UDP static port-based translation entry (NAPT only).
Call registerStaticEntryToTranslationList to create a new entry in the
NAT's translation list and bind list for this static entry.
******************************************************************************/
/*************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -