@Test void disconnectIfNoHelloExchanged() { AtomicReference<RLPxMessage> capturedDisconnect = new AtomicReference<>(); DefaultWireConnection conn = new DefaultWireConnection( "abc", nodeId, peerNodeId, LoggerProvider.nullProvider().getLogger("rlpx"), capturedDisconnect::set, helloMessage -> { }, () -> { }, new LinkedHashMap<>(), 3, "abc", 10000); conn.messageReceived(new RLPxMessage(45, Bytes.EMPTY)); assertEquals(1, capturedDisconnect.get().messageId()); DisconnectMessage msg = DisconnectMessage.read(capturedDisconnect.get().content()); assertEquals(2, msg.reason()); }
public void messageReceived(RLPxMessage message) { if (message.messageId() == 0) { peerHelloMessage = HelloMessage.read(message.content()); logger.debug("Received peer Hello message {}", peerHelloMessage); initSupportedRange(peerHelloMessage.capabilities()); if (peerHelloMessage.nodeId() == null || peerHelloMessage.nodeId().isEmpty()) { disconnect(DisconnectReason.NULL_NODE_IDENTITY_RECEIVED); return; } if (!peerHelloMessage.nodeId().equals(peerNodeId)) { disconnect(DisconnectReason.UNEXPECTED_IDENTITY); return; } if (peerHelloMessage.nodeId().equals(nodeId)) { disconnect(DisconnectReason.CONNECTED_TO_SELF); return; } if (peerHelloMessage.p2pVersion() > p2pVersion) { disconnect(DisconnectReason.INCOMPATIBLE_DEVP2P_VERSION); return; } if (myHelloMessage == null) { sendHello(); } afterHandshakeListener.accept(peerHelloMessage); for (SubProtocol subProtocol : subprotocolRangeMap.asMapOfRanges().values()) { subprotocols.get(subProtocol).handleNewPeerConnection(id); } return; } else if (message.messageId() == 1) { DisconnectMessage.read(message.content()); disconnectHandler.run(); return; } if (peerHelloMessage == null || myHelloMessage == null) { logger.debug("Message sent before hello exchanged {}", message.messageId()); disconnect(DisconnectReason.PROTOCOL_BREACH); } if (message.messageId() == 2) { sendPong(); } else if (message.messageId() == 3) { if (awaitingPong != null) { awaitingPong.complete(); } } else { Map.Entry<Range<Integer>, SubProtocol> subProtocolEntry = subprotocolRangeMap.getEntry(message.messageId()); if (subProtocolEntry == null) { disconnect(DisconnectReason.PROTOCOL_BREACH); } else { int offset = subProtocolEntry.getKey().lowerEndpoint(); subprotocols.get(subProtocolEntry.getValue()).handle(id, message.messageId() - offset, message.content()); } } }
DefaultWireConnection implements WireConnection { public void messageReceived(RLPxMessage message) { if (message.messageId() == 0) { peerHelloMessage = HelloMessage.read(message.content()); logger.debug("Received peer Hello message {}", peerHelloMessage); initSupportedRange(peerHelloMessage.capabilities()); if (peerHelloMessage.nodeId() == null || peerHelloMessage.nodeId().isEmpty()) { disconnect(DisconnectReason.NULL_NODE_IDENTITY_RECEIVED); return; } if (!peerHelloMessage.nodeId().equals(peerNodeId)) { disconnect(DisconnectReason.UNEXPECTED_IDENTITY); return; } if (peerHelloMessage.nodeId().equals(nodeId)) { disconnect(DisconnectReason.CONNECTED_TO_SELF); return; } if (peerHelloMessage.p2pVersion() > p2pVersion) { disconnect(DisconnectReason.INCOMPATIBLE_DEVP2P_VERSION); return; } if (myHelloMessage == null) { sendHello(); } afterHandshakeListener.accept(peerHelloMessage); for (SubProtocol subProtocol : subprotocolRangeMap.asMapOfRanges().values()) { subprotocols.get(subProtocol).handleNewPeerConnection(id); } return; } else if (message.messageId() == 1) { DisconnectMessage.read(message.content()); disconnectHandler.run(); return; } if (peerHelloMessage == null || myHelloMessage == null) { logger.debug("Message sent before hello exchanged {}", message.messageId()); disconnect(DisconnectReason.PROTOCOL_BREACH); } if (message.messageId() == 2) { sendPong(); } else if (message.messageId() == 3) { if (awaitingPong != null) { awaitingPong.complete(); } } else { Map.Entry<Range<Integer>, SubProtocol> subProtocolEntry = subprotocolRangeMap.getEntry(message.messageId()); if (subProtocolEntry == null) { disconnect(DisconnectReason.PROTOCOL_BREACH); } else { int offset = subProtocolEntry.getKey().lowerEndpoint(); subprotocols.get(subProtocolEntry.getValue()).handle(id, message.messageId() - offset, message.content()); } } } }
DefaultWireConnection implements WireConnection { public void messageReceived(RLPxMessage message) { if (message.messageId() == 0) { peerHelloMessage = HelloMessage.read(message.content()); logger.debug("Received peer Hello message {}", peerHelloMessage); initSupportedRange(peerHelloMessage.capabilities()); if (peerHelloMessage.nodeId() == null || peerHelloMessage.nodeId().isEmpty()) { disconnect(DisconnectReason.NULL_NODE_IDENTITY_RECEIVED); return; } if (!peerHelloMessage.nodeId().equals(peerNodeId)) { disconnect(DisconnectReason.UNEXPECTED_IDENTITY); return; } if (peerHelloMessage.nodeId().equals(nodeId)) { disconnect(DisconnectReason.CONNECTED_TO_SELF); return; } if (peerHelloMessage.p2pVersion() > p2pVersion) { disconnect(DisconnectReason.INCOMPATIBLE_DEVP2P_VERSION); return; } if (myHelloMessage == null) { sendHello(); } afterHandshakeListener.accept(peerHelloMessage); for (SubProtocol subProtocol : subprotocolRangeMap.asMapOfRanges().values()) { subprotocols.get(subProtocol).handleNewPeerConnection(id); } return; } else if (message.messageId() == 1) { DisconnectMessage.read(message.content()); disconnectHandler.run(); return; } if (peerHelloMessage == null || myHelloMessage == null) { logger.debug("Message sent before hello exchanged {}", message.messageId()); disconnect(DisconnectReason.PROTOCOL_BREACH); } if (message.messageId() == 2) { sendPong(); } else if (message.messageId() == 3) { if (awaitingPong != null) { awaitingPong.complete(); } } else { Map.Entry<Range<Integer>, SubProtocol> subProtocolEntry = subprotocolRangeMap.getEntry(message.messageId()); if (subProtocolEntry == null) { disconnect(DisconnectReason.PROTOCOL_BREACH); } else { int offset = subProtocolEntry.getKey().lowerEndpoint(); subprotocols.get(subProtocolEntry.getValue()).handle(id, message.messageId() - offset, message.content()); } } } DefaultWireConnection(
      String id,
      Bytes nodeId,
      Bytes peerNodeId,
      Logger logger,
      Consumer<RLPxMessage> writer,
      Consumer<HelloMessage> afterHandshakeListener,
      Runnable disconnectHandler,
      LinkedHashMap<SubProtocol, SubProtocolHandler> subprotocols,
      int p2pVersion,
      String clientId,
      int advertisedPort); }
