summaryrefslogtreecommitdiff
path: root/src/main/java/com/lumaserv/bgp/protocol/BGPPacket.java
blob: e0aa3d69a3961e9f695a54106e3010ff9b95dbb3 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.lumaserv.bgp.protocol;

import com.lumaserv.bgp.protocol.DataBuilder;
import com.lumaserv.bgp.protocol.message.BGPOpen;
import com.lumaserv.bgp.protocol.message.BGPUpdate;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;

@Setter
@Getter
@NoArgsConstructor
public class BGPPacket {

    Type type;
    DataBuilder message;

    public BGPPacket(ByteBuffer packet) {
        type = Type.fromValue(packet.get());
        switch (type) {
            case OPEN:
                message = new BGPOpen(packet);
                break;
            case UPDATE:
                message = new BGPUpdate(packet);
                break;
            default:
                System.out.println("type: " + type);
                break;
        }
    }

    public byte[] build() {
        ByteBuffer buf = ByteBuffer.allocate(4096).order(ByteOrder.BIG_ENDIAN); // Maximum BGP message size
        for (int i=0; i<16; i++)
            buf.put((byte) 0xFF);
        buf.putShort((short)19); // Length without message payload
        buf.put(type.getValue());
        if (message != null) {
            message.build(buf);
            buf.putShort(16, (short)buf.position()); // Set length
        }
        byte[] packet = new byte[buf.position()];
        buf.position(0);
        buf.get(packet); // TODO is working?
        return packet;
    }

    public static BGPPacket read(InputStream stream) throws IOException {
        ByteBuffer buf = ByteBuffer.allocate(4096).order(ByteOrder.BIG_ENDIAN);
        int value;
        for(int i=0; i<18; i++) {
            value = stream.read();
            if(value == -1)
                throw new IOException("Unexpected end of stream");
            buf.put((byte) value);
        }
        int length = buf.getShort(16);
        for(int i=18; i<length; i++) {
            value = stream.read();
            if(value == -1)
                throw new IOException("Unexpected end of stream");
            buf.put((byte) value);
        }
        buf.limit(buf.position());
        buf.position(18);
        return new BGPPacket(buf);
    }

    @AllArgsConstructor
    @Getter
    public enum Type {
        OPEN((byte) 1),
        UPDATE((byte) 2),
        NOTIFICATION((byte) 3),
        KEEPALIVE((byte) 4),
        ROUTE_REFRESH((byte) 5);
        final byte value;
        public static Type fromValue(byte value) {
            for(Type t : values()) {
                if(t.value == value)
                    return t;
            }
            return null;
        }
    }

}