public void EqualsNot()
		{
			PhysicalAddress phys1 = new PhysicalAddress(new byte[] { 0x06, 0x5, 0x04, 0x03, 0x02, 0x01 });
			PhysicalAddress phys2 = new PhysicalAddress(new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 });

			Assert.IsTrue(!phys1.Equals(phys2));
		}
Exemple #2
0
        /*
        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;
        }
Exemple #3
0
        /*
        public EthernetPacketType NAT_incoming_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;
            lock (NAT_list)
            {
                if (!NAT_list.TryGetValue(srcMAC, out nat_entry))
                {
                    IPAddress sourceIP = getSourceIPFromRawPacketData(ref data, ethernet_packet_type);
                    if (sourceIP.Equals(ip_zero))
                        return ethernet_packet_type;
                    IPAddress destinationIP = getDestinationIPFromRawPacketData(ref data, ethernet_packet_type);
                    nat_entry = ip_pool.requestIP( sourceIP, srcMAC );
                    if (nat_entry == null)
                    {
                        xbs_messages.addInfoMessage("!! % out of NAT IPs. Could not nat incoming packet");
                        return ethernet_packet_type;
                    }
                    NAT_list.Add(srcMAC, nat_entry);
#if DEBUG
                    xbs_messages.addDebugMessage("% new device in NAT list: " + srcMAC + " " + nat_entry.original_source_ip + "=>" + nat_entry.natted_source_ip);
#endif
                }
                else
                {
#if DEBUG
                    xbs_messages.addDebugMessage("% found device in NAT list: " + srcMAC + " " + nat_entry.original_source_ip + "=>" + nat_entry.natted_source_ip);
#endif
                }
            }
            replaceSourceIpWithNATSourceIP(ref data, ethernet_packet_type, ref nat_entry);
            if (ethernet_packet_type == EthernetPacketType.IpV4)
            {
                if (dstMAC.Equals(broadcast_mac) && nat_entry.natted_broadcast!=null)
                    replaceBroadcastIPAddress(ref data, ref nat_entry.natted_broadcast_bytes);
                updateIPChecksums(ref data);
            }
            return ethernet_packet_type;
        }
        */
        public EthernetPacketType NAT_incoming_packet_PacketDotNet(ref byte[] data, PhysicalAddress dstMAC, PhysicalAddress srcMAC, ref Packet p, ref IPv4Packet p_IPV4, ref ARPPacket p_ARP)
        {
            xbs_nat_entry nat_entry;

            EthernetPacketType p_type = ((EthernetPacket)p).Type;
            if (p_type != EthernetPacketType.IpV4 && p_type != EthernetPacketType.Arp)
                return p_type;

            lock (NAT_list)
            {
                if (!NAT_list.TryGetValue(srcMAC, out nat_entry))
                {
                    IPAddress sourceIP = (p_IPV4!=null) ? p_IPV4.SourceAddress : p_ARP.SenderProtocolAddress;
                    if (sourceIP.Equals(ip_zero))
                        return p_type;
                    IPAddress destinationIP = (p_IPV4 != null) ? p_IPV4.DestinationAddress : p_ARP.TargetProtocolAddress;
                    nat_entry = ip_pool.requestIP(sourceIP, srcMAC);
                    if (nat_entry == null)
                    {
                        if (!natIPpoolOverflow_warning_shown)
                        {
                            natIPpoolOverflow_warning_shown = true;
                            xbs_messages.addInfoMessage("!! % out of NAT IPs. Could not NAT incoming packet", xbs_message_sender.NAT, xbs_message_type.WARNING);
                        }
                        return p_type;
                    }
                    else
                    {
                        natIPpoolOverflow_warning_shown = false;
                        NAT_list.Add(srcMAC, nat_entry);
#if DEBUG
                        xbs_messages.addDebugMessage("% new device in NAT list: " + srcMAC + " " + nat_entry.original_source_ip + "=>" + nat_entry.natted_source_ip, xbs_message_sender.NAT);
#endif
                    }
                }
                else
                {
#if DEBUG
                    xbs_messages.addDebugMessage("% found device in NAT list: " + srcMAC + " " + nat_entry.original_source_ip + "=>" + nat_entry.natted_source_ip, xbs_message_sender.NAT);
#endif
                }
            }
            if (p_IPV4 != null)
            {
                p_IPV4.SourceAddress = nat_entry.natted_source_ip;
                if (dstMAC.Equals(broadcast_mac) && nat_entry.natted_broadcast != null)
                    p_IPV4.DestinationAddress = nat_entry.natted_broadcast;
                p_IPV4.UpdateIPChecksum();
                if (p_IPV4.Protocol == IPProtocolType.UDP)
                {
                    UdpPacket p_UDP = (UdpPacket)p_IPV4.PayloadPacket;
                    if (NAT_enablePS3mode)
                    {
                        bool ret = PS3_replaceInfoResponse_hostAddr(dstMAC, ref p_UDP, nat_entry.natted_source_ip);
                        if (!ret)
                            ret = PS3_replaceClientAnswer_hostAddr(dstMAC, ref p_UDP, nat_entry.natted_source_ip, nat_entry.original_source_ip);
                    }
                    p_UDP.UpdateUDPChecksum();
                }
                else if (p_IPV4.Protocol == IPProtocolType.TCP)
                    ((TcpPacket)p_IPV4.PayloadPacket).UpdateTCPChecksum();
            }
            else
            {
                p_ARP.SenderProtocolAddress = nat_entry.natted_source_ip;
            }
            data = p.BytesHighPerformance.ActualBytes();
            return p_type;
        }
        /// <summary>
        /// Search the local unicast IP according to the MAC
        /// </summary>
        /// <param name="macAddress">Local MAC address broadcasting the command packet</param>
        /// <returns>Unicast IP binding to this MAC</returns>
        private static IPAddress GetIPAddress4FromMACAddress(PhysicalAddress macAddress)
        {
            if (macAddress == null)
                return null;

            NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
            IPAddress address = null;
            foreach (NetworkInterface adapter in adapters)
            {
                if (macAddress.Equals(adapter.GetPhysicalAddress()))
                {
                    IPInterfaceProperties ipProperties = adapter.GetIPProperties();
                    foreach (IPAddressInformation addressInfo in ipProperties.UnicastAddresses)
                    {
                        if (addressInfo.Address.AddressFamily == AddressFamily.InterNetwork)
                        {
                            address = addressInfo.Address;
                            break;
                        }
                    }
                    break;
                }
            }

            return address;
        }
 /// <summary>
 ///     Get the IP and MAC addresses of all known devices on the LAN
 /// </summary>
 /// <remarks>
 ///     1) This table is not updated often - it can take some human-scale time
 ///     to notice that a device has dropped off the network, or a new device
 ///     has connected.
 ///     2) This discards non-local devices if they are found - these are multicast
 ///     and can be discarded by IP address range.
 /// </remarks>
 /// <returns></returns>
 private static Dictionary<IPAddress, PhysicalAddress> GetAllDevicesOnLAN()
 {
     var all = new Dictionary<IPAddress, PhysicalAddress>();
     // Add this PC to the list...
     all.Add(GetIPAddress(), GetMacAddress());
     var spaceForNetTable = 0;
     // Get the space needed
     // We do that by requesting the table, but not giving any space at all.
     // The return value will tell us how much we actually need.
     GetIpNetTable(IntPtr.Zero, ref spaceForNetTable, false);
     // Allocate the space
     // We use a try-finally block to ensure release.
     var rawTable = IntPtr.Zero;
     try
     {
         rawTable = Marshal.AllocCoTaskMem(spaceForNetTable);
         // Get the actual data
         var errorCode = GetIpNetTable(rawTable, ref spaceForNetTable, false);
         if (errorCode != 0)
         {
             // Failed for some reason - can do no more here.
             throw new Exception(string.Format(
                 "Unable to retrieve network table. Error code {0}", errorCode));
         }
         // Get the rows count
         var rowsCount = Marshal.ReadInt32(rawTable);
         var currentBuffer = new IntPtr(rawTable.ToInt64() + Marshal.SizeOf(typeof (int)));
         // Convert the raw table to individual entries
         var rows = new MIB_IPNETROW[rowsCount];
         for (var index = 0; index < rowsCount; index++)
         {
             rows[index] = (MIB_IPNETROW) Marshal.PtrToStructure(new IntPtr(currentBuffer.ToInt64() +
                                                                            index*
                                                                            Marshal.SizeOf(typeof (MIB_IPNETROW))
                 ),
                 typeof (MIB_IPNETROW));
         }
         // Define the dummy entries list (we can discard these)
         var virtualMAC = new PhysicalAddress(new byte[] {0, 0, 0, 0, 0, 0});
         var broadcastMAC = new PhysicalAddress(new byte[] {255, 255, 255, 255, 255, 255});
         foreach (var row in rows)
         {
             var ip = new IPAddress(BitConverter.GetBytes(row.dwAddr));
             byte[] rawMAC = {row.mac0, row.mac1, row.mac2, row.mac3, row.mac4, row.mac5};
             var pa = new PhysicalAddress(rawMAC);
             if (!pa.Equals(virtualMAC) && !pa.Equals(broadcastMAC) && !IsMulticast(ip))
             {
                 //Console.WriteLine("IP: {0}\t\tMAC: {1}", ip.ToString(), pa.ToString());
                 if (!all.ContainsKey(ip))
                 {
                     all.Add(ip, pa);
                 }
             }
         }
     }
     finally
     {
         // Release the memory.
         Marshal.FreeCoTaskMem(rawTable);
     }
     return all;
 }
Exemple #6
0
 public bool has_xbox(PhysicalAddress xbox_addr)
 {
     if (xbox_addr.Equals(xbs_nat.broadcast_mac) || xbox_addr.Equals(xbs_nat.zero_mac))
         return true;
     int hash = xbox_addr.GetHashCode();
     bool ret = false;
     lock (this)
     {
         foreach (xbs_xbox xbox in xbox_list)
             if (xbox.hash == hash)
                 ret = true;
     }
     return ret;
 }