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

📄 nat_timer.c

📁 VXWORKS NAT 部分源代码3 有兴趣朋友可以参考下
💻 C
字号:
/* nat_timer.c */

/* Copyright 2000-2003 Wind River Systems, Inc. */

/* @format.tab-size 4, @format.use-tabs true, @format.new-line lf */

/*
modification history
--------------------
01d,16jun03,myz  Call the new fragment translation timer and removed old one.
01c,25apr03,myz  added debug switch check before calling nat_printf in one case
01b,24apr03,zhu  updated copyright
01a,21apr03,myz  replaced swap(_long) with the htons(l) macros, 
                 replaced RWOS list functions with ones in dllLib.c.
040903  vks     updated Copyright info
040303  vks     replaced table_free with free
092502  vvv     modified fragment lists to use lstLib instead of rw_container;
		added semaphore protection (SPR #79675)
082301	tk	Modify check_tcp_translation_entry_timer so that if static TCP
		entry, don't delete entry when timer goes to 0. 
071101	tk	Call natFreeBind to delete TCP, UDP, and IP entry from its 
                respective translation list and the bind list.
062801	tk	Add the ip semaphore lock in check_ip_translation_entry_timer.
060401	tk	Fix the static entry disable problem with static address (IP) 
		translations. The IP static entry got permanently deleted from
                translation list so it can't be revived even when the static 
                entry is reenabled.
050401	tk	Delete TCP/UDP entry from translation list regardless of
		whether it is static or dynamic when its translation timer 
                expires.  The static entries are kept in separate tables.  
                A static flag in the translation list simply indicates the 
                address is bound to a static entry, but the entry shouldn't 
                stay permanently in the translation list as the next TCP/UDP
                connection will require creation of a new control block anyway
               (client's port changes with each connection).
*/
#include "nat.h"
#include "nat_api.h"
/************************************************************************/
void check_tcp_translation_entry_timer (NAT_CURRENCY_TRANSLATION_ENTRY *sptr_tcp_translation_list);
static void check_ip_translation_entry_timer (IP_TRANSLATION_HEADER *sptr_ip_translation_list);
void check_udp_translation_entry_timer (NAT_CURRENCY_TRANSLATION_ENTRY *sptr_udp_translation_list);
void check_icmp_translation_entry_timer (NAT_CURRENCY_TRANSLATION_ENTRY *sptr_icmp_translation_list);
static void check_sequence_entry_timer (SEQUENCE_HEADER *sptr_sequence_list);
extern STATUS 	ifFlagGet (char *interfaceName, int *flags);
extern unsigned char Link_Is_Change;
/************************************************************************/


