FileDocCategorySizeDatePackage
HostNameToIPResolver.javaAPI DocAzureus 3.0.3.45769Wed Jun 27 16:16:00 BST 2007org.gudy.azureus2.core3.util

HostNameToIPResolver

public class HostNameToIPResolver extends Object
author
parg

Fields Summary
protected static Thread
resolver_thread
protected static List
request_queue
protected static AEMonitor
request_queue_mon
protected static AESemaphore
request_semaphore
static final int
INADDRSZ
Constructors Summary
Methods Summary
public static voidaddResolverRequest(java.lang.String host, HostNameToIPResolverListener l)

		byte[]	bytes = textToNumericFormat( host );
				
		if ( bytes != null ){
		
			try{
				l.hostNameResolutionComplete( InetAddress.getByAddress( host, bytes ));
			
				return;
				
			}catch( UnknownHostException e ){
			}
		}
		
		try{
			request_queue_mon.enter();
			
			request_queue.add( new request( host, l ));
			
			request_semaphore.release();
			
			if ( resolver_thread == null ){
				
				resolver_thread = 
					new AEThread("HostNameToIPResolver")
					{
						public void
						runSupport()
						{
							while(true){
								
								try{
									request_semaphore.reserve();
									
									request	req;
									
									try{
										request_queue_mon.enter();
										
										req	= (request)request_queue.remove(0);
										
									}finally{
										
										request_queue_mon.exit();
									}
									
									try{
										InetAddress addr = syncResolve( req.getHost());
										
										req.getListener().hostNameResolutionComplete( addr );
											
									}catch( Throwable e ){
										
										req.getListener().hostNameResolutionComplete( null );
										
									}
								}catch( Throwable e ){
									
									Debug.printStackTrace( e );
								}
							}
						}
					};
					
				resolver_thread.setDaemon( true );	
					
				resolver_thread.start();
			}
		}finally{
			
			request_queue_mon.exit();
		}
	
public static byte[]hostAddressToBytes(java.lang.String host)

		byte[] res = textToNumericFormat( host );
		
		return( res );
	
public static booleanisNonDNSName(java.lang.String host)

	

	
	  
	
			 
	
		return( AENetworkClassifier.categoriseAddress( host ) != AENetworkClassifier.AT_PUBLIC );
	
public static java.net.InetAddresssyncResolve(java.lang.String host)

		if ( isNonDNSName( host )){
			
			throw( new HostNameToIPResolverException( "non-DNS name '" + host + "'", true ));
		}
		
			// handle any raw addresses up front
		
		byte[]	bytes = textToNumericFormat( host );

		if ( bytes != null ){
			
			return( InetAddress.getByAddress( bytes ));
		}
		
			// filter out partially complete raw addresses by applying the basic rule in ftp://ftp.is.co.za/rfc/rfc3696.txt
			// at least one dot + not all numeric
		
		char[]	chars = host.toCharArray();
		
		boolean	resolve = false;
		
		for (int i=0;i<chars.length;i++){
			
			if ( !( chars[i] == '." || Character.isDigit(chars[i]))){
				
				resolve	= true;
				
				break;
			}
		}
		
		if ( resolve ){
			
			return( InetAddress.getByName( host));
			
		}else{
						
			throw( new UnknownHostException( "Host '" + host + "' doesn't obey minimal validation rules"));
		}
	
static byte[]textToNumericFormat(java.lang.String src)

	
	  
	
		  
	
		if (src.length() == 0) {
		    return null;
		}
	
		if ( src.indexOf(':") != -1 ){
		
				// v6
			try{
				return( InetAddress.getByName(src).getAddress());
				
			}catch( Throwable e ){
				
				return( null );
			}
		}
		
		int octets;
		char ch;
		byte[] dst = new byte[INADDRSZ];
	
		char[] srcb = src.toCharArray();
		boolean saw_digit = false;

		octets = 0;
		int i = 0;
		int cur = 0;
		while (i < srcb.length) {
		    ch = srcb[i++];
		    if (Character.isDigit(ch)) {
			// note that Java byte is signed, so need to convert to int
			int sum = (dst[cur] & 0xff)*10
			    + (Character.digit(ch, 10) & 0xff);
			
			if (sum > 255)
			    return null;

			dst[cur] = (byte)(sum & 0xff);
			if (! saw_digit) {
			    if (++octets > INADDRSZ)
				return null;
			    saw_digit = true;
			}
		    } else if (ch == '." && saw_digit) {
			if (octets == INADDRSZ)
			    return null;
			cur++;
			dst[cur] = 0;
			saw_digit = false;
		    } else
			return null;
		}
		if (octets < INADDRSZ)
		    return null;
		return dst;