public void SendLayer3Packet(OSI.Layer3Packet packet, PacketSentHandler callback) { IpV4Layer ipV4Layer = new IpV4Layer { Source = new IpV4Address(packet.SourceIP.AsString), Ttl = packet.Ttl, // The rest of the important parameters will be set for each packet }; Common.IPv4Address ip = null; try { System.Net.IPAddress.Parse(packet.Destination); ip = new Common.IPv4Address(packet.Destination); } catch { ip = Dns.ResolveHost(packet.Destination).IPs[0]; } ipV4Layer.CurrentDestination = new IpV4Address(ip.AsString); OSI.Layer2Packet l2 = new OSI.Layer2Packet(); l2.SourceMac = Configuration.MacAddress; l2.DestinationMac = Arp.ResolveIP(ip); foreach (ILayer layer in packet.NextLayers) { l2.NextLayers.Add(layer); } l2.NextLayers.Insert(0, ipV4Layer); SendLayer2Packet(l2, callback); }
private void SendPacket(Packet pack, PacketSentHandler callback) { if (NetworkFirewall.TestOutgoing(new Common.PacketData(pack))) { Communicator.SendPacket(pack); callback(); } }
public void SendLayer4Packet(OSI.Layer4Packet packet, PacketSentHandler callback) { switch (packet.Protocol) { case OSI.TransportProtocol.TCP: SendTcpPacket(packet as OSI.Layer4TcpPacket, callback); break; case OSI.TransportProtocol.UDP: SendUdpPacket(packet as OSI.Layer4UdpPacket, callback); break; } }
public void SendLayer2Packet(OSI.Layer2Packet packet, PacketSentHandler callback) { EthernetLayer ethernetLayer = new EthernetLayer { Source = new MacAddress(packet.SourceMac.AsString), Destination = new MacAddress(packet.DestinationMac.AsString), EtherType = EthernetType.None, // Will be filled automatically. }; List <ILayer> layers = new List <ILayer>(packet.NextLayers); layers.Insert(0, ethernetLayer); Packet pack = PacketBuilder.Build(DateTime.Now, layers.ToArray()); SendPacket(pack, callback); }
private void SendTcpPacket(OSI.Layer4TcpPacket packet, PacketSentHandler callback) { TcpLayer tcpLayer = new TcpLayer { SourcePort = packet.LocalPort, DestinationPort = packet.RemotePort, Checksum = null, // Will be filled automatically. SequenceNumber = packet.SequenceNumber, AcknowledgmentNumber = packet.AcknowledgementNumber, Window = 64512, }; if (packet.ACK) { tcpLayer.ControlBits = tcpLayer.ControlBits | TcpControlBits.Acknowledgment; } if (packet.SYN) { tcpLayer.ControlBits = tcpLayer.ControlBits | TcpControlBits.Synchronize; } if (packet.RST) { tcpLayer.ControlBits = tcpLayer.ControlBits | TcpControlBits.Reset; } if (packet.PSH) { tcpLayer.ControlBits = tcpLayer.ControlBits | TcpControlBits.Push; } if (packet.FIN) { tcpLayer.ControlBits = tcpLayer.ControlBits | TcpControlBits.Fin; } OSI.Layer3Packet l3 = new OSI.Layer3Packet(); l3.Ttl = 255; l3.SourceIP = Configuration.IpAddress; l3.Destination = packet.Destination; foreach (ILayer layer in packet.NextLayers) { l3.NextLayers.Add(layer); } l3.NextLayers.Insert(0, tcpLayer); SendLayer3Packet(l3, callback); }
private void SendUdpPacket(OSI.Layer4UdpPacket packet, PacketSentHandler callback) { UdpLayer udpLayer = new UdpLayer { SourcePort = packet.LocalPort, DestinationPort = packet.RemotePort, Checksum = null, // Will be filled automatically. CalculateChecksumValue = true, }; OSI.Layer3Packet l3 = new OSI.Layer3Packet(); l3.Ttl = 255; l3.SourceIP = Configuration.IpAddress; l3.Destination = packet.Destination; foreach (ILayer layer in packet.NextLayers) { l3.NextLayers.Add(layer); } l3.NextLayers.Insert(0, udpLayer); SendLayer3Packet(l3, callback); }