-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathThreadedSMBConnectionsHandler.java
More file actions
303 lines (244 loc) · 9.08 KB
/
ThreadedSMBConnectionsHandler.java
File metadata and controls
303 lines (244 loc) · 9.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.filesys.smb.server;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.filesys.debug.Debug;
import org.filesys.netbios.NetworkSettings;
import org.filesys.server.SessionHandlerInterface;
import org.filesys.server.SessionHandlerList;
import org.filesys.server.SocketSessionHandler;
import org.filesys.server.config.InvalidConfigurationException;
import org.filesys.smb.mailslot.HostAnnouncer;
import org.filesys.smb.mailslot.TcpipNetBIOSHostAnnouncer;
import org.filesys.util.PlatformType;
/**
* Threaded SMB Connectins Handler Class
*
* <p>Create a seperate thread for each enabled SMB session handler.
*
* @author gkspencer
*/
public class ThreadedSMBConnectionsHandler implements SMBConnectionsHandler {
// List of session handlers that are waiting for incoming requests
private SessionHandlerList m_handlerList;
// List of host announcers
private List<HostAnnouncer> m_hostAnnouncers;
// SMB server
private SMBServer m_server;
// Debug output
private boolean m_debug;
/**
* Class constructor
*/
public ThreadedSMBConnectionsHandler() {
m_handlerList = new SessionHandlerList();
m_hostAnnouncers = new ArrayList<HostAnnouncer>();
}
/**
* Check if debug output is enabled
*
* @return boolean
*/
public final boolean hasDebug() {
return m_debug;
}
/**
* Initialize the connections handler
*
* @param srv SMBServer
* @param config SMBConfigSection
* @exception InvalidConfigurationException Failed to initialize the connection handler
*/
public void initializeHandler( SMBServer srv, SMBConfigSection config)
throws InvalidConfigurationException {
// Save the server
m_server = srv;
// Check if the socket connection debug flag is enabled
if ( config.getSessionDebugFlags().contains( SMBSrvSession.Dbg.SOCKET))
m_debug = true;
// Create the NetBIOS session socket handler, if enabled
if ( config.hasNetBIOSSMB()) {
// Create the TCP/IP NetBIOS SMB session handler(s), and host announcer(s)
SocketSessionHandler sessHandler = new NetBIOSSessionSocketHandler( srv, config.getSessionPort(), config.getSMBBindAddress(), hasDebug());
sessHandler.setSocketTimeout( config.getSocketTimeout());
try {
// Initialize the NetBIOS session handler
sessHandler.initializeSessionHandler( srv);
m_handlerList.addHandler( sessHandler);
// DEBUG
if ( Debug.EnableError && hasDebug())
Debug.println("[SMB] TCP NetBIOS session handler created");
// Check if a host announcer should be created
if ( config.hasEnableAnnouncer()) {
// Create the TCP NetBIOS host announcer
TcpipNetBIOSHostAnnouncer announcer = new TcpipNetBIOSHostAnnouncer();
// Set the host name to be announced
announcer.addHostName(config.getServerName());
announcer.setDomain(config.getDomainName());
announcer.setComment(config.getComment());
announcer.setBindAddress(config.getSMBBindAddress());
if ( config.getHostAnnouncerPort() != 0)
announcer.setPort(config.getHostAnnouncerPort());
// Check if there are alias names to be announced
if ( config.hasAliasNames())
announcer.addHostNames(config.getAliasNames());
// Set the announcement interval
if ( config.getHostAnnounceInterval() > 0)
announcer.setInterval(config.getHostAnnounceInterval());
try {
announcer.setBroadcastAddress(config.getBroadcastMask());
NetworkSettings.setBroadcastMask(config.getBroadcastMask());
}
catch (Exception ex) {
}
// Set the server type flags
announcer.setServerType(config.getServerType());
// Enable debug output
if ( config.hasHostAnnounceDebug())
announcer.setDebug(true);
// Add the announcer to the list
m_hostAnnouncers.add( announcer);
// DEBUG
if ( Debug.EnableError && hasDebug())
Debug.println("[SMB] TCP NetBIOS host announcer created");
}
}
catch (IOException ex) {
}
}
// Create the TCP/IP SMB session socket handler, if enabled
if ( config.hasTcpipSMB()) {
// Create the TCP/IP native SMB session handler(s)
SocketSessionHandler sessHandler = new TcpipSMBSessionSocketHandler( srv, config.getTcpipSMBPort(), config.getSMBBindAddress(), hasDebug());
sessHandler.setSocketTimeout( config.getSocketTimeout());
try {
// Initialize the native SMB handler
sessHandler.initializeSessionHandler( srv);
m_handlerList.addHandler( sessHandler);
// DEBUG
if ( Debug.EnableError && hasDebug())
Debug.println("[SMB] Native SMB TCP session handler created");
}
catch ( IOException ex) {
throw new InvalidConfigurationException( "Error initializing session handler, " + ex.getMessage());
}
}
// Create the Win32 NetBIOS session handler, if enabled and available on the classpath
if ( config.hasWin32NetBIOS()) {
// Only enable if running under Windows
if ( PlatformType.isWindowsNTOnwards()) {
// Check if the Win32 NetBIOS based connections handler is available
SMBChildConnectionHandler win32Handler = null;
try {
win32Handler = (SMBChildConnectionHandler) Class.forName( "org.filesys.smb.server.win32.Win32NetBIOSSMBConnectionsHandler").newInstance();
}
catch ( IllegalAccessException ex) {
}
catch ( InstantiationException ex) {
}
catch ( ClassNotFoundException ex) {
}
// If the Win32 NetBIOS handler is valid then add the Win32 NetBIOS handlers, announcers, LANA monitors
if ( win32Handler != null) {
// Add the Win32 NetBIOS handler
win32Handler.initializeHandler( srv, config, this);
}
}
}
}
/**
* Return the count of active session handlers
*
* @return int
*/
public int numberOfSessionHandlers() {
return m_handlerList.numberOfHandlers();
}
/**
* Add a host announcer to the connections handler
*
* @param announcer HostAnnouncer
*/
public void addHostAnnouncer(HostAnnouncer announcer) {
m_hostAnnouncers.add( announcer);
}
/**
* Add a session handler to the connections handler
*
* @param sessHandler SessionHandlerInterface
*/
public void addSessionHandler(SessionHandlerInterface sessHandler) {
m_handlerList.addHandler( sessHandler);
}
/**
* Start the connection handler thread
*/
public void startHandler() {
// Start each session handler in a seperate thread
for ( int idx = 0; idx < m_handlerList.numberOfHandlers(); idx++) {
SessionHandlerInterface sessHandler = m_handlerList.getHandlerAt( idx);
if ( sessHandler instanceof Runnable) {
// Start the session handler in a seperate thread
Thread handlerThread = new Thread(( Runnable) sessHandler);
handlerThread.setName( "SMBSessHandler_" + sessHandler.getHandlerName());
handlerThread.start();
// DEBUG
if ( Debug.EnableInfo && hasDebug())
Debug.println("[SMB] Created session handler thread " + handlerThread.getName());
}
}
// Start the host announcer(s)
if ( m_hostAnnouncers.size() > 0) {
for ( int idx = 0; idx < m_hostAnnouncers.size(); idx++) {
// Get the current host announcer
HostAnnouncer hostAnnouncer = (HostAnnouncer) m_hostAnnouncers.get( idx);
hostAnnouncer.startAnnouncer();
// DEBUG
if ( Debug.EnableInfo && hasDebug())
Debug.println( "[SMB] Started host announcer " + hostAnnouncer.getName());
}
}
}
/**
* Stop the connections handler
*/
public void stopHandler() {
// Stop each session handler
for ( int idx = 0; idx < m_handlerList.numberOfHandlers(); idx++) {
SessionHandlerInterface sessHandler = m_handlerList.getHandlerAt( idx);
sessHandler.closeSessionHandler( m_server);
// DEBUG
if ( Debug.EnableInfo && hasDebug())
Debug.println("[SMB] Shutting down session handler thread " + sessHandler.getHandlerName());
}
// Stop the host announcer(s)
if ( m_hostAnnouncers.size() > 0) {
for ( int idx = 0; idx < m_hostAnnouncers.size(); idx++) {
// Get the current host announcer
HostAnnouncer hostAnnouncer = (HostAnnouncer) m_hostAnnouncers.get( idx);
hostAnnouncer.shutdownAnnouncer();
// DEBUG
if ( Debug.EnableInfo && hasDebug())
Debug.println( "[SMB] Shutting down host announcer " + hostAnnouncer.getName());
}
}
}
}