Skip to content

Commit

Permalink
Add IPV6 support and some of extended commands close #2
Browse files Browse the repository at this point in the history
  • Loading branch information
abdesamie committed Apr 25, 2021
1 parent 82cbf48 commit 6411300
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 12 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,24 @@ For implementation details on the handler functions please refer to the comments
* SYST
* FEAT


## Examples of usage

These examples are based on the use of windows ftp client:

`ftp>open localhost 1025`

The terminal prompt this message asking for user (comp4621) :

`220 Welcome to the COMP4621 FTP-Server
501 Unknown command
Utilisateur (LAPTOP-0CETF771:(none)) :comp4621`

Afterwards introduce the password (network)

The root folder should also have a folder named 'test'.
When connected you can use ftp commands as stated here : https://www.windows-commandline.com/windows-ftp-command-line/

## Ressources used
* https://tools.ietf.org/html/rfc959
* http://www.nsftools.com/tips/RawFTP.htm
Expand Down
68 changes: 56 additions & 12 deletions src/ftpServer/Worker.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
public class Worker extends Thread
{
private static final String IPV4 = "1";
private static final String IPV6 = "2";
/**
* Enable debugging output to console
*/
Expand Down Expand Up @@ -142,7 +144,7 @@ private void executeCommand(String c)
// split command and arguments
int index = c.indexOf(' ');
String command = ((index == -1)? c.toUpperCase() : (c.substring(0, index)).toUpperCase());
String args = ((index == -1)? null : c.substring(index+1, c.length()));
String args = ((index == -1)? null : c.substring(index+1));


debugOutput("Command: " + command + " Args: " + args);
Expand Down Expand Up @@ -170,6 +172,7 @@ private void executeCommand(String c)
break;

case "PWD":
case "XPWD":
handlePwd();
break;

Expand All @@ -188,7 +191,7 @@ private void executeCommand(String c)
case "SYST":
handleSyst();
break;

case "FEAT":
handleFeat();
break;
Expand All @@ -198,18 +201,20 @@ private void executeCommand(String c)
break;

case "EPRT":
handlePort(parseExtendedArguments(args));
handleEPort(parseExtendedArguments(args));
break;

case "RETR":
handleRetr(args);
break;

case "MKD":
case "MKD" :
case "XMKD":
handleMkd(args);
break;

case "RMD":
case "XRMD":
handleRmd(args);
break;

Expand Down Expand Up @@ -516,7 +521,7 @@ private void handlePort(String args)
{
// Extract IP address and port number from arguments
String[] stringSplit = args.split(",");
String hostName = stringSplit[0] + "." + stringSplit[1] + "." +
String hostName = stringSplit[0] + "." + stringSplit[1] + "." +
stringSplit[2] + "." + stringSplit[3];

int p = Integer.parseInt(stringSplit[4])*256 + Integer.parseInt(stringSplit[5]);
Expand All @@ -527,7 +532,28 @@ private void handlePort(String args)

}


/**
* Handler for the EPORT command.
* The client issues an EPORT command to the server in active mode, so the
* server can open a data connection to the client through the given address
* and port number.
* @param args The first segment are is IP address.
* The last two segments encode the port number (port = seg1*256 + seg2)
*/
private void handleEPort(String args)
{
// Extract IP address and port number from arguments
String[] stringSplit = args.split(",");
String hostName = stringSplit[0];

int p = Integer.parseInt(stringSplit[1])*256 + Integer.parseInt(stringSplit[2]);

// Initiate data connection to client
openDataConnectionActive(hostName, p);
sendMsgToClient("200 Command OK");

}

/**
* Handler for PWD (Print working directory) command.
* Returns the path of the current directory back to the client.
Expand Down Expand Up @@ -934,7 +960,7 @@ private void handleStor(String file)

/**
* Helper method to parse the arguments of the EXXX commands (e.g. EPRT).
* EXXX commands are newer and support IPv6 (not supported here). The arguments
* EXXX commands are newer and support IPv6.The arguments
* get translated back to a "regular" argument.
* @param extArg The extended argument
* @return The regular argument
Expand All @@ -943,15 +969,33 @@ private void handleStor(String file)
private String parseExtendedArguments(String extArg)
{
String[] splitArgs = extArg.split("\\|");
String ipAddress = splitArgs[2].replace('.', ',');
int port = Integer.parseInt(splitArgs[3]);
String version = splitArgs[1];
if(IPV4.equals(version)){
return buildExtendedArgsIpV4(splitArgs);
}
else if(IPV6.equals(version)) {
return buildExtendedArgsIpV6(splitArgs);
}
throw new IllegalArgumentException("Unsupported IP version");
}

private String buildExtendedArgsIpV4(String[] args) {
String ipAddress = args[2].replace(',', '.');
int port = Integer.parseInt(args[3]);
int p1 = port/256;
int p2 = port%256;

return ipAddress + "," + p1 + "," + p2;

}


private String buildExtendedArgsIpV6(String[] args) {
String ipAddress = args[2];
int port = Integer.parseInt(args[3]);
int p1 = port / 256;
int p2 = port % 256;
return ipAddress + "," + p1 + "," + p2;
}


/**
* Debug output to the console. Also includes the Thread ID for better readability.
* @param msg Debug message
Expand Down

0 comments on commit 6411300

Please sign in to comment.