1

I cannot to connect to data port. I am able to connect to 21 command port, send username and password. Then I send PASV\r\n. I get response and compute data port A * 256 + B = new_port. Problem is that I cannot connect to this port. For creating socket I am using createRemoteConnection() function. That is used twice, first time for command port and I pass there new_port and then I call it once again. Could you help me please? Thank you.

PASV
227 Entering Passive Mode (80,251,0,59,158,36).
A: 158
B: 36
Port: 40484
Creating socket...
Socket created
connect failed. Error: Can't assign requested address

-

int createRemoteConnection(Arguments &arguments,int cmd) {
int sock;
struct sockaddr_in server;
if (DEBUG) {
    puts("Creating socket...");
}

//Create socket
sock = socket(AF_INET , SOCK_STREAM , 0);
if (sock == -1)
{
    fputs("Could not create socket.\n",stderr);
    exit(1);
}

if (DEBUG) {
    puts("Socket created");
}

char ip[100];
struct hostent *he;
struct in_addr **addr_list;

if ((he = gethostbyname(arguments.hostname)) == NULL) {
    herror("gethostbyname");
    exit(1);
}

addr_list = (struct in_addr **) he->h_addr_list;

for (int i = 0; addr_list[i] != NULL; i++) {
    strcpy(ip, inet_ntoa(*addr_list[i]));
}

server.sin_addr.s_addr = inet_addr(ip);
server.sin_family = AF_INET;
server.sin_port = htons(cmd);

//Connect to remote server
if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
{
    perror("connect failed. Error");
    exit(1);
}

if (DEBUG) {
    puts("Connected\n");
}

return sock;
}

1 Answers1

0

You cannot connect straight after the PASV command.

You have to call STOR, REST, LIST, MLSD or similar command first.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • I sent `RETR` and I got `425 Use PORT or PASV first.`, I continued with sending `PASV` and then tried to create data socket but it failed with same error like before. – Tomáš Hrnčiar Oct 21 '16 at 12:24
  • Send `PASV`, then `RETR` and only then try to connect to the port. – Martin Prikryl Oct 21 '16 at 12:33
  • + Can you download the file using a standalone FTP client running on the same machine? – Martin Prikryl Oct 21 '16 at 12:34
  • Yes, I have tried to download this file using another part of my project using FTP in active mode and also I am able to download this file using FileZilla, but in passive mode after seding `PASV`, then `RETR` and connect nothing changes and I am still getting `Can't assign requested address` error. – Tomáš Hrnčiar Oct 21 '16 at 14:01
  • Do you have an access to server-side log? If you have, post a log both for your code and FileZilla. + Show us more of your code. – Martin Prikryl Oct 21 '16 at 14:26
  • Unfortunately not, this is school project so I have just limited options for testing, I am using some public servers like test.talia.net, or speedtest.tele2.net. – Tomáš Hrnčiar Oct 21 '16 at 16:18
  • Which part of code would you like to see? I am quite afraid to show longer parts of code public, because there is more people with this task and I am afraid of plagiarism if somebody use it. Is there some option how to show you my work in private? Thank you. – Tomáš Hrnčiar Oct 21 '16 at 16:22
  • You can install a local server. You can use free FileZilla FTP server for example, if you are on Windows. – Martin Prikryl Oct 21 '16 at 18:00
  • It is your responsibility to post [mcve]. If you are not willing to post it, you are on a wrong place. – Martin Prikryl Oct 21 '16 at 18:01