diff -uwNr aria2-0.9.0.orig/configure.ac aria2-0.9.0/configure.ac --- aria2-0.9.0.orig/configure.ac 2006-11-08 08:43:56.000000000 -0800 +++ aria2-0.9.0/configure.ac 2006-11-12 19:25:40.000000000 -0800 @@ -3,11 +3,22 @@ # AC_PREREQ(2.59) AC_INIT(aria2c, 0.9.0, tujikawa@rednoah.com) + +AC_CANONICAL_HOST +AC_CANONICAL_SYSTEM + AM_INIT_AUTOMAKE() -AM_PATH_CPPUNIT(1.10.2) +dnl AM_PATH_CPPUNIT(1.10.2) AC_CONFIG_SRCDIR([src/Socket.h]) AC_CONFIG_HEADERS([config.h]) +case "$target" in + *mingw*|*cygwin*) + WINSOCK_LIBS="-lws2_32" + AC_SUBST(WINSOCK_LIBS) + ;; +esac + # Set localedir localedir=${datadir}/locale AC_SUBST(localedir) @@ -99,7 +110,7 @@ # Checks for header files. AC_FUNC_ALLOCA #AC_HEADER_STDC -AC_CHECK_HEADERS([argz.h arpa/inet.h fcntl.h inttypes.h langinfo.h libintl.h limits.h locale.h malloc.h netdb.h netinet/in.h stddef.h stdint.h stdio_ext.h stdlib.h string.h strings.h sys/param.h sys/socket.h sys/time.h unistd.h]) +AC_CHECK_HEADERS([winsock2.h ws2tcpip.h argz.h arpa/inet.h fcntl.h inttypes.h langinfo.h libgen.h libintl.h limits.h locale.h malloc.h netdb.h netinet/in.h stddef.h stdint.h stdio_ext.h stdlib.h string.h strings.h sys/param.h sys/socket.h sys/time.h unistd.h]) # Checks for typedefs, structures, and compiler characteristics. AC_HEADER_STDBOOL @@ -122,7 +133,28 @@ AC_FUNC_SELECT_ARGTYPES AC_FUNC_STAT AC_FUNC_VPRINTF -AC_CHECK_FUNCS([__argz_count __argz_next __argz_stringify ftruncate getcwd getpagesize gettimeofday inet_ntoa memchr mempcpy memset mkdir munmap nl_langinfo rmdir select setlocale socket stpcpy strcasecmp strchr strcspn strdup strerror strstr strtol strtoul]) +AC_CHECK_FUNCS([__argz_count __argz_next __argz_stringify basename ftruncate getaddrinfo getcwd getpagesize gettimeofday inet_aton inet_ntoa memchr mempcpy memset mkdir munmap nl_langinfo rmdir select setlocale socket stpcpy strcasecmp strchr strcspn strdup strerror strstr strtol strtoul]) + +if test "x$HAVE_INET_ATON" = "x"; then + AM_CONDITIONAL([HAVE_INET_ATON], false) +else + AM_CONDITIONAL([HAVE_INET_ATON], true) +fi + +case "$target" in + *mingw*) + dnl MinGW has getaddrinfo() in ws2tcpip.h + AM_CONDITIONAL([HAVE_GETADDRINFO], true) + ;; + *) + if test "x$HAVE_GETADDRINFO" = "x"; then + AM_CONDITIONAL([HAVE_GETADDRINFO], false) + else + AM_CONDITIONAL([HAVE_GETADDRINFO], true) + fi + ;; +esac + AC_CONFIG_FILES([Makefile src/Makefile test/Makefile diff -uwNr aria2-0.9.0.orig/reconf aria2-0.9.0/reconf --- aria2-0.9.0.orig/reconf 1969-12-31 16:00:00.000000000 -0800 +++ aria2-0.9.0/reconf 2006-11-09 17:47:38.000000000 -0800 @@ -0,0 +1,15 @@ +#!/bin/sh +# +# $Id$ +# +# re autoconf/automake shell script +# + +die(){ + echo "$@" ; exit +} + +aclocal -I . -I m4 || die "The command 'aclocal -I .' failed" +autoheader || die "The command 'autoheader' failed" +automake || die "The command 'automake' failed" +autoconf || die "The command 'autoconf' failed" diff -uwNr aria2-0.9.0.orig/src/BitfieldMan.cc aria2-0.9.0/src/BitfieldMan.cc --- aria2-0.9.0.orig/src/BitfieldMan.cc 2006-11-05 07:04:18.000000000 -0800 +++ aria2-0.9.0/src/BitfieldMan.cc 2006-11-12 16:49:46.000000000 -0800 @@ -106,7 +106,11 @@ BitfieldMan::getMissingIndexRandomly(const unsigned char* bitfield, int bitfieldLength) const { +#ifdef __MINGW32__ + int byte = (int)(((double)bitfieldLength)*rand()/(RAND_MAX+1.0)); +#else int byte = (int)(((double)bitfieldLength)*random()/(RAND_MAX+1.0)); +#endif unsigned char lastMask = 0; int lastByteLength = totalLength%(blockLength*8); diff -uwNr aria2-0.9.0.orig/src/DefaultBtContext.cc aria2-0.9.0/src/DefaultBtContext.cc --- aria2-0.9.0.orig/src/DefaultBtContext.cc 2006-11-05 07:04:18.000000000 -0800 +++ aria2-0.9.0/src/DefaultBtContext.cc 2006-11-09 18:28:54.000000000 -0800 @@ -41,6 +41,36 @@ #include "ShaVisitor.h" #include "Util.h" +#ifdef HAVE_LIBGEN_H +#include "libgen.h" +#endif + +/****************************************************************************/ +#ifndef HAVE_BASENAME +/****************************************************************************/ + +/* per http://www.scit.wlv.ac.uk/cgi-bin/mansec?3C+basename */ +static char* basename(char* s) { + char* rv; + + if (!s || !*s) + return "."; + + rv = s + strlen(s) - 1; + + do { + if (*rv == '/' || *rv == '\\') + return rv + 1; + --rv; + } while (rv >= s); + + return s; +} + +#define HAVE_BASENAME 1 + +#endif /* ! HAVE_BASENAME */ + DefaultBtContext::DefaultBtContext() {} DefaultBtContext::~DefaultBtContext() {} diff -uwNr aria2-0.9.0.orig/src/DownloadCommand.cc aria2-0.9.0/src/DownloadCommand.cc --- aria2-0.9.0.orig/src/DownloadCommand.cc 2006-11-08 08:25:38.000000000 -0800 +++ aria2-0.9.0/src/DownloadCommand.cc 2006-11-12 16:32:36.000000000 -0800 @@ -42,6 +42,11 @@ #include "prefs.h" #include +#ifdef __MINGW32__ +# include +# define usleep _sleep +#endif + DownloadCommand::DownloadCommand(int cuid, const RequestHandle req, DownloadEngine* e, diff -uwNr aria2-0.9.0.orig/src/DownloadEngine.h aria2-0.9.0/src/DownloadEngine.h --- aria2-0.9.0.orig/src/DownloadEngine.h 2006-09-21 08:31:24.000000000 -0700 +++ aria2-0.9.0/src/DownloadEngine.h 2006-11-12 16:29:44.000000000 -0800 @@ -35,6 +35,12 @@ #ifndef _D_DOWNLOAD_ENGINE_H_ #define _D_DOWNLOAD_ENGINE_H_ +#ifdef __MINGW32__ +# include +# undef ERROR +# include +#endif + #include "Command.h" #include "Socket.h" #include "SegmentMan.h" diff -uwNr aria2-0.9.0.orig/src/File.cc aria2-0.9.0/src/File.cc --- aria2-0.9.0.orig/src/File.cc 2006-11-05 07:04:18.000000000 -0800 +++ aria2-0.9.0/src/File.cc 2006-11-12 16:45:56.000000000 -0800 @@ -98,14 +98,18 @@ if(Util::startsWith(name, "/")) { accDir = "/"; } - mode_t mode = S_IRUSR|S_IWUSR|S_IXUSR; for(Strings::const_iterator itr = dirs.begin(); itr != dirs.end(); itr++, accDir += "/") { accDir += *itr; if(File(accDir).isDir()) { continue; } +#ifdef __MINGW32__ + if(mkdir(accDir.c_str()) == -1) { +#else + mode_t mode = S_IRUSR|S_IWUSR|S_IXUSR; if(mkdir(accDir.c_str(), mode) == -1) { +#endif return false; } } diff -uwNr aria2-0.9.0.orig/src/Makefile.am aria2-0.9.0/src/Makefile.am --- aria2-0.9.0.orig/src/Makefile.am 2006-11-05 07:04:18.000000000 -0800 +++ aria2-0.9.0/src/Makefile.am 2006-11-12 17:15:52.000000000 -0800 @@ -150,11 +150,19 @@ MetalinkRequestInfo.cc MetalinkRequestInfo.h endif # ENABLE_METALINK +if !HAVE_GETADDRINFO +SRCS += getaddrinfo.c getaddrinfo.h +endif # !HAVE_GETADDRINFO + +if !HAVE_INET_ATON +SRCS += inet_aton.c inet_aton.h +endif # !HAVE_INET_ATON + noinst_LIBRARIES = libaria2c.a libaria2c_a_SOURCES = $(SRCS) aria2c_LDADD = libaria2c.a @LIBINTL@ @ALLOCA@ @LIBGNUTLS_LIBS@\ @LIBGCRYPT_LIBS@ @OPENSSL_LIBS@ @XML_LIBS@ @LIBARES_LIBS@\ - @LIBCARES_LIBS@ + @LIBCARES_LIBS@ @WINSOCK_LIBS@ #aria2c_LDFLAGS = -pg AM_CPPFLAGS = -Wall\ -I../lib -I../intl -I$(top_srcdir)/intl\ diff -uwNr aria2-0.9.0.orig/src/SimpleLogger.cc aria2-0.9.0/src/SimpleLogger.cc --- aria2-0.9.0.orig/src/SimpleLogger.cc 2006-09-21 08:31:24.000000000 -0700 +++ aria2-0.9.0/src/SimpleLogger.cc 2006-11-12 16:42:14.000000000 -0800 @@ -106,7 +106,12 @@ } time_t now = time(NULL); char datestr[26]; +#ifdef __MINGW32__ + char *p = ctime(&now); + strcpy(datestr, p); +#else ctime_r(&now, datestr); +#endif datestr[strlen(datestr)-1] = '\0'; writeHeader(file, datestr, levelStr); vfprintf(file, string(Util::replace(msg, "\r", "")+"\n").c_str(), ap); diff -uwNr aria2-0.9.0.orig/src/SocketCore.cc aria2-0.9.0/src/SocketCore.cc --- aria2-0.9.0.orig/src/SocketCore.cc 2006-09-21 08:31:24.000000000 -0700 +++ aria2-0.9.0/src/SocketCore.cc 2006-11-12 19:48:14.000000000 -0800 @@ -32,19 +32,54 @@ * files in the program, then also delete it here. */ /* copyright --> */ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif // HAVE_CONFIG_H + +#ifdef __MINGW32__ +# define _WIN32_WINNT 0x501 +# include +# undef ERROR +# include +# include +# define SOCKOPT_T const char +# ifndef O_NONBLOCK +# define O_NONBLOCK O_NDELAY +# endif +# define EINPROGRESS WSAEINPROGRESS +#else +# define SOCKOPT_T socklen_t +# ifndef HAVE_GETADDRINFO +# include "getaddrinfo.h" +# endif // HAVE_GETADDRINFO +#endif // __MINGW32__ + +#ifndef HAVE_INET_ATON +# include "inet_aton.h" +#endif // HAVE_INET_ATON + +#ifdef HAVE_NETDB_H +# include +#endif // HAVE_NETDB_H +#ifdef HAVE_SYS_SOCKET_H +# include +#endif // HAVE_SYS_SOCKET_H +#ifdef HAVE_NETINET_IN_H +# include +#endif // HAVE_NETINET_IN_H +#ifdef HAVE_ARPA_INET_H +# include +#endif // HAVE_ARPA_INET_H + #include "SocketCore.h" #include "DlRetryEx.h" #include "DlAbortEx.h" #include "message.h" #include #include -#include #include -#include -#include -#include #include -#include #include SocketCore::SocketCore():sockfd(-1) { @@ -79,6 +114,31 @@ #endif // HAVE_LIBGNUTLS } +#ifdef __MINGW32__ + +static char *mingw_strerror(int err) { + err = WSAGetLastError(); + static char buf[2048]; + if (FormatMessage( + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + err, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language + (LPTSTR) &buf, + sizeof(buf), + NULL + ) == 0) { + snprintf(buf, sizeof(buf), _("Unknown socket error %d"), err); + } + return buf; +} + +#define strerror mingw_strerror +#define gai_strerror mingw_strerror + +#endif // __MINGW32__ + void SocketCore::beginListen(int port) { closeConnection(); //sockfd = socket(AF_UNSPEC, SOCK_STREAM, PF_UNSPEC); @@ -86,7 +146,7 @@ if(sockfd == -1) { throw new DlAbortEx(EX_SOCKET_OPEN, strerror(errno)); } - socklen_t sockopt = 1; + SOCKOPT_T sockopt = 1; if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof(socklen_t)) < 0) { close(sockfd); sockfd = -1; @@ -151,7 +211,7 @@ if(sockfd == -1) { throw new DlAbortEx(EX_SOCKET_OPEN, strerror(errno)); } - socklen_t sockopt = 1; + SOCKOPT_T sockopt = 1; if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof(socklen_t)) < 0) { close(sockfd); sockfd = -1; @@ -188,15 +248,25 @@ } void SocketCore::setNonBlockingMode() const { +#ifdef __MINGW32__ + u_long flag = 0; + ::ioctlsocket(sockfd, FIONBIO, &flag); +#else int flags = fcntl(sockfd, F_GETFL, 0); // TODO add error handling fcntl(sockfd, F_SETFL, flags|O_NONBLOCK); +#endif } void SocketCore::setBlockingMode() const { +#ifdef __MINGW32__ + u_long flag = 1; + ::ioctlsocket(sockfd, FIONBIO, &flag); +#else int flags = fcntl(sockfd, F_GETFL, 0); // TODO add error handling fcntl(sockfd, F_SETFL, flags&(~O_NONBLOCK)); +#endif } void SocketCore::closeConnection() { diff -uwNr aria2-0.9.0.orig/src/Util.cc aria2-0.9.0/src/Util.cc --- aria2-0.9.0.orig/src/Util.cc 2006-11-05 07:04:18.000000000 -0800 +++ aria2-0.9.0/src/Util.cc 2006-11-12 17:20:18.000000000 -0800 @@ -32,6 +32,32 @@ * files in the program, then also delete it here. */ /* copyright --> */ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef __MINGW32__ +# define _WIN32_WINNT 0x501 +# include +# undef ERROR +# include +#endif + +#ifndef HAVE_INET_ATON +# include "inet_aton.h" +#endif + +#ifdef HAVE_SYS_SOCKET_H +# include +#endif +#ifdef HAVE_NETINET_IN_H +# include +#endif +#ifdef HAVE_ARPA_INET_H +# include +#endif + #include "Util.h" #include "DlAbortEx.h" #include "File.h" @@ -41,10 +67,6 @@ #include #include #include -#include -#include -#include -#include #include string Util::itos(int value, bool comma) { @@ -553,7 +575,11 @@ string Util::randomAlpha(int length) { string str; for(int i = 0; i < length; i++) { +#ifdef __MINGW32__ + int index = (int)(((double)52)*rand()/(RAND_MAX+1.0)); +#else int index = (int)(((double)52)*random()/(RAND_MAX+1.0)); +#endif char ch; if(index < 26) { ch = (char)('A'+index); diff -uwNr aria2-0.9.0.orig/src/getaddrinfo.c aria2-0.9.0/src/getaddrinfo.c --- aria2-0.9.0.orig/src/getaddrinfo.c 1969-12-31 16:00:00.000000000 -0800 +++ aria2-0.9.0/src/getaddrinfo.c 2006-11-12 09:52:56.000000000 -0800 @@ -0,0 +1,608 @@ +/* + * Copyright (c) 2001, 02 Motoyuki Kasahara + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the project nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +/* + * This program provides getaddrinfo() and getnameinfo() described in + * RFC2133, 2553 and 3493. These functions are mainly used for IPv6 + * application to resolve hostname or address. + * + * This program is designed to be working on traditional IPv4 systems + * which don't have those functions. Therefore, this implementation + * supports IPv4 only. + * + * This program is useful for application which should support both IPv6 + * and traditional IPv4 systems. Use genuine getaddrinfo() and getnameinfo() + * provided by system if the system supports IPv6. Otherwise, use this + * implementation. + * + * This program is intended to be used in combination with GNU Autoconf. + * + * This program also provides freeaddrinfo() and gai_strerror(). + * + * To use this program in your application, insert the following lines to + * C source files after including `sys/types.h', `sys/socket.h' and + * `netdb.h'. `getaddrinfo.h' defines `struct addrinfo' and AI_, NI_, + * EAI_ macros. + * + * #ifndef HAVE_GETADDRINFO + * #include "getaddrinfo.h" + * #endif + * + * Restriction: + * getaddrinfo() and getnameinfo() of this program are NOT thread + * safe, unless the cpp macro ENABLE_PTHREAD is defined. + */ + +/* + * Add the following code to your configure.ac (or configure.in). + * AC_C_CONST + * AC_HEADER_STDC + * AC_CHECK_HEADERS(string.h memory.h stdlib.h) + * AC_CHECK_FUNCS(memcpy) + * AC_REPLACE_FUNCS(memset) + * AC_TYPE_SOCKLEN_T + * AC_TYPE_IN_PORT_T + * AC_DECL_H_ERRNO + * + * AC_CHECK_FUNCS(getaddrinfo getnameinfo) + * if test "$ac_cv_func_getaddrinfo$ac_cv_func_getnameinfo" != yesyes ; then + * LIBOBJS="$LIBOBJS getaddrinfo.$ac_objext" + * fi + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef __MINGW32__ +# include +# undef ERROR +# include +#endif + +#ifdef HAVE_SYS_SOCKET_H +# include +#endif +#ifdef HAVE_NETINET_IN_H +# include +#endif +#ifdef HAVE_ARPA_INET_H +# include +#endif +#ifdef HAVE_NETDB_H +# include +#endif + +#include +#include + +#if defined(STDC_HEADERS) || defined(HAVE_STRING_H) +#include +#if !defined(STDC_HEADERS) && defined(HAVE_MEMORY_H) +#include +#endif /* not STDC_HEADERS and HAVE_MEMORY_H */ +#else /* not STDC_HEADERS and not HAVE_STRING_H */ +#include +#endif /* not STDC_HEADERS and not HAVE_STRING_H */ + +#ifdef HAVE_STDLIB_H +#include +#endif + +#ifdef ENABLE_PTHREAD +#include +#endif + +#ifdef ENABLE_NLS +#include +#endif + +#ifndef HAVE_MEMCPY +#define memcpy(d, s, n) bcopy((s), (d), (n)) +#ifdef __STDC__ +void *memchr(const void *, int, size_t); +int memcmp(const void *, const void *, size_t); +void *memmove(void *, const void *, size_t); +void *memset(void *, int, size_t); +#else /* not __STDC__ */ +char *memchr(); +int memcmp(); +char *memmove(); +char *memset(); +#endif /* not __STDC__ */ +#endif /* not HAVE_MEMCPY */ + +#ifndef H_ERRNO_DECLARED +extern int h_errno; +#endif + +#include "getaddrinfo.h" + +#ifdef ENABLE_NLS +#define _(string) gettext(string) +#ifdef gettext_noop +#define N_(string) gettext_noop(string) +#else +#define N_(string) (string) +#endif +#else +#define gettext(string) (string) +#define _(string) (string) +#define N_(string) (string) +#endif + +/* + * Error messages for gai_strerror(). + */ +static char *eai_errlist[] = { + N_("Success"), + + /* EAI_ADDRFAMILY */ + N_("Address family for hostname not supported"), + + /* EAI_AGAIN */ + N_("Temporary failure in name resolution"), + + /* EAI_BADFLAGS */ + N_("Invalid value for ai_flags"), + + /* EAI_FAIL */ + N_("Non-recoverable failure in name resolution"), + + /* EAI_FAMILY */ + N_("ai_family not supported"), + + /* EAI_MEMORY */ + N_("Memory allocation failure"), + + /* EAI_NONAME */ + N_("hostname nor servname provided, or not known"), + + /* EAI_OVERFLOW */ + N_("An argument buffer overflowed"), + + /* EAI_SERVICE */ + N_("servname not supported for ai_socktype"), + + /* EAI_SOCKTYPE */ + N_("ai_socktype not supported"), + + /* EAI_SYSTEM */ + N_("System error returned in errno") +}; + +/* + * Default hints for getaddrinfo(). + */ +static struct addrinfo default_hints = { + 0, PF_UNSPEC, 0, 0, 0, NULL, NULL, NULL +}; + +/* + * Mutex. + */ +#ifdef ENABLE_PTHREAD +static pthread_mutex_t gai_mutex = PTHREAD_MUTEX_INITIALIZER; +#endif + +/* + * Declaration of static functions. + */ +#ifdef __STDC__ +static int is_integer(const char *); +static int is_address(const char *); +static int itoa_length(int); +#else +static int is_integer(); +static int is_address(); +static int itoa_length(); +#endif + +/* + * gai_strerror(). + */ +const char * +gai_strerror(ecode) + int ecode; +{ + if (ecode < 0 || ecode > EAI_SYSTEM) + return _("Unknown error"); + + return gettext(eai_errlist[ecode]); +} + +/* + * freeaddrinfo(). + */ +void +freeaddrinfo(ai) + struct addrinfo *ai; +{ + struct addrinfo *next_ai; + + while (ai != NULL) { + if (ai->ai_canonname != NULL) + free(ai->ai_canonname); + if (ai->ai_addr != NULL) + free(ai->ai_addr); + next_ai = ai->ai_next; + free(ai); + ai = next_ai; + } +} + +/* + * Return 1 if the string `s' represents an integer. + */ +static int +is_integer(s) + const char *s; +{ + if (*s == '-' || *s == '+') + s++; + if (*s < '0' || '9' < *s) + return 0; + + s++; + while ('0' <= *s && *s <= '9') + s++; + + return (*s == '\0'); +} + +/* + * Return 1 if the string `s' represents an IPv4 address. + * Unlike inet_addr(), it doesn't permit malformed nortation such + * as "192.168". + */ +static int +is_address(s) + const char *s; +{ + const static char delimiters[] = {'.', '.', '.', '\0'}; + int i, j; + int octet; + + for (i = 0; i < 4; i++) { + if (*s == '0' && *(s + 1) != delimiters[i]) + return 0; + for (j = 0, octet = 0; '0' <= *s && *s <= '9' && j < 3; s++, j++) + octet = octet * 10 + (*s - '0'); + if (j == 0 || octet > 255 || *s != delimiters[i]) + return 0; + s++; + } + + return 1; +} + +/* + * Calcurate length of the string `s', where `s' is set by + * sprintf(s, "%d", n). + */ +static int +itoa_length(n) + int n; +{ + int result = 1; + + if (n < 0) { + n = -n; + result++; + } + + while (n >= 10) { + result++; + n /= 10; + } + + return result; +} + +/* + * getaddrinfo(). + */ +int +getaddrinfo(nodename, servname, hints, res) + const char *nodename; + const char *servname; + const struct addrinfo *hints; + struct addrinfo **res; +{ + struct addrinfo *head_res = NULL; + struct addrinfo *tail_res = NULL; + struct addrinfo *new_res; + struct sockaddr_in *sa_in; + struct in_addr **addr_list; + struct in_addr *addr_list_buf[2]; + struct in_addr addr_buf; + struct in_addr **ap; + struct servent *servent; + struct hostent *hostent; + const char *canonname = NULL; + in_port_t port; + int saved_h_errno; + int result = 0; + +#ifdef ENABLE_PTHREAD + pthread_mutex_lock(&gai_mutex); +#endif + + saved_h_errno = h_errno; + + if (nodename == NULL && servname == NULL) { + result = EAI_NONAME; + goto end; + } + + if (hints != NULL) { + if (hints->ai_family != PF_INET && hints->ai_family != PF_UNSPEC) { + result = EAI_FAMILY; + goto end; + } + if (hints->ai_socktype != SOCK_DGRAM + && hints->ai_socktype != SOCK_STREAM + && hints->ai_socktype != 0) { + result = EAI_SOCKTYPE; + goto end; + } + } else { + hints = &default_hints; + } + + if (servname != NULL) { + if (is_integer(servname)) + port = htons(atoi(servname)); + else { + if (hints->ai_flags & AI_NUMERICSERV) { + result = EAI_NONAME; + goto end; + } + + if (hints->ai_socktype == SOCK_DGRAM) + servent = getservbyname(servname, "udp"); + else if (hints->ai_socktype == SOCK_STREAM) + servent = getservbyname(servname, "tcp"); + else if (hints->ai_socktype == 0) + servent = getservbyname(servname, "tcp"); + else { + result = EAI_SOCKTYPE; + goto end; + } + + if (servent == NULL) { + result = EAI_SERVICE; + goto end; + } + port = servent->s_port; + } + } else { + port = htons(0); + } + + if (nodename != NULL) { + if (is_address(nodename)) { + addr_buf.s_addr = inet_addr(nodename); + addr_list_buf[0] = &addr_buf; + addr_list_buf[1] = NULL; + addr_list = addr_list_buf; + + if (hints->ai_flags & AI_CANONNAME + && !(hints->ai_flags & AI_NUMERICHOST)) { + hostent = gethostbyaddr((char *)&addr_buf, + sizeof(struct in_addr), AF_INET); + if (hostent != NULL) + canonname = hostent->h_name; + else + canonname = nodename; + } + } else { + if (hints->ai_flags & AI_NUMERICHOST) { + result = EAI_NONAME; + goto end; + } + + hostent = gethostbyname(nodename); + if (hostent == NULL) { + switch (h_errno) { + case HOST_NOT_FOUND: + case NO_DATA: + result = EAI_NONAME; + goto end; + case TRY_AGAIN: + result = EAI_AGAIN; + goto end; + default: + result = EAI_FAIL; + goto end; + } + } + addr_list = (struct in_addr **)hostent->h_addr_list; + + if (hints->ai_flags & AI_CANONNAME) + canonname = hostent->h_name; + } + } else { + if (hints->ai_flags & AI_PASSIVE) + addr_buf.s_addr = htonl(INADDR_ANY); + else + addr_buf.s_addr = htonl(0x7F000001); + addr_list_buf[0] = &addr_buf; + addr_list_buf[1] = NULL; + addr_list = addr_list_buf; + } + + for (ap = addr_list; *ap != NULL; ap++) { + new_res = (struct addrinfo *)malloc(sizeof(struct addrinfo)); + if (new_res == NULL) { + if (head_res != NULL) + freeaddrinfo(head_res); + result = EAI_MEMORY; + goto end; + } + + new_res->ai_family = PF_INET; + new_res->ai_socktype = hints->ai_socktype; + new_res->ai_protocol = hints->ai_protocol; + new_res->ai_addr = NULL; + new_res->ai_addrlen = sizeof(struct sockaddr_in); + new_res->ai_canonname = NULL; + new_res->ai_next = NULL; + + new_res->ai_addr = (struct sockaddr *) + malloc(sizeof(struct sockaddr_in)); + if (new_res->ai_addr == NULL) { + free(new_res); + if (head_res != NULL) + freeaddrinfo(head_res); + result = EAI_MEMORY; + goto end; + } + + sa_in = (struct sockaddr_in *)new_res->ai_addr; + memset(sa_in, 0, sizeof(struct sockaddr_in)); + sa_in->sin_family = PF_INET; + sa_in->sin_port = port; + memcpy(&sa_in->sin_addr, *ap, sizeof(struct in_addr)); + + if (head_res == NULL) + head_res = new_res; + else + tail_res->ai_next = new_res; + tail_res = new_res; + } + + if (canonname != NULL && head_res != NULL) { + head_res->ai_canonname = (char *)malloc(strlen(canonname) + 1); + if (head_res->ai_canonname != NULL) + strcpy(head_res->ai_canonname, canonname); + } + + *res = head_res; + + end: + h_errno = saved_h_errno; +#ifdef ENABLE_PTHREAD + pthread_mutex_unlock(&gai_mutex); +#endif + return result; +} + +/* + * getnameinfo(). + */ +int +getnameinfo(sa, salen, node, nodelen, serv, servlen, flags) + const struct sockaddr *sa; + socklen_t salen; + char *node; + socklen_t nodelen; + char *serv; + socklen_t servlen; + int flags; +{ + const struct sockaddr_in *sa_in = (const struct sockaddr_in *)sa; + struct hostent *hostent; + struct servent *servent; + char *ntoa_address; + int saved_h_errno; + int result = 0; + +#ifdef ENABLE_PTHREAD + pthread_mutex_lock(&gai_mutex); +#endif + + saved_h_errno = h_errno; + + if (sa_in->sin_family != PF_INET) { + result = EAI_FAMILY; + goto end; + } else if (node == NULL && serv == NULL) { + result = EAI_NONAME; + goto end; + } + + if (serv != NULL && servlen > 0) { + if (flags & NI_NUMERICSERV) + servent = NULL; + else if (flags & NI_DGRAM) + servent = getservbyport(sa_in->sin_port, "udp"); + else + servent = getservbyport(sa_in->sin_port, "tcp"); + + if (servent != NULL) { + if (servlen <= strlen(servent->s_name)) { + result = EAI_OVERFLOW; + goto end; + } + strcpy(serv, servent->s_name); + } else { + if (servlen <= itoa_length(ntohs(sa_in->sin_port))) { + result = EAI_OVERFLOW; + goto end; + } + sprintf(serv, "%d", ntohs(sa_in->sin_port)); + } + } + + if (node != NULL && nodelen > 0) { + if (flags & NI_NUMERICHOST) + hostent = NULL; + else { + hostent = gethostbyaddr((char *)&sa_in->sin_addr, + sizeof(struct in_addr), AF_INET); + } + if (hostent != NULL) { + if (nodelen <= strlen(hostent->h_name)) { + result = EAI_OVERFLOW; + goto end; + } + strcpy(node, hostent->h_name); + } else { + if (flags & NI_NAMEREQD) { + result = EAI_NONAME; + goto end; + } + ntoa_address = inet_ntoa(sa_in->sin_addr); + if (nodelen <= strlen(ntoa_address)) { + result = EAI_OVERFLOW; + goto end; + } + strcpy(node, ntoa_address); + } + + } + + end: + h_errno = saved_h_errno; +#ifdef ENABLE_PTHREAD + pthread_mutex_unlock(&gai_mutex); +#endif + return result; +} + diff -uwNr aria2-0.9.0.orig/src/getaddrinfo.h aria2-0.9.0/src/getaddrinfo.h --- aria2-0.9.0.orig/src/getaddrinfo.h 1969-12-31 16:00:00.000000000 -0800 +++ aria2-0.9.0/src/getaddrinfo.h 2006-11-12 17:21:02.000000000 -0800 @@ -0,0 +1,246 @@ +/* + * Copyright (c) 2001, 02 Motoyuki Kasahara + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the project nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef GETADDRINFO_H +#define GETADDRINFO_H + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif // HAVE_CONFIG_H + +#ifdef __MINGW32__ +# include +# undef ERROR +# include +#endif // __MINGW32__ + +#ifdef HAVE_SYS_SOCKET_H +# include +#endif // HAVE_SYS_SOCKET_H +#ifdef HAVE_NETDB_H +# include +#endif // HAVE_NETDB_H + +#include + +/********************************************************************/ +/* + * Undefine all the macros. + * might defines some of them. + */ +#ifdef EAI_ADDRFAMILY +#undef EAI_ADDRFAMILY +#endif +#ifdef EAI_AGAIN +#undef EAI_AGAIN +#endif +#ifdef EAI_BADFLAGS +#undef EAI_BADFLAGS +#endif +#ifdef EAI_FAIL +#undef EAI_FAIL +#endif +#ifdef EAI_FAMILY +#undef EAI_FAMILY +#endif +#ifdef EAI_MEMORY +#undef EAI_MEMORY +#endif +#ifdef EAI_NONAME +#undef EAI_NONAME +#endif +#ifdef EAI_OVERFLOW +#undef EAI_OVERFLOW +#endif +#ifdef EAI_SERVICE +#undef EAI_SERVICE +#endif +#ifdef EAI_SOCKTYPE +#undef EAI_SOCKTYPE +#endif +#ifdef EAI_SYSTEM +#undef EAI_SYSTEM +#endif + +#ifdef AI_PASSIVE +#undef AI_PASSIVE +#endif +#ifdef AI_CANONNAME +#undef AI_CANONNAME +#endif +#ifdef AI_NUMERICHOST +#undef AI_NUMERICHOST +#endif +#ifdef AI_NUMERICSERV +#undef AI_NUMERICSERV +#endif +#ifdef AI_V4MAPPED +#undef AI_V4MAPPED +#endif +#ifdef AI_ALL +#undef AI_ALL +#endif +#ifdef AI_ADDRCONFIG +#undef AI_ADDRCONFIG +#endif +#ifdef AI_DEFAULT +#undef AI_DEFAULT +#endif + +#ifdef NI_NOFQDN +#undef NI_NOFQDN +#endif +#ifdef NI_NUMERICHOST +#undef NI_NUMERICHOST +#endif +#ifdef NI_NAMEREQD +#undef NI_NAMEREQD +#endif +#ifdef NI_NUMERICSERV +#undef NI_NUMERICSERV +#endif +#ifdef NI_NUMERICSCOPE +#undef NI_NUMERICSCOPE +#endif + +#ifdef NI_DGRAM +#undef NI_DGRAM +#endif +#ifdef NI_MAXHOST +#undef NI_MAXHOST +#endif +#ifdef NI_MAXSERV +#undef NI_MAXSERV +#endif + +/* + * Fake struct and function names. + * might declares all or some of them. + */ +#if defined(HAVE_GETADDRINFO) || defined(HAVE_GETNAMEINFO) +#define addrinfo my_addrinfo +#define gai_strerror my_gai_strerror +#define freeaddrinfo my_freeaddrinfo +#define getaddrinfo my_getaddrinfo +#define getnameinfo my_getnameinfo +#endif + +/********************************************************************/ +/* + * Error codes. + */ +#define EAI_ADDRFAMILY 1 +#define EAI_AGAIN 2 +#define EAI_BADFLAGS 3 +#define EAI_FAIL 4 +#define EAI_FAMILY 5 +#define EAI_MEMORY 6 +#define EAI_NONAME 7 +#define EAI_OVERFLOW 8 +#define EAI_SERVICE 9 +#define EAI_SOCKTYPE 10 +#define EAI_SYSTEM 11 + +/* + * Flags for getaddrinfo(). + */ +#define AI_ADDRCONFIG 0x0001 +#define AI_ALL 0x0002 +#define AI_CANONNAME 0x0004 +#define AI_NUMERICHOST 0x0008 +#define AI_NUMERICSERV 0x0010 +#define AI_PASSIVE 0x0020 +#define AI_V4MAPPED 0x0040 +#define AI_DEFAULT (AI_V4MAPPED | AI_ADDRCONFIG) + +/* + * Flags for getnameinfo(). + */ +#define NI_DGRAM 0x0001 +#define NI_NAMEREQD 0x0002 +#define NI_NOFQDN 0x0004 +#define NI_NUMERICHOST 0x0008 +#define NI_NUMERICSCOPE 0x0010 +#define NI_NUMERICSERV 0x0020 + +/* + * Maximum length of FQDN and servie name for getnameinfo(). + */ +#define NI_MAXHOST 1025 +#define NI_MAXSERV 32 + +/* + * Address families and Protocol families. + */ +#ifndef AF_UNSPEC +#define AF_UNSPEC AF_INET +#endif +#ifndef PF_UNSPEC +#define PF_UNSPEC PF_INET +#endif + +/* + * struct addrinfo. + */ +struct addrinfo { + int ai_flags; + int ai_family; + int ai_socktype; + int ai_protocol; + socklen_t ai_addrlen; + char *ai_canonname; + struct sockaddr *ai_addr; + struct addrinfo *ai_next; +}; + +/* + * Functions. + */ +#ifdef __STDC__ +const char *gai_strerror(int); +void freeaddrinfo(struct addrinfo *); +int getaddrinfo(const char *, const char *, const struct addrinfo *, + struct addrinfo **); +int getnameinfo(const struct sockaddr *, socklen_t, char *, + socklen_t, char *, socklen_t, int); +#else +const char *gai_strerror(); +void freeaddrinfo(); +int getaddrinfo(); +int getnameinfo(); +#endif + +#ifdef __cplusplus +}; +#endif /* __cplusplus */ + +#endif /* not GETADDRINFO_H */ diff -uwNr aria2-0.9.0.orig/src/inet_aton.c aria2-0.9.0/src/inet_aton.c --- aria2-0.9.0.orig/src/inet_aton.c 1969-12-31 16:00:00.000000000 -0800 +++ aria2-0.9.0/src/inet_aton.c 2006-11-12 18:40:12.000000000 -0800 @@ -0,0 +1,69 @@ +/* */ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif // HAVE_CONFIG_H + +#ifdef __MINGW32__ +# define _WIN32_WINNT 0x501 +# include +# undef ERROR +# include +#endif // __MINGW32__ + +#ifdef HAVE_NETDB_H +# include +#endif // HAVE_NETDB_H +#ifdef HAVE_SYS_SOCKET_H +# include +#endif // HAVE_SYS_SOCKET_H +#ifdef HAVE_NETINET_IN_H +# include +#endif // HAVE_NETINET_IN_H +#ifdef HAVE_ARPA_INET_H +# include +#endif // HAVE_ARPA_INET_H + +#include + +int inet_aton(const char *cp, struct in_addr *inp) { + unsigned long res = inet_addr(cp); + if (res == INADDR_NONE && strcmp(cp, "255.255.255.255")) + return 0; + if (inp) + inp->s_addr = res; + return 1; +} diff -uwNr aria2-0.9.0.orig/src/inet_aton.h aria2-0.9.0/src/inet_aton.h --- aria2-0.9.0.orig/src/inet_aton.h 1969-12-31 16:00:00.000000000 -0800 +++ aria2-0.9.0/src/inet_aton.h 2006-11-12 18:40:16.000000000 -0800 @@ -0,0 +1,73 @@ +/* */ + +#ifndef INET_ATON_H +#define INET_ATON_H + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif // HAVE_CONFIG_H + +#ifdef __MINGW32__ +# define _WIN32_WINNT 0x501 +# include +# undef ERROR +# include +#endif // __MINGW32__ + +#ifdef HAVE_NETDB_H +# include +#endif // HAVE_NETDB_H +#ifdef HAVE_SYS_SOCKET_H +# include +#endif // HAVE_SYS_SOCKET_H +#ifdef HAVE_NETINET_IN_H +# include +#endif // HAVE_NETINET_IN_H +#ifdef HAVE_ARPA_INET_H +# include +#endif // HAVE_ARPA_INET_H + +int inet_aton(const char *cp, struct in_addr *inp); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* INET_ATON_H */ diff -uwNr aria2-0.9.0.orig/src/main.cc aria2-0.9.0/src/main.cc --- aria2-0.9.0.orig/src/main.cc 2006-11-08 08:25:38.000000000 -0800 +++ aria2-0.9.0/src/main.cc 2006-11-12 19:49:10.000000000 -0800 @@ -32,6 +32,21 @@ * files in the program, then also delete it here. */ /* copyright --> */ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif // HAVE_CONFIG_H + +#ifdef HAVE_WINSOCK2_H +# include +# undef ERROR +# define DEV_STDOUT "con" +# define DEV_NULL "nul" +#else +# define DEV_STDOUT "/dev/stdout" +# define DEV_NULL "/dev/null" +#endif // HAVE_WINSOCK2_H + #include "HttpInitiateConnectionCommand.h" #include "ConsoleDownloadEngine.h" #include "SegmentMan.h" @@ -49,9 +64,13 @@ #include #include #include +#include #include + +#ifdef HAVE_LIBGEN_H #include -#include +#endif // HAVE_LIBGEN_H + extern char* optarg; extern int optind, opterr, optopt; #include @@ -75,11 +94,13 @@ bool timeoutSpecified; void setSignalHander(int signal, void (*handler)(int), int flags) { +#ifndef __MINGW32__ struct sigaction sigact; sigact.sa_handler = handler; sigact.sa_flags = flags; sigemptyset(&sigact.sa_mask); sigaction(signal, &sigact, NULL); +#endif } void showVersion() { @@ -703,10 +724,15 @@ } } if(op->getAsBool(PREF_DAEMON)) { +#ifdef __MINGW32__ + perror(_("Option not implemented")); + exit(EXIT_FAILURE); +#else if(daemon(1, 1) < 0) { perror(_("daemon failed")); exit(EXIT_FAILURE); } +#endif } Strings args(argv+optind, argv+argc); @@ -721,21 +747,37 @@ #ifdef ENABLE_METALINK xmlInitParser(); #endif // ENABLE_METALINK +#ifdef __MINGW32__ + srand(time(NULL)); +#else srandom(time(NULL)); +#endif if(op->getAsBool(PREF_STDOUT_LOG)) { - LogFactory::setLogFile("/dev/stdout"); + LogFactory::setLogFile(DEV_STDOUT); } else if(op->get(PREF_LOG).size()) { LogFactory::setLogFile(op->get(PREF_LOG)); } else { - LogFactory::setLogFile("/dev/null"); + LogFactory::setLogFile(DEV_NULL); } int exitStatus = EXIT_SUCCESS; + try { Logger* logger = LogFactory::getInstance(); logger->info("%s %s", PACKAGE, PACKAGE_VERSION); logger->info("Logging started."); +#ifdef HAVE_WINSOCK2_H + WSADATA wsaData; + memset((char*)&wsaData, 0, sizeof(wsaData)); + if (WSAStartup(MAKEWORD(1, 1), &wsaData)) { + logger->error(_("Windows socket library initialization failed")); + exit(EXIT_FAILURE); + } +#endif // HAVE_WINSOCK2_H + +#ifdef SIGPIPE setSignalHander(SIGPIPE, SIG_IGN, 0); +#endif RequestInfo* firstReqInfo = 0; #ifdef ENABLE_BITTORRENT @@ -798,5 +840,8 @@ xmlCleanupParser(); #endif // ENABLE_METALINK FeatureConfig::release(); +#ifdef HAVE_WINSOCK2_H + WSACleanup(); +#endif // HAVE_WINSOCK2_H return exitStatus; }