mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-24 17:32:48 +00:00
Moving to root below branch label
git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@2005 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.filesys.netbios;
|
||||
|
||||
/**
|
||||
* Name Template Exception Class
|
||||
* <p>
|
||||
* Thrown when a NetBIOS name template contains invalid characters or is too long.
|
||||
*/
|
||||
public class NameTemplateException extends Exception
|
||||
{
|
||||
private static final long serialVersionUID = 3256439188231762230L;
|
||||
|
||||
/**
|
||||
* Default constructor.
|
||||
*/
|
||||
public NameTemplateException()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Class constructor
|
||||
*
|
||||
* @param s java.lang.String
|
||||
*/
|
||||
public NameTemplateException(String s)
|
||||
{
|
||||
super(s);
|
||||
}
|
||||
}
|
672
source/java/org/alfresco/filesys/netbios/NetBIOSDatagram.java
Normal file
672
source/java/org/alfresco/filesys/netbios/NetBIOSDatagram.java
Normal file
@@ -0,0 +1,672 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.filesys.netbios;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import org.alfresco.filesys.util.DataPacker;
|
||||
|
||||
/**
|
||||
* NetBIOS datagram class.
|
||||
*/
|
||||
public class NetBIOSDatagram
|
||||
{
|
||||
// Datagram types
|
||||
|
||||
public static final int DIRECT_UNIQUE = 0x10;
|
||||
public static final int DIRECT_GROUP = 0x11;
|
||||
public static final int BROADCAST = 0x12;
|
||||
public static final int DATAGRAM_ERROR = 0x13;
|
||||
public static final int DATAGRAM_QUERY = 0x14;
|
||||
public static final int POSITIVE_RESP = 0x15;
|
||||
public static final int NEGATIVE_RESP = 0x16;
|
||||
|
||||
// Datagram flags
|
||||
|
||||
public static final int FLG_MOREFRAGMENTS = 0x01;
|
||||
public static final int FLG_FIRSTPKT = 0x02;
|
||||
|
||||
// Default NetBIOS packet buffer size to allocate
|
||||
|
||||
public static final int DEFBUFSIZE = 4096;
|
||||
|
||||
// NetBIOS datagram offsets
|
||||
|
||||
public static final int NB_MSGTYPE = 0;
|
||||
public static final int NB_FLAGS = 1;
|
||||
public static final int NB_DATAGRAMID = 2;
|
||||
public static final int NB_SOURCEIP = 4;
|
||||
public static final int NB_SOURCEPORT = 8;
|
||||
public static final int NB_DATAGRAMLEN = 10;
|
||||
public static final int NB_PKTOFFSET = 12;
|
||||
public static final int NB_FROMNAME = 14;
|
||||
public static final int NB_TONAME = 48;
|
||||
public static final int NB_USERDATA = 82;
|
||||
|
||||
public static final int NB_MINLENGTH = 82;
|
||||
public static final int NB_MINSMBLEN = 100;
|
||||
|
||||
// NetBIOS packet buffer
|
||||
|
||||
protected byte[] m_buf;
|
||||
|
||||
// Next available datagram id
|
||||
|
||||
private static int m_nextId;
|
||||
|
||||
/**
|
||||
* NetBIOS Datagram constructor
|
||||
*/
|
||||
|
||||
public NetBIOSDatagram()
|
||||
{
|
||||
|
||||
// Allocaet a NetBIOS packet buffer
|
||||
|
||||
m_buf = new byte[DEFBUFSIZE];
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new NetBIOS datagram using the specified packet buffer.
|
||||
*
|
||||
* @param pkt byte[]
|
||||
*/
|
||||
public NetBIOSDatagram(byte[] pkt)
|
||||
{
|
||||
m_buf = pkt;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new NetBIOS datagram with the specified buffer size.
|
||||
*
|
||||
* @param bufSize int
|
||||
*/
|
||||
public NetBIOSDatagram(int bufSize)
|
||||
{
|
||||
m_buf = new byte[bufSize];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the next available datagram id.
|
||||
*/
|
||||
public final static synchronized int getNextDatagramId()
|
||||
{
|
||||
|
||||
// Update and return the next available datagram id
|
||||
|
||||
return m_nextId++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the NetBIOS buffer.
|
||||
*
|
||||
* @return byte[]
|
||||
*/
|
||||
public final byte[] getBuffer()
|
||||
{
|
||||
return m_buf;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the datagram id.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public final int getDatagramId()
|
||||
{
|
||||
return DataPacker.getIntelShort(m_buf, NB_DATAGRAMID);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the datagram destination name.
|
||||
*
|
||||
* @return NetBIOSName
|
||||
*/
|
||||
public final NetBIOSName getDestinationName()
|
||||
{
|
||||
|
||||
// Decode the NetBIOS name to a string
|
||||
|
||||
String name = NetBIOSSession.DecodeName(m_buf, NB_TONAME + 1);
|
||||
if (name != null)
|
||||
{
|
||||
|
||||
// Convert the name string to a NetBIOS name
|
||||
|
||||
NetBIOSName nbName = new NetBIOSName(name.substring(0, 14), name.charAt(15), false);
|
||||
if (getMessageType() == DIRECT_GROUP)
|
||||
nbName.setGroup(true);
|
||||
return nbName;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the datagram flags value.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public final int getFlags()
|
||||
{
|
||||
return m_buf[NB_FLAGS] & 0xFF;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the datagram length.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public final int getLength()
|
||||
{
|
||||
return DataPacker.getShort(m_buf, NB_DATAGRAMLEN);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the user data length
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public final int getDataLength()
|
||||
{
|
||||
return getLength() - NB_USERDATA;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the NetBIOS datagram message type.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public final int getMessageType()
|
||||
{
|
||||
return m_buf[NB_MSGTYPE] & 0xFF;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the datagram source IP address.
|
||||
*
|
||||
* @return byte[]
|
||||
*/
|
||||
public final byte[] getSourceIPAddress()
|
||||
{
|
||||
|
||||
// Allocate a 4 byte array for the IP address
|
||||
|
||||
byte[] ipaddr = new byte[4];
|
||||
|
||||
// Copy the IP address bytes from the datagram
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
ipaddr[i] = m_buf[NB_SOURCEIP + i];
|
||||
|
||||
// Return the IP address bytes
|
||||
|
||||
return ipaddr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the datagram source IP address, as a string
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public final String getSourceAddress()
|
||||
{
|
||||
|
||||
// Get the IP address
|
||||
|
||||
byte[] addr = getSourceIPAddress();
|
||||
|
||||
// Build the IP address string
|
||||
|
||||
StringBuffer addrStr = new StringBuffer();
|
||||
|
||||
addrStr.append(addr[0]);
|
||||
addrStr.append(".");
|
||||
addrStr.append(addr[1]);
|
||||
addrStr.append(".");
|
||||
addrStr.append(addr[2]);
|
||||
addrStr.append(".");
|
||||
addrStr.append(addr[3]);
|
||||
|
||||
return addrStr.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the source NetBIOS name.
|
||||
*
|
||||
* @return java.lang.String
|
||||
*/
|
||||
public final NetBIOSName getSourceName()
|
||||
{
|
||||
|
||||
// Decode the NetBIOS name string
|
||||
|
||||
String name = NetBIOSSession.DecodeName(m_buf, NB_FROMNAME + 1);
|
||||
|
||||
// Convert the name to a NetBIOS name
|
||||
|
||||
if (name != null)
|
||||
{
|
||||
|
||||
// Convert the name string to a NetBIOS name
|
||||
|
||||
NetBIOSName nbName = new NetBIOSName(name.substring(0, 14), name.charAt(15), false);
|
||||
return nbName;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the source port/socket for the datagram.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public final int getSourcePort()
|
||||
{
|
||||
return DataPacker.getIntelShort(m_buf, NB_SOURCEPORT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the user data is an SMB packet
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public final boolean isSMBData()
|
||||
{
|
||||
if (m_buf[NB_USERDATA] == (byte) 0xFF && m_buf[NB_USERDATA + 1] == (byte) 'S'
|
||||
&& m_buf[NB_USERDATA + 2] == (byte) 'M' && m_buf[NB_USERDATA + 3] == (byte) 'B'
|
||||
&& getLength() >= NB_MINSMBLEN)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the message type as a string
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
|
||||
public final String getMessageTypeString()
|
||||
{
|
||||
|
||||
// Determine the message type
|
||||
|
||||
String typ = null;
|
||||
|
||||
switch (getMessageType())
|
||||
{
|
||||
case DIRECT_GROUP:
|
||||
typ = "DIRECT GROUP";
|
||||
break;
|
||||
case DIRECT_UNIQUE:
|
||||
typ = "DIRECT UNIQUE";
|
||||
break;
|
||||
case DATAGRAM_ERROR:
|
||||
typ = "DATAGRAM ERROR";
|
||||
break;
|
||||
case DATAGRAM_QUERY:
|
||||
typ = "DATAGRAM QUERY";
|
||||
break;
|
||||
case BROADCAST:
|
||||
typ = "BROADCAST";
|
||||
break;
|
||||
case POSITIVE_RESP:
|
||||
typ = "POSITIVE RESP";
|
||||
break;
|
||||
case NEGATIVE_RESP:
|
||||
typ = "NEGATIVE RESP";
|
||||
break;
|
||||
default:
|
||||
typ = "UNKNOWN";
|
||||
break;
|
||||
}
|
||||
|
||||
// Return the message type string
|
||||
|
||||
return typ;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a datagram to the specified NetBIOS name using the global NetBIOS datagram socket
|
||||
*
|
||||
* @param dgramTyp Datagram type
|
||||
* @param fromName From NetBIOS name
|
||||
* @param fromNameTyp From NetBIOS name type.
|
||||
* @param toName To NetBIOS name
|
||||
* @param toNameType To NetBIOS name type.
|
||||
* @param userData User data buffer
|
||||
* @param userLen User data length.
|
||||
* @param userOff Offset of data within user buffer.
|
||||
* @param addr Address to send to
|
||||
* @param port Port to send to
|
||||
* @exception java.io.IOException Error occurred sending datagram
|
||||
* @exception UnknownHostException Failed to generate the broadcast mask for the network
|
||||
*/
|
||||
public final void SendDatagram(int dgramTyp, String fromName, char fromNameType, String toName, char toNameType,
|
||||
byte[] userData, int userLen, int userOff, InetAddress addr, int port) throws IOException,
|
||||
UnknownHostException
|
||||
{
|
||||
|
||||
// Set the datagram header values
|
||||
|
||||
setMessageType(dgramTyp);
|
||||
setSourceName(fromName, fromNameType);
|
||||
setDestinationName(toName, toNameType);
|
||||
setSourcePort(RFCNetBIOSProtocol.DATAGRAM);
|
||||
setSourceIPAddress(InetAddress.getLocalHost().getAddress());
|
||||
setFlags(FLG_FIRSTPKT);
|
||||
|
||||
if (m_nextId == 0)
|
||||
m_nextId = (int) (System.currentTimeMillis() & 0x7FFF);
|
||||
setDatagramId(m_nextId++);
|
||||
|
||||
// Set the user data and length
|
||||
|
||||
setLength(userLen + NB_USERDATA);
|
||||
setUserData(userData, userLen, userOff);
|
||||
|
||||
// Use the global NetBIOS datagram socket to sent the broadcast datagram
|
||||
|
||||
NetBIOSDatagramSocket nbSocket = NetBIOSDatagramSocket.getInstance();
|
||||
nbSocket.sendDatagram(this, addr, port);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a datagram to the specified NetBIOS name using the global NetBIOS datagram socket
|
||||
*
|
||||
* @param dgramTyp Datagram type
|
||||
* @param fromName From NetBIOS name
|
||||
* @param fromNameTyp From NetBIOS name type.
|
||||
* @param toName To NetBIOS name
|
||||
* @param toNameType To NetBIOS name type.
|
||||
* @param userData User data buffer
|
||||
* @param userLen User data length.
|
||||
* @param userOff Offset of data within user buffer.
|
||||
* @exception java.io.IOException Error occurred sending datagram
|
||||
* @exception UnknownHostException Failed to generate the broadcast mask for the network
|
||||
*/
|
||||
public final void SendDatagram(int dgramTyp, String fromName, char fromNameType, String toName, char toNameType,
|
||||
byte[] userData, int userLen, int userOff) throws IOException, UnknownHostException
|
||||
{
|
||||
|
||||
// Set the datagram header values
|
||||
|
||||
setMessageType(dgramTyp);
|
||||
setSourceName(fromName, fromNameType);
|
||||
setDestinationName(toName, toNameType);
|
||||
setSourcePort(RFCNetBIOSProtocol.DATAGRAM);
|
||||
setSourceIPAddress(InetAddress.getLocalHost().getAddress());
|
||||
setFlags(FLG_FIRSTPKT);
|
||||
|
||||
if (m_nextId == 0)
|
||||
m_nextId = (int) (System.currentTimeMillis() & 0x7FFF);
|
||||
setDatagramId(m_nextId++);
|
||||
|
||||
// Set the user data and length
|
||||
|
||||
setLength(userLen + NB_USERDATA);
|
||||
setUserData(userData, userLen, userOff);
|
||||
|
||||
// Use the global NetBIOS datagram socket to sent the broadcast datagram
|
||||
|
||||
NetBIOSDatagramSocket nbSocket = NetBIOSDatagramSocket.getInstance();
|
||||
nbSocket.sendBroadcastDatagram(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a datagram to the specified NetBIOS name using the global NetBIOS datagram socket
|
||||
*
|
||||
* @param dgramTyp Datagram type
|
||||
* @param fromName From NetBIOS name
|
||||
* @param fromNameTyp From NetBIOS name type.
|
||||
* @param toName To NetBIOS name
|
||||
* @param toNameType To NetBIOS name type.
|
||||
* @param userData User data buffer
|
||||
* @param userLen User data length.
|
||||
* @exception java.io.IOException Error occurred sending datagram
|
||||
* @exception UnknownHostException Failed to generate the broadcast mask for the network
|
||||
*/
|
||||
public final void SendDatagram(int dgramTyp, String fromName, String toName, byte[] userData, int userLen)
|
||||
throws IOException, UnknownHostException
|
||||
{
|
||||
|
||||
// Send the datagram from the standard port
|
||||
|
||||
SendDatagram(dgramTyp, fromName, NetBIOSName.FileServer, toName, NetBIOSName.FileServer, userData, userLen, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a datagram to the specified NetBIOS name using the supplised datagram socket.
|
||||
*
|
||||
* @param dgramTyp Datagram type
|
||||
* @param sock Datagram socket to use to send the datagram packet.
|
||||
* @param fromName From NetBIOS name
|
||||
* @param fromNameTyp From NetBIOS name type.
|
||||
* @param toName To NetBIOS name
|
||||
* @param toNameType To NetBIOS name type.
|
||||
* @param userData User data buffer
|
||||
* @param userLen User data length.
|
||||
* @param userOff Offset of data within user buffer.
|
||||
* @exception java.io.IOException The exception description.
|
||||
*/
|
||||
public final void SendDatagram(int dgramTyp, DatagramSocket sock, String fromName, char fromNameType,
|
||||
String toName, char toNameType, byte[] userData, int userLen, int userOff) throws IOException
|
||||
{
|
||||
|
||||
// Set the datagram header values
|
||||
|
||||
setMessageType(dgramTyp);
|
||||
setSourceName(fromName, fromNameType);
|
||||
setDestinationName(toName, toNameType);
|
||||
setSourcePort(RFCNetBIOSProtocol.DATAGRAM);
|
||||
setSourceIPAddress(InetAddress.getLocalHost().getAddress());
|
||||
setFlags(FLG_FIRSTPKT);
|
||||
|
||||
if (m_nextId == 0)
|
||||
m_nextId = (int) (System.currentTimeMillis() & 0x7FFF);
|
||||
setDatagramId(m_nextId++);
|
||||
|
||||
// Set the user data and length
|
||||
|
||||
setLength(userLen + NB_USERDATA);
|
||||
setUserData(userData, userLen, userOff);
|
||||
|
||||
// Build a broadcast destination address
|
||||
|
||||
InetAddress destAddr = InetAddress.getByName(NetworkSettings.GenerateBroadcastMask(null));
|
||||
DatagramPacket dgram = new DatagramPacket(m_buf, userLen + NB_USERDATA, destAddr, RFCNetBIOSProtocol.DATAGRAM);
|
||||
|
||||
// Debug
|
||||
|
||||
// HexDump.Dump( m_buf, userLen + NB_USERDATA, 0);
|
||||
|
||||
// Send the datagram
|
||||
|
||||
sock.send(dgram);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a datagram to the specified NetBIOS name using the supplied datagram socket.
|
||||
*
|
||||
* @param fromName java.lang.String
|
||||
* @param toName java.lang.String
|
||||
* @param userData byte[]
|
||||
* @param userLen int
|
||||
* @exception java.io.IOException The exception description.
|
||||
*/
|
||||
public final void SendDatagram(int dgramTyp, DatagramSocket sock, String fromName, String toName, byte[] userData,
|
||||
int userLen) throws IOException
|
||||
{
|
||||
|
||||
// Send the datagram from the standard port
|
||||
|
||||
SendDatagram(dgramTyp, sock, fromName, NetBIOSName.FileServer, toName, NetBIOSName.FileServer, userData,
|
||||
userLen, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the datagram id.
|
||||
*
|
||||
* @param id int
|
||||
*/
|
||||
public final void setDatagramId(int id)
|
||||
{
|
||||
DataPacker.putIntelShort(id, m_buf, NB_DATAGRAMID);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the datagram destination name.
|
||||
*
|
||||
* @param name java.lang.String
|
||||
*/
|
||||
public final void setDestinationName(String name)
|
||||
{
|
||||
setDestinationName(name, NetBIOSName.FileServer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the datagram destination name.
|
||||
*
|
||||
* @param name java.lang.String
|
||||
*/
|
||||
public final void setDestinationName(String name, char typ)
|
||||
{
|
||||
|
||||
// Convert the name to NetBIOS RFC encoded name
|
||||
|
||||
NetBIOSSession.EncodeName(name, typ, m_buf, NB_TONAME);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the datagram flags value.
|
||||
*
|
||||
* @param flg int
|
||||
*/
|
||||
public final void setFlags(int flg)
|
||||
{
|
||||
m_buf[NB_FLAGS] = (byte) (flg & 0xFF);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the datagram length.
|
||||
*
|
||||
* @param len int
|
||||
*/
|
||||
public final void setLength(int len)
|
||||
{
|
||||
DataPacker.putShort((short) len, m_buf, NB_DATAGRAMLEN);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the NetBIOS datagram message type.
|
||||
*
|
||||
* @param msg int
|
||||
*/
|
||||
public final void setMessageType(int msg)
|
||||
{
|
||||
m_buf[NB_MSGTYPE] = (byte) (msg & 0xFF);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the source IP address for the datagram.
|
||||
*
|
||||
* @param ipaddr byte[]
|
||||
*/
|
||||
public final void setSourceIPAddress(byte[] ipaddr)
|
||||
{
|
||||
|
||||
// Pack the IP address into the datagram buffer
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
m_buf[NB_SOURCEIP + i] = ipaddr[i];
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the datagram source NetBIOS name.
|
||||
*
|
||||
* @param name java.lang.String
|
||||
*/
|
||||
public final void setSourceName(String name)
|
||||
{
|
||||
|
||||
// Convert the name to NetBIOS RFC encoded name
|
||||
|
||||
NetBIOSSession.EncodeName(name, NetBIOSName.FileServer, m_buf, NB_FROMNAME);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the datagram source NetBIOS name.
|
||||
*
|
||||
* @param name java.lang.String
|
||||
*/
|
||||
public final void setSourceName(String name, char typ)
|
||||
{
|
||||
|
||||
// Convert the name to NetBIOS RFC encoded name
|
||||
|
||||
NetBIOSSession.EncodeName(name, typ, m_buf, NB_FROMNAME);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the source port/socket for the datagram.
|
||||
*
|
||||
* @param port int
|
||||
*/
|
||||
public final void setSourcePort(int port)
|
||||
{
|
||||
DataPacker.putShort((short) port, m_buf, NB_SOURCEPORT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the user data portion of the datagram.
|
||||
*
|
||||
* @param buf byte[]
|
||||
* @param len int
|
||||
*/
|
||||
public final void setUserData(byte[] buf, int len)
|
||||
{
|
||||
|
||||
// Copy the user data
|
||||
|
||||
System.arraycopy(buf, 0, m_buf, NB_USERDATA, len);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the user data portion of the datagram.
|
||||
*
|
||||
* @param buf User data buffer
|
||||
* @param len Length of user data
|
||||
* @param off Offset to start of data within buffer.
|
||||
*/
|
||||
public final void setUserData(byte[] buf, int len, int off)
|
||||
{
|
||||
|
||||
// Copy the user data
|
||||
|
||||
System.arraycopy(buf, off, m_buf, NB_USERDATA, len);
|
||||
}
|
||||
|
||||
/**
|
||||
* Common constructor initialization code.
|
||||
*/
|
||||
protected final void CommonInit()
|
||||
{
|
||||
}
|
||||
}
|
@@ -0,0 +1,172 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.filesys.netbios;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.SocketException;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
/**
|
||||
* NetBIOS Datagram Socket Class
|
||||
* <p>
|
||||
* Singleton class that allows multiple users of the socket.
|
||||
*/
|
||||
public class NetBIOSDatagramSocket
|
||||
{
|
||||
// Global NetBIOS datagram socket instance
|
||||
|
||||
private static NetBIOSDatagramSocket m_nbSocket;
|
||||
|
||||
// Default port and bind address
|
||||
|
||||
private static int m_defPort = RFCNetBIOSProtocol.DATAGRAM;
|
||||
private static InetAddress m_defBindAddr;
|
||||
|
||||
// Datagram socket
|
||||
|
||||
private DatagramSocket m_socket;
|
||||
|
||||
// Broadcast address
|
||||
|
||||
private InetAddress m_broadcastAddr;
|
||||
|
||||
/**
|
||||
* Class constructor
|
||||
*
|
||||
* @exception SocketException
|
||||
* @exception UnknownHostException
|
||||
*/
|
||||
private NetBIOSDatagramSocket() throws SocketException, UnknownHostException
|
||||
{
|
||||
|
||||
// Create the datagram socket
|
||||
|
||||
if (m_defBindAddr == null)
|
||||
m_socket = new DatagramSocket(m_defPort);
|
||||
else
|
||||
m_socket = new DatagramSocket(m_defPort, m_defBindAddr);
|
||||
|
||||
// Generate the broadcast mask
|
||||
|
||||
if (m_defBindAddr == null)
|
||||
m_broadcastAddr = InetAddress.getByName(NetworkSettings.GenerateBroadcastMask(null));
|
||||
else
|
||||
m_broadcastAddr = InetAddress.getByName(NetworkSettings.GenerateBroadcastMask(m_defBindAddr
|
||||
.getHostAddress()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the global NetBIOS datagram instance
|
||||
*
|
||||
* @return NetBIOSDatagramSocket
|
||||
* @exception SocketException
|
||||
* @exception UnknownHostException
|
||||
*/
|
||||
public final static synchronized NetBIOSDatagramSocket getInstance() throws SocketException, UnknownHostException
|
||||
{
|
||||
|
||||
// Check if the datagram socket has been created
|
||||
|
||||
if (m_nbSocket == null)
|
||||
m_nbSocket = new NetBIOSDatagramSocket();
|
||||
|
||||
// Return the global NetBIOS datagram socket instance
|
||||
|
||||
return m_nbSocket;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the default port to use
|
||||
*
|
||||
* @param port int
|
||||
*/
|
||||
public final static void setDefaultPort(int port)
|
||||
{
|
||||
m_defPort = port;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the address to bind the datagram socket to
|
||||
*
|
||||
* @param bindAddr InetAddress
|
||||
*/
|
||||
public final static void setBindAddress(InetAddress bindAddr)
|
||||
{
|
||||
m_defBindAddr = bindAddr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive a NetBIOS datagram
|
||||
*
|
||||
* @param dgram NetBIOSDatagram
|
||||
* @return int
|
||||
* @exception IOException
|
||||
*/
|
||||
public final int receiveDatagram(NetBIOSDatagram dgram) throws IOException
|
||||
{
|
||||
|
||||
// Create a datagram packet using the NetBIOS datagram buffer
|
||||
|
||||
DatagramPacket pkt = new DatagramPacket(dgram.getBuffer(), dgram.getBuffer().length);
|
||||
|
||||
// Receive a datagram
|
||||
|
||||
m_socket.receive(pkt);
|
||||
return pkt.getLength();
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a NetBIOS datagram
|
||||
*
|
||||
* @param dgram NetBIOSDatagram
|
||||
* @param destAddr InetAddress
|
||||
* @param destPort int
|
||||
* @exception IOException
|
||||
*/
|
||||
public final void sendDatagram(NetBIOSDatagram dgram, InetAddress destAddr, int destPort) throws IOException
|
||||
{
|
||||
|
||||
// Create a datagram packet using the NetBIOS datagram buffer
|
||||
|
||||
DatagramPacket pkt = new DatagramPacket(dgram.getBuffer(), dgram.getLength(), destAddr, destPort);
|
||||
|
||||
// Send the NetBIOS datagram
|
||||
|
||||
m_socket.send(pkt);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a broadcast NetBIOS datagram
|
||||
*
|
||||
* @param dgram NetBIOSDatagram
|
||||
* @exception IOException
|
||||
*/
|
||||
public final void sendBroadcastDatagram(NetBIOSDatagram dgram) throws IOException
|
||||
{
|
||||
|
||||
// Create a datagram packet using the NetBIOS datagram buffer
|
||||
|
||||
DatagramPacket pkt = new DatagramPacket(dgram.getBuffer(), dgram.getLength(), m_broadcastAddr, m_defPort);
|
||||
|
||||
// Send the NetBIOS datagram
|
||||
|
||||
m_socket.send(pkt);
|
||||
}
|
||||
}
|
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.filesys.netbios;
|
||||
|
||||
/**
|
||||
* NetBIOS exception class.
|
||||
*/
|
||||
public class NetBIOSException extends Exception
|
||||
{
|
||||
private static final long serialVersionUID = 3256438122995988025L;
|
||||
|
||||
/**
|
||||
* NetBIOSException constructor comment.
|
||||
*/
|
||||
public NetBIOSException()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* NetBIOSException constructor comment.
|
||||
*
|
||||
* @param s java.lang.String
|
||||
*/
|
||||
public NetBIOSException(String s)
|
||||
{
|
||||
super(s);
|
||||
}
|
||||
}
|
1049
source/java/org/alfresco/filesys/netbios/NetBIOSName.java
Normal file
1049
source/java/org/alfresco/filesys/netbios/NetBIOSName.java
Normal file
File diff suppressed because it is too large
Load Diff
198
source/java/org/alfresco/filesys/netbios/NetBIOSNameList.java
Normal file
198
source/java/org/alfresco/filesys/netbios/NetBIOSNameList.java
Normal file
@@ -0,0 +1,198 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.filesys.netbios;
|
||||
|
||||
import java.util.Vector;
|
||||
|
||||
/**
|
||||
* NetBIOS Name List Class
|
||||
*/
|
||||
public class NetBIOSNameList
|
||||
{
|
||||
|
||||
// List of NetBIOS names
|
||||
|
||||
private Vector<NetBIOSName> m_nameList;
|
||||
|
||||
/**
|
||||
* Class constructor
|
||||
*/
|
||||
public NetBIOSNameList()
|
||||
{
|
||||
m_nameList = new Vector<NetBIOSName>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a name to the list
|
||||
*
|
||||
* @param name NetBIOSName
|
||||
*/
|
||||
public final void addName(NetBIOSName name)
|
||||
{
|
||||
m_nameList.add(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a name from the list
|
||||
*
|
||||
* @param idx int
|
||||
* @return NetBIOSName
|
||||
*/
|
||||
public final NetBIOSName getName(int idx)
|
||||
{
|
||||
if (idx < m_nameList.size())
|
||||
return m_nameList.get(idx);
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the number of names in the list
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public final int numberOfNames()
|
||||
{
|
||||
return m_nameList.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Find names of the specified name of different types and return a subset of the available
|
||||
* names.
|
||||
*
|
||||
* @param name String
|
||||
* @return NetBIOSNameList
|
||||
*/
|
||||
public final NetBIOSNameList findNames(String name)
|
||||
{
|
||||
|
||||
// Allocate the sub list and search for required names
|
||||
|
||||
NetBIOSNameList subList = new NetBIOSNameList();
|
||||
for (int i = 0; i < m_nameList.size(); i++)
|
||||
{
|
||||
NetBIOSName nbName = getName(i);
|
||||
if (nbName.getName().compareTo(name) == 0)
|
||||
subList.addName(nbName);
|
||||
}
|
||||
|
||||
// Return the sub list of names
|
||||
|
||||
return subList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the first name of the specified type
|
||||
*
|
||||
* @param typ char
|
||||
* @param group boolean
|
||||
* @return NetBIOSName
|
||||
*/
|
||||
public final NetBIOSName findName(char typ, boolean group)
|
||||
{
|
||||
|
||||
// Search for the first name of the required type
|
||||
|
||||
for (int i = 0; i < m_nameList.size(); i++)
|
||||
{
|
||||
NetBIOSName name = getName(i);
|
||||
if (name.getType() == typ && name.isGroupName() == group)
|
||||
return name;
|
||||
}
|
||||
|
||||
// Name type not found
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the specified name and type
|
||||
*
|
||||
* @param name String
|
||||
* @param typ char
|
||||
* @param group boolean
|
||||
* @return NetBIOSName
|
||||
*/
|
||||
public final NetBIOSName findName(String name, char typ, boolean group)
|
||||
{
|
||||
|
||||
// Search for the first name of the required type
|
||||
|
||||
for (int i = 0; i < m_nameList.size(); i++)
|
||||
{
|
||||
NetBIOSName nbName = getName(i);
|
||||
if (nbName.getName().equals(name) && nbName.getType() == typ && nbName.isGroupName() == group)
|
||||
return nbName;
|
||||
}
|
||||
|
||||
// Name/type not found
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find names of the specified type and return a subset of the available names
|
||||
*
|
||||
* @param typ char
|
||||
* @param group boolean
|
||||
* @return NetBIOSNameList
|
||||
*/
|
||||
public final NetBIOSNameList findNames(char typ, boolean group)
|
||||
{
|
||||
|
||||
// Allocate the sub list and search for names of the required type
|
||||
|
||||
NetBIOSNameList subList = new NetBIOSNameList();
|
||||
for (int i = 0; i < m_nameList.size(); i++)
|
||||
{
|
||||
NetBIOSName name = getName(i);
|
||||
if (name.getType() == typ && name.isGroupName() == group)
|
||||
subList.addName(name);
|
||||
}
|
||||
|
||||
// Return the sub list of names
|
||||
|
||||
return subList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a name from the list
|
||||
*
|
||||
* @param name NetBIOSName
|
||||
* @return NetBIOSName
|
||||
*/
|
||||
public final NetBIOSName removeName(NetBIOSName name)
|
||||
{
|
||||
for (int i = 0; i < m_nameList.size(); i++)
|
||||
{
|
||||
NetBIOSName curName = getName(i);
|
||||
if (curName.equals(name))
|
||||
{
|
||||
m_nameList.removeElementAt(i);
|
||||
return curName;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all names from the list
|
||||
*/
|
||||
public final void removeAllNames()
|
||||
{
|
||||
m_nameList.removeAllElements();
|
||||
}
|
||||
}
|
1247
source/java/org/alfresco/filesys/netbios/NetBIOSPacket.java
Normal file
1247
source/java/org/alfresco/filesys/netbios/NetBIOSPacket.java
Normal file
File diff suppressed because it is too large
Load Diff
1938
source/java/org/alfresco/filesys/netbios/NetBIOSSession.java
Normal file
1938
source/java/org/alfresco/filesys/netbios/NetBIOSSession.java
Normal file
File diff suppressed because it is too large
Load Diff
202
source/java/org/alfresco/filesys/netbios/NetworkSettings.java
Normal file
202
source/java/org/alfresco/filesys/netbios/NetworkSettings.java
Normal file
@@ -0,0 +1,202 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.filesys.netbios;
|
||||
|
||||
import java.net.InetAddress;
|
||||
|
||||
/**
|
||||
* The network settings class contains various Windows Networking settings that are needed by the
|
||||
* NetBIOS and SMB layers.
|
||||
*/
|
||||
public class NetworkSettings
|
||||
{
|
||||
|
||||
// Broadcast mask for broadcast messages
|
||||
|
||||
private static String m_broadcastMask;
|
||||
|
||||
// Domain name/workgroup that this node is part of
|
||||
|
||||
private static String m_domain;
|
||||
|
||||
// Subnet mask address
|
||||
|
||||
private static InetAddress m_subnetAddr;
|
||||
|
||||
/**
|
||||
* Determine the boradcast mask from the local hosts TCP/IP address
|
||||
*
|
||||
* @param addr TCP/IP address to set the broadcast mask for, in 'nnn.nnn.nnn.nnn' format.
|
||||
*/
|
||||
public static String GenerateBroadcastMask(String addr) throws java.net.UnknownHostException
|
||||
{
|
||||
|
||||
// Check if the broadcast mask has already been set
|
||||
|
||||
if (m_broadcastMask != null)
|
||||
return m_broadcastMask;
|
||||
|
||||
// Set the TCP/IP address string
|
||||
|
||||
String localIP = addr;
|
||||
|
||||
if (localIP == null)
|
||||
localIP = InetAddress.getLocalHost().getHostAddress();
|
||||
|
||||
// Find the location of the first dot in the TCP/IP address
|
||||
|
||||
int dotPos = localIP.indexOf('.');
|
||||
if (dotPos != -1)
|
||||
{
|
||||
|
||||
// Extract the leading IP address value
|
||||
|
||||
String ipStr = localIP.substring(0, dotPos);
|
||||
int ipVal = Integer.valueOf(ipStr).intValue();
|
||||
|
||||
// Determine the broadcast mask to use
|
||||
|
||||
if (ipVal <= 127)
|
||||
{
|
||||
|
||||
// Class A address
|
||||
|
||||
m_broadcastMask = "" + ipVal + ".255.255.255";
|
||||
}
|
||||
else if (ipVal <= 191)
|
||||
{
|
||||
|
||||
// Class B adddress
|
||||
|
||||
dotPos++;
|
||||
while (localIP.charAt(dotPos) != '.' && dotPos < localIP.length())
|
||||
dotPos++;
|
||||
|
||||
if (dotPos < localIP.length())
|
||||
m_broadcastMask = localIP.substring(0, dotPos) + ".255.255";
|
||||
}
|
||||
else if (ipVal <= 223)
|
||||
{
|
||||
|
||||
// Class C address
|
||||
|
||||
dotPos++;
|
||||
int dotCnt = 1;
|
||||
|
||||
while (dotCnt < 3 && dotPos < localIP.length())
|
||||
{
|
||||
|
||||
// Check if the current character is a dot
|
||||
|
||||
if (localIP.charAt(dotPos++) == '.')
|
||||
dotCnt++;
|
||||
}
|
||||
|
||||
if (dotPos < localIP.length())
|
||||
m_broadcastMask = localIP.substring(0, dotPos - 1) + ".255";
|
||||
}
|
||||
}
|
||||
|
||||
// Check if the broadcast mask has been set, if not then use a general
|
||||
// broadcast mask
|
||||
|
||||
if (m_broadcastMask == null)
|
||||
{
|
||||
|
||||
// Invalid TCP/IP address string format, use a general broadcast mask
|
||||
// for now.
|
||||
|
||||
m_broadcastMask = "255.255.255.255";
|
||||
}
|
||||
|
||||
// Return the broadcast mask string
|
||||
|
||||
return m_broadcastMask;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the broadcast mask as an address.
|
||||
*
|
||||
* @return java.net.InetAddress
|
||||
*/
|
||||
public final static InetAddress getBroadcastAddress() throws java.net.UnknownHostException
|
||||
{
|
||||
|
||||
// Check if the subnet address is valid
|
||||
|
||||
if (m_subnetAddr == null)
|
||||
{
|
||||
|
||||
// Generate the subnet mask
|
||||
|
||||
String subnet = GenerateBroadcastMask(null);
|
||||
m_subnetAddr = InetAddress.getByName(subnet);
|
||||
}
|
||||
|
||||
// Return the subnet mask address
|
||||
|
||||
return m_subnetAddr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the broadcast mask.
|
||||
*
|
||||
* @return java.lang.String
|
||||
*/
|
||||
public static String getBroadcastMask()
|
||||
{
|
||||
return m_broadcastMask;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the local domain/workgroup name.
|
||||
*/
|
||||
public static String getDomain()
|
||||
{
|
||||
return m_domain;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the broadcast mask has been setup.
|
||||
*/
|
||||
public static boolean hasBroadcastMask()
|
||||
{
|
||||
if (m_broadcastMask == null)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the broadcast mask to be used for broadcast packets.
|
||||
*
|
||||
* @param mask java.lang.String
|
||||
*/
|
||||
public static void setBroadcastMask(String mask)
|
||||
{
|
||||
m_broadcastMask = mask;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the local domain/workgroup name.
|
||||
*
|
||||
* @param domain java.lang.String
|
||||
*/
|
||||
public static void setDomain(String domain)
|
||||
{
|
||||
m_domain = domain;
|
||||
}
|
||||
}
|
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.filesys.netbios;
|
||||
|
||||
/**
|
||||
* RFC NetBIOS constants.
|
||||
*/
|
||||
public final class RFCNetBIOSProtocol
|
||||
{
|
||||
|
||||
// RFC NetBIOS default port/socket
|
||||
|
||||
public static final int PORT = 139;
|
||||
|
||||
// RFC NetBIOS datagram port
|
||||
|
||||
public static final int DATAGRAM = 138;
|
||||
|
||||
// RFC NetBIOS default name lookup datagram port
|
||||
|
||||
public static final int NAME_PORT = 137;
|
||||
|
||||
// RFC NetBIOS default socket timeout
|
||||
|
||||
public static final int TMO = 30000; // 30 seconds, in milliseconds
|
||||
|
||||
// RFC NetBIOS message types.
|
||||
|
||||
public static final int SESSION_MESSAGE = 0x00;
|
||||
public static final int SESSION_REQUEST = 0x81;
|
||||
public static final int SESSION_ACK = 0x82;
|
||||
public static final int SESSION_REJECT = 0x83;
|
||||
public static final int SESSION_RETARGET = 0x84;
|
||||
public static final int SESSION_KEEPALIVE = 0x85;
|
||||
|
||||
// RFC NetBIOS packet header length, and various message lengths.
|
||||
|
||||
public static final int HEADER_LEN = 4;
|
||||
public static final int SESSREQ_LEN = 72;
|
||||
public static final int SESSRESP_LEN = 9;
|
||||
|
||||
// Maximum packet size that RFC NetBIOS can handle (17bit value)
|
||||
|
||||
public static final int MaxPacketSize = 0x01FFFF + HEADER_LEN;
|
||||
}
|
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.filesys.netbios.server;
|
||||
|
||||
/**
|
||||
* NetBIOS add name listener interface.
|
||||
*/
|
||||
public interface AddNameListener
|
||||
{
|
||||
/**
|
||||
* Signal that a NetBIOS name has been added, or an error occurred whilst trying to add a new
|
||||
* NetBIOS name.
|
||||
*
|
||||
* @param evt NetBIOSNameEvent
|
||||
*/
|
||||
public void netbiosNameAdded(NetBIOSNameEvent evt);
|
||||
}
|
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.filesys.netbios.server;
|
||||
|
||||
import org.alfresco.filesys.netbios.NetBIOSName;
|
||||
|
||||
/**
|
||||
* NetBIOS name server event class.
|
||||
*/
|
||||
public class NetBIOSNameEvent
|
||||
{
|
||||
/*
|
||||
* NetBIOS name event status codes
|
||||
*/
|
||||
|
||||
public static final int ADD_SUCCESS = 0; // local name added successfully
|
||||
public static final int ADD_FAILED = 1; // local name add failure
|
||||
public static final int ADD_DUPLICATE = 2; // local name already in use
|
||||
public static final int ADD_IOERROR = 3; // I/O error during add name broadcast
|
||||
public static final int QUERY_NAME = 4; // query for local name
|
||||
public static final int REGISTER_NAME = 5; // remote name registered
|
||||
public static final int REFRESH_NAME = 6; // name refresh
|
||||
public static final int REFRESH_IOERROR = 7; // refresh name I/O error
|
||||
|
||||
/**
|
||||
* NetBIOS name details
|
||||
*/
|
||||
|
||||
private NetBIOSName m_name;
|
||||
|
||||
/**
|
||||
* Name status
|
||||
*/
|
||||
|
||||
private int m_status;
|
||||
|
||||
/**
|
||||
* Create a NetBIOS name event.
|
||||
*
|
||||
* @param name NetBIOSName
|
||||
* @param sts int
|
||||
*/
|
||||
protected NetBIOSNameEvent(NetBIOSName name, int sts)
|
||||
{
|
||||
m_name = name;
|
||||
m_status = sts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the NetBIOS name details.
|
||||
*
|
||||
* @return NetBIOSName
|
||||
*/
|
||||
public final NetBIOSName getNetBIOSName()
|
||||
{
|
||||
return m_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the NetBIOS name status.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public final int getStatus()
|
||||
{
|
||||
return m_status;
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,239 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.filesys.netbios.server;
|
||||
|
||||
import org.alfresco.filesys.netbios.NetBIOSName;
|
||||
|
||||
/**
|
||||
* NetBIOS Request Class
|
||||
* <p>
|
||||
* Contains the details of NetBIOS server request, such as an add name request.
|
||||
*/
|
||||
class NetBIOSRequest
|
||||
{
|
||||
|
||||
// Request types
|
||||
|
||||
public final static int AddName = 0;
|
||||
public final static int DeleteName = 1;
|
||||
public final static int RefreshName = 2;
|
||||
|
||||
// Default retry count and interval
|
||||
|
||||
public final static int DefaultRetries = 5;
|
||||
public final static long DefaultInterval = 2000; // ms
|
||||
|
||||
// Requets type strings
|
||||
|
||||
private final static String[] _typeNames = { "AddName", "DelName", "RefreshName" };
|
||||
|
||||
// Request type
|
||||
|
||||
private int m_type;
|
||||
|
||||
// NetBIOS name details
|
||||
|
||||
private NetBIOSName m_nbName;
|
||||
|
||||
// Retry count and interval
|
||||
|
||||
private int m_retry;
|
||||
private long m_retryIntvl;
|
||||
|
||||
// Response status
|
||||
|
||||
private boolean m_error;
|
||||
|
||||
// Transaction id for this request
|
||||
|
||||
private int m_tranId;
|
||||
|
||||
/**
|
||||
* Class constructor
|
||||
*
|
||||
* @param typ int
|
||||
* @param nbName NetBIOSName
|
||||
* @param tranId int
|
||||
*/
|
||||
public NetBIOSRequest(int typ, NetBIOSName nbName, int tranId)
|
||||
{
|
||||
m_type = typ;
|
||||
m_nbName = nbName;
|
||||
m_tranId = tranId;
|
||||
|
||||
m_retry = DefaultRetries;
|
||||
m_retryIntvl = DefaultInterval;
|
||||
|
||||
m_error = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class constructor
|
||||
*
|
||||
* @param typ int
|
||||
* @param nbName NetBIOSName
|
||||
* @param tranId int
|
||||
* @param retry int
|
||||
*/
|
||||
public NetBIOSRequest(int typ, NetBIOSName nbName, int tranId, int retry)
|
||||
{
|
||||
m_type = typ;
|
||||
m_nbName = nbName;
|
||||
m_tranId = tranId;
|
||||
|
||||
m_retry = retry;
|
||||
m_retryIntvl = DefaultInterval;
|
||||
|
||||
m_error = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the request type
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public final int isType()
|
||||
{
|
||||
return m_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the type as a string
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public final String getTypeAsString()
|
||||
{
|
||||
if (m_type < 0 || m_type >= _typeNames.length)
|
||||
return "";
|
||||
return _typeNames[m_type];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the NetBIOS name details
|
||||
*
|
||||
* @return NetBIOSName
|
||||
*/
|
||||
public final NetBIOSName getNetBIOSName()
|
||||
{
|
||||
return m_nbName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the retry count
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public final int getRetryCount()
|
||||
{
|
||||
return m_retry;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the retry interval
|
||||
*
|
||||
* @return long
|
||||
*/
|
||||
public final long getRetryInterval()
|
||||
{
|
||||
return m_retryIntvl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the transaction id
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public final int getTransactionId()
|
||||
{
|
||||
return m_tranId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the request has an error status
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public final boolean hasErrorStatus()
|
||||
{
|
||||
return m_error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrement the retry count
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
protected final int decrementRetryCount()
|
||||
{
|
||||
return m_retry--;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the error status
|
||||
*
|
||||
* @param sts boolean
|
||||
*/
|
||||
protected final void setErrorStatus(boolean sts)
|
||||
{
|
||||
m_error = sts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the request retry count
|
||||
*
|
||||
* @param retry int
|
||||
*/
|
||||
public final void setRetryCount(int retry)
|
||||
{
|
||||
m_retry = retry;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the retry interval, in milliseconds
|
||||
*
|
||||
* @param interval long
|
||||
*/
|
||||
public final void setRetryInterval(long interval)
|
||||
{
|
||||
m_retryIntvl = interval;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the request as a string
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public String toString()
|
||||
{
|
||||
StringBuffer str = new StringBuffer();
|
||||
|
||||
str.append("[");
|
||||
str.append(getTypeAsString());
|
||||
str.append(":");
|
||||
str.append(getNetBIOSName());
|
||||
str.append(",");
|
||||
str.append(getRetryCount());
|
||||
str.append(",");
|
||||
str.append(getRetryInterval());
|
||||
str.append(",");
|
||||
str.append(getTransactionId());
|
||||
str.append("]");
|
||||
|
||||
return str.toString();
|
||||
}
|
||||
}
|
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.filesys.netbios.server;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramSocket;
|
||||
|
||||
/**
|
||||
* Interface for NetBIOS packet receivers.
|
||||
*/
|
||||
public interface PacketReceiver
|
||||
{
|
||||
|
||||
/**
|
||||
* Receive packets on the specified datagram socket.
|
||||
*
|
||||
* @param sock java.net.DatagramSocket
|
||||
* @exception java.io.IOException The exception description.
|
||||
*/
|
||||
void ReceivePacket(DatagramSocket sock) throws IOException;
|
||||
}
|
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.filesys.netbios.server;
|
||||
|
||||
import java.net.InetAddress;
|
||||
|
||||
/**
|
||||
* NetBIOS name query listener interface.
|
||||
*/
|
||||
public interface QueryNameListener
|
||||
{
|
||||
|
||||
/**
|
||||
* Signal that a NetBIOS name query has been received, for the specified local NetBIOS name.
|
||||
*
|
||||
* @param evt Local NetBIOS name details.
|
||||
* @param addr IP address of the remote node that sent the name query request.
|
||||
*/
|
||||
public void netbiosNameQuery(NetBIOSNameEvent evt, InetAddress addr);
|
||||
}
|
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.filesys.netbios.server;
|
||||
|
||||
import java.net.InetAddress;
|
||||
|
||||
/**
|
||||
* NetBIOS remote name listener interface.
|
||||
*/
|
||||
public interface RemoteNameListener
|
||||
{
|
||||
|
||||
/**
|
||||
* Signal that a remote host has added a new NetBIOS name.
|
||||
*
|
||||
* @param evt NetBIOSNameEvent
|
||||
* @param addr java.net.InetAddress
|
||||
*/
|
||||
public void netbiosAddRemoteName(NetBIOSNameEvent evt, InetAddress addr);
|
||||
|
||||
/**
|
||||
* Signal that a remote host has released a NetBIOS name.
|
||||
*
|
||||
* @param evt NetBIOSNameEvent
|
||||
* @param addr java.net.InetAddress
|
||||
*/
|
||||
public void netbiosReleaseRemoteName(NetBIOSNameEvent evt, InetAddress addr);
|
||||
}
|
260
source/java/org/alfresco/filesys/netbios/win32/NetBIOS.java
Normal file
260
source/java/org/alfresco/filesys/netbios/win32/NetBIOS.java
Normal file
@@ -0,0 +1,260 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.filesys.netbios.win32;
|
||||
|
||||
/**
|
||||
* NetBIOS API Constants Class
|
||||
*/
|
||||
public class NetBIOS
|
||||
{
|
||||
// NetBIOS command codes
|
||||
|
||||
public final static int NCBCall = 0x10;
|
||||
public final static int NCBListen = 0x11;
|
||||
public final static int NCBHangup = 0x12;
|
||||
public final static int NCBSend = 0x14;
|
||||
public final static int NCBRecv = 0x15;
|
||||
public final static int NCBRecvAny = 0x16;
|
||||
public final static int NCBChainSend = 0x17;
|
||||
public final static int NCBDGSend = 0x20;
|
||||
public final static int NCBDGRecv = 0x21;
|
||||
public final static int NCBDGSendBc = 0x22;
|
||||
public final static int NCBDGRecvBc = 0x23;
|
||||
public final static int NCBAddName = 0x30;
|
||||
public final static int NCBDelName = 0x31;
|
||||
public final static int NCBReset = 0x32;
|
||||
public final static int NCBAStat = 0x33;
|
||||
public final static int NCBSStat = 0x34;
|
||||
public final static int NCBCancel = 0x35;
|
||||
public final static int NCBAddGrName = 0x36;
|
||||
public final static int NCBEnum = 0x37;
|
||||
public final static int NCBUnlink = 0x70;
|
||||
public final static int NCBSendNA = 0x71;
|
||||
public final static int NCBChainSendNA = 0x72;
|
||||
public final static int NCBLANStAlert = 0x73;
|
||||
public final static int NCBAction = 0x77;
|
||||
public final static int NCBFindName = 0x78;
|
||||
public final static int NCBTrace = 0x79;
|
||||
|
||||
public final static int Asynch = 0x80;
|
||||
|
||||
// Status codes
|
||||
|
||||
public final static int NRC_GoodRet = 0x00;
|
||||
public final static int NRC_BufLen = 0x01;
|
||||
public final static int NRC_IllCmd = 0x03;
|
||||
public final static int NRC_CmdTmo = 0x05;
|
||||
public final static int NRC_Incomp = 0x06;
|
||||
public final static int NRC_Baddr = 0x07;
|
||||
public final static int NRC_SNumOut = 0x08;
|
||||
public final static int NRC_NoRes = 0x09;
|
||||
public final static int NRC_SClosed = 0x0A;
|
||||
public final static int NRC_CmdCan = 0x0B;
|
||||
public final static int NRC_DupName = 0x0D;
|
||||
public final static int NRC_NamTFul = 0x0E;
|
||||
public final static int NRC_ActSes = 0x0F;
|
||||
public final static int NRC_LocTFul = 0x11;
|
||||
public final static int NRC_RemTFul = 0x12;
|
||||
public final static int NRC_IllNN = 0x13;
|
||||
public final static int NRC_NoCall = 0x14;
|
||||
public final static int NRC_NoWild = 0x15;
|
||||
public final static int NRC_InUse = 0x16;
|
||||
public final static int NRC_NamErr = 0x17;
|
||||
public final static int NRC_SAbort = 0x18;
|
||||
public final static int NRC_NamConf = 0x19;
|
||||
public final static int NRC_IfBusy = 0x21;
|
||||
public final static int NRC_TooMany = 0x22;
|
||||
public final static int NRC_Bridge = 0x23;
|
||||
public final static int NRC_CanOccr = 0x24;
|
||||
public final static int NRC_Cancel = 0x26;
|
||||
public final static int NRC_DupEnv = 0x30;
|
||||
public final static int NRC_EnvNotDef = 0x34;
|
||||
public final static int NRC_OSResNotAv = 0x35;
|
||||
public final static int NRC_MaxApps = 0x36;
|
||||
public final static int NRC_NoSaps = 0x37;
|
||||
public final static int NRC_NoResources = 0x38;
|
||||
public final static int NRC_InvAddress = 0x39;
|
||||
public final static int NRC_InvDDid = 0x3B;
|
||||
public final static int NRC_LockFail = 0x3C;
|
||||
public final static int NRC_OpenErr = 0x3F;
|
||||
public final static int NRC_System = 0x40;
|
||||
public final static int NRC_Pending = 0xFF;
|
||||
|
||||
// Various constants
|
||||
|
||||
public final static int NCBNameSize = 16;
|
||||
public final static int MaxLANA = 254;
|
||||
|
||||
public final static int NameFlagsMask = 0x87;
|
||||
|
||||
public final static int GroupName = 0x80;
|
||||
public final static int UniqueName = 0x00;
|
||||
public final static int Registering = 0x00;
|
||||
public final static int Registered = 0x04;
|
||||
public final static int Deregistered = 0x05;
|
||||
public final static int Duplicate = 0x06;
|
||||
public final static int DuplicateDereg = 0x07;
|
||||
public final static int ListenOutstanding = 0x01;
|
||||
public final static int CallPending = 0x02;
|
||||
public final static int SessionEstablished = 0x03;
|
||||
public final static int HangupPending = 0x04;
|
||||
public final static int HangupComplete = 0x05;
|
||||
public final static int SessionAborted = 0x06;
|
||||
|
||||
public final static String AllTransports = "M\0\0\0";
|
||||
|
||||
// Maximum receive size (16bits)
|
||||
//
|
||||
// Multiple receives must be issued to receive data packets over this size
|
||||
|
||||
public final static int MaxReceiveSize = 0xFFFF;
|
||||
|
||||
/**
|
||||
* Return the status string for a NetBIOS error code
|
||||
*
|
||||
* @param nbError int
|
||||
* @return String
|
||||
*/
|
||||
public final static String getErrorString(int nbError)
|
||||
{
|
||||
|
||||
String str = "";
|
||||
|
||||
switch (nbError)
|
||||
{
|
||||
case NRC_GoodRet:
|
||||
str = "Success status";
|
||||
break;
|
||||
case NRC_BufLen:
|
||||
str = "Illegal buffer length";
|
||||
break;
|
||||
case NRC_IllCmd:
|
||||
str = "Illegal command";
|
||||
break;
|
||||
case NRC_CmdTmo:
|
||||
str = "Command timed out";
|
||||
break;
|
||||
case NRC_Incomp:
|
||||
str = "Message incomplete, issue another command";
|
||||
break;
|
||||
case NRC_Baddr:
|
||||
str = "Illegal buffer address";
|
||||
break;
|
||||
case NRC_SNumOut:
|
||||
str = "Session number out of range";
|
||||
break;
|
||||
case NRC_NoRes:
|
||||
str = "No resource available";
|
||||
break;
|
||||
case NRC_SClosed:
|
||||
str = "Session closed";
|
||||
break;
|
||||
case NRC_CmdCan:
|
||||
str = "Command cancelled";
|
||||
break;
|
||||
case NRC_DupName:
|
||||
str = "Duplicate name";
|
||||
break;
|
||||
case NRC_NamTFul:
|
||||
str = "Name table full";
|
||||
break;
|
||||
case NRC_ActSes:
|
||||
str = "No deletions, name has active sessions";
|
||||
break;
|
||||
case NRC_LocTFul:
|
||||
str = "Local session table full";
|
||||
break;
|
||||
case NRC_RemTFul:
|
||||
str = "Remote session table full";
|
||||
break;
|
||||
case NRC_IllNN:
|
||||
str = "Illegal name number";
|
||||
break;
|
||||
case NRC_NoCall:
|
||||
str = "No callname";
|
||||
break;
|
||||
case NRC_NoWild:
|
||||
str = "Cannot put * in ncb_name";
|
||||
break;
|
||||
case NRC_InUse:
|
||||
str = "Name in use on remote adapter";
|
||||
break;
|
||||
case NRC_NamErr:
|
||||
str = "Name deleted";
|
||||
break;
|
||||
case NRC_SAbort:
|
||||
str = "Session ended abnormally";
|
||||
break;
|
||||
case NRC_NamConf:
|
||||
str = "Name conflict detected";
|
||||
break;
|
||||
case NRC_IfBusy:
|
||||
str = "Interface busy, IRET before retrying";
|
||||
break;
|
||||
case NRC_TooMany:
|
||||
str = "Too many commands outstanding, try later";
|
||||
break;
|
||||
case NRC_Bridge:
|
||||
str = "ncb_lana_num field invalid";
|
||||
break;
|
||||
case NRC_CanOccr:
|
||||
str = "Command completed whilst cancel occurring";
|
||||
break;
|
||||
case NRC_Cancel:
|
||||
str = "Command not valid to cancel";
|
||||
break;
|
||||
case NRC_DupEnv:
|
||||
str = "Name defined by another local process";
|
||||
break;
|
||||
case NRC_EnvNotDef:
|
||||
str = "Environment undefined, RESET required";
|
||||
break;
|
||||
case NRC_OSResNotAv:
|
||||
str = "Require OS resources exhausted";
|
||||
break;
|
||||
case NRC_MaxApps:
|
||||
str = "Max number of applications exceeded";
|
||||
break;
|
||||
case NRC_NoSaps:
|
||||
str = "No saps available for NetBIOS";
|
||||
break;
|
||||
case NRC_NoResources:
|
||||
str = "Requested resources not available";
|
||||
break;
|
||||
case NRC_InvAddress:
|
||||
str = "Invalid ncb address or length";
|
||||
break;
|
||||
case NRC_InvDDid:
|
||||
str = "Ivalid NCB DDID";
|
||||
break;
|
||||
case NRC_LockFail:
|
||||
str = "Lock of user area failed";
|
||||
break;
|
||||
case NRC_OpenErr:
|
||||
str = "NetBIOS not loaded";
|
||||
break;
|
||||
case NRC_System:
|
||||
str = "System error";
|
||||
break;
|
||||
case NRC_Pending:
|
||||
str = "Asyncrhonous command pending";
|
||||
break;
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
}
|
@@ -0,0 +1,400 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.filesys.netbios.win32;
|
||||
|
||||
import org.alfresco.filesys.netbios.NetBIOSName;
|
||||
|
||||
/**
|
||||
* NetBIOS Socket Class
|
||||
*
|
||||
* <p>Contains the details of a Winsock NetBIOS socket that was opened using native code.
|
||||
*
|
||||
* @author GKSpencer
|
||||
*/
|
||||
public class NetBIOSSocket
|
||||
{
|
||||
// Socket types
|
||||
|
||||
private static final int TypeNormal = 0;
|
||||
private static final int TypeListener = 1;
|
||||
private static final int TypeDatagram = 2;
|
||||
|
||||
// Flag to indicate if the NetBIOS socket interface has been initialized
|
||||
|
||||
private static boolean _nbSocketInit;
|
||||
|
||||
// NetBIOS LANA that the socket is associated with
|
||||
|
||||
private int m_lana;
|
||||
|
||||
// Socket pointer (Windows SOCKET)
|
||||
|
||||
private int m_socket;
|
||||
|
||||
// NetBIOS name, either listening name or callers name
|
||||
|
||||
private NetBIOSName m_nbName;
|
||||
|
||||
// Socket type
|
||||
|
||||
private int m_socketType;
|
||||
|
||||
/**
|
||||
* Initialize the Winsock NetBIOS interface
|
||||
*/
|
||||
public static final void initializeSockets()
|
||||
throws WinsockNetBIOSException {
|
||||
|
||||
// Check if the NetBIOS socket interface has been initialized
|
||||
|
||||
if ( _nbSocketInit == false)
|
||||
{
|
||||
// Initialize the NetBIOS socket interface
|
||||
|
||||
Win32NetBIOS.InitializeSockets();
|
||||
|
||||
// Indicate that the NetBIOS socket interface is initialized
|
||||
|
||||
_nbSocketInit = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Shutdown the Winsock NetBIOS interface
|
||||
*/
|
||||
public static final void shutdownSockets()
|
||||
{
|
||||
// Check if the NetBIOS socket interface has been initialized
|
||||
|
||||
if ( _nbSocketInit == true)
|
||||
{
|
||||
// Indicate that the NetBIOS socket interface is not initialized
|
||||
|
||||
_nbSocketInit = false;
|
||||
|
||||
// Initialize the NetBIOS socket interface
|
||||
|
||||
Win32NetBIOS.ShutdownSockets();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the Winsock NetBIOS interface is initialized
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public static final boolean isInitialized()
|
||||
{
|
||||
return _nbSocketInit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a NetBIOS socket to listen for incoming sessions on the specified LANA
|
||||
*
|
||||
* @param lana int
|
||||
* @param nbName NetBIOSName
|
||||
* @return NetBIOSSocket
|
||||
* @exception NetBIOSSocketException
|
||||
* @exception WinsockNetBIOSException
|
||||
*/
|
||||
public static final NetBIOSSocket createListenerSocket(int lana, NetBIOSName nbName)
|
||||
throws WinsockNetBIOSException, NetBIOSSocketException
|
||||
{
|
||||
// Initialize the Winsock NetBIOS interface
|
||||
|
||||
initializeSockets();
|
||||
|
||||
// Create a new NetBIOS socket
|
||||
|
||||
int sockPtr = Win32NetBIOS.CreateSocket(lana);
|
||||
if ( sockPtr == 0)
|
||||
throw new NetBIOSSocketException("Failed to create NetBIOS socket");
|
||||
|
||||
// Bind the socket to a NetBIOS name
|
||||
|
||||
if ( Win32NetBIOS.BindSocket( sockPtr, nbName.getNetBIOSName()) != 0)
|
||||
throw new NetBIOSSocketException("Failed to bind NetBIOS socket");
|
||||
|
||||
// Return the NetBIOS socket
|
||||
|
||||
return new NetBIOSSocket(lana, sockPtr, nbName, TypeListener);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a NetBIOS datagram socket to send out mailslot announcements on the specified LANA
|
||||
*
|
||||
* @param lana int
|
||||
* @return NetBIOSSocket
|
||||
* @exception NetBIOSSocketException
|
||||
* @exception WinsockNetBIOSException
|
||||
*/
|
||||
public static final NetBIOSSocket createDatagramSocket(int lana)
|
||||
throws WinsockNetBIOSException, NetBIOSSocketException
|
||||
{
|
||||
// Initialize the Winsock NetBIOS interface
|
||||
|
||||
initializeSockets();
|
||||
|
||||
// Create a new NetBIOS socket
|
||||
|
||||
int sockPtr = Win32NetBIOS.CreateDatagramSocket(lana);
|
||||
if ( sockPtr == 0)
|
||||
throw new NetBIOSSocketException("Failed to create NetBIOS datagram socket");
|
||||
|
||||
// Return the NetBIOS socket
|
||||
|
||||
return new NetBIOSSocket(lana, sockPtr, null, TypeDatagram);
|
||||
}
|
||||
|
||||
/**
|
||||
* Class constructor
|
||||
*
|
||||
* @param lana int
|
||||
* @param sockPtr int
|
||||
* @param nbName NetBIOSName
|
||||
* @param sockerType int
|
||||
*/
|
||||
private NetBIOSSocket(int lana, int sockPtr, NetBIOSName nbName, int socketType)
|
||||
{
|
||||
m_lana = lana;
|
||||
m_nbName = nbName;
|
||||
m_socket = sockPtr;
|
||||
|
||||
m_socketType = socketType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the NetBIOS LANA the socket is associated with
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public final int getLana()
|
||||
{
|
||||
return m_lana;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if this is a datagram socket
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public final boolean isDatagramSocket()
|
||||
{
|
||||
return m_socketType == TypeDatagram ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if this is a listener type socket
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public final boolean isListener()
|
||||
{
|
||||
return m_socketType == TypeListener ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the socket is valid
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public final boolean hasSocket()
|
||||
{
|
||||
return m_socket != 0 ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the socket pointer
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public final int getSocket()
|
||||
{
|
||||
return m_socket;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the NetBIOS name. For a listening socket this is the local name, for a session
|
||||
* socket this is the remote callers name.
|
||||
*
|
||||
* @return NetBIOSName
|
||||
*/
|
||||
public final NetBIOSName getName()
|
||||
{
|
||||
return m_nbName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write data to the session socket
|
||||
*
|
||||
* @param buf byte[]
|
||||
* @param off int
|
||||
* @param len int
|
||||
* @return int
|
||||
* @exception WinsockNetBIOSException
|
||||
*/
|
||||
public final int write(byte[] buf, int off, int len)
|
||||
throws WinsockNetBIOSException
|
||||
{
|
||||
// Check if this is a datagram socket
|
||||
|
||||
if ( isDatagramSocket())
|
||||
throw new WinsockNetBIOSException("Write not allowed for datagram socket");
|
||||
|
||||
return Win32NetBIOS.SendSocket( getSocket(), buf, off, len);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read data from the session socket
|
||||
*
|
||||
* @param buf byte[]
|
||||
* @param off int
|
||||
* @param maxLen int
|
||||
* @return int
|
||||
* @exception WinsockNetBIOSException
|
||||
*/
|
||||
public final int read(byte[] buf, int off, int maxLen)
|
||||
throws WinsockNetBIOSException
|
||||
{
|
||||
// Check if this is a datagram socket
|
||||
|
||||
if ( isDatagramSocket())
|
||||
throw new WinsockNetBIOSException("Read not allowed for datagram socket");
|
||||
|
||||
return Win32NetBIOS.ReceiveSocket( getSocket(), buf, off, maxLen);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a datagram to a group name
|
||||
*
|
||||
* @param toName NetBIOSName
|
||||
* @param buf byte[]
|
||||
* @param off int
|
||||
* @param len int
|
||||
* @return int
|
||||
* @exception WinsockNetBIOSException
|
||||
*/
|
||||
public final int sendDatagram(NetBIOSName toName, byte[] buf, int off, int len)
|
||||
throws WinsockNetBIOSException
|
||||
{
|
||||
// Check if this is a datagram socket
|
||||
|
||||
if ( isDatagramSocket() == false)
|
||||
throw new WinsockNetBIOSException("Not a datagram type socket");
|
||||
|
||||
return Win32NetBIOS.SendSocketDatagram( getSocket(), toName.getNetBIOSName(), buf, off, len);
|
||||
}
|
||||
|
||||
/**
|
||||
* Listen for an incoming session connection and create a session socket for the new session
|
||||
*
|
||||
* @return NetBIOSSocket
|
||||
* @exception NetBIOSSocketException
|
||||
* @exception winsockNetBIOSException
|
||||
*/
|
||||
public final NetBIOSSocket listen()
|
||||
throws WinsockNetBIOSException, NetBIOSSocketException
|
||||
{
|
||||
// Check if this socket is a listener socket, and the socket is valid
|
||||
|
||||
if ( isListener() == false)
|
||||
throw new NetBIOSSocketException("Not a listener type socket");
|
||||
|
||||
if ( hasSocket() == false)
|
||||
throw new NetBIOSSocketException("NetBIOS socket not valid");
|
||||
|
||||
// Wait for an incoming session request
|
||||
|
||||
byte[] callerName = new byte[NetBIOSName.NameLength];
|
||||
|
||||
int sessSockPtr = Win32NetBIOS.ListenSocket( getSocket(), callerName);
|
||||
if ( sessSockPtr == 0)
|
||||
throw new NetBIOSSocketException("NetBIOS socket listen failed");
|
||||
|
||||
// Return the new NetBIOS socket session
|
||||
|
||||
return new NetBIOSSocket(getLana(), sessSockPtr, new NetBIOSName(callerName, 0), TypeNormal);
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the socket
|
||||
*/
|
||||
public final void closeSocket()
|
||||
{
|
||||
// Close the native socket, if valid
|
||||
|
||||
if ( hasSocket())
|
||||
{
|
||||
Win32NetBIOS.CloseSocket( getSocket());
|
||||
setSocket(0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the socket pointer
|
||||
*
|
||||
* @param sockPtr int
|
||||
*/
|
||||
protected final void setSocket(int sockPtr)
|
||||
{
|
||||
m_socket = sockPtr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the NetBIOS socket details as a string
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public String toString()
|
||||
{
|
||||
StringBuilder str = new StringBuilder();
|
||||
|
||||
str.append("[LANA:");
|
||||
str.append(getLana());
|
||||
str.append(",Name:");
|
||||
if ( getName() != null)
|
||||
str.append(getName());
|
||||
else
|
||||
str.append("<None>");
|
||||
|
||||
str.append(",Socket:");
|
||||
if ( hasSocket())
|
||||
{
|
||||
str.append("0x");
|
||||
str.append(Integer.toHexString(getSocket()));
|
||||
}
|
||||
else
|
||||
str.append("<None>");
|
||||
|
||||
switch( m_socketType)
|
||||
{
|
||||
case TypeNormal:
|
||||
str.append("Session");
|
||||
break;
|
||||
case TypeListener:
|
||||
str.append("Listener");
|
||||
break;
|
||||
case TypeDatagram:
|
||||
str.append("Datagram");
|
||||
break;
|
||||
}
|
||||
|
||||
str.append("]");
|
||||
|
||||
return str.toString();
|
||||
}
|
||||
}
|
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.filesys.netbios.win32;
|
||||
|
||||
/**
|
||||
* NetBIOS Socket Exception Class
|
||||
*
|
||||
* @author GKSpencer
|
||||
*/
|
||||
public class NetBIOSSocketException extends Exception
|
||||
{
|
||||
private static final long serialVersionUID = 2363178480979507007L;
|
||||
|
||||
/**
|
||||
* Class constructor
|
||||
*
|
||||
* @param msg String
|
||||
*/
|
||||
public NetBIOSSocketException(String msg)
|
||||
{
|
||||
super(msg);
|
||||
}
|
||||
}
|
713
source/java/org/alfresco/filesys/netbios/win32/Win32NetBIOS.java
Normal file
713
source/java/org/alfresco/filesys/netbios/win32/Win32NetBIOS.java
Normal file
@@ -0,0 +1,713 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.filesys.netbios.win32;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.SocketException;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Hashtable;
|
||||
|
||||
import org.alfresco.filesys.netbios.NetBIOSName;
|
||||
import org.alfresco.filesys.util.DataBuffer;
|
||||
import org.alfresco.filesys.util.IPAddress;
|
||||
|
||||
/**
|
||||
* Win32 NetBIOS Native Call Wrapper Class
|
||||
*/
|
||||
public class Win32NetBIOS
|
||||
{
|
||||
|
||||
// Constants
|
||||
//
|
||||
// FIND_NAME_BUFFER structure length
|
||||
|
||||
protected final static int FindNameBufferLen = 33;
|
||||
|
||||
// Exception if the native code DLL load failed
|
||||
|
||||
private static Throwable m_loadDLLException;
|
||||
|
||||
/**
|
||||
* Check if the native code was loaded successfully
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public static final boolean isInitialized()
|
||||
{
|
||||
return m_loadDLLException == null ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the native code load exception
|
||||
*
|
||||
* @return Throwable
|
||||
*/
|
||||
public static final Throwable getInitializationException()
|
||||
{
|
||||
return m_loadDLLException;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if NetBIOS is enabled on any network adapters
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public static final boolean isAvailable() {
|
||||
|
||||
// Check if the DLL was loaded successfully
|
||||
|
||||
if ( isInitialized() == false)
|
||||
return false;
|
||||
|
||||
// Check if there are any valid LANAs, if not then NetBIOS is not enabled or network
|
||||
// adapters that have NetBIOS enabled are not currently enabled
|
||||
|
||||
int[] lanas = LanaEnum();
|
||||
if ( lanas != null && lanas.length > 0)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a NetBIOS name to the local name table
|
||||
*
|
||||
* @param lana int
|
||||
* @param name byte[]
|
||||
* @return int
|
||||
*/
|
||||
public static native int AddName(int lana, byte[] name);
|
||||
|
||||
/**
|
||||
* Add a group NetBIOS name to the local name table
|
||||
*
|
||||
* @param lana int
|
||||
* @param name byte[]
|
||||
* @return int
|
||||
*/
|
||||
public static native int AddGroupName(int lana, byte[] name);
|
||||
|
||||
/**
|
||||
* Find a NetBIOS name, return the name buffer
|
||||
*
|
||||
* @param lana int
|
||||
* @param name byte[]
|
||||
* @param nameBuf byte[]
|
||||
* @param bufLen int
|
||||
* @return int
|
||||
*/
|
||||
public static native int FindNameRaw(int lana, byte[] name, byte[] nameBuf, int bufLen);
|
||||
|
||||
/**
|
||||
* Find a NetBIOS name
|
||||
*
|
||||
* @param lana int
|
||||
* @param name NetBIOSName
|
||||
* @return int
|
||||
*/
|
||||
public static int FindName(int lana, NetBIOSName nbName)
|
||||
{
|
||||
|
||||
// Allocate a buffer to receive the name details
|
||||
|
||||
byte[] nameBuf = new byte[nbName.isGroupName() ? 65535 : 4096];
|
||||
|
||||
// Get the raw NetBIOS name data
|
||||
|
||||
int sts = FindNameRaw(lana, nbName.getNetBIOSName(), nameBuf, nameBuf.length);
|
||||
|
||||
if (sts != NetBIOS.NRC_GoodRet)
|
||||
return -sts;
|
||||
|
||||
// Unpack the FIND_NAME_HEADER structure
|
||||
|
||||
DataBuffer buf = new DataBuffer(nameBuf, 0, nameBuf.length);
|
||||
|
||||
int nodeCount = buf.getShort();
|
||||
buf.skipBytes(1);
|
||||
boolean isGroupName = buf.getByte() == 0 ? false : true;
|
||||
|
||||
// Unpack the FIND_NAME_BUFFER structures
|
||||
|
||||
int curPos = buf.getPosition();
|
||||
|
||||
for (int i = 0; i < nodeCount; i++)
|
||||
{
|
||||
|
||||
// FIND_NAME_BUFFER:
|
||||
// UCHAR length
|
||||
// UCHAR access_control
|
||||
// UCHAR frame_control
|
||||
// UCHAR destination_addr[6]
|
||||
// UCHAR source_addr[6]
|
||||
// UCHAR routing_info[18]
|
||||
|
||||
// Skip to the source_addr field
|
||||
|
||||
buf.skipBytes(9);
|
||||
|
||||
// Source address field format should be 0.0.n.n.n.n for TCP/IP address
|
||||
|
||||
if (buf.getByte() == 0 && buf.getByte() == 0)
|
||||
{
|
||||
|
||||
// Looks like a TCP/IP format address, unpack it
|
||||
|
||||
byte[] ipAddr = new byte[4];
|
||||
|
||||
ipAddr[0] = (byte) buf.getByte();
|
||||
ipAddr[1] = (byte) buf.getByte();
|
||||
ipAddr[2] = (byte) buf.getByte();
|
||||
ipAddr[3] = (byte) buf.getByte();
|
||||
|
||||
// Add the address to the list of TCP/IP addresses for the NetBIOS name
|
||||
|
||||
nbName.addIPAddress(ipAddr);
|
||||
|
||||
// Skip to the start of the next FIND_NAME_BUFFER structure
|
||||
|
||||
curPos += FindNameBufferLen;
|
||||
buf.setPosition(curPos);
|
||||
}
|
||||
}
|
||||
|
||||
// Return the node count
|
||||
|
||||
return nodeCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a NetBIOS name from the local name table
|
||||
*
|
||||
* @param lana int
|
||||
* @param name byte[]
|
||||
* @return int
|
||||
*/
|
||||
public static native int DeleteName(int lana, byte[] name);
|
||||
|
||||
/**
|
||||
* Enumerate the available LANAs
|
||||
*
|
||||
* @return int[]
|
||||
*/
|
||||
public static int[] LanaEnumerate()
|
||||
{
|
||||
// Make sure that there is an active network adapter as making calls to the LanaEnum native call
|
||||
// causes problems when there are no active network adapters.
|
||||
|
||||
boolean adapterAvail = false;
|
||||
|
||||
try
|
||||
{
|
||||
// Enumerate the available network adapters and check for an active adapter, not including
|
||||
// the loopback adapter
|
||||
|
||||
Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
|
||||
|
||||
while ( nis.hasMoreElements() && adapterAvail == false)
|
||||
{
|
||||
NetworkInterface ni = nis.nextElement();
|
||||
if ( ni.getName().equals("lo") == false)
|
||||
{
|
||||
// Make sure the adapter has a valid IP address
|
||||
|
||||
Enumeration<InetAddress> addrs = ni.getInetAddresses();
|
||||
if ( addrs.hasMoreElements())
|
||||
adapterAvail = true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
catch ( SocketException ex)
|
||||
{
|
||||
}
|
||||
|
||||
// Check if there are network adapter(s) available
|
||||
|
||||
if ( adapterAvail == false)
|
||||
return null;
|
||||
|
||||
// Call the native code to return the available LANA list
|
||||
|
||||
return LanaEnum();
|
||||
}
|
||||
|
||||
/**
|
||||
* Enumerate the available LANAs
|
||||
*
|
||||
* @return int[]
|
||||
*/
|
||||
private static native int[] LanaEnum();
|
||||
|
||||
/**
|
||||
* Reset the NetBIOS environment
|
||||
*
|
||||
* @param lana int
|
||||
* @return int
|
||||
*/
|
||||
public static native int Reset(int lana);
|
||||
|
||||
/**
|
||||
* Listen for an incoming session request
|
||||
*
|
||||
* @param lana int
|
||||
* @param toName byte[]
|
||||
* @param fromName byte[]
|
||||
* @param callerName byte[]
|
||||
* @return int
|
||||
*/
|
||||
public static native int Listen(int lana, byte[] toName, byte[] fromName, byte[] callerName);
|
||||
|
||||
/**
|
||||
* Receive a data packet on a session
|
||||
*
|
||||
* @param lana int
|
||||
* @param lsn int
|
||||
* @param buf byte[]
|
||||
* @param off int
|
||||
* @param maxLen int
|
||||
* @return int
|
||||
*/
|
||||
public static native int Receive(int lana, int lsn, byte[] buf, int off, int maxLen);
|
||||
|
||||
/**
|
||||
* Send a data packet on a session
|
||||
*
|
||||
* @param lana int
|
||||
* @param lsn int
|
||||
* @param buf byte[]
|
||||
* @param off int
|
||||
* @param len int
|
||||
* @return int
|
||||
*/
|
||||
public static native int Send(int lana, int lsn, byte[] buf, int off, int len);
|
||||
|
||||
/**
|
||||
* Send a datagram to a specified name
|
||||
*
|
||||
* @param lana int
|
||||
* @param srcNum int
|
||||
* @param destName byte[]
|
||||
* @param buf byte[]
|
||||
* @param off int
|
||||
* @param len int
|
||||
* @return int
|
||||
*/
|
||||
public static native int SendDatagram(int lana, int srcNum, byte[] destName, byte[] buf, int off, int len);
|
||||
|
||||
/**
|
||||
* Send a broadcast datagram
|
||||
*
|
||||
* @param lana
|
||||
* @param buf byte[]
|
||||
* @param off int
|
||||
* @param len int
|
||||
* @return int
|
||||
*/
|
||||
public static native int SendBroadcastDatagram(int lana, byte[] buf, int off, int len);
|
||||
|
||||
/**
|
||||
* Receive a datagram on a specified name
|
||||
*
|
||||
* @param lana int
|
||||
* @param nameNum int
|
||||
* @param buf byte[]
|
||||
* @param off int
|
||||
* @param maxLen int
|
||||
* @return int
|
||||
*/
|
||||
public static native int ReceiveDatagram(int lana, int nameNum, byte[] buf, int off, int maxLen);
|
||||
|
||||
/**
|
||||
* Receive a broadcast datagram
|
||||
*
|
||||
* @param lana int
|
||||
* @param nameNum int
|
||||
* @param buf byte[]
|
||||
* @param off int
|
||||
* @param maxLen int
|
||||
* @return int
|
||||
*/
|
||||
public static native int ReceiveBroadcastDatagram(int lana, int nameNum, byte[] buf, int off, int maxLen);
|
||||
|
||||
/**
|
||||
* Hangup a session
|
||||
*
|
||||
* @param lsn int
|
||||
* @return int
|
||||
*/
|
||||
public static native int Hangup(int lana, int lsn);
|
||||
|
||||
/**
|
||||
* Return the local computers NetBIOS name
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public static native String GetLocalNetBIOSName();
|
||||
|
||||
/**
|
||||
* Return the local domain name
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public static native String GetLocalDomainName();
|
||||
|
||||
/**
|
||||
* Return a comma delimeted list of WINS server TCP/IP addresses, or null if no WINS servers are
|
||||
* configured.
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public static native String getWINSServerList();
|
||||
|
||||
/**
|
||||
* Find the TCP/IP address for a LANA
|
||||
*
|
||||
* @param lana int
|
||||
* @return String
|
||||
*/
|
||||
public static final String getIPAddressForLANA(int lana)
|
||||
{
|
||||
|
||||
// Get the local NetBIOS name
|
||||
|
||||
String localName = GetLocalNetBIOSName();
|
||||
if (localName == null)
|
||||
return null;
|
||||
|
||||
// Create a NetBIOS name for the local name
|
||||
|
||||
NetBIOSName nbName = new NetBIOSName(localName, NetBIOSName.WorkStation, false);
|
||||
|
||||
// Get the local NetBIOS name details
|
||||
|
||||
int sts = FindName(lana, nbName);
|
||||
|
||||
if (sts == -NetBIOS.NRC_EnvNotDef)
|
||||
{
|
||||
|
||||
// Reset the LANA then try the name lookup again
|
||||
|
||||
Reset(lana);
|
||||
sts = FindName(lana, nbName);
|
||||
}
|
||||
|
||||
// Check if the name lookup was successful
|
||||
|
||||
String ipAddr = null;
|
||||
|
||||
if (sts >= 0)
|
||||
{
|
||||
|
||||
// Get the first IP address from the list
|
||||
|
||||
ipAddr = nbName.getIPAddressString(0);
|
||||
}
|
||||
|
||||
// Return the TCP/IP address for the LANA
|
||||
|
||||
return ipAddr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the adapter name for a LANA
|
||||
*
|
||||
* @param lana int
|
||||
* @return String
|
||||
*/
|
||||
public static final String getAdapterNameForLANA(int lana)
|
||||
{
|
||||
|
||||
// Get the TCP/IP address for a LANA
|
||||
|
||||
String ipAddr = getIPAddressForLANA(lana);
|
||||
if (ipAddr == null)
|
||||
return null;
|
||||
|
||||
// Get the list of available network adapters
|
||||
|
||||
Hashtable<String, NetworkInterface> adapters = getNetworkAdapterList();
|
||||
String adapterName = null;
|
||||
|
||||
if (adapters != null)
|
||||
{
|
||||
|
||||
// Find the network adapter for the TCP/IP address
|
||||
|
||||
NetworkInterface ni = adapters.get(ipAddr);
|
||||
if (ni != null)
|
||||
adapterName = ni.getDisplayName();
|
||||
}
|
||||
|
||||
// Return the adapter name for the LANA
|
||||
|
||||
return adapterName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the LANA for a TCP/IP address
|
||||
*
|
||||
* @param addr String
|
||||
* @return int
|
||||
*/
|
||||
public static final int getLANAForIPAddress(String addr)
|
||||
{
|
||||
|
||||
// Check if the address is a numeric TCP/IP address
|
||||
|
||||
if (IPAddress.isNumericAddress(addr) == false)
|
||||
return -1;
|
||||
|
||||
// Get a list of the available NetBIOS LANAs
|
||||
|
||||
int[] lanas = LanaEnum();
|
||||
if (lanas == null || lanas.length == 0)
|
||||
return -1;
|
||||
|
||||
// Search for the LANA with the matching TCP/IP address
|
||||
|
||||
for (int i = 0; i < lanas.length; i++)
|
||||
{
|
||||
|
||||
// Get the current LANAs TCP/IP address
|
||||
|
||||
String curAddr = getIPAddressForLANA(lanas[i]);
|
||||
if (curAddr != null && curAddr.equals(addr))
|
||||
return lanas[i];
|
||||
}
|
||||
|
||||
// Failed to find the LANA for the specified TCP/IP address
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the LANA for a network adapter
|
||||
*
|
||||
* @param name String
|
||||
* @return int
|
||||
*/
|
||||
public static final int getLANAForAdapterName(String name)
|
||||
{
|
||||
|
||||
// Get the list of available network adapters
|
||||
|
||||
Hashtable<String, NetworkInterface> niList = getNetworkAdapterList();
|
||||
|
||||
// Search for the address of the specified network adapter
|
||||
|
||||
Enumeration<String> niEnum = niList.keys();
|
||||
|
||||
while (niEnum.hasMoreElements())
|
||||
{
|
||||
|
||||
// Get the current TCP/IP address
|
||||
|
||||
String ipAddr = niEnum.nextElement();
|
||||
NetworkInterface ni = niList.get(ipAddr);
|
||||
|
||||
if (ni.getDisplayName().equalsIgnoreCase(name))
|
||||
{
|
||||
|
||||
// Return the LANA for the network adapters TCP/IP address
|
||||
|
||||
return getLANAForIPAddress(ipAddr);
|
||||
}
|
||||
}
|
||||
|
||||
// Failed to find matching network adapter
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a hashtable of NetworkInterfaces indexed by TCP/IP address
|
||||
*
|
||||
* @return Hashtable<String,NetworkInterface>
|
||||
*/
|
||||
private static final Hashtable<String, NetworkInterface> getNetworkAdapterList()
|
||||
{
|
||||
|
||||
// Get a list of the local network adapters
|
||||
|
||||
Hashtable<String, NetworkInterface> niList = new Hashtable<String, NetworkInterface>();
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
// Enumerate the available network adapters
|
||||
|
||||
Enumeration<NetworkInterface> niEnum = NetworkInterface.getNetworkInterfaces();
|
||||
|
||||
while (niEnum.hasMoreElements())
|
||||
{
|
||||
|
||||
// Get the current network interface details
|
||||
|
||||
NetworkInterface ni = niEnum.nextElement();
|
||||
Enumeration<InetAddress> addrEnum = ni.getInetAddresses();
|
||||
|
||||
while (addrEnum.hasMoreElements())
|
||||
{
|
||||
|
||||
// Get the address and add the adapter to the list indexed via the numeric IP
|
||||
// address string
|
||||
|
||||
InetAddress addr = addrEnum.nextElement();
|
||||
niList.put(addr.getHostAddress(), ni);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
}
|
||||
|
||||
// Return the network adapter list
|
||||
|
||||
return niList;
|
||||
}
|
||||
|
||||
//---------- Winsock based NetBIOS interface ----------//
|
||||
|
||||
/**
|
||||
* Initialize the NetBIOS socket interface
|
||||
*
|
||||
* @exception WinsockNetBIOSException If a Winsock error occurs
|
||||
*/
|
||||
protected static native void InitializeSockets()
|
||||
throws WinsockNetBIOSException;
|
||||
|
||||
/**
|
||||
* Shutdown the NetBIOS socket interface
|
||||
*/
|
||||
protected static native void ShutdownSockets();
|
||||
|
||||
/**
|
||||
* Create a NetBIOS socket
|
||||
*
|
||||
* @param lana int
|
||||
* @return int
|
||||
* @exception WinsockNetBIOSException If a Winsock error occurs
|
||||
*/
|
||||
protected static native int CreateSocket(int lana)
|
||||
throws WinsockNetBIOSException;
|
||||
|
||||
/**
|
||||
* Create a NetBIOS datagram socket
|
||||
*
|
||||
* @param lana int
|
||||
* @return int
|
||||
* @exception WinsockNetBIOSException If a Winsock error occurs
|
||||
*/
|
||||
protected static native int CreateDatagramSocket(int lana)
|
||||
throws WinsockNetBIOSException;
|
||||
|
||||
/**
|
||||
* Bind a NetBIOS socket to a name to listen for incoming sessions
|
||||
*
|
||||
* @param sockPtr int
|
||||
* @param name byte[]
|
||||
* @exception WinsockNetBIOSException If a Winsock error occurs
|
||||
*/
|
||||
protected static native int BindSocket(int sockPtr, byte[] name)
|
||||
throws WinsockNetBIOSException;
|
||||
|
||||
/**
|
||||
* Listen for an incoming connection
|
||||
*
|
||||
* @param sockPtr int
|
||||
* @param callerName byte[]
|
||||
* @return int
|
||||
* @exception WinsockNetBIOSException If a Winsock error occurs
|
||||
*/
|
||||
protected static native int ListenSocket(int sockPtr, byte[] callerName)
|
||||
throws WinsockNetBIOSException;
|
||||
|
||||
/**
|
||||
* Close a NetBIOS socket
|
||||
*
|
||||
* @param sockPtr int
|
||||
*/
|
||||
protected static native void CloseSocket(int sockPtr);
|
||||
|
||||
/**
|
||||
* Send data on a session socket
|
||||
*
|
||||
* @param sockPtr int
|
||||
* @param buf byte[]
|
||||
* @param off int
|
||||
* @param len int
|
||||
* @return int
|
||||
* @exception WinsockNetBIOSException If a Winsock error occurs
|
||||
*/
|
||||
protected static native int SendSocket(int sockPtr, byte[] buf, int off, int len)
|
||||
throws WinsockNetBIOSException;
|
||||
|
||||
/**
|
||||
* Receive data on a session socket
|
||||
*
|
||||
* @param sockPtr int
|
||||
* @param toName byte[]
|
||||
* @param buf byte[]
|
||||
* @param off int
|
||||
* @param maxLen int
|
||||
* @return int
|
||||
* @exception WinsockNetBIOSException If a Winsock error occurs
|
||||
*/
|
||||
protected static native int ReceiveSocket(int sockPtr, byte[] buf, int off, int maxLen)
|
||||
throws WinsockNetBIOSException;
|
||||
|
||||
/**
|
||||
* Send data on a datagram socket
|
||||
*
|
||||
* @param sockPtr int
|
||||
* @param toName byte[]
|
||||
* @param buf byte[]
|
||||
* @param off int
|
||||
* @param len int
|
||||
* @return int
|
||||
* @exception WinsockNetBIOSException If a Winsock error occurs
|
||||
*/
|
||||
protected static native int SendSocketDatagram(int sockPtr, byte[] toName, byte[] buf, int off, int len)
|
||||
throws WinsockNetBIOSException;
|
||||
|
||||
/**
|
||||
* Wait for a network address change event, block until a change occurs or the Winsock NetBIOS
|
||||
* interface is shut down
|
||||
*/
|
||||
public static native void waitForNetworkAddressChange();
|
||||
|
||||
/**
|
||||
* Static initializer used to load the native code library
|
||||
*/
|
||||
static
|
||||
{
|
||||
|
||||
// Load the Win32 NetBIOS interface library
|
||||
|
||||
try
|
||||
{
|
||||
System.loadLibrary("Win32NetBIOS");
|
||||
}
|
||||
catch (Throwable ex)
|
||||
{
|
||||
// Save the native code load exception
|
||||
|
||||
m_loadDLLException = ex;
|
||||
}
|
||||
}
|
||||
}
|
223
source/java/org/alfresco/filesys/netbios/win32/WinsockError.java
Normal file
223
source/java/org/alfresco/filesys/netbios/win32/WinsockError.java
Normal file
@@ -0,0 +1,223 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.filesys.netbios.win32;
|
||||
|
||||
/**
|
||||
* Winsock Error Codes Class
|
||||
*
|
||||
* <p>Contains a list of the error codes that the Win32 Winsock calls may generate, and a method to convert
|
||||
* to an error text string.
|
||||
*
|
||||
* @author GKSpencer
|
||||
*/
|
||||
public class WinsockError
|
||||
{
|
||||
// Winsock error code constants
|
||||
|
||||
public static final int WsaEIntr = 10004;
|
||||
public static final int WsaEAcces = 10013;
|
||||
public static final int WsaEFault = 10014;
|
||||
public static final int WsaEInval = 10022;
|
||||
public static final int WsaEMfile = 10024;
|
||||
public static final int WsaEWouldBlock = 10035;
|
||||
public static final int WsaEInProgress = 10036;
|
||||
public static final int WsaEAlready = 10037;
|
||||
public static final int WsaENotSock = 10038;
|
||||
public static final int WsaEDestAddrReq = 10039;
|
||||
public static final int WsaEMsgSize = 10040;
|
||||
public static final int WsaEPrototype = 10041;
|
||||
public static final int WsaENoProtoOpt = 10042;
|
||||
public static final int WsaEProtoNoSupp = 10043;
|
||||
public static final int WsaESocktNoSupp = 10044;
|
||||
public static final int WsaEOpNotSupp = 10045;
|
||||
public static final int WsaEPFNoSupport = 10046;
|
||||
public static final int WsaEAFNoSupport = 10047;
|
||||
public static final int WsaEAddrInUse = 10048;
|
||||
public static final int WsaEAddrNotAvail= 10049;
|
||||
public static final int WsaENetDown = 10050;
|
||||
public static final int WsaENetUnReach = 10051;
|
||||
public static final int WsaENetReset = 10052;
|
||||
public static final int WsaEConnAborted = 10053;
|
||||
public static final int WsaEConnReset = 10054;
|
||||
public static final int WsaENoBufs = 10055;
|
||||
public static final int WsaEIsConn = 10056;
|
||||
public static final int WsaENotConn = 10057;
|
||||
public static final int WsaEShutdown = 10058;
|
||||
public static final int WsaETimedout = 10060;
|
||||
public static final int WsaEConnRefused = 10061;
|
||||
public static final int WsaEHostDown = 10064;
|
||||
public static final int WsaEHostUnreach = 10065;
|
||||
public static final int WsaEProcLim = 10067;
|
||||
public static final int WsaSysNotReady = 10091;
|
||||
public static final int WsaVerNotSupp = 10092;
|
||||
public static final int WsaNotInit = 10093;
|
||||
public static final int WsaEDiscon = 10101;
|
||||
public static final int WsaTypeNotFound = 10109;
|
||||
public static final int WsaHostNotFound = 11001;
|
||||
public static final int WsaTryAgain = 11002;
|
||||
public static final int WsaNoRecovery = 11003;
|
||||
public static final int WsaNoData = 11004;
|
||||
|
||||
/**
|
||||
* Convert a Winsock error code to a text string
|
||||
*
|
||||
* @param sts int
|
||||
* @return String
|
||||
*/
|
||||
public static final String asString(int sts)
|
||||
{
|
||||
String errText = null;
|
||||
|
||||
switch ( sts)
|
||||
{
|
||||
case WsaEIntr:
|
||||
errText = "Interrupted function call";
|
||||
break;
|
||||
case WsaEAcces:
|
||||
errText = "Permission denied";
|
||||
break;
|
||||
case WsaEFault:
|
||||
errText = "Bad address";
|
||||
break;
|
||||
case WsaEInval:
|
||||
errText = "Invalid argument";
|
||||
break;
|
||||
case WsaEMfile:
|
||||
errText = "Too many open files";
|
||||
break;
|
||||
case WsaEWouldBlock:
|
||||
errText = "Resource temporarily unavailable";
|
||||
break;
|
||||
case WsaEInProgress:
|
||||
errText = "Operation now in progress";
|
||||
break;
|
||||
case WsaEAlready:
|
||||
errText = "Operation already in progress";
|
||||
break;
|
||||
case WsaENotSock:
|
||||
errText = "Socket operation on nonsocket";
|
||||
break;
|
||||
case WsaEDestAddrReq:
|
||||
errText = "Destination address required";
|
||||
break;
|
||||
case WsaEMsgSize:
|
||||
errText = "Message too long";
|
||||
break;
|
||||
case WsaEPrototype:
|
||||
errText = "Protocol wrong type for socket";
|
||||
break;
|
||||
case WsaENoProtoOpt:
|
||||
errText = "Bad protocol option";
|
||||
break;
|
||||
case WsaEProtoNoSupp:
|
||||
errText = "Protocol not supported";
|
||||
break;
|
||||
case WsaESocktNoSupp:
|
||||
errText = "Socket type not supported";
|
||||
break;
|
||||
case WsaEOpNotSupp:
|
||||
errText = "Operation not supported";
|
||||
break;
|
||||
case WsaEPFNoSupport:
|
||||
errText = "Protocol family not supported";
|
||||
break;
|
||||
case WsaEAFNoSupport:
|
||||
errText = "Address family not supported by protocol family";
|
||||
break;
|
||||
case WsaEAddrInUse:
|
||||
errText = "Address already in use";
|
||||
break;
|
||||
case WsaEAddrNotAvail:
|
||||
errText = "Cannot assign requested address";
|
||||
break;
|
||||
case WsaENetDown:
|
||||
errText = "Network is down";
|
||||
break;
|
||||
case WsaENetUnReach:
|
||||
errText = "Network is unreachable";
|
||||
break;
|
||||
case WsaENetReset:
|
||||
errText = "Network dropped connection on reset";
|
||||
break;
|
||||
case WsaEConnAborted:
|
||||
errText = "Software caused connection abort";
|
||||
break;
|
||||
case WsaEConnReset:
|
||||
errText = "Connection reset by peer";
|
||||
break;
|
||||
case WsaENoBufs:
|
||||
errText = "No buffer space available";
|
||||
break;
|
||||
case WsaEIsConn:
|
||||
errText = "Socket is already connected";
|
||||
break;
|
||||
case WsaENotConn:
|
||||
errText = "Socket is not connected";
|
||||
break;
|
||||
case WsaEShutdown:
|
||||
errText = "Cannot send after socket shutdown";
|
||||
break;
|
||||
case WsaETimedout:
|
||||
errText = "Connection timed out";
|
||||
break;
|
||||
case WsaEConnRefused:
|
||||
errText = "Connection refused";
|
||||
break;
|
||||
case WsaEHostDown:
|
||||
errText = "Host is down";
|
||||
break;
|
||||
case WsaEHostUnreach:
|
||||
errText = "No route to host";
|
||||
break;
|
||||
case WsaEProcLim:
|
||||
errText = "Too many processes";
|
||||
break;
|
||||
case WsaSysNotReady:
|
||||
errText = "Network subsystem is unavailable";
|
||||
break;
|
||||
case WsaVerNotSupp:
|
||||
errText = "Winsock.dll version out of range";
|
||||
break;
|
||||
case WsaNotInit:
|
||||
errText = "Successful WSAStartup not yet performed";
|
||||
break;
|
||||
case WsaEDiscon:
|
||||
errText = "Graceful shutdown in progress";
|
||||
break;
|
||||
case WsaTypeNotFound:
|
||||
errText = "Class type not found";
|
||||
break;
|
||||
case WsaHostNotFound:
|
||||
errText = "Host not found";
|
||||
break;
|
||||
case WsaTryAgain:
|
||||
errText = "Nonauthoritative host not found";
|
||||
break;
|
||||
case WsaNoRecovery:
|
||||
errText = "This is a nonrecoverable error";
|
||||
break;
|
||||
case WsaNoData:
|
||||
errText = "Valid name, no data record of requested type";
|
||||
break;
|
||||
default:
|
||||
errText = "Unknown Winsock error 0x" + Integer.toHexString(sts);
|
||||
break;
|
||||
}
|
||||
|
||||
return errText;
|
||||
}
|
||||
}
|
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
* Copyright (C) 2005 Alfresco, Inc.
|
||||
*
|
||||
* Licensed under the Mozilla Public License version 1.1
|
||||
* with a permitted attribution clause. You may obtain a
|
||||
* copy of the License at
|
||||
*
|
||||
* http://www.alfresco.org/legal/license.txt
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the
|
||||
* License.
|
||||
*/
|
||||
package org.alfresco.filesys.netbios.win32;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Winsock NetBIOS Exception Class
|
||||
*
|
||||
* <p>Contains the Winsock error code from the failed Winsock call.
|
||||
*
|
||||
* @author GKSpencer
|
||||
*/
|
||||
public class WinsockNetBIOSException extends IOException
|
||||
{
|
||||
private static final long serialVersionUID = 5933702607108016674L;
|
||||
|
||||
// Winsock error code
|
||||
|
||||
private int m_errCode;
|
||||
|
||||
/**
|
||||
* Default constructor
|
||||
*/
|
||||
public WinsockNetBIOSException()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Class constructor
|
||||
*
|
||||
* @param msg String
|
||||
*/
|
||||
public WinsockNetBIOSException(String msg)
|
||||
{
|
||||
super(msg);
|
||||
|
||||
// Split out the error code
|
||||
|
||||
if ( msg != null)
|
||||
{
|
||||
int pos = msg.indexOf(":");
|
||||
if ( pos != -1)
|
||||
m_errCode = Integer.valueOf(msg.substring(0, pos));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Class constructor
|
||||
*
|
||||
* @param sts int
|
||||
*/
|
||||
public WinsockNetBIOSException(int sts)
|
||||
{
|
||||
super();
|
||||
|
||||
m_errCode = sts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the Winsock error code
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public final int getErrorCode()
|
||||
{
|
||||
return m_errCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the error code
|
||||
*
|
||||
* @param sts int
|
||||
*/
|
||||
public final void setErrorCode(int sts)
|
||||
{
|
||||
m_errCode = sts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the error message string
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public String getMessage()
|
||||
{
|
||||
StringBuilder msg = new StringBuilder();
|
||||
|
||||
msg.append( super.getMessage());
|
||||
String winsockErr = WinsockError.asString(getErrorCode());
|
||||
if ( winsockErr != null)
|
||||
{
|
||||
msg.append(" - ");
|
||||
msg.append(winsockErr);
|
||||
}
|
||||
|
||||
return msg.toString();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user