📄 nbtaddress.java
字号:
this.macAddress = macAddress;
isDataFromNodeStatus = true;
}
/* Guess next called name to try for session establishment. These
* methods are used by the smb package.
*/
public String firstCalledName() {
calledName = hostName.name;
if( Character.isDigit( calledName.charAt( 0 ))) {
int i, len, dots;
char[] data;
i = dots = 0; /* quick IP address validation */
len = calledName.length();
data = calledName.toCharArray();
while( i < len && Character.isDigit( data[i++] )) {
if( i == len && dots == 3 ) {
// probably an IP address
calledName = SMBSERVER_NAME;
break;
}
if( i < len && data[i] == '.' ) {
dots++;
i++;
}
}
} else {
switch (hostName.hexCode) {
case 0x1B:
case 0x1C:
case 0x1D:
calledName = SMBSERVER_NAME;
}
}
return calledName;
}
public String nextCalledName() {
if( calledName == hostName.name ) {
calledName = SMBSERVER_NAME;
} else if( calledName == SMBSERVER_NAME ) {
NbtAddress[] addrs;
try {
addrs = CLIENT.getNodeStatus( this );
if( hostName.hexCode == 0x1D ) {
for( int i = 0; i < addrs.length; i++ ) {
if( addrs[i].hostName.hexCode == 0x20 ) {
return addrs[i].hostName.name;
}
}
return null;
} else if( isDataFromNodeStatus ) {
/* 'this' has been updated and should now
* have a real NetBIOS name
*/
calledName = null;
return hostName.name;
}
} catch( UnknownHostException uhe ) {
calledName = null;
}
} else {
calledName = null;
}
return calledName;
}
/*
* There are three degrees of state that any NbtAddress can have.
*
* 1) IP Address - If a dot-quad IP string is used with getByName (or used
* to create an NbtAddress internal to this netbios package), no query is
* sent on the wire and the only state this object has is it's IP address
* (but that's enough to connect to a host using *SMBSERVER for CallingName).
*
* 2) IP Address, NetBIOS name, nodeType, groupName - If however a
* legal NetBIOS name string is used a name query request will retreive
* the IP, node type, and whether or not this NbtAddress represents a
* group name. This degree of state can be obtained with a Name Query
* Request or Node Status Request.
*
* 3) All - The NbtAddress will be populated with all state such as mac
* address, isPermanent, isBeingDeleted, ...etc. This information can only
* be retrieved with the Node Status request.
*
* The degree of state that an NbtAddress has is dependant on how it was
* created and what is required of it. The second degree of state is the
* most common. This is the state information that would be retrieved from
* WINS for example. Natrually it is not practical for every NbtAddress
* to be populated will all state requiring a Node Status on every host
* encountered. The below methods allow state to be populated when requested
* in a lazy fashon.
*/
void checkData() throws UnknownHostException {
if( hostName == UNKNOWN_NAME ) {
getAllByAddress( this );
}
}
void checkNodeStatusData() throws UnknownHostException {
if( isDataFromNodeStatus == false ) {
getAllByAddress( this );
}
}
/**
* Determines if the address is a group address. This is also
* known as a workgroup name or group name.
*
* @throws UnknownHostException if the host cannot be resolved to find out.
*/
public boolean isGroupAddress() throws UnknownHostException {
checkData();
return groupName;
}
/**
* Checks the node type of this address.
* @return {@link jcifs.netbios.NbtAddress#B_NODE},
* {@link jcifs.netbios.NbtAddress#P_NODE}, {@link jcifs.netbios.NbtAddress#M_NODE},
* {@link jcifs.netbios.NbtAddress#H_NODE}
*
* @throws UnknownHostException if the host cannot be resolved to find out.
*/
public int getNodeType() throws UnknownHostException {
checkData();
return nodeType;
}
/**
* Determines if this address in the process of being deleted.
*
* @throws UnknownHostException if the host cannot be resolved to find out.
*/
public boolean isBeingDeleted() throws UnknownHostException {
checkNodeStatusData();
return isBeingDeleted;
}
/**
* Determines if this address in conflict with another address.
*
* @throws UnknownHostException if the host cannot be resolved to find out.
*/
public boolean isInConflict() throws UnknownHostException {
checkNodeStatusData();
return isInConflict;
}
/**
* Determines if this address is active.
*
* @throws UnknownHostException if the host cannot be resolved to find out.
*/
public boolean isActive() throws UnknownHostException {
checkNodeStatusData();
return isActive;
}
/**
* Determines if this address is set to be permanent.
*
* @throws UnknownHostException if the host cannot be resolved to find out.
*/
public boolean isPermanent() throws UnknownHostException {
checkNodeStatusData();
return isPermanent;
}
/**
* Retrieves the MAC address of the remote network interface. Samba returns all zeros.
*
* @return the MAC address as an array of six bytes
* @throws UnknownHostException if the host cannot be resolved to
* determine the MAC address.
*/
public byte[] getMacAddress() throws UnknownHostException {
checkNodeStatusData();
return macAddress;
}
/**
* The hostname of this address. If the hostname is null the local machines
* IP address is returned.
*
* @return the text representation of the hostname associated with this address
*/
public String getHostName() {
try {
checkData();
} catch( UnknownHostException uhe ) {
return getHostAddress();
}
return hostName.name;
}
/**
* Returns the raw IP address of this NbtAddress. The result is in network
* byte order: the highest order byte of the address is in getAddress()[0].
*
* @return a four byte array
*/
public byte[] getAddress() {
byte[] addr = new byte[4];
addr[0] = (byte)(( address >>> 24 ) & 0xFF );
addr[1] = (byte)(( address >>> 16 ) & 0xFF );
addr[2] = (byte)(( address >>> 8 ) & 0xFF );
addr[3] = (byte)( address & 0xFF );
return addr;
}
/**
* To convert this address to an <code>InetAddress</code>.
*
* @return the {@link java.net.InetAddress} representation of this address.
*/
public InetAddress getInetAddress() throws UnknownHostException {
return InetAddress.getByName( getHostAddress() );
}
/**
* Returns this IP adress as a {@link java.lang.String} in the form "%d.%d.%d.%d".
*/
public String getHostAddress() {
return (( address >>> 24 ) & 0xFF ) + "." +
(( address >>> 16 ) & 0xFF ) + "." +
(( address >>> 8 ) & 0xFF ) + "." +
(( address >>> 0 ) & 0xFF );
}
/**
* Returned the hex code associated with this name(e.g. 0x20 is for the file service)
*/
public int getNameType() {
return hostName.hexCode;
}
/**
* Returns a hashcode for this IP address. The hashcode comes from the IP address
* and is not generated from the string representation. So because NetBIOS nodes
* can have many names, all names associated with an IP will have the same
* hashcode.
*/
public int hashCode() {
return address;
}
/**
* Determines if this address is equal two another. Only the IP Addresses
* are compared. Similar to the {@link #hashCode} method, the comparison
* is based on the integer IP address and not the string representation.
*/
public boolean equals( Object obj ) {
return ( obj != null ) && ( obj instanceof NbtAddress ) &&
( ((NbtAddress)obj).address == address );
}
/**
* Returns the {@link java.lang.String} representaion of this address.
*/
public String toString() {
return hostName.toString() + "/" + getHostAddress();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -