Example #1
0
        //Event must only be raised once per fixed port
        public void HandleFixedPortClosed(object sender, EventArgs e)
        {
            UDPFixedPort s = (UDPFixedPort)sender;

            s.ConnectionClosedEvent -= HandleFixedPortClosed;
            lock (sendSentry)
            {
                lock (recvSentry)
                {
                    connections.TryRemove(s.Key, out Session dummy);
                    fixedUDPPorts.TryRemove(s.Port, out UDPFixedPort dummy2);
                    s.Dispose();
                }
            }
            Log_Info("Closed Dead Fixed Port");
        }
Example #2
0
        public Winsock(DEV9_State parDev9, string parDevice)
            : base(parDev9)
        {
            //Add allways on connections
            byte[] dns1 = null;
            byte[] dns2 = null;
            Dictionary <string, byte[]> hosts   = new Dictionary <string, byte[]>();
            NetworkInterface            adapter = null;

            if (parDevice != "Auto")
            {
                adapter = GetAdapterFromGuid(parDevice);
                if (adapter == null)
                {
                    //System.Windows.Forms.MessageBox.Show("Failed to GetAdapter");
                    throw new NullReferenceException("Failed to GetAdapter");
                }
                adapterIP = (from ip in adapter.GetIPProperties().UnicastAddresses
                             where ip.Address.AddressFamily == AddressFamily.InterNetwork
                             select ip.Address).SingleOrDefault();
            }
            else
            {
                adapter   = UDP_DHCPSession.AutoAdapter();
                adapterIP = IPAddress.Any;
            }

            if (adapter == null)
            {
                throw new NullReferenceException("Auto Selection Failed, Check You Connection or Manually Specify Adapter");
            }

            if (!DEV9Header.config.SocketConnectionSettings.AutoDNS1)
            {
                dns1 = IPAddress.Parse(DEV9Header.config.SocketConnectionSettings.DNS1).GetAddressBytes();
            }
            if (!DEV9Header.config.SocketConnectionSettings.AutoDNS2)
            {
                dns2 = IPAddress.Parse(DEV9Header.config.SocketConnectionSettings.DNS2).GetAddressBytes();
            }

            foreach (Config.ConfigHost host in DEV9Header.config.Hosts)
            {
                if (host.Enabled)
                {
                    hosts.Add(host.URL, IPAddress.Parse(host.IP).GetAddressBytes());
                }
            }

            //DHCP emulated server
            ConnectionKey dhcpKey = new ConnectionKey
            {
                Protocol = (byte)IPType.UDP,
                SRVPort  = 67
            };

            dhcpServer = new UDP_DHCPSession(dhcpKey, adapter, dns1, dns2, DEV9Header.config.SocketConnectionSettings.LANMode);
            dhcpServer.ConnectionClosedEvent += HandleConnectionClosed;

            dhcpServer.SourceIP = new byte[] { 255, 255, 255, 255 };
            dhcpServer.DestIP   = DefaultDHCPConfig.DHCP_IP;

            if (!connections.TryAdd(dhcpServer.Key, dhcpServer))
            {
                throw new Exception("Connection Add Failed");
            }
            //DNS emulated server
            ConnectionKey dnsKey = new ConnectionKey
            {
                Protocol = (byte)IPType.UDP,
                SRVPort  = 53
            };

            dnsServer = new UDP_DNSSession(dnsKey, hosts);
            dnsServer.ConnectionClosedEvent += HandleConnectionClosed;
            dnsServer.SourceIP = dhcpServer.PS2IP;
            dnsServer.DestIP   = DefaultDHCPConfig.DHCP_IP;

            if (!connections.TryAdd(dnsServer.Key, dnsServer))
            {
                throw new Exception("Connection Add Failed");
            }
            //

            foreach (Config.ConfigIncomingPort port in
                     DEV9Header.config.SocketConnectionSettings.IncomingPorts)
            {
                if (!port.Enabled)
                {
                    continue;
                }

                ConnectionKey Key = new ConnectionKey
                {
                    Protocol = (byte)port.Protocol,
                    PS2Port  = port.Port,
                    SRVPort  = port.Port
                };

                Session s = null;

                if (port.Protocol == IPType.UDP)
                {
                    //avoid duplicates
                    if (fixedUDPPorts.ContainsKey(port.Port))
                    {
                        continue;
                    }

                    ConnectionKey fKey = new ConnectionKey
                    {
                        Protocol = (byte)IPType.UDP,
                        PS2Port  = port.Port,
                        SRVPort  = 0
                    };

                    UDPFixedPort fPort = new UDPFixedPort(fKey, adapterIP, port.Port);
                    fPort.ConnectionClosedEvent += HandleFixedPortClosed;

                    fPort.DestIP   = new byte[] { 0, 0, 0, 0 };
                    fPort.SourceIP = dhcpServer.PS2IP;

                    if (!connections.TryAdd(fPort.Key, fPort) |
                        !fixedUDPPorts.TryAdd(port.Port, fPort))
                    {
                        fPort.Dispose();
                        throw new Exception("Connection Add Failed");
                    }

                    s = fPort.NewListenSession(Key);
                }

                s.ConnectionClosedEvent += HandleConnectionClosed;

                s.SourceIP = dhcpServer.PS2IP;
                s.DestIP   = dhcpServer.Broadcast;

                if (!connections.TryAdd(s.Key, s))
                {
                    s.Dispose();
                    throw new Exception("Connection Add Failed");
                }
            }

            SetMAC(null);
            SetMAC(adapter.GetPhysicalAddress().GetAddressBytes());
        }
