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

📄 natalgapi.c

📁 VXWORKS NAT 部分源代码3 有兴趣朋友可以参考下
💻 C
📖 第 1 页 / 共 3 页
字号:
					*/
					if (bind_info->direction == NAT_OUTBOUND)
					{
						if (bind_info->use_local_port == TRUE)
						{
							sptr_translation_entry->spoofed_local_port = bind_info->local_transport;
						}
						bind_info->global_transport = sptr_translation_entry->spoofed_local_port;
					}
					sptr_translation_entry->protocol_type = IPPROTO_TCP;
					sptr_translation_entry->direction = bind_info->direction;
					sptr_translation_entry->bind_id = bind_info->id;
					bind_info->nat_transport_entry = (u_long) sptr_translation_entry;
					break;

				case IPPROTO_UDP:
					
					
					semTake(natentrylock, WAIT_FOREVER);
					if ((sptr_translation_entry = natUdpXlatAdd (
						(u_long)local_address, local_port_number,(u_long) remote_address, remote_port_number, static_flag)) == NULL)
					{
							/*free (new_bind_info);*/
							semGive(natentrylock);
							semGive (bindListLock);
							return (NAT_BIND_MAKE_FAILED);
					}
					
					bind_info->global_addr = nat.global_address;
					
					
					/* For outbound session, the global port is the spoofed port unless the 
					   the option to use local port as the global port is set.  However, for
					   inbound session, the global port is the same as the static UDP global
					   port (set by NAT).
					*/
					if (bind_info->direction == NAT_OUTBOUND)
					{
						if (bind_info->use_local_port == TRUE)
						{
							sptr_translation_entry->spoofed_local_port = bind_info->local_transport;
						}
						
						bind_info->global_transport = sptr_translation_entry->spoofed_local_port;
						
					}
					sptr_translation_entry->protocol_type = IPPROTO_UDP;
					sptr_translation_entry->direction = bind_info->direction;
					sptr_translation_entry->bind_id = bind_info->id;
					
					bind_info->nat_transport_entry = (u_long) sptr_translation_entry;
					break;
					
				default:
					
					nat_printf (NAT_PRINTF_ERROR, "natSetBind: Unsupported protocol\n");
					break;
			}			
		}
		else	/* basic NAT */
		{
			new_bind_info = (NAT_BIND_INFO*) malloc (sizeof (NAT_BIND_INFO));

			if (new_bind_info == NULL)
			{
				semGive (bindListLock);
				return(NAT_SYSTEM_ERROR);
			}
			
			bind_info->id = (u_long)new_bind_info;	/* Use item pointer for ID */
			switch (bind_info->protocol)
			{
				case IPPROTO_TCP:

					/* note that in basic NAT, the TCP entry is spawned off the ip translation
					   entry, so must get the IP translation entry first.
					*/
					ip_translation_entry = (IP_TRANSLATION_ENTRY *) bind_info->nat_address_entry;

					
					semTake(natentrylock, WAIT_FOREVER);
					sptr_translation_entry = new_tcp_translation_entry (
							/*&ip_translation_entry->tcp_translation_list, */
							htonl(local_address), 
							htons(local_port_number),
							remote_address, 
							htons(remote_port_number), 
							static_flag);
					
					if (sptr_translation_entry == NULL)
					{
							free (new_bind_info);
							semGive(natentrylock);
							semGive (bindListLock);
							return (NAT_BIND_MAKE_FAILED);
					}
					bind_info->global_addr = ip_translation_entry->sa_global_address;
					/* note that in Basic-NAT, the source port doesn't get translated */
					bind_info->global_transport = sptr_translation_entry->local_port;
					sptr_translation_entry->bind_id = bind_info->id;
					bind_info->nat_transport_entry = (u_long) sptr_translation_entry;
					
					break;

				case IPPROTO_IP:	/* create new IP translation entry */
					semTake(natentrylock, WAIT_FOREVER);
					if (static_flag == FALSE)
					{
						ip_translation_entry = (IP_TRANSLATION_ENTRY *) natIpXlatAdd (local_address, 
												static_flag);
					}
					else	/* create a static IP entry */
					{
						ip_translation_entry = (IP_TRANSLATION_ENTRY *) create_static_ip_entry (
										&nat.natg.ip_translation_list, local_address, global_address);
					}
					
					if (ip_translation_entry == NULL) 
					{
						free (new_bind_info);
						semGive(natentrylock);
                                          semGive (bindListLock);
						return(NAT_BIND_MAKE_FAILED);
					}

					bind_info->global_addr = ip_translation_entry->sa_global_address;					
					bind_info->nat_address_entry = (u_long) ip_translation_entry;
					ip_translation_entry->bind_id = bind_info->id;
					break;
			}
			memcpy(new_bind_info, bind_info, sizeof(NAT_BIND_INFO));
			lstAdd (&nat.bind_list, (NODE*) new_bind_info);
		}

		
		semGive(natentrylock);
		semGive(bindListLock);
		
		return(NAT_OK);
	}
	
	semGive(bindListLock);
	return(NAT_UNSUPPORTED_FEATURE);
}

