Repository navigation
Milestone
Description
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.
Metadata
Metadata
Assignees
Labels
No labels
Activity
infiniteloopltd commentedon Nov 19, 2020
The same error can be replicated with this code:
Which is adapted from Npgsql.NpgsqlConnector.Connect
infiniteloopltd commentedon Nov 19, 2020
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
YohDeadfall commentedon Nov 19, 2020
Would you like to submit a PR?
vonzshik commentedon Nov 19, 2020
Funny thing is, it seems to work fine for me. @infiniteloopltd are you using .net 5?
infiniteloopltd commentedon Nov 19, 2020
Using .NET Core 3.1 (AWS Lambda)
vonzshik commentedon Nov 19, 2020
Yeah, that does explain it.
vonzshik commentedon Nov 19, 2020
I propose to fix it like so:
var protocolType = endpoint.AddressFamily == AddressFamily.InterNetwork || endpoint.AddressFamily == AddressFamily.InterNetworkV6 ? ProtocolType.Tcp : ProtocolType.IP;infiniteloopltd commentedon Nov 19, 2020
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.
Fixed socket connection using IPv6 on .NET < 5.0 (#3364)
Fixed socket connection using IPv6 on .NET < 5.0 (#3364)
Fixed socket connection using IPv6 on .NET < 5.0 (#3364)
Update NpgsqlConnector.cs
Fixed socket connection using IPv6 for async method (#3424)
Fixed socket connection using IPv6 for async method (#3424)
Fixed socket connection using IPv6 for async method (#3424)