diff options
author | Mikael Magnusson <mikma@users.sourceforge.net> | 2023-03-05 22:47:36 +0100 |
---|---|---|
committer | Mikael Magnusson <mikma@users.sourceforge.net> | 2023-03-05 22:47:36 +0100 |
commit | ee8401cdc36337c8f8ffac36c5ca8277af6daba9 (patch) | |
tree | 33d546861f21b360ac8ba07090e91a1558502e13 | |
parent | 0da29d475cbe2ed0c854103d788df4472b3a75b4 (diff) |
WIP: uncommitted changes
8 files changed, 60 insertions, 5 deletions
diff --git a/src/main/java/com/lumaserv/bgp/BGPFsm.java b/src/main/java/com/lumaserv/bgp/BGPFsm.java index 4fa35b3..4d7aceb 100644 --- a/src/main/java/com/lumaserv/bgp/BGPFsm.java +++ b/src/main/java/com/lumaserv/bgp/BGPFsm.java @@ -363,7 +363,7 @@ public class BGPFsm { setHoldTimer(holdTime); setState(OPEN_CONFIRM); } else { - System.out.println("Bad asn:" + openAsn); + System.out.println("Bad asn:" + (openAsn & 0xFFFFFFFFL)); try { BGPNotification notification = new BGPNotification() .setMajorErrorCode(BGPNotification.Error.OPEN_MESSAGE_ERROR.getCode()) @@ -547,7 +547,7 @@ public class BGPFsm { System.out.println("Finalize BGPFsm: " + this); } - private void setKeepAliveTimer() { + void setKeepAliveTimer() { // System.out.println("setKeepAliveTimer: " + holdTime / 3); if (keepAliveTimerTask != null) { diff --git a/src/main/java/com/lumaserv/bgp/BGPListener.java b/src/main/java/com/lumaserv/bgp/BGPListener.java index 54f4fe5..76cd035 100644 --- a/src/main/java/com/lumaserv/bgp/BGPListener.java +++ b/src/main/java/com/lumaserv/bgp/BGPListener.java @@ -7,5 +7,4 @@ public interface BGPListener { void onOpen(BGPSession session); void onUpdate(BGPSession session, BGPUpdate update); void onClose(BGPSession session); - } diff --git a/src/main/java/com/lumaserv/bgp/BGPSession.java b/src/main/java/com/lumaserv/bgp/BGPSession.java index f78467b..a070596 100644 --- a/src/main/java/com/lumaserv/bgp/BGPSession.java +++ b/src/main/java/com/lumaserv/bgp/BGPSession.java @@ -152,7 +152,7 @@ public class BGPSession implements Runnable { System.out.println("Sent open"); } - public void sendOpen() throws IOException { + void sendOpen() throws IOException { BGPOpen.Capabilities caps = new BGPOpen.Capabilities(); caps.getCapabilities().add(new BGPOpen.MultiprotocolExtensionCapability(AFI.IPV4.getValue(), SAFI.UNICAST.getValue())); caps.getCapabilities().add(new BGPOpen.MultiprotocolExtensionCapability(AFI.IPV6.getValue(), SAFI.UNICAST.getValue())); @@ -169,6 +169,14 @@ System.out.println("Sent open"); sendOpen(request); } + public void sendUpdate(BGPUpdate request) throws IOException { + if (fsm.getCurrentState() != fsm.ESTABLISHED) { + throw new RuntimeException("Can't send UPDATE in state " + fsm.getCurrentState()); + } + outputStream.write(new BGPPacket().setType(BGPPacket.Type.UPDATE).setMessage(request.build()).build()); + fsm.setKeepAliveTimer(); + } + public void retryConnection() { if (!socket.isConnected()) { try { diff --git a/src/main/java/com/lumaserv/bgp/protocol/IPPrefix.java b/src/main/java/com/lumaserv/bgp/protocol/IPPrefix.java index 57cfe90..0035ec0 100644 --- a/src/main/java/com/lumaserv/bgp/protocol/IPPrefix.java +++ b/src/main/java/com/lumaserv/bgp/protocol/IPPrefix.java @@ -1,5 +1,6 @@ package com.lumaserv.bgp.protocol; +import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; @@ -7,6 +8,7 @@ import lombok.Setter; import java.net.InetAddress; import java.net.UnknownHostException; +@AllArgsConstructor @NoArgsConstructor @Setter @Getter 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 c4c6962..ed95b2a 100644 --- a/src/main/java/com/lumaserv/bgp/protocol/attribute/ASPathAttribute.java +++ b/src/main/java/com/lumaserv/bgp/protocol/attribute/ASPathAttribute.java @@ -2,6 +2,7 @@ package com.lumaserv.bgp.protocol.attribute; import com.lumaserv.bgp.BGPSession; +import lombok.NoArgsConstructor; import lombok.Getter; import lombok.Setter; @@ -9,6 +10,7 @@ import java.util.ArrayList; import java.util.List; @Getter +@NoArgsConstructor @Setter public class ASPathAttribute implements PathAttribute { 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 2146364..98cda4b 100644 --- a/src/main/java/com/lumaserv/bgp/protocol/attribute/NextHopAttribute.java +++ b/src/main/java/com/lumaserv/bgp/protocol/attribute/NextHopAttribute.java @@ -4,9 +4,11 @@ import java.net.InetAddress; import java.net.UnknownHostException; import lombok.Getter; +import lombok.NoArgsConstructor; import lombok.Setter; @Getter +@NoArgsConstructor @Setter public class NextHopAttribute implements PathAttribute { 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 9827bae..dcb17fb 100644 --- a/src/main/java/com/lumaserv/bgp/protocol/attribute/OriginAttribute.java +++ b/src/main/java/com/lumaserv/bgp/protocol/attribute/OriginAttribute.java @@ -1,16 +1,18 @@ package com.lumaserv.bgp.protocol.attribute; +import lombok.AllArgsConstructor; import lombok.Getter; import lombok.Setter; import java.util.EnumSet; +@AllArgsConstructor @Getter @Setter public class OriginAttribute implements PathAttribute { @Getter - enum Origin { + public enum Origin { IGP(0), EGP(1), INCOMPLETE(2); diff --git a/src/main/java/com/lumaserv/bgp/protocol/message/BGPUpdate.java b/src/main/java/com/lumaserv/bgp/protocol/message/BGPUpdate.java index f5af399..8621118 100644 --- a/src/main/java/com/lumaserv/bgp/protocol/message/BGPUpdate.java +++ b/src/main/java/com/lumaserv/bgp/protocol/message/BGPUpdate.java @@ -4,12 +4,15 @@ import com.lumaserv.bgp.BGPSession; import com.lumaserv.bgp.protocol.IPPrefix; import com.lumaserv.bgp.protocol.attribute.PathAttribute; import lombok.Getter; +import lombok.NoArgsConstructor; import lombok.Setter; +import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.List; @Getter +@NoArgsConstructor @Setter public class BGPUpdate { @@ -61,4 +64,41 @@ public class BGPUpdate { } } + public byte[] build() { + ByteBuffer buf = ByteBuffer.allocate(256); + build(buf); + byte[] message = new byte[buf.position()]; + buf.rewind(); + buf.get(message); + return message; + } + + public void build(ByteBuffer message) { + int first = message.position(); + message.putShort((short)0); // Withdrawn + message.putShort((short)0); // Attributes + // TODO + int pos = message.position(); + for (PathAttribute attr: attributes) { +// FIXME add header + byte flags = 0b0100_0000; // TODO + int start = message.position(); + message.put(flags); + message.put(attr.getTypeCode()); + message.put((byte)0); // Dummy length + byte[] data = attr.build(); + message.put(data); + int length = message.position() - (start + 3); + if (length > 255) + throw new RuntimeException("attribute length > 255"); + message.put(start + 2, (byte)(length & 0xFF)); + } + int attributesLength = message.position() - pos; + message.putShort(first + 2, (short)attributesLength); + for (IPPrefix prefix : prefixes) { + message.put((byte)prefix.getLength()); + int addressLen = (int) Math.ceil(prefix.getLength() / 8d); + message.put(prefix.getAddress(), 0, addressLen); + } + } } |