public bool SendIGMP(ConnectionKey Key, IPPacket ipPkt) { Log_Verb("IGMP"); //lock (sentry) //{ 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); IGMPSession s = new IGMPSession(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)); } //} }
public int SendFromConnection(ConnectionKey Key, IPPacket ipPkt) { connections.TryGetValue(Key, out Session s); if (s != null) { return(s.Send(ipPkt.Payload) ? 1 : 0); } else { return(-1); } }
public bool SendIP(IPPacket ipPkt) { //TODO Optimise Checksum for implace checksumming if (ipPkt.VerifyCheckSum() == false) { Log_Error("IP packet with bad CSUM"); return(false); } if (ipPkt.Payload.VerifyCheckSum(ipPkt.SourceIP, ipPkt.DestinationIP) == false) { Log_Error("IP packet with bad Payload CSUM"); return(false); } ConnectionKey Key = new ConnectionKey { IP0 = ipPkt.DestinationIP[0], IP1 = ipPkt.DestinationIP[1], IP2 = ipPkt.DestinationIP[2], IP3 = ipPkt.DestinationIP[3], Protocol = ipPkt.Protocol }; switch (ipPkt.Protocol) //(Prase Payload) { case (byte)IPType.ICMP: return(SendICMP(Key, ipPkt)); case (byte)IPType.IGMP: return(SendIGMP(Key, ipPkt)); case (byte)IPType.TCP: return(SendTCP(Key, ipPkt)); case (byte)IPType.UDP: return(SendUDP(Key, ipPkt)); default: //Log_Error("Unkown Protocol"); throw new NotImplementedException("Unkown IPv4 Protocol " + ipPkt.Protocol.ToString("X2")); //return false; } }
public bool SendTCP(ConnectionKey Key, IPPacket ipPkt) { Log_Verb("TCP"); TCP tcp = (TCP)ipPkt.Payload; Key.PS2Port = tcp.SourcePort; Key.SRVPort = tcp.DestinationPort; if (tcp.DestinationPort == 53 && Utils.memcmp(ipPkt.DestinationIP, 0, DefaultDHCPConfig.DHCP_IP, 0, 4)) { //DNS throw new NotImplementedException("DNS over TCP not supported"); } 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 TCP Connection with Dest Port " + tcp.DestinationPort); TCPSession s = new TCPSession(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)); } }
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()); }
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)); } }