KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > 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 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 import jcifs.Config;
27 import jcifs.util.LogStream;
28
29 /**
30 Do not use this class. Writing to the OutputStream of this type of socket
31 requires leaving a 4 byte prefix for the NBT header. IOW you must call
32 write( buf, 4, len ). Calling write( buf, 0, len ) will generate an error.
33  */

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