Переглянути джерело

2007-08-01 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>

	Make a2netcompat.h include a2io.h to fix compilation error:
	* src/a2netcompat.h
	* src/SocketCore.cc: Removed include of a2io.h
	* src/Util.cc: Removed include of a2io.h
	
	Gather time related functions to a2time.h:
	* src/a2time.h: New file.
	* src/DefaultPeerStorage.cc
	* src/SimpleLogger.cc
	* src/Util.{h, cc}
	* src/SimpleRandomizer.h
	* src/TimeA2.{h,cc}
	* src/DownloadCommand.cc
	* src/main.cc

	Removed #ifdef __MINGW32__ since gai_strerror is included in
	a2netcompat.h:
	* src/NameResolver.cc
	
	Fixed compilation error without openssl:
	* src/SocketCore.{h,cc}: Moved include of openssl/err.h to 
SocketCore.h

	Added default block to suppress compiler warnings:
	* src/MetalinkRequestInfo.cc (AccumulateNonP2PUrl::operator())
	
2007-07-26  Ross Smith II  <aria2spam at smithii dot com>

	MinGW build enhancements. The following files are added:
	* src/gai_strerror.{c,h}
	* src/gettimeofday.{c,h}

	Changes to support the above new files:
	* configure.ac
	* src/Makefile.am
	* src/a2netcompat.h
	* src/TimeA2.cc
	* src/DefaultPeerStorage.cc
	* src/NameResolver.cc: removed mingw_strerror() function.
	* src/SocketCore.cc: removed mingw_strerror() function.

	Miscellaneous MinGW build fixes. 
	* src/a2io.h: Use _lseeki64() instead of lseek()
	* src/common.h
	* src/DefaultFileAllocator.cc
	* src/GlowFileAllocator.cc
	* src/main.cc: Moved #include "prefs.h" to quiet compile error.
	* src/NameResolver.cc
	(callback): Changed int32_t to int.
	(resolve): Changed int32_t to int.
	* src/Platform.cc
	* src/Platform.h
	* test/MultiDiskWriterTest.cc
	* test/PeerMessageUtilTest.cc
	* src/localtime_r.c: Add DeleteCriticalSection() and at exit().

	Other enhancements and fixes.
	* src/HttpRequestCommand.cc
	(executeInternal) Use non-blocking socket for HTTPS (MinGW 
only).
	* src/SocketCore.cc: 
	(error): New function to abstract errno/WSAGetLastError().
	(errorMsg): New function to abstract errno/WSAGetLastError().
	(initiateSecureConnection): Added more detailed error reporting.
	* src/SocketCore.h: Added private variable blocking, to allow
	proper handling of OpenSSL psuedo-errors.
	* src/message.h
	(EX_SSL_INIT_FAILURE)
	(EX_SSL_IO_ERROR)
	(EX_SSL_PROTOCOL_ERROR)
	(EX_SSL_UNKNOWN_ERROR)
	(EX_SSL_CONNECT_ERROR)
	(EX_SOCKET_BLOCKING)
	(EX_SOCKET_NONBLOCKING)
	(EX_SOCKET_UNKNOWN_ERROR)
	* src/Util.cc
	(setGlobalSignalHandler): Renamed signal to sig as signal is a
	reserved name.
	(httpGMT): Fixed typo.
Tatsuhiro Tsujikawa 18 роки тому
батько
коміт
46e8c332cd

+ 80 - 0
ChangeLog

@@ -1,3 +1,83 @@
+2007-08-01  Tatsuhiro Tsujikawa  <tujikawa at rednoah dot com>
+
+	Make a2netcompat.h include a2io.h to fix compilation error:
+	* src/a2netcompat.h
+	* src/SocketCore.cc: Removed include of a2io.h
+	* src/Util.cc: Removed include of a2io.h
+	
+	Gather time related functions to a2time.h:
+	* src/a2time.h: New file.
+	* src/DefaultPeerStorage.cc
+	* src/SimpleLogger.cc
+	* src/Util.{h, cc}
+	* src/SimpleRandomizer.h
+	* src/TimeA2.{h,cc}
+	* src/DownloadCommand.cc
+	* src/main.cc
+
+	Removed #ifdef __MINGW32__ since gai_strerror is included in
+	a2netcompat.h:
+	* src/NameResolver.cc
+	
+	Fixed compilation error without openssl:
+	* src/SocketCore.{h,cc}: Moved include of openssl/err.h to SocketCore.h
+
+	Added default block to suppress compiler warnings:
+	* src/MetalinkRequestInfo.cc (AccumulateNonP2PUrl::operator())
+	
+2007-07-26  Ross Smith II  <aria2spam at smithii dot com>
+
+	MinGW build enhancements. The following files are added:
+	* src/gai_strerror.{c,h}
+	* src/gettimeofday.{c,h}
+
+	Changes to support the above new files:
+	* configure.ac
+	* src/Makefile.am
+	* src/a2netcompat.h
+	* src/TimeA2.cc
+	* src/DefaultPeerStorage.cc
+	* src/NameResolver.cc: removed mingw_strerror() function.
+	* src/SocketCore.cc: removed mingw_strerror() function.
+
+	Miscellaneous MinGW build fixes. 
+	* src/a2io.h: Use _lseeki64() instead of lseek()
+	* src/common.h
+	* src/DefaultFileAllocator.cc
+	* src/GlowFileAllocator.cc
+	* src/main.cc: Moved #include "prefs.h" to quiet compile error.
+	* src/NameResolver.cc
+	(callback): Changed int32_t to int.
+	(resolve): Changed int32_t to int.
+	* src/Platform.cc
+	* src/Platform.h
+	* test/MultiDiskWriterTest.cc
+	* test/PeerMessageUtilTest.cc
+	* src/localtime_r.c: Add DeleteCriticalSection() and at exit().
+
+	Other enhancements and fixes.
+	* src/HttpRequestCommand.cc
+	(executeInternal) Use non-blocking socket for HTTPS (MinGW only).
+	* src/SocketCore.cc: 
+	(error): New function to abstract errno/WSAGetLastError().
+	(errorMsg): New function to abstract errno/WSAGetLastError().
+	(initiateSecureConnection): Added more detailed error reporting.
+	* src/SocketCore.h: Added private variable blocking, to allow
+	proper handling of OpenSSL psuedo-errors.
+	* src/message.h
+	(EX_SSL_INIT_FAILURE)
+	(EX_SSL_IO_ERROR)
+	(EX_SSL_PROTOCOL_ERROR)
+	(EX_SSL_UNKNOWN_ERROR)
+	(EX_SSL_CONNECT_ERROR)
+	(EX_SOCKET_BLOCKING)
+	(EX_SOCKET_NONBLOCKING)
+	(EX_SOCKET_UNKNOWN_ERROR)
+	* src/Util.cc
+	(setGlobalSignalHandler): Renamed signal to sig as signal is a
+	reserved name.
+	(httpGMT): Fixed typo.
+
 2007-07-23  Tatsuhiro Tsujikawa  <tujikawa at rednoah dot com>
 
 	Merged Ross's win32 patch(manually)

+ 4 - 0
Makefile.in

@@ -130,8 +130,12 @@ GMSGFMT = @GMSGFMT@
 GREP = @GREP@
 HAVE_BASENAME_FALSE = @HAVE_BASENAME_FALSE@
 HAVE_BASENAME_TRUE = @HAVE_BASENAME_TRUE@
+HAVE_GAI_STRERROR_FALSE = @HAVE_GAI_STRERROR_FALSE@
+HAVE_GAI_STRERROR_TRUE = @HAVE_GAI_STRERROR_TRUE@
 HAVE_GETADDRINFO_FALSE = @HAVE_GETADDRINFO_FALSE@
 HAVE_GETADDRINFO_TRUE = @HAVE_GETADDRINFO_TRUE@
+HAVE_GETTIMEOFDAY_FALSE = @HAVE_GETTIMEOFDAY_FALSE@
+HAVE_GETTIMEOFDAY_TRUE = @HAVE_GETTIMEOFDAY_TRUE@
 HAVE_INET_ATON_FALSE = @HAVE_INET_ATON_FALSE@
 HAVE_INET_ATON_TRUE = @HAVE_INET_ATON_TRUE@
 HAVE_LOCALTIME_R_FALSE = @HAVE_LOCALTIME_R_FALSE@

+ 1 - 8
TODO

@@ -38,12 +38,5 @@
 
 * io related headers and compatible macros and definition -> a2io.h
 * net related headers and compatible macros and definition -> a2netcompat.h
+* time related headers and compatible macros and definition -> a2time.h
 * used globally -> common.h
-
-* Following files have copyright issue
-	libgen.c
-	libgen.h
-	inet_aton.c
-	inet_aton.h
-	localtime_r.{h,c}
-	strptime.h

+ 3 - 0
config.h.in

@@ -65,6 +65,9 @@
 /* Define to 1 if you have the `ftruncate' function. */
 #undef HAVE_FTRUNCATE
 
+/* Define to 1 if you have the `gai_strerror' function. */
+#undef HAVE_GAI_STRERROR
+
 /* Define to 1 if you have the `getaddrinfo' function. */
 #undef HAVE_GETADDRINFO
 

+ 514 - 244
configure

@@ -766,8 +766,12 @@ POSUB
 LIBOBJS
 HAVE_BASENAME_TRUE
 HAVE_BASENAME_FALSE
+HAVE_GAI_STRERROR_TRUE
+HAVE_GAI_STRERROR_FALSE
 HAVE_GETADDRINFO_TRUE
 HAVE_GETADDRINFO_FALSE
+HAVE_GETTIMEOFDAY_TRUE
+HAVE_GETTIMEOFDAY_FALSE
 HAVE_INET_ATON_TRUE
 HAVE_INET_ATON_FALSE
 HAVE_LOCALTIME_R_TRUE
@@ -10885,6 +10889,10 @@ esac
 fi
 
 
+case "$target" in
+	*mingw*)
+		;;
+	*)
 
 for ac_header in stdlib.h
 do
@@ -11120,6 +11128,244 @@ fi
 
 
 
