/// <summary> /// Create an ARPPacket from values /// </summary> /// <param name="Operation"> /// A <see cref="ARPOperation"/> /// </param> /// <param name="TargetHardwareAddress"> /// A <see cref="PhysicalAddress"/> /// </param> /// <param name="TargetProtocolAddress"> /// A <see cref="System.Net.IPAddress"/> /// </param> /// <param name="SenderHardwareAddress"> /// A <see cref="PhysicalAddress"/> /// </param> /// <param name="SenderProtocolAddress"> /// A <see cref="System.Net.IPAddress"/> /// </param> public ARPPacket(ARPOperation Operation, PhysicalAddress TargetHardwareAddress, System.Net.IPAddress TargetProtocolAddress, PhysicalAddress SenderHardwareAddress, System.Net.IPAddress SenderProtocolAddress) { log.Debug(""); // allocate memory for this packet int offset = 0; int length = ARPFields.HeaderLength; var headerBytes = new byte[length]; header = new ByteArraySegment(headerBytes, offset, length); this.Operation = Operation; this.TargetHardwareAddress = TargetHardwareAddress; this.TargetProtocolAddress = TargetProtocolAddress; this.SenderHardwareAddress = SenderHardwareAddress; this.SenderProtocolAddress = SenderProtocolAddress; // set some internal properties to fully define the packet this.HardwareAddressType = LinkLayers.Ethernet; this.HardwareAddressLength = EthernetFields.MacAddressLength; this.ProtocolAddressType = EthernetPacketType.IpV4; this.ProtocolAddressLength = IPv4Fields.AddressLength; }
/// <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; }
/// <summary> /// Инициализация ARP-пакета. /// </summary> /// <param name = "htype">Канальный уровень (тип).</param> /// <param name = "ptype">Сетевой уровень (тип).</param> /// <param name = "oper">Операция ARP.</param> /// <param name = "sha">Канальный адрес источника.</param> /// <param name = "spa">Сетевой адрес источника.</param> /// <param name = "tha">Канальный адрес назначения.</param> /// <param name = "tpa">Сетевой адрес назначения.</param> /// <exception cref = "ArgumentNullException"><paramref name = "sha" /> является <c>null</c>.</exception> /// <exception cref = "ArgumentNullException"><paramref name = "spa" /> является <c>null</c>.</exception> /// <exception cref = "ArgumentNullException"><paramref name = "tha" /> является <c>null</c>.</exception> /// <exception cref = "ArgumentNullException"><paramref name = "tpa" /> является <c>null</c>.</exception> /// <exception cref = "IncorrectFieldLengthException"><paramref name = "sha" /> имеет недопустимую длину.</exception> /// <exception cref = "IncorrectFieldLengthException"><paramref name = "spa" /> имеет недопустимую длину.</exception> /// <exception cref = "IncorrectFieldLengthException"><paramref name = "tha" /> имеет недопустимую длину.</exception> /// <exception cref = "IncorrectFieldLengthException"><paramref name = "tpa" /> имеет недопустимую длину.</exception> public ARPPacket(HardwareType htype, L3ProtocolType ptype, ARPOperation oper, IArray <byte> sha, IArray <byte> spa, IArray <byte> tha, IArray <byte> tpa) { var hlen = FieldLengthAttribute.GetLength (htype); var plen = FieldLengthAttribute.GetLength (ptype); if (sha == null) throw new ArgumentNullException ("sha"); if (spa == null) throw new ArgumentNullException ("spa"); if (tha == null) throw new ArgumentNullException ("tha"); if (tpa == null) throw new ArgumentNullException ("tpa"); if (sha.Length != hlen) throw new IncorrectFieldLengthException ("SHA", sha.Length, hlen); if (spa.Length != plen) throw new IncorrectFieldLengthException ("SPA", spa.Length, plen); if (tha.Length != hlen) throw new IncorrectFieldLengthException ("THA", sha.Length, hlen); if (tpa.Length != plen) throw new IncorrectFieldLengthException ("TPA", spa.Length, plen); this.Data = new Array <byte> (new byte[8 + sha.Length + spa.Length + tha.Length + tpa.Length]); this.HardwareType = htype; this.ProtocolType = ptype; this.HardwareLength = hlen; this.ProtocolLength = plen; this.Operation = oper; this.SenderHardwareAddress = sha; this.SenderProtocolAddress = spa; this.TargetHardwareAddress = tha; this.TargetProtocolAddress = tpa; }
public ARPPacket(ARPOperation Operation, PhysicalAddress TargetHardwareAddress, IPAddress TargetProtocolAddress, PhysicalAddress SenderHardwareAddress, IPAddress SenderProtocolAddress) : base(new PosixTimeval()) { int offset = 0; int headerLength = ARPFields.HeaderLength; byte[] bytes = new byte[headerLength]; base.header = new ByteArraySegment(bytes, offset, headerLength); this.Operation = Operation; this.TargetHardwareAddress = TargetHardwareAddress; this.TargetProtocolAddress = TargetProtocolAddress; this.SenderHardwareAddress = SenderHardwareAddress; this.SenderProtocolAddress = SenderProtocolAddress; this.HardwareAddressType = LinkLayers.Ethernet; this.HardwareAddressLength = EthernetFields.MacAddressLength; this.ProtocolAddressType = EthernetPacketType.IpV4; this.ProtocolAddressLength = IPv4Fields.AddressLength; }
/// <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); }