Hi everyone,
I think I found a bug with the FTP utility that is included with tcpip 4.25 and 5.0. There is no timeout on the data connection which result of total freeze. Don’t get confuse with the FTP login. Data connection is when the server is trying to connect to your socket to send you data.
I’m just going to go over some stuff to bring uou up to speed
Ok so first
To eliminate the delay when the server down, I wrote my own ping function. If the connection is refused then the ping is ok.
Work pretty good and here is the code.
//****************************************************************
// Ping
//****************************************************************
int Ping (char *IpAddress)
{
static void SendTcpString_Timeout (int sig_number);
timer_t tj;
struct itimerspec TimerValue;
struct sigevent event;
static int sock= -1;
struct sockaddr_in server;
struct hostent *hp, *gethostbyname();
if (sock < 0 )
{
/* Create socket */
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0)
{
perror("opening stream socket");
exit(1);
}
/* Connect socket using name specified by command line. */
server.sin_family = AF_INET;
hp = gethostbyname(IpAddress);
if (hp == 0)
{
fprintf(stderr, "%s: unknown host\n", IpAddress);
exit(2);
}
memcpy(&server.sin_addr, hp->h_addr, hp->h_length);
server.sin_port = htons(1024);
signal(SIGUSR1 , SendTcpString_Timeout);
event.sigev_signo = SIGUSR1;
tj = timer_create ( CLOCK_REALTIME, &event );
if ( tj == -1 ) fprintf ( stderr, "Unable to attach timer\n");
TimerValue.it_value.tv_sec = 50;
TimerValue.it_value.tv_nsec = 0;
TimerValue.it_interval.tv_sec = 0;
TimerValue.it_interval.tv_nsec = 0;
timer_settime( tj, TIMER_ABSTIME, &TimerValue, NULL );
if (connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0)
{
//perror("->Connecting stream socket");
timer_delete (tj);
close(sock);
sock = -1;
if (errno == EINTR) return (0);
else return (1);
}
timer_delete (tj);
close (sock);
sock = -1;
}
return (1);
}
//****************************************************************
// TcpSendString_TimeOut
//****************************************************************
static void SendTcpString_Timeout (int sig_number)
{
}
Then I wrote the following FTP script to check for update and to download file.
#!/bin/sh
SERVER=$1
PATH=$2
FTP_OPTION=$3
FILE=$4
LOCAL_PATH=/home/myftp
USER=ftp_user
PASSWD=ftp_user
# Delete old list
/bin/rm -f myftp.lst
case $FTP_OPTION in
GET_LIST)
/usr/ucb/ftp -n $SERVER <<SCRIPT
user $USER $PASSWD
dir $PATH myftp.lst
quit
SCRIPT
;;
GET_FILE)
/usr/ucb/ftp -n $SERVER <<SCRIPT
user $USER $PASSWD
binary
cd $PATH
lcd download
get $FILE
quit
SCRIPT
;;
*) ;;
esac
There is also an aplication that ping the sever just before to launch the script. This application is launched at startup and everytime the device is rebooted then we are checking for update.
This is where my FTP nightmare begin. Sometime the FTP utility will freeze and it is totaly random. I added delay up to 10 sec thinking that the socket didn’t have time to initialize. Tried ftp 4.25 , 5.0 and the result was the same.
So I was browsing on QNX for clue and I found
quics.qnx.com/cgi-bin/dir_find.c … tcpip/ftp/
An FTP library wrote by Oleg Orel.
So I wrote an apllication using the library and guess what? I was experiencing the same issue , program freeze !!!
Apparently to receive data from the server you have to establish a data connection by opening a port using a socket and I found exactly what was the problem.The program was waiting to accept a connection from the server to receive the data but for some reason the server didn’t try to connect.
So I added the following code to FtpData.c
FD_ZERO(&ready);
FD_SET(NewSocket, &ready);
to.tv_sec = 5;
to.tv_usec = 0;
if (select(NewSocket + 1, &ready, 0, 0, &to) < 0)
{
perror("select");
return -1;
}
if (FD_ISSET(NewSocket, &ready))
{
if (( Accepted_Socket = accept (NewSocket , &from , &fromlen )) < 0)
{
close(NewSocket);
return -1;
}
}
else
{
printf("Timeout 5 sec - Exit FtpData \n");
return -1;
}
If the timeout expired then I launched the function again until it failed 3 times.
It’s worked but It is really random since u can failed up to 3 time (rarely but it happened).
This is really slowing down the boot up of my system and we would like to know if anybody got any insight about why the server is not trying to connect sometime.
Server is running
Micorsoft FTP Service (Version 5.0)
It doesn’t happen when u start the application manually or with the FTp utility. So even if i add a delay of 15 sec when I tried to launch application at startup the server will randomly not connect to my data connection.
If anybody is an FTP expert and could give me some hint about this behavior that will be really appreciated.
Johnny