001: /* jcifs smb client library in Java
002: * Copyright (C) 2000 "Michael B. Allen" <jcifs at samba dot org>
003: * "Paul Walker" <jcifs at samba dot org>
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019:
020: package jcifs.smb;
021:
022: import java.net.URL;
023: import java.io.IOException;
024: import java.io.InputStream;
025: import java.io.OutputStream;
026: import java.net.MalformedURLException;
027: import java.net.UnknownHostException;
028:
029: /**
030: * This class will allow a Java program to read and write data to Named
031: * Pipes and Transact NamedPipes.
032: *
033: * <p>There are three Win32 function calls provided by the Windows SDK
034: * that are important in the context of using jCIFS. They are:
035: *
036: * <ul>
037: * <li> <code>CallNamedPipe</code> A message-type pipe call that opens,
038: * writes to, reads from, and closes the pipe in a single operation.
039: * <li> <code>TransactNamedPipe</code> A message-type pipe call that
040: * writes to and reads from an existing pipe descriptor in one operation.
041: * <li> <code>CreateFile</code>, <code>ReadFile</code>,
042: * <code>WriteFile</code>, and <code>CloseFile</code> A byte-type pipe can
043: * be opened, written to, read from and closed using the standard Win32
044: * file operations.
045: * </ul>
046: *
047: * <p>The jCIFS API maps all of these operations into the standard Java
048: * <code>XxxputStream</code> interface. A special <code>PIPE_TYPE</code>
049: * flags is necessary to distinguish which type of Named Pipe behavior
050: * is desired.
051: *
052: * <p><table border="1" cellpadding="3" cellspacing="0" width="100%">
053: * <tr bgcolor="#ccccff">
054: * <td colspan="2"><b><code>SmbNamedPipe</code> Constructor Examples</b></td>
055: * <tr><td width="20%"><b>Code Sample</b></td><td><b>Description</b></td></tr>
056: * <tr><td width="20%"><pre>
057: * new SmbNamedPipe( "smb://server/IPC$/PIPE/foo",
058: * SmbNamedPipe.PIPE_TYPE_RDWR |
059: * SmbNamedPipe.PIPE_TYPE_CALL );
060: * </pre></td><td>
061: * Open the Named Pipe foo for reading and writing. The pipe will behave like the <code>CallNamedPipe</code> interface.
062: * </td></tr>
063: * <tr><td width="20%"><pre>
064: * new SmbNamedPipe( "smb://server/IPC$/foo",
065: * SmbNamedPipe.PIPE_TYPE_RDWR |
066: * SmbNamedPipe.PIPE_TYPE_TRANSACT );
067: * </pre></td><td>
068: * Open the Named Pipe foo for reading and writing. The pipe will behave like the <code>TransactNamedPipe</code> interface.
069: * </td></tr>
070: * <tr><td width="20%"><pre>
071: * new SmbNamedPipe( "smb://server/IPC$/foo",
072: * SmbNamedPipe.PIPE_TYPE_RDWR );
073: * </pre></td><td>
074: * Open the Named Pipe foo for reading and writing. The pipe will
075: * behave as though the <code>CreateFile</code>, <code>ReadFile</code>,
076: * <code>WriteFile</code>, and <code>CloseFile</code> interface was
077: * being used.
078: * </td></tr>
079: * </table>
080: *
081: * <p>See <a href="../../../pipes.html">Using jCIFS to Connect to Win32
082: * Named Pipes</a> for a detailed description of how to use jCIFS with
083: * Win32 Named Pipe server processes.
084: *
085: */
086:
087: public class SmbNamedPipe extends SmbFile {
088:
089: /**
090: * The pipe should be opened read-only.
091: */
092:
093: public static final int PIPE_TYPE_RDONLY = O_RDONLY;
094:
095: /**
096: * The pipe should be opened only for writing.
097: */
098:
099: public static final int PIPE_TYPE_WRONLY = O_WRONLY;
100:
101: /**
102: * The pipe should be opened for both reading and writing.
103: */
104:
105: public static final int PIPE_TYPE_RDWR = O_RDWR;
106:
107: /**
108: * Pipe operations should behave like the <code>CallNamedPipe</code> Win32 Named Pipe function.
109: */
110:
111: public static final int PIPE_TYPE_CALL = 0x01;
112:
113: /**
114: * Pipe operations should behave like the <code>TransactNamedPipe</code> Win32 Named Pipe function.
115: */
116:
117: public static final int PIPE_TYPE_TRANSACT = 0x02;
118:
119: public static final int PIPE_TYPE_DCE_TRANSACT = 0x02 | 0x04;
120:
121: InputStream pipeIn;
122: OutputStream pipeOut;
123: int pipeType;
124:
125: /**
126: * Open the Named Pipe resource specified by the url
127: * parameter. The pipeType parameter should be at least one of
128: * the <code>PIPE_TYPE</code> flags combined with the bitwise OR
129: * operator <code>|</code>. See the examples listed above.
130: */
131:
132: public SmbNamedPipe(String url, int pipeType)
133: throws MalformedURLException, UnknownHostException {
134: super (url);
135: this .pipeType = pipeType;
136: type = TYPE_NAMED_PIPE;
137: }
138:
139: public SmbNamedPipe(String url, int pipeType,
140: NtlmPasswordAuthentication auth)
141: throws MalformedURLException, UnknownHostException {
142: super (url, auth);
143: this .pipeType = pipeType;
144: type = TYPE_NAMED_PIPE;
145: }
146:
147: public SmbNamedPipe(URL url, int pipeType,
148: NtlmPasswordAuthentication auth)
149: throws MalformedURLException, UnknownHostException {
150: super (url, auth);
151: this .pipeType = pipeType;
152: type = TYPE_NAMED_PIPE;
153: }
154:
155: /**
156: * Return the <code>InputStream</code> used to read information
157: * from this pipe instance. Presumably data would first be written
158: * to the <code>OutputStream</code> associated with this Named
159: * Pipe instance although this is not a requirement (e.g. a
160: * read-only named pipe would write data to this stream on
161: * connection). Reading from this stream may block. Therefore it
162: * may be necessary that an addition thread be used to read and
163: * write to a Named Pipe.
164: */
165:
166: public InputStream getNamedPipeInputStream() throws IOException {
167: if (pipeIn == null) {
168: if ((pipeType & PIPE_TYPE_CALL) == PIPE_TYPE_CALL
169: || (pipeType & PIPE_TYPE_TRANSACT) == PIPE_TYPE_TRANSACT) {
170: pipeIn = new TransactNamedPipeInputStream(this );
171: } else {
172: pipeIn = new SmbFileInputStream(this ,
173: (pipeType & 0xFF0000) | SmbFile.O_EXCL);
174: }
175: }
176: return pipeIn;
177: }
178:
179: /**
180: * Return the <code>OutputStream</code> used to write
181: * information to this pipe instance. The act of writing data
182: * to this stream will result in response data recieved in the
183: * <code>InputStream</code> associated with this Named Pipe
184: * instance (unless of course it does not elicite a response or the pipe is write-only).
185: */
186:
187: public OutputStream getNamedPipeOutputStream() throws IOException {
188: if (pipeOut == null) {
189: if ((pipeType & PIPE_TYPE_CALL) == PIPE_TYPE_CALL
190: || (pipeType & PIPE_TYPE_TRANSACT) == PIPE_TYPE_TRANSACT) {
191: pipeOut = new TransactNamedPipeOutputStream(this );
192: } else {
193: pipeOut = new SmbFileOutputStream(this , false,
194: (pipeType & 0xFF0000) | SmbFile.O_EXCL);
195: }
196: }
197: return pipeOut;
198: }
199: }
|