private void ParseData(byte[] byteData, int nReceived) { IPHeader ipHeader = new IPHeader(byteData, nReceived); switch (ipHeader.ProtocolType) { /*case Protocol.TCP: { * TCPHeader tcpHeader = new TCPHeader(ipHeader.Data, ipHeader.MessageLength); * if (tcpHeader.Flags.Equals("0x18 (PSH, ACK)")) { * byte[] data = new byte[(int)ipHeader.MessageLength]; * for (int i = 0; i < data.Length; i++) data[i] = tcpHeader.Data[i]; * string content = Encoding.ASCII.GetString(data); * if (content.StartsWith("GET ")) parseData(ipHeader, tcpHeader, content, true); * else if (content.StartsWith("HTTP/1.1 ")) parseData(ipHeader, tcpHeader, content, false); * } * break; * }*/ case Protocol.UDP: { UDPHeader udpHeader = new UDPHeader(ipHeader.Data, ipHeader.MessageLength); int destinationPort = Int32.Parse(udpHeader.DestinationPort); int sourcePort = Int32.Parse(udpHeader.SourcePort); if (destinationPort == 53 || sourcePort == 53) { byte[] fillBuffer = new byte[ipHeader.MessageLength]; string result = string.Empty; for (int i = 0, c = 0; i < fillBuffer.Length; i++) { int byteInt = ipHeader.Data[i]; if (byteInt < 32) { if (c > 3 && result.Length > 0) { result += "."; } else { c++; } } else if (byteInt > 32 && byteInt < 127) { result += (char)byteInt; } } char[] charArray = result.ToCharArray(); char oldChar = '.'; for (int i = 0; i < charArray.Length; i++) { if (i > 3 && charArray[i] != oldChar && charArray[i - 1] == oldChar && charArray[i - 2] == oldChar) { result = string.Empty; for (int j = i; j < charArray.Length; j++) { result += charArray[j]; } break; } oldChar = charArray[i]; } result = result.Replace(".....", ""); Console.WriteLine("DNS: " + result); } break; } default: { break; } } }
private void ParseData(byte[] byteData, int nReceived) { IPHeader ipHeader = new IPHeader(byteData, nReceived); switch (ipHeader.ProtocolType) { /*case Protocol.TCP: { TCPHeader tcpHeader = new TCPHeader(ipHeader.Data, ipHeader.MessageLength); if (tcpHeader.Flags.Equals("0x18 (PSH, ACK)")) { byte[] data = new byte[(int)ipHeader.MessageLength]; for (int i = 0; i < data.Length; i++) data[i] = tcpHeader.Data[i]; string content = Encoding.ASCII.GetString(data); if (content.StartsWith("GET ")) parseData(ipHeader, tcpHeader, content, true); else if (content.StartsWith("HTTP/1.1 ")) parseData(ipHeader, tcpHeader, content, false); } break; }*/ case Protocol.UDP: { UDPHeader udpHeader = new UDPHeader(ipHeader.Data, ipHeader.MessageLength); int destinationPort = Int32.Parse(udpHeader.DestinationPort); int sourcePort = Int32.Parse(udpHeader.SourcePort); if (destinationPort == 53 || sourcePort == 53) { byte[] fillBuffer = new byte[ipHeader.MessageLength]; string result = string.Empty; for (int i = 0, c=0; i < fillBuffer.Length; i++) { int byteInt = ipHeader.Data[i]; if (byteInt < 32) { if (c > 3 && result.Length > 0) result += "."; else c++; } else if (byteInt > 32 && byteInt < 127) result += (char)byteInt; } char[] charArray = result.ToCharArray(); char oldChar = '.'; for (int i = 0; i < charArray.Length; i++) { if (i > 3 && charArray[i] != oldChar && charArray[i-1] == oldChar && charArray[i-2] == oldChar) { result = string.Empty; for (int j = i; j < charArray.Length; j++) result += charArray[j]; break; } oldChar = charArray[i]; } result = result.Replace(".....", ""); Console.WriteLine("DNS: " + result); } break; } default: { break; } } }