rana_b 2003/01/20 07:19:23
Modified: ftpserver/src/java/org/apache/avalon/ftpserver
FtpConfig.java
Log:
making it firewall friendly
Revision Changes Path
1.17 +153 -80
jakarta-avalon-apps/ftpserver/src/java/org/apache/avalon/ftpserver/FtpConfig.java
Index: FtpConfig.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-apps/ftpserver/src/java/org/apache/avalon/ftpserver/FtpConfig.java,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -r1.16 -r1.17
--- FtpConfig.java 10 Oct 2002 16:06:27 -0000 1.16
+++ FtpConfig.java 20 Jan 2003 15:19:23 -0000 1.17
@@ -13,6 +13,7 @@
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
+import java.util.StringTokenizer;
import org.apache.avalon.phoenix.BlockContext;
import org.apache.avalon.framework.configuration.Configuration;
@@ -38,19 +39,6 @@
public
class FtpConfig {
- private int miMaxConnection;
- private int miMaxAnonConnection;
- private int miServerPort;
- private int miRmiPort;
- private int miSchedulerInterval;
- private int miDefaultIdle;
-
- private boolean mbAnonAllowed;
- private boolean mbRemoteAdminAllowed;
- private boolean mbCreateHome;
-
- private File mDefaultRoot = null;
-
private FtpStatus mStatus = null;
private ConnectionService mConService = null;
private IpRestrictorInterface mIpRestrictor = null;
@@ -64,17 +52,29 @@
private Logger mLogger = null;
private FtpStatistics mStatistics = null;
- private AsyncMessageQueue mMsgQ = null;
private RemoteHandler mRemoteHandler = null;
+ private int miServerPort;
+ private int miDataPort[][];
+ private int miRmiPort;
+ private int miMaxLogin;
+ private int miAnonLogin;
+ private int miPollInterval;
+ private int miDefaultIdle;
+ private boolean mbAnonAllowed;
+ private boolean mbCreateHome;
+ private boolean mbRemoteAdminAllowed;
+ private File mDefaultRoot;
+ private AsyncMessageQueue mQueue;
+
/**
* Default constructor - first step.
*/
public FtpConfig() throws IOException {
mStatus = new FtpStatus();
- mMsgQ = new AsyncMessageQueue();
- mMsgQ.setMaxSize(4096);
+ mQueue = new AsyncMessageQueue();
+ mQueue.setMaxSize(4096);
}
/**
@@ -109,11 +109,17 @@
Configuration tmpConf = null;
// get server address
- tmpConf = conf.getChild("ftp-host", false);
+ tmpConf = conf.getChild("server-host", false);
if(tmpConf != null) {
mServerAddress = InetAddress.getByName(tmpConf.getValue());
}
-
+
+ // get self address
+ tmpConf = conf.getChild("self-host", false);
+ if(tmpConf != null) {
+ mSelfAddress = InetAddress.getByName(tmpConf.getValue());
+ }
+
// get server port
miServerPort = 21;
tmpConf = conf.getChild("ftp-port", false);
@@ -122,10 +128,10 @@
}
// get maximum number of connections
- miMaxConnection = 20;
+ miMaxLogin = 20;
tmpConf = conf.getChild("max-connection", false);
if(tmpConf != null) {
- miMaxConnection = tmpConf.getValueAsInteger(miMaxConnection);
+ miMaxLogin = tmpConf.getValueAsInteger(miMaxLogin);
}
// get anonymous login allow flag
@@ -136,17 +142,17 @@
}
// get maximum number of anonymous connections
- miMaxAnonConnection = 10;
+ miAnonLogin = 10;
tmpConf = conf.getChild("anonymous-max-connection", false);
if(tmpConf != null) {
- miMaxAnonConnection = tmpConf.getValueAsInteger(miMaxAnonConnection);
+ miAnonLogin = tmpConf.getValueAsInteger(miAnonLogin);
}
// get scheduler interval
- miSchedulerInterval = 120;
+ miPollInterval = 120;
tmpConf = conf.getChild("poll-interval", false);
if(tmpConf != null) {
- miSchedulerInterval = tmpConf.getValueAsInteger(miSchedulerInterval);
+ miPollInterval = tmpConf.getValueAsInteger(miPollInterval);
}
// get rmi port
@@ -185,13 +191,26 @@
}
mDefaultRoot = new File(defaultRoot);
- // get self address
- if(mServerAddress != null) {
- mSelfAddress = mServerAddress;
- }
- else {
+ // get data port number
+ String dataPort = "0";
+ tmpConf = conf.getChild("data-port-pool", false);
+ if(tmpConf != null) {
+ dataPort = tmpConf.getValue(dataPort);
+ }
+ StringTokenizer st = new StringTokenizer(dataPort, ", \t\n\r\f");
+ miDataPort = new int[st.countTokens()][2];
+ for(int i=0; i<miDataPort.length; i++) {
+ miDataPort[i][0] = Integer.parseInt(st.nextToken());
+ miDataPort[i][1] = 0;
+ }
+
+ // get host addresses
+ if (mSelfAddress == null) {
mSelfAddress = InetAddress.getLocalHost();
- }
+ }
+ if(mServerAddress == null) {
+ mServerAddress = mSelfAddress;
+ }
mStatistics = new FtpStatistics(this);
mConService = new ConnectionService(this);
@@ -201,17 +220,55 @@
}
/**
- * Get ftp status resource.
+ * Get data port. Data port number zero (0) means that
+ * any available port will be used.
*/
- public FtpStatus getStatus() {
- return mStatus;
- }
+ public int getDataPort() {
+ synchronized(miDataPort) {
+ int dataPort = -1;
+ int loopTimes = 2;
+ Thread currThread = Thread.currentThread();
+ while( (dataPort==-1) && (--loopTimes >= 0) &&
(!currThread.isInterrupted()) ) {
+
+ // search for a free port
+ for(int i=0; i<miDataPort.length; i++) {
+ if(miDataPort[i][1] == 0) {
+ if(miDataPort[i][0] != 0) {
+ miDataPort[i][1] = 1;
+ }
+ dataPort = miDataPort[i][0];
+ break;
+ }
+ }
+
+ // no available free port - wait for the release notification
+ if(dataPort == -1) {
+ try {
+ miDataPort.wait();
+ }
+ catch(InterruptedException ex) {
+ }
+ }
+
+ }
+ return dataPort;
+ }
+ }
+
/**
- * Get message queue.
+ * Release data port
*/
- public AsyncMessageQueue getMessageQueue() {
- return mMsgQ;
+ public void releaseDataPort(int port) {
+ synchronized(miDataPort) {
+ for(int i=0; i<miDataPort.length; i++) {
+ if(miDataPort[i][0] == port) {
+ miDataPort[i][1] = 0;
+ break;
+ }
+ }
+ miDataPort.notify();
+ }
}
/**
@@ -222,24 +279,17 @@
}
/**
- * Get context
- */
- public BlockContext getContext() {
- return mContext;
- }
-
- /**
- * Get user manager
+ * Get server port.
*/
- public UserManagerInterface getUserManager() {
- return mUserManager;
+ public int getServerPort() {
+ return miServerPort;
}
-
+
/**
- * Get ip restrictor
+ * Get context
*/
- public IpRestrictorInterface getIpRestrictor() {
- return mIpRestrictor;
+ public BlockContext getContext() {
+ return mContext;
}
/**
@@ -264,31 +314,38 @@
}
/**
- * Get server port.
- */
- public int getServerPort() {
- return miServerPort;
- }
-
- /**
* Check annonymous login support.
*/
public boolean isAnonymousLoginAllowed() {
return mbAnonAllowed;
}
+
+ /**
+ * Get ftp status resource.
+ */
+ public FtpStatus getStatus() {
+ return mStatus;
+ }
/**
- * Get user properties.
+ * Get connection service.
*/
public ConnectionService getConnectionService() {
return mConService;
}
-
+
+ /**
+ * Get user manager.
+ */
+ public UserManagerInterface getUserManager() {
+ return mUserManager;
+ }
+
/**
* Get maximum number of connections.
*/
public int getMaxConnections() {
- return miMaxConnection;
+ return miMaxLogin;
}
/**
@@ -298,14 +355,14 @@
if(!isAnonymousLoginAllowed()) {
return 0;
}
- return miMaxAnonConnection;
+ return miAnonLogin;
}
/**
* Get poll interval in seconds.
*/
public int getSchedulerInterval() {
- return miSchedulerInterval;
+ return miPollInterval;
}
/**
@@ -314,7 +371,7 @@
public int getDefaultIdleTime() {
return miDefaultIdle;
}
-
+
/**
* Get default root directory
*/
@@ -323,10 +380,10 @@
}
/**
- * Get global statistics object.
+ * Create user home directory if not exist during login
*/
- public FtpStatistics getStatistics() {
- return mStatistics;
+ public boolean isCreateHome() {
+ return mbCreateHome;
}
/**
@@ -344,20 +401,35 @@
}
/**
- * Is autometic user home creation allowed?
- */
- public boolean isCreateHome() {
- return mbCreateHome;
- }
-
- /**
* Get base directory
*/
public File getBaseDirectory() {
return mContext.getBaseDirectory();
}
+
+ /**
+ * Get IP restrictor object.
+ */
+ public IpRestrictorInterface getIpRestrictor() {
+ return mIpRestrictor;
+ }
+
+ /**
+ * Get global statistics object.
+ */
+ public FtpStatistics getStatistics() {
+ return mStatistics;
+ }
/**
+ * Get message queue
+ */
+ public AsyncMessageQueue getMessageQueue() {
+ return mQueue;
+ }
+
+
+ /**
* Get the system name.
*/
public String getSystemName() {
@@ -370,8 +442,8 @@
systemName = systemName.replace(' ', '-');
}
return systemName;
- }
-
+ }
+
/**
* Close this config and all the related resources. Ftp server
* <code>FtpServer.stop()</code> method will call this method.
@@ -391,13 +463,14 @@
}
// close message queue
- if (mMsgQ != null) {
- mMsgQ.stop();
- mMsgQ = null;
+ if (mQueue != null) {
+ mQueue.stop();
+ mQueue = null;
}
}
}
+
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>