📄 nat_api.c
字号:
/* nat_api.c - WIND NET NAT system management interface *//* WindNet NAT Application Programming Interface *//* Copyright 2000-2003 Wind River Systems, Inc. *//* @format.tab-size 4, @format.use-tabs true, @format.new-line lf *//*modification history--------------------01h,29aug03,zhu updated the format for refgen01g,06may03,zhu fixed SPR80248: Pass through API mismatched with natPassThruList implementation01f,29apr03,myz fixed one compiler warning on MIPS32diab01e,25apr03,svk Implement version number01d,24apr03,zhu updated copyright01c,21apr03,myz replaced swap(_long) with the ntohs(l) and htons(l) macros, replaced RWOS list functions with ones in dllLib.c01b,17apr03,zhu removed #if 001a,15apr03,zhu allow localAddress of 0 when global address is also 0 in natTcpStaticAdd and natUdpStaticAdd040803 vks updated Copyright info040303 vks replaced table_free with free120602 zhu fixed the printf error111502 zhu fixed memory leak SPR#83726102102 ep removing diab warnings101902 zhu added SPR65740 patch093002 vvv fixed typo in natShow092402 vvv fixed build error092402 vvv replaced rw_container lists with linked lists to improve performance092302 vvv fixed Diab warning092302 vvv unconditionally include patch for SPR #65740112601 tk Fix SPR65740: ARP problem with Basic-NAT.100901 tk Bug fix. udpListLock semaphore didn't get released in natUdpXlatDelete.100501 tk Bug fix in natTcpStaticAdd and natUdpStaticAdd to prevent addition of duplicate static entry.100301 tk Put semaphore lock around natTcpXlatAdd, natTcpXladDelete, natUdpXlatAdd, and natUdpXlatDelete.091801 tk Add natGetGlobalAddr function. Comment out passthru functions.091001 tk Change NAT version from NAT 1.1 to NAT FCS 1.1.082301 tk Modify natTcp(and Udp)StaticAdd(and Delete) functions so that adding or deleting an entry not only add/delete the entry to/from the static table, but also to/from the NAT translation list and bind list.070201 tk Replace natXlatAdd and natXlatDelete with natIpXlatAdd and natIpXlatDelete. Put appropriate semaphore lock around the call to add a new entry or delete an entry in each translation list.052501 tk Change natShow to show "Global Addresses In Use" in Basic NAT only the addresses above the configured starting global address unless they are static entries. For each entry, show it is static or dynamic.051701 tk Rewrite natPassThruListAdd(), add functions natPassThruListDelete() and natPassThruListShow().051401 tk Change natTcpXlatShow to show IP translation entries and TCP Client translation entries created based on address-based static entries. Also, for IP translation static entries, display time-stamp to be 0 to indicate it is not applicable. 050901 tk Add natTcpStaticAdd, natUdpStaticAdd, natTcpStaticDelete, natUdpStaticDelete. Change natShow to check all TCP and UDP static entries and show them if entry is not 0. Previously, it would stop at the first encounter of entry 0.042101 tk Fix natShow, NAT's global address mask showed global address Fix call to new_udp_translation_entry in natUdpXlatAdd, htonl was on remoteAddress instead of on localAddress.*//*DESCRIPTIONThis library supplies functions that system managers can use to enable or disable NAT entirely for the system as a whole or on just on a specific port. This library also supplies functions for reviewing translation lists and adding or deleting entries from those lists. Other than that, the library does not supply a generic interface for configuring NAT. That is handled in your 'natcfg.c' file. */#include <stdio.h> /* printf */#include <etherLib.h>#include <arpLib.h>#include <nat_api.h> /* verify prototypes */#include "nat.h"extern STATUS registerStaticEntryToTranslationList (NAT_PORT_STATIC_ENTRY *, u_short);/******************************************************************************** natShow - display current NAT status information** This routine displays the current status and configuration of the NAT * device, as well as the static bind tables and all of the translation * lists. In NAPT mode, all the static binds that apply to NAT in NAPT mode * are shown, including the TCP and UDP port-based static binds, as well as * the IP address-based static binds. In Basic NAT mode, only the IP * address-based static binds are shown.* * Similarly, all of the static binds, as well as the dynamic binds that * are still active in the translation lists, are displayed. In NAPT mode, * the translation lists displayed include TCP, UDP, IP, and ICMP translation * lists. In Basic NAT mode, they include only the IP translation list and * the TCP translation list of each IP bind. NAT creates and maintains a * separate TCP translation list in each IP bind when TCP sessions are * initiated off of the IP bind.* * RETURNS* * OK (success), or ERROR (failure).* */STATUS natShow(){ char addr[INET_ADDR_LEN]; int status; int port_index; int addr_index; struct in_addr iaddr; printf("\nWindNet NAT %s", natVersion); printf("\n===================\n"); printf("Mode: %-8s Enabled: %s\n" ,nat.single_global_address_enabled ? "NAPT" : "Basic" ,nat.enabled ? "Yes" : "No"); printf("Static translation entries enabled: %s\n" ,nat.static_entries_enabled ? "Yes" : "No"); printf("Filter non-corporate addressed packets on global interface: %s\n" ,nat.filter_non_corporate_addresses ? "Yes" : "No"); printf("\nPort/Interface List"); printf("\n-------------------\n"); printf("# Name %-*s Type Dynamic DefXlat Enabled\n", 15, "Address"); for(port_index=0; port_index<NUMBER_OF_IP_PORTS; port_index++) { iaddr.s_addr = htonl (nat.port[port_index].address); inet_ntoa_b(iaddr, addr); printf("%-3d %-6s %-*s %-6s %-7s %-7s %s\n" ,port_index ,nat.port[port_index].ifname ,15,addr ,nat.port[port_index].type_string ,nat.port[port_index].ifunit == NULL ? "Yes" : "No" ,nat.port[port_index].default_translate_enabled ? "Yes" : "No" ,nat.port[port_index].enabled ? "Yes" : "No" ); } iaddr.s_addr = htonl (nat.global_address); inet_ntoa_b(iaddr, addr); printf("\nGlobal Address: %s\n", addr); iaddr.s_addr = htonl (nat.global_address_mask); inet_ntoa_b(iaddr, addr); printf("\nGlobal Address Mask: %s\n", addr); if(nat.single_global_address_enabled == TRUE) /* NAPT */ { /* Show NAPT-specific details */ } else /* Basic NAT */ { iaddr.s_addr = htonl (nat.starting_global_address); inet_ntoa_b(iaddr, addr); printf("\nBasic NAT Starting Global Address (static entries exempted): %s\n", addr); } printf("\nGlobal Addresses In Use\n"); printf("------------------------\n"); for(addr_index=1; addr_index < (nat.natg.global_address_pool_size - 1); addr_index++) { if (addr_index >= nat.natg.global_address_index_start) { /* don't show global addresses below the configured starting address */ if(nat.natg.global_address_pool[addr_index].address_in_use == TRUE) { iaddr.s_addr = htonl (nat.natg.global_address_pool[addr_index].address); inet_ntoa_b(iaddr, addr); printf("%s\t", addr); if (nat.natg.global_address_pool[addr_index].static_entry == true) { printf("Static\n"); } else { printf("Dynamic\n"); } } } else /* unless it is a static entry */ { if(nat.natg.global_address_pool[addr_index].static_entry == true) { iaddr.s_addr = htonl (nat.natg.global_address_pool[addr_index].address); inet_ntoa_b(iaddr, addr); printf("%s\tStatic\n", addr); } } } status = natXlatShow(); return(status);}/******************************************************************************* * natEnable - enable or disable the NAT agent* * Use this routine to enable or disable the NAT agent. ** RETURNS* * OK (success) always.**/STATUS natEnable ( BOOL enable /* TRUE to enable NAT agent, FALSE to disable. */ ){ nat.enabled = enable; return(OK);}/******************************************************************************* * natPortEnable - enable or disable translations on a specific port * * Use this routine to enable or disable translations on the specified port. ** RETURNS* * OK (success), or ERROR (failure). **/STATUS natPortEnable ( int port, /* The port or interface to enable or disable. */ BOOL enable /* TRUE to enable translations, FALSE to disable. */ ){ if (port >=NUMBER_OF_IP_PORTS) { return(ERROR); } nat.port[port].enabled = enable; return(OK);}/******************************************************************************* * natStaticXlatEnable - enable or disable static translations * * Use this routine to enable or disable static translations. ** RETURNS* * OK (success) always. **/STATUS natStaticXlatEnable ( BOOL enable /* TRUE to enable static translations, FALSE to disable. */ ){ nat.static_entries_enabled = enable; return(OK);}/******************************************************************************** natXlatShow - display all translation lists* * This routine displays all translation lists.** RETURNS * * OK (success), or ERROR (failure). **/STATUS natXlatShow(){ int status; printf("\n"); printf("NAT Translation Lists\n"); printf("=====================\n"); status=natTcpXlatShow(); if(status!=OK) { printf("natTcpXlatShow returned %d\n",status); return(status); } if(nat.single_global_address_enabled == TRUE) /* NAPT */ { status=natUdpXlatShow(); if(status!=OK) { printf("natUdpXlatShow returned %d\n",status); return(status); } status=natIcmpXlatShow(); if(status!=OK) { printf("natIcmpXlatShow returned %d\n",status); return(status); } } return(OK);}/****************************************************************************/static void natTcpXlatListShow(TCP_TRANSLATION_ENTRY* tcp_entry, BOOL napt){ char local_addr[INET_ADDR_LEN]; char remote_addr[INET_ADDR_LEN]; int entry_num; struct in_addr iaddr; entry_num = 0; while (tcp_entry != NULL) { if(entry_num==0) { printf("\n"); if (napt == TRUE) { printf("NAT TCP Client Translation List (port-based)\n"); printf("--------------------------------------------\n"); } else { printf("NAT TCP Client Translation List (address-based)\n"); printf("-----------------------------------------------\n"); } printf("# %-*s Port Spoof State Timer" " %-*s Port State Timer Static\n" ,15,"Local Address" ,15,"Remote Address"); } entry_num++; iaddr.s_addr = htonl (tcp_entry->local_address); inet_ntoa_b(iaddr, local_addr); iaddr.s_addr = htonl (tcp_entry->remote_address); inet_ntoa_b(iaddr, remote_addr); printf("%-3d %-*s %5d %5d %5d %5lu" " %-*s %5d %5d %5lu %s\n", entry_num, 15,local_addr, tcp_entry->local_port, tcp_entry->spoofed_local_port, tcp_entry->local_state, tcp_entry->local_connection_timer, 15,remote_addr, tcp_entry->remote_port, tcp_entry->global_state, tcp_entry->global_connection_timer, tcp_entry->static_entry ? "Yes" : "No" ); tcp_entry = (TCP_TRANSLATION_ENTRY *) DLL_NEXT( (DL_NODE *) tcp_entry); }}/**********************************************************************************Description: For NAPT: Show the TCP Client translation entries (NAPT) Show the TCP port-based static entries Show the TCP address-based static entries Show the TCP Client translation entries (Basic NAT) Basic NAT: Show IP translation entries Show TCP Client translation entriesNOTE: NAPT needs to show entries in Basic NAT also because the user's guide specifies that Basic NAT can also work in conjunction with NAPT.***********************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -