From 4e91bb88a5fa8852272b8396f7e4b36b5dad46c7 Mon Sep 17 00:00:00 2001
From: jrandom <jrandom>
Date: Fri, 25 Jun 2004 19:21:11 +0000
Subject: [PATCH] workaround an aggressively up-to-spec kaffe implementation
 (the spec says Socket.getInetAddress() is null if not connected, but sun lets
 the getInetAddress() return a value if it had connected then disconnected,
 while kaffe buggers off and NPEs)

---
 .../transport/tcp/RestrictiveTCPConnection.java    | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/router/java/src/net/i2p/router/transport/tcp/RestrictiveTCPConnection.java b/router/java/src/net/i2p/router/transport/tcp/RestrictiveTCPConnection.java
index 770e9a5bf8..165561b9ab 100644
--- a/router/java/src/net/i2p/router/transport/tcp/RestrictiveTCPConnection.java
+++ b/router/java/src/net/i2p/router/transport/tcp/RestrictiveTCPConnection.java
@@ -250,6 +250,14 @@ class RestrictiveTCPConnection extends TCPConnection {
         long start = _context.clock().now();
         long success = 0;
         if (_log.shouldLog(Log.DEBUG)) _log.debug("Establishing connection...");
+        int port = _socket.getPort();
+        String host = null;
+        // sun keeps the socket's InetAddress around after its been closed, but kaffe (and the rest of classpath)
+        // doesn't, so we've got to check & cache it here if we want to log it later.  (kaffe et al are acting per
+        // spec, btw)
+        if (_socket.isConnected())
+            host = _socket.getInetAddress().getHostName();
+        
         BigInteger myPub = _builder.getMyPublicValue();
         try {
             _socket.setSoTimeout(ESTABLISHMENT_TIMEOUT);
@@ -307,17 +315,17 @@ class RestrictiveTCPConnection extends TCPConnection {
             
         } catch (IOException ioe) {
             if (_log.shouldLog(Log.WARN))
-                _log.warn("Error establishing connection with " + _socket.getInetAddress().getHostAddress() + ":" + _socket.getPort(), ioe);
+                _log.warn("Error establishing connection with " + host + ":" + port, ioe);
             closeConnection();
             return null;
         } catch (DataFormatException dfe) {
             if (_log.shouldLog(Log.WARN))
-                _log.warn("Error establishing connection with " + _socket.getInetAddress().getHostAddress() + ":" + _socket.getPort(), dfe);
+                _log.warn("Error establishing connection with " + host + ":" + port, dfe);
             closeConnection();
             return null;
         } catch (Throwable t) {
             if (_log.shouldLog(Log.ERROR))
-                _log.error("jrandom is paranoid so we're catching it all during establishConnection " + _socket.getInetAddress().getHostAddress() + ":" + _socket.getPort(), t);
+                _log.error("jrandom is paranoid so we're catching it all during establishConnection " + host + ":" + port, t);
             closeConnection();
             return null;
         } finally {
-- 
GitLab