[jcifs] NbtSocket Bug

Michael B Allen mba2000 at ioplex.com
Sun Jun 8 13:59:48 EST 2003


Whoops, file is really attached now.

Mike

On Sat, 7 Jun 2003, Michael B Allen wrote:

> If a server accepts the SMB_COM_NEGOTIATE request but does not
> respond, the NbtSocket.connect() method will hang indefinately. I
> believe this may have been reported before actually. Attached is a new
> src/jcifs/netbios/NbtSocket.java that sets an the socket timeout to
> jcifs.netbios.soTimeout which by default is 5000ms.
> 
> Eric,
> 
> If you have changes now is the time.
> 
> Mike
> 
> 

-- 
A  program should be written to model the concepts of the task it
performs rather than the physical world or a process because this
maximizes  the  potential  for it to be applied to tasks that are
conceptually  similar and, more important, to tasks that have not
yet been conceived. 
-------------- next part --------------
/* jcifs smb client library in Java

 * Copyright (C) 2000  "Michael B. Allen" <jcifs at samba dot org>

 * 

 * This library is free software; you can redistribute it and/or

 * modify it under the terms of the GNU Lesser General Public

 * License as published by the Free Software Foundation; either

 * version 2.1 of the License, or (at your option) any later version.

 * 

 * This library is distributed in the hope that it will be useful,

 * but WITHOUT ANY WARRANTY; without even the implied warranty of

 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU

 * Lesser General Public License for more details.

 * 

 * You should have received a copy of the GNU Lesser General Public

 * License along with this library; if not, write to the Free Software

 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

 */



package jcifs.netbios;



import java.net.Socket;

import java.net.InetAddress;

import java.io.InputStream;

import java.io.OutputStream;

import java.io.IOException;

import jcifs.Config;



/**

 * This class represents a netbios TCP/IP socket. Under no

 * conditions will users of jcifs need not be concerned with this class as

 * netbios services are handled internally to the smb package. It is

 * only public so the smb package can access it.

 */



public class NbtSocket extends Socket {



	private static final int SSN_SRVC_PORT = 139;

	private static final int BUFFER_SIZE = 512;

    private static final int DEFAULT_SO_TIMEOUT = 5000;



	NbtAddress address;

	Name calledName;

	int soTimeout;



	public NbtSocket() {

		super();

	}

	public NbtSocket( NbtAddress address, int port ) throws IOException {

		this( address, port, null, 0 );

	}

	public NbtSocket( NbtAddress address, int port,

								InetAddress localAddr, int localPort ) throws IOException {

		this( address, null, port, localAddr, localPort );

	}

	public NbtSocket( NbtAddress address, String calledName, int port,

								InetAddress localAddr, int localPort ) throws IOException {

		super( address.getInetAddress(), ( port == 0 ? SSN_SRVC_PORT : port ),

								localAddr, localPort );

		this.address = address;

		if( calledName == null ) {

			this.calledName = address.hostName;

		} else {

			this.calledName = new Name( calledName, 0x20, null );

		}

        soTimeout = Config.getInt( "jcifs.netbios.soTimeout", DEFAULT_SO_TIMEOUT );

		connect();

	}



	public NbtAddress getNbtAddress() {

		return address;

	}

	InputStream getRawInputStream() throws IOException {

		return super.getInputStream();

	}

	OutputStream getRawOutputStream() throws IOException {

		return super.getOutputStream();

	}

	public InputStream getInputStream() throws IOException {

		return new SocketInputStream( super.getInputStream() );

	}

	public OutputStream getOutputStream() throws IOException {

		return new SocketOutputStream( super.getOutputStream() );

	}

	public int getPort() {

		return super.getPort();

	}

	public InetAddress getLocalAddress() {

		return super.getLocalAddress();

	}

	public int getLocalPort() {

		return super.getLocalPort();

	}

	public String toString() {

		return "NbtSocket[addr=" + address +

				",port=" + super.getPort() +

				",localport=" + super.getLocalPort() + "]";

	}

	private void connect() throws IOException {

		byte[] buffer = new byte[BUFFER_SIZE];



		InputStream in = super.getInputStream();

		OutputStream out = super.getOutputStream();



		SessionServicePacket ssp0 = new SessionRequestPacket( calledName, NbtAddress.localhost.hostName );

		out.write( buffer, 0, ssp0.writeWireFormat( buffer, 0 ));



		setSoTimeout( soTimeout );

		switch( ssp0.readPacketType( in, buffer, 0 )) {

			case SessionServicePacket.POSITIVE_SESSION_RESPONSE:

				Log.println( Log.WARNINGS, "session service warning", " session established ok with " + address );

				return;

			case SessionServicePacket.NEGATIVE_SESSION_RESPONSE:

				int errorCode = (int)( in.read() & 0xFF );

				throw new NbtException( NbtException.ERR_SSN_SRVC, errorCode );

			case -1:

				throw new NbtException( NbtException.ERR_SSN_SRVC, NbtException.CONNECTION_REFUSED );

			default:

				throw new NbtException( NbtException.ERR_SSN_SRVC, 0 );

		}

	}

	public void close() throws IOException {

		Log.println( Log.WARNINGS, "netbios socket closed", this );

		super.close();

	}

}



More information about the jcifs mailing list