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 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: import jcifs.Config;
027: import jcifs.util.LogStream;
028:
029: /**
030: Do not use this class. Writing to the OutputStream of this type of socket
031: requires leaving a 4 byte prefix for the NBT header. IOW you must call
032: write( buf, 4, len ). Calling write( buf, 0, len ) will generate an error.
033: */
034:
035: public class NbtSocket extends Socket {
036:
037: private static final int SSN_SRVC_PORT = 139;
038: private static final int BUFFER_SIZE = 512;
039: private static final int DEFAULT_SO_TIMEOUT = 5000;
040:
041: private static LogStream log = LogStream.getInstance();
042:
043: private NbtAddress address;
044: private Name calledName;
045: private int soTimeout;
046:
047: public NbtSocket() {
048: super ();
049: }
050:
051: public NbtSocket(NbtAddress address, int port) throws IOException {
052: this (address, port, null, 0);
053: }
054:
055: public NbtSocket(NbtAddress address, int port,
056: InetAddress localAddr, int localPort) throws IOException {
057: this (address, null, port, localAddr, localPort);
058: }
059:
060: public NbtSocket(NbtAddress address, String calledName, int port,
061: InetAddress localAddr, int localPort) throws IOException {
062: super (address.getInetAddress(), (port == 0 ? SSN_SRVC_PORT
063: : port), localAddr, localPort);
064: this .address = address;
065: if (calledName == null) {
066: this .calledName = address.hostName;
067: } else {
068: this .calledName = new Name(calledName, 0x20, null);
069: }
070: soTimeout = Config.getInt("jcifs.netbios.soTimeout",
071: DEFAULT_SO_TIMEOUT);
072: connect();
073: }
074:
075: public NbtAddress getNbtAddress() {
076: return address;
077: }
078:
079: public InputStream getInputStream() throws IOException {
080: return new SocketInputStream(super .getInputStream());
081: }
082:
083: public OutputStream getOutputStream() throws IOException {
084: return new SocketOutputStream(super .getOutputStream());
085: }
086:
087: public int getPort() {
088: return super .getPort();
089: }
090:
091: public InetAddress getLocalAddress() {
092: return super .getLocalAddress();
093: }
094:
095: public int getLocalPort() {
096: return super .getLocalPort();
097: }
098:
099: public String toString() {
100: return "NbtSocket[addr=" + address + ",port=" + super .getPort()
101: + ",localport=" + super .getLocalPort() + "]";
102: }
103:
104: private void connect() throws IOException {
105: byte[] buffer = new byte[BUFFER_SIZE];
106: int type;
107: InputStream in;
108:
109: try {
110: in = super .getInputStream();
111: OutputStream out = super .getOutputStream();
112:
113: SessionServicePacket ssp0 = new SessionRequestPacket(
114: calledName, NbtAddress.localhost.hostName);
115: out.write(buffer, 0, ssp0.writeWireFormat(buffer, 0));
116:
117: setSoTimeout(soTimeout);
118: type = ssp0.readPacketType(in, buffer, 0);
119: } catch (IOException ioe) {
120: close();
121: throw ioe;
122: }
123:
124: switch (type) {
125: case SessionServicePacket.POSITIVE_SESSION_RESPONSE:
126: if (log.level > 2)
127: log.println("session established ok with " + 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 (log.level > 3)
144: log.println("close: " + this);
145: super.close();
146: }
147: }
|