You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2017/02/09 16:59:58 UTC

svn commit: r1782371 - in /commons/proper/net/trunk/src: changes/changes.xml test/java/org/apache/commons/net/tftp/TFTPServer.java

Author: sebb
Date: Thu Feb  9 16:59:58 2017
New Revision: 1782371

URL: http://svn.apache.org/viewvc?rev=1782371&view=rev
Log:
NET-320 Allow TFTPServer.java to bind to a specific network adapter

Modified:
    commons/proper/net/trunk/src/changes/changes.xml
    commons/proper/net/trunk/src/test/java/org/apache/commons/net/tftp/TFTPServer.java

Modified: commons/proper/net/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/net/trunk/src/changes/changes.xml?rev=1782371&r1=1782370&r2=1782371&view=diff
==============================================================================
--- commons/proper/net/trunk/src/changes/changes.xml [utf-8] (original)
+++ commons/proper/net/trunk/src/changes/changes.xml [utf-8] Thu Feb  9 16:59:58 2017
@@ -87,6 +87,9 @@ without checking it if is a space.
   The POP3Mail examples can now get password from console, stdin or an environment variable.
   
 ">
+            <action issue="NET-320" type="fix" dev="sebb" due-to="Kevin Bulebush">
+            Allow TFTPServer.java to bind to a specific network adapter
+            </action>
             <action issue="NET-414" type="fix" dev="sebb" due-to="Chuck Wolber">
             Apache Commons TFTP does not reject request replies that originate from a control port.
             </action>

Modified: commons/proper/net/trunk/src/test/java/org/apache/commons/net/tftp/TFTPServer.java
URL: http://svn.apache.org/viewvc/commons/proper/net/trunk/src/test/java/org/apache/commons/net/tftp/TFTPServer.java?rev=1782371&r1=1782370&r2=1782371&view=diff
==============================================================================
--- commons/proper/net/trunk/src/test/java/org/apache/commons/net/tftp/TFTPServer.java (original)
+++ commons/proper/net/trunk/src/test/java/org/apache/commons/net/tftp/TFTPServer.java Thu Feb  9 16:59:58 2017
@@ -27,8 +27,11 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.PrintStream;
+import java.net.InetAddress;
+import java.net.NetworkInterface;
 import java.net.SocketTimeoutException;
 import java.util.HashSet;
+import java.util.Enumeration;
 import java.util.Iterator;
 
 import org.apache.commons.net.io.FromNetASCIIOutputStream;
@@ -89,6 +92,7 @@ public class TFTPServer implements Runna
     private File serverReadDirectory_;
     private File serverWriteDirectory_;
     private final int port_;
+    private final InetAddress laddr_;
     private Exception serverException = null;
     private final ServerMode mode_;
 
@@ -161,6 +165,81 @@ public class TFTPServer implements Runna
         mode_ = mode;
         log_ = (log == null ? nullStream: log);
         logError_ = (errorLog == null ? nullStream : errorLog);
+        laddr_ = null;
+        launch(serverReadDirectory, serverWriteDirectory);
+    }
+
+    /**
+     * Start a TFTP Server on the specified port. Gets and Puts occur in the specified directory.
+     *
+     * The server will start in another thread, allowing this constructor to return immediately.
+     *
+     * If a get or a put comes in with a relative path that tries to get outside of the
+     * serverDirectory, then the get or put will be denied.
+     *
+     * GET_ONLY mode only allows gets, PUT_ONLY mode only allows puts, and GET_AND_PUT allows both.
+     * Modes are defined as int constants in this class.
+     *
+     * @param serverReadDirectory directory for GET requests
+     * @param serverWriteDirectory directory for PUT requests
+     * @param port The local port to bind to.
+     * @param localaddr The local address to bind to.
+     * @param mode A value as specified above.
+     * @param log Stream to write log message to. If not provided, uses System.out
+     * @param errorLog Stream to write error messages to. If not provided, uses System.err.
+     * @throws IOException if the server directory is invalid or does not exist.
+     */
+    public TFTPServer(File serverReadDirectory, File serverWriteDirectory, int port,
+        InetAddress localaddr, ServerMode mode, PrintStream log, PrintStream errorLog)
+        throws IOException
+    {
+        port_ = port;
+        mode_ = mode;
+        laddr_ = localaddr;
+        log_ = (log == null ? nullStream: log);
+        logError_ = (errorLog == null ? nullStream : errorLog);
+        launch(serverReadDirectory, serverWriteDirectory);
+    }
+
+    /**
+     * Start a TFTP Server on the specified port. Gets and Puts occur in the specified directory.
+     *
+     * The server will start in another thread, allowing this constructor to return immediately.
+     *
+     * If a get or a put comes in with a relative path that tries to get outside of the
+     * serverDirectory, then the get or put will be denied.
+     *
+     * GET_ONLY mode only allows gets, PUT_ONLY mode only allows puts, and GET_AND_PUT allows both.
+     * Modes are defined as int constants in this class.
+     *
+     * @param serverReadDirectory directory for GET requests
+     * @param serverWriteDirectory directory for PUT requests
+     * @param port the port to use
+     * @param localiface The local network interface to bind to.
+     *  The interface's first address wil be used.
+     * @param mode A value as specified above.
+     * @param log Stream to write log message to. If not provided, uses System.out
+     * @param errorLog Stream to write error messages to. If not provided, uses System.err.
+     * @throws IOException if the server directory is invalid or does not exist.
+     */
+    public TFTPServer(File serverReadDirectory, File serverWriteDirectory, int port,
+        NetworkInterface localiface, ServerMode mode, PrintStream log, PrintStream errorLog)
+        throws IOException
+    {
+        mode_ = mode;
+        port_= port;
+        InetAddress iaddr = null;
+        if (localiface != null)
+        {
+            Enumeration<InetAddress> ifaddrs = localiface.getInetAddresses();
+            if (ifaddrs != null)
+            {
+                if (ifaddrs.hasMoreElements()) iaddr = ifaddrs.nextElement();
+            }
+        }
+        log_ = (log == null ? nullStream: log);
+        logError_ = (errorLog == null ? nullStream : errorLog);
+        laddr_ = iaddr;
         launch(serverReadDirectory, serverWriteDirectory);
     }
 
@@ -242,7 +321,11 @@ public class TFTPServer implements Runna
         // we want the server thread to listen forever.
         serverTftp_.setDefaultTimeout(0);
 
-        serverTftp_.open(port_);
+        if (laddr_ != null) {
+            serverTftp_.open(port_, laddr_);
+        } else {
+            serverTftp_.open(port_);
+        }
 
         serverThread = new Thread(this);
         serverThread.setDaemon(true);