+
+for ac_header in stdlib.h
+do
+as_ac_Header=`echo "ac_cv_header_$ac_header" | $as_tr_sh`
+if { as_var=$as_ac_Header; eval "test \"\${$as_var+set}\" = set"; }; then
+  { echo "$as_me:$LINENO: checking for $ac_header" >&5
+echo $ECHO_N "checking for $ac_header... $ECHO_C" >&6; }
+if { as_var=$as_ac_Header; eval "test \"\${$as_var+set}\" = set"; }; then
+  echo $ECHO_N "(cached) $ECHO_C" >&6
+fi
+ac_res=`eval echo '${'$as_ac_Header'}'`
+	       { echo "$as_me:$LINENO: result: $ac_res" >&5
+echo "${ECHO_T}$ac_res" >&6; }
+else
+  # Is the header compilable?
+{ echo "$as_me:$LINENO: checking $ac_header usability" >&5
+echo $ECHO_N "checking $ac_header usability... $ECHO_C" >&6; }
+cat >conftest.$ac_ext <<_ACEOF
+/* confdefs.h.  */
+_ACEOF
+cat confdefs.h >>conftest.$ac_ext
+cat >>conftest.$ac_ext <<_ACEOF
+/* end confdefs.h.  */
+$ac_includes_default
+#include <$ac_header>
+_ACEOF
+rm -f conftest.$ac_objext
+if { (ac_try="$ac_compile"
+case "(($ac_try" in
+  *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
+  *) ac_try_echo=$ac_try;;
+esac
+eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5
+  (eval "$ac_compile") 2>conftest.er1
+  ac_status=$?
+  grep -v '^ *+' conftest.er1 >conftest.err
+  rm -f conftest.er1
+  cat conftest.err >&5
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); } && {
+	 test -z "$ac_c_werror_flag" ||
+	 test ! -s conftest.err
+       } && test -s conftest.$ac_objext; then
+  ac_header_compiler=yes
+else
+  echo "$as_me: failed program was:" >&5
+sed 's/^/| /' conftest.$ac_ext >&5
+
+	ac_header_compiler=no
+fi
+
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+{ echo "$as_me:$LINENO: result: $ac_header_compiler" >&5
+echo "${ECHO_T}$ac_header_compiler" >&6; }
+
+# Is the header present?
+{ echo "$as_me:$LINENO: checking $ac_header presence" >&5
+echo $ECHO_N "checking $ac_header presence... $ECHO_C" >&6; }
+cat >conftest.$ac_ext <<_ACEOF
+/* confdefs.h.  */
+_ACEOF
+cat confdefs.h >>conftest.$ac_ext
+cat >>conftest.$ac_ext <<_ACEOF
+/* end confdefs.h.  */
+#include <$ac_header>
+_ACEOF
+if { (ac_try="$ac_cpp conftest.$ac_ext"
+case "(($ac_try" in
+  *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
+  *) ac_try_echo=$ac_try;;
+esac
+eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5
+  (eval "$ac_cpp conftest.$ac_ext") 2>conftest.er1
+  ac_status=$?
+  grep -v '^ *+' conftest.er1 >conftest.err
+  rm -f conftest.er1
+  cat conftest.err >&5
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); } >/dev/null && {
+	 test -z "$ac_c_preproc_warn_flag$ac_c_werror_flag" ||
+	 test ! -s conftest.err
+       }; then
+  ac_header_preproc=yes
+else
+  echo "$as_me: failed program was:" >&5
+sed 's/^/| /' conftest.$ac_ext >&5
+
+  ac_header_preproc=no
+fi
+
+rm -f conftest.err conftest.$ac_ext
+{ echo "$as_me:$LINENO: result: $ac_header_preproc" >&5
+echo "${ECHO_T}$ac_header_preproc" >&6; }
+
+# So?  What about this header?
+case $ac_header_compiler:$ac_header_preproc:$ac_c_preproc_warn_flag in
+  yes:no: )
+    { echo "$as_me:$LINENO: WARNING: $ac_header: accepted by the compiler, rejected by the preprocessor!" >&5
+echo "$as_me: WARNING: $ac_header: accepted by the compiler, rejected by the preprocessor!" >&2;}
+    { echo "$as_me:$LINENO: WARNING: $ac_header: proceeding with the compiler's result" >&5
+echo "$as_me: WARNING: $ac_header: proceeding with the compiler's result" >&2;}
+    ac_header_preproc=yes
+    ;;
+  no:yes:* )
+    { echo "$as_me:$LINENO: WARNING: $ac_header: present but cannot be compiled" >&5
+echo "$as_me: WARNING: $ac_header: present but cannot be compiled" >&2;}
+    { echo "$as_me:$LINENO: WARNING: $ac_header:     check for missing prerequisite headers?" >&5
+echo "$as_me: WARNING: $ac_header:     check for missing prerequisite headers?" >&2;}
+    { echo "$as_me:$LINENO: WARNING: $ac_header: see the Autoconf documentation" >&5
+echo "$as_me: WARNING: $ac_header: see the Autoconf documentation" >&2;}
+    { echo "$as_me:$LINENO: WARNING: $ac_header:     section \"Present But Cannot Be Compiled\"" >&5
+echo "$as_me: WARNING: $ac_header:     section \"Present But Cannot Be Compiled\"" >&2;}
+    { echo "$as_me:$LINENO: WARNING: $ac_header: proceeding with the preprocessor's result" >&5
+echo "$as_me: WARNING: $ac_header: proceeding with the preprocessor's result" >&2;}
+    { echo "$as_me:$LINENO: WARNING: $ac_header: in the future, the compiler will take precedence" >&5
+echo "$as_me: WARNING: $ac_header: in the future, the compiler will take precedence" >&2;}
+    ( cat <<\_ASBOX
+## ----------------------------------------------- ##
+## Report this to t-tujikawa@users.sourceforge.net ##
+## ----------------------------------------------- ##
+_ASBOX
+     ) | sed "s/^/$as_me: WARNING:     /" >&2
+    ;;
+esac
+{ echo "$as_me:$LINENO: checking for $ac_header" >&5
+echo $ECHO_N "checking for $ac_header... $ECHO_C" >&6; }
+if { as_var=$as_ac_Header; eval "test \"\${$as_var+set}\" = set"; }; then
+  echo $ECHO_N "(cached) $ECHO_C" >&6
+else
+  eval "$as_ac_Header=\$ac_header_preproc"
+fi
+ac_res=`eval echo '${'$as_ac_Header'}'`
+	       { echo "$as_me:$LINENO: result: $ac_res" >&5
+echo "${ECHO_T}$ac_res" >&6; }
+
+fi
+if test `eval echo '${'$as_ac_Header'}'` = yes; then
+  cat >>confdefs.h <<_ACEOF
+#define `echo "HAVE_$ac_header" | $as_tr_cpp` 1
+_ACEOF
+
+fi
+
+done
+
+{ echo "$as_me:$LINENO: checking for GNU libc compatible realloc" >&5
+echo $ECHO_N "checking for GNU libc compatible realloc... $ECHO_C" >&6; }
+if test "${ac_cv_func_realloc_0_nonnull+set}" = set; then
+  echo $ECHO_N "(cached) $ECHO_C" >&6
+else
+  if test "$cross_compiling" = yes; then
+  ac_cv_func_realloc_0_nonnull=no
+else
+  cat >conftest.$ac_ext <<_ACEOF
+/* confdefs.h.  */
+_ACEOF
+cat confdefs.h >>conftest.$ac_ext
+cat >>conftest.$ac_ext <<_ACEOF
+/* end confdefs.h.  */
+#if defined STDC_HEADERS || defined HAVE_STDLIB_H
+# include <stdlib.h>
+#else
+char *realloc ();
+#endif
+
+int
+main ()
+{
+return ! realloc (0, 0);
+  ;
+  return 0;
+}
+_ACEOF
+rm -f conftest$ac_exeext
+if { (ac_try="$ac_link"
+case "(($ac_try" in
+  *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
+  *) ac_try_echo=$ac_try;;
+esac
+eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5
+  (eval "$ac_link") 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); } && { ac_try='./conftest$ac_exeext'
+  { (case "(($ac_try" in
+  *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
+  *) ac_try_echo=$ac_try;;
+esac
+eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5
+  (eval "$ac_try") 2>&5
+  ac_status=$?
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); }; }; then
+  ac_cv_func_realloc_0_nonnull=yes
+else
+  echo "$as_me: program exited with status $ac_status" >&5
+echo "$as_me: failed program was:" >&5
+sed 's/^/| /' conftest.$ac_ext >&5
+
+( exit $ac_status )
+ac_cv_func_realloc_0_nonnull=no
+fi
+rm -f core *.core core.conftest.* gmon.out bb.out conftest$ac_exeext conftest.$ac_objext conftest.$ac_ext
+fi
+
+
+fi
+{ echo "$as_me:$LINENO: result: $ac_cv_func_realloc_0_nonnull" >&5
+echo "${ECHO_T}$ac_cv_func_realloc_0_nonnull" >&6; }
+if test $ac_cv_func_realloc_0_nonnull = yes; then
+
+cat >>confdefs.h <<\_ACEOF
+#define HAVE_REALLOC 1
+_ACEOF
+
+else
+  cat >>confdefs.h <<\_ACEOF
+#define HAVE_REALLOC 0
+_ACEOF
+
+   case " $LIBOBJS " in
+  *" realloc.$ac_objext "* ) ;;
+  *) LIBOBJS="$LIBOBJS realloc.$ac_objext"
+ ;;
+esac
+
+
+cat >>confdefs.h <<\_ACEOF
+#define realloc rpl_realloc
+_ACEOF
+
+fi
+
+
+
+		;;
+esac
+
 { echo "$as_me:$LINENO: checking for working memcmp" >&5
 echo $ECHO_N "checking for working memcmp... $ECHO_C" >&6; }
 if test "${ac_cv_func_memcmp_working+set}" = set; then
@@ -11618,258 +11864,23 @@ echo "$as_me: failed program was:" >&5
 sed 's/^/| /' conftest.$ac_ext >&5
 
 ( exit $ac_status )
