PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/azureus-4.7.0.2/com/aelitis/azureus/core/networkmanager/admin/impl/NetworkAdminUDPTester.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 271 lines | 146 code | 82 blank | 43 comment | 15 complexity | 7d40fc33283b6fbd2e10be8bd8b1d1be MD5 | raw file
  1. /*
  2. * Created on 1 Nov 2006
  3. * Created by Paul Gardner
  4. * Copyright (C) 2006 Aelitis, All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * AELITIS, SAS au capital de 63.529,40 euros
  19. * 8 Allee Lenotre, La Grille Royale, 78600 Le Mesnil le Roi, France.
  20. *
  21. */
  22. package com.aelitis.azureus.core.networkmanager.admin.impl;
  23. import java.net.InetAddress;
  24. import java.net.InetSocketAddress;
  25. import java.util.*;
  26. import org.gudy.azureus2.core3.internat.MessageText;
  27. import org.gudy.azureus2.core3.util.Constants;
  28. import org.gudy.azureus2.plugins.PluginInterface;
  29. import com.aelitis.azureus.core.AzureusCore;
  30. import com.aelitis.azureus.core.networkmanager.admin.NetworkAdmin;
  31. import com.aelitis.azureus.core.networkmanager.admin.NetworkAdminASN;
  32. import com.aelitis.azureus.core.networkmanager.admin.NetworkAdminException;
  33. import com.aelitis.azureus.core.networkmanager.admin.NetworkAdminProgressListener;
  34. import com.aelitis.azureus.core.versioncheck.VersionCheckClient;
  35. import com.aelitis.azureus.plugins.upnp.UPnPPlugin;
  36. import com.aelitis.azureus.plugins.upnp.UPnPPluginService;
  37. import com.aelitis.net.udp.uc.PRUDPPacketHandler;
  38. import com.aelitis.net.udp.uc.PRUDPPacketHandlerFactory;
  39. import com.aelitis.net.udp.uc.PRUDPReleasablePacketHandler;
  40. public class
  41. NetworkAdminUDPTester
  42. implements NetworkAdminProtocolTester
  43. {
  44. public static final String UDP_SERVER_ADDRESS = Constants.NAT_TEST_SERVER;
  45. public static final int UDP_SERVER_PORT = 2081; // 2084;
  46. static{
  47. NetworkAdminNATUDPCodecs.registerCodecs();
  48. }
  49. private AzureusCore core;
  50. private NetworkAdminProgressListener listener;
  51. protected
  52. NetworkAdminUDPTester(
  53. AzureusCore _core,
  54. NetworkAdminProgressListener _listener )
  55. {
  56. core = _core;
  57. listener = _listener;
  58. }
  59. public InetAddress
  60. testOutbound(
  61. InetAddress bind_ip,
  62. int bind_port )
  63. throws NetworkAdminException
  64. {
  65. try{
  66. return( VersionCheckClient.getSingleton().getExternalIpAddressUDP(bind_ip, bind_port,false));
  67. }catch( Throwable e ){
  68. throw( new NetworkAdminException( "Outbound test failed", e ));
  69. }
  70. }
  71. public InetAddress
  72. testInbound(
  73. InetAddress bind_ip,
  74. int bind_port )
  75. throws NetworkAdminException
  76. {
  77. PRUDPReleasablePacketHandler handler = PRUDPPacketHandlerFactory.getReleasableHandler( bind_port );
  78. PRUDPPacketHandler packet_handler = handler.getHandler();
  79. HashMap data_to_send = new HashMap();
  80. PluginInterface pi_upnp = core.getPluginManager().getPluginInterfaceByClass( UPnPPlugin.class );
  81. String upnp_str = null;
  82. if( pi_upnp != null ) {
  83. UPnPPlugin upnp = (UPnPPlugin)pi_upnp.getPlugin();
  84. /*
  85. UPnPMapping mapping = upnp.getMapping( true, port );
  86. if ( mapping == null ) {
  87. new_mapping = mapping = upnp.addMapping( "NAT Tester", true, port, true );
  88. // give UPnP a chance to work
  89. try {
  90. Thread.sleep( 500 );
  91. }
  92. catch (Throwable e) {
  93. Debug.printStackTrace( e );
  94. }
  95. }
  96. */
  97. UPnPPluginService[] services = upnp.getServices();
  98. if ( services.length > 0 ){
  99. upnp_str = "";
  100. for (int i=0;i<services.length;i++){
  101. UPnPPluginService service = services[i];
  102. upnp_str += (i==0?"":",") + service.getInfo();
  103. }
  104. }
  105. }
  106. if ( upnp_str != null ){
  107. data_to_send.put( "upnp", upnp_str );
  108. }
  109. NetworkAdminASN net_asn = NetworkAdmin.getSingleton().getCurrentASN();
  110. String as = net_asn.getAS();
  111. String asn = net_asn.getASName();
  112. if ( as.length() > 0 ){
  113. data_to_send.put( "as", as );
  114. }
  115. if ( asn.length() > 0 ){
  116. data_to_send.put( "asn", asn );
  117. }
  118. data_to_send.put( "locale", MessageText.getCurrentLocale().toString());
  119. Random random = new Random();
  120. data_to_send.put( "id", new Long( random.nextLong()));
  121. try{
  122. packet_handler.setExplicitBindAddress( bind_ip );
  123. Throwable last_error = null;
  124. long timeout = 5000;
  125. long timeout_inc = 5000;
  126. try{
  127. for (int i=0;i<3;i++){
  128. data_to_send.put( "seq", new Long(i));
  129. try{
  130. // connection ids for requests must always have their msb set...
  131. // apart from the original darn udp tracker spec....
  132. long connection_id = 0x8000000000000000L | random.nextLong();
  133. NetworkAdminNATUDPRequest request_packet = new NetworkAdminNATUDPRequest( connection_id );
  134. request_packet.setPayload( data_to_send );
  135. listener.reportProgress( "Sending outbound packet and waiting for reply probe (timeout=" + timeout + ")" );
  136. NetworkAdminNATUDPReply reply_packet =
  137. (NetworkAdminNATUDPReply)packet_handler.sendAndReceive(
  138. null,
  139. request_packet,
  140. new InetSocketAddress( UDP_SERVER_ADDRESS, UDP_SERVER_PORT ),
  141. timeout,
  142. PRUDPPacketHandler.PRIORITY_IMMEDIATE );
  143. Map reply = reply_packet.getPayload();
  144. byte[] ip_bytes = (byte[])reply.get( "ip_address" );
  145. if ( ip_bytes == null ){
  146. throw( new NetworkAdminException( "IP address missing in reply" ));
  147. }
  148. byte[] reason = (byte[])reply.get( "reason" );
  149. if ( reason != null ) {
  150. throw( new NetworkAdminException( new String( reason, "UTF8")));
  151. }
  152. return( InetAddress.getByAddress( ip_bytes ));
  153. }catch( Throwable e){
  154. last_error = e;
  155. timeout += timeout_inc;
  156. }
  157. }
  158. if ( last_error != null ){
  159. throw( last_error );
  160. }
  161. throw( new NetworkAdminException( "Timeout" ));
  162. }finally{
  163. try{
  164. data_to_send.put( "seq", new Long(99));
  165. long connection_id = 0x8000000000000000L | random.nextLong();
  166. NetworkAdminNATUDPRequest request_packet = new NetworkAdminNATUDPRequest( connection_id );
  167. request_packet.setPayload( data_to_send );
  168. // fire off one last packet in attempt to inform server of completion
  169. listener.reportProgress( "Sending completion event" );
  170. packet_handler.send( request_packet, new InetSocketAddress( UDP_SERVER_ADDRESS, UDP_SERVER_PORT ));
  171. }catch( Throwable e){
  172. }
  173. }
  174. }catch( NetworkAdminException e ){
  175. throw( e );
  176. }catch( Throwable e ){
  177. throw( new NetworkAdminException( "Inbound test failed", e ));
  178. }finally{
  179. packet_handler.setExplicitBindAddress( null );
  180. handler.release();
  181. }
  182. }
  183. }