summaryrefslogtreecommitdiff
path: root/src/main/java/com/lumaserv/bgp/protocol/message/BGPUpdate.java
blob: fa234326d5c83c1cdfba4b50e14f085f0ae45a64 (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
60
61
62
63
package com.lumaserv.bgp.protocol.message;

import com.lumaserv.bgp.protocol.IPPrefix;
import com.lumaserv.bgp.protocol.attribute.PathAttribute;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;

@Getter
@Setter
public class BGPUpdate {

    List<IPPrefix> withdrawnPrefixes = new ArrayList<>();
    List<PathAttribute> attributes = new ArrayList<>();
    List<IPPrefix> prefixes = new ArrayList<>();

    public BGPUpdate(byte[] message) {
        int routesLength = ((message[0] & 0xFF) << 8) | (message[1] & 0xFF);
        int offset = 2;
        int offsetOffset = 2;
        while ((offset - offsetOffset) < routesLength) {
            IPPrefix prefix = new IPPrefix()
                    .setLength(message[offset])
                    .setAddress(new byte[4]);
            offset++;
            int addressLen = (int) Math.ceil(prefix.getLength() / 8d);
            System.arraycopy(message, offset, prefix.getAddress(), 0, addressLen);
            offset += addressLen;
            withdrawnPrefixes.add(prefix);
        }
        int attributesLength = ((message[offset] & 0xFF) << 8) | (message[offset + 1] & 0xFF);
        offset += 2;
        offsetOffset = offset;
        while ((offset - offsetOffset) < attributesLength) {
            byte flags = message[offset];
            byte typeCode = message[offset + 1];
            int length = message[offset + 2] & 0xFF;
            offset += 3;
            if((flags & 0b0001_0000) > 0) {
                length <<= 8;
                length |= message[offset] & 0xFF;
                offset++;
            }
            byte[] data = new byte[length];
            System.arraycopy(message, offset, data, 0, length);
            offset += length;
            attributes.add(PathAttribute.from(typeCode, data));
        }
        while (offset < message.length) {
            IPPrefix prefix = new IPPrefix()
                    .setLength(message[offset])
                    .setAddress(new byte[4]);
            offset++;
            int addressLen = (int) Math.ceil(prefix.getLength() / 8d);
            System.arraycopy(message, offset, prefix.getAddress(), 0, addressLen);
            offset += addressLen;
            prefixes.add(prefix);
        }
    }

}