From 27b48034c5e725864dec75d1969f7ce65be5befb Mon Sep 17 00:00:00 2001
From: zzz <zzz@mail.i2p>
Date: Wed, 25 May 2011 13:34:32 +0000
Subject: [PATCH] b32 cleanups

---
 .../client/naming/BlockfileNamingService.java |  3 +++
 .../i2p/client/naming/DummyNamingService.java | 22 +++++++++++++++----
 .../client/naming/EepGetNamingService.java    |  3 +++
 .../i2p/client/naming/ExecNamingService.java  |  3 +++
 .../i2p/client/naming/MetaNamingService.java  |  3 +++
 .../net/i2p/client/naming/NamingService.java  |  2 ++
 6 files changed, 32 insertions(+), 4 deletions(-)

diff --git a/core/java/src/net/i2p/client/naming/BlockfileNamingService.java b/core/java/src/net/i2p/client/naming/BlockfileNamingService.java
index cd00197bd2..ced910183b 100644
--- a/core/java/src/net/i2p/client/naming/BlockfileNamingService.java
+++ b/core/java/src/net/i2p/client/naming/BlockfileNamingService.java
@@ -371,6 +371,9 @@ public class BlockfileNamingService extends DummyNamingService {
             d = super.lookup(hostname, null, null);
             if (d != null)
                 return d;
+            // Base32 failed?
+            if (hostname.length() == BASE32_HASH_LENGTH + 8 && hostname.toLowerCase().endsWith(".b32.i2p"))
+                return null;
         }
 
         String key = hostname.toLowerCase();