DefaultWireConnection implements WireConnection { public void messageReceived(RLPxMessage message) { if (message.messageId() == 0) { peerHelloMessage = HelloMessage.read(message.content()); logger.debug("Received peer Hello message {}", peerHelloMessage); initSupportedRange(peerHelloMessage.capabilities()); if (peerHelloMessage.nodeId() == null || peerHelloMessage.nodeId().isEmpty()) { disconnect(DisconnectReason.NULL_NODE_IDENTITY_RECEIVED); return; } if (!peerHelloMessage.nodeId().equals(peerNodeId)) { disconnect(DisconnectReason.UNEXPECTED_IDENTITY); return; } if (peerHelloMessage.nodeId().equals(nodeId)) { disconnect(DisconnectReason.CONNECTED_TO_SELF); return; } if (peerHelloMessage.p2pVersion() > p2pVersion) { disconnect(DisconnectReason.INCOMPATIBLE_DEVP2P_VERSION); return; } if (myHelloMessage == null) { sendHello(); } afterHandshakeListener.accept(peerHelloMessage); for (SubProtocol subProtocol : subprotocolRangeMap.asMapOfRanges().values()) { subprotocols.get(subProtocol).handleNewPeerConnection(id); } return; } else if (message.messageId() == 1) { DisconnectMessage.read(message.content()); disconnectHandler.run(); return; } if (peerHelloMessage == null || myHelloMessage == null) { logger.debug("Message sent before hello exchanged {}", message.messageId()); disconnect(DisconnectReason.PROTOCOL_BREACH); } if (message.messageId() == 2) { sendPong(); } else if (message.messageId() == 3) { if (awaitingPong != null) { awaitingPong.complete(); } } else { Map.Entry<Range<Integer>, SubProtocol> subProtocolEntry = subprotocolRangeMap.getEntry(message.messageId()); if (subProtocolEntry == null) { disconnect(DisconnectReason.PROTOCOL_BREACH); } else { int offset = subProtocolEntry.getKey().lowerEndpoint(); subprotocols.get(subProtocolEntry.getValue()).handle(id, message.messageId() - offset, message.content()); } } } DefaultWireConnection(
      String id,
      Bytes nodeId,
      Bytes peerNodeId,
      Logger logger,
      Consumer<RLPxMessage> writer,
      Consumer<HelloMessage> afterHandshakeListener,
      Runnable disconnectHandler,
      LinkedHashMap<SubProtocol, SubProtocolHandler> subprotocols,
      int p2pVersion,
      String clientId,
      int advertisedPort); void messageReceived(RLPxMessage message); void disconnect(DisconnectReason reason); AsyncCompletion sendPing(); @Override String id(); void sendMessage(SubProtocolIdentifier subProtocolIdentifier, int messageType, Bytes message); void handleConnectionStart(); @Override String toString(); }
