From 028ff0be508bf65cd7f811e645384c34ae8983b4 Mon Sep 17 00:00:00 2001 From: Mikael Magnusson Date: Sun, 5 Mar 2023 22:47:36 +0100 Subject: Uncommitted changes --- src/main/java/com/lumaserv/bgp/BGPFsm.java | 4 +-- src/main/java/com/lumaserv/bgp/BGPListener.java | 1 - src/main/java/com/lumaserv/bgp/BGPSession.java | 10 +++++- .../java/com/lumaserv/bgp/protocol/IPPrefix.java | 2 ++ .../bgp/protocol/attribute/ASPathAttribute.java | 2 ++ .../bgp/protocol/attribute/NextHopAttribute.java | 2 ++ .../bgp/protocol/attribute/OriginAttribute.java | 4 ++- .../lumaserv/bgp/protocol/message/BGPUpdate.java | 40 ++++++++++++++++++++++ 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 96378a5..533cb96 100644 --- a/src/main/java/com/lumaserv/bgp/BGPFsm.java +++ b/src/main/java/com/lumaserv/bgp/BGPFsm.java @@ -360,7 +360,7 @@ public class BGPFsm { setHoldTimer(holdTime); setState(OPEN_CONFIRM); } else { - System.out.println("Bad asn"); + System.out.println("Bad asn:" + (openAsn & 0xFFFFFFFFL)); try { BGPNotification notification = new BGPNotification() .setMajorErrorCode(BGPNotification.Error.OPEN_MESSAGE_ERROR.getCode()) @@ -515,7 +515,7 @@ public class BGPFsm { connectRetryTimer = new Timer(); } - 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 e785e3b..9de32ab 100644 --- a/src/main/java/com/lumaserv/bgp/BGPSession.java +++ b/src/main/java/com/lumaserv/bgp/BGPSession.java @@ -144,7 +144,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())); @@ -161,6 +161,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); + } + } } -- cgit v1.2.3