Example #3
0
        public bool SendUDP(ConnectionKey Key, IPPacket ipPkt)
        {
            Log_Verb("UDP");
            UDP udp = (UDP)ipPkt.Payload;

            Key.PS2Port = udp.SourcePort; Key.SRVPort = udp.DestinationPort;

            if (udp.DestinationPort == 67)
            { //DHCP
                return(dhcpServer.Send(ipPkt.Payload));
            }

            if (udp.DestinationPort == 53 && Utils.memcmp(ipPkt.DestinationIP, 0, DefaultDHCPConfig.DHCP_IP, 0, 4))
            { //DNS
                return(dnsServer.Send(ipPkt.Payload));
            }

            int res = SendFromConnection(Key, ipPkt);

            if (res == 1)
            {
                return(true);
            }
            else if (res == 0)
            {
                return(false);
            }
            else
            {
                Log_Verb("Creating New Connection with key " + Key);
                Log_Info("Creating New UDP Connection with Dest Port " + udp.DestinationPort);
                UDPSession s;
                if (udp.SourcePort == udp.DestinationPort ||                                 //Used for LAN games that assume the destination port
                    Utils.memcmp(ipPkt.DestinationIP, 0, dhcpServer.Broadcast, 0, 4) ||
                    Utils.memcmp(ipPkt.DestinationIP, 0, dhcpServer.LimitedBroadcast, 0, 4)) //Broadcast packets
                {
                    //Limit of one udpclient per local port
                    //need to reuse the udpclient
                    UDPFixedPort fPort;
                    if (fixedUDPPorts.ContainsKey(udp.SourcePort))
                    {
                        Log_Verb("Using Existing UDPFixedPort");
                        fPort = fixedUDPPorts[udp.SourcePort];
                    }
                    else
                    {
                        ConnectionKey fKey = new ConnectionKey
                        {
                            Protocol = (byte)IPType.UDP,
                            PS2Port  = udp.SourcePort,
                            SRVPort  = 0
                        };

                        Log_Verb("Creating New UDPFixedPort with key " + fKey);
                        Log_Info("Creating New UDPFixedPort with Port " + udp.SourcePort);

                        fPort = new UDPFixedPort(fKey, adapterIP, udp.SourcePort);
                        fPort.ConnectionClosedEvent += HandleFixedPortClosed;

                        fPort.DestIP   = new byte[] { 0, 0, 0, 0 };
                        fPort.SourceIP = dhcpServer.PS2IP;

                        if (!connections.TryAdd(fKey, fPort) |
                            !fixedUDPPorts.TryAdd(udp.SourcePort, fPort))
                        {
                            fPort.Dispose();
                            throw new Exception("Connection Add Failed");
                        }
                    }
                    s = fPort.NewClientSession(Key, Utils.memcmp(ipPkt.DestinationIP, 0, dhcpServer.Broadcast, 0, 4) |
                                               Utils.memcmp(ipPkt.DestinationIP, 0, dhcpServer.LimitedBroadcast, 0, 4));
                }
                else
                {
                    s = new UDPSession(Key, adapterIP);
                }
                s.ConnectionClosedEvent += HandleConnectionClosed;
                s.DestIP   = ipPkt.DestinationIP;
                s.SourceIP = dhcpServer.PS2IP;
                if (!connections.TryAdd(Key, s))
                {
                    throw new Exception("Connection Add Failed");
                }
                return(s.Send(ipPkt.Payload));
            }
        }