FileDocCategorySizeDatePackage
NetworkAdminUDPTester.javaAPI DocAzureus 3.0.3.46468Wed Jul 11 22:48:30 BST 2007com.aelitis.azureus.core.networkmanager.admin.impl

NetworkAdminUDPTester

public class NetworkAdminUDPTester extends Object implements NetworkAdminProtocolTester

Fields Summary
public static final String
UDP_SERVER_ADDRESS
public static final int
UDP_SERVER_PORT
private com.aelitis.azureus.core.AzureusCore
core
Constructors Summary
protected NetworkAdminUDPTester(com.aelitis.azureus.core.AzureusCore _core)

		core	= _core;
	
Methods Summary
public java.net.InetAddresstestInbound(java.net.InetAddress bind_ip, int bind_port)

		PRUDPReleasablePacketHandler handler = PRUDPPacketHandlerFactory.getReleasableHandler( bind_port );

		PRUDPPacketHandler	packet_handler = handler.getHandler();

		long timeout = 5000;

		HashMap	data_to_send = new HashMap();

		PluginInterface pi_upnp = core.getPluginManager().getPluginInterfaceByClass( UPnPPlugin.class );

		String	upnp_str = null;

		if( pi_upnp != null ) {

			UPnPPlugin upnp = (UPnPPlugin)pi_upnp.getPlugin();

			/*
			UPnPMapping mapping = upnp.getMapping( true, port );

			if ( mapping == null ) {

				new_mapping = mapping = upnp.addMapping( "NAT Tester", true, port, true );

				// give UPnP a chance to work

				try {
					Thread.sleep( 500 );

				}
				catch (Throwable e) {

					Debug.printStackTrace( e );
				}
			}
			*/
			
			UPnPPluginService[]	services = upnp.getServices();

			if ( services.length > 0 ){

				upnp_str = "";

				for (int i=0;i<services.length;i++){

					UPnPPluginService service = services[i];

					upnp_str += (i==0?"":",") + service.getInfo();
				}
			}
		}

		if ( upnp_str != null ){

			data_to_send.put( "upnp", upnp_str );
		}

	    NetworkAdminASN net_asn = NetworkAdmin.getSingleton().getCurrentASN();

		String	as 	= net_asn.getAS();
		String	asn = net_asn.getASName();

		if ( as.length() > 0 ){

			data_to_send.put( "as", as );
		}
		
		if ( asn.length() > 0 ){

			data_to_send.put( "asn", asn );
		}

		data_to_send.put( "locale", MessageText.getCurrentLocale().toString());

		Random 	random = new Random();

		data_to_send.put( "id", new Long( random.nextLong()));		  

		try{
			packet_handler.setExplicitBindAddress( bind_ip );	  

			Throwable last_error = null;

			final int tries = 3;

			try{
				for (int i=0;i<tries;i++){

					data_to_send.put( "seq", new Long(i));

					try{

						// connection ids for requests must always have their msb set...
						// apart from the original darn udp tracker spec....

						long connection_id = 0x8000000000000000L | random.nextLong();

						NetworkAdminNATUDPRequest	request_packet = new NetworkAdminNATUDPRequest( connection_id );

						request_packet.setPayload( data_to_send );

						NetworkAdminNATUDPReply reply_packet = (NetworkAdminNATUDPReply)packet_handler.sendAndReceive( null, request_packet, new InetSocketAddress( UDP_SERVER_ADDRESS, UDP_SERVER_PORT ), timeout );

						Map	reply = reply_packet.getPayload();

						byte[]	ip_bytes = (byte[])reply.get( "ip_address" );

						if ( ip_bytes == null ){

							throw( new NetworkAdminException( "IP address missing in reply" ));
						}

						byte[] reason = (byte[])reply.get( "reason" );

						if ( reason != null ) {

							throw( new NetworkAdminException( new String( reason, "UTF8")));
						}

						return( InetAddress.getByAddress( ip_bytes ));

					}catch( Throwable e){

						last_error	= e;

						timeout = timeout * 2;
					}
				}

				if ( last_error != null ){

					throw( last_error );
				}

				throw( new NetworkAdminException( "Timeout" ));

			}finally{

				try{
					data_to_send.put( "seq", new Long(99));

					long connection_id = 0x8000000000000000L | random.nextLong();

					NetworkAdminNATUDPRequest	request_packet = new NetworkAdminNATUDPRequest( connection_id );

					request_packet.setPayload( data_to_send );

					// fire off one last packet in attempt to inform server of completion

					packet_handler.send( request_packet, new InetSocketAddress( UDP_SERVER_ADDRESS, UDP_SERVER_PORT ));

				}catch( Throwable e){  
				}
			}
		}catch( NetworkAdminException e ){

			throw( e );

		}catch( Throwable e ){

			throw( new NetworkAdminException( "Inbound check failed", e ));

		}finally{

			packet_handler.setExplicitBindAddress( null );

			handler.release();
		}
	
public java.net.InetAddresstestOutbound(java.net.InetAddress bind_ip, int bind_port)

		try{
			return( VersionCheckClient.getSingleton().getExternalIpAddressUDP(bind_ip, bind_port,false));
			
		}catch( Throwable e ){
		
			throw( new NetworkAdminException( "Outbound check failed", e ));
		}