summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMikael Magnusson <mikma@users.sourceforge.net>2022-01-30 14:10:47 +0100
committerMikael Magnusson <mikma@users.sourceforge.net>2022-03-10 21:20:01 +0100
commit49d32fe2302542ce9aeeaf169ba48ab5449eab37 (patch)
tree195dba2d04dbb7e50f580ccd40a5e5652d974efe /src
parenta56e09ec29b081d77e3b260f5f87f626ae84d16c (diff)
Implement toString in various path attributes
Diffstat (limited to 'src')
-rw-r--r--src/main/java/com/lumaserv/bgp/protocol/attribute/ASPathAttribute.java25
-rw-r--r--src/main/java/com/lumaserv/bgp/protocol/attribute/ExtendedCommuntiesAttribute.java8
-rw-r--r--src/main/java/com/lumaserv/bgp/protocol/attribute/NextHopAttribute.java15
-rw-r--r--src/main/java/com/lumaserv/bgp/protocol/attribute/OriginAttribute.java30
-rw-r--r--src/main/java/com/lumaserv/bgp/protocol/attribute/UnknownAttribute.java3
-rw-r--r--src/main/java/com/lumaserv/bgp/protocol/message/BGPNotification.java38
6 files changed, 117 insertions, 2 deletions
diff --git a/src/main/java/com/lumaserv/bgp/protocol/attribute/ASPathAttribute.java b/src/main/java/com/lumaserv/bgp/protocol/attribute/ASPathAttribute.java
index d6f14ea..6405924 100644
--- a/src/main/java/com/lumaserv/bgp/protocol/attribute/ASPathAttribute.java
+++ b/src/main/java/com/lumaserv/bgp/protocol/attribute/ASPathAttribute.java
@@ -37,6 +37,31 @@ public class ASPathAttribute implements PathAttribute {
public static class Segment {
byte type;
List<Integer> asns = new ArrayList<>();
+
+ public String toString() {
+ StringBuffer buf = new StringBuffer();
+
+ buf.append("(");
+ for(Integer asn : asns) {
+ if (buf.length() > 1)
+ buf.append(" ");
+ buf.append(asn);
+ }
+ buf.append(")");
+ return buf.toString();
+ }
}
+ public String toString() {
+ StringBuffer buf = new StringBuffer();
+
+ buf.append("[");
+ for(Segment segment : segments) {
+ if (buf.length() > 1)
+ buf.append(", ");
+ buf.append(segment);
+ }
+ buf.append("]");
+ return buf.toString();
+ }
}
diff --git a/src/main/java/com/lumaserv/bgp/protocol/attribute/ExtendedCommuntiesAttribute.java b/src/main/java/com/lumaserv/bgp/protocol/attribute/ExtendedCommuntiesAttribute.java
index 759ae60..6909277 100644
--- a/src/main/java/com/lumaserv/bgp/protocol/attribute/ExtendedCommuntiesAttribute.java
+++ b/src/main/java/com/lumaserv/bgp/protocol/attribute/ExtendedCommuntiesAttribute.java
@@ -30,4 +30,12 @@ public class ExtendedCommuntiesAttribute implements PathAttribute {
return new byte[0];
}
+ public String toString() {
+ if (type == 0x03 && subType == 0x0b) {
+ // Color extended community
+ return String.format("color:%d", an);
+ }
+
+ return String.format("%d,%d,%d,%d", type, subType, as, an);
+ }
}
diff --git a/src/main/java/com/lumaserv/bgp/protocol/attribute/NextHopAttribute.java b/src/main/java/com/lumaserv/bgp/protocol/attribute/NextHopAttribute.java
index 14d5367..2146364 100644
--- a/src/main/java/com/lumaserv/bgp/protocol/attribute/NextHopAttribute.java
+++ b/src/main/java/com/lumaserv/bgp/protocol/attribute/NextHopAttribute.java
@@ -1,5 +1,8 @@
package com.lumaserv.bgp.protocol.attribute;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
import lombok.Getter;
import lombok.Setter;
@@ -8,10 +11,16 @@ import lombok.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() {
@@ -22,4 +31,10 @@ public class NextHopAttribute implements PathAttribute {
return address;
}
+ public String toString() {
+ if (ip != null)
+ return ip.getHostAddress();
+ else
+ return address.toString();
+ }
}
diff --git a/src/main/java/com/lumaserv/bgp/protocol/attribute/OriginAttribute.java b/src/main/java/com/lumaserv/bgp/protocol/attribute/OriginAttribute.java
index 2bf1208..9827bae 100644
--- a/src/main/java/com/lumaserv/bgp/protocol/attribute/OriginAttribute.java
+++ b/src/main/java/com/lumaserv/bgp/protocol/attribute/OriginAttribute.java
@@ -3,14 +3,37 @@ package com.lumaserv.bgp.protocol.attribute;
import lombok.Getter;
import lombok.Setter;
+import java.util.EnumSet;
+
@Getter
@Setter
public class OriginAttribute implements PathAttribute {
- byte origin;
+ @Getter
+ enum Origin {
+ IGP(0),
+ EGP(1),
+ INCOMPLETE(2);
+
+ byte code;
+
+ Origin(int code) {
+ this.code = (byte)code;
+ }
+
+ public static Origin fromByte(byte code) {
+ return EnumSet.allOf(Origin.class)
+ .stream()
+ .filter(e -> e.getCode() == code)
+ .findAny()
+ .orElseThrow(() -> new IllegalArgumentException("unknown origin: " + code));
+ }
+ }
+
+ Origin origin;
public OriginAttribute(byte typeCode, byte[] data) {
- origin = data[0];
+ origin = Origin.fromByte(data[0]);
}
public byte getTypeCode() {
@@ -21,4 +44,7 @@ public class OriginAttribute implements PathAttribute {
return new byte[0];
}
+ public String toString() {
+ return origin.toString();
+ }
}
diff --git a/src/main/java/com/lumaserv/bgp/protocol/attribute/UnknownAttribute.java b/src/main/java/com/lumaserv/bgp/protocol/attribute/UnknownAttribute.java
index 9dbe0f9..69dae2d 100644
--- a/src/main/java/com/lumaserv/bgp/protocol/attribute/UnknownAttribute.java
+++ b/src/main/java/com/lumaserv/bgp/protocol/attribute/UnknownAttribute.java
@@ -20,4 +20,7 @@ public class UnknownAttribute implements PathAttribute {
return typeCode;
}
+ public String toString() {
+ return "Unknown:" + typeCode;
+ }
}
diff --git a/src/main/java/com/lumaserv/bgp/protocol/message/BGPNotification.java b/src/main/java/com/lumaserv/bgp/protocol/message/BGPNotification.java
index 54ee9bd..19272e5 100644
--- a/src/main/java/com/lumaserv/bgp/protocol/message/BGPNotification.java
+++ b/src/main/java/com/lumaserv/bgp/protocol/message/BGPNotification.java
@@ -1,5 +1,6 @@
package com.lumaserv.bgp.protocol.message;
+import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
@@ -10,9 +11,46 @@ public class BGPNotification {
byte majorErrorCode;
byte minorErrorCode;
+ /** Major error codes */
+ @Getter
+ public enum Error {
+ MESSAGE_HEADER_ERROR(1),
+ OPEN_MESSAGE_ERROR(2),
+ UPDATE_MESSAGE_ERROR(3),
+ HOLD_TIMER_ERROR(4),
+ FSM_ERROR(5),
+ CEASE(6);
+
+ byte code;
+
+ Error(int code) {
+ this.code = (byte)code;
+ }
+ }
+
+ /** Minor error codes for OPEN_MESSAGE_ERROR */
+ @Getter
+ public enum OpenMessageError {
+ UNSUPPORTED_VERSION_NUMBER(1),
+ BAD_PEER_AS(2),
+ BAD_BGP_IDENTIFIER(3),
+ UNSUPPORTED_OPTION_PARAMETER(4),
+ UNACCEPTABLE_HOLD_TIME(5);
+
+ byte code;
+
+ OpenMessageError(int code) {
+ this.code = (byte)code;
+ }
+ }
+
public BGPNotification(byte[] message) {
this.majorErrorCode = message[0];
this.minorErrorCode = message[1];
}
+ @Override
+ public String toString() {
+ return "" + majorErrorCode + ":" + minorErrorCode;
+ }
}