⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nat_api.c

📁 vxworks NAT 实现部分源代码。有兴趣可以参考下
💻 C
📖 第 1 页 / 共 5 页
字号:
*/

STATUS natXlatPortShow( char protocol,USHORT port)
{
	int 	status;



	if(port>UPPER_EPHEMERAL_PORT_VALUE-1)
		return ERROR;
	
	printf("\n");
	printf("NAT Translation Lists\n");
	printf("=====================\n");

	switch(protocol)
	{
		case IPPROTO_TCP:
			status=natTcpXlatShow(port);

			if(status!=OK)
			{
				printf("natTcpXlatShow returned %d\n",status);
				return(status);
			}
		break;
		case IPPROTO_UDP:
			if(nat.single_global_address_enabled == TRUE)	/* NAPT */
			{
				status=natUdpXlatShow(port);

				if(status!=OK)
				{
					printf("natUdpXlatShow returned %d\n",status);
					return(status);
				}
			}
		break;
		case IPPROTO_ICMP:
			if(nat.single_global_address_enabled == TRUE)	/* NAPT */
			{
				status=natIcmpXlatShow(port);

				if(status!=OK)
				{
					printf("natIcmpXlatShow returned %d\n",status);
					return(status);
				}
			}
		break;
		default:
			return ERROR;
	}
	return(OK);
}

void natTcpXlatListShow(NAT_CURRENCY_TRANSLATION_ENTRY * tcp_entry, BOOL napt,unsigned short enumber)
{
	char	local_addr[INET_ADDR_LEN];
	char	remote_addr[INET_ADDR_LEN];
	unsigned short		entry_num;
	struct 	in_addr iaddr;

	entry_num = enumber;

	while (tcp_entry != NULL) 
	{
		if(tcp_entry->protocol_type==IPPROTO_TCP)
		{
		
			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");
			}

			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_entry->next_entrys_link;
	}
	return;
}

/**********************************************************************************
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 entries

NOTE:
	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.
***********************************************************************************/

/******************************************************************************
* 
* natTcpXlatShow - display all TCP translation lists
* 
* This routine displays the TCP translation lists.
*
* RETURNS
* 
* OK (success) always.
*
*/

STATUS natTcpXlatShow(USHORT port)
{
	unsigned long out_porthash;
	NAT_CURRENCY_TRANSLATION_ENTRY *	tcp_entry=NULL;
	unsigned short enumber=0;
	
	if(nat.single_global_address_enabled == TRUE)	/* NAPT */
	{

		/* Dynamic List (may contain both static and dynamic entries) */
		out_porthash=port&(UPPER_EPHEMERAL_PORT_VALUE-1);
		tcp_entry = out_port_map[out_porthash].next_entrys_link;

		natTcpXlatListShow(tcp_entry, TRUE,enumber);	/* show port-based translations */
		enumber++;
	}
	return(OK);
}


/******************************************************************************
* 
* natUdpXlatShow - display all UDP translation lists
* 
* This routine displays the UDP translation lists.
*
* RETURNS
* 
* OK (success), or ERROR (failure). 
*
*/

STATUS natUdpXlatShow(USHORT port)
{
	char					local_addr[INET_ADDR_LEN];
	char					remote_addr[INET_ADDR_LEN];
	int						entry_num;
	NAT_CURRENCY_TRANSLATION_ENTRY*	udp_entry=NULL;
	unsigned long out_porthash;
	struct in_addr			iaddr;

	if(nat.single_global_address_enabled == TRUE)	/* NAPT */
	{
		entry_num = 0;
		out_porthash=port&(UPPER_EPHEMERAL_PORT_VALUE-1);
		udp_entry = out_port_map[out_porthash].next_entrys_link;

		/* Dynamic List (may contain static or dynamic entries) */
		while (udp_entry != NULL) 
		{
			if(udp_entry->protocol_type==IPPROTO_UDP)
			{
				if(entry_num==0)
				{
					printf("\n");
					printf("NAT UDP Client Translation List\n");
					printf("-------------------------------\n");
					printf("#   %-*s  Port Spoof            "
							  "   %-*s  Port       Timer Static\n"
						,15,"Local Address"
						,15,"Remote Address");
				}

				entry_num++;

				iaddr.s_addr = htonl (udp_entry->local_address);
				inet_ntoa_b(iaddr, local_addr);

				iaddr.s_addr = htonl (udp_entry->remote_address);
				inet_ntoa_b(iaddr, remote_addr);

				
				printf("%-3d %-*s %5d %5d            " 
						 "   %-*s %5d       %5lu %s\n",
					entry_num, 
					15,local_addr, 
					udp_entry->local_port,
					udp_entry->spoofed_local_port,
					15,remote_addr,
					udp_entry->remote_port,
					udp_entry->currenty_translation_entry_timer,
					udp_entry->static_entry ? "Yes" : "No"
					);
			}
			udp_entry =udp_entry->next_entrys_link;
		}

		/* Static List (static entries only) */

		return (OK);
	}
	
	/* Basic NAT */

	return(ERROR);	/* Use natXlatShow instead */
}



/******************************************************************************
* 
* natIcmpXlatShow - display all ICMP translation lists
* 
* This routine displays the Internet Control Message Protocol (ICMP)
* translation lists.
*
* 'Note:' This routine is accessible in NAPT mode only.
*
* RETURNS
* 
* OK (success), or ERROR (failure). 
*
*/