-ac_cv_func_mmap_fixed_mapped=no
-fi
-rm -f core *.core core.conftest.* gmon.out bb.out conftest$ac_exeext conftest.$ac_objext conftest.$ac_ext
-fi
-
-
-fi
-{ echo "$as_me:$LINENO: result: $ac_cv_func_mmap_fixed_mapped" >&5
-echo "${ECHO_T}$ac_cv_func_mmap_fixed_mapped" >&6; }
-if test $ac_cv_func_mmap_fixed_mapped = yes; then
-
-cat >>confdefs.h <<\_ACEOF
-#define HAVE_MMAP 1
-_ACEOF
-
-fi
-rm -f conftest.mmap
-
-
-for ac_header in stdlib.h
-do
-as_ac_Header=`echo "ac_cv_header_$ac_header" | $as_tr_sh`
-if { as_var=$as_ac_Header; eval "test \"\${$as_var+set}\" = set"; }; then
-  { echo "$as_me:$LINENO: checking for $ac_header" >&5
-echo $ECHO_N "checking for $ac_header... $ECHO_C" >&6; }
-if { as_var=$as_ac_Header; eval "test \"\${$as_var+set}\" = set"; }; then
-  echo $ECHO_N "(cached) $ECHO_C" >&6
-fi
-ac_res=`eval echo '${'$as_ac_Header'}'`
-	       { echo "$as_me:$LINENO: result: $ac_res" >&5
-echo "${ECHO_T}$ac_res" >&6; }
-else
-  # Is the header compilable?
-{ echo "$as_me:$LINENO: checking $ac_header usability" >&5
-echo $ECHO_N "checking $ac_header usability... $ECHO_C" >&6; }
-cat >conftest.$ac_ext <<_ACEOF
-/* confdefs.h.  */
-_ACEOF
-cat confdefs.h >>conftest.$ac_ext
-cat >>conftest.$ac_ext <<_ACEOF
-/* end confdefs.h.  */
-$ac_includes_default
-#include <$ac_header>
-_ACEOF
-rm -f conftest.$ac_objext
-if { (ac_try="$ac_compile"
-case "(($ac_try" in
-  *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
-  *) ac_try_echo=$ac_try;;
-esac
-eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5
-  (eval "$ac_compile") 2>conftest.er1
-  ac_status=$?
-  grep -v '^ *+' conftest.er1 >conftest.err
-  rm -f conftest.er1
-  cat conftest.err >&5
-  echo "$as_me:$LINENO: \$? = $ac_status" >&5
-  (exit $ac_status); } && {
-	 test -z "$ac_c_werror_flag" ||
-	 test ! -s conftest.err
-       } && test -s conftest.$ac_objext; then
-  ac_header_compiler=yes
-else
-  echo "$as_me: failed program was:" >&5
-sed 's/^/| /' conftest.$ac_ext >&5
-
-	ac_header_compiler=no
-fi
-
-rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
-{ echo "$as_me:$LINENO: result: $ac_header_compiler" >&5
-echo "${ECHO_T}$ac_header_compiler" >&6; }
-
-# Is the header present?
-{ echo "$as_me:$LINENO: checking $ac_header presence" >&5
-echo $ECHO_N "checking $ac_header presence... $ECHO_C" >&6; }
-cat >conftest.$ac_ext <<_ACEOF
-/* confdefs.h.  */
-_ACEOF
-cat confdefs.h >>conftest.$ac_ext
-cat >>conftest.$ac_ext <<_ACEOF
-/* end confdefs.h.  */
-#include <$ac_header>
-_ACEOF
-if { (ac_try="$ac_cpp conftest.$ac_ext"
-case "(($ac_try" in
-  *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
-  *) ac_try_echo=$ac_try;;
-esac
-eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5
-  (eval "$ac_cpp conftest.$ac_ext") 2>conftest.er1
-  ac_status=$?
-  grep -v '^ *+' conftest.er1 >conftest.err
-  rm -f conftest.er1
-  cat conftest.err >&5
-  echo "$as_me:$LINENO: \$? = $ac_status" >&5
-  (exit $ac_status); } >/dev/null && {
-	 test -z "$ac_c_preproc_warn_flag$ac_c_werror_flag" ||
-	 test ! -s conftest.err
-       }; then
-  ac_header_preproc=yes
-else
-  echo "$as_me: failed program was:" >&5
-sed 's/^/| /' conftest.$ac_ext >&5
-
-  ac_header_preproc=no
-fi
-
-rm -f conftest.err conftest.$ac_ext
-{ echo "$as_me:$LINENO: result: $ac_header_preproc" >&5
-echo "${ECHO_T}$ac_header_preproc" >&6; }
-
-# So?  What about this header?
-case $ac_header_compiler:$ac_header_preproc:$ac_c_preproc_warn_flag in
-  yes:no: )
-    { echo "$as_me:$LINENO: WARNING: $ac_header: accepted by the compiler, rejected by the preprocessor!" >&5
-echo "$as_me: WARNING: $ac_header: accepted by the compiler, rejected by the preprocessor!" >&2;}
-    { echo "$as_me:$LINENO: WARNING: $ac_header: proceeding with the compiler's result" >&5
-echo "$as_me: WARNING: $ac_header: proceeding with the compiler's result" >&2;}
-    ac_header_preproc=yes
-    ;;
-  no:yes:* )
-    { echo "$as_me:$LINENO: WARNING: $ac_header: present but cannot be compiled" >&5
-echo "$as_me: WARNING: $ac_header: present but cannot be compiled" >&2;}
-    { echo "$as_me:$LINENO: WARNING: $ac_header:     check for missing prerequisite headers?" >&5
-echo "$as_me: WARNING: $ac_header:     check for missing prerequisite headers?" >&2;}
-    { echo "$as_me:$LINENO: WARNING: $ac_header: see the Autoconf documentation" >&5
-echo "$as_me: WARNING: $ac_header: see the Autoconf documentation" >&2;}
-    { echo "$as_me:$LINENO: WARNING: $ac_header:     section \"Present But Cannot Be Compiled\"" >&5
-echo "$as_me: WARNING: $ac_header:     section \"Present But Cannot Be Compiled\"" >&2;}
-    { echo "$as_me:$LINENO: WARNING: $ac_header: proceeding with the preprocessor's result" >&5
-echo "$as_me: WARNING: $ac_header: proceeding with the preprocessor's result" >&2;}
-    { echo "$as_me:$LINENO: WARNING: $ac_header: in the future, the compiler will take precedence" >&5
-echo "$as_me: WARNING: $ac_header: in the future, the compiler will take precedence" >&2;}
-    ( cat <<\_ASBOX
-## ----------------------------------------------- ##
-## Report this to t-tujikawa@users.sourceforge.net ##
-## ----------------------------------------------- ##
-_ASBOX
-     ) | sed "s/^/$as_me: WARNING:     /" >&2
-    ;;
-esac
-{ echo "$as_me:$LINENO: checking for $ac_header" >&5
-echo $ECHO_N "checking for $ac_header... $ECHO_C" >&6; }
-if { as_var=$as_ac_Header; eval "test \"\${$as_var+set}\" = set"; }; then
-  echo $ECHO_N "(cached) $ECHO_C" >&6
-else
-  eval "$as_ac_Header=\$ac_header_preproc"
-fi
-ac_res=`eval echo '${'$as_ac_Header'}'`
-	       { echo "$as_me:$LINENO: result: $ac_res" >&5
-echo "${ECHO_T}$ac_res" >&6; }
-
-fi
-if test `eval echo '${'$as_ac_Header'}'` = yes; then
-  cat >>confdefs.h <<_ACEOF
-#define `echo "HAVE_$ac_header" | $as_tr_cpp` 1
-_ACEOF
-
-fi
-
-done
-
-{ echo "$as_me:$LINENO: checking for GNU libc compatible realloc" >&5
-echo $ECHO_N "checking for GNU libc compatible realloc... $ECHO_C" >&6; }
-if test "${ac_cv_func_realloc_0_nonnull+set}" = set; then
-  echo $ECHO_N "(cached) $ECHO_C" >&6
-else
-  if test "$cross_compiling" = yes; then
-  ac_cv_func_realloc_0_nonnull=no
-else
-  cat >conftest.$ac_ext <<_ACEOF
-/* confdefs.h.  */
-_ACEOF
-cat confdefs.h >>conftest.$ac_ext
-cat >>conftest.$ac_ext <<_ACEOF
-/* end confdefs.h.  */
-#if defined STDC_HEADERS || defined HAVE_STDLIB_H
-# include <stdlib.h>
-#else
-char *realloc ();
-#endif
-
-int
-main ()
-{
-return ! realloc (0, 0);
-  ;
-  return 0;
-}
-_ACEOF
-rm -f conftest$ac_exeext
-if { (ac_try="$ac_link"
-case "(($ac_try" in
-  *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
-  *) ac_try_echo=$ac_try;;
-esac
-eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5
-  (eval "$ac_link") 2>&5
-  ac_status=$?
-  echo "$as_me:$LINENO: \$? = $ac_status" >&5
-  (exit $ac_status); } && { ac_try='./conftest$ac_exeext'
-  { (case "(($ac_try" in
-  *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
-  *) ac_try_echo=$ac_try;;
-esac
-eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5
-  (eval "$ac_try") 2>&5
-  ac_status=$?
-  echo "$as_me:$LINENO: \$? = $ac_status" >&5
-  (exit $ac_status); }; }; then
-  ac_cv_func_realloc_0_nonnull=yes
-else
-  echo "$as_me: program exited with status $ac_status" >&5
-echo "$as_me: failed program was:" >&5
-sed 's/^/| /' conftest.$ac_ext >&5
-
-( exit $ac_status )
-ac_cv_func_realloc_0_nonnull=no
+ac_cv_func_mmap_fixed_mapped=no
 fi
 rm -f core *.core core.conftest.* gmon.out bb.out conftest$ac_exeext conftest.$ac_objext conftest.$ac_ext
 fi
 
 
 fi
-{ echo "$as_me:$LINENO: result: $ac_cv_func_realloc_0_nonnull" >&5
-echo "${ECHO_T}$ac_cv_func_realloc_0_nonnull" >&6; }
-if test $ac_cv_func_realloc_0_nonnull = yes; then
-
-cat >>confdefs.h <<\_ACEOF
-#define HAVE_REALLOC 1
-_ACEOF
-
-else
-  cat >>confdefs.h <<\_ACEOF
-#define HAVE_REALLOC 0
-_ACEOF
-
-   case " $LIBOBJS " in
-  *" realloc.$ac_objext "* ) ;;
-  *) LIBOBJS="$LIBOBJS realloc.$ac_objext"
- ;;
-esac
-
+{ echo "$as_me:$LINENO: result: $ac_cv_func_mmap_fixed_mapped" >&5
+echo "${ECHO_T}$ac_cv_func_mmap_fixed_mapped" >&6; }
+if test $ac_cv_func_mmap_fixed_mapped = yes; then
 
 cat >>confdefs.h <<\_ACEOF
-#define realloc rpl_realloc
+#define HAVE_MMAP 1
 _ACEOF
 
 fi
-
-
+rm -f conftest.mmap
 
 
 
@@ -12487,8 +12498,7 @@ done
 
 
 
-
-for ac_func in __argz_count __argz_next __argz_stringify daemon ftruncate getcwd getpagesize gettimeofday inet_ntoa memchr mempcpy memset mkdir munmap nl_langinfo random rmdir select setlocale setmode sigaction sleep socket srandom stpcpy strcasecmp strchr strcspn strdup strerror strstr strtol strtoul timegm usleep
+for ac_func in __argz_count __argz_next __argz_stringify daemon ftruncate getcwd getpagesize inet_ntoa memchr mempcpy memset mkdir munmap nl_langinfo random rmdir select setlocale setmode sigaction sleep socket srandom stpcpy strcasecmp strchr strcspn strdup strerror strstr strtol strtoul timegm usleep
 do
 as_ac_var=`echo "ac_cv_func_$ac_func" | $as_tr_sh`
 { echo "$as_me:$LINENO: checking for $ac_func" >&5
@@ -12697,6 +12707,120 @@ fi
 done
 
 
+for ac_func in gai_strerror
+do
+as_ac_var=`echo "ac_cv_func_$ac_func" | $as_tr_sh`
+{ echo "$as_me:$LINENO: checking for $ac_func" >&5
+echo $ECHO_N "checking for $ac_func... $ECHO_C" >&6; }
+if { as_var=$as_ac_var; eval "test \"\${$as_var+set}\" = set"; }; then
+  echo $ECHO_N "(cached) $ECHO_C" >&6
+else
+  cat >conftest.$ac_ext <<_ACEOF
+/* confdefs.h.  */
+_ACEOF
+cat confdefs.h >>conftest.$ac_ext
+cat >>conftest.$ac_ext <<_ACEOF
+/* end confdefs.h.  */
+/* Define $ac_func to an innocuous variant, in case <limits.h> declares $ac_func.
+   For example, HP-UX 11i <limits.h> declares gettimeofday.  */
+#define $ac_func innocuous_$ac_func
+
+/* System header to define __stub macros and hopefully few prototypes,
+    which can conflict with char $ac_func (); below.
+    Prefer <limits.h> to <assert.h> if __STDC__ is defined, since
+    <limits.h> exists even on freestanding compilers.  */
+
+#ifdef __STDC__
+# include <limits.h>
+#else
+# include <assert.h>
+#endif
+
+#undef $ac_func
+
+/* Override any GCC internal prototype to avoid an error.
+   Use char because int might match the return type of a GCC
+   builtin and then its argument prototype would still apply.  */
+#ifdef __cplusplus
+extern "C"
+#endif
+char $ac_func ();
+/* The GNU C library defines this for functions which it implements
+    to always fail with ENOSYS.  Some functions are actually named
+    something starting with __ and the normal name is an alias.  */
+#if defined __stub_$ac_func || defined __stub___$ac_func
+choke me
+#endif
+
+int
+main ()
+{
+return $ac_func ();
+  ;
+  return 0;
+}
+_ACEOF
+rm -f conftest.$ac_objext conftest$ac_exeext
+if { (ac_try="$ac_link"
+case "(($ac_try" in
+  *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
+  *) ac_try_echo=$ac_try;;
+esac
+eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5
+  (eval "$ac_link") 2>conftest.er1
+  ac_status=$?
+  grep -v '^ *+' conftest.er1 >conftest.err
+  rm -f conftest.er1
+  cat conftest.err >&5
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); } && {
+	 test -z "$ac_c_werror_flag" ||
+	 test ! -s conftest.err
+       } && test -s conftest$ac_exeext &&
+       $as_test_x conftest$ac_exeext; then
+  eval "$as_ac_var=yes"
+else
+  echo "$as_me: failed program was:" >&5
+sed 's/^/| /' conftest.$ac_ext >&5
+
+	eval "$as_ac_var=no"
+fi
+
+rm -f core conftest.err conftest.$ac_objext conftest_ipa8_conftest.oo \
+      conftest$ac_exeext conftest.$ac_ext
+fi
+ac_res=`eval echo '${'$as_ac_var'}'`
+	       { echo "$as_me:$LINENO: result: $ac_res" >&5
+echo "${ECHO_T}$ac_res" >&6; }
+if test `eval echo '${'$as_ac_var'}'` = yes; then
+  cat >>confdefs.h <<_ACEOF
+#define `echo "HAVE_$ac_func" | $as_tr_cpp` 1
+_ACEOF
+
+
+if true; then
+  HAVE_GAI_STRERROR_TRUE=
+  HAVE_GAI_STRERROR_FALSE='#'
+else
+  HAVE_GAI_STRERROR_TRUE='#'
+  HAVE_GAI_STRERROR_FALSE=
+fi
+
+else
+
+
+if false; then
+  HAVE_GAI_STRERROR_TRUE=
+  HAVE_GAI_STRERROR_FALSE='#'
+else
+  HAVE_GAI_STRERROR_TRUE='#'
+  HAVE_GAI_STRERROR_FALSE=
+fi
+
+fi
+done
+
+
 for ac_func in getaddrinfo
 do
 as_ac_var=`echo "ac_cv_func_$ac_func" | $as_tr_sh`
@@ -12811,6 +12935,120 @@ fi
 done
 
 
