@Test public void testIncomingKeyAgreementConnection() 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)); KeyAgreementListener kal = plugin.createKeyAgreementListener(new byte[COMMIT_LENGTH]); assertNotNull(kal); CountDownLatch latch = new CountDownLatch(1); AtomicBoolean error = new AtomicBoolean(false); new Thread(() -> { try { kal.accept(); latch.countDown(); } catch (IOException e) { error.set(true); } }).start(); BdfList descriptor = kal.getDescriptor(); assertEquals(3, descriptor.size()); assertEquals(TRANSPORT_ID_LAN, descriptor.getLong(0).longValue()); byte[] address = descriptor.getRaw(1); InetAddress addr = InetAddress.getByAddress(address); assertTrue(addr instanceof Inet4Address); assertFalse(addr.isLoopbackAddress()); assertTrue(addr.isLinkLocalAddress() || addr.isSiteLocalAddress()); int port = descriptor.getLong(2).intValue(); assertTrue(port > 0 && port < 65536); InetSocketAddress socketAddr = new InetSocketAddress(addr, port); Socket s = new Socket(); s.connect(socketAddr, 100); assertTrue(latch.await(5, SECONDS)); assertFalse(error.get()); s.close(); kal.close(); plugin.stop(); }
@Override public KeyAgreementListener createKeyAgreementListener(byte[] commitment) { ServerSocket ss = null; for (InetSocketAddress addr : getLocalSocketAddresses()) { addr = new InetSocketAddress(addr.getAddress(), 0); try { ss = new ServerSocket(); ss.bind(addr); break; } catch (IOException e) { if (LOG.isLoggable(INFO)) LOG.info("Failed to bind " + scrubSocketAddress(addr)); tryToClose(ss); } } if (ss == null || !ss.isBound()) { LOG.info("Could not bind server socket for key agreement"); return null; } BdfList descriptor = new BdfList(); descriptor.add(TRANSPORT_ID_LAN); InetSocketAddress local = (InetSocketAddress) ss.getLocalSocketAddress(); descriptor.add(local.getAddress().getAddress()); descriptor.add(local.getPort()); return new LanKeyAgreementListener(descriptor, ss); }
LanTcpPlugin extends TcpPlugin { @Override public KeyAgreementListener createKeyAgreementListener(byte[] commitment) { ServerSocket ss = null; for (InetSocketAddress addr : getLocalSocketAddresses()) { addr = new InetSocketAddress(addr.getAddress(), 0); try { ss = new ServerSocket(); ss.bind(addr); break; } catch (IOException e) { if (LOG.isLoggable(INFO)) LOG.info("Failed to bind " + scrubSocketAddress(addr)); tryToClose(ss); } } if (ss == null || !ss.isBound()) { LOG.info("Could not bind server socket for key agreement"); return null; } BdfList descriptor = new BdfList(); descriptor.add(TRANSPORT_ID_LAN); InetSocketAddress local = (InetSocketAddress) ss.getLocalSocketAddress(); descriptor.add(local.getAddress().getAddress()); descriptor.add(local.getPort()); return new LanKeyAgreementListener(descriptor, ss); } }
LanTcpPlugin extends TcpPlugin { @Override public KeyAgreementListener createKeyAgreementListener(byte[] commitment) { ServerSocket ss = null; for (InetSocketAddress addr : getLocalSocketAddresses()) { addr = new InetSocketAddress(addr.getAddress(), 0); try { ss = new ServerSocket(); ss.bind(addr); break; } catch (IOException e) { if (LOG.isLoggable(INFO)) LOG.info("Failed to bind " + scrubSocketAddress(addr)); tryToClose(ss); } } if (ss == null || !ss.isBound()) { LOG.info("Could not bind server socket for key agreement"); return null; } BdfList descriptor = new BdfList(); descriptor.add(TRANSPORT_ID_LAN); InetSocketAddress local = (InetSocketAddress) ss.getLocalSocketAddress(); descriptor.add(local.getAddress().getAddress()); descriptor.add(local.getPort()); return new LanKeyAgreementListener(descriptor, ss); } LanTcpPlugin(Executor ioExecutor, Backoff backoff,
			DuplexPluginCallback callback, int maxLatency, int maxIdleTime); }
LanTcpPlugin extends TcpPlugin { @Override public KeyAgreementListener createKeyAgreementListener(byte[] commitment) { ServerSocket ss = null; for (InetSocketAddress addr : getLocalSocketAddresses()) { addr = new InetSocketAddress(addr.getAddress(), 0); try { ss = new ServerSocket(); ss.bind(addr); break; } catch (IOException e) { if (LOG.isLoggable(INFO)) LOG.info("Failed to bind " + scrubSocketAddress(addr)); tryToClose(ss); } } if (ss == null || !ss.isBound()) { LOG.info("Could not bind server socket for key agreement"); return null; } BdfList descriptor = new BdfList(); descriptor.add(TRANSPORT_ID_LAN); InetSocketAddress local = (InetSocketAddress) ss.getLocalSocketAddress(); descriptor.add(local.getAddress().getAddress()); descriptor.add(local.getPort()); return new LanKeyAgreementListener(descriptor, ss); } 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 KeyAgreementListener createKeyAgreementListener(byte[] commitment) { ServerSocket ss = null; for (InetSocketAddress addr : getLocalSocketAddresses()) { addr = new InetSocketAddress(addr.getAddress(), 0); try { ss = new ServerSocket(); ss.bind(addr); break; } catch (IOException e) { if (LOG.isLoggable(INFO)) LOG.info("Failed to bind " + scrubSocketAddress(addr)); tryToClose(ss); } } if (ss == null || !ss.isBound()) { LOG.info("Could not bind server socket for key agreement"); return null; } BdfList descriptor = new BdfList(); descriptor.add(TRANSPORT_ID_LAN); InetSocketAddress local = (InetSocketAddress) ss.getLocalSocketAddress(); descriptor.add(local.getAddress().getAddress()); descriptor.add(local.getPort()); return new LanKeyAgreementListener(descriptor, ss); } 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);  }