KickJava   Java API By Example, From Geeks To Geeks.

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


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  * NT Transaction Packet Class
25  */

26 public class NTTransPacket extends SMBSrvPacket
27 {
28
29     // Define the number of standard parameter words/bytes
30

31     private static final int StandardParams = 19;
32     private static final int ParameterBytes = 36; // 8 x 32bit params + max setup count byte +
33
// setup count byte + reserved word
34

35     // Standard reply word count
36

37     private static final int ReplyParams = 18;
38
39     // Offset to start of NT parameters from start of packet
40

41     private static final int NTMaxSetupCount = SMBPacket.PARAMWORDS;
42     private static final int NTParams = SMBPacket.PARAMWORDS + 3;
43     private static final int NTSetupCount = NTParams + 32;
44     private static final int NTFunction = NTSetupCount + 1;
45
46     // Default return parameter/data byte counts
47

48     private static final int DefaultReturnParams = 4;
49     private static final int DefaultReturnData = 1024;
50
51     /**
52      * Default constructor
53      */

54     public NTTransPacket()
55     {
56         super();
57     }
58
59     /**
60      * Class constructor
61      *
62      * @param buf byte[]
63      */

64     public NTTransPacket(byte[] buf)
65     {
66         super(buf);
67     }
68
69     /**
70      * Copy constructor
71      *
72      * @param pkt NTTransPacket
73      */

74     public NTTransPacket(NTTransPacket pkt)
75     {
76         super(pkt);
77     }
78
79     /**
80      * Return the data block size
81      *
82      * @return Data block size in bytes
83      */

84     public final int getDataLength()
85     {
86         return getNTParameter(6);
87     }
88
89     /**
90      * Return the data block offset
91      *
92      * @return Data block offset within the SMB packet.
93      */

94     public final int getDataOffset()
95     {
96         return getNTParameter(7) + RFCNetBIOSProtocol.HEADER_LEN;
97     }
98
99     /**
100      * Unpack the parameter block
101      *
102      * @return int[]
103      */

104     public final int[] getParameterBlock()
105     {
106
107         // Get the parameter count and allocate the parameter buffer
108

109         int prmcnt = getParameterBlockCount() / 4; // convert to number of ints
110
if (prmcnt <= 0)
111             return null;
112         int[] prmblk = new int[prmcnt];
113
114         // Get the offset to the parameter words, add the NetBIOS header length
115
// to the offset.
116

117         int pos = getParameterBlockOffset();
118
119         // Unpack the parameter ints
120

121         setBytePointer(pos, getByteCount());
122
123         for (int idx = 0; idx < prmcnt; idx++)
124         {
125
126             // Unpack the current parameter value
127

128             prmblk[idx] = unpackInt();
129         }
130
131         // Return the parameter block
132

133         return prmblk;
134     }
135
136     /**
137      * Return the total parameter count
138      *
139      * @return int
140      */

141     public final int getTotalParameterCount()
142     {
143         return getNTParameter(0);
144     }
145
146     /**
147      * Return the total data count
148      *
149      * @return int
150      */

151     public final int getTotalDataCount()
152     {
153         return getNTParameter(1);
154     }
155
156     /**
157      * Return the maximum parameter block length to be returned
158      *
159      * @return int
160      */

161     public final int getMaximumParameterReturn()
162     {
163         return getNTParameter(2);
164     }
165
166     /**
167      * Return the maximum data block length to be returned
168      *
169      * @return int
170      */

171     public final int getMaximumDataReturn()
172     {
173         return getNTParameter(3);
174     }
175
176     /**
177      * Return the parameter block count
178      *
179      * @return int
180      */

181     public final int getParameterBlockCount()
182     {
183         return getNTParameter(getCommand() == PacketType.NTTransact ? 4 : 2);
184     }
185
186     /**
187      * Return the parameter block offset
188      *
189      * @return int
190      */

191     public final int getParameterBlockOffset()
192     {
193         return getNTParameter(getCommand() == PacketType.NTTransact ? 5 : 3) + RFCNetBIOSProtocol.HEADER_LEN;
194     }
195
196     /**
197      * Return the paramater block displacement
198      *
199      * @return int
200      */

201     public final int getParameterBlockDisplacement()
202     {
203         return getNTParameter(4);
204     }
205
206     /**
207      * Return the data block count
208      *
209      * @return int
210      */

211     public final int getDataBlockCount()
212     {
213         return getNTParameter(getCommand() == PacketType.NTTransact ? 6 : 5);
214     }
215
216     /**
217      * Return the data block offset
218      *
219      * @return int
220      */

221     public final int getDataBlockOffset()
222     {
223         return getNTParameter(getCommand() == PacketType.NTTransact ? 7 : 6) + RFCNetBIOSProtocol.HEADER_LEN;
224     }
225
226     /**
227      * Return the data block displacment
228      *
229      * @return int
230      */

231     public final int getDataBlockDisplacement()
232     {
233         return getNTParameter(7);
234     }
235
236     /**
237      * Get an NT parameter (32bit)
238      *
239      * @param idx int
240      * @return int
241      */

242     protected final int getNTParameter(int idx)
243     {
244         int pos = NTParams + (4 * idx);
245         return DataPacker.getIntelInt(getBuffer(), pos);
246     }
247
248     /**
249      * Get the setup parameter count
250      *
251      * @return int
252      */

253     public final int getSetupCount()
254     {
255         byte[] buf = getBuffer();
256         return (int) buf[NTSetupCount] & 0xFF;
257     }
258
259     /**
260      * Return the offset to the setup words data
261      *
262      * @return int
263      */

264     public final int getSetupOffset()
265     {
266         return NTFunction + 2;
267     }
268
269     /**
270      * Get the NT transaction function code
271      *
272      * @return int
273      */

274     public final int getNTFunction()
275     {
276         byte[] buf = getBuffer();
277         return DataPacker.getIntelShort(buf, NTFunction);
278     }
279
280     /**
281      * Initialize the transact SMB packet
282      *
283      * @param func NT transaction function code
284      * @param paramblk Parameter block data bytes
285      * @param plen Parameter block data length
286      * @param datablk Data block data bytes
287      * @param dlen Data block data length
288      * @param setupcnt Number of setup parameters
289      */

290     public final void initTransact(int func, byte[] paramblk, int plen, byte[] datablk, int dlen, int setupcnt)
291     {
292         initTransact(func, paramblk, plen, datablk, dlen, setupcnt, DefaultReturnParams, DefaultReturnData);
293     }
294
295     /**
296      * Initialize the transact SMB packet
297      *
298      * @param func NT transaction function code
299      * @param paramblk Parameter block data bytes
300      * @param plen Parameter block data length
301      * @param datablk Data block data bytes
302      * @param dlen Data block data length
303      * @param setupcnt Number of setup parameters
304      * @param maxPrm Maximum parameter bytes to return
305      * @param maxData Maximum data bytes to return
306      */

307     public final void initTransact(int func, byte[] paramblk, int plen, byte[] datablk, int dlen, int setupcnt,
308             int maxPrm, int maxData)
309     {
310
311         // Set the SMB command and parameter count
312

313         setCommand(PacketType.NTTransact);
314         setParameterCount(StandardParams + setupcnt);
315
316         // Initialize the parameters
317

318         setTotalParameterCount(plen);
319         setTotalDataCount(dlen);
320         setMaximumParameterReturn(maxPrm);
321         setMaximumDataReturn(maxData);
322         setParameterCount(plen);
323         setParameterBlockOffset(0);
324         setDataBlockCount(dlen);
325         setDataBlockOffset(0);
326
327         setSetupCount(setupcnt);
328         setNTFunction(func);
329
330         resetBytePointerAlign();
331
332         // Pack the parameter block
333

334         if (paramblk != null)
335         {
336
337             // Set the parameter block offset, from the start of the SMB packet
338

339             setParameterBlockOffset(getPosition());
340
341             // Pack the parameter block
342

343             packBytes(paramblk, plen);
344         }
345
346         // Pack the data block
347

348         if (datablk != null)
349         {
350
351             // Align the byte area offset and set the data block offset in the request
352

353             alignBytePointer();
354             setDataBlockOffset(getPosition());
355
356             // Pack the data block
357

358             packBytes(datablk, dlen);
359         }
360
361         // Set the byte count for the SMB packet
362

363         setByteCount();
364     }
365
366     /**
367      * Initialize the NT transaction reply
368      *
369      * @param paramblk Parameter block data bytes
370      * @param plen Parameter block data length
371      * @param datablk Data block data bytes
372      * @param dlen Data block data length
373      */

374     public final void initTransactReply(byte[] paramblk, int plen, byte[] datablk, int dlen)
375     {
376
377         // Set the parameter count
378

379         setParameterCount(ReplyParams);
380         setSetupCount(0);
381
382         // Initialize the parameters
383

384         setTotalParameterCount(plen);
385         setTotalDataCount(dlen);
386
387         setReplyParameterCount(plen);
388         setReplyParameterOffset(0);
389         setReplyParameterDisplacement(0);
390
391         setReplyDataCount(dlen);
392         setDataBlockOffset(0);
393         setReplyDataDisplacement(0);
394
395         setSetupCount(0);
396
397         resetBytePointerAlign();
398
399         // Pack the parameter block
400

401         if (paramblk != null)
402         {
403
404             // Set the parameter block offset, from the start of the SMB packet
405

406             setReplyParameterOffset(getPosition() - 4);
407
408             // Pack the parameter block
409

410             packBytes(paramblk, plen);
411         }
412
413         // Pack the data block
414

415         if (datablk != null)
416         {
417
418             // Align the byte area offset and set the data block offset in the request
419

420             alignBytePointer();
421             setReplyDataOffset(getPosition() - 4);
422
423             // Pack the data block
424

425             packBytes(datablk, dlen);
426         }
427
428         // Set the byte count for the SMB packet
429

430         setByteCount();
431     }
432
433     /**
434      * Initialize the NT transaction reply
435      *
436      * @param paramblk Parameter block data bytes
437      * @param plen Parameter block data length
438      * @param datablk Data block data bytes
439      * @param dlen Data block data length
440      * @param setupCnt Number of setup parameter
441      */

442     public final void initTransactReply(byte[] paramblk, int plen, byte[] datablk, int dlen, int setupCnt)
443     {
444
445         // Set the parameter count, add the setup parameter count
446

447         setParameterCount(ReplyParams + setupCnt);
448         setSetupCount(setupCnt);
449
450         // Initialize the parameters
451

452         setTotalParameterCount(plen);
453         setTotalDataCount(dlen);
454
455         setReplyParameterCount(plen);
456         setReplyParameterOffset(0);
457         setReplyParameterDisplacement(0);
458
459         setReplyDataCount(dlen);
460         setDataBlockOffset(0);
461         setReplyDataDisplacement(0);
462
463         setSetupCount(setupCnt);
464
465         resetBytePointerAlign();
466
467         // Pack the parameter block
468

469         if (paramblk != null)
470         {
471
472             // Set the parameter block offset, from the start of the SMB packet
473

474             setReplyParameterOffset(getPosition() - 4);
475
476             // Pack the parameter block
477

478             packBytes(paramblk, plen);
479         }
480
481         // Pack the data block
482

483         if (datablk != null)
484         {
485
486             // Align the byte area offset and set the data block offset in the request
487

488             alignBytePointer();
489             setReplyDataOffset(getPosition() - 4);
490
491             // Pack the data block
492

493             packBytes(datablk, dlen);
494         }
495
496         // Set the byte count for the SMB packet
497

498         setByteCount();
499     }
500
501     /**
502      * Set the total parameter count
503      *
504      * @param cnt int
505      */

506     public final void setTotalParameterCount(int cnt)
507     {
508         setNTParameter(0, cnt);
509     }
510
511     /**
512      * Set the total data count
513      *
514      * @param cnt int
515      */

516     public final void setTotalDataCount(int cnt)
517     {
518         setNTParameter(1, cnt);
519     }
520
521     /**
522      * Set the maximum return parameter count
523      *
524      * @param cnt int
525      */

526     public final void setMaximumParameterReturn(int cnt)
527     {
528         setNTParameter(2, cnt);
529     }
530
531     /**
532      * Set the maximum return data count
533      *
534      * @param cnt int
535      */

536     public final void setMaximumDataReturn(int cnt)
537     {
538         setNTParameter(3, cnt);
539     }
540
541     /**
542      * Set the paramater block count
543      *
544      * @param disp int
545      */

546     public final void setTransactParameterCount(int cnt)
547     {
548         setNTParameter(4, cnt);
549     }
550
551     /**
552      * Set the reply parameter byte count
553      *
554      * @param cnt int
555      */

556     public final void setReplyParameterCount(int cnt)
557     {
558         setNTParameter(2, cnt);
559     }
560
561     /**
562      * Set the reply parameter offset
563      *
564      * @param off int
565      */

566     public final void setReplyParameterOffset(int off)
567     {
568         setNTParameter(3, off);
569     }
570
571     /**
572      * Set the reply parameter bytes displacement
573      *
574      * @param disp int
575      */

576     public final void setReplyParameterDisplacement(int disp)
577     {
578         setNTParameter(4, disp);
579     }
580
581     /**
582      * Set the reply data byte count
583      *
584      * @param cnt int
585      */

586     public final void setReplyDataCount(int cnt)
587     {
588         setNTParameter(5, cnt);
589     }
590
591     /**
592      * Set the reply data offset
593      *
594      * @param off int
595      */

596     public final void setReplyDataOffset(int off)
597     {
598         setNTParameter(6, off);
599     }
600
601     /**
602      * Set the reply data bytes displacement
603      *
604      * @param disp int
605      */

606     public final void setReplyDataDisplacement(int disp)
607     {
608         setNTParameter(7, disp);
609     }
610
611     /**
612      * Set the parameter block offset within the packet
613      *
614      * @param off int
615      */

616     public final void setParameterBlockOffset(int off)
617     {
618         setNTParameter(5, off != 0 ? off - RFCNetBIOSProtocol.HEADER_LEN : 0);
619     }
620
621     /**
622      * Set the data block count
623      *
624      * @param cnt int
625      */

626     public final void setDataBlockCount(int cnt)
627     {
628         setNTParameter(6, cnt);
629     }
630
631     /**
632      * Set the data block offset
633      *
634      * @param disp int
635      */

636     public final void setDataBlockOffset(int off)
637     {
638         setNTParameter(7, off != 0 ? off - RFCNetBIOSProtocol.HEADER_LEN : 0);
639     }
640
641     /**
642      * Set an NT parameter (32bit)
643      *
644      * @param idx int
645      * @param val int
646      */

647     public final void setNTParameter(int idx, int val)
648     {
649         int pos = NTParams + (4 * idx);
650         DataPacker.putIntelInt(val, getBuffer(), pos);
651     }
652
653     /**
654      * Set the maximum setup parameter count
655      *
656      * @param cnt Maximum count of setup paramater words
657      */

658     public final void setMaximumSetupCount(int cnt)
659     {
660         byte[] buf = getBuffer();
661         buf[NTMaxSetupCount] = (byte) cnt;
662     }
663
664     /**
665      * Set the setup parameter count
666      *
667      * @param cnt Count of setup paramater words
668      */

669     public final void setSetupCount(int cnt)
670     {
671         byte[] buf = getBuffer();
672         buf[NTSetupCount] = (byte) cnt;
673     }
674
675     /**
676      * Set the specified setup parameter
677      *
678      * @param setupIdx Setup parameter index
679      * @param setupVal Setup parameter value
680      */

681     public final void setSetupParameter(int setupIdx, int setupVal)
682     {
683         int pos = NTSetupCount + 1 + (setupIdx * 2);
684         DataPacker.putIntelShort(setupVal, getBuffer(), pos);
685     }
686
687     /**
688      * Set the NT transaction function code
689      *
690      * @param func int
691      */

692     public final void setNTFunction(int func)
693     {
694         byte[] buf = getBuffer();
695         DataPacker.putIntelShort(func, buf, NTFunction);
696     }
697
698     /**
699      * Reset the byte/parameter pointer area for packing/unpacking setup paramaters items to the
700      * packet
701      */

702     public final void resetSetupPointer()
703     {
704         m_pos = NTFunction + 2;
705         m_endpos = m_pos;
706     }
707
708     /**
709      * Reset the byte/parameter pointer area for packing/unpacking the transaction data block
710      */

711     public final void resetDataBlockPointer()
712     {
713         m_pos = getDataBlockOffset();
714         m_endpos = m_pos;
715     }
716
717     /**
718      * Reset the byte/parameter pointer area for packing/unpacking the transaction paramater block
719      */

720     public final void resetParameterBlockPointer()
721     {
722         m_pos = getParameterBlockOffset();
723         m_endpos = m_pos;
724     }
725 }
726
Popular Tags