DefaultWireConnection implements WireConnection { public void messageReceived(RLPxMessage message) { if (message.messageId() == 0) { peerHelloMessage = HelloMessage.read(message.content()); logger.debug("Received peer Hello message {}", peerHelloMessage); initSupportedRange(peerHelloMessage.capabilities()); if (peerHelloMessage.nodeId() == null || peerHelloMessage.nodeId().isEmpty()) { disconnect(DisconnectReason.NULL_NODE_IDENTITY_RECEIVED); return; } if (!peerHelloMessage.nodeId().equals(peerNodeId)) { disconnect(DisconnectReason.UNEXPECTED_IDENTITY); return; } if (peerHelloMessage.nodeId().equals(nodeId)) { disconnect(DisconnectReason.CONNECTED_TO_SELF); return; } if (peerHelloMessage.p2pVersion() > p2pVersion) { disconnect(DisconnectReason.INCOMPATIBLE_DEVP2P_VERSION); return; } if (myHelloMessage == null) { sendHello(); } afterHandshakeListener.accept(peerHelloMessage); for (SubProtocol subProtocol : subprotocolRangeMap.asMapOfRanges().values()) { subprotocols.get(subProtocol).handleNewPeerConnection(id); } return; } else if (message.messageId() == 1) { DisconnectMessage.read(message.content()); disconnectHandler.run(); return; } if (peerHelloMessage == null || myHelloMessage == null) { logger.debug("Message sent before hello exchanged {}", message.messageId()); disconnect(DisconnectReason.PROTOCOL_BREACH); } if (message.messageId() == 2) { sendPong(); } else if (message.messageId() == 3) { if (awaitingPong != null) { awaitingPong.complete(); } } else { Map.Entry<Range<Integer>, SubProtocol> subProtocolEntry = subprotocolRangeMap.getEntry(message.messageId()); if (subProtocolEntry == null) { disconnect(DisconnectReason.PROTOCOL_BREACH); } else { int offset = subProtocolEntry.getKey().lowerEndpoint(); subprotocols.get(subProtocolEntry.getValue()).handle(id, message.messageId() - offset, message.content()); } } } DefaultWireConnection(
      String id,
      Bytes nodeId,
      Bytes peerNodeId,
      Logger logger,
      Consumer<RLPxMessage> writer,
      Consumer<HelloMessage> afterHandshakeListener,
      Runnable disconnectHandler,
      LinkedHashMap<SubProtocol, SubProtocolHandler> subprotocols,
      int p2pVersion,
      String clientId,
      int advertisedPort); void messageReceived(RLPxMessage message); void disconnect(DisconnectReason reason); AsyncCompletion sendPing(); @Override String id(); void sendMessage(SubProtocolIdentifier subProtocolIdentifier, int messageType, Bytes message); void handleConnectionStart(); @Override String toString();  }