public static DhcpPacket CreateFromRaw(byte[] rawPacket) { DhcpPacket packet = new DhcpPacket(); using (var reader = new BinaryReader(new MemoryStream(rawPacket))) { packet.MessageType = (DhcpMessageType)reader.ReadByte(); HardwareAddressType hardwareAddressType = (HardwareAddressType)reader.ReadByte(); byte hardwareAddressLength = reader.ReadByte(); packet.Hops = reader.ReadByte(); packet.TransactionId = (uint)IPAddress.NetworkToHostOrder(reader.ReadInt32()); packet.SecondsElapsed = (ushort)IPAddress.NetworkToHostOrder(reader.ReadInt16()); packet.Flags = (ushort)IPAddress.NetworkToHostOrder(reader.ReadInt16()); packet.ClientAddress = new IPAddress(reader.ReadBytes(4)); packet.YourClientAddress = new IPAddress(reader.ReadBytes(4)); packet.NextServerAddress = new IPAddress(reader.ReadBytes(4)); packet.RelayAgentAddress = new IPAddress(reader.ReadBytes(4)); packet.HardwareAddress = new HardwareAddress(hardwareAddressType, reader.ReadBytes(hardwareAddressLength)); reader.ReadBytes(16 - hardwareAddressLength); // padding packet.ServerName = Encoding.ASCII.GetString(reader.ReadBytes(64)); packet.BootFileName = Encoding.ASCII.GetString(reader.ReadBytes(128)); int packetMagicCookie = IPAddress.NetworkToHostOrder(reader.ReadInt32()); if (packetMagicCookie != MagicCookie) { throw new InvalidDataException("Magic cookie not found!"); } byte messageType = reader.ReadByte(); if (messageType != 53) { throw new InvalidDataException("Options should start with Message type!"); } reader.ReadByte(); // length packet.MessageType = (DhcpMessageType)reader.ReadByte(); var optionFactory = new DhcpOptionFactory(); byte optionType = reader.ReadByte(); while (optionType != 255) { DhcpOptionType type = (DhcpOptionType)optionType; byte length = reader.ReadByte(); byte[] optionData = reader.ReadBytes(length); DhcpOption option = optionFactory.CreateFromRaw(type, optionData); if (option != null) { packet.Options.Add(option); } else { Console.WriteLine($"WARN: Unsupported option type: {type}"); } optionType = reader.ReadByte(); } } return(packet); }
public override string ToString() { var sb = new StringBuilder(); sb.Append("Message Type (op): ").AppendLine(MessageType.ToString()); sb.Append("Hardware Address Type (htype): ").AppendLine(HardwareAddressType.ToString()); sb.Append("Hardware Address Length (hlen): ").AppendLine(HardwareAddressLength.ToString()); sb.Append("Gateway Hops (hops): ").AppendLine(GatewayHops.ToString()); sb.Append("Transaction Id (xid): ").AppendLine(TransactionId.ToString()); sb.Append("Seconds Elapsed (secs): ").AppendLine(SecondsElapsed.ToString()); sb.Append("Flags (flags): ").AppendLine(Convert.ToString((int)Flags, 2)); foreach (DhcpServerPacketFlags flag in Enum.GetValues(typeof(DhcpServerPacketFlags))) { sb.Append(" "); var mask = Convert.ToString((short)flag, 2).Replace('0', '.'); if (Flags.HasFlag(flag)) { sb.Append(mask).Append(": ").AppendLine(flag.ToString()); } else { sb.Append(mask.Replace('1', '0')).Append(": No ").AppendLine(flag.ToString()); } } sb.Append("Client IP Address (ciaddr): ").AppendLine(ClientIpAddress.ToString()); sb.Append("Your IP Address (yiaddr): ").AppendLine(YourIpAddress.ToString()); sb.Append("Next Server IP Address (siaddr): ").AppendLine(NextServerIpAddress.ToString()); sb.Append("Relay Agent IP Address (giaddr): ").AppendLine(RelayAgentIpAddress.ToString()); sb.Append("Client Hardware Address (chaddr): ").AppendLine(ClientHardwareAddress.ToString()); sb.Append("Server Host Name (sname): ").AppendLine(ServerHostName); sb.Append("File Name (file): ").AppendLine(FileName); sb.Append("Options Magic Cookie: ").AppendLine(OptionsMagicCookie.ToString()); var options = Options.ToList(); if (options.Count == 0) { sb.Append("Options: None"); } else { sb.Append("Options:"); foreach (var option in options) { sb.AppendLine(); sb.Append(" "); sb.Append(((byte)option.Id).ToString("000")); sb.Append(" "); sb.Append(option.Id.ToString()); sb.Append(" ["); sb.Append(option.Type.ToString()); sb.AppendLine("]"); sb.Append(" "); option.DataAsFormatted(sb); } } return(sb.ToString()); }
/// <summary cref="Packet.ToString(StringOutputType)" /> public override string ToString(StringOutputType outputFormat) { var buffer = new StringBuilder(); string color = ""; string colorEscape = ""; if (outputFormat == StringOutputType.Colored || outputFormat == StringOutputType.VerboseColored) { color = Color; colorEscape = AnsiEscapeSequences.Reset; } if (outputFormat == StringOutputType.Normal || outputFormat == StringOutputType.Colored) { // build the output string buffer.AppendFormat("{0}[ARPPacket: Operation={2}, SenderHardwareAddress={3}, TargetHardwareAddress={4}, SenderProtocolAddress={5}, TargetProtocolAddress={6}]{1}", color, colorEscape, Operation, HexPrinter.PrintMACAddress(SenderHardwareAddress), HexPrinter.PrintMACAddress(TargetHardwareAddress), SenderProtocolAddress, TargetProtocolAddress); } if (outputFormat == StringOutputType.Verbose || outputFormat == StringOutputType.VerboseColored) { // collect the properties and their value Dictionary <string, string> properties = new Dictionary <string, string>(); properties.Add("hardware type", HardwareAddressType.ToString() + " (0x" + HardwareAddressType.ToString("x") + ")"); properties.Add("protocol type", ProtocolAddressType.ToString() + " (0x" + ProtocolAddressType.ToString("x") + ")"); properties.Add("operation", Operation.ToString() + " (0x" + Operation.ToString("x") + ")"); properties.Add("source hardware address", HexPrinter.PrintMACAddress(SenderHardwareAddress)); properties.Add("destination hardware address", HexPrinter.PrintMACAddress(TargetHardwareAddress)); properties.Add("source protocol address", SenderProtocolAddress.ToString()); properties.Add("destination protocol address", TargetProtocolAddress.ToString()); // calculate the padding needed to right-justify the property names int padLength = Utils.RandomUtils.LongestStringLength(new List <string>(properties.Keys)); // build the output string buffer.AppendLine("ARP: ******* ARP - \"Address Resolution Protocol\" - offset=? length=" + TotalPacketLength); buffer.AppendLine("ARP:"); foreach (var property in properties) { buffer.AppendLine("ARP: " + property.Key.PadLeft(padLength) + " = " + property.Value); } buffer.AppendLine("ARP:"); } // append the base string output buffer.Append(base.ToString(outputFormat)); return(buffer.ToString()); }
/// <summary> /// Creates a new instance of this class /// </summary> public ARPFrame() { arpHardwareAddressType = HardwareAddressType.Ethernet; arpProtocolAddressType = EtherType.IPv4; arpOperation = ARPOperation.Request; macSource = new MACAddress(); macDestination = new MACAddress(); ipaSource = IPAddress.Any; ipaDestination = IPAddress.Any; this.fEncapsulatedFrame = null; }
static ClientHardwareAddress ReadClientHardwareAddress(HardwareAddressType htype, byte[] buffer, long offset) { switch (htype) { case HardwareAddressType.Ethernet: if ((offset + 6) < buffer.Length) { var addressArray = new byte[6]; Array.Copy(buffer, offset, addressArray, 0, 6); return(new EthernetClientHardwareAddress(addressArray)); } throw new IndexOutOfRangeException("Provided buffer and starting index is too short to supply a 6 byte hardware address"); default: throw new Exception("Unhandled hardware address type " + htype.ToString()); } }
/// <summary> /// Creates a new instance of this class /// </summary> public DHCPFrame() { dhtMessageType = DHCPType.BootRequest; dhtHardwareType = eExNetworkLibrary.HardwareAddressType.Ethernet; sHardwarelen = 6; sHops = 0; iTransactionID = 0; iSecs = 0; bValidIPFlag = true; ipaClientAddress = IPAddress.Any; ipaOwnAddress = IPAddress.Any; ipaServerAddress = IPAddress.Any; ipaRelayAddress = IPAddress.Any; macClientMac = MACAddress.Empty; strRequestedFile = ""; strRequestedServerName = ""; lTLVs = new List <DHCPTLVItem>(); }
/// <summary> /// Creates a newinstance of this class from the given data. /// </summary> /// <param name="bData">The data to parse.</param> public ARPFrame(byte[] bData) { arpHardwareAddressType = (HardwareAddressType)((int)((bData[0] << 8) + bData[1])); arpProtocolAddressType = (EtherType)((int)((bData[2] << 8) + bData[3])); int iHardwareAddressLength = bData[4]; int iProtocolAddressLength = bData[5]; arpOperation = (ARPOperation)((int)((bData[6] << 8) + bData[7])); if (arpHardwareAddressType != HardwareAddressType.Ethernet || (arpProtocolAddressType != EtherType.IPv4 && arpProtocolAddressType != EtherType.IPv6)) { throw new ArgumentException("Only IPv6 and IPv4 in conjunction with ethernet is supported at the moment."); } if (iHardwareAddressLength != 6 && arpHardwareAddressType != HardwareAddressType.Ethernet) { throw new ArgumentException("The hardware address type of the ARP frame indicates Ethernet, but the address data is not 6 bytes long."); } if (iProtocolAddressLength != 4 && EtherType.IPv4 == arpProtocolAddressType) { throw new ArgumentException("The protocol address type of the ARP frame indicates IPv4, but the address data is not 4 bytes long."); } if (iProtocolAddressLength != 16 && EtherType.IPv6 == arpProtocolAddressType) { throw new ArgumentException("The protocol address type of the ARP frame indicates IPv6, but the address data is not 16 bytes long."); } int iC1 = 8; byte[] bAddress = new byte[iHardwareAddressLength]; for (int iC2 = 0; iC2 < iHardwareAddressLength; iC2++) { Array.Copy(bData, iC1, bAddress, 0, iHardwareAddressLength); } iC1 += iHardwareAddressLength; macSource = new MACAddress(bAddress); bAddress = new byte[iProtocolAddressLength]; Array.Copy(bData, iC1, bAddress, 0, iProtocolAddressLength); iC1 += iProtocolAddressLength; ipaSource = new IPAddress(bAddress); bAddress = new byte[iHardwareAddressLength]; Array.Copy(bData, iC1, bAddress, 0, iHardwareAddressLength); iC1 += iHardwareAddressLength; macDestination = new MACAddress(bAddress); bAddress = new byte[iProtocolAddressLength]; Array.Copy(bData, iC1, bAddress, 0, iProtocolAddressLength); iC1 += iProtocolAddressLength; ipaDestination = new IPAddress(bAddress); byte[] bPad = new byte[bData.Length - iC1]; for (int iC2 = 0; iC2 < bPad.Length; iC2++) { bPad[iC2] = bData[iC2 + iC1]; } this.fEncapsulatedFrame = new RawDataFrame(bPad); }
/// <summary> /// Creates a new instance of this class by parsing the given data /// </summary> /// <param name="bData">The data to parse</param> public DHCPFrame(byte[] bData) { byte[] bAddressBytes = new byte[4]; byte[] bMacBytes = new byte[6]; lTLVs = new List <DHCPTLVItem>(); DHCPTLVItem dhcpItem; dhtMessageType = (DHCPType)bData[0]; dhtHardwareType = (HardwareAddressType)bData[1]; sHardwarelen = (short)bData[2]; sHops = (short)bData[3]; iTransactionID = BitConverter.ToInt32(bData, 4); iSecs = BitConverter.ToInt16(bData, 8); bValidIPFlag = (bData[10] & 0x80) == 1; for (int iC1 = 0; iC1 < 4; iC1++) { bAddressBytes[iC1] = bData[12 + iC1]; } ipaClientAddress = new IPAddress(bAddressBytes); for (int iC1 = 0; iC1 < 4; iC1++) { bAddressBytes[iC1] = bData[16 + iC1]; } ipaOwnAddress = new IPAddress(bAddressBytes); for (int iC1 = 0; iC1 < 4; iC1++) { bAddressBytes[iC1] = bData[20 + iC1]; } ipaServerAddress = new IPAddress(bAddressBytes); for (int iC1 = 0; iC1 < 4; iC1++) { bAddressBytes[iC1] = bData[24 + iC1]; } ipaRelayAddress = new IPAddress(bAddressBytes); for (int iC1 = 0; iC1 < 6; iC1++) { bMacBytes[iC1] = bData[28 + iC1]; } macClientMac = new MACAddress(bMacBytes); strRequestedServerName = Encoding.ASCII.GetString(bData, 34, 64).Trim(new char[] { '\0' }); strRequestedFile = Encoding.ASCII.GetString(bData, 98, 128).Trim(new char[] { '\0' }); if (bData[236] != 0x63 || bData[237] != 0x82 || bData[238] != 0x53 || bData[239] != 0x63) { throw new Exception("Invalid DHCP magic number"); } int iC2 = 240; while (iC2 < bData.Length) { if (bData[iC2] == (int)DHCPOptions.End) { break; } dhcpItem = new DHCPTLVItem(bData, iC2); lTLVs.Add(dhcpItem); iC2 += dhcpItem.Length; } }
public HardwareAddress(HardwareAddressType type, byte[] address) { Type = type; Address = address; }