c - FTP Active mode client error in Ubuntu: I won't open a connection to 127.0.1.1 -
i have created ftp active mode client. on os x operating system works great. when have migrated ubuntu, has started fail. problem on os x, when enabling data connection, listening socket listens on wifi router ip. don't know why, on ubuntu listens on 127.0.1.1. because of this, response port commnd:
i won't open connection 127.0.1.1
how prioritize hosts had in os x?
data connection:
else if (strcmp(command, "ls") == 0) { // listening data socket initialization data_socket = listen_to_server(&ip_address); if (data_socket == socket_error) { goto exit; } // ftp commad port request if(request_port(&command_socket, ip_address) == socket_error) { goto exit; } // server response receiving if(receive_packet(&command_socket, packet) == socket_error) { goto exit; } // server response printing printf ("%s\n", packet); // ftp command list request if(request_list(&command_socket) == socket_error) { goto exit; } // server response receiving if(receive_packet(&command_socket, packet) == socket_error) { goto exit; } // server response printing printf ("%s\n", packet); // server connection accepting (active mode) int address_size = sizeof(struct sockaddr_in); struct sockaddr_in remote_address; data_socket_server = accept(data_socket, (struct sockaddr *)&remote_address, (socklen_t *)&address_size); // server response receiving if(receive_packet(&command_socket, packet) == socket_error) { goto exit; } // server response printing printf ("%s\n", packet); // server data receiving if(receive_packet(&data_socket_server, packet) == socket_error) { goto exit; } // server data printing printf ("%s\n", packet); // sockets closing closesocket(data_socket_server); closesocket(data_socket); }
listening socket creation:
// listening server function socket listen_to_server(char **ip) { // socket variable socket data_socket; // socket address structure struct sockaddr_in socket_address; // listening host name char host_name [256] = {0}; // listening host entry struct hostent *host_entry; // host name initialization if (gethostname(host_name, sizeof(host_name)) == invalid_socket) { printf("error: failed host name\n"); return invalid_socket; } // host entry structure initialization if ((host_entry = gethostbyname(host_name)) == null) { printf("error: failed host name \'%s\'\n", host_name); return invalid_socket; } // socket address structure initialization socket_address.sin_family = af_inet; socket_address.sin_port = htons(data_port); socket_address.sin_addr = *(struct in_addr *)host_entry -> h_addr; memset (&(socket_address.sin_zero), 0, 8); // socket initialization if ((data_socket = socket(af_inet, sock_stream, 0)) == invalid_socket) { printf("error: failed initialize socket\n"); return invalid_socket; } // socket , address binding if (socket_error == bind(data_socket, (struct sockaddr *)&socket_address, sizeof(socket_address))) { printf("error: failed bind socket\n"); closesocket(data_socket); return invalid_socket; } // socket seting listening if (socket_error == listen(data_socket, max_queue_length)) { printf("error: failed set socket listening\n"); closesocket (data_socket); return invalid_socket; } *ip = inet_ntoa(*(struct in_addr *)host_entry -> h_addr); return data_socket; }
also, netstat doesn't show listening data soxket foreign address:
tcp 0 0 127.0.1.1:1024 0.0.0.0:* listen
if ((host_entry = gethostbyname(host_name)) == null) ... socket_address.sin_addr = *(struct in_addr *)host_entry -> h_addr; ... if (socket_error == bind(data_socket, (struct sockaddr *)&socket_address, sizeof(socket_address)))
it looks assume, locally configured hostname of system resolve external address. assumption wrong.
instead should use local address of ftp control connection (getsockname
) local address of data connection.
Comments
Post a Comment