diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2012-05-14 01:17:50 +0900 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2012-05-14 01:17:50 +0900 |
| commit | ca60020fa4230b27c0c3ded1ddac375061a027a9 (patch) | |
| tree | e84332d21b1bbfa7093e63c465b5c9e14faf6e32 /src | |
| parent | 06405ce97b3da52241adb3f6732846f824823951 (diff) | |
Removed dependency on inet_aton
Implemented inetPton as a replacement of inet_aton. inetPton is
implemented using net::getBinAddr.
This change fixes bug#3525424.
Diffstat (limited to 'src')
| -rw-r--r-- | src/Makefile.am | 4 | ||||
| -rw-r--r-- | src/SocketCore.cc | 42 | ||||
| -rw-r--r-- | src/SocketCore.h | 9 | ||||
| -rw-r--r-- | src/a2netcompat.h | 4 | ||||
| -rw-r--r-- | src/inet_aton.c | 69 | ||||
| -rw-r--r-- | src/inet_aton.h | 79 |
6 files changed, 43 insertions, 164 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 453ded74..04112fe1 100644 --- a/src/Makefile.am +++ b/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 diff --git a/src/SocketCore.cc b/src/SocketCore.cc index f619eb13..7c3c1da6 100644 --- a/src/SocketCore.cc +++ b/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; diff --git a/src/SocketCore.h b/src/SocketCore.h index 4fad452e..bbf2fe39 100644 --- a/src/SocketCore.h +++ b/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. diff --git a/src/a2netcompat.h b/src/a2netcompat.h index 3e2e8ccc..41378307 100644 --- a/src/a2netcompat.h +++ b/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 diff --git a/src/inet_aton.c b/src/inet_aton.c deleted file mode 100644 index c408ee75..00000000 --- a/src/inet_aton.c +++ /dev/null @@ -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; -} diff --git a/src/inet_aton.h b/src/inet_aton.h deleted file mode 100644 index 29ac525f..00000000 --- a/src/inet_aton.h +++ /dev/null @@ -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 */ |
