@Test public void testOutgoingKeyAgreementConnection() throws Exception { if (!systemHasLocalIpv4Address()) { System.err.println("WARNING: Skipping test, no local IPv4 address"); return; } Callback callback = new Callback(); Executor executor = Executors.newCachedThreadPool(); DuplexPlugin plugin = new LanTcpPlugin(executor, backoff, callback, 0, 0); plugin.start(); assertTrue(callback.propertiesLatch.await(5, SECONDS)); String ipPorts = callback.local.get("ipPorts"); assertNotNull(ipPorts); String[] split = ipPorts.split(","); assertEquals(1, split.length); split = split[0].split(":"); assertEquals(2, split.length); String addrString = split[0]; ServerSocket ss = new ServerSocket(); ss.bind(new InetSocketAddress(addrString, 0), 10); CountDownLatch latch = new CountDownLatch(1); AtomicBoolean error = new AtomicBoolean(false); new Thread(() -> { try { ss.accept(); latch.countDown(); } catch (IOException e) { error.set(true); } }).start(); BdfList descriptor = new BdfList(); descriptor.add(TRANSPORT_ID_LAN); InetSocketAddress local = (InetSocketAddress) ss.getLocalSocketAddress(); descriptor.add(local.getAddress().getAddress()); descriptor.add(local.getPort()); DuplexTransportConnection d = plugin.createKeyAgreementConnection( new byte[COMMIT_LENGTH], descriptor); assertNotNull(d); assertTrue(latch.await(5, SECONDS)); assertFalse(error.get()); d.getReader().dispose(false, true); d.getWriter().dispose(false); ss.close(); plugin.stop(); }
@Override public DuplexTransportConnection createKeyAgreementConnection( byte[] commitment, BdfList descriptor) { if (!isRunning()) return null; InetSocketAddress remote; try { remote = parseSocketAddress(descriptor); } catch (FormatException e) { LOG.info("Invalid IP/port in key agreement descriptor"); return null; } if (!isConnectable(remote)) { if (LOG.isLoggable(INFO)) { SocketAddress local = socket.getLocalSocketAddress(); LOG.info(scrubSocketAddress(remote) + " is not connectable from " + scrubSocketAddress(local)); } return null; } try { if (LOG.isLoggable(INFO)) LOG.info("Connecting to " + scrubSocketAddress(remote)); Socket s = createSocket(); s.bind(new InetSocketAddress(socket.getInetAddress(), 0)); s.connect(remote); s.setSoTimeout(socketTimeout); if (LOG.isLoggable(INFO)) LOG.info("Connected to " + scrubSocketAddress(remote)); return new TcpTransportConnection(this, s); } catch (IOException e) { if (LOG.isLoggable(INFO)) LOG.info("Could not connect to " + scrubSocketAddress(remote)); return null; } }
LanTcpPlugin extends TcpPlugin { @Override public DuplexTransportConnection createKeyAgreementConnection( byte[] commitment, BdfList descriptor) { if (!isRunning()) return null; InetSocketAddress remote; try { remote = parseSocketAddress(descriptor); } catch (FormatException e) { LOG.info("Invalid IP/port in key agreement descriptor"); return null; } if (!isConnectable(remote)) { if (LOG.isLoggable(INFO)) { SocketAddress local = socket.getLocalSocketAddress(); LOG.info(scrubSocketAddress(remote) + " is not connectable from " + scrubSocketAddress(local)); } return null; } try { if (LOG.isLoggable(INFO)) LOG.info("Connecting to " + scrubSocketAddress(remote)); Socket s = createSocket(); s.bind(new InetSocketAddress(socket.getInetAddress(), 0)); s.connect(remote); s.setSoTimeout(socketTimeout); if (LOG.isLoggable(INFO)) LOG.info("Connected to " + scrubSocketAddress(remote)); return new TcpTransportConnection(this, s); } catch (IOException e) { if (LOG.isLoggable(INFO)) LOG.info("Could not connect to " + scrubSocketAddress(remote)); return null; } } }
LanTcpPlugin extends TcpPlugin { @Override public DuplexTransportConnection createKeyAgreementConnection( byte[] commitment, BdfList descriptor) { if (!isRunning()) return null; InetSocketAddress remote; try { remote = parseSocketAddress(descriptor); } catch (FormatException e) { LOG.info("Invalid IP/port in key agreement descriptor"); return null; } if (!isConnectable(remote)) { if (LOG.isLoggable(INFO)) { SocketAddress local = socket.getLocalSocketAddress(); LOG.info(scrubSocketAddress(remote) + " is not connectable from " + scrubSocketAddress(local)); } return null; } try { if (LOG.isLoggable(INFO)) LOG.info("Connecting to " + scrubSocketAddress(remote)); Socket s = createSocket(); s.bind(new InetSocketAddress(socket.getInetAddress(), 0)); s.connect(remote); s.setSoTimeout(socketTimeout); if (LOG.isLoggable(INFO)) LOG.info("Connected to " + scrubSocketAddress(remote)); return new TcpTransportConnection(this, s); } catch (IOException e) { if (LOG.isLoggable(INFO)) LOG.info("Could not connect to " + scrubSocketAddress(remote)); return null; } } LanTcpPlugin(Executor ioExecutor, Backoff backoff,
			DuplexPluginCallback callback, int maxLatency, int maxIdleTime); }
