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

📄 natalgapi.c

📁 VXWORKS NAT 部分源代码3 有兴趣朋友可以参考下
💻 C
📖 第 1 页 / 共 3 页
字号:

*****************************************************************************/


/******************************************************************************
* 
* natFreeBundle - terminate a bundle of sessions
* 
* This routine is not now supported because the session descriptor 
* is not supported. Calling this routine returns NAT_UNSUPPORTED_FEATURE 
* (defined in natAlgApi.h).
*
*/

NAT_STATUS natFreeBundle
    (u_long nat_id, 
    u_long agent_id, 
    u_long bundle_id
    )
{
	return (NAT_UNSUPPORTED_FEATURE);

}

/*****************************************************************************
Function:	natGetAddrBind

Description:
This function is called by the external agent to obtain address bind 
information.  The caller may specify both or just the local_addr or the 
global_addr and set the other to zero.  NAT will fill up the NAT_BIND_INFO 
data structure with the address bind information unless it cannot find a match 
for the addresses specified.
*****************************************************************************/

/******************************************************************************
* 
* natGetAddrBind - get address bind information
* 
* The external agent calls this routing to get address bind information. The 
* caller can specify both addresses, or specify just the <local_addr> or the 
* <global_addr> and set the other to zero. NAT fills in the NAT_BIND_INFO 
* data structure with the address bind information unless it cannot find a 
* match for the specified address or addresses. If it cannot find the match, 
* it returns NAT_BIND_NO_MATCH.
* 
* RETURNS
* \is
* \i NAT_OK
* Successful.
* \i NAT_INVALID_NAT_ID
* Invalid <nat_id> value.
* \i NAT_INVALID_ARG_PTR
* Invalid pointer to bind information.
* \i NAT_BIND_NO_MATCH
* Cannot find a match to the specified address.
* \ie
*
*/

NAT_STATUS natGetAddrBind
    (
    u_long nat_id, 
    u_long local_addr, 
    u_long global_addr,
    NAT_BIND_INFO* bind_found
    )
{
	NAT_CLASS*				nat_p;	
	BOOL					found;
	NAT_BIND_INFO*			bind_item;

	if (nat_id == 0)
	{
		return(ERROR);
	}

	nat_p = (NAT_CLASS*) nat_id;

	semGive(bindListLock);
	semTake(bindListLock, WAIT_FOREVER);
	if (lstCount(&nat_p->bind_list) == 0)
	{
		semGive(bindListLock);
		nat_printf(NAT_PRINTF_TRACE, "No elements in the bind list \n");
		return(NAT_BIND_NO_MATCH);
	}
	if (bind_found == NULL)
	{
		semGive(bindListLock);
		return(NAT_INVALID_ARG_PTR);
	}

	found = FALSE;

        bind_item = (NAT_BIND_INFO *) lstFirst(&nat.bind_list);

	while (bind_item != NULL)
	{

		if (bind_item->local_addr == local_addr || 
			bind_item->global_addr == global_addr)
		{
			found = TRUE;

			memcpy(bind_found, bind_item, sizeof(NAT_BIND_INFO));

			nat_printf(NAT_PRINTF_TRACE, "Address bind found\n");

			nat_printf(NAT_PRINTF_DATA, "Address bind found for la = %x, ga = %x\n",
				bind_item->local_addr,  bind_item->global_addr);

			break;
		}
		bind_item = (NAT_BIND_INFO *) lstNext((NODE*) bind_item);
	}
	semGive(bindListLock);
	if (found == FALSE) /* bind not found */
	{
		nat_printf(NAT_PRINTF_TRACE, "No address bind match found\n");
		return(NAT_BIND_NO_MATCH);	
	}

	return(NAT_OK);
}

/*****************************************************************************
Function:	natGetTransportBind

Description:
This function is called by the external agent to obtain the transport ID bind 
information.  The caller may specify both or just one of either local or global 
(address and transport).  NAT will fill up the NAT_BIND_INFO data structure 
with the transport bind information unless it cannot find a match for the 
addresses specified.

The "check" flag is added to give the option to do:
FULL,    check for match of remote address and port
PARTIAL, check for match of remote address only

The PARTIAL check option is added for the H.323 ALG who is looking for a bind
match but doesn't know what the remote port is. This is a work around for the
H.323 ALG because instead of decoding the negotiation protocol, it simply does 
a binary search of local address and port in the payload without really reading 
or knowing what the remote port is.  
*****************************************************************************/


