Git-Url: 
http://git.frugalware.org/gitweb/gitweb.cgi?p=pacman-g2.git;a=commitdiff;h=a2f23cb3291b863db54e81b6ee6349ef8f4e05fa

commit a2f23cb3291b863db54e81b6ee6349ef8f4e05fa
Author: Elentir <[email protected]>
Date:   Sat Jan 28 22:55:49 2012 +0100

libftp: set SO_*TIMEO on sockets to avoid blocking I/O

diff --git a/lib/libftp/ftplib.c b/lib/libftp/ftplib.c
index 0a32291..9b960fe 100644
--- a/lib/libftp/ftplib.c
+++ b/lib/libftp/ftplib.c
@@ -460,6 +460,23 @@ GLOBALDEF int FtpConnect(const char *host, netbuf 
**nControl)
net_close(sControl);
return 0;
}
+       struct timeval timeout;
+       timeout.tv_sec = 1;
+       timeout.tv_usec = 0;
+       if (setsockopt (sControl, SOL_SOCKET, SO_RCVTIMEO,
+                       SETSOCKOPT_OPTVAL_TYPE &timeout, sizeof(timeout)) == -1)
+       {
+               perror("setsockopt");
+               net_close(sControl);
+               return -1;
+       }
+       if (setsockopt (sControl, SOL_SOCKET, SO_SNDTIMEO,
+                       SETSOCKOPT_OPTVAL_TYPE &timeout, sizeof(timeout)) == -1)
+       {
+               perror("setsockopt");
+               net_close(sControl);
+               return -1;
+       }
if (connect(sControl, (struct sockaddr *)&sin, sizeof(sin)) == -1)
{
perror("connect");
@@ -674,6 +691,24 @@ static int FtpOpenPort(netbuf *nControl, netbuf **nData, 
int mode, int dir)
net_close(sData);
return -1;
}
+       struct timeval timeout;
+       timeout.tv_sec = 1;
+       timeout.tv_usec = 0;
+       if (setsockopt (sData, SOL_SOCKET, SO_RCVTIMEO,
+                       SETSOCKOPT_OPTVAL_TYPE &timeout, sizeof(timeout)) == -1)
+       {
+               perror("setsockopt");
+               net_close(sData);
+               return -1;
+       }
+       if (setsockopt (sData, SOL_SOCKET, SO_SNDTIMEO,
+                       SETSOCKOPT_OPTVAL_TYPE &timeout, sizeof(timeout)) == -1)
+       {
+               perror("setsockopt");
+               net_close(sData);
+               return -1;
+       }
+
if (nControl->cmode == FTPLIB_PASSIVE)
{
if (connect(sData, &sin.sa, sizeof(sin.sa)) == -1)
@@ -901,11 +936,11 @@ GLOBALDEF int FtpRead(void *buf, int max, netbuf *nData)
{
i = socket_wait(nData);
if (i != 1)
-                       return 0;
+                       return -1;
i = net_read(nData->handle, buf, max);
}
if (i == -1)
-               return 0;
+               return -1;
nData->xfered += i;
if (nData->idlecb && nData->cbbytes)
{
@@ -969,7 +1004,7 @@ GLOBALDEF int FtpClose(netbuf *nData)
net_close(nData->handle);
ctrl = nData->ctrl;
free(nData);
-                       if (ctrl && (nData->errcode != ETIMEDOUT))
+                       if (ctrl)
{
ctrl->data = NULL;
return(readresp('2', ctrl));
@@ -1168,6 +1203,13 @@ static int FtpXfer(const char *localfile, const char 
*path,
}
}

+       if((l == -1) && (errno == EWOULDBLOCK))
+       {
+               nControl->errcode = ETIMEDOUT;
+               strncpy(nControl->response, strerror(ETIMEDOUT),
+                               sizeof(nControl->response));
+       }
+
if (nData->errcode)
rv = 0;

@@ -1401,6 +1443,23 @@ GLOBALREF int HttpConnect(const char *host, unsigned 
short port, netbuf **nContr
perror("socket");
return 0;
}
+       struct timeval timeout;
+       timeout.tv_sec = 1;
+       timeout.tv_usec = 0;
+       if (setsockopt (sControl, SOL_SOCKET, SO_RCVTIMEO,
+                       SETSOCKOPT_OPTVAL_TYPE &timeout, sizeof(timeout)) == -1)
+       {
+               perror("setsockopt");
+               net_close(sControl);
+               return -1;
+       }
+       if (setsockopt (sControl, SOL_SOCKET, SO_SNDTIMEO,
+                       SETSOCKOPT_OPTVAL_TYPE &timeout, sizeof(timeout)) == -1)
+       {
+               perror("setsockopt");
+               net_close(sControl);
+               return -1;
+       }
if (connect(sControl, (struct sockaddr *)&sin, sizeof(sin)) == -1)
{
perror("connect");
@@ -1528,6 +1587,13 @@ static int HttpXfer(const char *localfile, const char 
*path, int *size,
}
}

+       if((l == -1) && (errno == EWOULDBLOCK))
+       {
+               nControl->errcode = ETIMEDOUT;
+               strncpy(nControl->response, strerror(ETIMEDOUT),
+                               sizeof(nControl->response));
+       }
+
if(nControl->errcode)
rv = 0;
nControl->dir = FTPLIB_CONTROL;
_______________________________________________
Frugalware-git mailing list
[email protected]
http://frugalware.org/mailman/listinfo/frugalware-git

Reply via email to