KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcifs > smb > SmbTree


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

18
19 package jcifs.smb;
20
21 import java.io.IOException JavaDoc;
22 import java.net.UnknownHostException JavaDoc;
23 import jcifs.UniAddress;
24 import jcifs.netbios.NbtAddress;
25 import jcifs.Config;
26
27 class SmbTree {
28
29     private static int tree_conn_counter;
30
31     private int tid;
32     private String JavaDoc share;
33
34     String JavaDoc service = "?????";
35     SmbSession session;
36     boolean treeConnected, inDfs;
37     int tree_num;
38
39     SmbTree( SmbSession session, String JavaDoc share, String JavaDoc service ) {
40         this.session = session;
41         this.share = share.toUpperCase();
42         if( service != null && service.startsWith( "??" ) == false ) {
43             this.service = service;
44         }
45     }
46
47     boolean matches( String JavaDoc share, String JavaDoc service ) {
48         return this.share.equalsIgnoreCase( share ) &&
49                 ( service == null || service.startsWith( "??" ) ||
50                 this.service.equalsIgnoreCase( service ));
51     }
52     void send( ServerMessageBlock request,
53                             ServerMessageBlock response ) throws SmbException {
54         if( response != null ) {
55             response.received = false;
56         }
57         treeConnect( request, response );
58         if( request == null || (response != null && response.received )) {
59             return;
60         }
61         if( service.equals( "A:" ) == false ) {
62             switch( request.command ) {
63                 case ServerMessageBlock.SMB_COM_OPEN_ANDX:
64                 case ServerMessageBlock.SMB_COM_NT_CREATE_ANDX:
65                 case ServerMessageBlock.SMB_COM_READ_ANDX:
66                 case ServerMessageBlock.SMB_COM_WRITE_ANDX:
67                 case ServerMessageBlock.SMB_COM_CLOSE:
68                 case ServerMessageBlock.SMB_COM_TREE_DISCONNECT:
69                     break;
70                 case ServerMessageBlock.SMB_COM_TRANSACTION:
71                 case ServerMessageBlock.SMB_COM_TRANSACTION2:
72                     switch( ((SmbComTransaction)request).subCommand & 0xFF ) {
73                         case SmbComTransaction.NET_SHARE_ENUM:
74                         case SmbComTransaction.NET_SERVER_ENUM2:
75                         case SmbComTransaction.NET_SERVER_ENUM3:
76                         case SmbComTransaction.TRANS_PEEK_NAMED_PIPE:
77                         case SmbComTransaction.TRANS_WAIT_NAMED_PIPE:
78                         case SmbComTransaction.TRANS_CALL_NAMED_PIPE:
79                         case SmbComTransaction.TRANS_TRANSACT_NAMED_PIPE:
80                         case SmbComTransaction.TRANS2_GET_DFS_REFERRAL:
81                             break;
82                         default:
83                             throw new SmbException( "Invalid operation for " + service + " service" );
84                     }
85                     break;
86                 default:
87                     throw new SmbException( "Invalid operation for " + service + " service" + request );
88             }
89         }
90         request.tid = tid;
91         if( inDfs && request.path != null && request.path.length() > 0 ) {
92             /* When DFS is in action all request paths are
93              * full UNC paths minus the first backslash like
94              * \server\share\path\to\file
95              * as opposed to normally
96              * \path\to\file
97              */

98             request.flags2 = ServerMessageBlock.FLAGS2_RESOLVE_PATHS_IN_DFS;
99             request.path = '\\' + session.transport().tconHostName + '\\' + share + request.path;
100         }
101         try {
102             session.send( request, response );
103         } catch( SmbException se ) {
104             if (se.getNtStatus() == se.NT_STATUS_NETWORK_NAME_DELETED) {
105                 /* Someone removed the share while we were
106                  * connected. Bastards! Disconnect this tree
107                  * so that it reconnects cleanly should the share
108                  * reappear in this client's lifetime.
109                  */

110                 treeDisconnect( true );
111             }
112             throw se;
113         }
114     }
115     void treeConnect( ServerMessageBlock andx,
116                             ServerMessageBlock andxResponse ) throws SmbException {
117         String JavaDoc unc;
118 synchronized( session.transport() ) {
119
120         if (treeConnected) {
121             return;
122         }
123
124         /* The hostname to use in the path is only known for
125          * sure if the NetBIOS session has been successfully
126          * established.
127          */

128
129         session.transport.connect();
130
131         unc = "\\\\" + session.transport.tconHostName + '\\' + share;
132
133         /*
134          * Tree Connect And X Request / Response
135          */

136
137         if( session.transport.log.level > 2 )
138             session.transport.log.println( "treeConnect: unc=" + unc + ",service=" + service );
139
140         SmbComTreeConnectAndXResponse response =
141                 new SmbComTreeConnectAndXResponse( andxResponse );
142         SmbComTreeConnectAndX request =
143                 new SmbComTreeConnectAndX( session, unc, service, andx );
144         session.send( request, response );
145
146         tid = response.tid;
147         service = response.service;
148         inDfs = response.shareIsInDfs;
149         treeConnected = true;
150         tree_num = tree_conn_counter++;
151 }
152     }
153     void treeDisconnect( boolean inError ) {
154 synchronized( session.transport ) {
155         if (treeConnected && !inError) {
156             try {
157                 send( new SmbComTreeDisconnect(), null );
158             } catch( SmbException se ) {
159                 if (session.transport.log.level > 1) {
160                     se.printStackTrace( session.transport.log );
161                 }
162             }
163         }
164         treeConnected = false;
165 }
166     }
167
168     public String JavaDoc toString() {
169         return "SmbTree[share=" + share +
170             ",service=" + service +
171             ",tid=" + tid +
172             ",inDfs=" + inDfs +
173             ",treeConnected=" + treeConnected + "]";
174     }
175 }
176
Popular Tags