From 8ce7e47624a1251eb8c4c10b9ad1b6eb6362994b Mon Sep 17 00:00:00 2001 From: zzz Date: Fri, 27 Mar 2026 16:39:02 -0400 Subject: [PATCH] SAM: Use session for b32 lookups if available so router doesn't use expl. tunnels and put LS in main db --- .../java/src/net/i2p/sam/SAMMessageSess.java | 9 ++++++ .../src/net/i2p/sam/SAMMessageSession.java | 23 +++++++++++++++ .../src/net/i2p/sam/SAMStreamSession.java | 11 ++++++++ apps/sam/java/src/net/i2p/sam/SAMUtils.java | 8 +++--- .../java/src/net/i2p/sam/SAMv1Handler.java | 28 ++++++++++++++++--- 5 files changed, 71 insertions(+), 8 deletions(-) diff --git a/apps/sam/java/src/net/i2p/sam/SAMMessageSess.java b/apps/sam/java/src/net/i2p/sam/SAMMessageSess.java index 2fc45431f..8aef9e473 100644 --- a/apps/sam/java/src/net/i2p/sam/SAMMessageSess.java +++ b/apps/sam/java/src/net/i2p/sam/SAMMessageSess.java @@ -60,4 +60,13 @@ interface SAMMessageSess extends Closeable { public int getListenProtocol(); public int getListenPort(); + + /** + * Lookup a destination through the I2CP session. + * Blocking. + * + * @return the Destination or null + * @since 0.9.69 + */ + public Destination lookupDest(String name) throws I2PSessionException ; } diff --git a/apps/sam/java/src/net/i2p/sam/SAMMessageSession.java b/apps/sam/java/src/net/i2p/sam/SAMMessageSession.java index f52cb2564..259de3eda 100644 --- a/apps/sam/java/src/net/i2p/sam/SAMMessageSession.java +++ b/apps/sam/java/src/net/i2p/sam/SAMMessageSession.java @@ -210,6 +210,29 @@ abstract class SAMMessageSession implements SAMMessageSess { return session.sendMessage(d, data, 0, data.length, proto, fromPort, toPort, opts); } + /** + * Lookup a destination through the I2CP session. + * Blocking. + * + * @return the Destination or null + * @since 0.9.69 + */ + public Destination lookupDest(String name) throws I2PSessionException { + return lookupDest(session, name); + } + + /** + * Lookup a destination through the I2CP session. + * Blocking. + * + * @return the Destination or null + * @since 0.9.69 + */ + static Destination lookupDest(I2PSession session, String name) throws I2PSessionException { + // session will convert b32 to hash, no need to do it here + return session.lookupDest(name, 10*1000); + } + /** * Close a SAM message-based session. */ diff --git a/apps/sam/java/src/net/i2p/sam/SAMStreamSession.java b/apps/sam/java/src/net/i2p/sam/SAMStreamSession.java index 102d69e3e..497708777 100644 --- a/apps/sam/java/src/net/i2p/sam/SAMStreamSession.java +++ b/apps/sam/java/src/net/i2p/sam/SAMStreamSession.java @@ -347,6 +347,17 @@ class SAMStreamSession implements SAMMessageSess { return true; } + /** + * Lookup a destination through the I2CP session. + * Blocking. + * + * @return the Destination or null + * @since 0.9.69 + */ + public Destination lookupDest(String name) throws I2PSessionException { + return SAMMessageSession.lookupDest(socketMgr.getSession(), name); + } + /** * Close a SAM STREAM session. * diff --git a/apps/sam/java/src/net/i2p/sam/SAMUtils.java b/apps/sam/java/src/net/i2p/sam/SAMUtils.java index a9f2737e5..8df46622f 100644 --- a/apps/sam/java/src/net/i2p/sam/SAMUtils.java +++ b/apps/sam/java/src/net/i2p/sam/SAMUtils.java @@ -170,12 +170,12 @@ class SAMUtils { if (d == null) { String msg; if (s.length() >= 516) - msg = "Bad Base64 dest: "; + msg = "Bad Base64 dest"; else if (s.length() >= 60 && s.endsWith(".b32.i2p")) - msg = "Lease set not found: "; + msg = "Lease set not found"; else - msg = "Host name not found: "; - throw new DataFormatException(msg + s); + msg = "Host name not found"; + throw new DataFormatException(msg); } return d; } diff --git a/apps/sam/java/src/net/i2p/sam/SAMv1Handler.java b/apps/sam/java/src/net/i2p/sam/SAMv1Handler.java index 088a98200..7bf386ba0 100644 --- a/apps/sam/java/src/net/i2p/sam/SAMv1Handler.java +++ b/apps/sam/java/src/net/i2p/sam/SAMv1Handler.java @@ -19,6 +19,7 @@ import java.net.Socket; import java.net.SocketTimeoutException; import java.nio.channels.SocketChannel; import java.nio.ByteBuffer; +import java.util.Locale; import java.util.Properties; import java.util.concurrent.atomic.AtomicLong; @@ -389,10 +390,29 @@ class SAMv1Handler extends SAMHandler implements SAMRawReceiver, SAMDatagramRece return writeString("NAMING REPLY RESULT=KEY_NOT_FOUND NAME=\"\" MESSAGE=\"Name=ME requires established session\"\n"); } } else { - try { - dest = SAMUtils.getDest(name); - } catch (DataFormatException e) { - } + try { + if (name.length() >= 516 || !name.toLowerCase(Locale.US).endsWith(".b32.i2p")) { + // out of session + dest = SAMUtils.getDest(name); + } else if (streamSession != null) { + // lookup in-session so the router will use the client tunnels to get the LS + // and put the LS in the client's netdb + dest = streamSession.lookupDest(name); + } else if (datagramSession != null) { + dest = datagramSession.lookupDest(name); + } else if (rawSession != null) { + dest = rawSession.lookupDest(name); + } else { + // out of session + // leaseset will end up in the main netdb + // and will have to be looked up again by the router if a message is sent to it. + dest = SAMUtils.getDest(name); + } + } catch (I2PSessionException e) { + return writeString("NAMING REPLY RESULT=KEY_NOT_FOUND NAME=" + name, e.getMessage()); + } catch (DataFormatException e) { + return writeString("NAMING REPLY RESULT=KEY_NOT_FOUND NAME=" + name, e.getMessage()); + } } if (dest == null) {