/******************************************************************************
* 
* natGetTransportBind - get TCP or UDP bind information.
* 
* The external agent calls this routine to get the transport bind 
* information. The caller may specify both transport addresses, or just one 
* of either the local or global transport address (defined as the tuple of 
* IP address and TCP/UDP port number). NAT searches through its bind 
* descriptor list to find the descriptor that satisfies the specified 
* criteria. If one of the transport address matches is found, the function 
* continues to look for a match for the remote transport address. A search 
* option (FULL or PARTIAL) is provided for the caller to specify whether to 
* search for a bind entry whose remote address and port match the given 
* parameters specified in the NAT_BIND_SESSION structure or just search for 
* a match for the remote address only.
* 
* If a match is found, NAT fills the NAT_BIND_INFO data structure 
* with the matching bind descriptor's information. If no match is found, 
* it returns NAT_BIND_NO_MATCH.
*
* RETURNS
* \is 
* \i NAT_OK
* Successful.
* \i NAT_INVALID_NAT_ID
* Invalid NAT service ID.
* \i NAT_INVALID_ARG_PTR
* Invalid pointer to bind information.
* \i NAT_BIND_NO_MATCH
* Cannot find a match to the specified transport.
* \ie 
*
*/

NAT_STATUS natGetTransportBind
    (
    u_long nat_id,             /* NAT instance ID. */ 
    NAT_BIND_SESSION* session, /* The bind and session information you seek. */
    NAT_BIND_INFO* bind_found, /* Output area for any found bind information. */
    NAT_BIND_CHECK check       /* Search option. */
    )
{
	NAT_CLASS*				nat_p;
	BOOL					found;
	USHORT spoofed_tcp_port;
	NAT_CURRENCY_TRANSLATION_ENTRY *sptr_tcp_entry=NULL;
	
	NAT_BIND_INFO*			bind_item;
	
	
	if (nat_id == 0)
	{
		return(ERROR);
	}
	if (bind_found == NULL)
	{
		
		semGive(bindListLock);
		return(NAT_INVALID_ARG_PTR);
	}

	found = FALSE;
	if(nat.single_global_address_enabled==TRUE)
	{
		for(spoofed_tcp_port=0;spoofed_tcp_port < MAX_NAT_ENTRYS; spoofed_tcp_port++)
		{
			sptr_tcp_entry=&natTabArray[spoofed_tcp_port];
			if (sptr_tcp_entry->protocol_type== session->protocol && 
				((sptr_tcp_entry->local_address== session->local_addr && 
					sptr_tcp_entry->local_port== session->local_transport) ||
				(nat.global_address== session->global_addr &&
					sptr_tcp_entry->spoofed_local_port== session->global_transport)))
			{
				if (check == NAT_BIND_FULL)
				{
					if (sptr_tcp_entry->remote_address== session->remote_addr &&
						sptr_tcp_entry->remote_port== session->remote_transport)
					{
						found = TRUE;
					}
				}
				else
				{
					if (sptr_tcp_entry->remote_address== session->remote_addr)
					{
						found = TRUE;
					}
				}

				if (found == TRUE)
				{
					
					bind_found->direction = sptr_tcp_entry->direction;
					bind_found->global_addr=nat.global_address;
					bind_found->global_transport=sptr_tcp_entry->spoofed_local_port;
					bind_found->local_addr=sptr_tcp_entry->local_address;
					bind_found->local_transport=sptr_tcp_entry->local_port;
					bind_found->protocol=sptr_tcp_entry->protocol_type;
					bind_found->remote_addr=sptr_tcp_entry->remote_address;
					bind_found->remote_transport=sptr_tcp_entry->remote_port;
					bind_found->id=sptr_tcp_entry->bind_id;
					bind_found->static_entry=sptr_tcp_entry->static_entry;
					bind_found->nat_address_entry=0;
					bind_found->nat_transport_entry=(u_long) sptr_tcp_entry;
					/*bind_found->type=sptr_tcp_entry-> ;*/
					
					break;
				}
			}
		}
	}
	else
	{
		semTake(bindListLock, WAIT_FOREVER);
		nat_p = (NAT_CLASS*) nat_id;

		if ( lstCount(&nat_p->bind_list) == 0)
		{
			
			
			semGive(bindListLock);
			nat_printf(NAT_PRINTF_TRACE, "No elements in bind list \n");
			return(NAT_BIND_NO_MATCH);
		}
	 	bind_item = ( NAT_BIND_INFO *) lstFirst(&nat.bind_list);

		while (bind_item != NULL)
		{
			
			if (bind_item->protocol == session->protocol && 
				((bind_item->local_addr == session->local_addr && 
					bind_item->local_transport == session->local_transport) ||
				(bind_item->global_addr == session->global_addr &&
					bind_item->global_transport == session->global_transport)))
			{
				if (check == NAT_BIND_FULL)
				{
					if (bind_item->remote_addr == session->remote_addr &&
						bind_item->remote_transport == session->remote_transport)
					{
						found = TRUE;
					}
				}
				else
				{
					if (bind_item->remote_addr == session->remote_addr)
					{
						found = TRUE;
					}
				}

				if (found == TRUE)
				{
					
					memcpy(bind_found, bind_item, sizeof(NAT_BIND_INFO));

					nat_printf(NAT_PRINTF_TRACE, "Transport bind found\n");

					nat_printf(NAT_PRINTF_DATA, 
						"Transport bind found for la = %x, lp = %d, ga = %x, gp = %d\n",
						bind_item->local_addr, bind_item->local_transport, 
						bind_item->global_addr, bind_item->global_transport);
					
					break;
				}
			}
			bind_item = (NAT_BIND_INFO *) lstNext((NODE*) bind_item);
		}

		semGive(bindListLock);
	}
	if (found == FALSE) /* bind not found */
	{
		nat_printf(NAT_PRINTF_TRACE, "No transport bind match found\n");
		return(NAT_BIND_NO_MATCH);	
	}

	return(NAT_OK);

}

