How to use libzt with OkHttpClient?

Hello,

I need some guidance on using libzt with OkHttpClient on Android Studio.

My ZeroTierNode is working fine - connects to my network and is assigned a valid IPv4 address that I can ping from another ZeroTier device.

I tried creating a custom SocketFactory that binds to a local IP Address (i.e the node’s address). Here’s my custom SocketFactory code.

class MySocketFactory extends SocketFactory {
        private SocketFactory socketFactory;
        private InetAddress nodeAddress;

        public MySocketFactory(InetAddress nodeAddress) {
            this.socketFactory = SocketFactory.getDefault();
            this.nodeAddress = nodeAddress;
        }

        @Override
        public Socket createSocket() throws IOException {
            System.out.println("MySocketFactory - createSocket");
            Socket socket = socketFactory.createSocket();
            socket.bind(new InetSocketAddress(nodeAddress,0));

            return socket;
        }

        @Override
        public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
            return null;
        }

        @Override
        public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException {
            return null;
        }

        @Override
        public Socket createSocket(InetAddress host, int port) throws IOException {
            return null;
        }

        @Override
        public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
            return null;
        }
    }

And my implementation of the socket factory in OkHttpClient builder -

     try {
            SSLContext sc = SSLContext.getInstance("TLS");
            trustManager = getDefaultTrustManager();
            sc.init(new KeyManager[] { keyManager }, new TrustManager[] { trustManager }, new SecureRandom());

            httpClient = new OkHttpClient.Builder()
                    .connectionPool(new ConnectionPool(0,1, TimeUnit.MILLISECONDS))
                    .readTimeout(2000, TimeUnit.MILLISECONDS)
                    .connectTimeout(3000, TimeUnit.MILLISECONDS)
                    .proxy(Proxy.NO_PROXY)
                    .build();

            httpClient = httpClient.newBuilder()
                    .socketFactory(new MySocketFactory(node.getIPv4Address(networkId)))
                    .sslSocketFactory(sc.getSocketFactory(), trustManager)
                    .build();

            System.out.println("checkpoint ");

        } catch (NoSuchAlgorithmException | KeyManagementException  e) {
            throw new RuntimeException(e);
        }

However, this consistently throws the bind exception :

java.net.BindException: bind failed: EADDRNOTAVAIL (Cannot assign requested address)

I figured this was because libzt doesn’t create a network interface the way the full client does.

Is there any way to get libzt to work with OkHttpClient? (I saw it in the documentations and figured it has to be possible)

I’m very new to socket/network programming, so do let me know if I’ve missed something entirely.

Thanks in advance! Cheers.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.