public static void StartCapture(SocketObject state) { var deviceState1 = new DeviceState(); lock (deviceState1) { deviceState1.Device = state.device; deviceState1.Handle = IntPtr.Zero; deviceState1.Cancel = false; deviceState1.State = state; var num = Marshal.AllocHGlobal(12); try { if (_activeDevices.ContainsKey(state.device.Name)) { StopCapture(state.device.Name); } var errbuff = new StringBuilder(256); deviceState1.Handle = pcap_open(state.device.Name, 65536, 0, 500, IntPtr.Zero, errbuff); if (deviceState1.Handle == IntPtr.Zero) { throw new ApplicationException("Cannot open pcap interface [" + state.device.Name + "]. Error: " + errbuff); } deviceState1.LinkType = pcap_datalink(deviceState1.Handle); if (deviceState1.LinkType != 1 && deviceState1.LinkType != 0) { throw new ApplicationException("Interface [" + state.device.Description + "] does not appear to support Ethernet."); } if (pcap_compile(deviceState1.Handle, num, "ip and tcp", 1, 0U) != 0) { throw new ApplicationException("Unable to create TCP packet filter."); } if (pcap_setfilter(deviceState1.Handle, num) != 0) { throw new ApplicationException("Unable to apply TCP packet filter."); } pcap_freecode(num); _activeDevices[state.device.Name] = deviceState1; } catch (Exception ex) { if (deviceState1.Handle != IntPtr.Zero) { pcap_close(deviceState1.Handle); } throw new ApplicationException("Unable to open winpcap device [" + state.device.Name + "].", ex); } finally { Marshal.FreeHGlobal(num); } ThreadPool.QueueUserWorkItem(PollNetworkDevice, _activeDevices[state.device.Name]); } }
public static void StartCapture(SocketObject state) { var deviceState1 = new DeviceState(); lock (deviceState1) { deviceState1.Device = state.device; deviceState1.Handle = IntPtr.Zero; deviceState1.Cancel = false; deviceState1.State = state; var num = Marshal.AllocHGlobal(12); try { if (_activeDevices.ContainsKey(state.device.Name)) { StopCapture(state.device.Name); } var errbuff = new StringBuilder(256); deviceState1.Handle = pcap_open(state.device.Name, 65536, 0, 500, IntPtr.Zero, errbuff); if (deviceState1.Handle == IntPtr.Zero) { throw new ApplicationException("Cannot open pcap interface [" + state.device.Name + "]. Error: " + errbuff); } deviceState1.LinkType = pcap_datalink(deviceState1.Handle); if (deviceState1.LinkType != 1 && deviceState1.LinkType != 0) { throw new ApplicationException("Interface [" + state.device.Description + "] does not appear to support Ethernet."); } if (pcap_compile(deviceState1.Handle, num, "ip and tcp", 1, 0U) != 0) { throw new ApplicationException("Unable to create TCP packet filter."); } if (pcap_setfilter(deviceState1.Handle, num) != 0) { throw new ApplicationException("Unable to apply TCP packet filter."); } pcap_freecode(num); _activeDevices[state.device.Name] = deviceState1; } catch (Exception ex) { if (deviceState1.Handle != IntPtr.Zero) { pcap_close(deviceState1.Handle); } throw new ApplicationException("Unable to open winpcap device [" + state.device.Name + "].", ex); } finally { Marshal.FreeHGlobal(num); } ThreadPool.QueueUserWorkItem(PollNetworkDevice, _activeDevices[state.device.Name]); } }
private void ParseData(SocketObject asyncState, byte[] byteData, int nReceived) { if ((byteData == null) || (byteData[9] != 6)) { return; } var startIndex = (byte) ((byteData[0] & 15) * 4); var lengthCheck = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(byteData, 2)); if ((nReceived < lengthCheck) || (startIndex > lengthCheck)) { return; } var IP = new IPHeader(byteData, nReceived); var TCP = new TCPHeader(byteData, nReceived); var serverConnection = new ServerConnection { SourceAddress = (uint) BitConverter.ToInt32(byteData, 12), DestinationAddress = (uint) BitConverter.ToInt32(byteData, 16), SourcePort = (ushort) BitConverter.ToInt16(byteData, startIndex), DestinationPort = (ushort) BitConverter.ToInt16(byteData, startIndex + 2), TimeStamp = DateTime.Now /* // these don't return the right ports for some reason DestinationAddress = BitConverter.ToUInt32(IP.DestinationAddress.GetAddressBytes(), 0), DestinationPort = Convert.ToUInt16(TCP.DestinationPort), SourcePort = Convert.ToUInt16(TCP.SourcePort), SourceAddress = BitConverter.ToUInt32(IP.SourceAddress.GetAddressBytes(), 0), TimeStamp = DateTime.Now */ }; lock (Lock) { var found = Enumerable.Contains(ServerConnections, serverConnection); if (!found) { if (Enumerable.Contains(DroppedConnections, serverConnection)) { return; } UpdateConnectionList(); if (!Enumerable.Contains(ServerConnections, serverConnection)) { DroppedConnections.Add(serverConnection); return; } } } if ((startIndex + 12) > nReceived) { return; } var nextTCPSequence = (uint) IPAddress.NetworkToHostOrder(BitConverter.ToInt32(byteData, startIndex + 4)); var cut = (byte) (((byteData[startIndex + 12] & 240) >> 4) * 4); var length = (nReceived - startIndex) - cut; if ((length < 0) || (length > 0x10000)) { return; } if (lengthCheck == startIndex + cut) { return; } lock (asyncState.SocketLock) { var connection = asyncState.Connections.FirstOrDefault(x => x.Equals(serverConnection)); if (connection == null) { connection = new NetworkConnection { SourceAddress = serverConnection.SourceAddress, SourcePort = serverConnection.SourcePort, DestinationAddress = serverConnection.DestinationAddress, DestinationPort = serverConnection.DestinationPort }; asyncState.Connections.Add(connection); } if (length == 0) { return; } var destinationBuffer = new byte[length]; Array.Copy(byteData, startIndex + cut, destinationBuffer, 0, length); if (connection.StalePackets.ContainsKey(nextTCPSequence)) { connection.StalePackets.Remove(nextTCPSequence); } var packet = new NetworkPacket { TCPSequence = nextTCPSequence, Buffer = destinationBuffer, Push = (byteData[startIndex + 13] & 8) != 0 }; connection.StalePackets.Add(nextTCPSequence, packet); if (!connection.NextTCPSequence.HasValue) { connection.NextTCPSequence = nextTCPSequence; } if (connection.StalePackets.Count == 1) { connection.LastGoodNetworkPacketTime = DateTime.Now; } if (!connection.StalePackets.Any(x => (x.Key <= connection.NextTCPSequence.Value))) { if (DateTime.Now.Subtract(connection.LastGoodNetworkPacketTime) .TotalSeconds <= 10.0) { return; } connection.NextTCPSequence = connection.StalePackets.Min(x => x.Key); } while (connection.StalePackets.Any(x => x.Key <= connection.NextTCPSequence.Value)) { NetworkPacket stalePacket; uint sequenceLength = 0; if (connection.StalePackets.ContainsKey(connection.NextTCPSequence.Value)) { stalePacket = connection.StalePackets[connection.NextTCPSequence.Value]; } else { stalePacket = (connection.StalePackets.Where(x => x.Key <= connection.NextTCPSequence.Value) .OrderBy(x => x.Key)).FirstOrDefault() .Value; sequenceLength = connection.NextTCPSequence.Value - stalePacket.TCPSequence; } connection.StalePackets.Remove(stalePacket.TCPSequence); if (connection.NetworkBufferPosition == 0) { connection.LastNetworkBufferUpdate = DateTime.Now; } if (sequenceLength >= stalePacket.Buffer.Length) { continue; } connection.NextTCPSequence = stalePacket.TCPSequence + ((uint) stalePacket.Buffer.Length); Array.Copy(stalePacket.Buffer, sequenceLength, connection.NetworkBuffer, connection.NetworkBufferPosition, stalePacket.Buffer.Length - sequenceLength); connection.NetworkBufferPosition += stalePacket.Buffer.Length - ((int) sequenceLength); if (stalePacket.Push) { ProcessNetworkBuffer(connection); } } } }
private void ParseData(SocketObject asyncState, byte[] byteData, int nReceived) { if ((byteData == null) || (byteData[9] != 6)) { return; } var startIndex = (byte)((byteData[0] & 15) * 4); var lengthCheck = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(byteData, 2)); if ((nReceived < lengthCheck) || (startIndex > lengthCheck)) { return; } var IP = new IPHeader(byteData, nReceived); var TCP = new TCPHeader(byteData, nReceived); var serverConnection = new ServerConnection { SourceAddress = (uint)BitConverter.ToInt32(byteData, 12), DestinationAddress = (uint)BitConverter.ToInt32(byteData, 16), SourcePort = (ushort)BitConverter.ToInt16(byteData, startIndex), DestinationPort = (ushort)BitConverter.ToInt16(byteData, startIndex + 2), TimeStamp = DateTime.Now /* * // these don't return the right ports for some reason * DestinationAddress = BitConverter.ToUInt32(IP.DestinationAddress.GetAddressBytes(), 0), * DestinationPort = Convert.ToUInt16(TCP.DestinationPort), * SourcePort = Convert.ToUInt16(TCP.SourcePort), * SourceAddress = BitConverter.ToUInt32(IP.SourceAddress.GetAddressBytes(), 0), * TimeStamp = DateTime.Now */ }; lock (Lock) { var found = Enumerable.Contains(ServerConnections, serverConnection); if (!found) { if (Enumerable.Contains(DroppedConnections, serverConnection)) { return; } UpdateConnectionList(); if (!Enumerable.Contains(ServerConnections, serverConnection)) { DroppedConnections.Add(serverConnection); return; } } } if ((startIndex + 12) > nReceived) { return; } var nextTCPSequence = (uint)IPAddress.NetworkToHostOrder(BitConverter.ToInt32(byteData, startIndex + 4)); var cut = (byte)(((byteData[startIndex + 12] & 240) >> 4) * 4); var length = (nReceived - startIndex) - cut; if ((length < 0) || (length > 0x10000)) { return; } if (lengthCheck == startIndex + cut) { return; } lock (asyncState.SocketLock) { var connection = asyncState.Connections.FirstOrDefault(x => x.Equals(serverConnection)); if (connection == null) { connection = new NetworkConnection { SourceAddress = serverConnection.SourceAddress, SourcePort = serverConnection.SourcePort, DestinationAddress = serverConnection.DestinationAddress, DestinationPort = serverConnection.DestinationPort }; asyncState.Connections.Add(connection); } if (length == 0) { return; } var destinationBuffer = new byte[length]; Array.Copy(byteData, startIndex + cut, destinationBuffer, 0, length); if (connection.StalePackets.ContainsKey(nextTCPSequence)) { connection.StalePackets.Remove(nextTCPSequence); } var packet = new NetworkPacket { TCPSequence = nextTCPSequence, Buffer = destinationBuffer, Push = (byteData[startIndex + 13] & 8) != 0 }; connection.StalePackets.Add(nextTCPSequence, packet); if (!connection.NextTCPSequence.HasValue) { connection.NextTCPSequence = nextTCPSequence; } if (connection.StalePackets.Count == 1) { connection.LastGoodNetworkPacketTime = DateTime.Now; } if (!connection.StalePackets.Any(x => (x.Key <= connection.NextTCPSequence.Value))) { if (DateTime.Now.Subtract(connection.LastGoodNetworkPacketTime) .TotalSeconds <= 10.0) { return; } connection.NextTCPSequence = connection.StalePackets.Min(x => x.Key); } while (connection.StalePackets.Any(x => x.Key <= connection.NextTCPSequence.Value)) { NetworkPacket stalePacket; uint sequenceLength = 0; if (connection.StalePackets.ContainsKey(connection.NextTCPSequence.Value)) { stalePacket = connection.StalePackets[connection.NextTCPSequence.Value]; } else { stalePacket = (connection.StalePackets.Where(x => x.Key <= connection.NextTCPSequence.Value) .OrderBy(x => x.Key)).FirstOrDefault() .Value; sequenceLength = connection.NextTCPSequence.Value - stalePacket.TCPSequence; } connection.StalePackets.Remove(stalePacket.TCPSequence); if (connection.NetworkBufferPosition == 0) { connection.LastNetworkBufferUpdate = DateTime.Now; } if (sequenceLength >= stalePacket.Buffer.Length) { continue; } connection.NextTCPSequence = stalePacket.TCPSequence + ((uint)stalePacket.Buffer.Length); Array.Copy(stalePacket.Buffer, sequenceLength, connection.NetworkBuffer, connection.NetworkBufferPosition, stalePacket.Buffer.Length - sequenceLength); connection.NetworkBufferPosition += stalePacket.Buffer.Length - ((int)sequenceLength); if (stalePacket.Push) { ProcessNetworkBuffer(connection); } } } }