001: /* jcifs smb client library in Java
002: * Copyright (C) 2000 "Michael B. Allen" <jcifs at samba dot org>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.knowgate.jcifs.netbios;
020:
021: import java.net.Socket;
022: import java.net.InetAddress;
023: import java.io.InputStream;
024: import java.io.OutputStream;
025: import java.io.IOException;
026:
027: import com.knowgate.debug.DebugFile;
028: import com.knowgate.jcifs.Config;
029:
030: /**
031: Do not use this class. Writing to the OutputStream of this type of socket
032: requires leaving a 4 byte prefix for the NBT header. IOW you must call
033: write( buf, 4, len ). Calling write( buf, 0, len ) will generate an error.
034: */
035:
036: public class NbtSocket extends Socket {
037:
038: private static final int SSN_SRVC_PORT = 139;
039: private static final int BUFFER_SIZE = 512;
040: private static final int DEFAULT_SO_TIMEOUT = 5000;
041:
042: private NbtAddress address;
043: private Name calledName;
044: private int soTimeout;
045:
046: public NbtSocket() {
047: super ();
048: }
049:
050: public NbtSocket(NbtAddress address, int port) throws IOException {
051: this (address, port, null, 0);
052: }
053:
054: public NbtSocket(NbtAddress address, int port,
055: InetAddress localAddr, int localPort) throws IOException {
056: this (address, null, port, localAddr, localPort);
057: }
058:
059: public NbtSocket(NbtAddress address, String calledName, int port,
060: InetAddress localAddr, int localPort) throws IOException {
061: super (address.getInetAddress(), (port == 0 ? SSN_SRVC_PORT
062: : port), localAddr, localPort);
063: this .address = address;
064: if (calledName == null) {
065: this .calledName = address.hostName;
066: } else {
067: this .calledName = new Name(calledName, 0x20, null);
068: }
069: soTimeout = Config.getInt("jcifs.netbios.soTimeout",
070: DEFAULT_SO_TIMEOUT);
071: connect();
072: }
073:
074: public NbtAddress getNbtAddress() {
075: return address;
076: }
077:
078: public InputStream getInputStream() throws IOException {
079: return new SocketInputStream(super .getInputStream());
080: }
081:
082: public OutputStream getOutputStream() throws IOException {
083: return new SocketOutputStream(super .getOutputStream());
084: }
085:
086: public int getPort() {
087: return super .getPort();
088: }
089:
090: public InetAddress getLocalAddress() {
091: return super .getLocalAddress();
092: }
093:
094: public int getLocalPort() {
095: return super .getLocalPort();
096: }
097:
098: public String toString() {
099: return "NbtSocket[addr=" + address + ",port=" + super .getPort()
100: + ",localport=" + super .getLocalPort() + "]";
101: }
102:
103: private void connect() throws IOException {
104: byte[] buffer = new byte[BUFFER_SIZE];
105: int type;
106: InputStream in;
107:
108: try {
109: in = super .getInputStream();
110: OutputStream out = super .getOutputStream();
111:
112: SessionServicePacket ssp0 = new SessionRequestPacket(
113: calledName, NbtAddress.localhost.hostName);
114: out.write(buffer, 0, ssp0.writeWireFormat(buffer, 0));
115:
116: setSoTimeout(soTimeout);
117: type = ssp0.readPacketType(in, buffer, 0);
118: } catch (IOException ioe) {
119: close();
120: throw ioe;
121: }
122:
123: switch (type) {
124: case SessionServicePacket.POSITIVE_SESSION_RESPONSE:
125: if (DebugFile.trace)
126: DebugFile.writeln("session established ok with "
127: + address);
128: return;
129: case SessionServicePacket.NEGATIVE_SESSION_RESPONSE:
130: int errorCode = (int) (in.read() & 0xFF);
131: close();
132: throw new NbtException(NbtException.ERR_SSN_SRVC, errorCode);
133: case -1:
134: throw new NbtException(NbtException.ERR_SSN_SRVC,
135: NbtException.CONNECTION_REFUSED);
136: default:
137: close();
138: throw new NbtException(NbtException.ERR_SSN_SRVC, 0);
139: }
140: }
141:
142: public void close() throws IOException {
143: if (DebugFile.trace)
144: DebugFile.writeln("close: " + this);
145: super.close();
146: }
147: }
|