From 95626768e200b4a124696b5fe983296d86efe82c Mon Sep 17 00:00:00 2001 From: Mikael Magnusson Date: Thu, 10 Mar 2022 21:06:36 +0100 Subject: Move Request creation to BGPSession --- src/main/java/com/lumaserv/bgp/BGPFsm.java | 31 ++++---------------------- src/main/java/com/lumaserv/bgp/BGPSession.java | 19 +++++++++++++++- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/lumaserv/bgp/BGPFsm.java b/src/main/java/com/lumaserv/bgp/BGPFsm.java index 2cf598f..a2143bd 100644 --- a/src/main/java/com/lumaserv/bgp/BGPFsm.java +++ b/src/main/java/com/lumaserv/bgp/BGPFsm.java @@ -1,8 +1,5 @@ package com.lumaserv.bgp; -import com.lumaserv.bgp.protocol.AFI; -import com.lumaserv.bgp.protocol.BGPPacket; -import com.lumaserv.bgp.protocol.SAFI; import com.lumaserv.bgp.protocol.message.BGPNotification; import com.lumaserv.bgp.protocol.message.BGPOpen; import com.lumaserv.bgp.protocol.message.BGPUpdate; @@ -15,8 +12,8 @@ import java.util.Timer; import java.util.TimerTask; public class BGPFsm { - final int CONFIG_HOLD_TIME = 80; // FIXME use from configuration - final byte CONFIG_VERSION = 4; + public static final int CONFIG_HOLD_TIME = 80; // FIXME use from configuration + public static final byte CONFIG_VERSION = 4; public final State IDLE; public final State CONNECT; @@ -182,18 +179,8 @@ public class BGPFsm { setConnectRetryTimer(0); // - completes BGP initialization - BGPSessionConfiguration config = session.getConfiguration(); - 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())); - BGPOpen request = new BGPOpen() - .setAsn(config.getLocalAs()) - .setHoldTime(CONFIG_HOLD_TIME) - .setVersion(CONFIG_VERSION) - .setIdentifier(config.getLocalIdentifier()); - request.getOptionalParameters().add(caps); try { - session.sendOpen(request); + session.sendOpen(); setHoldTimer(4 * 60); setState(OPEN_SENT); } catch (IOException ex) { @@ -242,18 +229,8 @@ public class BGPFsm { setConnectRetryTimer(0); // - completes the BGP initialization, - BGPSessionConfiguration config = session.getConfiguration(); - 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())); - BGPOpen request = new BGPOpen() - .setAsn(config.getLocalAs()) - .setHoldTime(CONFIG_HOLD_TIME) - .setVersion(CONFIG_VERSION) - .setIdentifier(config.getLocalIdentifier()); - request.getOptionalParameters().add(caps); try { - session.sendOpen(request); + session.sendOpen(); setHoldTimer(4 * 60); setState(OPEN_SENT); } catch (IOException ex) { diff --git a/src/main/java/com/lumaserv/bgp/BGPSession.java b/src/main/java/com/lumaserv/bgp/BGPSession.java index 94a4f5a..21a5564 100644 --- a/src/main/java/com/lumaserv/bgp/BGPSession.java +++ b/src/main/java/com/lumaserv/bgp/BGPSession.java @@ -1,6 +1,8 @@ package com.lumaserv.bgp; +import com.lumaserv.bgp.protocol.AFI; import com.lumaserv.bgp.protocol.BGPPacket; +import com.lumaserv.bgp.protocol.SAFI; import com.lumaserv.bgp.protocol.message.BGPNotification; import com.lumaserv.bgp.protocol.message.BGPOpen; import com.lumaserv.bgp.protocol.message.BGPUpdate; @@ -92,11 +94,26 @@ public class BGPSession implements Runnable { thread.start(); } - public void sendOpen(BGPOpen request) throws IOException { + void sendOpen(BGPOpen request) throws IOException { outputStream.write(new BGPPacket().setType(BGPPacket.Type.OPEN).setMessage(request.build()).build()); System.out.println("Sent open"); } + public 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())); + + BGPOpen request = new BGPOpen() + .setAsn(configuration.getLocalAs()) + .setHoldTime(BGPFsm.CONFIG_HOLD_TIME) + .setVersion(BGPFsm.CONFIG_VERSION) + .setIdentifier(configuration.getLocalIdentifier()); + request.getOptionalParameters().add(caps); + + sendOpen(request); + } + public void retryConnection() { if (!socket.isConnected()) { try { -- cgit v1.2.3