/*****************************************************************************
Function:	natBindShow

Description:
This function is provided for a human user to display all the bind descriptors 
that are currently alive in the list.  It is intended for informational 
purpose only and not for the use by an external agent.
*****************************************************************************/


/******************************************************************************
* 
* natBindShow - display all bind descriptor information
* 
* This routine displays the TCP translation lists.
* 
* This routine displays the information of all bind descriptors still alive 
* (that is, have not been freed) in NAT's bind list. Using this routine 
* along with the natShow() routine may enable testers and developers to 
* detect problems or abnormalities that are occurring in NAT. 
* 
* Note that natBindShow() does not show the static IP address-based entries 
* that typically apply to Basic NAT. To view these entries, you must call 
* the natShow() routine instead.
* 
* SEE ALSO
*
* natShow( )
*/

void natBindShow(void)
{
	char					local_addr[INET_ADDR_LEN];
	char					global_addr[INET_ADDR_LEN];
	char					remote_addr[INET_ADDR_LEN];
	struct 	in_addr			iaddr;
	NAT_BIND_INFO*			bind_item;
	NAT_AGENT_INFO*			agent_info;
	int				entryNo = 0;

	semTake(bindListLock,WAIT_FOREVER);
	bind_item = (NAT_BIND_INFO*) lstFirst(&nat.bind_list);
	while (bind_item != NULL)
	{
		entryNo++;

		iaddr.s_addr = ntohl(bind_item->local_addr);
		inet_ntoa_b(iaddr, local_addr);

		iaddr.s_addr = ntohl(bind_item->global_addr);
		inet_ntoa_b(iaddr, global_addr);

		iaddr.s_addr = ntohl(bind_item->remote_addr);
		inet_ntoa_b(iaddr, remote_addr);

		printf("========== entry %d ==========\n", entryNo);
		printf("Local = %s:%d  ", local_addr, bind_item->local_transport);
		printf("Global = %s:%d  ", global_addr, bind_item->global_transport);
		printf("Remote = %s:%d\n", remote_addr, bind_item->remote_transport);
						
		if (bind_item->type == NAT_BIND_NAPT)
		{
			printf("Type = NAPT\t");
		}
		else
		{
			printf("Type = Basic\t");
		}

		if (bind_item->direction == NAT_OUTBOUND)
		{
			printf("Dir = outbound\t");
		}
		else
		{
			printf("Dir = inbound\t");
		}

		printf("Protocol = ");
		switch (bind_item->protocol)
		{
			case IPPROTO_TCP:
				printf("TCP\t");
				break;
			case IPPROTO_UDP:
				printf("UDP\t");
				break;
			case IPPROTO_IP:
				printf("IP\t");
				break;
			default:
				printf("unknown\t");
				break;
		}

		printf("Static = ");
		if (bind_item->static_entry == TRUE)
		{
			printf("yes\n");
		}
		else
		{
			printf("no\n");
		}

		printf("Bind id = 0x%x\n", (unsigned int) bind_item->id);

		if (bind_item->agent_id == 0)
		{
			printf("Bind created by = NAT\n");
		}
		else
		{
			agent_info = (NAT_AGENT_INFO *) bind_item->agent_id;
			printf("Bind created by = %s\n", agent_info->name);
		}	
		printf("max_idle_time = %d, current_idle_time = %d\n",(int)bind_item->max_idle_time, (int)bind_item->current_idle_time);
		printf("nat_transport_entry = 0x%08X\n", (int)bind_item->nat_transport_entry);
		printf("nat_address_entry = 0x%08X\n", (int)bind_item->nat_address_entry);
		bind_item = (NAT_BIND_INFO *) lstNext((NODE*) bind_item);	
	}
	semGive(bindListLock);
}

⌨️ 快捷键说明

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