예제 #1
0
 /// <summary>
 /// Returns a hash code for the layer.
 /// The hash code is a XOR of a combination of the protocol type and operation and the hash codes of the layer length and data link.
 /// </summary>
 public override int GetHashCode()
 {
     return(base.GetHashCode() ^
            BitSequence.Merge((ushort)ProtocolType, (ushort)Operation).GetHashCode() ^
            SenderHardwareAddress.BytesSequenceGetHashCode() ^
            SenderProtocolAddress.BytesSequenceGetHashCode() ^
            TargetHardwareAddress.BytesSequenceGetHashCode() ^
            TargetProtocolAddress.BytesSequenceGetHashCode());
 }
예제 #2
0
 /// <summary>
 /// True iff the two ARP layers have equal protocol type, operation and addresses.
 /// </summary>
 /// <param name="other">The ARP layer to compare the layer to.</param>
 /// <returns>True iff the two layers are equal.</returns>
 public bool Equals(ArpLayer other)
 {
     return(other != null &&
            ProtocolType == other.ProtocolType && Operation == other.Operation &&
            SenderHardwareAddress.SequenceEqual(other.SenderHardwareAddress) &&
            SenderProtocolAddress.SequenceEqual(other.SenderProtocolAddress) &&
            TargetHardwareAddress.SequenceEqual(other.TargetHardwareAddress) &&
            TargetProtocolAddress.SequenceEqual(other.TargetProtocolAddress));
 }
예제 #3
0
        /// <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());
        }