void nat_timer (void)
{
	unsigned long nat_timeisup_count;
	int if_flag;
	NAT_CURRENCY_TRANSLATION_ENTRY *sptr_translation_entry=NULL;
	NAT_BIND_INFO* bind_info=NULL;

	if(!nat.port[nat.global_port_number].enabled)
	{
		if((ifFlagGet(nat.port[nat.global_port_number].ifname,&if_flag))==ERROR)
		{
			return;
		}
		if(if_flag&0x1)/*interface is up*/
		{
			nat.port[nat.global_port_number].enabled=TRUE;
			nat.port[nat.global_port_number].ifunit=NULL;
		}
		return ;
	}
	if (nat.single_global_address_enabled == TRUE)
	{
		if(Link_Is_Change)
		{
			for(nat_timeisup_count=0;nat_timeisup_count<MAX_NAT_ENTRYS;nat_timeisup_count++)
			{
				if( natTabArray[nat_timeisup_count].local_address== 0 ) 
					continue;
				else
				{
					sptr_translation_entry=&natTabArray[nat_timeisup_count];
					if(sptr_translation_entry->bind_id)
					{
						if(natFreeBind ((u_long)&nat, 0, sptr_translation_entry->bind_id)!=NAT_OK)
						{
							bind_info=(NAT_BIND_INFO*)sptr_translation_entry->bind_id;
							if(bind_info)
							{
								free(bind_info);
								bind_info=NULL;
							}
						}
					}
				
					semTake(natentrylock, WAIT_FOREVER);
					natDelete_entrys(sptr_translation_entry);
					semGive(natentrylock);
				}
			}
			Link_Is_Change=0;
			return;
		}
		for(nat_timeisup_count=0;nat_timeisup_count < MAX_NAT_ENTRYS; nat_timeisup_count++)
		{
			sptr_translation_entry=&natTabArray[nat_timeisup_count];
			if(sptr_translation_entry==NULL)
				continue;
			if(sptr_translation_entry->local_address==0)
			{
				continue;
			}
			if( sptr_translation_entry->static_entry== TRUE)
			{
				continue;
			}
			switch(sptr_translation_entry->protocol_type)
			{
				
				case IPPROTO_ICMP :
					check_icmp_translation_entry_timer(sptr_translation_entry);
					break;
				case IPPROTO_TCP :
					check_tcp_translation_entry_timer(sptr_translation_entry);
					break;
				case IPPROTO_UDP :
					check_udp_translation_entry_timer(sptr_translation_entry);
					break;
				default :
					break;
			}
			
		}
		/*
		check_ip_translation_entry_timer (&nat.natg.ip_translation_list);
		*/
		
	}
	else
	{
		check_ip_translation_entry_timer (&nat.natg.ip_translation_list);
	}
	/*
	natFragTranTimerProcess ();
	*/
}
/************************************************************************/
void check_icmp_translation_entry_timer (NAT_CURRENCY_TRANSLATION_ENTRY *sptr_icmp_translation_entry)
{
	NAT_BIND_INFO* bind_info=NULL;

	if(sptr_icmp_translation_entry->currenty_translation_entry_timer>0)
		sptr_icmp_translation_entry->currenty_translation_entry_timer-=1;
	if(sptr_icmp_translation_entry->currenty_translation_entry_timer<=0)
	{
		
		if(sptr_icmp_translation_entry->bind_id)
		{
			if(natFreeBind ((u_long)&nat, 0, sptr_icmp_translation_entry->bind_id)!=NAT_OK)
			{
				bind_info=(NAT_BIND_INFO*)sptr_icmp_translation_entry->bind_id;
				if(bind_info)
				{
					free(bind_info);
					bind_info=NULL;
				}
			}
		}
		
		semTake(natentrylock, WAIT_FOREVER);
		natDelete_entrys(sptr_icmp_translation_entry);
		semGive(natentrylock);
	}
	return;
}
/************************************************************************/
void check_udp_translation_entry_timer (NAT_CURRENCY_TRANSLATION_ENTRY *sptr_udp_translation_entry)
{
	NAT_BIND_INFO* bind_info=NULL;

    	if(sptr_udp_translation_entry->currenty_translation_entry_timer>0)
		sptr_udp_translation_entry->currenty_translation_entry_timer-=1;
	if(sptr_udp_translation_entry->currenty_translation_entry_timer<=0)
	{
	
		if(sptr_udp_translation_entry->bind_id)
		{
			if(natFreeBind ((u_long)&nat, 0, sptr_udp_translation_entry->bind_id)!=NAT_OK)
			{
				bind_info=(NAT_BIND_INFO*)sptr_udp_translation_entry->bind_id;
				if(bind_info)
				{
					free(bind_info);
					bind_info=NULL;
				}
			}
		}
		
		semTake(natentrylock, WAIT_FOREVER);
		natDelete_entrys(sptr_udp_translation_entry);
		semGive (natentrylock);
	}
	return;
}
/************************************************************************/
void check_tcp_translation_entry_timer (NAT_CURRENCY_TRANSLATION_ENTRY *sptr_tcp_translation_entry)
{
	NAT_BIND_INFO*			bind_item=NULL;
	
	semTake(natentrylock, WAIT_FOREVER);

/*
	if (sptr_tcp_translation_entry->local_connection_timer > 0x00000000L)
	{
		sptr_tcp_translation_entry->local_connection_timer-=2;
	}


	if (sptr_tcp_translation_entry->global_connection_timer > 0x00000000L)
	{
		sptr_tcp_translation_entry->global_connection_timer-=2;
	}
*/
    	if(sptr_tcp_translation_entry->currenty_translation_entry_timer>0/*&&
			sptr_tcp_translation_entry->nat_aging_state == NAT_SYNCH_STATE*/)
    	{
		sptr_tcp_translation_entry->currenty_translation_entry_timer-=1;
    	}
		
	check_sequence_entry_timer (&sptr_tcp_translation_entry->local_sequence_delta_list);
	check_sequence_entry_timer (&sptr_tcp_translation_entry->global_sequence_delta_list);

	if (/*(sptr_tcp_translation_entry->local_connection_timer == 0 &&		
		sptr_tcp_translation_entry->global_connection_timer == 0)||
		(*/sptr_tcp_translation_entry->currenty_translation_entry_timer<=0)
	{
		delete_sequence_entry_list (&sptr_tcp_translation_entry->local_sequence_delta_list);
		delete_sequence_entry_list (&sptr_tcp_translation_entry->global_sequence_delta_list);
		
		semGive(natentrylock);
		
		if(sptr_tcp_translation_entry->bind_id)
		{
			if(natFreeBind ((u_long)&nat, 0, sptr_tcp_translation_entry->bind_id)!=NAT_OK)
			{
				bind_item=(NAT_BIND_INFO*)sptr_tcp_translation_entry->bind_id;
				if(bind_item)
				{
					free(bind_item);
					bind_item=NULL;
				}
			}
		}
		
		semTake(natentrylock, WAIT_FOREVER);
		natDelete_entrys(sptr_tcp_translation_entry);
	}
	semGive(natentrylock);
	
	return;
}
/************************************************************************/
static void check_sequence_entry_timer (SEQUENCE_HEADER *sptr_sequence_list)
{
	SEQUENCE_ENTRY *sptr_sequence_entry;
	SEQUENCE_ENTRY *sptr_next_sequence_entry;
	

	for (sptr_sequence_entry = (SEQUENCE_ENTRY *) DLL_FIRST ((DL_LIST *) sptr_sequence_list);
		sptr_sequence_entry != NULL;
		sptr_sequence_entry = sptr_next_sequence_entry)
	{
		sptr_next_sequence_entry = (SEQUENCE_ENTRY *) DLL_NEXT ((DL_NODE *) sptr_sequence_entry);
		
		if (sptr_sequence_entry->timer_enabled == TRUE)
		{
			if (sptr_sequence_entry->entry_timer > 0x00000000L)
			{
				sptr_sequence_entry->entry_timer-=1;
			}
			else
			{
				
				dllRemove ((DL_LIST *) sptr_sequence_list,
						(DL_NODE *) sptr_sequence_entry);

				free (sptr_sequence_entry);
			}
		}
	}
}	
/************************************************************************/
static void check_ip_translation_entry_timer (IP_TRANSLATION_HEADER *sptr_ip_translation_list)
{
	IP_TRANSLATION_ENTRY *sptr_ip_translation_entry=NULL;
	IP_TRANSLATION_ENTRY *sptr_ip_translation_entry_next=NULL;
	NAT_CURRENCY_TRANSLATION_ENTRY *sptr_tcp_translation_entry=NULL;
	char addr_str[32];

	semTake (ipListLock, WAIT_FOREVER);

	for (sptr_ip_translation_entry = (IP_TRANSLATION_ENTRY *) DLL_FIRST ((DL_LIST *) sptr_ip_translation_list);
		sptr_ip_translation_entry != NULL;
		sptr_ip_translation_entry = sptr_ip_translation_entry_next)
	{
		sptr_ip_translation_entry_next = (IP_TRANSLATION_ENTRY *) DLL_NEXT ((DL_NODE *) sptr_ip_translation_entry);

		sptr_tcp_translation_entry = (NAT_CURRENCY_TRANSLATION_ENTRY *) DLL_FIRST ((DL_LIST *) &sptr_ip_translation_entry->tcp_translation_list);

		if (sptr_tcp_translation_entry != NULL)
		{
			check_tcp_translation_entry_timer (&sptr_ip_translation_entry->tcp_translation_list);
		}
		else if (sptr_ip_translation_entry->time_stamp > 0x00000000L)
		{
			--sptr_ip_translation_entry->time_stamp;
		}
		else if (!sptr_ip_translation_entry->static_entry)
		{
			delete_tcp_connection_entry_list ();

			sptr_ip_translation_entry->sptr_local_address_use_entry->address_in_use = FALSE;

			semGive (ipListLock);	/* need to give semaphore since natFreeBind will take it */
		
			if (nat.logging_enabled == true || nat.printing_enabled == true)
			{
				struct in_addr iaddr;

				iaddr.s_addr = htonl(sptr_ip_translation_entry->sa_local_address);
				inet_ntoa_b(iaddr,addr_str);

				nat_printf (NAT_PRINTF_TRACE, "IP translation entry expired, addr: %s\n",addr_str);
			}

			natFreeBind ((u_long)&nat, 0, sptr_ip_translation_entry->bind_id);

			semTake (ipListLock, WAIT_FOREVER);
		}
	}

	semGive (ipListLock);
}

⌨️ 快捷键说明

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