FileDocCategorySizeDatePackage
UniAddress.javaAPI DocJCIFS 1.3.17 API16953Tue Oct 18 15:26:24 BST 2011jcifs

UniAddress

public class UniAddress extends Object

Under normal conditions it is not necessary to use this class to use jCIFS properly. Name resolusion is handled internally to the jcifs.smb package.

This class is a wrapper for both {@link jcifs.netbios.NbtAddress} and {@link java.net.InetAddress}. The name resolution mechanisms used will systematically query all available configured resolution services including WINS, broadcasts, DNS, and LMHOSTS. See Setting Name Resolution Properties and the jcifs.resolveOrder property. Changing jCIFS name resolution properties can greatly affect the behavior of the client and may be necessary for proper operation.

This class should be used in favor of InetAddress to resolve hostnames on LANs and WANs that support a mixture of NetBIOS/WINS and DNS resolvable hosts.

Fields Summary
private static final int
RESOLVER_WINS
private static final int
RESOLVER_BCAST
private static final int
RESOLVER_DNS
private static final int
RESOLVER_LMHOSTS
private static int[]
resolveOrder
private static InetAddress
baddr
private static jcifs.util.LogStream
log
Object
addr
Perform DNS SRV lookup on successively shorter suffixes of name and return successful suffix or throw an UnknownHostException. import javax.naming.*; import javax.naming.directory.*; public static String getDomainByName(String name) throws UnknownHostException { DirContext context; UnknownHostException uhe = null; try { context = new InitialDirContext(); for ( ;; ) { try { Attributes attributes = context.getAttributes( "dns:/_ldap._tcp.dc._msdcs." + name, new String[] { "SRV" } ); return name; } catch (NameNotFoundException nnfe) { uhe = new UnknownHostException(nnfe.getMessage()); } int dot = name.indexOf('.'); if (dot == -1) break; name = name.substring(dot + 1); } } catch (NamingException ne) { if (log.level > 1) ne.printStackTrace(log); } throw uhe != null ? uhe : new UnknownHostException("invalid name"); }
String
calledName
Constructors Summary
public UniAddress(Object addr)
Create a UniAddress by wrapping an InetAddress or NbtAddress.

        if( addr == null ) {
            throw new IllegalArgumentException();
        }
        this.addr = addr;
    
Methods Summary
public booleanequals(java.lang.Object obj)
Compare two addresses for equality. Two UniAddresss are equal if they are both UniAddress' and refer to the same IP address.

        return obj instanceof UniAddress && addr.equals(((UniAddress)obj).addr);
    
