KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > jcifs > netbios > NbtSocket


1 /* jcifs smb client library in Java
2  * Copyright (C) 2000 "Michael B. Allen" <jcifs at samba dot org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package com.knowgate.jcifs.netbios;
20
21 import java.net.Socket JavaDoc;
22 import java.net.InetAddress JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26
27 import com.knowgate.debug.DebugFile;
28 import com.knowgate.jcifs.Config;
29
30 /**
31 Do not use this class. Writing to the OutputStream of this type of socket
32 requires leaving a 4 byte prefix for the NBT header. IOW you must call
33 write( buf, 4, len ). Calling write( buf, 0, len ) will generate an error.
34  */

35
36 public class NbtSocket extends Socket JavaDoc {
37
38     private static final int SSN_SRVC_PORT = 139;
39     private static final int BUFFER_SIZE = 512;
40     private static final int DEFAULT_SO_TIMEOUT = 5000;
41
42     private NbtAddress address;
43     private Name calledName;
44     private int soTimeout;
45
46     public NbtSocket() {
47         super();
48     }
49     public NbtSocket( NbtAddress address, int port ) throws IOException JavaDoc {
50         this( address, port, null, 0 );
51     }
52     public NbtSocket( NbtAddress address, int port,
53                                 InetAddress JavaDoc localAddr, int localPort ) throws IOException JavaDoc {
54         this( address, null, port, localAddr, localPort );
55     }
56     public NbtSocket( NbtAddress address, String JavaDoc calledName, int port,
57                                 InetAddress JavaDoc localAddr, int localPort ) throws IOException JavaDoc {
58         super( address.getInetAddress(), ( port == 0 ? SSN_SRVC_PORT : port ),
59                                 localAddr, localPort );
60         this.address = address;
61         if( calledName == null ) {
62             this.calledName = address.hostName;
63         } else {
64             this.calledName = new Name( calledName, 0x20, null );
65         }
66         soTimeout = Config.getInt( "jcifs.netbios.soTimeout", DEFAULT_SO_TIMEOUT );
67         connect();
68     }
69
70     public NbtAddress getNbtAddress() {
71         return address;
72     }
73     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
74         return new SocketInputStream( super.getInputStream() );
75     }
76     public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
77         return new SocketOutputStream( super.getOutputStream() );
78     }
79     public int getPort() {
80         return super.getPort();
81     }
82     public InetAddress JavaDoc getLocalAddress() {
83         return super.getLocalAddress();
84     }
85     public int getLocalPort() {
86         return super.getLocalPort();
87     }
88     public String JavaDoc toString() {
89         return "NbtSocket[addr=" + address +
90                 ",port=" + super.getPort() +
91                 ",localport=" + super.getLocalPort() + "]";
92     }
93     private void connect() throws IOException JavaDoc {
94         byte[] buffer = new byte[BUFFER_SIZE];
95         int type;
96         InputStream JavaDoc in;
97
98         try {
99             in = super.getInputStream();
100             OutputStream JavaDoc out = super.getOutputStream();
101
102             SessionServicePacket ssp0 = new SessionRequestPacket( calledName, NbtAddress.localhost.hostName );
103             out.write( buffer, 0, ssp0.writeWireFormat( buffer, 0 ));
104
105             setSoTimeout( soTimeout );
106             type = ssp0.readPacketType( in, buffer, 0 );
107         } catch( IOException JavaDoc ioe ) {
108             close();
109             throw ioe;
110         }
111
112         switch( type ) {
113             case SessionServicePacket.POSITIVE_SESSION_RESPONSE:
114                 if( DebugFile.trace )
115                     DebugFile.writeln( "session established ok with " + address );
116                 return;
117             case SessionServicePacket.NEGATIVE_SESSION_RESPONSE:
118                 int errorCode = (int)( in.read() & 0xFF );
119                 close();
120                 throw new NbtException( NbtException.ERR_SSN_SRVC, errorCode );
121             case -1:
122                 throw new NbtException( NbtException.ERR_SSN_SRVC, NbtException.CONNECTION_REFUSED );
123             default:
124                 close();
125                 throw new NbtException( NbtException.ERR_SSN_SRVC, 0 );
126         }
127     }
128     public void close() throws IOException JavaDoc {
129         if( DebugFile.trace )
130             DebugFile.writeln( "close: " + this );
131         super.close();
132     }
133 }
134
Popular Tags