+for ac_func in gettimeofday
+do
+as_ac_var=`echo "ac_cv_func_$ac_func" | $as_tr_sh`
+{ echo "$as_me:$LINENO: checking for $ac_func" >&5
+echo $ECHO_N "checking for $ac_func... $ECHO_C" >&6; }
+if { as_var=$as_ac_var; eval "test \"\${$as_var+set}\" = set"; }; then
+  echo $ECHO_N "(cached) $ECHO_C" >&6
+else
+  cat >conftest.$ac_ext <<_ACEOF
+/* confdefs.h.  */
+_ACEOF
+cat confdefs.h >>conftest.$ac_ext
+cat >>conftest.$ac_ext <<_ACEOF
+/* end confdefs.h.  */
+/* Define $ac_func to an innocuous variant, in case <limits.h> declares $ac_func.
+   For example, HP-UX 11i <limits.h> declares gettimeofday.  */
+#define $ac_func innocuous_$ac_func
+
+/* System header to define __stub macros and hopefully few prototypes,
+    which can conflict with char $ac_func (); below.
+    Prefer <limits.h> to <assert.h> if __STDC__ is defined, since
+    <limits.h> exists even on freestanding compilers.  */
+
+#ifdef __STDC__
+# include <limits.h>
+#else
+# include <assert.h>
+#endif
+
+#undef $ac_func
+
+/* Override any GCC internal prototype to avoid an error.
+   Use char because int might match the return type of a GCC
+   builtin and then its argument prototype would still apply.  */
+#ifdef __cplusplus
+extern "C"
+#endif
+char $ac_func ();
+/* The GNU C library defines this for functions which it implements
+    to always fail with ENOSYS.  Some functions are actually named
+    something starting with __ and the normal name is an alias.  */
+#if defined __stub_$ac_func || defined __stub___$ac_func
+choke me
+#endif
+
+int
+main ()
+{
+return $ac_func ();
+  ;
+  return 0;
+}
+_ACEOF
+rm -f conftest.$ac_objext conftest$ac_exeext
+if { (ac_try="$ac_link"
+case "(($ac_try" in
+  *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
+  *) ac_try_echo=$ac_try;;
+esac
+eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5
+  (eval "$ac_link") 2>conftest.er1
+  ac_status=$?
+  grep -v '^ *+' conftest.er1 >conftest.err
+  rm -f conftest.er1
+  cat conftest.err >&5
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); } && {
+	 test -z "$ac_c_werror_flag" ||
+	 test ! -s conftest.err
+       } && test -s conftest$ac_exeext &&
+       $as_test_x conftest$ac_exeext; then
+  eval "$as_ac_var=yes"
+else
+  echo "$as_me: failed program was:" >&5
+sed 's/^/| /' conftest.$ac_ext >&5
+
+	eval "$as_ac_var=no"
+fi
+
+rm -f core conftest.err conftest.$ac_objext conftest_ipa8_conftest.oo \
+      conftest$ac_exeext conftest.$ac_ext
+fi
+ac_res=`eval echo '${'$as_ac_var'}'`
+	       { echo "$as_me:$LINENO: result: $ac_res" >&5
+echo "${ECHO_T}$ac_res" >&6; }
+if test `eval echo '${'$as_ac_var'}'` = yes; then
+  cat >>confdefs.h <<_ACEOF
+#define `echo "HAVE_$ac_func" | $as_tr_cpp` 1
+_ACEOF
+
+
+if true; then
+  HAVE_GETTIMEOFDAY_TRUE=
+  HAVE_GETTIMEOFDAY_FALSE='#'
+else
+  HAVE_GETTIMEOFDAY_TRUE='#'
+  HAVE_GETTIMEOFDAY_FALSE=
+fi
+
+else
+
+
+if false; then
+  HAVE_GETTIMEOFDAY_TRUE=
+  HAVE_GETTIMEOFDAY_FALSE='#'
+else
+  HAVE_GETTIMEOFDAY_TRUE='#'
+  HAVE_GETTIMEOFDAY_FALSE=
+fi
+
+fi
+done
+
+
 for ac_func in inet_aton
 do
 as_ac_var=`echo "ac_cv_func_$ac_func" | $as_tr_sh`
@@ -13357,6 +13595,20 @@ echo "$as_me: error: conditional \"HAVE_BASENAME\" was never defined.
 Usually this means the macro was only invoked conditionally." >&2;}
    { (exit 1); exit 1; }; }
 fi
