📄 nat_udp.c
字号:
checksum_fixup ((BYTE *) &checksum, (BYTE *) &sptr_udp_packet->header.destination_port, sizeof (USHORT), (BYTE *) &local_port_number, sizeof (USHORT)); sptr_udp_packet->header.checksum = checksum; } sptr_udp_packet->header.destination_port = local_port_number; } sptr_udp_translation_entry->udp_translation_entry_timer = nat.udp_translation_entry_timer; address = htonl (sptr_udp_translation_entry->local_address); if (sptr_udp_packet->header.checksum != 0) { checksum_fixup ((BYTE *) &sptr_udp_packet->header.checksum, (BYTE *) &sptr_udp_packet->ip_header.destination_address, sizeof (IP_ADDRESS), (BYTE *) &address, sizeof (IP_ADDRESS)); } checksum = sptr_udp_packet->ip_header.header_checksum; checksum_fixup ((BYTE *) &checksum, (BYTE *) &sptr_udp_packet->ip_header.destination_address, sizeof (IP_ADDRESS), (BYTE *) &address, sizeof (IP_ADDRESS)); sptr_udp_packet->ip_header.destination_address = address; sptr_udp_packet->ip_header.header_checksum = checksum; return (PASS);}/************************************************************************************* This function handles outbound UDP packet translation for NAPT.*************************************************************************************/enum TEST handle_udp_translation_local_rx_nats (UDP_PACKET *sptr_udp_packet){ /* port_number passed is the outbound port number - the one on which packet will be tx */ UDP_TRANSLATION_ENTRY *sptr_udp_translation_entry; USHORT local_spoofed_port_number; IP_ADDRESS address; USHORT checksum; NAT_BIND_INFO bind_info; NAT_STATUS status; sptr_udp_translation_entry = match_ports_with_udp_entry_outbound ( ntohs (sptr_udp_packet->header.destination_port), ntohs (sptr_udp_packet->header.source_port), ntohl (sptr_udp_packet->ip_header.source_address), &nat.nats.udp_translation_list); /* tk: 08/07/01 This code is added for H.323 protocol to avoid duplicate bind entries. The UDP bind might have been created with its global port == local port. In such case, we can use the existing bind entry. */ if (sptr_udp_translation_entry == NULL) { sptr_udp_translation_entry = match_ports_with_udp_entry_global ( ntohs (sptr_udp_packet->header.source_port), ntohl (sptr_udp_packet->ip_header.source_address), &nat.nats.udp_translation_list); } if (sptr_udp_translation_entry == NULL) { /* look for source address match in ip static table. If found, process as basic NAT. */ if (match_sa_with_static_entry ( ntohl (sptr_udp_packet->ip_header.source_address)) == OK) { if (handle_udp_translation_local_rx_natg (sptr_udp_packet) == PASS) { return (PASS); } else { return (FAIL); } } /* no static address match, create new bind in NAPT */ memset(&bind_info,0,sizeof(bind_info)); bind_info.type = NAT_BIND_NAPT; bind_info.direction = NAT_OUTBOUND; bind_info.protocol = IPPROTO_UDP; bind_info.static_entry = FALSE; bind_info.local_addr = ntohl(sptr_udp_packet->ip_header.source_address); bind_info.local_transport = ntohs(sptr_udp_packet->header.source_port); bind_info.remote_addr = ntohl(sptr_udp_packet->ip_header.destination_address); bind_info.remote_transport = ntohs(sptr_udp_packet->header.destination_port); status = natSetBind((u_long)&nat, 0, &bind_info); if(status != NAT_OK) { nat_printf (NAT_PRINTF_ERROR, "handle_udp_translation_local_rx_nats: natSetBind returned %d\n",status); return(FAIL); } sptr_udp_translation_entry = (UDP_TRANSLATION_ENTRY *) bind_info.nat_transport_entry; } /* Replace source ip and port with NAT's global ip and spoofed port. Adjust the checksums in UDP and IP headers. */ if (sptr_udp_translation_entry->spoofed_local_port != 0x0000) { local_spoofed_port_number = htons (sptr_udp_translation_entry->spoofed_local_port); checksum = sptr_udp_packet->header.checksum; if (checksum !=0) { checksum_fixup ((BYTE *) &checksum, (BYTE *) &sptr_udp_packet->header.source_port, sizeof (USHORT), (BYTE *) &local_spoofed_port_number, sizeof (USHORT)); sptr_udp_packet->header.checksum = checksum; } sptr_udp_packet->header.source_port = local_spoofed_port_number; } sptr_udp_translation_entry->udp_translation_entry_timer = nat.udp_translation_entry_timer; address = htonl (nat.global_address); if (sptr_udp_packet->header.checksum != 0) { checksum_fixup ((BYTE *) &sptr_udp_packet->header.checksum, (BYTE *) &sptr_udp_packet->ip_header.source_address, sizeof (IP_ADDRESS), (BYTE *) &address, sizeof (IP_ADDRESS)); } checksum = sptr_udp_packet->ip_header.header_checksum; checksum_fixup ((BYTE *) &checksum, (BYTE *) &sptr_udp_packet->ip_header.source_address, sizeof (IP_ADDRESS), (BYTE *) &address, sizeof (IP_ADDRESS)); sptr_udp_packet->ip_header.source_address = address; sptr_udp_packet->ip_header.header_checksum = checksum; return (PASS);}/***************************************************************************/static enum TEST handle_udp_translation_local_rx_natg (UDP_PACKET *sptr_udp_packet){ IP_ADDRESS source_address; source_address = sptr_udp_packet->ip_header.source_address; if (handle_ip_translation_local_rx_natg ((IP_PACKET *)sptr_udp_packet) != NULL) { if (sptr_udp_packet->header.checksum !=0) { checksum_fixup ((BYTE *) &sptr_udp_packet->header.checksum, (BYTE *) &source_address, sizeof (source_address), (BYTE *) &sptr_udp_packet->ip_header.source_address, sizeof (IP_ADDRESS)); } return (PASS); } else { return (FAIL); } }/************************************************************************/UDP_TRANSLATION_ENTRY *new_udp_translation_entry ( UDP_TRANSLATION_HEADER *sptr_udp_translation_list, IP_ADDRESS local_address, USHORT local_port, IP_ADDRESS remote_address, USHORT remote_port, bool static_entry){ UDP_TRANSLATION_ENTRY *sptr_udp_translation_entry; nat_printf (NAT_PRINTF_DATA, "new_udp_translation_entry: " "local_addr/port=%08lx:%hu remote_addr/port=%08lx:%hu\n" ,local_address, local_port ,remote_address, remote_port); /* Take the spoofing port lock */ semTake (spoofingPortLock, WAIT_FOREVER); if (increment_nat_port_spoofing_number (&nat.current_port_spoofing_number) == FAIL) { nat_printf (NAT_PRINTF_ERROR, "new_udp_translation_entry: increment_nat_port_spoofing_number failed\n"); semGive (spoofingPortLock); return (NULL); } sptr_udp_translation_entry = (UDP_TRANSLATION_ENTRY *) calloc (1, sizeof (UDP_TRANSLATION_ENTRY)); if (sptr_udp_translation_entry == NULL) { nat_printf (NAT_PRINTF_ERROR, "new_udp_translation_entry: NAT failed calloc\n"); semGive (spoofingPortLock); return (NULL); } sptr_udp_translation_entry->udp_translation_entry_timer = nat.udp_translation_entry_timer; sptr_udp_translation_entry->spoofed_local_port = nat.current_port_spoofing_number; sptr_udp_translation_entry->local_port = ntohs (local_port); sptr_udp_translation_entry->remote_port = ntohs (remote_port); sptr_udp_translation_entry->static_entry = static_entry; sptr_udp_translation_entry->local_address = ntohl (local_address); sptr_udp_translation_entry->remote_address = remote_address; dllAdd ((DL_LIST *) sptr_udp_translation_list, (DL_NODE *) sptr_udp_translation_entry); semGive (spoofingPortLock); return (sptr_udp_translation_entry); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -