diff options
author | Mikael Magnusson <mikma@users.sourceforge.net> | 2022-01-30 14:50:01 +0100 |
---|---|---|
committer | Mikael Magnusson <mikma@users.sourceforge.net> | 2023-11-13 00:17:57 +0100 |
commit | 9f35136d97673b825fb51c2e1c7067881a3d6820 (patch) | |
tree | cd11a6bb62058983d2844cf83164970ea25b19a6 | |
parent | d4eb9c36754afcfbfbf758afd80816ee71a57362 (diff) |
WIP active connection
-rw-r--r-- | src/main/java/com/lumaserv/bgp/BGPServer.java | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/main/java/com/lumaserv/bgp/BGPServer.java b/src/main/java/com/lumaserv/bgp/BGPServer.java index b8f934a..27019b6 100644 --- a/src/main/java/com/lumaserv/bgp/BGPServer.java +++ b/src/main/java/com/lumaserv/bgp/BGPServer.java @@ -72,4 +72,52 @@ public class BGPServer implements Runnable { } } + public boolean connect(BGPSessionConfiguration config, String host) throws IOException { + return connect(config, host, 179); + } + + public boolean connect(BGPSessionConfiguration config, String host, int port) throws IOException { + try { + byte version = 4; + int holdTime = 90; + + Socket socket = new Socket(host, port); + BGPOpen request = new BGPOpen() + .setAsn(config.getLocalAs()) + .setHoldTime(holdTime) + .setVersion(version) + .setIdentifier(config.getLocalIdentifier()); + socket.getOutputStream().write(new BGPPacket().setType(BGPPacket.Type.OPEN).setMessage(request.build()).build()); + +System.out.println("Sent open"); + + + BGPPacket packet = BGPPacket.read(socket.getInputStream()); +System.out.println("Received packet"); + if(packet.getType() != BGPPacket.Type.OPEN) { + // TODO close +System.out.println("Bad open"); + return false; + } +System.out.println("Received open"); + BGPOpen response = new BGPOpen(packet.getMessage()); + if(config.getRemoteAs() != response.getAsn()) { + // TODO shutdown, close +System.out.println("Bad asn"); + return false; + } + + BGPSession session = new BGPSession(socket, config); + config.getListener().onOpen(session); + + session.keepAlive(); +System.out.println("Sent keepalive"); + + new Thread(session).start(); + return true; + } catch (IOException ex) { + ex.printStackTrace(); + throw(ex); + } + } } |