public void freeIP(xbs_nat_entry entry) { lock (ip_pool) { entry.original_source_ip = null; entry.original_source_ip_bytes = null; entry.source_mac = null; entry.last_change_time = DateTime.Now; last_update = entry.last_change_time; CountOfUsedIPs--; } }
public xbs_nat_entry requestIP(IPAddress originalIP, PhysicalAddress mac) { xbs_nat_entry entry = null; lock (ip_pool) { if (CountOfUsedIPs == ip_pool.Count) { return(null); } entry = ip_pool.Find(isFreeEntry); if (entry == null) { return(null); } entry.original_source_ip = originalIP; entry.original_source_ip_bytes = originalIP.GetAddressBytes(); entry.source_mac = mac; entry.last_change_time = DateTime.Now; last_update = entry.last_change_time; CountOfUsedIPs++; } return(entry); }
private void replaceDestinationIpWithOriginalIP(ref byte[] data, EthernetPacketType ethernet_packet_type, ref xbs_nat_entry nat_entry) { replaceDestinationIP(ref data, ethernet_packet_type, ref nat_entry.original_source_ip_bytes); }
private void replaceSourceIpWithNATSourceIP( ref byte[] data, EthernetPacketType ethernet_packet_type, ref xbs_nat_entry nat_entry) { int offset = (ethernet_packet_type == EthernetPacketType.Arp) ? ARP_HEADER_SOURCE_OFFSET : IP_HEADER_SOURCE_OFFSET; Buffer.BlockCopy(nat_entry.natted_source_ip_bytes, 0, data, offset, 4); }
private bool compare_tmpIP(xbs_nat_entry entry) { bool ret = (entry.natted_source_ip.Equals(_tmpIP)); return ret; }
private bool isFreeEntry(xbs_nat_entry entry) { return (entry.source_mac == null); }
private void replaceSourceIpWithNATSourceIP(ref byte[] data, EthernetPacketType ethernet_packet_type, ref xbs_nat_entry nat_entry) { int offset = (ethernet_packet_type == EthernetPacketType.Arp) ? ARP_HEADER_SOURCE_OFFSET : IP_HEADER_SOURCE_OFFSET; Buffer.BlockCopy(nat_entry.natted_source_ip_bytes, 0, data, offset, 4); }
/* * public EthernetPacketType deNAT_outgoing_packet(ref byte[] data, PhysicalAddress dstMAC, PhysicalAddress srcMAC) * { * EthernetPacketType ethernet_packet_type = getEthernetPacketType(ref data); * if (!isIpOrArpPacket(ethernet_packet_type)) * return ethernet_packet_type; * xbs_nat_entry nat_entry = null; * lock (NAT_list) * { * if (NAT_list.ContainsKey(dstMAC)) * nat_entry = NAT_list[dstMAC]; * } * * if (nat_entry != null) * { * replaceDestinationIpWithOriginalIP(ref data, ethernet_packet_type, ref nat_entry); * updateIPChecksums(ref data); * } * return ethernet_packet_type; * } */ public EthernetPacketType deNAT_outgoing_packet_PacketDotNet(ref byte[] data, PhysicalAddress dstMAC, PhysicalAddress srcMAC) { EthernetPacket p = (EthernetPacket)EthernetPacket.ParsePacket(LinkLayers.Ethernet, data); EthernetPacketType ethernet_packet_type = getEthernetPacketType(ref data); IPv4Packet p_IPV4 = null; ARPPacket p_ARP = null; bool packet_changed = false; if (ethernet_packet_type == EthernetPacketType.IpV4) { p_IPV4 = (IPv4Packet)p.PayloadPacket; } else if (ethernet_packet_type == EthernetPacketType.Arp) { p_ARP = (ARPPacket)p.PayloadPacket; } else { return(ethernet_packet_type); } xbs_nat_entry nat_entry = null; lock (NAT_list) { if (dstMAC.Equals(broadcast_mac)) { if (p_ARP != null) { foreach (xbs_nat_entry ne in NAT_list.Values) { if (ne.natted_source_ip.Equals(p_ARP.TargetProtocolAddress)) { nat_entry = ne; break; } } } } else if (NAT_list.ContainsKey(dstMAC)) { nat_entry = NAT_list[dstMAC]; } } #if DEBUG if (nat_entry != null) { xbs_messages.addDebugMessage("% found device in deNAT list: " + dstMAC + " " + nat_entry.natted_source_ip + "=>" + nat_entry.original_source_ip, xbs_message_sender.NAT); } #endif if (nat_entry != null) { if (p_IPV4 != null) { p_IPV4.DestinationAddress = nat_entry.original_source_ip; p_IPV4.UpdateIPChecksum(); if (p_IPV4.Protocol == IPProtocolType.UDP) { UdpPacket p_UDP = (UdpPacket)p_IPV4.PayloadPacket; if (NAT_enablePS3mode) { bool ret = PS3_replaceClientAnswer_hostAddr(dstMAC, ref p_UDP, nat_entry.original_source_ip, nat_entry.natted_source_ip); } p_UDP.UpdateUDPChecksum(); packet_changed = true; } else if (p_IPV4.Protocol == IPProtocolType.TCP) { ((TcpPacket)p_IPV4.PayloadPacket).UpdateTCPChecksum(); packet_changed = true; } } else if (p_ARP != null) { p_ARP.TargetProtocolAddress = nat_entry.original_source_ip; packet_changed = true; } } if (packet_changed) { data = p.BytesHighPerformance.ActualBytes(); } return((packet_changed) ? ethernet_packet_type : EthernetPacketType.None); }
private bool compare_tmpIP(xbs_nat_entry entry) { bool ret = (entry.natted_source_ip.Equals(_tmpIP)); return(ret); }
private bool isFreeEntry(xbs_nat_entry entry) { return(entry.source_mac == null); }