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

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

import lombok.Getter;
import lombok.Setter;

import java.nio.ByteBuffer;

@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();
    }
}