FileDocCategorySizeDatePackage
NetworkAdminASNLookupImpl.javaAPI DocAzureus 3.0.3.47753Wed Jul 11 22:59:54 BST 2007com.aelitis.azureus.core.networkmanager.admin.impl

NetworkAdminASNLookupImpl

public class NetworkAdminASNLookupImpl extends Object

Fields Summary
private static final String
WHOIS_ADDRESS
private static final int
WHOIS_PORT
private static final int
TIMEOUT
private InetAddress
address
Constructors Summary
protected NetworkAdminASNLookupImpl(InetAddress _address)

	
	 
	
				 
	
		address= _address;
	
Methods Summary
protected NetworkAdminASNImpllookup()

		Debug.outDiagLoggerOnly( "ASN lookup for '" + address + "'" );
		
		return( lookupDNS( address ));
	
protected NetworkAdminASNImpllookupDNS(java.net.InetAddress address)

			// first query for the as
		
		byte[]	bytes = address.getAddress();
		
		String	ip_query	= "origin.asn.cymru.com";
		
		for (int i=0;i<4;i++){
			
			ip_query =  ( bytes[i] & 0xff ) + "." + ip_query;
		}
		
			// "33544 | 64.71.0.0/20 | US | arin | 2006-05-04"

		String	ip_result = lookupDNS( ip_query );
					
		NetworkAdminASNImpl result = 
			processResult( 
				"AS | BGP Prefix | CC | Reg | Date | AS Name" + "\n" + 
				ip_result + " | n/a" );

		String	as = result.getAS();
		
		if ( as.length() > 0 ){
			
				// now query for ASN
			
				// 33544 | US | arin | 2005-01-19 | WILINE - WiLine Networks Inc.
			
			String	asn_query = "AS" + as + ".asn.cymru.com";

			try{
				
				String	asn_result = lookupDNS( asn_query );
				
				if ( asn_result != null ){
					
					int	pos = asn_result.lastIndexOf( '|" );
					
					if ( pos != -1 ){
						
						String asn = asn_result.substring( pos+1 ).trim();
						
						result.setASName( asn );
					}
				}
			}catch( Throwable e ){
				
				Debug.outNoStack( "ASN lookup for '" + asn_query+ "' failed: " + e.getMessage());
			}
		}
		
		return( result );
	
protected java.lang.StringlookupDNS(java.lang.String query)

		DirContext context = null;
		
		try{
			Hashtable env = new Hashtable();
			
			env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
			
			context = new InitialDirContext(env);
			
			Attributes attrs = context.getAttributes( query, new String[]{ "TXT" });
			
			NamingEnumeration n_enum = attrs.getAll();

			while( n_enum.hasMoreElements()){
				
				Attribute	attr =  (Attribute)n_enum.next();

				NamingEnumeration n_enum2 = attr.getAll();
				
				while( n_enum2.hasMoreElements()){
				
					String attribute = (String)n_enum2.nextElement();

					if ( attribute != null ){
						
						attribute = attribute.trim();
						
						if ( attribute.startsWith( "\"" )){
							
							attribute = attribute.substring(1);
						}
						
						if ( attribute.endsWith( "\"" )){
							
							attribute = attribute.substring(0,attribute.length()-1);
						}
						
						if ( attribute.length() > 0 ){
														
							return( attribute );
						}
					}
				}
			}
			
			throw( new NetworkAdminException( "DNS query returned no results" ));
			
		}catch( Throwable e ){
			
			throw( new NetworkAdminException( "DNS query failed", e ));
			
		}finally{
			
			if ( context != null ){
				
				try{
					context.close();
					
				}catch( Throwable e ){
				}
			}
		}
	
protected NetworkAdminASNImpllookupTCP(java.net.InetAddress address)

		try{
			Socket	socket = new Socket();
			
			int	timeout = TIMEOUT;
				
			long	start = SystemTime.getCurrentTime();
			
			socket.connect( new InetSocketAddress( WHOIS_ADDRESS, WHOIS_PORT ), timeout );
		
			long	end = SystemTime.getCurrentTime();
			
			timeout -= (end - start );
			
			if ( timeout <= 0 ){
				
				throw( new NetworkAdminException( "Timeout on connect" ));
				
			}else if ( timeout > TIMEOUT ){
				
				timeout = TIMEOUT;
			}
			
			socket.setSoTimeout( timeout );
			
			try{
				OutputStream	os = socket.getOutputStream();
				
				String	command = "-u -p " + address.getHostAddress() + "\r\n";
				
				os.write( command.getBytes());
				
				os.flush();
				
				InputStream	is = socket.getInputStream();
				
				byte[]	buffer = new byte[1024];
				
				String	result = "";
				
				while( true ){
					
					int	len = is.read( buffer );
					
					if ( len <= 0 ){
						
						break;
					}
					
					result += new String( buffer, 0, len );
				}

				return( processResult( result ));

			}finally{
				
				socket.close();
			}
		}catch( Throwable e ){
			
			throw( new NetworkAdminException( "whois connection failed", e ));
		}	
	
public static voidmain(java.lang.String[] args)

		try{
			
			NetworkAdminASNLookupImpl lookup = new NetworkAdminASNLookupImpl( InetAddress.getByName( "64.71.8.82" ));
			
			System.out.println( lookup.lookup().getString());
			
			
			/*
			InetAddress	test = InetAddress.getByName( "255.71.15.1" );
			
			System.out.println( test + " -> " + matchesCIDR( "255.71.0.0/20", test ));
			*/
		}catch( Throwable e ){
			
			e.printStackTrace();
		}
	
protected NetworkAdminASNImplprocessResult(java.lang.String result)

		StringTokenizer	lines = new StringTokenizer( result, "\n" );

		int	line_number = 0;
		
		List	keywords = new ArrayList();
		
		Map	map = new HashMap();
		
		while( lines.hasMoreTokens()){
			
			String	line = lines.nextToken().trim();
			
			line_number++;
			
			if ( line_number > 2 ){
				
				break;
			}
			
			StringTokenizer	tok = new StringTokenizer( line, "|" );
		
			int	token_number = 0;
			
			while( tok.hasMoreTokens()){
				
				String	token = tok.nextToken().trim();
				
				if ( line_number == 1 ){
					
					keywords.add( token.toLowerCase());
					
				}else{
					
					if ( token_number >= keywords.size()){
						
						break;
						
					}else{
						
						String	kw = (String)keywords.get( token_number );

						map.put( kw, token );
					}
				}
				
				token_number++;
			}
		}
		
		String as 			= (String)map.get( "as" );
		String asn 		= (String)map.get( "as name" );
		String bgp_prefix	= (String)map.get( "bgp prefix" );
		
		if ( bgp_prefix != null ){
			
			int	pos = bgp_prefix.indexOf(' ");
			
			if ( pos != -1 ){
				
				bgp_prefix = bgp_prefix.substring(pos+1).trim();
			}
			
			if ( bgp_prefix.indexOf('/") == -1 ){
				
				bgp_prefix = null;
			}
		}
		
		return( new NetworkAdminASNImpl( as, asn, bgp_prefix ));