diff --git a/core/java/src/net/i2p/client/naming/DummyNamingService.java b/core/java/src/net/i2p/client/naming/DummyNamingService.java
index 69c3dcb32c..2e009fe3fe 100644
--- a/core/java/src/net/i2p/client/naming/DummyNamingService.java
+++ b/core/java/src/net/i2p/client/naming/DummyNamingService.java
@@ -19,7 +19,7 @@ import net.i2p.data.Destination;
  */
 class DummyNamingService extends NamingService {
 
-    private static final int BASE32_HASH_LENGTH = 52;   // 1 + Hash.HASH_LENGTH * 8 / 5
+    protected static final int BASE32_HASH_LENGTH = 52;   // 1 + Hash.HASH_LENGTH * 8 / 5
     public final static String PROP_B32 = "i2p.naming.hostsTxt.useB32";
     protected static final int CACHE_MAX_SIZE = 32;
     public static final int DEST_SIZE = 516;                    // Std. Base64 length (no certificate)
@@ -41,6 +41,13 @@ class DummyNamingService extends NamingService {
         super(context);
     }
     
+    /**
+     *  @param hostname mixed case as it could be a key
+     *  @param lookupOptions input parameter, NamingService-specific, can be null
+     *  @param storedOptions output parameter, NamingService-specific, any stored properties will be added if non-null
+     *  @return dest or null
+     *  @since 0.8.7
+     */
     @Override
     public Destination lookup(String hostname, Properties lookupOptions, Properties storedOptions) {
         Destination d = getCache(hostname);
@@ -56,7 +63,7 @@ class DummyNamingService extends NamingService {
         }
 
         // Try Base32 decoding
-        if (hostname.length() == BASE32_HASH_LENGTH + 8 && hostname.endsWith(".b32.i2p") &&
+        if (hostname.length() == BASE32_HASH_LENGTH + 8 && hostname.toLowerCase().endsWith(".b32.i2p") &&
             _context.getBooleanPropertyDefaultTrue(PROP_B32)) {
             d = LookupDest.lookupBase32Hash(_context, hostname.substring(0, BASE32_HASH_LENGTH));
             if (d != null) {
@@ -70,6 +77,7 @@ class DummyNamingService extends NamingService {
 
     /**
      *  Provide basic static caching for all services
+     *  @param s case-sensitive, could be a hostname or a full b64 string
      */
     protected static void putCache(String s, Destination d) {
         if (d == null)
@@ -79,14 +87,20 @@ class DummyNamingService extends NamingService {
         }
     }
 
-    /** @return cached dest or null */
+    /**
+     *  @param s case-sensitive, could be a hostname or a full b64 string
+     *  @return cached dest or null
+     */
     protected static Destination getCache(String s) {
         synchronized (_cache) {
             return _cache.get(s);
         }
     }
 
-    /** @since 0.8.7 */
+    /**
+     *  @param s case-sensitive, could be a hostname or a full b64 string
+     *  @since 0.8.7
+     */
     protected static void removeCache(String s) {
         synchronized (_cache) {
             _cache.remove(s);
diff --git a/core/java/src/net/i2p/client/naming/EepGetNamingService.java b/core/java/src/net/i2p/client/naming/EepGetNamingService.java
index eeadd6dd4f..cd63bcc62a 100644
--- a/core/java/src/net/i2p/client/naming/EepGetNamingService.java
+++ b/core/java/src/net/i2p/client/naming/EepGetNamingService.java
@@ -66,6 +66,9 @@ public class EepGetNamingService extends DummyNamingService {
             return d;
 
         hostname = hostname.toLowerCase();
+        // Base32 failed?
+        if (hostname.length() == BASE32_HASH_LENGTH + 8 && hostname.endsWith(".b32.i2p"))
+            return null;
 
         List URLs = getURLs();
         if (URLs.isEmpty())
diff --git a/core/java/src/net/i2p/client/naming/ExecNamingService.java b/core/java/src/net/i2p/client/naming/ExecNamingService.java
index 78c902e4f2..40383ca789 100644
--- a/core/java/src/net/i2p/client/naming/ExecNamingService.java
+++ b/core/java/src/net/i2p/client/naming/ExecNamingService.java
@@ -64,6 +64,9 @@ public class ExecNamingService extends DummyNamingService {
         Destination d = super.lookup(hostname, null, null);
         if (d != null)
             return d;
+        // Base32 failed?
+        if (hostname.length() == BASE32_HASH_LENGTH + 8 && hostname.toLowerCase().endsWith(".b32.i2p"))
+            return null;
 
         hostname = hostname.toLowerCase();
 
diff --git a/core/java/src/net/i2p/client/naming/MetaNamingService.java b/core/java/src/net/i2p/client/naming/MetaNamingService.java
index 7227b1adc9..215aa11c0b 100644
--- a/core/java/src/net/i2p/client/naming/MetaNamingService.java
+++ b/core/java/src/net/i2p/client/naming/MetaNamingService.java
@@ -100,6 +100,9 @@ public class MetaNamingService extends DummyNamingService {
         Destination d = super.lookup(hostname, null, null);
         if (d != null)
             return d;
+        // Base32 failed?
+        if (hostname.length() == BASE32_HASH_LENGTH + 8 && hostname.toLowerCase().endsWith(".b32.i2p"))
+            return null;
 
         for (NamingService ns : _services) { 
             d = ns.lookup(hostname, lookupOptions, storedOptions);
diff --git a/core/java/src/net/i2p/client/naming/NamingService.java b/core/java/src/net/i2p/client/naming/NamingService.java
index efc2711881..6ee288abbe 100644
--- a/core/java/src/net/i2p/client/naming/NamingService.java
+++ b/core/java/src/net/i2p/client/naming/NamingService.java
@@ -422,6 +422,8 @@ public abstract class NamingService {
      * will be only one naming service instance (singleton) as well as
      * choose the implementation from the "i2p.naming.impl" system
      * property.
+     *
+     * FIXME Actually, it doesn't ensure that. Only call this once!!!
      */
     public static final synchronized NamingService createInstance(I2PAppContext context) {
         NamingService instance = null;
-- 
GitLab