Reputation: 13
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;
}
Upvotes: 1
Views: 3843
Reputation: 202642
You cannot connect straight after the PASV
command.
You have to call STOR
, REST
, LIST
, MLSD
or similar command first.
Upvotes: 0