summaryrefslogtreecommitdiff
path: root/src/main/java/com/lumaserv/bgp/protocol/attribute/MPReachableNLRIAttribute.java
blob: 346cb0e7cbb5fde6d86dd56a9beb8969e3758e82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.lumaserv.bgp.protocol.attribute;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collection;

import lombok.Getter;
import lombok.Setter;

import com.lumaserv.bgp.protocol.AFI;
import com.lumaserv.bgp.protocol.IPPrefix;
import com.lumaserv.bgp.protocol.SAFI;

@Getter
@Setter
public class MPReachableNLRIAttribute implements PathAttribute {

    AFI afi;
    SAFI safi;
    byte[] nextHop;
    Collection<IPPrefix> nlriPrefixes;

    public MPReachableNLRIAttribute(byte typeCode, byte[] data) {
        afi = AFI.fromInteger((data[0] << 8) | data[1]);
        safi = SAFI.fromInteger(data[2]);
        int nhLen = data[3];
        nextHop = new byte[nhLen];
        System.arraycopy(data, 4, nextHop, 0, nextHop.length);
        int nlriLen = data.length - 5 - nhLen;
        nlriPrefixes = new ArrayList<>();

        int offset = 4 + nhLen + 1;
        int offsetOffset = offset;
        while ((offset - offsetOffset) < nlriLen) {
            IPPrefix prefix = new IPPrefix()
                    .setLength((int)(data[offset]&0xff)) // FIXME IPv4/IPv6 etc
                    .setAddress(new byte[16]);
            offset++;
            int addressLen = (int) Math.ceil(prefix.getLength() / 8d);
            System.arraycopy(data, offset, prefix.getAddress(), 0, addressLen);
            offset += addressLen;
            nlriPrefixes.add(prefix);
        }
    }

    public byte getTypeCode() {
        return 14;
    }

    public void build(ByteBuffer b) {
        // FIXME
    }

    public String toString() {
        return "AFI:" + afi + " SAFI:" + safi + " NLRIs:" + nlriPrefixes;
    }
}