diff options
author | JanHolger <jan@bebendorf.eu> | 2021-07-06 17:01:33 +0200 |
---|---|---|
committer | JanHolger <jan@bebendorf.eu> | 2021-07-06 17:01:33 +0200 |
commit | 1d26c0a20d3c595d2846de31c2034e27595540b1 (patch) | |
tree | d19fb1f538c0c0cd5e0db55e45e1f16230782aac /src/main/java/com/lumaserv/bgp/protocol/attribute |
Initial commit
Diffstat (limited to 'src/main/java/com/lumaserv/bgp/protocol/attribute')
14 files changed, 447 insertions, 0 deletions
diff --git a/src/main/java/com/lumaserv/bgp/protocol/attribute/AS4AggregatorAttribute.java b/src/main/java/com/lumaserv/bgp/protocol/attribute/AS4AggregatorAttribute.java new file mode 100644 index 0000000..c579210 --- /dev/null +++ b/src/main/java/com/lumaserv/bgp/protocol/attribute/AS4AggregatorAttribute.java @@ -0,0 +1,30 @@ +package com.lumaserv.bgp.protocol.attribute; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class AS4AggregatorAttribute implements PathAttribute { + + int as; + byte[] origin; + + public AS4AggregatorAttribute(byte typeCode, byte[] data) { + as = ((data[0] & 0xFF) << 24) | + ((data[1] & 0xFF) << 16) | + ((data[2] & 0xFF) << 8) | + (data[3] & 0xFF); + origin = new byte[4]; + System.arraycopy(data, 4, origin, 0, origin.length); + } + + public byte getTypeCode() { + return 18; + } + + public byte[] build() { + return new byte[0]; + } + +} diff --git a/src/main/java/com/lumaserv/bgp/protocol/attribute/AS4PathAttribute.java b/src/main/java/com/lumaserv/bgp/protocol/attribute/AS4PathAttribute.java new file mode 100644 index 0000000..415dd5f --- /dev/null +++ b/src/main/java/com/lumaserv/bgp/protocol/attribute/AS4PathAttribute.java @@ -0,0 +1,45 @@ +package com.lumaserv.bgp.protocol.attribute; + +import lombok.Getter; +import lombok.Setter; + +import java.util.ArrayList; +import java.util.List; + +public class AS4PathAttribute implements PathAttribute { + + List<Segment> segments = new ArrayList<>(); + + public AS4PathAttribute(byte typeCode, byte[] data) { + int offset = 0; + while (offset < data.length) { + Segment segment = new Segment().setType(data[offset]); + byte length = data[offset + 1]; + for(int i=0; i<length; i++) + segment.asns.add( + ((data[offset + 2 + (i * 4)] & 0xFF) << 24) | + ((data[offset + 3 + (i * 4)] & 0xFF) << 16) | + ((data[offset + 4 + (i * 4)] & 0xFF) << 8) | + (data[offset + 5 + (i * 4)] & 0xFF) + ); + offset += 2 + (length * 4); + segments.add(segment); + } + } + + public byte getTypeCode() { + return 17; + } + + public byte[] build() { + return new byte[0]; + } + + @Getter + @Setter + public static class Segment { + byte type; + List<Integer> asns = new ArrayList<>(); + } + +} diff --git a/src/main/java/com/lumaserv/bgp/protocol/attribute/ASPathAttribute.java b/src/main/java/com/lumaserv/bgp/protocol/attribute/ASPathAttribute.java new file mode 100644 index 0000000..64be45b --- /dev/null +++ b/src/main/java/com/lumaserv/bgp/protocol/attribute/ASPathAttribute.java @@ -0,0 +1,40 @@ +package com.lumaserv.bgp.protocol.attribute; + +import lombok.Getter; +import lombok.Setter; + +import java.util.ArrayList; +import java.util.List; + +public class ASPathAttribute implements PathAttribute { + + List<Segment> segments = new ArrayList<>(); + + public ASPathAttribute(byte typeCode, byte[] data) { + int offset = 0; + while (offset < data.length) { + Segment segment = new Segment().setType(data[offset]); + byte length = data[offset + 1]; + for(int i=0; i<length; i++) + segment.asns.add(((data[offset + 2 + (i * 2)] & 0xFF) << 8) | (data[offset + 3 + (i * 2)] & 0xFF)); + offset += 2 + (length * 2); + segments.add(segment); + } + } + + public byte getTypeCode() { + return 2; + } + + public byte[] build() { + return new byte[0]; + } + + @Getter + @Setter + public static class Segment { + byte type; + List<Integer> asns = new ArrayList<>(); + } + +} diff --git a/src/main/java/com/lumaserv/bgp/protocol/attribute/ASPathLimitAttribute.java b/src/main/java/com/lumaserv/bgp/protocol/attribute/ASPathLimitAttribute.java new file mode 100644 index 0000000..732b018 --- /dev/null +++ b/src/main/java/com/lumaserv/bgp/protocol/attribute/ASPathLimitAttribute.java @@ -0,0 +1,29 @@ +package com.lumaserv.bgp.protocol.attribute; + +import lombok.Getter; +import lombok.Setter; + +@Setter +@Getter +public class ASPathLimitAttribute implements PathAttribute { + + byte upperBound; + int as; + + public ASPathLimitAttribute(byte typeCode, byte[] data) { + upperBound = data[0]; + as = ((data[1] & 0xFF) << 24) | + ((data[2] & 0xFF) << 16) | + ((data[3] & 0xFF) << 8) | + (data[4] & 0xFF); + } + + public byte getTypeCode() { + return 21; + } + + public byte[] build() { + return new byte[0]; + } + +} diff --git a/src/main/java/com/lumaserv/bgp/protocol/attribute/AggregatorAttribute.java b/src/main/java/com/lumaserv/bgp/protocol/attribute/AggregatorAttribute.java new file mode 100644 index 0000000..8a3dfdb --- /dev/null +++ b/src/main/java/com/lumaserv/bgp/protocol/attribute/AggregatorAttribute.java @@ -0,0 +1,27 @@ +package com.lumaserv.bgp.protocol.attribute; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class AggregatorAttribute implements PathAttribute { + + int as; + byte[] origin; + + public AggregatorAttribute(byte typeCode, byte[] data) { + as = ((data[0] & 0xFF) << 8) | (data[1] & 0xFF); + origin = new byte[4]; + System.arraycopy(data, 2, origin, 0, origin.length); + } + + public byte getTypeCode() { + return 7; + } + + public byte[] build() { + return new byte[0]; + } + +} diff --git a/src/main/java/com/lumaserv/bgp/protocol/attribute/AtomicAggregateAttribute.java b/src/main/java/com/lumaserv/bgp/protocol/attribute/AtomicAggregateAttribute.java new file mode 100644 index 0000000..6f7e479 --- /dev/null +++ b/src/main/java/com/lumaserv/bgp/protocol/attribute/AtomicAggregateAttribute.java @@ -0,0 +1,17 @@ +package com.lumaserv.bgp.protocol.attribute; + +public class AtomicAggregateAttribute implements PathAttribute { + + public AtomicAggregateAttribute(byte typeCode, byte[] data) { + + } + + public byte getTypeCode() { + return 6; + } + + public byte[] build() { + return new byte[0]; + } + +} diff --git a/src/main/java/com/lumaserv/bgp/protocol/attribute/CommunitiesAttribute.java b/src/main/java/com/lumaserv/bgp/protocol/attribute/CommunitiesAttribute.java new file mode 100644 index 0000000..7c7f060 --- /dev/null +++ b/src/main/java/com/lumaserv/bgp/protocol/attribute/CommunitiesAttribute.java @@ -0,0 +1,41 @@ +package com.lumaserv.bgp.protocol.attribute; + +import lombok.Getter; +import lombok.Setter; + +import java.util.ArrayList; +import java.util.List; + +@Getter +@Setter +public class CommunitiesAttribute implements PathAttribute { + + List<Community> communities = new ArrayList<>(); + + public CommunitiesAttribute(byte typeCode, byte[] data) { + int offset = 0; + while (offset < data.length) { + communities.add(new Community() + .setValueA(((data[offset] & 0xFF) << 8) | (data[offset + 1] & 0xFF)) + .setValueB(((data[offset + 2] & 0xFF) << 8) | (data[offset + 3] & 0xFF)) + ); + offset += 4; + } + } + + public byte getTypeCode() { + return 8; + } + + public byte[] build() { + return new byte[0]; + } + + @Setter + @Getter + public static class Community { + int valueA; + int valueB; + } + +} diff --git a/src/main/java/com/lumaserv/bgp/protocol/attribute/DevelopmentAttribute.java b/src/main/java/com/lumaserv/bgp/protocol/attribute/DevelopmentAttribute.java new file mode 100644 index 0000000..453f934 --- /dev/null +++ b/src/main/java/com/lumaserv/bgp/protocol/attribute/DevelopmentAttribute.java @@ -0,0 +1,24 @@ +package com.lumaserv.bgp.protocol.attribute; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class DevelopmentAttribute implements PathAttribute { + + byte[] data; + + public DevelopmentAttribute(byte typeCode, byte[] data) { + this.data = data; + } + + public byte[] build() { + return data; + } + + public byte getTypeCode() { + return (byte) 255; + } + +} diff --git a/src/main/java/com/lumaserv/bgp/protocol/attribute/ExtendedCommuntiesAttribute.java b/src/main/java/com/lumaserv/bgp/protocol/attribute/ExtendedCommuntiesAttribute.java new file mode 100644 index 0000000..759ae60 --- /dev/null +++ b/src/main/java/com/lumaserv/bgp/protocol/attribute/ExtendedCommuntiesAttribute.java @@ -0,0 +1,33 @@ +package com.lumaserv.bgp.protocol.attribute; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class ExtendedCommuntiesAttribute implements PathAttribute { + + byte type; + byte subType; + int as; + int an; + + public ExtendedCommuntiesAttribute(byte typeCode, byte[] data) { + type = data[0]; + subType = data[1]; + as = ((data[2] & 0xFF) << 24) | + ((data[3] & 0xFF) << 16) | + ((data[4] & 0xFF) << 8) | + (data[5] & 0xFF); + an = ((data[6] & 0xFF) << 8) | (data[7] & 0xFF); + } + + public byte getTypeCode() { + return 16; + } + + public byte[] build() { + return new byte[0]; + } + +} diff --git a/src/main/java/com/lumaserv/bgp/protocol/attribute/LargeCommunityAttribute.java b/src/main/java/com/lumaserv/bgp/protocol/attribute/LargeCommunityAttribute.java new file mode 100644 index 0000000..921d5a5 --- /dev/null +++ b/src/main/java/com/lumaserv/bgp/protocol/attribute/LargeCommunityAttribute.java @@ -0,0 +1,56 @@ +package com.lumaserv.bgp.protocol.attribute; + +import lombok.Getter; +import lombok.Setter; + +import java.util.ArrayList; +import java.util.List; + +@Getter +@Setter +public class LargeCommunityAttribute implements PathAttribute { + + List<LargeCommunity> communities = new ArrayList<>(); + + public LargeCommunityAttribute(byte typeCode, byte[] data) { + for(int i=0; i<data.length; i+=12) { + communities.add(new LargeCommunity() + .setGlobalAdministrator( + ((data[i] & 0xFF) << 24) | + ((data[i+1] & 0xFF) << 16) | + ((data[i+2] & 0xFF) << 8) | + (data[i+3] & 0xFF) + ) + .setLocalDataPart1( + ((data[i+4] & 0xFF) << 24) | + ((data[i+5] & 0xFF) << 16) | + ((data[i+6] & 0xFF) << 8) | + (data[i+7] & 0xFF) + ) + .setLocalDataPart2( + ((data[i+8] & 0xFF) << 24) | + ((data[i+9] & 0xFF) << 16) | + ((data[i+10] & 0xFF) << 8) | + (data[i+11] & 0xFF) + ) + ); + } + } + + public byte getTypeCode() { + return 32; + } + + public byte[] build() { + return new byte[0]; + } + + @Setter + @Getter + public static class LargeCommunity { + int globalAdministrator; + int localDataPart1; + int localDataPart2; + } + +} diff --git a/src/main/java/com/lumaserv/bgp/protocol/attribute/NextHopAttribute.java b/src/main/java/com/lumaserv/bgp/protocol/attribute/NextHopAttribute.java new file mode 100644 index 0000000..f60d236 --- /dev/null +++ b/src/main/java/com/lumaserv/bgp/protocol/attribute/NextHopAttribute.java @@ -0,0 +1,20 @@ +package com.lumaserv.bgp.protocol.attribute; + +public class NextHopAttribute implements PathAttribute { + + byte[] address; + + public NextHopAttribute(byte typeCode, byte[] data) { + this.address = new byte[data.length]; + System.arraycopy(data, 0, address, 0, address.length); + } + + public byte getTypeCode() { + return 3; + } + + public byte[] build() { + return address; + } + +} diff --git a/src/main/java/com/lumaserv/bgp/protocol/attribute/OriginAttribute.java b/src/main/java/com/lumaserv/bgp/protocol/attribute/OriginAttribute.java new file mode 100644 index 0000000..2bf1208 --- /dev/null +++ b/src/main/java/com/lumaserv/bgp/protocol/attribute/OriginAttribute.java @@ -0,0 +1,24 @@ +package com.lumaserv.bgp.protocol.attribute; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class OriginAttribute implements PathAttribute { + + byte origin; + + public OriginAttribute(byte typeCode, byte[] data) { + origin = data[0]; + } + + public byte getTypeCode() { + return 1; + } + + public byte[] build() { + return new byte[0]; + } + +} diff --git a/src/main/java/com/lumaserv/bgp/protocol/attribute/PathAttribute.java b/src/main/java/com/lumaserv/bgp/protocol/attribute/PathAttribute.java new file mode 100644 index 0000000..4a10daa --- /dev/null +++ b/src/main/java/com/lumaserv/bgp/protocol/attribute/PathAttribute.java @@ -0,0 +1,38 @@ +package com.lumaserv.bgp.protocol.attribute; + +public interface PathAttribute { + + byte getTypeCode(); + byte[] build(); + + static PathAttribute from(byte typeCode, byte[] data) { + switch (typeCode & 0xFF) { + case 1: + return new OriginAttribute(typeCode, data); + case 2: + return new ASPathAttribute(typeCode, data); + case 3: + return new NextHopAttribute(typeCode, data); + case 6: + return new AtomicAggregateAttribute(typeCode, data); + case 7: + return new AggregatorAttribute(typeCode, data); + case 8: + return new CommunitiesAttribute(typeCode, data); + case 16: + return new ExtendedCommuntiesAttribute(typeCode, data); + case 17: + return new AS4PathAttribute(typeCode, data); + case 18: + return new AS4AggregatorAttribute(typeCode, data); + case 21: + return new ASPathLimitAttribute(typeCode, data); + case 32: + return new LargeCommunityAttribute(typeCode, data); + case 255: + return new DevelopmentAttribute(typeCode, data); + } + return new UnknownAttribute(typeCode, data); + } + +} diff --git a/src/main/java/com/lumaserv/bgp/protocol/attribute/UnknownAttribute.java b/src/main/java/com/lumaserv/bgp/protocol/attribute/UnknownAttribute.java new file mode 100644 index 0000000..9dbe0f9 --- /dev/null +++ b/src/main/java/com/lumaserv/bgp/protocol/attribute/UnknownAttribute.java @@ -0,0 +1,23 @@ +package com.lumaserv.bgp.protocol.attribute; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; + +@AllArgsConstructor +@Getter +@Setter +public class UnknownAttribute implements PathAttribute { + + byte typeCode; + byte[] data; + + public byte[] build() { + return data; + } + + public byte getTypeCode() { + return typeCode; + } + +} |