/*****************************************************************************
Function:	natSetSession

Description:
This function is called by the external agent to create a new session or set 
certain parameters of an existing session.  The caller supplies the specifics 
of the parameters either for a new session or for updating an existing session.  
A new session request is made by setting the session ID in the NAT_SESSION_INFO 
to 0, and non-zero for setting the parameters of an existing session.

If the caller requests for a session creation, and NAT is successful in creating 
a new session, NAT will fill the NAT_SESSION_INFO structure with the newly 
assigned session ID.  If the request is for setting some session parameters and 
the session ID is valid, it will replace the NAT session information with the 
new one.

NOTE:
This function is only partially implemented and has no impact on NAT.

* The external agent calls this routine to create a session or set parameter 
* values in an existing session. The caller supplies the specifics of the 
* parameters either for a new session or for updating an existing session. In 
* NAT_SESSION_INFO, set the session ID to zero (0) to request a new session; 
* set the session ID to non-zero to update the parameters for an existing 
* session.
* 
* If the caller requests session creation, and NAT successfully  creates a 
* session, NAT fills the NAT_SESSION_INFO structure with the newly assigned 
* session ID. If the request is for setting some session parameters and the 
* session ID is valid, it replaces the parameters in the existing session 
* with the new parameters.
* 
* RETURNS
* \is
* \i NAT_OK
* Success.
* \i NAT_INVALID_NAT_ID
* Invalid NAT ID.
* \i NAT_INVALID_AGENT_ID
* Invalid agent ID.
* \i NAT_INVALID_ARG_PTR
* Invalid pointer to session information.
* \i NAT_SYSTEM_ERROR
* Cannot create new session (system is out of memory).
* \i NAT_UNSUPPORTED_FEATURE
* Unsupported feature.
* \ie
* 


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


/******************************************************************************
* 
* natSetSession - create a NAT session or set parameters in an existing session
* 
* 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 natSetSession
    (
    u_long nat_id, 
    u_long agent_id, 
    NAT_SESSION_INFO* session_info
    )
{
	return(NAT_UNSUPPORTED_FEATURE);

}

/*****************************************************************************
Function:	natFreeBind

Description:
This function is called by the external agent to terminate the specified bind 
and any sessions that are based on this bind.
*****************************************************************************/


/******************************************************************************
* 
* natFreeBind - terminate a specified bind
* 
* The external agent uses this routine to remove the specified bind. 
* Typically, the external agent calls this routine to free the bind pertinent 
* to the session upon NAT's event notification that the session has been 
* terminated. The WIND NET NAT service is capable of cleaning up after itself 
* upon each session termination; it internally calls natFreeBind() to remove 
* the bind when the timer value associated with this bind expires. For more 
* information. 
*
* RETURNS
* \is
* \i NAT_OK
* Successful.
* \i NAT_INVALID_NAT_ID
* Invalid NAT service ID.
* \i NAT_INVALID_AGENT_ID
* Invalid agent ID.
* \i NAT_INVALID_BIND_ID
* Invalid bind ID.
* \i NAT_INVALID_BIND_INFO
* Invalid information in the given bind descriptor.
* \i NAT_SYSTEM_ERROR
* Cannot free or delete the bind.
* \ie
*
*/

