<commit_before>﻿using System;
using System.Collections.Generic;
using System.Net;

namespace BmpListener.Bgp
{
    public class PathAttributeMPReachNLRI : PathAttribute
    {
        public PathAttributeMPReachNLRI(ArraySegment<byte> data) : base(data)
        {
            NLRI = new List<IPAddrPrefix>();
            Decode(AttributeValue);
        }

        public AddressFamily AFI { get; private set; }
        public SubsequentAddressFamily SAFI { get; private set; }
        public IPAddress NextHop { get; private set; }
        public IPAddress LinkLocalNextHop { get; private set; }
        public IList<IPAddrPrefix> NLRI { get; }
        
        protected void Decode(ArraySegment<byte> data)
        {
            var offset = data.Offset;
            Array.Reverse(data.Array, offset, 2);
            AFI = (AddressFamily)BitConverter.ToInt16(data.Array, offset);
            SAFI = (SubsequentAddressFamily)data.Array[offset + 2];
            var nextHopLength = data.Array[offset + 3];
            offset += 3;

            if (nextHopLength > 0)
            {
                var nextHopBytes = new byte[16];
                Array.Copy(data.Array, offset, nextHopBytes, 0, nextHopLength);
                NextHop = new IPAddress(nextHopBytes);

                // RFC 2545 - The value of the Length of Next Hop Network 
                // Address field on a MP_REACH_NLRI attribute shall be set to
                // 16, when only a global address is present, or 32 if a
                // link - local address is also included in the Next Hop field.

                if (nextHopLength == 32)
                {
                    var linklocalBytes = new byte[16];
                    Array.Copy(data.Array, 16, linklocalBytes, 0, 16);
                    LinkLocalNextHop = new IPAddress(linklocalBytes);
                    offset += 16;
                }
            }<commit_msg>Fix offset bug<commit_after>﻿using System;
using System.Collections.Generic;
using System.Net;

namespace BmpListener.Bgp
{
    public class PathAttributeMPReachNLRI : PathAttribute
    {
        public PathAttributeMPReachNLRI(ArraySegment<byte> data) : base(data)
        {
            NLRI = new List<IPAddrPrefix>();
            Decode(AttributeValue);
        }

        public AddressFamily AFI { get; private set; }
        public SubsequentAddressFamily SAFI { get; private set; }
        public IPAddress NextHop { get; private set; }
        public IPAddress LinkLocalNextHop { get; private set; }
        public IList<IPAddrPrefix> NLRI { get; }

        protected void Decode(ArraySegment<byte> data)
        {
            var offset = data.Offset;
            Array.Reverse(data.Array, offset, 2);
            AFI = (AddressFamily)BitConverter.ToInt16(data.Array, offset);
            SAFI = (SubsequentAddressFamily)data.Array[offset + 2];
            var nextHopLength = data.Array[offset + 3];
            offset += 4;

            if (nextHopLength > 0)
            {
                var nextHopBytes = new byte[16];
                Array.Copy(data.Array, offset, nextHopBytes, 0, nextHopLength);
                NextHop = new IPAddress(nextHopBytes);

                // RFC 2545 - The value of the Length of Next Hop Network 
                // Address field on a MP_REACH_NLRI attribute shall be set to
                // 16, when only a global address is present, or 32 if a
                // link - local address is also included in the Next Hop field.

                if (nextHopLength == 32)
                {
                    var linklocalBytes = new byte[16];
                    Array.Copy(data.Array, 16, linklocalBytes, 0, 16);
                    LinkLocalNextHop = new IPAddress(linklocalBytes);
                    offset += 16;
                }
            }
{'fraction_non_alphanumeric': 0.081557484872402, 'fraction_numerical': 0.015259142330965536, 'mean_word_length': 1.773158278628738, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6708817', 'n_tokens_mistral': 1088, 'n_tokens_neox': 963, 'n_words': 294}