public override Frame Parse(Frame fFrame) { IPv6Frame ipFrame = new IPv6Frame(fFrame.FrameBytes); //Automatically parse IPv6 headers Frame fLastFrame = ipFrame; while (fLastFrame.FrameType != RawDataFrame.DefaultFrameType) { byte[] bPayload = fLastFrame.EncapsulatedFrame.FrameBytes; switch (((IIPHeader)fLastFrame).Protocol) { case IPProtocol.IPv6_Frag: fLastFrame.EncapsulatedFrame = new FragmentExtensionHeader(bPayload); break; case IPProtocol.IPv6_Route: fLastFrame.EncapsulatedFrame = new RoutingExtensionHeader(bPayload); break; default: fLastFrame.EncapsulatedFrame = new RawDataFrame(bPayload); break; } fLastFrame = fLastFrame.EncapsulatedFrame; } return(ipFrame); }
/// <summary> /// Returns the IP pseudo header for the given frame. /// </summary> /// <param name="fFrame">The frame to calculate the pseudo-header for.</param> /// <returns>The pseudo header of the given frame.</returns> public byte[] GetPseudoHeader(Frame fFrame) { IPFrame ipFrame; if (iIPVersion == 4) { ipFrame = new IPv4Frame(); } else if (iIPVersion == 6) { ipFrame = new IPv6Frame(); } else { throw new ArgumentException("Only IPv4 or IPv6 addresses are supportet on the moment."); } ipFrame.DestinationAddress = RemoteBinding; ipFrame.SourceAddress = LocalBinding; ipFrame.Protocol = ProtocolBinding; ipFrame.EncapsulatedFrame = fFrame; return(ipFrame.GetPseudoHeader()); }
private void SendICMPNeighborSolicitation(IPAddress ipaQuery, IPAddress ipaSource) { if (ipaQuery.AddressFamily != System.Net.Sockets.AddressFamily.InterNetworkV6) { throw new InvalidOperationException("Cannot send an ICMP Neighbor Solicitation for a non IPv6 address."); } ICMPv6Frame icmpFrame = new ICMPv6Frame(); icmpFrame.ICMPv6Type = ICMPv6Type.NeighborSolicitation; icmpFrame.ICMPCode = 0; NeighborSolicitation icmpDiscoveryMessage = new NeighborSolicitation(); icmpDiscoveryMessage.TargetAddress = ipaQuery; NeighborDiscoveryOption icmpDiscoveryOption = new NeighborDiscoveryOption(); icmpDiscoveryOption.OptionType = ICMP.V6.NeighborDiscoveryOptionType.SourceLinkLayerAddress; icmpDiscoveryOption.OptionData = this.PrimaryMACAddress.AddressBytes; IPv6Frame ipFrame = new IPv6Frame(); ipFrame.SourceAddress = ipaSource; ipFrame.DestinationAddress = IPAddressAnalysis.GetSolicitedNodeMulticastAddress(ipaQuery); ipFrame.NextHeader = IPProtocol.IPv6_ICMP; EthernetFrame ethFrame = new EthernetFrame(); ethFrame.Destination = MACAddress.MulticastFromIPv6Address(ipaQuery); ethFrame.Source = PrimaryMACAddress; ethFrame.EtherType = EtherType.IPv6; ethFrame.EncapsulatedFrame = ipFrame; ipFrame.EncapsulatedFrame = icmpFrame; icmpFrame.EncapsulatedFrame = icmpDiscoveryMessage; icmpDiscoveryMessage.EncapsulatedFrame = icmpDiscoveryOption; icmpFrame.Checksum = icmpFrame.CalculateChecksum(ipFrame.GetPseudoHeader()); this.Send(ethFrame); }
/// <summary> /// Encapsulates the given IP frame according to the binding of this socket and invokes the FrameEncapsulated event when finished. /// <remarks>This method also handles IP fragmentation</remarks> /// </summary> /// <param name="fFrame">The frame to process</param> /// <param name="bPush">A bool indicating whether the frame is delivered with a push flag</param> public override void PushDown(Frame fFrame, bool bPush) { if (iIPVersion == 4) { IPv4Frame ipv4Frame = new IPv4Frame(); ipv4Frame.DestinationAddress = RemoteBinding; ipv4Frame.SourceAddress = LocalBinding; ipv4Frame.Protocol = ProtocolBinding; ipv4Frame.Identification = (uint)rRandom.Next(Int32.MaxValue); ipv4Frame.EncapsulatedFrame = fFrame; foreach (IPFrame fFragment in IPFragmenter.FragmentV4(ipv4Frame, MaximumTransmissionUnit)) { InvokeFrameEncapsulated(fFragment, bPush); } } else if (iIPVersion == 6) { IPv6Frame ipv6Frame = new IPv6Frame(); ipv6Frame.DestinationAddress = RemoteBinding; ipv6Frame.SourceAddress = LocalBinding; ipv6Frame.Protocol = ProtocolBinding; ipv6Frame.EncapsulatedFrame = fFrame; foreach (IPFrame fFragment in IPFragmenter.FragmentV6(ipv6Frame, MaximumTransmissionUnit)) { InvokeFrameEncapsulated(fFragment, bPush); } } else { throw new ArgumentException("Only IPv4 and IPv6 addresses are supportet on the moment."); } }
public static IPv6Frame[] FragmentV6(IPv6Frame ipv6Frame, int iMaximumTransmissionUnit, uint iIdentification) { Frame fFrame = ipv6Frame.EncapsulatedFrame; List <IPv6Frame> lIPv6Frames = new List <IPv6Frame>(); if (ipv6Frame.Length > iMaximumTransmissionUnit) { ipv6Frame.EncapsulatedFrame = null; byte[][] bChunks = CreateChunks(fFrame.FrameBytes, iMaximumTransmissionUnit - ipv6Frame.Length); int iDataCounter = 0; for (int iC1 = 0; iC1 < bChunks.Length; iC1++) { IPv6Frame ipv6Clone = (IPv6Frame)ipv6Frame.Clone(); FragmentExtensionHeader fragHeader = new FragmentExtensionHeader(); fragHeader.EncapsulatedFrame = new RawDataFrame(bChunks[iC1]); ipv6Clone.EncapsulatedFrame = fragHeader; fragHeader.FragmentOffset = ((iDataCounter) / 8); fragHeader.MoreFragments = iC1 != bChunks.Length - 1; fragHeader.Identification = iIdentification; fragHeader.Protocol = ipv6Clone.Protocol; ipv6Clone.Protocol = IPProtocol.IPv6_Frag; iDataCounter += bChunks[iC1].Length; lIPv6Frames.Add(ipv6Clone); } } else { lIPv6Frames.Add(ipv6Frame); } return(lIPv6Frames.ToArray()); }
private void SendICMPNeighborAdvertisment(IPAddress ipaDestination, IPAddress ipaSource) { if (ipaDestination.AddressFamily != System.Net.Sockets.AddressFamily.InterNetworkV6) { throw new InvalidOperationException("Cannot send an ICMP Neighbor Advertisement for a non IPv6 address."); } ICMPv6Frame icmpFrame = new ICMPv6Frame(); icmpFrame.ICMPv6Type = ICMPv6Type.NeighborAdvertisement; icmpFrame.ICMPCode = 0; NeighborAdvertisment icmpDiscoveryMessage = new NeighborAdvertisment(); icmpDiscoveryMessage.SolicitedFlag = true; icmpDiscoveryMessage.OverrideFlag = true; icmpDiscoveryMessage.TargetAddress = ipaSource; NeighborDiscoveryOption icmpDiscoveryOption = new NeighborDiscoveryOption(); icmpDiscoveryOption.OptionType = ICMP.V6.NeighborDiscoveryOptionType.TargetLinkLayerAddress; icmpDiscoveryOption.OptionData = this.PrimaryMACAddress.AddressBytes; IPv6Frame ipFrame = new IPv6Frame(); ipFrame.SourceAddress = ipaSource; ipFrame.DestinationAddress = ipaDestination; ipFrame.NextHeader = IPProtocol.IPv6_ICMP; ipFrame.EncapsulatedFrame = icmpFrame; icmpFrame.EncapsulatedFrame = icmpDiscoveryMessage; icmpDiscoveryMessage.EncapsulatedFrame = icmpDiscoveryOption; icmpFrame.Checksum = icmpFrame.CalculateChecksum(ipFrame.GetPseudoHeader()); this.Send(ipFrame, ipaDestination); }
public static IPv6Frame[] FragmentV6(IPv6Frame ipv6Frame, int iMaximumTransmissionUnit) { return(FragmentV6(ipv6Frame, iMaximumTransmissionUnit, (uint)(new Random().Next(Int32.MaxValue)))); }