diff options
author | Mikael Magnusson <mikma@users.sourceforge.net> | 2022-01-30 14:50:01 +0100 |
---|---|---|
committer | Mikael Magnusson <mikma@users.sourceforge.net> | 2022-03-12 22:53:19 +0100 |
commit | 0c8eeb0ea14eb1d06b5fcc359c2c32505df100bb (patch) | |
tree | 764736df8b6fc4687648149c878cce152b3cce97 /src/main/java/com/lumaserv/bgp/BGPServer.java | |
parent | 49d32fe2302542ce9aeeaf169ba48ab5449eab37 (diff) |
Add BGP finite-state machine
Implement BGPFsm.
Diffstat (limited to 'src/main/java/com/lumaserv/bgp/BGPServer.java')
-rw-r--r-- | src/main/java/com/lumaserv/bgp/BGPServer.java | 51 |
1 files changed, 32 insertions, 19 deletions
diff --git a/src/main/java/com/lumaserv/bgp/BGPServer.java b/src/main/java/com/lumaserv/bgp/BGPServer.java index b8f934a..695ef96 100644 --- a/src/main/java/com/lumaserv/bgp/BGPServer.java +++ b/src/main/java/com/lumaserv/bgp/BGPServer.java @@ -42,34 +42,47 @@ public class BGPServer implements Runnable { while (true) { try { Socket socket = serverSocket.accept(); - BGPPacket packet = BGPPacket.read(socket.getInputStream()); - if(packet.getType() != BGPPacket.Type.OPEN) - continue; - BGPOpen request = new BGPOpen(packet.getMessage()); + System.out.println("Accept"); + BGPSessionConfiguration config = sessionConfigurations.stream() - .filter(c -> c.getRemoteAs() == request.getAsn()) + .filter(c -> socket.getInetAddress().equals(c.getRemoteAddr())) .findFirst() .orElse(null); - if(config == null) + System.out.println("Config: " + config); + if(config == null) { + System.out.println("Peer not found:" + socket.getInetAddress()); + socket.close(); continue; - BGPSession session = new BGPSession(socket, config); - config.getListener().onOpen(session); - BGPOpen response = new BGPOpen() - .setAsn(config.getLocalAs()) - .setHoldTime(request.getHoldTime()) - .setVersion(request.getVersion()) - .setIdentifier(config.getLocalIdentifier()); - try { - socket.getOutputStream().write(new BGPPacket().setType(BGPPacket.Type.OPEN).setMessage(response.build()).build()); - } catch (IOException e) { - e.printStackTrace(); } - session.keepAlive(); - new Thread(session).start(); + + BGPFsm fsm = new BGPFsm(); + BGPSession session = new BGPSession(socket, config, fsm); + fsm.setSession(session); + System.out.println("automaticStartPassive"); + fsm.getCurrentState().automaticStartPassive(); + + // System.out.println("tcpConnectionConfirmed in " + fsm.getCurrentState()); + // fsm.getCurrentState().tcpConnectionConfirmed(); } catch (IOException ex) { ex.printStackTrace(); } } } + 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 { + BGPFsm fsm = new BGPFsm(); + BGPSession session = new BGPSession(config, fsm, host, port); + fsm.setSession(session); + fsm.getCurrentState().automaticStart(); + return true; + } catch (IOException ex) { + ex.printStackTrace(); + throw(ex); + } + } } |