/* This file is part of PacketDotNet PacketDotNet is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. PacketDotNet is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with PacketDotNet. If not, see . */ /* * Copyright 2012 Alan Rushforth */ using System; using System.Collections.Generic; using System.Linq; using System.Text; using PacketDotNet.Utils; using MiscUtil.Conversion; using System.Net.NetworkInformation; namespace PacketDotNet { namespace Ieee80211 { /// /// Data frame. /// public abstract class DataFrame : MacFrame { /// /// SourceAddress /// public PhysicalAddress SourceAddress { get; set; } /// /// DestinationAddress /// public PhysicalAddress DestinationAddress { get; set; } /// /// ReceiverAddress /// public PhysicalAddress ReceiverAddress { get; set; } /// /// TransmitterAddress /// public PhysicalAddress TransmitterAddress { get; set; } /// /// BssID /// public PhysicalAddress BssId { get; set; } /// /// Assigns the default MAC address of 00-00-00-00-00-00 to all address fields. /// protected void AssignDefaultAddresses () { PhysicalAddress zeroAddress = PhysicalAddress.Parse ("000000000000"); SourceAddress = zeroAddress; DestinationAddress = zeroAddress; TransmitterAddress = zeroAddress; ReceiverAddress = zeroAddress; BssId = zeroAddress; } /// /// Reads the addresses from the backing ByteArraySegment into the the address properties. /// /// /// The ToDS and FromDS properties dictate /// which of the 4 possible address fields is read into which address property. /// protected void ReadAddresses () { if ((!FrameControl.ToDS) && (!FrameControl.FromDS)) { DestinationAddress = GetAddress (0); SourceAddress = GetAddress (1); BssId = GetAddress (2); } else if ((FrameControl.ToDS) && (!FrameControl.FromDS)) { BssId = GetAddress (0); SourceAddress = GetAddress (1); DestinationAddress = GetAddress (2); } else if ((!FrameControl.ToDS) && (FrameControl.FromDS)) { DestinationAddress = GetAddress (0); BssId = GetAddress (1); SourceAddress = GetAddress (2); } else { //both are true so we are in WDS mode again. BSSID is not valid in this mode ReceiverAddress = GetAddress (0); TransmitterAddress = GetAddress (1); DestinationAddress = GetAddress (2); SourceAddress = GetAddress (3); } } /// /// Writes the address properties into the backing . /// /// /// The address position into which a particular address property is written is determined by the /// value of ToDS and FromDS properties. /// protected void WriteAddressBytes () { if ((!FrameControl.ToDS) && (!FrameControl.FromDS)) { SetAddress (0, DestinationAddress); SetAddress (1, SourceAddress); SetAddress (2, BssId); } else if ((FrameControl.ToDS) && (!FrameControl.FromDS)) { SetAddress (0, BssId); SetAddress (1, SourceAddress); SetAddress (2, DestinationAddress); } else if ((!FrameControl.ToDS) && (FrameControl.FromDS)) { SetAddress (0, DestinationAddress); SetAddress (1, BssId); SetAddress (2, SourceAddress); } else { SetAddress (0, ReceiverAddress); SetAddress (1, TransmitterAddress); SetAddress (2, DestinationAddress); SetAddress (3, SourceAddress); } } /// /// Frame control bytes are the first two bytes of the frame /// protected UInt16 SequenceControlBytes { get { if(header.Length >= (MacFields.SequenceControlPosition + MacFields.SequenceControlLength)) { return EndianBitConverter.Little.ToUInt16 (header.Bytes, (header.Offset + MacFields.Address1Position + (MacFields.AddressLength * 3))); } else { return 0; } } set { EndianBitConverter.Little.CopyBytes (value, header.Bytes, (header.Offset + MacFields.Address1Position + (MacFields.AddressLength * 3))); } } /// /// Sequence control field /// public SequenceControlField SequenceControl { get; set; } /// /// Returns a string with a description of the addresses used in the packet. /// This is used as a compoent of the string returned by ToString(). /// /// /// The address string. /// protected override String GetAddressString() { String addresses = null; if (FrameControl.ToDS && FrameControl.FromDS) { addresses = String.Format("SA {0} DA {1} TA {2} RA {3}", SourceAddress, DestinationAddress, TransmitterAddress, ReceiverAddress); } else { addresses = String.Format("SA {0} DA {1} BSSID {2}", SourceAddress, DestinationAddress, BssId); } return addresses; } } } }