KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcifs > dcerpc > DcerpcMessage


1 /* jcifs msrpc client library in Java
2  * Copyright (C) 2006 "Michael B. Allen" <jcifs at samba dot org>
3  * "Eric Glass" <jcifs at samba dot org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package jcifs.dcerpc;
21
22 import java.io.IOException JavaDoc;
23 import jcifs.dcerpc.ndr.*;
24
25 public abstract class DcerpcMessage extends NdrObject implements DcerpcConstants {
26
27     protected int ptype = -1;
28     protected int flags = 0;
29     protected int length = 0;
30     protected int call_id = 0;
31     protected int alloc_hint = 0;
32     protected int result = 0;
33
34     public boolean isFlagSet(int flag) {
35         return (flags & flag) == flag;
36     }
37     public void unsetFlag(int flag) {
38         flags |= flag;
39     }
40     public void setFlag(int flag) {
41         flags |= flag;
42     }
43     public DcerpcException getResult() {
44         if (result != 0)
45             return new DcerpcException(result);
46         return null;
47     }
48
49     void encode_header(NdrBuffer buf) {
50         buf.enc_ndr_small(5); /* RPC version */
51         buf.enc_ndr_small(0); /* minor version */
52         buf.enc_ndr_small(ptype);
53         buf.enc_ndr_small(flags);
54         buf.enc_ndr_long(0x00000010); /* Little-endian / ASCII / IEEE */
55         buf.enc_ndr_short(length);
56         buf.enc_ndr_short(0); /* length of auth_value */
57         buf.enc_ndr_long(call_id);
58     }
59     void decode_header(NdrBuffer buf) throws NdrException {
60         buf.dec_ndr_small(); /* RPC version */
61         buf.dec_ndr_small(); /* minor version */
62         ptype = buf.dec_ndr_small();
63         flags = buf.dec_ndr_small();
64         if (buf.dec_ndr_long() != 0x00000010) /* Little-endian / ASCII / IEEE */
65             throw new NdrException("Data representation not supported");
66         length = buf.dec_ndr_short();
67         if (buf.dec_ndr_short() != 0)
68             throw new NdrException("DCERPC authentication not supported");
69         call_id = buf.dec_ndr_long();
70     }
71     public void encode(NdrBuffer buf) throws NdrException {
72         int start = buf.getIndex();
73         int alloc_hint_index = 0;
74
75         buf.advance(16); /* momentarily skip header */
76         if (ptype == 0) { /* Request */
77             alloc_hint_index = buf.getIndex();
78             buf.enc_ndr_long(0); /* momentarily skip alloc hint */
79             buf.enc_ndr_short(0); /* context id */
80             buf.enc_ndr_short(getOpnum());
81         }
82
83         encode_in(buf);
84         length = buf.getIndex() - start;
85
86         if (ptype == 0) {
87             buf.setIndex(alloc_hint_index);
88             alloc_hint = length - alloc_hint_index;
89             buf.enc_ndr_long(alloc_hint);
90         }
91
92         buf.setIndex(start);
93         encode_header(buf);
94         buf.setIndex(start + length);
95     }
96     public void decode(NdrBuffer buf) throws NdrException {
97         decode_header(buf);
98
99         if (ptype != 12 && ptype != 2 && ptype != 3)
100             throw new NdrException("Unexpected ptype: " + ptype);
101
102         if (ptype == 2 || ptype == 3) { /* Response or Fault */
103             alloc_hint = buf.dec_ndr_long();
104             buf.dec_ndr_short(); /* context id */
105             buf.dec_ndr_short(); /* cancel count */
106         }
107         if (ptype == 3) { /* Fault */
108             result = buf.dec_ndr_long();
109         } else { /* Bind_ack or Response */
110             decode_out(buf);
111         }
112     }
113
114     public abstract int getOpnum();
115     public abstract void encode_in(NdrBuffer buf) throws NdrException;
116     public abstract void decode_out(NdrBuffer buf) throws NdrException;
117 }
118
Popular Tags