How do I force my android app to use ipv4 instead of ipv6?

I'm having problems with my app when making REST requests using retrofit + okhttp on ipv6 networks. The response time appears to be very high when connected to ipv6 networks (approximately 10 seconds for each request). Can you tell me if there is any way to limit the app so that it prioritizes ipv4 networks? I know that this can impact the usability of the app, but the app is for use in more restricted environments, it is not for the general public.

Upvotes: 8

Views: 10696

Answers (2)

Yuri Schimke
Yuri Schimke

Reputation: 13488

OkHttp 5 (alpha) supports HappyEyeballs. It's by default in alpha11. This should correctly handle a mix of IPv4 and IPv6 without code changes.

See https://twitter.com/jessewilson/status/1495780761819037697?lang=en-GB for the announcement.

Upvotes: 0

Yuri Schimke
Yuri Schimke

Reputation: 13488

You can set a custom Dns implementation that just filters the Dns.SYSTEM results to IPv4.

https://github.com/yschimke/okurl/blob/master/src/main/kotlin/com/baulsupp/okurl/network/DnsSelector.kt

class DnsSelector() : Dns {
  override fun lookup(hostname: String): List<InetAddress> {
    return Dns.SYSTEM.lookup(hostname).filter { Inet4Address::class.java.isInstance(it) }
  }
}

then set it

  val client = OkHttpClient.Builder().dns(DnsSelector()).build()

Upvotes: 13

Related Questions