KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcifs > dcerpc > DcerpcPipeHandle


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.net.*;
23 import java.io.*;
24
25 import jcifs.dcerpc.ndr.NdrBuffer;
26 import jcifs.smb.*;
27 import jcifs.util.*;
28
29 public class DcerpcPipeHandle extends DcerpcHandle {
30
31     SmbNamedPipe pipe;
32     SmbFileInputStream in = null;
33     SmbFileOutputStream out = null;
34     boolean isStart = true;
35
36     public DcerpcPipeHandle(String JavaDoc url,
37                 NtlmPasswordAuthentication auth)
38                 throws UnknownHostException, MalformedURLException, DcerpcException {
39         binding = DcerpcHandle.parseBinding(url);
40         url = "smb://" + binding.server + "/IPC$/" + binding.endpoint.substring(6);
41         pipe = new SmbNamedPipe(url,
42                 /* This 0x20000 bit is going to get chopped! */
43                 (0x2019F << 16) | SmbNamedPipe.PIPE_TYPE_RDWR | SmbNamedPipe.PIPE_TYPE_DCE_TRANSACT,
44                 auth);
45     }
46
47     protected void doSendFragment(byte[] buf,
48                     int off,
49                     int length,
50                     boolean isDirect) throws IOException {
51         if (in == null)
52             in = (SmbFileInputStream)pipe.getNamedPipeInputStream();
53         if (out == null)
54             out = (SmbFileOutputStream)pipe.getNamedPipeOutputStream();
55         if (isDirect) {
56             out.writeDirect( buf, off, length, 1 );
57             return;
58         }
59         out.write(buf, off, length);
60     }
61     protected void doReceiveFragment(byte[] buf, boolean isDirect) throws IOException {
62         int off, flags, length;
63
64         if (buf.length < max_recv)
65             throw new IllegalArgumentException JavaDoc("buffer too small");
66
67         if (isStart && !isDirect) { // start of new frag, do trans
68
off = in.read(buf, 0, 1024);
69         } else {
70             off = in.readDirect(buf, 0, buf.length);
71         }
72
73         if (buf[0] != 5 && buf[1] != 0)
74             throw new IOException("Unexpected DCERPC PDU header");
75
76         flags = buf[3] & 0xFF;
77         // next read is start of new frag
78
isStart = (flags & DCERPC_LAST_FRAG) == DCERPC_LAST_FRAG;
79
80         length = Encdec.dec_uint16le(buf, 8);
81         if (length > max_recv)
82             throw new IOException("Unexpected fragment length: " + length);
83
84         while (off < length) {
85             off += in.readDirect(buf, off, length - off);
86         }
87     }
88     public void close() throws IOException {
89         if (out != null)
90             out.close();
91     }
92 }
93
94
Popular Tags