NAT_STATUS natFreeBind
    (
    u_long nat_id, 
    u_long agent_id, 
    u_long bind_id
    )
{
	NAT_CLASS*				nat_p;
	NAT_BIND_INFO*	bind_info;
	NAT_BIND_INFO*	cur_bind_info;
	BOOL			found;
	NAT_STATUS		status;
	STATUS			entry_delete;
	IP_ADDRESS		local_address;

	nat_p = (NAT_CLASS *) nat_id;

	if (nat_p != &nat)
	{
		return(NAT_INVALID_NAT_ID);
	}

	if ((status = match_nat_agent(nat_id, agent_id)) != NAT_OK)
	{
		return(status);
	}

	if (bind_id == 0)
	{
		return(OK);
	}
	bind_info = (NAT_BIND_INFO *) bind_id;
	local_address = (IP_ADDRESS) bind_info->local_addr;
	found = FALSE;

	semTake(bindListLock, WAIT_FOREVER);
	semTake(natentrylock, WAIT_FOREVER);
	cur_bind_info = (NAT_BIND_INFO *) lstFirst(&nat.bind_list);

	while (cur_bind_info != NULL)
	{
		if (cur_bind_info == bind_info)
		{
			switch (bind_info->protocol)
			{
				#if 0
				case IPPROTO_TCP:

					entry_delete = natTcpXlatDelete(local_address, local_port_number,
							remote_address, remote_port_number);

					if (entry_delete == ERROR)
					{
						semGive(bindListLock);
						nat_printf (NAT_PRINTF_ERROR, 
							"natFreeBind: Failed to free TCP entry with la = %x, lp = %d, ra = %x, rp = %d\n",
							local_address, local_port_number, remote_address, remote_port_number);


						return (NAT_SYSTEM_ERROR);
					}
					break;

				case IPPROTO_UDP:

					entry_delete = natUdpXlatDelete(local_address, local_port_number,
							remote_address, remote_port_number);

					if (entry_delete == ERROR)
					{
						semGive(bindListLock);
						nat_printf (NAT_PRINTF_ERROR, 
							"natFreeBind: Failed to free UDP entry with la = %x, lp = %d, ra = %x, rp = %d\n",
							local_address, local_port_number, remote_address, remote_port_number);


						return (NAT_SYSTEM_ERROR);
					}
					break;
					#endif
				case IPPROTO_IP:

					entry_delete = natIpXlatDelete(local_address);

					if (entry_delete == ERROR)
					{
						semGive(natentrylock);
						semGive(bindListLock);
						return (NAT_SYSTEM_ERROR);
					}
					break;

				default:
					semGive(natentrylock);
					semGive(bindListLock);
					return (NAT_INVALID_BIND_INFO);
			}

			lstDelete (&nat.bind_list, (NODE *) bind_info);
			
			if(bind_info)
			{
				free(bind_info);
				bind_info=NULL;
			}

			found = TRUE;

			break;
		}
	        cur_bind_info = (NAT_BIND_INFO *) lstNext((NODE*) cur_bind_info);
	}

	semGive(natentrylock);
	if (found == FALSE)	/* bind not found */
	{
		semGive(bindListLock);
		return(NAT_INVALID_BIND_ID);	
	}

	semGive(bindListLock);
	return(NAT_OK);
}

/*****************************************************************************
Function:	natFreeSession

Description:
This function is called by the external agent to terminate the specified session.

NOTE:
This function is only partially implemented and has no impact on NAT.

* The external agent calls this routine to terminate the specified session.
* 
* RETURNS
* \is
* \i NAT_OK
* Successful. 
* \i NAT_INVALID_NAT_ID
* Invalid NAT service ID.
* \i NAT_INVALID_AGENT_ID
* Invalid agent ID.
* \i NAT_INVALID_SESSION_ID
* Cannot find the specified session.
* \i NAT_UNSUPPORTED_FEATURE
* Unsupported feature.
* \ie
*

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


/******************************************************************************
* 
* natFreeSession - terminate a specified session
* 
* 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 natFreeSession(u_long nat_id, u_long agent_id, u_long session_id)
{
	return (NAT_UNSUPPORTED_FEATURE);

}

/*****************************************************************************
Function:	natFreeBundle

Description:
This function is called by the external agent to terminate a bundle of sessions 
identified by the same bundle ID.

NOTE:
This function is only partially implemented and has no impact on NAT.

* The external agent calls this routine to terminate a bundle of sessions 
* associated with the specified bundle ID.
* 
* RETURNS
* \is
* \i NAT_OK
* Successful.
* \i NAT_INVALID_NAT_ID
* Invalid NAT service ID.
* \i NAT_INVALID_AGENT_ID
* Invalid agent ID.
* \i NAT_INVALID_BUNDLE_ID
* Cannot find the specified session bundle.
* NAT_UNSUPPORTED_FEATURE
* Unsupported feature.
* \ie
*

⌨️ 快捷键说明

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