Changeset 937d2c54


Ignore:
Timestamp:
Dec 18, 2011 1:25:51 PM (9 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
fb4d85f
Parents:
5b37df5
Message:
  • Addresses: Add utility toString() methods
  • Transport: Log tweaks
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • core/java/src/net/i2p/util/Addresses.java

    r5b37df5 r937d2c54  
    1717
    1818/**
    19  * Get the local addresses
     19 * Methods to get the local addresses, and other IP utilities
    2020 *
    21  * @since 0.8.3 moved to core
     21 * @since 0.8.3 moved to core from router/transport
    2222 * @author zzz
    2323 */
     
    115115    }
    116116
     117    /**
     118     *  Convenience method to convert an IP address to a String
     119     *  without throwing an exception.
     120     *  @return "null" for null, and "bad IP length x" if length is invalid
     121     *  @since 0.8.12
     122     */
     123    public static String toString(byte[] addr) {
     124        if (addr == null)
     125            return "null";
     126        try {
     127            return InetAddress.getByAddress(addr).getHostAddress();
     128        } catch (UnknownHostException uhe) {
     129            return "bad IP length " + addr.length;
     130        }
     131    }
     132
     133    /**
     134     *  Convenience method to convert an IP address and port to a String
     135     *  without throwing an exception.
     136     *  @return "ip:port"
     137     *  @since 0.8.12
     138     */
     139    public static String toString(byte[] addr, int port) {
     140        if (addr == null)
     141            return "null:" + port;
     142        try {
     143            String ip = InetAddress.getByAddress(addr).getHostAddress();
     144            if (addr.length != 16)
     145                return ip + ':' + port;
     146            return '[' + ip + "]:" + port;
     147        } catch (UnknownHostException uhe) {
     148            return "(bad IP length " + addr.length + "):" + port;
     149        }
     150    }
     151
     152    /**
     153     *  Print out the local addresses
     154     */
    117155    public static void main(String[] args) {
    118156        System.err.println("External Addresses:");
  • router/java/src/net/i2p/router/transport/TransportImpl.java

    r5b37df5 r937d2c54  
    213213        if (msToSend > 1000) {
    214214            if (_log.shouldLog(Log.WARN))
    215                 _log.warn("afterSend slow: [success=" + sendSuccessful + "] " + msg.getMessageSize() + " byte "
    216                           + msg.getMessageType() + " " + msg.getMessageId() + " to "
    217                           + msg.getTarget().getIdentity().calculateHash().toBase64().substring(0,6) + " took " + msToSend);
     215                _log.warn(getStyle() + " afterSend slow: " + (sendSuccessful ? "success " : "FAIL ")
     216                          + msg.getMessageSize() + " byte "
     217                          + msg.getMessageType() + ' ' + msg.getMessageId() + " to "
     218                          + msg.getTarget().getIdentity().calculateHash().toBase64().substring(0,6) + " took " + msToSend + " ms");
    218219        }
    219220        //if (true)
     
    226227                level = Log.INFO;
    227228            if (_log.shouldLog(level))
    228                 _log.log(level, "afterSend slow (" + lifetime + "/" + msToSend + "): [success=" + sendSuccessful + "] " + msg.getMessageSize() + " byte "
     229                _log.log(level, getStyle() + " afterSend slow (" + (sendSuccessful ? "success " : "FAIL ")
     230                          + lifetime + "/" + msToSend + "): " + msg.getMessageSize() + " byte "
    229231                          + msg.getMessageType() + " " + msg.getMessageId() + " from " + _context.routerHash().toBase64().substring(0,6)
    230232                          + " to " + msg.getTarget().getIdentity().calculateHash().toBase64().substring(0,6) + ": " + msg.toString());
    231233        } else {
    232234            if (_log.shouldLog(Log.INFO))
    233                 _log.info("afterSend: [success=" + sendSuccessful + "] " + msg.getMessageSize() + " byte "
     235                _log.info(getStyle() + " afterSend: " + (sendSuccessful ? "success " : "FAIL ")
     236                          + msg.getMessageSize() + " byte "
    234237                          + msg.getMessageType() + " " + msg.getMessageId() + " from " + _context.routerHash().toBase64().substring(0,6)
    235238                          + " to " + msg.getTarget().getIdentity().calculateHash().toBase64().substring(0,6) + "\n" + msg.toString());
     
    238241        if (sendSuccessful) {
    239242            if (_log.shouldLog(Log.DEBUG))
    240                 _log.debug("Send message " + msg.getMessageType() + " to "
    241                            + msg.getTarget().getIdentity().getHash().toBase64() + " with transport "
    242                            + getStyle() + " successfully");
     243                _log.debug(getStyle() + " Sent " + msg.getMessageType() + " successfully to "
     244                           + msg.getTarget().getIdentity().getHash().toBase64());
    243245            Job j = msg.getOnSendJob();
    244246            if (j != null)
     
    248250        } else {
    249251            if (_log.shouldLog(Log.INFO))
    250                 _log.info("Failed to send message " + msg.getMessageType()
     252                _log.info(getStyle() + " Failed to send " + msg.getMessageType()
    251253                          + " to " + msg.getTarget().getIdentity().getHash().toBase64()
    252                           + " with transport " + getStyle() + " (details: " + msg + ")");
     254                          + " (details: " + msg + ')');
    253255            if (msg.getExpiration() < _context.clock().now())
    254256                _context.statManager().addRateData("transport.expiredOnQueueLifetime", lifetime, lifetime);
  • router/java/src/net/i2p/router/transport/UPnP.java

    r5b37df5 r937d2c54  
    123123                _log.info("UP&P.getAddress() is called \\o/");
    124124                if(isDisabled) {
    125                         _log.warn("Plugin has been disabled previously, ignoring request.");
     125                        if (_log.shouldLog(Log.WARN))
     126                                _log.warn("Plugin has been disabled previously, ignoring request.");
    126127                        return null;
    127128                } else if(!isNATPresent()) {
    128                         _log.warn("No UP&P device found, detection of the external ip address using the plugin has failed");
     129                        if (_log.shouldLog(Log.WARN))
     130                                _log.warn("No UP&P device found, detection of the external ip address using the plugin has failed");
    129131                        return null;
    130132                }
     
    133135                final String natAddress = getNATAddress();
    134136                if (natAddress == null || natAddress.length() <= 0) {
    135                         _log.warn("No external address returned");
     137                        if (_log.shouldLog(Log.WARN))
     138                                _log.warn("No external address returned");
    136139                        return null;
    137140                }
     
    142145                        thinksWeAreDoubleNatted = !TransportImpl.isPubliclyRoutable(detectedIP.getAddress());
    143146                        // If we have forwarded a port AND we don't have a private address
    144                         _log.warn("NATAddress: \"" + natAddress + "\" detectedIP: " + detectedIP + " double? " + thinksWeAreDoubleNatted);
     147                        if (_log.shouldLog(Log.WARN))
     148                                _log.warn("NATAddress: \"" + natAddress + "\" detectedIP: " + detectedIP + " double? " + thinksWeAreDoubleNatted);
    145149                        if((portsForwarded.size() > 1) && (!thinksWeAreDoubleNatted))
    146150                                status = DetectedIP.FULL_INTERNET;
     
    148152                        result = new DetectedIP(detectedIP, status);
    149153                       
    150                         _log.warn("Successful UP&P discovery :" + result);
     154                        if (_log.shouldLog(Log.WARN))
     155                                _log.warn("Successful UP&P discovery :" + result);
    151156                       
    152157                        return new DetectedIP[] { result };
     
    160165                synchronized (lock) {
    161166                        if(isDisabled) {
    162                                 _log.warn("Plugin has been disabled previously, ignoring new device.");
     167                                if (_log.shouldLog(Log.WARN))
     168                                        _log.warn("Plugin has been disabled previously, ignoring new device.");
    163169                                return;
    164170                        }
    165171                }
    166172                if(!ROUTER_DEVICE.equals(dev.getDeviceType()) || !dev.isRootDevice()) {
    167                         _log.warn("UP&P non-IGD device found, ignoring : " + dev.getFriendlyName());
     173                        if (_log.shouldLog(Log.WARN))
     174                                _log.warn("UP&P non-IGD device found, ignoring : " + dev.getFriendlyName());
    168175                        return; // ignore non-IGD devices
    169176                } else if(isNATPresent()) {
    170177                        // maybe we should see if the old one went away before ignoring the new one?
    171                         _log.warn("UP&P ignoring additional IGD device found: " + dev.getFriendlyName() + " UDN: " + dev.getUDN());
     178                        if (_log.shouldLog(Log.WARN))
     179                                _log.warn("UP&P ignoring additional IGD device found: " + dev.getFriendlyName() + " UDN: " + dev.getUDN());
    172180                        /********** seems a little drastic
    173181                        isDisabled = true;
     
    183191                }
    184192               
    185                 _log.warn("UP&P IGD found : " + dev.getFriendlyName() + " UDN: " + dev.getUDN() + " lease time: " + dev.getLeaseTime());
     193                if (_log.shouldLog(Log.WARN))
     194                        _log.warn("UP&P IGD found : " + dev.getFriendlyName() + " UDN: " + dev.getUDN() + " lease time: " + dev.getLeaseTime());
    186195                synchronized(lock) {
    187196                        _router = dev;
     
    232241                                        _service = current2.getService(WAN_PPP_CONNECTION);
    233242                                        if(_service == null) {
    234                                                 _log.warn(_router.getFriendlyName()+ " doesn't seems to be using PPP; we won't be able to extract bandwidth-related informations out of it.");
     243                                                if (_log.shouldLog(Log.WARN))
     244                                                        _log.warn(_router.getFriendlyName()+ " doesn't seems to be using PPP; we won't be able to extract bandwidth-related informations out of it.");
    235245                                                _service = current2.getService(WAN_IP_CONNECTION);
    236246                                                if(_service == null)
     
    245255       
    246256        public boolean tryAddMapping(String protocol, int port, String description, ForwardPort fp) {
    247                 _log.warn("Registering a port mapping for " + port + "/" + protocol);
     257                if (_log.shouldLog(Log.WARN))
     258                        _log.warn("Registering a port mapping for " + port + "/" + protocol);
    248259                int nbOfTries = 0;
    249260                boolean isPortForwarded = false;
     
    256267                        } catch (InterruptedException e) {}
    257268                }
    258                 _log.warn((isPortForwarded ? "Mapping is successful!" : "Mapping has failed!") + " ("+ nbOfTries + " tries)");
     269                if (_log.shouldLog(Log.WARN))
     270                        _log.warn((isPortForwarded ? "Mapping is successful!" : "Mapping has failed!") + " ("+ nbOfTries + " tries)");
    259271                return isPortForwarded;
    260272        }
     
    269281       
    270282        public void deviceRemoved(Device dev ){
    271                 _log.warn("UP&P device removed : " + dev.getFriendlyName() + " UDN: " + dev.getUDN());
     283                if (_log.shouldLog(Log.WARN))
     284                        _log.warn("UP&P device removed : " + dev.getFriendlyName() + " UDN: " + dev.getUDN());
    272285                synchronized (lock) {
    273286                        if(_router == null) return;
     
    278291                           stringEquals(_router.getFriendlyName(), dev.getFriendlyName()) &&
    279292                           stringEquals(_router.getUDN(), dev.getUDN())) {
    280                                 _log.warn("UP&P IGD device removed : " + dev.getFriendlyName());
     293                                if (_log.shouldLog(Log.WARN))
     294                                        _log.warn("UP&P IGD device removed : " + dev.getFriendlyName());
    281295                                _router = null;
    282296                                _service = null;
     
    288302        public void eventNotifyReceived(String uuid, long seq, String varName, String value) {
    289303                if (_log.shouldLog(Log.WARN))
    290                         _log.error("Event: " + uuid + ' ' + seq + ' ' + varName + '=' + value);
     304                        _log.warn("Event: " + uuid + ' ' + seq + ' ' + varName + '=' + value);
    291305        }
    292306
     
    648662                }
    649663               
    650                 if(!noLog)
     664                if(_log.shouldLog(Log.WARN) && !noLog)
    651665                        _log.warn("UPnP: Removed mapping for "+fp.name+" "+port+" / "+protocol);
    652666                return retval;
     
    660674                Set<ForwardPort> portsToDumpNow = null;
    661675                Set<ForwardPort> portsToForwardNow = null;
    662                 _log.warn("UP&P Forwarding "+ports.size()+" ports...");
     676                if (_log.shouldLog(Log.WARN))
     677                        _log.warn("UP&P Forwarding "+ports.size()+" ports...");
    663678                synchronized(lock) {
    664679                        if(forwardCallback != null && forwardCallback != cb && cb != null) {
  • router/java/src/net/i2p/router/transport/udp/InboundEstablishState.java

    r5b37df5 r937d2c54  
    1313import net.i2p.data.Signature;
    1414import net.i2p.router.RouterContext;
     15import net.i2p.util.Addresses;
    1516import net.i2p.util.Log;
    1617
     
    9495        req.readIP(_bobIP, 0);
    9596        if (_log.shouldLog(Log.DEBUG))
    96             _log.debug("Receive sessionRequest, BobIP = " + Base64.encode(_bobIP));
     97            _log.debug("Receive sessionRequest, BobIP = " + Addresses.toString(_bobIP));
    9798        if (_currentState == STATE_UNKNOWN)
    9899            _currentState = STATE_REQUEST_RECEIVED;
     
    187188            buf.append(" ReceivedX: ").append(Base64.encode(_receivedX));
    188189            buf.append(" SentY: ").append(Base64.encode(_sentY));
    189             buf.append(" AliceIP: ").append(Base64.encode(_aliceIP));
    190             buf.append(" AlicePort: ").append(_alicePort);
    191             buf.append(" BobIP: ").append(Base64.encode(_bobIP));
    192             buf.append(" BobPort: ").append(_bobPort);
     190            buf.append(" Alice: ").append(Addresses.toString(_aliceIP, _alicePort));
     191            buf.append(" Bob: ").append(Addresses.toString(_bobIP, _bobPort));
    193192            buf.append(" RelayTag: ").append(_sentRelayTag);
    194193            buf.append(" SignedOn: ").append(_sentSignedOnTime);
     
    347346        if (_sentY != null)
    348347            buf.append(" SentY: ").append(Base64.encode(_sentY, 0, 4));
    349         if (_aliceIP != null)
    350             buf.append(" AliceIP: ").append(Base64.encode(_aliceIP));
    351         buf.append(" AlicePort: ").append(_alicePort);
    352         if (_bobIP != null)
    353             buf.append(" BobIP: ").append(Base64.encode(_bobIP));
    354         buf.append(" BobPort: ").append(_bobPort);
     348        buf.append(" Alice: ").append(Addresses.toString(_aliceIP, _alicePort));
     349        buf.append(" Bob: ").append(Addresses.toString(_bobIP, _bobPort));
    355350        buf.append(" RelayTag: ").append(_sentRelayTag);
    356351        buf.append(" SignedOn: ").append(_sentSignedOnTime);
  • router/java/src/net/i2p/router/transport/udp/OutboundEstablishState.java

    r5b37df5 r937d2c54  
    1414import net.i2p.router.OutNetMessage;
    1515import net.i2p.router.RouterContext;
     16import net.i2p.util.Addresses;
    1617import net.i2p.util.Log;
    1718
     
    166167            _log.debug("Receive session created:\neSig: " + Base64.encode(_receivedEncryptedSignature)
    167168                       + "\nreceivedIV: " + Base64.encode(_receivedIV)
    168                        + "\nAliceIP: " + Base64.encode(_aliceIP)
     169                       + "\nAliceIP: " + Addresses.toString(_aliceIP)
    169170                       + " RelayTag: " + _receivedRelayTag
    170171                       + " SignedOn: " + _receivedSignedOnTime
     
    291292            StringBuilder buf = new StringBuilder(128);
    292293            buf.append("Signed sessionCreated:");
    293             buf.append(" AliceIP: ").append(Base64.encode(_aliceIP));
    294             buf.append(" AlicePort: ").append(_alicePort);
    295             buf.append(" BobIP: ").append(Base64.encode(_bobIP));
    296             buf.append(" BobPort: ").append(_bobPort);
     294            buf.append(" Alice: ").append(Addresses.toString(_aliceIP, _alicePort));
     295            buf.append(" Bob: ").append(Addresses.toString(_bobIP, _bobPort));
    297296            buf.append(" RelayTag: ").append(_receivedRelayTag);
    298297            buf.append(" SignedOn: ").append(_receivedSignedOnTime);
  • router/java/src/net/i2p/router/transport/udp/PacketBuilder.java

    r5b37df5 r937d2c54  
    1717import net.i2p.data.Signature;
    1818import net.i2p.util.ByteCache;
     19import net.i2p.util.Addresses;
    1920import net.i2p.util.Log;
    2021
     
    597598            StringBuilder buf = new StringBuilder(128);
    598599            buf.append("Sending sessionCreated:");
    599             buf.append(" AliceIP: ").append(Base64.encode(sentIP));
    600             buf.append(" AlicePort: ").append(state.getSentPort());
    601             buf.append(" BobIP: ").append(Base64.encode(state.getReceivedOurIP()));
    602             buf.append(" BobPort: ").append(externalPort);
     600            buf.append(" Alice: ").append(Addresses.toString(sentIP, state.getSentPort()));
     601            buf.append(" Bob: ").append(Addresses.toString(state.getReceivedOurIP(), externalPort));
    603602            buf.append(" RelayTag: ").append(state.getSentRelayTag());
    604603            buf.append(" SignedOn: ").append(state.getSentSignedOnTime());
Note: See TracChangeset for help on using the changeset viewer.