📄 nat_api.c
字号:
ipList = &nat.natg.ip_translation_list; localAddress = (IP_ADDRESS) ntohl(inet_addr(localAddr)); globalAddress = (IP_ADDRESS) ntohl(inet_addr(globalAddr)); if (localAddress == 0 || globalAddress == 0) { printf("Give a valid IP address.\n"); return (ERROR); } if (match_sa_with_local_address (localAddress, ipList) != NULL || match_sa_with_global_address (globalAddress, ipList) != NULL) { printf("Local/global address is in use.\n"); return (ERROR); } /* create a new static IP translation entry */ memset(&bind_info,0,sizeof(bind_info)); bind_info.agent_id = 0; /* agent is NAT */ bind_info.type = NAT_BIND_BASIC; bind_info.direction = NAT_OUTBOUND; bind_info.protocol = IPPROTO_IP; bind_info.static_entry = TRUE; bind_info.local_addr = localAddress; bind_info.global_addr = globalAddress; status = natSetBind((u_long)&nat, 0, &bind_info); if(status != NAT_OK) { printf ("Failed to bind the requested static entry, status = %d\n", status); return(ERROR); } return (OK);}/******************************************************************************Description: Delete an IP static address translation entry (Basic NAT only).******************************************************************************/STATUS natIpStaticDelete(char *localAddr, char *globalAddr){ IP_ADDRESS localAddress, globalAddress; IP_TRANSLATION_HEADER *ipList; IP_TRANSLATION_ENTRY *ipEntryLoc, *ipEntryGlob; NAT_STATUS status; ipList = &nat.natg.ip_translation_list; localAddress = (IP_ADDRESS) ntohl(inet_addr(localAddr)); globalAddress = (IP_ADDRESS) ntohl(inet_addr(globalAddr)); if (localAddress == 0 || globalAddress == 0) { printf("Give a valid IP address.\n"); return (ERROR); } ipEntryGlob = match_sa_with_global_address (globalAddress, ipList); if (ipEntryGlob == NULL) { printf("Given global address not found.\n"); return (ERROR); } ipEntryLoc = match_sa_with_local_address (localAddress, ipList); if (ipEntryLoc == NULL) { printf("Given local address not found.\n"); return (ERROR); } if (ipEntryGlob != ipEntryLoc) { printf("Can't find the match for given local/global address pair\n"); return (ERROR); } if (ipEntryLoc->static_entry == FALSE) { printf("Given local/global address pair is not a static entry\n"); return (ERROR); } status = natFreeBind((u_long)&nat, 0, ipEntryLoc->bind_id); if (status != NAT_OK) { printf("Failed to delete static IP entry, status = %d\n", status); return (ERROR); } return (OK);}#endif /* _STATIC_IP_API *//*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ The following NAT API functions are intended for use by software agent to interact with NAT core, such as calls to create TCP, UDP, and IP control blocks. For example, these api functions may be called by ALG API functions to create a translation entry. NOTE: Unlike the API functions defined above, all the API functions defined below are not intended for user to run from the command shell.@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*//*******************************************************************************Description: This function is called to add a translation entry in the TCP translation list (effectively to add TCP control block). Since the tuples of address and port for both the source and destination must be known, this function is intended for use by an external software agent (e.g. ALG) as one does not usually know what the client's port number will be (until after the packet is received). If the static_entry is set to TRUE, the TCP control block will not be deleted once its default time stamp expires. Subsequent traffic to the same destination from the same source will reuse the same TCP control block and reset the time stamp to its default value. Returns: Pointer to the new translation entry NULL, failed to add the entry (e.g. memory allocation error)*******************************************************************************/u_long natTcpXlatAdd(u_long localAddr, u_short localPort, u_long remoteAddr, u_short remotePort, BOOL static_entry){ TCP_TRANSLATION_ENTRY* new_tcp_entry; TCP_TRANSLATION_HEADER* xlat_list; if (nat.single_global_address_enabled == TRUE) /* NAPT */ { xlat_list=&nat.nats.tcp_translation_list; } else /* Basic NAT */ { printf("This function applies to NAPT only.\n"); return((u_long)NULL); } semTake (tcpListLock, WAIT_FOREVER); new_tcp_entry = new_tcp_translation_entry ( xlat_list, htonl(localAddr), htons(localPort), remoteAddr, htons(remotePort), static_entry); semGive (tcpListLock); return((u_long)new_tcp_entry);}/**********************************************************************************Description: This function is called by the external agent to delete a translation entry from the TCP translation list. This function is intended for use by an external software agent (e.g. ALG) only.Returns: OK, ERROR, entry not found in the translation list**********************************************************************************/STATUS natTcpXlatDelete(u_long localAddr, u_short localPort, u_long remoteAddr, u_short remotePort){ IP_TRANSLATION_ENTRY* ip_entry; TCP_TRANSLATION_ENTRY* tcp_entry; semTake (tcpListLock, WAIT_FOREVER); if(nat.single_global_address_enabled == TRUE) /* NAPT */ { tcp_entry = (TCP_TRANSLATION_ENTRY *) DLL_FIRST( (DL_LIST *) &nat.nats.tcp_translation_list); while (tcp_entry != NULL) { if(tcp_entry->local_address == localAddr && tcp_entry->local_port == localPort && tcp_entry->remote_address == remoteAddr && tcp_entry->remote_port == remotePort) { dllRemove ((DL_LIST *) &nat.nats.tcp_translation_list, (DL_NODE *)tcp_entry); free(tcp_entry); nat_printf(NAT_PRINTF_TRACE, "TCP entry deleted\n"); semGive (tcpListLock); return(OK); } tcp_entry = (TCP_TRANSLATION_ENTRY *) DLL_NEXT( (DL_NODE *) tcp_entry); } } /* Basic NAT */ ip_entry = (IP_TRANSLATION_ENTRY *) DLL_FIRST( (DL_LIST *) &nat.natg.ip_translation_list); while (ip_entry != NULL) { tcp_entry = (TCP_TRANSLATION_ENTRY *) DLL_FIRST( (DL_LIST *) &ip_entry->tcp_translation_list); while (tcp_entry != NULL) { if(tcp_entry->local_address == localAddr && tcp_entry->local_port == localPort && tcp_entry->remote_address == remoteAddr && tcp_entry->remote_port == remotePort) { dllRemove ((DL_LIST *) &ip_entry->tcp_translation_list, (DL_NODE *)tcp_entry); free(tcp_entry); semGive (tcpListLock); return(OK); } tcp_entry = (TCP_TRANSLATION_ENTRY *) DLL_NEXT( (DL_NODE *) tcp_entry); } ip_entry = (IP_TRANSLATION_ENTRY *) DLL_NEXT( (DL_NODE *) ip_entry); } semGive (tcpListLock); return(ERROR); /* Translation entry not found */}/**********************************************************************************Description: This function is called to add a translation entry in the UDP translation list (effectively to add UDP control block). Since the tuples of address and port for both the source and destination must be known, this function is intended for use by an external software agent (e.g. ALG) as one does not usually know what the client's port number will be (until after the packet is received). If the static_entry is set to TRUE, the UDP control block will not be deleted once its default time stamp expires. Subsequent traffic to the same destination from the same source will reuse the same UDP control block and reset the time stamp to its default value. Returns: Pointer to the new translation entry NULL, failed to add the entry (e.g. memory allocation error)**********************************************************************************/u_long natUdpXlatAdd(u_long localAddr, u_short localPort, u_long remoteAddr, u_short remotePort, BOOL static_entry){ UDP_TRANSLATION_ENTRY* new_udp_entry; UDP_TRANSLATION_HEADER* xlat_list; if (nat.single_global_address_enabled == TRUE) /* NAPT */ { xlat_list=&nat.nats.udp_translation_list; } else /* Basic NAT */ { printf("This function applies to NAPT only.\n"); return((u_long)NULL); } semTake (udpListLock, WAIT_FOREVER); new_udp_entry = new_udp_translation_entry (xlat_list, htonl(localAddr), htons(localPort), remoteAddr, htons(remotePort), static_entry); semGive (udpListLock); return((u_long)new_udp_entry);}/***********************************************************************************Description: This function is called by the external agent to delete a translation entry from the TCP translation list. This function is intended for use by an external software agent (e.g. ALG) only.Returns: OK, ERROR, entry not found in the translation list***********************************************************************************/STATUS natUdpXlatDelete(u_long localAddr, u_short localPort, u_long remoteAddr, u_short remotePort){ UDP_TRANSLATION_ENTRY* udp_entry; if(nat.single_global_address_enabled == TRUE) /* NAPT */ { semTake (udpListLock, WAIT_FOREVER); udp_entry = (UDP_TRANSLATION_ENTRY *) DLL_FIRST( (DL_LIST *) &nat.nats.udp_translation_list); while (udp_entry != NULL) { if(udp_entry->local_address == localAddr && udp_entry->local_port == localPort && udp_entry->remote_address == remoteAddr && udp_entry->remote_port == remotePort) { dllRemove ((DL_LIST *) &nat.nats.udp_translation_list, (DL_NODE *)udp_entry); free(udp_entry); nat_printf(NAT_PRINTF_TRACE, "UDP entry deleted\n"); semGive (udpListLock); return(OK); } udp_entry = (UDP_TRANSLATION_ENTRY *) DLL_NEXT( (DL_NODE *) udp_entry); } semGive (udpListLock); return (ERROR); } /* Basic NAT */ return(ERROR); /* Use natXlatDelete instead */}/*******************************************************************************Description: This function is called to add an address (IP) translation entry in the IP translation list. Returns: Pointer to the new translation entry NULL, failed to add the entry (e.g. memory allocation error)*******************************************************************************/u_long natIpXlatAdd(u_long localAddr, BOOL static_entry){ IP_TRANSLATION_ENTRY* new_ip_entry; semTake (ipListLock, WAIT_FOREVER); new_ip_entry = new_ip_translation_entry ( &nat.natg.ip_translation_list, localAddr, static_entry); semGive (ipListLock); return((u_long)new_ip_entry);}/*******************************************************************************Description: This function is called to delete an address (IP) translation entry in the IP translation list.*******************************************************************************/STATUS natIpXlatDelete(u_long localAddr){ IP_TRANSLATION_ENTRY* ip_entry; struct in_addr iaddr; char asciiAddr[INET_ADDR_LEN]; semTake (ipListLock, WAIT_FOREVER); ip_entry = (IP_TRANSLATION_ENTRY *) DLL_FIRST( (DL_LIST *) &nat.natg.ip_translation_list); while (ip_entry != NULL) { if (ip_entry->sa_local_address == localAddr) { iaddr.s_addr = htonl(ip_entry->sa_global_address); inet_ntoa_b(iaddr, asciiAddr); arpDelete(asciiAddr); dllRemove ((DL_LIST *) &nat.natg.ip_translation_list ,(DL_NODE *)ip_entry); free(ip_entry); semGive (ipListLock); return(OK); } ip_entry = (IP_TRANSLATION_ENTRY *) DLL_NEXT( (DL_NODE *) ip_entry); } semGive (ipListLock); return(ERROR); /* Translation entry not found */}/* API to get the ethernet address of the given port number */STATUS natEtherAddr(int portNum){ struct ifnet *pIf; char targetAddr[INET_ADDR_LEN]; unsigned char eHdr[6]; struct in_addr iaddr; char buf[18]; pIf = nat.port[portNum].ifunit; iaddr.s_addr = htonl(nat.port[portNum].address); inet_ntoa_b(iaddr, targetAddr); printf("internet address = %s\n", targetAddr); if (etherAddrResolve(pIf, targetAddr, (char *)eHdr, 2, 5) == OK) { sprintf(buf, "%x:%x:%x:%x:%x:%x", eHdr[0], eHdr[1], eHdr[2], eHdr[3], eHdr[4], eHdr[5]); printf("ethernet address = "); printf("%s",buf); printf("\n"); return(OK); } return(ERROR);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -