Browse Source

Fixed MinGW non-blocking bug.
* src/SocketCore.cc
* src/HttpRequestCommand.cc

Added src/timegm,{h,c}

Tatsuhiro Tsujikawa 18 years ago
parent
commit
834625364f
5 changed files with 145 additions and 10 deletions
  1. 4 0
      ChangeLog
  2. 0 4
      src/HttpRequestCommand.cc
  3. 13 6
      src/SocketCore.cc
  4. 71 0
      src/timegm.c
  5. 57 0
      src/timegm.h

+ 4 - 0
ChangeLog

@@ -62,6 +62,10 @@
 	* src/Util.cc:
 	* src/strptime.c: Added support for %Z option.
 
+	Fixed MinGW non-blocking bug.
+	* src/SocketCore.cc
+	* src/HttpRequestCommand.cc
+
 	Miscellenous build fixes/enhancements.
 	* configure.ac: Added summary report.
 	* src/Platform.h: Tweaked #include's.

+ 0 - 4
src/HttpRequestCommand.cc

@@ -52,10 +52,6 @@ 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)) {

+ 13 - 6
src/SocketCore.cc

@@ -188,16 +188,21 @@ 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 && SOCKET_ERRNO != EINPROGRESS) {
+  if(connect(sockfd, (struct sockaddr*)&sockaddr, (socklen_t)sizeof(sockaddr)) == -1 && SOCKET_ERRNO != 
+#ifndef __MINGW32__
+EINPROGRESS
+#else
+WSAEWOULDBLOCK
+#endif // __MINGW32__
+  ) {
     throw new DlAbortEx(EX_SOCKET_CONNECT, host.c_str(), errorMsg());
   }
 }
 
 void SocketCore::setNonBlockingMode() {
 #ifdef __MINGW32__
-  u_long flag = 0;
-  ::ioctlsocket(sockfd, FIONBIO, &flag);
-  if (WSAGetLastError()) {
+  static u_long flag = 1;
+  if (::ioctlsocket(sockfd, FIONBIO, &flag) == -1) {
     throw new DlAbortEx(EX_SOCKET_NONBLOCKING, errorMsg());
   }
 #else
@@ -210,8 +215,10 @@ void SocketCore::setNonBlockingMode() {
 
 void SocketCore::setBlockingMode() {
 #ifdef __MINGW32__
-  u_long flag = 1;
-  ::ioctlsocket(sockfd, FIONBIO, &flag);
+  static u_long flag = 0;
+  if (::ioctlsocket(sockfd, FIONBIO, &flag) == -1) {
+    throw new DlAbortEx(EX_SOCKET_BLOCKING, errorMsg());
+  }
 #else
   int32_t flags = fcntl(sockfd, F_GETFL, 0);
   // TODO add error handling

+ 71 - 0
src/timegm.c

@@ -0,0 +1,71 @@
+/* timegm.c - libc replacement function
+ * Copyright (C) 2004 Free Software Foundation, Inc.
+ *
+ * This file is part of GnuPG.
+ *
+ * GnuPG 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.
+ *
+ * GnuPG 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.
+ */
+
+/*
+  timegm() is a GNU function that might not be available everywhere.
+  It's basically the inverse of gmtime() - you give it a struct tm,
+  and get back a time_t.  It differs from mktime() in that it handles
+  the case where the struct tm is UTC and the local environment isn't.
+
+  Some BSDs don't handle the putenv("foo") case properly, so we use
+  unsetenv if the platform has it to remove environment variables.
+*/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif // HAVE_CONFIG_H
+
+#include <time.h>
+#include <stdlib.h>
+#include <string.h>
+
+time_t
+timegm(struct tm *tm)
+{
+  time_t answer;
+  char *zone;
+
+  zone=getenv("TZ");
+  putenv("TZ=UTC");
+  tzset();
+  answer=mktime(tm);
+  if(zone)
+    {
+      char *old_zone;
+
+      old_zone=malloc(3+strlen(zone)+1);
+      if(old_zone)
+	{
+	  strcpy(old_zone,"TZ=");
+	  strcat(old_zone,zone);
+	  putenv(old_zone);
+	}
+    }
+  else
+#ifdef HAVE_UNSETENV
+    unsetenv("TZ");
+#else
+    putenv("TZ=");
+#endif
+
+  tzset();
+  return answer;
+}

+ 57 - 0
src/timegm.h

@@ -0,0 +1,57 @@
+/*
+ * 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_TIMEGM_H_
+#define _D_TIMEGM_H_
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif // HAVE_CONFIG_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+#include <time.h>
+
+#ifndef HAVE_TIMEGM
+
+time_t timegm(struct tm *tm);
+
+#endif // HAVE_TIMEGM
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif // _D_TIMEGM_H_