STATUS natIcmpXlatShow(USHORT port)
{
	char					local_addr[INET_ADDR_LEN];
	int						entry_num;
	NAT_CURRENCY_TRANSLATION_ENTRY*	icmp_entry=NULL;
	struct in_addr			iaddr;
	unsigned long out_porthash;

	if(nat.single_global_address_enabled == TRUE)	/* NAPT */
	{
		entry_num = 0;
		out_porthash=port&(UPPER_EPHEMERAL_PORT_VALUE-1);
		icmp_entry = out_port_map[out_porthash].next_entrys_link;

		while (icmp_entry != NULL) 
		{
			if(icmp_entry->protocol_type==IPPROTO_ICMP)
			{
				if(entry_num==0)
				{
					printf("\n");
					printf("NAT ICMP Client Translation List\n");
					printf("--------------------------------\n");
					printf("#   %-*s    ID Spoof       Timer\n"
						,15,"Local Address");
				}

				entry_num++;

				iaddr.s_addr = htonl(icmp_entry->local_address);
				inet_ntoa_b(iaddr, local_addr);

				printf("%-3d %-*s %5d %5d       %5lu\n" ,
					entry_num, 
					15,local_addr, 
					icmp_entry->local_port,
					icmp_entry->spoofed_local_port,
					icmp_entry->currenty_translation_entry_timer
					);
			}
			icmp_entry = icmp_entry->next_entrys_link;
		}

		return (OK);
	}

	/* Basic NAT */
	
	return(ERROR); /* Use natXlatShow instead */
}
/******************************************************************************
*
* natXlatAllShow - display all translation lists
* 
* This routine displays all translation lists.
*
* RETURNS 
* 
* OK (success), or ERROR (failure). 
*
*/
STATUS natXlatAllShow(void)
{
	unsigned long nat_count;
	struct in_addr			iaddr;
	char					local_addr[INET_ADDR_LEN];
	char					remote_addr[INET_ADDR_LEN];
	NAT_CURRENCY_TRANSLATION_ENTRY *sptr_translation_entry=NULL;
	unsigned short tcpenumber=0,icmpenumber=0,udpenumber=0;
	

	printf("\n");
	printf("NAT Translation Lists\n");
	printf("=====================\n");
	for(nat_count=0;nat_count < MAX_NAT_ENTRYS; nat_count++)
	{
		sptr_translation_entry=&natTabArray[nat_count];
		if(sptr_translation_entry==NULL)
			continue;
		if(sptr_translation_entry->local_address==0)
		{
			continue;
		}
		switch(sptr_translation_entry->protocol_type)
		{
			case IPPROTO_TCP:
				if(nat.single_global_address_enabled == TRUE)	
					natTcpXlatListShow(sptr_translation_entry,TRUE,tcpenumber);
				else
					natTcpXlatListShow(sptr_translation_entry,FALSE,tcpenumber);
				tcpenumber++;
			break;
			case IPPROTO_UDP:
				if(nat.single_global_address_enabled == TRUE)	/* NAPT */
				{
					if(udpenumber==0)
					{
						printf("\n");
						printf("NAT UDP Client Translation List\n");
						printf("-------------------------------\n");
						printf("#   %-*s  Port Spoof            "
								  "   %-*s  Port       Timer Static\n"
							,15,"Local Address"
							,15,"Remote Address");
					}

					iaddr.s_addr = htonl (sptr_translation_entry->local_address);
					inet_ntoa_b(iaddr, local_addr);

					iaddr.s_addr = htonl (sptr_translation_entry->remote_address);
					inet_ntoa_b(iaddr, remote_addr);

					
					printf("%-3d %-*s %5d %5d            " 
							 "   %-*s %5d       %5lu %s\n",
						udpenumber, 
						15,local_addr, 
						sptr_translation_entry->local_port,
						sptr_translation_entry->spoofed_local_port,
						15,remote_addr,
						sptr_translation_entry->remote_port,
						sptr_translation_entry->currenty_translation_entry_timer,
						sptr_translation_entry->static_entry ? "Yes" : "No"
						);
						udpenumber++;
					}
			break;
			case IPPROTO_ICMP:
				if(nat.single_global_address_enabled == TRUE)	/* NAPT */
				{
					if(icmpenumber==0)
					{
						printf("\n");
						printf("NAT ICMP Client Translation List\n");
						printf("--------------------------------\n");
						printf("#   %-*s    ID Spoof       Timer\n"
							,15,"Local Address");
					}

					iaddr.s_addr = htonl(sptr_translation_entry->local_address);
					inet_ntoa_b(iaddr, local_addr);

					printf("%-3d %-*s %5d %5d       %5lu\n" ,
						icmpenumber, 
						15,local_addr, 
						sptr_translation_entry->local_port,
						sptr_translation_entry->spoofed_local_port,
						sptr_translation_entry->currenty_translation_entry_timer
						);
					icmpenumber++;
				}
			break;
			default:
				break;
		}
		
	}
	return (OK);
}
#endif
#ifdef NAT_PASS_THRU_ENABLE
/************************************************************************
Description:
	Add an address/mask pair to the pass through list.  Outbound packets
	destined to this address/mask will not be translated by NAT.
************************************************************************/
STATUS natPassThruListAdd(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))
		{
			printf("Entry already exists in pass through list.\n");
			return (ERROR);
		}
		p_pair = (NAT_PASSTHRU_PAIR *)lstNext((NODE*)p_pair);	
	}

	/* new entry, add to container */

	p_pair = (NAT_PASSTHRU_PAIR *) malloc (sizeof (NAT_PASSTHRU_PAIR));

	if (p_pair == NULL)
	{
		printf("Can't allocate memory!\n");
		return(ERROR);
	}

	p_pair->address = addr;
	p_pair->mask = msk;
	lstAdd (&nat.passthru_list, (NODE *) p_pair);

	return(OK);
}

/************************************************************************
Description:
	Delete an address/mask pair to the pass through list.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -