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()); }
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()); } }