Skip to content

IPv6 support for NpSQL #3363

Closed
Closed

Description

@infiniteloopltd

Steps to reproduce

  var dsn = "Host=2a03:4000:49:2fb::;Username=xxx;Password=xxx;Database=xxx";
  var conn = new NpgsqlConnection(dsn);
  conn.Open();
  conn.Close();

The issue

Host names defined by IPv6 addresses (Also FQDN's with AAAA DNS records) cause an error;

If you are seeing an exception, include the full exceptions details (message and stack trace).

Exception message: Address family not supported by protocol
Stack trace:
  "at System.Net.Sockets.Socket..ctor(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)",
    "at Npgsql.NpgsqlConnector.Connect(NpgsqlTimeout timeout)",
    "at Npgsql.NpgsqlConnector.RawOpen(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)",
    "at Npgsql.NpgsqlConnector.Open(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)",
    "at Npgsql.ConnectorPool.OpenNewConnector(NpgsqlConnection conn, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)",
    "at Npgsql.ConnectorPool.<>c__DisplayClass38_0.<<Rent>g__RentAsync|0>d.MoveNext()",
    "--- End of stack trace from previous location where exception was thrown ---",
    "at Npgsql.NpgsqlConnection.<>c__DisplayClass41_0.<<Open>g__OpenAsync|0>d.MoveNext()",
    "--- End of stack trace from previous location where exception was thrown ---",
    "at Npgsql.NpgsqlConnection.Open()",

Further technical details

Npgsql version: 5.0.0
PostgreSQL version: PostgreSQL 12.4 (Debian 12.4-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
Operating system: Debian 12.4

Other details about my project setup:

The host needs to support IPv6, so this particular example was a lambda function running on AWS.

Activity

  1. infiniteloopltd commented on Nov 19, 2020

    @infiniteloopltd
    Author

    The same error can be replicated with this code:

    IPAddress ipAddress = IPAddress.Parse("2a03:4000:49:2fb::");
    IPEndPoint endpoint = new IPEndPoint(ipAddress, 5432);
    var protocolType = endpoint.AddressFamily == AddressFamily.InterNetwork ? ProtocolType.Tcp : ProtocolType.IP;
    var socket = new Socket(endpoint.AddressFamily, SocketType.Stream, protocolType);
    socket.Connect(endpoint);
    

    Which is adapted from Npgsql.NpgsqlConnector.Connect

  2. infiniteloopltd commented on Nov 19, 2020

    @infiniteloopltd
    Author

    The above code can be fixed using

    var protocolType = endpoint.AddressFamily == AddressFamily.InterNetworkV6 ? ProtocolType.Tcp : ProtocolType.IP;

    Where InterNetworkV6 works with both IPv4 and IPv6

  3. YohDeadfall commented on Nov 19, 2020

    @YohDeadfall
    Contributor

    Would you like to submit a PR?

  4. added this to the 4.1.7 milestone on Nov 19, 2020
  5. vonzshik commented on Nov 19, 2020

    @vonzshik
    Contributor

    Funny thing is, it seems to work fine for me. @infiniteloopltd are you using .net 5?

  6. infiniteloopltd commented on Nov 19, 2020

    @infiniteloopltd
    Author

    Funny thing is, it seems to work fine for me. @infiniteloopltd are you using .net 5?

    Using .NET Core 3.1 (AWS Lambda)

  7. vonzshik commented on Nov 19, 2020

    @vonzshik
    Contributor

    Funny thing is, it seems to work fine for me. @infiniteloopltd are you using .net 5?

    Using .NET Core 3.1 (AWS Lambda)

    Yeah, that does explain it.

  8. vonzshik commented on Nov 19, 2020

    @vonzshik
    Contributor

    The above code can be fixed using

    var protocolType = endpoint.AddressFamily == AddressFamily.InterNetworkV6 ? ProtocolType.Tcp : ProtocolType.IP;

    Where InterNetworkV6 works with both IPv4 and IPv6

    I propose to fix it like so:

    var protocolType = endpoint.AddressFamily == AddressFamily.InterNetwork || endpoint.AddressFamily == AddressFamily.InterNetworkV6 ? ProtocolType.Tcp : ProtocolType.IP;

  9. infiniteloopltd commented on Nov 19, 2020

    @infiniteloopltd
    Author

    Would you like to submit a PR?

    Would like to, but I can't get the library to compile locally. If anyone else would like to submit the PR, I'd be happy to verify the fix.

  10. added 3 commits that reference this issue on Nov 19, 2020
    9b8f9a4
    a650b5a
    6292c68
  11. added a commit that references this issue on Dec 22, 2020
  12. added theissue type on Jun 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions