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) { 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) { 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); }