Esempio n. 1
0
        protected void InitDHCP(NetworkInterface parAdapter)
        {
            //Cleanup to pass options as paramaters instead of accessing the config directly?
            dhcpActive = true;
            byte[] ps2IP   = IPAddress.Parse(DEV9Header.config.DirectConnectionSettings.PS2IP).GetAddressBytes();
            byte[] netMask = null;
            byte[] gateway = null;

            byte[] dns1 = null;
            byte[] dns2 = null;

            if (!DEV9Header.config.DirectConnectionSettings.AutoSubNet)
            {
                netMask = IPAddress.Parse(DEV9Header.config.DirectConnectionSettings.SubNet).GetAddressBytes();
            }

            if (!DEV9Header.config.DirectConnectionSettings.AutoGateway)
            {
                gateway = IPAddress.Parse(DEV9Header.config.DirectConnectionSettings.Gateway).GetAddressBytes();
            }

            if (!DEV9Header.config.DirectConnectionSettings.AutoDNS1)
            {
                dns1 = IPAddress.Parse(DEV9Header.config.DirectConnectionSettings.DNS1).GetAddressBytes();
            }
            if (!DEV9Header.config.DirectConnectionSettings.AutoDNS2)
            {
                dns2 = IPAddress.Parse(DEV9Header.config.DirectConnectionSettings.DNS2).GetAddressBytes();
            }
            //Create DHCP Session
            dhcp       = new UDP_DHCPSession(new ConnectionKey(), parAdapter, ps2IP, netMask, gateway, dns1, dns2);
            dhcpActive = true;
        }
Esempio n. 2
0
 public override void Dispose(bool disposing)
 {
     base.Dispose(disposing);
     if (dhcpActive & disposing)
     {
         dhcpActive = false;
         dhcp.Dispose();
         dhcp = null;
     }
 }
Esempio n. 3
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());
        }