LanTcpPlugin extends TcpPlugin { @Override public DuplexTransportConnection createKeyAgreementConnection( byte[] commitment, BdfList descriptor) { if (!isRunning()) return null; InetSocketAddress remote; try { remote = parseSocketAddress(descriptor); } catch (FormatException e) { LOG.info("Invalid IP/port in key agreement descriptor"); return null; } if (!isConnectable(remote)) { if (LOG.isLoggable(INFO)) { SocketAddress local = socket.getLocalSocketAddress(); LOG.info(scrubSocketAddress(remote) + " is not connectable from " + scrubSocketAddress(local)); } return null; } try { if (LOG.isLoggable(INFO)) LOG.info("Connecting to " + scrubSocketAddress(remote)); Socket s = createSocket(); s.bind(new InetSocketAddress(socket.getInetAddress(), 0)); s.connect(remote); s.setSoTimeout(socketTimeout); if (LOG.isLoggable(INFO)) LOG.info("Connected to " + scrubSocketAddress(remote)); return new TcpTransportConnection(this, s); } catch (IOException e) { if (LOG.isLoggable(INFO)) LOG.info("Could not connect to " + scrubSocketAddress(remote)); return null; } } LanTcpPlugin(Executor ioExecutor, Backoff backoff,
			DuplexPluginCallback callback, int maxLatency, int maxIdleTime); @Override TransportId getId(); @Override boolean supportsKeyAgreement(); @Override KeyAgreementListener createKeyAgreementListener(byte[] commitment); @Override DuplexTransportConnection createKeyAgreementConnection(
			byte[] commitment, BdfList descriptor); }
LanTcpPlugin extends TcpPlugin { @Override public DuplexTransportConnection createKeyAgreementConnection( byte[] commitment, BdfList descriptor) { if (!isRunning()) return null; InetSocketAddress remote; try { remote = parseSocketAddress(descriptor); } catch (FormatException e) { LOG.info("Invalid IP/port in key agreement descriptor"); return null; } if (!isConnectable(remote)) { if (LOG.isLoggable(INFO)) { SocketAddress local = socket.getLocalSocketAddress(); LOG.info(scrubSocketAddress(remote) + " is not connectable from " + scrubSocketAddress(local)); } return null; } try { if (LOG.isLoggable(INFO)) LOG.info("Connecting to " + scrubSocketAddress(remote)); Socket s = createSocket(); s.bind(new InetSocketAddress(socket.getInetAddress(), 0)); s.connect(remote); s.setSoTimeout(socketTimeout); if (LOG.isLoggable(INFO)) LOG.info("Connected to " + scrubSocketAddress(remote)); return new TcpTransportConnection(this, s); } catch (IOException e) { if (LOG.isLoggable(INFO)) LOG.info("Could not connect to " + scrubSocketAddress(remote)); return null; } } LanTcpPlugin(Executor ioExecutor, Backoff backoff,
			DuplexPluginCallback callback, int maxLatency, int maxIdleTime); @Override TransportId getId(); @Override boolean supportsKeyAgreement(); @Override KeyAgreementListener createKeyAgreementListener(byte[] commitment); @Override DuplexTransportConnection createKeyAgreementConnection(
			byte[] commitment, BdfList descriptor);  }