internal static void WriteHeader(byte[] buffer, int offset, byte trafficClass, int flowLabel, ushort payloadLength, IpV4Protocol?nextHeader, IpV4Protocol?nextLayerProtocol, byte hopLimit, IpV6Address source, IpV6Address currentDestination, IpV6ExtensionHeaders extensionHeaders) { buffer.Write(offset + Offset.Version, (uint)((((DefaultVersion << 8) | trafficClass) << 20) | flowLabel), Endianity.Big); buffer.Write(offset + Offset.PayloadLength, payloadLength, Endianity.Big); IpV4Protocol actualNextHeader; if (nextHeader.HasValue) { actualNextHeader = nextHeader.Value; } else if (extensionHeaders.Any()) { actualNextHeader = extensionHeaders.FirstHeader.Value; } else if (nextLayerProtocol.HasValue) { actualNextHeader = nextLayerProtocol.Value; } else { throw new InvalidOperationException("Can't determine next header. No extension headers and no known next layer protocol."); } buffer.Write(offset + Offset.NextHeader, (byte)actualNextHeader); buffer.Write(offset + Offset.HopLimit, hopLimit); buffer.Write(offset + Offset.SourceAddress, source, Endianity.Big); buffer.Write(offset + Offset.DestinationAddress, currentDestination, Endianity.Big); extensionHeaders.Write(buffer, offset + HeaderLength, nextLayerProtocol); }
/// <summary> /// Two addresses are equal if the have the exact same value. /// </summary> public bool Equals(IpV6Address other) { return(_value == other._value); }
private static ushort CalculateTransportChecksum(byte[] buffer, int offset, int fullHeaderLength, uint transportLength, int transportChecksumOffset, bool isChecksumOptional, IpV6Address destination) { int offsetAfterChecksum = offset + fullHeaderLength + transportChecksumOffset + sizeof(ushort); uint sum = Sum16Bits(buffer, offset + Offset.SourceAddress, IpV6Address.SizeOf) + Sum16Bits(destination) + Sum16Bits(transportLength) + buffer[offset + Offset.NextHeader] + Sum16Bits(buffer, offset + fullHeaderLength, transportChecksumOffset) + Sum16Bits(buffer, offsetAfterChecksum, (int)(transportLength - transportChecksumOffset - sizeof(ushort))); ushort checksumResult = Sum16BitsToChecksum(sum); if (checksumResult == 0 && isChecksumOptional) { return(0xFFFF); } return(checksumResult); }
internal static void WriteTransportChecksum(byte[] buffer, int offset, int headerLength, uint transportLength, int transportChecksumOffset, bool isChecksumOptional, ushort?checksum, IpV6Address destination) { ushort checksumValue = checksum ?? CalculateTransportChecksum(buffer, offset, headerLength, transportLength, transportChecksumOffset, isChecksumOptional, destination); buffer.Write(offset + headerLength + transportChecksumOffset, checksumValue, Endianity.Big); }