KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcifs > smb > SmbComTransaction


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.smb;
20
21 import java.util.Enumeration JavaDoc;
22 import jcifs.Config;
23 import jcifs.util.Hexdump;
24
25 abstract class SmbComTransaction extends ServerMessageBlock implements Enumeration JavaDoc {
26
27     private static final int DEFAULT_MAX_DATA_COUNT =
28             Config.getInt( "jcifs.smb.client.transaction_buf_size",
29                     SmbComTransaction.TRANSACTION_BUF_SIZE ) - 512;
30
31     // relative to headerStart
32
private static final int PRIMARY_SETUP_OFFSET = 61;
33     private static final int SECONDARY_PARAMETER_OFFSET = 51;
34
35     private static final int DISCONNECT_TID = 0x01;
36     private static final int ONE_WAY_TRANSACTION = 0x02;
37
38     private static final int PADDING_SIZE = 2;
39
40     private int flags = 0x00;
41     private int fid;
42     private int pad = 0;
43     private int pad1 = 0;
44     private boolean hasMore = true;
45     private boolean isPrimary = true;
46     private int bufParameterOffset;
47     private int bufDataOffset;
48
49     static final int TRANSACTION_BUF_SIZE = 0xFFFF;
50
51     static final byte TRANS2_FIND_FIRST2 = (byte)0x01;
52     static final byte TRANS2_FIND_NEXT2 = (byte)0x02;
53     static final byte TRANS2_QUERY_FS_INFORMATION = (byte)0x03;
54     static final byte TRANS2_QUERY_PATH_INFORMATION = (byte)0x05;
55     static final byte TRANS2_GET_DFS_REFERRAL = (byte)0x10;
56     static final byte TRANS2_SET_FILE_INFORMATION = (byte)0x08;
57
58     static final int NET_SHARE_ENUM = 0x0000;
59     static final int NET_SERVER_ENUM2 = 0x0068;
60     static final int NET_SERVER_ENUM3 = 0x00D7;
61
62     static final byte TRANS_PEEK_NAMED_PIPE = (byte)0x23;
63     static final byte TRANS_WAIT_NAMED_PIPE = (byte)0x53;
64     static final byte TRANS_CALL_NAMED_PIPE = (byte)0x54;
65     static final byte TRANS_TRANSACT_NAMED_PIPE = (byte)0x26;
66
67     protected int primarySetupOffset;
68     protected int secondaryParameterOffset;
69     protected int parameterCount;
70     protected int parameterOffset;
71     protected int parameterDisplacement;
72     protected int dataCount;
73     protected int dataOffset;
74     protected int dataDisplacement;
75
76     int totalParameterCount;
77     int totalDataCount;
78     int maxParameterCount;
79     int maxDataCount = DEFAULT_MAX_DATA_COUNT;
80     byte maxSetupCount;
81     int timeout = 0;
82     int setupCount = 1;
83     byte subCommand;
84     String JavaDoc name = "";
85     int maxBufferSize; // set in SmbTransport.sendTransaction() before nextElement called
86

87     byte[] txn_buf;
88
89     SmbComTransaction() {
90         maxParameterCount = 1024;
91         primarySetupOffset = PRIMARY_SETUP_OFFSET;
92         secondaryParameterOffset = SECONDARY_PARAMETER_OFFSET;
93     }
94
95     void reset() {
96         super.reset();
97         isPrimary = hasMore = true;
98     }
99     void reset( int key, String JavaDoc lastName ) {
100         reset();
101     }
102     public boolean hasMoreElements() {
103         return hasMore;
104     }
105     public Object JavaDoc nextElement() {
106         if( isPrimary ) {
107             isPrimary = false;
108
109             parameterOffset = primarySetupOffset + ( setupCount * 2 ) + 2;
110             if (command != SMB_COM_NT_TRANSACT) {
111                 if( command == SMB_COM_TRANSACTION && isResponse() == false ) {
112                     parameterOffset += stringWireLength( name, parameterOffset );
113                 }
114             } else if (command == SMB_COM_NT_TRANSACT) {
115                 parameterOffset += 2;
116             }
117             pad = parameterOffset % PADDING_SIZE;
118             pad = pad == 0 ? 0 : PADDING_SIZE - pad;
119             parameterOffset += pad;
120
121             totalParameterCount = writeParametersWireFormat( txn_buf, bufParameterOffset );
122             bufDataOffset = totalParameterCount; // data comes right after data
123

124             int available = maxBufferSize - parameterOffset;
125             parameterCount = Math.min( totalParameterCount, available );
126             available -= parameterCount;
127
128             dataOffset = parameterOffset + parameterCount;
129             pad1 = dataOffset % PADDING_SIZE;
130             pad1 = pad1 == 0 ? 0 : PADDING_SIZE - pad1;
131             dataOffset += pad1;
132
133             totalDataCount = writeDataWireFormat( txn_buf, bufDataOffset );
134
135             dataCount = Math.min( totalDataCount, available );
136         } else {
137             if (command != SMB_COM_NT_TRANSACT) {
138                 command = SMB_COM_TRANSACTION_SECONDARY;
139             } else {
140                 command = SMB_COM_NT_TRANSACT_SECONDARY;
141             }
142             // totalParameterCount and totalDataCount are set ok from primary
143

144             parameterOffset = SECONDARY_PARAMETER_OFFSET;
145             if(( totalParameterCount - parameterDisplacement ) > 0 ) {
146                 pad = parameterOffset % PADDING_SIZE;
147                 pad = pad == 0 ? 0 : PADDING_SIZE - pad;
148                 parameterOffset += pad;
149             }
150
151             // caclulate parameterDisplacement before calculating new parameterCount
152
parameterDisplacement += parameterCount;
153
154             int available = maxBufferSize - parameterOffset - pad;
155             parameterCount = Math.min( totalParameterCount - parameterDisplacement, available);
156             available -= parameterCount;
157
158             dataOffset = parameterOffset + parameterCount;
159             pad1 = dataOffset % PADDING_SIZE;
160             pad1 = pad1 == 0 ? 0 : PADDING_SIZE - pad1;
161             dataOffset += pad1;
162
163             dataDisplacement += dataCount;
164
165             available -= pad1;
166             dataCount = Math.min( totalDataCount - dataDisplacement, available );
167         }
168         if(( parameterDisplacement + parameterCount ) >= totalParameterCount &&
169                     ( dataDisplacement + dataCount ) >= totalDataCount ) {
170             hasMore = false;
171         }
172         return this;
173     }
174     int writeParameterWordsWireFormat( byte[] dst, int dstIndex ) {
175         int start = dstIndex;
176
177         writeInt2( totalParameterCount, dst, dstIndex );
178         dstIndex += 2;
179         writeInt2( totalDataCount, dst, dstIndex );
180         dstIndex += 2;
181         if( command != SMB_COM_TRANSACTION_SECONDARY ) {
182             writeInt2( maxParameterCount, dst, dstIndex );
183             dstIndex += 2;
184             writeInt2( maxDataCount, dst, dstIndex );
185             dstIndex += 2;
186             dst[dstIndex++] = maxSetupCount;
187             dst[dstIndex++] = (byte)0x00; // Reserved1
188
writeInt2( flags, dst, dstIndex );
189             dstIndex += 2;
190             writeInt4( timeout, dst, dstIndex );
191             dstIndex += 4;
192             dst[dstIndex++] = (byte)0x00; // Reserved2
193
dst[dstIndex++] = (byte)0x00;
194         }
195         writeInt2( parameterCount, dst, dstIndex );
196         dstIndex += 2;
197 // writeInt2(( parameterCount == 0 ? 0 : parameterOffset ), dst, dstIndex );
198
writeInt2(parameterOffset, dst, dstIndex );
199         dstIndex += 2;
200         if( command == SMB_COM_TRANSACTION_SECONDARY ) {
201             writeInt2( parameterDisplacement, dst, dstIndex );
202             dstIndex += 2;
203         }
204         writeInt2( dataCount, dst, dstIndex );
205         dstIndex += 2;
206         writeInt2(( dataCount == 0 ? 0 : dataOffset ), dst, dstIndex );
207         dstIndex += 2;
208         if( command == SMB_COM_TRANSACTION_SECONDARY ) {
209             writeInt2( dataDisplacement, dst, dstIndex );
210             dstIndex += 2;
211         } else {
212             dst[dstIndex++] = (byte)setupCount;
213             dst[dstIndex++] = (byte)0x00; // Reserved3
214
dstIndex += writeSetupWireFormat( dst, dstIndex );
215         }
216
217         return dstIndex - start;
218     }
219     int writeBytesWireFormat( byte[] dst, int dstIndex ) {
220         int start = dstIndex;
221         int p = pad;
222
223         if( command == SMB_COM_TRANSACTION && isResponse() == false ) {
224             dstIndex += writeString( name, dst, dstIndex );
225         }
226
227         if( parameterCount > 0 ) {
228             while( p-- > 0 ) {
229                 dst[dstIndex++] = (byte)0x00; // Pad
230
}
231
232             System.arraycopy( txn_buf, bufParameterOffset, dst, dstIndex, parameterCount );
233             dstIndex += parameterCount;
234         }
235
236         if( dataCount > 0 ) {
237             p = pad1;
238             while( p-- > 0 ) {
239                 dst[dstIndex++] = (byte)0x00; // Pad1
240
}
241             System.arraycopy( txn_buf, bufDataOffset, dst, dstIndex, dataCount );
242             bufDataOffset += dataCount;
243             dstIndex += dataCount;
244         }
245
246         return dstIndex - start;
247     }
248     int readParameterWordsWireFormat( byte[] buffer, int bufferIndex ) {
249         return 0;
250     }
251     int readBytesWireFormat( byte[] buffer, int bufferIndex ) {
252         return 0;
253     }
254
255     abstract int writeSetupWireFormat( byte[] dst, int dstIndex );
256     abstract int writeParametersWireFormat( byte[] dst, int dstIndex );
257     abstract int writeDataWireFormat( byte[] dst, int dstIndex );
258     abstract int readSetupWireFormat( byte[] buffer, int bufferIndex, int len );
259     abstract int readParametersWireFormat( byte[] buffer, int bufferIndex, int len );
260     abstract int readDataWireFormat( byte[] buffer, int bufferIndex, int len );
261
262     public String JavaDoc toString() {
263         return new String JavaDoc( super.toString() +
264             ",totalParameterCount=" + totalParameterCount +
265             ",totalDataCount=" + totalDataCount +
266             ",maxParameterCount=" + maxParameterCount +
267             ",maxDataCount=" + maxDataCount +
268             ",maxSetupCount=" + (int)maxSetupCount +
269             ",flags=0x" + Hexdump.toHexString( flags, 2 ) +
270             ",timeout=" + timeout +
271             ",parameterCount=" + parameterCount +
272             ",parameterOffset=" + parameterOffset +
273             ",parameterDisplacement=" + parameterDisplacement +
274             ",dataCount=" + dataCount +
275             ",dataOffset=" + dataOffset +
276             ",dataDisplacement=" + dataDisplacement +
277             ",setupCount=" + setupCount +
278             ",pad=" + pad +
279             ",pad1=" + pad1 );
280     }
281 }
282
Popular Tags