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

📄 natalgapi.c

📁 vxworks下ppp的实现源码
💻 C
📖 第 1 页 / 共 3 页
字号:
					if ((tcp_translation_entry = (TCP_TRANSLATION_ENTRY *) natTcpXlatAdd (						(u_long)local_address, local_port_number,						(u_long) remote_address, remote_port_number, static_flag)) == NULL)						{							free (new_bind_info);							semGive (bindListLock);							nat_printf(NAT_PRINTF_ERROR, 								"natSetBind: Failed to create TCP bind la = %x, lp = %d, ra = %x, rp = %d\n",								local_address, local_port_number, remote_address, remote_port_number);							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 TCP global					   port (set by NAT).					*/					if (bind_info->direction == NAT_OUTBOUND)						{						if (bind_info->use_local_port == TRUE)							{							tcp_translation_entry->spoofed_local_port = bind_info->local_transport;							}						bind_info->global_transport = tcp_translation_entry->spoofed_local_port;						}										tcp_translation_entry->bind_id = bind_info->id;					bind_info->nat_transport_entry = (u_long) tcp_translation_entry;					nat_printf(NAT_PRINTF_TRACE,						"New TCP bind created\n");					nat_printf(NAT_PRINTF_DATA, 						"New TCP bind: la = %x, lp = %d, ga = %x, gp = %d, ra = %x, rp = %d\n",						bind_info->local_addr, bind_info->local_transport,						bind_info->global_addr, bind_info->global_transport,						bind_info->remote_addr, bind_info->remote_transport);					break;				case IPPROTO_UDP:					if ((udp_translation_entry = (UDP_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 (bindListLock);							nat_printf(NAT_PRINTF_ERROR, 								"natSetBind: Failed to create UDP bind la = %x, lp = %d, ra = %x, rp = %d\n",								local_address, local_port_number, remote_address, remote_port_number);							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)							{							udp_translation_entry->spoofed_local_port = bind_info->local_transport;							}						bind_info->global_transport = udp_translation_entry->spoofed_local_port;						}										udp_translation_entry->bind_id = bind_info->id;					bind_info->nat_transport_entry = (u_long) udp_translation_entry;					nat_printf(NAT_PRINTF_TRACE,						"New UDP bind created\n");					nat_printf(NAT_PRINTF_DATA, 						"New UDP bind: la = %x, lp = %d, ga = %x, gp = %d, ra = %x, rp = %d\n",						bind_info->local_addr, bind_info->local_transport,						bind_info->global_addr, bind_info->global_transport,						bind_info->remote_addr, bind_info->remote_transport);					break;									default:					semGive (bindListLock);					nat_printf (NAT_PRINTF_ERROR, "natSetBind: Unsupported protocol\n");					break;				}						}		else	/* basic NAT */			{			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 (tcpListLock, WAIT_FOREVER);					tcp_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 (tcp_translation_entry == NULL)						{							nat_printf(NAT_PRINTF_ERROR, 								"natSetBind: Failed to create TCP bind la = %x, lp = %d, ra = %x, rp = %d\n",								local_address, local_port_number, remote_address, remote_port_number);									free (new_bind_info);							semGive (tcpListLock);							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 = tcp_translation_entry->local_port;					tcp_translation_entry->bind_id = bind_info->id;					bind_info->nat_transport_entry = (u_long) tcp_translation_entry;					semGive (tcpListLock);					nat_printf(NAT_PRINTF_TRACE,						"New TCP bind created off IP translation\n");					nat_printf(NAT_PRINTF_DATA, 						"New TCP bind off IP: la = %x, lp = %d, ga = %x, gp = %d, ra = %x, rp = %d\n",						bind_info->local_addr, bind_info->local_transport,						bind_info->global_addr, bind_info->global_transport,						bind_info->remote_addr, bind_info->remote_transport);										break;				case IPPROTO_IP:	/* create new IP translation entry */					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 (bindListLock);						nat_printf(NAT_PRINTF_ERROR, 								"natSetBind: Failed to create IP bind la = %x\n", local_address);						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;					nat_printf(NAT_PRINTF_TRACE,						"New IP bind created\n");					nat_printf(NAT_PRINTF_DATA, 						"New IP bind: la = %x, ga = %x\n",						bind_info->local_addr, bind_info->global_addr);					break;				}			}		memcpy(new_bind_info, bind_info, sizeof(NAT_BIND_INFO));		lstAdd (&nat.bind_list, (NODE*) new_bind_info);		semGive(bindListLock);		return(NAT_OK);		}#if 0	/* setting existing bind descriptor's parameters is not supported */	new_bind_info = (NAT_BIND_INFO*) bind_info->id;	/* won't be NULL */	new_bind_info->agent_id = agent_id;	memcpy(bind_info, new_bind_info, sizeof(NAT_BIND_INFO));#endif	semGive(bindListLock);	return(NAT_UNSUPPORTED_FEATURE);}/*****************************************************************************Function:	natSetSessionDescription: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:	natFreeBindDescription: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. ** See also the <WIND NET NAT User's Guide> for a description of callback * functions within an external agent provided.* * 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;	u_short			local_port_number, remote_port_number;	IP_ADDRESS		local_address, remote_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(NAT_INVALID_BIND_ID);		}	bind_info = (NAT_BIND_INFO *) bind_id;	local_address = (IP_ADDRESS) bind_info->local_addr;	local_port_number = bind_info->local_transport;	remote_address = (IP_ADDRESS) bind_info->remote_addr;	remote_port_number = bind_info->remote_transport;	found = FALSE;	semTake(bindListLock, 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)				{				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;				case IPPROTO_IP:					entry_delete = natIpXlatDelete(local_address);					if (entry_delete == ERROR)						{						semGive(bindListLock);						nat_printf (NAT_PRINTF_ERROR, 							"natFreeBind: Failed to free IP entry with la = %x\n", local_address);						return (NAT_SYSTEM_ERROR);						}					break;				default:					semGive(bindListLock);					nat_printf(NAT_PRINTF_ERROR, "natFreeBind: Unknown protocol\n");					return (NAT_INVALID_BIND_INFO);				}			lstDelete (&nat.bind_list, (NODE *) bind_info);					free(bind_info);			found = TRUE;			break;			}	        cur_bind_info = (NAT_BIND_INFO *) lstNext((NODE*) cur_bind_info);		}	if (found == FALSE)	/* bind not found */		{		semGive(bindListLock);		return(NAT_INVALID_BIND_ID);			}	semGive(bindListLock);	return(NAT_OK);}/*****************************************************************************Function:	natFreeSessionDescription:This function is called by the external agent to terminate the specified session.NOTE:

⌨️ 快捷键说明

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