public java.lang.StringfirstCalledName()
Guess first called name to try for session establishment. This method is used exclusively by the jcifs.smb package.

        if( addr instanceof NbtAddress ) {
            return ((NbtAddress)addr).firstCalledName();
        } else {
            calledName = ((InetAddress)addr).getHostName();
            if( isDotQuadIP( calledName )) {
                calledName = NbtAddress.SMBSERVER_NAME;
            } else {
                int i = calledName.indexOf( '." );
                if( i > 1 && i < 15 ) {
                    calledName = calledName.substring( 0, i ).toUpperCase();
                } else if( calledName.length() > 15 ) {
                    calledName = NbtAddress.SMBSERVER_NAME;
                } else {
                    calledName = calledName.toUpperCase();
                }
            }
        }

        return calledName;
    
public java.lang.ObjectgetAddress()
Return the underlying NbtAddress or InetAddress.

        return addr;
    
public static jcifs.UniAddress[]getAllByName(java.lang.String hostname, boolean possibleNTDomainOrWorkgroup)

        Object addr;
        int i;

        if( hostname == null || hostname.length() == 0 ) {
            throw new UnknownHostException();
        }

        if( isDotQuadIP( hostname )) {
            UniAddress[] addrs = new UniAddress[1];
            addrs[0] = new UniAddress( NbtAddress.getByName( hostname ));
            return addrs;
        }

        for( i = 0; i < resolveOrder.length; i++ ) {
            try {
                switch( resolveOrder[i] ) {
                    case RESOLVER_LMHOSTS:
                        if(( addr = Lmhosts.getByName( hostname )) == null ) {
                            continue;
                        }
                        break;
                    case RESOLVER_WINS:
                        if( hostname == NbtAddress.MASTER_BROWSER_NAME ||
                                                    hostname.length() > 15 ) {
                                                    // invalid netbios name
                            continue;
                        }
                        if( possibleNTDomainOrWorkgroup ) {
                            addr = lookupServerOrWorkgroup( hostname, NbtAddress.getWINSAddress() );
                        } else {
                            addr = NbtAddress.getByName( hostname, 0x20, null, NbtAddress.getWINSAddress() );
                        }
                        break;
                    case RESOLVER_BCAST:
                        if( hostname.length() > 15 ) {
                            // invalid netbios name
                            continue;
                        }
                        if( possibleNTDomainOrWorkgroup ) {
                            addr = lookupServerOrWorkgroup( hostname, baddr );
                        } else {
                            addr = NbtAddress.getByName( hostname, 0x20, null, baddr );
                        }
                        break;
                    case RESOLVER_DNS:
                        if( isAllDigits( hostname )) {
                            throw new UnknownHostException( hostname );
                        }
                        InetAddress[] iaddrs = InetAddress.getAllByName( hostname );
                        UniAddress[] addrs = new UniAddress[iaddrs.length];
                        for (int ii = 0; ii < iaddrs.length; ii++) {
                            addrs[ii] = new UniAddress(iaddrs[ii]);
                        }
                        return addrs; // Success
                    default:
                        throw new UnknownHostException( hostname );
                }
                UniAddress[] addrs = new UniAddress[1];
                addrs[0] = new UniAddress( addr );
                return addrs; // Success
            } catch( IOException ioe ) {
                // Failure
            }
        }
        throw new UnknownHostException( hostname );
    
public static jcifs.UniAddressgetByName(java.lang.String hostname)
Determines the address of a host given it's host name. The name can be a machine name like "jcifs.samba.org", or an IP address like "192.168.1.15".

param
hostname NetBIOS or DNS hostname to resolve
throws
java.net.UnknownHostException if there is an error resolving the name

        return getByName( hostname, false );
    
public static jcifs.UniAddressgetByName(java.lang.String hostname, boolean possibleNTDomainOrWorkgroup)
Lookup hostname and return it's UniAddress. If the possibleNTDomainOrWorkgroup parameter is true an addtional name query will be performed to locate a master browser.

        UniAddress[] addrs = UniAddress.getAllByName(hostname, possibleNTDomainOrWorkgroup);
        return addrs[0];
    
public java.lang.StringgetHostAddress()
Return the IP address as text such as "192.168.1.15".

        if( addr instanceof NbtAddress ) {
            return ((NbtAddress)addr).getHostAddress();
        }
        return ((InetAddress)addr).getHostAddress();
    
public java.lang.StringgetHostName()
Return the hostname of this address such as "MYCOMPUTER".

        if( addr instanceof NbtAddress ) {
            return ((NbtAddress)addr).getHostName();
        }
        return ((InetAddress)addr).getHostName();
    
public inthashCode()
Return the IP address of this address as a 32 bit integer.

        return addr.hashCode();
    
static booleanisAllDigits(java.lang.String hostname)

        for (int i = 0; i < hostname.length(); i++) {
            if (Character.isDigit( hostname.charAt( i )) == false) {
                return false;
            }
        }
        return true;
    
static booleanisDotQuadIP(java.lang.String hostname)

        if( Character.isDigit( hostname.charAt( 0 ))) {
            int i, len, dots;
            char[] data;

            i = dots = 0;                    /* quick IP address validation */
            len = hostname.length();
            data = hostname.toCharArray();
            while( i < len && Character.isDigit( data[i++] )) {
                if( i == len && dots == 3 ) {
                    // probably an IP address
                    return true;
                }
                if( i < len && data[i] == '." ) {
                    dots++;
                    i++;
                }
            }
        }

        return false;
    
static jcifs.netbios.NbtAddresslookupServerOrWorkgroup(java.lang.String name, java.net.InetAddress svr)

        Sem sem = new Sem( 2 );
        int type = NbtAddress.isWINS( svr ) ? 0x1b : 0x1d;

        QueryThread q1x = new QueryThread( sem, name, type, null, svr );
        QueryThread q20 = new QueryThread( sem, name, 0x20, null, svr );
        q1x.setDaemon( true );
        q20.setDaemon( true );
        try {
            synchronized( sem ) {
                q1x.start();
                q20.start();

                while( sem.count > 0 && q1x.ans == null && q20.ans == null ) {
                    sem.wait();
                }
            }
        } catch( InterruptedException ie ) {
            throw new UnknownHostException( name );
        }
        if( q1x.ans != null ) {
            return q1x.ans;
        } else if( q20.ans != null ) {
            return q20.ans;
        } else {
            throw q1x.uhe;
        }
    
public java.lang.StringnextCalledName()
Guess next called name to try for session establishment. This method is used exclusively by the jcifs.smb package.

        if( addr instanceof NbtAddress ) {
            return ((NbtAddress)addr).nextCalledName();
        } else if( calledName != NbtAddress.SMBSERVER_NAME ) {
            calledName = NbtAddress.SMBSERVER_NAME;
            return calledName;
        }
        return null;
    
public java.lang.StringtoString()
Return the a text representation of this address such as MYCOMPUTER/192.168.1.15.

        return addr.toString();