Просмотр исходного кода

Removed dependency on inet_aton

Implemented inetPton as a replacement of inet_aton. inetPton is
implemented using net::getBinAddr.

This change fixes bug#3525424.
Tatsuhiro Tsujikawa 13 лет назад
Родитель
Сommit
ca60020fa4
8 измененных файлов с 68 добавлено и 168 удалено
  1. 0 4
      configure.ac
  2. 0 4
      src/Makefile.am
  3. 35 7
      src/SocketCore.cc
  4. 8 1
      src/SocketCore.h
  5. 0 4
      src/a2netcompat.h
  6. 0 69
      src/inet_aton.c
  7. 0 79
      src/inet_aton.h
  8. 25 0
      test/SocketCoreTest.cc

+ 0 - 4
configure.ac

@@ -304,7 +304,6 @@ AC_SEARCH_LIBS([clock_gettime], [rt])
 case "$target" in
 	*solaris*)
                 AC_SEARCH_LIBS([getaddrinfo], [nsl socket])
-                AC_SEARCH_LIBS([inet_aton], [nsl socket])
 		;;
 esac
 
@@ -475,9 +474,6 @@ AC_CHECK_FUNCS([getaddrinfo],
 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)])
 AC_CHECK_FUNCS([localtime_r],
 	[AM_CONDITIONAL([HAVE_LOCALTIME_R], true)],
 	[AM_CONDITIONAL([HAVE_LOCALTIME_R], false)])

+ 0 - 4
src/Makefile.am

@@ -540,10 +540,6 @@ 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
-
 if !HAVE_LOCALTIME_R
 SRCS += localtime_r.c localtime_r.h
 endif # !HAVE_LOCALTIME_R

+ 35 - 7
src/SocketCore.cc

