public abstract bool Send(IPPayload payload);
public override bool Send(IPPayload payload) { DHCP dhcp = new DHCP(payload.GetPayload()); hType = dhcp.HardwareType; hLen = dhcp.HardwareAddressLength; xID = dhcp.TransactionID; cMac = dhcp.ClientHardwareAddress; cookie = dhcp.MagicCookie; DHCPopClientID clientID = null; byte msg = 0; byte[] reqList = null; uint leaseTime = 86400; for (int i = 0; i < dhcp.Options.Count; i++) { switch (dhcp.Options[i].Code) { case 0: //Error.WriteLine("Got NOP"); continue; case 1: Log_Info("Got SubnetMask?"); if (Utils.memcmp(NetMask, 0, ((DHCPopSubnet)dhcp.Options[i]).SubnetMask, 0, 4) == false) { throw new Exception("SubnetMask missmatch"); } break; case 3: Log_Info("Got Router?"); if (((DHCPopRouter)dhcp.Options[i]).RouterIPs.Count != 1) { throw new Exception("RouterIPs count missmatch"); } if (Utils.memcmp(Gateway, 0, ((DHCPopRouter)dhcp.Options[i]).RouterIPs[0], 0, 4) == false) { throw new Exception("RouterIPs missmatch"); } break; case 6: Log_Info("Got DNS?"); if ((((DHCPopDNS)dhcp.Options[i]).DNSServers.Count != 0 & DNS1 == null) || (((DHCPopDNS)dhcp.Options[i]).DNSServers.Count != 1 & DNS2 == null) || (((DHCPopDNS)dhcp.Options[i]).DNSServers.Count != 2 & DNS2 != null)) { throw new Exception("DNS count missmatch"); } if ((DNS1 != null && Utils.memcmp(DNS1, 0, ((DHCPopDNS)dhcp.Options[i]).DNSServers[0], 0, 4) == false) || (DNS2 != null && Utils.memcmp(DNS2, 0, ((DHCPopDNS)dhcp.Options[i]).DNSServers[1], 0, 4) == false)) { throw new Exception("DNS missmatch"); } break; case 12: Log_Info("Got HostName"); //TODO use name? break; case 50: Log_Info("Got Request IP"); if (Utils.memcmp(PS2IP, 0, ((DHCPopREQIP)dhcp.Options[i]).IPaddress, 0, 4) == false) { throw new Exception("ReqIP missmatch"); } break; case 51: Log_Info("Got Requested Lease Time"); leaseTime = ((DHCPopIPLT)(dhcp.Options[i])).IPLeaseTime; break; case 53: msg = ((DHCPopMSG)(dhcp.Options[i])).Message; Log_Info("Got MSG ID = " + msg); break; case 54: Log_Info("Got Server IP"); if (Utils.memcmp(DefaultDHCPConfig.DHCP_IP, 0, ((DHCPopSERVIP)dhcp.Options[i]).ServerIP, 0, 4) == false) { throw new Exception("ServIP missmatch"); } break; case 55: reqList = ((DHCPopREQLIST)(dhcp.Options[i])).RequestList; Log_Verb("Got Request List of length " + reqList.Length); for (int rID = 0; rID < reqList.Length; rID++) { Log_Verb("Requested : " + reqList[rID]); } break; case 56: Log_Verb("Got String Message of " + ((DHCPopMSGStr)dhcp.Options[i]).Message); break; case 57: maxMs = ((DHCPopMMSGS)(dhcp.Options[i])).MaxMessageSize; Log_Verb("Got Max Message Size of " + maxMs); break; case 60: Log_Verb("Got Class Id of " + ((DHCPopClassID)dhcp.Options[i]).ClassID); break; case 61: Log_Verb("Got Client ID"); clientID = (DHCPopClientID)dhcp.Options[i]; //Ignore break; case 255: Log_Verb("Got END"); break; default: Log_Error("Got Unknown Option " + dhcp.Options[i].Code); throw new Exception("Got Unknown Option " + dhcp.Options[i].Code); //break; } } DHCP retPay = new DHCP { OP = 2, HardwareType = hType, HardwareAddressLength = hLen, TransactionID = xID, YourIP = PS2IP,//IPaddress.GetAddressBytes(); ServerIP = DefaultDHCPConfig.DHCP_IP, ClientHardwareAddress = cMac, MagicCookie = cookie }; if (msg == 1 || msg == 3) //Fill out Requests { if (msg == 1) { retPay.Options.Add(new DHCPopMSG(2)); } if (msg == 3) { retPay.Options.Add(new DHCPopMSG(5)); } if (reqList != null) { for (int i = 0; i < reqList.Length; i++) { switch (reqList[i]) { case 1: Log_Verb("Sending Subnet"); //retPay.Options.Add(new DHCPopSubnet(NetMask.GetAddressBytes())); retPay.Options.Add(new DHCPopSubnet(NetMask)); break; case 3: Log_Verb("Sending Router"); if (Gateway != null) { retPay.Options.Add(new DHCPopRouter(new List <byte[]>() { Gateway })); } break; case 6: Log_Verb("Sending DNS"); if (DNS1 != null) { if (DNS2 != null) { retPay.Options.Add(new DHCPopDNS(new List <byte[]>() { DNS1, DNS2 })); } else { retPay.Options.Add(new DHCPopDNS(new List <byte[]>() { DNS1 })); } } break; case 15: Log_Verb("Sending Domain Name"); //retPay.Options.Add(new DHCPopDNSNAME(Dns.GetHostName())); retPay.Options.Add(new DHCPopDNSNAME("PCSX2-CLRDEV9")); break; case 28: Log_Verb("Sending Broadcast Addr"); retPay.Options.Add(new DHCPopBCIP(Broadcast)); break; case 50: Log_Verb("Sending PS2 IP Addr"); retPay.Options.Add(new DHCPopREQIP(PS2IP)); break; case 53: Log_Verb("Sending MSG (Already Added)"); break; case 54: Log_Verb("Sending Server Identifier (Already Added)"); break; case 77: //Isn't this surpossed to be sent by the client? Log_Error("Request for User-Class, Ignoring"); break; default: Log_Error("Got Unknown Req " + reqList[i]); throw new Exception("Got Unknown Req " + reqList[i]); } } retPay.Options.Add(new DHCPopIPLT(leaseTime)); } } if (msg == 7) { Log_Info("PS2 has Disconnected"); return(true); } retPay.Options.Add(new DHCPopSERVIP(DefaultDHCPConfig.DHCP_IP)); retPay.Options.Add(new DHCPopEND()); byte[] udpPayload = retPay.GetBytes((UInt16)(maxMs - (8 + 20))); UDP retudp = new UDP(udpPayload) { SourcePort = 67, DestinationPort = 68 }; recvBuff.Enqueue(retudp); return(true); }
public override bool Send(IPPayload payload) { throw new NotImplementedException(); }
public bool FromBytes(byte[] ipv6Packet) { byte[] addressBytes = new byte[16]; uint tempVal = 0, tempVal2 = 0; // Ensure byte array is large enough to contain an IPv6 header if (ipv6Packet.Length < Ipv6HeaderLength) { return(false); } tempVal = ipv6Packet[0]; tempVal = (tempVal >> 4) & 0xF; Version = (byte)tempVal; tempVal = ipv6Packet[0]; tempVal = (tempVal & 0xF) >> 4; TrafficClass = (byte)(tempVal | (uint)((ipv6Packet[1] >> 4) & 0xF)); tempVal2 = ipv6Packet[1]; tempVal2 = (tempVal2 & 0xF) << 16; tempVal = ipv6Packet[2]; tempVal = tempVal << 8; Flow = tempVal2 | tempVal | ipv6Packet[3]; PayloadLength = NetUtilities.ToLittleEndian(BitConverter.ToUInt16(ipv6Packet, 4)); NextHeader = (IPv6Protocol)ipv6Packet[6]; HopLimit = ipv6Packet[7]; Array.Copy(ipv6Packet, 8, addressBytes, 0, 16); SourceAddress = new IPv6Address(addressBytes); Array.Copy(ipv6Packet, 24, addressBytes, 0, 16); DestinationAddress = new IPv6Address(addressBytes); packetIndex += Ipv6HeaderLength; IPv6PseudoHeader ipv6PseudoHeader = new IPv6PseudoHeader(SourceAddress, DestinationAddress, PayloadLength, (byte)NextHeader); ushort checkSum = ipv6PseudoHeader.GetCheckSum(); if (ipv6Packet.Length > packetIndex) { switch (NextHeader) { case IPv6Protocol.ICMPv6: Icmpv6Packet icmpv6packet = new Icmpv6Packet(); if ((icmpv6packet.FromBytes(ipv6Packet, ref packetIndex)) == false) { return(false); } Payload = icmpv6packet; checkSum = NetUtilities.ComputeChecksum(checkSum, Payload.ToBytes(), true); if (checkSum != 0) { return(false); } break; case IPv6Protocol.Udp: UdpDatagram udpDatagram = new UdpDatagram(); if ((udpDatagram.FromBytes(ipv6Packet, ref packetIndex)) == false) { return(false); } Payload = udpDatagram; checkSum = NetUtilities.ComputeChecksum(checkSum, Payload.ToBytes(), true); if (checkSum != 0) { return(false); } break; } } return(true); }
public override bool Send(IPPayload payload) { lock (deathClock) { deathClock.Restart(); } UDP udp = (UDP)payload; if (destPort != 0) { //client already created if (!(udp.DestinationPort == destPort && udp.SourcePort == srcPort)) { Log_Error("UDP packet invalid for current session (Duplicate key?)"); return(false); } } else { //create client destPort = udp.DestinationPort; srcPort = udp.SourcePort; //Multicast address start with 0b1110 if ((DestIP[0] & 0xF0) == 0xE0) { isMulticast = true; } client = new UdpClient(); client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); client.Client.Bind(new IPEndPoint(adapterIP, 0)); //needs testing if (isMulticast) { Log_Info("Is Multicast"); //client.JoinMulticastGroup(address); } else { IPAddress address = new IPAddress(DestIP); client.Connect(address, destPort); } if (srcPort != 0) { open = true; } } if (destPort == 53) { Log_Info("DNS Packet Sent To " + new IPAddress(DestIP)); Log_Info("Contents"); PacketReader.DNS.DNS pDNS = new PacketReader.DNS.DNS(udp.GetPayload()); } if (isBroadcast) { client.Send(udp.GetPayload(), udp.GetPayload().Length, new IPEndPoint(IPAddress.Broadcast, destPort)); } else if (isMulticast | isFixedPort) { client.Send(udp.GetPayload(), udp.GetPayload().Length, new IPEndPoint(new IPAddress(DestIP), destPort)); } else if (isFixedPort) { client.Send(udp.GetPayload(), udp.GetPayload().Length); } else { //As fair as I know, //this won't throw a 10061 Connection refused or 10054 Connection reset //under any probable event on windows (As much as I've tried) //Yet it can throw 10061 on linux try { client.Send(udp.GetPayload(), udp.GetPayload().Length); } catch (SocketException err) { if (!hasRetryed) { Log_Error("UDP Send Error: " + err.Message); Log_Error("Error Code: " + err.ErrorCode); Log_Error("Hiding further errors from this connection"); hasRetryed = true; } client.Close(); //recreate UDP client IPAddress address = new IPAddress(DestIP); client = new UdpClient(); client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); client.Client.Bind(new IPEndPoint(adapterIP, 0)); client.Connect(address, destPort); //And retry sending client.Send(udp.GetPayload(), udp.GetPayload().Length); } } if (srcPort == 0) { RaiseEventConnectionClosed(); } return(true); }
public override bool send(IPPayload payload) { #region "Get Network Info" //Get comp IP IPAddress IPaddress = null; //IPAddress NetMask = null; //IPAddress GatewayIP = null; List <IPAddress> DNS_IP = new List <IPAddress>(); //IPAddress BroadCastIP = null; NetworkInterface[] Interfaces = NetworkInterface.GetAllNetworkInterfaces(); bool FoundAdapter = false; foreach (NetworkInterface adapter in Interfaces) { if (adapter.NetworkInterfaceType == NetworkInterfaceType.Loopback) { continue; } if (adapter.OperationalStatus == OperationalStatus.Up) { UnicastIPAddressInformationCollection IPInfo = adapter.GetIPProperties().UnicastAddresses; IPInterfaceProperties properties = adapter.GetIPProperties(); //GatewayIPAddressInformationCollection myGateways = properties.GatewayAddresses; foreach (UnicastIPAddressInformation IPAddressInfo in IPInfo) { if (IPAddressInfo.DuplicateAddressDetectionState == DuplicateAddressDetectionState.Preferred & IPAddressInfo.AddressPreferredLifetime != UInt32.MaxValue & IPAddressInfo.Address.AddressFamily == AddressFamily.InterNetwork) { Console.Error.WriteLine("Matched Adapter"); IPaddress = IPAddressInfo.Address;; FoundAdapter = true; break; } } //foreach (GatewayIPAddressInformation Gateway in myGateways) //allow more than one gateway? //{ // if (FoundAdapter == true) // { // GatewayIP = Gateway.Address; // GATEWAY_IP = GatewayIP.GetAddressBytes(); // break; // } //} foreach (IPAddress DNSaddress in properties.DnsAddresses) //allow more than one DNS address? { if (FoundAdapter == true) { if (!(DNSaddress.AddressFamily == AddressFamily.InterNetworkV6)) { DNS_IP.Add(DNSaddress); } } } } if (FoundAdapter == true) { Console.Error.WriteLine(adapter.Name); Console.Error.WriteLine(adapter.Description); Console.Error.WriteLine("IP Address :" + IPaddress.ToString()); Console.Error.WriteLine("Domain Name :" + Dns.GetHostName()); //Console.Error.WriteLine("Subnet Mask :" + NetMask.ToString()); //Console.Error.WriteLine("Gateway IP :" + GatewayIP.ToString()); Console.Error.WriteLine("DNS 1 : " + DNS_IP[0].ToString()); break; } } #endregion DHCP dhcp = new DHCP(payload.GetPayload()); HType = dhcp.HardwareType; Hlen = dhcp.HardwareAddressLength; xID = dhcp.TransactionID; cMac = dhcp.ClientHardwareAddress; cookie = dhcp.MagicCookie; TCPOption clientID = null; byte msg = 0; byte[] reqList = null; for (int i = 0; i < dhcp.Options.Count; i++) { switch (dhcp.Options[i].Code) { case 0: //Console.Error.WriteLine("Got NOP"); continue; case 50: Console.Error.WriteLine("Got Request IP"); if (Utils.memcmp(PS2_IP, 0, ((DHCPopREQIP)dhcp.Options[i]).IPaddress, 0, 4) == false) { throw new Exception("ReqIP missmatch"); } break; case 53: msg = ((DHCPopMSG)(dhcp.Options[i])).Message; Console.Error.WriteLine("Got MSG ID = " + msg); break; case 54: Console.Error.WriteLine("Got Server IP"); if (Utils.memcmp(DHCP_IP, 0, ((DHCPopSERVIP)dhcp.Options[i]).IPaddress, 0, 4) == false) { throw new Exception("ServIP missmatch"); } break; case 55: reqList = ((DHCPopREQLIST)(dhcp.Options[i])).RequestList; Console.Error.WriteLine("Got Request List of length " + reqList.Length); for (int rID = 0; rID < reqList.Length; rID++) { Console.Error.WriteLine("Requested : " + reqList[rID]); } break; case 56: Console.Error.WriteLine("Got String Message"); break; case 57: maMs = ((DHCPopMMSGS)(dhcp.Options[i])).MaxMessageSize; Console.Error.WriteLine("Got Max Message Size of " + maMs); break; case 61: Console.Error.WriteLine("Got Client ID"); clientID = dhcp.Options[i]; //Ignore break; case 255: Console.Error.WriteLine("Got END"); break; default: Console.Error.WriteLine("Got Unknown Option " + dhcp.Options[i].Code); throw new Exception(); //break; } } DHCP retPay = new DHCP(); retPay.OP = 2; retPay.HardwareType = HType; retPay.HardwareAddressLength = Hlen; retPay.TransactionID = xID; retPay.YourIP = PS2_IP;//IPaddress.GetAddressBytes(); retPay.ServerIP = DHCP_IP; retPay.ClientHardwareAddress = cMac; retPay.MagicCookie = cookie; if (msg == 1 || msg == 3) //Fill out Requests { if (msg == 1) { retPay.Options.Add(new DHCPopMSG(2)); } if (msg == 3) { retPay.Options.Add(new DHCPopMSG(5)); } for (int i = 0; i < reqList.Length; i++) { switch (reqList[i]) { case 1: Console.Error.WriteLine("Sending Subnet"); //retPay.Options.Add(new DHCPopSubnet(NetMask.GetAddressBytes())); retPay.Options.Add(new DHCPopSubnet(NETMASK)); break; case 3: Console.Error.WriteLine("Sending Router"); retPay.Options.Add(new DHCPopRouter(GATEWAY_IP)); break; case 6: Console.Error.WriteLine("Sending DNS"); //TODO support more than 1 retPay.Options.Add(new DHCPopDNS(DNS_IP[0])); //retPay.Options.Add(new DHCPopDNS(IPAddress.Parse("1.1.1.1"))); break; case 15: Console.Error.WriteLine("Sending Domain Name"); //retPay.Options.Add(new DHCPopDNSNAME(Dns.GetHostName())); retPay.Options.Add(new DHCPopDNSNAME("PCSX2-CLRDEV9")); break; case 28: Console.Error.WriteLine("Sending Broadcast Addr"); for (int i2 = 0; i2 < 4; i2++) { BROADCAST[i2] = (byte)((PS2_IP[i2]) | (~NETMASK[i2])); } retPay.Options.Add(new DHCPopBCIP(BROADCAST)); break; default: Console.Error.WriteLine("Got Unknown Option " + reqList[i]); throw new Exception(); } } retPay.Options.Add(new DHCPopIPLT(86400)); } if (msg == 7) { Console.Error.WriteLine("PS2 has Disconnected"); return(true); } retPay.Options.Add(new DHCPopSERVIP(DHCP_IP)); retPay.Options.Add(new DHCPopEND()); byte[] udpPayload = retPay.GetBytes((UInt16)(maMs - (8 + 20))); UDP retudp = new UDP(udpPayload); retudp.SourcePort = 67; retudp.DestinationPort = 68; recvbuff.Add(retudp); return(true); }
public override bool send(IPPayload payload) { DeathClock.Restart(); UDP udp = (UDP)payload; if (DestPort != 0) { if (!(udp.DestinationPort == DestPort && udp.SourcePort == SrcPort)) { Console.Error.WriteLine("UDP packet invalid for current session (Duplicate key?)"); return(false); } } else { DestPort = udp.DestinationPort; SrcPort = udp.SourcePort; if (Utils.memcmp(DestIP, 0, UDP_DHCPsession.BROADCAST, 0, 4)) { isBroadcast = true; } if (isBroadcast) { Console.Error.WriteLine("Is Broadcast"); client = new UdpClient(SrcPort); //Assuming broadcast wants a return message client.EnableBroadcast = true; //client.Close(); //client = new UdpClient(SrcPort); //client.BeginReceive(ReceiveFromBroadcast, new object()); open = true; } else { IPAddress address = new IPAddress(DestIP); if (SrcPort == DestPort) { client = new UdpClient(SrcPort); //Needed for Crash TTR (and probable other games) LAN } else { client = new UdpClient(); } client.Connect(address, DestPort); //address to send on if (SrcPort != 0) { //Console.Error.WriteLine("UDP expects Data"); open = true; } } } if (isBroadcast) { client.Send(udp.GetPayload(), udp.GetPayload().Length, new IPEndPoint(IPAddress.Broadcast, DestPort)); } else { client.Send(udp.GetPayload(), udp.GetPayload().Length); } //Console.Error.WriteLine("UDP Sent"); return(true); }
public override bool Send(IPPayload payload) { TCP tcp = (TCP)payload; if (destPort != 0) { if (!(tcp.DestinationPort == destPort && tcp.SourcePort == srcPort)) { Log_Error("TCP packet invalid for current session (Duplicate key?)"); return(false); } } if (tcp.RST == true) //Test this { Log_Info("PS2 has reset connection"); if (client != null) { if (client.Connected) { client.Close(); } } else { Log_Error("RESET CLOSED CONNECTION"); } //PS2 sent RST //clearly not expecting //more data state = TCPState.CloseCompleted; RaiseEventConnectionClosed(); return(true); } switch (state) { case TCPState.None: return(SendConnect(tcp)); case TCPState.SendingSYN_ACK: if (CheckRepeatSYNNumbers(tcp) == NumCheckResult.Bad) { Log_Error("Invalid Repeated SYN (SendingSYN_ACK)"); throw new Exception("Invalid Repeated SYN"); } return(true); //Ignore reconnect attempts while we are still attempting connection case TCPState.SentSYN_ACK: return(SendConnected(tcp)); case TCPState.Connected: if (tcp.FIN == true) //Connection Close Part 1, received FIN from PS2 { return(CloseByPS2Stage1_2(tcp)); } return(SendData(tcp)); case TCPState.Closing_ClosedByPS2: return(SendNoData(tcp)); case TCPState.Closing_ClosedByPS2ThenRemote_WaitingForAck: return(CloseByPS2Stage4(tcp)); case TCPState.Closing_ClosedByRemote: if (tcp.FIN == true) //Connection Close Part 3, received FIN from PS2 { return(CloseByRemoteStage3_4(tcp)); } return(SendData(tcp)); case TCPState.Closing_ClosedByRemoteThenPS2_WaitingForAck: return(CloseByRemoteStage2_ButAfter4(tcp)); case TCPState.CloseCompleted: throw new Exception("Attempt to send data on closed TCP connection"); default: throw new Exception("Invalid State"); } }
public override bool send(IPPayload payload) { TCP tcp = (TCP)payload; if (DestPort != 0) { if (!(tcp.DestinationPort == DestPort && tcp.SourcePort == SrcPort)) { Console.Error.WriteLine("TCP packet invalid for current session (Duplicate key?)"); return(false); } } if (tcp.RST == true) //Test this { if (client.Connected) { lock (sentry) { client.Close(); } state = TCPState.Closed; open = false; return(true); } } switch (state) { case TCPState.None: #region "SYN" DestPort = tcp.DestinationPort; SrcPort = tcp.SourcePort; if (tcp.SYN == false) { PerformRST(); Console.Error.WriteLine("Connection Not in Connected State"); return(true); } ExpectedSequenceNumber = tcp.SequenceNumber + 1; //Fill out last received numbers ReceivedSequenceNumbers.Add(tcp.SequenceNumber); ReceivedSequenceNumbers.Add(tcp.SequenceNumber); ReceivedSequenceNumbers.Add(tcp.SequenceNumber); ReceivedSequenceNumbers.Add(tcp.SequenceNumber); ReceivedSequenceNumbers.Add(tcp.SequenceNumber); for (int i = 0; i < tcp.Options.Count; i++) { switch (tcp.Options[i].Code) { case 0: //End case 1: //Nop continue; case 2: //MSS MaxSegmentSize = ((TCPopMSS)(tcp.Options[i])).MaxSegmentSize; break; case 3: //WinScale //Console.Error.WriteLine("Got WinScale"); // = ((TCPopWS)(tcp.Options[i])).WindowScale; break; case 8: //TimeStamp LastRecivedTimeStamp = ((TCPopTS)(tcp.Options[i])).SenderTimeStamp; SendTimeStamps = true; TimeStamp.Start(); break; default: Console.Error.WriteLine("Got Unknown Option " + tcp.Options[i].Code); throw new Exception(); //break; } } client = new TcpClient(); IPAddress address = new IPAddress(DestIP); client.BeginConnect(address, DestPort, new AsyncCallback(AsyncConnectComplete), tcp); state = TCPState.SendingSYN_ACK; open = true; return(true); #endregion case TCPState.SendingSYN_ACK: return(true); //Ignore reconnect attempts while we are still attempting connection case TCPState.SentSYN_ACK: #region "Syn-Ack" lock (sentry) { if (tcp.SYN == true) { throw new Exception("Attempt to Connect to an operning Port"); } NumCheckResult Result = CheckNumbers(tcp); if (Result == NumCheckResult.Bad) { throw new Exception("Bad TCP Number Received"); } for (int i = 0; i < tcp.Options.Count; i++) { switch (tcp.Options[i].Code) { case 0: //End case 1: //Nop continue; case 8: //Timestamp LastRecivedTimeStamp = ((TCPopTS)(tcp.Options[i])).SenderTimeStamp; break; default: Console.Error.WriteLine("Got Unknown Option " + tcp.Options[i].Code); throw new Exception(); //break; } } //Next packet will be data state = TCPState.Connected; } return(true); #endregion case TCPState.Connected: #region "Connected" if (tcp.SYN == true) { throw new Exception("Attempt to Connect to an open Port"); } lock (sentry) { for (int i = 0; i < tcp.Options.Count; i++) { switch (tcp.Options[i].Code) { case 0: //End case 1: //Nop continue; case 8: //Console.Error.WriteLine("Got TimeStamp"); LastRecivedTimeStamp = ((TCPopTS)(tcp.Options[i])).SenderTimeStamp; break; default: Console.Error.WriteLine("Got Unknown Option " + tcp.Options[i].Code); throw new Exception(); //break; } } NumCheckResult Result = CheckNumbers(tcp); if (Result == NumCheckResult.GotOldData) { throw new NotImplementedException(); //return true; } if (Result == NumCheckResult.Bad) { throw new Exception("Bad TCP Number Received"); } if (tcp.FIN == true) //Connection Close Part 1, receive FIN from PS2 { PerformCloseByPS2(); return(true); } if (tcp.GetPayload().Length != 0) { ReceivedSequenceNumbers.RemoveAt(0); ReceivedSequenceNumbers.Add(ExpectedSequenceNumber); //Send the Data try { client.GetStream().Write(tcp.GetPayload(), 0, tcp.GetPayload().Length); } catch (Exception e) { System.Windows.Forms.MessageBox.Show("Got IO Error :" + e.ToString()); //Connection Lost //Send Shutdown (Untested) PerformRST(); open = false; return(true); } unchecked { ExpectedSequenceNumber += ((uint)tcp.GetPayload().Length); } //Done send //ACK data TCP ret = CreateBasePacket(); ret.ACK = true; recvbuff.Add(ret); } } return(true); #endregion case TCPState.ConnectionClosedByPS2AndRemote: #region "Closing" //Close Part 4, Recive ACK from PS2 Console.Error.WriteLine("Compleated Close By PS2"); NumCheckResult ResultFIN = CheckNumbers(tcp); if (ResultFIN == NumCheckResult.GotOldData) { return(false); } if (ResultFIN == NumCheckResult.Bad) { throw new Exception("Bad TCP Number Received"); } state = TCPState.Closed; open = false; return(true); #endregion case TCPState.ConnectionClosedByRemote: #region "Closing" //Expect fin+ack if (tcp.FIN == true) { Console.Error.WriteLine("Compleated Close By Remote"); ReceivedSequenceNumbers.RemoveAt(0); ReceivedSequenceNumbers.Add(ExpectedSequenceNumber); unchecked { ExpectedSequenceNumber += 1; } TCP ret = CreateBasePacket(); ret.ACK = true; recvbuff.Add(ret); state = TCPState.ConnectionClosedByPS2AndRemote; open = false; return(true); } //throw new Exception("Invalid Packet"); return(false); //break; #endregion default: throw new Exception("Invalid State"); } }
public override bool Send(IPPayload payload) { ICMP icmp = (ICMP)payload; switch (icmp.Type) { case 8: //Echo //Code == zero Log_Verb("Send Ping"); lock (sentry) { open += 1; } PingData pd; pd.Data = icmp.Data; pd.HeaderData = icmp.HeaderData; Ping nPing = new Ping(); nPing.PingCompleted += PingCompleate; lock (sentry) { pings.Add(nPing); } nPing.SendAsync(new IPAddress(DestIP), pd); System.Threading.Thread.Sleep(1); //Hack Fix break; case 3: // switch (icmp.Code) { case 3: Log_Error("Recived Packet Rejected, Port Closed"); IPPacket retPkt = new IPPacket(icmp); byte[] srvIP = retPkt.SourceIP; byte prot = retPkt.Protocol; UInt16 srvPort = 0; UInt16 ps2Port = 0; switch (prot) { case (byte)IPType.TCP: TCP tcp = (TCP)retPkt.Payload; srvPort = tcp.SourcePort; ps2Port = tcp.DestinationPort; break; case (byte)IPType.UDP: UDP udp = (UDP)retPkt.Payload; srvPort = udp.SourcePort; ps2Port = udp.DestinationPort; break; } ConnectionKey Key = new ConnectionKey(); Key.IP0 = srvIP[0]; Key.IP1 = srvIP[1]; Key.IP2 = srvIP[2]; Key.IP3 = srvIP[3]; Key.Protocol = prot; Key.PS2Port = ps2Port; Key.SRVPort = srvPort; //is from NormalPort Session s; connections.TryGetValue(Key, out s); if (s != null) { s.Reset(); Log_Info("Reset Rejected Connection"); break; } //Is from FixedPort Key.IP0 = 0; Key.IP1 = 0; Key.IP2 = 0; Key.IP3 = 0; Key.SRVPort = 0; connections.TryGetValue(Key, out s); if (s != null) { s.Reset(); Log_Info("Reset Rejected Connection"); break; } Log_Error("Failed To Reset Rejected Connection"); break; default: throw new NotImplementedException("Unsupported ICMP Code For Destination Unreachable" + icmp.Code); } break; default: throw new NotImplementedException("Unsupported ICMP Type" + icmp.Type); } return(true); }
public override bool Send(IPPayload payload) { Log_Info("DNS Packet Sent To CLR_DEV9 DNS server"); Log_Info("Contents"); DNS dns = new DNS(payload.GetPayload()); if (dns.OPCode == (byte)DNSOPCode.Query & dns.QuestionCount > 0 & dns.QR == false) { List <string> reqs = new List <string>(); foreach (DNSQuestionEntry q in dns.Questions) { if (q.Type == 1 & q.Class == 1) { reqs.Add(q.Name); } else { Log_Error("Unexpected question type of class, T:" + q.Type.ToString() + " C:" + q.Class.ToString()); } } if (reqs.Count == 0) { return(true); } if (dns.TC == true) { throw new NotImplementedException("Truncated DNS packet"); } //Interlocked.Increment(ref open); DNS ret = new DNS { ID = dns.ID, //TODO, drop duplicate requests based on ID QR = true, OPCode = (byte)DNSOPCode.Query, AA = false, TC = false, RD = true, RA = true, AD = false, CD = false, RCode = (byte)DNSRCode.NoError, //Counts Questions = dns.Questions }; DNSState state = new DNSState(reqs.Count, reqs.ToArray(), ret, ((UDP)payload).SourcePort); foreach (string req in reqs) { if (CheckHost(req, state)) { continue; } Task <bool> res = GetHost(req, state); } return(true); } else { Log_Error("Unexpected OPCode, Code:" + dns.OPCode); return(true); } }
public override bool Send(IPPayload payload) { lock (deathClock) { deathClock.Restart(); } UDP udp = (UDP)payload; if (destPort != 0) { //client already created if (!(udp.DestinationPort == destPort && udp.SourcePort == srcPort)) { Log_Error("UDP packet invalid for current session (Duplicate key?)"); return(false); } } else { //create client destPort = udp.DestinationPort; srcPort = udp.SourcePort; //Multicast address start with 0b1110 if ((DestIP[0] & 0xF0) == 0xE0) { isMulticast = true; } //needs testing if (isMulticast) { Log_Info("Is Multicast"); client = new UdpClient(new IPEndPoint(adapterIP, 0)); //client.JoinMulticastGroup(address); } else { IPAddress address = new IPAddress(DestIP); client = new UdpClient(new IPEndPoint(adapterIP, 0)); client.Connect(address, destPort); } if (srcPort != 0) { open = true; } } if (destPort == 53) { Log_Info("DNS Packet Sent To " + new IPAddress(DestIP)); Log_Info("Contents"); PacketReader.DNS.DNS pDNS = new PacketReader.DNS.DNS(udp.GetPayload()); } if (isBroadcast) { client.Send(udp.GetPayload(), udp.GetPayload().Length, new IPEndPoint(IPAddress.Broadcast, destPort)); } else if (isMulticast | isFixedPort) { client.Send(udp.GetPayload(), udp.GetPayload().Length, new IPEndPoint(new IPAddress(DestIP), destPort)); } else { client.Send(udp.GetPayload(), udp.GetPayload().Length); } if (srcPort == 0) { RaiseEventConnectionClosed(); } return(true); }