summaryrefslogtreecommitdiff
path: root/src/main/java/com/lumaserv/bgp/protocol/attribute/NextHopAttribute.java
blob: 1bf72e4b66d1c34730c4ca97634a96468f1d9f84 (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
package com.lumaserv.bgp.protocol.attribute;

import java.net.InetAddress;
import java.net.UnknownHostException;

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

import java.nio.ByteBuffer;

@NoArgsConstructor
@Getter
@Setter
public class NextHopAttribute implements PathAttribute {

    byte[] address;
    InetAddress ip;

    public NextHopAttribute(byte typeCode, byte[] data) {
        this.address = new byte[data.length];
        System.arraycopy(data, 0, address, 0, address.length);
        try {
            ip = InetAddress.getByAddress(address);
        } catch (UnknownHostException ex) {
            // Ignore
        }
    }

    public byte getTypeCode() {
        return 3;
    }

    public void build(ByteBuffer b) {
        b.put(address);
    }

    public String toString() {
        if (ip != null)
            return ip.getHostAddress();
        else
            return address.toString();
    }
}