From b5bfe4ce6949406012381a8c8bd942a07e5569b4 Mon Sep 17 00:00:00 2001 From: Gabriele Santomaggio Date: Tue, 31 Jan 2023 13:39:00 +0100 Subject: [PATCH] Decode the address type (#219) Fixes https://github.com/rabbitmq/rabbitmq-stream-dotnet-client/issues/218 Signed-off-by: Gabriele Santomaggio --- RabbitMQ.Stream.Client/RoutingClient.cs | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/RabbitMQ.Stream.Client/RoutingClient.cs b/RabbitMQ.Stream.Client/RoutingClient.cs index 147476a4..5bc008ef 100644 --- a/RabbitMQ.Stream.Client/RoutingClient.cs +++ b/RabbitMQ.Stream.Client/RoutingClient.cs @@ -51,7 +51,7 @@ private static async Task LookupConnection( // We use the localhost ip as default // this is mostly to have a default value. - var endPointNoLb = new IPEndPoint(IPAddress.Loopback, (int)broker.Port); + EndPoint endPointNoLb = new IPEndPoint(IPAddress.Loopback, (int)broker.Port); // ValidateDns just validate the DNS // it the real world application is always TRUE @@ -59,8 +59,7 @@ private static async Task LookupConnection( // it should not change. if (routing.ValidateDns) { - var hostEntry = await Dns.GetHostEntryAsync(broker.Host); - endPointNoLb = new IPEndPoint(hostEntry.AddressList.First(), (int)broker.Port); + endPointNoLb = await GetEndPoint(broker); } // In this case we just return the node (leader for producer, random for consumer) @@ -100,6 +99,25 @@ private static async Task LookupConnection( return client; } + internal static async Task GetEndPoint(Broker broker) + { + switch (Uri.CheckHostName(broker.Host)) + { + case UriHostNameType.Basic: + case UriHostNameType.Dns: + var hostEntry = await Dns.GetHostEntryAsync(broker.Host); + var endPointNoLb = new IPEndPoint(hostEntry.AddressList.First(), (int)broker.Port); + return endPointNoLb; + case UriHostNameType.IPv4: + case UriHostNameType.IPv6: + return new IPEndPoint(IPAddress.Parse(broker.Host), (int)broker.Port); + case UriHostNameType.Unknown: + throw new RoutingClientException($"Unknown host name {broker.Host}"); + default: + throw new RoutingClientException($"Unknown host name {broker.Host}"); + } + } + private static int MaxAttempts(StreamInfo metaDataInfo) { // Here we have a reasonable number of retry.