Skip to content

Please add option to force resolve DNS address by using IPv4 or IPv6 #22089

Closed

Description

@vdjuric

For example, this is supported by CURL with command line parameters -4, --ipv4 and -6, --ipv6 (https://curl.haxx.se/docs/manpage.html#-6)
This parameter would probably be in HttpClientHandler class with default value set to IPv4.

One could resolve IP address manually by calling System.Net.Dns.GetHostAddressesAsync and use this in Uri.Host, but there can be issues when connecting to servers with HTTPS protocol.

Activity

  1. PeterSmithRedmond commented on Jun 1, 2017

    @PeterSmithRedmond

    Can you speak to why you would find this useful? Most of the IPv4/IPv6 kinks have been worked out, and tons of people can connect without specifying IPv4 versus IPv6. in this case, the OS should automatically get both the IPv4 and IPv6 addresses for the host and then pick the "best" one. In some cases, that will be the IPv4 address, and in some case (like the newer Cell networks) it will be the IPv6 address.

    What kind of network are your customers on such that the wrong IP address is being picked?

  2. vdjuric commented on Jun 2, 2017

    @vdjuric
    ContributorAuthor

    This is just an idea to make API more powerful (maybe this could be long-term plan). Similar like user can choose HTTP Version (1.0, 1.1, 2.0), it would be also nice if IP version could be explicitly set.

    And default value should be probably "Auto" and not "IPv4". Auto setting would behave like current version (probably takes first record from DNS server regardless if it IPv4 or IPv6).

    On .NET Core for Unix which uses CURL, I think this change would be easy because CURL already supports this.

    Another (even more poweful) idea for hostname resolving would be as following:

    • user could optionally set non-static property httpClientHandlerInstance.ResolveHostAddressAsync
    • ResolveHostAddressAsync would accept delegate with signature similar to System.Net.Dns.GetHostAddressesAsync but instead of array of addresses it would return single address: System.Threading.Tasks.Task<System.Net.IPAddress>
    • this IP address would then be used when establishing connection with server
    • by default this ResolveHostAddressAsync property would be null (current behaviour)
    • user could then easily setup custom resolving logic which would be very powerful feature
      • for example, one could use for integration testing production URLs on HttpClient, but ResolveHostAddressAsync would resolve it to test IP (instead of production)

    What do you think?

  3. vdjuric commented on Jun 2, 2017

    @vdjuric
    ContributorAuthor

    Update:

    Good thing about second idea is that:

    • it automatically solves initial problem (no need to set IPv4 or IPv6)
    • if this feature is used, IP resolution is then completely on user of the HttpClient library (they can't "blame" you for any issues)
  4. vdjuric commented on Jun 2, 2017

    @vdjuric
    ContributorAuthor

    Bonus: with this user could also achieve 100% same IP resolution regardless of underlying operating system

  5. svick commented on Jun 2, 2017

    @svick
    Member

    This is just an idea to make API more powerful

    That doesn't sound like a good enough reason to me. Can you describe a situation when someone would actually use this?

  6. vdjuric commented on Jun 2, 2017

    @vdjuric
    ContributorAuthor
    • unit/integration testing (production URLs resolve to test IPs)
    • IP would always be resolved same regardless of operating system (consistency)
    • possible to choose between IPv4 / IPv6 addresses by querying DNS server directly
    • possible to implement custom caching of resolved IP addresses, not relying on operating system DNS caching
    • one could easily create programmatic round-robin strategy built into their client library to access web services by using different IP address each time
    • more robust client libraries: if server resources are accessible on multiple IP addresses, you could quickly ping each server before resolving IP address, making sure it is healthy. This way you would always return only working IP address
    • ...

    Again, this is just an idea. You would have to explicitly "turn on" this feature to use it, so backward compatibility would not be an issue.

    Please close this if you think it is not relevant :)

  7. wernerb90 commented on Nov 15, 2017

    @wernerb90

    Did anything come of this suggestion?

    I would +1 to the feature of adding something similar to curl's --resolve, to manually put an IP address behind a hostname.

  8. geoffkizer commented on Jan 16, 2018

    @geoffkizer
    Contributor

    Adding something like a ResolveHostAddress hook does seem interesting. At this point I don't think it's a priority, but it's something to consider for the future.

    Note we would probably only support this in the managed handler, since for winhttp and curl the DNS lookup is handled by the underlying library.

  9. vdjuric commented on Jan 16, 2018

    @vdjuric
    ContributorAuthor

    Sounds good! I will look into managed implementation of HttpClient and try to prepare prototype.

  10. vdjuric commented on Jan 20, 2018

    @vdjuric
    ContributorAuthor

    I am currently playing with HttpRequestMessage class. Just for test I added new property:

    • AddressFamily? DnsResolveAddressFamily {get;set;}

    (AddressFamily? = null would mean current default behaviour) and updated System.Net.Http.ConnectHelper to pass this parameter.

    Now I need to test this but I am having DLL conflicts with official .NET Core System.Net.Http library :)
    Will spend some more time :)

  11. vdjuric commented on Jan 20, 2018

    @vdjuric
    ContributorAuthor

    Supported values for DnsResolveAddressFamily would be:

    • AddressFamily.InterNetwork (IPv4)
    • AddressFamily.InterNetworkV6 (IPv6)
    • null (default behaviour)
  12. vdjuric commented on Jan 20, 2018

    @vdjuric
    ContributorAuthor

    Possible alternative for null: AddressFamily.Unspecified

  13. PaulDotNet commented on Sep 28, 2018

    @PaulDotNet

    This is badly needed. IPv4 vs IPv6 kinks are not worked out at least when it comes to VPN clients.

    Git credential manager is failing because standard IP address resolution does not always work right.

  14. karelz commented on Oct 10, 2019

    @karelz
    Member

    Triage: We believe this should be addressed by dotnet/corefx#35404.

  15. karelz commented on Oct 10, 2019

    @karelz
    Member

    Duplicate of dotnet/corefx#35404

  16. transferred this issue fromdotnet/corefxon Jan 31, 2020
  17. added this to the 5.0 milestone on Jan 31, 2020
  18. scalablecory commented on Sep 17, 2020

    @scalablecory
    Contributor

    This has been resolved via the API added here in .NET 5: #41949

  19. ghost locked as resolved and limited conversation to collaborators on Dec 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-needs-workAPI needs work before it is approved, it is NOT ready for implementationarea-System.Net.Http

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions