KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > smb > server > SMBTransPacket


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.filesys.smb.server;
18
19 import org.alfresco.filesys.netbios.RFCNetBIOSProtocol;
20 import org.alfresco.filesys.smb.PacketType;
21 import org.alfresco.filesys.util.DataPacker;
22
23 /**
24  * SMB transact packet class
25  */

26
27 public class SMBTransPacket extends SMBSrvPacket
28 {
29
30     // Define the number of standard parameters
31

32     protected static final int STD_PARAMS = 14;
33
34     // Transaction status that indicates that this transaction has more data
35
// to be returned.
36

37     public static final int IsContinued = 234;
38
39     // Transact name, not used for transact 2
40

41     protected String JavaDoc m_transName;
42
43     // Parameter count for this transaction
44

45     protected int m_paramCnt;
46
47     // Multiplex identifier, to identify each transaction request
48

49     private static int m_nextMID = 1;
50
51     /**
52      * Construct an SMB transaction packet
53      *
54      * @param buf Buffer that contains the SMB transaction packet.
55      */

56     public SMBTransPacket(byte[] buf)
57     {
58         super(buf);
59     }
60
61     /**
62      * Construct an SMB transaction packet
63      *
64      * @param siz Size of packet to allocate.
65      */

66     public SMBTransPacket(int siz)
67     {
68         super(siz);
69
70         // Set the multiplex id for this transaction
71

72         setMultiplexId(getNextMultiplexId());
73     }
74
75     /**
76      * Get the next multiplex id to uniquely identify this transaction
77      *
78      * @return Unique multiplex id for this transaction
79      */

80     public final static int getNextMultiplexId()
81     {
82         return m_nextMID++;
83     }
84
85     /**
86      * Return the total parameter byte count
87      *
88      * @return int
89      */

90     public final int getTotalParameterCount()
91     {
92         return getParameter(0);
93     }
94
95     /**
96      * Return the total data byte count
97      *
98      * @return int
99      */

100     public final int getTotalDataCount()
101     {
102         return getParameter(1);
103     }
104
105     /**
106      * Return the parameter count size in bytes for this section
107      *
108      * @return int
109      */

110     public final int getParameterBlockCount()
111     {
112         return getParameter(9);
113     }
114
115     /**
116      * Return the parameter block offset
117      *
118      * @return Paramter block offset within the SMB packet
119      */

120     public final int getParameterBlockOffset()
121     {
122         return getParameter(10) + RFCNetBIOSProtocol.HEADER_LEN;
123     }
124
125     /**
126      * Return the data block size in bytes for this section
127      *
128      * @return int
129      */

130     public final int getDataBlockCount()
131     {
132         return getParameter(11);
133     }
134
135     /**
136      * Return the data block offset
137      *
138      * @return Data block offset within the SMB packet.
139      */

140     public final int getDataBlockOffset()
141     {
142         return getParameter(12) + RFCNetBIOSProtocol.HEADER_LEN;
143     }
144
145     /**
146      * Return the secondary parameter block size in bytes
147      *
148      * @return int
149      */

150     public final int getSecondaryParameterBlockCount()
151     {
152         return getParameter(2);
153     }
154
155     /**
156      * Return the secondary parameter block offset
157      *
158      * @return int
159      */

160     public final int getSecondaryParameterBlockOffset()
161     {
162         return getParameter(3) + RFCNetBIOSProtocol.HEADER_LEN;
163     }
164
165     /**
166      * Return the secondary parameter block displacement
167      *
168      * @return int
169      */

170     public final int getParameterBlockDisplacement()
171     {
172         return getParameter(4);
173     }
174
175     /**
176      * Return the secondary data block size in bytes
177      *
178      * @return int
179      */

180     public final int getSecondaryDataBlockCount()
181     {
182         return getParameter(5);
183     }
184
185     /**
186      * Return the secondary data block offset
187      *
188      * @return int
189      */

190     public final int getSecondaryDataBlockOffset()
191     {
192         return getParameter(6) + RFCNetBIOSProtocol.HEADER_LEN;
193     }
194
195     /**
196      * Return the secondary data block displacement
197      *
198      * @return int
199      */

200     public final int getDataBlockDisplacement()
201     {
202         return getParameter(7);
203     }
204
205     /**
206      * Return the transaction sub-command
207      *
208      * @return int
209      */

210     public final int getSubFunction()
211     {
212         return getParameter(14);
213     }
214
215     /**
216      * Unpack the parameter block into the supplied array.
217      *
218      * @param prmblk Array to unpack the parameter block words into.
219      */

220     public final void getParameterBlock(short[] prmblk) throws java.lang.ArrayIndexOutOfBoundsException JavaDoc
221     {
222
223         // Determine how many parameters are to be unpacked, check if the user
224
// buffer is long enough
225

226         int prmcnt = getParameter(3) / 2; // convert to number of words
227
if (prmblk.length < prmcnt)
228             throw new java.lang.ArrayIndexOutOfBoundsException JavaDoc();
229
230         // Get the offset to the parameter words, add the NetBIOS header length
231
// to the offset.
232

233         int pos = getParameter(4) + RFCNetBIOSProtocol.HEADER_LEN;
234
235         // Unpack the parameter words
236

237         byte[] buf = getBuffer();
238
239         for (int idx = 0; idx < prmcnt; idx++)
240         {
241
242             // Unpack the current parameter word
243

244             prmblk[idx] = (short) DataPacker.getIntelShort(buf, pos);
245             pos += 2;
246         }
247     }
248
249     /**
250      * Initialize the transact SMB packet
251      *
252      * @param pcnt Total parameter count for this transaction
253      * @param paramblk Parameter block data bytes
254      * @param plen Parameter block data length
255      * @param datablk Data block data bytes
256      * @param dlen Data block data length
257      */

258     public final void InitializeTransact(int pcnt, byte[] paramblk, int plen, byte[] datablk, int dlen)
259     {
260
261         // Set the SMB command code
262

263         if (m_transName == null)
264             setCommand(PacketType.Transaction2);
265         else
266             setCommand(PacketType.Transaction);
267
268         // Set the parameter count
269

270         setParameterCount(pcnt);
271
272         // Save the parameter count, add an extra parameter for the data byte count
273

274         m_paramCnt = pcnt;
275
276         // Initialize the parameters
277

278         setParameter(0, plen); // total parameter bytes being sent
279
setParameter(1, dlen); // total data bytes being sent
280

281         for (int i = 2; i < 9; setParameter(i++, 0))
282             ;
283
284         setParameter(9, plen); // parameter bytes sent in this packet
285
setParameter(11, dlen); // data bytes sent in this packet
286

287         setParameter(13, pcnt - STD_PARAMS); // number of setup words
288

289         // Get the data byte offset
290

291         int pos = getByteOffset();
292         int startPos = pos;
293
294         // Check if this is a named transaction, if so then store the name
295

296         int idx;
297         byte[] buf = getBuffer();
298
299         if (m_transName != null)
300         {
301
302             // Store the transaction name
303

304             byte[] nam = m_transName.getBytes();
305
306             for (idx = 0; idx < nam.length; idx++)
307                 buf[pos++] = nam[idx];
308         }
309
310         // Word align the buffer offset
311

312         if ((pos % 2) > 0)
313             pos++;
314
315         // Store the parameter block
316

317         if (paramblk != null)
318         {
319
320             // Set the parameter block offset
321

322             setParameter(10, pos - RFCNetBIOSProtocol.HEADER_LEN);
323
324             // Store the parameter block
325

326             for (idx = 0; idx < plen; idx++)
327                 buf[pos++] = paramblk[idx];
328         }
329         else
330         {
331
332             // Clear the parameter block offset
333

334             setParameter(10, 0);
335         }
336
337         // Word align the data block
338

339         if ((pos % 2) > 0)
340             pos++;
341
342         // Store the data block
343

344         if (datablk != null)
345         {
346
347             // Set the data block offset
348

349             setParameter(12, pos - RFCNetBIOSProtocol.HEADER_LEN);
350
351             // Store the data block
352

353             for (idx = 0; idx < dlen; idx++)
354                 buf[pos++] = datablk[idx];
355         }
356         else
357         {
358
359             // Zero the data block offset
360

361             setParameter(12, 0);
362         }
363
364         // Set the byte count for the SMB packet
365

366         setByteCount(pos - startPos);
367     }
368
369     /**
370      * Set the specifiec setup parameter within the SMB packet.
371      *
372      * @param idx Setup parameter index.
373      * @param val Setup parameter value.
374      */

375
376     public final void setSetupParameter(int idx, int val)
377     {
378         setParameter(STD_PARAMS + idx, val);
379     }
380
381     /**
382      * Set the transaction name for normal transactions
383      *
384      * @param tname Transaction name string
385      */

386
387     public final void setTransactionName(String JavaDoc tname)
388     {
389         m_transName = tname;
390     }
391 }
Popular Tags