+if test -z "${HAVE_GAI_STRERROR_TRUE}" && test -z "${HAVE_GAI_STRERROR_FALSE}"; then
+  { { echo "$as_me:$LINENO: error: conditional \"HAVE_GAI_STRERROR\" was never defined.
+Usually this means the macro was only invoked conditionally." >&5
+echo "$as_me: error: conditional \"HAVE_GAI_STRERROR\" was never defined.
+Usually this means the macro was only invoked conditionally." >&2;}
+   { (exit 1); exit 1; }; }
+fi
+if test -z "${HAVE_GAI_STRERROR_TRUE}" && test -z "${HAVE_GAI_STRERROR_FALSE}"; then
+  { { echo "$as_me:$LINENO: error: conditional \"HAVE_GAI_STRERROR\" was never defined.
+Usually this means the macro was only invoked conditionally." >&5
+echo "$as_me: error: conditional \"HAVE_GAI_STRERROR\" was never defined.
+Usually this means the macro was only invoked conditionally." >&2;}
+   { (exit 1); exit 1; }; }
+fi
 if test -z "${HAVE_GETADDRINFO_TRUE}" && test -z "${HAVE_GETADDRINFO_FALSE}"; then
   { { echo "$as_me:$LINENO: error: conditional \"HAVE_GETADDRINFO\" was never defined.
 Usually this means the macro was only invoked conditionally." >&5
@@ -13371,6 +13623,20 @@ echo "$as_me: error: conditional \"HAVE_GETADDRINFO\" was never defined.
 Usually this means the macro was only invoked conditionally." >&2;}
    { (exit 1); exit 1; }; }
 fi
+if test -z "${HAVE_GETTIMEOFDAY_TRUE}" && test -z "${HAVE_GETTIMEOFDAY_FALSE}"; then
+  { { echo "$as_me:$LINENO: error: conditional \"HAVE_GETTIMEOFDAY\" was never defined.
+Usually this means the macro was only invoked conditionally." >&5
+echo "$as_me: error: conditional \"HAVE_GETTIMEOFDAY\" was never defined.
+Usually this means the macro was only invoked conditionally." >&2;}
+   { (exit 1); exit 1; }; }
+fi
+if test -z "${HAVE_GETTIMEOFDAY_TRUE}" && test -z "${HAVE_GETTIMEOFDAY_FALSE}"; then
+  { { echo "$as_me:$LINENO: error: conditional \"HAVE_GETTIMEOFDAY\" was never defined.
+Usually this means the macro was only invoked conditionally." >&5
+echo "$as_me: error: conditional \"HAVE_GETTIMEOFDAY\" was never defined.
+Usually this means the macro was only invoked conditionally." >&2;}
+   { (exit 1); exit 1; }; }
+fi
 if test -z "${HAVE_INET_ATON_TRUE}" && test -z "${HAVE_INET_ATON_FALSE}"; then
   { { echo "$as_me:$LINENO: error: conditional \"HAVE_INET_ATON\" was never defined.
 Usually this means the macro was only invoked conditionally." >&5
@@ -14157,8 +14423,12 @@ POSUB!$POSUB$ac_delim
 LIBOBJS!$LIBOBJS$ac_delim
 HAVE_BASENAME_TRUE!$HAVE_BASENAME_TRUE$ac_delim
 HAVE_BASENAME_FALSE!$HAVE_BASENAME_FALSE$ac_delim
+HAVE_GAI_STRERROR_TRUE!$HAVE_GAI_STRERROR_TRUE$ac_delim
+HAVE_GAI_STRERROR_FALSE!$HAVE_GAI_STRERROR_FALSE$ac_delim
 HAVE_GETADDRINFO_TRUE!$HAVE_GETADDRINFO_TRUE$ac_delim
 HAVE_GETADDRINFO_FALSE!$HAVE_GETADDRINFO_FALSE$ac_delim
+HAVE_GETTIMEOFDAY_TRUE!$HAVE_GETTIMEOFDAY_TRUE$ac_delim
+HAVE_GETTIMEOFDAY_FALSE!$HAVE_GETTIMEOFDAY_FALSE$ac_delim
 HAVE_INET_ATON_TRUE!$HAVE_INET_ATON_TRUE$ac_delim
 HAVE_INET_ATON_FALSE!$HAVE_INET_ATON_FALSE$ac_delim
 HAVE_LOCALTIME_R_TRUE!$HAVE_LOCALTIME_R_TRUE$ac_delim
@@ -14168,7 +14438,7 @@ HAVE_STRPTIME_FALSE!$HAVE_STRPTIME_FALSE$ac_delim
 LTLIBOBJS!$LTLIBOBJS$ac_delim
 _ACEOF
 
-  if test `sed -n "s/.*$ac_delim\$/X/p" conf$$subs.sed | grep -c X` = 63; then
+  if test `sed -n "s/.*$ac_delim\$/X/p" conf$$subs.sed | grep -c X` = 67; then
     break
   elif $ac_last_try; then
     { { echo "$as_me:$LINENO: error: could not make $CONFIG_STATUS" >&5

+ 15 - 2
configure.ac

@@ -127,21 +127,34 @@ AM_GNU_GETTEXT
 AM_GNU_GETTEXT_VERSION(0.12.1)
 AC_FUNC_ERROR_AT_LINE
 
+case "$target" in
+	*mingw*)
+		;;
+	*)
 AC_FUNC_MALLOC
+		AC_FUNC_REALLOC
+		;;
+esac
+
 AC_FUNC_MEMCMP
 AC_FUNC_MMAP
-AC_FUNC_REALLOC
 AC_FUNC_SELECT_ARGTYPES
 AC_FUNC_STAT
 AC_FUNC_VPRINTF
-AC_CHECK_FUNCS([__argz_count __argz_next __argz_stringify daemon ftruncate getcwd getpagesize gettimeofday inet_ntoa memchr mempcpy memset mkdir munmap nl_langinfo random rmdir select setlocale setmode sigaction sleep socket srandom stpcpy strcasecmp strchr strcspn strdup strerror strstr strtol strtoul timegm usleep])
+AC_CHECK_FUNCS([__argz_count __argz_next __argz_stringify daemon ftruncate getcwd getpagesize inet_ntoa memchr mempcpy memset mkdir munmap nl_langinfo random rmdir select setlocale setmode sigaction sleep socket srandom stpcpy strcasecmp strchr strcspn strdup strerror strstr strtol strtoul timegm usleep])
 
 AC_CHECK_FUNCS([basename],
 	[AM_CONDITIONAL([HAVE_BASENAME], true)],
 	[AM_CONDITIONAL([HAVE_BASENAME], false)])
+AC_CHECK_FUNCS([gai_strerror],
+	[AM_CONDITIONAL([HAVE_GAI_STRERROR], true)],
+ 	[AM_CONDITIONAL([HAVE_GAI_STRERROR], false)])
 AC_CHECK_FUNCS([getaddrinfo],
 	[AM_CONDITIONAL([HAVE_GETADDRINFO], true)],
 	[AM_CONDITIONAL([HAVE_GETADDRINFO], false)])
+AC_CHECK_FUNCS([gettimeofday],
+	[AM_CONDITIONAL([HAVE_GETTIMEOFDAY], true)],
+	[AM_CONDITIONAL([HAVE_GETTIMEOFDAY], false)])
 AC_CHECK_FUNCS([inet_aton],
 	[AM_CONDITIONAL([HAVE_INET_ATON], true)],
 	[AM_CONDITIONAL([HAVE_INET_ATON], false)])

+ 4 - 0
doc/Makefile.in

@@ -108,8 +108,12 @@ GMSGFMT = @GMSGFMT@
 GREP = @GREP@
 HAVE_BASENAME_FALSE = @HAVE_BASENAME_FALSE@
 HAVE_BASENAME_TRUE = @HAVE_BASENAME_TRUE@
+HAVE_GAI_STRERROR_FALSE = @HAVE_GAI_STRERROR_FALSE@
+HAVE_GAI_STRERROR_TRUE = @HAVE_GAI_STRERROR_TRUE@
 HAVE_GETADDRINFO_FALSE = @HAVE_GETADDRINFO_FALSE@
 HAVE_GETADDRINFO_TRUE = @HAVE_GETADDRINFO_TRUE@
+HAVE_GETTIMEOFDAY_FALSE = @HAVE_GETTIMEOFDAY_FALSE@
+HAVE_GETTIMEOFDAY_TRUE = @HAVE_GETTIMEOFDAY_TRUE@
 HAVE_INET_ATON_FALSE = @HAVE_INET_ATON_FALSE@
 HAVE_INET_ATON_TRUE = @HAVE_INET_ATON_TRUE@
 HAVE_LOCALTIME_R_FALSE = @HAVE_LOCALTIME_R_FALSE@

+ 4 - 0
m4/Makefile.in

@@ -104,8 +104,12 @@ GMSGFMT = @GMSGFMT@
 GREP = @GREP@
 HAVE_BASENAME_FALSE = @HAVE_BASENAME_FALSE@
 HAVE_BASENAME_TRUE = @HAVE_BASENAME_TRUE@
+HAVE_GAI_STRERROR_FALSE = @HAVE_GAI_STRERROR_FALSE@
+HAVE_GAI_STRERROR_TRUE = @HAVE_GAI_STRERROR_TRUE@
 HAVE_GETADDRINFO_FALSE = @HAVE_GETADDRINFO_FALSE@
 HAVE_GETADDRINFO_TRUE = @HAVE_GETADDRINFO_TRUE@
+HAVE_GETTIMEOFDAY_FALSE = @HAVE_GETTIMEOFDAY_FALSE@
+HAVE_GETTIMEOFDAY_TRUE = @HAVE_GETTIMEOFDAY_TRUE@
 HAVE_INET_ATON_FALSE = @HAVE_INET_ATON_FALSE@
 HAVE_INET_ATON_TRUE = @HAVE_INET_ATON_TRUE@
 HAVE_LOCALTIME_R_FALSE = @HAVE_LOCALTIME_R_FALSE@

+ 1 - 0
src/DefaultFileAllocator.cc

@@ -35,6 +35,7 @@
 #include "DefaultFileAllocator.h"
 #include "DlAbortEx.h"
 #include "TimeA2.h"
+#include "a2io.h"
 #include <sys/types.h>
 #include <unistd.h>
 #include <errno.h>

+ 1 - 0
src/DefaultPeerStorage.cc

@@ -36,6 +36,7 @@
 #include "LogFactory.h"
 #include "BtRegistry.h"
 #include "message.h"
+#include "a2time.h"
 
 DefaultPeerStorage::DefaultPeerStorage(BtContextHandle btContext,
 				       const Option* option):

+ 0 - 1
src/DownloadCommand.cc

@@ -43,7 +43,6 @@
 #ifdef ENABLE_MESSAGE_DIGEST
 # include "ChecksumCommand.h"
 #endif // ENABLE_MESSAGE_DIGEST
-#include <sys/time.h>
 #include <stdlib.h>
 
 DownloadCommand::DownloadCommand(int cuid,

+ 1 - 0
src/GlowFileAllocator.cc

@@ -35,6 +35,7 @@
 #include "GlowFileAllocator.h"
 #include "DlAbortEx.h"
 #include "TimeA2.h"
+#include "a2io.h"
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>

+ 4 - 0
src/HttpRequestCommand.cc

@@ -52,6 +52,10 @@ HttpRequestCommand::~HttpRequestCommand() {}
 bool HttpRequestCommand::executeInternal() {
   socket->setBlockingMode();
   if(req->getProtocol() == "https") {
+#ifdef __MINGW32__
+    // it only works in non-blocking mode
+    socket->setNonBlockingMode();
+#endif // __MINGW32__
     socket->initiateSecureConnection();
   }
   if(!e->option->getAsBool(PREF_HTTP_KEEP_ALIVE)) {

+ 12 - 0
src/Makefile.am

@@ -235,6 +235,18 @@ if !HAVE_GETADDRINFO
 SRCS += getaddrinfo.c getaddrinfo.h
 endif # !HAVE_GETADDRINFO
 
+if !HAVE_GAI_STRERROR
+SRCS += gai_strerror.c gai_strerror.h
+endif # !HAVE_GAI_STRERROR
+
+if !HAVE_GETTIMEOFDAY
+SRCS += gettimeofday.c gettimeofday.h
+endif # !HAVE_GETTIMEOFDAY
+
+if !HAVE_GETTIMEOFDAY
+SRCS += gettimeofday.c gettimeofday.h
+endif # !HAVE_GETTIMEOFDAY
+
 if !HAVE_INET_ATON
 SRCS += inet_aton.c inet_aton.h
 endif # !HAVE_INET_ATON

+ 25 - 11
src/Makefile.in

@@ -161,9 +161,12 @@ bin_PROGRAMS = aria2c$(EXEEXT)
 
 @HAVE_BASENAME_FALSE@am__append_4 = libgen.c libgen.h
 @HAVE_GETADDRINFO_FALSE@am__append_5 = getaddrinfo.c getaddrinfo.h
-@HAVE_INET_ATON_FALSE@am__append_6 = inet_aton.c inet_aton.h
-@HAVE_LOCALTIME_R_FALSE@am__append_7 = localtime_r.c localtime_r.h
-@HAVE_STRPTIME_FALSE@am__append_8 = strptime.c strptime.h
+@HAVE_GAI_STRERROR_FALSE@am__append_6 = gai_strerror.c gai_strerror.h
+@HAVE_GETTIMEOFDAY_FALSE@am__append_7 = gettimeofday.c gettimeofday.h \
+@HAVE_GETTIMEOFDAY_FALSE@	gettimeofday.c gettimeofday.h
+@HAVE_INET_ATON_FALSE@am__append_8 = inet_aton.c inet_aton.h
+@HAVE_LOCALTIME_R_FALSE@am__append_9 = localtime_r.c localtime_r.h
+@HAVE_STRPTIME_FALSE@am__append_10 = strptime.c strptime.h
 subdir = src
 DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in alloca.c
 ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
@@ -326,7 +329,8 @@ am__libaria2c_a_SOURCES_DIST = Socket.h SocketCore.cc SocketCore.h \
 	MetalinkResource.cc MetalinkResource.h MetalinkProcessor.h \
 	Xml2MetalinkProcessor.cc Xml2MetalinkProcessor.h \
 	MetalinkRequestInfo.cc MetalinkRequestInfo.h libgen.c libgen.h \
-	getaddrinfo.c getaddrinfo.h inet_aton.c inet_aton.h \
+	getaddrinfo.c getaddrinfo.h gai_strerror.c gai_strerror.h \
+	gettimeofday.c gettimeofday.h inet_aton.c inet_aton.h \
 	localtime_r.c localtime_r.h strptime.c strptime.h
 @ENABLE_MESSAGE_DIGEST_TRUE@am__objects_1 =  \
 @ENABLE_MESSAGE_DIGEST_TRUE@	ChunkChecksumValidator.$(OBJEXT) \
@@ -406,10 +410,13 @@ am__libaria2c_a_SOURCES_DIST = Socket.h SocketCore.cc SocketCore.h \
 @ENABLE_METALINK_TRUE@	MetalinkRequestInfo.$(OBJEXT)
 @HAVE_BASENAME_FALSE@am__objects_4 = libgen.$(OBJEXT)
 @HAVE_GETADDRINFO_FALSE@am__objects_5 = getaddrinfo.$(OBJEXT)
-@HAVE_INET_ATON_FALSE@am__objects_6 = inet_aton.$(OBJEXT)
-@HAVE_LOCALTIME_R_FALSE@am__objects_7 = localtime_r.$(OBJEXT)
-@HAVE_STRPTIME_FALSE@am__objects_8 = strptime.$(OBJEXT)
-am__objects_9 = SocketCore.$(OBJEXT) Command.$(OBJEXT) \
+@HAVE_GAI_STRERROR_FALSE@am__objects_6 = gai_strerror.$(OBJEXT)
+@HAVE_GETTIMEOFDAY_FALSE@am__objects_7 = gettimeofday.$(OBJEXT) \
+@HAVE_GETTIMEOFDAY_FALSE@	gettimeofday.$(OBJEXT)
+@HAVE_INET_ATON_FALSE@am__objects_8 = inet_aton.$(OBJEXT)
+@HAVE_LOCALTIME_R_FALSE@am__objects_9 = localtime_r.$(OBJEXT)
+@HAVE_STRPTIME_FALSE@am__objects_10 = strptime.$(OBJEXT)
+am__objects_11 = SocketCore.$(OBJEXT) Command.$(OBJEXT) \
 	AbstractCommand.$(OBJEXT) \
 	InitiateConnectionCommandFactory.$(OBJEXT) \
 	DownloadCommand.$(OBJEXT) \
@@ -452,8 +459,8 @@ am__objects_9 = SocketCore.$(OBJEXT) Command.$(OBJEXT) \
 	FileEntry.$(OBJEXT) Platform.$(OBJEXT) $(am__objects_1) \
 	$(am__objects_2) $(am__objects_3) $(am__objects_4) \
 	$(am__objects_5) $(am__objects_6) $(am__objects_7) \
-	$(am__objects_8)
-am_libaria2c_a_OBJECTS = $(am__objects_9)
+	$(am__objects_8) $(am__objects_9) $(am__objects_10)
+am_libaria2c_a_OBJECTS = $(am__objects_11)
 libaria2c_a_OBJECTS = $(am_libaria2c_a_OBJECTS)
 am__installdirs = "$(DESTDIR)$(bindir)"
 binPROGRAMS_INSTALL = $(INSTALL_PROGRAM)
@@ -523,8 +530,12 @@ GMSGFMT = @GMSGFMT@
 GREP = @GREP@
 HAVE_BASENAME_FALSE = @HAVE_BASENAME_FALSE@
 HAVE_BASENAME_TRUE = @HAVE_BASENAME_TRUE@
+HAVE_GAI_STRERROR_FALSE = @HAVE_GAI_STRERROR_FALSE@
+HAVE_GAI_STRERROR_TRUE = @HAVE_GAI_STRERROR_TRUE@
 HAVE_GETADDRINFO_FALSE = @HAVE_GETADDRINFO_FALSE@
 HAVE_GETADDRINFO_TRUE = @HAVE_GETADDRINFO_TRUE@
+HAVE_GETTIMEOFDAY_FALSE = @HAVE_GETTIMEOFDAY_FALSE@
+HAVE_GETTIMEOFDAY_TRUE = @HAVE_GETTIMEOFDAY_TRUE@
 HAVE_INET_ATON_FALSE = @HAVE_INET_ATON_FALSE@
 HAVE_INET_ATON_TRUE = @HAVE_INET_ATON_TRUE@
 HAVE_LOCALTIME_R_FALSE = @HAVE_LOCALTIME_R_FALSE@
@@ -701,7 +712,8 @@ SRCS = Socket.h SocketCore.cc SocketCore.h Command.cc Command.h \
 	HttpHeaderProcessor.h FileEntry.cc FileEntry.h Platform.cc \
 	Platform.h $(am__append_1) $(am__append_2) $(am__append_3) \
 	$(am__append_4) $(am__append_5) $(am__append_6) \
-	$(am__append_7) $(am__append_8)
+	$(am__append_7) $(am__append_8) $(am__append_9) \
+	$(am__append_10)
 noinst_LIBRARIES = libaria2c.a
 libaria2c_a_SOURCES = $(SRCS)
 aria2c_LDADD = libaria2c.a @LIBINTL@ @ALLOCA@ @LIBGNUTLS_LIBS@\
@@ -941,7 +953,9 @@ distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/UriListParser.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Util.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Xml2MetalinkProcessor.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/gai_strerror.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/getaddrinfo.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/gettimeofday.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/inet_aton.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libgen.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/localtime_r.Po@am__quote@

+ 2 - 0
src/MetalinkRequestInfo.cc

@@ -58,6 +58,8 @@ public:
 	urlsPtr->push_back(resource->url);
       }
       break;
+    default:
+      break;
     }
   }
 };

+ 2 - 2
src/NameResolver.cc

@@ -36,7 +36,7 @@
 
 #ifdef ENABLE_ASYNC_DNS
 
-void callback(void* arg, int32_t status, struct hostent* host) {
+void callback(void* arg, int status, struct hostent* host) {
   NameResolver* resolverPtr = (NameResolver*)arg;
 #ifdef HAVE_LIBARES
   // This block is required since the assertion in ares_strerror fails
@@ -75,7 +75,7 @@ void NameResolver::resolve(const string& hostname)
   ai.ai_socktype = SOCK_STREAM;
   ai.ai_protocol = 0; 
   struct addrinfo* res;
-  int32_t ec;
+  int ec;
   if((ec = getaddrinfo(hostname.c_str(), 0, &ai, &res)) != 0) {
     throw new DlAbortEx(EX_RESOLVE_HOSTNAME,
 			hostname.c_str(), gai_strerror(ec));

+ 0 - 1
src/PeerStat.h

@@ -38,7 +38,6 @@
 #include "common.h"
 #include "SpeedCalc.h"
 #include "SharedHandle.h"
-#include <sys/time.h>
 
 class PeerStat {
 public:

+ 5 - 10
src/Platform.cc

@@ -37,27 +37,22 @@
 #endif // HAVE_CONFIG_H
 
 #ifdef HAVE_WINSOCK2_H
-# ifndef _WIN32_WINNT
-#  define _WIN32_WINNT 0x501
-# endif // _WIN32_WINNT
-# include <winsock2.h>
-#endif // HAVE_WINSOCK2_H
 
-#include "Platform.h"
+#include "common.h"
+#include "a2netcompat.h"
 #include "DlAbortEx.h"
+#include "Platform.h"
 
 Platform::Platform() {
-#ifdef HAVE_WINSOCK2_H
   WSADATA wsaData;
   memset((char*)&wsaData, 0, sizeof(wsaData));
   if (WSAStartup(MAKEWORD(1, 1), &wsaData)) {
     throw new DlAbortEx(_("Windows socket library initialization failed"));
   }
-#endif // HAVE_WINSOCK2_H
 }
 
 Platform::~Platform() {
-#ifdef HAVE_WINSOCK2_H
   WSACleanup();
-#endif // HAVE_WINSOCK2_H
 }
+
+#endif // HAVE_WINSOCK2_H

+ 8 - 0
src/Platform.h

@@ -35,6 +35,12 @@
 #ifndef _D_PLATFORM_H_
 #define _D_PLATFORM_H_
 
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif // HAVE_CONFIG_H
+
+#ifdef HAVE_WINSOCK2_H
+
 class Platform {
 public:
   Platform();
@@ -42,4 +48,6 @@ public:
   ~Platform();
 };
 
+#endif // HAVE_WINSOCK2_H
+
 #endif // _D_PLATFORM_H_

+ 1 - 5
src/SimpleLogger.cc

@@ -37,14 +37,10 @@
 #include "DlAbortEx.h"
 #include "message.h"
 #include "a2io.h"
-#include <time.h>
+#include "a2time.h"
 #include <stdarg.h>
 #include <errno.h>
 
-#ifndef HAVE_LOCALTIME_R
-# include "localtime_r.h"
-#endif // HAVE_LOCALTIME_R
-
 #define WRITE_LOG(LEVEL, MSG) \
 va_list ap;\
 va_start(ap, MSG);\

+ 1 - 1
src/SimpleRandomizer.h

@@ -36,8 +36,8 @@
 #define _D_SIMPLE_RANDOMIZER_H_
 
 #include "Randomizer.h"
+#include "a2time.h"
 #include <stdlib.h>
-#include <time.h>
 
 class SimpleRandomizer : public Randomizer {
 private:

+ 135 - 95
src/SocketCore.cc

@@ -36,36 +36,16 @@
 #include "DlRetryEx.h"
 #include "DlAbortEx.h"
 #include "message.h"
-#include "a2io.h"
 #include "a2netcompat.h"
+#include "a2time.h"
 #include <unistd.h>
 #include <fcntl.h>
-#include <sys/time.h>
 #include <errno.h>
 
-#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
-
+#ifndef __MINGW32__
+# define SOCKET_ERRNO (errno)
+#else
+# define SOCKET_ERRNO (WSAGetLastError())
 #endif // __MINGW32__
 
 SocketCore::SocketCore():sockfd(-1) {
@@ -78,6 +58,7 @@ SocketCore::SocketCore(int32_t sockfd):sockfd(sockfd) {
 
 void SocketCore::init() {
   use = 1;
+  blocking = true;
   secure = false;
 #ifdef HAVE_LIBSSL
   // for SSL
@@ -105,13 +86,17 @@ void SocketCore::beginListen(int32_t port) {
   //sockfd = socket(AF_UNSPEC, SOCK_STREAM, PF_UNSPEC);
   sockfd = socket(AF_INET, SOCK_STREAM, 0);
   if(sockfd == -1) {
-    throw new DlAbortEx(EX_SOCKET_OPEN, strerror(errno));
+    throw new DlAbortEx(EX_SOCKET_OPEN, errorMsg());
   }
   SOCKOPT_T sockopt = 1;
   if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof(socklen_t)) < 0) {
+#ifdef __MINGW32__
+    ::closesocket(sockfd);
+#else
     close(sockfd);
+#endif // __MINGW32__
     sockfd = -1;
-    throw new DlAbortEx(EX_SOCKET_SET_OPT, strerror(errno));
+    throw new DlAbortEx(EX_SOCKET_SET_OPT, errorMsg());
   }
 
   struct sockaddr_in sockaddr;
@@ -121,11 +106,11 @@ void SocketCore::beginListen(int32_t port) {
   sockaddr.sin_port = htons(port);
   
   if(bind(sockfd, (struct sockaddr*)&sockaddr, sizeof(sockaddr)) == -1) {
-    throw new DlAbortEx(EX_SOCKET_BIND, strerror(errno));
+    throw new DlAbortEx(EX_SOCKET_BIND, errorMsg());
   }
 
   if(listen(sockfd, 1) == -1) {
-    throw new DlAbortEx(EX_SOCKET_LISTEN, strerror(errno));
+    throw new DlAbortEx(EX_SOCKET_LISTEN, errorMsg());
   }
 
   setNonBlockingMode();
@@ -137,7 +122,7 @@ SocketCore* SocketCore::acceptConnection() const {
   memset((char*)&sockaddr, 0, sizeof(sockaddr));
   int32_t fd;
   if((fd = accept(sockfd, (struct sockaddr*)&sockaddr, &len)) == -1) {
-    throw new DlAbortEx(EX_SOCKET_ACCEPT, strerror(errno));
+    throw new DlAbortEx(EX_SOCKET_ACCEPT, errorMsg());
   }
 
   SocketCore* s = new SocketCore(fd);
@@ -149,7 +134,7 @@ void SocketCore::getAddrInfo(pair<string, int32_t>& addrinfo) const {
   memset((char*)&listenaddr, 0, sizeof(listenaddr));
   socklen_t len = sizeof(listenaddr);
   if(getsockname(sockfd, (struct sockaddr*)&listenaddr, &len) == -1) {
-    throw new DlAbortEx(EX_SOCKET_GET_NAME, strerror(errno));
+    throw new DlAbortEx(EX_SOCKET_GET_NAME, errorMsg());
   }
   addrinfo.first = inet_ntoa(listenaddr.sin_addr);
   addrinfo.second = ntohs(listenaddr.sin_port);
@@ -160,7 +145,7 @@ void SocketCore::getPeerInfo(pair<string, int32_t>& peerinfo) const {
   memset(&peerin, 0, sizeof(peerin));
   int32_t len = sizeof(peerin);
   if(getpeername(sockfd, (struct sockaddr*)&peerin, (socklen_t*)&len) < 0) {
-    throw new DlAbortEx(EX_SOCKET_GET_PEER, strerror(errno));
+    throw new DlAbortEx(EX_SOCKET_GET_PEER, errorMsg());
   }
   peerinfo.first = inet_ntoa(peerin.sin_addr);
   peerinfo.second = ntohs(peerin.sin_port);
@@ -170,13 +155,13 @@ void SocketCore::establishConnection(const string& host, int32_t port) {
   closeConnection();
   sockfd = socket(AF_INET, SOCK_STREAM, 0);
   if(sockfd == -1) {
-      throw new DlAbortEx(EX_SOCKET_OPEN, strerror(errno));
+      throw new DlAbortEx(EX_SOCKET_OPEN, errorMsg());
   }
   SOCKOPT_T sockopt = 1;
   if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof(socklen_t)) < 0) {
     close(sockfd);
     sockfd = -1;
-    throw new DlAbortEx(EX_SOCKET_SET_OPT, strerror(errno));
+    throw new DlAbortEx(EX_SOCKET_SET_OPT, errorMsg());
   }
 
   struct sockaddr_in sockaddr;
@@ -203,23 +188,27 @@ void SocketCore::establishConnection(const string& host, int32_t port) {
   }
   // make socket non-blocking mode
   setNonBlockingMode();
-  if(connect(sockfd, (struct sockaddr*)&sockaddr, (socklen_t)sizeof(sockaddr)) == -1 && errno != EINPROGRESS) {
-    throw new DlAbortEx(EX_SOCKET_CONNECT, host.c_str(), strerror(errno));
+  if(connect(sockfd, (struct sockaddr*)&sockaddr, (socklen_t)sizeof(sockaddr)) == -1 && SOCKET_ERRNO != EINPROGRESS) {
+    throw new DlAbortEx(EX_SOCKET_CONNECT, host.c_str(), errorMsg());
   }
 }
 
-void SocketCore::setNonBlockingMode() const {
+void SocketCore::setNonBlockingMode() {
 #ifdef __MINGW32__
   u_long flag = 0;
   ::ioctlsocket(sockfd, FIONBIO, &flag);
+  if (WSAGetLastError()) {
+    throw new DlAbortEx(EX_SOCKET_NONBLOCKING, errorMsg());
+  }
 #else
   int32_t flags = fcntl(sockfd, F_GETFL, 0);
   // TODO add error handling
   fcntl(sockfd, F_SETFL, flags|O_NONBLOCK);
 #endif // __MINGW32__
+  blocking = false;
 }
 
-void SocketCore::setBlockingMode() const {
+void SocketCore::setBlockingMode() {
 #ifdef __MINGW32__
   u_long flag = 1;
   ::ioctlsocket(sockfd, FIONBIO, &flag);
@@ -228,6 +217,7 @@ void SocketCore::setBlockingMode() const {
   // TODO add error handling
   fcntl(sockfd, F_SETFL, flags&(~O_NONBLOCK));
 #endif // __MINGW32__
+  blocking = true;
 }
 
 void SocketCore::closeConnection() {
@@ -243,7 +233,11 @@ void SocketCore::closeConnection() {
   }
 #endif // HAVE_LIBGNUTLS
   if(sockfd != -1) {
+#ifdef __MINGW32__
+  ::closesocket(sockfd);
+#else
     close(sockfd);
+#endif // __MINGW32__
     sockfd = -1;
   }
 #ifdef HAVE_LIBSSL
@@ -277,10 +271,10 @@ bool SocketCore::isWritable(int32_t timeout) const {
     // time out
     return false;
   } else {
-    if(errno == EINPROGRESS || errno == EINTR) {
+    if(SOCKET_ERRNO == EINPROGRESS || SOCKET_ERRNO == EINTR) {
       return false;
     } else {
-      throw new DlRetryEx(EX_SOCKET_CHECK_WRITABLE, strerror(errno));
+      throw new DlRetryEx(EX_SOCKET_CHECK_WRITABLE, errorMsg());
     }
   }
 }
@@ -306,91 +300,84 @@ bool SocketCore::isReadable(int32_t timeout) const {
     // time out
     return false;
   } else {
-    if(errno == EINPROGRESS || errno == EINTR) {
+    if(SOCKET_ERRNO == EINPROGRESS || SOCKET_ERRNO == EINTR) {
       return false;
     } else {
-      throw new DlRetryEx(EX_SOCKET_CHECK_READABLE, strerror(errno));
+      throw new DlRetryEx(EX_SOCKET_CHECK_READABLE, errorMsg());
     }
   }
 }
 
 void SocketCore::writeData(const char* data, int32_t len) {
   int32_t ret = 0;
-  if(!secure && (ret = send(sockfd, data, (size_t)len, 0)) != len
+
+  if(!secure) {
+    if((ret = send(sockfd, data, (size_t)len, 0)) != len) {
+      throw new DlRetryEx(EX_SOCKET_SEND, errorMsg());
+    }
+  } else {
 #ifdef HAVE_LIBSSL
      // for SSL
      // TODO handling len == 0 case required
-     || secure && (ret = SSL_write(ssl, data, len)) != len
+    if((ret = SSL_write(ssl, data, len)) != len) {
+      throw new DlRetryEx(EX_SOCKET_SEND, ERR_error_string(ERR_get_error(), NULL));
+    }
 #endif // HAVE_LIBSSL
 #ifdef HAVE_LIBGNUTLS
-     || secure && (ret = gnutls_record_send(sslSession, data, len)) != len
-#endif // HAVE_LIBGNUTLS
-     ) {
-    const char* errorMsg;
-#ifdef HAVE_LIBGNUTLS
-    if(secure) {
-      errorMsg = gnutls_strerror(ret);
-    } else {
-      errorMsg = strerror(errno);
+    if((ret = gnutls_record_send(sslSession, data, len)) == len) {
+      throw new DlRetryEx(EX_SOCKET_SEND, gnutls_strerror(ret));
     }
-#else // HAVE_LIBGNUTLS
-    errorMsg = strerror(errno);
-#endif
-    throw new DlRetryEx(EX_SOCKET_SEND, errorMsg);
+#endif // HAVE_LIBGNUTLS
   }
 }
 
 void SocketCore::readData(char* data, int32_t& len) {
   int32_t ret = 0;
-  if(!secure && (ret = recv(sockfd, data, (size_t)len, 0)) < 0
+
+  if(!secure) {
+    if ((ret = recv(sockfd, data, (size_t)len, 0)) < 0) {
+      throw new DlRetryEx(EX_SOCKET_RECV, errorMsg());
+    }
+  } else {
 #ifdef HAVE_LIBSSL
      // for SSL
      // TODO handling len == 0 case required
-     || secure && (ret = SSL_read(ssl, data, len)) < 0
+    if ((ret = SSL_read(ssl, data, len)) < 0) {
+      throw new DlRetryEx(EX_SOCKET_RECV, ERR_error_string(ERR_get_error(), NULL));
+    }
 #endif // HAVE_LIBSSL
 #ifdef HAVE_LIBGNUTLS
-     || secure && (ret = gnutlsRecv(data, len)) < 0
-#endif // HAVE_LIBGNUTLS
-     ) {
-    const char* errorMsg;
-#ifdef HAVE_LIBGNUTLS
-    if(secure) {
-      errorMsg = gnutls_strerror(ret);
-    } else {
-      errorMsg = strerror(errno);
+    if ((ret = gnutlsRecv(data, len)) < 0) {
+      throw new DlRetryEx(EX_SOCKET_RECV, gnutls_strerror(ret));
     }
-#else // HAVE_LIBGNUTLS
-    errorMsg = strerror(errno);
-#endif
-    throw new DlRetryEx(EX_SOCKET_RECV, errorMsg);
+#endif // HAVE_LIBGNUTLS
   }
+
   len = ret;
 }
 
 void SocketCore::peekData(char* data, int32_t& len) {
   int32_t ret = 0;
-  if(!secure && (ret = recv(sockfd, data, (size_t)len, MSG_PEEK)) < 0
+
+  if(!secure) {
+    if ((ret = recv(sockfd, data, (size_t)len, MSG_PEEK)) < 0) {
+      throw new DlRetryEx(EX_SOCKET_PEEK, errorMsg());
+    }
+  } else {
 #ifdef HAVE_LIBSSL
      // for SSL
      // TODO handling len == 0 case required
-     || secure && (ret = SSL_peek(ssl, data, len)) < 0
+    if ((ret = SSL_peek(ssl, data, len)) < 0) {
+      throw new DlRetryEx(EX_SOCKET_PEEK, ERR_error_string(ERR_get_error(), NULL));
+    }
 #endif // HAVE_LIBSSL
 #ifdef HAVE_LIBGNUTLS
-     || secure && (ret = gnutlsPeek(data, len)) < 0
-#endif // HAVE_LIBGNUTLS
-     ) {
-    const char* errorMsg;
-#ifdef HAVE_LIBGNUTLS
-    if(secure) {
-      errorMsg = gnutls_strerror(ret);
-    } else {
-      errorMsg = strerror(errno);
+    if ((ret = gnutlsPeek(data, len)) < 0) {
+      throw new DlRetryEx(EX_SOCKET_PEEK, gnutls_strerror(ret));
     }
-#else // HAVE_LIBGNUTLS
-    errorMsg = strerror(errno);
-#endif
-    throw new DlRetryEx(EX_SOCKET_PEEK, errorMsg);
+#endif // HAVE_LIBGNUTLS
   }
+
   len = ret;
 }
 
@@ -460,21 +447,44 @@ void SocketCore::initiateSecureConnection() {
   if(!secure) {
     sslCtx = SSL_CTX_new(SSLv23_client_method());
     if(sslCtx == NULL) {
-      throw new DlAbortEx(EX_SSL_INIT_FAILURE);
+      throw new DlAbortEx(EX_SSL_INIT_FAILURE, ERR_error_string(ERR_get_error(), NULL));
     }
     SSL_CTX_set_mode(sslCtx, SSL_MODE_AUTO_RETRY);
     ssl = SSL_new(sslCtx);
     if(ssl == NULL) {
-      throw new DlAbortEx(EX_SSL_INIT_FAILURE);
+      throw new DlAbortEx(EX_SSL_INIT_FAILURE, ERR_error_string(ERR_get_error(), NULL));
     }
     if(SSL_set_fd(ssl, sockfd) == 0) {
-      throw new DlAbortEx(EX_SSL_INIT_FAILURE);
+      throw new DlAbortEx(EX_SSL_INIT_FAILURE, ERR_error_string(ERR_get_error(), NULL));
     }
      // TODO handling return value == 0 case required
-    if(SSL_connect(ssl) <= 0) {
-      throw new DlAbortEx(EX_SSL_INIT_FAILURE);
+    int e = SSL_connect(ssl);
+
+    if (e <= 0) {
+      int ssl_error = SSL_get_error(ssl, e);
+      switch(ssl_error) {
+        case SSL_ERROR_NONE:
+          break;
+
+        case SSL_ERROR_WANT_READ:
+        case SSL_ERROR_WANT_WRITE:
+        case SSL_ERROR_WANT_X509_LOOKUP:
+        case SSL_ERROR_ZERO_RETURN:
+          if (blocking) {
+            throw new DlAbortEx(EX_SSL_CONNECT_ERROR, ssl_error);
+          }
+          break;
+
+        case SSL_ERROR_SYSCALL:
+          throw new DlAbortEx(EX_SSL_IO_ERROR);
+
+        case SSL_ERROR_SSL:
+          throw new DlAbortEx(EX_SSL_PROTOCOL_ERROR);
+
+        default:
+          throw new DlAbortEx(EX_SSL_UNKNOWN_ERROR, ssl_error);
+      }
     }
-    secure = true;
   }
 #endif // HAVE_LIBSSL
 #ifdef HAVE_LIBGNUTLS
@@ -493,9 +503,39 @@ void SocketCore::initiateSecureConnection() {
     gnutls_transport_set_ptr(sslSession, (gnutls_transport_ptr_t)sockfd);
     int32_t ret = gnutls_handshake(sslSession);
     if(ret < 0) {
-      throw new DlAbortEx(gnutls_strerror(ret));
+      throw new DlAbortEx(EX_SSL_INIT_FAILURE, gnutls_strerror(ret));
     }
-    secure = true;
   }
 #endif // HAVE_LIBGNUTLS
+
+  secure = true;
+}
+
+/* static */ int SocketCore::error() {
+  return SOCKET_ERRNO;
+}
+
+/* static */ const char *SocketCore::errorMsg() {
+  return errorMsg(SOCKET_ERRNO);
+}
+
+/* static */ const char *SocketCore::errorMsg(const int err) {
+#ifndef __MINGW32__
+  return strerror(err);
+#else
+  static char buf[256];
+  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), EX_SOCKET_UNKNOWN_ERROR, err, err);
+  }
+  return buf;
+#endif // __MINGW32__
 }

+ 7 - 3
src/SocketCore.h

@@ -42,6 +42,7 @@
 #ifdef HAVE_LIBSSL
 // for SSL
 # include <openssl/ssl.h>
+# include <openssl/err.h>
 #endif // HAVE_LIBSSL
 #ifdef HAVE_LIBGNUTLS
 # include <gnutls/gnutls.h>
@@ -58,6 +59,7 @@ private:
   int32_t sockfd;
   // reference counter for this object.
   int32_t use;
+  bool blocking;
   bool secure;
 #ifdef HAVE_LIBSSL
   // for SSL
@@ -79,6 +81,9 @@ private:
 
   void init();
   SocketCore(int32_t sockfd);
+  static int error();
+  static const char *errorMsg();
+  static const char *errorMsg(const int err);
 public:
   SocketCore();
   ~SocketCore();
@@ -123,12 +128,12 @@ public:
    */
   void establishConnection(const string& host, int32_t port);
 
-  void setNonBlockingMode() const;
+  void setNonBlockingMode();
 
   /**
    * Makes this socket blocking mode.
    */
-  void setBlockingMode() const;
+  void setBlockingMode();
 
   /**
    * Closes the connection of this socket.
@@ -207,6 +212,5 @@ public:
   bool operator<(const SocketCore& s) {
     return sockfd < s.sockfd;
   }
-
 };
 #endif // _D_SOCKET_CORE_H_

+ 1 - 0
src/TimeA2.cc

@@ -32,6 +32,7 @@
  * files in the program, then also delete it here.
  */
 /* copyright --> */
+
 #include "TimeA2.h"
 #include "Util.h"
 

+ 1 - 1
src/TimeA2.h

@@ -36,7 +36,7 @@
 #define _D_TIME_H_
 
 #include "common.h"
-#include <sys/time.h>
+#include "a2time.h"
 
 class Time {
 private:

+ 3 - 8
src/Util.cc

@@ -37,7 +37,6 @@
 #include "File.h"
 #include "message.h"
 #include "SimpleRandomizer.h"
-#include "a2io.h"
 #include "a2netcompat.h"
 #include <ctype.h>
 #include <errno.h>
@@ -46,10 +45,6 @@
 #include <signal.h>
 #include <iomanip>
 
-#ifndef HAVE_STRPTIME
-# include "strptime.h"
-#endif // HAVE_STRPTIME
-
 template<typename T>
 string uint2str(T value, bool comma) {
   string str;
@@ -644,13 +639,13 @@ bool Util::isNumbersAndDotsNotation(const string& name) {
   }
 }
 
-void Util::setGlobalSignalHandler(int32_t signal, void (*handler)(int32_t), int32_t flags) {
+void Util::setGlobalSignalHandler(int32_t sig, void (*handler)(int32_t), int32_t flags) {
 #ifdef HAVE_SIGACTION
   struct sigaction sigact;
   sigact.sa_handler = handler;
   sigact.sa_flags = flags;
   sigemptyset(&sigact.sa_mask);
-  sigaction(signal, &sigact, NULL);
+  sigaction(sig, &sigact, NULL);
 #else
   signal(sig, handler);
 #endif // HAVE_SIGACTION
@@ -728,7 +723,7 @@ time_t Util::httpGMT(const string& httpStdTime)
   time_t thetime = mktime(&tm);
   if (tz) {
     char s[256];
-    sprintf("TZ=%s", tz);
+    snprintf(s, sizeof(s), "TZ=%s", tz);
     putenv(s);
   }
   tzset();

+ 1 - 1
src/Util.h

@@ -37,13 +37,13 @@
 
 #include "common.h"
 #include "FileEntry.h"
+#include "a2time.h"
 #ifdef ENABLE_MESSAGE_DIGEST
 #include "messageDigest.h"
 #endif // ENABLE_MESSAGE_DIGEST
 #include <string>
 #include <utility>
 #include <deque>
-#include <sys/time.h>
 #include <stdio.h>
 #include <ostream>
 

+ 1 - 1
src/a2io.h

@@ -109,8 +109,8 @@
 # define DEV_STDOUT "/dev/stdout"
 #endif // HAVE_WINSOCK2_H
 
-
 #ifdef __MINGW32__
+# define lseek(a, b, c) _lseeki64((a), (b), (c))
 # define a2mkdir(path, openMode) mkdir(path)
 #else
 # define a2mkdir(path, openMode) mkdir(path, openMode)

+ 17 - 1
src/a2netcompat.h

@@ -34,6 +34,8 @@
 #ifndef _D_A2NETCOMPAT_H_
 #define _D_A2NETCOMPAT_H_
 
+#include "a2io.h"
+
 #ifdef HAVE_NETDB_H
 # include <netdb.h>
 #endif // HAVE_NETDB_H
@@ -58,16 +60,30 @@
 # include "inet_aton.h"
 #endif // HAVE_INET_ATON
 
-#ifdef __MINGW32__
+#ifdef HAVE_WINSOCK2_H
+#ifndef _WIN32_WINNT
+# define _WIN32_WINNT 0x501
+#endif // _WIN32_WINNT
 # include <winsock2.h>
 # undef ERROR
+#endif // HAVE_WINSOCK2_H
+
+#ifdef HAVE_WS2TCPIP_H
 # include <ws2tcpip.h>
+#endif // HAVE_WS2TCPIP_H
+
+#ifdef __MINGW32__
 # define SOCKOPT_T const char
 # define HAVE_GETADDRINFO
+# undef HAVE_GETADDRINFO
 #else
 # define SOCKOPT_T socklen_t
 #endif // __MINGW32__
 
+#ifndef HAVE_GAI_STRERROR
+# include "gai_strerror.h"
+#endif // HAVE_GAI_STRERROR
+
 #ifndef HAVE_GETADDRINFO
 # include "getaddrinfo.h"
 #endif // HAVE_GETADDRINFO

+ 52 - 0
src/a2time.h

@@ -0,0 +1,52 @@
+/*
+ * aria2 - The high speed download utility
+ *
+ * Copyright (C) 2006 Tatsuhiro Tsujikawa
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#ifndef _D_A2TIME_H_
+#define _D_A2TIME_H_
+
+#include <time.h>
+#include <sys/time.h>
+
+#ifndef HAVE_LOCALTIME_R
+# include "localtime_r.h"
+#endif // HAVE_LOCALTIME_R
+
+#ifndef HAVE_GETTIMEOFDAY
+# include "gettimeofday.h"
+#endif // HAVE_GETTIMEOFDAY
+
+#ifndef HAVE_STRPTIME
+# include "strptime.h"
+#endif // HAVE_STRPTIME
+
+#endif // _D_A2TIME_H_

+ 10 - 6
src/common.h

@@ -39,6 +39,16 @@
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #endif
+
+#ifdef __MINGW32__
+#ifdef malloc
+#	undef malloc
+#endif
+#ifdef realloc
+#	undef realloc
+#endif
+#endif // __MINGW32__
+
 #include <stdio.h>
 #include <stdint.h>
 #include <iostream>
@@ -82,12 +92,6 @@ typedef deque<int32_t> Integers;
 #define OPEN_MODE S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH
 #define DIR_OPEN_MODE S_IRWXU|S_IRWXG|S_IRWXO
 
-#ifdef __MINGW32__
-# ifndef _WIN32_WINNT
-#  define _WIN32_WINNT 0x501
-# endif // _WIN32_WINNT
-#endif // __MINGW32__
-
 #ifdef __MINGW32__
 # define LONGLONG_PRINTF "%I64d"
 # define ULONGLONG_PRINTF "%I64u"

+ 103 - 0
src/gai_strerror.c

@@ -0,0 +1,103 @@
+/*
+ * 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.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#ifdef ENABLE_NLS
+#include <libintl.h>
+#endif
+
+#include "gai_strerror.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")
+};
+
+/*
+ * gai_strerror().
+ */
+const char *
+gai_strerror(ecode)
+    int ecode;
+{
+    if (ecode < 0 || ecode > EAI_SYSTEM)
+	return _("Unknown error");
+
+    return gettext(eai_errlist[ecode]);
+}

+ 167 - 0
src/gai_strerror.h

@@ -0,0 +1,167 @@
+/*
+ * 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 _D_GETADDRINFO_H
+#define _D_GETADDRINFO_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif // HAVE_CONFIG_H
+
+/********************************************************************/
+/*
+ * Undefine all the macros.
+ * <netdb.h> 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.
+ * <netdb.h> might declares all or some of them.
+ */
+#if defined(HAVE_GETADDRINFO) || defined(HAVE_GETNAMEINFO) || defined(HAVE_GAI_STRERROR)
+#define gai_strerror my_gai_strerror
+#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
+
+/*
+ * Functions.
+ */
+#ifdef __STDC__
+const char *gai_strerror(int);
+#else
+const char *gai_strerror();
+#endif
+
+#ifdef __cplusplus
+};
+#endif /* __cplusplus */
+
+#endif /* not _D_GETADDRINFO_H */

+ 55 - 0
src/gettimeofday.c

@@ -0,0 +1,55 @@
+/* source: cygwin-1.5.24-2-src/cygwin-1.5.24-2/winsup/mingw/mingwex/gettimeofday.c */
+
+/*
+ * gettimeofday
+ * Implementation according to:
+ * The Open Group Base Specifications Issue 6
+ * IEEE Std 1003.1, 2004 Edition
+ */
+
+/*
+ *  THIS SOFTWARE IS NOT COPYRIGHTED
+ *
+ *  This source code is offered for use in the public domain. You may
+ *  use, modify or distribute it freely.
+ *
+ *  This code is distributed in the hope that it will be useful but
+ *  WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESS OR IMPLIED ARE HEREBY
+ *  DISCLAIMED. This includes but is not limited to warranties of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ *  Contributed by:
+ *  Danny Smith <dannysmith@users.sourceforge.net>
+ */
+
+#include <sys/time.h>
+
+#ifdef __MINGW32__
+
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+
+/* Offset between 1/1/1601 and 1/1/1970 in 100 nanosec units */
+#define _W32_FT_OFFSET (116444736000000000ULL)
+
+
+int __cdecl gettimeofday(struct timeval *__restrict__ tp,
+			 void *__restrict__ tzp __attribute__((unused)))
+ {
+  union {
+    unsigned long long ns100; /*time since 1 Jan 1601 in 100ns units */
+    FILETIME ft;
+  }  _now;
+
+  if(tp)
+    {
+      GetSystemTimeAsFileTime (&_now.ft);
+      tp->tv_usec=(long)((_now.ns100 / 10ULL) % 1000000ULL );
+      tp->tv_sec= (long)((_now.ns100 - _W32_FT_OFFSET) / 10000000ULL);
+    }
+  /* Always return 0 as per Open Group Base Specifications Issue 6.
+     Do not set errno on error.  */
+  return 0;
+}
+
+#endif // __MINGW32__

+ 57 - 0
src/gettimeofday.h

@@ -0,0 +1,57 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * Copyright (C) 2007 Tatsuhiro Tsujikawa
+ *
+ * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+
+#ifndef _D_GETTIMEOFDAY_H
+#define _D_GETTIMEOFDAY_H 1
+
+#include <sys/time.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+#ifndef HAVE_GETTIMEOFDAY
+
+int __cdecl gettimeofday(struct timeval *__restrict__ tp,
+			 void *__restrict__ tzp __attribute__((unused)));
+
+#endif // HAVE_GETTIMEOFDAY
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* not _D_GETTIMEOFDAY_H */
+

+ 18 - 4
src/localtime_r.c

@@ -34,28 +34,42 @@
 /* copyright --> */
 
 #include <time.h>
+ #include <stdlib.h>
 #ifdef __MINGW32__
+#define WIN32_LEAN_AND_MEAN
 #include <windows.h>
 #endif // __MINGW32__
 
 #include "localtime_r.h"
 
+#ifdef __MINGW32__
+
+static CRITICAL_SECTION localtime_r_cs;
+
+static void localtime_r_atexit()
+{
+	DeleteCriticalSection(&localtime_r_cs);
+}
+
+#endif // __MINGW32__
+
 struct tm * localtime_r(const time_t *clock, struct tm *result)
 {
 	static struct tm *local_tm;
 #ifdef __MINGW32__
-	static CRITICAL_SECTION cs;
 	static int initialized = 0;
 
 	if (!initialized) {
 		++initialized;
-		InitializeCriticalSection(&cs);
+		InitializeCriticalSection(&localtime_r_cs);
+		atexit(localtime_r_atexit);
 	}
 
-	EnterCriticalSection(&cs);
+	EnterCriticalSection(&localtime_r_cs);
 	local_tm = localtime(clock);
 	memcpy(result, local_tm, sizeof(struct tm));
-	LeaveCriticalSection(&cs);
+	LeaveCriticalSection(&localtime_r_cs);
 #endif // __MINGW32__
 	return result;
 };
+

+ 2 - 2
src/main.cc

@@ -35,7 +35,6 @@
 #include "common.h"
 #include "LogFactory.h"
 #include "Util.h"
-#include "prefs.h"
 #include "FeatureConfig.h"
 #include "MultiUrlRequestInfo.h"
 #include "TorrentRequestInfo.h"
@@ -55,10 +54,11 @@
 #include "a2algo.h"
 #include "message.h"
 #include "a2io.h"
+#include "a2time.h"
 #include "Platform.h"
+#include "prefs.h"
 #include <deque>
 #include <algorithm>
-#include <time.h>
 #include <signal.h>
 #include <unistd.h>
 #include <libgen.h>

+ 8 - 1
src/message.h

@@ -138,7 +138,11 @@
 #define EX_BAD_STATUS _("The response status is not successful. status=%d")
 #define EX_TOO_LARGE_FILE _("Too large file size. size=%s")
 #define EX_TRANSFER_ENCODING_NOT_SUPPORTED _("Transfer encoding %s is not supported.")
-#define EX_SSL_INIT_FAILURE _("SSL initialization failed.")
+#define EX_SSL_INIT_FAILURE _("SSL initialization failed: %s")
+#define EX_SSL_IO_ERROR _("SSL I/O error")
+#define EX_SSL_PROTOCOL_ERROR _("SSL protocol error")
+#define EX_SSL_UNKNOWN_ERROR _("SSL unknown error %d")
+#define EX_SSL_CONNECT_ERROR _("SSL initialization failed: OpenSSL connect error %d")
 #define EX_SIZE_MISMATCH _("Size mismatch Expected:%s Actual:%s")
 #define EX_AUTH_FAILED _("Authorization failed.")
 #define EX_GOT_EOF _("Got EOF from the server.")
@@ -160,6 +164,8 @@
 
 #define EX_SOCKET_OPEN _("Failed to open a socket, cause: %s")
 #define EX_SOCKET_SET_OPT _("Failed to set a socket option, cause: %s")
+#define EX_SOCKET_BLOCKING _("Failed to set a socket as blocking, cause: %s")
+#define EX_SOCKET_NONBLOCKING _("Failed to set a socket as non-blocking, cause: %s")
 #define EX_SOCKET_BIND _("Failed to bind a socket, cause: %s")
 #define EX_SOCKET_LISTEN _("Failed to listen to a socket, cause: %s")
 #define EX_SOCKET_ACCEPT _("Failed to accept a peer connection, cause: %s")
@@ -172,6 +178,7 @@
 #define EX_SOCKET_SEND _("Failed to send data, cause: %s")
 #define EX_SOCKET_RECV _("Failed to receive data, cause: %s")
 #define EX_SOCKET_PEEK _("Failed to peek data, cause: %s")
+#define EX_SOCKET_UNKNOWN_ERROR _("Unknown socket error %d (0x%x)")
 #define EX_FILE_ALREADY_EXISTS _("File %s exists, but %s does not exist.")
 #define EX_INVALID_PAYLOAD_SIZE _("Invalid payload size for %s, size=%d. It should be %d.")
 #define EX_INVALID_BT_MESSAGE_ID _("Invalid ID=%d for %s. It should be %d.")

+ 4 - 0
test/Makefile.in

@@ -249,8 +249,12 @@ GMSGFMT = @GMSGFMT@
 GREP = @GREP@
 HAVE_BASENAME_FALSE = @HAVE_BASENAME_FALSE@
 HAVE_BASENAME_TRUE = @HAVE_BASENAME_TRUE@
+HAVE_GAI_STRERROR_FALSE = @HAVE_GAI_STRERROR_FALSE@
+HAVE_GAI_STRERROR_TRUE = @HAVE_GAI_STRERROR_TRUE@
 HAVE_GETADDRINFO_FALSE = @HAVE_GETADDRINFO_FALSE@
 HAVE_GETADDRINFO_TRUE = @HAVE_GETADDRINFO_TRUE@
+HAVE_GETTIMEOFDAY_FALSE = @HAVE_GETTIMEOFDAY_FALSE@
+HAVE_GETTIMEOFDAY_TRUE = @HAVE_GETTIMEOFDAY_TRUE@
 HAVE_INET_ATON_FALSE = @HAVE_INET_ATON_FALSE@
 HAVE_INET_ATON_TRUE = @HAVE_INET_ATON_TRUE@
 HAVE_LOCALTIME_R_FALSE = @HAVE_LOCALTIME_R_FALSE@

+ 1 - 4
test/MultiDiskWriterTest.cc

@@ -1,12 +1,9 @@
 #include "common.h"
 #include "MultiDiskWriter.h"
+#include "a2io.h"
 #include <string>
 #include <cppunit/extensions/HelperMacros.h>
 
-#ifdef HAVE_IO_H
-# include <io.h>
-#endif // HAVE_IO_H
-
 using namespace std;
 
 class MultiDiskWriterTest:public CppUnit::TestFixture {

+ 1 - 3
test/PeerMessageUtilTest.cc

@@ -1,8 +1,6 @@
 #include "common.h"
 #include "PeerMessageUtil.h"
-#ifdef HAVE_NETINET_IN_H
-#include <netinet/in.h>
-#endif // HAVE_NETINET_IN_H
+#include "a2netcompat.h"
 #include <cppunit/extensions/HelperMacros.h>
 
 using namespace std;