diff options
Diffstat (limited to 'src/main/java')
-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); + } + } } |