Add proxychains singal fixes, refactor code while I was there it was pretty horrid :/.

This commit is contained in:
Adam Hamsik 2011-02-25 16:18:29 +02:00
parent 282d3d0390
commit 6f451f094d

View File

@ -43,6 +43,33 @@ extern int proxychains_quiet_mode;
static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static int poll_retry(struct pollfd *fds, nfds_t nfsd, int timeout)
{
int ret;
int time_remain = timeout;
int time_elapsed = 0;
struct timeval start_time;
struct timeval tv;
gettimeofday(&start_time, NULL);
do
{
//printf("Retry %d\n", time_remain);
ret = poll(fds, nfsd, time_remain);
gettimeofday(&tv, NULL);
time_elapsed = ((tv.tv_sec - start_time.tv_sec) * 1000 + (tv.tv_usec - start_time.tv_usec) / 1000);
//printf("Time elapsed %d\n", time_elapsed);
time_remain = timeout - time_elapsed;
}
while (ret == -1 && errno == EINTR && time_remain > 0);
//if (ret == -1)
//printf("Return %d %d %s\n", ret, errno, strerror(errno));
return ret;
}
static void encode_base_64(char* src,char* dest,int max_len) static void encode_base_64(char* src,char* dest,int max_len)
{ {
int n,l,i; int n,l,i;
@ -123,7 +150,7 @@ static int read_line(int fd, char *buff, size_t size)
for(i=0;i<size-1;i++) for(i=0;i<size-1;i++)
{ {
pfd[0].revents=0; pfd[0].revents=0;
ready=poll(pfd,1,tcp_read_time_out); ready=poll_retry(pfd,1,tcp_read_time_out);
if(ready!=1 || !(pfd[0].revents&POLLIN) || 1!=read(fd,&buff[i],1)) if(ready!=1 || !(pfd[0].revents&POLLIN) || 1!=read(fd,&buff[i],1))
return -1; return -1;
else if(buff[i]=='\n') else if(buff[i]=='\n')
@ -142,10 +169,9 @@ static int read_n_bytes(int fd,char *buff, size_t size)
pfd[0].fd=fd; pfd[0].fd=fd;
pfd[0].events=POLLIN; pfd[0].events=POLLIN;
for(i=0;i<size;i++) for(i=0; i < size; i++) {
{
pfd[0].revents=0; pfd[0].revents=0;
ready=poll(pfd,1,tcp_read_time_out); ready=poll_retry(pfd,1,tcp_read_time_out);
if(ready!=1 || !(pfd[0].revents&POLLIN) || 1!=read(fd,&buff[i],1)) if(ready!=1 || !(pfd[0].revents&POLLIN) || 1!=read(fd,&buff[i],1))
return -1; return -1;
} }
@ -162,12 +188,10 @@ static int timed_connect(int sock, const struct sockaddr *addr, unsigned int len
fcntl(sock, F_SETFL, O_NONBLOCK); fcntl(sock, F_SETFL, O_NONBLOCK);
ret=true_connect(sock, addr, len); ret=true_connect(sock, addr, len);
// printf("\nconnect ret=%d\n",ret);fflush(stdout); // printf("\nconnect ret=%d\n",ret);fflush(stdout);
if(ret==-1 && errno==EINPROGRESS) if(ret==-1 && errno==EINPROGRESS) {
{ ret=poll_retry(pfd,1,tcp_connect_time_out);
ret=poll(pfd,1,tcp_connect_time_out);
//printf("\npoll ret=%d\n",ret);fflush(stdout); //printf("\npoll ret=%d\n",ret);fflush(stdout);
if(ret==1) if(ret == 1) {
{
value_len=sizeof(int); value_len=sizeof(int);
getsockopt(sock,SOL_SOCKET,SO_ERROR,&value,&value_len) ; getsockopt(sock,SOL_SOCKET,SO_ERROR,&value,&value_len) ;
//printf("\nvalue=%d\n",value);fflush(stdout); //printf("\nvalue=%d\n",value);fflush(stdout);
@ -175,15 +199,13 @@ static int timed_connect(int sock, const struct sockaddr *addr, unsigned int len
ret=0; ret=0;
else else
ret=-1; ret=-1;
} } else {
else
ret=-1; ret=-1;
} }
else if (ret==0) } else {
; if (ret != 0)
else
ret=-1; ret=-1;
}
fcntl(sock, F_SETFL, !O_NONBLOCK); fcntl(sock, F_SETFL, !O_NONBLOCK);
return ret; return ret;