KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > smb > dcerpc > server > DCESrvPacket


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.dcerpc.server;
18
19 import org.alfresco.filesys.netbios.RFCNetBIOSProtocol;
20 import org.alfresco.filesys.smb.PacketType;
21 import org.alfresco.filesys.smb.dcerpc.DCECommand;
22 import org.alfresco.filesys.smb.dcerpc.DCEDataPacker;
23 import org.alfresco.filesys.smb.server.SMBTransPacket;
24 import org.alfresco.filesys.util.DataPacker;
25
26 /**
27  * DCE/RPC Server Packet Class
28  */

29 public class DCESrvPacket extends SMBTransPacket
30 {
31
32     // DCE/RPC header offsets
33

34     private static final int VERSIONMAJOR = 0;
35     private static final int VERSIONMINOR = 1;
36     private static final int PDUTYPE = 2;
37     private static final int HEADERFLAGS = 3;
38     private static final int PACKEDDATAREP = 4;
39     private static final int FRAGMENTLEN = 8;
40     private static final int AUTHLEN = 10;
41     private static final int CALLID = 12;
42     private static final int DCEDATA = 16;
43
44     // DCE/RPC Request offsets
45

46     private static final int ALLOCATIONHINT = 16;
47     private static final int PRESENTIDENT = 20;
48     private static final int OPERATIONID = 22;
49     private static final int OPERATIONDATA = 24;
50
51     // Header flags
52

53     public static final int FLG_FIRSTFRAG = 0x01;
54     public static final int FLG_LASTFRAG = 0x02;
55     public static final int FLG_ONLYFRAG = 0x03;
56
57     // DCE/RPC header constants
58

59     private static final byte HDR_VERSIONMAJOR = 5;
60     private static final byte HDR_VERSIONMINOR = 0;
61     private static final int HDR_PACKEDDATAREP = 0x00000010;
62
63     // Offset to DCE/RPC header
64

65     private int m_offset;
66
67     /**
68      * Construct a DCE/RPC transaction packet
69      *
70      * @param buf Buffer that contains the SMB transaction packet.
71      */

72     public DCESrvPacket(byte[] buf)
73     {
74         super(buf);
75         // m_offset = getParameterOffset();
76
}
77
78     /**
79      * Construct a DCE/RPC transaction packet
80      *
81      * @param siz Size of packet to allocate.
82      */

83     public DCESrvPacket(int siz)
84     {
85         super(siz);
86
87         // Set the multiplex id for this transaction
88

89         setMultiplexId(getNextMultiplexId());
90     }
91
92     /**
93      * Return the major version number
94      *
95      * @return int
96      */

97     public final int getMajorVersion()
98     {
99         return (int) (getBuffer()[m_offset + VERSIONMAJOR] & 0xFF);
100     }
101
102     /**
103      * Return the minor version number
104      *
105      * @return int
106      */

107     public final int getMinorVersion()
108     {
109         return (int) (getBuffer()[m_offset + VERSIONMINOR] & 0xFF);
110     }
111
112     /**
113      * Return the PDU packet type
114      *
115      * @return int
116      */

117     public final int getPDUType()
118     {
119         return (int) (getBuffer()[m_offset + PDUTYPE] & 0xFF);
120     }
121
122     /**
123      * Return the header flags
124      *
125      * @return int
126      */

127     public final int getHeaderFlags()
128     {
129         return (int) (getBuffer()[m_offset + HEADERFLAGS] & 0xFF);
130     }
131
132     /**
133      * Return the packed data representation
134      *
135      * @return int
136      */

137     public final int getPackedDataRepresentation()
138     {
139         return DataPacker.getIntelInt(getBuffer(), m_offset + PACKEDDATAREP);
140     }
141
142     /**
143      * Return the fragment length
144      *
145      * @return int
146      */

147     public final int getFragmentLength()
148     {
149         return DataPacker.getIntelShort(getBuffer(), m_offset + FRAGMENTLEN);
150     }
151
152     /**
153      * Set the fragment length
154      *
155      * @param len int
156      */

157     public final void setFragmentLength(int len)
158     {
159
160         // Set the DCE header fragment length
161

162         DataPacker.putIntelShort(len, getBuffer(), m_offset + FRAGMENTLEN);
163     }
164
165     /**
166      * Return the authentication length
167      *
168      * @return int
169      */

170     public final int getAuthenticationLength()
171     {
172         return DataPacker.getIntelShort(getBuffer(), m_offset + AUTHLEN);
173     }
174
175     /**
176      * Return the call id
177      *
178      * @return int
179      */

180     public final int getCallId()
181     {
182         return DataPacker.getIntelInt(getBuffer(), m_offset + CALLID);
183     }
184
185     /**
186      * Determine if this is the first fragment
187      *
188      * @return boolean
189      */

190     public final boolean isFirstFragment()
191     {
192         if ((getHeaderFlags() & FLG_FIRSTFRAG) != 0)
193             return true;
194         return false;
195     }
196
197     /**
198      * Determine if this is the last fragment
199      *
200      * @return boolean
201      */

202     public final boolean isLastFragment()
203     {
204         if ((getHeaderFlags() & FLG_LASTFRAG) != 0)
205             return true;
206         return false;
207     }
208
209     /**
210      * Determine if this is the only fragment in the request
211      *
212      * @return boolean
213      */

214     public final boolean isOnlyFragment()
215     {
216         if ((getHeaderFlags() & FLG_ONLYFRAG) == FLG_ONLYFRAG)
217             return true;
218         return false;
219     }
220
221     /**
222      * Get the offset to the DCE/RPC data within the SMB packet
223      *
224      * @return int
225      */

226     public final int getDCEDataOffset()
227     {
228
229         // Determine the data offset from the DCE/RPC packet type
230

231         int dataOff = -1;
232         switch (getPDUType())
233         {
234
235         // Bind/bind acknowledge
236

237         case DCECommand.BIND:
238         case DCECommand.BINDACK:
239             dataOff = m_offset + DCEDATA;
240             break;
241
242         // Request/response
243

244         case DCECommand.REQUEST:
245         case DCECommand.RESPONSE:
246             dataOff = m_offset + OPERATIONDATA;
247             break;
248         }
249
250         // Return the data offset
251

252         return dataOff;
253     }
254
255     /**
256      * Get the request allocation hint
257      *
258      * @return int
259      */

260     public final int getAllocationHint()
261     {
262         return DataPacker.getIntelInt(getBuffer(), m_offset + ALLOCATIONHINT);
263     }
264
265     /**
266      * Set the allocation hint
267      *
268      * @param alloc int
269      */

270     public final void setAllocationHint(int alloc)
271     {
272         DataPacker.putIntelInt(alloc, getBuffer(), m_offset + ALLOCATIONHINT);
273     }
274
275     /**
276      * Get the request presentation identifier
277      *
278      * @return int
279      */

280     public final int getPresentationIdentifier()
281     {
282         return DataPacker.getIntelShort(getBuffer(), m_offset + PRESENTIDENT);
283     }
284
285     /**
286      * Set the presentation identifier
287      *
288      * @param ident int
289      */

290     public final void setPresentationIdentifier(int ident)
291     {
292         DataPacker.putIntelShort(ident, getBuffer(), m_offset + PRESENTIDENT);
293     }
294
295     /**
296      * Get the request operation id
297      *
298      * @return int
299      */

300     public final int getOperationId()
301     {
302         return DataPacker.getIntelShort(getBuffer(), m_offset + OPERATIONID);
303     }
304
305     /**
306      * Initialize the DCE/RPC request. Set the SMB transaction parameter count so that the data
307      * offset can be calculated.
308      *
309      * @param handle int
310      * @param typ byte
311      * @param flags int
312      * @param callId int
313      */

314     public final void initializeDCERequest(int handle, byte typ, int flags, int callId)
315     {
316
317         // Initialize the transaction
318

319         InitializeTransact(16, null, 0, null, 0);
320
321         // Set the parameter byte count/offset for this packet
322

323         int bytPos = DCEDataPacker.longwordAlign(getByteOffset());
324
325         setParameter(3, 0);
326         setParameter(4, bytPos - RFCNetBIOSProtocol.HEADER_LEN);
327
328         // Set the parameter displacement
329

330         setParameter(5, 0);
331
332         // Set the data byte count/offset for this packet
333

334         setParameter(6, 0);
335         setParameter(7, bytPos - RFCNetBIOSProtocol.HEADER_LEN);
336
337         // Set the data displacement
338

339         setParameter(8, 0);
340
341         // Set up word count
342

343         setParameter(9, 0);
344
345         // Set the setup words
346

347         setSetupParameter(0, PacketType.TransactNmPipe);
348         setSetupParameter(1, handle);
349
350         // Reset the DCE offset for a DCE reply
351

352         m_offset = bytPos;
353
354         // Build the DCE/RPC header
355

356         byte[] buf = getBuffer();
357         DataPacker.putZeros(buf, m_offset, 24);
358
359         buf[m_offset + VERSIONMAJOR] = HDR_VERSIONMAJOR;
360         buf[m_offset + VERSIONMINOR] = HDR_VERSIONMINOR;
361         buf[m_offset + PDUTYPE] = typ;
362         buf[m_offset + HEADERFLAGS] = (byte) (flags & 0xFF);
363         DataPacker.putIntelInt(HDR_PACKEDDATAREP, buf, m_offset + PACKEDDATAREP);
364         DataPacker.putIntelInt(0, buf, m_offset + AUTHLEN);
365         DataPacker.putIntelInt(callId, buf, m_offset + CALLID);
366     }
367
368     /**
369      * Initialize the DCE/RPC reply. Set the SMB transaction parameter count so that the data offset
370      * can be calculated.
371      */

372     public final void initializeDCEReply()
373     {
374
375         // Set the total parameter words
376

377         setParameterCount(10);
378
379         // Set the total parameter/data bytes
380

381         setParameter(0, 0);
382         setParameter(1, 0);
383
384         // Set the parameter byte count/offset for this packet
385

386         int bytPos = DCEDataPacker.longwordAlign(getByteOffset());
387
388         setParameter(3, 0);
389         setParameter(4, bytPos - RFCNetBIOSProtocol.HEADER_LEN);
390
391         // Set the parameter displacement
392

393         setParameter(5, 0);
394
395         // Set the data byte count/offset for this packet
396

397         setParameter(6, 0);
398         setParameter(7, bytPos - RFCNetBIOSProtocol.HEADER_LEN);
399
400         // Set the data displacement
401

402         setParameter(8, 0);
403
404         // Set up word count
405

406         setParameter(9, 0);
407     }
408
409     /**
410      * Dump the DCE/RPC header details
411      */

412     public final void DumpHeader()
413     {
414
415         // Dump the PDU type
416

417         System.out.println("** DCE/RPC Header - PDU Type = " + DCECommand.getCommandString(getPDUType()));
418         System.out.println(" Version : " + getMajorVersion() + "." + getMinorVersion());
419         System.out.println(" Flags : 0x" + getHeaderFlags());
420         System.out.println(" Packed Data Rep : 0x" + getPackedDataRepresentation());
421         System.out.println(" Fragment Length : " + getFragmentLength());
422         System.out.println(" Auth Length : " + getAuthenticationLength());
423         System.out.println(" Call ID : " + getCallId());
424     }
425 }
426
Popular Tags