@@ -471,8 +471,9 @@ void SocketCore::setMulticastInterface(const std::string& localAddr)
   if(localAddr.empty()) {
     addr.s_addr = htonl(INADDR_ANY);
   } else {
-    if(inet_aton(localAddr.c_str(), &addr) == 0) {
-      throw DL_ABORT_EX(fmt("inet_aton failed for %s", localAddr.c_str()));
+    if(inetPton(AF_INET, localAddr.c_str(), &addr) != 0) {
+      throw DL_ABORT_EX(fmt("%s is not valid IPv4 numeric address",
+                            localAddr.c_str()));
     }
   }
   setSockOpt(IPPROTO_IP, IP_MULTICAST_IF, &addr, sizeof(addr));
@@ -493,15 +494,17 @@ void SocketCore::joinMulticastGroup
  const std::string& localAddr)
 {
   in_addr multiAddr;
-  if(inet_aton(multicastAddr.c_str(), &multiAddr) == 0) {
-    throw DL_ABORT_EX(fmt("inet_aton failed for %s", multicastAddr.c_str()));
+  if(inetPton(AF_INET, multicastAddr.c_str(), &multiAddr) != 0) {
+    throw DL_ABORT_EX(fmt("%s is not valid IPv4 numeric address",
+                          multicastAddr.c_str()));
   }
   in_addr ifAddr;
   if(localAddr.empty()) {
     ifAddr.s_addr = htonl(INADDR_ANY);
   } else {
-    if(inet_aton(localAddr.c_str(), &ifAddr) == 0) {
-      throw DL_ABORT_EX(fmt("inet_aton failed for %s", localAddr.c_str()));
+    if(inetPton(AF_INET, localAddr.c_str(), &ifAddr) != 0) {
+      throw DL_ABORT_EX(fmt("%s is not valid IPv4 numeric address",
+                            localAddr.c_str()));
     }
   }
   struct ip_mreq mreq;
@@ -1299,9 +1302,34 @@ int inetNtop(int af, const void* src, char* dst, socklen_t size)
   return s;
 }
 
+int inetPton(int af, const char* src, void* dst)
+{
+  union {
+    uint32_t ipv4_addr;
+    unsigned char ipv6_addr[16];
+  } binaddr;
+  size_t len = net::getBinAddr(binaddr.ipv6_addr, src);
+  if(af == AF_INET) {
+    if(len != 4) {
+      return -1;
+    }
+    in_addr* addr = reinterpret_cast<in_addr*>(dst);
+    addr->s_addr = binaddr.ipv4_addr;
+  } else if(af == AF_INET6) {
+    if(len != 16) {
+      return -1;
+    }
+    in6_addr* addr = reinterpret_cast<in6_addr*>(dst);
+    memcpy(addr->s6_addr, binaddr.ipv6_addr, sizeof(addr->s6_addr));
+  } else {
+    return -1;
+  }
+  return 0;
+}
+
 namespace net {
 
-size_t getBinAddr(unsigned char* dest, const std::string& ip)
+size_t getBinAddr(void* dest, const std::string& ip)
 {
   size_t len = 0;
   addrinfo* res;

+ 8 - 1
src/SocketCore.h

@@ -381,6 +381,13 @@ void getInterfaceAddress
 // message using gai_strerror(3).
 int inetNtop(int af, const void* src, char* dst, socklen_t size);
 
+// Provides functionality of inet_pton using getBinAddr.  If af is
+// AF_INET, dst is assumed to be the pointer to struct in_addr.  If af
+// is AF_INET6, dst is assumed to be the pointer to struct in6_addr.
+//
+// This function returns 0 if it succeeds, or -1.
+int inetPton(int af, const char* src, void* dst);
+
 namespace net {
 
 // Stores binary representation of IP address ip which is represented
@@ -389,7 +396,7 @@ namespace net {
 // at least 4. For IPv6 address, dest must be at least 16. Returns the
 // number of bytes written in dest, that is 4 for IPv4 and 16 for
 // IPv6. Return 0 if error occurred.
-size_t getBinAddr(unsigned char* dest, const std::string& ip);
+size_t getBinAddr(void* dest, const std::string& ip);
 
 // Verifies hostname against presented identifiers in the certificate.
 // The implementation is based on the procedure described in RFC 6125.

+ 0 - 4
src/a2netcompat.h

@@ -87,10 +87,6 @@
 # include <netinet/in.h>
 #endif // HAVE_NETINET_IN_H
 
-#ifndef HAVE_INET_ATON
-# include "inet_aton.h"
-#endif // HAVE_INET_ATON
-
 #ifndef HAVE_GETADDRINFO
 # include "getaddrinfo.h"
 # define HAVE_GAI_STRERROR

+ 0 - 69
src/inet_aton.c

@@ -1,69 +0,0 @@
-/* <!-- copyright */
-/*
- * 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 --> */
-
-#ifdef HAVE_CONFIG_H
-# include "config.h"
-#endif // HAVE_CONFIG_H
-
-#ifdef __MINGW32__
-# define _WIN32_WINNT 0x501
-# include <winsock2.h>
-# undef ERROR
-# include <ws2tcpip.h>
-#endif // __MINGW32__
-
-#ifdef HAVE_NETDB_H
-# include <netdb.h>
-#endif // HAVE_NETDB_H
-#ifdef HAVE_SYS_SOCKET_H
-# include <sys/socket.h>
-#endif // HAVE_SYS_SOCKET_H
-#ifdef HAVE_NETINET_IN_H
-# include <netinet/in.h>
-#endif // HAVE_NETINET_IN_H
-#ifdef HAVE_ARPA_INET_H
-# include <arpa/inet.h>
-#endif // HAVE_ARPA_INET_H
-
-#include <string.h>
-
-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;
-}

+ 0 - 79
src/inet_aton.h

@@ -1,79 +0,0 @@
-/* <!-- copyright */
-/*
- * 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_INET_ATON_H
-#define _D_INET_ATON_H
-
-#ifdef __cplusplus
-extern "C" {
-#endif /* __cplusplus */
-
-#ifdef __MINGW32__
-# undef SIZE_MAX
-#endif // __MINGW32__
-
-#ifdef HAVE_CONFIG_H
-# include "config.h"
-#endif // HAVE_CONFIG_H
-
-#ifdef __MINGW32__
-# ifndef _WIN32_WINNT
-#  define _WIN32_WINNT 0x501
-# endif // _WIN32_WINNT
-# include <winsock2.h>
-# undef ERROR
-# include <ws2tcpip.h>
-#endif // __MINGW32__
-
-#ifdef HAVE_NETDB_H
-# include <netdb.h>
-#endif // HAVE_NETDB_H
-#ifdef HAVE_SYS_SOCKET_H
-# include <sys/socket.h>
-#endif // HAVE_SYS_SOCKET_H
-#ifdef HAVE_NETINET_IN_H
-# include <netinet/in.h>
-#endif // HAVE_NETINET_IN_H
-#ifdef HAVE_ARPA_INET_H
-# include <arpa/inet.h>
-#endif // HAVE_ARPA_INET_H
-
-int inet_aton(const char *cp, struct in_addr *inp);
-
-#ifdef __cplusplus
-}
-#endif /* __cplusplus */
-
-#endif /* not _D_INET_ATON_H */

+ 25 - 0
test/SocketCoreTest.cc

@@ -15,6 +15,7 @@ class SocketCoreTest:public CppUnit::TestFixture {
   CPPUNIT_TEST(testWriteAndReadDatagram);
   CPPUNIT_TEST(testGetSocketError);
   CPPUNIT_TEST(testInetNtop);
+  CPPUNIT_TEST(testInetPton);
   CPPUNIT_TEST(testGetBinAddr);
   CPPUNIT_TEST(testVerifyHostname);
   CPPUNIT_TEST_SUITE_END();
@@ -26,6 +27,7 @@ public:
   void testWriteAndReadDatagram();
   void testGetSocketError();
   void testInetNtop();
+  void testInetPton();
   void testGetBinAddr();
   void testVerifyHostname();
 };
@@ -108,6 +110,29 @@ void SocketCoreTest::testInetNtop()
   }
 }
 
+void SocketCoreTest::testInetPton()
+{
+  {
+    const char ipaddr[] = "192.168.0.1";
+    uint32_t ans;
+    CPPUNIT_ASSERT_EQUAL((size_t)4, net::getBinAddr(&ans, ipaddr));
+    in_addr dest;
+    CPPUNIT_ASSERT_EQUAL(0, inetPton(AF_INET, ipaddr, &dest));
+    CPPUNIT_ASSERT(ans == dest.s_addr);
+  }
+  {
+    const char ipaddr[] = "2001:db8::2:1";
+    unsigned char ans[16];
+    CPPUNIT_ASSERT_EQUAL((size_t)16, net::getBinAddr(ans, ipaddr));
+    in6_addr dest;
+    CPPUNIT_ASSERT_EQUAL(0, inetPton(AF_INET6, ipaddr, &dest));
+    CPPUNIT_ASSERT(memcmp(ans, &dest, sizeof(ans)) == 0);
+  }
+  unsigned char dest[16];
+  CPPUNIT_ASSERT_EQUAL(-1, inetPton(AF_INET, "localhost", &dest));
+  CPPUNIT_ASSERT_EQUAL(-1, inetPton(AF_INET6, "localhost", &dest));
+}
+
 void SocketCoreTest::testGetBinAddr()
 {
   unsigned char dest[16];