From 8fe8503cbdee9f7557d47d1ba134e33cce8b6415 Mon Sep 17 00:00:00 2001 From: Mario Fetka Date: Wed, 1 Sep 2010 10:52:02 +0200 Subject: [PATCH] uncrustify sources --- libprozilla/src/common.h | 36 +- libprozilla/src/connect.c | 285 ++-- libprozilla/src/connect.h | 52 +- libprozilla/src/connection.c | 687 +++++---- libprozilla/src/connection.h | 64 +- libprozilla/src/debug.c | 105 +- libprozilla/src/debug.h | 30 +- libprozilla/src/download.c | 2788 +++++++++++++++++----------------- libprozilla/src/download.h | 48 +- libprozilla/src/ftp-retr.c | 337 ++-- libprozilla/src/ftp-retr.h | 38 +- libprozilla/src/ftp.c | 1142 +++++++------- libprozilla/src/ftp.h | 86 +- libprozilla/src/ftpparse.c | 289 ++-- libprozilla/src/ftpparse.h | 38 +- libprozilla/src/ftpsearch.c | 1093 +++++++------ libprozilla/src/ftpsearch.h | 28 +- libprozilla/src/http-retr.c | 581 +++---- libprozilla/src/http-retr.h | 36 +- libprozilla/src/http.c | 776 +++++----- libprozilla/src/http.h | 62 +- libprozilla/src/logfile.c | 298 ++-- libprozilla/src/logfile.h | 30 +- libprozilla/src/main.c | 78 +- libprozilla/src/misc.c | 543 ++++--- libprozilla/src/misc.h | 30 +- libprozilla/src/netrc.c | 376 ++--- libprozilla/src/netrc.h | 8 +- libprozilla/src/ping.c | 313 ++-- libprozilla/src/ping.h | 26 +- libprozilla/src/prozilla.h | 855 ++++++----- libprozilla/src/test.c | 46 +- libprozilla/src/url.c | 861 ++++++----- libprozilla/src/url.h | 74 +- src/download_win.cpp | 1012 ++++++------ src/download_win.h | 65 +- src/ftps_win.h | 10 +- src/ftpsearch_win.cpp | 268 ++-- src/ftpsearch_win.h | 55 +- src/index.html | 37 - src/init.cpp | 105 +- src/init.h | 6 +- src/interface.c | 380 +++-- src/interface.h | 18 +- src/main.cpp | 875 +++++------ src/main.h | 30 +- src/misc.cpp | 129 +- src/prefs.cpp | 616 ++++---- src/prefs.h | 26 +- 49 files changed, 7937 insertions(+), 7834 deletions(-) delete mode 100644 src/index.html diff --git a/libprozilla/src/common.h b/libprozilla/src/common.h index e614efc..a212db9 100644 --- a/libprozilla/src/common.h +++ b/libprozilla/src/common.h @@ -1,25 +1,25 @@ /****************************************************************************** - libprozilla - a download accelerator library - Copyright (C) 2001 Kalum Somaratna + libprozilla - a download accelerator library + Copyright (C) 2001 Kalum Somaratna - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ******************************************************************************/ /* Common #includes and #defines. */ -/* $Id: common.h,v 1.7 2001/10/27 23:20:24 kalum Exp $ */ +/* $Id$ */ #ifndef COMMON_H @@ -82,7 +82,7 @@ #include - + #ifdef __FreeBSD__ #include #include @@ -170,12 +170,12 @@ typedef int boolean; /* Gettext */ #include -#define _(String) dgettext (PACKAGE, String) +#define _(String) dgettext(PACKAGE, String) #define gettext_noop(String) (String) #ifndef HAVE_GNOME -#define N_(String) gettext_noop (String) +#define N_(String) gettext_noop(String) #endif /* Gettext */ -#endif /* COMMON_H */ +#endif /* COMMON_H */ diff --git a/libprozilla/src/connect.c b/libprozilla/src/connect.c index d531a01..b545ed8 100644 --- a/libprozilla/src/connect.c +++ b/libprozilla/src/connect.c @@ -1,20 +1,20 @@ /****************************************************************************** - libprozilla - a download accelerator library - Copyright (C) 2001 Kalum Somaratna + libprozilla - a download accelerator library + Copyright (C) 2001 Kalum Somaratna - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ******************************************************************************/ /* Connection routines. */ @@ -30,18 +30,18 @@ /****************************************************************************** - Connect to the specified server. + Connect to the specified server. ******************************************************************************/ uerr_t connect_to_server(int *sock, const char *name, int port, - struct timeval *tout) + struct timeval *tout) { int status, noblock, flags; char szPort[10]; extern int h_errno; - int opt; - struct timeval timeout; - struct addrinfo hints, *res=NULL; - int error; + int opt; + struct timeval timeout; + struct addrinfo hints, *res = NULL; + int error; assert(name != NULL); @@ -53,17 +53,17 @@ uerr_t connect_to_server(int *sock, const char *name, int port, hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; - error = getaddrinfo(name, szPort, &hints, &res); - if (error) - return HOSTERR; + error = getaddrinfo(name, szPort, &hints, &res); + if (error) + return HOSTERR; /* Create a socket. */ if ((*sock = socket(res->ai_family, res->ai_socktype, IPPROTO_TCP)) < 1) - { - freeaddrinfo(res); - return CONSOCKERR; - } + { + freeaddrinfo(res); + return CONSOCKERR; + } /* Experimental. */ flags = fcntl(*sock, F_GETFL, 0); @@ -76,68 +76,71 @@ uerr_t connect_to_server(int *sock, const char *name, int port, if ((status == -1) && (noblock != -1) && (errno == EINPROGRESS)) - { - fd_set writefd; - - FD_ZERO(&writefd); - FD_SET(*sock, &writefd); - - status = select((*sock + 1), NULL, &writefd, NULL, &timeout); - - /* Do we need to retry if the err is EINTR? */ - - if (status > 0) { - socklen_t arglen = sizeof(int); + fd_set writefd; - if (getsockopt(*sock, SOL_SOCKET, SO_ERROR, &status, &arglen) < 0) - status = errno; + FD_ZERO(&writefd); + FD_SET(*sock, &writefd); - if (status != 0) - errno = status, status = -1; + status = select((*sock + 1), NULL, &writefd, NULL, &timeout); - if (errno == EINPROGRESS) - errno = ETIMEDOUT; - } else if (status == 0) - errno = ETIMEDOUT, status = -1; - } + /* Do we need to retry if the err is EINTR? */ + + if (status > 0) + { + socklen_t arglen = sizeof(int); + + if (getsockopt(*sock, SOL_SOCKET, SO_ERROR, &status, &arglen) < 0) + status = errno; + + if (status != 0) + errno = status, status = -1; + + if (errno == EINPROGRESS) + errno = ETIMEDOUT; + } + else if (status == 0) + errno = ETIMEDOUT, status = -1; + } if (status < 0) - { - close(*sock); + { + close(*sock); - if (errno == ECONNREFUSED) - { - freeaddrinfo(res); - return CONREFUSED; - } else - { - freeaddrinfo(res); - return CONERROR; + if (errno == ECONNREFUSED) + { + freeaddrinfo(res); + return CONREFUSED; + } + else + { + freeaddrinfo(res); + return CONERROR; + } } - } else - { - flags = fcntl(*sock, F_GETFL, 0); + else + { + flags = fcntl(*sock, F_GETFL, 0); - if (flags != -1) - fcntl(*sock, F_SETFL, flags & ~O_NONBLOCK); - } + if (flags != -1) + fcntl(*sock, F_SETFL, flags & ~O_NONBLOCK); + } + + + /* Enable KEEPALIVE, so dead connections could be closed + * earlier. Useful in conjuction with TCP kernel tuning + * in /proc/sys/net/ipv4/tcp_* files. */ + opt = 1; + setsockopt(*sock, SOL_SOCKET, SO_KEEPALIVE, + (char *)&opt, (int)sizeof(opt)); - - /* Enable KEEPALIVE, so dead connections could be closed - * earlier. Useful in conjuction with TCP kernel tuning - * in /proc/sys/net/ipv4/tcp_* files. */ - opt = 1; - setsockopt(*sock, SOL_SOCKET, SO_KEEPALIVE, - (char *) &opt, (int) sizeof(opt)); - freeaddrinfo(res); return NOCONERROR; } /****************************************************************************** - ... + ... ******************************************************************************/ uerr_t bind_socket(int *sockfd) { @@ -148,32 +151,32 @@ uerr_t bind_socket(int *sockfd) return CONSOCKERR; /* Fill in the structure fields for binding. */ - memset((void *) &serv_addr, 0, sizeof(serv_addr)); + memset((void *)&serv_addr, 0, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); - serv_addr.sin_port = htons(0); /* Let the system choose. */ + serv_addr.sin_port = htons(0); /* Let the system choose. */ /* Bind the address to the socket. */ - if (bind(*sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) - { - perror("bind"); - close(*sockfd); - return BINDERR; - } + if (bind(*sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) + { + perror("bind"); + close(*sockfd); + return BINDERR; + } /* Allow only one server. */ if (listen(*sockfd, 1) < 0) - { - perror("listen"); - close(*sockfd); - return LISTENERR; - } + { + perror("listen"); + close(*sockfd); + return LISTENERR; + } return BINDOK; } /****************************************************************************** - ... + ... ******************************************************************************/ int select_fd(int fd, struct timeval *timeout, int writep) { @@ -186,15 +189,15 @@ int select_fd(int fd, struct timeval *timeout, int writep) FD_SET(fd, &exceptfds); memcpy(&to, timeout, sizeof(struct timeval)); - return (select(fd + 1, writep ? NULL : &fds, writep ? &fds : NULL, - &exceptfds, &to)); + return(select(fd + 1, writep ? NULL : &fds, writep ? &fds : NULL, + &exceptfds, &to)); } /****************************************************************************** - Receive size bytes from sock with a time delay. + Receive size bytes from sock with a time delay. ******************************************************************************/ int krecv(int sock, char *buffer, int size, int flags, - struct timeval *timeout) + struct timeval *timeout) { int ret, arglen; @@ -203,40 +206,38 @@ int krecv(int sock, char *buffer, int size, int flags, assert(size >= 0); do - { - if (timeout) { - do - { - ret = select_fd(sock, timeout, 0); - } - while ((ret == -1) && (errno == EINTR)); + if (timeout) + { + do + { + ret = select_fd(sock, timeout, 0); + } while ((ret == -1) && (errno == EINTR)); - if (ret <= 0) - { - /* proz_debug("Error after select res=%d errno=%d.", ret, errno); */ + if (ret <= 0) + { + /* proz_debug("Error after select res=%d errno=%d.", ret, errno); */ - /* Set errno to ETIMEDOUT on timeout. */ - if (ret == 0) - errno = ETIMEDOUT; + /* Set errno to ETIMEDOUT on timeout. */ + if (ret == 0) + errno = ETIMEDOUT; - return -1; - } - } + return -1; + } + } - ret = recv(sock, buffer, size, flags); - } - while ((ret == -1) && (errno == EINTR)); + ret = recv(sock, buffer, size, flags); + } while ((ret == -1) && (errno == EINTR)); return ret; } /****************************************************************************** - Send size bytes to sock with a time delay. + Send size bytes to sock with a time delay. ******************************************************************************/ int ksend(int sock, char *buffer, int size, int flags, - struct timeval *timeout) + struct timeval *timeout) { int ret = 0; @@ -246,40 +247,38 @@ int ksend(int sock, char *buffer, int size, int flags, innermost loop deals with the same during select(). */ while (size != 0) - { - do { - if (timeout) - { - do - { - ret = select_fd(sock, timeout, 1); - } - while ((ret == -1) && (errno == EINTR)); + do + { + if (timeout) + { + do + { + ret = select_fd(sock, timeout, 1); + } while ((ret == -1) && (errno == EINTR)); - if (ret <= 0) - { - /* Set errno to ETIMEDOUT on timeout. */ - if (ret == 0) - errno = ETIMEDOUT; - return -1; - } - } - ret = send(sock, buffer, size, flags); + if (ret <= 0) + { + /* Set errno to ETIMEDOUT on timeout. */ + if (ret == 0) + errno = ETIMEDOUT; + return -1; + } + } + ret = send(sock, buffer, size, flags); + } while ((ret == -1) && (errno == EINTR)); + + if (ret <= 0) + break; + + buffer += ret; + size -= ret; } - while ((ret == -1) && (errno == EINTR)); - - if (ret <= 0) - break; - - buffer += ret; - size -= ret; - } return ret; } /****************************************************************************** - Accept a connection. + Accept a connection. ******************************************************************************/ uerr_t accept_connection(int listen_sock, int *data_sock) { @@ -287,12 +286,12 @@ uerr_t accept_connection(int listen_sock, int *data_sock) socklen_t clilen = sizeof(cli_addr); int sockfd; - sockfd = accept(listen_sock, (struct sockaddr *) &cli_addr, &clilen); + sockfd = accept(listen_sock, (struct sockaddr *)&cli_addr, &clilen); if (sockfd < 0) - { - perror("accept"); - return ACCEPTERR; - } + { + perror("accept"); + return ACCEPTERR; + } *data_sock = sockfd; diff --git a/libprozilla/src/connect.h b/libprozilla/src/connect.h index 2880a02..1478b73 100644 --- a/libprozilla/src/connect.h +++ b/libprozilla/src/connect.h @@ -1,25 +1,25 @@ /****************************************************************************** - libprozilla - a download accelerator library - Copyright (C) 2001 Kalum Somaratna + libprozilla - a download accelerator library + Copyright (C) 2001 Kalum Somaratna - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ******************************************************************************/ /* Connection routines. */ -/* $Id: connect.h,v 1.17 2001/05/09 22:58:00 kalum Exp $ */ +/* $Id$ */ #ifndef CONNECT_H @@ -32,22 +32,22 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ - uerr_t connect_to_server(int *sock, const char *name, int port, - struct timeval *timeout); +uerr_t connect_to_server(int *sock, const char *name, int port, + struct timeval *timeout); - uerr_t bind_socket(int *sockfd); +uerr_t bind_socket(int *sockfd); - int select_fd(int fd, struct timeval *timeout, int writep); +int select_fd(int fd, struct timeval *timeout, int writep); - int krecv(int sock, char *buffer, int size, int flags, - struct timeval *timeout); - int ksend(int sock, char *buffer, int size, int flags, - struct timeval *timeout); - uerr_t accept_connection(int listen_sock, int *data_sock); +int krecv(int sock, char *buffer, int size, int flags, + struct timeval *timeout); +int ksend(int sock, char *buffer, int size, int flags, + struct timeval *timeout); +uerr_t accept_connection(int listen_sock, int *data_sock); #ifdef __cplusplus } -#endif /* __cplusplus */ -#endif /* CONNECT_H */ +#endif /* __cplusplus */ +#endif /* CONNECT_H */ diff --git a/libprozilla/src/connection.c b/libprozilla/src/connection.c index 8b4934d..ef8481a 100644 --- a/libprozilla/src/connection.c +++ b/libprozilla/src/connection.c @@ -1,25 +1,25 @@ /****************************************************************************** - libprozilla - a download accelerator library - Copyright (C) 2001 Kalum Somaratna + libprozilla - a download accelerator library + Copyright (C) 2001 Kalum Somaratna - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ******************************************************************************/ /* Several connection-related routines. */ -/* $Id: connection.c,v 1.48 2005/09/19 16:18:02 kalum Exp $ */ +/* $Id$ */ #include "common.h" @@ -33,7 +33,7 @@ /****************************************************************************** - ... + ... ******************************************************************************/ void init_response(connection_t * connection) { @@ -41,8 +41,8 @@ void init_response(connection_t * connection) } /****************************************************************************** - This will free up the serv_ret_lines array if necessary, in order to prepare - it for the storage of the servers response. + This will free up the serv_ret_lines array if necessary, in order to prepare + it for the storage of the servers response. ******************************************************************************/ void done_with_response(connection_t * connection) { @@ -55,52 +55,51 @@ void done_with_response(connection_t * connection) p1 = connection->serv_ret_lines; do - { - p = p1; - p1 = p->next; - kfree(p); - } - while (p1 != 0); + { + p = p1; + p1 = p->next; + kfree(p); + } while (p1 != 0); connection->serv_ret_lines = 0; } /****************************************************************************** - Initialises the connection and sets it with values from the runtime struct. + Initialises the connection and sets it with values from the runtime struct. ******************************************************************************/ -connection_t * proz_connection_init(urlinfo *url,pthread_mutex_t * mutex) +connection_t * proz_connection_init(urlinfo *url, pthread_mutex_t * mutex) { + connection_t * connection = kmalloc(sizeof(connection_t)); - connection_t * connection=kmalloc(sizeof(connection_t)); memset(connection, 0, sizeof(connection_t)); /* memcpy(&connection->u, url, sizeof(urlinfo));*/ - if(url) + if (url) memcpy(&connection->u, - proz_copy_url(url), - sizeof(urlinfo)); + proz_copy_url(url), + sizeof(urlinfo)); /* Copy the proxy structs. */ if (libprozrtinfo.ftp_proxy) - { - connection->ftp_proxy = kmalloc(sizeof(proxy_info)); - memcpy(connection->ftp_proxy, libprozrtinfo.ftp_proxy, - sizeof(proxy_info)); - /* - connection->use_ftp_proxy = libprozrtinfo.use_ftp_proxy; - */ - } + { + connection->ftp_proxy = kmalloc(sizeof(proxy_info)); + memcpy(connection->ftp_proxy, libprozrtinfo.ftp_proxy, + sizeof(proxy_info)); + /* + connection->use_ftp_proxy = libprozrtinfo.use_ftp_proxy; + */ + } if (libprozrtinfo.http_proxy) - { - connection->http_proxy = kmalloc(sizeof(proxy_info)); - memcpy(connection->http_proxy, libprozrtinfo.http_proxy, - sizeof(proxy_info)); - /* - connection->use_http_proxy = libprozrtinfo.use_http_proxy; - */ - } + { + connection->http_proxy = kmalloc(sizeof(proxy_info)); + memcpy(connection->http_proxy, libprozrtinfo.http_proxy, + sizeof(proxy_info)); + /* + connection->use_http_proxy = libprozrtinfo.use_http_proxy; + */ + } connection->use_netrc = libprozrtinfo.use_netrc; @@ -115,13 +114,13 @@ connection_t * proz_connection_init(urlinfo *url,pthread_mutex_t * mutex) connection->attempts = 0; /* Initialise all with default timeouts */ memcpy(&connection->xfer_timeout, &libprozrtinfo.conn_timeout, - sizeof(connection->xfer_timeout)); + sizeof(connection->xfer_timeout)); memcpy(&connection->ctrl_timeout, &libprozrtinfo.conn_timeout, - sizeof(connection->ctrl_timeout)); + sizeof(connection->ctrl_timeout)); memcpy(&connection->conn_timeout, &libprozrtinfo.conn_timeout, - sizeof(connection->conn_timeout)); + sizeof(connection->conn_timeout)); memcpy(&connection->retry_delay, &libprozrtinfo.conn_retry_delay, - sizeof(&connection->retry_delay)); + sizeof(&connection->retry_delay)); connection->max_attempts = libprozrtinfo.max_attempts; connection->rate_bps = 0; @@ -130,9 +129,9 @@ connection_t * proz_connection_init(urlinfo *url,pthread_mutex_t * mutex) pthread_cond_init(&connection->connecting_cond, NULL); connection->status_change_mutex = mutex; if (connection->status_change_mutex != 0) - { - pthread_mutex_init(connection->status_change_mutex, NULL); - } + { + pthread_mutex_init(connection->status_change_mutex, NULL); + } pthread_mutex_init(&connection->access_mutex, NULL); return connection; @@ -142,23 +141,23 @@ connection_t * proz_connection_init(urlinfo *url,pthread_mutex_t * mutex) void connection_change_status(connection_t * connection, dl_status status) { if (connection->status_change_mutex != 0) - { - pthread_mutex_lock(connection->status_change_mutex); - connection->status = status; - pthread_mutex_unlock(connection->status_change_mutex); - } + { + pthread_mutex_lock(connection->status_change_mutex); + connection->status = status; + pthread_mutex_unlock(connection->status_change_mutex); + } } -/* this will open connection->localfile and read from connection->data_sock - (which should be already setup) till a EOF is reached or - the server closes the connection, in which case there is no way to know - whether we got the complete file. -*/ +/* this will open connection->localfile and read from connection->data_sock + (which should be already setup) till a EOF is reached or + the server closes the connection, in which case there is no way to know + whether we got the complete file. + */ uerr_t connection_retr_fsize_not_known(connection_t * connection, - char *read_buffer, - int read_buffer_size) + char *read_buffer, + int read_buffer_size) { off_t bytes_read; @@ -166,64 +165,63 @@ uerr_t connection_retr_fsize_not_known(connection_t * connection, gettimeofday(&connection->time_begin, NULL); do - { - bytes_read = - krecv(connection->data_sock, read_buffer, read_buffer_size, 0, - &connection->xfer_timeout); - if (bytes_read > 0) { - if (write_data_with_lock(connection, read_buffer, sizeof(char), bytes_read) < bytes_read) - { - proz_debug(_("write failed")); - connection_show_message(connection, - _ - ("Unable to write to file %s: %s!"), - connection->localfile, strerror(errno)); - connection_change_status(connection, LOCALFATAL); - return FWRITEERR; - } - pthread_mutex_lock(&connection->access_mutex); - connection->remote_bytes_received += bytes_read; - pthread_mutex_unlock(&connection->access_mutex); + bytes_read = + krecv(connection->data_sock, read_buffer, read_buffer_size, 0, + &connection->xfer_timeout); + if (bytes_read > 0) + { + if (write_data_with_lock(connection, read_buffer, sizeof(char), bytes_read) < bytes_read) + { + proz_debug(_("write failed")); + connection_show_message(connection, + _ + ("Unable to write to file %s: %s!"), + connection->localfile, strerror(errno)); + connection_change_status(connection, LOCALFATAL); + return FWRITEERR; + } + pthread_mutex_lock(&connection->access_mutex); + connection->remote_bytes_received += bytes_read; + pthread_mutex_unlock(&connection->access_mutex); - /*TODO: caclculate and throttle connections speed here */ - /*DONE: */ - connection_calc_ratebps(connection); - connection_throttle_bps(connection); - } - } - while (bytes_read > 0); + /*TODO: caclculate and throttle connections speed here */ + /*DONE: */ + connection_calc_ratebps(connection); + connection_throttle_bps(connection); + } + } while (bytes_read > 0); if (bytes_read == -1) - { - if (errno == ETIMEDOUT) { - proz_debug(_("connection timed out")); - connection_change_status(connection, TIMEDOUT); + if (errno == ETIMEDOUT) + { + proz_debug(_("connection timed out")); + connection_change_status(connection, TIMEDOUT); + return READERR; + } + connection_change_status(connection, REMOTEFATAL); return READERR; } - connection_change_status(connection, REMOTEFATAL); - return READERR; - } connection_change_status(connection, COMPLETED); connection_show_message(connection, - _("download for this connection completed" - "%s : %ld received"), connection->localfile, - connection->remote_bytes_received); + _("download for this connection completed" + "%s : %ld received"), connection->localfile, + connection->remote_bytes_received); return FILEGETOK; } -/* This will open connection->localfile and read from connection->data_sock +/* This will open connection->localfile and read from connection->data_sock (which should be already setup) till the requested number of bytes are read. - Now since we explicitly know how much bytes to get we can do so, and is the server - closes the connection prematurely we know that has hapenned (because it hasn't supplied + Now since we explicitly know how much bytes to get we can do so, and is the server + closes the connection prematurely we know that has hapenned (because it hasn't supplied the required number of bytes) and return a READERR. -*/ + */ uerr_t connection_retr_fsize_known(connection_t * connection, - char *read_buffer, int read_buffer_size) + char *read_buffer, int read_buffer_size) { off_t bytes_read; off_t bytes_to_get; @@ -236,94 +234,90 @@ uerr_t connection_retr_fsize_known(connection_t * connection, gettimeofday(&connection->time_begin, NULL); while (bytes_to_get > 0) - { - bytes_read = - krecv(connection->data_sock, read_buffer, - bytes_to_get > - read_buffer_size ? read_buffer_size : bytes_to_get, 0, - &connection->xfer_timeout); - - if (bytes_read == 0 && bytes_to_get > 0) { - connection_show_message(connection, - _("Server Closed Connection Prematurely!")); - connection_change_status(connection, REMOTEFATAL); - return READERR; + bytes_read = + krecv(connection->data_sock, read_buffer, + bytes_to_get > + read_buffer_size ? read_buffer_size : bytes_to_get, 0, + &connection->xfer_timeout); + + if (bytes_read == 0 && bytes_to_get > 0) + { + connection_show_message(connection, + _("Server Closed Connection Prematurely!")); + connection_change_status(connection, REMOTEFATAL); + return READERR; + } + + if (bytes_read == -1) + { + if (errno == ETIMEDOUT) + { + proz_debug(_("connection timed out")); + connection_change_status(connection, TIMEDOUT); + return READERR; + } + connection_change_status(connection, REMOTEFATAL); + return READERR; + } + + bytes_to_get -= bytes_read; + + if (bytes_read > 0) + { + if (write_data_with_lock(connection, read_buffer, sizeof(char), bytes_read) < bytes_read) + { + proz_debug(_("write failed")); + connection_show_message(connection, + _ + ("Unable to write to file %s: %s!"), + connection->localfile, strerror(errno)); + connection_change_status(connection, LOCALFATAL); + return FWRITEERR; + } + pthread_mutex_lock(&connection->access_mutex); + connection->remote_bytes_received += bytes_read; + pthread_mutex_unlock(&connection->access_mutex); + + /*TODO: caclculate and throttle connections speed here */ + /*DONE: */ + connection_calc_ratebps(connection); + connection_throttle_bps(connection); + } } - if (bytes_read == -1) - { - if (errno == ETIMEDOUT) - { - proz_debug(_("connection timed out")); - connection_change_status(connection, TIMEDOUT); - return READERR; - } - connection_change_status(connection, REMOTEFATAL); - return READERR; - } - - bytes_to_get -= bytes_read; - - if (bytes_read > 0) - { - - if (write_data_with_lock(connection, read_buffer, sizeof(char), bytes_read) < bytes_read) - { - - proz_debug(_("write failed")); - connection_show_message(connection, - _ - ("Unable to write to file %s: %s!"), - connection->localfile, strerror(errno)); - connection_change_status(connection, LOCALFATAL); - return FWRITEERR; - } - pthread_mutex_lock(&connection->access_mutex); - connection->remote_bytes_received += bytes_read; - pthread_mutex_unlock(&connection->access_mutex); - - /*TODO: caclculate and throttle connections speed here */ - /*DONE: */ - connection_calc_ratebps(connection); - connection_throttle_bps(connection); - } - } - connection_change_status(connection, COMPLETED); connection_show_message(connection, - _("download for this connection completed" - "%s : %ld received"), connection->localfile, - connection->remote_bytes_received); + _("download for this connection completed" + "%s : %ld received"), connection->localfile, + connection->remote_bytes_received); return FILEGETOK; } -/* This function modifies a single connections download start and - end info it returns 1 on sucess and -1 on error. -*/ +/* This function modifies a single connections download start and + end info it returns 1 on sucess and -1 on error. + */ int connection_load_resume_info(connection_t * connection) { + if (connection->remote_startpos - connection->orig_remote_startpos != connection->remote_bytes_received) + { + proz_debug("connection->remote start pos before loading %ld", connection->remote_startpos); + //connection->remote_startpos +=connection->remote_bytes_received; + connection->remote_startpos += (connection->remote_bytes_received - (connection->remote_startpos - connection->orig_remote_startpos)); - - - if(connection->remote_startpos-connection->orig_remote_startpos!=connection->remote_bytes_received) - { - proz_debug("connection->remote start pos before loading %ld", connection->remote_startpos); - //connection->remote_startpos +=connection->remote_bytes_received; - connection->remote_startpos +=(connection->remote_bytes_received-(connection->remote_startpos-connection->orig_remote_startpos)); - - proz_debug("connection->remote start pos after loading %ld", connection->remote_startpos); - } + proz_debug("connection->remote start pos after loading %ld", connection->remote_startpos); + } return 1; } dl_status proz_connection_get_status(connection_t * connection) { dl_status status; + pthread_mutex_lock(connection->status_change_mutex); status = connection->status; pthread_mutex_unlock(connection->status_change_mutex); @@ -335,48 +329,50 @@ dl_status proz_connection_get_status(connection_t * connection) char *proz_connection_get_status_string(connection_t * connection) { dl_status status; + pthread_mutex_lock(connection->status_change_mutex); status = connection->status; pthread_mutex_unlock(connection->status_change_mutex); switch (connection->status) - { + { + case IDLE: + return(_("Idle")); - case IDLE: - return (_("Idle")); + case CONNECTING: + return(_("Connecting")); - case CONNECTING: - return (_("Connecting")); + case LOGGININ: + return(_("Logging in")); - case LOGGININ: - return (_("Logging in")); + case DOWNLOADING: + return(_("Downloading")); + break; - case DOWNLOADING: - return (_("Downloading")); - break; - case COMPLETED: - return (_("Completed")); + case COMPLETED: + return(_("Completed")); - case LOGINFAIL: - return (_("Login Denied")); + case LOGINFAIL: + return(_("Login Denied")); - case CONREJECT: - return (_("Connect Refused")); + case CONREJECT: + return(_("Connect Refused")); - case REMOTEFATAL: - return (_("Remote Fatal")); + case REMOTEFATAL: + return(_("Remote Fatal")); - case LOCALFATAL: - return (_("Local Fatal")); + case LOCALFATAL: + return(_("Local Fatal")); - case TIMEDOUT: - return (_("Timed Out")); - case MAXTRYS: - return (_("Max attempts reached")); + case TIMEDOUT: + return(_("Timed Out")); - default: - return (_("Unkown Status!")); - } + case MAXTRYS: + return(_("Max attempts reached")); + + default: + return(_("Unkown Status!")); + } } @@ -384,7 +380,7 @@ pthread_mutex_t connection_msg_mutex = PTHREAD_MUTEX_INITIALIZER; /*calls the msg_proc function if not null */ void connection_show_message(connection_t * connection, const char *format, - ...) + ...) { va_list args; char message[MAX_MSG_SIZE + 1]; @@ -412,21 +408,21 @@ off_t proz_connection_get_total_bytes_got(connection_t * connection) pthread_mutex_unlock(&connection->access_mutex); return ret; - } -/***************************************************************************** -Returns the total number of bytes that has being got from the server -by this connection. -******************************************************************************/ +/***************************************************************************** + Returns the total number of bytes that has being got from the server + by this connection. + ******************************************************************************/ off_t proz_connection_get_total_remote_bytes_got(connection_t * connection) { off_t ret; + pthread_mutex_lock(&connection->access_mutex); ret = (connection->remote_bytes_received - - (connection->remote_startpos-connection->orig_remote_startpos)); + - (connection->remote_startpos - connection->orig_remote_startpos)); pthread_mutex_unlock(&connection->access_mutex); - //proz_debug("CONNECTION TOTAL REMOTE BYTES GOT =%lld", ret); + //proz_debug("CONNECTION TOTAL REMOTE BYTES GOT =%lld", ret); return ret; } @@ -436,18 +432,17 @@ void proz_get_url_info_loop(connection_t * connection, pthread_t *thread) assert(thread); connection->running = TRUE; pthread_create(thread, NULL, - (void *(*)(void *)) get_url_info_loop, - (void *) connection); + (void *(*)(void *))get_url_info_loop, + (void *)connection); } /************************************************************************ -This Fucntion will retreive info about the given url in the connection, -handling conditions like redirection from http to ftp etc + This Fucntion will retreive info about the given url in the connection, + handling conditions like redirection from http to ftp etc -*************************************************************************/ + *************************************************************************/ void get_url_info_loop(connection_t * connection) { - pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); /*TODO Should we try to make it broadcast a condition to the other threads? */ @@ -456,65 +451,66 @@ void get_url_info_loop(connection_t * connection) pthread_mutex_unlock(&connection->access_mutex); do - { - switch (connection->u.proto) { - case URLHTTP: - connection->err = http_get_url_info_loop(connection); - break; - case URLFTP: - connection->err = ftp_get_url_info_loop(connection); - break; - default: - proz_die(_("Error: unsupported protocol")); - } + switch (connection->u.proto) + { + case URLHTTP: + connection->err = http_get_url_info_loop(connection); + break; - if (connection->err == NEWLOCATION) - { - char *constructed_newloc; - char *referer; - referer=kstrdup(connection->u.url); - /*DONE : handle relative urls too */ - constructed_newloc = - uri_merge(connection->u.url, connection->hs.newloc); + case URLFTP: + connection->err = ftp_get_url_info_loop(connection); + break; - proz_debug("Redirected to %s, merged URL = %s", - connection->hs.newloc, constructed_newloc); + default: + proz_die(_("Error: unsupported protocol")); + } - proz_free_url(&connection->u, 0); - connection->err = - proz_parse_url(constructed_newloc, &connection->u, 0); + if (connection->err == NEWLOCATION) + { + char *constructed_newloc; + char *referer; + referer = kstrdup(connection->u.url); + /*DONE : handle relative urls too */ + constructed_newloc = + uri_merge(connection->u.url, connection->hs.newloc); + + proz_debug("Redirected to %s, merged URL = %s", + connection->hs.newloc, constructed_newloc); + + proz_free_url(&connection->u, 0); + connection->err = + proz_parse_url(constructed_newloc, &connection->u, 0); - if (connection->err != URLOK) - { - connection_show_message(connection, - _ - ("The server returned location is wrong: %s!"), - constructed_newloc); - pthread_mutex_lock(&connection->access_mutex); - connection->running = FALSE; - pthread_mutex_unlock(&connection->access_mutex); - kfree(constructed_newloc); - connection->err = HERR; - return; - } else - connection_show_message(connection, _("Redirected to => %s"), - constructed_newloc); - connection->u.referer=referer; - kfree(constructed_newloc); - connection->err = NEWLOCATION; - } - - } - while (connection->err == NEWLOCATION); + if (connection->err != URLOK) + { + connection_show_message(connection, + _ + ("The server returned location is wrong: %s!"), + constructed_newloc); + pthread_mutex_lock(&connection->access_mutex); + connection->running = FALSE; + pthread_mutex_unlock(&connection->access_mutex); + kfree(constructed_newloc); + connection->err = HERR; + return; + } + else + connection_show_message(connection, _("Redirected to => %s"), + constructed_newloc); + connection->u.referer = referer; + kfree(constructed_newloc); + connection->err = NEWLOCATION; + } + } while (connection->err == NEWLOCATION); return; } void proz_connection_set_msg_proc(connection_t * connection, - message_proc msg_proc, void *cb_data) + message_proc msg_proc, void *cb_data) { assert(connection != NULL); @@ -535,16 +531,16 @@ void connection_calc_ratebps(connection_t * connection) if (connection->time_begin.tv_sec == 0 && connection->time_begin.tv_usec == 0) - { - - connection->rate_bps = 0; - pthread_mutex_unlock(&connection->access_mutex); - return; - } else - { - gettimeofday(&tv_cur, NULL); - proz_timeval_subtract(&tv_diff, &tv_cur, &(connection->time_begin)); - diff_us = ((float) tv_diff.tv_sec * 10e5) + tv_diff.tv_usec; + { + connection->rate_bps = 0; + pthread_mutex_unlock(&connection->access_mutex); + return; + } + else + { + gettimeofday(&tv_cur, NULL); + proz_timeval_subtract(&tv_diff, &tv_cur, &(connection->time_begin)); + diff_us = ((float)tv_diff.tv_sec * 10e5) + tv_diff.tv_usec; /* if (diff_us == 0) */ /* { */ @@ -552,21 +548,21 @@ void connection_calc_ratebps(connection_t * connection) /* return; */ /* } */ /* connection->rate_bps = */ -/* ((float) (connection->remote_bytes_received */ -/* - (connection->remote_startpos-connection->orig_remote_startpos)) * 10e5 / diff_us); */ +/* ((float) (connection->remote_bytes_received */ +/* - (connection->remote_startpos-connection->orig_remote_startpos)) * 10e5 / diff_us); */ /* } */ - if (diff_us <100000) - { - connection->rate_bps = 0; - pthread_mutex_unlock(&connection->access_mutex); - return; + if (diff_us < 100000) + { + connection->rate_bps = 0; + pthread_mutex_unlock(&connection->access_mutex); + return; + } + else + connection->rate_bps = + ((float)(connection->remote_bytes_received + - (connection->remote_startpos - connection->orig_remote_startpos)) * 10e5 / diff_us); } - else - connection->rate_bps = - ((float) (connection->remote_bytes_received - - (connection->remote_startpos-connection->orig_remote_startpos)) * 10e5 / diff_us); - } pthread_mutex_unlock(&connection->access_mutex); return; @@ -575,7 +571,6 @@ void connection_calc_ratebps(connection_t * connection) void connection_throttle_bps(connection_t * connection) { - struct timeval tv_cur; struct timeval tv_diff; float diff_us; @@ -586,93 +581,94 @@ void connection_throttle_bps(connection_t * connection) pthread_mutex_lock(&connection->access_mutex); con_timeout_usecs = - (connection->conn_timeout.tv_sec * 10e5) + - connection->conn_timeout.tv_usec; + (connection->conn_timeout.tv_sec * 10e5) + + connection->conn_timeout.tv_usec; if (connection->rate_bps == 0 || connection->max_allowed_bps == 0) - { - pthread_mutex_unlock(&connection->access_mutex); - return; - } + { + pthread_mutex_unlock(&connection->access_mutex); + return; + } if (connection->time_begin.tv_sec == 0 && connection->time_begin.tv_usec == 0) - { - pthread_mutex_unlock(&connection->access_mutex); - return; - } + { + pthread_mutex_unlock(&connection->access_mutex); + return; + } gettimeofday(&tv_cur, NULL); proz_timeval_subtract(&tv_diff, &tv_cur, &(connection->time_begin)); - diff_us = ((float) tv_diff.tv_sec * 10e5) + tv_diff.tv_usec; + diff_us = ((float)tv_diff.tv_sec * 10e5) + tv_diff.tv_usec; if (diff_us == 0) - { - pthread_mutex_unlock(&connection->access_mutex); - return; - } + { + pthread_mutex_unlock(&connection->access_mutex); + return; + } wtime = - 10e5 * (connection->remote_bytes_received - - (connection->remote_startpos-connection->orig_remote_startpos)) / - connection->max_allowed_bps; + 10e5 * (connection->remote_bytes_received + - (connection->remote_startpos - connection->orig_remote_startpos)) / + connection->max_allowed_bps; pthread_mutex_unlock(&connection->access_mutex); memset(&tv_delay, 0, sizeof(tv_delay)); if (wtime > diff_us) - { - /*too fast have to delay */ - // proz_debug("wtime %f, diff_us %f", wtime, diff_us); - if ((wtime - diff_us) > con_timeout_usecs) /* problem here */ { - /*If we were to delay for wtime-diff_us we would cause a connection - timeout, so rather than doing that shall we delay for a bit lesser - than the time for the timeout, like say 1 second less - */ - const int limit_time_us = 2 * 10e5; + /*too fast have to delay */ + // proz_debug("wtime %f, diff_us %f", wtime, diff_us); + if ((wtime - diff_us) > con_timeout_usecs) /* problem here */ + { + /*If we were to delay for wtime-diff_us we would cause a connection + timeout, so rather than doing that shall we delay for a bit lesser + than the time for the timeout, like say 1 second less + */ + const int limit_time_us = 2 * 10e5; - /* Will the connection timeout - limit_time_us be less or equal to 0? - If so no point in delaing beacuse the connection wold timeout - */ + /* Will the connection timeout - limit_time_us be less or equal to 0? + If so no point in delaing beacuse the connection wold timeout + */ - if ((con_timeout_usecs - limit_time_us) <= 0) - { - proz_debug - ("Cant throttle: Connection would timeout if done so, please try increasing the timeout value"); - return; - } + if ((con_timeout_usecs - limit_time_us) <= 0) + { + proz_debug + ("Cant throttle: Connection would timeout if done so, please try increasing the timeout value"); + return; + } - tv_delay.tv_usec = con_timeout_usecs - limit_time_us; - /* message - ("Cant throttle fully : Connection would timeout if done so, please try increasing the timeout value"); */ + tv_delay.tv_usec = con_timeout_usecs - limit_time_us; + /* message + ("Cant throttle fully : Connection would timeout if done so, please try increasing the timeout value"); */ - proz_debug("delaymaxlimit %ld sec\n", tv_delay.tv_usec); - } else - { - tv_delay.tv_usec = (wtime - diff_us); + proz_debug("delaymaxlimit %ld sec\n", tv_delay.tv_usec); + } + else + { + tv_delay.tv_usec = (wtime - diff_us); //#warning "comment out the following line before releasing the code base" - proz_debug("sleeping %f secs\n", (wtime - diff_us) / 10e5); + proz_debug("sleeping %f secs\n", (wtime - diff_us) / 10e5); + } + + tv_delay.tv_sec = tv_delay.tv_usec / 1000000; + tv_delay.tv_usec = tv_delay.tv_usec % 1000000; + + if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv_delay) < 0) + { + proz_debug("Unable to throttle Bandwith\n"); + } } - - tv_delay.tv_sec = tv_delay.tv_usec / 1000000; - tv_delay.tv_usec = tv_delay.tv_usec % 1000000; - - if (select(0, (fd_set *) 0, (fd_set *) 0, (fd_set *) 0, &tv_delay) < 0) - { - proz_debug("Unable to throttle Bandwith\n"); - } - - } } boolean proz_connection_running(connection_t * connection) { boolean running; + pthread_mutex_lock(&connection->access_mutex); running = connection->running; pthread_mutex_unlock(&connection->access_mutex); @@ -682,13 +678,13 @@ boolean proz_connection_running(connection_t * connection) void proz_connection_set_url(connection_t * connection, urlinfo *url) { assert(url); - memcpy(&connection->u, - proz_copy_url(url), - sizeof(urlinfo)); + memcpy(&connection->u, + proz_copy_url(url), + sizeof(urlinfo)); } void proz_connection_free_connection(connection_t * connection, - boolean complete) + boolean complete) { assert(connection); /*TODO what about szBuffer..also have to free the URL u */ @@ -707,9 +703,9 @@ void proz_connection_free_connection(connection_t * connection, /* free the serv_ret_lines array */ if (connection->serv_ret_lines != 0) - { - done_with_response(connection); - } + { + done_with_response(connection); + } if (complete == TRUE) kfree(connection); @@ -719,11 +715,12 @@ void proz_connection_free_connection(connection_t * connection, size_t write_data_with_lock(connection_t * connection, const void *ptr, size_t size, size_t nmemb) { size_t ret; + flockfile(connection->fp); /*Seek appropriately......*/ - ret=fseeko(connection->fp, connection->local_startpos+connection->remote_bytes_received, SEEK_SET); - ret=fwrite( ptr, size, nmemb, connection->fp); + ret = fseeko(connection->fp, connection->local_startpos + connection->remote_bytes_received, SEEK_SET); + ret = fwrite(ptr, size, nmemb, connection->fp); funlockfile(connection->fp); return ret; diff --git a/libprozilla/src/connection.h b/libprozilla/src/connection.h index 249431a..f95f368 100644 --- a/libprozilla/src/connection.h +++ b/libprozilla/src/connection.h @@ -1,25 +1,25 @@ /****************************************************************************** - libprozilla - a download accelerator library - Copyright (C) 2001 Kalum Somaratna + libprozilla - a download accelerator library + Copyright (C) 2001 Kalum Somaratna - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ******************************************************************************/ /* Several connection-related routines. */ -/* $Id: connection.h,v 1.34 2005/01/11 01:49:11 sean Exp $ */ +/* $Id$ */ #ifndef CONNECTION_H @@ -35,29 +35,29 @@ extern "C" { #endif - void init_response(connection_t * connection); - void done_with_response(connection_t * connection); - void connection_change_status(connection_t * connection, - dl_status status); +void init_response(connection_t * connection); +void done_with_response(connection_t * connection); +void connection_change_status(connection_t * connection, + dl_status status); - uerr_t connection_retr_fsize_not_known(connection_t * connection, - char *read_buffer, - int read_buffer_size); +uerr_t connection_retr_fsize_not_known(connection_t * connection, + char *read_buffer, + int read_buffer_size); - uerr_t connection_retr_fsize_known(connection_t * connection, - char *read_buffer, - int read_buffer_size); - int connection_load_resume_info(connection_t * connection); +uerr_t connection_retr_fsize_known(connection_t * connection, + char *read_buffer, + int read_buffer_size); +int connection_load_resume_info(connection_t * connection); - void connection_show_message(connection_t * connection, - const char *format, ...); - void connection_calc_ratebps(connection_t * connection); - void connection_throttle_bps(connection_t * connection); - void get_url_info_loop(connection_t * connection); - size_t write_data_with_lock(connection_t *connection, const void *ptr, size_t size, size_t nmemb); +void connection_show_message(connection_t * connection, + const char *format, ...); +void connection_calc_ratebps(connection_t * connection); +void connection_throttle_bps(connection_t * connection); +void get_url_info_loop(connection_t * connection); +size_t write_data_with_lock(connection_t *connection, const void *ptr, size_t size, size_t nmemb); #ifdef __cplusplus } #endif -#endif /* CONNECTION_H */ +#endif /* CONNECTION_H */ diff --git a/libprozilla/src/debug.c b/libprozilla/src/debug.c index faa152b..9d18752 100644 --- a/libprozilla/src/debug.c +++ b/libprozilla/src/debug.c @@ -1,25 +1,25 @@ /****************************************************************************** - libprozilla - a download accelerator library - Copyright (C) 2001 Kalum Somaratna + libprozilla - a download accelerator library + Copyright (C) 2001 Kalum Somaratna - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ******************************************************************************/ /* Debugging routines. */ -/* $Id: debug.c,v 1.20 2001/08/17 21:53:39 kalum Exp $ */ +/* $Id$ */ #include "common.h" @@ -32,7 +32,7 @@ static pthread_mutex_t debug_mutex = PTHREAD_MUTEX_INITIALIZER; /****************************************************************************** -Initialises the debug system, deletes any prior debug.log file if present + Initialises the debug system, deletes any prior debug.log file if present ******************************************************************************/ void debug_init() { @@ -43,28 +43,28 @@ void debug_init() void proz_debug_delete_log() { - char logfile_name[PATH_MAX]; + snprintf(logfile_name, PATH_MAX, "%s/debug.log", libprozrtinfo.log_dir); if (unlink(logfile_name) == -1) - { - /* - * if the file is not present the continue silently - */ - if (errno == ENOENT) - return; - else { - proz_debug(_("unable to delete the file %s. Reason-: %s"), - strerror(errno)); + /* + * if the file is not present the continue silently + */ + if (errno == ENOENT) + return; + else + { + proz_debug(_("unable to delete the file %s. Reason-: %s"), + strerror(errno)); + } } - } } /****************************************************************************** - Write a message to the debug-file. + Write a message to the debug-file. ******************************************************************************/ void proz_debug(const char *format, ...) { @@ -73,41 +73,42 @@ void proz_debug(const char *format, ...) char message[MAX_MSG_SIZE + 1 + 1]; char logfile_name[PATH_MAX]; + snprintf(logfile_name, PATH_MAX, "%s/debug.log", libprozrtinfo.log_dir); pthread_mutex_lock(&debug_mutex); if (libprozrtinfo.debug_mode == TRUE) - { - va_start(args, format); - vsnprintf((char *) &message, MAX_MSG_SIZE, format, args); - va_end(args); - - /* Remove all newlines from the end of the string. */ - while ((message[strlen(message) - 1] == '\r') - || (message[strlen(message) - 1] == '\n')) - message[strlen(message) - 1] = '\0'; - - /* Append a newline. */ - message[strlen(message) + 1] = '\0'; - message[strlen(message)] = '\n'; - - if (!(fp = fopen(logfile_name, "at"))) { - pthread_mutex_unlock(&debug_mutex); - return; - } + va_start(args, format); + vsnprintf((char *)&message, MAX_MSG_SIZE, format, args); + va_end(args); + + /* Remove all newlines from the end of the string. */ + while ((message[strlen(message) - 1] == '\r') + || (message[strlen(message) - 1] == '\n')) + message[strlen(message) - 1] = '\0'; + + /* Append a newline. */ + message[strlen(message) + 1] = '\0'; + message[strlen(message)] = '\n'; + + if (!(fp = fopen(logfile_name, "at"))) + { + pthread_mutex_unlock(&debug_mutex); + return; + } + + if (fwrite(message, 1, strlen(message), fp) != strlen(message)) + { + pthread_mutex_unlock(&debug_mutex); + fclose(fp); + return; + } - if (fwrite(message, 1, strlen(message), fp) != strlen(message)) - { - pthread_mutex_unlock(&debug_mutex); fclose(fp); - return; } - fclose(fp); - } - pthread_mutex_unlock(&debug_mutex); } diff --git a/libprozilla/src/debug.h b/libprozilla/src/debug.h index 7dcbb2a..5b42fce 100644 --- a/libprozilla/src/debug.h +++ b/libprozilla/src/debug.h @@ -1,25 +1,25 @@ /****************************************************************************** - libprozilla - a download accelerator library - Copyright (C) 2001 Kalum Somaratna + libprozilla - a download accelerator library + Copyright (C) 2001 Kalum Somaratna - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ******************************************************************************/ /* Debugging routines. */ -/* $Id: debug.h,v 1.14 2001/07/11 23:19:41 kalum Exp $ */ +/* $Id$ */ #ifndef DEBUG_H @@ -30,4 +30,4 @@ void debug_init(); -#endif /* DEBUG_H */ +#endif /* DEBUG_H */ diff --git a/libprozilla/src/download.c b/libprozilla/src/download.c index d3e0294..2dbb815 100644 --- a/libprozilla/src/download.c +++ b/libprozilla/src/download.c @@ -1,25 +1,25 @@ /****************************************************************************** - libprozilla - a download accelerator library - Copyright (C) 2001 Kalum Somaratna + libprozilla - a download accelerator library + Copyright (C) 2001 Kalum Somaratna - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ******************************************************************************/ /* Download routines. */ -/* $Id: download.c,v 1.42 2005/09/19 16:18:02 kalum Exp $ */ +/* $Id$ */ #include "common.h" @@ -32,11 +32,10 @@ /****************************************************************************** - Initialize the download. + Initialize the download. ******************************************************************************/ download_t *proz_download_init(urlinfo * u) { - /* pthread_mutexattr_t attr; */ download_t *download = kmalloc(sizeof(download_t)); @@ -61,19 +60,19 @@ download_t *proz_download_init(urlinfo * u) download->max_simul_connections = 0; download->max_allowed_bps = libprozrtinfo.max_bps_per_dl; - download->file_build_msg = (char *) kmalloc(MAX_MSG_SIZE + 1); + download->file_build_msg = (char *)kmalloc(MAX_MSG_SIZE + 1); download->using_ftpsearch = FALSE; return download; } /****************************************************************************** - This will setup a download based on the connection info, and will attempt - to setup req_connections, but it might not be possible if we dont know the - file size, returns the number of connections allocated. + This will setup a download based on the connection info, and will attempt + to setup req_connections, but it might not be possible if we dont know the + file size, returns the number of connections allocated. ******************************************************************************/ int proz_download_setup_connections_no_ftpsearch(download_t * download, - connection_t * connection, - int req_connections) + connection_t * connection, + int req_connections) { int num_connections, i; off_t bytes_per_connection; @@ -90,140 +89,138 @@ int proz_download_setup_connections_no_ftpsearch(download_t * download, num_connections = 1; bytes_per_connection = -1; bytes_left = -1; + } + else + { + if (connection->resume_support == FALSE) + num_connections = 1; + else + num_connections = req_connections; - } else - { - if (connection->resume_support == FALSE) - num_connections = 1; - else - num_connections = req_connections; - - bytes_per_connection = connection->main_file_size / num_connections; - bytes_left = connection->main_file_size % num_connections; - } + bytes_per_connection = connection->main_file_size / num_connections; + bytes_left = connection->main_file_size % num_connections; + } download->pconnections = kmalloc(sizeof(connection_t *) * num_connections); download->num_connections = num_connections; - out_file=kmalloc(PATH_MAX); + out_file = kmalloc(PATH_MAX); snprintf(out_file, PATH_MAX, "%s/%s.prozilla", - download->dl_dir, connection->u.file); + download->dl_dir, connection->u.file); + + proz_debug("out file %s", out_file); - proz_debug("out file %s",out_file); - //First see if the file exists then we dont create a new one else we do if (stat(out_file, &stat_buf) == -1) { - proz_debug("stat failed"); + proz_debug("stat failed"); /* the call failed */ - /* if the error is due to the file not been present then there is no - need to do anything..just continue, otherwise return error (-1) - */ + /* if the error is due to the file not been present then there is no + need to do anything..just continue, otherwise return error (-1) + */ if (errno == ENOENT) - { - proz_debug("file doesnt exist"); - //File not exists so create it - if (! - (fp = - fopen(out_file, "w+"))) - { - download_show_message(download, - _ - ("Unable to open file %s: %s!"), - out_file, strerror(errno)); - proz_debug("Unable to open file %s: %s!", - out_file, strerror(errno)); - return -1; - } - proz_debug("created file"); - - } + { + proz_debug("file doesnt exist"); + //File not exists so create it + if (! + (fp = + fopen(out_file, "w+"))) + { + download_show_message(download, + _ + ("Unable to open file %s: %s!"), + out_file, strerror(errno)); + proz_debug("Unable to open file %s: %s!", + out_file, strerror(errno)); + return -1; + } + proz_debug("created file"); + } else - { - proz_debug("something else happened %d", errno); - return -1; - } + { + proz_debug("something else happened %d", errno); + return -1; + } } else { - proz_debug("stat success"); + proz_debug("stat success"); //TODO: File exists : if it doesnt match file size warna boput it... if (! - (fp = - fopen(out_file, "r+"))) - { - download_show_message(download, - _ - ("Unable to open file %s: %s!"), - out_file, strerror(errno)); - proz_debug("Unable to open file %s: %s!", - out_file, strerror(errno)); - return -1; - } - + (fp = + fopen(out_file, "r+"))) + { + download_show_message(download, + _ + ("Unable to open file %s: %s!"), + out_file, strerror(errno)); + proz_debug("Unable to open file %s: %s!", + out_file, strerror(errno)); + return -1; + } } //TRY setting the offset; if (download->main_file_size != -1) { - - if(fseeko(fp, download->main_file_size, SEEK_SET)!=0) - { - proz_debug("fseek failed"); - return -1; - } + if (fseeko(fp, download->main_file_size, SEEK_SET) != 0) + { + proz_debug("fseek failed"); + return -1; + } } /*Make sure all writes go directly to the file */ - setvbuf(fp, NULL, _IONBF, 0); + setvbuf(fp, NULL, _IONBF, 0); for (i = 0; i < num_connections; i++) { - download->pconnections[i]=proz_connection_init(&download->u, - &download->status_change_mutex); + download->pconnections[i] = proz_connection_init(&download->u, + &download->status_change_mutex); /*Copy somethings we need from the original connection */ download->resume_support = download->pconnections[i]->resume_support = - connection->resume_support; + connection->resume_support; memcpy(&download->pconnections[i]->hs, &connection->hs, - sizeof(http_stat_t)); + sizeof(http_stat_t)); download->pconnections[i]->localfile = kmalloc(PATH_MAX); strcpy(out_file, download->pconnections[i]->localfile); - - download->pconnections[i]->fp=fp; + + download->pconnections[i]->fp = fp; download->pconnections[i]->retry = TRUE; if (connection->main_file_size == -1) - { - download->pconnections[i]->main_file_size = -1; - download->pconnections[i]->remote_startpos = 0; - download->pconnections[i]->orig_remote_startpos = 0; - download->pconnections[i]->remote_endpos = -1; + { + download->pconnections[i]->main_file_size = -1; + download->pconnections[i]->remote_startpos = 0; + download->pconnections[i]->orig_remote_startpos = 0; + download->pconnections[i]->remote_endpos = -1; - download->pconnections[i]->local_startpos = 0; - } else - { - download->pconnections[i]->main_file_size = connection->main_file_size; - download->pconnections[i]->orig_remote_startpos = download->pconnections[i]->remote_startpos = i * bytes_per_connection; - download->pconnections[i]->remote_endpos = - i * bytes_per_connection + bytes_per_connection; + download->pconnections[i]->local_startpos = 0; + } + else + { + download->pconnections[i]->main_file_size = connection->main_file_size; + download->pconnections[i]->orig_remote_startpos = download->pconnections[i]->remote_startpos = i * bytes_per_connection; + download->pconnections[i]->remote_endpos = + i * bytes_per_connection + bytes_per_connection; - //Changing things here..... - download->pconnections[i]->local_startpos = download->pconnections[i]->remote_startpos; - } + //Changing things here..... + download->pconnections[i]->local_startpos = download->pconnections[i]->remote_startpos; + } /*Set the connections message to be download->msg_proc calback */ proz_connection_set_msg_proc(download->pconnections[i], - download->msg_proc, download->cb_data); + download->msg_proc, download->cb_data); } /* Add the remaining bytes to the last connection */ @@ -232,12 +229,12 @@ int proz_download_setup_connections_no_ftpsearch(download_t * download, download->using_ftpsearch = FALSE; - /*NOTE: Should we check for previously started downloads here and adjust - the local_startpos, accordingly or check for resumes later in another function - which is called after this? - */ + /*NOTE: Should we check for previously started downloads here and adjust + the local_startpos, accordingly or check for resumes later in another function + which is called after this? + */ - proz_debug("return num_connections %d",num_connections); + proz_debug("return num_connections %d", num_connections); return num_connections; } @@ -248,93 +245,91 @@ int proz_download_load_resume_info(download_t * download) int i; int ret = 1; logfile lf; - if(proz_log_read_logfile(&lf, download, TRUE)==1) - proz_debug("sucessfully loaded resume info"); + + if (proz_log_read_logfile(&lf, download, TRUE) == 1) + proz_debug("sucessfully loaded resume info"); for (i = 0; i < download->num_connections; i++) - { + { + if (download->pconnections[i]->remote_endpos - download->pconnections[i]->remote_startpos == download->pconnections[i]->remote_bytes_received) + { + connection_change_status(download->pconnections[i], COMPLETED); + //This should fix the error we received when resuming when the + //average rate was too high. + download->pconnections[i]->remote_startpos += download->pconnections[i]->remote_bytes_received; + continue; + } - if(download->pconnections[i]->remote_endpos - download->pconnections[i]->remote_startpos == download->pconnections[i]->remote_bytes_received) - { - - connection_change_status(download->pconnections[i], COMPLETED); - //This should fix the error we received when resuming when the - //average rate was too high. - download->pconnections[i]->remote_startpos +=download->pconnections[i]->remote_bytes_received; - continue; - } - - download->pconnections[i]->remote_startpos +=download->pconnections[i]->remote_bytes_received; - - - } + download->pconnections[i]->remote_startpos += download->pconnections[i]->remote_bytes_received; + } download->resume_mode = TRUE; return ret; } -/* This will create the threads and start the downloads, +/* This will create the threads and start the downloads, if resume is true it will load the resume info too if the download supports it -*/ + */ void proz_download_start_downloads(download_t * download, - boolean resume_mode) + boolean resume_mode) { - int i; - + if (resume_mode) - { - /*Does this download suport resume? */ - if (download->resume_support == TRUE) - proz_download_load_resume_info(download); - } -else - { - /*Create the log file */ - if (log_create_logfile - (download->num_connections, download->main_file_size, - download->u.url, download) != 1) - { - download_show_message(download, - _("Warning! Unable to create logfile!")); - } -} + { + /*Does this download suport resume? */ + if (download->resume_support == TRUE) + proz_download_load_resume_info(download); + } + else + { + /*Create the log file */ + if (log_create_logfile + (download->num_connections, download->main_file_size, + download->u.url, download) != 1) + { + download_show_message(download, + _("Warning! Unable to create logfile!")); + } + } /* Allocate number of threads */ download->threads = - (pthread_t *) kmalloc(sizeof(pthread_t) * download->num_connections); + (pthread_t *)kmalloc(sizeof(pthread_t) * download->num_connections); /*Create them */ for (i = 0; i < download->num_connections; i++) - { - switch (download->pconnections[i]->u.proto) { - case URLHTTP: - /* http_loop(&download->connections[i]); */ - if (pthread_create(&download->threads[i], NULL, - (void *) &http_loop, - (void *) (download->pconnections[i])) != 0) + switch (download->pconnections[i]->u.proto) + { + case URLHTTP: + /* http_loop(&download->connections[i]); */ + if (pthread_create(&download->threads[i], NULL, + (void *)&http_loop, + (void *)(download->pconnections[i])) != 0) - proz_die(_("Error: Not enough system resources")); + proz_die(_("Error: Not enough system resources")); - break; - case URLFTP: + break; - /* ftp_loop(&download->connections[i]); */ + case URLFTP: + + /* ftp_loop(&download->connections[i]); */ - if (pthread_create(&download->threads[i], NULL, - (void *) &ftp_loop, - (void *) (download->pconnections[i])) != 0) - proz_die(_("Error: Not enough system resources")); + if (pthread_create(&download->threads[i], NULL, + (void *)&ftp_loop, + (void *)(download->pconnections[i])) != 0) + proz_die(_("Error: Not enough system resources")); - break; - default: - proz_die(_("Error: Unsupported Protocol was specified")); + break; + + default: + proz_die(_("Error: Unsupported Protocol was specified")); + } } - } download_show_message(download, _("All threads created")); } @@ -342,22 +337,21 @@ else void proz_download_stop_downloads(download_t * download) { int i; + /*Stop the threads */ for (i = 0; i < download->num_connections; i++) - { - pthread_cancel(download->threads[i]); - pthread_join(download->threads[i], NULL); - } + { + pthread_cancel(download->threads[i]); + pthread_join(download->threads[i], NULL); + } } /* returns one of DLINPROGRESS, DLERR, DLDONE, DLREMOTEFATAL, DLLOCALFATAL*/ uerr_t proz_download_handle_threads(download_t * download) { - - //Create logfile everytime this is callaed - log_create_logfile(download->num_connections, download->main_file_size,download->u.url, download); + log_create_logfile(download->num_connections, download->main_file_size, download->u.url, download); if (download->using_ftpsearch == TRUE) return download_handle_threads_ftpsearch(download); @@ -375,11 +369,11 @@ uerr_t download_handle_threads_no_ftpsearch(download_t * download) /*Set the DL start time if it is not done so */ pthread_mutex_lock(download->pconnections[i]->status_change_mutex); if (download->pconnections[i]->status == DOWNLOADING - && download->start_time.tv_sec == 0 - && download->start_time.tv_usec == 0) - { - gettimeofday(&download->start_time, NULL); - } + && download->start_time.tv_sec == 0 + && download->start_time.tv_usec == 0) + { + gettimeofday(&download->start_time, NULL); + } pthread_mutex_unlock(download->pconnections[i]->status_change_mutex); } @@ -390,8 +384,8 @@ uerr_t download_handle_threads_no_ftpsearch(download_t * download) char * out_filename; char * orig_filename; download_show_message(download, - "All the conenctions have retreived the file" - "..waiting for them to end"); + "All the conenctions have retreived the file" + "..waiting for them to end"); proz_download_wait_till_all_end(download); download_show_message(download, "All the threads have being ended."); @@ -400,22 +394,22 @@ uerr_t download_handle_threads_no_ftpsearch(download_t * download) fclose(download->pconnections[0]->fp); // funlockfile(download->pconnections[0]->fp); - out_filename=kmalloc(PATH_MAX); - orig_filename=kmalloc(PATH_MAX); + out_filename = kmalloc(PATH_MAX); + orig_filename = kmalloc(PATH_MAX); snprintf(orig_filename, PATH_MAX, "%s/%s", - download->dl_dir, download->pconnections[0]->u.file); + download->dl_dir, download->pconnections[0]->u.file); snprintf(out_filename, PATH_MAX, "%s/%s.prozilla", - download->dl_dir, download->pconnections[0]->u.file); - if(rename(out_filename, orig_filename)==-1) - { - download_show_message(download, "Error While attempting to rename the file: %s", strerror(errno)); - } + download->dl_dir, download->pconnections[0]->u.file); + if (rename(out_filename, orig_filename) == -1) + { + download_show_message(download, "Error While attempting to rename the file: %s", strerror(errno)); + } download_show_message(download, "Successfully renamed file"); /*Delete the logfile as we dont need it now */ - if(proz_log_delete_logfile(download)!=1) - download_show_message(download, "Error: Unable to delete the logfile: %s", strerror(errno)); + if (proz_log_delete_logfile(download) != 1) + download_show_message(download, "Error: Unable to delete the logfile: %s", strerror(errno)); return DLDONE; } @@ -436,271 +430,272 @@ uerr_t download_handle_threads_no_ftpsearch(download_t * download) switch (status) - { - case MAXTRYS: - break; + { + case MAXTRYS: + break; - case REMOTEFATAL: - /* handle the CANTRESUME err code */ + case REMOTEFATAL: + /* handle the CANTRESUME err code */ - if (connection_err == CANTRESUME) - { - /*Terminate the connections */ - proz_download_stop_downloads(download); - /*FIXME Do we delete any downloaded portions here ? */ - return CANTRESUME; - } else /*Handle the file not being found on the server */ - if (connection_err == FTPNSFOD || connection_err == HTTPNSFOD) - { - if (proz_download_all_dls_filensfod(download) == TRUE) - { - download_show_message(download, - _ - ("The file was not found in all the connections!")); - /*Terminate the connections */ - proz_download_stop_downloads(download); - return DLREMOTEFATAL; - } else - { - download_show_message(download, _("Relaunching download")); - /* Make sure this thread has terminated */ - pthread_join(download->threads[i], NULL); + if (connection_err == CANTRESUME) + { + /*Terminate the connections */ + proz_download_stop_downloads(download); + /*FIXME Do we delete any downloaded portions here ? */ + return CANTRESUME; + } + else /*Handle the file not being found on the server */ + if (connection_err == FTPNSFOD || connection_err == HTTPNSFOD) + { + if (proz_download_all_dls_filensfod(download) == TRUE) + { + download_show_message(download, + _ + ("The file was not found in all the connections!")); + /*Terminate the connections */ + proz_download_stop_downloads(download); + return DLREMOTEFATAL; + } + else + { + download_show_message(download, _("Relaunching download")); + /* Make sure this thread has terminated */ + pthread_join(download->threads[i], NULL); - pthread_mutex_lock(&download->status_change_mutex); - if (pthread_create - (&download->threads[i], NULL, (void *) &ftp_loop, - (void *) (download->pconnections[i])) != 0) - proz_die(_("Error: Not enough system resources")); - pthread_cond_wait(&download->pconnections[i]->connecting_cond, - &download->status_change_mutex); - pthread_mutex_unlock(&download->status_change_mutex); - } - } else /*Handle the file not being found on the server */ - if (connection_err == FTPCWDFAIL) - { - if (proz_download_all_dls_ftpcwdfail(download) == TRUE) - { - download_show_message(download, - _ - ("Failed to change to the working directory on all the connections!")); - /*Terminate the connections */ - proz_download_stop_downloads(download); - return DLREMOTEFATAL; - } else - { - download_show_message(download, _("Relaunching download")); - /* Make sure this thread has terminated */ - pthread_join(download->threads[i], NULL); + pthread_mutex_lock(&download->status_change_mutex); + if (pthread_create + (&download->threads[i], NULL, (void *)&ftp_loop, + (void *)(download->pconnections[i])) != 0) + proz_die(_("Error: Not enough system resources")); + pthread_cond_wait(&download->pconnections[i]->connecting_cond, + &download->status_change_mutex); + pthread_mutex_unlock(&download->status_change_mutex); + } + } + else /*Handle the file not being found on the server */ + if (connection_err == FTPCWDFAIL) + { + if (proz_download_all_dls_ftpcwdfail(download) == TRUE) + { + download_show_message(download, + _ + ("Failed to change to the working directory on all the connections!")); + /*Terminate the connections */ + proz_download_stop_downloads(download); + return DLREMOTEFATAL; + } + else + { + download_show_message(download, _("Relaunching download")); + /* Make sure this thread has terminated */ + pthread_join(download->threads[i], NULL); - pthread_mutex_lock(&download->status_change_mutex); - if (pthread_create - (&download->threads[i], NULL, (void *) &ftp_loop, - (void *) (download->pconnections[i])) != 0) - proz_die(_("Error: Not enough system resources")); - pthread_cond_wait(&download->pconnections[i]->connecting_cond, &download->status_change_mutex); - pthread_mutex_unlock(&download->status_change_mutex); - } - } + pthread_mutex_lock(&download->status_change_mutex); + if (pthread_create + (&download->threads[i], NULL, (void *)&ftp_loop, + (void *)(download->pconnections[i])) != 0) + proz_die(_("Error: Not enough system resources")); + pthread_cond_wait(&download->pconnections[i]->connecting_cond, &download->status_change_mutex); + pthread_mutex_unlock(&download->status_change_mutex); + } + } - break; + break; - case LOCALFATAL: - proz_download_stop_downloads(download); - download_show_message(download, - _ - ("Connection %d, had a local fatal error: %s .Aborting download. "), - i, - proz_strerror(download->pconnections[i]->err)); - return DLLOCALFATAL; - break; + case LOCALFATAL: + proz_download_stop_downloads(download); + download_show_message(download, + _ + ("Connection %d, had a local fatal error: %s .Aborting download. "), + i, + proz_strerror(download->pconnections[i]->err)); + return DLLOCALFATAL; + break; - case LOGINFAIL: - /* - * First check if the ftp server did not allow any thread - * to login at all, then retry the curent thread - */ - if (proz_download_all_dls_status(download, LOGINFAIL) == TRUE) - { - download_show_message(download, - _ - ("All logins rejected! Retrying connection")); - /* Make sure this thread has terminated */ - pthread_join(download->threads[i], NULL); + case LOGINFAIL: + /* + * First check if the ftp server did not allow any thread + * to login at all, then retry the curent thread + */ + if (proz_download_all_dls_status(download, LOGINFAIL) == TRUE) + { + download_show_message(download, + _ + ("All logins rejected! Retrying connection")); + /* Make sure this thread has terminated */ + pthread_join(download->threads[i], NULL); - pthread_mutex_lock(&download->status_change_mutex); - if (pthread_create - (&download->threads[i], NULL, (void *) &ftp_loop, - (void *) (download->pconnections[i])) != 0) - proz_die(_("Error: Not enough system resources")); - pthread_cond_wait(&download->pconnections[i]->connecting_cond, - &download->status_change_mutex); - pthread_mutex_unlock(&download->status_change_mutex); - break; - } else - { + pthread_mutex_lock(&download->status_change_mutex); + if (pthread_create + (&download->threads[i], NULL, (void *)&ftp_loop, + (void *)(download->pconnections[i])) != 0) + proz_die(_("Error: Not enough system resources")); + pthread_cond_wait(&download->pconnections[i]->connecting_cond, + &download->status_change_mutex); + pthread_mutex_unlock(&download->status_change_mutex); + break; + } + else + { + /* + * Ok so at least there is one download whos login has not been rejected, + * so lets see if it has completed, if so we can relaunch this connection, + * as the commonest reason for a ftp login being rejected is because, the + * ftp server has a limit on the number of logins permitted from the same + * IP address. + */ - /* - * Ok so at least there is one download whos login has not been rejected, - * so lets see if it has completed, if so we can relaunch this connection, - * as the commonest reason for a ftp login being rejected is because, the - * ftp server has a limit on the number of logins permitted from the same - * IP address. - */ + /* + * Query the number of threads that are downloading + * if it is zero then relaunch this connection + */ + int dling_conns_count = + download_query_conns_status_count(download, DOWNLOADING, NULL); - /* - * Query the number of threads that are downloading - * if it is zero then relaunch this connection - */ - int dling_conns_count = - download_query_conns_status_count(download, DOWNLOADING, NULL); - - if (dling_conns_count > download->max_simul_connections) - { - download->max_simul_connections = dling_conns_count; - break; - } + if (dling_conns_count > download->max_simul_connections) + { + download->max_simul_connections = dling_conns_count; + break; + } - if (dling_conns_count == 0 - && - (download_query_conns_status_count(download, CONNECTING, NULL) - == 0) - && (download_query_conns_status_count(download, LOGGININ, NULL) - == 0)) - { + if (dling_conns_count == 0 + && + (download_query_conns_status_count(download, CONNECTING, NULL) + == 0) + && (download_query_conns_status_count(download, LOGGININ, NULL) + == 0)) + { + /* Make sure this thread has terminated */ + pthread_join(download->threads[i], NULL); + pthread_mutex_lock(&download->status_change_mutex); + download_show_message(download, _("Relaunching download")); + if (pthread_create(&download->threads[i], NULL, + (void *)&ftp_loop, + (void *)(download->pconnections[i])) != 0) + proz_die(_("Error: Not enough system resources")); + pthread_cond_wait(&download->pconnections[i]->connecting_cond, + &download->status_change_mutex); + pthread_mutex_unlock(&download->status_change_mutex); + } + else + if (dling_conns_count < download->max_simul_connections + && + (download_query_conns_status_count + (download, CONNECTING, NULL) == 0) + && + (download_query_conns_status_count + (download, LOGGININ, NULL) == 0)) + { + /* Make sure this thread has terminated */ + pthread_join(download->threads[i], NULL); + pthread_mutex_lock(&download->status_change_mutex); + download_show_message(download, _("Relaunching download")); + if (pthread_create(&download->threads[i], NULL, + (void *)&ftp_loop, + (void *)(download->pconnections[i])) != 0) + proz_die(_("Error: Not enough system resources")); + pthread_cond_wait(&download->pconnections[i]->connecting_cond, + &download->status_change_mutex); + pthread_mutex_unlock(&download->status_change_mutex); + } + } + break; - /* Make sure this thread has terminated */ - pthread_join(download->threads[i], NULL); - pthread_mutex_lock(&download->status_change_mutex); - download_show_message(download, _("Relaunching download")); - if (pthread_create(&download->threads[i], NULL, - (void *) &ftp_loop, - (void *) (download->pconnections[i])) != 0) - proz_die(_("Error: Not enough system resources")); - pthread_cond_wait(&download->pconnections[i]->connecting_cond, - &download->status_change_mutex); - pthread_mutex_unlock(&download->status_change_mutex); - } else - if (dling_conns_count < download->max_simul_connections - && - (download_query_conns_status_count - (download, CONNECTING, NULL) == 0) - && - (download_query_conns_status_count - (download, LOGGININ, NULL) == 0)) - { + case CONREJECT: + /* + * First check if the ftp server did not allow any thread + * to login at all, then retry the curent thread + */ + if (proz_download_all_dls_status(download, CONREJECT) == TRUE) + { + download_show_message(download, + _ + ("All connections attempts have been rejected! Retrying connection")); + /* Make sure this thread has terminated */ + pthread_join(download->threads[i], NULL); - /* Make sure this thread has terminated */ - pthread_join(download->threads[i], NULL); - pthread_mutex_lock(&download->status_change_mutex); - download_show_message(download, _("Relaunching download")); - if (pthread_create(&download->threads[i], NULL, - (void *) &ftp_loop, - (void *) (download->pconnections[i])) != 0) - proz_die(_("Error: Not enough system resources")); - pthread_cond_wait(&download->pconnections[i]->connecting_cond, - &download->status_change_mutex); - pthread_mutex_unlock(&download->status_change_mutex); - } + pthread_mutex_lock(&download->status_change_mutex); + if (pthread_create + (&download->threads[i], NULL, (void *)&ftp_loop, + (void *)(download->pconnections[i])) != 0) + proz_die(_("Error: Not enough system resources")); - } - break; + pthread_cond_wait(&download->pconnections[i]->connecting_cond, + &download->status_change_mutex); + pthread_mutex_unlock(&download->status_change_mutex); + break; + } + else + { + /* + * Ok so at least there is one download whos connections attempt has not been rejected, + * so lets see if it has completed, if so we can relaunch this connection, + * as the commonest reason for a ftp login being rejected is because, the + * ftp server has a limit on the number of logins permitted from the same + * IP address. + */ - case CONREJECT: - /* - * First check if the ftp server did not allow any thread - * to login at all, then retry the curent thread - */ - if (proz_download_all_dls_status(download, CONREJECT) == TRUE) - { - download_show_message(download, - _ - ("All connections attempts have been rejected! Retrying connection")); - /* Make sure this thread has terminated */ - pthread_join(download->threads[i], NULL); + /* + * Query the number of threads that are downloading + * if it is zero then relaunch this connection + */ + int dling_conns_count = + download_query_conns_status_count(download, DOWNLOADING, NULL); - pthread_mutex_lock(&download->status_change_mutex); - if (pthread_create - (&download->threads[i], NULL, (void *) &ftp_loop, - (void *) (download->pconnections[i])) != 0) - proz_die(_("Error: Not enough system resources")); - - pthread_cond_wait(&download->pconnections[i]->connecting_cond, - &download->status_change_mutex); - pthread_mutex_unlock(&download->status_change_mutex); - break; - } else - { - /* - * Ok so at least there is one download whos connections attempt has not been rejected, - * so lets see if it has completed, if so we can relaunch this connection, - * as the commonest reason for a ftp login being rejected is because, the - * ftp server has a limit on the number of logins permitted from the same - * IP address. - */ - - /* - * Query the number of threads that are downloading - * if it is zero then relaunch this connection - */ - int dling_conns_count = - download_query_conns_status_count(download, DOWNLOADING, NULL); - - if (dling_conns_count > download->max_simul_connections) - { - download->max_simul_connections = dling_conns_count; - break; - } + if (dling_conns_count > download->max_simul_connections) + { + download->max_simul_connections = dling_conns_count; + break; + } - if (dling_conns_count == 0 - && - (download_query_conns_status_count(download, CONNECTING, NULL) - == 0) - && (download_query_conns_status_count(download, LOGGININ, NULL) - == 0)) - { + if (dling_conns_count == 0 + && + (download_query_conns_status_count(download, CONNECTING, NULL) + == 0) + && (download_query_conns_status_count(download, LOGGININ, NULL) + == 0)) + { + /* Make sure this thread has terminated */ + pthread_join(download->threads[i], NULL); + pthread_mutex_lock(&download->status_change_mutex); + download_show_message(download, _("Relaunching download")); + if (pthread_create(&download->threads[i], NULL, + (void *)&ftp_loop, + (void *)(download->pconnections[i])) != 0) + proz_die(_("Error: Not enough system resources")); + pthread_cond_wait(&download->pconnections[i]->connecting_cond, + &download->status_change_mutex); + pthread_mutex_unlock(&download->status_change_mutex); + } + else + if (dling_conns_count < download->max_simul_connections + && + (download_query_conns_status_count + (download, CONNECTING, NULL) == 0) + && + (download_query_conns_status_count + (download, LOGGININ, NULL) == 0)) + { + /* Make sure this thread has terminated */ + pthread_join(download->threads[i], NULL); + pthread_mutex_lock(&download->status_change_mutex); + download_show_message(download, _("Relaunching download")); + if (pthread_create(&download->threads[i], NULL, + (void *)&ftp_loop, + (void *)(download->pconnections[i])) != 0) + proz_die(_("Error: Not enough system resources")); + pthread_cond_wait(&download->pconnections[i]->connecting_cond, + &download->status_change_mutex); + pthread_mutex_unlock(&download->status_change_mutex); + } + } + break; - /* Make sure this thread has terminated */ - pthread_join(download->threads[i], NULL); - pthread_mutex_lock(&download->status_change_mutex); - download_show_message(download, _("Relaunching download")); - if (pthread_create(&download->threads[i], NULL, - (void *) &ftp_loop, - (void *) (download->pconnections[i])) != 0) - proz_die(_("Error: Not enough system resources")); - pthread_cond_wait(&download->pconnections[i]->connecting_cond, - &download->status_change_mutex); - pthread_mutex_unlock(&download->status_change_mutex); - } else - if (dling_conns_count < download->max_simul_connections - && - (download_query_conns_status_count - (download, CONNECTING, NULL) == 0) - && - (download_query_conns_status_count - (download, LOGGININ, NULL) == 0)) - { - - /* Make sure this thread has terminated */ - pthread_join(download->threads[i], NULL); - pthread_mutex_lock(&download->status_change_mutex); - download_show_message(download, _("Relaunching download")); - if (pthread_create(&download->threads[i], NULL, - (void *) &ftp_loop, - (void *) (download->pconnections[i])) != 0) - proz_die(_("Error: Not enough system resources")); - pthread_cond_wait(&download->pconnections[i]->connecting_cond, - &download->status_change_mutex); - pthread_mutex_unlock(&download->status_change_mutex); - } - - } - break; - - default: - break; - } + default: + break; + } } /*bandwith throttling */ @@ -733,11 +728,10 @@ void download_show_message(download_t * download, const char *format, ...) /*This will return a pointer to the connection requested. */ connection_t *proz_download_get_connection(download_t * download, - int number) + int number) { assert(number >= 0 && number < download->num_connections); - return (download->pconnections[number]); - + return(download->pconnections[number]); } @@ -750,10 +744,10 @@ off_t proz_download_get_total_bytes_got(download_t * download) for (i = 0; i < download->num_connections; i++) - { - total_bytes_got += - proz_connection_get_total_bytes_got(download->pconnections[i]); - } + { + total_bytes_got += + proz_connection_get_total_bytes_got(download->pconnections[i]); + } return total_bytes_got; } @@ -761,7 +755,7 @@ off_t proz_download_get_total_bytes_got(download_t * download) /*Returns 1 if a previous download exits, 0 if not, and -1 on error */ int proz_download_prev_download_exists(download_t * download) { - /* Currently if a logfile exists it assumes that a previous uncompleted + /* Currently if a logfile exists it assumes that a previous uncompleted download exists */ @@ -776,60 +770,62 @@ float proz_download_get_average_speed(download_t * download) struct timeval cur_time; struct timeval diff_time; off_t total_remote_bytes_got = - proz_download_get_total_remote_bytes_got(download); + proz_download_get_total_remote_bytes_got(download); /*Has the download has been started.... */ if (download->start_time.tv_sec > 0 || download->start_time.tv_usec > 0) - { - gettimeofday(&cur_time, NULL); - /*Get the diff. */ - proz_timeval_subtract(&diff_time, &cur_time, &download->start_time); - - if (diff_time.tv_sec >= 1)// || diff_time.tv_usec > 0) { - speed = (float) total_remote_bytes_got / ((float) diff_time.tv_sec + - ((float) diff_time. - tv_usec / 10e5)); - } else - speed = 0; - } else - speed = 0; /*The DL hasnt started yet */ + gettimeofday(&cur_time, NULL); + /*Get the diff. */ + proz_timeval_subtract(&diff_time, &cur_time, &download->start_time); + + if (diff_time.tv_sec >= 1) // || diff_time.tv_usec > 0) + { + speed = (float)total_remote_bytes_got / ((float)diff_time.tv_sec + + ((float)diff_time. + tv_usec / 10e5)); + } + else + speed = 0; + } + else + speed = 0; /*The DL hasnt started yet */ return speed; - } -/* This can be called to erase the portions of the main file +/* This can be called to erase the portions of the main file that have been got. -*/ + */ int proz_download_delete_dl_file(download_t * download) { char *out_file; - out_file=kmalloc(PATH_MAX); + + out_file = kmalloc(PATH_MAX); snprintf(out_file, PATH_MAX, "%s/%s.prozilla", - download->dl_dir, download->u.file); + download->dl_dir, download->u.file); if (unlink(out_file) == -1) { /* - * if the file is not present no need for a error message ;-) + * if the file is not present no need for a error message ;-) */ if (errno == ENOENT) - { - return 1; - } + { + return 1; + } else - { - download_show_message(download, - _ - ("unable to delete the file %s. Reason-: %s"), - out_file, strerror(errno)); - return -1; - } + { + download_show_message(download, + _ + ("unable to delete the file %s. Reason-: %s"), + out_file, strerror(errno)); + return -1; + } } - + return 1; } @@ -838,12 +834,13 @@ int proz_download_delete_dl_file(download_t * download) void proz_download_wait_till_all_end(download_t * download) { int i; + /*Wait till the end of all the threads */ for (i = 0; i < download->num_connections; i++) - { - pthread_join(download->threads[i], NULL); - } + { + pthread_join(download->threads[i], NULL); + } } @@ -851,9 +848,9 @@ void proz_download_wait_till_all_end(download_t * download) void proz_download_join_downloads(download_t * download) { download->building = TRUE; - pthread_create(&download->join_thread, NULL, - (void *(*)(void *)) download_join_downloads, - (void *) download); + pthread_create(&download->join_thread, NULL, + (void *(*)(void *))download_join_downloads, + (void *)download); } @@ -868,8 +865,7 @@ void download_join_downloads(download_t * download) /*This function will join the downloaded files*/ void join_downloads(download_t * download) { - - pthread_mutex_lock(&download->access_mutex); + pthread_mutex_lock(&download->access_mutex); download->building = 0; pthread_mutex_unlock(&download->access_mutex); return; @@ -877,23 +873,22 @@ void join_downloads(download_t * download) -/***************************************************************************** -Returns the total number of bytes that has being got from the server -by the all the connections managed by this download -******************************************************************************/ +/***************************************************************************** + Returns the total number of bytes that has being got from the server + by the all the connections managed by this download + ******************************************************************************/ off_t proz_download_get_total_remote_bytes_got(download_t * download) { - off_t total_bytes_recv = 0; int i; for (i = 0; i < download->num_connections; i++) - { - proz_debug("DOWNLOAD_TOTAL_BYTES_RECV=%lld for connection %d", total_bytes_recv, i); - total_bytes_recv += - proz_connection_get_total_remote_bytes_got(download-> - pconnections[i]); - } + { + proz_debug("DOWNLOAD_TOTAL_BYTES_RECV=%lld for connection %d", total_bytes_recv, i); + total_bytes_recv += + proz_connection_get_total_remote_bytes_got(download-> + pconnections[i]); + } return total_bytes_recv; } @@ -903,15 +898,16 @@ off_t proz_download_get_total_remote_bytes_got(download_t * download) int proz_download_all_dls_status(download_t * download, dl_status status) { int i; + pthread_mutex_lock(&download->status_change_mutex); for (i = 0; i < download->num_connections; i++) - { - if (download->pconnections[i]->status != status) { - pthread_mutex_unlock(&download->status_change_mutex); - return FALSE; + if (download->pconnections[i]->status != status) + { + pthread_mutex_unlock(&download->status_change_mutex); + return FALSE; + } } - } pthread_mutex_unlock(&download->status_change_mutex); return TRUE; @@ -924,18 +920,19 @@ boolean proz_download_all_dls_filensfod(download_t * download) { int i; uerr_t err; + /*Lock mutex */ for (i = 0; i < download->num_connections; i++) - { - pthread_mutex_lock(&download->pconnections[i]->access_mutex); - err = download->pconnections[i]->err; - pthread_mutex_unlock(&download->pconnections[i]->access_mutex); - if (err != FTPNSFOD && err != HTTPNSFOD) { - return FALSE; + pthread_mutex_lock(&download->pconnections[i]->access_mutex); + err = download->pconnections[i]->err; + pthread_mutex_unlock(&download->pconnections[i]->access_mutex); + if (err != FTPNSFOD && err != HTTPNSFOD) + { + return FALSE; + } } - } return TRUE; @@ -946,18 +943,19 @@ boolean proz_download_all_dls_ftpcwdfail(download_t * download) { int i; uerr_t err; + /*Lock mutex */ for (i = 0; i < download->num_connections; i++) - { - pthread_mutex_lock(&download->pconnections[i]->access_mutex); - err = download->pconnections[i]->err; - pthread_mutex_unlock(&download->pconnections[i]->access_mutex); - if (err != FTPNSFOD && err != FTPCWDFAIL) { - return FALSE; + pthread_mutex_lock(&download->pconnections[i]->access_mutex); + err = download->pconnections[i]->err; + pthread_mutex_unlock(&download->pconnections[i]->access_mutex); + if (err != FTPNSFOD && err != FTPCWDFAIL) + { + return FALSE; + } } - } return TRUE; @@ -968,42 +966,44 @@ boolean proz_download_all_dls_ftpcwdfail(download_t * download) boolean proz_download_all_dls_err(download_t * download, uerr_t in_err) { - int i; + int i; uerr_t err; + /*Lock mutex */ for (i = 0; i < download->num_connections; i++) - { - pthread_mutex_lock(&download->pconnections[i]->access_mutex); - err = download->pconnections[i]->err; - pthread_mutex_unlock(&download->pconnections[i]->access_mutex); - if (err !=in_err) { - return FALSE; + pthread_mutex_lock(&download->pconnections[i]->access_mutex); + err = download->pconnections[i]->err; + pthread_mutex_unlock(&download->pconnections[i]->access_mutex); + if (err != in_err) + { + return FALSE; + } } - } return TRUE; } /* Returns the number of connections whose status is status ie (connecting to the server specified), - if server is NULL then it returns the total number of connections that are having the status which is equal the to the status specified */ + if server is NULL then it returns the total number of connections that are having the status which is equal the to the status specified */ int download_query_conns_status_count(download_t * download, - dl_status status, char *server) + dl_status status, char *server) { int i; int count = 0; + pthread_mutex_lock(&download->status_change_mutex); for (i = 0; i < download->num_connections; i++) - { - if (download->pconnections[i]->status == status) { - if (server == NULL - || (strcasecmp(server, download->pconnections[i]->u.host) == 0)) - count++; + if (download->pconnections[i]->status == status) + { + if (server == NULL + || (strcasecmp(server, download->pconnections[i]->u.host) == 0)) + count++; + } } - } pthread_mutex_unlock(&download->status_change_mutex); return count; @@ -1013,7 +1013,7 @@ int download_query_conns_status_count(download_t * download, void proz_download_set_msg_proc(download_t * download, - message_proc msg_proc, void *cb_data) + message_proc msg_proc, void *cb_data) { assert(download != NULL); @@ -1023,11 +1023,10 @@ void proz_download_set_msg_proc(download_t * download, } /*Returns the number of seconds left remaining in the download, -if it cannot be calculated say if the file size of not known, it returns -1 -*/ + if it cannot be calculated say if the file size of not known, it returns -1 + */ off_t proz_download_get_est_time_left(download_t * download) { - long secs_left; float average_speed; off_t total_bytes_got; @@ -1042,15 +1041,13 @@ off_t proz_download_get_est_time_left(download_t * download) return -1; return secs_left = - (off_t) ((download->main_file_size - - total_bytes_got) / average_speed); - + (off_t)((download->main_file_size - + total_bytes_got) / average_speed); } void proz_download_free_download(download_t * download, boolean complete) { - assert(download); /*TODO free the URL */ @@ -1070,14 +1067,14 @@ void proz_download_free_download(download_t * download, boolean complete) /*Now handle the freeing of the connections */ if (download->num_connections > 0 && download->pconnections) - { - int i; - for (i = 0; i < download->num_connections; i++) { - proz_connection_free_connection(download->pconnections[i], 0); + int i; + for (i = 0; i < download->num_connections; i++) + { + proz_connection_free_connection(download->pconnections[i], 0); + } + kfree(download->pconnections); } - kfree(download->pconnections); - } if (complete == TRUE) kfree(download); @@ -1092,7 +1089,7 @@ void download_calc_throttle_factor(download_t * download) long avg_rate; int num_dl_cons = - download_query_conns_status_count(download, DOWNLOADING, NULL); + download_query_conns_status_count(download, DOWNLOADING, NULL); if (num_dl_cons == 0) return; @@ -1100,29 +1097,29 @@ void download_calc_throttle_factor(download_t * download) avg_rate = download->max_allowed_bps / num_dl_cons; if (download->max_allowed_bps == 0) - { - for (i = 0; i < download->num_connections; i++) { - pthread_mutex_lock(&(download->pconnections[i]->access_mutex)); - download->pconnections[i]->max_allowed_bps = 0; - pthread_mutex_unlock(&(download->pconnections[i]->access_mutex)); + for (i = 0; i < download->num_connections; i++) + { + pthread_mutex_lock(&(download->pconnections[i]->access_mutex)); + download->pconnections[i]->max_allowed_bps = 0; + pthread_mutex_unlock(&(download->pconnections[i]->access_mutex)); + } + return; } - return; - } /*MAKE IR USE THE NUMBER OF ACTIVE DOWNLOAdING CONENCTIONS: Done */ for (i = 0; i < download->num_connections; i++) - { - pthread_mutex_lock(&(download->pconnections[i]->access_mutex)); - if ((proz_connection_get_status(download->pconnections[i]) == - DOWNLOADING) && download->pconnections[i]->rate_bps < avg_rate) { - t_slow_rates += download->pconnections[i]->rate_bps; - num_slow_cons++; + pthread_mutex_lock(&(download->pconnections[i]->access_mutex)); + if ((proz_connection_get_status(download->pconnections[i]) == + DOWNLOADING) && download->pconnections[i]->rate_bps < avg_rate) + { + t_slow_rates += download->pconnections[i]->rate_bps; + num_slow_cons++; + } + pthread_mutex_unlock(&(download->pconnections[i]->access_mutex)); } - pthread_mutex_unlock(&(download->pconnections[i]->access_mutex)); - } /*fixme mutex to preven this conenctions */ @@ -1133,20 +1130,20 @@ void download_calc_throttle_factor(download_t * download) /*If all the connections are slower then no need to do anything */ if (num_slow_cons == num_dl_cons) - { - for (i = 0; i < download->num_connections; i++) { - pthread_mutex_lock(&(download->pconnections[i]->access_mutex)); - download->pconnections[i]->max_allowed_bps = 0; - pthread_mutex_unlock(&(download->pconnections[i]->access_mutex)); + for (i = 0; i < download->num_connections; i++) + { + pthread_mutex_lock(&(download->pconnections[i]->access_mutex)); + download->pconnections[i]->max_allowed_bps = 0; + pthread_mutex_unlock(&(download->pconnections[i]->access_mutex)); + } + return; } - return; - } limit_high_cons_rate = - (download->max_allowed_bps - t_slow_rates) / (num_dl_cons - - num_slow_cons); + (download->max_allowed_bps - t_slow_rates) / (num_dl_cons - + num_slow_cons); /* proz_debug("total slow connections = %ld", num_slow_cons); @@ -1155,136 +1152,131 @@ void download_calc_throttle_factor(download_t * download) */ for (i = 0; i < download->num_connections; i++) - { - pthread_mutex_lock(&(download->pconnections[i]->access_mutex)); - if ((proz_connection_get_status(download->pconnections[i]) == - DOWNLOADING) && download->pconnections[i]->rate_bps >= avg_rate) { - download->pconnections[i]->max_allowed_bps = limit_high_cons_rate; + pthread_mutex_lock(&(download->pconnections[i]->access_mutex)); + if ((proz_connection_get_status(download->pconnections[i]) == + DOWNLOADING) && download->pconnections[i]->rate_bps >= avg_rate) + { + download->pconnections[i]->max_allowed_bps = limit_high_cons_rate; + } + pthread_mutex_unlock(&(download->pconnections[i]->access_mutex)); } - pthread_mutex_unlock(&(download->pconnections[i]->access_mutex)); - } } -/*This function will check if the target output file, -ie: the file that we are going to rebuild to exists. -Returns, - 1= file exists - 0 = file does not exists - -1 = error, ie cant have permissions to stat the file etc etc -*/ +/*This function will check if the target output file, + ie: the file that we are going to rebuild to exists. + Returns, + 1= file exists + 0 = file does not exists + -1 = error, ie cant have permissions to stat the file etc etc + */ int proz_download_target_exist(download_t * download) { - char out_file_name[PATH_MAX]; struct stat st_buf; int ret; snprintf(out_file_name, PATH_MAX, "%s/%s", download->output_dir, - download->u.file); + download->u.file); ret = stat(out_file_name, &st_buf); if (ret == -1) - { - if (errno == ENOENT) - return 0; - else - return -1; - } + { + if (errno == ENOENT) + return 0; + else + return -1; + } /*File was statable so it exists */ return 1; - } -/*This function will delete the target output file, -Returns, - 1= sucessfully delted the file - 0 = file does not exist - -1 = error, ie cant have permissions to delete the file etc etc -*/ +/*This function will delete the target output file, + Returns, + 1= sucessfully delted the file + 0 = file does not exist + -1 = error, ie cant have permissions to delete the file etc etc + */ int proz_download_delete_target(download_t * download) { - char out_file_name[PATH_MAX]; int ret; snprintf(out_file_name, PATH_MAX, "%s/%s", download->output_dir, - download->u.file); + download->u.file); ret = remove(out_file_name); if (ret == -1) - { - if (errno == ENOENT) - return 0; - else - return -1; - } + { + if (errno == ENOENT) + return 0; + else + return -1; + } /*File was statable so it exists */ return 1; - } /*Tries to switch to another server which is downloading or has completed, returns 1 on sucesss, or -1 on failure */ int download_switch_server_ftpsearch(download_t *download, int bad_connection) { - int j, usable_server; + int j, usable_server; - usable_server = -1; - /*Search for a server which is downloading or completed and try to switch to it -*/ + usable_server = -1; + /*Search for a server which is downloading or completed and try to switch to it + */ - pthread_mutex_lock(&download->status_change_mutex); - for (j = 0; j < download->num_connections; j++) - { - if (download->pconnections[j]->status == DOWNLOADING || - download->pconnections[j]->status == COMPLETED) - { - usable_server = j; - } - } - pthread_mutex_unlock(&download->status_change_mutex); + pthread_mutex_lock(&download->status_change_mutex); + for (j = 0; j < download->num_connections; j++) + { + if (download->pconnections[j]->status == DOWNLOADING || + download->pconnections[j]->status == COMPLETED) + { + usable_server = j; + } + } + pthread_mutex_unlock(&download->status_change_mutex); - if (usable_server != -1) - { - /*We have a server which is DLING */ + if (usable_server != -1) + { + /*We have a server which is DLING */ - /* Make sure this thread has terminated */ - pthread_join(download->threads[bad_connection], NULL); + /* Make sure this thread has terminated */ + pthread_join(download->threads[bad_connection], NULL); - /*copy url and relaunch */ - proz_free_url(&download->pconnections[bad_connection]->u, 0); - memcpy(&download->pconnections[bad_connection]->u, - proz_copy_url(&download-> - pconnections[usable_server]->u), - sizeof(urlinfo)); - proz_debug - ("Found server %s which is downloading will relaunch based on it", - download->pconnections[usable_server]->u.host); + /*copy url and relaunch */ + proz_free_url(&download->pconnections[bad_connection]->u, 0); + memcpy(&download->pconnections[bad_connection]->u, + proz_copy_url(&download-> + pconnections[usable_server]->u), + sizeof(urlinfo)); + proz_debug + ("Found server %s which is downloading will relaunch based on it", + download->pconnections[usable_server]->u.host); - pthread_mutex_lock(&download->status_change_mutex); - /*Relaunch thread */ - if (pthread_create - (&download->threads[bad_connection], NULL, (void *) &ftp_loop, - (void *) (download->pconnections[bad_connection])) != 0) - proz_die(_("Error: Not enough system resources")); - pthread_cond_wait(&download->pconnections[bad_connection]->connecting_cond, &download->status_change_mutex); - pthread_mutex_unlock(&download->status_change_mutex); - return 1; - - } else - { - /*Shit! no servers are downloading or completed so what shall we do? we shall do nothing and wait for one connectection at least to start downloading */ - return -1; - } - -} + pthread_mutex_lock(&download->status_change_mutex); + /*Relaunch thread */ + if (pthread_create + (&download->threads[bad_connection], NULL, (void *)&ftp_loop, + (void *)(download->pconnections[bad_connection])) != 0) + proz_die(_("Error: Not enough system resources")); + pthread_cond_wait(&download->pconnections[bad_connection]->connecting_cond, &download->status_change_mutex); + pthread_mutex_unlock(&download->status_change_mutex); + return 1; + } + else + { + /*Shit! no servers are downloading or completed so what shall we do? we shall do nothing and wait for one connectection at least to start downloading */ + return -1; + } +} /* returns one of DLINPROGRESS, DLERR, DLDONE, DLREMOTEFATAL, DLLOCALFATAL*/ uerr_t download_handle_threads_ftpsearch(download_t * download) @@ -1292,627 +1284,627 @@ uerr_t download_handle_threads_ftpsearch(download_t * download) int i; for (i = 0; i < download->num_connections; i++) - { - /*Set the DL start time if it is not done so */ - pthread_mutex_lock(download->pconnections[i]->status_change_mutex); - if (download->pconnections[i]->status == DOWNLOADING - && download->start_time.tv_sec == 0 - && download->start_time.tv_usec == 0) { - gettimeofday(&download->start_time, NULL); + /*Set the DL start time if it is not done so */ + pthread_mutex_lock(download->pconnections[i]->status_change_mutex); + if (download->pconnections[i]->status == DOWNLOADING + && download->start_time.tv_sec == 0 + && download->start_time.tv_usec == 0) + { + gettimeofday(&download->start_time, NULL); + } + pthread_mutex_unlock(download->pconnections[i]->status_change_mutex); } - pthread_mutex_unlock(download->pconnections[i]->status_change_mutex); - } /*If all the connections are completed then end them, and return complete */ if ((proz_download_all_dls_status(download, COMPLETED)) == TRUE) - { + { char * out_filename; char * orig_filename; - download_show_message(download, - "All the conenctions have retreived the file" - "..waiting for them to end"); - proz_download_wait_till_all_end(download); - download_show_message(download, "All the threads have being ended."); + download_show_message(download, + "All the conenctions have retreived the file" + "..waiting for them to end"); + proz_download_wait_till_all_end(download); + download_show_message(download, "All the threads have being ended."); /*Close and rename file to original */ flockfile(download->pconnections[0]->fp); fclose(download->pconnections[0]->fp); funlockfile(download->pconnections[0]->fp); - out_filename=kmalloc(PATH_MAX); - orig_filename=kmalloc(PATH_MAX); + out_filename = kmalloc(PATH_MAX); + orig_filename = kmalloc(PATH_MAX); snprintf(orig_filename, PATH_MAX, "%s/%s", - download->dl_dir, download->pconnections[0]->u.file); + download->dl_dir, download->pconnections[0]->u.file); snprintf(out_filename, PATH_MAX, "%s/%s.prozilla", - download->dl_dir, download->pconnections[0]->u.file); - if(rename(out_filename, orig_filename)==-1) - { - download_show_message(download, "Error While attempting to rename the file: %s", strerror(errno)); - } + download->dl_dir, download->pconnections[0]->u.file); + if (rename(out_filename, orig_filename) == -1) + { + download_show_message(download, "Error While attempting to rename the file: %s", strerror(errno)); + } download_show_message(download, "Successfully renamed file"); /*Delete the logfile as we dont need it now */ - if(proz_log_delete_logfile(download)!=1) - download_show_message(download, "Error: Unable to delete the logfile: %s", strerror(errno)); - return DLDONE; - } + if (proz_log_delete_logfile(download) != 1) + download_show_message(download, "Error: Unable to delete the logfile: %s", strerror(errno)); + return DLDONE; + } /*TODO handle restartable connections */ for (i = 0; i < download->num_connections; i++) - { - dl_status status; - uerr_t connection_err; - pthread_mutex_lock(download->pconnections[i]->status_change_mutex); - status = download->pconnections[i]->status; - pthread_mutex_unlock(download->pconnections[i]->status_change_mutex); - - pthread_mutex_lock(&download->pconnections[i]->access_mutex); - connection_err = download->pconnections[i]->err; - pthread_mutex_unlock(&download->pconnections[i]->access_mutex); - - - switch (status) { - case MAXTRYS: - break; - - case REMOTEFATAL: - /* handle the CANTRESUME err code */ - - if (connection_err == CANTRESUME) - { - /*Terminate the connections */ - proz_download_stop_downloads(download); - /*FIXME Do we delete any downloaded portions here ? */ - return CANTRESUME; - } else /*Handle the file not being found on the server */ - if (connection_err == FTPNSFOD || connection_err == HTTPNSFOD) - { - if (proz_download_all_dls_filensfod(download) == TRUE) - { - download_show_message(download, - _ - ("The file was not found in all the connections!")); - /*Terminate the connections */ - proz_download_stop_downloads(download); - return DLREMOTEFATAL; - } else - { - - /*Now we have to be careful */ - int server_pos, cur_path_pos; - - server_pos = - ftpsearch_get_server_position(download->ftps_info, - download->pconnections[i]->u. - host); - cur_path_pos = - ftpsearch_get_path_position(download->ftps_info, - download->pconnections[i]->u.host, - download->pconnections[i]->u.dir); - assert(cur_path_pos != -1); - - proz_debug("Server pos = %d, cur_path_pos=%d", server_pos, - cur_path_pos); - /*mark path as not valid */ - download->ftps_info->mirrors[server_pos].paths[cur_path_pos]. - valid = FALSE; - - /*See if more paths are avail */ ; - if (cur_path_pos < - (download->ftps_info->mirrors[server_pos].num_paths - 1)) - { - char *url_buf; - - /* Make sure this thread has terminated */ - pthread_join(download->threads[i], NULL); - /*additional paths avail to try */ - - download_show_message(download, - _ - ("Trying additional paths available on this server")); - proz_debug("Trying path %s", - download->ftps_info->mirrors[server_pos]. - paths[cur_path_pos + 1]); - - url_buf = - malloc(strlen - (download->ftps_info->mirrors[server_pos]. - server_name) + - strlen(download->ftps_info->mirrors[server_pos]. - paths[cur_path_pos + 1].path) + - strlen(download->pconnections[i]->u.file) + 11 + 1); - - sprintf(url_buf, "ftp://%s/%s/%s", - download->ftps_info->mirrors[server_pos].server_name, - download->ftps_info->mirrors[server_pos]. - paths[cur_path_pos + 1].path, - download->pconnections[i]->u.file); - - proz_debug("Target url for relaunching is %s", url_buf); - /*FIXME */ - proz_parse_url(url_buf, &download->pconnections[i]->u, 0); - free(url_buf); - - /* Make sure this thread has terminated */ - pthread_join(download->threads[i], NULL); - pthread_mutex_lock(&download->status_change_mutex); - download_show_message(download, _("Relaunching download")); - /*Relaunch thread */ - if (pthread_create - (&download->threads[i], NULL, (void *) &ftp_loop, - (void *) (download->pconnections[i])) != 0) - proz_die(_("Error: Not enough system resources")); - pthread_cond_wait(&download->pconnections[i]->connecting_cond, - &download->status_change_mutex); - pthread_mutex_unlock(&download->status_change_mutex); - } else - { - - /*download_show_message(download, - _ - ("No additional paths on this server available, so will try to switch to another server")); */ - /*Find any server that is downloading or completed and use it */ -download_switch_server_ftpsearch(download, i); - } - } - } else /*Handle the file not being found on the server */ - if (connection_err == FTPCWDFAIL) - { - if (proz_download_all_dls_ftpcwdfail(download) == TRUE) - { - download_show_message(download, - _ - ("Failed to change to the working directory on all the connections!")); - /*Terminate the connections */ - proz_download_stop_downloads(download); - return DLREMOTEFATAL; - } else - { - - /*Now we have to be careful */ - int server_pos, cur_path_pos; - - server_pos = - ftpsearch_get_server_position(download->ftps_info, - download->pconnections[i]->u. - host); - assert(server_pos != -1); - cur_path_pos = - ftpsearch_get_path_position(download->ftps_info, - download->pconnections[i]->u.host, - download->pconnections[i]->u.dir); - assert(cur_path_pos != -1); - - proz_debug("Server pos = %d, cur_path_pos=%d", server_pos, - cur_path_pos); - - /*mark path as not valid */ - download->ftps_info->mirrors[server_pos].paths[cur_path_pos]. - valid = FALSE; - /*See if more paths are avail */ ; - if (cur_path_pos < - (download->ftps_info->mirrors[server_pos].num_paths - 1)) - { - char *url_buf; - - /* Make sure this thread has terminated */ - pthread_join(download->threads[i], NULL); - /*additional paths avail to try */ - - download_show_message(download, - _ - ("Trying additional paths available on this server")); - proz_debug("Trying path %s", - download->ftps_info->mirrors[server_pos]. - paths[cur_path_pos + 1]); - - url_buf = - malloc(strlen - (download->ftps_info->mirrors[server_pos]. - server_name) + - strlen(download->ftps_info->mirrors[server_pos]. - paths[cur_path_pos + 1].path) + - strlen(download->pconnections[i]->u.file) + 11 + 1); - - sprintf(url_buf, "ftp://%s/%s/%s", - download->ftps_info->mirrors[server_pos].server_name, - download->ftps_info->mirrors[server_pos]. - paths[cur_path_pos + 1].path, - download->pconnections[i]->u.file); - - proz_debug("Target url for relaunching is %s", url_buf); - /*FIXME */ - proz_parse_url(url_buf, &download->pconnections[i]->u, 0); - free(url_buf); - - /*Relaunch thread */ - if (pthread_create - (&download->threads[i], NULL, (void *) &ftp_loop, - (void *) (download->pconnections[i])) != 0) - proz_die(_("Error: Not enough system resources")); - } else - { - - /* download_show_message(download, _("No additional paths on this server available, so will try to switch to another server")); */ - - - proz_debug - ("No additional paths on this server available, so will try to switch to another server"); - /*Find any server that is downloading or completed and use it */ - download_switch_server_ftpsearch(download, i); - } - } - } else if (connection_err == FTPRESTFAIL) - { - /*Handle the server not supporting REST */ - if (proz_download_all_dls_err(download, FTPRESTFAIL) == TRUE) - { - download_show_message(download, - _ - ("The server(s) do not support REST on all the connections!")); - /*Terminate the connections */ - proz_download_stop_downloads(download); - return DLREMOTEFATAL; - } else - { - - /*Now we have to be careful */ - int server_pos; - - server_pos = - ftpsearch_get_server_position(download->ftps_info, - download->pconnections[i]->u. - host); - - proz_debug("Server pos = %d", server_pos); - /*mark server as not supporting resume */ - download->ftps_info->mirrors[server_pos].resume_supported = - FALSE; - - { - download_show_message(download, - _ - ("This server does not support resuming downloads, so will switch to another server")); - download_switch_server_ftpsearch(download, i); - - } - } - } - - break; - - case LOCALFATAL: - proz_download_stop_downloads(download); - download_show_message(download, - _ - ("Connection %d, had a local fatal error: %s .Aborting download. "), - i, - proz_strerror(download->pconnections[i]->err)); - return DLLOCALFATAL; - break; - - case LOGINFAIL: - /* - * First check if the ftp server did not allow any thread - * to login at all, then retry the curent thread - */ - if (proz_download_all_dls_status(download, LOGINFAIL) == TRUE) - { - download_show_message(download, - _ - ("All logins rejected! Retrying connection")); - /* Make sure this thread has terminated */ - pthread_join(download->threads[i], NULL); - pthread_mutex_lock(&download->status_change_mutex); - if (pthread_create - (&download->threads[i], NULL, (void *) &ftp_loop, - (void *) (download->pconnections[i])) != 0) - proz_die(_("Error: Not enough system resources")); - - pthread_cond_wait(&download->pconnections[i]->connecting_cond, - &download->status_change_mutex); - pthread_mutex_unlock(&download->status_change_mutex); - break; - } else - { - - int j, usable_server; - - /*Find any server that is downloading or completed and use it instead */ - - usable_server = -1; - - pthread_mutex_lock(&download->status_change_mutex); - for (j = 0; j < download->num_connections; j++) - { - if ((download->pconnections[j]->status == DOWNLOADING || - download->pconnections[j]->status == COMPLETED) && j!=i) - { - usable_server = j; - } - } - pthread_mutex_unlock(&download->status_change_mutex); - - if (usable_server != -1) - { - /*We have a server which is DLING */ - download_show_message(download, - _ - ("This server has rejected the login attempt, so will switch to another server")); - /* Make sure this thread has terminated */ - pthread_join(download->threads[i], NULL); - - /*copy url and relaunch */ - proz_free_url(&download->pconnections[i]->u, 0); - memcpy(&download->pconnections[i]->u, - proz_copy_url(&download-> - pconnections[usable_server]->u), - sizeof(urlinfo)); - proz_debug - ("Found server %s which is downloading will relaunch based on it", - download->pconnections[usable_server]->u.host); - - - /*Relaunch thread */ - pthread_join(download->threads[i], NULL); - pthread_mutex_lock(&download->status_change_mutex); - download_show_message(download, _("Relaunching download")); - if (pthread_create - (&download->threads[i], NULL, (void *) &ftp_loop, - (void *) (download->pconnections[i])) != 0) - proz_die(_("Error: Not enough system resources")); - - pthread_cond_wait(&download->pconnections[i]->connecting_cond, - &download->status_change_mutex); - pthread_mutex_unlock(&download->status_change_mutex); - } else - { - - /* - * Ok so at least there is one download whos login has not been rejected, - * so lets see if it has completed, if so we can relaunch this connection, - * as the commonest reason for a ftp login being rejected is because, the - * ftp server has a limit on the number of logins permitted from the same - * IP address. - */ - - /* - * Query the number of threads that are downloading - * if it is zero then relaunch this connection - */ - int server_pos; - int dling_conns_count = - download_query_conns_status_count(download, DOWNLOADING, - download-> - pconnections[i]->u.host); - - server_pos = - ftpsearch_get_server_position(download->ftps_info, - download->pconnections[i]->u.host); - - if (dling_conns_count > - download->ftps_info->mirrors[server_pos].max_simul_connections) - { - download->ftps_info->mirrors[server_pos].max_simul_connections = - dling_conns_count; - break; - } - - - if (dling_conns_count == 0 - && - (download_query_conns_status_count - (download, CONNECTING, download->pconnections[i]->u.host) == 0) - && - (download_query_conns_status_count - (download, LOGGININ, download->pconnections[i]->u.host) == 0)) - { - - /* Make sure this thread has terminated */ - pthread_join(download->threads[i], NULL); - pthread_mutex_lock(&download->status_change_mutex); - download_show_message(download, _("Relaunching download")); - if (pthread_create(&download->threads[i], NULL, - (void *) &ftp_loop, - (void *) (download->pconnections[i])) != 0) - proz_die(_("Error: Not enough system resources")); - pthread_cond_wait(&download->pconnections[i]->connecting_cond, - &download->status_change_mutex); - pthread_mutex_unlock(&download->status_change_mutex); - } else - if (dling_conns_count < - download->ftps_info->mirrors[server_pos]. - max_simul_connections - && - (download_query_conns_status_count - (download, CONNECTING, - download->pconnections[i]->u.host) == 0) - && - (download_query_conns_status_count - (download, LOGGININ, - download->pconnections[i]->u.host) == 0)) - { - - /* Make sure this thread has terminated */ - pthread_join(download->threads[i], NULL); - pthread_mutex_lock(&download->status_change_mutex); - download_show_message(download, _("Relaunching download")); - if (pthread_create(&download->threads[i], NULL, - (void *) &ftp_loop, - (void *) (download->pconnections[i])) != 0) - proz_die(_("Error: Not enough system resources")); - pthread_cond_wait(&download->pconnections[i]->connecting_cond, - &download->status_change_mutex); - pthread_mutex_unlock(&download->status_change_mutex); - } - } - } - break; - - case CONREJECT: - /* - * First check if the ftp server did not allow any thread - * to login at all, then retry the curent thread - */ - if (proz_download_all_dls_status(download, CONREJECT) == TRUE) - { - download_show_message(download, - _ - ("All connections attempts have been rejected! Retrying connection")); - /* Make sure this thread has terminated */ - pthread_join(download->threads[i], NULL); - pthread_mutex_lock(&download->status_change_mutex); - - if (pthread_create - (&download->threads[i], NULL, (void *) &ftp_loop, - (void *) (download->pconnections[i])) != 0) - proz_die(_("Error: Not enough system resources")); - pthread_cond_wait(&download->pconnections[i]->connecting_cond, - &download->status_change_mutex); - pthread_mutex_unlock(&download->status_change_mutex); - break; - } else - { - - int j, usable_server; - - /*Find any server that is downloading or completed and use it instead */ - - usable_server = -1; - - pthread_mutex_lock(&download->status_change_mutex); - for (j = 0; j < download->num_connections; j++) - { - if ((download->pconnections[j]->status == DOWNLOADING || - download->pconnections[j]->status == COMPLETED) && j!=i) - { - usable_server = j; - } - } - pthread_mutex_unlock(&download->status_change_mutex); - - if (usable_server != -1) - { - /*We have a server which is DLING */ - download_show_message(download, - _ - ("This server has rejected the connection attempt, so will switch to another server")); - /* Make sure this thread has terminated */ - pthread_join(download->threads[i], NULL); - - /*copy url and relaunch */ - proz_free_url(&download->pconnections[i]->u, 0); - memcpy(&download->pconnections[i]->u, - proz_copy_url(&download-> - pconnections[usable_server]->u), - sizeof(urlinfo)); - proz_debug - ("Found server %s which is downloading will relaunch based on it", - download->pconnections[usable_server]->u.host); - - /*Relaunch thread */ - pthread_mutex_lock(&download->status_change_mutex); - - if (pthread_create - (&download->threads[i], NULL, (void *) &ftp_loop, - (void *) (download->pconnections[i])) != 0) - proz_die(_("Error: Not enough system resources")); - - pthread_cond_wait(&download->pconnections[i]->connecting_cond, - &download->status_change_mutex); - pthread_mutex_unlock(&download->status_change_mutex); - - } else - { - /* - * Ok so at least there is one download whos connections attempt has not been rejected, - * so lets see if it has completed, if so we can relaunch this connection, - * as the commonest reason for a ftp login being rejected is because, the - * ftp server has a limit on the number of logins permitted from the same - * IP address. - */ - - /* - * Query the number of threads that are downloading - * if it is zero then relaunch this connection - */ - int server_pos; - int dling_conns_count = - download_query_conns_status_count(download, DOWNLOADING, - download-> - pconnections[i]->u.host); - server_pos = - ftpsearch_get_server_position(download->ftps_info, - download->pconnections[i]->u.host); - - - if (dling_conns_count > - download->ftps_info->mirrors[server_pos].max_simul_connections) - { - download->ftps_info->mirrors[server_pos].max_simul_connections = - dling_conns_count; - break; - } - - - if (dling_conns_count == 0 - && - (download_query_conns_status_count - (download, CONNECTING, download->pconnections[i]->u.host) == 0) - && - (download_query_conns_status_count - (download, LOGGININ, download->pconnections[i]->u.host) == 0)) - { - - /* Make sure this thread has terminated */ - pthread_join(download->threads[i], NULL); - pthread_mutex_lock(&download->status_change_mutex); - download_show_message(download, _("Relaunching download")); - if (pthread_create(&download->threads[i], NULL, - (void *) &ftp_loop, - (void *) (download->pconnections[i])) != 0) - proz_die(_("Error: Not enough system resources")); - pthread_cond_wait(&download->pconnections[i]->connecting_cond, - &download->status_change_mutex); - pthread_mutex_unlock(&download->status_change_mutex); - } else - if (dling_conns_count < - download->ftps_info->mirrors[server_pos]. - max_simul_connections - && - (download_query_conns_status_count - (download, CONNECTING, - download->pconnections[i]->u.host) == 0) - && - (download_query_conns_status_count - (download, LOGGININ, - download->pconnections[i]->u.host) == 0)) - { - - /* Make sure this thread has terminated */ - pthread_join(download->threads[i], NULL); - pthread_mutex_lock(&download->status_change_mutex); - download_show_message(download, _("Relaunching download")); - if (pthread_create(&download->threads[i], NULL, - (void *) &ftp_loop, - (void *) (download->pconnections[i])) != 0) - proz_die(_("Error: Not enough system resources")); - pthread_cond_wait(&download->pconnections[i]->connecting_cond, - &download->status_change_mutex); - pthread_mutex_unlock(&download->status_change_mutex); - } - } - } - break; - - default: - break; + dl_status status; + uerr_t connection_err; + pthread_mutex_lock(download->pconnections[i]->status_change_mutex); + status = download->pconnections[i]->status; + pthread_mutex_unlock(download->pconnections[i]->status_change_mutex); + + pthread_mutex_lock(&download->pconnections[i]->access_mutex); + connection_err = download->pconnections[i]->err; + pthread_mutex_unlock(&download->pconnections[i]->access_mutex); + + + switch (status) + { + case MAXTRYS: + break; + + case REMOTEFATAL: + /* handle the CANTRESUME err code */ + + if (connection_err == CANTRESUME) + { + /*Terminate the connections */ + proz_download_stop_downloads(download); + /*FIXME Do we delete any downloaded portions here ? */ + return CANTRESUME; + } + else /*Handle the file not being found on the server */ + if (connection_err == FTPNSFOD || connection_err == HTTPNSFOD) + { + if (proz_download_all_dls_filensfod(download) == TRUE) + { + download_show_message(download, + _ + ("The file was not found in all the connections!")); + /*Terminate the connections */ + proz_download_stop_downloads(download); + return DLREMOTEFATAL; + } + else + { + /*Now we have to be careful */ + int server_pos, cur_path_pos; + + server_pos = + ftpsearch_get_server_position(download->ftps_info, + download->pconnections[i]->u. + host); + cur_path_pos = + ftpsearch_get_path_position(download->ftps_info, + download->pconnections[i]->u.host, + download->pconnections[i]->u.dir); + assert(cur_path_pos != -1); + + proz_debug("Server pos = %d, cur_path_pos=%d", server_pos, + cur_path_pos); + /*mark path as not valid */ + download->ftps_info->mirrors[server_pos].paths[cur_path_pos]. + valid = FALSE; + + /*See if more paths are avail */; + if (cur_path_pos < + (download->ftps_info->mirrors[server_pos].num_paths - 1)) + { + char *url_buf; + + /* Make sure this thread has terminated */ + pthread_join(download->threads[i], NULL); + /*additional paths avail to try */ + + download_show_message(download, + _ + ("Trying additional paths available on this server")); + proz_debug("Trying path %s", + download->ftps_info->mirrors[server_pos]. + paths[cur_path_pos + 1]); + + url_buf = + malloc(strlen + (download->ftps_info->mirrors[server_pos]. + server_name) + + strlen(download->ftps_info->mirrors[server_pos]. + paths[cur_path_pos + 1].path) + + strlen(download->pconnections[i]->u.file) + 11 + 1); + + sprintf(url_buf, "ftp://%s/%s/%s", + download->ftps_info->mirrors[server_pos].server_name, + download->ftps_info->mirrors[server_pos]. + paths[cur_path_pos + 1].path, + download->pconnections[i]->u.file); + + proz_debug("Target url for relaunching is %s", url_buf); + /*FIXME */ + proz_parse_url(url_buf, &download->pconnections[i]->u, 0); + free(url_buf); + + /* Make sure this thread has terminated */ + pthread_join(download->threads[i], NULL); + pthread_mutex_lock(&download->status_change_mutex); + download_show_message(download, _("Relaunching download")); + /*Relaunch thread */ + if (pthread_create + (&download->threads[i], NULL, (void *)&ftp_loop, + (void *)(download->pconnections[i])) != 0) + proz_die(_("Error: Not enough system resources")); + pthread_cond_wait(&download->pconnections[i]->connecting_cond, + &download->status_change_mutex); + pthread_mutex_unlock(&download->status_change_mutex); + } + else + { + /*download_show_message(download, + _ + ("No additional paths on this server available, so will try to switch to another server")); */ + /*Find any server that is downloading or completed and use it */ + download_switch_server_ftpsearch(download, i); + } + } + } + else /*Handle the file not being found on the server */ + if (connection_err == FTPCWDFAIL) + { + if (proz_download_all_dls_ftpcwdfail(download) == TRUE) + { + download_show_message(download, + _ + ("Failed to change to the working directory on all the connections!")); + /*Terminate the connections */ + proz_download_stop_downloads(download); + return DLREMOTEFATAL; + } + else + { + /*Now we have to be careful */ + int server_pos, cur_path_pos; + + server_pos = + ftpsearch_get_server_position(download->ftps_info, + download->pconnections[i]->u. + host); + assert(server_pos != -1); + cur_path_pos = + ftpsearch_get_path_position(download->ftps_info, + download->pconnections[i]->u.host, + download->pconnections[i]->u.dir); + assert(cur_path_pos != -1); + + proz_debug("Server pos = %d, cur_path_pos=%d", server_pos, + cur_path_pos); + + /*mark path as not valid */ + download->ftps_info->mirrors[server_pos].paths[cur_path_pos]. + valid = FALSE; + /*See if more paths are avail */; + if (cur_path_pos < + (download->ftps_info->mirrors[server_pos].num_paths - 1)) + { + char *url_buf; + + /* Make sure this thread has terminated */ + pthread_join(download->threads[i], NULL); + /*additional paths avail to try */ + + download_show_message(download, + _ + ("Trying additional paths available on this server")); + proz_debug("Trying path %s", + download->ftps_info->mirrors[server_pos]. + paths[cur_path_pos + 1]); + + url_buf = + malloc(strlen + (download->ftps_info->mirrors[server_pos]. + server_name) + + strlen(download->ftps_info->mirrors[server_pos]. + paths[cur_path_pos + 1].path) + + strlen(download->pconnections[i]->u.file) + 11 + 1); + + sprintf(url_buf, "ftp://%s/%s/%s", + download->ftps_info->mirrors[server_pos].server_name, + download->ftps_info->mirrors[server_pos]. + paths[cur_path_pos + 1].path, + download->pconnections[i]->u.file); + + proz_debug("Target url for relaunching is %s", url_buf); + /*FIXME */ + proz_parse_url(url_buf, &download->pconnections[i]->u, 0); + free(url_buf); + + /*Relaunch thread */ + if (pthread_create + (&download->threads[i], NULL, (void *)&ftp_loop, + (void *)(download->pconnections[i])) != 0) + proz_die(_("Error: Not enough system resources")); + } + else + { + /* download_show_message(download, _("No additional paths on this server available, so will try to switch to another server")); */ + + + proz_debug + ("No additional paths on this server available, so will try to switch to another server"); + /*Find any server that is downloading or completed and use it */ + download_switch_server_ftpsearch(download, i); + } + } + } + else if (connection_err == FTPRESTFAIL) + { + /*Handle the server not supporting REST */ + if (proz_download_all_dls_err(download, FTPRESTFAIL) == TRUE) + { + download_show_message(download, + _ + ("The server(s) do not support REST on all the connections!")); + /*Terminate the connections */ + proz_download_stop_downloads(download); + return DLREMOTEFATAL; + } + else + { + /*Now we have to be careful */ + int server_pos; + + server_pos = + ftpsearch_get_server_position(download->ftps_info, + download->pconnections[i]->u. + host); + + proz_debug("Server pos = %d", server_pos); + /*mark server as not supporting resume */ + download->ftps_info->mirrors[server_pos].resume_supported = + FALSE; + + { + download_show_message(download, + _ + ("This server does not support resuming downloads, so will switch to another server")); + download_switch_server_ftpsearch(download, i); + } + } + } + + break; + + case LOCALFATAL: + proz_download_stop_downloads(download); + download_show_message(download, + _ + ("Connection %d, had a local fatal error: %s .Aborting download. "), + i, + proz_strerror(download->pconnections[i]->err)); + return DLLOCALFATAL; + break; + + case LOGINFAIL: + /* + * First check if the ftp server did not allow any thread + * to login at all, then retry the curent thread + */ + if (proz_download_all_dls_status(download, LOGINFAIL) == TRUE) + { + download_show_message(download, + _ + ("All logins rejected! Retrying connection")); + /* Make sure this thread has terminated */ + pthread_join(download->threads[i], NULL); + pthread_mutex_lock(&download->status_change_mutex); + if (pthread_create + (&download->threads[i], NULL, (void *)&ftp_loop, + (void *)(download->pconnections[i])) != 0) + proz_die(_("Error: Not enough system resources")); + + pthread_cond_wait(&download->pconnections[i]->connecting_cond, + &download->status_change_mutex); + pthread_mutex_unlock(&download->status_change_mutex); + break; + } + else + { + int j, usable_server; + + /*Find any server that is downloading or completed and use it instead */ + + usable_server = -1; + + pthread_mutex_lock(&download->status_change_mutex); + for (j = 0; j < download->num_connections; j++) + { + if ((download->pconnections[j]->status == DOWNLOADING || + download->pconnections[j]->status == COMPLETED) && j != i) + { + usable_server = j; + } + } + pthread_mutex_unlock(&download->status_change_mutex); + + if (usable_server != -1) + { + /*We have a server which is DLING */ + download_show_message(download, + _ + ("This server has rejected the login attempt, so will switch to another server")); + /* Make sure this thread has terminated */ + pthread_join(download->threads[i], NULL); + + /*copy url and relaunch */ + proz_free_url(&download->pconnections[i]->u, 0); + memcpy(&download->pconnections[i]->u, + proz_copy_url(&download-> + pconnections[usable_server]->u), + sizeof(urlinfo)); + proz_debug + ("Found server %s which is downloading will relaunch based on it", + download->pconnections[usable_server]->u.host); + + + /*Relaunch thread */ + pthread_join(download->threads[i], NULL); + pthread_mutex_lock(&download->status_change_mutex); + download_show_message(download, _("Relaunching download")); + if (pthread_create + (&download->threads[i], NULL, (void *)&ftp_loop, + (void *)(download->pconnections[i])) != 0) + proz_die(_("Error: Not enough system resources")); + + pthread_cond_wait(&download->pconnections[i]->connecting_cond, + &download->status_change_mutex); + pthread_mutex_unlock(&download->status_change_mutex); + } + else + { + /* + * Ok so at least there is one download whos login has not been rejected, + * so lets see if it has completed, if so we can relaunch this connection, + * as the commonest reason for a ftp login being rejected is because, the + * ftp server has a limit on the number of logins permitted from the same + * IP address. + */ + + /* + * Query the number of threads that are downloading + * if it is zero then relaunch this connection + */ + int server_pos; + int dling_conns_count = + download_query_conns_status_count(download, DOWNLOADING, + download-> + pconnections[i]->u.host); + + server_pos = + ftpsearch_get_server_position(download->ftps_info, + download->pconnections[i]->u.host); + + if (dling_conns_count > + download->ftps_info->mirrors[server_pos].max_simul_connections) + { + download->ftps_info->mirrors[server_pos].max_simul_connections = + dling_conns_count; + break; + } + + + if (dling_conns_count == 0 + && + (download_query_conns_status_count + (download, CONNECTING, download->pconnections[i]->u.host) == 0) + && + (download_query_conns_status_count + (download, LOGGININ, download->pconnections[i]->u.host) == 0)) + { + /* Make sure this thread has terminated */ + pthread_join(download->threads[i], NULL); + pthread_mutex_lock(&download->status_change_mutex); + download_show_message(download, _("Relaunching download")); + if (pthread_create(&download->threads[i], NULL, + (void *)&ftp_loop, + (void *)(download->pconnections[i])) != 0) + proz_die(_("Error: Not enough system resources")); + pthread_cond_wait(&download->pconnections[i]->connecting_cond, + &download->status_change_mutex); + pthread_mutex_unlock(&download->status_change_mutex); + } + else + if (dling_conns_count < + download->ftps_info->mirrors[server_pos]. + max_simul_connections + && + (download_query_conns_status_count + (download, CONNECTING, + download->pconnections[i]->u.host) == 0) + && + (download_query_conns_status_count + (download, LOGGININ, + download->pconnections[i]->u.host) == 0)) + { + /* Make sure this thread has terminated */ + pthread_join(download->threads[i], NULL); + pthread_mutex_lock(&download->status_change_mutex); + download_show_message(download, _("Relaunching download")); + if (pthread_create(&download->threads[i], NULL, + (void *)&ftp_loop, + (void *)(download->pconnections[i])) != 0) + proz_die(_("Error: Not enough system resources")); + pthread_cond_wait(&download->pconnections[i]->connecting_cond, + &download->status_change_mutex); + pthread_mutex_unlock(&download->status_change_mutex); + } + } + } + break; + + case CONREJECT: + /* + * First check if the ftp server did not allow any thread + * to login at all, then retry the curent thread + */ + if (proz_download_all_dls_status(download, CONREJECT) == TRUE) + { + download_show_message(download, + _ + ("All connections attempts have been rejected! Retrying connection")); + /* Make sure this thread has terminated */ + pthread_join(download->threads[i], NULL); + pthread_mutex_lock(&download->status_change_mutex); + + if (pthread_create + (&download->threads[i], NULL, (void *)&ftp_loop, + (void *)(download->pconnections[i])) != 0) + proz_die(_("Error: Not enough system resources")); + pthread_cond_wait(&download->pconnections[i]->connecting_cond, + &download->status_change_mutex); + pthread_mutex_unlock(&download->status_change_mutex); + break; + } + else + { + int j, usable_server; + + /*Find any server that is downloading or completed and use it instead */ + + usable_server = -1; + + pthread_mutex_lock(&download->status_change_mutex); + for (j = 0; j < download->num_connections; j++) + { + if ((download->pconnections[j]->status == DOWNLOADING || + download->pconnections[j]->status == COMPLETED) && j != i) + { + usable_server = j; + } + } + pthread_mutex_unlock(&download->status_change_mutex); + + if (usable_server != -1) + { + /*We have a server which is DLING */ + download_show_message(download, + _ + ("This server has rejected the connection attempt, so will switch to another server")); + /* Make sure this thread has terminated */ + pthread_join(download->threads[i], NULL); + + /*copy url and relaunch */ + proz_free_url(&download->pconnections[i]->u, 0); + memcpy(&download->pconnections[i]->u, + proz_copy_url(&download-> + pconnections[usable_server]->u), + sizeof(urlinfo)); + proz_debug + ("Found server %s which is downloading will relaunch based on it", + download->pconnections[usable_server]->u.host); + + /*Relaunch thread */ + pthread_mutex_lock(&download->status_change_mutex); + + if (pthread_create + (&download->threads[i], NULL, (void *)&ftp_loop, + (void *)(download->pconnections[i])) != 0) + proz_die(_("Error: Not enough system resources")); + + pthread_cond_wait(&download->pconnections[i]->connecting_cond, + &download->status_change_mutex); + pthread_mutex_unlock(&download->status_change_mutex); + } + else + { + /* + * Ok so at least there is one download whos connections attempt has not been rejected, + * so lets see if it has completed, if so we can relaunch this connection, + * as the commonest reason for a ftp login being rejected is because, the + * ftp server has a limit on the number of logins permitted from the same + * IP address. + */ + + /* + * Query the number of threads that are downloading + * if it is zero then relaunch this connection + */ + int server_pos; + int dling_conns_count = + download_query_conns_status_count(download, DOWNLOADING, + download-> + pconnections[i]->u.host); + server_pos = + ftpsearch_get_server_position(download->ftps_info, + download->pconnections[i]->u.host); + + + if (dling_conns_count > + download->ftps_info->mirrors[server_pos].max_simul_connections) + { + download->ftps_info->mirrors[server_pos].max_simul_connections = + dling_conns_count; + break; + } + + + if (dling_conns_count == 0 + && + (download_query_conns_status_count + (download, CONNECTING, download->pconnections[i]->u.host) == 0) + && + (download_query_conns_status_count + (download, LOGGININ, download->pconnections[i]->u.host) == 0)) + { + /* Make sure this thread has terminated */ + pthread_join(download->threads[i], NULL); + pthread_mutex_lock(&download->status_change_mutex); + download_show_message(download, _("Relaunching download")); + if (pthread_create(&download->threads[i], NULL, + (void *)&ftp_loop, + (void *)(download->pconnections[i])) != 0) + proz_die(_("Error: Not enough system resources")); + pthread_cond_wait(&download->pconnections[i]->connecting_cond, + &download->status_change_mutex); + pthread_mutex_unlock(&download->status_change_mutex); + } + else + if (dling_conns_count < + download->ftps_info->mirrors[server_pos]. + max_simul_connections + && + (download_query_conns_status_count + (download, CONNECTING, + download->pconnections[i]->u.host) == 0) + && + (download_query_conns_status_count + (download, LOGGININ, + download->pconnections[i]->u.host) == 0)) + { + /* Make sure this thread has terminated */ + pthread_join(download->threads[i], NULL); + pthread_mutex_lock(&download->status_change_mutex); + download_show_message(download, _("Relaunching download")); + if (pthread_create(&download->threads[i], NULL, + (void *)&ftp_loop, + (void *)(download->pconnections[i])) != 0) + proz_die(_("Error: Not enough system resources")); + pthread_cond_wait(&download->pconnections[i]->connecting_cond, + &download->status_change_mutex); + pthread_mutex_unlock(&download->status_change_mutex); + } + } + } + break; + + default: + break; + } } - } /*bandwith throttling */ download_calc_throttle_factor(download); @@ -1922,12 +1914,12 @@ download_switch_server_ftpsearch(download, i); /****************************************************************************** - This will setup a download based on the connection info and the ftpsearch results. + This will setup a download based on the connection info and the ftpsearch results. ******************************************************************************/ int proz_download_setup_connections_ftpsearch(download_t * download, - connection_t * connection, - ftps_request_t * request, - int req_connections) + connection_t * connection, + ftps_request_t * request, + int req_connections) { int num_connections, i, num_usable_mirrors = 0; off_t bytes_per_connection; @@ -1935,6 +1927,7 @@ int proz_download_setup_connections_ftpsearch(download_t * download, FILE *fp; char *out_file; struct stat stat_buf; + /*TODO Check for log file and use same number of threads */ proz_debug("proz_download_setup_connections_ftpsearch"); @@ -1947,7 +1940,7 @@ int proz_download_setup_connections_ftpsearch(download_t * download, if (request->num_mirrors > 0 && request->mirrors[0].status == RESPONSEOK) { - proz_debug("got mirrors"); + proz_debug("got mirrors"); /*We should get the info and allocate here */ download->resume_support = TRUE; @@ -1957,166 +1950,164 @@ int proz_download_setup_connections_ftpsearch(download_t * download, /* Get the number of mirros that are within 200% ping speed of the fastest */ for (i = 0; i < request->num_mirrors; i++) - { - if ((request->mirrors[i].status == RESPONSEOK) && - (request->mirrors[i].milli_secs <= - (request->mirrors[0].milli_secs + - ((request->mirrors[0].milli_secs * 100) / 100)))) - num_usable_mirrors++; - } + { + if ((request->mirrors[i].status == RESPONSEOK) && + (request->mirrors[i].milli_secs <= + (request->mirrors[0].milli_secs + + ((request->mirrors[0].milli_secs * 100) / 100)))) + num_usable_mirrors++; + } proz_debug("usable mirrors = %d", num_usable_mirrors); proz_debug("num_connections = %d", num_connections); download->pconnections = - kmalloc(sizeof(connection_t*) * num_connections); + kmalloc(sizeof(connection_t*) * num_connections); download->num_connections = num_connections; for (i = 0; i < num_connections; i++) - { - urlinfo *url = malloc(sizeof(urlinfo)); - char *url_buf; + { + urlinfo *url = malloc(sizeof(urlinfo)); + char *url_buf; - if (i < num_usable_mirrors) - { - url_buf = - malloc(strlen(request->mirrors[i].server_name) + - strlen(request->mirrors[i].paths[0].path) + - strlen(connection->u.file) + 11 + 1); + if (i < num_usable_mirrors) + { + url_buf = + malloc(strlen(request->mirrors[i].server_name) + + strlen(request->mirrors[i].paths[0].path) + + strlen(connection->u.file) + 11 + 1); - sprintf(url_buf, "ftp://%s/%s/%s", request->mirrors[i].server_name, - request->mirrors[i].paths[0].path, connection->u.file); + sprintf(url_buf, "ftp://%s/%s/%s", request->mirrors[i].server_name, + request->mirrors[i].paths[0].path, connection->u.file); - proz_debug("Target url is %s", url_buf); - /*FIXME */ - proz_parse_url(url_buf, url, 0); - free(url_buf); + proz_debug("Target url is %s", url_buf); + /*FIXME */ + proz_parse_url(url_buf, url, 0); + free(url_buf); - download->pconnections[i]=proz_connection_init(url, - &download->status_change_mutex); + download->pconnections[i] = proz_connection_init(url, + &download->status_change_mutex); + } + else + { + int extra_mirror = 0; + /*FIXME improve allocation algorithm in this part */ - } else - { - int extra_mirror = 0; - /*FIXME improve allocation algorithm in this part */ + url_buf = + malloc(strlen(request->mirrors[extra_mirror].server_name) + + strlen(request->mirrors[extra_mirror].paths[0].path) + + strlen(connection->u.file) + 11 + 1); - url_buf = - malloc(strlen(request->mirrors[extra_mirror].server_name) + - strlen(request->mirrors[extra_mirror].paths[0].path) + - strlen(connection->u.file) + 11 + 1); + sprintf(url_buf, "ftp://%s/%s/%s", + request->mirrors[extra_mirror].server_name, + request->mirrors[extra_mirror].paths[0].path, + connection->u.file); - sprintf(url_buf, "ftp://%s/%s/%s", - request->mirrors[extra_mirror].server_name, - request->mirrors[extra_mirror].paths[0].path, - connection->u.file); + proz_debug("Target url is %s", url_buf); + /*FIXME */ + proz_parse_url(url_buf, url, 0); + free(url_buf); - proz_debug("Target url is %s", url_buf); - /*FIXME */ - proz_parse_url(url_buf, url, 0); - free(url_buf); - - download->pconnections[i]=proz_connection_init(url, - &download->status_change_mutex); - } + download->pconnections[i] = proz_connection_init(url, + &download->status_change_mutex); + } - download->resume_support = download->pconnections[i]->resume_support = - TRUE; - memcpy(&download->pconnections[i]->hs, &connection->hs, - sizeof(http_stat_t)); + download->resume_support = download->pconnections[i]->resume_support = + TRUE; + memcpy(&download->pconnections[i]->hs, &connection->hs, + sizeof(http_stat_t)); - out_file=kmalloc(PATH_MAX); - snprintf(out_file, PATH_MAX, "%s/%s.prozilla", - download->dl_dir, connection->u.file); + out_file = kmalloc(PATH_MAX); + snprintf(out_file, PATH_MAX, "%s/%s.prozilla", + download->dl_dir, connection->u.file); - //First see if the file exists then we dont create a new one else we do + //First see if the file exists then we dont create a new one else we do - if (stat(out_file, &stat_buf) == -1) - { - /* the call failed */ - /* if the error is due to the file not been present then there is no - need to do anything..just continue, otherwise return error (-1) - */ - if (errno == ENOENT) - { - //File not exists so create it - if (! - (fp = - fopen(out_file, "w+"))) - { - download_show_message(download, - _ - ("Unable to open file %s: %s!"), - out_file, strerror(errno)); - return -1; - } - - } - else - return -1; - } - else - { - //TODO: File exists : so stat it and if it doesnt match file size warna boput it... - if (! - (fp = - fopen(out_file, "r+"))) - { - download_show_message(download, - _ - ("Unable to open file %s: %s!"), - out_file, strerror(errno)); - return -1; - } - - } + if (stat(out_file, &stat_buf) == -1) + { + /* the call failed */ + /* if the error is due to the file not been present then there is no + need to do anything..just continue, otherwise return error (-1) + */ + if (errno == ENOENT) + { + //File not exists so create it + if (! + (fp = + fopen(out_file, "w+"))) + { + download_show_message(download, + _ + ("Unable to open file %s: %s!"), + out_file, strerror(errno)); + return -1; + } + } + else + return -1; + } + else + { + //TODO: File exists : so stat it and if it doesnt match file size warna boput it... + if (! + (fp = + fopen(out_file, "r+"))) + { + download_show_message(download, + _ + ("Unable to open file %s: %s!"), + out_file, strerror(errno)); + return -1; + } + } - //TRY setting the offset; - if (download->main_file_size != -1) - { + //TRY setting the offset; + if (download->main_file_size != -1) + { + if (fseeko(fp, download->main_file_size, SEEK_SET) != 0) + return -1; + } - if(fseeko(fp, download->main_file_size, SEEK_SET)!=0) - return -1; - } - - /*Make sure all writes go directly to the file */ - setvbuf(fp, NULL, _IONBF, 0); + /*Make sure all writes go directly to the file */ + setvbuf(fp, NULL, _IONBF, 0); - download->pconnections[i]->localfile = kmalloc(PATH_MAX); - strcpy(out_file, download->pconnections[i]->localfile); - download->pconnections[i]->fp=fp; + download->pconnections[i]->localfile = kmalloc(PATH_MAX); + strcpy(out_file, download->pconnections[i]->localfile); + download->pconnections[i]->fp = fp; - download->pconnections[i]->retry = TRUE; + download->pconnections[i]->retry = TRUE; - download->pconnections[i]->main_file_size = connection->main_file_size; - download->pconnections[i]->orig_remote_startpos = download->pconnections[i]->remote_startpos = i * bytes_per_connection; - download->pconnections[i]->remote_endpos = - i * bytes_per_connection + bytes_per_connection; + download->pconnections[i]->main_file_size = connection->main_file_size; + download->pconnections[i]->orig_remote_startpos = download->pconnections[i]->remote_startpos = i * bytes_per_connection; + download->pconnections[i]->remote_endpos = + i * bytes_per_connection + bytes_per_connection; - //Changing things here..... - download->pconnections[i]->local_startpos = download->pconnections[i]->remote_startpos; + //Changing things here..... + download->pconnections[i]->local_startpos = download->pconnections[i]->remote_startpos; - /*Set the connections message to be download->msg_proc calback */ - proz_connection_set_msg_proc(download->pconnections[i], - download->msg_proc, download->cb_data); - } + /*Set the connections message to be download->msg_proc calback */ + proz_connection_set_msg_proc(download->pconnections[i], + download->msg_proc, download->cb_data); + } /* Add the remaining bytes to the last connection */ download->pconnections[--i]->remote_endpos += bytes_left; - } else - { - proz_debug("No mirrors, which were up are found"); - download->using_ftpsearch = FALSE; - return proz_download_setup_connections_no_ftpsearch(download, - connection, - req_connections); - } + } + else + { + proz_debug("No mirrors, which were up are found"); + download->using_ftpsearch = FALSE; + return proz_download_setup_connections_no_ftpsearch(download, + connection, + req_connections); + } download->using_ftpsearch = TRUE; return num_connections; @@ -2125,45 +2116,48 @@ int proz_download_setup_connections_ftpsearch(download_t * download, uerr_t proz_download_get_join_status(download_t *download) { - int building_status; - pthread_mutex_lock(&download->access_mutex); - building_status = download->building; - pthread_mutex_unlock(&download->access_mutex); - - switch(building_status) - { - case 1: - return JOININPROGRESS; - case 0: - return JOINDONE; - case -1: - return JOINERR; - default: - proz_die("Bad building falg in download structure"); - } - return -1; + pthread_mutex_lock(&download->access_mutex); + building_status = download->building; + pthread_mutex_unlock(&download->access_mutex); + + switch (building_status) + { + case 1: + return JOININPROGRESS; + + case 0: + return JOINDONE; + + case -1: + return JOINERR; + + default: + proz_die("Bad building falg in download structure"); + } + + return -1; } float proz_download_get_file_build_percentage(download_t *download) { - float percent_done; + pthread_mutex_lock(&download->access_mutex); - percent_done =100; + percent_done = 100; pthread_mutex_unlock(&download->access_mutex); return percent_done; } void proz_download_cancel_joining_thread(download_t * download) { - pthread_cancel(download->join_thread); - pthread_join(download->join_thread, NULL); + pthread_cancel(download->join_thread); + pthread_join(download->join_thread, NULL); } void proz_download_wait_till_end_joining_thread(download_t * download) { - pthread_join(download->join_thread, NULL); + pthread_join(download->join_thread, NULL); } diff --git a/libprozilla/src/download.h b/libprozilla/src/download.h index 9687a06..e0c53dd 100644 --- a/libprozilla/src/download.h +++ b/libprozilla/src/download.h @@ -1,25 +1,25 @@ /****************************************************************************** - libprozilla - a download accelerator library - Copyright (C) 2001 Kalum Somaratna + libprozilla - a download accelerator library + Copyright (C) 2001 Kalum Somaratna - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ******************************************************************************/ /* Download routines. */ -/* $Id: download.h,v 1.25 2001/09/30 23:13:50 kalum Exp $ */ +/* $Id$ */ #ifndef DOWNLOAD_H @@ -35,17 +35,17 @@ extern "C" { #endif - void download_show_message(download_t * download, const char *format, - ...); - int download_query_conns_status_count(download_t * download, - dl_status status, char *server); +void download_show_message(download_t * download, const char *format, + ...); +int download_query_conns_status_count(download_t * download, + dl_status status, char *server); - void download_join_downloads(download_t * download); - void join_downloads(download_t * download); - void download_calc_throttle_factor(download_t * download); - uerr_t download_handle_threads_ftpsearch(download_t * download); - uerr_t download_handle_threads_no_ftpsearch(download_t * download); +void download_join_downloads(download_t * download); +void join_downloads(download_t * download); +void download_calc_throttle_factor(download_t * download); +uerr_t download_handle_threads_ftpsearch(download_t * download); +uerr_t download_handle_threads_no_ftpsearch(download_t * download); #ifdef __cplusplus } #endif -#endif /* DOWNLOAD_H */ +#endif /* DOWNLOAD_H */ diff --git a/libprozilla/src/ftp-retr.c b/libprozilla/src/ftp-retr.c index 4bd6aef..2d04010 100644 --- a/libprozilla/src/ftp-retr.c +++ b/libprozilla/src/ftp-retr.c @@ -1,23 +1,23 @@ /****************************************************************************** - libprozilla - a download accelerator library - Copyright (C) 2001 Kalum Somaratna + libprozilla - a download accelerator library + Copyright (C) 2001 Kalum Somaratna - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ******************************************************************************/ -/* $Id: ftp-retr.c,v 1.18 2005/01/11 01:49:11 sean Exp $ */ +/* $Id$ */ #include "common.h" @@ -57,10 +57,10 @@ uerr_t proz_ftp_get_file(connection_t * connection) /* if there is nothing to download then return */ if (connection->status == COMPLETED) - { - gettimeofday(&connection->time_begin, NULL); - return FTPOK; - } + { + gettimeofday(&connection->time_begin, NULL); + return FTPOK; + } init_response(connection); @@ -74,34 +74,35 @@ uerr_t proz_ftp_get_file(connection_t * connection) */ if (ftp_use_proxy(connection) && connection->ftp_proxy->type == HTTPPROXY) - { - err = ftp_get_file_from_http_proxy(connection); - return err; - } + { + err = ftp_get_file_from_http_proxy(connection); + return err; + } if (ftp_use_proxy(connection)) - { - /* Connect to the proxy server here. */ - err = ftp_connect_to_server(connection, - connection->ftp_proxy->proxy_url.host, - connection->ftp_proxy->proxy_url.port); - } else - { - err = ftp_connect_to_server(connection, connection->u.host, - connection->u.port); - } + { + /* Connect to the proxy server here. */ + err = ftp_connect_to_server(connection, + connection->ftp_proxy->proxy_url.host, + connection->ftp_proxy->proxy_url.port); + } + else + { + err = ftp_connect_to_server(connection, connection->u.host, + connection->u.port); + } if (err == FTPCONREFUSED) - { - connection_change_status(connection, CONREJECT); - close_sock(&connection->ctrl_sock); - return err; - } + { + connection_change_status(connection, CONREJECT); + close_sock(&connection->ctrl_sock); + return err; + } if (err != FTPOK) - { - connection_change_status(connection, REMOTEFATAL); - return err; - } + { + connection_change_status(connection, REMOTEFATAL); + return err; + } done_with_response(connection); @@ -110,15 +111,15 @@ uerr_t proz_ftp_get_file(connection_t * connection) /* Use .netrc if asked to do so. */ if (connection->use_netrc == TRUE) - { - netrc_ent = search_netrc(libprozrtinfo.netrc_list, connection->u.host); - - if (netrc_ent != NULL) { - user = netrc_ent->account; - passwd = netrc_ent->password; + netrc_ent = search_netrc(libprozrtinfo.netrc_list, connection->u.host); + + if (netrc_ent != NULL) + { + user = netrc_ent->account; + passwd = netrc_ent->password; + } } - } user = user ? user : libprozrtinfo.ftp_default_user; passwd = passwd ? passwd : libprozrtinfo.ftp_default_passwd; @@ -129,97 +130,100 @@ uerr_t proz_ftp_get_file(connection_t * connection) init_response(connection); err = ftp_login(connection, user, passwd); if (err != FTPOK) - { - if (err == FTPLOGREFUSED) { - connection_change_status(connection, LOGINFAIL); - } else - { - connection_change_status(connection, REMOTEFATAL); + if (err == FTPLOGREFUSED) + { + connection_change_status(connection, LOGINFAIL); + } + else + { + connection_change_status(connection, REMOTEFATAL); + } + close_sock(&connection->ctrl_sock); + return err; } - close_sock(&connection->ctrl_sock); - return err; - } done_with_response(connection); init_response(connection); err = ftp_binary(connection); if (err != FTPOK) - { - connection_change_status(connection, REMOTEFATAL); - close_sock(&connection->ctrl_sock); - return err; - } + { + connection_change_status(connection, REMOTEFATAL); + close_sock(&connection->ctrl_sock); + return err; + } done_with_response(connection); /* Do we need to CWD? */ if (*connection->u.dir) - { - init_response(connection); + { + init_response(connection); - err = ftp_cwd(connection, connection->u.dir); - if (err != FTPOK) - { - connection_change_status(connection, REMOTEFATAL); - proz_debug(_("CWD failed to change to directory '%s'."), - connection->u.dir); - close_sock(&connection->ctrl_sock); - return err; - } else - { - proz_debug(_("CWD ok.")); - done_with_response(connection); + err = ftp_cwd(connection, connection->u.dir); + if (err != FTPOK) + { + connection_change_status(connection, REMOTEFATAL); + proz_debug(_("CWD failed to change to directory '%s'."), + connection->u.dir); + close_sock(&connection->ctrl_sock); + return err; + } + else + { + proz_debug(_("CWD ok.")); + done_with_response(connection); + } } - } else + else proz_debug(_("CWD not needed.")); err = ftp_setup_data_sock_1(connection, &passive_mode); if (err != FTPOK) - { - connection_change_status(connection, REMOTEFATAL); - close_sock(&connection->ctrl_sock); - return err; - } + { + connection_change_status(connection, REMOTEFATAL); + close_sock(&connection->ctrl_sock); + return err; + } /* do we need to REST */ if (connection->remote_startpos > 0 && connection->resume_support == TRUE) - { - err = ftp_rest(connection, connection->remote_startpos); - } + { + err = ftp_rest(connection, connection->remote_startpos); + } if (err != FTPOK) - { - if (err == FTPRESTFAIL) - proz_debug - (_ - ("I have a bug in my code!!, check remote_starpos and resume_support values")); + { + if (err == FTPRESTFAIL) + proz_debug + (_ + ("I have a bug in my code!!, check remote_starpos and resume_support values")); - connection_change_status(connection, REMOTEFATAL); - close_sock(&connection->ctrl_sock); - return err; - } + connection_change_status(connection, REMOTEFATAL); + close_sock(&connection->ctrl_sock); + return err; + } err = ftp_retr(connection, connection->u.file); if (err != FTPOK) - { - proz_debug(_("RETR failed")); - close_sock(&connection->ctrl_sock); - connection_change_status(connection, REMOTEFATAL); - return err; - } + { + proz_debug(_("RETR failed")); + close_sock(&connection->ctrl_sock); + connection_change_status(connection, REMOTEFATAL); + return err; + } err = ftp_setup_data_sock_2(connection, &passive_mode); if (err != FTPOK) - { - return err; - } + { + return err; + } /* which routine to call */ if (connection->main_file_size == -1) err = - connection_retr_fsize_not_known(connection, buffer, - sizeof(buffer)); + connection_retr_fsize_not_known(connection, buffer, + sizeof(buffer)); else err = connection_retr_fsize_known(connection, buffer, sizeof(buffer)); @@ -233,11 +237,11 @@ uerr_t proz_ftp_get_file(connection_t * connection) } -/* A genuine loop ;) It willed be called by the main thread, and -this will handle all possible errors itself, retrying until the number -of maximum tries for the connection is realised or a error occurs which -needs to be passed upwards for handling, such as LOGINFAIL -*/ +/* A genuine loop ;) It willed be called by the main thread, and + this will handle all possible errors itself, retrying until the number + of maximum tries for the connection is realised or a error occurs which + needs to be passed upwards for handling, such as LOGINFAIL + */ uerr_t ftp_loop(connection_t * connection) { @@ -249,84 +253,82 @@ uerr_t ftp_loop(connection_t * connection) pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); do - { - if (connection->attempts > 0) { + if (connection->attempts > 0) + { + if (retrying_from_loop == TRUE) + { + connection_show_message(connection, + _("Retrying..Attempt %d in %d seconds"), + connection->attempts, + connection->retry_delay.tv_sec); + delay_ms(connection->retry_delay.tv_sec * 1000); + } - if (retrying_from_loop == TRUE) - { - connection_show_message(connection, - _("Retrying..Attempt %d in %d seconds"), - connection->attempts, - connection->retry_delay.tv_sec); - delay_ms(connection->retry_delay.tv_sec * 1000); - } - - if (connection->resume_support == TRUE) - { - if (connection_load_resume_info(connection) == -1) - { - connection_show_message(connection, - _ - ("Error while attemting to process download file ")); - } - } else - { - /*If we cant resume then reset the connections bytesreceived to 0 */ - connection->remote_bytes_received = 0; - } - } + if (connection->resume_support == TRUE) + { + if (connection_load_resume_info(connection) == -1) + { + connection_show_message(connection, + _ + ("Error while attemting to process download file ")); + } + } + else + { + /*If we cant resume then reset the connections bytesreceived to 0 */ + connection->remote_bytes_received = 0; + } + } - /*Push the handler which will cleanup any sockets that are left open */ - pthread_cleanup_push(cleanup_socks, (void *) connection); + /*Push the handler which will cleanup any sockets that are left open */ + pthread_cleanup_push(cleanup_socks, (void *)connection); - connection->err = proz_ftp_get_file(connection); - /*pop the handler */ - pthread_cleanup_pop(0); + connection->err = proz_ftp_get_file(connection); + /*pop the handler */ + pthread_cleanup_pop(0); - connection->attempts++; + connection->attempts++; - /*Should the error be handled at this level ? */ - if (!ftp_loop_handle_error(connection->err)) - { - return connection->err; /*If not return and the main thread will handle it */ - } + /*Should the error be handled at this level ? */ + if (!ftp_loop_handle_error(connection->err)) + { + return connection->err; /*If not return and the main thread will handle it */ + } - switch (connection->err) - { - case FTPOK: - connection_show_message(connection, _("Successfully got download")); - return connection->err; - break; + switch (connection->err) + { + case FTPOK: + connection_show_message(connection, _("Successfully got download")); + return connection->err; + break; - default: - connection_show_message(connection, - _("Error occured in connection...")); - break; - } - retrying_from_loop = TRUE; - } - while ((connection->attempts < connection->max_attempts) - || connection->max_attempts == 0); + default: + connection_show_message(connection, + _("Error occured in connection...")); + break; + } + retrying_from_loop = TRUE; + } while ((connection->attempts < connection->max_attempts) + || connection->max_attempts == 0); connection_show_message(connection, - _ - ("I have tried %d attempt(s) and have failed, aborting"), - connection->attempts); + _ + ("I have tried %d attempt(s) and have failed, aborting"), + connection->attempts); return connection->err; } /*Return true if it is a error which can be handled within the ftp_loop, -or false if it should be passed upwards so that the main download thread can -restart it when necessary after processing other threads status too */ + or false if it should be passed upwards so that the main download thread can + restart it when necessary after processing other threads status too */ boolean ftp_loop_handle_error(uerr_t err) { - proz_debug("Error encountered in ftp_loop is %d", err); if (err == FTPNSFOD || err == FTPLOGREFUSED || err == FTPCONREFUSED || err == FWRITEERR || err == FOPENERR || err == FTPCWDFAIL @@ -334,5 +336,4 @@ boolean ftp_loop_handle_error(uerr_t err) return FALSE; else return TRUE; - } diff --git a/libprozilla/src/ftp-retr.h b/libprozilla/src/ftp-retr.h index faef39f..9eaa523 100644 --- a/libprozilla/src/ftp-retr.h +++ b/libprozilla/src/ftp-retr.h @@ -1,23 +1,23 @@ /****************************************************************************** - libprozilla - a download accelerator library - Copyright (C) 2001 Kalum Somaratna + libprozilla - a download accelerator library + Copyright (C) 2001 Kalum Somaratna - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ******************************************************************************/ -/* $Id: ftp-retr.h,v 1.6 2001/06/25 12:30:55 kalum Exp $ */ +/* $Id$ */ #ifndef FTP_RETR_H #define FTP_RETR_H @@ -29,12 +29,12 @@ extern "C" { #endif - uerr_t proz_ftp_get_file(connection_t * connection); - uerr_t ftp_loop(connection_t * connection); - boolean ftp_loop_handle_error(uerr_t err); - uerr_t ftp_get_file_from_http_proxy(connection_t * connection); +uerr_t proz_ftp_get_file(connection_t * connection); +uerr_t ftp_loop(connection_t * connection); +boolean ftp_loop_handle_error(uerr_t err); +uerr_t ftp_get_file_from_http_proxy(connection_t * connection); #ifdef __cplusplus } #endif -#endif /* FTP_RETR_H */ +#endif /* FTP_RETR_H */ diff --git a/libprozilla/src/ftp.c b/libprozilla/src/ftp.c index 555e44f..a6503dd 100644 --- a/libprozilla/src/ftp.c +++ b/libprozilla/src/ftp.c @@ -1,25 +1,25 @@ /****************************************************************************** - libprozilla - a download accelerator library - Copyright (C) 2001 Kalum Somaratna + libprozilla - a download accelerator library + Copyright (C) 2001 Kalum Somaratna - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ******************************************************************************/ /* FTP support. */ -/* $Id: ftp.c,v 1.55 2005/09/04 00:06:50 kalum Exp $ */ +/* $Id$ */ #include "common.h" @@ -38,19 +38,20 @@ /****************************************************************************** - Return the numeric response of the FTP server by reading the first three - characters in the buffer. + Return the numeric response of the FTP server by reading the first three + characters in the buffer. ******************************************************************************/ static int ftp_get_return(const char *ftp_buffer) { char code[4]; + strncpy(code, ftp_buffer, 3); code[3] = '\0'; return atoi(code); } /****************************************************************************** - ... + ... ******************************************************************************/ static uerr_t ftp_get_reply(connection_t * connection) { @@ -62,7 +63,7 @@ static uerr_t ftp_get_reply(connection_t * connection) /* Allocate the space in the buffer for the request. */ char szBuffer[FTP_BUFFER_SIZE]; - char *strtok_saveptr;// = (char *) alloca(FTP_BUFFER_SIZE); + char *strtok_saveptr; // = (char *) alloca(FTP_BUFFER_SIZE); memset(szBuffer, 0, FTP_BUFFER_SIZE); @@ -82,7 +83,7 @@ static uerr_t ftp_get_reply(connection_t * connection) else cont = 0; - (void) strtok_r(szBuffer, "\r\n", &strtok_saveptr); + (void)strtok_r(szBuffer, "\r\n", &strtok_saveptr); srl = connection->serv_ret_lines = kmalloc(sizeof(response_line)); srl->line = kstrdup(szBuffer); @@ -91,65 +92,65 @@ static uerr_t ftp_get_reply(connection_t * connection) /* Add the first line to the struct. */ while (cont) - { - if (ftp_get_line(connection, szBuffer) != FTPOK) - return FTPERR; + { + if (ftp_get_line(connection, szBuffer) != FTPOK) + return FTPERR; - /* Server closed the connection. */ - if (*szBuffer == '\0') - return FTPERR; + /* Server closed the connection. */ + if (*szBuffer == '\0') + return FTPERR; - // proz_debug("Code %d",code); - if ((ftp_get_return(szBuffer) == code) && (szBuffer[3] == ' ')) - cont = 0; + // proz_debug("Code %d",code); + if ((ftp_get_return(szBuffer) == code) && (szBuffer[3] == ' ')) + cont = 0; - (void) strtok_r(szBuffer, "\r\n", &strtok_saveptr); - // proz_debug(_("Message = %s"), szBuffer); - srl->next = kmalloc(sizeof(response_line)); - srl = srl->next; - srl->line = kstrdup(szBuffer); - srl->next = 0; - } + (void)strtok_r(szBuffer, "\r\n", &strtok_saveptr); + // proz_debug(_("Message = %s"), szBuffer); + srl->next = kmalloc(sizeof(response_line)); + srl = srl->next; + srl->line = kstrdup(szBuffer); + srl->next = 0; + } return FTPOK; } /****************************************************************************** - ... + ... ******************************************************************************/ int ftp_check_msg(connection_t * connection, int len) { int ret; if ((ret = krecv(connection->ctrl_sock, connection->szBuffer, len, - MSG_PEEK, &connection->ctrl_timeout)) == -1) - { - proz_debug(_("Error checking for FTP data: %s"), strerror(errno)); - return ret; - } + MSG_PEEK, &connection->ctrl_timeout)) == -1) + { + proz_debug(_("Error checking for FTP data: %s"), strerror(errno)); + return ret; + } return ret; } /****************************************************************************** - ... + ... ******************************************************************************/ int ftp_read_msg(connection_t * connection, int len) { int ret; if ((ret = krecv(connection->ctrl_sock, connection->szBuffer, len, 0, - &connection->ctrl_timeout)) == -1) - { - proz_debug(_("Error receiving FTP data: %s"), strerror(errno)); - return ret; - } + &connection->ctrl_timeout)) == -1) + { + proz_debug(_("Error receiving FTP data: %s"), strerror(errno)); + return ret; + } return ret; } /****************************************************************************** - ... + ... ******************************************************************************/ uerr_t ftp_send_msg(connection_t * connection, const char *format, ...) { @@ -168,17 +169,17 @@ uerr_t ftp_send_msg(connection_t * connection, const char *format, ...) proz_debug(_("Sending: %s"), command); if ((ksend(connection->ctrl_sock, command, strlen(command), 0, - &connection->ctrl_timeout)) == -1) - { - proz_debug(_("Error sending FTP data: %s"), strerror(errno)); - return WRITEERR; - } + &connection->ctrl_timeout)) == -1) + { + proz_debug(_("Error sending FTP data: %s"), strerror(errno)); + return WRITEERR; + } return FTPOK; } /****************************************************************************** - ... + ... ******************************************************************************/ uerr_t ftp_get_line(connection_t * connection, char *line) { @@ -188,28 +189,28 @@ uerr_t ftp_get_line(connection_t * connection, char *line) connection->szBuffer = &ch; while ((iBuffLen < BUFFER_SIZE) - && ((ret = ftp_check_msg(connection, 1)) > 0)) - { - /* Now get the full string. */ - iLen = ftp_read_msg(connection, 1); + && ((ret = ftp_check_msg(connection, 1)) > 0)) + { + /* Now get the full string. */ + iLen = ftp_read_msg(connection, 1); - if (iLen != 1) - return FTPERR; + if (iLen != 1) + return FTPERR; - iBuffLen += iLen; - *szptr = ch; - szptr += iLen; + iBuffLen += iLen; + *szptr = ch; + szptr += iLen; - if (ch == '\n') - break; /* We have a line -> return. */ - } + if (ch == '\n') + break; /* We have a line -> return. */ + } /* Check for error returned in ftp_check_msg(). */ if (ret == -1) return FTPERR; /* if zero bytes were found that means the server has closed the connection*/ - if(ret==0) + if (ret == 0) *(szptr) = '\0'; else *(szptr + 1) = '\0'; @@ -220,7 +221,7 @@ uerr_t ftp_get_line(connection_t * connection, char *line) } /****************************************************************************** - ... + ... ******************************************************************************/ uerr_t ftp_ascii(connection_t * connection) { @@ -241,7 +242,7 @@ uerr_t ftp_ascii(connection_t * connection) } /****************************************************************************** - ... + ... ******************************************************************************/ uerr_t ftp_binary(connection_t * connection) { @@ -262,7 +263,7 @@ uerr_t ftp_binary(connection_t * connection) } /****************************************************************************** - ... + ... ******************************************************************************/ uerr_t ftp_port(connection_t * connection, const char *command) { @@ -283,7 +284,7 @@ uerr_t ftp_port(connection_t * connection, const char *command) } /****************************************************************************** - ... + ... ******************************************************************************/ uerr_t ftp_list(connection_t * connection, const char *file) { @@ -297,14 +298,14 @@ uerr_t ftp_list(connection_t * connection, const char *file) if (err != FTPOK) return err; - if(ftp_get_return(connection->serv_ret_lines->line)==550) + if (ftp_get_return(connection->serv_ret_lines->line) == 550) { return FTPNSFOD; } /*TODO Fix this up, any other return code with 5xx is a fatal error */ - if (connection->serv_ret_lines->line[0] == '5') + if (connection->serv_ret_lines->line[0] == '5') return FTPERR; if (connection->serv_ret_lines->line[0] != '1') @@ -314,7 +315,7 @@ uerr_t ftp_list(connection_t * connection, const char *file) } /****************************************************************************** - ... + ... ******************************************************************************/ uerr_t ftp_retr(connection_t * connection, const char *file) { @@ -338,7 +339,7 @@ uerr_t ftp_retr(connection_t * connection, const char *file) } /****************************************************************************** - ... + ... ******************************************************************************/ uerr_t ftp_pasv(connection_t * connection, unsigned char *addr) { @@ -361,31 +362,32 @@ uerr_t ftp_pasv(connection_t * connection, unsigned char *addr) return FTPNOPASV; /* Parse it. */ - p = (unsigned char *) connection->serv_ret_lines->line; - for (p += 4; *p && !isdigit(*p); p++); + p = (unsigned char *)connection->serv_ret_lines->line; + for (p += 4; *p && !isdigit(*p); p++) + ; if (!*p) return FTPINVPASV; for (i = 0; i < 6; i++) - { - addr[i] = 0; - for (; isdigit(*p); p++) - addr[i] = (*p - '0') + 10 * addr[i]; - - if (*p == ',') - p++; - else if (i < 5) { - return FTPINVPASV; + addr[i] = 0; + for (; isdigit(*p); p++) + addr[i] = (*p - '0') + 10 * addr[i]; + + if (*p == ',') + p++; + else if (i < 5) + { + return FTPINVPASV; + } } - } return FTPOK; } /****************************************************************************** - ... + ... ******************************************************************************/ uerr_t ftp_rest(connection_t * connection, off_t bytes) { @@ -406,7 +408,7 @@ uerr_t ftp_rest(connection_t * connection, off_t bytes) } /****************************************************************************** - ... + ... ******************************************************************************/ uerr_t ftp_cwd(connection_t * connection, const char *dir) { @@ -421,14 +423,14 @@ uerr_t ftp_cwd(connection_t * connection, const char *dir) return err; if (connection->serv_ret_lines->line[0] == '5') - { - /* Is it due to the file being not found? */ - if (strstr(connection->serv_ret_lines->line, "o such file") - || strstr(connection->serv_ret_lines->line, "o Such File") - || strstr(connection->serv_ret_lines->line, "ot found") - || strstr(connection->serv_ret_lines->line, "ot Found")) - return FTPNSFOD; - } + { + /* Is it due to the file being not found? */ + if (strstr(connection->serv_ret_lines->line, "o such file") + || strstr(connection->serv_ret_lines->line, "o Such File") + || strstr(connection->serv_ret_lines->line, "ot found") + || strstr(connection->serv_ret_lines->line, "ot Found")) + return FTPNSFOD; + } if (connection->serv_ret_lines->line[0] != '2') return FTPCWDFAIL; @@ -437,7 +439,7 @@ uerr_t ftp_cwd(connection_t * connection, const char *dir) } /****************************************************************************** - Returns the current working directory in dir. + Returns the current working directory in dir. ******************************************************************************/ uerr_t ftp_pwd(connection_t * connection, char *dir) { @@ -460,30 +462,31 @@ uerr_t ftp_pwd(connection_t * connection, char *dir) return FTPPWDFAIL; if ((r = strrchr(connection->serv_ret_lines->line, '"')) != NULL) - { - l = strchr(connection->serv_ret_lines->line, '"'); - if ((l != NULL) && (l != r)) { - *r = '\0'; - ++l; - strcpy(dir, l); - *r = '"'; + l = strchr(connection->serv_ret_lines->line, '"'); + if ((l != NULL) && (l != r)) + { + *r = '\0'; + ++l; + strcpy(dir, l); + *r = '"'; + } } - } else - { - if ((r = strchr(connection->serv_ret_lines->line, ' ')) != NULL) + else { - *r = '\0'; - strcpy(dir, szBuffer); - *r = ' '; + if ((r = strchr(connection->serv_ret_lines->line, ' ')) != NULL) + { + *r = '\0'; + strcpy(dir, szBuffer); + *r = ' '; + } } - } return FTPOK; } /****************************************************************************** - Returns the size of the file in size, on error size will be -1. + Returns the size of the file in size, on error size will be -1. ******************************************************************************/ uerr_t ftp_size(connection_t * connection, const char *file, off_t *size) { @@ -501,37 +504,37 @@ uerr_t ftp_size(connection_t * connection, const char *file, off_t *size) /* Now lets figure out what happened. */ if (connection->serv_ret_lines->line[0] == '2') - { - sscanf(connection->serv_ret_lines->line + 3, "%lld", size); - return FTPOK; - } else if (connection->serv_ret_lines->line[0] == '5') /* An error occured. */ - { - - if(ftp_get_return(connection->serv_ret_lines->line)==550) { - return FTPNSFOD; + sscanf(connection->serv_ret_lines->line + 3, "%lld", size); + return FTPOK; + } + else if (connection->serv_ret_lines->line[0] == '5') /* An error occured. */ + { + if (ftp_get_return(connection->serv_ret_lines->line) == 550) + { + return FTPNSFOD; + } + /* Is it due to the file being not found? */ + if (strstr(connection->serv_ret_lines->line, "o such file") + || strstr(connection->serv_ret_lines->line, "o Such File") + || strstr(connection->serv_ret_lines->line, "ot found") + || strstr(connection->serv_ret_lines->line, "ot Found")) + return FTPNSFOD; } - /* Is it due to the file being not found? */ - if (strstr(connection->serv_ret_lines->line, "o such file") - || strstr(connection->serv_ret_lines->line, "o Such File") - || strstr(connection->serv_ret_lines->line, "ot found") - || strstr(connection->serv_ret_lines->line, "ot Found")) - return FTPNSFOD; - } return FTPSIZEFAIL; } /****************************************************************************** - Connect to the given FTP server. + Connect to the given FTP server. ******************************************************************************/ uerr_t ftp_connect_to_server(connection_t * connection, const char *name, - int port) + int port) { uerr_t err; err = connect_to_server(&(connection->ctrl_sock), name, port, - &connection->conn_timeout); + &connection->conn_timeout); if (err != NOCONERROR) return err; @@ -546,8 +549,8 @@ uerr_t ftp_connect_to_server(connection_t * connection, const char *name, } /****************************************************************************** - This function will call bind() to return a bound socket then the FTP server - will be connected with a port request and asked to connect. + This function will call bind() to return a bound socket then the FTP server + will be connected with a port request and asked to connect. ******************************************************************************/ uerr_t ftp_get_listen_socket(connection_t * connection, int *listen_sock) { @@ -565,34 +568,34 @@ uerr_t ftp_get_listen_socket(connection_t * connection, int *listen_sock) return LISTENERR; len = sizeof(serv_addr); - if (getsockname(sockfd, (struct sockaddr *) &serv_addr, &len) < 0) - { - perror("getsockname"); - close(sockfd); - return CONPORTERR; - } + if (getsockname(sockfd, (struct sockaddr *)&serv_addr, &len) < 0) + { + perror("getsockname"); + close(sockfd); + return CONPORTERR; + } /* Get hosts info. */ len = sizeof(TempAddr); - if (getsockname(connection->ctrl_sock, (struct sockaddr *) &TempAddr, - &len) < 0) - { - perror("getsockname"); - close(sockfd); - return CONPORTERR; - } + if (getsockname(connection->ctrl_sock, (struct sockaddr *)&TempAddr, + &len) < 0) + { + perror("getsockname"); + close(sockfd); + return CONPORTERR; + } - ipaddr = (char *) &TempAddr.sin_addr; + ipaddr = (char *)&TempAddr.sin_addr; - port = (char *) &serv_addr.sin_port; + port = (char *)&serv_addr.sin_port; -#define UC(b) (((int)b)&0xff) +#define UC(b) (((int)b) & 0xff) sprintf(command, "PORT %d,%d,%d,%d,%d,%d\r\n", UC(ipaddr[0]), - UC(ipaddr[1]), UC(ipaddr[2]), UC(ipaddr[3]), UC(port[0]), - UC(port[1])); + UC(ipaddr[1]), UC(ipaddr[2]), UC(ipaddr[3]), UC(port[0]), + UC(port[1])); err = ftp_port(connection, command); if (err != FTPOK) @@ -604,183 +607,197 @@ uerr_t ftp_get_listen_socket(connection_t * connection, int *listen_sock) } /****************************************************************************** - ... + ... ******************************************************************************/ uerr_t ftp_login(connection_t * connection, const char *username, - const char *passwd) + const char *passwd) { uerr_t err = FTPERR; int ret_code = 220; boolean logged_in = FALSE; while (1) - { - switch (ret_code) { - case 220: - /* IDEA: Lets add the proxy support here. */ + switch (ret_code) + { + case 220: + /* IDEA: Lets add the proxy support here. */ - if (!ftp_use_proxy(connection)) - { - /* No proxy just direct connection. */ - err = ftp_send_msg(connection, "USER %s\r\n", username); - } else - { - switch (connection->ftp_proxy->type) - { - case USERatSITE: - err = ftp_send_msg(connection, "USER %s@%s:%d\r\n", username, - connection->u.host, connection->u.port); - break; - case USERatPROXYUSERatSITE: - err = ftp_send_msg(connection, "USER %s@%s@%s:%d\r\n", username, - connection->ftp_proxy->username, - connection->u.host, connection->u.port); - break; - case USERatSITE_PROXYUSER: - err = ftp_send_msg(connection, "USER %s:%d@%s %s\r\n", username, - connection->u.host, connection->u.port, - connection->ftp_proxy->username); - break; - case PROXYUSERatSITE: - err = ftp_send_msg(connection, "USER %s@%s:%d\r\n", - connection->ftp_proxy->username, - connection->u.host, connection->u.port); - break; - default: - /* Something else, just send PROXY USER. */ - err = ftp_send_msg(connection, "USER %s\r\n", - connection->ftp_proxy->username); - break; - } - } + if (!ftp_use_proxy(connection)) + { + /* No proxy just direct connection. */ + err = ftp_send_msg(connection, "USER %s\r\n", username); + } + else + { + switch (connection->ftp_proxy->type) + { + case USERatSITE: + err = ftp_send_msg(connection, "USER %s@%s:%d\r\n", username, + connection->u.host, connection->u.port); + break; - if (err != FTPOK) - return err; + case USERatPROXYUSERatSITE: + err = ftp_send_msg(connection, "USER %s@%s@%s:%d\r\n", username, + connection->ftp_proxy->username, + connection->u.host, connection->u.port); + break; - err = ftp_get_reply(connection); - if (err != FTPOK) - return err; + case USERatSITE_PROXYUSER: + err = ftp_send_msg(connection, "USER %s:%d@%s %s\r\n", username, + connection->u.host, connection->u.port, + connection->ftp_proxy->username); + break; - break; + case PROXYUSERatSITE: + err = ftp_send_msg(connection, "USER %s@%s:%d\r\n", + connection->ftp_proxy->username, + connection->u.host, connection->u.port); + break; + + default: + /* Something else, just send PROXY USER. */ + err = ftp_send_msg(connection, "USER %s\r\n", + connection->ftp_proxy->username); + break; + } + } + + if (err != FTPOK) + return err; + + err = ftp_get_reply(connection); + if (err != FTPOK) + return err; + + break; - case 230: /* Fallthrough. */ - case 231: /* Fallthrough. */ - case 202: + case 230: /* Fallthrough. */ + case 231: /* Fallthrough. */ + case 202: - logged_in = TRUE; + logged_in = TRUE; - if (!ftp_use_proxy(connection)) - return FTPOK; /* Logged in succesfully. */ + if (!ftp_use_proxy(connection)) + return FTPOK; /* Logged in succesfully. */ - switch (connection->ftp_proxy->type) - { - case LOGINthenUSERatSITE: - err = ftp_send_msg(connection, "USER %s@%s:%d\r\n", username, - connection->u.host, connection->u.port); - break; - case OPENSITE: - err = - ftp_send_msg(connection, "OPEN %s:%d\r\n", connection->u.host, - connection->u.port); - break; - case SITESITE: - err = - ftp_send_msg(connection, "SITE %s:%d\r\n", connection->u.host, - connection->u.port); - break; - case PROXYUSERatSITE: - err = ftp_send_msg(connection, "USER %s\r\n", username); - break; - default: - /* TODO What is the default here? */ - return FTPOK; - break; - } + switch (connection->ftp_proxy->type) + { + case LOGINthenUSERatSITE: + err = ftp_send_msg(connection, "USER %s@%s:%d\r\n", username, + connection->u.host, connection->u.port); + break; - if (err != FTPOK) - return err; + case OPENSITE: + err = + ftp_send_msg(connection, "OPEN %s:%d\r\n", connection->u.host, + connection->u.port); + break; - err = ftp_get_reply(connection); - if (err != FTPOK) - return err; + case SITESITE: + err = + ftp_send_msg(connection, "SITE %s:%d\r\n", connection->u.host, + connection->u.port); + break; - break; + case PROXYUSERatSITE: + err = ftp_send_msg(connection, "USER %s\r\n", username); + break; + + default: + /* TODO What is the default here? */ + return FTPOK; + break; + } + + if (err != FTPOK) + return err; + + err = ftp_get_reply(connection); + if (err != FTPOK) + return err; + + break; - /* Handle 421 services not available. */ - case 421: - return FTPSERVCLOSEDATLOGIN; - break; + /* Handle 421 services not available. */ + case 421: + return FTPSERVCLOSEDATLOGIN; + break; - /* User name is all right, need password. */ - case 331: - if (!ftp_use_proxy(connection)) - { - /* No proxy just direct connection. */ - err = ftp_send_msg(connection, "PASS %s\r\n", passwd); - } else - { - switch (connection->ftp_proxy->type) - { - case USERatSITE: - err = ftp_send_msg(connection, "PASS %s\r\n", passwd); - break; - case USERatPROXYUSERatSITE: - err = ftp_send_msg(connection, "PASS %s@%s\r\n", passwd, - connection->ftp_proxy->passwd); - break; - case USERatSITE_PROXYUSER: - err = ftp_send_msg(connection, "PASS %s\r\n", passwd); - break; - case PROXYUSERatSITE: - err = ftp_send_msg(connection, "PASS %s\r\n", - connection->ftp_proxy->passwd); - break; - default: - /* Something else we dont know about. */ - err = ftp_send_msg(connection, "PASS %s\r\n", - connection->ftp_proxy->passwd); - break; - } - } + /* User name is all right, need password. */ + case 331: + if (!ftp_use_proxy(connection)) + { + /* No proxy just direct connection. */ + err = ftp_send_msg(connection, "PASS %s\r\n", passwd); + } + else + { + switch (connection->ftp_proxy->type) + { + case USERatSITE: + err = ftp_send_msg(connection, "PASS %s\r\n", passwd); + break; - if (err != FTPOK) - return err; + case USERatPROXYUSERatSITE: + err = ftp_send_msg(connection, "PASS %s@%s\r\n", passwd, + connection->ftp_proxy->passwd); + break; - err = ftp_get_reply(connection); - if (err != FTPOK) - return err; + case USERatSITE_PROXYUSER: + err = ftp_send_msg(connection, "PASS %s\r\n", passwd); + break; - break; + case PROXYUSERatSITE: + err = ftp_send_msg(connection, "PASS %s\r\n", + connection->ftp_proxy->passwd); + break; + + default: + /* Something else we dont know about. */ + err = ftp_send_msg(connection, "PASS %s\r\n", + connection->ftp_proxy->passwd); + break; + } + } + + if (err != FTPOK) + return err; + + err = ftp_get_reply(connection); + if (err != FTPOK) + return err; + + break; - /* 5xx series of commands indicate error. */ - case 530: - return FTPLOGREFUSED; - break; + /* 5xx series of commands indicate error. */ + case 530: + return FTPLOGREFUSED; + break; - case 501: /* Fallthrough. */ - case 503: /* Fallthrough. */ - case 550: - return FTPERR; - break; + case 501: /* Fallthrough. */ + case 503: /* Fallthrough. */ + case 550: + return FTPERR; + break; - default: - /* Unknown error code. */ - proz_debug(_("Unknown code %d retuned during FTP login"), ret_code); - return FTPERR; - break; + default: + /* Unknown error code. */ + proz_debug(_("Unknown code %d retuned during FTP login"), ret_code); + return FTPERR; + break; + } + + ret_code = ftp_get_return(connection->serv_ret_lines->line); + done_with_response(connection); } - ret_code = ftp_get_return(connection->serv_ret_lines->line); - done_with_response(connection); - } - if (err != FTPOK) return err; @@ -788,17 +805,17 @@ uerr_t ftp_login(connection_t * connection, const char *username, } /****************************************************************************** - ... + ... ******************************************************************************/ boolean ftp_use_proxy(connection_t * connection) { return (connection->ftp_proxy && connection->ftp_proxy->use_proxy && - connection->ftp_proxy->proxy_url.url) ? TRUE : FALSE; + connection->ftp_proxy->proxy_url.url) ? TRUE : FALSE; } /****************************************************************************** - Gets info about the url (connection->u) from the FTP server, and fills in - info like whether the server supports resume, the file size etc. + Gets info about the url (connection->u) from the FTP server, and fills in + info like whether the server supports resume, the file size etc. ******************************************************************************/ uerr_t proz_ftp_get_url_info(connection_t * connection) { @@ -809,57 +826,59 @@ uerr_t proz_ftp_get_url_info(connection_t * connection) longstring buffer; boolean size_ok; struct ftpparse fp; + /* if we have to use a HTTP proxy call the routine which is defined in http.c and just return. */ if (ftp_use_proxy(connection) && connection->ftp_proxy->type == HTTPPROXY) - { - err = ftp_get_url_info_from_http_proxy(connection); - return err; - } + { + err = ftp_get_url_info_from_http_proxy(connection); + return err; + } init_response(connection); if (ftp_use_proxy(connection)) - { - connection_show_message(connection, _("Connecting to %s"), - connection->ftp_proxy->proxy_url.host); - - /* Connect to the proxy server here. */ - err = ftp_connect_to_server(connection, - connection->ftp_proxy->proxy_url.host, - connection->ftp_proxy->proxy_url.port); - - if (err != FTPOK) { - connection_show_message(connection, - _("Error while connecting to %s"), - connection->ftp_proxy->proxy_url.host); - return err; + connection_show_message(connection, _("Connecting to %s"), + connection->ftp_proxy->proxy_url.host); + + /* Connect to the proxy server here. */ + err = ftp_connect_to_server(connection, + connection->ftp_proxy->proxy_url.host, + connection->ftp_proxy->proxy_url.port); + + if (err != FTPOK) + { + connection_show_message(connection, + _("Error while connecting to %s"), + connection->ftp_proxy->proxy_url.host); + return err; + } + + connection_show_message(connection, _("Connected to %s"), + connection->ftp_proxy->proxy_url.host); } - - connection_show_message(connection, _("Connected to %s"), - connection->ftp_proxy->proxy_url.host); - } else - { - connection_show_message(connection, _("Connecting to %s"), - connection->u.host); - - err = ftp_connect_to_server(connection, connection->u.host, - connection->u.port); - - if (err != FTPOK) + else { - connection_show_message(connection, - _("Error while connecting to %s"), - connection->u.host);; - return err; + connection_show_message(connection, _("Connecting to %s"), + connection->u.host); + + err = ftp_connect_to_server(connection, connection->u.host, + connection->u.port); + + if (err != FTPOK) + { + connection_show_message(connection, + _("Error while connecting to %s"), + connection->u.host);; + return err; + } + connection_show_message(connection, _("Connected to %s"), + connection->u.host); } - connection_show_message(connection, _("Connected to %s"), - connection->u.host); - } @@ -870,42 +889,42 @@ uerr_t proz_ftp_get_url_info(connection_t * connection) /* Use .netrc if asked to do so. */ if (connection->use_netrc == TRUE) - { - netrc_ent = search_netrc(libprozrtinfo.netrc_list, connection->u.host); - - if (netrc_ent != NULL) { - user = netrc_ent->account; - passwd = netrc_ent->password; + netrc_ent = search_netrc(libprozrtinfo.netrc_list, connection->u.host); + + if (netrc_ent != NULL) + { + user = netrc_ent->account; + passwd = netrc_ent->password; + } } - } user = user ? user : libprozrtinfo.ftp_default_user; passwd = passwd ? passwd : libprozrtinfo.ftp_default_passwd; if (strcmp(user, "anonymous") == 0) connection_show_message(connection, - _("Logging in as user %s with password %s"), - user, passwd); + _("Logging in as user %s with password %s"), + user, passwd); else - { - int pwd_len = strlen(passwd); - char *tmp_pwd = (char *) kmalloc(pwd_len + 1); - memset(tmp_pwd, 'x', pwd_len); - tmp_pwd[pwd_len] = 0; - connection_show_message(connection, - _("Logging in as user %s with password %s"), - user, tmp_pwd); - kfree(tmp_pwd); - } + { + int pwd_len = strlen(passwd); + char *tmp_pwd = (char *)kmalloc(pwd_len + 1); + memset(tmp_pwd, 'x', pwd_len); + tmp_pwd[pwd_len] = 0; + connection_show_message(connection, + _("Logging in as user %s with password %s"), + user, tmp_pwd); + kfree(tmp_pwd); + } init_response(connection); err = ftp_login(connection, user, passwd); if (err != FTPOK) - { - close_sock(&connection->ctrl_sock); - return err; - } + { + close_sock(&connection->ctrl_sock); + return err; + } done_with_response(connection); connection_show_message(connection, _("Logged in successfully")); @@ -913,118 +932,123 @@ uerr_t proz_ftp_get_url_info(connection_t * connection) init_response(connection); err = ftp_binary(connection); if (err != FTPOK) - { - close_sock(&connection->ctrl_sock); - return err; - } + { + close_sock(&connection->ctrl_sock); + return err; + } done_with_response(connection); /* Do we need to CWD? */ if (*connection->u.dir) - { - init_response(connection); + { + init_response(connection); - err = ftp_cwd(connection, connection->u.dir); - if (err != FTPOK) - { - connection_show_message(connection, - _("CWD failed to change to directory '%s'"), - connection->u.dir); - close_sock(&connection->ctrl_sock); - return err; - } else - { - done_with_response(connection); + err = ftp_cwd(connection, connection->u.dir); + if (err != FTPOK) + { + connection_show_message(connection, + _("CWD failed to change to directory '%s'"), + connection->u.dir); + close_sock(&connection->ctrl_sock); + return err; + } + else + { + done_with_response(connection); + } } - } else + else connection_show_message(connection, _("CWD not needed")); init_response(connection); err = ftp_rest(connection, 0); if (err != FTPOK) - { - connection->resume_support = FALSE; - connection_show_message(connection, _("REST failed")); - /* NOTE: removed return err; */ - } else - { - connection->resume_support = TRUE; - connection_show_message(connection, _("REST ok")); - } + { + connection->resume_support = FALSE; + connection_show_message(connection, _("REST failed")); + /* NOTE: removed return err; */ + } + else + { + connection->resume_support = TRUE; + connection_show_message(connection, _("REST ok")); + } done_with_response(connection); /* Lets see whether the URL really is a file. */ init_response(connection); err = ftp_cwd(connection, connection->u.file); if (err == FTPOK) - { - /* So connection->u.file is a directory and not a file. */ - connection->file_type = DIRECTORY; - return FTPOK; - } else - { - - /* FIXME: The statement below is strictly not true, it could be a symlink - but for the moment lets leave this as it is, later we will perform a - LIST command and detect whether it is a symlink. */ - connection->file_type = REGULAR_FILE; - } + { + /* So connection->u.file is a directory and not a file. */ + connection->file_type = DIRECTORY; + return FTPOK; + } + else + { + /* FIXME: The statement below is strictly not true, it could be a symlink + but for the moment lets leave this as it is, later we will perform a + LIST command and detect whether it is a symlink. */ + connection->file_type = REGULAR_FILE; + } done_with_response(connection); init_response(connection); err = - ftp_size(connection, connection->u.file, - &connection->main_file_size); + ftp_size(connection, connection->u.file, + &connection->main_file_size); + - /* if ((err == FTPOK) || (err == FTPNSFOD) || (err != FTPSIZEFAIL)) */ /* { */ /* close_sock(&connection->ctrl_sock); */ /* return err; */ /* } */ - switch (err) + switch (err) { case FTPNSFOD: - { - close_sock(&connection->ctrl_sock); - return err; - } - - case FTPOK: - size_ok=TRUE; - break; - case FTPSIZEFAIL: - size_ok=FALSE; - break; - default: - size_ok=FALSE; - } + { + close_sock(&connection->ctrl_sock); + return err; + } - - done_with_response(connection); + case FTPOK: + size_ok = TRUE; + break; + + case FTPSIZEFAIL: + size_ok = FALSE; + break; + + default: + size_ok = FALSE; + } + + + done_with_response(connection); /* Now we additionaly will get the server to display info with the list command, initially we only called the LIST command only if the SIZE failed -*/ + */ err = ftp_setup_data_sock_1(connection, &passive_mode); if (err != FTPOK) - { - close_sock(&connection->ctrl_sock); - return err; - } + { + close_sock(&connection->ctrl_sock); + return err; + } init_response(connection); err = ftp_ascii(connection); if (err != FTPOK) - { - close_sock(&connection->ctrl_sock); - return err; - } + { + close_sock(&connection->ctrl_sock); + return err; + } done_with_response(connection); @@ -1033,153 +1057,157 @@ uerr_t proz_ftp_get_url_info(connection_t * connection) err = ftp_list(connection, connection->u.file); if (err != FTPOK) { - if(err==FTPNSFOD) - { - //If the remote server returns ftpnsfod which could be due - //to the fact that the server doesnt permit the directory - //contents to be listed we will print a warning and return - //FTPOK as it is not a fatal error. - connection_show_message(connection, - _("FTP LIST failed: File not found or access not permitted.")); - close_sock(&connection->ctrl_sock); - return FTPOK; - } + if (err == FTPNSFOD) + { + //If the remote server returns ftpnsfod which could be due + //to the fact that the server doesnt permit the directory + //contents to be listed we will print a warning and return + //FTPOK as it is not a fatal error. + connection_show_message(connection, + _("FTP LIST failed: File not found or access not permitted.")); + close_sock(&connection->ctrl_sock); + return FTPOK; + } else - { - connection_show_message(connection, - "FTP LIST failed: Server returned %s",connection->serv_ret_lines->line ); - close_sock(&connection->ctrl_sock); - return FTPOK; - } + { + connection_show_message(connection, + "FTP LIST failed: Server returned %s", connection->serv_ret_lines->line); + close_sock(&connection->ctrl_sock); + return FTPOK; + } } done_with_response(connection); err = ftp_setup_data_sock_2(connection, &passive_mode); if (err != FTPOK) - { - close_sock(&connection->ctrl_sock); - return err; - } + { + close_sock(&connection->ctrl_sock); + return err; + } /* Now read the data to the buffer. */ /* TODO Create a buffer which dynamically resizes itself as we add data. */ if (krecv(connection->data_sock, buffer, sizeof(buffer), 0, - &connection->xfer_timeout) == -1) - { - connection_show_message(connection, - _("Error receiving FTP transfer data: %s"), - strerror(errno)); - return FTPERR; - } + &connection->xfer_timeout) == -1) + { + connection_show_message(connection, + _("Error receiving FTP transfer data: %s"), + strerror(errno)); + return FTPERR; + } proz_debug(_("String received after the LIST command = %s"), buffer); while ((tmp = strrchr(buffer, '\n')) || (tmp = strrchr(buffer, '\r'))) - { - *tmp = 0; - }; + { + *tmp = 0; + } + ; close_sock(&connection->data_sock); close_sock(&connection->ctrl_sock); // size_rt = size_returner(buffer, strlen(buffer)); - err =ftp_parse(&fp, buffer, strlen(buffer)); + err = ftp_parse(&fp, buffer, strlen(buffer)); if (err != FTPPARSEOK) - { - connection_show_message(connection, - _ - ("Unable to parse the line the FTP server returned:please report URL to prozilla@genesys.ro ")); - } - - if(err==FTPPARSEOK) { - proz_debug("size returned from LIST %ld",fp.filesize); + connection_show_message(connection, + _ + ("Unable to parse the line the FTP server returned:please report URL to prozilla@genesys.ro ")); + } + + if (err == FTPPARSEOK) + { + proz_debug("size returned from LIST %ld", fp.filesize); //SEC size_rt off_t? - if(size_ok==FALSE) - { - proz_debug("SIZE failed, setting file size based on LIST"); - connection->main_file_size = fp.filesize; - } + if (size_ok == FALSE) + { + proz_debug("SIZE failed, setting file size based on LIST"); + connection->main_file_size = fp.filesize; + } } return FTPOK; } /****************************************************************************** - This will be the first step in setting up a data sock, it will try - PASV or PORT. + This will be the first step in setting up a data sock, it will try + PASV or PORT. ******************************************************************************/ uerr_t ftp_setup_data_sock_1(connection_t * connection, - boolean * passive_mode) + boolean * passive_mode) { uerr_t err; /* If enabled lets try PASV. */ if (connection->ftp_use_pasv == TRUE) - { - init_response(connection); - err = ftp_pasv(connection, connection->pasv_addr); - - /* If the error is due to the server not supporting PASV then set the - flag and lets try PORT. */ - if ((err == FTPNOPASV) || (err == FTPINVPASV)) { - proz_debug(_("Server doesn't seem to support PASV")); - *passive_mode = FALSE; - } else if (err == FTPOK) /* Server supports PASV. */ - { - char dhost[256]; - unsigned short dport; + init_response(connection); + err = ftp_pasv(connection, connection->pasv_addr); - sprintf(dhost, "%d.%d.%d.%d", connection->pasv_addr[0], - connection->pasv_addr[1], connection->pasv_addr[2], - connection->pasv_addr[3]); + /* If the error is due to the server not supporting PASV then set the + flag and lets try PORT. */ + if ((err == FTPNOPASV) || (err == FTPINVPASV)) + { + proz_debug(_("Server doesn't seem to support PASV")); + *passive_mode = FALSE; + } + else if (err == FTPOK) /* Server supports PASV. */ + { + char dhost[256]; + unsigned short dport; - dport = (connection->pasv_addr[4] << 8) + connection->pasv_addr[5]; + sprintf(dhost, "%d.%d.%d.%d", connection->pasv_addr[0], + connection->pasv_addr[1], connection->pasv_addr[2], + connection->pasv_addr[3]); - err = connect_to_server(&connection->data_sock, dhost, dport, - &connection->xfer_timeout); - if (err != NOCONERROR) - return err; + dport = (connection->pasv_addr[4] << 8) + connection->pasv_addr[5]; - /* Everything seems to be ok. */ - *passive_mode = TRUE; - } else - return err; + err = connect_to_server(&connection->data_sock, dhost, dport, + &connection->xfer_timeout); + if (err != NOCONERROR) + return err; - done_with_response(connection); - } else - *passive_mode = FALSE; /* Ok... Since PASV is not to be used. */ + /* Everything seems to be ok. */ + *passive_mode = TRUE; + } + else + return err; + + done_with_response(connection); + } + else + *passive_mode = FALSE; /* Ok... Since PASV is not to be used. */ if (*passive_mode == FALSE) - { - /* Obtain a listen socket. */ - err = ftp_get_listen_socket(connection, &connection->listen_sock); - if (err != FTPOK) - return err; - } + { + /* Obtain a listen socket. */ + err = ftp_get_listen_socket(connection, &connection->listen_sock); + if (err != FTPOK) + return err; + } return FTPOK; } /****************************************************************************** - This will be the second step in setting up a data sock, if passive mode is - FALSE, it will call accept_connection(). + This will be the second step in setting up a data sock, if passive mode is + FALSE, it will call accept_connection(). ******************************************************************************/ uerr_t ftp_setup_data_sock_2(connection_t * connection, - boolean * passive_mode) + boolean * passive_mode) { uerr_t err; - if (*passive_mode == FALSE) /* We have to accept the connection. */ - { - err = - accept_connection(connection->listen_sock, &connection->data_sock); - if (err != ACCEPTOK) - return err; - } + if (*passive_mode == FALSE) /* We have to accept the connection. */ + { + err = + accept_connection(connection->listen_sock, &connection->data_sock); + if (err != ACCEPTOK) + return err; + } return FTPOK; } @@ -1197,59 +1225,55 @@ uerr_t ftp_get_url_info_loop(connection_t * connection) do - { - if (connection->attempts > 0 && connection->err != NEWLOCATION) { + if (connection->attempts > 0 && connection->err != NEWLOCATION) + { + connection_show_message(connection, + _("Retrying attempt %d in %d seconds"), + connection->attempts, + connection->retry_delay.tv_sec); + delay_ms(connection->retry_delay.tv_sec * 1000); + } - connection_show_message(connection, - _("Retrying attempt %d in %d seconds"), - connection->attempts, - connection->retry_delay.tv_sec); - delay_ms(connection->retry_delay.tv_sec * 1000); + /*Push the handler which will cleanup any sockets that are left open */ + pthread_cleanup_push(cleanup_socks, (void *)connection); - } + connection->err = proz_ftp_get_url_info(connection); + /*pop the handler */ + pthread_cleanup_pop(0); - /*Push the handler which will cleanup any sockets that are left open */ - pthread_cleanup_push(cleanup_socks, (void *) connection); + connection->attempts++; - connection->err = proz_ftp_get_url_info(connection); - /*pop the handler */ - pthread_cleanup_pop(0); + switch (connection->err) + { + case FTPOK: + connection_show_message(connection, _("Successfully got info")); + pthread_mutex_lock(&connection->access_mutex); + connection->running = FALSE; + pthread_mutex_unlock(&connection->access_mutex); + return connection->err; + break; - connection->attempts++; + case FTPNSFOD: + connection_show_message(connection, _("File not found!")); + pthread_mutex_lock(&connection->access_mutex); + connection->running = FALSE; + pthread_mutex_unlock(&connection->access_mutex); + return connection->err; + break; - switch (connection->err) - { - case FTPOK: - connection_show_message(connection, _("Successfully got info")); - pthread_mutex_lock(&connection->access_mutex); - connection->running = FALSE; - pthread_mutex_unlock(&connection->access_mutex); - return connection->err; - break; - - case FTPNSFOD: - connection_show_message(connection, _("File not found!")); - pthread_mutex_lock(&connection->access_mutex); - connection->running = FALSE; - pthread_mutex_unlock(&connection->access_mutex); - return connection->err; - break; - - default: - connection_show_message(connection, proz_strerror(connection->err)); - break; - } - - } - while ((connection->attempts < connection->max_attempts) - || connection->max_attempts == 0); + default: + connection_show_message(connection, proz_strerror(connection->err)); + break; + } + } while ((connection->attempts < connection->max_attempts) + || connection->max_attempts == 0); connection_show_message(connection, - _ - ("I have tried %d attempt(s) and have failed, aborting"), - connection->attempts); + _ + ("I have tried %d attempt(s) and have failed, aborting"), + connection->attempts); pthread_mutex_lock(&connection->access_mutex); connection->running = FALSE; pthread_mutex_unlock(&connection->access_mutex); diff --git a/libprozilla/src/ftp.h b/libprozilla/src/ftp.h index 40d3f46..bc6dff7 100644 --- a/libprozilla/src/ftp.h +++ b/libprozilla/src/ftp.h @@ -1,25 +1,25 @@ /****************************************************************************** - libprozilla - a download accelerator library - Copyright (C) 2001 Kalum Somaratna + libprozilla - a download accelerator library + Copyright (C) 2001 Kalum Somaratna - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ******************************************************************************/ /* FTP support. */ -/* $Id: ftp.h,v 1.35 2005/09/04 00:06:50 kalum Exp $ */ +/* $Id$ */ #ifndef FTP_H @@ -34,44 +34,44 @@ extern "C" { #endif - int ftp_check_msg(connection_t * connection, int len); - int ftp_read_msg(connection_t * connection, int len); - uerr_t ftp_send_msg(connection_t * connection, const char *format, ...); +int ftp_check_msg(connection_t * connection, int len); +int ftp_read_msg(connection_t * connection, int len); +uerr_t ftp_send_msg(connection_t * connection, const char *format, ...); - uerr_t ftp_get_line(connection_t * connection, char *line); +uerr_t ftp_get_line(connection_t * connection, char *line); - uerr_t ftp_ascii(connection_t * connection); - uerr_t ftp_binary(connection_t * connection); - uerr_t ftp_port(connection_t * connection, const char *command); - uerr_t ftp_list(connection_t * connection, const char *file); - uerr_t ftp_retr(connection_t * connection, const char *file); - uerr_t ftp_pasv(connection_t * connection, unsigned char *addr); - uerr_t ftp_rest(connection_t * connection, off_t bytes); - uerr_t ftp_cwd(connection_t * connection, const char *dir); - uerr_t ftp_pwd(connection_t * connection, char *dir); - uerr_t ftp_size(connection_t * connection, const char *file, off_t *size); +uerr_t ftp_ascii(connection_t * connection); +uerr_t ftp_binary(connection_t * connection); +uerr_t ftp_port(connection_t * connection, const char *command); +uerr_t ftp_list(connection_t * connection, const char *file); +uerr_t ftp_retr(connection_t * connection, const char *file); +uerr_t ftp_pasv(connection_t * connection, unsigned char *addr); +uerr_t ftp_rest(connection_t * connection, off_t bytes); +uerr_t ftp_cwd(connection_t * connection, const char *dir); +uerr_t ftp_pwd(connection_t * connection, char *dir); +uerr_t ftp_size(connection_t * connection, const char *file, off_t *size); - uerr_t ftp_connect_to_server(connection_t * connection, const char *name, - int port); +uerr_t ftp_connect_to_server(connection_t * connection, const char *name, + int port); - uerr_t ftp_get_listen_socket(connection_t * connection, - int *listen_sock); +uerr_t ftp_get_listen_socket(connection_t * connection, + int *listen_sock); - uerr_t ftp_login(connection_t * connection, const char *username, - const char *passwd); +uerr_t ftp_login(connection_t * connection, const char *username, + const char *passwd); - boolean ftp_use_proxy(connection_t * connection); +boolean ftp_use_proxy(connection_t * connection); - uerr_t proz_ftp_get_url_info(connection_t * connection); +uerr_t proz_ftp_get_url_info(connection_t * connection); - uerr_t ftp_setup_data_sock_1(connection_t * connection, - boolean * passive_mode); - uerr_t ftp_setup_data_sock_2(connection_t * connection, - boolean * passive_mode); - uerr_t ftp_get_url_info_loop(connection_t * connection); +uerr_t ftp_setup_data_sock_1(connection_t * connection, + boolean * passive_mode); +uerr_t ftp_setup_data_sock_2(connection_t * connection, + boolean * passive_mode); +uerr_t ftp_get_url_info_loop(connection_t * connection); - uerr_t ftp_get_url_info_from_http_proxy(connection_t * connection); +uerr_t ftp_get_url_info_from_http_proxy(connection_t * connection); #ifdef __cplusplus } #endif -#endif /* FTP_H */ +#endif /* FTP_H */ diff --git a/libprozilla/src/ftpparse.c b/libprozilla/src/ftpparse.c index 13ab85c..fa1f17a 100644 --- a/libprozilla/src/ftpparse.c +++ b/libprozilla/src/ftpparse.c @@ -1,20 +1,20 @@ /****************************************************************************** - libprozilla - a download accelerator library - Copyright (C) 2001 Kalum Somaratna + libprozilla - a download accelerator library + Copyright (C) 2001 Kalum Somaratna - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ******************************************************************************/ //Rewrite of the ftp parsing code as we needed to extract the date of @@ -26,24 +26,25 @@ #include "prozilla.h" - - - /* MultiNet (some spaces removed from examples) */ - /* "00README.TXT;1 2 30-DEC-1996 17:44 [SYSTEM] (RWED,RWED,RE,RE)" */ - /* "CORE.DIR;1 1 8-SEP-1996 16:09 [SYSTEM] (RWE,RWE,RE,RE)" */ - /* and non-MutliNet VMS: */ - /* "CII-MANUAL.TEX;1 213/216 29-JAN-1996 03:33:12 [ANONYMOU,ANONYMOUS] (RWED,RWED,,)" */ - /* MSDOS format */ - /* 04-27-00 09:09PM licensed */ - /* 07-18-00 10:16AM pub */ - /* 04-14-00 03:47PM 589 readme.htm */ + +/* MultiNet (some spaces removed from examples) */ +/* "00README.TXT;1 2 30-DEC-1996 17:44 [SYSTEM] (RWED,RWED,RE,RE)" */ +/* "CORE.DIR;1 1 8-SEP-1996 16:09 [SYSTEM] (RWE,RWE,RE,RE)" */ +/* and non-MutliNet VMS: */ +/* "CII-MANUAL.TEX;1 213/216 29-JAN-1996 03:33:12 [ANONYMOU,ANONYMOUS] (RWED,RWED,,)" */ + +/* MSDOS format */ +/* 04-27-00 09:09PM licensed */ +/* 07-18-00 10:16AM pub */ +/* 04-14-00 03:47PM 589 readme.htm */ -long getlong(char *buf,int len) +long getlong(char *buf, int len) { long u = 0; + while (len-- > 0) u = u * 10 + (*buf++ - '0'); return u; @@ -59,63 +60,82 @@ long getlong(char *buf,int len) ** can optionally be quoted using <"> or "<" ">" ** Comments surrrounded by '(' ')' are filtered out ** -** On exit, +** On exit, ** *pstr has been moved to the first delimiter past the ** field ** THE STRING HAS BEEN MUTILATED by a 0 terminator ** ** Returns a pointer to the first word or NULL on error */ -char * get_nextfield (char ** pstr) +char * get_nextfield(char ** pstr) { - char * p = *pstr; + char * p = *pstr; - char * start = NULL; - if (!pstr || !*pstr) return NULL; - while (1) { - /* Strip white space and other delimiters */ - while (*p && (isspace((int) *p) || *p==',' || *p==';' || *p=='=')) p++; - if (!*p) { - *pstr = p; - return NULL; /* No field */ - } + char * start = NULL; - if (*p == '"') { /* quoted field */ - start = ++p; - for(;*p && *p!='"'; p++) - if (*p == '\\' && *(p+1)) p++; /* Skip escaped chars */ - break; /* kr95-10-9: needs to stop here */ - } else if (*p == '<') { /* quoted field */ - start = ++p; - for(;*p && *p!='>'; p++) - if (*p == '\\' && *(p+1)) p++; /* Skip escaped chars */ - break; /* kr95-10-9: needs to stop here */ - } else if (*p == '(') { /* Comment */ - for(;*p && *p!=')'; p++) - if (*p == '\\' && *(p+1)) p++; /* Skip escaped chars */ - p++; - } else { /* Spool field */ - start = p; - while(*p && !isspace((int) *p) && *p!=',' && *p!=';' && *p!='=') - p++; - break; /* Got it */ - } + if (!pstr || !*pstr) + return NULL; + while (1) + { + /* Strip white space and other delimiters */ + while (*p && (isspace((int)*p) || *p == ',' || *p == ';' || *p == '=')) + p++; + if (!*p) + { + *pstr = p; + return NULL; /* No field */ + } + + if (*p == '"') /* quoted field */ + { + start = ++p; + for (; *p && *p != '"'; p++) + if (*p == '\\' && *(p + 1)) + p++; + /* Skip escaped chars */ + break; /* kr95-10-9: needs to stop here */ + } + else if (*p == '<') /* quoted field */ + { + start = ++p; + for (; *p && *p != '>'; p++) + if (*p == '\\' && *(p + 1)) + p++; + /* Skip escaped chars */ + break; /* kr95-10-9: needs to stop here */ + } + else if (*p == '(') /* Comment */ + { + for (; *p && *p != ')'; p++) + if (*p == '\\' && *(p + 1)) + p++; + /* Skip escaped chars */ + p++; + } + else /* Spool field */ + { + start = p; + while (*p && !isspace((int)*p) && *p != ',' && *p != ';' && *p != '=') + p++; + break; /* Got it */ + } } - if (*p) *p++ = '\0'; - *pstr = p; - return start; + if (*p) + *p++ = '\0'; + *pstr = p; + return start; } -uerr_t ftp_parse(ftpparse *fp,char *buf,int len) +uerr_t ftp_parse(ftpparse *fp, char *buf, int len) { - char *cp; char *token; char *ptr; char *date; int i; + fp->filename = 0; fp->namelen = 0; // fp->flagtrycwd = 0; @@ -128,13 +148,14 @@ uerr_t ftp_parse(ftpparse *fp,char *buf,int len) fp->id = 0; // fp->idlen = 0; - proz_debug("FTP LIST to be parsed is %s", cp=strdup(buf)); + proz_debug("FTP LIST to be parsed is %s", cp = strdup(buf)); free(cp); if (len < 2) /* an empty name in EPLF, with no info, could be 2 chars */ - return FTPPARSENOTEXIST; + return FTPPARSENOTEXIST; - switch(*buf) { + switch (*buf) + { case 'b': case 'c': case 'd': @@ -142,65 +163,71 @@ uerr_t ftp_parse(ftpparse *fp,char *buf,int len) case 'p': case 's': case '-': - /* UNIX-style listing, without inum and without blocks */ - /* "-rw-r--r-- 1 root other 531 Jan 29 03:26 README" */ - /* "dr-xr-xr-x 2 root other 512 Apr 8 1994 etc" */ - /* "dr-xr-xr-x 2 root 512 Apr 8 1994 etc" */ - /* "lrwxrwxrwx 1 root other 7 Jan 25 00:17 bin -> usr/bin" */ - /* Also produced by Microsoft's FTP servers for Windows: */ - /* "---------- 1 owner group 1803128 Jul 10 10:18 ls-lR.Z" */ - /* "d--------- 1 owner group 0 May 9 19:45 Softlib" */ - /* Also WFTPD for MSDOS: */ - /* "-rwxrwxrwx 1 noone nogroup 322 Aug 19 1996 message.ftp" */ - /* Also NetWare: */ - /* "d [R----F--] supervisor 512 Jan 16 18:53 login" */ - /* "- [R----F--] rhesus 214059 Oct 20 15:27 cx.exe" */ - /* Also NetPresenz for the Mac: */ - /* "-------r-- 326 1391972 1392298 Nov 22 1995 MegaPhone.sit" */ - /* "drwxrwxr-x folder 2 May 10 1996 network" + /* UNIX-style listing, without inum and without blocks */ + /* "-rw-r--r-- 1 root other 531 Jan 29 03:26 README" */ + /* "dr-xr-xr-x 2 root other 512 Apr 8 1994 etc" */ + /* "dr-xr-xr-x 2 root 512 Apr 8 1994 etc" */ + /* "lrwxrwxrwx 1 root other 7 Jan 25 00:17 bin -> usr/bin" */ + /* Also produced by Microsoft's FTP servers for Windows: */ + /* "---------- 1 owner group 1803128 Jul 10 10:18 ls-lR.Z" */ + /* "d--------- 1 owner group 0 May 9 19:45 Softlib" */ + /* Also WFTPD for MSDOS: */ + /* "-rwxrwxrwx 1 noone nogroup 322 Aug 19 1996 message.ftp" */ + /* Also NetWare: */ + /* "d [R----F--] supervisor 512 Jan 16 18:53 login" */ + /* "- [R----F--] rhesus 214059 Oct 20 15:27 cx.exe" */ + /* Also NetPresenz for the Mac: */ + /* "-------r-- 326 1391972 1392298 Nov 22 1995 MegaPhone.sit" */ + /* "drwxrwxr-x folder 2 May 10 1996 network" */ - if (*buf == 'd') fp->filetype = DIRECTORY; - if (*buf == '-') fp->filetype = DIRECTORY; - if (*buf == 'l') fp->filetype = SYMBOLIC_LINK; - ptr=cp=strdup(buf); - - for (i=0;i<4;i++) - { - //add checking - token= get_nextfield(&cp); - if(token == NULL) //failed to parse - return FTPPARSEFAIL; + if (*buf == 'd') + fp->filetype = DIRECTORY; + if (*buf == '-') + fp->filetype = DIRECTORY; + if (*buf == 'l') + fp->filetype = SYMBOLIC_LINK; + ptr = cp = strdup(buf); + + for (i = 0; i < 4; i++) + { + //add checking + token = get_nextfield(&cp); + if (token == NULL) //failed to parse + return FTPPARSEFAIL; + } + /* + ** This field can either be group or size. We find out by looking at the + ** next field. If this is a non-digit then this field is the size. + */ + while (*cp && isspace((int)*cp)) + cp++; + if (isdigit((int)*cp)) + { + token = get_nextfield(&cp); + while (*cp && isspace((int)*cp)) + cp++; + } + //if it is a filename + fp->filesize = strtol(token, NULL, 10); + proz_debug("FTP file size is %ld", fp->filesize); + + + while (*cp && isspace((int)*cp)) + cp++; + assert(cp + 12 < ptr + len); + date = cp; + cp += 12; + *cp++ = '\0'; + fp->date_str = strdup(date); + + proz_debug("LIST date is %s", fp->date_str); + + return FTPPARSEOK; + + default: + return FTPPARSEFAIL; } - /* - ** This field can either be group or size. We find out by looking at the - ** next field. If this is a non-digit then this field is the size. - */ - while (*cp && isspace((int) *cp)) cp++; - if (isdigit((int) *cp)) { - token = get_nextfield(&cp); - while (*cp && isspace((int) *cp)) cp++; - } - //if it is a filename - fp->filesize=strtol(token,NULL,10); - proz_debug("FTP file size is %ld", fp->filesize); - - - while (*cp && isspace((int) *cp)) cp++; - assert(cp+12date_str = strdup(date); - - proz_debug("LIST date is %s", fp->date_str); - - return FTPPARSEOK; - default: - return FTPPARSEFAIL; - - } - } @@ -208,18 +235,16 @@ uerr_t ftp_parse(ftpparse *fp,char *buf,int len) time_t parse_time(const char * str) { + char * p; + struct tm tm; + time_t t; - char * p; - struct tm tm; - time_t t; - - if (!str) return 0; - - if ((p = strchr(str, ','))) - { - -} - + if (!str) return 0; + if ((p = strchr(str, ','))) + { + } + + return 0; } diff --git a/libprozilla/src/ftpparse.h b/libprozilla/src/ftpparse.h index 0356343..2f9a203 100644 --- a/libprozilla/src/ftpparse.h +++ b/libprozilla/src/ftpparse.h @@ -1,25 +1,25 @@ /****************************************************************************** - libprozilla - a download accelerator library - Copyright (C) 2001 Kalum Somaratna + libprozilla - a download accelerator library + Copyright (C) 2001 Kalum Somaratna - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ******************************************************************************/ /* FTP LIST command parsing code. */ -/* $Id: ftpparse.h,v 1.15 2005/08/06 17:07:35 kalum Exp $ */ +/* $Id$ */ #ifndef FTPPARSE_H @@ -34,20 +34,20 @@ extern "C" { typedef struct ftpparse { - char *filename; + char *filename; int namelen; off_t filesize; /* number of octets */ // int mtimetype; time_t mtime; /* modification time */ file_type_t filetype; char *id; /* not necessarily 0-terminated */ -char *date_str; -}ftpparse ; + char *date_str; +}ftpparse; -uerr_t ftp_parse(ftpparse *fp,char *buf,int len); +uerr_t ftp_parse(ftpparse *fp, char *buf, int len); #ifdef __cplusplus } #endif -#endif /* FTPPARSE_H */ +#endif /* FTPPARSE_H */ diff --git a/libprozilla/src/ftpsearch.c b/libprozilla/src/ftpsearch.c index 5bc8759..3dd5250 100644 --- a/libprozilla/src/ftpsearch.c +++ b/libprozilla/src/ftpsearch.c @@ -1,20 +1,20 @@ /****************************************************************************** - libprozilla - a download accelerator library - Copyright (C) 2001 Kalum Somaratna + libprozilla - a download accelerator library + Copyright (C) 2001 Kalum Somaratna - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ******************************************************************************/ #include "common.h" @@ -27,9 +27,9 @@ urlinfo *prepare_lycos_url(ftps_request_t * request, char *ftps_loc, - int num_req_mirrors); + int num_req_mirrors); urlinfo *prepare_filesearching_url(ftps_request_t * request, char *ftps_loc, - int num_req_mirrors); + int num_req_mirrors); uerr_t parse_lycos_html_mirror_list(ftps_request_t * request, char *p); uerr_t parse_filesearching_html_mirror_list(ftps_request_t * request, char *p); @@ -44,17 +44,17 @@ char *find_closed_a(char *buf); char *get_string_ahref(char *buf, char *out, size_t out_size); char *grow_buffer(char *buf_start, char *cur_pos, int *buf_len, - int data_len); + int data_len); uerr_t get_complete_mirror_list(ftps_request_t * request); ftp_mirror_t *reprocess_mirror_list(ftp_mirror_t * mirrors, - int *num_servers); + int *num_servers); ftps_request_t * proz_ftps_request_init( - urlinfo * requested_url, off_t file_size, - char *ftps_loc, - ftpsearch_server_type_t server_type, - int num_req_mirrors) + urlinfo * requested_url, off_t file_size, + char *ftps_loc, + ftpsearch_server_type_t server_type, + int num_req_mirrors) { urlinfo *url; ftps_request_t * request; @@ -62,7 +62,7 @@ ftps_request_t * proz_ftps_request_init( assert(requested_url); assert(requested_url->file); - request=kmalloc(sizeof(ftps_request_t)); + request = kmalloc(sizeof(ftps_request_t)); memset(request, 0, sizeof(ftps_request_t)); request->file_name = strdup(requested_url->file); request->requested_url = proz_copy_url(requested_url); @@ -71,57 +71,59 @@ ftps_request_t * proz_ftps_request_init( pthread_mutex_init(&request->access_mutex, 0); switch (server_type) - { - case LYCOS: - url = prepare_lycos_url(request, ftps_loc, num_req_mirrors); - if (url == 0) - proz_die("Bad URl specification"); - - /*NOTE pasing zero as the status change mutes as we dont need it here */ - request->connection=proz_connection_init(url,0); + { + case LYCOS: + url = prepare_lycos_url(request, ftps_loc, num_req_mirrors); + if (url == 0) + proz_die("Bad URl specification"); + + /*NOTE pasing zero as the status change mutes as we dont need it here */ + request->connection = proz_connection_init(url, 0); - break; - case FILESEARCH_RU: - url = prepare_filesearching_url(request, ftps_loc, num_req_mirrors); - if (url == 0) - proz_die("Bad URl specification"); - - /*NOTE pasing zero as the status change mutes as we dont need it here */ - request->connection=proz_connection_init(url,0); - break; - default: - proz_debug("Unsupported FTP search server type"); - proz_die("Unsupported FTP search server type"); - } + break; + + case FILESEARCH_RU: + url = prepare_filesearching_url(request, ftps_loc, num_req_mirrors); + if (url == 0) + proz_die("Bad URl specification"); + + /*NOTE pasing zero as the status change mutes as we dont need it here */ + request->connection = proz_connection_init(url, 0); + break; + + default: + proz_debug("Unsupported FTP search server type"); + proz_die("Unsupported FTP search server type"); + } return request; } urlinfo *prepare_lycos_url(ftps_request_t * request, char *ftps_loc, - int num_req_mirrors) + int num_req_mirrors) { urlinfo *url; uerr_t err; char *lycos_url_buf; int lycos_url_len = strlen(ftps_loc) + - strlen - ("?form=advanced&query=%s&doit=Search&type=Exact+search&hits=%d&matches=&hitsprmatch=&limdom=&limpath=&limsize1=%d&limsize2=%d&limtime1=&limtime2=&f1=Host&f2=Path&f3=Size&f4=-&f5=-&f6=-&header=none&sort=none&trlen=20"); + strlen + ("?form=advanced&query=%s&doit=Search&type=Exact+search&hits=%d&matches=&hitsprmatch=&limdom=&limpath=&limsize1=%d&limsize2=%d&limtime1=&limtime2=&f1=Host&f2=Path&f3=Size&f4=-&f5=-&f6=-&header=none&sort=none&trlen=20"); assert(request->file_name); - url = (urlinfo *) kmalloc(sizeof(urlinfo)); + url = (urlinfo *)kmalloc(sizeof(urlinfo)); /* Okay lets now construct the URL we want to do lycos */ lycos_url_buf = - (char *) kmalloc(lycos_url_len + strlen(request->file_name) + 300); + (char *)kmalloc(lycos_url_len + strlen(request->file_name) + 300); sprintf(lycos_url_buf, - "%s?form=advanced&query=%s&doit=Search&type=Exact+search&hits=%d&matches=&hitsprmatch=&limdom=&limpath=&limsize1=%zd&limsize2=%zd&f1=Host&f2=Path&f3=Size&f4=-&f5=-&f6=-&header=none&sort=none&trlen=20", - ftps_loc, request->file_name, num_req_mirrors, - request->file_size, request->file_size); + "%s?form=advanced&query=%s&doit=Search&type=Exact+search&hits=%d&matches=&hitsprmatch=&limdom=&limpath=&limsize1=%zd&limsize2=%zd&f1=Host&f2=Path&f3=Size&f4=-&f5=-&f6=-&header=none&sort=none&trlen=20", + ftps_loc, request->file_name, num_req_mirrors, + request->file_size, request->file_size); /* Debugging purposes */ /*sprintf(lycos_url_buf,"localhost/search.html"); */ @@ -140,28 +142,28 @@ urlinfo *prepare_lycos_url(ftps_request_t * request, char *ftps_loc, urlinfo *prepare_filesearching_url(ftps_request_t * request, char *ftps_loc, - int num_req_mirrors) + int num_req_mirrors) { urlinfo *url; uerr_t err; char *filesearching_url_buf; int filesearching_url_len = strlen(ftps_loc) + - strlen - ("?q=ddd-3.3.tar.bz2&l=en&t=f&e=on&m=20&o=n&s=on&s1=4811576&s2=4811576&d=&p=&p2=&x=10&y=14"); + strlen + ("?q=ddd-3.3.tar.bz2&l=en&t=f&e=on&m=20&o=n&s=on&s1=4811576&s2=4811576&d=&p=&p2=&x=10&y=14"); assert(request->file_name); - url = (urlinfo *) kmalloc(sizeof(urlinfo)); + url = (urlinfo *)kmalloc(sizeof(urlinfo)); /* Okay lets now construct the URL we want to do lycos */ filesearching_url_buf = - (char *) kmalloc(filesearching_url_len + strlen(request->file_name) + 300); + (char *)kmalloc(filesearching_url_len + strlen(request->file_name) + 300); sprintf(filesearching_url_buf, - "%s?q=%s&l=en&t=f&e=on&m=%d&o=n&s=on&s1=%zd&s2=%zd&d=&p=&p2=&x=10&y=14", - ftps_loc, request->file_name, num_req_mirrors, - request->file_size, request->file_size); + "%s?q=%s&l=en&t=f&e=on&m=%d&o=n&s=on&s1=%zd&s2=%zd&d=&p=&p2=&x=10&y=14", + ftps_loc, request->file_name, num_req_mirrors, + request->file_size, request->file_size); /* Debugging purposes */ /* sprintf(filesearching_url_buf,"localhost/fs.html"); */ @@ -181,25 +183,25 @@ urlinfo *prepare_filesearching_url(ftps_request_t * request, char *ftps_loc, uerr_t parse_html_mirror_list(ftps_request_t * request, char *p) { - switch (request->server_type) - { - case LYCOS: - return parse_lycos_html_mirror_list(request, p); - break; - case FILESEARCH_RU: - return parse_filesearching_html_mirror_list(request, p); - break; - default: - proz_debug("Unsupported FTP search server type"); - proz_die("Unsupported FTP search server type"); - } + { + case LYCOS: + return parse_lycos_html_mirror_list(request, p); + break; + + case FILESEARCH_RU: + return parse_filesearching_html_mirror_list(request, p); + break; + + default: + proz_debug("Unsupported FTP search server type"); + proz_die("Unsupported FTP search server type"); + } return MIRPARSEFAIL; } uerr_t parse_lycos_html_mirror_list(ftps_request_t * request, char *p) { - struct ftp_mirror **pmirrors = &request->mirrors; int *num_servers = &request->num_mirrors; char *p1, *p2, *i = 0, *j; @@ -209,77 +211,77 @@ uerr_t parse_lycos_html_mirror_list(ftps_request_t * request, char *p) if (strstr(p, "No hits") != 0) - { - *num_servers = 0; - return MIRINFOK; - } + { + *num_servers = 0; + return MIRINFOK; + } /*Check the number of PRE tags */ p1 = p; while (((p1 = strstr(p1, "
")) != NULL) && p1)
-  {
-    num_pre++;
-    p1 += 5;
-  }
+    {
+      num_pre++;
+      p1 += 5;
+    }
 
   proz_debug("Number of PRE tags found = %d\n", num_pre);
 
   if (num_pre == 1)
-  {
-
-    if ((i = strstr(p, "
")) == NULL)
     {
-      proz_debug("nomatches found");
-      return MIRPARSEFAIL;
+      if ((i = strstr(p, "
")) == NULL)
+        {
+          proz_debug("nomatches found");
+          return MIRPARSEFAIL;
+        }
+
+      proz_debug("match at %d found", i - p);
+
+      if ((j = strstr(p, "
")) == NULL) + { + proz_debug("nomatches found"); + return MIRPARSEFAIL; + } } - - proz_debug("match at %d found", i - p); - - if ((j = strstr(p, "
")) == NULL) + else { - proz_debug("nomatches found"); - return MIRPARSEFAIL; + /*search for the reported hits text */ + char *rep_hits; + int prior_pres = 0; + + if ((rep_hits = strstr(p, "reported hits")) == NULL) + { + proz_debug("no reported hits found"); + return MIRPARSEFAIL; + } + + /* Okay so we got the position after the results, lets see how many PRE tags were there before it */ + + p1 = p; + while (((p1 = strstr(p1, "
")) < rep_hits) && p1)
+        {
+          prior_pres++;
+          p1 += 5;
+        }
+      /* now get the location of the PRE before the output */
+
+      p1 = p;
+      i = 0;
+      while (prior_pres--)
+        {
+          p1 = strstr(p1, "
");
+          p1 += 5;
+        }
+      i = p1 - 5;
+
+      /*now find the 
tag which is after the results */ + j = strstr(i, "
"); + + if (j == NULL) + { + proz_debug("The expected
tag was not found!\n"); + return MIRPARSEFAIL; + } } - } else - { - /*search for the reported hits text */ - char *rep_hits; - int prior_pres = 0; - - if ((rep_hits = strstr(p, "reported hits")) == NULL) - { - proz_debug("no reported hits found"); - return MIRPARSEFAIL; - } - - /* Okay so we got the position after the results, lets see how many PRE tags were there before it */ - - p1 = p; - while (((p1 = strstr(p1, "
")) < rep_hits) && p1)
-    {
-      prior_pres++;
-      p1 += 5;
-    }
-    /* now get the location of the PRE before the output */
-
-    p1 = p;
-    i = 0;
-    while (prior_pres--)
-    {
-      p1 = strstr(p1, "
");
-      p1 += 5;
-    }
-    i = p1 - 5;
-
-    /*now find the 
tag which is after the results */ - j = strstr(i, "
"); - - if (j == NULL) - { - proz_debug("The expected tag was not found!\n"); - return MIRPARSEFAIL; - } - } p1 = kmalloc((j - i - 5) + 100); strncpy(p1, i + 5, j - i - 5); @@ -290,18 +292,18 @@ uerr_t parse_lycos_html_mirror_list(ftps_request_t * request, char *p) p2 = p1; while ((i = strstr(p1, "requested_url->proto == URLFTP) - { - memset(&(ftp_mirrors[k]), 0, sizeof(ftp_mirror_t)); - ftp_mirrors[k].server_name = kstrdup(request->requested_url->host); + { + memset(&(ftp_mirrors[k]), 0, sizeof(ftp_mirror_t)); + ftp_mirrors[k].server_name = kstrdup(request->requested_url->host); - ftp_mirrors[k].paths = kmalloc(sizeof(mirror_path_t)); - ftp_mirrors[k].num_paths = 1; + ftp_mirrors[k].paths = kmalloc(sizeof(mirror_path_t)); + ftp_mirrors[k].num_paths = 1; - if (*(request->requested_url->dir)) - ftp_mirrors[k].paths[0].path = kstrdup(request->requested_url->dir); - else - ftp_mirrors[k].paths[0].path = kstrdup(""); + if (*(request->requested_url->dir)) + ftp_mirrors[k].paths[0].path = kstrdup(request->requested_url->dir); + else + ftp_mirrors[k].paths[0].path = kstrdup(""); - ftp_mirrors[k].file_name = kstrdup(request->requested_url->file); - *num_servers += 1; - } + ftp_mirrors[k].file_name = kstrdup(request->requested_url->file); + *num_servers += 1; + } proz_debug("%d servers found\n", *num_servers); for (k = 0; k < *num_servers; k++) - { + { + ftp_mirrors[k].full_name = + (char *)kmalloc(strlen(ftp_mirrors[k].server_name) + + strlen(ftp_mirrors[k].paths[0].path) + + strlen(ftp_mirrors[k].file_name) + 13); + sprintf(ftp_mirrors[k].full_name, "%s%s:21/%s%s%s", "ftp://", + ftp_mirrors[k].server_name, ftp_mirrors[k].paths[0].path, "/", + ftp_mirrors[k].file_name); - ftp_mirrors[k].full_name = - (char *) kmalloc(strlen(ftp_mirrors[k].server_name) + - strlen(ftp_mirrors[k].paths[0].path) + - strlen(ftp_mirrors[k].file_name) + 13); - sprintf(ftp_mirrors[k].full_name, "%s%s:21/%s%s%s", "ftp://", - ftp_mirrors[k].server_name, ftp_mirrors[k].paths[0].path, "/", - ftp_mirrors[k].file_name); - - proz_debug("%s\n", ftp_mirrors[k].full_name); - } + proz_debug("%s\n", ftp_mirrors[k].full_name); + } *pmirrors = reprocess_mirror_list(ftp_mirrors, num_servers); @@ -379,7 +380,6 @@ uerr_t parse_lycos_html_mirror_list(ftps_request_t * request, char *p) uerr_t parse_filesearching_html_mirror_list(ftps_request_t * request, char *p) { - struct ftp_mirror **pmirrors = &request->mirrors; int *num_servers = &request->num_mirrors; char *p1, *p2, *i = 0, *j; @@ -389,77 +389,77 @@ uerr_t parse_filesearching_html_mirror_list(ftps_request_t * request, char *p) if (strstr(p, "not found") != 0) - { - *num_servers = 0; - return MIRINFOK; - } + { + *num_servers = 0; + return MIRINFOK; + } /*Check the number of PRE tags */ p1 = p; while (((p1 = strstr(p1, "")) == NULL) { - proz_debug("nomatches found"); - return MIRPARSEFAIL; + if ((i = strstr(p, "
")) == NULL)
+        {
+          proz_debug("nomatches found");
+          return MIRPARSEFAIL;
+        }
+
+      proz_debug("match at %d found", i - p);
+
+      if ((j = strstr(p, "
")) == NULL) + { + proz_debug("nomatches found"); + return MIRPARSEFAIL; + } } - - proz_debug("match at %d found", i - p); - - if ((j = strstr(p, "")) == NULL) + else { - proz_debug("nomatches found"); - return MIRPARSEFAIL; + /*search for the reported hits text */ + char *rep_hits; + int prior_pres = 0; + + if ((rep_hits = strstr(p, "reported hits")) == NULL) + { + proz_debug("no reported hits found"); + return MIRPARSEFAIL; + } + + /* Okay so we got the position after the results, lets see how many PRE tags were there before it */ + + p1 = p; + while (((p1 = strstr(p1, ""); + p1 += 5; + } + i = p1 - 5; + + /*now find the tag which is after the results */ + j = strstr(i, ""); + + if (j == NULL) + { + proz_debug("The expected tag was not found!\n"); + return MIRPARSEFAIL; + } } - } else - { - /*search for the reported hits text */ - char *rep_hits; - int prior_pres = 0; - - if ((rep_hits = strstr(p, "reported hits")) == NULL) - { - proz_debug("no reported hits found"); - return MIRPARSEFAIL; - } - - /* Okay so we got the position after the results, lets see how many PRE tags were there before it */ - - p1 = p; - while (((p1 = strstr(p1, ""); - p1 += 5; - } - i = p1 - 5; - - /*now find the tag which is after the results */ - j = strstr(i, ""); - - if (j == NULL) - { - proz_debug("The expected tag was not found!\n"); - return MIRPARSEFAIL; - } - } p1 = kmalloc((j - i - 16) + 100); strncpy(p1, i + 16, j - i - 16); @@ -472,18 +472,18 @@ uerr_t parse_filesearching_html_mirror_list(ftps_request_t * request, char *p) p2 = p1; while ((i = strstr(p1, "
requested_url->proto == URLFTP) - { - memset(&(ftp_mirrors[k]), 0, sizeof(ftp_mirror_t)); - ftp_mirrors[k].server_name = kstrdup(request->requested_url->host); + { + memset(&(ftp_mirrors[k]), 0, sizeof(ftp_mirror_t)); + ftp_mirrors[k].server_name = kstrdup(request->requested_url->host); - ftp_mirrors[k].paths = kmalloc(sizeof(mirror_path_t)); - ftp_mirrors[k].num_paths = 1; + ftp_mirrors[k].paths = kmalloc(sizeof(mirror_path_t)); + ftp_mirrors[k].num_paths = 1; - if (*(request->requested_url->dir)) - ftp_mirrors[k].paths[0].path = kstrdup(request->requested_url->dir); - else - ftp_mirrors[k].paths[0].path = kstrdup(""); + if (*(request->requested_url->dir)) + ftp_mirrors[k].paths[0].path = kstrdup(request->requested_url->dir); + else + ftp_mirrors[k].paths[0].path = kstrdup(""); - ftp_mirrors[k].file_name = kstrdup(request->requested_url->file); - *num_servers += 1; - } + ftp_mirrors[k].file_name = kstrdup(request->requested_url->file); + *num_servers += 1; + } proz_debug("%d servers found\n", *num_servers); for (k = 0; k < *num_servers; k++) - { + { + ftp_mirrors[k].full_name = + (char *)kmalloc(strlen(ftp_mirrors[k].server_name) + + strlen(ftp_mirrors[k].paths[0].path) + + strlen(ftp_mirrors[k].file_name) + 13); + sprintf(ftp_mirrors[k].full_name, "%s%s:21/%s%s%s", "ftp://", + ftp_mirrors[k].server_name, ftp_mirrors[k].paths[0].path, "/", + ftp_mirrors[k].file_name); - ftp_mirrors[k].full_name = - (char *) kmalloc(strlen(ftp_mirrors[k].server_name) + - strlen(ftp_mirrors[k].paths[0].path) + - strlen(ftp_mirrors[k].file_name) + 13); - sprintf(ftp_mirrors[k].full_name, "%s%s:21/%s%s%s", "ftp://", - ftp_mirrors[k].server_name, ftp_mirrors[k].paths[0].path, "/", - ftp_mirrors[k].file_name); - - proz_debug("%s\n", ftp_mirrors[k].full_name); - } + proz_debug("%s\n", ftp_mirrors[k].full_name); + } *pmirrors = reprocess_mirror_list(ftp_mirrors, num_servers); @@ -567,7 +566,7 @@ uerr_t get_mirror_info(connection_t * connection, char **ret_buf) uerr_t err; int remote_port_len; char *user, *passwd, *www_auth = NULL, *proxy_auth = NULL, *location = - NULL, *referer = NULL, *pragma_no_cache = NULL; + NULL, *referer = NULL, *pragma_no_cache = NULL; char *request, *remote_port; netrc_entry *netrc_ent; char buffer[HTTP_BUFFER_SIZE]; @@ -589,125 +588,129 @@ uerr_t get_mirror_info(connection_t * connection, char **ret_buf) connection_change_status(connection, CONNECTING); if (http_use_proxy(connection)) - { - connection_show_message(connection, _("Connecting to %s"), - connection->http_proxy->proxy_url.host); - err = connect_to_server(&connection->data_sock, - connection->http_proxy->proxy_url.host, - connection->http_proxy->proxy_url.port, - &connection->xfer_timeout); - if (err != NOCONERROR) { - proz_debug(_("Error connecting to %s"), - connection->http_proxy->proxy_url.host); - connection_change_status(connection, REMOTEFATAL); - return err; + connection_show_message(connection, _("Connecting to %s"), + connection->http_proxy->proxy_url.host); + err = connect_to_server(&connection->data_sock, + connection->http_proxy->proxy_url.host, + connection->http_proxy->proxy_url.port, + &connection->xfer_timeout); + if (err != NOCONERROR) + { + proz_debug(_("Error connecting to %s"), + connection->http_proxy->proxy_url.host); + connection_change_status(connection, REMOTEFATAL); + return err; + } } - } else - { - connection_show_message(connection, _("Connecting to %s"), - connection->u.host); + else + { + connection_show_message(connection, _("Connecting to %s"), + connection->u.host); - err = connect_to_server(&connection->data_sock, connection->u.host, - connection->u.port, &connection->xfer_timeout); - if (err != NOCONERROR) - { - proz_debug(_("Error connecting to %s"), connection->u.host); - connection_change_status(connection, REMOTEFATAL); - return err; + err = connect_to_server(&connection->data_sock, connection->u.host, + connection->u.port, &connection->xfer_timeout); + if (err != NOCONERROR) + { + proz_debug(_("Error connecting to %s"), connection->u.host); + connection_change_status(connection, REMOTEFATAL); + return err; + } } - } user = connection->u.user; passwd = connection->u.passwd; /* Use .netrc if asked to do so. */ if (connection->use_netrc == TRUE) - { - netrc_ent = search_netrc(libprozrtinfo.netrc_list, connection->u.host); - - if (netrc_ent != NULL) { - user = netrc_ent->account; - passwd = netrc_ent->password; + netrc_ent = search_netrc(libprozrtinfo.netrc_list, connection->u.host); + + if (netrc_ent != NULL) + { + user = netrc_ent->account; + passwd = netrc_ent->password; + } } - } user = user ? user : ""; passwd = passwd ? passwd : ""; if (strlen(user) || strlen(passwd)) - { - /* Construct the necessary header. */ - www_auth = get_basic_auth_str(user, passwd, "Authorization"); - proz_debug(_("Authenticating as user %s password %s"), user, passwd); - proz_debug(_("Authentification string=%s"), www_auth); - } else + { + /* Construct the necessary header. */ + www_auth = get_basic_auth_str(user, passwd, "Authorization"); + proz_debug(_("Authenticating as user %s password %s"), user, passwd); + proz_debug(_("Authentification string=%s"), www_auth); + } + else www_auth = 0; if (http_use_proxy(connection)) - { - if (strlen(connection->http_proxy->username) - || strlen(connection->http_proxy->passwd)) - proxy_auth = - get_basic_auth_str(connection->http_proxy->username, - connection->http_proxy->passwd, - "Proxy-Authorization"); - } + { + if (strlen(connection->http_proxy->username) + || strlen(connection->http_proxy->passwd)) + proxy_auth = + get_basic_auth_str(connection->http_proxy->username, + connection->http_proxy->passwd, + "Proxy-Authorization"); + } if (connection->u.port == 80) - { - remote_port = NULL; - remote_port_len = 0; - } else - { - remote_port = (char *) alloca(64); - remote_port_len = sprintf(remote_port, ":%d", connection->u.port); - } + { + remote_port = NULL; + remote_port_len = 0; + } + else + { + remote_port = (char *)alloca(64); + remote_port_len = sprintf(remote_port, ":%d", connection->u.port); + } if (connection->u.referer) - { - referer = (char *) alloca(13 + strlen(connection->u.referer)); - sprintf(referer, "Referer: %s\r\n", connection->u.referer); - } + { + referer = (char *)alloca(13 + strlen(connection->u.referer)); + sprintf(referer, "Referer: %s\r\n", connection->u.referer); + } /* If we go through a proxy the request for the URL is different */ if (http_use_proxy(connection)) - { - location = (char *) alloca(strlen(connection->u.url) + 1); - strcpy(location, connection->u.url); - } else - { - location = (char *) alloca(strlen(connection->u.path) + 1); - strcpy(location, connection->u.path); - } + { + location = (char *)alloca(strlen(connection->u.url) + 1); + strcpy(location, connection->u.url); + } + else + { + location = (char *)alloca(strlen(connection->u.path) + 1); + strcpy(location, connection->u.path); + } /*Use no-cache directive for proxy servers, yes by default here as we dont want ftpsearch rsults which can change soon to be cached */ if (http_use_proxy(connection)) - { - pragma_no_cache = (char *) alloca(21); - sprintf(pragma_no_cache, "Pragma: no-cache\r\n"); - } + { + pragma_no_cache = (char *)alloca(21); + sprintf(pragma_no_cache, "Pragma: no-cache\r\n"); + } - request = (char *) alloca(strlen(location) - + strlen(connection->user_agent) - + strlen(connection->u.host) + remote_port_len - + (referer ? strlen(referer) : 0) - + (www_auth ? strlen(www_auth) : 0) - + (proxy_auth ? strlen(proxy_auth) : 0) - + 64 - + - (pragma_no_cache ? strlen(pragma_no_cache) : - 0)); + request = (char *)alloca(strlen(location) + + strlen(connection->user_agent) + + strlen(connection->u.host) + remote_port_len + + (referer ? strlen(referer) : 0) + + (www_auth ? strlen(www_auth) : 0) + + (proxy_auth ? strlen(proxy_auth) : 0) + + 64 + + + (pragma_no_cache ? strlen(pragma_no_cache) : + 0)); /* TODO Add referrer tag. */ sprintf(request, - "GET %s HTTP/1.0\r\nUser-Agent: %s\r\nHost: %s%s\r\nAccept: */*\r\n%s%s%s%s\r\n", - location, connection->user_agent, connection->u.host, - remote_port ? remote_port : "", - referer ? referer : "", - www_auth ? www_auth : "", proxy_auth ? proxy_auth : "", - pragma_no_cache ? pragma_no_cache : ""); + "GET %s HTTP/1.0\r\nUser-Agent: %s\r\nHost: %s%s\r\nAccept: */*\r\n%s%s%s%s\r\n", + location, connection->user_agent, connection->u.host, + remote_port ? remote_port : "", + referer ? referer : "", + www_auth ? www_auth : "", proxy_auth ? proxy_auth : "", + pragma_no_cache ? pragma_no_cache : ""); proz_debug("1 HTTP request = %s", request); @@ -716,54 +719,52 @@ uerr_t get_mirror_info(connection_t * connection, char **ret_buf) /* What hapenned ? */ if (err != HOK) - { - proz_debug("1 http_fetch_headers err != HOK %d",err); - /*Check if we authenticated using any user or password and if we - were kicked out, if so return HAUTHFAIL */ - if (err == HAUTHREQ && (strlen(user) || strlen(passwd))) - err = HAUTHFAIL; - /* - * a error occured druing the process - */ - close_sock(&connection->data_sock); - connection_change_status(connection, REMOTEFATAL); - return err; - } + { + proz_debug("1 http_fetch_headers err != HOK %d", err); + /*Check if we authenticated using any user or password and if we + were kicked out, if so return HAUTHFAIL */ + if (err == HAUTHREQ && (strlen(user) || strlen(passwd))) + err = HAUTHFAIL; + /* + * a error occured druing the process + */ + close_sock(&connection->data_sock); + connection_change_status(connection, REMOTEFATAL); + return err; + } /* Ok start fetching the data */ - p1 = p = (char *) kmalloc(HTTP_BUFFER_SIZE + 1); + p1 = p = (char *)kmalloc(HTTP_BUFFER_SIZE + 1); p_len = HTTP_BUFFER_SIZE + 1; total = 0; do - { - - ret = - krecv(connection->data_sock, buffer, sizeof(buffer), 0, - &connection->xfer_timeout); - if (ret > 0) { - p2 = grow_buffer(p, p1, &p_len, ret); - memcpy(p2 + (p1 - p), buffer, ret); - p1 = (p1 - p) + ret + p2; - p = p2; - } - total += ret; - } - while (ret > 0); + ret = + krecv(connection->data_sock, buffer, sizeof(buffer), 0, + &connection->xfer_timeout); + if (ret > 0) + { + p2 = grow_buffer(p, p1, &p_len, ret); + memcpy(p2 + (p1 - p), buffer, ret); + p1 = (p1 - p) + ret + p2; + p = p2; + } + total += ret; + } while (ret > 0); if (ret == -1) - { - if (errno == ETIMEDOUT) { + if (errno == ETIMEDOUT) + { + close(connection->data_sock); + return READERR; + } close(connection->data_sock); return READERR; } - close(connection->data_sock); - return READERR; - } p[total] = 0; *ret_buf = p; @@ -777,18 +778,17 @@ uerr_t get_mirror_info(connection_t * connection, char **ret_buf) char *find_ahref(char *buf) { - - return (strcasestr(buf, "")); + return(strcasestr(buf, ">")); } char *find_closed_a(char *buf) { - return (strcasestr(buf, "info_thread, NULL, - (void *) &get_complete_mirror_list, - (void *) request) != 0) + (void *)&get_complete_mirror_list, + (void *)request) != 0) proz_die(_("Error: Not enough system resources")); - - - } void proz_cancel_mirror_list_request(ftps_request_t * request) { - request->info_running = FALSE; - pthread_cancel(request->info_thread); - pthread_join(request->info_thread,0); + request->info_running = FALSE; + pthread_cancel(request->info_thread); + pthread_join(request->info_thread, 0); } uerr_t get_complete_mirror_list(ftps_request_t * request) @@ -872,64 +870,64 @@ uerr_t get_complete_mirror_list(ftps_request_t * request) pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); do - { - pthread_mutex_lock(&request->access_mutex); - request->info_running = TRUE; - pthread_mutex_unlock(&request->access_mutex); - - pthread_cleanup_push(cleanup_socks, (void *) request->connection); - request->err = get_mirror_info(request->connection, &data_buf); - pthread_cleanup_pop(0); - - if (request->err == NEWLOCATION) { - char *constructed_newloc; - /*DONE : handle relative urls too */ - constructed_newloc = - uri_merge(request->connection->u.url, - request->connection->hs.newloc); + pthread_mutex_lock(&request->access_mutex); + request->info_running = TRUE; + pthread_mutex_unlock(&request->access_mutex); - proz_debug("Redirected to %s, merged URL = %s", - request->connection->hs.newloc, constructed_newloc); + pthread_cleanup_push(cleanup_socks, (void *)request->connection); + request->err = get_mirror_info(request->connection, &data_buf); + pthread_cleanup_pop(0); - proz_free_url(&request->connection->u, 0); - request->err = - proz_parse_url(constructed_newloc, &request->connection->u, 0); + if (request->err == NEWLOCATION) + { + char *constructed_newloc; + /*DONE : handle relative urls too */ + constructed_newloc = + uri_merge(request->connection->u.url, + request->connection->hs.newloc); + + proz_debug("Redirected to %s, merged URL = %s", + request->connection->hs.newloc, constructed_newloc); + + proz_free_url(&request->connection->u, 0); + request->err = + proz_parse_url(constructed_newloc, &request->connection->u, 0); - if (request->err != URLOK) - { - connection_show_message(request->connection, - _ - ("The server returned location is wrong: %s!"), - constructed_newloc); - pthread_mutex_lock(&request->connection->access_mutex); - request->info_running = FALSE; - pthread_mutex_unlock(&request->connection->access_mutex); - kfree(constructed_newloc); - pthread_mutex_lock(&request->access_mutex); - request->info_running = FALSE; - pthread_mutex_unlock(&request->access_mutex); - return (request->err = HERR); - } else - connection_show_message(request->connection, - _("Redirected to => %s"), - constructed_newloc); + if (request->err != URLOK) + { + connection_show_message(request->connection, + _ + ("The server returned location is wrong: %s!"), + constructed_newloc); + pthread_mutex_lock(&request->connection->access_mutex); + request->info_running = FALSE; + pthread_mutex_unlock(&request->connection->access_mutex); + kfree(constructed_newloc); + pthread_mutex_lock(&request->access_mutex); + request->info_running = FALSE; + pthread_mutex_unlock(&request->access_mutex); + return(request->err = HERR); + } + else + connection_show_message(request->connection, + _("Redirected to => %s"), + constructed_newloc); - kfree(constructed_newloc); - request->err = NEWLOCATION; - } - } - while (request->err == NEWLOCATION); + kfree(constructed_newloc); + request->err = NEWLOCATION; + } + } while (request->err == NEWLOCATION); /*TODO handle and process the redirection here */ if (request->err != HOK) - { - pthread_mutex_lock(&request->access_mutex); - request->info_running = FALSE; - pthread_mutex_unlock(&request->access_mutex); - return request->err; - } + { + pthread_mutex_lock(&request->access_mutex); + request->info_running = FALSE; + pthread_mutex_unlock(&request->access_mutex); + return request->err; + } request->err = parse_html_mirror_list(request, data_buf); /*TODO see if can give further info */ @@ -937,13 +935,13 @@ uerr_t get_complete_mirror_list(ftps_request_t * request) request->info_running = FALSE; pthread_mutex_unlock(&request->access_mutex); return request->err; - } boolean proz_request_info_running(ftps_request_t * request) { boolean ret; + pthread_mutex_lock(&request->access_mutex); ret = request->info_running; pthread_mutex_unlock(&request->access_mutex); @@ -953,6 +951,7 @@ boolean proz_request_info_running(ftps_request_t * request) boolean proz_request_mass_ping_running(ftps_request_t * request) { boolean ret; + pthread_mutex_lock(&request->access_mutex); ret = request->mass_ping_running; pthread_mutex_unlock(&request->access_mutex); @@ -962,67 +961,66 @@ boolean proz_request_mass_ping_running(ftps_request_t * request) ftp_mirror_t *reprocess_mirror_list(ftp_mirror_t * mirrors, - int *num_servers) + int *num_servers) { - ftp_mirror_t *ftp_mirrors; int i, j; int num_new_servers = 0; ftp_mirrors = - (ftp_mirror_t *) kmalloc(sizeof(ftp_mirror_t) * ((*num_servers))); + (ftp_mirror_t *)kmalloc(sizeof(ftp_mirror_t) * ((*num_servers))); for (i = 0; i < *num_servers; i++) - { - if (mirrors[i].copied != 1) { - num_new_servers++; - memset(ftp_mirrors + num_new_servers - 1, 0, sizeof(ftp_mirror_t)); - memcpy(ftp_mirrors + num_new_servers - 1, mirrors + i, - sizeof(ftp_mirror_t)); + if (mirrors[i].copied != 1) + { + num_new_servers++; + memset(ftp_mirrors + num_new_servers - 1, 0, sizeof(ftp_mirror_t)); + memcpy(ftp_mirrors + num_new_servers - 1, mirrors + i, + sizeof(ftp_mirror_t)); - /*For the moment assume that all the mirrors support resume */ - ftp_mirrors[num_new_servers - 1].resume_supported=TRUE; + /*For the moment assume that all the mirrors support resume */ + ftp_mirrors[num_new_servers - 1].resume_supported = TRUE; - for (j = i + 1; j < *num_servers; j++) - { - if ((strcasecmp - (mirrors[i].server_name, - mirrors[j].server_name) == 0) && mirrors[j].copied != 1) - { - /*found a match */ - ftp_mirrors[num_new_servers - 1].num_paths++; - ftp_mirrors[num_new_servers - 1].paths = - krealloc(ftp_mirrors[num_new_servers - 1].paths, - (sizeof(mirror_path_t) * - ftp_mirrors[num_new_servers - 1].num_paths)); + for (j = i + 1; j < *num_servers; j++) + { + if ((strcasecmp + (mirrors[i].server_name, + mirrors[j].server_name) == 0) && mirrors[j].copied != 1) + { + /*found a match */ + ftp_mirrors[num_new_servers - 1].num_paths++; + ftp_mirrors[num_new_servers - 1].paths = + krealloc(ftp_mirrors[num_new_servers - 1].paths, + (sizeof(mirror_path_t) * + ftp_mirrors[num_new_servers - 1].num_paths)); - // ftp_mirrors[num_new_servers-1].paths = krealloc(ftp_mirrors[num_new_servers-1].paths,ftp_mirrors[num_new_servers-1].num_paths ); + // ftp_mirrors[num_new_servers-1].paths = krealloc(ftp_mirrors[num_new_servers-1].paths,ftp_mirrors[num_new_servers-1].num_paths ); - ftp_mirrors[num_new_servers - - 1].paths[ftp_mirrors[num_new_servers - 1].num_paths - - 1].path = strdup(mirrors[j].paths[0].path); + ftp_mirrors[num_new_servers - + 1].paths[ftp_mirrors[num_new_servers - 1].num_paths - + 1].path = strdup(mirrors[j].paths[0].path); - ftp_mirrors[num_new_servers - - 1].paths[ftp_mirrors[num_new_servers - 1].num_paths - - 1].valid = TRUE; + ftp_mirrors[num_new_servers - + 1].paths[ftp_mirrors[num_new_servers - 1].num_paths - + 1].valid = TRUE; - mirrors[j].copied = 1; - } - } + mirrors[j].copied = 1; + } + } + } } - } *num_servers = num_new_servers; proz_debug("Displaying the reparsed list \n"); for (i = 0; i < num_new_servers; i++) - { - proz_debug("%s\n", ftp_mirrors[i].full_name); - for (j = 0; j < ftp_mirrors[i].num_paths; j++) - proz_debug("\t%s\n", ftp_mirrors[i].paths[j].path); - } + { + proz_debug("%s\n", ftp_mirrors[i].full_name); + for (j = 0; j < ftp_mirrors[i].num_paths; j++) + proz_debug("\t%s\n", ftp_mirrors[i].paths[j].path); + } proz_debug("End display reparsed list\n"); /*TODO free the mirros struct which we will not use now */ return ftp_mirrors; @@ -1034,8 +1032,8 @@ ftp_mirror_t *reprocess_mirror_list(ftp_mirror_t * mirrors, int compare_two_servers(const void *a, const void *b) { - const ftp_mirror_t *ma = (const ftp_mirror_t *) a; - const ftp_mirror_t *mb = (const ftp_mirror_t *) b; + const ftp_mirror_t *ma = (const ftp_mirror_t *)a; + const ftp_mirror_t *mb = (const ftp_mirror_t *)b; int milli_sec_a; int milli_sec_b; @@ -1047,30 +1045,31 @@ int compare_two_servers(const void *a, const void *b) milli_sec_a = ma->milli_secs; if (ma->status != RESPONSEOK) - { - milli_sec_a = 1000000; - } + { + milli_sec_a = 1000000; + } milli_sec_b = mb->milli_secs; if (mb->status != RESPONSEOK) - { - milli_sec_b = 1000000; - } + { + milli_sec_b = 1000000; + } - return (milli_sec_a - milli_sec_b); + return(milli_sec_a - milli_sec_b); } void proz_sort_mirror_list(ftp_mirror_t * mirrors, int num_servers) { int i; + qsort(mirrors, num_servers, sizeof(ftp_mirror_t), compare_two_servers); for (i = 0; i < num_servers; i++) proz_debug("Mirror = %s, time =%d", mirrors[i].server_name, - mirrors[i].milli_secs); + mirrors[i].milli_secs); } @@ -1079,18 +1078,18 @@ void proz_sort_mirror_list(ftp_mirror_t * mirrors, int num_servers) int ftpsearch_get_server_position(ftps_request_t * request, char *server) { int i; - for (i = 0; i < request->num_mirrors; i++) - { - if (strcmp(request->mirrors[i].server_name, server) == 0) - return i; - } + for (i = 0; i < request->num_mirrors; i++) + { + if (strcmp(request->mirrors[i].server_name, server) == 0) + return i; + } return -1; } int ftpsearch_get_path_position(ftps_request_t * request, char *server, - char *path) + char *path) { int i, pos; @@ -1100,13 +1099,11 @@ int ftpsearch_get_path_position(ftps_request_t * request, char *server, proz_debug("num avail paths %d", request->mirrors[pos].num_paths); for (i = 0; i < request->mirrors[pos].num_paths; i++) - { - proz_debug("avail path is %s", request->mirrors[pos].paths[i].path); - proz_debug("path to check is %s", path); - if (strcmp(request->mirrors[pos].paths[i].path, path) == 0) - return i; - - } + { + proz_debug("avail path is %s", request->mirrors[pos].paths[i].path); + proz_debug("path to check is %s", path); + if (strcmp(request->mirrors[pos].paths[i].path, path) == 0) + return i; + } return -1; - } diff --git a/libprozilla/src/ftpsearch.h b/libprozilla/src/ftpsearch.h index 02c4c54..81a1843 100644 --- a/libprozilla/src/ftpsearch.h +++ b/libprozilla/src/ftpsearch.h @@ -1,20 +1,20 @@ /****************************************************************************** - libprozilla - a download accelerator library - Copyright (C) 2001 Kalum Somaratna + libprozilla - a download accelerator library + Copyright (C) 2001 Kalum Somaratna - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ******************************************************************************/ @@ -24,6 +24,6 @@ int ftpsearch_get_server_position(ftps_request_t * request, char *server); int ftpsearch_get_path_position(ftps_request_t * request, char *server, - char *path); + char *path); #endif diff --git a/libprozilla/src/http-retr.c b/libprozilla/src/http-retr.c index 449747d..c592dae 100644 --- a/libprozilla/src/http-retr.c +++ b/libprozilla/src/http-retr.c @@ -1,23 +1,23 @@ /****************************************************************************** - libprozilla - a download accelerator library - Copyright (C) 2001 Kalum Somaratna + libprozilla - a download accelerator library + Copyright (C) 2001 Kalum Somaratna - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ******************************************************************************/ -/* $Id: http-retr.c,v 1.20 2005/03/31 20:10:57 sean Exp $ */ +/* $Id$ */ #include "common.h" #include "prozilla.h" @@ -36,7 +36,7 @@ uerr_t proz_http_get_file(connection_t * connection) uerr_t err; int remote_port_len; char *user, *passwd, *www_auth = NULL, *proxy_auth = NULL, *range = - NULL, *location = NULL, *referer = NULL, *pragma_no_cache = NULL; + NULL, *location = NULL, *referer = NULL, *pragma_no_cache = NULL; char *request, *remote_port; netrc_entry *netrc_ent; char buffer[HTTP_BUFFER_SIZE]; @@ -55,144 +55,148 @@ uerr_t proz_http_get_file(connection_t * connection) /* if there is nothing to download then return */ if (connection->status == COMPLETED) - { - pthread_mutex_lock(&connection->access_mutex); - gettimeofday(&connection->time_begin, NULL); - pthread_mutex_unlock(&connection->access_mutex); - return HOK; - } + { + pthread_mutex_lock(&connection->access_mutex); + gettimeofday(&connection->time_begin, NULL); + pthread_mutex_unlock(&connection->access_mutex); + return HOK; + } connection_change_status(connection, CONNECTING); if (http_use_proxy(connection)) - { - connection_show_message(connection, _("Connecting to %s"), - connection->http_proxy->proxy_url.host); - err = connect_to_server(&connection->data_sock, - connection->http_proxy->proxy_url.host, - connection->http_proxy->proxy_url.port, - &connection->xfer_timeout); - if (err != NOCONERROR) { - proz_debug(_("Error connecting to %s"), - connection->http_proxy->proxy_url.host); - connection_change_status(connection, REMOTEFATAL); - return err; + connection_show_message(connection, _("Connecting to %s"), + connection->http_proxy->proxy_url.host); + err = connect_to_server(&connection->data_sock, + connection->http_proxy->proxy_url.host, + connection->http_proxy->proxy_url.port, + &connection->xfer_timeout); + if (err != NOCONERROR) + { + proz_debug(_("Error connecting to %s"), + connection->http_proxy->proxy_url.host); + connection_change_status(connection, REMOTEFATAL); + return err; + } } - } else - { - connection_show_message(connection, _("Connecting to %s"), - connection->u.host); + else + { + connection_show_message(connection, _("Connecting to %s"), + connection->u.host); - err = connect_to_server(&connection->data_sock, connection->u.host, - connection->u.port, &connection->xfer_timeout); - if (err != NOCONERROR) - { - proz_debug(_("Error connecting to %s"), connection->u.host); - connection_change_status(connection, REMOTEFATAL); - return err; + err = connect_to_server(&connection->data_sock, connection->u.host, + connection->u.port, &connection->xfer_timeout); + if (err != NOCONERROR) + { + proz_debug(_("Error connecting to %s"), connection->u.host); + connection_change_status(connection, REMOTEFATAL); + return err; + } } - } user = connection->u.user; passwd = connection->u.passwd; /* Use .netrc if asked to do so. */ if (connection->use_netrc == TRUE) - { - netrc_ent = search_netrc(libprozrtinfo.netrc_list, connection->u.host); - - if (netrc_ent != NULL) { - user = netrc_ent->account; - passwd = netrc_ent->password; + netrc_ent = search_netrc(libprozrtinfo.netrc_list, connection->u.host); + + if (netrc_ent != NULL) + { + user = netrc_ent->account; + passwd = netrc_ent->password; + } } - } user = user ? user : ""; passwd = passwd ? passwd : ""; if (strlen(user) || strlen(passwd)) - { - /* Construct the necessary header. */ - www_auth = get_basic_auth_str(user, passwd, "Authorization"); - proz_debug(_("Authenticating as user %s password %s"), user, passwd); - proz_debug(_("Authentification string=%s"), www_auth); - } else + { + /* Construct the necessary header. */ + www_auth = get_basic_auth_str(user, passwd, "Authorization"); + proz_debug(_("Authenticating as user %s password %s"), user, passwd); + proz_debug(_("Authentification string=%s"), www_auth); + } + else www_auth = 0; if (http_use_proxy(connection)) - { - if (strlen(connection->http_proxy->username) - || strlen(connection->http_proxy->passwd)) - proxy_auth = - get_basic_auth_str(connection->http_proxy->username, - connection->http_proxy->passwd, - "Proxy-Authorization"); - } + { + if (strlen(connection->http_proxy->username) + || strlen(connection->http_proxy->passwd)) + proxy_auth = + get_basic_auth_str(connection->http_proxy->username, + connection->http_proxy->passwd, + "Proxy-Authorization"); + } if (connection->u.port == 80) - { - remote_port = NULL; - remote_port_len = 0; - } else - { - remote_port = (char *) alloca(64); - remote_port_len = sprintf(remote_port, ":%d", connection->u.port); - } + { + remote_port = NULL; + remote_port_len = 0; + } + else + { + remote_port = (char *)alloca(64); + remote_port_len = sprintf(remote_port, ":%d", connection->u.port); + } if (connection->hs.accept_ranges == 1) - { - range = (char *) alloca(18 + 64); - sprintf(range, "Range: bytes=%lld-\r\n", connection->remote_startpos); - proz_debug("Range = %lld Range = %s",connection->remote_startpos, range); - } + { + range = (char *)alloca(18 + 64); + sprintf(range, "Range: bytes=%lld-\r\n", connection->remote_startpos); + proz_debug("Range = %lld Range = %s", connection->remote_startpos, range); + } if (connection->u.referer) - { - referer = (char *) alloca(13 + strlen(connection->u.referer)); - sprintf(referer, "Referer: %s\r\n", connection->u.referer); - } + { + referer = (char *)alloca(13 + strlen(connection->u.referer)); + sprintf(referer, "Referer: %s\r\n", connection->u.referer); + } /* If we go through a proxy the request for the URL is different */ if (http_use_proxy(connection)) - { - location = (char *) alloca(strlen(connection->u.url) + 1); - strcpy(location, connection->u.url); - } else - { - location = (char *) alloca(strlen(connection->u.path) + 1); - strcpy(location, connection->u.path); - } + { + location = (char *)alloca(strlen(connection->u.url) + 1); + strcpy(location, connection->u.url); + } + else + { + location = (char *)alloca(strlen(connection->u.path) + 1); + strcpy(location, connection->u.path); + } /*Use no-cache directive for proxy servers? */ if (http_use_proxy(connection) && (connection->http_no_cache || connection->attempts > 0)) - { - pragma_no_cache = (char *) alloca(21); - sprintf(pragma_no_cache, "Pragma: no-cache\r\n"); - } + { + pragma_no_cache = (char *)alloca(21); + sprintf(pragma_no_cache, "Pragma: no-cache\r\n"); + } - request = (char *) alloca(strlen(location) - + strlen(connection->user_agent) - + strlen(connection->u.host) + remote_port_len - + (range ? strlen(range) : 0) - + (referer ? strlen(referer) : 0) - + (www_auth ? strlen(www_auth) : 0) - + (proxy_auth ? strlen(proxy_auth) : 0) - + 64 - + - (pragma_no_cache ? strlen(pragma_no_cache) : - 0)); + request = (char *)alloca(strlen(location) + + strlen(connection->user_agent) + + strlen(connection->u.host) + remote_port_len + + (range ? strlen(range) : 0) + + (referer ? strlen(referer) : 0) + + (www_auth ? strlen(www_auth) : 0) + + (proxy_auth ? strlen(proxy_auth) : 0) + + 64 + + + (pragma_no_cache ? strlen(pragma_no_cache) : + 0)); /* TODO Add referrer tag. */ sprintf(request, - "GET %s HTTP/1.0\r\nUser-Agent: %s\r\nHost: %s%s\r\nAccept: */*\r\n%s%s%s%s%s\r\n", - location, connection->user_agent, connection->u.host, - remote_port ? remote_port : "", range ? range : "", - referer ? referer : "", - www_auth ? www_auth : "", proxy_auth ? proxy_auth : "", - pragma_no_cache ? pragma_no_cache : ""); + "GET %s HTTP/1.0\r\nUser-Agent: %s\r\nHost: %s%s\r\nAccept: */*\r\n%s%s%s%s%s\r\n", + location, connection->user_agent, connection->u.host, + remote_port ? remote_port : "", range ? range : "", + referer ? referer : "", + www_auth ? www_auth : "", proxy_auth ? proxy_auth : "", + pragma_no_cache ? pragma_no_cache : ""); proz_debug("2 HTTP request = %s", request); @@ -201,41 +205,41 @@ uerr_t proz_http_get_file(connection_t * connection) /* What hapenned ? */ if (err != HOK) - { - proz_debug("2 http_fetch_headers err != HOK %d", err); - /*Check if we authenticated using any user or password and if we - were kicked out, if so return HAUTHFAIL */ - if (err == HAUTHREQ && (strlen(user) || strlen(passwd))) - err = HAUTHFAIL; - /* - * a error occured druing the process - */ - close_sock(&connection->data_sock); - connection_change_status(connection, REMOTEFATAL); - return err; - } + { + proz_debug("2 http_fetch_headers err != HOK %d", err); + /*Check if we authenticated using any user or password and if we + were kicked out, if so return HAUTHFAIL */ + if (err == HAUTHREQ && (strlen(user) || strlen(passwd))) + err = HAUTHFAIL; + /* + * a error occured druing the process + */ + close_sock(&connection->data_sock); + connection_change_status(connection, REMOTEFATAL); + return err; + } /*Check for the server lying about it being able to handle ranges */ if (hs_after_get.contlen != -1) - { - if (connection->resume_support == TRUE) { - if (hs_after_get.contlen != - connection->main_file_size - connection->remote_startpos) - { - proz_debug("Error contlen does not match the requested range!"); - close_sock(&connection->data_sock); - connection_change_status(connection, REMOTEFATAL); - return CANTRESUME; - } + if (connection->resume_support == TRUE) + { + if (hs_after_get.contlen != + connection->main_file_size - connection->remote_startpos) + { + proz_debug("Error contlen does not match the requested range!"); + close_sock(&connection->data_sock); + connection_change_status(connection, REMOTEFATAL); + return CANTRESUME; + } + } } - } /* which routine to call */ if (connection->main_file_size == -1) err = - connection_retr_fsize_not_known(connection, buffer, - sizeof(buffer)); + connection_retr_fsize_not_known(connection, buffer, + sizeof(buffer)); else err = connection_retr_fsize_known(connection, buffer, sizeof(buffer)); @@ -244,105 +248,103 @@ uerr_t proz_http_get_file(connection_t * connection) if (err == FILEGETOK) return HOK; else - { - proz_debug("err != FILEGETOK %d", err); - return err; - } + { + proz_debug("err != FILEGETOK %d", err); + return err; + } } -/* A genuine loop ;) It willed be called by the main thread, and -this will handle all possible errors itself, retrying until the number -of maximum tries for the connection is realised -*/ +/* A genuine loop ;) It willed be called by the main thread, and + this will handle all possible errors itself, retrying until the number + of maximum tries for the connection is realised + */ uerr_t http_loop(connection_t * connection) { boolean retrying_from_loop = FALSE; + assert(connection->max_attempts >= 0); assert(connection->attempts >= 0); pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); do - { - if (connection->attempts > 0) { + if (connection->attempts > 0) + { + if (retrying_from_loop == TRUE) + { + connection_show_message(connection, + _("Retrying...Attempt %d in %d seconds"), + connection->attempts, + connection->retry_delay.tv_sec); + delay_ms(connection->retry_delay.tv_sec * 1000); + } - if (retrying_from_loop == TRUE) - { - connection_show_message(connection, - _("Retrying...Attempt %d in %d seconds"), - connection->attempts, - connection->retry_delay.tv_sec); - delay_ms(connection->retry_delay.tv_sec * 1000); - } + if (connection->resume_support == TRUE) + { + if (connection_load_resume_info(connection) == -1) + { + connection_show_message(connection, + _ + ("Error while attemting to process download file ")); + } + } + else + { + /*If we cant resume then reset the connections bytesreceived to 0 */ + connection->remote_bytes_received = 0; + } + } - if (connection->resume_support == TRUE) - { - if (connection_load_resume_info(connection) == -1) - { - connection_show_message(connection, - _ - ("Error while attemting to process download file ")); - } - } else - { - /*If we cant resume then reset the connections bytesreceived to 0 */ - connection->remote_bytes_received = 0; - } - } + /*Push the handler which will cleanup any sockets that are left open */ + pthread_cleanup_push(cleanup_socks, (void *)connection); + connection->err = proz_http_get_file(connection); + /*pop the handler */ + pthread_cleanup_pop(0); - /*Push the handler which will cleanup any sockets that are left open */ - pthread_cleanup_push(cleanup_socks, (void *) connection); - connection->err = proz_http_get_file(connection); - /*pop the handler */ - pthread_cleanup_pop(0); + connection->attempts++; - connection->attempts++; + /*Should the error be handled at this level ? */ + if (!http_loop_handle_error(connection->err)) + { + connection_show_message(connection, _("Will be handled in main ")); + return connection->err; /*If not return and the main thread will handle it */ + } - /*Should the error be handled at this level ? */ - if (!http_loop_handle_error(connection->err)) - { - connection_show_message(connection, _("Will be handled in main ")); - return connection->err; /*If not return and the main thread will handle it */ - } + switch (connection->err) + { + case HOK: + connection_show_message(connection, _("Successfully got download")); + return connection->err; + break; - switch (connection->err) - { - case HOK: - connection_show_message(connection, _("Successfully got download")); - return connection->err; - break; + /*TODO : What should we do if the file is not found, well pass it up to the main routine */ - /*TODO : What should we do if the file is not found, well pass it up to the main routine */ + default: + connection_show_message(connection, proz_strerror(connection->err)); + break; + } - default: - connection_show_message(connection, proz_strerror(connection->err)); - break; - } - - retrying_from_loop = TRUE; - - } - while ((connection->attempts < connection->max_attempts) - || connection->max_attempts == 0); + retrying_from_loop = TRUE; + } while ((connection->attempts < connection->max_attempts) + || connection->max_attempts == 0); connection_show_message(connection, - _ - ("I have tried %d attempt(s) and have failed, aborting"), - connection->attempts); + _ + ("I have tried %d attempt(s) and have failed, aborting"), + connection->attempts); return connection->err; - } /*Return true if it is a error which can be handled within the htp_loop, -or false if it should be passed upwards so that the main download thread can -restart it when necessary after processing other threads status too */ + or false if it should be passed upwards so that the main download thread can + restart it when necessary after processing other threads status too */ boolean http_loop_handle_error(uerr_t err) { proz_debug("Error encountered in http_loop is %d", err); @@ -355,17 +357,16 @@ boolean http_loop_handle_error(uerr_t err) /* - I am writing a seperate function to handle FTP proxying through HTTP, I - MHO whoever thought of using HTTP to proxy FTP is a shithead, - its such a PITA ;) + I am writing a seperate function to handle FTP proxying through HTTP, I + MHO whoever thought of using HTTP to proxy FTP is a shithead, + its such a PITA ;) */ uerr_t ftp_get_file_from_http_proxy(connection_t * connection) { - uerr_t err; int remote_port_len; char *user, *passwd, *www_auth = NULL, *proxy_auth = NULL, *range = - NULL, *pragma_no_cache = NULL; + NULL, *pragma_no_cache = NULL; char *request, *remote_port; netrc_entry *netrc_ent; @@ -381,100 +382,101 @@ uerr_t ftp_get_file_from_http_proxy(connection_t * connection) /* if there is nothing to download then return */ if (connection->status == COMPLETED) - { - pthread_mutex_lock(&connection->access_mutex); - gettimeofday(&connection->time_begin, NULL); - pthread_mutex_unlock(&connection->access_mutex); - return FTPOK; - } + { + pthread_mutex_lock(&connection->access_mutex); + gettimeofday(&connection->time_begin, NULL); + pthread_mutex_unlock(&connection->access_mutex); + return FTPOK; + } err = connect_to_server(&connection->data_sock, - connection->ftp_proxy->proxy_url.host, - connection->ftp_proxy->proxy_url.port, - &connection->xfer_timeout); + connection->ftp_proxy->proxy_url.host, + connection->ftp_proxy->proxy_url.port, + &connection->xfer_timeout); if (err != NOCONERROR) - { - connection_show_message(connection, _("Error connecting to %s"), - connection->ftp_proxy->proxy_url.host); - return err; - } + { + connection_show_message(connection, _("Error connecting to %s"), + connection->ftp_proxy->proxy_url.host); + return err; + } user = connection->u.user; passwd = connection->u.passwd; /* Use .netrc if asked to do so. */ if (connection->use_netrc == TRUE) - { - netrc_ent = search_netrc(libprozrtinfo.netrc_list, connection->u.host); - - if (netrc_ent != NULL) { - user = netrc_ent->account; - passwd = netrc_ent->password; + netrc_ent = search_netrc(libprozrtinfo.netrc_list, connection->u.host); + + if (netrc_ent != NULL) + { + user = netrc_ent->account; + passwd = netrc_ent->password; + } } - } user = user ? user : ""; passwd = passwd ? passwd : ""; if (strlen(user) || strlen(passwd)) - { - /* Construct the necessary header. */ - www_auth = get_basic_auth_str(user, passwd, "Authorization"); - proz_debug(_("Authenticating as user %s password %s"), user, passwd); - proz_debug(_("Authentification string=%s"), www_auth); - } else + { + /* Construct the necessary header. */ + www_auth = get_basic_auth_str(user, passwd, "Authorization"); + proz_debug(_("Authenticating as user %s password %s"), user, passwd); + proz_debug(_("Authentification string=%s"), www_auth); + } + else www_auth = 0; if (strlen(connection->ftp_proxy->username) || strlen(connection->ftp_proxy->passwd)) proxy_auth = - get_basic_auth_str(connection->ftp_proxy->username, - connection->ftp_proxy->passwd, - "Proxy-Authorization"); + get_basic_auth_str(connection->ftp_proxy->username, + connection->ftp_proxy->passwd, + "Proxy-Authorization"); - remote_port = (char *) alloca(64); + remote_port = (char *)alloca(64); remote_port_len = sprintf(remote_port, ":%d", connection->u.port); if (connection->hs.accept_ranges == 1) - { - range = (char *) alloca(18 + 64); - sprintf(range, "Range: bytes=%lld-\r\n", connection->remote_startpos); - } + { + range = (char *)alloca(18 + 64); + sprintf(range, "Range: bytes=%lld-\r\n", connection->remote_startpos); + } /*Use no-cache directive for proxy servers? */ if (http_use_proxy(connection) && (connection->http_no_cache || connection->attempts > 0)) - { - pragma_no_cache = (char *) alloca(21); - sprintf(pragma_no_cache, "Pragma: no-cache\r\n"); - } + { + pragma_no_cache = (char *)alloca(21); + sprintf(pragma_no_cache, "Pragma: no-cache\r\n"); + } /*Referrer TAG should not be needed in FTP through HTTP proxy..right */ - request = (char *) alloca(strlen(connection->u.url) - + strlen(connection->user_agent) - + strlen(connection->u.host) + remote_port_len - + (range ? strlen(range) : 0) - + (www_auth ? strlen(www_auth) : 0) - + (proxy_auth ? strlen(proxy_auth) : 0) - + 64 - + - (pragma_no_cache ? strlen(pragma_no_cache) : - 0)); + request = (char *)alloca(strlen(connection->u.url) + + strlen(connection->user_agent) + + strlen(connection->u.host) + remote_port_len + + (range ? strlen(range) : 0) + + (www_auth ? strlen(www_auth) : 0) + + (proxy_auth ? strlen(proxy_auth) : 0) + + 64 + + + (pragma_no_cache ? strlen(pragma_no_cache) : + 0)); /* TODO Add referrer tag. */ sprintf(request, - "GET %s HTTP/1.0\r\nUser-Agent: %s\r\nHost: %s%s\r\nAccept: */*\r\n%s%s%s%s\r\n", - connection->u.url, connection->user_agent, connection->u.host, - remote_port ? remote_port : "", range ? range : "", - www_auth ? www_auth : "", proxy_auth ? proxy_auth : "", - pragma_no_cache ? pragma_no_cache : ""); + "GET %s HTTP/1.0\r\nUser-Agent: %s\r\nHost: %s%s\r\nAccept: */*\r\n%s%s%s%s\r\n", + connection->u.url, connection->user_agent, connection->u.host, + remote_port ? remote_port : "", range ? range : "", + www_auth ? www_auth : "", proxy_auth ? proxy_auth : "", + pragma_no_cache ? pragma_no_cache : ""); proz_debug("HTTP request = %s", request); @@ -484,39 +486,41 @@ uerr_t ftp_get_file_from_http_proxy(connection_t * connection) if (err == HAUTHREQ) - { - connection_change_status(connection, LOGINFAIL); - return FTPLOGREFUSED; - } else if (err == HTTPNSFOD) - { - connection_change_status(connection, REMOTEFATAL); - return FTPNSFOD; - } else if (err != HOK) - { - connection_change_status(connection, REMOTEFATAL); - return FTPERR; - } + { + connection_change_status(connection, LOGINFAIL); + return FTPLOGREFUSED; + } + else if (err == HTTPNSFOD) + { + connection_change_status(connection, REMOTEFATAL); + return FTPNSFOD; + } + else if (err != HOK) + { + connection_change_status(connection, REMOTEFATAL); + return FTPERR; + } /*Check for the server lying about it being able to handle ranges */ if (hs_after_get.contlen != -1) - { - if (connection->resume_support == TRUE) { - if (hs_after_get.contlen != - connection->main_file_size - connection->remote_startpos) - { - proz_debug("Error contlen does not match the requested range!"); - connection_change_status(connection, REMOTEFATAL); - return CANTRESUME; - } + if (connection->resume_support == TRUE) + { + if (hs_after_get.contlen != + connection->main_file_size - connection->remote_startpos) + { + proz_debug("Error contlen does not match the requested range!"); + connection_change_status(connection, REMOTEFATAL); + return CANTRESUME; + } + } } - } /* which routine to call */ if (connection->main_file_size == -1) err = - connection_retr_fsize_not_known(connection, buffer, - sizeof(buffer)); + connection_retr_fsize_not_known(connection, buffer, + sizeof(buffer)); else err = connection_retr_fsize_known(connection, buffer, sizeof(buffer)); @@ -526,5 +530,4 @@ uerr_t ftp_get_file_from_http_proxy(connection_t * connection) return FTPOK; else return err; - } diff --git a/libprozilla/src/http-retr.h b/libprozilla/src/http-retr.h index 34e4672..cd6bb0d 100644 --- a/libprozilla/src/http-retr.h +++ b/libprozilla/src/http-retr.h @@ -1,23 +1,23 @@ /****************************************************************************** - libprozilla - a download accelerator library - Copyright (C) 2001 Kalum Somaratna + libprozilla - a download accelerator library + Copyright (C) 2001 Kalum Somaratna - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ******************************************************************************/ -/* $Id: http-retr.h,v 1.5 2001/06/25 12:30:56 kalum Exp $ */ +/* $Id$ */ #ifndef HTTP_RETR_H @@ -30,10 +30,10 @@ extern "C" { #endif - uerr_t proz_http_get_file(connection_t * connection); - uerr_t http_loop(connection_t * connection); - boolean http_loop_handle_error(uerr_t err); +uerr_t proz_http_get_file(connection_t * connection); +uerr_t http_loop(connection_t * connection); +boolean http_loop_handle_error(uerr_t err); #ifdef __cplusplus } #endif -#endif /* FTP_RETR_H */ +#endif /* FTP_RETR_H */ diff --git a/libprozilla/src/http.c b/libprozilla/src/http.c index 75f9c11..a620b91 100644 --- a/libprozilla/src/http.c +++ b/libprozilla/src/http.c @@ -1,25 +1,25 @@ /****************************************************************************** - libprozilla - a download accelerator library - Copyright (C) 2001 Kalum Somaratna + libprozilla - a download accelerator library + Copyright (C) 2001 Kalum Somaratna - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ******************************************************************************/ /* HTTP support. */ -/* $Id: http.c,v 1.22 2005/03/31 20:10:57 sean Exp $ */ +/* $Id$ */ #include "common.h" @@ -68,7 +68,7 @@ /****************************************************************************** - ... + ... ******************************************************************************/ int buf_readchar(int fd, char *ret, struct timeval *timeout) { @@ -83,7 +83,7 @@ int buf_readchar(int fd, char *ret, struct timeval *timeout) } /****************************************************************************** - This is similar to buf_readchar(), only it doesn't move the buffer position. + This is similar to buf_readchar(), only it doesn't move the buffer position. ******************************************************************************/ int buf_peek(int fd, char *ret, struct timeval *timeout) { @@ -98,16 +98,16 @@ int buf_peek(int fd, char *ret, struct timeval *timeout) } /****************************************************************************** - Function to fetch a header from socket/file descriptor fd. The header may be - of arbitrary length, since the function allocates as much memory as necessary - for the header to fit. Most errors are handled. + Function to fetch a header from socket/file descriptor fd. The header may be + of arbitrary length, since the function allocates as much memory as necessary + for the header to fit. Most errors are handled. - The header may be terminated by LF or CRLF. If the character after LF is SP - or HT (horizontal tab), the header spans to another line (continuation - header), as per RFC2068. + The header may be terminated by LF or CRLF. If the character after LF is SP + or HT (horizontal tab), the header spans to another line (continuation + header), as per RFC2068. - The trailing CRLF or LF are stripped from the header, and it is - zero-terminated. + The trailing CRLF or LF are stripped from the header, and it is + zero-terminated. ******************************************************************************/ uerr_t fetch_next_header(int fd, char **hdr, struct timeval * timeout) { @@ -118,58 +118,59 @@ uerr_t fetch_next_header(int fd, char **hdr, struct timeval * timeout) *hdr = kmalloc(bufsize); for (i = 0; 1; i++) - { - if (i > bufsize - 1) - *hdr = krealloc(*hdr, (bufsize <<= 1)); - - res = buf_readchar(fd, *hdr + i, timeout); - - if (res == 1) { - if ((*hdr)[i] == '\n') - { - if (!(i == 0 || (i == 1 && (*hdr)[0] == '\r'))) - { - /* If the header is non-empty, we need to check if it continues on - to the other line. We do that by getting the next character - without actually downloading it (i.e. peeking it). */ - res = buf_peek(fd, &next, timeout); + if (i > bufsize - 1) + *hdr = krealloc(*hdr, (bufsize <<= 1)); - if (res == 0) - return HEOF; - else if (res == -1) - return HERR; + res = buf_readchar(fd, *hdr + i, timeout); - /* If the next character is SP or HT, just continue. */ - if (next == '\t' || next == ' ') - continue; - } + if (res == 1) + { + if ((*hdr)[i] == '\n') + { + if (!(i == 0 || (i == 1 && (*hdr)[0] == '\r'))) + { + /* If the header is non-empty, we need to check if it continues on + to the other line. We do that by getting the next character + without actually downloading it (i.e. peeking it). */ + res = buf_peek(fd, &next, timeout); - /* The header ends. */ - (*hdr)[i] = '\0'; + if (res == 0) + return HEOF; + else if (res == -1) + return HERR; - /* Get rid of '\r'. */ - if (i > 0 && (*hdr)[i - 1] == '\r') - (*hdr)[i - 1] = '\0'; + /* If the next character is SP or HT, just continue. */ + if (next == '\t' || next == ' ') + continue; + } - break; - } - } else if (res == 0) - return HEOF; - else - return HERR; - } + /* The header ends. */ + (*hdr)[i] = '\0'; + + /* Get rid of '\r'. */ + if (i > 0 && (*hdr)[i - 1] == '\r') + (*hdr)[i - 1] = '\0'; + + break; + } + } + else if (res == 0) + return HEOF; + else + return HERR; + } return HOK; } /****************************************************************************** - ... + ... ******************************************************************************/ int hparsestatline(const char *hdr, const char **rp) { - int mjr, mnr; /* HTTP major and minor version. */ - int statcode; /* HTTP status code. */ + int mjr, mnr; /* HTTP major and minor version. */ + int statcode; /* HTTP status code. */ const char *p; *rp = NULL; @@ -211,20 +212,21 @@ int hparsestatline(const char *hdr, const char **rp) servers/CGI are (incorrectly) setup to drop the SPC, we'll be liberal and allow the status line to end here. */ if (hdr[3] != ' ') - { - if (!hdr[3]) - *rp = hdr + 3; - else - return -1; - } else + { + if (!hdr[3]) + *rp = hdr + 3; + else + return -1; + } + else *rp = hdr + 4; return statcode; } /****************************************************************************** - Skip LWS (linear white space), if present. Returns number of characters to - skip. + Skip LWS (linear white space), if present. Returns number of characters to + skip. ******************************************************************************/ int hskip_lws(const char *hdr) { @@ -238,12 +240,12 @@ int hskip_lws(const char *hdr) } /****************************************************************************** - Return the content length of the document body, if this is Content-length - header, -1 otherwise. + Return the content length of the document body, if this is Content-length + header, -1 otherwise. ******************************************************************************/ off_t hgetlen(const char *hdr) { - const int l = 15; /* strlen("content-length:"). */ + const int l = 15; /* strlen("content-length:"). */ off_t len; if (strncasecmp(hdr, "content-length:", l)) @@ -259,17 +261,17 @@ off_t hgetlen(const char *hdr) for (len = 0; isdigit(*hdr); hdr++) len = 10 * len + (*hdr - '0'); - proz_debug("contenlen %s contentlen %lld",*hdr,len); + proz_debug("contenlen %s contentlen %lld", *hdr, len); return len; } /****************************************************************************** - Return the content-range in bytes, as returned by the server, if this is - Content-range header, -1 otherwise. + Return the content-range in bytes, as returned by the server, if this is + Content-range header, -1 otherwise. ******************************************************************************/ off_t hgetrange(const char *hdr) { - const int l = 14; /* strlen("content-range:"). */ + const int l = 14; /* strlen("content-range:"). */ off_t len; if (strncasecmp(hdr, "content-range:", l)) @@ -282,12 +284,12 @@ off_t hgetrange(const char *hdr) /* Nutscape proxy server sends content-length without "bytes" specifier, which is a breach of HTTP/1.1 draft. But heck, I must support it... */ if (!strncasecmp(hdr, "bytes", 5)) - { - hdr += 5; - hdr += hskip_lws(hdr); - if (!*hdr) - return -1; - } + { + hdr += 5; + hdr += hskip_lws(hdr); + if (!*hdr) + return -1; + } if (!isdigit(*hdr)) return -1; @@ -295,17 +297,17 @@ off_t hgetrange(const char *hdr) for (len = 0; isdigit(*hdr); hdr++) len = 10 * len + (*hdr - '0'); - proz_debug("range %s range %lld",*hdr,len); + proz_debug("range %s range %lld", *hdr, len); return len; } /****************************************************************************** - Returns a malloc-ed copy of the location of the document, if the string hdr - begins with LOCATION_H, or NULL. + Returns a malloc-ed copy of the location of the document, if the string hdr + begins with LOCATION_H, or NULL. ******************************************************************************/ char *hgetlocation(const char *hdr) { - const int l = 9; /* strlen("location:"). */ + const int l = 9; /* strlen("location:"). */ if (strncasecmp(hdr, "location:", l)) return NULL; @@ -316,12 +318,12 @@ char *hgetlocation(const char *hdr) } /****************************************************************************** - Returns a malloc-ed copy of the last-modified date of the document, if the - hdr begins with LASTMODIFIED_H. + Returns a malloc-ed copy of the last-modified date of the document, if the + hdr begins with LASTMODIFIED_H. ******************************************************************************/ char *hgetmodified(const char *hdr) { - const int l = 14; /* strlen("last-modified:"). */ + const int l = 14; /* strlen("last-modified:"). */ if (strncasecmp(hdr, "last-modified:", l)) return NULL; @@ -332,13 +334,13 @@ char *hgetmodified(const char *hdr) } /****************************************************************************** - Returns 0 if the header is accept-ranges, and it contains the word "none", - -1 if there is no accept ranges, 1 is there is accept-ranges and it is not - none. + Returns 0 if the header is accept-ranges, and it contains the word "none", + -1 if there is no accept ranges, 1 is there is accept-ranges and it is not + none. ******************************************************************************/ int hgetaccept_ranges(const char *hdr) { - const int l = 14; /* strlen("accept-ranges:"). */ + const int l = 14; /* strlen("accept-ranges:"). */ if (strncasecmp(hdr, "accept-ranges:", l)) return -1; @@ -352,10 +354,10 @@ int hgetaccept_ranges(const char *hdr) } /****************************************************************************** - ... + ... ******************************************************************************/ uerr_t http_fetch_headers(connection_t * connection, http_stat_t * hs, - char *command) + char *command) { uerr_t err; int num_written, hcount, statcode, all_length; @@ -372,12 +374,12 @@ uerr_t http_fetch_headers(connection_t * connection, http_stat_t * hs, hs->error = NULL; num_written = ksend(connection->data_sock, command, strlen(command), 0, - &connection->xfer_timeout); + &connection->xfer_timeout); if (num_written != strlen(command)) - { - proz_debug(_("Failed writing HTTP request")); - return WRITEERR; - } + { + proz_debug(_("Failed writing HTTP request")); + return WRITEERR; + } all_headers = NULL; all_length = 0; @@ -388,105 +390,106 @@ uerr_t http_fetch_headers(connection_t * connection, http_stat_t * hs, /* Header-fetching loop. */ hcount = 0; - for (;;) - { - ++hcount; - - /* Get the header. */ - err = fetch_next_header(connection->data_sock, &hdr, - &connection->xfer_timeout); - - proz_debug(_("Header = %s"), hdr); - - if (err == HEOF) + for (;; ) { - proz_debug(_("End of file while parsing headers")); + ++hcount; + + /* Get the header. */ + err = fetch_next_header(connection->data_sock, &hdr, + &connection->xfer_timeout); + + proz_debug(_("Header = %s"), hdr); + + if (err == HEOF) + { + proz_debug(_("End of file while parsing headers")); + + kfree(hdr); + if (type) + kfree(type); + if (all_headers) + kfree(all_headers); + + return HEOF; + } + else if (err == HERR) + { + proz_debug(_("Read error in headers")); + + kfree(hdr); + if (type) + kfree(type); + if (all_headers) + kfree(all_headers); + + return HERR; + } + + /* Exit on empty header. */ + if (!*hdr) + { + kfree(hdr); + break; + } + + /* Check for errors documented in the first header. */ + if (hcount == 1) + { + statcode = hparsestatline(hdr, &error); + hs->statcode = statcode; + + /* Store the descriptive response. */ + if (statcode == -1) /* Malformed request. */ + hs->error = kstrdup(_("UNKNOWN")); + else if (!*error) + hs->error = kstrdup(_("(no description)")); + else + hs->error = kstrdup(error); + } + + if (contlen == -1) + { + contlen = hgetlen(hdr); + hs->contlen = contlen; + } + + /* If the server specified a new location then lets store it. */ + + if (!hs->newloc) + hs->newloc = hgetlocation(hdr); + + if (!hs->remote_time) + hs->remote_time = hgetmodified(hdr); + + if (hs->accept_ranges == -1) + hs->accept_ranges = hgetaccept_ranges(hdr); + + if (!hs->newloc) + hs->newloc = hgetlocation(hdr); kfree(hdr); - if (type) - kfree(type); - if (all_headers) - kfree(all_headers); - - return HEOF; - } else if (err == HERR) - { - proz_debug(_("Read error in headers")); - - kfree(hdr); - if (type) - kfree(type); - if (all_headers) - kfree(all_headers); - - return HERR; } - /* Exit on empty header. */ - if (!*hdr) - { - kfree(hdr); - break; - } - - /* Check for errors documented in the first header. */ - if (hcount == 1) - { - statcode = hparsestatline(hdr, &error); - hs->statcode = statcode; - - /* Store the descriptive response. */ - if (statcode == -1) /* Malformed request. */ - hs->error = kstrdup(_("UNKNOWN")); - else if (!*error) - hs->error = kstrdup(_("(no description)")); - else - hs->error = kstrdup(error); - } - - if (contlen == -1) - { - contlen = hgetlen(hdr); - hs->contlen = contlen; - } - - /* If the server specified a new location then lets store it. */ - - if (!hs->newloc) - hs->newloc = hgetlocation(hdr); - - if (!hs->remote_time) - hs->remote_time = hgetmodified(hdr); - - if (hs->accept_ranges == -1) - hs->accept_ranges = hgetaccept_ranges(hdr); - - if (!hs->newloc) - hs->newloc = hgetlocation(hdr); - - kfree(hdr); - } - if (H_20X(statcode)) return HOK; if (H_REDIRECTED(statcode) || statcode == HTTP_MULTIPLE_CHOICES) - { - /* RFC2068 says that in case of the 300 (multiple choices) response, the - server can output a preferred URL through `Location' header; otherwise, - the request should be treated like GET. So, if the location is set, it - will be a redirection; otherwise, just proceed normally. */ - if (statcode == HTTP_MULTIPLE_CHOICES && !hs->newloc) - return HOK; - else { - if (all_headers) - kfree(all_headers); - if (type) - kfree(type); - return NEWLOCATION; + /* RFC2068 says that in case of the 300 (multiple choices) response, the + server can output a preferred URL through `Location' header; otherwise, + the request should be treated like GET. So, if the location is set, it + will be a redirection; otherwise, just proceed normally. */ + if (statcode == HTTP_MULTIPLE_CHOICES && !hs->newloc) + return HOK; + else + { + if (all_headers) + kfree(all_headers); + if (type) + kfree(type); + return NEWLOCATION; + } } - } if (statcode == HTTP_UNAUTHORIZED) return HAUTHREQ; @@ -513,7 +516,7 @@ uerr_t http_fetch_headers(connection_t * connection, http_stat_t * hs, } /****************************************************************************** - ... + ... ******************************************************************************/ char *get_basic_auth_str(char *user, char *passwd, char *auth_header) { @@ -537,147 +540,151 @@ char *get_basic_auth_str(char *user, char *passwd, char *auth_header) } /****************************************************************************** - ... + ... ******************************************************************************/ boolean http_use_proxy(connection_t * connection) { return (connection->http_proxy && connection->http_proxy->use_proxy - && connection->http_proxy->proxy_url.url) ? TRUE : FALSE; + && connection->http_proxy->proxy_url.url) ? TRUE : FALSE; } /****************************************************************************** - ... + ... ******************************************************************************/ uerr_t proz_http_get_url_info(connection_t * connection) { uerr_t err; int remote_port_len; char *user, *passwd, *www_auth = NULL, *proxy_auth = NULL, - *referer = NULL, *location = NULL, *pragma_no_cache = NULL; + *referer = NULL, *location = NULL, *pragma_no_cache = NULL; char *request, *remote_port; netrc_entry *netrc_ent; memset(&connection->hs, 0, sizeof(connection->hs)); if (http_use_proxy(connection)) - { - connection_show_message(connection, _("Connecting to %s"), - connection->http_proxy->proxy_url.host); + { + connection_show_message(connection, _("Connecting to %s"), + connection->http_proxy->proxy_url.host); - err = connect_to_server(&connection->data_sock, - connection->http_proxy->proxy_url.host, - connection->http_proxy->proxy_url.port, - &connection->xfer_timeout); - if (err != NOCONERROR) - { - connection_show_message(connection, _("Error connecting to %s"), - connection->http_proxy->proxy_url.host); - return err; + err = connect_to_server(&connection->data_sock, + connection->http_proxy->proxy_url.host, + connection->http_proxy->proxy_url.port, + &connection->xfer_timeout); + if (err != NOCONERROR) + { + connection_show_message(connection, _("Error connecting to %s"), + connection->http_proxy->proxy_url.host); + return err; + } } - } else - { - connection_show_message(connection, _("Connecting to %s"), - connection->u.host); - err = connect_to_server(&connection->data_sock, connection->u.host, - connection->u.port, &connection->xfer_timeout); - if (err != NOCONERROR) + else { - connection_show_message(connection, _("Error connecting to %s"), - connection->u.host); - return err; + connection_show_message(connection, _("Connecting to %s"), + connection->u.host); + err = connect_to_server(&connection->data_sock, connection->u.host, + connection->u.port, &connection->xfer_timeout); + if (err != NOCONERROR) + { + connection_show_message(connection, _("Error connecting to %s"), + connection->u.host); + return err; + } } - } user = connection->u.user; passwd = connection->u.passwd; /* Use .netrc if asked to do so. */ if (connection->use_netrc == TRUE) - { - netrc_ent = search_netrc(libprozrtinfo.netrc_list, connection->u.host); - - if (netrc_ent != NULL) { - user = netrc_ent->account; - passwd = netrc_ent->password; + netrc_ent = search_netrc(libprozrtinfo.netrc_list, connection->u.host); + + if (netrc_ent != NULL) + { + user = netrc_ent->account; + passwd = netrc_ent->password; + } } - } user = user ? user : ""; passwd = passwd ? passwd : ""; if (strlen(user) || strlen(passwd)) - { - /* Construct the necessary header. */ - www_auth = get_basic_auth_str(user, passwd, "Authorization"); - proz_debug(_("Authenticating as user %s password %s"), user, passwd); - proz_debug(_("Authentification string=%s"), www_auth); - } else + { + /* Construct the necessary header. */ + www_auth = get_basic_auth_str(user, passwd, "Authorization"); + proz_debug(_("Authenticating as user %s password %s"), user, passwd); + proz_debug(_("Authentification string=%s"), www_auth); + } + else www_auth = 0; if (http_use_proxy(connection)) - { - if (strlen(connection->http_proxy->username) - || strlen(connection->http_proxy->passwd)) - proxy_auth = - get_basic_auth_str(connection->http_proxy->username, - connection->http_proxy->passwd, - "Proxy-Authorization"); - } + { + if (strlen(connection->http_proxy->username) + || strlen(connection->http_proxy->passwd)) + proxy_auth = + get_basic_auth_str(connection->http_proxy->username, + connection->http_proxy->passwd, + "Proxy-Authorization"); + } if (connection->u.port == 80) - { - remote_port = NULL; - remote_port_len = 0; - } else - { - remote_port = (char *) alloca(64); - remote_port_len = sprintf(remote_port, ":%d", connection->u.port); - } + { + remote_port = NULL; + remote_port_len = 0; + } + else + { + remote_port = (char *)alloca(64); + remote_port_len = sprintf(remote_port, ":%d", connection->u.port); + } if (connection->u.referer) - { - referer = (char *) alloca(13 + strlen(connection->u.referer)); - sprintf(referer, "Referer: %s\r\n", connection->u.referer); - } + { + referer = (char *)alloca(13 + strlen(connection->u.referer)); + sprintf(referer, "Referer: %s\r\n", connection->u.referer); + } /* If we go through a proxy the request for the URL is different */ if (http_use_proxy(connection)) - { - location = (char *) alloca(strlen(connection->u.url) + 1); - strcpy(location, connection->u.url); - } else - { - location = (char *) alloca(strlen(connection->u.path) + 1); - strcpy(location, connection->u.path); - } + { + location = (char *)alloca(strlen(connection->u.url) + 1); + strcpy(location, connection->u.url); + } + else + { + location = (char *)alloca(strlen(connection->u.path) + 1); + strcpy(location, connection->u.path); + } /*Use no-cache directive for proxy servers? */ if (http_use_proxy(connection) && (connection->http_no_cache || connection->attempts > 0)) - { - pragma_no_cache = (char *) alloca(21); - sprintf(pragma_no_cache, "Pragma: no-cache\r\n"); - } + { + pragma_no_cache = (char *)alloca(21); + sprintf(pragma_no_cache, "Pragma: no-cache\r\n"); + } - request = (char *) alloca(strlen(location) - + strlen(connection->user_agent) - + strlen(connection->u.host) + remote_port_len - + (referer ? strlen(referer) : 0) - + (www_auth ? strlen(www_auth) : 0) - + (proxy_auth ? strlen(proxy_auth) : 0) + 64 - + - (pragma_no_cache ? strlen(pragma_no_cache) : - 0)); + request = (char *)alloca(strlen(location) + + strlen(connection->user_agent) + + strlen(connection->u.host) + remote_port_len + + (referer ? strlen(referer) : 0) + + (www_auth ? strlen(www_auth) : 0) + + (proxy_auth ? strlen(proxy_auth) : 0) + 64 + + + (pragma_no_cache ? strlen(pragma_no_cache) : + 0)); sprintf(request, - "GET %s HTTP/1.0\r\nUser-Agent: %s\r\nHost: %s%s\r\nAccept: */*\r\n%s%s%s%s\r\n", - location, connection->user_agent, connection->u.host, - remote_port ? remote_port : "", - referer ? referer : "", - www_auth ? www_auth : "", proxy_auth ? proxy_auth : "", - pragma_no_cache ? pragma_no_cache : ""); + "GET %s HTTP/1.0\r\nUser-Agent: %s\r\nHost: %s%s\r\nAccept: */*\r\n%s%s%s%s\r\n", + location, connection->user_agent, connection->u.host, + remote_port ? remote_port : "", + referer ? referer : "", + www_auth ? www_auth : "", proxy_auth ? proxy_auth : "", + pragma_no_cache ? pragma_no_cache : ""); proz_debug("HTTP request = %s", request); @@ -687,13 +694,13 @@ uerr_t proz_http_get_url_info(connection_t * connection) close_sock(&connection->data_sock); if (err == HOK) - { - connection->main_file_size = connection->hs.contlen; - if (connection->hs.accept_ranges == 1) - connection->resume_support = TRUE; - else if (connection->hs.accept_ranges == -1) - connection->resume_support = FALSE; - } + { + connection->main_file_size = connection->hs.contlen; + if (connection->hs.accept_ranges == 1) + connection->resume_support = TRUE; + else if (connection->hs.accept_ranges == -1) + connection->resume_support = FALSE; + } connection->file_type = REGULAR_FILE; @@ -704,68 +711,64 @@ uerr_t proz_http_get_url_info(connection_t * connection) /*Loops for connection->attempts */ uerr_t http_get_url_info_loop(connection_t * connection) { - pthread_mutex_lock(&connection->access_mutex); connection->running = TRUE; pthread_mutex_unlock(&connection->access_mutex); assert(connection->attempts >= 0); do - { - if (connection->attempts > 0 && connection->err != NEWLOCATION) { + if (connection->attempts > 0 && connection->err != NEWLOCATION) + { + connection_show_message(connection, + _("Retrying...Attempt %d in %d seconds"), + connection->attempts, + connection->retry_delay.tv_sec); + delay_ms(connection->retry_delay.tv_sec * 1000); + } - connection_show_message(connection, - _("Retrying...Attempt %d in %d seconds"), - connection->attempts, - connection->retry_delay.tv_sec); - delay_ms(connection->retry_delay.tv_sec * 1000); - } + /*Push the handler which will cleanup any sockets that are left open */ + pthread_cleanup_push(cleanup_socks, (void *)connection); + connection->err = proz_http_get_url_info(connection); + /*pop the handler */ + pthread_cleanup_pop(0); - /*Push the handler which will cleanup any sockets that are left open */ - pthread_cleanup_push(cleanup_socks, (void *) connection); - connection->err = proz_http_get_url_info(connection); - /*pop the handler */ - pthread_cleanup_pop(0); + connection->attempts++; - connection->attempts++; + switch (connection->err) + { + case HOK: + connection_show_message(connection, _("Successfully got info")); + pthread_mutex_lock(&connection->access_mutex); + connection->running = FALSE; + pthread_mutex_unlock(&connection->access_mutex); + return connection->err; + break; - switch (connection->err) - { - case HOK: - connection_show_message(connection, _("Successfully got info")); - pthread_mutex_lock(&connection->access_mutex); - connection->running = FALSE; - pthread_mutex_unlock(&connection->access_mutex); - return connection->err; - break; + case NEWLOCATION: + return connection->err; + break; - case NEWLOCATION: - return connection->err; - break; + case HTTPNSFOD: + connection_show_message(connection, _("File not found!")); + pthread_mutex_lock(&connection->access_mutex); + connection->running = FALSE; + pthread_mutex_unlock(&connection->access_mutex); + return connection->err; + break; - case HTTPNSFOD: - connection_show_message(connection, _("File not found!")); - pthread_mutex_lock(&connection->access_mutex); - connection->running = FALSE; - pthread_mutex_unlock(&connection->access_mutex); - return connection->err; - break; - - default: - connection_show_message(connection, proz_strerror(connection->err)); - break; - } - - } - while ((connection->attempts < connection->max_attempts) - || connection->max_attempts == 0); + default: + connection_show_message(connection, proz_strerror(connection->err)); + break; + } + } while ((connection->attempts < connection->max_attempts) + || connection->max_attempts == 0); connection_show_message(connection, - _ - ("I have tried %d attempt(s) and have failed, aborting"), - connection->attempts); + _ + ("I have tried %d attempt(s) and have failed, aborting"), + connection->attempts); pthread_mutex_lock(&connection->access_mutex); connection->running = FALSE; pthread_mutex_unlock(&connection->access_mutex); @@ -775,18 +778,17 @@ uerr_t http_get_url_info_loop(connection_t * connection) /* - I am writing a seperate function to handle FTP proxying through HTTP, I - MHO whoever thought of using HTTP to proxy FTP is a shithead, - its such a PITA ;) + I am writing a seperate function to handle FTP proxying through HTTP, I + MHO whoever thought of using HTTP to proxy FTP is a shithead, + its such a PITA ;) */ uerr_t ftp_get_url_info_from_http_proxy(connection_t * connection) { - uerr_t err; int remote_port_len; char *user, *passwd, *www_auth = NULL, *proxy_auth = - NULL, *pragma_no_cache = NULL; + NULL, *pragma_no_cache = NULL; char *request, *remote_port; netrc_entry *netrc_ent; @@ -794,80 +796,81 @@ uerr_t ftp_get_url_info_from_http_proxy(connection_t * connection) memset(&connection->hs, 0, sizeof(connection->hs)); err = connect_to_server(&connection->data_sock, - connection->ftp_proxy->proxy_url.host, - connection->ftp_proxy->proxy_url.port, - &connection->xfer_timeout); + connection->ftp_proxy->proxy_url.host, + connection->ftp_proxy->proxy_url.port, + &connection->xfer_timeout); if (err != NOCONERROR) - { - connection_show_message(connection, _("Error connecting to %s"), - connection->ftp_proxy->proxy_url.host); - return err; - } + { + connection_show_message(connection, _("Error connecting to %s"), + connection->ftp_proxy->proxy_url.host); + return err; + } user = connection->u.user; passwd = connection->u.passwd; /* Use .netrc if asked to do so. */ if (connection->use_netrc == TRUE) - { - netrc_ent = search_netrc(libprozrtinfo.netrc_list, connection->u.host); - - if (netrc_ent != NULL) { - user = netrc_ent->account; - passwd = netrc_ent->password; + netrc_ent = search_netrc(libprozrtinfo.netrc_list, connection->u.host); + + if (netrc_ent != NULL) + { + user = netrc_ent->account; + passwd = netrc_ent->password; + } } - } user = user ? user : ""; passwd = passwd ? passwd : ""; if (strlen(user) || strlen(passwd)) - { - /* Construct the necessary header. */ - www_auth = get_basic_auth_str(user, passwd, "Authorization"); - proz_debug(_("Authenticating as user %s password %s"), user, passwd); - proz_debug(_("Authentification string=%s"), www_auth); - } else + { + /* Construct the necessary header. */ + www_auth = get_basic_auth_str(user, passwd, "Authorization"); + proz_debug(_("Authenticating as user %s password %s"), user, passwd); + proz_debug(_("Authentification string=%s"), www_auth); + } + else www_auth = 0; if (strlen(connection->ftp_proxy->username) || strlen(connection->ftp_proxy->passwd)) proxy_auth = - get_basic_auth_str(connection->ftp_proxy->username, - connection->ftp_proxy->passwd, - "Proxy-Authorization"); + get_basic_auth_str(connection->ftp_proxy->username, + connection->ftp_proxy->passwd, + "Proxy-Authorization"); - remote_port = (char *) alloca(64); + remote_port = (char *)alloca(64); remote_port_len = sprintf(remote_port, ":%d", connection->u.port); if (http_use_proxy(connection) && (connection->http_no_cache || connection->attempts > 0)) - { - pragma_no_cache = (char *) alloca(21); - sprintf(pragma_no_cache, "Pragma: no-cache\r\n"); - } + { + pragma_no_cache = (char *)alloca(21); + sprintf(pragma_no_cache, "Pragma: no-cache\r\n"); + } /*Referrer TAG should not be needed in FTP through HTTP proxy..right */ - request = (char *) alloca(strlen(connection->u.url) - + strlen(connection->user_agent) - + strlen(connection->u.host) + remote_port_len - + (www_auth ? strlen(www_auth) : 0) - + (proxy_auth ? strlen(proxy_auth) : 0) - + 64 - + - (pragma_no_cache ? strlen(pragma_no_cache) : - 0)); + request = (char *)alloca(strlen(connection->u.url) + + strlen(connection->user_agent) + + strlen(connection->u.host) + remote_port_len + + (www_auth ? strlen(www_auth) : 0) + + (proxy_auth ? strlen(proxy_auth) : 0) + + 64 + + + (pragma_no_cache ? strlen(pragma_no_cache) : + 0)); sprintf(request, - "GET %s HTTP/1.0\r\nUser-Agent: %s\r\nHost: %s%s\r\nAccept: */*\r\n%s%s%s\r\n", - connection->u.url, connection->user_agent, connection->u.host, - remote_port, - www_auth ? www_auth : "", proxy_auth ? proxy_auth : "", - pragma_no_cache ? pragma_no_cache : ""); + "GET %s HTTP/1.0\r\nUser-Agent: %s\r\nHost: %s%s\r\nAccept: */*\r\n%s%s%s\r\n", + connection->u.url, connection->user_agent, connection->u.host, + remote_port, + www_auth ? www_auth : "", proxy_auth ? proxy_auth : "", + pragma_no_cache ? pragma_no_cache : ""); proz_debug("HTTP request = %s", request); @@ -879,14 +882,14 @@ uerr_t ftp_get_url_info_from_http_proxy(connection_t * connection) /*Convert the error code to the equivalent FTP one if possible */ if (err == HOK) - { - connection->main_file_size = connection->hs.contlen; - if (connection->hs.accept_ranges == 1) - connection->resume_support = TRUE; - else if (connection->hs.accept_ranges == -1) - connection->resume_support = FALSE; - return FTPOK; - } + { + connection->main_file_size = connection->hs.contlen; + if (connection->hs.accept_ranges == 1) + connection->resume_support = TRUE; + else if (connection->hs.accept_ranges == -1) + connection->resume_support = FALSE; + return FTPOK; + } if (err == HAUTHREQ) @@ -897,5 +900,4 @@ uerr_t ftp_get_url_info_from_http_proxy(connection_t * connection) /* connection->file_type = REGULAR_FILE; */ return FTPERR; - } diff --git a/libprozilla/src/http.h b/libprozilla/src/http.h index aaf377d..d44ac24 100644 --- a/libprozilla/src/http.h +++ b/libprozilla/src/http.h @@ -1,25 +1,25 @@ /****************************************************************************** - libprozilla - a download accelerator library - Copyright (C) 2001 Kalum Somaratna + libprozilla - a download accelerator library + Copyright (C) 2001 Kalum Somaratna - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ******************************************************************************/ /* HTTP support. */ -/* $Id: http.h,v 1.12 2005/03/31 20:10:57 sean Exp $ */ +/* $Id$ */ #ifndef HTTP_H @@ -34,27 +34,27 @@ extern "C" { #endif - int buf_readchar(int fd, char *ret, struct timeval *timeout); - int buf_peek(int fd, char *ret, struct timeval *timeout); - uerr_t fetch_next_header(int fd, char **hdr, struct timeval *timeout); +int buf_readchar(int fd, char *ret, struct timeval *timeout); +int buf_peek(int fd, char *ret, struct timeval *timeout); +uerr_t fetch_next_header(int fd, char **hdr, struct timeval *timeout); - int hparsestatline(const char *hdr, const char **rp); - int hskip_lws(const char *hdr); - off_t hgetlen(const char *hdr); - off_t hgetrange(const char *hdr); - char *hgetlocation(const char *hdr); - char *hgetmodified(const char *hdr); - int hgetaccept_ranges(const char *hdr); +int hparsestatline(const char *hdr, const char **rp); +int hskip_lws(const char *hdr); +off_t hgetlen(const char *hdr); +off_t hgetrange(const char *hdr); +char *hgetlocation(const char *hdr); +char *hgetmodified(const char *hdr); +int hgetaccept_ranges(const char *hdr); - uerr_t http_fetch_headers(connection_t * connection, http_stat_t * hs, - char *command); +uerr_t http_fetch_headers(connection_t * connection, http_stat_t * hs, + char *command); - char *get_basic_auth_str(char *user, char *passwd, char *auth_header); +char *get_basic_auth_str(char *user, char *passwd, char *auth_header); - boolean http_use_proxy(connection_t * connection); - uerr_t proz_http_get_url_info(connection_t * connection); - uerr_t http_get_url_info_loop(connection_t * connection); +boolean http_use_proxy(connection_t * connection); +uerr_t proz_http_get_url_info(connection_t * connection); +uerr_t http_get_url_info_loop(connection_t * connection); #ifdef __cplusplus } #endif -#endif /* HTTP_H */ +#endif /* HTTP_H */ diff --git a/libprozilla/src/logfile.c b/libprozilla/src/logfile.c index 50ab71c..ef95ae6 100644 --- a/libprozilla/src/logfile.c +++ b/libprozilla/src/logfile.c @@ -1,20 +1,20 @@ /****************************************************************************** - libprozilla - a download accelerator library - Copyright (C) 2001 Kalum Somaratna + libprozilla - a download accelerator library + Copyright (C) 2001 Kalum Somaratna - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ******************************************************************************/ #include "common.h" @@ -26,7 +26,7 @@ /*If download is not NULL will store info about the downloads connection allocations to it too.*/ int log_create_logfile(int num_connections, int file_size, char *url, - download_t * download) + download_t * download) { char buffer[PATH_MAX]; FILE *fp = NULL; @@ -35,19 +35,18 @@ int log_create_logfile(int num_connections, int file_size, char *url, memset(&lf, 0, sizeof(lf)); /* - * Compute the name of the logfile + * Compute the name of the logfile */ snprintf(buffer, PATH_MAX, "%s/%s%s.log", download->log_dir, - download->u.file, DEFAULT_FILE_EXT); + download->u.file, DEFAULT_FILE_EXT); if (!(fp = fopen(buffer, "wb"))) { - /* - * fixme add the error displaing to the main function + * fixme add the error displaing to the main function */ download_show_message(download, - _("Error opening file %s for writing: %s"), - buffer, strerror(errno)); + _("Error opening file %s for writing: %s"), + buffer, strerror(errno)); return -1; } @@ -63,7 +62,7 @@ int log_create_logfile(int num_connections, int file_size, char *url, if (fwrite(&lf, 1, sizeof(lf), fp) != sizeof(lf)) { download_show_message(download, _("Error writing to file %s: %s"), - buffer, strerror(errno)); + buffer, strerror(errno)); fclose(fp); return -1; } @@ -73,7 +72,7 @@ int log_create_logfile(int num_connections, int file_size, char *url, if (fwrite(url, 1, strlen(url), fp) != strlen(url)) { download_show_message(download, _("Error writing to file %s: %s"), - buffer, strerror(errno)); + buffer, strerror(errno)); fclose(fp); return -1; } @@ -83,64 +82,58 @@ int log_create_logfile(int num_connections, int file_size, char *url, if (download != NULL) { for (i = 0; i < download->num_connections; i++) - { + { + pthread_mutex_lock(&download->pconnections[i]->access_mutex); - pthread_mutex_lock(&download->pconnections[i]->access_mutex); - - if (fwrite - (&download->pconnections[i]->local_startpos, 1, - sizeof(download->pconnections[i]->local_startpos), - fp) != sizeof(download->pconnections[i]->local_startpos)) - { - - pthread_mutex_unlock(&download->pconnections[i]->access_mutex); - download_show_message(download, _("Error writing to file %s: %s"), - buffer, strerror(errno)); - fclose(fp); - return -1; - } - - if (fwrite - (&download->pconnections[i]->orig_remote_startpos, 1, - sizeof(download->pconnections[i]->orig_remote_startpos), - fp) != sizeof(download->pconnections[i]->orig_remote_startpos)) + if (fwrite + (&download->pconnections[i]->local_startpos, 1, + sizeof(download->pconnections[i]->local_startpos), + fp) != sizeof(download->pconnections[i]->local_startpos)) { - - pthread_mutex_unlock(&download->pconnections[i]->access_mutex); - download_show_message(download, _("Error writing to file %s: %s"), - buffer, strerror(errno)); - fclose(fp); - return -1; + pthread_mutex_unlock(&download->pconnections[i]->access_mutex); + download_show_message(download, _("Error writing to file %s: %s"), + buffer, strerror(errno)); + fclose(fp); + return -1; } - if (fwrite - (&download->pconnections[i]->remote_endpos, 1, - sizeof(download->pconnections[i]->remote_endpos), - fp) != sizeof(download->pconnections[i]->remote_endpos)) - { - pthread_mutex_unlock(&download->pconnections[i]->access_mutex); - download_show_message(download, _("Error writing to file %s: %s"), - buffer, strerror(errno)); - fclose(fp); - return -1; - } + if (fwrite + (&download->pconnections[i]->orig_remote_startpos, 1, + sizeof(download->pconnections[i]->orig_remote_startpos), + fp) != sizeof(download->pconnections[i]->orig_remote_startpos)) + { + pthread_mutex_unlock(&download->pconnections[i]->access_mutex); + download_show_message(download, _("Error writing to file %s: %s"), + buffer, strerror(errno)); + fclose(fp); + return -1; + } - if (fwrite - (&download->pconnections[i]->remote_bytes_received, 1, - sizeof(download->pconnections[i]->remote_bytes_received), - fp) != sizeof(download->pconnections[i]->remote_bytes_received)) - { - - pthread_mutex_unlock(&download->pconnections[i]->access_mutex); - download_show_message(download, _("Error writing to file %s: %s"), - buffer, strerror(errno)); - fclose(fp); - return -1; - } - pthread_mutex_unlock(&download->pconnections[i]->access_mutex); - - } + if (fwrite + (&download->pconnections[i]->remote_endpos, 1, + sizeof(download->pconnections[i]->remote_endpos), + fp) != sizeof(download->pconnections[i]->remote_endpos)) + { + pthread_mutex_unlock(&download->pconnections[i]->access_mutex); + download_show_message(download, _("Error writing to file %s: %s"), + buffer, strerror(errno)); + fclose(fp); + return -1; + } + if (fwrite + (&download->pconnections[i]->remote_bytes_received, 1, + sizeof(download->pconnections[i]->remote_bytes_received), + fp) != sizeof(download->pconnections[i]->remote_bytes_received)) + { + pthread_mutex_unlock(&download->pconnections[i]->access_mutex); + download_show_message(download, _("Error writing to file %s: %s"), + buffer, strerror(errno)); + fclose(fp); + return -1; + } + pthread_mutex_unlock(&download->pconnections[i]->access_mutex); + } } fclose(fp); @@ -155,19 +148,20 @@ int proz_log_logfile_exists(download_t * download) struct stat st_buf; /* - * Compute the name of the logfile + * Compute the name of the logfile */ snprintf(buffer, PATH_MAX, "%s/%s%s.log", download->log_dir, - download->u.file, DEFAULT_FILE_EXT); + download->u.file, DEFAULT_FILE_EXT); ret = stat(buffer, &st_buf); if (ret == -1) - { - if (errno == ENOENT) - return 0; - else - return -1; - } else + { + if (errno == ENOENT) + return 0; + else + return -1; + } + else return 1; } @@ -178,20 +172,21 @@ int proz_log_delete_logfile(download_t * download) int ret; snprintf(buffer, PATH_MAX, "%s/%s%s.log", download->log_dir, - download->u.file, DEFAULT_FILE_EXT); + download->u.file, DEFAULT_FILE_EXT); ret = unlink(buffer); if (ret == -1) { if (errno == ENOENT) - { - download_show_message(download, _("logfile doesn't exist")); - return 1; - } else - { - download_show_message(download, "Error: Unable to delete the logfile: %s", strerror(errno)); - return -1; - } + { + download_show_message(download, _("logfile doesn't exist")); + return 1; + } + else + { + download_show_message(download, "Error: Unable to delete the logfile: %s", strerror(errno)); + return -1; + } } return 1; @@ -200,26 +195,26 @@ int proz_log_delete_logfile(download_t * download) /* Read the logfile into the logfile structure */ int proz_log_read_logfile(logfile * lf, download_t * download, - boolean load_con_info) + boolean load_con_info) { char buffer[PATH_MAX]; FILE *fp = NULL; int i; /* - * Compute the name of the logfile + * Compute the name of the logfile */ snprintf(buffer, PATH_MAX, "%s/%s%s.log", download->log_dir, - download->u.file, DEFAULT_FILE_EXT); + download->u.file, DEFAULT_FILE_EXT); if (!(fp = fopen(buffer, "rb"))) { /* - * fixme add the error displaing to the main function + * fixme add the error displaing to the main function */ download_show_message(download, - _("Error opening file %s for reading: %s"), - buffer, strerror(errno)); + _("Error opening file %s for reading: %s"), + buffer, strerror(errno)); return -1; } @@ -244,74 +239,69 @@ int proz_log_read_logfile(logfile * lf, download_t * download, if (load_con_info == TRUE) { for (i = 0; i < lf->num_connections; i++) - { + { + proz_debug("value before= %d", download->pconnections[i]->local_startpos); - - - proz_debug("value before= %d", download->pconnections[i]->local_startpos); - - if (fread - (&download->pconnections[i]->local_startpos, 1, - sizeof(download->pconnections[i]->local_startpos), - fp) != sizeof(download->pconnections[i]->local_startpos)) + if (fread + (&download->pconnections[i]->local_startpos, 1, + sizeof(download->pconnections[i]->local_startpos), + fp) != sizeof(download->pconnections[i]->local_startpos)) { - download_show_message(download, - _("Error reading from file %s: %s"), buffer, - strerror(errno)); - fclose(fp); - return -1; + download_show_message(download, + _("Error reading from file %s: %s"), buffer, + strerror(errno)); + fclose(fp); + return -1; } - proz_debug("value after= %d", download->pconnections[i]->local_startpos); + proz_debug("value after= %d", download->pconnections[i]->local_startpos); - proz_debug("orig_remote_startpos before= %d", download->pconnections[i]->orig_remote_startpos); - if (fread - (&download->pconnections[i]->orig_remote_startpos, 1, - sizeof(download->pconnections[i]->orig_remote_startpos), - fp) != sizeof(download->pconnections[i]->orig_remote_startpos)) - { - download_show_message(download, - _("Error reading from file %s: %s"), buffer, - strerror(errno)); - fclose(fp); - return -1; - } + proz_debug("orig_remote_startpos before= %d", download->pconnections[i]->orig_remote_startpos); + if (fread + (&download->pconnections[i]->orig_remote_startpos, 1, + sizeof(download->pconnections[i]->orig_remote_startpos), + fp) != sizeof(download->pconnections[i]->orig_remote_startpos)) + { + download_show_message(download, + _("Error reading from file %s: %s"), buffer, + strerror(errno)); + fclose(fp); + return -1; + } - proz_debug("orig_remote_startpos after= %d", download->pconnections[i]->orig_remote_startpos); - + proz_debug("orig_remote_startpos after= %d", download->pconnections[i]->orig_remote_startpos); - proz_debug("remote_edndpos before= %d", download->pconnections[i]->remote_endpos); - if (fread - (&download->pconnections[i]->remote_endpos, 1, - sizeof(download->pconnections[i]->remote_endpos), - fp) != sizeof(download->pconnections[i]->remote_endpos)) - { - download_show_message(download, - _("Error reading from file %s: %s"), buffer, - strerror(errno)); - fclose(fp); - return -1; - } - - proz_debug("remote_endpos after= %d", download->pconnections[i]->remote_endpos); - proz_debug("remote_bytes_received before= %d", download->pconnections[i]->remote_bytes_received); - if (fread - (&download->pconnections[i]->remote_bytes_received, 1, - sizeof(download->pconnections[i]->remote_bytes_received), - fp) != sizeof(download->pconnections[i]->remote_bytes_received)) - { - download_show_message(download, - _("Error reading from file %s: %s"), buffer, - strerror(errno)); - fclose(fp); - return -1; - } + proz_debug("remote_edndpos before= %d", download->pconnections[i]->remote_endpos); + if (fread + (&download->pconnections[i]->remote_endpos, 1, + sizeof(download->pconnections[i]->remote_endpos), + fp) != sizeof(download->pconnections[i]->remote_endpos)) + { + download_show_message(download, + _("Error reading from file %s: %s"), buffer, + strerror(errno)); + fclose(fp); + return -1; + } - proz_debug("remote_bytes_received after= %d", download->pconnections[i]->remote_bytes_received); - } + proz_debug("remote_endpos after= %d", download->pconnections[i]->remote_endpos); + proz_debug("remote_bytes_received before= %d", download->pconnections[i]->remote_bytes_received); + if (fread + (&download->pconnections[i]->remote_bytes_received, 1, + sizeof(download->pconnections[i]->remote_bytes_received), + fp) != sizeof(download->pconnections[i]->remote_bytes_received)) + { + download_show_message(download, + _("Error reading from file %s: %s"), buffer, + strerror(errno)); + fclose(fp); + return -1; + } + proz_debug("remote_bytes_received after= %d", download->pconnections[i]->remote_bytes_received); + } } diff --git a/libprozilla/src/logfile.h b/libprozilla/src/logfile.h index 582b0c5..a625906 100644 --- a/libprozilla/src/logfile.h +++ b/libprozilla/src/logfile.h @@ -1,20 +1,20 @@ /****************************************************************************** - libprozilla - a download accelerator library - Copyright (C) 2001 Kalum Somaratna + libprozilla - a download accelerator library + Copyright (C) 2001 Kalum Somaratna - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ******************************************************************************/ #ifndef LOGFILE_H @@ -30,8 +30,8 @@ extern "C" { #endif - int log_create_logfile(int num_connections, int file_size, char *url, - download_t * download); +int log_create_logfile(int num_connections, int file_size, char *url, + download_t * download); diff --git a/libprozilla/src/main.c b/libprozilla/src/main.c index 87ea903..04787aa 100644 --- a/libprozilla/src/main.c +++ b/libprozilla/src/main.c @@ -1,20 +1,20 @@ /****************************************************************************** - libprozilla - a download accelerator library - Copyright (C) 2001 Kalum Somaratna + libprozilla - a download accelerator library + Copyright (C) 2001 Kalum Somaratna - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ******************************************************************************/ /* The main file. */ @@ -31,18 +31,18 @@ /* static struct option long_opts[] = { - // { name has_arg *flag val } - {"no-netrc", no_argument, NULL, 'n'}, - {"use-port", no_argument, NULL, 129}, - {"libdebug", no_argument, NULL, 130}, - {NULL, 0, NULL, 0} -};*/ + // { name has_arg *flag val } + {"no-netrc", no_argument, NULL, 'n'}, + {"use-port", no_argument, NULL, 129}, + {"libdebug", no_argument, NULL, 130}, + {NULL, 0, NULL, 0} + };*/ libprozinfo libprozrtinfo; /****************************************************************************** - Initialize the library. + Initialize the library. ******************************************************************************/ int proz_init(int argc, char **argv) { @@ -78,7 +78,7 @@ int proz_init(int argc, char **argv) /* Parse the options and set the relevant ones for the library. */ /* while ((c = getopt_long(argc, argv, "", long_opts, NULL)) != EOF) - { + { switch (c) { case 129: @@ -87,24 +87,26 @@ int proz_init(int argc, char **argv) default: continue; } - } -*/ - /* TODO Get home directory and read .netrc. */ + } + */ +/* TODO Get home directory and read .netrc. */ libprozrtinfo.home_dir = home_dir(); if (libprozrtinfo.home_dir != NULL) - { - char *netrc_file = kmalloc(strlen(libprozrtinfo.home_dir) - + strlen(".netrc") + 2); + { + char *netrc_file = kmalloc(strlen(libprozrtinfo.home_dir) + + strlen(".netrc") + 2); - sprintf(netrc_file, "%s/%s", libprozrtinfo.home_dir, ".netrc"); + sprintf(netrc_file, "%s/%s", libprozrtinfo.home_dir, ".netrc"); - libprozrtinfo.netrc_list = parse_netrc(netrc_file); - } else { - /* Make sure home dir is never NULL */ - libprozrtinfo.home_dir = kstrdup("."); - } + libprozrtinfo.netrc_list = parse_netrc(netrc_file); + } + else + { + /* Make sure home dir is never NULL */ + libprozrtinfo.home_dir = kstrdup("."); + } libprozrtinfo.dl_dir = kstrdup("."); libprozrtinfo.output_dir = kstrdup("."); @@ -114,7 +116,7 @@ int proz_init(int argc, char **argv) } /****************************************************************************** - ... + ... ******************************************************************************/ void proz_shutdown(void) { @@ -126,7 +128,7 @@ void proz_shutdown(void) } /****************************************************************************** - Abort with an error message. + Abort with an error message. ******************************************************************************/ void proz_die(const char *format, ...) { @@ -144,7 +146,7 @@ void proz_die(const char *format, ...) } /****************************************************************************** - ... + ... ******************************************************************************/ void proz_set_http_proxy(proxy_info * proxy) { @@ -155,7 +157,7 @@ void proz_set_http_proxy(proxy_info * proxy) } /****************************************************************************** - ... + ... ******************************************************************************/ void proz_set_ftp_proxy(proxy_info * proxy) { @@ -199,7 +201,6 @@ void proz_set_download_dir(char *dir) kfree(libprozrtinfo.dl_dir); libprozrtinfo.dl_dir = kstrdup(dir); - } void proz_set_logfile_dir(char *dir) @@ -224,6 +225,5 @@ void proz_set_output_dir(char *dir) char *proz_get_libprozilla_version() { - return strdup(VERSION); } diff --git a/libprozilla/src/misc.c b/libprozilla/src/misc.c index e913fea..748b917 100644 --- a/libprozilla/src/misc.c +++ b/libprozilla/src/misc.c @@ -1,25 +1,25 @@ /****************************************************************************** - libprozilla - a download accelerator library - Copyright (C) 2001 Kalum Somaratna + libprozilla - a download accelerator library + Copyright (C) 2001 Kalum Somaratna - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ******************************************************************************/ /* Miscellaneous routines. */ -/* $Id: misc.c,v 1.32 2005/01/11 01:49:11 sean Exp $ */ +/* $Id$ */ #include "common.h" @@ -32,8 +32,8 @@ void cleanup_httpsocks(connection_t * connection); void cleanup_ftpsocks(connection_t * connection); /****************************************************************************** - Allocates size bytes of memory. If size is 0 it returns NULL. If there is - not enough memory the program quits with an error message. + Allocates size bytes of memory. If size is 0 it returns NULL. If there is + not enough memory the program quits with an error message. ******************************************************************************/ void *kmalloc(size_t size) { @@ -51,7 +51,7 @@ void *kmalloc(size_t size) } /****************************************************************************** - A wrapper for realloc() which aborts if not enough memory is present. + A wrapper for realloc() which aborts if not enough memory is present. ******************************************************************************/ void *krealloc(void *ptr, size_t new_size) { @@ -66,7 +66,7 @@ void *krealloc(void *ptr, size_t new_size) } /****************************************************************************** - A wrapper for free() which handles NULL pointers. + A wrapper for free() which handles NULL pointers. ******************************************************************************/ void kfree(void *ptr) { @@ -75,7 +75,7 @@ void kfree(void *ptr) } /****************************************************************************** - A wrapper for strdup() which aborts if not enough memory is present. + A wrapper for strdup() which aborts if not enough memory is present. ******************************************************************************/ char *kstrdup(const char *str) { @@ -88,7 +88,7 @@ char *kstrdup(const char *str) } /****************************************************************************** - Checks whether the specified string is a number or digit. + Checks whether the specified string is a number or digit. ******************************************************************************/ boolean is_number(const char *str) { @@ -98,16 +98,16 @@ boolean is_number(const char *str) return FALSE; while (str[i] != '\0') - { - if (!isdigit(str[i])) - return FALSE; - i++; - } + { + if (!isdigit(str[i])) + return FALSE; + i++; + } return TRUE; } /****************************************************************************** - How many digits are in a long integer? + How many digits are in a long integer? ******************************************************************************/ int numdigit(long a) { @@ -120,8 +120,8 @@ int numdigit(long a) } /****************************************************************************** - Copy the string formed by two pointers (one on the beginning, other on the - char after the last char) to a new, malloc()-ed location. 0-terminate it. + Copy the string formed by two pointers (one on the beginning, other on the + char after the last char) to a new, malloc()-ed location. 0-terminate it. ******************************************************************************/ char *strdupdelim(const char *beg, const char *end) { @@ -134,8 +134,8 @@ char *strdupdelim(const char *beg, const char *end) } /****************************************************************************** - Print a long integer to the string buffer. The digits are first written in - reverse order (the least significant digit first), and are then reversed. + Print a long integer to the string buffer. The digits are first written in + reverse order (the least significant digit first), and are then reversed. ******************************************************************************/ void prnum(char *where, long num) { @@ -144,63 +144,64 @@ void prnum(char *where, long num) char c; if (num < 0) - { - *where++ = '-'; - num = -num; - } + { + *where++ = '-'; + num = -num; + } p = where; /* Print the digits to the string. */ do - { - *p++ = num % 10 + '0'; - num /= 10; - } - while (num); + { + *p++ = num % 10 + '0'; + num /= 10; + } while (num); /* And reverse them. */ l = p - where - 1; for (i = l / 2; i >= 0; i--) - { - c = where[i]; - where[i] = where[l - i]; - where[l - i] = c; - } + { + c = where[i]; + where[i] = where[l - i]; + where[l - i] = c; + } where[l + 1] = '\0'; } /****************************************************************************** - Extracts a numurical argument from an option, when it has been specified for - example as -l=3 or -l3. Returns 1 on success or 0 on error (non numerical - argument etc). + Extracts a numurical argument from an option, when it has been specified for + example as -l=3 or -l3. Returns 1 on success or 0 on error (non numerical + argument etc). ******************************************************************************/ int setargval(char *optstr, int *num) { if (*optstr == '=') - { - if (is_number(optstr + 1)) { - *num = atoi(optstr + 1); - return 1; - } else - return 0; - } else - { - if (is_number(optstr)) + if (is_number(optstr + 1)) + { + *num = atoi(optstr + 1); + return 1; + } + else + return 0; + } + else { - *num = atoi(optstr); - return 1; - } else - return 0; - } - + if (is_number(optstr)) + { + *num = atoi(optstr); + return 1; + } + else + return 0; + } } /****************************************************************************** - Encode the given string to base64 format and place it into store. store will - be 0-terminated, and must point to a writable buffer of at least - 1+BASE64_LENGTH(length) bytes. Note: Routine stolen from wget (grendel). + Encode the given string to base64 format and place it into store. store will + be 0-terminated, and must point to a writable buffer of at least + 1+BASE64_LENGTH(length) bytes. Note: Routine stolen from wget (grendel). ******************************************************************************/ void base64_encode(const char *s, char *store, int length) { @@ -217,17 +218,17 @@ void base64_encode(const char *s, char *store, int length) }; int i; - unsigned char *p = (unsigned char *) store; + unsigned char *p = (unsigned char *)store; /* Transform the 3x8 bits to 4x6 bits, as required by base64. */ for (i = 0; i < length; i += 3) - { - *p++ = tbl[s[0] >> 2]; - *p++ = tbl[((s[0] & 3) << 4) + (s[1] >> 4)]; - *p++ = tbl[((s[1] & 0xf) << 2) + (s[2] >> 6)]; - *p++ = tbl[s[2] & 0x3f]; - s += 3; - } + { + *p++ = tbl[s[0] >> 2]; + *p++ = tbl[((s[0] & 3) << 4) + (s[1] >> 4)]; + *p++ = tbl[((s[1] & 0xf) << 2) + (s[2] >> 6)]; + *p++ = tbl[s[2] & 0x3f]; + s += 3; + } /* Pad the result if necessary... */ if (i == length + 1) @@ -239,46 +240,45 @@ void base64_encode(const char *s, char *store, int length) } /****************************************************************************** - Return the user's home directory (strdup-ed), or NULL if none is found. + Return the user's home directory (strdup-ed), or NULL if none is found. ******************************************************************************/ char *home_dir(void) { char *home = getenv("HOME"); if (home == NULL) - { - /* If $HOME is not defined, try getting it from the passwd file. */ - struct passwd *pwd = getpwuid(getuid()); - if (!pwd || !pwd->pw_dir) - return NULL; - home = pwd->pw_dir; - } + { + /* If $HOME is not defined, try getting it from the passwd file. */ + struct passwd *pwd = getpwuid(getuid()); + if (!pwd || !pwd->pw_dir) + return NULL; + home = pwd->pw_dir; + } return home ? kstrdup(home) : NULL; } /****************************************************************************** - Subtract the `struct timeval' values X and Y, storing the result in RESULT. - Return 1 if the difference is negative, otherwise 0. + Subtract the `struct timeval' values X and Y, storing the result in RESULT. + Return 1 if the difference is negative, otherwise 0. ******************************************************************************/ int proz_timeval_subtract(struct timeval *result, struct timeval *x, - struct timeval *y) + struct timeval *y) { - /* Perform the carry for the later subtraction by updating Y. */ if (x->tv_usec < y->tv_usec) - { - int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1; - y->tv_usec -= 1000000 * nsec; - y->tv_sec += nsec; - } + { + int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1; + y->tv_usec -= 1000000 * nsec; + y->tv_sec += nsec; + } if (x->tv_usec - y->tv_usec > 1000000) - { - int nsec = (x->tv_usec - y->tv_usec) / 1000000; - y->tv_usec += 1000000 * nsec; - y->tv_sec -= nsec; - } + { + int nsec = (x->tv_usec - y->tv_usec) / 1000000; + y->tv_usec += 1000000 * nsec; + y->tv_sec -= nsec; + } /* Compute the time remaining to wait. `tv_usec' is certainly positive. */ result->tv_sec = x->tv_sec - y->tv_sec; @@ -289,7 +289,7 @@ int proz_timeval_subtract(struct timeval *result, struct timeval *x, } /****************************************************************************** - Wait for 'ms' milliseconds. + Wait for 'ms' milliseconds. ******************************************************************************/ void delay_ms(int ms) { @@ -300,7 +300,7 @@ void delay_ms(int ms) tv_delay.tv_sec = ms / 1000; tv_delay.tv_usec = (ms * 1000) % 1000000; - if (select(0, (fd_set *) 0, (fd_set *) 0, (fd_set *) 0, &tv_delay) < 0) + if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv_delay) < 0) proz_debug(_("Warning: Unable to delay")); } @@ -308,8 +308,8 @@ void delay_ms(int ms) /*Closes a socket and zeroes the socket before returning */ int close_sock(int *sock) { - int retval = close(*sock); + *sock = 0; return retval; } @@ -318,158 +318,198 @@ int close_sock(int *sock) char *proz_strerror(uerr_t error) { - switch (error) - { + { + case HOSTERR: + return _("Unable to lookup hostname"); - case HOSTERR: - return _("Unable to lookup hostname"); - case CONSOCKERR: - return _("Unable to create socket"); - case CONERROR: - return _("Error occured while connecting"); - case CONREFUSED: - return _("The connection attempt was refused"); - case ACCEPTERR: - return _("Error while accepting the connection"); - case BINDERR: - return _("Error while Binding socket"); - case LISTENERR: - return _("Error while listening"); - case SERVERCLOSECONERR: - return _("The connection was reset/closed by the peer"); - case URLUNKNOWN: - return _("The URL Protocol was unknown"); - case URLBADPORT: - return _("The port specified in the URL is not valid!"); - case URLBADHOST: - return _("The Hostname specified in the URL is not valid!"); - case URLBADPATTERN: - return _("The Pattern specified in the URL does not look valid!"); - case HEOF: - return _("End of file reached in HTTP connection"); - case HERR: - return _("Error occured in HTTP data transfer"); - case HAUTHREQ: - return _("Authentification is required to access this resource"); - case HAUTHFAIL: - return _("Failed to Authenticate with host!"); - case HTTPNSFOD: - return _("The URL was not found on the host!"); - case FTPLOGREFUSED: - return _("The host disallowed the login attempt"); - case FTPPORTERR: - return _("The PORT request was rejected by the server"); - case FTPNSFOD: - return _("The object file/dir was not found on the host!"); - case FTPUNKNOWNTYPE: - return _("The TYPE specified in not known by the FTP server!"); - case FTPUNKNOWNCMD: - return _("The command is not known by the FTP server!"); - case FTPSIZEFAIL: - return _("The SIZE command failed"); - case FTPERR: - return _("Error occured in FTP data transfer"); - case FTPRESTFAIL: - return _("The REST command failed"); - case FTPACCDENIED: - return _("The peer did not allow access"); - case FTPPWDERR: - return _("The host rejected the password"); - case FTPPWDFAIL: - return _("The host rejected the password"); - case FTPINVPASV: - return _("The PASV (passive mode) was not supported the host"); - case FTPNOPASV: - return _("The host does not support PASV (passive mode) transfers"); - case FTPCONREFUSED: - return _("The connection attempt was refused"); - case FTPCWDFAIL: - return _("Failed to (CWD)change to the directory"); - case FTPSERVCLOSEDATLOGIN: - return - _ - ("The host said the requested service was unavailable and closed the control connection"); - case CONPORTERR: - return _("getsockname failed!"); + case CONSOCKERR: + return _("Unable to create socket"); - case GATEWAYTIMEOUT: - return - _ - ("The server, while acting as a gateway or proxy, received an invalid response from the upstream server it accessed in attempting to fulfill the request"); + case CONERROR: + return _("Error occured while connecting"); - case SERVICEUNAVAIL: - return - _ - ("The server is currently unable to handle the request due to a temporary overloading or maintenance of the server."); + case CONREFUSED: + return _("The connection attempt was refused"); - case BADGATEWAY: - return - _ - ("The server, while acting as a gateway or proxy, received an invalid response from the upstream server it accessed in attempting to fulfill the request"); + case ACCEPTERR: + return _("Error while accepting the connection"); - case INTERNALSERVERR: - return - _ - ("The server encountered an unexpected condition which prevented it from fulfilling the request."); + case BINDERR: + return _("Error while Binding socket"); - case UNKNOWNREQ: - return - _ - ("The server does not support the functionality required to fulfill the request."); + case LISTENERR: + return _("Error while listening"); - case FOPENERR: - return _("Error while opening file"); - case FWRITEERR: - return _("Error while writing to file"); + case SERVERCLOSECONERR: + return _("The connection was reset/closed by the peer"); - case DLABORTED: - return _("The Download was aborted"); - case DLLOCALFATAL: - return _("The Download encountered a local fatal error"); - case CANTRESUME: - return _("Error: Resuming this connection is not possible"); - case READERR: - return _("Error while reading data from socket"); - case WRITEERR: - return _("Error while writing data to socket"); - case PROXERR: - return _("Error while Proxying"); - case FILEISDIR: - return _("The location is a directory"); + case URLUNKNOWN: + return _("The URL Protocol was unknown"); - default: - return _("Unknown/Unsupported error code"); - } + case URLBADPORT: + return _("The port specified in the URL is not valid!"); + case URLBADHOST: + return _("The Hostname specified in the URL is not valid!"); + + case URLBADPATTERN: + return _("The Pattern specified in the URL does not look valid!"); + + case HEOF: + return _("End of file reached in HTTP connection"); + + case HERR: + return _("Error occured in HTTP data transfer"); + + case HAUTHREQ: + return _("Authentification is required to access this resource"); + + case HAUTHFAIL: + return _("Failed to Authenticate with host!"); + + case HTTPNSFOD: + return _("The URL was not found on the host!"); + + case FTPLOGREFUSED: + return _("The host disallowed the login attempt"); + + case FTPPORTERR: + return _("The PORT request was rejected by the server"); + + case FTPNSFOD: + return _("The object file/dir was not found on the host!"); + + case FTPUNKNOWNTYPE: + return _("The TYPE specified in not known by the FTP server!"); + + case FTPUNKNOWNCMD: + return _("The command is not known by the FTP server!"); + + case FTPSIZEFAIL: + return _("The SIZE command failed"); + + case FTPERR: + return _("Error occured in FTP data transfer"); + + case FTPRESTFAIL: + return _("The REST command failed"); + + case FTPACCDENIED: + return _("The peer did not allow access"); + + case FTPPWDERR: + return _("The host rejected the password"); + + case FTPPWDFAIL: + return _("The host rejected the password"); + + case FTPINVPASV: + return _("The PASV (passive mode) was not supported the host"); + + case FTPNOPASV: + return _("The host does not support PASV (passive mode) transfers"); + + case FTPCONREFUSED: + return _("The connection attempt was refused"); + + case FTPCWDFAIL: + return _("Failed to (CWD)change to the directory"); + + case FTPSERVCLOSEDATLOGIN: + return + _ + ("The host said the requested service was unavailable and closed the control connection"); + + case CONPORTERR: + return _("getsockname failed!"); + + case GATEWAYTIMEOUT: + return + _ + ("The server, while acting as a gateway or proxy, received an invalid response from the upstream server it accessed in attempting to fulfill the request"); + + case SERVICEUNAVAIL: + return + _ + ("The server is currently unable to handle the request due to a temporary overloading or maintenance of the server."); + + case BADGATEWAY: + return + _ + ("The server, while acting as a gateway or proxy, received an invalid response from the upstream server it accessed in attempting to fulfill the request"); + + case INTERNALSERVERR: + return + _ + ("The server encountered an unexpected condition which prevented it from fulfilling the request."); + + case UNKNOWNREQ: + return + _ + ("The server does not support the functionality required to fulfill the request."); + + case FOPENERR: + return _("Error while opening file"); + + case FWRITEERR: + return _("Error while writing to file"); + + case DLABORTED: + return _("The Download was aborted"); + + case DLLOCALFATAL: + return _("The Download encountered a local fatal error"); + + case CANTRESUME: + return _("Error: Resuming this connection is not possible"); + + case READERR: + return _("Error while reading data from socket"); + + case WRITEERR: + return _("Error while writing data to socket"); + + case PROXERR: + return _("Error while Proxying"); + + case FILEISDIR: + return _("The location is a directory"); + + default: + return _("Unknown/Unsupported error code"); + } } /* Cleanup handler which will be popped and which will be called when the thread is cancelled, it make sure that there will be no sockets left open if the thread is cancelled prematurely -*/ + */ #include "ftp.h" void cleanup_socks(void *cdata) { - connection_t *connection = (connection_t *) cdata; + connection_t *connection = (connection_t *)cdata; switch (connection->u.proto) - { - case URLHTTP: - cleanup_httpsocks(connection); - break; - case URLFTP: - if (ftp_use_proxy(connection) - && connection->ftp_proxy->type == HTTPPROXY) { - /* We have to cleanup the http socks instead - if we are going through a http proxy */ + case URLHTTP: cleanup_httpsocks(connection); - } else - cleanup_ftpsocks(connection); - break; - default: - proz_die(_("Error: unsupported protocol")); - } + break; + + case URLFTP: + if (ftp_use_proxy(connection) + && connection->ftp_proxy->type == HTTPPROXY) + { + /* We have to cleanup the http socks instead + if we are going through a http proxy */ + cleanup_httpsocks(connection); + } + else + cleanup_ftpsocks(connection); + break; + + default: + proz_die(_("Error: unsupported protocol")); + } } @@ -482,25 +522,26 @@ void cleanup_ftpsocks(connection_t * connection) if (connection->data_sock > 0) - { - flags = fcntl(connection->data_sock, F_GETFD, 0); - if (flags == -1) { - proz_debug("data sock invalid\n"); - } else - close_sock(&connection->data_sock); - } + flags = fcntl(connection->data_sock, F_GETFD, 0); + if (flags == -1) + { + proz_debug("data sock invalid\n"); + } + else + close_sock(&connection->data_sock); + } if (connection->ctrl_sock > 0) - { - flags = fcntl(connection->ctrl_sock, F_GETFD, 0); - if (flags == -1) { - proz_debug("control sock invalid\n"); - } else - close_sock(&connection->ctrl_sock); - } - + flags = fcntl(connection->ctrl_sock, F_GETFD, 0); + if (flags == -1) + { + proz_debug("control sock invalid\n"); + } + else + close_sock(&connection->ctrl_sock); + } } @@ -511,15 +552,15 @@ void cleanup_httpsocks(connection_t * connection) proz_debug("in clean http sock\n"); if (connection->data_sock > 0) - { - flags = fcntl(connection->data_sock, F_GETFD, 0); - if (flags == -1) { - proz_debug("sock invalid\n"); - } else - close(connection->data_sock); - } - + flags = fcntl(connection->data_sock, F_GETFD, 0); + if (flags == -1) + { + proz_debug("sock invalid\n"); + } + else + close(connection->data_sock); + } } diff --git a/libprozilla/src/misc.h b/libprozilla/src/misc.h index bf218fc..f7935ea 100644 --- a/libprozilla/src/misc.h +++ b/libprozilla/src/misc.h @@ -1,25 +1,25 @@ /****************************************************************************** - libprozilla - a download accelerator library - Copyright (C) 2001 Kalum Somaratna + libprozilla - a download accelerator library + Copyright (C) 2001 Kalum Somaratna - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ******************************************************************************/ /* Miscellaneous routines. */ -/* $Id: misc.h,v 1.25 2001/09/02 23:29:16 kalum Exp $ */ +/* $Id$ */ #ifndef MISC_H @@ -46,4 +46,4 @@ char *home_dir(void); void delay_ms(int ms); int close_sock(int *sock); void cleanup_socks(void *cdata); -#endif /* MISC_H */ +#endif /* MISC_H */ diff --git a/libprozilla/src/netrc.c b/libprozilla/src/netrc.c index ea8bd39..c3b2aea 100644 --- a/libprozilla/src/netrc.c +++ b/libprozilla/src/netrc.c @@ -6,19 +6,19 @@ it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. - + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - + You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ /* Slightly modified for libprozilla, by Grendel . */ -/* $Id: netrc.c,v 1.17 2001/08/09 23:35:26 kalum Exp $ */ +/* $Id$ */ #include "common.h" @@ -29,35 +29,37 @@ #define POPBUFSIZE BUFSIZ /****************************************************************************** - Maybe add NEWENTRY to the account information list, LIST. NEWENTRY is - set to a ready-to-use netrc_entry, in any event. + Maybe add NEWENTRY to the account information list, LIST. NEWENTRY is + set to a ready-to-use netrc_entry, in any event. ******************************************************************************/ static void maybe_add_to_list(netrc_entry ** newentry, netrc_entry ** list) { netrc_entry *a, *l; + a = *newentry; l = *list; /* We need an account name in order to add the entry to the list. */ if (a && !a->account) - { - /* Free any allocated space. */ - if (a->host) - kfree(a->host); - if (a->password) - kfree(a->password); - } else - { - if (a) { - /* Add the current machine into our list. */ - a->next = l; - l = a; + /* Free any allocated space. */ + if (a->host) + kfree(a->host); + if (a->password) + kfree(a->password); } + else + { + if (a) + { + /* Add the current machine into our list. */ + a->next = l; + l = a; + } - /* Allocate a new netrc_entry structure. */ - a = kmalloc(sizeof(netrc_entry)); - } + /* Allocate a new netrc_entry structure. */ + a = kmalloc(sizeof(netrc_entry)); + } /* Zero the structure, so that it is ready to use. */ memset(a, 0, sizeof(*a)); @@ -69,8 +71,8 @@ static void maybe_add_to_list(netrc_entry ** newentry, netrc_entry ** list) } /****************************************************************************** - Parse FILE as a .netrc file (as described in ftp(1)), and return a list of - entries. NULL is returned if the file could not be parsed. + Parse FILE as a .netrc file (as described in ftp(1)), and return a list of + entries. NULL is returned if the file could not be parsed. ******************************************************************************/ netrc_entry *parse_netrc(char *file) { @@ -90,10 +92,10 @@ netrc_entry *parse_netrc(char *file) fp = fopen(file, "r"); if (!fp) - { - /* Just return NULL if we can't open the file. */ - return NULL; - } + { + /* Just return NULL if we can't open the file. */ + return NULL; + } /* Initialize the file data. */ ln = 0; @@ -101,151 +103,153 @@ netrc_entry *parse_netrc(char *file) /* While there are lines in the file... */ while (fgets(buf, POPBUFSIZE, fp)) - { - ln++; - - /* Strip trailing CRLF. */ - for (p = buf + strlen(buf) - 1; (p >= buf) && isspace((unsigned) *p); - p--) - *p = '\0'; - - /* Parse the line. */ - p = buf; - - /* If the line is empty... */ - if (!*p) { - if (last_token == tok_macdef) - last_token = tok_nothing; /* End of macro. */ - else - continue; /* Otherwise ignore it. */ + ln++; + + /* Strip trailing CRLF. */ + for (p = buf + strlen(buf) - 1; (p >= buf) && isspace((unsigned)*p); + p--) + *p = '\0'; + + /* Parse the line. */ + p = buf; + + /* If the line is empty... */ + if (!*p) + { + if (last_token == tok_macdef) + last_token = tok_nothing; /* End of macro. */ + else + continue; /* Otherwise ignore it. */ + } + + /* If we are defining macros, then skip parsing the line. */ + while (*p && last_token != tok_macdef) + { + char quote_char = 0; + char *pp; + + /* Skip any whitespace. */ + while (*p && isspace((unsigned)*p)) + p++; + + /* Discard end-of-line comments. */ + if (*p == '#') + break; + + tok = pp = p; + + /* Find the end of the token. */ + while (*p && (quote_char || !isspace((unsigned)*p))) + { + if (quote_char) + { + if (quote_char == *p) + { + quote_char = 0; + p++; + } + else + { + *pp = *p; + p++; + pp++; + } + } + else + { + if (*p == '"' || *p == '\'') + quote_char = *p; + else + { + *pp = *p; + pp++; + } + p++; + } + } + + /* Null-terminate the token, if it isn't already. */ + if (*p) + *p++ = '\0'; + *pp = 0; + + switch (last_token) + { + case tok_login: + if (current) + current->account = kstrdup(tok); + else + premature_token = "login"; + break; + + case tok_machine: + /* Start a new machine entry. */ + maybe_add_to_list(¤t, &retval); + current->host = kstrdup(tok); + break; + + case tok_password: + if (current) + current->password = kstrdup(tok); + else + premature_token = "password"; + break; + + /* We handle most of tok_macdef above. */ + case tok_macdef: + if (!current) + premature_token = "macdef"; + break; + + /* We don't handle the account keyword at all. */ + case tok_account: + if (!current) + premature_token = "account"; + break; + + /* We handle tok_nothing below this switch. */ + case tok_nothing: + break; + } + + if (premature_token) + { + fprintf(stderr, + _("%s:%d: warning: found \"%s\" before any host names\n"), + file, ln, premature_token); + premature_token = NULL; + } + + if (last_token != tok_nothing) + /* We got a value, so reset the token state. */ + last_token = tok_nothing; + else + { + /* Fetch the next token. */ + if (!strcmp(tok, "default")) + maybe_add_to_list(¤t, &retval); + else if (!strcmp(tok, "login")) + last_token = tok_login; + else if (!strcmp(tok, "user")) + last_token = tok_login; + else if (!strcmp(tok, "macdef")) + last_token = tok_macdef; + else if (!strcmp(tok, "machine")) + last_token = tok_machine; + else if (!strcmp(tok, "password")) + last_token = tok_password; + else if (!strcmp(tok, "passwd")) + last_token = tok_password; + else if (!strcmp(tok, "account")) + last_token = tok_account; + else + fprintf(stderr, _("%s:%d: warning: unknown token \"%s\"\n"), + file, ln, tok); + } + } } - /* If we are defining macros, then skip parsing the line. */ - while (*p && last_token != tok_macdef) - { - char quote_char = 0; - char *pp; - - /* Skip any whitespace. */ - while (*p && isspace((unsigned) *p)) - p++; - - /* Discard end-of-line comments. */ - if (*p == '#') - break; - - tok = pp = p; - - /* Find the end of the token. */ - while (*p && (quote_char || !isspace((unsigned) *p))) - { - if (quote_char) - { - if (quote_char == *p) - { - quote_char = 0; - p++; - } else - { - *pp = *p; - p++; - pp++; - } - } else - { - if (*p == '"' || *p == '\'') - quote_char = *p; - else - { - *pp = *p; - pp++; - } - p++; - } - } - - /* Null-terminate the token, if it isn't already. */ - if (*p) - *p++ = '\0'; - *pp = 0; - - switch (last_token) - { - case tok_login: - if (current) - current->account = kstrdup(tok); - else - premature_token = "login"; - break; - - case tok_machine: - /* Start a new machine entry. */ - maybe_add_to_list(¤t, &retval); - current->host = kstrdup(tok); - break; - - case tok_password: - if (current) - current->password = kstrdup(tok); - else - premature_token = "password"; - break; - - /* We handle most of tok_macdef above. */ - case tok_macdef: - if (!current) - premature_token = "macdef"; - break; - - /* We don't handle the account keyword at all. */ - case tok_account: - if (!current) - premature_token = "account"; - break; - - /* We handle tok_nothing below this switch. */ - case tok_nothing: - break; - } - - if (premature_token) - { - fprintf(stderr, - _("%s:%d: warning: found \"%s\" before any host names\n"), - file, ln, premature_token); - premature_token = NULL; - } - - if (last_token != tok_nothing) - /* We got a value, so reset the token state. */ - last_token = tok_nothing; - else - { - /* Fetch the next token. */ - if (!strcmp(tok, "default")) - maybe_add_to_list(¤t, &retval); - else if (!strcmp(tok, "login")) - last_token = tok_login; - else if (!strcmp(tok, "user")) - last_token = tok_login; - else if (!strcmp(tok, "macdef")) - last_token = tok_macdef; - else if (!strcmp(tok, "machine")) - last_token = tok_machine; - else if (!strcmp(tok, "password")) - last_token = tok_password; - else if (!strcmp(tok, "passwd")) - last_token = tok_password; - else if (!strcmp(tok, "account")) - last_token = tok_account; - else - fprintf(stderr, _("%s:%d: warning: unknown token \"%s\"\n"), - file, ln, tok); - } - } - } - fclose(fp); /* Finalize the last machine entry we found. */ @@ -256,37 +260,37 @@ netrc_entry *parse_netrc(char *file) current = retval; retval = NULL; while (current) - { - netrc_entry *saved_reference; + { + netrc_entry *saved_reference; - /* Change the direction of the pointers. */ - saved_reference = current->next; - current->next = retval; + /* Change the direction of the pointers. */ + saved_reference = current->next; + current->next = retval; - /* Advance to the next node. */ - retval = current; - current = saved_reference; - } + /* Advance to the next node. */ + retval = current; + current = saved_reference; + } return retval; } /****************************************************************************** - Return the netrc entry from LIST corresponding to HOST. NULL is returned if - no such entry exists. + Return the netrc entry from LIST corresponding to HOST. NULL is returned if + no such entry exists. ******************************************************************************/ netrc_entry *search_netrc(netrc_entry * list, const char *host) { /* Look for the HOST in LIST. */ while (list) - { - if (!list->host) - break; /* We hit the default entry. */ - else if (!strcmp(list->host, host)) - break; /* We found a matching entry. */ + { + if (!list->host) + break; /* We hit the default entry. */ + else if (!strcmp(list->host, host)) + break; /* We found a matching entry. */ - list = list->next; - } + list = list->next; + } /* Return the matching entry, or NULL. */ return list; diff --git a/libprozilla/src/netrc.h b/libprozilla/src/netrc.h index 8766c06..539d574 100644 --- a/libprozilla/src/netrc.h +++ b/libprozilla/src/netrc.h @@ -16,7 +16,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -/* $Id: netrc.h,v 1.11 2001/05/30 15:01:40 kalum Exp $ */ +/* $Id$ */ #ifndef NETRC_H @@ -33,8 +33,8 @@ # define __BEGIN_DECLS extern "C" { # define __END_DECLS } #else -# define __BEGIN_DECLS /* Empty. */ -# define __END_DECLS /* Empty. */ +# define __BEGIN_DECLS /* Empty. */ +# define __END_DECLS /* Empty. */ #endif #undef __P @@ -63,4 +63,4 @@ __BEGIN_DECLS netrc_entry *parse_netrc __P((char *file)); netrc_entry *search_netrc __P((netrc_entry * list, const char *host)); __END_DECLS -#endif /* NETRC_H */ +#endif /* NETRC_H */ diff --git a/libprozilla/src/ping.c b/libprozilla/src/ping.c index 0712835..a91d73d 100644 --- a/libprozilla/src/ping.c +++ b/libprozilla/src/ping.c @@ -1,20 +1,20 @@ /****************************************************************************** - libprozilla - a download accelerator library - Copyright (C) 2001 Kalum Somaratna + libprozilla - a download accelerator library + Copyright (C) 2001 Kalum Somaratna - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ******************************************************************************/ @@ -40,9 +40,9 @@ uerr_t tcp_ping(ping_t * ping_data) struct timeval end_time; char ping_buf[TCP_PING_PACKSIZE]; int bytes_read; - struct addrinfo hints, *res=NULL; - char szPort[10]; - int error; + struct addrinfo hints, *res = NULL; + char szPort[10]; + int error; assert(ping_data->host); memset(&hints, 0, sizeof(hints)); @@ -51,16 +51,17 @@ uerr_t tcp_ping(ping_t * ping_data) hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; - error = getaddrinfo(ping_data->host, szPort, &hints, &res); - if (error) { + error = getaddrinfo(ping_data->host, szPort, &hints, &res); + if (error) + { return ping_data->err = HOSTERR; - } - - if ((ping_data->sock = socket(res->ai_family, res->ai_socktype, IPPROTO_TCP)) < 1) - { - free(res); - return ping_data->err = CONSOCKERR; - } + } + + if ((ping_data->sock = socket(res->ai_family, res->ai_socktype, IPPROTO_TCP)) < 1) + { + free(res); + return ping_data->err = CONSOCKERR; + } /* Experimental. */ flags = fcntl(ping_data->sock, F_GETFL, 0); @@ -73,62 +74,66 @@ uerr_t tcp_ping(ping_t * ping_data) gettimeofday(&start_time, 0); status = - connect(ping_data->sock, res->ai_addr, res->ai_addrlen); + connect(ping_data->sock, res->ai_addr, res->ai_addrlen); if ((status == -1) && (noblock != -1) && (errno == EINPROGRESS)) - { - fd_set writefd; - - FD_ZERO(&writefd); - FD_SET(ping_data->sock, &writefd); - - status = - select((ping_data->sock + 1), NULL, &writefd, NULL, - &ping_data->timeout); - - /* Do we need to retry if the err is EINTR? */ - - if (status > 0) { - socklen_t arglen = sizeof(int); + fd_set writefd; - if (getsockopt - (ping_data->sock, SOL_SOCKET, SO_ERROR, &status, &arglen) < 0) - status = errno; + FD_ZERO(&writefd); + FD_SET(ping_data->sock, &writefd); - if (status != 0) - errno = status, status = -1; + status = + select((ping_data->sock + 1), NULL, &writefd, NULL, + &ping_data->timeout); - if (errno == EINPROGRESS) - errno = ETIMEDOUT; - } else if (status == 0) - errno = ETIMEDOUT, status = -1; - } + /* Do we need to retry if the err is EINTR? */ + + if (status > 0) + { + socklen_t arglen = sizeof(int); + + if (getsockopt + (ping_data->sock, SOL_SOCKET, SO_ERROR, &status, &arglen) < 0) + status = errno; + + if (status != 0) + errno = status, status = -1; + + if (errno == EINPROGRESS) + errno = ETIMEDOUT; + } + else if (status == 0) + errno = ETIMEDOUT, status = -1; + } if (status < 0) - { - close_sock(&ping_data->sock); + { + close_sock(&ping_data->sock); - if (errno == ECONNREFUSED) - { - free(res); - return ping_data->err = CONREFUSED; - } else if (errno == ETIMEDOUT) - { - free(res); - return ping_data->err = PINGTIMEOUT; - } else - { - free(res); - return ping_data->err = CONERROR; + if (errno == ECONNREFUSED) + { + free(res); + return ping_data->err = CONREFUSED; + } + else if (errno == ETIMEDOUT) + { + free(res); + return ping_data->err = PINGTIMEOUT; + } + else + { + free(res); + return ping_data->err = CONERROR; + } } - } else - { - flags = fcntl(ping_data->sock, F_GETFL, 0); + else + { + flags = fcntl(ping_data->sock, F_GETFL, 0); - if (flags != -1) - fcntl(ping_data->sock, F_SETFL, flags & ~O_NONBLOCK); - } + if (flags != -1) + fcntl(ping_data->sock, F_SETFL, flags & ~O_NONBLOCK); + } /* setsockopt(*sock, SOL_SOCKET, SO_KEEPALIVE, (char *)&opt, (int) sizeof(opt)); */ @@ -139,18 +144,18 @@ uerr_t tcp_ping(ping_t * ping_data) /*So far so good connection established */ bytes_read = - krecv(ping_data->sock, ping_buf, TCP_PING_PACKSIZE, 0, - &ping_data->timeout); + krecv(ping_data->sock, ping_buf, TCP_PING_PACKSIZE, 0, + &ping_data->timeout); close_sock(&ping_data->sock); proz_debug("bytes read = %d", bytes_read); if (bytes_read == -1) - { - if (errno == ETIMEDOUT) - return ping_data->err = PINGTIMEOUT; - else - return ping_data->err = READERR; - } + { + if (errno == ETIMEDOUT) + return ping_data->err = PINGTIMEOUT; + else + return ping_data->err = READERR; + } if (bytes_read == 0 || bytes_read < TCP_PING_PACKSIZE) return ping_data->err = READERR; @@ -172,12 +177,10 @@ uerr_t tcp_ping(ping_t * ping_data) void proz_mass_ping(ftps_request_t * request) { - request->mass_ping_running = TRUE; if (pthread_create(&request->mass_ping_thread, NULL, - (void *) &mass_ping, (void *) request) != 0) + (void *)&mass_ping, (void *)request) != 0) proz_die(_("Error: Not enough system resources")); - } void proz_cancel_mass_ping(ftps_request_t * request) @@ -185,7 +188,7 @@ void proz_cancel_mass_ping(ftps_request_t * request) /*TODO Rewrite so that this will terminate the pingin threads as well */ request->mass_ping_running = FALSE; pthread_cancel(request->mass_ping_thread); - pthread_join(request->mass_ping_thread,0); + pthread_join(request->mass_ping_thread, 0); } void mass_ping(ftps_request_t * request) @@ -198,7 +201,7 @@ void mass_ping(ftps_request_t * request) simul_pings = request->max_simul_pings; pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); - ping_threads = (pthread_t *) kmalloc(sizeof(pthread_t) * simul_pings); + ping_threads = (pthread_t *)kmalloc(sizeof(pthread_t) * simul_pings); ping_requests = kmalloc(sizeof(ping_t) * request->num_mirrors); num_iter = request->num_mirrors / simul_pings; @@ -214,95 +217,97 @@ void mass_ping(ftps_request_t * request) k = 0; for (i = 0; i < num_iter; i++) - { - for (j = 0; j < simul_pings; j++) + { + for (j = 0; j < simul_pings; j++) + { + ping_t ping_request; + + memset(ping_requests + k, 0, sizeof(ping_request)); + /*FIXME */ + ping_requests[k].timeout.tv_sec = request->ping_timeout.tv_sec; + ping_requests[k].timeout.tv_usec = request->ping_timeout.tv_usec; + ping_requests[k].host = strdup(request->mirrors[k].server_name); + ping_requests[k].port = 21; + + if (pthread_create(&ping_threads[j], NULL, + (void *)&tcp_ping, + (void *)(ping_requests + k)) != 0) + proz_die("Error: Not enough system resources" + "to create thread!\n"); + k++; + } + + k -= simul_pings; + + for (j = 0; j < simul_pings; j++) + { + /*Wait till the end of each thread. */ + pthread_join(ping_threads[j], NULL); + if (ping_requests[k].err == PINGOK) + { + pthread_mutex_lock(&request->access_mutex); + request->mirrors[k].milli_secs = + (ping_requests[k].ping_time.tv_sec * 1000) + + (ping_requests[k].ping_time.tv_usec / 1000); + request->mirrors[k].status = RESPONSEOK; + pthread_mutex_unlock(&request->access_mutex); + } + else + { + pthread_mutex_lock(&request->access_mutex); + request->mirrors[k].status = NORESPONSE; + pthread_mutex_unlock(&request->access_mutex); + } + k++; + } + } + + + for (j = 0; j < num_left; j++) { ping_t ping_request; memset(ping_requests + k, 0, sizeof(ping_request)); /*FIXME */ ping_requests[k].timeout.tv_sec = request->ping_timeout.tv_sec; - ping_requests[k].timeout.tv_usec = request->ping_timeout.tv_usec; + ping_requests[k].timeout.tv_usec = 0; ping_requests[k].host = strdup(request->mirrors[k].server_name); ping_requests[k].port = 21; if (pthread_create(&ping_threads[j], NULL, - (void *) &tcp_ping, - (void *) (ping_requests + k)) != 0) - proz_die("Error: Not enough system resources" - "to create thread!\n"); + (void *)&tcp_ping, + (void *)(&ping_requests[k])) != 0) + proz_die("Error: Not enough system resources" "to create thread!\n"); + k++; } - k -= simul_pings; - - for (j = 0; j < simul_pings; j++) - { - /*Wait till the end of each thread. */ - pthread_join(ping_threads[j], NULL); - if (ping_requests[k].err == PINGOK) - { - pthread_mutex_lock(&request->access_mutex); - request->mirrors[k].milli_secs = - (ping_requests[k].ping_time.tv_sec * 1000) + - (ping_requests[k].ping_time.tv_usec / 1000); - request->mirrors[k].status = RESPONSEOK; - pthread_mutex_unlock(&request->access_mutex); - } else - { - pthread_mutex_lock(&request->access_mutex); - request->mirrors[k].status = NORESPONSE; - pthread_mutex_unlock(&request->access_mutex); - } - k++; - } - } - - - for (j = 0; j < num_left; j++) - { - ping_t ping_request; - - memset(ping_requests + k, 0, sizeof(ping_request)); - /*FIXME */ - ping_requests[k].timeout.tv_sec = request->ping_timeout.tv_sec; - ping_requests[k].timeout.tv_usec = 0; - ping_requests[k].host = strdup(request->mirrors[k].server_name); - ping_requests[k].port = 21; - - if (pthread_create(&ping_threads[j], NULL, - (void *) &tcp_ping, - (void *) (&ping_requests[k])) != 0) - proz_die("Error: Not enough system resources" "to create thread!\n"); - - k++; - } - k -= num_left; for (j = 0; j < num_left; j++) - { - /*Wait till the end of each thread. */ - pthread_join(ping_threads[j], NULL); + { + /*Wait till the end of each thread. */ + pthread_join(ping_threads[j], NULL); - /*Wait till the end of each thread. */ - pthread_join(ping_threads[j], NULL); - if (ping_requests[k].err == PINGOK) - { - pthread_mutex_lock(&request->access_mutex); - request->mirrors[k].milli_secs = - (ping_requests[k].ping_time.tv_sec * 1000) + - (ping_requests[k].ping_time.tv_usec / 1000); - request->mirrors[k].status = RESPONSEOK; - pthread_mutex_unlock(&request->access_mutex); - } else - { - pthread_mutex_lock(&request->access_mutex); - request->mirrors[k].status = NORESPONSE; - pthread_mutex_unlock(&request->access_mutex); + /*Wait till the end of each thread. */ + pthread_join(ping_threads[j], NULL); + if (ping_requests[k].err == PINGOK) + { + pthread_mutex_lock(&request->access_mutex); + request->mirrors[k].milli_secs = + (ping_requests[k].ping_time.tv_sec * 1000) + + (ping_requests[k].ping_time.tv_usec / 1000); + request->mirrors[k].status = RESPONSEOK; + pthread_mutex_unlock(&request->access_mutex); + } + else + { + pthread_mutex_lock(&request->access_mutex); + request->mirrors[k].status = NORESPONSE; + pthread_mutex_unlock(&request->access_mutex); + } + k++; } - k++; - } proz_debug("mass_ping complete."); pthread_mutex_lock(&request->access_mutex); diff --git a/libprozilla/src/ping.h b/libprozilla/src/ping.h index 5cc9863..76c3757 100644 --- a/libprozilla/src/ping.h +++ b/libprozilla/src/ping.h @@ -1,20 +1,20 @@ /****************************************************************************** - libprozilla - a download accelerator library - Copyright (C) 2001 Kalum Somaratna + libprozilla - a download accelerator library + Copyright (C) 2001 Kalum Somaratna - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ******************************************************************************/ diff --git a/libprozilla/src/prozilla.h b/libprozilla/src/prozilla.h index 206d96b..556bc77 100644 --- a/libprozilla/src/prozilla.h +++ b/libprozilla/src/prozilla.h @@ -1,25 +1,25 @@ /****************************************************************************** - libprozilla - a download accelerator library - Copyright (C) 2001 Kalum Somaratna + libprozilla - a download accelerator library + Copyright (C) 2001 Kalum Somaratna - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ******************************************************************************/ /* Main include-file. */ -/* $Id: prozilla.h,v 1.63 2005/09/19 15:25:48 kalum Exp $ */ +/* $Id$ */ #ifndef PROZILLA_H @@ -34,55 +34,55 @@ extern "C" { #endif - typedef enum { - /*Connect establishment related values */ - NOCONERROR, HOSTERR, CONSOCKERR, CONERROR, - CONREFUSED, ACCEPTERR, ACCEPTOK, BINDERR, BINDOK, LISTENERR, LISTENOK, - SERVERCLOSECONERR, CONPORTERR, +typedef enum { + /*Connect establishment related values */ + NOCONERROR, HOSTERR, CONSOCKERR, CONERROR, + CONREFUSED, ACCEPTERR, ACCEPTOK, BINDERR, BINDOK, LISTENERR, LISTENOK, + SERVERCLOSECONERR, CONPORTERR, - /* URL handling related value */ - URLOK, URLHTTP, URLFTP, URLFILE, URLUNKNOWN, URLBADPORT, - URLBADHOST, URLBADPATTERN, + /* URL handling related value */ + URLOK, URLHTTP, URLFTP, URLFILE, URLUNKNOWN, URLBADPORT, + URLBADHOST, URLBADPATTERN, - /* HTTP related values */ - NEWLOCATION, HOK, HEOF, HERR, HAUTHREQ, HAUTHFAIL, - HTTPNSFOD, + /* HTTP related values */ + NEWLOCATION, HOK, HEOF, HERR, HAUTHREQ, HAUTHFAIL, + HTTPNSFOD, - /*FTP related value */ - FTPOK, FTPLOGINC, FTPLOGREFUSED, FTPPORTERR, - FTPNSFOD, FTPRETROK, FTPUNKNOWNTYPE, FTPUNKNOWNCMD, FTPSIZEFAIL, - FTPERR, FTPRESTFAIL, FTPACCDENIED, - FTPPWDERR, - FTPINVPASV, FTPNOPASV, FTPCONREFUSED, FTPCWDFAIL, FTPPWDFAIL, - FTPSERVCLOSEDATLOGIN, - /*FTP parsing related values */ - FTPPARSEOK, FTPPARSENOTEXIST, FTPPARSEFAIL, + /*FTP related value */ + FTPOK, FTPLOGINC, FTPLOGREFUSED, FTPPORTERR, + FTPNSFOD, FTPRETROK, FTPUNKNOWNTYPE, FTPUNKNOWNCMD, FTPSIZEFAIL, + FTPERR, FTPRESTFAIL, FTPACCDENIED, + FTPPWDERR, + FTPINVPASV, FTPNOPASV, FTPCONREFUSED, FTPCWDFAIL, FTPPWDFAIL, + FTPSERVCLOSEDATLOGIN, + /*FTP parsing related values */ + FTPPARSEOK, FTPPARSENOTEXIST, FTPPARSEFAIL, - /*Error values that can happen due to failed proxie request */ - GATEWAYTIMEOUT, SERVICEUNAVAIL, BADGATEWAY, INTERNALSERVERR, - UNKNOWNREQ, + /*Error values that can happen due to failed proxie request */ + GATEWAYTIMEOUT, SERVICEUNAVAIL, BADGATEWAY, INTERNALSERVERR, + UNKNOWNREQ, - /*File handling related return values. */ - FOPENERR, FWRITEERR, - RETROK, + /*File handling related return values. */ + FOPENERR, FWRITEERR, + RETROK, - /*Download related retrn values. */ - DLINPROGRESS, DLABORTED, DLDONE, CANTRESUME, DLLOCALFATAL, - DLREMOTEFATAL, - /*FTPSEARCH+ping related return values. */ - FTPSINPROGRESS, MASSPINGINPROGRESS, FTPSFAIL, MASSPINGDONE, + /*Download related retrn values. */ + DLINPROGRESS, DLABORTED, DLDONE, CANTRESUME, DLLOCALFATAL, + DLREMOTEFATAL, + /*FTPSEARCH+ping related return values. */ + FTPSINPROGRESS, MASSPINGINPROGRESS, FTPSFAIL, MASSPINGDONE, - /*PING related values */ - PINGOK, PINGTIMEOUT, - /*Misc Value */ - RETRFINISHED, READERR, + /*PING related values */ + PINGOK, PINGTIMEOUT, + /*Misc Value */ + RETRFINISHED, READERR, - PROXERR, WRITEERR, - FILEISDIR, - MIRINFOK, MIRPARSEOK, MIRPARSEFAIL, FILEGETOK, - /*Related to file joining */ - JOININPROGRESS, JOINDONE, JOINERR - } uerr_t; + PROXERR, WRITEERR, + FILEISDIR, + MIRINFOK, MIRPARSEOK, MIRPARSEFAIL, FILEGETOK, + /*Related to file joining */ + JOININPROGRESS, JOINDONE, JOINERR +} uerr_t; #define FTP_BUFFER_SIZE 2048 @@ -105,467 +105,466 @@ extern "C" { #define DEFAULT_USER_AGENT "Prozilla" - typedef char longstring[1024]; +typedef char longstring[1024]; /* Callback message function. */ - typedef void (*message_proc) (const char *msg, void *cb_data); +typedef void (*message_proc)(const char *msg, void *cb_data); /* Structure containing info on a URL. */ - typedef struct _urlinfo { - char *url; /* Unchanged URL. */ - uerr_t proto; /* URL protocol. */ - char *host; /* Extracted hostname. */ - unsigned short port; - char ftp_type; - char *path, *dir, *file; /* Path, dir and file (properly decoded). */ - char *user, *passwd; /* For FTP. */ +typedef struct _urlinfo { + char *url; /* Unchanged URL. */ + uerr_t proto; /* URL protocol. */ + char *host; /* Extracted hostname. */ + unsigned short port; + char ftp_type; + char *path, *dir, *file; /* Path, dir and file (properly decoded). */ + char *user, *passwd; /* For FTP. */ - char *referer; /* The source from which the request - URI was obtained. */ - } urlinfo; + char *referer; /* The source from which the request + URI was obtained. */ +} urlinfo; - typedef enum { - USERatSITE, - USERatPROXYUSERatSITE, - USERatSITE_PROXYUSER, - PROXYUSERatSITE, - LOGINthenUSERatSITE, - OPENSITE, - SITESITE, - HTTPPROXY, - FTPGATE, - WINGATE - } proxy_type; +typedef enum { + USERatSITE, + USERatPROXYUSERatSITE, + USERatSITE_PROXYUSER, + PROXYUSERatSITE, + LOGINthenUSERatSITE, + OPENSITE, + SITESITE, + HTTPPROXY, + FTPGATE, + WINGATE +} proxy_type; - typedef enum { - IDLE = 0, - CONNECTING, - LOGGININ, - DOWNLOADING, - COMPLETED, - LOGINFAIL, - CONREJECT, - REMOTEFATAL, - LOCALFATAL, - TIMEDOUT, - MAXTRYS - } dl_status; +typedef enum { + IDLE = 0, + CONNECTING, + LOGGININ, + DOWNLOADING, + COMPLETED, + LOGINFAIL, + CONREJECT, + REMOTEFATAL, + LOCALFATAL, + TIMEDOUT, + MAXTRYS +} dl_status; - typedef enum { - NOT_FOUND, - REGULAR_FILE, - DIRECTORY, - SYMBOLIC_LINK, - UNKNOWN - } file_type_t; +typedef enum { + NOT_FOUND, + REGULAR_FILE, + DIRECTORY, + SYMBOLIC_LINK, + UNKNOWN +} file_type_t; - typedef struct { - off_t len; /* Received length. */ - off_t contlen; /* Expected length. */ - int res; /* The result of last read. */ +typedef struct { + off_t len; /* Received length. */ + off_t contlen; /* Expected length. */ + int res; /* The result of last read. */ - /* -1 = Accept ranges not found. - 0 = Accepts range is none. - 1 = Accepts ranges. */ - int accept_ranges; + /* -1 = Accept ranges not found. + 0 = Accepts range is none. + 1 = Accepts ranges. */ + int accept_ranges; - char *newloc; /* New location (redirection). */ - char *remote_time; /* Remote time-stamp string. */ - char *error; /* Textual HTTP error. */ - int statcode; /* Status code. */ - } http_stat_t; + char *newloc; /* New location (redirection). */ + char *remote_time; /* Remote time-stamp string. */ + char *error; /* Textual HTTP error. */ + int statcode; /* Status code. */ +} http_stat_t; - typedef struct { - urlinfo proxy_url; - char *username; - char *passwd; - proxy_type type; - boolean use_proxy; - } proxy_info; +typedef struct { + urlinfo proxy_url; + char *username; + char *passwd; + proxy_type type; + boolean use_proxy; +} proxy_info; - typedef struct libprozinfo { - int argc; - char **argv; - boolean debug_mode; +typedef struct libprozinfo { + int argc; + char **argv; + boolean debug_mode; - /* For netrc. */ - netrc_entry *netrc_list; - boolean use_netrc; + /* For netrc. */ + netrc_entry *netrc_list; + boolean use_netrc; - char *home_dir; - char *ftp_default_user; - char *ftp_default_passwd; - char *dl_dir; - char *output_dir; - char *log_dir; - boolean ftp_use_pasv; - proxy_info *ftp_proxy; - proxy_info *http_proxy; - boolean http_no_cache; - /* the default timeout for all the connection types (ctrl, data etc) */ - struct timeval conn_timeout; - struct timeval conn_retry_delay; - int max_attempts; - long max_bps_per_dl; - } libprozinfo; + char *home_dir; + char *ftp_default_user; + char *ftp_default_passwd; + char *dl_dir; + char *output_dir; + char *log_dir; + boolean ftp_use_pasv; + proxy_info *ftp_proxy; + proxy_info *http_proxy; + boolean http_no_cache; + /* the default timeout for all the connection types (ctrl, data etc) */ + struct timeval conn_timeout; + struct timeval conn_retry_delay; + int max_attempts; + long max_bps_per_dl; +} libprozinfo; - extern libprozinfo libprozrtinfo; +extern libprozinfo libprozrtinfo; - typedef struct response_line { - char *line; - struct response_line *next; - } response_line; +typedef struct response_line { + char *line; + struct response_line *next; +} response_line; - typedef struct connection_t { +typedef struct connection_t { + /* struct which contains the parsed url info. It includes the remote file, + path,protocol etc. */ + urlinfo u; - /* struct which contains the parsed url info. It includes the remote file, - path,protocol etc. */ - urlinfo u; + /* The error status of the connection. */ + uerr_t err; - /* The error status of the connection. */ - uerr_t err; + /* Proxy specific info. */ - /* Proxy specific info. */ + proxy_info *ftp_proxy; + proxy_info *http_proxy; - proxy_info *ftp_proxy; - proxy_info *http_proxy; + /* Netrc. */ + boolean use_netrc; - /* Netrc. */ - boolean use_netrc; + /* FTP specific info. */ + boolean ftp_use_pasv; + struct timeval xfer_timeout; + struct timeval conn_timeout; + struct timeval ctrl_timeout; + unsigned char pasv_addr[6]; + int ctrl_sock; + int data_sock; + int listen_sock; - /* FTP specific info. */ - boolean ftp_use_pasv; - struct timeval xfer_timeout; - struct timeval conn_timeout; - struct timeval ctrl_timeout; - unsigned char pasv_addr[6]; - int ctrl_sock; - int data_sock; - int listen_sock; + /* Additional info about what this URL is. */ + /* FIXME Should this be in the url_info struct? */ + file_type_t file_type; - /* Additional info about what this URL is. */ - /* FIXME Should this be in the url_info struct? */ - file_type_t file_type; + /* The lines that the server returned. */ + response_line *serv_ret_lines; - /* The lines that the server returned. */ - response_line *serv_ret_lines; + /* Does the server support resume? */ + boolean resume_support; - /* Does the server support resume? */ - boolean resume_support; + /* The file name to save the data to locally. */ + char *localfile; - /* The file name to save the data to locally. */ - char *localfile; + /* Pointer to file that we will be saving the data to locally. */ + FILE *fp; - /* Pointer to file that we will be saving the data to locally. */ - FILE *fp; + /* Tells whether to open the file for appending or for writing etc. + Used for adding resume support. */ + char *file_mode; - /* Tells whether to open the file for appending or for writing etc. - Used for adding resume support. */ - char *file_mode; + /* FIXME Add an enum here to say whether run mode is resume or normal etc. + and remove the file mode. */ - /* FIXME Add an enum here to say whether run mode is resume or normal etc. - and remove the file mode. */ + off_t remote_startpos; + off_t orig_remote_startpos; + off_t remote_endpos; + off_t remote_bytes_received; - off_t remote_startpos; - off_t orig_remote_startpos; - off_t remote_endpos; - off_t remote_bytes_received; + off_t main_file_size; - off_t main_file_size; + /* The permanent base offset from the beginning of the file, put in + anticipation of making the threads download to a single file. */ + off_t local_base_offset; - /* The permanent base offset from the beginning of the file, put in - anticipation of making the threads download to a single file. */ - off_t local_base_offset; + /* Indicates the startpos of the localfile. It is always 0 in normal mode + and can be any positive value in resume mode. */ + off_t local_startpos; - /* Indicates the startpos of the localfile. It is always 0 in normal mode - and can be any positive value in resume mode. */ - off_t local_startpos; + /* The start position at the beginning of the download. */ + off_t orig_local_startpos; + /* long bytes_xferred; */ + dl_status status; + char *szBuffer; - /* The start position at the beginning of the download. */ - off_t orig_local_startpos; - /* long bytes_xferred; */ - dl_status status; - char *szBuffer; + /* Tells the download thread whether to abort the download or not. */ + boolean abort; - /* Tells the download thread whether to abort the download or not. */ - boolean abort; + /* Information about the connection's start and end time. */ + struct timeval time_begin; + struct timeval time_end; - /* Information about the connection's start and end time. */ - struct timeval time_begin; - struct timeval time_end; + /* Info about whether to retry the thread or not. */ + boolean retry; - /* Info about whether to retry the thread or not. */ - boolean retry; + /* The number of attempts to try to complete a connection. 0 means unlimited + connection attempts. */ + int max_attempts; + /* The number of attempts that this connection has been tried */ + int attempts; - /* The number of attempts to try to complete a connection. 0 means unlimited - connection attempts. */ - int max_attempts; - /* The number of attempts that this connection has been tried */ - int attempts; + /* The time when to try to restart the connection. */ + struct timeval retry_delay; - /* The time when to try to restart the connection. */ - struct timeval retry_delay; + /* Each connection will acquire this mutex before changing state. */ + pthread_mutex_t *status_change_mutex; + /*This will be broadcast when the connection starts connecting */ + pthread_cond_t connecting_cond; - /* Each connection will acquire this mutex before changing state. */ - pthread_mutex_t *status_change_mutex; - /*This will be broadcast when the connection starts connecting */ - pthread_cond_t connecting_cond; + /* User agent for HTTP. */ + char *user_agent; - /* User agent for HTTP. */ - char *user_agent; - - http_stat_t hs; - message_proc msg_proc; - /*additional callback data whcih is specified by the user - that is passed to msg_proc */ - void *cb_data; - /* Indicates whether a conenction is running or not */ - int running; - /*Mutex used to lock acesss to data in this struct - that is accesed/written by other threads */ - pthread_mutex_t access_mutex; - /*This indicates that we should use the pragma no-cache directive for http proxies */ - boolean http_no_cache; - /*The rate of this connection whcih is calcualted */ - long rate_bps; - /*We limit the connections speed to this */ - long max_allowed_bps; - } connection_t; + http_stat_t hs; + message_proc msg_proc; + /*additional callback data whcih is specified by the user + that is passed to msg_proc */ + void *cb_data; + /* Indicates whether a conenction is running or not */ + int running; + /*Mutex used to lock acesss to data in this struct + that is accesed/written by other threads */ + pthread_mutex_t access_mutex; + /*This indicates that we should use the pragma no-cache directive for http proxies */ + boolean http_no_cache; + /*The rate of this connection whcih is calcualted */ + long rate_bps; + /*We limit the connections speed to this */ + long max_allowed_bps; +} connection_t; - typedef enum { - UNTESTED = 0, RESPONSEOK, NORESPONSE, ERROR - } ftp_mirror_stat_t; +typedef enum { + UNTESTED = 0, RESPONSEOK, NORESPONSE, ERROR +} ftp_mirror_stat_t; - typedef enum { - LYCOS, FILESEARCH_RU - } ftpsearch_server_type_t; +typedef enum { + LYCOS, FILESEARCH_RU +} ftpsearch_server_type_t; - typedef struct { - char *path; - boolean valid; - } mirror_path_t; +typedef struct { + char *path; + boolean valid; +} mirror_path_t; - typedef struct ftp_mirror { - char *server_name; - mirror_path_t *paths; - char *file_name; - char *full_name; - char *file_size; - struct timeval tv; - int milli_secs; - int num_paths; - ftp_mirror_stat_t status; - int copied; - boolean resume_supported; - int max_simul_connections; - } ftp_mirror_t; +typedef struct ftp_mirror { + char *server_name; + mirror_path_t *paths; + char *file_name; + char *full_name; + char *file_size; + struct timeval tv; + int milli_secs; + int num_paths; + ftp_mirror_stat_t status; + int copied; + boolean resume_supported; + int max_simul_connections; +} ftp_mirror_t; - typedef struct { - off_t file_size; - char *file_name; - connection_t *connection; - ftpsearch_server_type_t server_type; - ftp_mirror_t *mirrors; - int num_mirrors; - uerr_t err; - boolean info_running; - boolean mass_ping_running; - pthread_mutex_t access_mutex; - pthread_t info_thread; - pthread_t mass_ping_thread; - int max_simul_pings; - struct timeval ping_timeout; - urlinfo *requested_url; - } ftps_request_t; +typedef struct { + off_t file_size; + char *file_name; + connection_t *connection; + ftpsearch_server_type_t server_type; + ftp_mirror_t *mirrors; + int num_mirrors; + uerr_t err; + boolean info_running; + boolean mass_ping_running; + pthread_mutex_t access_mutex; + pthread_t info_thread; + pthread_t mass_ping_thread; + int max_simul_pings; + struct timeval ping_timeout; + urlinfo *requested_url; +} ftps_request_t; - typedef struct download_t { - urlinfo u; - char *dl_dir; - char *log_dir; - char *output_dir; - connection_t **pconnections; - pthread_t *threads; - pthread_mutex_t status_change_mutex; - int num_connections; - /* Optional will be called back with info about download. */ - message_proc msg_proc; - /*additional callback data which is specified by the user - that is passed to msg_proc */ - void *cb_data; - off_t main_file_size; - boolean resume_mode; - struct timeval start_time; - /*Does this DL support resume? */ - boolean resume_support; - /*This contains the building status, 1 = building,0 build finished, -1 error occured */ - int building; - /*This is the percentage of the file that is been built currently */ - float file_build_percentage; - int max_simul_connections; - /*Mutex used to lock acesss to data in this struct - that is accesed/written by other threads */ - pthread_mutex_t access_mutex; - /* The message that is returned when the file build process is finished - */ - char *file_build_msg; - /*Max mps for this download */ - long max_allowed_bps; - ftps_request_t *ftps_info; - boolean using_ftpsearch; - pthread_t join_thread; - } download_t; +typedef struct download_t { + urlinfo u; + char *dl_dir; + char *log_dir; + char *output_dir; + connection_t **pconnections; + pthread_t *threads; + pthread_mutex_t status_change_mutex; + int num_connections; + /* Optional will be called back with info about download. */ + message_proc msg_proc; + /*additional callback data which is specified by the user + that is passed to msg_proc */ + void *cb_data; + off_t main_file_size; + boolean resume_mode; + struct timeval start_time; + /*Does this DL support resume? */ + boolean resume_support; + /*This contains the building status, 1 = building,0 build finished, -1 error occured */ + int building; + /*This is the percentage of the file that is been built currently */ + float file_build_percentage; + int max_simul_connections; + /*Mutex used to lock acesss to data in this struct + that is accesed/written by other threads */ + pthread_mutex_t access_mutex; + /* The message that is returned when the file build process is finished + */ + char *file_build_msg; + /*Max mps for this download */ + long max_allowed_bps; + ftps_request_t *ftps_info; + boolean using_ftpsearch; + pthread_t join_thread; +} download_t; - typedef struct { - /* the number of connections that this download was started with */ - int num_connections; - /*I have added these newly */ - int version; /*The version of this logfile */ - /*Indicates whether we have got info about the files size, final URL etc */ - boolean got_info; - off_t file_size; - int url_len; /*The length in bytes of the url text stred in the log file */ - char *url; - int reserved[30]; - } logfile; +typedef struct { + /* the number of connections that this download was started with */ + int num_connections; + /*I have added these newly */ + int version; /*The version of this logfile */ + /*Indicates whether we have got info about the files size, final URL etc */ + boolean got_info; + off_t file_size; + int url_len; /*The length in bytes of the url text stred in the log file */ + char *url; + int reserved[30]; +} logfile; - /*Structs for logfile handling */ +/*Structs for logfile handling */ - typedef struct { - char *host; - int port; - struct timeval timeout; - struct timeval ping_time; - int sock; - uerr_t err; - } ping_t; +typedef struct { + char *host; + int port; + struct timeval timeout; + struct timeval ping_time; + int sock; + uerr_t err; +} ping_t; /*Functions for URL parsing */ - uerr_t proz_parse_url(const char *url, urlinfo * u, boolean strict); - urlinfo *proz_copy_url(urlinfo * u); - void proz_free_url(urlinfo * u, boolean complete); +uerr_t proz_parse_url(const char *url, urlinfo * u, boolean strict); +urlinfo *proz_copy_url(urlinfo * u); +void proz_free_url(urlinfo * u, boolean complete); /*Functions that set values which will apply for all conenctions. */ - int proz_init(int argc, char **argv); - void proz_shutdown(void); - void proz_die(const char *message, ...); +int proz_init(int argc, char **argv); +void proz_shutdown(void); +void proz_die(const char *message, ...); - void proz_set_http_proxy(proxy_info * proxy); - void proz_set_ftp_proxy(proxy_info * proxy); - void proz_use_ftp_proxy(boolean use); - void proz_use_http_proxy(boolean use); - void proz_set_connection_timeout(struct timeval *timeout); - void proz_set_connection_retry_delay(struct timeval *delay); - void proz_set_download_dir(char *dir); - void proz_set_logfile_dir(char *dir); - void proz_set_output_dir(char *dir); - char *proz_get_libprozilla_version(); +void proz_set_http_proxy(proxy_info * proxy); +void proz_set_ftp_proxy(proxy_info * proxy); +void proz_use_ftp_proxy(boolean use); +void proz_use_http_proxy(boolean use); +void proz_set_connection_timeout(struct timeval *timeout); +void proz_set_connection_retry_delay(struct timeval *delay); +void proz_set_download_dir(char *dir); +void proz_set_logfile_dir(char *dir); +void proz_set_output_dir(char *dir); +char *proz_get_libprozilla_version(); - /*Functions for loggind debug messages */ - void proz_debug(const char *format, ...); - void proz_debug_delete_log(); +/*Functions for loggind debug messages */ +void proz_debug(const char *format, ...); +void proz_debug_delete_log(); /*Functions which are for handling a single connection. */ - connection_t * proz_connection_init(urlinfo * url, pthread_mutex_t * mutex); - void proz_connection_set_url(connection_t * connection, urlinfo *url); +connection_t * proz_connection_init(urlinfo * url, pthread_mutex_t * mutex); +void proz_connection_set_url(connection_t * connection, urlinfo *url); - char *proz_connection_get_status_string(connection_t * connection); - off_t proz_connection_get_total_bytes_got(connection_t * connection); - void proz_get_url_info_loop(connection_t * connection, pthread_t *thread); - off_t proz_connection_get_total_remote_bytes_got(connection_t * - connection); - void proz_connection_set_msg_proc(connection_t * connection, - message_proc msg_proc, void *cb_data); - void proz_connection_free_connection(connection_t * connection, - boolean complete); - dl_status proz_connection_get_status(connection_t * connection); - boolean proz_connection_running(connection_t * connection); +char *proz_connection_get_status_string(connection_t * connection); +off_t proz_connection_get_total_bytes_got(connection_t * connection); +void proz_get_url_info_loop(connection_t * connection, pthread_t *thread); +off_t proz_connection_get_total_remote_bytes_got(connection_t * + connection); +void proz_connection_set_msg_proc(connection_t * connection, + message_proc msg_proc, void *cb_data); +void proz_connection_free_connection(connection_t * connection, + boolean complete); +dl_status proz_connection_get_status(connection_t * connection); +boolean proz_connection_running(connection_t * connection); /*Functions which are for handling a download */ - download_t *proz_download_init(urlinfo * u); - int proz_download_setup_connections_no_ftpsearch(download_t * download, - connection_t * - connection, - int req_connections); - void proz_download_start_downloads(download_t * download, - boolean resume); - int proz_download_load_resume_info(download_t * download); - void proz_download_stop_downloads(download_t * download); +download_t *proz_download_init(urlinfo * u); +int proz_download_setup_connections_no_ftpsearch(download_t * download, + connection_t * + connection, + int req_connections); +void proz_download_start_downloads(download_t * download, + boolean resume); +int proz_download_load_resume_info(download_t * download); +void proz_download_stop_downloads(download_t * download); - boolean proz_download_all_dls_status(download_t * download, - dl_status status); - boolean proz_download_all_dls_err(download_t * download, uerr_t err); +boolean proz_download_all_dls_status(download_t * download, + dl_status status); +boolean proz_download_all_dls_err(download_t * download, uerr_t err); - boolean proz_download_all_dls_filensfod(download_t * download); - boolean proz_download_all_dls_ftpcwdfail(download_t * download); +boolean proz_download_all_dls_filensfod(download_t * download); +boolean proz_download_all_dls_ftpcwdfail(download_t * download); - off_t proz_download_get_total_bytes_got(download_t * download); - uerr_t proz_download_handle_threads(download_t * download); - int proz_download_prev_download_exists(download_t * download); - float proz_download_get_average_speed(download_t * download); - int proz_download_delete_dl_file(download_t * download); - void proz_download_wait_till_all_end(download_t * download); - off_t proz_download_get_total_remote_bytes_got(download_t * download); - void proz_download_set_msg_proc(download_t * download, - message_proc msg_proc, void *cb_data); - off_t proz_download_get_est_time_left(download_t * download); - void proz_download_free_download(download_t * download, - boolean complete); - int proz_download_target_exist(download_t * download); - int proz_download_delete_target(download_t * download); +off_t proz_download_get_total_bytes_got(download_t * download); +uerr_t proz_download_handle_threads(download_t * download); +int proz_download_prev_download_exists(download_t * download); +float proz_download_get_average_speed(download_t * download); +int proz_download_delete_dl_file(download_t * download); +void proz_download_wait_till_all_end(download_t * download); +off_t proz_download_get_total_remote_bytes_got(download_t * download); +void proz_download_set_msg_proc(download_t * download, + message_proc msg_proc, void *cb_data); +off_t proz_download_get_est_time_left(download_t * download); +void proz_download_free_download(download_t * download, + boolean complete); +int proz_download_target_exist(download_t * download); +int proz_download_delete_target(download_t * download); - /* Functions related to handling the logfile created for a download */ - int proz_log_read_logfile(logfile * lf, download_t * download, - boolean load_con_info); - int proz_log_delete_logfile(download_t * download); - int proz_log_logfile_exists(download_t * download); - char *proz_strerror(uerr_t error); +/* Functions related to handling the logfile created for a download */ +int proz_log_read_logfile(logfile * lf, download_t * download, + boolean load_con_info); +int proz_log_delete_logfile(download_t * download); +int proz_log_logfile_exists(download_t * download); +char *proz_strerror(uerr_t error); - /*Ftpsearch releated */ +/*Ftpsearch releated */ ftps_request_t * proz_ftps_request_init( - urlinfo * requested_url, off_t file_size, - char *ftps_loc, - ftpsearch_server_type_t server_type, - int num_req_mirrors); - void proz_get_complete_mirror_list(ftps_request_t * request); - boolean proz_request_info_running(ftps_request_t * request); - boolean proz_request_mass_ping_running(ftps_request_t * request); - void proz_mass_ping(ftps_request_t * request); - void proz_sort_mirror_list(ftp_mirror_t * mirrors, int num_servers); - void proz_cancel_mirror_list_request(ftps_request_t * request); + urlinfo * requested_url, off_t file_size, + char *ftps_loc, + ftpsearch_server_type_t server_type, + int num_req_mirrors); +void proz_get_complete_mirror_list(ftps_request_t * request); +boolean proz_request_info_running(ftps_request_t * request); +boolean proz_request_mass_ping_running(ftps_request_t * request); +void proz_mass_ping(ftps_request_t * request); +void proz_sort_mirror_list(ftp_mirror_t * mirrors, int num_servers); +void proz_cancel_mirror_list_request(ftps_request_t * request); - int proz_download_setup_connections_ftpsearch(download_t * download, - connection_t * connection, - ftps_request_t * request, - int req_connections); - void proz_cancel_mass_ping(ftps_request_t * request); +int proz_download_setup_connections_ftpsearch(download_t * download, + connection_t * connection, + ftps_request_t * request, + int req_connections); +void proz_cancel_mass_ping(ftps_request_t * request); - /*Misc functions */ - int proz_timeval_subtract(struct timeval *result, struct timeval *x, - struct timeval *y); +/*Misc functions */ +int proz_timeval_subtract(struct timeval *result, struct timeval *x, + struct timeval *y); - /*Funcs related to joining the downloaded file portions */ - void proz_download_join_downloads(download_t * download); - uerr_t proz_download_get_join_status(download_t *download); - float proz_download_get_file_build_percentage(download_t *download); - void proz_download_cancel_joining_thread(download_t * download); - void proz_download_wait_till_end_joining_thread(download_t * download); +/*Funcs related to joining the downloaded file portions */ +void proz_download_join_downloads(download_t * download); +uerr_t proz_download_get_join_status(download_t *download); +float proz_download_get_file_build_percentage(download_t *download); +void proz_download_cancel_joining_thread(download_t * download); +void proz_download_wait_till_end_joining_thread(download_t * download); #ifdef __cplusplus } #endif -#endif /* PROZILLA_H */ +#endif /* PROZILLA_H */ diff --git a/libprozilla/src/test.c b/libprozilla/src/test.c index 9fc724e..453505a 100644 --- a/libprozilla/src/test.c +++ b/libprozilla/src/test.c @@ -1,25 +1,25 @@ /****************************************************************************** - libprozilla - a download accelerator library - Copyright (C) 2001 Kalum Somaratna + libprozilla - a download accelerator library + Copyright (C) 2001 Kalum Somaratna - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ******************************************************************************/ /* A test program. */ -/* $Id: test.c,v 1.30 2001/09/23 01:39:16 kalum Exp $ */ +/* $Id$ */ #include "common.h" @@ -35,7 +35,7 @@ /****************************************************************************** - The main function. + The main function. ******************************************************************************/ int main(int argc, char **argv) { @@ -49,18 +49,18 @@ int main(int argc, char **argv) libprozrtinfo.conn_timeout.tv_sec = 100; proz_prepare_ftps_request(&request, "ddd-3.3.tar.bz2", 4811576, - "http://ftpsearch.uniovi.es:8000/ftpsearch", - LYCOS, 40); + "http://ftpsearch.uniovi.es:8000/ftpsearch", + LYCOS, 40); proz_get_complete_mirror_list(&request); for (i = 0; i < request.num_mirrors; i++) - { - printf("%s\n", request.mirrors[i].server_name); - printf("\t%d\n", request.mirrors[i].num_paths); - for (j = 0; j < request.mirrors[i].num_paths; j++) - printf("\t\t%s\n", request.mirrors[i].paths[j].path); - } + { + printf("%s\n", request.mirrors[i].server_name); + printf("\t%d\n", request.mirrors[i].num_paths); + for (j = 0; j < request.mirrors[i].num_paths; j++) + printf("\t\t%s\n", request.mirrors[i].paths[j].path); + } timeout.tv_sec = 5; timeout.tv_usec = 0; diff --git a/libprozilla/src/url.c b/libprozilla/src/url.c index 44604af..c953ee9 100644 --- a/libprozilla/src/url.c +++ b/libprozilla/src/url.c @@ -1,21 +1,21 @@ /* URL handling. Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc. - + This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. - + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - + You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -/* $Id: url.c,v 1.23 2001/10/27 11:24:40 kalum Exp $ */ +/* $Id$ */ #include "common.h" @@ -31,7 +31,7 @@ However, a string that does not match anything in the list will be considered a relative URL. Thus it's important that this list has anything anyone could think of being legal. - + There are wild things here. :-) Take a look at to see more fun. */ @@ -82,8 +82,8 @@ char *protostrings[] = { /* Similar to former, but for supported protocols: */ proto_t sup_protos[] = { - {"http://", URLHTTP, DEFAULT_HTTP_PORT}, - {"ftp://", URLFTP, DEFAULT_FTP_PORT} + { "http://", URLHTTP, DEFAULT_HTTP_PORT }, + { "ftp://", URLFTP, DEFAULT_FTP_PORT } /* { "file://", URLFILE, DEFAULT_FTP_PORT } */ }; @@ -98,7 +98,7 @@ enum { #define R urlchr_reserved #define U urlchr_unsafe -#define RU R|U +#define RU R | U #define urlchr_test(c, mask) (urlchr_table[(unsigned char)(c)] & (mask)) @@ -120,8 +120,8 @@ enum { '0' and '9', or between 'A' and 'F', or between 'a' and 'f'. The result is a number between 0 and 15. If X is not a hexadecimal digit character, the result is undefined. */ -#define XCHAR_TO_XDIGIT(x) \ - (((x) >= '0' && (x) <= '9') ? \ +#define XCHAR_TO_XDIGIT(x) \ + (((x) >= '0' && (x) <= '9') ? \ ((x) - '0') : (toupper(x) - 'A' + 10)) /* The reverse of the above: convert a HEX digit in the [0, 15] range @@ -129,25 +129,25 @@ enum { always in upper case. */ #define XDIGIT_TO_XCHAR(x) (((x) < 10) ? ((x) + '0') : ((x) - 10 + 'A')) -#define ARRAY_SIZE(array) (sizeof (array) / sizeof (*(array))) +#define ARRAY_SIZE(array) (sizeof(array) / sizeof(*(array))) const static unsigned char urlchr_table[256] = { - U, U, U, U, U, U, U, U, /* NUL SOH STX ETX EOT ENQ ACK BEL */ - U, U, U, U, U, U, U, U, /* BS HT LF VT FF CR SO SI */ - U, U, U, U, U, U, U, U, /* DLE DC1 DC2 DC3 DC4 NAK SYN ETB */ - U, U, U, U, U, U, U, U, /* CAN EM SUB ESC FS GS RS US */ - U, 0, U, U, 0, U, R, 0, /* SP ! " # $ % & ' */ - 0, 0, 0, R, 0, 0, 0, R, /* ( ) * + , - . / */ - 0, 0, 0, 0, 0, 0, 0, 0, /* 0 1 2 3 4 5 6 7 */ - 0, 0, U, R, U, R, U, R, /* 8 9 : ; < = > ? */ - RU, 0, 0, 0, 0, 0, 0, 0, /* @ A B C D E F G */ - 0, 0, 0, 0, 0, 0, 0, 0, /* H I J K L M N O */ - 0, 0, 0, 0, 0, 0, 0, 0, /* P Q R S T U V W */ - 0, 0, 0, U, U, U, U, 0, /* X Y Z [ \ ] ^ _ */ - U, 0, 0, 0, 0, 0, 0, 0, /* ` a b c d e f g */ - 0, 0, 0, 0, 0, 0, 0, 0, /* h i j k l m n o */ - 0, 0, 0, 0, 0, 0, 0, 0, /* p q r s t u v w */ - 0, 0, 0, U, U, U, U, U, /* x y z { | } ~ DEL */ + U, U, U, U, U, U, U, U, /* NUL SOH STX ETX EOT ENQ ACK BEL */ + U, U, U, U, U, U, U, U, /* BS HT LF VT FF CR SO SI */ + U, U, U, U, U, U, U, U, /* DLE DC1 DC2 DC3 DC4 NAK SYN ETB */ + U, U, U, U, U, U, U, U, /* CAN EM SUB ESC FS GS RS US */ + U, 0, U, U, 0, U, R, 0, /* SP ! " # $ % & ' */ + 0, 0, 0, R, 0, 0, 0, R, /* ( ) * + , - . / */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 0 1 2 3 4 5 6 7 */ + 0, 0, U, R, U, R, U, R, /* 8 9 : ; < = > ? */ + RU, 0, 0, 0, 0, 0, 0, 0, /* @ A B C D E F G */ + 0, 0, 0, 0, 0, 0, 0, 0, /* H I J K L M N O */ + 0, 0, 0, 0, 0, 0, 0, 0, /* P Q R S T U V W */ + 0, 0, 0, U, U, U, U, 0, /* X Y Z [ \ ] ^ _ */ + U, 0, 0, 0, 0, 0, 0, 0, /* ` a b c d e f g */ + 0, 0, 0, 0, 0, 0, 0, 0, /* h i j k l m n o */ + 0, 0, 0, 0, 0, 0, 0, 0, /* p q r s t u v w */ + 0, 0, 0, U, U, U, U, U, /* x y z { | } ~ DEL */ U, U, U, U, U, U, U, U, U, U, U, U, U, U, U, U, U, U, U, U, U, U, U, U, U, U, U, U, U, U, U, U, @@ -181,6 +181,7 @@ int skip_uname(const char *url) { const char *p; const char *q = NULL; + for (p = url; *p && *p != '/'; p++) if (*p == '@') q = p; @@ -200,26 +201,27 @@ int skip_uname(const char *url) void decode_string(char *s) { - char *t = s; /* t - tortoise */ - char *h = s; /* h - hare */ + char *t = s; /* t - tortoise */ + char *h = s; /* h - hare */ for (; *h; h++, t++) - { - if (*h != '%') + { + if (*h != '%') // if(1) - { - copychar: - *t = *h; - } else - { - /* Do nothing if '%' is not followed by two hex digits. */ - if (!*(h + 1) || !*(h + 2) - || !(isxdigit(*(h + 1)) && isxdigit(*(h + 2)))) - goto copychar; - *t = (XCHAR_TO_XDIGIT(*(h + 1)) << 4) + XCHAR_TO_XDIGIT(*(h + 2)); - h += 2; + { +copychar: + *t = *h; + } + else + { + /* Do nothing if '%' is not followed by two hex digits. */ + if (!*(h + 1) || !*(h + 2) + || !(isxdigit(*(h + 1)) && isxdigit(*(h + 2)))) + goto copychar; + *t = (XCHAR_TO_XDIGIT(*(h + 1)) << 4) + XCHAR_TO_XDIGIT(*(h + 2)); + h += 2; + } } - } *t = '\0'; } @@ -231,34 +233,37 @@ char *encode_string_maybe(const char *s) char *p2, *newstr; int newlen; int addition = 0; + /*Changes Grendel: (*p1!='%') added */ - + for (p1 = s; *p1; p1++) - if ((*p1!='%') && UNSAFE_CHAR(*p1)) - addition += 2; /* Two more characters (hex digits) */ + if ((*p1 != '%') && UNSAFE_CHAR(*p1)) + addition += 2; + /* Two more characters (hex digits) */ if (!addition) - return (char *) s; + return (char *)s; newlen = (p1 - s) + addition; - newstr = (char *) kmalloc(newlen + 1); + newstr = (char *)kmalloc(newlen + 1); p1 = s; p2 = newstr; while (*p1) - { - // if (UNSAFE_CHAR(*p1)) -if ((*p1!='%') && UNSAFE_CHAR(*p1)) -/* if(0)*/ { - const unsigned char c = *p1++; - *p2++ = '%'; - *p2++ = XDIGIT_TO_XCHAR(c >> 4); - *p2++ = XDIGIT_TO_XCHAR(c & 0xf); - } else - *p2++ = *p1++; - } + // if (UNSAFE_CHAR(*p1)) + if ((*p1 != '%') && UNSAFE_CHAR(*p1)) +/* if(0)*/ + { + const unsigned char c = *p1++; + *p2++ = '%'; + *p2++ = XDIGIT_TO_XCHAR(c >> 4); + *p2++ = XDIGIT_TO_XCHAR(c & 0xf); + } + else + *p2++ = *p1++; + } *p2 = '\0'; assert(p2 - newstr == newlen); @@ -282,15 +287,15 @@ char *encode_string(const char *s) the old value of PTR is freed and PTR is made to point to the newly allocated storage. */ -#define ENCODE(ptr) do { \ - char *e_new = encode_string_maybe (ptr); \ - if (e_new != ptr) \ - { \ - kfree (ptr); \ - ptr = e_new; \ - } \ -} while (0) - +#define ENCODE(ptr) do { \ + char *e_new = encode_string_maybe(ptr); \ + if (e_new != ptr) \ + { \ + kfree(ptr); \ + ptr = e_new; \ + } \ + } while (0) + /* Returns the protocol type if URL's protocol is supported, or URLUNKNOWN if not. */ uerr_t urlproto(const char *url) @@ -300,17 +305,19 @@ uerr_t urlproto(const char *url) for (i = 0; i < ARRAY_SIZE(sup_protos); i++) if (!strncasecmp(url, sup_protos[i].name, strlen(sup_protos[i].name))) return sup_protos[i].ind; - for (i = 0; url[i] && url[i] != ':' && url[i] != '/'; i++); + for (i = 0; url[i] && url[i] != ':' && url[i] != '/'; i++) + ; if (url[i] == ':') - { - for (++i; url[i] && url[i] != '/'; i++) - if (!isdigit(url[i])) - return URLBADPORT; - if (url[i - 1] == ':') - return URLFTP; - else - return URLHTTP; - } else + { + for (++i; url[i] && url[i] != '/'; i++) + if (!isdigit(url[i])) + return URLBADPORT; + if (url[i - 1] == ':') + return URLFTP; + else + return URLHTTP; + } + else return URLHTTP; } @@ -321,29 +328,30 @@ char process_ftp_type(char *path) int len = strlen(path); if (len >= 7 && !memcmp(path + len - 7, ";type=", 6)) - { - path[len - 7] = '\0'; - return path[len - 1]; - } else + { + path[len - 7] = '\0'; + return path[len - 1]; + } + else return '\0'; } /* Canonicalize PATH, and return a new path. The new path differs from PATH in that: - Multple `/'s are collapsed to a single `/'. - Leading `./'s and trailing `/.'s are removed. - Trailing `/'s are removed. - Non-leading `../'s and trailing `..'s are handled by removing - portions of the path. + Multple `/'s are collapsed to a single `/'. + Leading `./'s and trailing `/.'s are removed. + Trailing `/'s are removed. + Non-leading `../'s and trailing `..'s are handled by removing + portions of the path. E.g. "a/b/c/./../d/.." will yield "a/b". This function originates from GNU Bash. Changes for Wget: - Always use '/' as stub_char. - Don't check for local things using canon_stat. - Change the original string instead of strdup-ing. - React correctly when beginning with `./' and `../'. */ + Always use '/' as stub_char. + Don't check for local things using canon_stat. + Change the original string instead of strdup-ing. + React correctly when beginning with `./' and `../'. */ void path_simplify(char *path) { register int i, start, ddot; @@ -360,92 +368,94 @@ void path_simplify(char *path) i = 0; ddot = 0; while (1) - { - if (path[i] == '.' && path[i + 1] == '/') - i += 2; - else if (path[i] == '.' && path[i + 1] == '.' && path[i + 2] == '/') { - i += 3; - ddot = 1; - } else - break; - } + if (path[i] == '.' && path[i + 1] == '/') + i += 2; + else if (path[i] == '.' && path[i + 1] == '.' && path[i + 2] == '/') + { + i += 3; + ddot = 1; + } + else + break; + } if (i) strcpy(path, path + i - ddot); /* Replace single `.' or `..' with `/'. */ if ((path[0] == '.' && path[1] == '\0') || (path[0] == '.' && path[1] == '.' && path[2] == '\0')) - { - path[0] = stub_char; - path[1] = '\0'; - return; - } + { + path[0] = stub_char; + path[1] = '\0'; + return; + } /* Walk along PATH looking for things to compact. */ i = 0; while (1) - { - if (!path[i]) - break; - - while (path[i] && path[i] != '/') - i++; - - start = i++; - - /* If we didn't find any slashes, then there is nothing left to do. */ - if (!path[start]) - break; - - /* Handle multiple `/'s in a row. */ - while (path[i] == '/') - i++; - - if ((start + 1) != i) { - strcpy(path + start + 1, path + i); - i = start + 1; - } + if (!path[i]) + break; - /* Check for trailing `/'. */ - if (start && !path[i]) - { - zero_last: - path[--i] = '\0'; - break; - } + while (path[i] && path[i] != '/') + i++; - /* Check for `../', `./' or trailing `.' by itself. */ - if (path[i] == '.') - { - /* Handle trailing `.' by itself. */ - if (!path[i + 1]) - goto zero_last; + start = i++; - /* Handle `./'. */ - if (path[i + 1] == '/') - { - strcpy(path + i, path + i + 1); - i = (start < 0) ? 0 : start; - continue; - } + /* If we didn't find any slashes, then there is nothing left to do. */ + if (!path[start]) + break; - /* Handle `../' or trailing `..' by itself. */ - if (path[i + 1] == '.' && (path[i + 2] == '/' || !path[i + 2])) - { - while (--start > -1 && path[start] != '/'); - strcpy(path + start + 1, path + i + 2); - i = (start < 0) ? 0 : start; - continue; - } - } /* path == '.' */ - } /* while */ + /* Handle multiple `/'s in a row. */ + while (path[i] == '/') + i++; + + if ((start + 1) != i) + { + strcpy(path + start + 1, path + i); + i = start + 1; + } + + /* Check for trailing `/'. */ + if (start && !path[i]) + { +zero_last: + path[--i] = '\0'; + break; + } + + /* Check for `../', `./' or trailing `.' by itself. */ + if (path[i] == '.') + { + /* Handle trailing `.' by itself. */ + if (!path[i + 1]) + goto zero_last; + + /* Handle `./'. */ + if (path[i + 1] == '/') + { + strcpy(path + i, path + i + 1); + i = (start < 0) ? 0 : start; + continue; + } + + /* Handle `../' or trailing `..' by itself. */ + if (path[i + 1] == '.' && (path[i + 2] == '/' || !path[i + 2])) + { + while (--start > -1 && path[start] != '/') + ; + strcpy(path + start + 1, path + i + 2); + i = (start < 0) ? 0 : start; + continue; + } + } /* path == '.' */ + } /* while */ if (!*path) - { - *path = stub_char; - path[1] = '\0'; - } + { + *path = stub_char; + path[1] = '\0'; + } } /* Special versions of DOTP and DDOTP for parse_dir(). They work like @@ -454,13 +464,14 @@ void path_simplify(char *path) strings. */ #define PD_DOTP(x) ((*(x) == '.') && (!*((x) + 1) || *((x) + 1) == '?')) -#define PD_DDOTP(x) ((*(x) == '.') && (*(x) == '.') \ - && (!*((x) + 2) || *((x) + 2) == '?')) +#define PD_DDOTP(x) ((*(x) == '.') && (*(x) == '.') \ + && (!*((x) + 2) || *((x) + 2) == '?')) /* Like strlen(), but allow the URL to be ended with '?'. */ int urlpath_length(const char *url) { const char *q = strchr(url, '?'); + if (q) return q - url; return strlen(url); @@ -477,45 +488,51 @@ void parse_dir(const char *path, char **dir, char **file) int i, l; l = urlpath_length(path); - for (i = l; i && path[i] != '/'; i--); + for (i = l; i && path[i] != '/'; i--) + ; - if (!i && *path != '/') /* Just filename */ - { - if (PD_DOTP(path) || PD_DDOTP(path)) + if (!i && *path != '/') /* Just filename */ { - *dir = strdupdelim(path, path + l); - *file = kstrdup(path + l); /* normally empty, but could - contain ?... */ - } else - { - *dir = kstrdup(""); /* This is required because of FTP */ - *file = kstrdup(path); + if (PD_DOTP(path) || PD_DDOTP(path)) + { + *dir = strdupdelim(path, path + l); + *file = kstrdup(path + l); /* normally empty, but could + contain ?... */ + } + else + { + *dir = kstrdup(""); /* This is required because of FTP */ + *file = kstrdup(path); + } } - } else if (!i) /* /filename */ - { - if (PD_DOTP(path + 1) || PD_DDOTP(path + 1)) + else if (!i) /* /filename */ { - *dir = strdupdelim(path, path + l); - *file = kstrdup(path + l); /* normally empty, but could - contain ?... */ - } else - { - *dir = kstrdup("/"); - *file = kstrdup(path + 1); + if (PD_DOTP(path + 1) || PD_DDOTP(path + 1)) + { + *dir = strdupdelim(path, path + l); + *file = kstrdup(path + l); /* normally empty, but could + contain ?... */ + } + else + { + *dir = kstrdup("/"); + *file = kstrdup(path + 1); + } } - } else /* Nonempty directory with or without a filename */ - { - if (PD_DOTP(path + i + 1) || PD_DDOTP(path + i + 1)) + else /* Nonempty directory with or without a filename */ { - *dir = strdupdelim(path, path + l); - *file = kstrdup(path + l); /* normally empty, but could - contain ?... */ - } else - { - *dir = strdupdelim(path, path + i); - *file = kstrdup(path + i + 1); + if (PD_DOTP(path + i + 1) || PD_DDOTP(path + i + 1)) + { + *dir = strdupdelim(path, path + l); + *file = kstrdup(path + l); /* normally empty, but could + contain ?... */ + } + else + { + *dir = strdupdelim(path, path + i); + *file = kstrdup(path + i + 1); + } } - } } /* Skip the protocol part of the URL, e.g. `http://'. If no protocol @@ -565,20 +582,20 @@ static uerr_t parse_uname(const char *url, char **user, char **passwd) return URLOK; /* Else find the username and password. */ for (p = q = col = url; *p && *p != '/'; p++) - { - if (*p == ':' && !*user) { - *user = (char *) kmalloc(p - url + 1); - memcpy(*user, url, p - url); - (*user)[p - url] = '\0'; - col = p + 1; + if (*p == ':' && !*user) + { + *user = (char *)kmalloc(p - url + 1); + memcpy(*user, url, p - url); + (*user)[p - url] = '\0'; + col = p + 1; + } + if (*p == '@') + q = p; } - if (*p == '@') - q = p; - } /* Decide whether you have only the username or both. */ where = *user ? passwd : user; - *where = (char *) kmalloc(q - col + 1); + *where = (char *)kmalloc(q - col + 1); memcpy(*where, col, q - col); (*where)[q - col] = '\0'; return URLOK; @@ -610,27 +627,27 @@ char *str_url(const urlinfo * u, int hide) if (u->user) user = encode_string(u->user); if (u->passwd) - { - if (hide) - /* Don't output the password, or someone might see it over the user's - shoulder (or in saved wget output). Don't give away the number of - characters in the password, either, as we did in past versions of - this code, when we replaced the password characters with 'x's. */ - passwd = kstrdup(""); - else - passwd = encode_string(u->passwd); - } + { + if (hide) + /* Don't output the password, or someone might see it over the user's + shoulder (or in saved wget output). Don't give away the number of + characters in the password, either, as we did in past versions of + this code, when we replaced the password characters with 'x's. */ + passwd = kstrdup(""); + else + passwd = encode_string(u->passwd); + } if (u->proto == URLFTP && *dir == '/') - { - char *tmp = (char *) kmalloc(strlen(dir) + 3); - /*sprintf (tmp, "%%2F%s", dir + 1); */ - tmp[0] = '%'; - tmp[1] = '2'; - tmp[2] = 'F'; - strcpy(tmp + 3, dir + 1); - kfree(dir); - dir = tmp; - } + { + char *tmp = (char *)kmalloc(strlen(dir) + 3); + /*sprintf (tmp, "%%2F%s", dir + 1); */ + tmp[0] = '%'; + tmp[1] = '2'; + tmp[2] = 'F'; + strcpy(tmp + 3, dir + 1); + kfree(dir); + dir = tmp; + } ln = strlen(proto_name); lu = user ? strlen(user) : 0; @@ -638,7 +655,7 @@ char *str_url(const urlinfo * u, int hide) lh = strlen(host); ld = strlen(dir); lf = strlen(file); - res = (char *) kmalloc(ln + lu + lp + lh + ld + lf + 20); /* safe sex */ + res = (char *)kmalloc(ln + lu + lp + lh + ld + lf + 20); /* safe sex */ /* sprintf (res, "%s%s%s%s%s%s:%d/%s%s%s", proto_name, (user ? user : ""), (passwd ? ":" : ""), (passwd ? passwd : ""), (user ? "@" : ""), @@ -647,27 +664,27 @@ char *str_url(const urlinfo * u, int hide) memcpy(res, proto_name, ln); l += ln; if (user) - { - memcpy(res + l, user, lu); - l += lu; - if (passwd) { - res[l++] = ':'; - memcpy(res + l, passwd, lp); - l += lp; + memcpy(res + l, user, lu); + l += lu; + if (passwd) + { + res[l++] = ':'; + memcpy(res + l, passwd, lp); + l += lp; + } + res[l++] = '@'; } - res[l++] = '@'; - } memcpy(res + l, host, lh); l += lh; if (u->port != proto_default_port) - { - res[l++] = ':'; + { + res[l++] = ':'; - sprintf(res + l, "%ld", (long) u->port); + sprintf(res + l, "%ld", (long)u->port); - l += numdigit(u->port); - } + l += numdigit(u->port); + } res[l++] = '/'; memcpy(res + l, dir, ld); l += ld; @@ -704,10 +721,10 @@ char *str_url(const urlinfo * u, int hide) uerr_t proz_parse_url(const char *url, urlinfo * u, int strict) { int i, l, abs_ftp; - int recognizable; /* Recognizable URL is the one where - the protocol name was explicitly - named, i.e. it wasn't deduced from - the URL format. */ + int recognizable; /* Recognizable URL is the one where + the protocol name was explicitly + named, i.e. it wasn't deduced from + the URL format. */ uerr_t type; memset(u, 0, sizeof(urlinfo)); @@ -715,11 +732,11 @@ uerr_t proz_parse_url(const char *url, urlinfo * u, int strict) if (strict && !recognizable) return URLUNKNOWN; for (i = 0, l = 0; i < ARRAY_SIZE(sup_protos); i++) - { - l = strlen(sup_protos[i].name); - if (!strncasecmp(sup_protos[i].name, url, l)) - break; - } + { + l = strlen(sup_protos[i].name); + if (!strncasecmp(sup_protos[i].name, url, l)) + break; + } /* If protocol is recognizable, but unsupported, bail out, else suppose unknown. */ if (recognizable && i == ARRAY_SIZE(sup_protos)) @@ -735,7 +752,8 @@ uerr_t proz_parse_url(const char *url, urlinfo * u, int strict) them for now). */ if (recognizable) l += skip_uname(url + l); - for (i = l; url[i] && url[i] != ':' && url[i] != '/'; i++); + for (i = l; url[i] && url[i] != ':' && url[i] != '/'; i++) + ; if (i == l) return URLBADHOST; /* Get the hostname. */ @@ -744,51 +762,53 @@ uerr_t proz_parse_url(const char *url, urlinfo * u, int strict) /* Assume no port has been given. */ u->port = 0; if (url[i] == ':') - { - /* We have a colon delimiting the hostname. It could mean that - a port number is following it, or a directory. */ - if (isdigit(url[++i])) /* A port number */ { - if (type == URLUNKNOWN) - u->proto = type = URLHTTP; - for (; url[i] && url[i] != '/'; i++) - if (isdigit(url[i])) - u->port = 10 * u->port + (url[i] - '0'); - else - return URLBADPORT; - if (!u->port) - return URLBADPORT; - } else if (type == URLUNKNOWN) /* or a directory */ - u->proto = type = URLFTP; - else /* or just a misformed port number */ - return URLBADPORT; - } else if (type == URLUNKNOWN) + /* We have a colon delimiting the hostname. It could mean that + a port number is following it, or a directory. */ + if (isdigit(url[++i])) /* A port number */ + { + if (type == URLUNKNOWN) + u->proto = type = URLHTTP; + for (; url[i] && url[i] != '/'; i++) + if (isdigit(url[i])) + u->port = 10 * u->port + (url[i] - '0'); + else + return URLBADPORT; + if (!u->port) + return URLBADPORT; + } + else if (type == URLUNKNOWN) /* or a directory */ + u->proto = type = URLFTP; + else /* or just a misformed port number */ + return URLBADPORT; + } + else if (type == URLUNKNOWN) u->proto = type = URLHTTP; if (!u->port) - { - int ind; - for (ind = 0; ind < ARRAY_SIZE(sup_protos); ind++) - if (sup_protos[ind].ind == type) - break; - if (ind == ARRAY_SIZE(sup_protos)) - return URLUNKNOWN; - u->port = sup_protos[ind].port; - } + { + int ind; + for (ind = 0; ind < ARRAY_SIZE(sup_protos); ind++) + if (sup_protos[ind].ind == type) + break; + if (ind == ARRAY_SIZE(sup_protos)) + return URLUNKNOWN; + u->port = sup_protos[ind].port; + } /* Some delimiter troubles... */ if (url[i] == '/' && url[i - 1] != ':') ++i; if (type == URLHTTP) while (url[i] && url[i] == '/') ++i; - u->path = (char *) kmalloc(strlen(url + i) + 8); + u->path = (char *)kmalloc(strlen(url + i) + 8); strcpy(u->path, url + i); if (type == URLFTP) - { - u->ftp_type = process_ftp_type(u->path); - /* #### We don't handle type `d' correctly yet. */ - if (!u->ftp_type || toupper(u->ftp_type) == 'D') - u->ftp_type = 'I'; - } + { + u->ftp_type = process_ftp_type(u->path); + /* #### We don't handle type `d' correctly yet. */ + if (!u->ftp_type || toupper(u->ftp_type) == 'D') + u->ftp_type = 'I'; + } /* Parse the username and password (if existing). */ @@ -836,20 +856,20 @@ uerr_t proz_parse_url(const char *url, urlinfo * u, int strict) /****************************************************************************** - This function constructs and returns a malloced copy of the relative link - from two pieces of information: local name of the referring file (s1) and - local name of the referred file (s2). + This function constructs and returns a malloced copy of the relative link + from two pieces of information: local name of the referring file (s1) and + local name of the referred file (s2). - So, if s1 is "jagor.srce.hr/index.html" and s2 is - "jagor.srce.hr/images/news.gif", new name should be "images/news.gif". + So, if s1 is "jagor.srce.hr/index.html" and s2 is + "jagor.srce.hr/images/news.gif", new name should be "images/news.gif". - Alternately, if the s1 is "fly.cc.fer.hr/ioccc/index.html", and s2 is - "fly.cc.fer.hr/images/fly.gif", new name should be "../images/fly.gif". + Alternately, if the s1 is "fly.cc.fer.hr/ioccc/index.html", and s2 is + "fly.cc.fer.hr/images/fly.gif", new name should be "../images/fly.gif". - Caveats: s1 should not begin with '/', unless s2 begins with '/' too. - s1 should not contain things like ".." and such -- - construct_relative("fly/ioccc/../index.html", "fly/images/fly.gif") - will fail. (workaround is to call path_simplify on s1). + Caveats: s1 should not begin with '/', unless s2 begins with '/' too. + s1 should not contain things like ".." and such -- + construct_relative("fly/ioccc/../index.html", "fly/images/fly.gif") + will fail. (workaround is to call path_simplify on s1). ******************************************************************************/ char *construct_relative(const char *s1, const char *s2) { @@ -865,15 +885,16 @@ char *construct_relative(const char *s1, const char *s2) /* Skip the directories common to both strings. */ while (1) - { - for (; - s1[i] && s2[i] && s1[i] == s2[i] && s1[i] != '/' - && s2[i] != '/'; i++); - if (s1[i] == '/' && s2[i] == '/') - cnt = ++i; - else - break; - } + { + for (; + s1[i] && s2[i] && s1[i] == s2[i] && s1[i] != '/' + && s2[i] != '/'; i++) + ; + if (s1[i] == '/' && s2[i] == '/') + cnt = ++i; + else + break; + } for (sepdirs1 = 0; s1[i]; i++) if (s1[i] == '/') ++sepdirs1; @@ -890,7 +911,7 @@ char *construct_relative(const char *s1, const char *s2) } /****************************************************************************** - Add a URL to the list. + Add a URL to the list. ******************************************************************************/ urlpos *add_url(urlpos * l, const char *url, const char *file) { @@ -913,8 +934,6 @@ urlpos *add_url(urlpos * l, const char *url, const char *file) /*This will copy a url structure to another */ void url_cpy(urlinfo * src, urlinfo * dest) { - - } @@ -941,131 +960,135 @@ const char *find_last_char(const char *b, const char *e, char c) The parameters LINKLENGTH is useful if LINK is not zero-terminated. See uri_merge for a gentler interface to this functionality. - #### This function should handle `./' and `../' so that the evil + #### This function should handle `./' and `../' so that the evil path_simplify can go. */ char *uri_merge_1(const char *base, const char *link, int linklength, - int no_proto) + int no_proto) { char *constr; if (no_proto) - { - const char *end = base + urlpath_length(base); - - if (*link != '/') { - /* LINK is a relative URL: we need to replace everything - after last slash (possibly empty) with LINK. + const char *end = base + urlpath_length(base); - So, if BASE is "whatever/foo/bar", and LINK is "qux/xyzzy", - our result should be "whatever/foo/qux/xyzzy". */ - int need_explicit_slash = 0; - int span; - const char *start_insert; - const char *last_slash = find_last_char(base, end, '/'); - if (!last_slash) - { - /* No slash found at all. Append LINK to what we have, - but we'll need a slash as a separator. + if (*link != '/') + { + /* LINK is a relative URL: we need to replace everything + after last slash (possibly empty) with LINK. - Example: if base == "foo" and link == "qux/xyzzy", then - we cannot just append link to base, because we'd get - "fooqux/xyzzy", whereas what we want is - "foo/qux/xyzzy". + So, if BASE is "whatever/foo/bar", and LINK is "qux/xyzzy", + our result should be "whatever/foo/qux/xyzzy". */ + int need_explicit_slash = 0; + int span; + const char *start_insert; + const char *last_slash = find_last_char(base, end, '/'); + if (!last_slash) + { + /* No slash found at all. Append LINK to what we have, + but we'll need a slash as a separator. - To make sure the / gets inserted, we set - need_explicit_slash to 1. We also set start_insert - to end + 1, so that the length calculations work out - correctly for one more (slash) character. Accessing - that character is fine, since it will be the - delimiter, '\0' or '?'. */ - /* example: "foo?..." */ - /* ^ ('?' gets changed to '/') */ - start_insert = end + 1; - need_explicit_slash = 1; - } else if (last_slash && last_slash != base - && *(last_slash - 1) == '/') - { - /* example: http://host" */ - /* ^ */ - start_insert = end + 1; - need_explicit_slash = 1; - } else - { - /* example: "whatever/foo/bar" */ - /* ^ */ - start_insert = last_slash + 1; - } + Example: if base == "foo" and link == "qux/xyzzy", then + we cannot just append link to base, because we'd get + "fooqux/xyzzy", whereas what we want is + "foo/qux/xyzzy". - span = start_insert - base; - constr = (char *) kmalloc(span + linklength + 1); - if (span) - memcpy(constr, base, span); - if (need_explicit_slash) - constr[span - 1] = '/'; - if (linklength) - memcpy(constr + span, link, linklength); - constr[span + linklength] = '\0'; - } else /* *link == `/' */ - { - /* LINK is an absolute path: we need to replace everything - after (and including) the FIRST slash with LINK. + To make sure the / gets inserted, we set + need_explicit_slash to 1. We also set start_insert + to end + 1, so that the length calculations work out + correctly for one more (slash) character. Accessing + that character is fine, since it will be the + delimiter, '\0' or '?'. */ + /* example: "foo?..." */ + /* ^ ('?' gets changed to '/') */ + start_insert = end + 1; + need_explicit_slash = 1; + } + else if (last_slash && last_slash != base + && *(last_slash - 1) == '/') + { + /* example: http://host" */ + /* ^ */ + start_insert = end + 1; + need_explicit_slash = 1; + } + else + { + /* example: "whatever/foo/bar" */ + /* ^ */ + start_insert = last_slash + 1; + } - So, if BASE is "http://host/whatever/foo/bar", and LINK is - "/qux/xyzzy", our result should be - "http://host/qux/xyzzy". */ - int span; - const char *slash; - const char *start_insert = NULL; /* for gcc to shut up. */ - const char *pos = base; - int seen_slash_slash = 0; - /* We're looking for the first slash, but want to ignore - double slash. */ - again: - slash = memchr(pos, '/', end - pos); - if (slash && !seen_slash_slash) - if (*(slash + 1) == '/') - { - pos = slash + 2; - seen_slash_slash = 1; - goto again; - } + span = start_insert - base; + constr = (char *)kmalloc(span + linklength + 1); + if (span) + memcpy(constr, base, span); + if (need_explicit_slash) + constr[span - 1] = '/'; + if (linklength) + memcpy(constr + span, link, linklength); + constr[span + linklength] = '\0'; + } + else /* *link == `/' */ + { + /* LINK is an absolute path: we need to replace everything + after (and including) the FIRST slash with LINK. - /* At this point, SLASH is the location of the first / after - "//", or the first slash altogether. START_INSERT is the - pointer to the location where LINK will be inserted. When - examining the last two examples, keep in mind that LINK - begins with '/'. */ + So, if BASE is "http://host/whatever/foo/bar", and LINK is + "/qux/xyzzy", our result should be + "http://host/qux/xyzzy". */ + int span; + const char *slash; + const char *start_insert = NULL; /* for gcc to shut up. */ + const char *pos = base; + int seen_slash_slash = 0; + /* We're looking for the first slash, but want to ignore + double slash. */ +again: + slash = memchr(pos, '/', end - pos); + if (slash && !seen_slash_slash) + if (*(slash + 1) == '/') + { + pos = slash + 2; + seen_slash_slash = 1; + goto again; + } - if (!slash && !seen_slash_slash) - /* example: "foo" */ - /* ^ */ - start_insert = base; - else if (!slash && seen_slash_slash) - /* example: "http://foo" */ - /* ^ */ - start_insert = end; - else if (slash && !seen_slash_slash) - /* example: "foo/bar" */ - /* ^ */ - start_insert = base; - else if (slash && seen_slash_slash) - /* example: "http://something/" */ - /* ^ */ - start_insert = slash; + /* At this point, SLASH is the location of the first / after + "//", or the first slash altogether. START_INSERT is the + pointer to the location where LINK will be inserted. When + examining the last two examples, keep in mind that LINK + begins with '/'. */ - span = start_insert - base; - constr = (char *) kmalloc(span + linklength + 1); - if (span) - memcpy(constr, base, span); - if (linklength) - memcpy(constr + span, link, linklength); - constr[span + linklength] = '\0'; + if (!slash && !seen_slash_slash) + /* example: "foo" */ + /* ^ */ + start_insert = base; + else if (!slash && seen_slash_slash) + /* example: "http://foo" */ + /* ^ */ + start_insert = end; + else if (slash && !seen_slash_slash) + /* example: "foo/bar" */ + /* ^ */ + start_insert = base; + else if (slash && seen_slash_slash) + /* example: "http://something/" */ + /* ^ */ + start_insert = slash; + + span = start_insert - base; + constr = (char *)kmalloc(span + linklength + 1); + if (span) + memcpy(constr, base, span); + if (linklength) + memcpy(constr + span, link, linklength); + constr[span + linklength] = '\0'; + } + } + else /* !no_proto */ + { + constr = strdupdelim(link, link + linklength); } - } else /* !no_proto */ - { - constr = strdupdelim(link, link + linklength); - } return constr; } @@ -1078,9 +1101,9 @@ char *uri_merge(const char *base, const char *link) } /****************************************************************************** - Perform a "deep" free of the urlinfo structure. The structure should have - been created with newurl, but need not have been used. If free_pointer - is non-0, free the pointer itself. + Perform a "deep" free of the urlinfo structure. The structure should have + been created with newurl, but need not have been used. If free_pointer + is non-0, free the pointer itself. ******************************************************************************/ void proz_free_url(urlinfo * u, boolean complete) { @@ -1112,7 +1135,7 @@ urlinfo *proz_copy_url(urlinfo * u) { urlinfo *dest_url; - dest_url = (urlinfo *) kmalloc(sizeof(urlinfo)); + dest_url = (urlinfo *)kmalloc(sizeof(urlinfo)); memset(dest_url, 0, sizeof(urlinfo)); if (u->url) diff --git a/libprozilla/src/url.h b/libprozilla/src/url.h index 327323c..050a5d2 100644 --- a/libprozilla/src/url.h +++ b/libprozilla/src/url.h @@ -1,21 +1,21 @@ /* Declarations for URL handling. Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc. - + This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. - + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - + You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -/* $Id: url.h,v 1.20 2001/08/17 21:53:39 kalum Exp $ */ +/* $Id$ */ #ifndef URL_H @@ -31,44 +31,44 @@ extern "C" { #endif /* Structure containing info on a protocol. */ - typedef struct proto { - char *name; - uerr_t ind; - unsigned short port; - } proto_t; +typedef struct proto { + char *name; + uerr_t ind; + unsigned short port; +} proto_t; - enum uflags { - URELATIVE = 0x0001, /* Is URL relative? */ - UNOPROTO = 0x0002, /* Is URL without a protocol? */ - UABS2REL = 0x0004, /* Convert absolute to relative? */ - UREL2ABS = 0x0008 /* Convert relative to absolute? */ - }; +enum uflags { + URELATIVE = 0x0001, /* Is URL relative? */ + UNOPROTO = 0x0002, /* Is URL without a protocol? */ + UABS2REL = 0x0004, /* Convert absolute to relative? */ + UREL2ABS = 0x0008 /* Convert relative to absolute? */ +}; /* A structure that defines the whereabouts of a URL, i.e. its position in an HTML document, etc. */ - typedef struct _urlpos { - char *url; /* URL */ - char *local_name; /* Local file to which it was saved. */ - enum uflags flags; /* Various flags. */ - int pos, size; /* Rekative position in the buffer. */ - struct _urlpos *next; /* Next struct in list. */ - } urlpos; +typedef struct _urlpos { + char *url; /* URL */ + char *local_name; /* Local file to which it was saved. */ + enum uflags flags; /* Various flags. */ + int pos, size; /* Rekative position in the buffer. */ + struct _urlpos *next; /* Next struct in list. */ +} urlpos; - int - has_proto(const char *url); - int - skip_uname(const char *url); - void - parse_dir(const char *path, char **dir, char **file); - void - path_simplify(char *path); - char *uri_merge(const char *base, const char *link); - int - urlpath_length(const char *url); - int - skip_proto(const char *url); - char *str_url(const urlinfo * u, int hide); +int +has_proto(const char *url); +int +skip_uname(const char *url); +void +parse_dir(const char *path, char **dir, char **file); +void +path_simplify(char *path); +char *uri_merge(const char *base, const char *link); +int +urlpath_length(const char *url); +int +skip_proto(const char *url); +char *str_url(const urlinfo * u, int hide); #ifdef __cplusplus } #endif -#endif /* URL_H */ +#endif /* URL_H */ diff --git a/src/download_win.cpp b/src/download_win.cpp index 92022ab..bbcc99c 100644 --- a/src/download_win.cpp +++ b/src/download_win.cpp @@ -1,20 +1,20 @@ /****************************************************************************** - fltk prozilla - a front end for prozilla, a download accelerator library - Copyright (C) 2001 Kalum Somaratna - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +* fltk prozilla - a front end for prozilla, a download accelerator library +* Copyright (C) 2001 Kalum Somaratna +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ******************************************************************************/ #if HAVE_CONFIG_H @@ -29,459 +29,472 @@ #include #else #include -#endif +#endif #include "main.h" #include "download_win.h" #include "interface.h" void -DL_Window::cleanup (boolean erase_dlparts) +DL_Window::cleanup(boolean erase_dlparts) { + /*handle cleanup */ + if (status == DL_DOWNLOADING) + { + proz_download_stop_downloads(download); + if (erase_dlparts == TRUE) + { + proz_download_delete_target(download); + proz_log_delete_logfile(download); + } + } + else if (status == DL_GETTING_INFO) + { + /*terminate info thread */ + pthread_cancel(info_thread); + pthread_join(info_thread, NULL); + } + else if (status == DL_JOINING) + { + /*terminate joining thread */ + proz_download_cancel_joining_thread(download); + pthread_join(download->join_thread, NULL); + } + else if (status == DL_FTPSEARCHING) + { + ftpsearch_win->cleanup(); + } - /*handle cleanup */ - if (status == DL_DOWNLOADING) - { - proz_download_stop_downloads (download); - if (erase_dlparts == TRUE) - { - proz_download_delete_target (download); - proz_log_delete_logfile (download); - } - } - else if (status == DL_GETTING_INFO) - { - /*terminate info thread */ - pthread_cancel (info_thread); - pthread_join (info_thread, NULL); - } - else if (status == DL_JOINING) - { - /*terminate joining thread */ - proz_download_cancel_joining_thread (download); - pthread_join (download->join_thread, NULL); - } - else if (status == DL_FTPSEARCHING) - { - ftpsearch_win->cleanup (); - } - - status = DL_ABORTED; + status = DL_ABORTED; } -DL_Window::~DL_Window () -{ - proz_connection_free_connection (connection, true); - delete (ftpsearch_win); +DL_Window::~DL_Window() +{ + proz_connection_free_connection(connection, true); + delete (ftpsearch_win); } -DL_Window::DL_Window (urlinfo * url_data) + +DL_Window::DL_Window(urlinfo *url_data) { // key = 0; - got_info = FALSE; - got_dl = FALSE; - status = DL_IDLING; + got_info = FALSE; + got_dl = FALSE; + status = DL_IDLING; - memcpy (&u, url_data, sizeof (u)); - memset (&update_time, 0, sizeof (struct timeval)); - num_connections = rt.num_connections; - - ftpsearch_win = new FTPS_Window (); - do_ftpsearch = FALSE; - using_ftpsearch = FALSE; + memcpy(&u, url_data, sizeof(u)); + memset(&update_time, 0, sizeof(struct timeval)); + num_connections = rt.num_connections; + ftpsearch_win = new FTPS_Window(); + do_ftpsearch = FALSE; + using_ftpsearch = FALSE; } + void -DL_Window::my_cb () +DL_Window::my_cb() { - if (status == DL_GETTING_INFO) - { - handle_info_thread (); - return; - } + if (status == DL_GETTING_INFO) + { + handle_info_thread(); + return; + } - // if ((got_info == TRUE && status == DL_IDLING && got_dl == FALSE) - if ((status == DL_RESTARTING && got_info == TRUE) - || status == DL_DLPRESTART) - { - do_download (); - } + // if ((got_info == TRUE && status == DL_IDLING && got_dl == FALSE) + if (((status == DL_RESTARTING) && (got_info == TRUE)) || + (status == DL_DLPRESTART)) + { + do_download(); + } - if (status == DL_DOWNLOADING) - { - handle_download_thread (); + if (status == DL_DOWNLOADING) + { + handle_download_thread(); - return; - } + return; + } - if (status == DL_JOINING) - { - handle_joining_thread (); - return; - } + if (status == DL_JOINING) + { + handle_joining_thread(); + return; + } - if (status == DL_FATALERR) - { - handle_dl_fatal_error (); - return; - } - - if (status == DL_PAUSED) - { - //TODO what to do when paused - return; - } - if (status == DL_FTPSEARCHING) - { - handle_ftpsearch (); - return; - } + if (status == DL_FATALERR) + { + handle_dl_fatal_error(); + return; + } + if (status == DL_PAUSED) + { + //TODO what to do when paused + return; + } + if (status == DL_FTPSEARCHING) + { + handle_ftpsearch(); + return; + } } - void -DL_Window::dl_start (int num_connections, boolean ftpsearch) +DL_Window::dl_start(int num_connections, boolean ftpsearch) { + do_ftpsearch = ftpsearch; - do_ftpsearch = ftpsearch; + connection = proz_connection_init(&u, &getinfo_mutex); + proz_connection_set_msg_proc(connection, ms, this); - connection = proz_connection_init (&u, &getinfo_mutex); - proz_connection_set_msg_proc (connection, ms, this); + PrintMessage("Creating the thread that gets info about file..\n"); + proz_get_url_info_loop(connection, &info_thread); - PrintMessage("Creating the thread that gets info about file..\n"); - proz_get_url_info_loop (connection, &info_thread); - - status = DL_GETTING_INFO; + status = DL_GETTING_INFO; } void -DL_Window::do_download () +DL_Window::do_download() { logfile lf; + status = DL_DLPRESTART; - //setup the download - download = proz_download_init (&connection->u); + //setup the download + download = proz_download_init(&connection->u); proz_debug("proz_download_init complete"); - handle_prev_download (); - if(status==DL_FATALERR) + handle_prev_download(); + if (status == DL_FATALERR) { return; } - if(download->resume_mode ==TRUE) + if (download->resume_mode == TRUE) { if (proz_log_read_logfile(&lf, download, FALSE) != 1) - { - PrintMessage - ("A error occured while processing the logfile! Assuming default number of connections"); - } + { + PrintMessage + ("A error occured while processing the logfile! Assuming default number of connections"); + } // validate the info returned //TODO check whether files size is the same and if not prompt the user what to do. if (lf.num_connections != num_connections) - { - PrintMessage - ("The previous download used a different number of connections than the default! so I will use the previous number of threads"); - num_connections = lf.num_connections; - assert(num_connections > 0); - // cleanup(FALSE); - } + { + PrintMessage + ("The previous download used a different number of connections than the default! so I will use the previous number of threads"); + num_connections = lf.num_connections; + assert(num_connections > 0); + // cleanup(FALSE); + } + } + if (rt.display_mode == DISP_CURSES) + { + erase(); } - if (rt.display_mode == DISP_CURSES) - erase(); - start_download (); + start_download(); } + void -DL_Window::handle_prev_download () +DL_Window::handle_prev_download() { int ret = 0; + /* we will check and see if 1. previous partial download exists and - 2. if the file to be downloaded already exists in the local - directory - */ + * 2. if the file to be downloaded already exists in the local + * directory + */ //Check and see if the file is already downloaded.. //check the --no-getch flag if (rt.dont_prompt != TRUE) { - int target_exists=proz_download_target_exist(download); - if(target_exists) - { - ret=askUserOverwrite(connection); - if(ret=='O') - { - //just continue, it will anyway be overwritten by the download routines. - } - if(ret=='A') - { - status = DL_FATALERR; - return; - } - - } + int target_exists = proz_download_target_exist(download); + if (target_exists) + { + ret = askUserOverwrite(connection); + if (ret == 'O') + { + //just continue, it will anyway be overwritten by the download routines. + } + if (ret == 'A') + { + status = DL_FATALERR; + return; + } + } } /*Check for a prior download */ - int previous_dl = proz_download_prev_download_exists (download); + int previous_dl = proz_download_prev_download_exists(download); if (previous_dl == 1) { - download->resume_mode = TRUE; //connection supports resume if (connection->resume_support) - { - //check the --no-getch flag - if (rt.dont_prompt == TRUE) - { - //see if the user had a preference, resume or overwrite - if (rt.resume_mode == TRUE) - download->resume_mode = TRUE; - else - if (rt.force_mode == TRUE) - download->resume_mode = FALSE; - } - else - { - //ask the user (curses or terminal) - ret = askUserResume(connection, true); - if (ret == 'R') - download->resume_mode = TRUE; - else - download->resume_mode = FALSE; - } - } + { + //check the --no-getch flag + if (rt.dont_prompt == TRUE) + { + //see if the user had a preference, resume or overwrite + if (rt.resume_mode == TRUE) + { + download->resume_mode = TRUE; + } + else + if (rt.force_mode == TRUE) + { + download->resume_mode = FALSE; + } + } + else + { + //ask the user (curses or terminal) + ret = askUserResume(connection, true); + if (ret == 'R') + { + download->resume_mode = TRUE; + } + else + { + download->resume_mode = FALSE; + } + } + } else - {//resume NOT supported - // --no-getch and no force-mode means fatal error!!! - if (rt.dont_prompt == TRUE && rt.force_mode == FALSE) - { - status = DL_FATALERR; - handle_dl_fatal_error (); - return; - } - - //force overwrite - if (rt.dont_prompt == TRUE && rt.force_mode == TRUE) - download->resume_mode = FALSE; - else - { - //Ask the user - ret = askUserResume(connection, false); - if (ret == 'O') - download->resume_mode = FALSE; - else - { //Abort - status = DL_FATALERR; - handle_dl_fatal_error (); - return; - } - } - } + { //resume NOT supported + // --no-getch and no force-mode means fatal error!!! + if ((rt.dont_prompt == TRUE) && (rt.force_mode == FALSE)) + { + status = DL_FATALERR; + handle_dl_fatal_error(); + return; + } + + //force overwrite + if ((rt.dont_prompt == TRUE) && (rt.force_mode == TRUE)) + { + download->resume_mode = FALSE; + } + else + { + //Ask the user + ret = askUserResume(connection, false); + if (ret == 'O') + { + download->resume_mode = FALSE; + } + else + { //Abort + status = DL_FATALERR; + handle_dl_fatal_error(); + return; + } + } + } } - } - - - -void -DL_Window::start_download () -{ - int ret = 0; - - proz_debug("start_download"); - if (using_ftpsearch != TRUE) - ret = num_connections = - proz_download_setup_connections_no_ftpsearch - (download, connection, num_connections); - else - ret = proz_download_setup_connections_ftpsearch (download, - connection, - ftpsearch_win-> - request, - num_connections); - - if (ret == -1) - { - PrintMessage("Write Error: There may not be enough free space or a disk write failed when attempting to create output file\n"); - status = DL_ABORTED; - return; - } - - /*Display resume status */ - if (download->resume_support) - { - PrintMessage("RESUME supported\n\n"); - } - else - { - PrintMessage("RESUME NOT supported\n"); - } - - gettimeofday (&update_time, NULL); - - proz_download_start_downloads (download, download->resume_mode); - status = DL_DOWNLOADING; - return; - } + void -DL_Window::handle_info_thread () +DL_Window::start_download() { - bool getting_info = proz_connection_running (connection); + int ret = 0; + + proz_debug("start_download"); + if (using_ftpsearch != TRUE) + { + ret = num_connections = + proz_download_setup_connections_no_ftpsearch + (download, connection, num_connections); + } + else + { + ret = proz_download_setup_connections_ftpsearch(download, + connection, + ftpsearch_win-> + request, + num_connections); + } + + if (ret == -1) + { + PrintMessage("Write Error: There may not be enough free space or a disk write failed when attempting to create output file\n"); + status = DL_ABORTED; + return; + } + + /*Display resume status */ + if (download->resume_support) + { + PrintMessage("RESUME supported\n\n"); + } + else + { + PrintMessage("RESUME NOT supported\n"); + } + + gettimeofday(&update_time, NULL); + + proz_download_start_downloads(download, download->resume_mode); + status = DL_DOWNLOADING; +} + + +void +DL_Window::handle_info_thread() +{ + bool getting_info = proz_connection_running(connection); if (getting_info == FALSE) { - pthread_join (info_thread, NULL); + pthread_join(info_thread, NULL); - if (connection->err == HOK || connection->err == FTPOK) - { - got_info = TRUE; + if ((connection->err == HOK) || (connection->err == FTPOK)) + { + got_info = TRUE; - if (connection->main_file_size != -1) - { - PrintMessage("File Size = %lld Kb\n\n", - connection->main_file_size / 1024); - } - else - PrintMessage("File Size is UNKOWN\n\n"); + if (connection->main_file_size != -1) + { + PrintMessage("File Size = %lld Kb\n\n", + connection->main_file_size / 1024); + } + else + { + PrintMessage("File Size is UNKOWN\n\n"); + } - //Added ftpsearch only is size > min size - if ((connection->main_file_size != -1 - && do_ftpsearch == TRUE) && (connection->main_file_size / 1024 >= rt.min_search_size)) - { - status = DL_FTPSEARCHING; + //Added ftpsearch only is size > min size + if (((connection->main_file_size != -1) && + (do_ftpsearch == TRUE)) && (connection->main_file_size / 1024 >= rt.min_search_size)) + { + status = DL_FTPSEARCHING; - if (rt.ftpsearch_server_id == 0) - { - ftpsearch_win-> - fetch_mirror_info - (&connection->u, - connection->main_file_size, - "http://www.filesearching.com/cgi-bin/s", - FILESEARCH_RU, - rt.ftps_mirror_req_n); - } - else if (rt.ftpsearch_server_id == 1) - { - ftpsearch_win-> - fetch_mirror_info - (&connection->u, - connection->main_file_size, - "http://ftpsearch.elmundo.es:8000/ftpsearch", - LYCOS, rt.ftps_mirror_req_n); - } + if (rt.ftpsearch_server_id == 0) + { + ftpsearch_win-> + fetch_mirror_info + (&connection->u, + connection->main_file_size, + "http://www.filesearching.com/cgi-bin/s", + FILESEARCH_RU, + rt.ftps_mirror_req_n); + } + else if (rt.ftpsearch_server_id == 1) + { + ftpsearch_win-> + fetch_mirror_info + (&connection->u, + connection->main_file_size, + "http://ftpsearch.elmundo.es:8000/ftpsearch", + LYCOS, rt.ftps_mirror_req_n); + } + } + else + { + if ((connection->main_file_size / 1024 >= rt.min_search_size) && (do_ftpsearch == TRUE)) + { + PrintMessage("File size is less than the minimum, skipping ftpsearch"); + } - } - else - { - if (connection->main_file_size / 1024 >= rt.min_search_size && (do_ftpsearch == TRUE)) - PrintMessage("File size is less than the minimum, skipping ftpsearch"); - - do_download (); - } - - } + do_download(); + } + } else - { - - if (connection->err == FTPNSFOD - || connection->err == HTTPNSFOD) - { - PrintMessage("The URL %s doesnt exist!\n", - connection->u.url); - got_dl = FALSE; - got_info = FALSE; - status = DL_FATALERR; - } - else - { - PrintMessage("An error occurred: %s \n", - proz_strerror(connection->err)); - got_dl = FALSE; - got_info = FALSE; - status = DL_FATALERR; - } - - } + { + if ((connection->err == FTPNSFOD) || + (connection->err == HTTPNSFOD)) + { + PrintMessage("The URL %s doesnt exist!\n", + connection->u.url); + got_dl = FALSE; + got_info = FALSE; + status = DL_FATALERR; + } + else + { + PrintMessage("An error occurred: %s \n", + proz_strerror(connection->err)); + got_dl = FALSE; + got_info = FALSE; + status = DL_FATALERR; + } + } } } void -DL_Window::handle_ftpsearch () +DL_Window::handle_ftpsearch() { + uerr_t err; - uerr_t err; - err = ftpsearch_win->callback (); - if (err == MASSPINGDONE) - { - if (ftpsearch_win->request->num_mirrors == 0) - { - using_ftpsearch = FALSE; - PrintMessage("No suitable mirrors were found, downloading from original server\n"); - do_download (); - return; - } - using_ftpsearch = TRUE; - do_download (); - return; - } - if (err == FTPSFAIL) - { - using_ftpsearch = FALSE; - do_download (); - return; - } + err = ftpsearch_win->callback(); + if (err == MASSPINGDONE) + { + if (ftpsearch_win->request->num_mirrors == 0) + { + using_ftpsearch = FALSE; + PrintMessage("No suitable mirrors were found, downloading from original server\n"); + do_download(); + return; + } + using_ftpsearch = TRUE; + do_download(); + return; + } + if (err == FTPSFAIL) + { + using_ftpsearch = FALSE; + do_download(); + return; + } // if(ftpsearch_win->exit_ftpsearch_button_pressed==TRUE) - if (using_ftpsearch == TRUE) - { - if (ftpsearch_win->got_mirror_info == TRUE) - { - using_ftpsearch = TRUE; - } - else - { - using_ftpsearch = FALSE; - } - do_download (); - } + if (using_ftpsearch == TRUE) + { + if (ftpsearch_win->got_mirror_info == TRUE) + { + using_ftpsearch = TRUE; + } + else + { + using_ftpsearch = FALSE; + } + do_download(); + } } -void -DL_Window::handle_download_thread () -{ +void +DL_Window::handle_download_thread() +{ uerr_t err; struct timeval cur_time; struct timeval diff_time; - err = proz_download_handle_threads (download); + err = proz_download_handle_threads(download); - gettimeofday (&cur_time, NULL); + gettimeofday(&cur_time, NULL); - proz_timeval_subtract (&diff_time, &cur_time, &update_time); + proz_timeval_subtract(&diff_time, &cur_time, &update_time); - if ((((diff_time.tv_sec * 1000) + (diff_time.tv_usec / 1000)) > 200) - || err == DLDONE) + if ((((diff_time.tv_sec * 1000) + (diff_time.tv_usec / 1000)) > 200) || + (err == DLDONE)) { - - print_status (download, rt.quiet_mode); - + print_status(download, rt.quiet_mode); + if (download->main_file_size != -1) - { - } + { + } /*The time of the current screen */ - gettimeofday (&update_time, NULL); + gettimeofday(&update_time, NULL); } if (err == DLDONE) @@ -489,10 +502,10 @@ DL_Window::handle_download_thread () PrintMessage("Got DL succesfully, now renaming file\n"); got_dl = TRUE; - PrintMessage ("Renaming file %s .....\n", - download->u.file); + PrintMessage("Renaming file %s .....\n", + download->u.file); status = DL_JOINING; - proz_download_join_downloads (download); + proz_download_join_downloads(download); joining_thread_running = TRUE; } @@ -507,9 +520,8 @@ DL_Window::handle_download_thread () if (err == DLLOCALFATAL) { - - PrintMessage ("One connection of the download %s encountered a unrecoverable local error, usually lack of free space, or a write to bad medium, or a problem with permissions,so please fix this and retry\n", - connection->u.url); + PrintMessage("One connection of the download %s encountered a unrecoverable local error, usually lack of free space, or a write to bad medium, or a problem with permissions,so please fix this and retry\n", + connection->u.url); got_dl = FALSE; status = DL_FATALERR; } @@ -517,210 +529,224 @@ DL_Window::handle_download_thread () if (err == DLREMOTEFATAL) { - - PrintMessage (_ - ("A connection(s) of the download %s encountered a unrecoverable remote error, usually the file not being present in the remote server, therefore the download had to be aborted!\n"), - connection->u.url); + PrintMessage(_ + ("A connection(s) of the download %s encountered a unrecoverable remote error, usually the file not being present in the remote server, therefore the download had to be aborted!\n"), + connection->u.url); got_dl = FALSE; status = DL_FATALERR; } - } void -DL_Window::handle_joining_thread () +DL_Window::handle_joining_thread() { + boolean bDone = false; - boolean bDone = false; + uerr_t building_status = proz_download_get_join_status(download); - uerr_t building_status = proz_download_get_join_status (download); + if (building_status == JOINERR) + { + if (joining_thread_running == TRUE) + { + proz_download_wait_till_end_joining_thread(download); + joining_thread_running = FALSE; + } + } - if (building_status == JOINERR) - { - if (joining_thread_running == TRUE) - { - proz_download_wait_till_end_joining_thread (download); - joining_thread_running = FALSE; - } + if (building_status == JOINDONE) + { + if (joining_thread_running == TRUE) + { + proz_download_wait_till_end_joining_thread(download); + joining_thread_running = FALSE; + bDone = true; + } - } - - if (building_status == JOINDONE) - { - - if (joining_thread_running == TRUE) - { - proz_download_wait_till_end_joining_thread (download); - joining_thread_running = FALSE; - bDone = true; - } - - /*has the user pressed OK at the end of the download */ - if (bDone == true) - { - PrintMessage("All Done.\n"); - //curses_query_user_input("Press any key to exit."); - proz_download_delete_dl_file (download); - proz_download_free_download (download, 0); - status = DL_IDLING; - } - status = DL_DONE; - } + /*has the user pressed OK at the end of the download */ + if (bDone == true) + { + PrintMessage("All Done.\n"); + //curses_query_user_input("Press any key to exit."); + proz_download_delete_dl_file(download); + proz_download_free_download(download, 0); + status = DL_IDLING; + } + status = DL_DONE; + } } void -DL_Window::handle_dl_fatal_error () +DL_Window::handle_dl_fatal_error() { - - status = DL_FATALERR; - cleanup (FALSE); - return; + status = DL_FATALERR; + cleanup(FALSE); } -void -DL_Window::print_status (download_t * download, int quiet_mode) -{ +void +DL_Window::print_status(download_t *download, int quiet_mode) +{ if (rt.display_mode == DISP_CURSES) - DisplayCursesInfo(download); + { + DisplayCursesInfo(download); + } else { - if (quiet_mode == FALSE) - { - for (int i = 0; i < download->num_connections; i++) - { + { + for (int i = 0; i < download->num_connections; i++) + { + fprintf(stdout, + "%2.2d %-30.30s %15.15s %10zd\n", + i + 1, download->pconnections[i]->u.host, + proz_connection_get_status_string(download-> + pconnections + [i]), + proz_connection_get_total_bytes_got + (download->pconnections[i])); + } - fprintf (stdout, - "%2.2d %-30.30s %15.15s %10zd\n", - i + 1, download->pconnections[i]->u.host, - proz_connection_get_status_string (download-> - pconnections - [i]), - proz_connection_get_total_bytes_got - (download->pconnections[i])); - } - - fprintf (stdout, "Total Bytes received %zd Kb\n", - proz_download_get_total_bytes_got (download) / 1024); + fprintf(stdout, "Total Bytes received %zd Kb\n", + proz_download_get_total_bytes_got(download) / 1024); - fprintf (stdout, "Average Speed = %.3f Kb/sec\n", - proz_download_get_average_speed (download) / 1024); - } - + fprintf(stdout, "Average Speed = %.3f Kb/sec\n", + proz_download_get_average_speed(download) / 1024); + } + int secs_left; char timeLeft[30]; if ((secs_left = - proz_download_get_est_time_left (download)) != -1) - { - if (secs_left < 60) - snprintf (timeLeft, sizeof(timeLeft), "00:%.2d", secs_left); - else if (secs_left < 3600) - snprintf (timeLeft, sizeof(timeLeft), "00:%.2d:%.2d", - secs_left / 60, secs_left % 60); - else - snprintf (timeLeft, sizeof(timeLeft), "%.2d:%.2d:00", - secs_left / 3600, - (secs_left % 3600) / 60); - } + proz_download_get_est_time_left(download)) != -1) + { + if (secs_left < 60) + { + snprintf(timeLeft, sizeof(timeLeft), "00:%.2d", secs_left); + } + else if (secs_left < 3600) + { + snprintf(timeLeft, sizeof(timeLeft), "00:%.2d:%.2d", + secs_left / 60, secs_left % 60); + } + else + { + snprintf(timeLeft, sizeof(timeLeft), "%.2d:%.2d:00", + secs_left / 3600, + (secs_left % 3600) / 60); + } + } else - sprintf (timeLeft, "??:??:??"); + { + sprintf(timeLeft, "??:??:??"); + } off_t totalDownloaded = 0; off_t totalFile = 0; long aveSpeed = 0; totalFile = download->main_file_size / 1024; totalDownloaded = - proz_download_get_total_bytes_got (download) / - 1024; - aveSpeed = (long)(proz_download_get_average_speed (download) / 1024); + proz_download_get_total_bytes_got(download) / + 1024; + aveSpeed = (long)(proz_download_get_average_speed(download) / 1024); //WGET looks like this: //xx% [=======> ] nnn,nnn,nnn XXXX.XXK/s ETA hh:mm:ss - fprintf (stdout, " %.2lf%% %zdKb/%zdkb %0.3fKb/s ETA %s \r", - ((float)totalDownloaded) / ((float)totalFile / 100), - totalDownloaded, totalFile, (float)aveSpeed, timeLeft); - fflush (stdout); + fprintf(stdout, " %.2lf%% %zdKb/%zdkb %0.3fKb/s ETA %s \r", + ((float)totalDownloaded) / ((float)totalFile / 100), + totalDownloaded, totalFile, (float)aveSpeed, timeLeft); + fflush(stdout); } } + int DL_Window::askUserResume(connection_t *connection, boolean resumeSupported) { - int ret = 0; - const char msg[] = "Previous download of %s exists, would you like to (R)esume it or (O)verwrite it?"; - const char msg2[] = "Previous download of %s exists, would you like to (A)bort it or (O)verwrite it?"; + int ret = 0; + const char msg[] = "Previous download of %s exists, would you like to (R)esume it or (O)verwrite it?"; + const char msg2[] = "Previous download of %s exists, would you like to (A)bort it or (O)verwrite it?"; - - do { + + do + { if (rt.display_mode == DISP_CURSES) - ret = curses_query_user_input((resumeSupported == TRUE)?msg:msg2,connection->u.file); + { + ret = curses_query_user_input((resumeSupported == TRUE) ? msg : msg2, connection->u.file); + } else - { - fprintf(stdout,"\n"); - fprintf(stdout,(resumeSupported == TRUE)?msg:msg2,connection->u.file); - ret = getc(stdin); - ret = islower(ret) ? toupper(ret) : ret; - } - - switch(ret) - { + { + fprintf(stdout, "\n"); + fprintf(stdout, (resumeSupported == TRUE) ? msg : msg2, connection->u.file); + ret = getc(stdin); + ret = islower(ret) ? toupper(ret) : ret; + } + + switch (ret) + { case 'O': break; + case 'A': if (resumeSupported == TRUE) - ret=0; + { + ret = 0; + } break; + case 'R': if (resumeSupported == FALSE) - ret=0; + { + ret = 0; + } break; - default: - ret=0; - break; - } + default: + ret = 0; + break; + } } while (ret == 0); - - return ret; - + + return(ret); } int DL_Window::askUserOverwrite(connection_t *connectionb) { - int ret = 0; - const char msg[] = "File %s already exists, would you like to (A)bort it or (O)verwrite it?"; + int ret = 0; + const char msg[] = "File %s already exists, would you like to (A)bort it or (O)verwrite it?"; - - do { + + do + { if (rt.display_mode == DISP_CURSES) - ret = curses_query_user_input(msg,connection->u.file); + { + ret = curses_query_user_input(msg, connection->u.file); + } else - { - fprintf(stdout,"\n"); - fprintf(stdout,msg,connection->u.file); - ret = getc(stdin); - ret = islower(ret) ? toupper(ret) : ret; - } - - switch(ret) - { + { + fprintf(stdout, "\n"); + fprintf(stdout, msg, connection->u.file); + ret = getc(stdin); + ret = islower(ret) ? toupper(ret) : ret; + } + + switch (ret) + { case 'O': break; + case 'A': break; - default: - ret=0; - break; - } + default: + ret = 0; + break; + } } while (ret == 0); - - return ret; + + return(ret); } diff --git a/src/download_win.h b/src/download_win.h index 650b613..0ca0d0a 100644 --- a/src/download_win.h +++ b/src/download_win.h @@ -23,7 +23,7 @@ typedef enum { } dlwin_status_t; -//The running dialog type +//The running dialog type typedef enum { DLG_GENERIC, DLG_URLNSFOD, @@ -35,48 +35,47 @@ typedef enum { } dlg_class; class DL_Window { - public: - DL_Window(urlinfo * url_data); - ~DL_Window(); +DL_Window(urlinfo * url_data); +~DL_Window(); - void dl_start(int num_connections, boolean ftpsearch); - void my_cb(); +void dl_start(int num_connections, boolean ftpsearch); +void my_cb(); - void handle_info_thread(); - void handle_ftpsearch(); - void handle_download_thread(); +void handle_info_thread(); +void handle_ftpsearch(); +void handle_download_thread(); - void start_download(); +void start_download(); - void handle_joining_thread(); - void handle_dl_fatal_error(); - void cleanup(boolean erase_dlparts); - void print_status(download_t * download, int quiet_mode); +void handle_joining_thread(); +void handle_dl_fatal_error(); +void cleanup(boolean erase_dlparts); +void print_status(download_t * download, int quiet_mode); - connection_t *connection; - download_t *download; - urlinfo u; - boolean got_info; - boolean got_dl; +connection_t *connection; +download_t *download; +urlinfo u; +boolean got_info; +boolean got_dl; - dlwin_status_t status; +dlwin_status_t status; - pthread_t info_thread; - pthread_mutex_t getinfo_mutex; - int num_connections; - /*The time elapsed since the last update */ - struct timeval update_time; +pthread_t info_thread; +pthread_mutex_t getinfo_mutex; +int num_connections; +/*The time elapsed since the last update */ +struct timeval update_time; private: - void do_download(); - void handle_prev_download(); - boolean joining_thread_running; - boolean do_ftpsearch; - boolean using_ftpsearch; - FTPS_Window *ftpsearch_win; - int askUserResume(connection_t *connection, boolean resumeSupported); - int askUserOverwrite(connection_t *connection); +void do_download(); +void handle_prev_download(); +boolean joining_thread_running; +boolean do_ftpsearch; +boolean using_ftpsearch; +FTPS_Window *ftpsearch_win; +int askUserResume(connection_t *connection, boolean resumeSupported); +int askUserOverwrite(connection_t *connection); }; #endif diff --git a/src/ftps_win.h b/src/ftps_win.h index 4e656f2..6e90d8e 100644 --- a/src/ftps_win.h +++ b/src/ftps_win.h @@ -3,9 +3,9 @@ #define ftps_win_h #include "prozilla.h" /* -class ftps_gui { -public: - ftps_gui(); -}; -*/ + class ftps_gui { + public: + ftps_gui(); + }; + */ #endif diff --git a/src/ftpsearch_win.cpp b/src/ftpsearch_win.cpp index eff648e..8269c0a 100644 --- a/src/ftpsearch_win.cpp +++ b/src/ftpsearch_win.cpp @@ -1,20 +1,20 @@ /****************************************************************************** - fltk prozilla - a front end for prozilla, a download accelerator library - Copyright (C) 2001 Kalum Somaratna + fltk prozilla - a front end for prozilla, a download accelerator library + Copyright (C) 2001 Kalum Somaratna - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ******************************************************************************/ #if HAVE_CONFIG_H @@ -32,30 +32,30 @@ FTPS_Window::FTPS_Window () { - memset (&request, 0, sizeof (request)); + memset(&request, 0, sizeof(request)); - request_running = FALSE; - ping_running = FALSE; - got_mirror_info = FALSE; + request_running = FALSE; + ping_running = FALSE; + got_mirror_info = FALSE; } void -FTPS_Window::fetch_mirror_info (urlinfo * u, off_t file_size, - char *ftps_loc, - ftpsearch_server_type_t server_type, - int num_req_mirrors) +FTPS_Window::fetch_mirror_info(urlinfo * u, off_t file_size, + char *ftps_loc, + ftpsearch_server_type_t server_type, + int num_req_mirrors) { - assert (u->file); + assert(u->file); - request = proz_ftps_request_init (u, file_size, ftps_loc, - server_type, num_req_mirrors); - PrintMessage("Attempting to get %d mirrors from %s\n\n",num_req_mirrors, ftps_loc); - //proz_connection_set_msg_proc (request->connection, - // ftps_win_message_proc, this); - proz_get_complete_mirror_list (request); + request = proz_ftps_request_init(u, file_size, ftps_loc, + server_type, num_req_mirrors); + PrintMessage("Attempting to get %d mirrors from %s\n\n", num_req_mirrors, ftps_loc); + //proz_connection_set_msg_proc (request->connection, + // ftps_win_message_proc, this); + proz_get_complete_mirror_list(request); - request_running = TRUE; + request_running = TRUE; } @@ -66,138 +66,136 @@ FTPS_Window::fetch_mirror_info (urlinfo * u, off_t file_size, //} uerr_t -FTPS_Window::callback () +FTPS_Window::callback() { + if (request_running == TRUE) + { + if (proz_request_info_running(request) == FALSE) + { + pthread_join(request->info_thread, NULL); + if (request->err != MIRINFOK) + { + request_running = FALSE; + got_mirror_info = FALSE; + return FTPSFAIL; + } + else + { + print_status(request, rt.quiet_mode); + } - if (request_running == TRUE) - { - if (proz_request_info_running (request) == FALSE) - { - pthread_join (request->info_thread, NULL); - if (request->err != MIRINFOK) - { - request_running = FALSE; - got_mirror_info = FALSE; - return FTPSFAIL; - } - else - { - print_status(request, rt.quiet_mode); - } + request_running = FALSE; + got_mirror_info = TRUE; - request_running = FALSE; - got_mirror_info = TRUE; + request->max_simul_pings = rt.max_simul_pings; + request->ping_timeout.tv_sec = rt.max_ping_wait; + request->ping_timeout.tv_usec = 0; - request->max_simul_pings = rt.max_simul_pings; - request->ping_timeout.tv_sec = rt.max_ping_wait; - request->ping_timeout.tv_usec = 0; + //Launch the pinging thread + PrintMessage("Got mirror info, %d server(s) found\n", request->num_mirrors); + proz_mass_ping(request); + ping_running = TRUE; + return MASSPINGINPROGRESS; + } + return FTPSINPROGRESS; + } - //Launch the pinging thread - PrintMessage("Got mirror info, %d server(s) found\n", request->num_mirrors); - proz_mass_ping (request); - ping_running = TRUE; - return MASSPINGINPROGRESS; - } - return FTPSINPROGRESS; - } + if (ping_running == TRUE) + { + print_status(request, rt.quiet_mode); - if (ping_running == TRUE) - { + if (proz_request_mass_ping_running(request) == FALSE) + { + ping_done = TRUE; + ping_running = FALSE; + proz_sort_mirror_list(request->mirrors, + request->num_mirrors); - print_status(request, rt.quiet_mode); - - if (proz_request_mass_ping_running (request) == FALSE) - { - ping_done = TRUE; - ping_running = FALSE; - proz_sort_mirror_list (request->mirrors, - request->num_mirrors); + // We have a seprate func to display this + print_status(request, FALSE); - // We have a seprate func to display this - print_status(request, FALSE); - - return MASSPINGDONE; - } - return MASSPINGINPROGRESS; - } - return MASSPINGINPROGRESS; + return MASSPINGDONE; + } + return MASSPINGINPROGRESS; + } + return MASSPINGINPROGRESS; } void -FTPS_Window::cleanup () +FTPS_Window::cleanup() { - if (request_running == TRUE) - { - proz_cancel_mirror_list_request (request); - return; - } + if (request_running == TRUE) + { + proz_cancel_mirror_list_request(request); + return; + } - if (ping_running == TRUE) - { - proz_cancel_mass_ping (request); - return; - } + if (ping_running == TRUE) + { + proz_cancel_mass_ping(request); + return; + } } void -cb_exit_ftpsearch (void *data) +cb_exit_ftpsearch(void *data) { + FTPS_Window *window = (FTPS_Window *)data; - FTPS_Window *window = (FTPS_Window *) data; + window->exit_ftpsearch_button_pressed = TRUE; - window->exit_ftpsearch_button_pressed = TRUE; + if (window->request_running == TRUE) + { + proz_cancel_mirror_list_request(window->request); + } - if (window->request_running == TRUE) - { - proz_cancel_mirror_list_request (window->request); - } - - if (window->ping_running == TRUE) - { - proz_cancel_mass_ping (window->request); - } + if (window->ping_running == TRUE) + { + proz_cancel_mass_ping(window->request); + } } void FTPS_Window::print_status(ftps_request_t *request, int quiet_mode) { - if (quiet_mode == FALSE || rt.display_mode == DISP_CURSES) + if (quiet_mode == FALSE || rt.display_mode == DISP_CURSES) { - for (int i = 0; i < request->num_mirrors; i++) - { - pthread_mutex_lock (&request->access_mutex); - ftp_mirror_stat_t status = request->mirrors[i].status; - pthread_mutex_unlock (&request->access_mutex); - switch (status) - { - case UNTESTED: - DisplayInfo(i+1,1, "%-30.30s %s\n", - request->mirrors[i].server_name, - "NOT TESTED"); - break; - case RESPONSEOK: - DisplayInfo(i+1,1, "%-30.30s %dms\n", - request->mirrors[i].server_name, - request->mirrors[i].milli_secs); - break; - case NORESPONSE: - case ERROR: - DisplayInfo(i+1,1, "%-30.30s %s\n", - request->mirrors[i].server_name, - "NO REPONSE"); - break; - default: - DisplayInfo(i+1,1, "%-30.30s %s\n", - request->mirrors[i].server_name, - "Unkown condition!!"); - break; - } - } - if (rt.display_mode == DISP_STDOUT) - fprintf(stdout,"\n"); - - } - + for (int i = 0; i < request->num_mirrors; i++) + { + pthread_mutex_lock(&request->access_mutex); + ftp_mirror_stat_t status = request->mirrors[i].status; + pthread_mutex_unlock(&request->access_mutex); + switch (status) + { + case UNTESTED: + DisplayInfo(i + 1, 1, "%-30.30s %s\n", + request->mirrors[i].server_name, + "NOT TESTED"); + break; + + case RESPONSEOK: + DisplayInfo(i + 1, 1, "%-30.30s %dms\n", + request->mirrors[i].server_name, + request->mirrors[i].milli_secs); + break; + + case NORESPONSE: + case ERROR: + DisplayInfo(i + 1, 1, "%-30.30s %s\n", + request->mirrors[i].server_name, + "NO REPONSE"); + break; + + default: + DisplayInfo(i + 1, 1, "%-30.30s %s\n", + request->mirrors[i].server_name, + "Unkown condition!!"); + break; + } + } + if (rt.display_mode == DISP_STDOUT) + fprintf(stdout, "\n"); + } } diff --git a/src/ftpsearch_win.h b/src/ftpsearch_win.h index 4e8a20f..211cc74 100644 --- a/src/ftpsearch_win.h +++ b/src/ftpsearch_win.h @@ -1,20 +1,20 @@ /****************************************************************************** - fltk prozilla - a front end for prozilla, a download accelerator library - Copyright (C) 2001 Kalum Somaratna + fltk prozilla - a front end for prozilla, a download accelerator library + Copyright (C) 2001 Kalum Somaratna - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ******************************************************************************/ #ifndef FTPSEARCH_WIN_H @@ -29,27 +29,26 @@ class FTPS_Window { public: - FTPS_Window(); - void fetch_mirror_info(urlinfo *u, off_t file_size, char *ftps_loc, - ftpsearch_server_type_t server_type, - int num_req_mirrors); +FTPS_Window(); +void fetch_mirror_info(urlinfo *u, off_t file_size, char *ftps_loc, + ftpsearch_server_type_t server_type, + int num_req_mirrors); - void cleanup(); +void cleanup(); - uerr_t callback(); - void ping_list(); - void print_status(ftps_request_t * request, int quiet_mode); +uerr_t callback(); +void ping_list(); +void print_status(ftps_request_t * request, int quiet_mode); - ftps_request_t *request; +ftps_request_t *request; - boolean request_running; - boolean ping_running; - boolean got_mirror_info; - boolean ping_done; - boolean exit_ftpsearch_button_pressed; +boolean request_running; +boolean ping_running; +boolean got_mirror_info; +boolean ping_done; +boolean exit_ftpsearch_button_pressed; private: - }; #endif diff --git a/src/index.html b/src/index.html deleted file mode 100644 index 6999ad8..0000000 --- a/src/index.html +++ /dev/null @@ -1,37 +0,0 @@ - - - - Test Page for Apache Installation - - - -

If you can see this, it means that the installation of the Apache web server - software on this system was successful. You may now add content to this - directory and replace this page.

- -
- -

Seeing this instead of the website you expected?

- -

This page is here because the site administrator has changed the - configuration of this web server. Please contact the person - responsible for maintaining this server with questions. - The Apache Software Foundation, which wrote the web server software - this site administrator is using, has nothing to do with - maintaining this site and cannot help resolve configuration - issues.

- -
- -

The Apache documentation is available - online or has been installed - locally.

- -

You are free to use the image below on an Apache-powered web - server. Thanks for using Apache!

- -
- - diff --git a/src/init.cpp b/src/init.cpp index 2f59e76..f2847cb 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1,27 +1,27 @@ /****************************************************************************** - fltk prozilla - a front end for prozilla, a download accelerator library - Copyright (C) 2001 Kalum Somaratna + fltk prozilla - a front end for prozilla, a download accelerator library + Copyright (C) 2001 Kalum Somaratna - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ******************************************************************************/ #ifdef HAVE_CONFIG_H # include -#endif /* - * HAVE_CONFIG_H - */ +#endif /* + * HAVE_CONFIG_H + */ #include #include @@ -46,19 +46,19 @@ extern struct runtime rt; /* Sets the default config */ void set_defaults() { - struct stat st; char cwd[PATH_MAX]; + /* - * Zero the structure which holds the config data + * Zero the structure which holds the config data */ memset(&rt, 0, sizeof(rt)); if (getcwd(cwd, sizeof(cwd)) == NULL) - { - proz_debug("Failed to get the current working directory"); - strcpy(cwd, "."); - } + { + proz_debug("Failed to get the current working directory"); + strcpy(cwd, "."); + } if (rt.home_dir != 0) free(rt.home_dir); @@ -67,25 +67,26 @@ void set_defaults() /*TODO what to do if the homedir is NULL */ if (rt.config_dir != 0) free(rt.config_dir); - rt.config_dir = (char *) malloc(PATH_MAX); + rt.config_dir = (char *)malloc(PATH_MAX); snprintf(rt.config_dir, PATH_MAX, "%s/%s", rt.home_dir, PRZCONFDIR); /* Make the ~/.prozilla directory if necessary */ if (stat(rt.config_dir, &st) == -1) - { - /*error has hapenned */ - if (errno == ENOENT) { - /*Create the dir then */ - if (mkdir(rt.config_dir, S_IRWXU) != 0) - { - perror - (_("unable to create the directory to store the config info in")); - exit(0); - } - } else - perror(_("Error while stating the config info directory")); - } + /*error has hapenned */ + if (errno == ENOENT) + { + /*Create the dir then */ + if (mkdir(rt.config_dir, S_IRWXU) != 0) + { + perror + (_("unable to create the directory to store the config info in")); + exit(0); + } + } + else + perror(_("Error while stating the config info directory")); + } /*Output the file to the directory , cwd by default */ @@ -103,49 +104,49 @@ void set_defaults() /* - * The default no of connections and maximum redirections allowed + * The default no of connections and maximum redirections allowed */ rt.num_connections = 4; rt.max_redirections = 10; - /* Uses PASV by default + /* Uses PASV by default */ rt.ftp_use_pasv = libprozrtinfo.ftp_use_pasv; /* - * The force option, off by default when enabled + * The force option, off by default when enabled * cause Prozilla not to prompt the user about overwriting existent * files etc.. */ rt.force_mode = FALSE; /* - * .netrc options + * .netrc options */ rt.use_netrc = TRUE; /* - * The max number of trys and the delay between each + * The max number of trys and the delay between each */ - rt.max_attempts = 0; /*TODO it is currently UNLIMITED */ - rt.retry_delay = 15; /* - * delay in seconds - */ + rt.max_attempts = 0; /*TODO it is currently UNLIMITED */ + rt.retry_delay = 15; /* + * delay in seconds + */ /*Default is to not log any debug info */ rt.debug_mode = FALSE; rt.quiet_mode = TRUE; rt.libdebug_mode = TRUE; rt.ftp_search = FALSE; - rt.min_search_size=128; + rt.min_search_size = 128; rt.max_simul_pings = 5; rt.max_ping_wait = 8; rt.ftps_mirror_req_n = 40; - rt.max_bps_per_dl = 0; /* 0= No throttling */ - + rt.max_bps_per_dl = 0; /* 0= No throttling */ + if (rt.http_proxy != 0) free(rt.http_proxy); - rt.http_proxy = (proxy_info *) malloc(sizeof(proxy_info)); + rt.http_proxy = (proxy_info *)malloc(sizeof(proxy_info)); rt.http_proxy->username = strdup(""); rt.http_proxy->passwd = strdup(""); rt.http_proxy->type = HTTPPROXY; @@ -154,7 +155,7 @@ void set_defaults() if (rt.ftp_proxy != 0) free(rt.ftp_proxy); - rt.ftp_proxy = (proxy_info *) malloc(sizeof(proxy_info)); + rt.ftp_proxy = (proxy_info *)malloc(sizeof(proxy_info)); rt.ftp_proxy->username = strdup(""); rt.ftp_proxy->passwd = strdup(""); rt.ftp_proxy->type = HTTPPROXY; @@ -201,7 +202,6 @@ void set_runtime_values() void cleanuprt() { - if (rt.config_dir != 0) free(rt.config_dir); @@ -222,5 +222,4 @@ void cleanuprt() if (rt.logfile_dir != 0) free(rt.logfile_dir); - - } +} diff --git a/src/init.h b/src/init.h index d4aeb47..e772453 100644 --- a/src/init.h +++ b/src/init.h @@ -1,17 +1,17 @@ /* Declarations for initialising runtime variables - Copyright (C) 2000 Kalum Somaratna + Copyright (C) 2000 Kalum Somaratna This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. - + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - + You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ diff --git a/src/interface.c b/src/interface.c index 86f62ab..02a4735 100644 --- a/src/interface.c +++ b/src/interface.c @@ -1,25 +1,25 @@ -/* The file contins routines for managing curses - Copyright (C) 2000 Kalum Somaratna - +/* The file contins routines for managing curses + Copyright (C) 2000 Kalum Somaratna + This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. - + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - + You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #ifdef HAVE_CONFIG_H # include -#endif /* - * HAVE_CONFIG_H - */ +#endif /* + * HAVE_CONFIG_H + */ #include #include @@ -31,9 +31,9 @@ #include #else #include -#endif /* - * HAVE_CURSES - */ +#endif /* + * HAVE_CURSES + */ #include #include @@ -47,7 +47,7 @@ /* This is used to convert the connection.status enums to a user * friendly text message */ /* the enums are defined in connection.h and are as belor - *typedef enum { + *typedef enum { * IDLE = 0, * CONNECTING, * LOGGININ, @@ -57,37 +57,37 @@ * CONREJECTED, * REMOTEFATAL, * LOCALFATAL - *} dl_status; + **} dl_status; * * And here are the texts for the above enums. -*/ + */ const char *dl_status_txt[] = { - "Idle", - "Connecting", - "Logging in", - "Downloading", - "Completed", -/* I have decided to change the login failed message + "Idle", + "Connecting", + "Logging in", + "Downloading", + "Completed", +/* I have decided to change the login failed message * to something else like "login rejected" because people - * might get alarmed and abort the download rather than + * might get alarmed and abort the download rather than * letting prozilla handle it */ - "Login Denied", - "Connect Refused", - "Remote Fatal", - "Local Fatal", - "Timed Out", - "Max attempts reached", + "Login Denied", + "Connect Refused", + "Remote Fatal", + "Local Fatal", + "Timed Out", + "Max attempts reached", }; #define CTRL(x) ((x) & 0x1F) -//static int top_con = 0; // the connection that is on the top of the display - +//static int top_con = 0; // the connection that is on the top of the display + #define MAX_CON_VIEWS 4 /* - * needed for the message(...) function + * needed for the message(...) function */ pthread_mutex_t curses_msg_mutex = PTHREAD_MUTEX_INITIALIZER; char message_buffer[MAX_MSG_SIZE]; @@ -105,124 +105,121 @@ int messageRow = 0; int messageCol = 0; -/* because of different args for different ncurses, I had to write these - * my self +/* because of different args for different ncurses, I had to write these + * my self */ -#define kwattr_get(win,a,p,opts) ((void)((a) != 0 && (*(a) = (win)->_attrs)), (void)((p) != 0 && (*(p) = PAIR_NUMBER((win)->_attrs))),OK) +#define kwattr_get(win, a, p, opts) ((void)((a) != 0 && (*(a) = (win)->_attrs)), (void)((p) != 0 && (*(p) = PAIR_NUMBER((win)->_attrs))), OK) -#define kwattr_set(win,a,p,opts) ((win)->_attrs = (((a) & ~A_COLOR) | COLOR_PAIR(p)), OK) +#define kwattr_set(win, a, p, opts) ((win)->_attrs = (((a) & ~A_COLOR) | COLOR_PAIR(p)), OK) /* Message: prints a message to the screen */ void curses_message(char *message) { - short i; - int x, y; - attr_t attrs; - /* - * Lock the mutex - */ - pthread_mutex_lock(&curses_msg_mutex); + short i; + int x, y; + attr_t attrs; + + /* + * Lock the mutex + */ + pthread_mutex_lock(&curses_msg_mutex); - /* - * get the cursor position - */ - getyx(stdscr, y, x); - kwattr_get(stdscr, &attrs, &i, NULL); - move(messageRow-1, 0); - clrtoeol(); - move(messageRow, 0); - clrtoeol(); - move(messageRow+1, 0); - clrtoeol(); - attrset(COLOR_PAIR(MSG_PAIR) | A_BOLD); - mvprintw(messageRow, 0, "%s", message); - attrset(COLOR_PAIR(NULL_PAIR)); - /* - * Unlock the mutex - */ - refresh(); - kwattr_set(stdscr, attrs, i, NULL); - /* - * set the cursor to whhere it was */ + /* + * get the cursor position + */ + getyx(stdscr, y, x); + kwattr_get(stdscr, &attrs, &i, NULL); + move(messageRow - 1, 0); + clrtoeol(); + move(messageRow, 0); + clrtoeol(); + move(messageRow + 1, 0); + clrtoeol(); + attrset(COLOR_PAIR(MSG_PAIR) | A_BOLD); + mvprintw(messageRow, 0, "%s", message); + attrset(COLOR_PAIR(NULL_PAIR)); + /* + * Unlock the mutex + */ + refresh(); + kwattr_set(stdscr, attrs, i, NULL); + /* + * set the cursor to whhere it was */ - move(y, x); - pthread_mutex_unlock(&curses_msg_mutex); - + move(y, x); + pthread_mutex_unlock(&curses_msg_mutex); } /* Displays the mesasge and gets the users input for overwriting files*/ int curses_query_user_input(const char *args, ...) { - char p[MAX_MSG_SIZE+1]; - va_list vp; - attr_t attrs; - int x, y; - int ch; - int i; + char p[MAX_MSG_SIZE + 1]; + va_list vp; + attr_t attrs; + int x, y; + int ch; + int i; - va_start(vp, args); - vsnprintf(p,sizeof(p) , args, vp); - va_end(vp); - getyx(stdscr, y, x); - kwattr_get(stdscr, &attrs, &i, NULL); + va_start(vp, args); + vsnprintf(p, sizeof(p), args, vp); + va_end(vp); + getyx(stdscr, y, x); + kwattr_get(stdscr, &attrs, &i, NULL); - attrset(COLOR_PAIR(PROMPT_PAIR) | A_BOLD); - move(19, 0); - clrtoeol(); - move(20, 0); - clrtoeol(); - move(21, 0); - clrtoeol(); - mvprintw(19, 0, "%s", p); - echo(); - refresh(); - do + attrset(COLOR_PAIR(PROMPT_PAIR) | A_BOLD); + move(19, 0); + clrtoeol(); + move(20, 0); + clrtoeol(); + move(21, 0); + clrtoeol(); + mvprintw(19, 0, "%s", p); + echo(); + refresh(); + do { - napms(20); - ch = getch(); - } - while (ch == ERR); + napms(20); + ch = getch(); + } while (ch == ERR); - refresh(); - noecho(); - kwattr_set(stdscr, attrs, i, NULL); - /* - * set the cursor to where it was - */ - move(y, x); - /* - * the following strange line is for compatibility - */ - return islower(ch) ? toupper(ch) : ch; + refresh(); + noecho(); + kwattr_set(stdscr, attrs, i, NULL); + /* + * set the cursor to where it was + */ + move(y, x); + /* + * the following strange line is for compatibility + */ + return islower(ch) ? toupper(ch) : ch; } void initCurses() { - initscr(); - keypad(stdscr, TRUE); - start_color(); - noecho(); - nodelay(stdscr, TRUE); - init_pair(HIGHLIGHT_PAIR, COLOR_GREEN, COLOR_BLACK); - init_pair(MSG_PAIR, COLOR_YELLOW, COLOR_BLUE); - init_pair(WARN_PAIR, COLOR_RED, COLOR_BLACK); - init_pair(PROMPT_PAIR, COLOR_RED, COLOR_BLACK); - init_pair(SELECTED_PAIR, COLOR_WHITE, COLOR_BLUE); - getmaxyx(stdscr,maxRow,maxCol); - messageRow = maxRow - 2; - messageCol = 1; + initscr(); + keypad(stdscr, TRUE); + start_color(); + noecho(); + nodelay(stdscr, TRUE); + init_pair(HIGHLIGHT_PAIR, COLOR_GREEN, COLOR_BLACK); + init_pair(MSG_PAIR, COLOR_YELLOW, COLOR_BLUE); + init_pair(WARN_PAIR, COLOR_RED, COLOR_BLACK); + init_pair(PROMPT_PAIR, COLOR_RED, COLOR_BLACK); + init_pair(SELECTED_PAIR, COLOR_WHITE, COLOR_BLUE); + getmaxyx(stdscr, maxRow, maxCol); + messageRow = maxRow - 2; + messageCol = 1; } void shutdownCurses() { - endwin(); - } @@ -231,22 +228,22 @@ void PrintMessage(const char *format, ...) va_list args; char message[MAX_MSG_SIZE + 1 + 1]; - va_start(args, format); - vsnprintf((char *) &message, MAX_MSG_SIZE, format, args); - va_end(args); + va_start(args, format); + vsnprintf((char *)&message, MAX_MSG_SIZE, format, args); + va_end(args); if (rt.display_mode == DISP_CURSES) - { - curses_message(message); + { + curses_message(message); } else - { - if (fwrite(message, 1, strlen(message), stdout) != strlen(message)) - { - return; - } - } - delay_ms(500); + { + if (fwrite(message, 1, strlen(message), stdout) != strlen(message)) + { + return; + } + } + delay_ms(500); } void DisplayInfo(int row, int col, const char *format, ...) @@ -254,111 +251,112 @@ void DisplayInfo(int row, int col, const char *format, ...) va_list args; char message[MAX_MSG_SIZE + 1 + 1]; - va_start(args, format); - vsnprintf((char *) &message, MAX_MSG_SIZE, format, args); - va_end(args); + va_start(args, format); + vsnprintf((char *)&message, MAX_MSG_SIZE, format, args); + va_end(args); if (rt.display_mode == DISP_CURSES) - { - attrset(COLOR_PAIR(NULL_PAIR) | A_BOLD); - mvaddstr(row,col, message); - attrset(COLOR_PAIR(NULL_PAIR)); + { + attrset(COLOR_PAIR(NULL_PAIR) | A_BOLD); + mvaddstr(row, col, message); + attrset(COLOR_PAIR(NULL_PAIR)); - refresh(); + refresh(); } else - { - if (fwrite(message, 1, strlen(message), stdout) != strlen(message)) - { - return; - } - } + { + if (fwrite(message, 1, strlen(message), stdout) != strlen(message)) + { + return; + } + } // delay_ms(300); } -void DisplayCursesInfo(download_t * download ) +void DisplayCursesInfo(download_t * download) { char buf[1000]; int i = 0; int line = 1; - int secs_left; + int secs_left; + // erase(); - refresh(); - attrset(COLOR_PAIR(HIGHLIGHT_PAIR) | A_BOLD); - snprintf(buf, sizeof(buf), _("Connection Server Status Received")); - mvprintw(line++,1, buf); + refresh(); + attrset(COLOR_PAIR(HIGHLIGHT_PAIR) | A_BOLD); + snprintf(buf, sizeof(buf), _("Connection Server Status Received")); + mvprintw(line++, 1, buf); attrset(COLOR_PAIR(NULL_PAIR)); - total_bytes_got = proz_download_get_total_bytes_got(download) / 1024; - - if (start == 1) + total_bytes_got = proz_download_get_total_bytes_got(download) / 1024; + + if (start == 1) { time_stamp = time(NULL); bytes_got_last_time = total_bytes_got; start = 0; } - if (time_stamp + 1 == time(NULL)) + if (time_stamp + 1 == time(NULL)) { current_dl_speed = total_bytes_got - bytes_got_last_time; time_stamp = time(NULL); bytes_got_last_time = total_bytes_got; } - + for (i = 0; i < download->num_connections; i++) { - snprintf(buf,sizeof(buf), _(" %2d %-25.25s %-15.15s %10.1fK of %.1fK"), i + 1, - download->pconnections[i]->u.host, - proz_connection_get_status_string(download->pconnections[i]), - (float)proz_connection_get_total_bytes_got(download-> - pconnections[i])/1024, - ((float)download->pconnections[i]->main_file_size / 1024) / download->num_connections); - mvprintw(line++,1, buf); + snprintf(buf, sizeof(buf), _(" %2d %-25.25s %-15.15s %10.1fK of %.1fK"), i + 1, + download->pconnections[i]->u.host, + proz_connection_get_status_string(download->pconnections[i]), + (float)proz_connection_get_total_bytes_got(download-> + pconnections[i]) / 1024, + ((float)download->pconnections[i]->main_file_size / 1024) / download->num_connections); + mvprintw(line++, 1, buf); } - line = line + 2; - attrset(COLOR_PAIR(HIGHLIGHT_PAIR) | A_BOLD); - mvprintw(line++, 1, "%s", download->u.url); - line++; - - attrset(COLOR_PAIR(HIGHLIGHT_PAIR)); - if (download->main_file_size > 0) - mvprintw(line++,1,_("File Size = %lldK"),download->main_file_size / 1024); - else - mvprintw(line++,1,_("File Size = UNKNOWN")); - - snprintf(buf,sizeof(buf), _("Total Bytes received %lld Kb (%.2f%%)"), - total_bytes_got, - (((float)total_bytes_got * 100) / ((float)download->main_file_size / 1024))); - line++; - mvprintw(line++,1, buf); + line = line + 2; + attrset(COLOR_PAIR(HIGHLIGHT_PAIR) | A_BOLD); + mvprintw(line++, 1, "%s", download->u.url); + line++; - snprintf(buf,sizeof(buf),_("Current speed = %1.2fKb/s, Average D/L speed = %1.2fKb/s"), - current_dl_speed , proz_download_get_average_speed(download) / 1024); - mvprintw(line++,1, buf); - clrtoeol(); + attrset(COLOR_PAIR(HIGHLIGHT_PAIR)); + if (download->main_file_size > 0) + mvprintw(line++, 1, _("File Size = %lldK"), download->main_file_size / 1024); + else + mvprintw(line++, 1, _("File Size = UNKNOWN")); - if ((secs_left = proz_download_get_est_time_left(download)) != -1) + snprintf(buf, sizeof(buf), _("Total Bytes received %lld Kb (%.2f%%)"), + total_bytes_got, + (((float)total_bytes_got * 100) / ((float)download->main_file_size / 1024))); + line++; + mvprintw(line++, 1, buf); + + snprintf(buf, sizeof(buf), _("Current speed = %1.2fKb/s, Average D/L speed = %1.2fKb/s"), + current_dl_speed, proz_download_get_average_speed(download) / 1024); + mvprintw(line++, 1, buf); + clrtoeol(); + + if ((secs_left = proz_download_get_est_time_left(download)) != -1) { if (secs_left < 60) - snprintf(buf,sizeof(buf), _("Time Remaining %d Seconds"), secs_left); + snprintf(buf, sizeof(buf), _("Time Remaining %d Seconds"), secs_left); else if (secs_left < 3600) - snprintf(buf,sizeof(buf), _("Time Remaining %d Minutes %d Seconds"), secs_left / 60, - secs_left % 60); + snprintf(buf, sizeof(buf), _("Time Remaining %d Minutes %d Seconds"), secs_left / 60, + secs_left % 60); else - snprintf(buf,sizeof(buf), _("Time Remaining %d Hours %d minutes"), secs_left / 3600, - (secs_left % 3600) / 60); + snprintf(buf, sizeof(buf), _("Time Remaining %d Hours %d minutes"), secs_left / 3600, + (secs_left % 3600) / 60); - mvprintw(line++,1, buf); + mvprintw(line++, 1, buf); clrtoeol(); line++; - + attrset(COLOR_PAIR(HIGHLIGHT_PAIR) | A_BOLD); if (download->resume_support) - mvprintw(line++,1,_("Resume Supported")); + mvprintw(line++, 1, _("Resume Supported")); else - mvprintw(line++,1,_("Resume NOT Supported")); + mvprintw(line++, 1, _("Resume NOT Supported")); } attrset(COLOR_PAIR(NULL_PAIR)); refresh(); diff --git a/src/interface.h b/src/interface.h index 031d195..9f84a03 100644 --- a/src/interface.h +++ b/src/interface.h @@ -1,15 +1,15 @@ /* Declarations for the interface - + This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. - + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - + You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ @@ -26,12 +26,12 @@ extern "C" { /*definnitions about the ncurse colors that the app will use*/ enum { - NULL_PAIR = 0, - HIGHLIGHT_PAIR, - MSG_PAIR, - WARN_PAIR, - PROMPT_PAIR, - SELECTED_PAIR + NULL_PAIR = 0, + HIGHLIGHT_PAIR, + MSG_PAIR, + WARN_PAIR, + PROMPT_PAIR, + SELECTED_PAIR }; void initCurses(); diff --git a/src/main.cpp b/src/main.cpp index cc9eadf..f458ed3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,20 +1,20 @@ /****************************************************************************** - prozilla - a front end for prozilla, a download accelerator library - Copyright (C) 2001 Kalum Somaratna + prozilla - a front end for prozilla, a download accelerator library + Copyright (C) 2001 Kalum Somaratna - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ******************************************************************************/ #if HAVE_CONFIG_H # include @@ -47,39 +47,39 @@ struct runtime rt; // structure for options parsing, struct option long_opts[] = { - /* - * { name has_arg *flag val } - */ - {"resume", no_argument, NULL, 'r'}, + /* + * { name has_arg *flag val } + */ + { "resume", no_argument, NULL, 'r' }, /* {"connections", required_argument, NULL, 'c'},*/ - {"license", no_argument, NULL, 'L'}, - {"help", no_argument, NULL, 'h'}, - {"gtk", no_argument, NULL, 'g'}, - {"no-netrc", no_argument, NULL, 'n'}, - {"tries", required_argument, NULL, 't'}, - {"force", no_argument, NULL, 'f'}, - {"version", no_argument, NULL, 'v'}, - {"output-document", required_argument, NULL, 'O'}, - {"directory-prefix", required_argument, NULL, 'P'}, - {"use-port", no_argument, NULL, 129}, - {"retry-delay", required_argument, NULL, 130}, - {"timeout", required_argument, NULL, 131}, - {"no-getch", no_argument, NULL, 132}, - {"debug", no_argument, NULL, 133}, - {"ftpsearch", no_argument, NULL, 's'}, - {"no-search", no_argument, NULL, 135}, - {"pt", required_argument, NULL, 136}, - {"pao", required_argument, NULL, 137}, - {"max-ftps-servers", required_argument, NULL, 138}, - {"max-bps", required_argument, NULL, 139}, - {"verbose", no_argument, NULL, 'v'}, - {"no-curses", no_argument, NULL, 140}, - {"min-size",required_argument,NULL,141}, - {"ftpsid", required_argument, NULL,142}, - {0, 0, 0, 0} + { "license", no_argument, NULL, 'L' }, + { "help", no_argument, NULL, 'h' }, + { "gtk", no_argument, NULL, 'g' }, + { "no-netrc", no_argument, NULL, 'n' }, + { "tries", required_argument, NULL, 't' }, + { "force", no_argument, NULL, 'f' }, + { "version", no_argument, NULL, 'v' }, + { "output-document", required_argument, NULL, 'O' }, + { "directory-prefix", required_argument, NULL, 'P' }, + { "use-port", no_argument, NULL, 129 }, + { "retry-delay", required_argument, NULL, 130 }, + { "timeout", required_argument, NULL, 131 }, + { "no-getch", no_argument, NULL, 132 }, + { "debug", no_argument, NULL, 133 }, + { "ftpsearch", no_argument, NULL, 's' }, + { "no-search", no_argument, NULL, 135 }, + { "pt", required_argument, NULL, 136 }, + { "pao", required_argument, NULL, 137 }, + { "max-ftps-servers", required_argument, NULL, 138 }, + { "max-bps", required_argument, NULL, 139 }, + { "verbose", no_argument, NULL, 'v' }, + { "no-curses", no_argument, NULL, 140 }, + { "min-size", required_argument, NULL, 141 }, + { "ftpsid", required_argument, NULL, 142 }, + { 0, 0, 0, 0 } }; -int open_new_dl_win (urlinfo * url_data, boolean ftpsearch); +int open_new_dl_win(urlinfo * url_data, boolean ftpsearch); DL_Window *dl_win = NULL; @@ -89,87 +89,87 @@ extern int nextchar; /* displays the software license */ void -license (void) +license(void) { - fprintf (stderr, - " Copyright (C) 2000 Kalum Somaratna\n" - "\n" - " This program is free software; you can redistribute it and/or modify\n" - " it under the terms of the GNU General Public License as published by\n" - " the Free Software Foundation; either version 2, or (at your option)\n" - " any later version.\n" - "\n" - " This program is distributed in the hope that it will be useful,\n" - " but WITHOUT ANY WARRANTY; without even the implied warranty of\n" - " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" - " GNU General Public License for more details.\n" - "\n" - " You should have received a copy of the GNU General Public License\n" - " along with this program; if not, write to the Free Software\n" - " Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n"); + fprintf(stderr, + " Copyright (C) 2000 Kalum Somaratna\n" + "\n" + " This program is free software; you can redistribute it and/or modify\n" + " it under the terms of the GNU General Public License as published by\n" + " the Free Software Foundation; either version 2, or (at your option)\n" + " any later version.\n" + "\n" + " This program is distributed in the hope that it will be useful,\n" + " but WITHOUT ANY WARRANTY; without even the implied warranty of\n" + " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" + " GNU General Public License for more details.\n" + "\n" + " You should have received a copy of the GNU General Public License\n" + " along with this program; if not, write to the Free Software\n" + " Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n"); } /* displays the help message */ void -help (void) +help(void) { - fprintf (stderr, - "Usage: proz [OPTIONS] file_url\n" - "\n" - "Ex: proz http://gnu.org/gnu.jpg\n" - "\n" - "Options:\n" - " -h, --help Give this help\n" - " -r, --resume Resume an interrupted download\n" - " -f, --force Never prompt the user when overwriting files\n" - " -1 Force a single connection only\n" - " -n, --no-netrc Don't use .netrc, get the user/password\n" - " from the command line,otherwise use the\n" - " anonymous login for FTP sessions\n" - " --no-getch Instead of waiting for the user pressing a key,\n" - " print the error to stdout and quit\n" - " --debug Log debugging info to a file (default is debug.log)\n" - " -v,--verbose Increase the amount of information sent to stdout\n" - " --no-curses Don't use Curses, plain text to stdout\n" - "\n" - "Files:\n" - " -O, --output-document=FILE write documents to FILE\n" - "\n" - "Directories:\n" - " -P, --directory-prefix=DIR save the generated file to DIR/\n" - "\n" - "FTP Options:\n" - " --use-port Force usage of PORT insted of PASV (default)\n" - " for ftp transactions\n" - "\n" - "Download Options:\n" - " -s, --ftpsearch Do a ftpsearch for faster mirrors\n" - " --no-search Do a direct download (no ftpsearch)\n" - " -k=n Use n connections instead of the default(4)\n" - " --timeout=n Set the timeout for connections to n seconds\n" - " (default 180)\n" - " -t, --tries=n Set number of attempts to n (default(200), 0=unlimited)\n" - " --retry-delay=n Set the time between retrys to n seconds\n" - " (default 15 seconds)\n" - " --max-bps=n Limit bandwith consumed to n bps (0=unlimited)\n" - "\n" - "FTP Search Options:\n" - " --pt=n Wait 2*n seconds for a server response (default 2*4)\n" - " --pao=n Ping n servers at once(default 5 servers at once)\n" - " --max-ftps-servers=n Request a max of n servers from ftpsearch (default 40)\n" - " --min-size=n If a file is smaller than 'n'Kb, don't search, just download it\n" - " --ftpsid=n The ftpsearch server to use\n" - " (0=filesearching.com\n" - " 1=ftpsearch.elmundo.es\n" - "\n" - "Information Options:\n" - " -L, --license Display software license\n" - " -V, --version Display version number\n" - "\n" - "ProZilla homepage: http://prozilla.genesys.ro\n" - "Please report bugs to \n"); + fprintf(stderr, + "Usage: proz [OPTIONS] file_url\n" + "\n" + "Ex: proz http://gnu.org/gnu.jpg\n" + "\n" + "Options:\n" + " -h, --help Give this help\n" + " -r, --resume Resume an interrupted download\n" + " -f, --force Never prompt the user when overwriting files\n" + " -1 Force a single connection only\n" + " -n, --no-netrc Don't use .netrc, get the user/password\n" + " from the command line,otherwise use the\n" + " anonymous login for FTP sessions\n" + " --no-getch Instead of waiting for the user pressing a key,\n" + " print the error to stdout and quit\n" + " --debug Log debugging info to a file (default is debug.log)\n" + " -v,--verbose Increase the amount of information sent to stdout\n" + " --no-curses Don't use Curses, plain text to stdout\n" + "\n" + "Files:\n" + " -O, --output-document=FILE write documents to FILE\n" + "\n" + "Directories:\n" + " -P, --directory-prefix=DIR save the generated file to DIR/\n" + "\n" + "FTP Options:\n" + " --use-port Force usage of PORT insted of PASV (default)\n" + " for ftp transactions\n" + "\n" + "Download Options:\n" + " -s, --ftpsearch Do a ftpsearch for faster mirrors\n" + " --no-search Do a direct download (no ftpsearch)\n" + " -k=n Use n connections instead of the default(4)\n" + " --timeout=n Set the timeout for connections to n seconds\n" + " (default 180)\n" + " -t, --tries=n Set number of attempts to n (default(200), 0=unlimited)\n" + " --retry-delay=n Set the time between retrys to n seconds\n" + " (default 15 seconds)\n" + " --max-bps=n Limit bandwith consumed to n bps (0=unlimited)\n" + "\n" + "FTP Search Options:\n" + " --pt=n Wait 2*n seconds for a server response (default 2*4)\n" + " --pao=n Ping n servers at once(default 5 servers at once)\n" + " --max-ftps-servers=n Request a max of n servers from ftpsearch (default 40)\n" + " --min-size=n If a file is smaller than 'n'Kb, don't search, just download it\n" + " --ftpsid=n The ftpsearch server to use\n" + " (0=filesearching.com\n" + " 1=ftpsearch.elmundo.es\n" + "\n" + "Information Options:\n" + " -L, --license Display software license\n" + " -V, --version Display version number\n" + "\n" + "ProZilla homepage: http://prozilla.genesys.ro\n" + "Please report bugs to \n"); } @@ -177,381 +177,396 @@ help (void) /* Displays the version */ void -version (void) +version(void) { - fprintf (stderr, _("%s. Version: %s\n"), PACKAGE_NAME, VERSION); + fprintf(stderr, _("%s. Version: %s\n"), PACKAGE_NAME, VERSION); } void -ms (const char *msg, void *cb_data) +ms(const char *msg, void *cb_data) { - PrintMessage("%s\n",msg); + PrintMessage("%s\n", msg); } int -open_new_dl_win (urlinfo * url_data, boolean ftpsearch) +open_new_dl_win(urlinfo * url_data, boolean ftpsearch) { + dl_win = new DL_Window(url_data); - dl_win = new DL_Window (url_data); + dl_win->dl_start(rt.num_connections, ftpsearch); - dl_win->dl_start (rt.num_connections, ftpsearch); + proz_debug("calling the callback function..."); - proz_debug ("calling the callback function..."); + //need a timer here... + while (dl_win->status != DL_DONE && dl_win->status != DL_IDLING && dl_win->status != DL_ABORTED) + { + delay_ms(700); //wait before checking the status again... + dl_win->my_cb(); + } - //need a timer here... - while (dl_win->status != DL_DONE && dl_win->status != DL_IDLING && dl_win->status != DL_ABORTED) - { - delay_ms (700); //wait before checking the status again... - dl_win->my_cb (); - } - -return((dl_win->status==DL_DONE) ? 1:-1); - // delete (dl_win); + return((dl_win->status == DL_DONE) ? 1 : -1); + // delete (dl_win); } int -main (int argc, char **argv) +main(int argc, char **argv) { - int c; - int ret; - char *opt_file = NULL; - proz_init (argc, argv); //init libprozilla - set_defaults (); //set some reasonable defaults - load_prefs (); //load values from the config file + int c; + int ret; + char *opt_file = NULL; - while ((c = - getopt_long (argc, argv, "?hvrfk:1Lt:VgsP:O:", long_opts, - NULL)) != EOF) - { - switch (c) - { - case 'L': - license (); - exit (0); - case 'h': - help (); - exit (0); - case 'V': - version (); - exit (0); - case 'r': - rt.resume_mode = RESUME; - break; - case 'f': - rt.force_mode = TRUE; - break; - case 'k': - if (setargval (optarg, &rt.num_connections) != 1) - { - /* - * The call failed due to a invalid arg - */ - printf (_("Error: Invalid arguments for the -k option\n" "Please type proz --help for help\n")); - exit (0); - } + proz_init(argc, argv); //init libprozilla + set_defaults(); //set some reasonable defaults + load_prefs(); //load values from the config file - if (rt.num_connections == 0) - { - printf (_("Hey! How can I download anything with 0 (Zero)" " connections!?\n" "Please type proz --help for help\n")); - exit (0); - } + while ((c = + getopt_long(argc, argv, "?hvrfk:1Lt:VgsP:O:", long_opts, + NULL)) != EOF) + { + switch (c) + { + case 'L': + license(); + exit(0); - break; - case 't': - if (setargval (optarg, &rt.max_attempts) != 1) - { - /* - * The call failed due to a invalid arg - */ - printf (_("Error: Invalid arguments for the -t or --tries option(s)\n" "Please type proz --help for help\n")); - exit (0); - } - break; - case 'n': - /* - * Don't use ~/.netrc" - */ - rt.use_netrc = FALSE; - break; - case 'O': - /* - * Output file name - */ - opt_file = kstrdup(optarg); - break; - case 'P': - /* - * Save the downloaded file to DIR - */ - rt.output_dir = kstrdup (optarg); - break; - case '?': - help (); - exit (0); - break; - case '1': - rt.num_connections = 1; - break; + case 'h': + help(); + exit(0); - case 'g': - /* - * TODO solve this soon - */ - printf ("Error: GTK interface is not supported in " - "the development version currently\n"); - exit (0); - break; + case 'V': + version(); + exit(0); - case 129: - /* - * lets use PORT as the default then - */ - rt.ftp_use_pasv = FALSE; - break; - case 130: - /* - * retry-delay option - */ - if (setargval (optarg, &rt.retry_delay) != 1) - { - /* - * The call failed due to a invalid arg - */ - printf (_("Error: Invalid arguments for the --retry-delay option\n" "Please type proz --help for help\n")); - exit (0); - } - break; - case 131: - /*--timout option */ - if (setargval (optarg, &rt.itimeout) != 1) - { - /* - * The call failed due to a invalid arg - */ - printf (_("Error: Invalid arguments for the --timeout option\n" "Please type proz --help for help\n")); - exit (0); - } - break; - case 132: - /* --no-getch option */ - rt.dont_prompt = TRUE; - break; + case 'r': + rt.resume_mode = RESUME; + break; - case 133: - /* --debug option */ - rt.debug_mode = TRUE; - rt.libdebug_mode=TRUE; - break; + case 'f': + rt.force_mode = TRUE; + break; - case 'v': - /* --verbose option */ - rt.quiet_mode = FALSE; - break; + case 'k': + if (setargval(optarg, &rt.num_connections) != 1) + { + /* + * The call failed due to a invalid arg + */ + printf(_("Error: Invalid arguments for the -k option\n" "Please type proz --help for help\n")); + exit(0); + } - case 's': - /* --ftpsearch option */ - rt.ftp_search = TRUE; - break; + if (rt.num_connections == 0) + { + printf(_("Hey! How can I download anything with 0 (Zero)" " connections!?\n" "Please type proz --help for help\n")); + exit(0); + } - case 135: - /* --no-search option */ - rt.ftp_search = FALSE; - break; + break; - case 136: - /* --pt option */ - if (setargval (optarg, &rt.max_ping_wait) != 1) - { - /* - * The call failed due to a invalid arg - */ - printf (_("Error: Invalid arguments for the --pt option\n" "Please type proz --help for help\n")); - exit (0); - } + case 't': + if (setargval(optarg, &rt.max_attempts) != 1) + { + /* + * The call failed due to a invalid arg + */ + printf(_("Error: Invalid arguments for the -t or --tries option(s)\n" "Please type proz --help for help\n")); + exit(0); + } + break; - if (rt.max_ping_wait == 0) - { - printf (_("Hey! Does waiting for a server response for Zero(0)" " seconds make and sense to you!?\n" "Please type proz --help for help\n")); - exit (0); - } + case 'n': + /* + * Don't use ~/.netrc" + */ + rt.use_netrc = FALSE; + break; - break; - case 137: - /* --pao option */ - if (setargval (optarg, &rt.max_simul_pings) != 1) - { - /* - * The call failed due to a invalid arg - */ - printf (_("Error: Invalid arguments for the --pao option\n" "Please type proz --help for help\n")); - exit (0); - } + case 'O': + /* + * Output file name + */ + opt_file = kstrdup(optarg); + break; - if (rt.max_simul_pings == 0) - { - printf (_("Hey you! Will pinging Zero(0) servers at once" " achive anything for me!?\n" "Please type proz --help for help\n")); - exit (0); - } + case 'P': + /* + * Save the downloaded file to DIR + */ + rt.output_dir = kstrdup(optarg); + break; - break; + case '?': + help(); + exit(0); + break; - case 138: - /* --max-ftp-servers option */ - if (setargval (optarg, &rt.ftps_mirror_req_n) != 1) - { - /* - * The call failed due to a invalid arg - */ - printf (_("Error: Invalid arguments for the --pao option\n" "Please type proz --help for help\n")); - exit (0); - } + case '1': + rt.num_connections = 1; + break; - if (rt.ftps_mirror_req_n == 0) - { - printf (_("Hey! Will requesting Zero(0) servers at once" "from the ftpearch achive anything!?\n" "Please type proz --help for help\n")); - exit (0); - } + case 'g': + /* + * TODO solve this soon + */ + printf("Error: GTK interface is not supported in " + "the development version currently\n"); + exit(0); + break; - break; - case 139: - /* --max-bps */ - if (setlongargval (optarg, &rt.max_bps_per_dl) != 1) - { - /* - * The call failed due to a invalid arg - */ - printf (_("Error: Invalid arguments for the --max-bps option\n" "Please type proz --help for help\n")); - exit (0); - } - break; - case 140: - rt.display_mode = DISP_STDOUT; - break; - case 141: - /* --min-size */ - if (setlongargval (optarg, &rt.min_search_size) != 1) - { - /* - * The call failed due to a invalid arg - */ - printf (_("Error: Invalid arguments for the --min-size option\n" "Please type proz --help for help\n")); - exit (0); - } - break; + case 129: + /* + * lets use PORT as the default then + */ + rt.ftp_use_pasv = FALSE; + break; - case 142: - /* --ftpsid */ - - if (setargval (optarg, &rt.ftpsearch_server_id) != 1) - { - /* - * The call failed due to a invalid arg - */ - printf (_("Error: Invalid arguments for the --ftpsid option\n" "Please type proz --help for help\n")); - exit (0); - } + case 130: + /* + * retry-delay option + */ + if (setargval(optarg, &rt.retry_delay) != 1) + { + /* + * The call failed due to a invalid arg + */ + printf(_("Error: Invalid arguments for the --retry-delay option\n" "Please type proz --help for help\n")); + exit(0); + } + break; - if (rt.ftpsearch_server_id < 0 || rt.ftpsearch_server_id >1) - { - printf (_("The available servers are (0) filesearching.com and (1) ftpsearch.elmundo.es\n" "Please type proz --help for help\n")); - exit (0); - } + case 131: + /*--timout option */ + if (setargval(optarg, &rt.itimeout) != 1) + { + /* + * The call failed due to a invalid arg + */ + printf(_("Error: Invalid arguments for the --timeout option\n" "Please type proz --help for help\n")); + exit(0); + } + break; - break; + case 132: + /* --no-getch option */ + rt.dont_prompt = TRUE; + break; + + case 133: + /* --debug option */ + rt.debug_mode = TRUE; + rt.libdebug_mode = TRUE; + break; + + case 'v': + /* --verbose option */ + rt.quiet_mode = FALSE; + break; + + case 's': + /* --ftpsearch option */ + rt.ftp_search = TRUE; + break; + + case 135: + /* --no-search option */ + rt.ftp_search = FALSE; + break; + + case 136: + /* --pt option */ + if (setargval(optarg, &rt.max_ping_wait) != 1) + { + /* + * The call failed due to a invalid arg + */ + printf(_("Error: Invalid arguments for the --pt option\n" "Please type proz --help for help\n")); + exit(0); + } + + if (rt.max_ping_wait == 0) + { + printf(_("Hey! Does waiting for a server response for Zero(0)" " seconds make and sense to you!?\n" "Please type proz --help for help\n")); + exit(0); + } + + break; + + case 137: + /* --pao option */ + if (setargval(optarg, &rt.max_simul_pings) != 1) + { + /* + * The call failed due to a invalid arg + */ + printf(_("Error: Invalid arguments for the --pao option\n" "Please type proz --help for help\n")); + exit(0); + } + + if (rt.max_simul_pings == 0) + { + printf(_("Hey you! Will pinging Zero(0) servers at once" " achive anything for me!?\n" "Please type proz --help for help\n")); + exit(0); + } + + break; + + case 138: + /* --max-ftp-servers option */ + if (setargval(optarg, &rt.ftps_mirror_req_n) != 1) + { + /* + * The call failed due to a invalid arg + */ + printf(_("Error: Invalid arguments for the --pao option\n" "Please type proz --help for help\n")); + exit(0); + } + + if (rt.ftps_mirror_req_n == 0) + { + printf(_("Hey! Will requesting Zero(0) servers at once" "from the ftpearch achive anything!?\n" "Please type proz --help for help\n")); + exit(0); + } + + break; + + case 139: + /* --max-bps */ + if (setlongargval(optarg, &rt.max_bps_per_dl) != 1) + { + /* + * The call failed due to a invalid arg + */ + printf(_("Error: Invalid arguments for the --max-bps option\n" "Please type proz --help for help\n")); + exit(0); + } + break; + + case 140: + rt.display_mode = DISP_STDOUT; + break; + + case 141: + /* --min-size */ + if (setlongargval(optarg, &rt.min_search_size) != 1) + { + /* + * The call failed due to a invalid arg + */ + printf(_("Error: Invalid arguments for the --min-size option\n" "Please type proz --help for help\n")); + exit(0); + } + break; + + case 142: + /* --ftpsid */ + + if (setargval(optarg, &rt.ftpsearch_server_id) != 1) + { + /* + * The call failed due to a invalid arg + */ + printf(_("Error: Invalid arguments for the --ftpsid option\n" "Please type proz --help for help\n")); + exit(0); + } + + if (rt.ftpsearch_server_id < 0 || rt.ftpsearch_server_id > 1) + { + printf(_("The available servers are (0) filesearching.com and (1) ftpsearch.elmundo.es\n" "Please type proz --help for help\n")); + exit(0); + } + + break; - default: - printf (_("Error: Invalid option\n")); - exit (0); - } - } + default: + printf(_("Error: Invalid option\n")); + exit(0); + } + } - set_runtime_values (); //tell libprozilla about any changed settings + set_runtime_values(); //tell libprozilla about any changed settings - if (optind == argc) - { - help (); - } - else - { - /* Gettext stuff */ - setlocale (LC_ALL, ""); - bindtextdomain (PACKAGE, LOCALEDIR); - textdomain (PACKAGE); + if (optind == argc) + { + help(); + } + else + { + /* Gettext stuff */ + setlocale(LC_ALL, ""); + bindtextdomain(PACKAGE, LOCALEDIR); + textdomain(PACKAGE); - /*delete the ~/.prozilla/debug.log file if present at the start of each run */ - proz_debug_delete_log (); + /*delete the ~/.prozilla/debug.log file if present at the start of each run */ + proz_debug_delete_log(); - if (rt.display_mode == DISP_CURSES) - initCurses(); - - /* we will now see whether the user has specfied any urls in the command line and add them */ - for (int i = optind; i < argc; i++) - { - uerr_t err; - urlinfo *url_data; - url_data = (urlinfo *) malloc (sizeof (urlinfo)); - memset (url_data, 0, sizeof (urlinfo)); + if (rt.display_mode == DISP_CURSES) + initCurses(); - //parses and validates the command-line parm - err = proz_parse_url (argv[i], url_data, 0); - if (err != URLOK) - { - PrintMessage (_ - ("%s does not seem to be a valid URL"), - argv[optind]); - proz_debug - ("%s does not seem to be a valid URL", - argv[optind]); - if(url_data) - free(url_data); - shutdown(); - exit (0); - } + /* we will now see whether the user has specfied any urls in the command line and add them */ + for (int i = optind; i < argc; i++) + { + uerr_t err; + urlinfo *url_data; + url_data = (urlinfo *)malloc(sizeof(urlinfo)); + memset(url_data, 0, sizeof(urlinfo)); - if (opt_file) - url_data->file=opt_file; + //parses and validates the command-line parm + err = proz_parse_url(argv[i], url_data, 0); + if (err != URLOK) + { + PrintMessage(_ + ("%s does not seem to be a valid URL"), + argv[optind]); + proz_debug + ("%s does not seem to be a valid URL", + argv[optind]); + if (url_data) + free(url_data); + shutdown(); + exit(0); + } - PrintMessage("Starting....."); - //In to %s\n",url_data->host); - // start the download - ret=open_new_dl_win (url_data, rt.ftp_search); - /*If the download failed the return -1 */ - if(ret==-1) - { - if(url_data) - free(url_data); - if(dl_win) - delete(dl_win); - shutdown(); - return -1; - } - if(dl_win) - delete(dl_win); - if(url_data) - free (url_data); - } - } + if (opt_file) + url_data->file = opt_file; + + PrintMessage("Starting....."); + //In to %s\n",url_data->host); + // start the download + ret = open_new_dl_win(url_data, rt.ftp_search); + /*If the download failed the return -1 */ + if (ret == -1) + { + if (url_data) + free(url_data); + if (dl_win) + delete (dl_win); + shutdown(); + return -1; + } + if (dl_win) + delete (dl_win); + if (url_data) + free(url_data); + } + } shutdown(); - } void shutdown(void) { - - cleanuprt (); + cleanuprt(); if (rt.display_mode == DISP_CURSES) shutdownCurses(); - - proz_shutdown (); + proz_shutdown(); } diff --git a/src/main.h b/src/main.h index 7c59dc7..8b7a615 100644 --- a/src/main.h +++ b/src/main.h @@ -10,7 +10,7 @@ //#define _(String) dgettext (PACKAGE,String) #define gettext_noop(String) (String) #ifndef HAVE_GNOME -#define N_(String) gettext_noop (String) +#define N_(String) gettext_noop(String) #endif /* Gettext */ @@ -19,27 +19,27 @@ #define PRZCONFDIR ".prozilla" - typedef enum { - RESUME - } rto; - - typedef enum { - DISP_CURSES, - DISP_STDOUT - } DISPLAYMODE; - +typedef enum { + RESUME +} rto; + +typedef enum { + DISP_CURSES, + DISP_STDOUT +} DISPLAYMODE; + struct runtime { int num_connections; int max_redirections; /* - * whether to use the netrc file + * whether to use the netrc file */ int use_netrc; int ftp_use_pasv; int max_attempts; - int retry_delay; /*delay in seconds */ + int retry_delay; /*delay in seconds */ /* - * The timeout period for the connections + * The timeout period for the connections */ struct timeval timeout; int itimeout; @@ -71,8 +71,8 @@ struct runtime { //int use_ftpsearch; int ftpsearch_server_id; //new options - int resume_mode; // - int dont_prompt; //don't prompt user, display message and die + int resume_mode; // + int dont_prompt; //don't prompt user, display message and die int display_mode; //curses, bare terminal, others... long min_search_size; //size in K }; diff --git a/src/misc.cpp b/src/misc.cpp index f3141a5..1a19362 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -4,103 +4,104 @@ int is_number(char *str) { - int i = 0; + int i = 0; - while (str[i]) + while (str[i]) { - if (isdigit(str[i]) == 0) - { - return 0; - } - i++; + if (isdigit(str[i]) == 0) + { + return 0; + } + i++; } - return 1; + return 1; } /* TODO port these functions */ char *kstrdup(const char *s) { - char *s1; + char *s1; - s1 = strdup(s); - if (!s1) + s1 = strdup(s); + if (!s1) { // die("Not enough memory to continue: strdup failed"); } - return s1; - + return s1; } /* Extracts a numurical argument from a option, - when it has been specified for example as -l=3 or, -l3 - returns 1 on success or 0 on a error (non nemrical argument etc -*/ + when it has been specified for example as -l=3 or, -l3 + returns 1 on success or 0 on a error (non nemrical argument etc + */ int setargval(char *optstr, int *num) { - if (*optstr == '=') + if (*optstr == '=') { - if (is_number(optstr + 1) == 1) - { - *num = atoi(optstr + 1); - return 1; - } else - { - return 0; - } - } else - { - if (is_number(optstr) == 1) - { - *num = atoi(optstr); - return 1; - - } else - { - return 0; - } + if (is_number(optstr + 1) == 1) + { + *num = atoi(optstr + 1); + return 1; + } + else + { + return 0; + } + } + else + { + if (is_number(optstr) == 1) + { + *num = atoi(optstr); + return 1; + } + else + { + return 0; + } } - } int setlongargval(char *optstr, long *num) { - if (*optstr == '=') + if (*optstr == '=') { - if (is_number(optstr + 1) == 1) - { - *num = atol(optstr + 1); - return 1; - } else - { - return 0; - } - } else - { - if (is_number(optstr) == 1) - { - *num = atol(optstr); - return 1; - - } else - { - return 0; - } + if (is_number(optstr + 1) == 1) + { + *num = atol(optstr + 1); + return 1; + } + else + { + return 0; + } + } + else + { + if (is_number(optstr) == 1) + { + *num = atol(optstr); + return 1; + } + else + { + return 0; + } } - } void delay_ms(int ms) { - struct timeval tv_delay; + struct timeval tv_delay; - memset(&tv_delay, 0, sizeof(tv_delay)); + memset(&tv_delay, 0, sizeof(tv_delay)); - tv_delay.tv_sec = ms / 1000; - tv_delay.tv_usec = (ms * 1000) % 1000000; + tv_delay.tv_sec = ms / 1000; + tv_delay.tv_usec = (ms * 1000) % 1000000; - if (select(0, (fd_set *) 0, (fd_set *) 0, (fd_set *) 0, &tv_delay) < 0) + if (select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &tv_delay) < 0) { - proz_debug("Warning Unable to delay\n"); + proz_debug("Warning Unable to delay\n"); } } diff --git a/src/prefs.cpp b/src/prefs.cpp index fb3e946..fe3a9c7 100644 --- a/src/prefs.cpp +++ b/src/prefs.cpp @@ -1,20 +1,20 @@ /****************************************************************************** - fltk prozilla - a front end for prozilla, a download accelerator library - Copyright (C) 2001 Kalum Somaratna + fltk prozilla - a front end for prozilla, a download accelerator library + Copyright (C) 2001 Kalum Somaratna - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ******************************************************************************/ #if HAVE_CONFIG_H @@ -31,7 +31,7 @@ #include "prefs.h" -typedef void (*prefproc) (int i, const char *const, FILE * const fp); +typedef void (*prefproc)(int i, const char *const, FILE * const fp); typedef struct prefopt_t { const char *varname; @@ -54,7 +54,7 @@ void set_output_dir(int i, const char *const val, FILE * const fp); void set_http_proxy(int i, const char *const val, FILE * const fp); void set_http_proxy_username(int i, const char *const val, - FILE * const fp); + FILE * const fp); void set_http_proxy_passwd(int i, const char *const val, FILE * const fp); void set_http_use_proxy(int i, const char *const val, FILE * const fp); void set_http_proxy_type(int i, const char *const val, FILE * const fp); @@ -75,249 +75,251 @@ void set_search_size(int i, const char *const val, FILE * const fp); prefopt_t pref_opts[] = { - {"threads", set_num_threads, 1}, - {"tries", set_max_attempts, 1}, - {"pasv", set_use_pasv, 1}, - {"retrydelay", set_retry_delay, 1}, - {"conntimeout", set_conn_timeout, 1}, - {"maxbpsperdl", set_max_bps_per_dl, 1}, - {"debug", set_debug_mode, 1}, - {"libdebug", set_libdebug_mode, 1}, - {"pragmanocache", set_http_no_cache, 1}, + { "threads", set_num_threads, 1 }, + { "tries", set_max_attempts, 1 }, + { "pasv", set_use_pasv, 1 }, + { "retrydelay", set_retry_delay, 1 }, + { "conntimeout", set_conn_timeout, 1 }, + { "maxbpsperdl", set_max_bps_per_dl, 1 }, + { "debug", set_debug_mode, 1 }, + { "libdebug", set_libdebug_mode, 1 }, + { "pragmanocache", set_http_no_cache, 1 }, // {"outputdir", set_output_dir, 1}, - {"httpproxy", set_http_proxy, 1}, - {"httpproxyuser", set_http_proxy_username, 1}, - {"httpproxypassword", set_http_proxy_passwd, 1}, - {"httpproxytype", set_http_proxy_type, 1}, - {"usehttpproxy", set_http_use_proxy, 1}, - {"ftpproxy", set_ftp_proxy, 1}, - {"ftpproxyuser", set_ftp_proxy_username, 1}, - {"ftpproxypassword", set_ftp_proxy_passwd, 1}, - {"ftpproxytype", set_ftp_proxy_type, 1}, - {"useftpproxy", set_ftp_use_proxy, 1}, - {"mirrorsreq", set_mirrors_req, 1}, - {"maxsimulpings", set_max_simul_pings, 1}, - {"maxpingwait", set_max_ping_wait, 1}, - {"defaultftpsearch", set_use_ftpsearch, 1}, - {"ftpsearchserverid", set_ftpsearch_server_id, 1}, - {"displaymode",set_display_mode, 1}, - {"minsearchsize",set_search_size, 1}, - {NULL, 0, 0} + { "httpproxy", set_http_proxy, 1 }, + { "httpproxyuser", set_http_proxy_username, 1 }, + { "httpproxypassword", set_http_proxy_passwd, 1 }, + { "httpproxytype", set_http_proxy_type, 1 }, + { "usehttpproxy", set_http_use_proxy, 1 }, + { "ftpproxy", set_ftp_proxy, 1 }, + { "ftpproxyuser", set_ftp_proxy_username, 1 }, + { "ftpproxypassword", set_ftp_proxy_passwd, 1 }, + { "ftpproxytype", set_ftp_proxy_type, 1 }, + { "useftpproxy", set_ftp_use_proxy, 1 }, + { "mirrorsreq", set_mirrors_req, 1 }, + { "maxsimulpings", set_max_simul_pings, 1 }, + { "maxpingwait", set_max_ping_wait, 1 }, + { "defaultftpsearch", set_use_ftpsearch, 1 }, + { "ftpsearchserverid", set_ftpsearch_server_id, 1 }, + { "displaymode", set_display_mode, 1 }, + { "minsearchsize", set_search_size, 1 }, + { NULL, 0, 0 } }; void set_num_threads(int i, const char *const val, FILE * const fp) { - if (fp != NULL) - { - fprintf(fp, "%d", rt.num_connections); - } else - { - rt.num_connections = atoi(val); - if (rt.num_connections <= 0 || rt.num_connections > 30) - rt.num_connections = 4; - } + { + fprintf(fp, "%d", rt.num_connections); + } + else + { + rt.num_connections = atoi(val); + if (rt.num_connections <= 0 || rt.num_connections > 30) + rt.num_connections = 4; + } } void set_max_attempts(int i, const char *const val, FILE * const fp) { if (fp != NULL) - { - fprintf(fp, "%d", rt.max_attempts); - } else - { - rt.max_attempts = atoi(val); - if (rt.max_attempts < 0) - rt.max_attempts = 0; - } + { + fprintf(fp, "%d", rt.max_attempts); + } + else + { + rt.max_attempts = atoi(val); + if (rt.max_attempts < 0) + rt.max_attempts = 0; + } } void set_retry_delay(int i, const char *const val, FILE * const fp) { if (fp != NULL) - { - fprintf(fp, "%d", rt.retry_delay); - } else - { - rt.retry_delay = atoi(val); - if (rt.retry_delay < 0) - rt.retry_delay = 15; - } + { + fprintf(fp, "%d", rt.retry_delay); + } + else + { + rt.retry_delay = atoi(val); + if (rt.retry_delay < 0) + rt.retry_delay = 15; + } } void set_conn_timeout(int i, const char *const val, FILE * const fp) { if (fp != NULL) - { - fprintf(fp, "%d", (int) rt.timeout.tv_sec); - } else - { - rt.timeout.tv_sec = atoi(val); - rt.timeout.tv_usec = 0; + { + fprintf(fp, "%d", (int)rt.timeout.tv_sec); + } + else + { + rt.timeout.tv_sec = atoi(val); + rt.timeout.tv_usec = 0; - if (rt.timeout.tv_sec < 0) - rt.timeout.tv_sec = 90; - } + if (rt.timeout.tv_sec < 0) + rt.timeout.tv_sec = 90; + } } void set_max_bps_per_dl(int i, const char *const val, FILE * const fp) { if (fp != NULL) - { - fprintf(fp, "%d", (int) rt.max_bps_per_dl); - } else - { - rt.max_bps_per_dl = atoi(val); - - if (rt.max_bps_per_dl < 0) - rt.max_bps_per_dl = 0; - } + { + fprintf(fp, "%d", (int)rt.max_bps_per_dl); + } + else + { + rt.max_bps_per_dl = atoi(val); + if (rt.max_bps_per_dl < 0) + rt.max_bps_per_dl = 0; + } } void set_use_pasv(int i, const char *const val, FILE * const fp) { - if (fp != NULL) - { - fprintf(fp, "%d", rt.ftp_use_pasv); - } else - { - rt.ftp_use_pasv = atoi(val); - } - + { + fprintf(fp, "%d", rt.ftp_use_pasv); + } + else + { + rt.ftp_use_pasv = atoi(val); + } } void set_debug_mode(int i, const char *const val, FILE * const fp) { - if (fp != NULL) - { - fprintf(fp, "%d", rt.debug_mode); - } else - { - rt.debug_mode = atoi(val); - } + { + fprintf(fp, "%d", rt.debug_mode); + } + else + { + rt.debug_mode = atoi(val); + } } void set_libdebug_mode(int i, const char *const val, FILE * const fp) { - if (fp != NULL) - { - fprintf(fp, "%d", rt.libdebug_mode); - } else - { - rt.libdebug_mode = atoi(val); - } + { + fprintf(fp, "%d", rt.libdebug_mode); + } + else + { + rt.libdebug_mode = atoi(val); + } } void set_http_no_cache(int i, const char *const val, FILE * const fp) { - if (fp != NULL) - { - fprintf(fp, "%d", rt.http_no_cache); - } else - { - rt.http_no_cache = atoi(val); - } + { + fprintf(fp, "%d", rt.http_no_cache); + } + else + { + rt.http_no_cache = atoi(val); + } } void set_output_dir(int i, const char *const val, FILE * const fp) { if (fp != NULL) - { - fprintf(fp, "%s", rt.output_dir); - } else - { - free(rt.output_dir); - rt.output_dir = strdup(val); - } - + { + fprintf(fp, "%s", rt.output_dir); + } + else + { + free(rt.output_dir); + rt.output_dir = strdup(val); + } } void set_http_proxy(int i, const char *const val, FILE * const fp) { - uerr_t err; urlinfo url_data; if (fp != NULL) - { - fprintf(fp, "%s:%d", rt.http_proxy->proxy_url.host, - rt.http_proxy->proxy_url.port); - } else - { - err = proz_parse_url(val, &url_data, 0); - if (err != URLOK) { - proz_debug("%s does not seem to be a valid proxy value", val); - return; + fprintf(fp, "%s:%d", rt.http_proxy->proxy_url.host, + rt.http_proxy->proxy_url.port); + } + else + { + err = proz_parse_url(val, &url_data, 0); + if (err != URLOK) + { + proz_debug("%s does not seem to be a valid proxy value", val); + return; + } + proz_free_url(&rt.http_proxy->proxy_url, 0); + memcpy(&rt.http_proxy->proxy_url, &url_data, sizeof(url_data)); } - proz_free_url(&rt.http_proxy->proxy_url, 0); - memcpy(&rt.http_proxy->proxy_url, &url_data, sizeof(url_data)); - } } void set_http_proxy_username(int i, const char *const val, FILE * const fp) { - if (fp != NULL) - { - fprintf(fp, "%s", rt.http_proxy->username); - } else - { - free(rt.http_proxy->username); - rt.http_proxy->username = strdup(val); - } + { + fprintf(fp, "%s", rt.http_proxy->username); + } + else + { + free(rt.http_proxy->username); + rt.http_proxy->username = strdup(val); + } } void set_http_proxy_passwd(int i, const char *const val, FILE * const fp) { - if (fp != NULL) - { - fprintf(fp, "%s", rt.http_proxy->passwd); - } else - { - free(rt.http_proxy->passwd); - rt.http_proxy->passwd = strdup(val); - } + { + fprintf(fp, "%s", rt.http_proxy->passwd); + } + else + { + free(rt.http_proxy->passwd); + rt.http_proxy->passwd = strdup(val); + } } void set_http_proxy_type(int i, const char *const val, FILE * const fp) { - if (fp != NULL) - { - fprintf(fp, "%d", (int) rt.http_proxy->type); - } else - { - rt.http_proxy->type = (proxy_type) atoi(val); - } + { + fprintf(fp, "%d", (int)rt.http_proxy->type); + } + else + { + rt.http_proxy->type = (proxy_type)atoi(val); + } } void set_http_use_proxy(int i, const char *const val, FILE * const fp) { - if (fp != NULL) - { - fprintf(fp, "%d", rt.use_http_proxy); - } else - { - rt.use_http_proxy = atoi(val); - } + { + fprintf(fp, "%d", rt.use_http_proxy); + } + else + { + rt.use_http_proxy = atoi(val); + } } @@ -325,175 +327,174 @@ void set_http_use_proxy(int i, const char *const val, FILE * const fp) void set_ftp_proxy(int i, const char *const val, FILE * const fp) { - uerr_t err; urlinfo url_data; if (fp != NULL) - { - fprintf(fp, "%s:%d", rt.ftp_proxy->proxy_url.host, - rt.ftp_proxy->proxy_url.port); - } else - { - err = proz_parse_url(val, &url_data, 0); - if (err != URLOK) { - proz_debug("%s does not seem to be a valid proxy value", val); - return; + fprintf(fp, "%s:%d", rt.ftp_proxy->proxy_url.host, + rt.ftp_proxy->proxy_url.port); + } + else + { + err = proz_parse_url(val, &url_data, 0); + if (err != URLOK) + { + proz_debug("%s does not seem to be a valid proxy value", val); + return; + } + proz_free_url(&rt.ftp_proxy->proxy_url, 0); + memcpy(&rt.ftp_proxy->proxy_url, &url_data, sizeof(url_data)); } - proz_free_url(&rt.ftp_proxy->proxy_url, 0); - memcpy(&rt.ftp_proxy->proxy_url, &url_data, sizeof(url_data)); - } } void set_ftp_proxy_username(int i, const char *const val, FILE * const fp) { - if (fp != NULL) - { - fprintf(fp, "%s", rt.ftp_proxy->username); - } else - { - free(rt.ftp_proxy->username); - rt.ftp_proxy->username = strdup(val); - } + { + fprintf(fp, "%s", rt.ftp_proxy->username); + } + else + { + free(rt.ftp_proxy->username); + rt.ftp_proxy->username = strdup(val); + } } void set_ftp_proxy_passwd(int i, const char *const val, FILE * const fp) { - if (fp != NULL) - { - fprintf(fp, "%s", rt.ftp_proxy->passwd); - } else - { - free(rt.ftp_proxy->passwd); - rt.ftp_proxy->passwd = strdup(val); - } + { + fprintf(fp, "%s", rt.ftp_proxy->passwd); + } + else + { + free(rt.ftp_proxy->passwd); + rt.ftp_proxy->passwd = strdup(val); + } } void set_ftp_proxy_type(int i, const char *const val, FILE * const fp) { - if (fp != NULL) - { - fprintf(fp, "%d", (int) rt.ftp_proxy->type); - } else - { - rt.ftp_proxy->type = (proxy_type) atoi(val); - } + { + fprintf(fp, "%d", (int)rt.ftp_proxy->type); + } + else + { + rt.ftp_proxy->type = (proxy_type)atoi(val); + } } void set_ftp_use_proxy(int i, const char *const val, FILE * const fp) { - if (fp != NULL) - { - fprintf(fp, "%d", rt.use_ftp_proxy); - } else - { - rt.use_ftp_proxy = atoi(val); - } + { + fprintf(fp, "%d", rt.use_ftp_proxy); + } + else + { + rt.use_ftp_proxy = atoi(val); + } } void set_mirrors_req(int i, const char *const val, FILE * const fp) { - if (fp != NULL) - { - fprintf(fp, "%d", rt.ftps_mirror_req_n); - } else - { - rt.ftps_mirror_req_n = atoi(val); - if (rt.ftps_mirror_req_n <= 0 || rt.ftps_mirror_req_n> 1000) - rt.ftps_mirror_req_n = 40; - } + { + fprintf(fp, "%d", rt.ftps_mirror_req_n); + } + else + { + rt.ftps_mirror_req_n = atoi(val); + if (rt.ftps_mirror_req_n <= 0 || rt.ftps_mirror_req_n > 1000) + rt.ftps_mirror_req_n = 40; + } } void set_max_simul_pings(int i, const char *const val, FILE * const fp) { - if (fp != NULL) - { - fprintf(fp, "%d", rt.max_simul_pings); - } else - { - rt.max_simul_pings= atoi(val); - if (rt.max_simul_pings <= 0 || rt.max_simul_pings> 30) - rt.max_simul_pings=5; - } + { + fprintf(fp, "%d", rt.max_simul_pings); + } + else + { + rt.max_simul_pings = atoi(val); + if (rt.max_simul_pings <= 0 || rt.max_simul_pings > 30) + rt.max_simul_pings = 5; + } } void set_max_ping_wait(int i, const char *const val, FILE * const fp) { - if (fp != NULL) - { - fprintf(fp, "%d", rt.max_ping_wait); - } else - { - rt.max_ping_wait= atoi(val); - if (rt.max_ping_wait <= 0 || rt.max_ping_wait> 30) - rt.max_ping_wait=5; - } + { + fprintf(fp, "%d", rt.max_ping_wait); + } + else + { + rt.max_ping_wait = atoi(val); + if (rt.max_ping_wait <= 0 || rt.max_ping_wait > 30) + rt.max_ping_wait = 5; + } } void set_use_ftpsearch(int i, const char *const val, FILE * const fp) { - if (fp != NULL) - { - fprintf(fp, "%d", rt.ftp_search); - } else - { - rt.ftp_search = atoi(val); - } - + { + fprintf(fp, "%d", rt.ftp_search); + } + else + { + rt.ftp_search = atoi(val); + } } void set_ftpsearch_server_id(int i, const char *const val, FILE * const fp) { if (fp != NULL) - { - fprintf(fp, "%d", rt.ftpsearch_server_id); - } else - { - rt.ftpsearch_server_id = atoi(val); - if (rt.ftpsearch_server_id < 0) - rt.ftpsearch_server_id = 0; - else if (rt.ftpsearch_server_id > 1) - rt.ftpsearch_server_id = 1; - - } + { + fprintf(fp, "%d", rt.ftpsearch_server_id); + } + else + { + rt.ftpsearch_server_id = atoi(val); + if (rt.ftpsearch_server_id < 0) + rt.ftpsearch_server_id = 0; + else if (rt.ftpsearch_server_id > 1) + rt.ftpsearch_server_id = 1; + } } void set_display_mode(int i, const char *const val, FILE * const fp) { - if (fp != NULL) - { - fprintf(fp, "%d", rt.display_mode); - } else - { - rt.display_mode = atoi(val); - } + { + fprintf(fp, "%d", rt.display_mode); + } + else + { + rt.display_mode = atoi(val); + } } void set_search_size(int i, const char *const val, FILE * const fp) { - if (fp != NULL) - { - fprintf(fp, "%ld", rt.min_search_size); - } else - { - rt.min_search_size = atoi(val); - } + { + fprintf(fp, "%ld", rt.min_search_size); + } + else + { + rt.min_search_size = atoi(val); + } } void save_prefs() @@ -503,24 +504,24 @@ void save_prefs() int i; snprintf(config_fname, PATH_MAX, "%s/.prozilla/%s", rt.home_dir, - "prozconfig"); + "prozconfig"); if ((fp = fopen(config_fname, "wt")) == NULL) - { - perror("could not save preferences file"); - proz_debug("could not save preferences file :- %s", strerror(errno)); - return; - } + { + perror("could not save preferences file"); + proz_debug("could not save preferences file :- %s", strerror(errno)); + return; + } fprintf(fp, "%s", - "# ProZilla preferences file\n# This file is loaded and OVERWRITTEN each time ProZilla is run.\n# Please try to avoid writing to this file.\n#\n"); + "# ProZilla preferences file\n# This file is loaded and OVERWRITTEN each time ProZilla is run.\n# Please try to avoid writing to this file.\n#\n"); for (i = 0; pref_opts[i].varname != NULL; i++) - { - fprintf(fp, "%s=", pref_opts[i].varname); - (*pref_opts[i].proc) (i, NULL, fp); - fprintf(fp, "\n"); - } + { + fprintf(fp, "%s=", pref_opts[i].varname); + (*pref_opts[i].proc)(i, NULL, fp); + fprintf(fp, "\n"); + } fclose(fp); } @@ -533,44 +534,43 @@ void load_prefs() char *tok1, *tok2; snprintf(config_fname, PATH_MAX, "%s/.prozilla/%s", rt.home_dir, - "prozconfig"); + "prozconfig"); if ((fp = fopen(config_fname, "rt")) == NULL) - { - - if (errno == ENOENT) /*Create the file then if it doesnt exist */ { - save_prefs(); - return; - } + if (errno == ENOENT) /*Create the file then if it doesnt exist */ + { + save_prefs(); + return; + } - else - { - perror(_("could not open preferences file for reading")); - proz_debug("could not open preferences file :- %s", strerror(errno)); - return; + else + { + perror(_("could not open preferences file for reading")); + proz_debug("could not open preferences file :- %s", strerror(errno)); + return; + } } - } line[sizeof(line) - 1] = '\0'; while (fgets(line, sizeof(line) - 1, fp) != NULL) - { - tok1 = strtok(line, " =\t\r\n"); - if ((tok1 == NULL) || (tok1[0] == '#')) - continue; - tok2 = strtok(NULL, "\r\n"); - if (tok2 == NULL) - continue; - - for (i = 0; pref_opts[i].varname != NULL; i++) { - if (strcmp(tok1, pref_opts[i].varname) == 0) - { - if (pref_opts[i].proc != NULL) - (*pref_opts[i].proc) (i, tok2, NULL); - } + tok1 = strtok(line, " =\t\r\n"); + if ((tok1 == NULL) || (tok1[0] == '#')) + continue; + tok2 = strtok(NULL, "\r\n"); + if (tok2 == NULL) + continue; + + for (i = 0; pref_opts[i].varname != NULL; i++) + { + if (strcmp(tok1, pref_opts[i].varname) == 0) + { + if (pref_opts[i].proc != NULL) + (*pref_opts[i].proc)(i, tok2, NULL); + } + } } - } fclose(fp); } diff --git a/src/prefs.h b/src/prefs.h index 223fb0c..df0e47d 100644 --- a/src/prefs.h +++ b/src/prefs.h @@ -1,20 +1,20 @@ /****************************************************************************** - fltk prozilla - a front end for prozilla, a download accelerator library - Copyright (C) 2001 Kalum Somaratna + fltk prozilla - a front end for prozilla, a download accelerator library + Copyright (C) 2001 Kalum Somaratna - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ******************************************************************************/ #ifndef PREFS_H #define PREFS_H