From e5801be43ed555fd55970ab8af37a02c6107ae09 Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Wed, 28 Jan 2015 16:25:50 +0000
Subject: [PATCH] only use valid IP for geoIP

---
 .../transport/CommSystemFacadeImpl.java       | 24 +++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)

diff --git a/router/java/src/net/i2p/router/transport/CommSystemFacadeImpl.java b/router/java/src/net/i2p/router/transport/CommSystemFacadeImpl.java
index 88374cf496..f31bcce373 100644
--- a/router/java/src/net/i2p/router/transport/CommSystemFacadeImpl.java
+++ b/router/java/src/net/i2p/router/transport/CommSystemFacadeImpl.java
@@ -390,24 +390,40 @@ public class CommSystemFacadeImpl extends CommSystemFacade {
         RouterInfo ri = _context.netDb().lookupRouterInfoLocally(peer);
         if (ri == null)
             return null;
-        ip = getIP(ri);
+        ip = getValidIP(ri);
         if (ip != null)
             return _geoIP.get(ip);
         return null;
     }
 
+    /**
+     *  Return first IP (v4 or v6) we find, any transport.
+     *  Not validated, may be local, etc.
+     */
     private static byte[] getIP(RouterInfo ri) {
-        // Return first IP (v4 or v6) we find, any transport
-        // Assume IPv6 doesn't have geoIP for now
         for (RouterAddress ra : ri.getAddresses()) {
             byte[] rv = ra.getIP();
-            //if (rv != null && rv.length == 4)
             if (rv != null)
                 return rv;
         }
         return null;
     }
 
+    /**
+     *  Return first valid IP (v4 or v6) we find, any transport.
+     *  Local and other invalid IPs will not be returned.
+     *
+     *  @since 0.9.18
+     */
+    private static byte[] getValidIP(RouterInfo ri) {
+        for (RouterAddress ra : ri.getAddresses()) {
+            byte[] rv = ra.getIP();
+            if (rv != null && TransportUtil.isPubliclyRoutable(rv, true))
+                return rv;
+        }
+        return null;
+    }
+
     /** full name for a country code, or the code if we don't know the name */
     @Override
     public String getCountryName(String c) {
-- 
GitLab