private PortInfo ParsePortInfo(Packet responce) { TcpDatagram datagram = responce.Ethernet.IpV4.Tcp; TcpControlBits bits = responce.Ethernet.IpV4.Tcp.ControlBits; bool isSynAck = bits.HasFlag(TcpControlBits.Acknowledgment) && bits.HasFlag(TcpControlBits.Synchronize); PortInfo info = new PortInfo(datagram.SourcePort, isSynAck); return(info); }
private void ReceivePackets(object arg) { CancellationToken ct = (CancellationToken)arg; using (PacketCommunicator communicator = options.Device.Open(65535, PacketDeviceOpenAttributes.Promiscuous | PacketDeviceOpenAttributes.NoCaptureLocal, 100)) { communicator.SetFilter("tcp and src " + options.TargetIP + " and dst " + options.SourceIP); while (true) { if (ct.IsCancellationRequested) { return; } Packet responce; PacketCommunicatorReceiveResult result = communicator.ReceivePacket(out responce); if (result == PacketCommunicatorReceiveResult.Ok) { PortInfo info = ParsePortInfo(responce); Packet rstPacket = TcpPacketFactory.CreateRstPacketFor(this.options, info.Number); communicator.SendPacket(rstPacket); if (scanResults.Add(info)) { Console.WriteLine(info.ToString()); } int totalPorts = options.EndPort - options.StartPort; if (scanResults.Count == totalPorts) { break; } } } } }