From 060ff721755ad7a69f26ae7a79cc81e2873ddf47 Mon Sep 17 00:00:00 2001
From: Matt Joiner <anacrolix@gmail.com>
Date: Tue, 24 May 2016 19:46:24 +1000
Subject: [PATCH] Improve the logic for which address to use for DHT

---
 client.go | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/client.go b/client.go
index 340c7317..fe863299 100644
--- a/client.go
+++ b/client.go
@@ -314,9 +314,7 @@ func NewClient(cfg *Config) (cl *Client, err error) {
 		if dhtCfg.IPBlocklist == nil {
 			dhtCfg.IPBlocklist = cl.ipBlockList
 		}
-		if dhtCfg.Addr == "" {
-			dhtCfg.Addr = cl.listenAddr
-		}
+		dhtCfg.Addr = firstNonEmptyString(dhtCfg.Addr, cl.listenAddr, cl.config.ListenAddr)
 		if dhtCfg.Conn == nil && cl.utpSock != nil {
 			dhtCfg.Conn = cl.utpSock
 		}
@@ -329,6 +327,15 @@ func NewClient(cfg *Config) (cl *Client, err error) {
 	return
 }
 
+func firstNonEmptyString(ss ...string) string {
+	for _, s := range ss {
+		if s != "" {
+			return s
+		}
+	}
+	return ""
+}
+
 // Stops the client. All connections to peers are closed and all activity will
 // come to a halt.
 func (cl *Client) Close() {
-- 
2.51.0