📄 nat_api.c
字号:
){ 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. ******************************************************************************//******************************************************************************* * 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 *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 */ { printf("This function applies to NAPT only.\n"); 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) ) { printf("Give a valid IP address.\n"); 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; if (registerStaticEntryToTranslationList ( &(nat.tcp_static_entries[index]), IPPROTO_TCP) == ERROR) { printf("Failed to register TCP static entry\n"); 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) { printf("TCP static entry already exists\n"); 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; int index; TCP_TRANSLATION_ENTRY *tcpTranslationEntry; if (nat.single_global_address_enabled == FALSE) /* basic NAT */ { printf("This function applies to NAPT only.\n"); return(ERROR); } localAddress = ntohl(inet_addr(localAddr)); 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; tcpTranslationEntry = match_spoofed_port_with_tcp_entry ( globalPort, &nat.nats.tcp_translation_list, TRUE); if (tcpTranslationEntry == NULL) { printf("Deleted static entry is not in TCP translation list\n"); return(ERROR); } delete_sequence_entry_list (&tcpTranslationEntry->local_sequence_delta_list); delete_sequence_entry_list (&tcpTranslationEntry->global_sequence_delta_list); natFreeBind ((u_long)&nat, 0, tcpTranslationEntry->bind_id); return (OK); } } printf("No match found in the TCP static entries.\n"); 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.******************************************************************************//******************************************************************************* * natUdpStaticAdd - add a UDP static port-based translation entry * * Use this routine to add a UDP 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 to NAPT mode only.** RETURNS* * OK (success), or ERROR (failure). **/STATUS natUdpStaticAdd ( char *localAddr, /* local address, standard IP string format */ u_short localPort, /* local port number */ u_short globalPort /* global port number */ ){ u_long localAddress; int index; if (nat.single_global_address_enabled == FALSE) /* basic NAT */ { printf("This function applies to NAPT only.\n"); 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) ) { printf("Give a valid IP address.\n"); return (ERROR); } for (index=0; index < MAXIMUM_NUMBER_OF_UDP_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.udp_static_entries[index].local_address = localAddress; nat.udp_static_entries[index].local_port_number = localPort; nat.udp_static_entries[index].global_port_number = globalPort; if (registerStaticEntryToTranslationList ( &(nat.udp_static_entries[index]), IPPROTO_UDP) == ERROR) { printf("Failed to register UDP static entry\n"); return (ERROR); } break; } else { if (nat.udp_static_entries[index].local_address == localAddress && nat.udp_static_entries[index].local_port_number == localPort && nat.udp_static_entries[index].global_port_number == globalPort) { printf("UDP static entry already exists\n"); break; } } } return (OK);}/******************************************************************************Description: Delete a UDP static port-based translation entry (NAPT only). Look for the matching entry in the NAT's UDP translation list. If found, delete the entry from the list and from the bind list.******************************************************************************//******************************************************************************* * natUdpStaticDelete - delete a UDP static port-based translation entry* * Use this routine to delete a UDP 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 UDP entries, it displays "No match found in the UDP static entries"* and returns ERROR.* * 'Note:' This routine applies to NAPT mode only.** RETURNS* * OK (success), or ERROR (failure). **/STATUS natUdpStaticDelete ( char *localAddr, /* local address, standard IP string format */ u_short localPort, /* local port number */ u_short globalPort /* global port number */ ){ u_long localAddress; int index; UDP_TRANSLATION_ENTRY *udpTranslationEntry; if (nat.single_global_address_enabled == FALSE) /* basic NAT */ { printf("This function applies to NAPT only.\n"); return(ERROR); } localAddress = ntohl(inet_addr(localAddr)); for (index=0; index < MAXIMUM_NUMBER_OF_UDP_STATIC_ENTRIES; index++) { if (nat.udp_static_entries[index].local_address == localAddress && nat.udp_static_entries[index].local_port_number == localPort && nat.udp_static_entries[index].global_port_number == globalPort) { nat.udp_static_entries[index].local_address = 0; nat.udp_static_entries[index].local_port_number = 0; nat.udp_static_entries[index].global_port_number = 0; udpTranslationEntry = match_spoofed_port_with_udp_entry ( globalPort, &nat.nats.udp_translation_list, TRUE); if (udpTranslationEntry == NULL) { printf("Deleted static entry is not in UDP translation list\n"); return(ERROR); } natFreeBind ((u_long)&nat, 0, udpTranslationEntry->bind_id); return (OK); } } printf("No match found in the UDP static entries.\n"); return (ERROR);}/************************************************************************************Function to inquire interface port information (for debug purpose).************************************************************************************/void natIfInfo(int portNum){ if (nat.port[portNum].type == NAT_GLOBAL_PORT) { printf("Port %d interface type = global\n", portNum); } else { printf("Port %d interface type = local\n", portNum); } printf("Port %d interface address = %x\n", portNum, (unsigned int) nat.port[portNum].address); printf("Port %d interface mask = %x\n", portNum, (unsigned int) nat.port[portNum].mask); printf("Port %d interface name = %s\n", portNum, nat.port[portNum].ifname); printf("Port %d interface number = %d\n", portNum, nat.port[portNum].ifunit->if_index);}#if _STATIC_IP_API /*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! The following two api are commented out. It is probably not a good idea to give user the ability to add and delete a static address translation entry. The entry may be in use or there may be existing TCP connections branched off the IP entry.!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*//******************************************************************************Description: Add an IP static address translation entry (Basic NAT only). Check if the requested addresses already exist in the IP translation list (i.e. addresses have been mapped/used). If not, create a new static IP entry in the IP translation list.******************************************************************************/STATUS natIpStaticAdd(char *localAddr, char *globalAddr){ IP_ADDRESS localAddress, globalAddress; IP_TRANSLATION_HEADER *ipList; NAT_BIND_INFO bind_info; NAT_STATUS status;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -