~smaioli/azureus/ubuntu-experimental

« back to all changes in this revision

Viewing changes to org/gudy/azureus2/core3/tracker/client/impl/bt/TRTrackerBTAnnouncerImpl.java

MergedĀ VuzeĀ 4.2.0.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
166
166
        
167
167
        
168
168
        private int announceCount;
 
169
        private int announceFailCount;
 
170
        
169
171
        private byte autoUDPprobeEvery = 1;
170
172
        
171
173
  
992
994
                                }
993
995
                                                        
994
996
                         }else{
995
 
                                                        
 
997
                                        
 
998
                                 announceFailCount++;
 
999
                                 
996
1000
                                 last_failure_resp = resp;
997
1001
                         }
998
1002
                         
999
1003
                  }catch( MalformedURLException e ){
1000
1004
                        
 
1005
                        announceFailCount++;
 
1006
                        
1001
1007
                        Debug.printStackTrace( e );
1002
1008
                        
1003
1009
                        last_failure_resp = 
1008
1014
                                                getErrorRetryInterval(), 
1009
1015
                                                "malformed URL '" + (request_url==null?"<null>":request_url.toString()) + "'" );
1010
1016
                        
1011
 
                  }catch( Exception e ){
1012
 
                                                
 
1017
                  }catch( Throwable e ){
 
1018
                                
 
1019
                        announceFailCount++;
 
1020
 
1013
1021
                        last_failure_resp = 
1014
1022
                                new TRTrackerAnnouncerResponseImpl(
1015
1023
                                                original_url,
1091
1099
                                        
1092
1100
                                        boolean udp_probe = false;
1093
1101
                                        
1094
 
                                        if (protocol.equalsIgnoreCase("udp") && udpAnnounceEnabled)
1095
 
                                        {
 
1102
                                                // if we have multiple tracker URLs then do something sensible about
 
1103
                                        
 
1104
                                        if ( protocol.equalsIgnoreCase("udp") && udpAnnounceEnabled ){
 
1105
                                                
1096
1106
                                                udpAnnounceURL = reqUrl;
1097
 
                                        } else if (protocol.equalsIgnoreCase("http") && !az_tracker
1098
 
                                                && announceCount % autoUDPprobeEvery == 0 && udpAnnounceEnabled)
1099
 
                                        {
1100
 
                                                // if we don't know this tracker supports UDP then don't probe on
1101
 
                                                // first announce as we don't want a large delay on torrent startup
1102
 
                                                // also if we are stopping we don't want to initiate a probe as
1103
 
                                                // we want the stop instruction to get to tracker if possible
1104
 
                                                if ((stopped || announceCount == 0) && !TRTrackerUtils.isUDPProbeOK(reqUrl))
1105
 
                                                {
 
1107
                                                
 
1108
                                        }else if (      protocol.equalsIgnoreCase("http") && 
 
1109
                                                                !az_tracker     && 
 
1110
                                                                announceCount % autoUDPprobeEvery == 0 && 
 
1111
                                                                udpAnnounceEnabled ){
 
1112
                                                
 
1113
                                                        // if we don't know this tracker supports UDP then don't probe on
 
1114
                                                        // first announce as we don't want a large delay on torrent startup
 
1115
                                                        // also if we are stopping we don't want to initiate a probe as
 
1116
                                                        // we want the stop instruction to get to tracker if possible
 
1117
                                                
 
1118
                                                if (    (       stopped || 
 
1119
                                                                        announceCount == 0 ||
 
1120
                                                                        ( announceCount < trackerUrlLists.size() && announceFailCount == announceCount )) && 
 
1121
                                                                !TRTrackerUtils.isUDPProbeOK(reqUrl)){
 
1122
                                                        
1106
1123
                                                        // skip probe
1107
 
                                                } else
1108
 
                                                {
 
1124
                                                        
 
1125
                                                }else{
1109
1126
                                                        udpAnnounceURL = new URL(reqUrl.toString().replaceFirst("^http", "udp"));
 
1127
                                                        
1110
1128
                                                        udp_probe = true;
1111
1129
                                                }
1112
1130
                                        }
1113
1131
                                                        
1114
 
                                        if (udpAnnounceURL != null)
1115
 
                                        {
1116
 
                                                failure_reason = announceUDP(reqUrl, message, !udp_probe);
1117
 
                                                if ((failure_reason != null || message.size() == 0) && udp_probe)
1118
 
                                                {
1119
 
                                                        // automatic UDP probe failed, use HTTP again
 
1132
                                        if ( udpAnnounceURL != null ){
 
1133
                                                
 
1134
                                                failure_reason = announceUDP( reqUrl, message, udp_probe );
 
1135
                                                
 
1136
                                                if ((failure_reason != null || message.size() == 0) && udp_probe){
 
1137
                                                        
 
1138
                                                                // automatic UDP probe failed, use HTTP again
 
1139
                                                        
1120
1140
                                                        udpAnnounceURL = null;
1121
 
                                                        if (autoUDPprobeEvery < 16)
 
1141
                                                        
 
1142
                                                        if ( autoUDPprobeEvery < 16 ){
 
1143
                                                
1122
1144
                                                                autoUDPprobeEvery <<= 1;
1123
 
                                                        else // unregister in case the tracker somehow changed its capabilities
 
1145
                                                        }else{
 
1146
                                                                
 
1147
                                                                        // unregister in case the tracker somehow changed its capabilities
 
1148
                                                
1124
1149
                                                                TRTrackerUtils.setUDPProbeResult(reqUrl, false);
1125
 
                                                        if (Logger.isEnabled())
 
1150
                                                        }
 
1151
                                                        
 
1152
                                                        if (Logger.isEnabled()){
1126
1153
                                                                Logger.log(new LogEvent(torrent, LOGID, LogEvent.LT_INFORMATION, "redirection of http announce [" + tracker_url[0] + "] to udp failed, will retry in " + autoUDPprobeEvery + " announces"));
 
1154
                                                        }       
 
1155
                                                }else if (failure_reason == null && udp_probe){
1127
1156
                                                        
1128
 
                                                } else if (failure_reason == null && udp_probe)
1129
 
                                                {
1130
1157
                                                        TRTrackerUtils.setUDPProbeResult(reqUrl, true);
1131
 
                                                        if (Logger.isEnabled())
 
1158
                                                        
 
1159
                                                        if (Logger.isEnabled()){
1132
1160
                                                                Logger.log(new LogEvent(torrent, LOGID, LogEvent.LT_INFORMATION, "redirection of http announce [" + tracker_url[0] + "] to udp successful"));
 
1161
                                                        }
 
1162
                                                        
1133
1163
                                                        autoUDPprobeEvery = 1;
1134
1164
                                                }
1135
1165
                                        }
1136
1166
                                        
1137
1167
                                        announceCount++;
1138
1168
                                        
1139
 
                                        if ( udpAnnounceURL == null)
 
1169
                                        if ( udpAnnounceURL == null){
 
1170
                                                
1140
1171
                                                failure_reason = announceHTTP( tracker_url, reqUrl, message );
 
1172
                                        }
1141
1173
        
1142
 
                                        // if we've got some kind of response then return it
1143
 
                                        if ( message.size() > 0 )
 
1174
                                                // if we've got some kind of response then return it
 
1175
                                        
 
1176
                                        if ( message.size() > 0 ){
 
1177
                                                
1144
1178
                                                return( message.toByteArray());
1145
 
        
 
1179
                                        }
1146
1180
                                                
1147
1181
                                        if ( failure_reason == null ){
 
1182
                                                
1148
1183
                                                failure_reason = "No data received from tracker";
1149
1184
                                        }
1150
 
                                        
1151
 
                
1152
1185
                                }catch( SSLException e ){
1153
1186
                                        
1154
1187
                                        // e.printStackTrace();
1419
1452
        announceUDP(
1420
1453
                URL                                             reqUrl,
1421
1454
                ByteArrayOutputStream   message,
1422
 
                boolean                 do_auth_test)
 
1455
                boolean                 is_probe )
1423
1456
        
1424
1457
                throws IOException
1425
1458
        {
 
1459
                long timeout = is_probe?10000:PRUDPPacket.DEFAULT_UDP_TIMEOUT;
 
1460
                
1426
1461
                reqUrl = TRTrackerUtils.adjustURLForHosting( reqUrl );
1427
1462
 
1428
1463
                String  failure_reason = null;
1430
1465
                PasswordAuthentication  auth = null;    
1431
1466
                
1432
1467
                try{
1433
 
                        if (do_auth_test && UrlUtils.queryHasParameter(reqUrl.getQuery(), "auth", false)) {
 
1468
                        if ( (!is_probe) && UrlUtils.queryHasParameter(reqUrl.getQuery(), "auth", false)) {
1434
1469
                                 auth = SESecurityManager.getPasswordAuthentication( UDP_REALM, reqUrl );
1435
1470
                        }
1436
1471
                                                
1444
1479
                        
1445
1480
                                        PRUDPPacket connect_request = new PRUDPPacketRequestConnect();
1446
1481
                                        
1447
 
                                        PRUDPPacket reply = handler.sendAndReceive( auth, connect_request, destination );
 
1482
                                        PRUDPPacket reply = handler.sendAndReceive( auth, connect_request, destination, timeout );
1448
1483
                                        
1449
1484
                                        if ( reply.getAction() == PRUDPPacketTracker.ACT_REPLY_CONNECT ){
1450
1485
                                        
2903
2938
                                                        throw( new IOException( "peers missing from response" ));
2904
2939
                                        }
2905
2940
                                    }else if (!metaData.containsKey("peers6")){ 
2906
 
                                        // we got nothing useful under peers and no peers6 either
 
2941
                                        
 
2942
                                                //      we got nothing useful under peers and no peers6 either
 
2943
                                        
2907
2944
                                                throw( new IOException( "peers missing from response" ));
2908
2945
                                    }
2909
2946
                                
2910
2947
                                
2911
2948
                                final byte[] v6peers = (byte[])metaData.get("peers6");
2912
 
                                    if (v6peers != null)
2913
 
                                        {
2914
 
                                        // 16 bytes for v6 + 2 bytes for port
 
2949
                                
 
2950
                                    if ( v6peers != null ){
 
2951
                                        
 
2952
                                                // 16 bytes for v6 + 2 bytes for port
 
2953
                                        
2915
2954
                                                final int entry_size = 18;
2916
 
 
2917
 
                                                
 
2955
                
2918
2956
                                                final byte[] rawAddr = new byte[16]; 
2919
2957
                                                
2920
 
                                                for (int i = 0; i <= v6peers.length - entry_size; i += entry_size)
2921
 
                                                {
2922
 
                                                        System.arraycopy(v6peers, i, rawAddr, 0, 16);
 
2958
                                                for (int i=0; i<v6peers.length; i+=entry_size ){
 
2959
                                                        
 
2960
                                                        System.arraycopy( v6peers, i, rawAddr, 0, 16 );
 
2961
                                                        
2923
2962
                                                        String ip = InetAddress.getByAddress(rawAddr).getHostAddress();
2924
 
                                                        int tcp_port = rawAddr[i+16] * 256 + rawAddr[i+17];
2925
 
                                                        if (tcp_port < 0 || tcp_port > 65535)
2926
 
                                                        {
2927
 
                                                                if (Logger.isEnabled())
 
2963
                                                        
 
2964
                                                int     po1 = 0xFF & v6peers[i+16];
 
2965
                                                int     po2 = 0xFF & v6peers[i+17];
 
2966
 
 
2967
                                                        int tcp_port = po1*256 + po2;
 
2968
                                                        
 
2969
                                                        if (tcp_port < 0 || tcp_port > 65535){
 
2970
                                                        
 
2971
                                                                if (Logger.isEnabled()){
 
2972
                                                                        
2928
2973
                                                                        Logger.log(new LogEvent(torrent, LOGID, LogEvent.LT_ERROR, "Invalid compactv6 peer port given: " + ip + ": " + tcp_port));
 
2974
                                                                }
 
2975
                                                                
2929
2976
                                                                continue;
2930
2977
                                                        }
2931
 
                                                        byte[] peer_peer_id = getAnonymousPeerId(ip, tcp_port);
 
2978
                                                        
 
2979
                                                        byte[] peer_peer_id = getAnonymousPeerId( ip, tcp_port );
 
2980
                                                        
2932
2981
                                                        short protocol = DownloadAnnounceResultPeer.PROTOCOL_NORMAL;
 
2982
                                                        
2933
2983
                                                        TRTrackerAnnouncerResponsePeerImpl peer = new TRTrackerAnnouncerResponsePeerImpl(PEPeerSource.PS_BT_TRACKER, peer_peer_id, ip, tcp_port, 0, 0, protocol, TRTrackerAnnouncer.AZ_TRACKER_VERSION_1, (short) 0);
2934
 
                                                        if (Logger.isEnabled())
 
2984
                                                        
 
2985
                                                        if (Logger.isEnabled()){
 
2986
                                                                
2935
2987
                                                                Logger.log(new LogEvent(torrent, LOGID, "COMPACTv6 PEER: " + peer.getString()));
 
2988
                                                        }
 
2989
                                                        
2936
2990
                                                        valid_meta_peers.add(peer);
2937
2991
                                                }
2938
2992
                                        }