mirror of https://github.com/aria2/aria2
Remove auto_delete in favor of std::unique_ptr
parent
e791eb9ca3
commit
05d85e2965
|
@ -349,7 +349,7 @@ AppleTLSSession::AppleTLSSession(AppleTLSContext* ctx)
|
||||||
state_ = st_error;
|
state_ = st_error;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto_delete<const void*> del_certs(certs, CFRelease);
|
std::unique_ptr<void, decltype(&CFRelease)> del_certs(certs, CFRelease);
|
||||||
lastError_ = SSLSetCertificate(sslCtx_, certs);
|
lastError_ = SSLSetCertificate(sslCtx_, certs);
|
||||||
if (lastError_ != noErr) {
|
if (lastError_ != noErr) {
|
||||||
A2_LOG_ERROR(fmt("AppleTLS: Failed to set credentials: %s", getLastErrorString().c_str()));
|
A2_LOG_ERROR(fmt("AppleTLS: Failed to set credentials: %s", getLastErrorString().c_str()));
|
||||||
|
|
|
@ -209,7 +209,9 @@ int GnuTLSSession::tlsConnect(const std::string& hostname,
|
||||||
if(rv_ != GNUTLS_E_SUCCESS) {
|
if(rv_ != GNUTLS_E_SUCCESS) {
|
||||||
return TLS_ERR_ERROR;
|
return TLS_ERR_ERROR;
|
||||||
}
|
}
|
||||||
auto_delete<gnutls_x509_crt_t> certDeleter(cert, gnutls_x509_crt_deinit);
|
std::unique_ptr<std::remove_pointer<gnutls_x509_crt_t>::type,
|
||||||
|
decltype(&gnutls_x509_crt_deinit)> certDeleter
|
||||||
|
(cert, gnutls_x509_crt_deinit);
|
||||||
rv_ = gnutls_x509_crt_import(cert, &peerCerts[0], GNUTLS_X509_FMT_DER);
|
rv_ = gnutls_x509_crt_import(cert, &peerCerts[0], GNUTLS_X509_FMT_DER);
|
||||||
if(rv_ != GNUTLS_E_SUCCESS) {
|
if(rv_ != GNUTLS_E_SUCCESS) {
|
||||||
return TLS_ERR_ERROR;
|
return TLS_ERR_ERROR;
|
||||||
|
|
|
@ -193,7 +193,8 @@ int OpenSSLTLSSession::tlsConnect(const std::string& hostname,
|
||||||
handshakeErr = "certificate not found";
|
handshakeErr = "certificate not found";
|
||||||
return TLS_ERR_ERROR;
|
return TLS_ERR_ERROR;
|
||||||
}
|
}
|
||||||
auto_delete<X509*> certDeleter(peerCert, X509_free);
|
std::unique_ptr<X509, decltype(&X509_free)> certDeleter
|
||||||
|
(peerCert, X509_free);
|
||||||
long verifyResult = SSL_get_verify_result(ssl_);
|
long verifyResult = SSL_get_verify_result(ssl_);
|
||||||
if(verifyResult != X509_V_OK) {
|
if(verifyResult != X509_V_OK) {
|
||||||
handshakeErr = X509_verify_cert_error_string(verifyResult);
|
handshakeErr = X509_verify_cert_error_string(verifyResult);
|
||||||
|
@ -206,8 +207,8 @@ int OpenSSLTLSSession::tlsConnect(const std::string& hostname,
|
||||||
altNames = reinterpret_cast<GENERAL_NAMES*>
|
altNames = reinterpret_cast<GENERAL_NAMES*>
|
||||||
(X509_get_ext_d2i(peerCert, NID_subject_alt_name, NULL, NULL));
|
(X509_get_ext_d2i(peerCert, NID_subject_alt_name, NULL, NULL));
|
||||||
if(altNames) {
|
if(altNames) {
|
||||||
auto_delete<GENERAL_NAMES*> altNamesDeleter
|
std::unique_ptr<GENERAL_NAMES, decltype(&GENERAL_NAMES_free)>
|
||||||
(altNames, GENERAL_NAMES_free);
|
altNamesDeleter(altNames, GENERAL_NAMES_free);
|
||||||
size_t n = sk_GENERAL_NAME_num(altNames);
|
size_t n = sk_GENERAL_NAME_num(altNames);
|
||||||
for(size_t i = 0; i < n; ++i) {
|
for(size_t i = 0; i < n; ++i) {
|
||||||
const GENERAL_NAME* altName = sk_GENERAL_NAME_value(altNames, i);
|
const GENERAL_NAME* altName = sk_GENERAL_NAME_value(altNames, i);
|
||||||
|
|
|
@ -58,7 +58,8 @@ void NameResolver::resolve(std::vector<std::string>& resolvedAddresses,
|
||||||
hostname.c_str(), gai_strerror(s)),
|
hostname.c_str(), gai_strerror(s)),
|
||||||
error_code::NAME_RESOLVE_ERROR);
|
error_code::NAME_RESOLVE_ERROR);
|
||||||
}
|
}
|
||||||
WSAAPI_AUTO_DELETE<struct addrinfo*> resDeleter(res, freeaddrinfo);
|
std::unique_ptr<addrinfo, decltype(&freeaddrinfo)> resDeleter
|
||||||
|
(res, freeaddrinfo);
|
||||||
struct addrinfo* rp;
|
struct addrinfo* rp;
|
||||||
for(rp = res; rp; rp = rp->ai_next) {
|
for(rp = res; rp; rp = rp->ai_next) {
|
||||||
std::pair<std::string, uint16_t> addressPort
|
std::pair<std::string, uint16_t> addressPort
|
||||||
|
|
|
@ -250,7 +250,8 @@ static sock_t bindTo
|
||||||
error = gai_strerror(s);
|
error = gai_strerror(s);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
WSAAPI_AUTO_DELETE<struct addrinfo*> resDeleter(res, freeaddrinfo);
|
std::unique_ptr<addrinfo, decltype(&freeaddrinfo)> resDeleter
|
||||||
|
(res, freeaddrinfo);
|
||||||
struct addrinfo* rp;
|
struct addrinfo* rp;
|
||||||
for(rp = res; rp; rp = rp->ai_next) {
|
for(rp = res; rp; rp = rp->ai_next) {
|
||||||
sock_t fd = bindInternal(rp->ai_family, rp->ai_socktype, rp->ai_protocol,
|
sock_t fd = bindInternal(rp->ai_family, rp->ai_socktype, rp->ai_protocol,
|
||||||
|
@ -409,7 +410,8 @@ void SocketCore::establishConnection(const std::string& host, uint16_t port,
|
||||||
if(s) {
|
if(s) {
|
||||||
throw DL_ABORT_EX(fmt(EX_RESOLVE_HOSTNAME, host.c_str(), gai_strerror(s)));
|
throw DL_ABORT_EX(fmt(EX_RESOLVE_HOSTNAME, host.c_str(), gai_strerror(s)));
|
||||||
}
|
}
|
||||||
WSAAPI_AUTO_DELETE<struct addrinfo*> resDeleter(res, freeaddrinfo);
|
std::unique_ptr<addrinfo, decltype(&freeaddrinfo)> resDeleter
|
||||||
|
(res, freeaddrinfo);
|
||||||
struct addrinfo* rp;
|
struct addrinfo* rp;
|
||||||
int errNum;
|
int errNum;
|
||||||
for(rp = res; rp; rp = rp->ai_next) {
|
for(rp = res; rp; rp = rp->ai_next) {
|
||||||
|
@ -888,7 +890,8 @@ ssize_t SocketCore::writeData(const void* data, size_t len,
|
||||||
if(s) {
|
if(s) {
|
||||||
throw DL_ABORT_EX(fmt(EX_SOCKET_SEND, gai_strerror(s)));
|
throw DL_ABORT_EX(fmt(EX_SOCKET_SEND, gai_strerror(s)));
|
||||||
}
|
}
|
||||||
WSAAPI_AUTO_DELETE<struct addrinfo*> resDeleter(res, freeaddrinfo);
|
std::unique_ptr<addrinfo, decltype(&freeaddrinfo)> resDeleter
|
||||||
|
(res, freeaddrinfo);
|
||||||
struct addrinfo* rp;
|
struct addrinfo* rp;
|
||||||
ssize_t r = -1;
|
ssize_t r = -1;
|
||||||
int errNum = 0;
|
int errNum = 0;
|
||||||
|
@ -1005,7 +1008,8 @@ void getInterfaceAddress
|
||||||
A2_LOG_INFO(fmt(MSG_INTERFACE_NOT_FOUND,
|
A2_LOG_INFO(fmt(MSG_INTERFACE_NOT_FOUND,
|
||||||
iface.c_str(), errorMsg(errNum).c_str()));
|
iface.c_str(), errorMsg(errNum).c_str()));
|
||||||
} else {
|
} else {
|
||||||
auto_delete<ifaddrs*> ifaddrDeleter(ifaddr, freeifaddrs);
|
std::unique_ptr<ifaddrs, decltype(&freeifaddrs)> ifaddrDeleter
|
||||||
|
(ifaddr, freeifaddrs);
|
||||||
for(ifaddrs* ifa = ifaddr; ifa; ifa = ifa->ifa_next) {
|
for(ifaddrs* ifa = ifaddr; ifa; ifa = ifa->ifa_next) {
|
||||||
if(!ifa->ifa_addr) {
|
if(!ifa->ifa_addr) {
|
||||||
continue;
|
continue;
|
||||||
|
@ -1044,7 +1048,8 @@ void getInterfaceAddress
|
||||||
if(s) {
|
if(s) {
|
||||||
A2_LOG_INFO(fmt(MSG_INTERFACE_NOT_FOUND, iface.c_str(), gai_strerror(s)));
|
A2_LOG_INFO(fmt(MSG_INTERFACE_NOT_FOUND, iface.c_str(), gai_strerror(s)));
|
||||||
} else {
|
} else {
|
||||||
WSAAPI_AUTO_DELETE<addrinfo*> resDeleter(res, freeaddrinfo);
|
std::unique_ptr<addrinfo, decltype(&freeaddrinfo)> resDeleter
|
||||||
|
(res, freeaddrinfo);
|
||||||
addrinfo* rp;
|
addrinfo* rp;
|
||||||
for(rp = res; rp; rp = rp->ai_next) {
|
for(rp = res; rp; rp = rp->ai_next) {
|
||||||
// Try to bind socket with this address. If it fails, the
|
// Try to bind socket with this address. If it fails, the
|
||||||
|
@ -1156,7 +1161,8 @@ size_t getBinAddr(void* dest, const std::string& ip)
|
||||||
0, AI_NUMERICHOST, 0) != 0) {
|
0, AI_NUMERICHOST, 0) != 0) {
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
WSAAPI_AUTO_DELETE<addrinfo*> resDeleter(res, freeaddrinfo);
|
std::unique_ptr<addrinfo, decltype(&freeaddrinfo)> resDeleter
|
||||||
|
(res, freeaddrinfo);
|
||||||
for(addrinfo* rp = res; rp; rp = rp->ai_next) {
|
for(addrinfo* rp = res; rp; rp = rp->ai_next) {
|
||||||
sockaddr_union su;
|
sockaddr_union su;
|
||||||
memcpy(&su, rp->ai_addr, rp->ai_addrlen);
|
memcpy(&su, rp->ai_addr, rp->ai_addrlen);
|
||||||
|
@ -1309,7 +1315,8 @@ void checkAddrconfig()
|
||||||
A2_LOG_INFO(fmt("getifaddrs failed. Cause: %s", errorMsg(errNum).c_str()));
|
A2_LOG_INFO(fmt("getifaddrs failed. Cause: %s", errorMsg(errNum).c_str()));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto_delete<ifaddrs*> ifaddrDeleter(ifaddr, freeifaddrs);
|
std::unique_ptr<ifaddrs, decltype(&freeifaddrs)> ifaddrDeleter
|
||||||
|
(ifaddr, freeifaddrs);
|
||||||
char host[NI_MAXHOST];
|
char host[NI_MAXHOST];
|
||||||
sockaddr_union ad;
|
sockaddr_union ad;
|
||||||
for(ifaddrs* ifa = ifaddr; ifa; ifa = ifa->ifa_next) {
|
for(ifaddrs* ifa = ifaddr; ifa; ifa = ifa->ifa_next) {
|
||||||
|
|
|
@ -54,33 +54,6 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
class auto_delete {
|
|
||||||
private:
|
|
||||||
T obj_;
|
|
||||||
void (*deleter_)(T);
|
|
||||||
public:
|
|
||||||
auto_delete(T obj, void (*deleter)(T)):obj_(obj), deleter_(deleter) {}
|
|
||||||
|
|
||||||
~auto_delete()
|
|
||||||
{
|
|
||||||
deleter_(obj_);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
class auto_delete_d {
|
|
||||||
private:
|
|
||||||
T obj_;
|
|
||||||
public:
|
|
||||||
auto_delete_d(T obj):obj_(obj) {}
|
|
||||||
|
|
||||||
~auto_delete_d()
|
|
||||||
{
|
|
||||||
delete obj_;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename T, typename R>
|
template<typename T, typename R>
|
||||||
class auto_delete_r {
|
class auto_delete_r {
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -108,26 +108,6 @@
|
||||||
# endif // !SHUT_WR
|
# endif // !SHUT_WR
|
||||||
#endif // __MINGW32__
|
#endif // __MINGW32__
|
||||||
|
|
||||||
#ifdef __MINGW32__
|
|
||||||
template<typename T>
|
|
||||||
class wsaapi_auto_delete {
|
|
||||||
private:
|
|
||||||
T obj_;
|
|
||||||
void (WSAAPI*deleter_)(T);
|
|
||||||
public:
|
|
||||||
wsaapi_auto_delete(T obj, void (WSAAPI*deleter)(T)):
|
|
||||||
obj_(obj), deleter_(deleter) {}
|
|
||||||
|
|
||||||
~wsaapi_auto_delete()
|
|
||||||
{
|
|
||||||
deleter_(obj_);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
# define WSAAPI_AUTO_DELETE wsaapi_auto_delete
|
|
||||||
#else // !__MINGW32__
|
|
||||||
# define WSAAPI_AUTO_DELETE auto_delete
|
|
||||||
#endif // !__MINGW32__
|
|
||||||
|
|
||||||
union sockaddr_union {
|
union sockaddr_union {
|
||||||
sockaddr sa;
|
sockaddr sa;
|
||||||
sockaddr_storage storage;
|
sockaddr_storage storage;
|
||||||
|
|
|
@ -89,7 +89,8 @@ void SocketCoreTest::testInetNtop()
|
||||||
addrinfo* res;
|
addrinfo* res;
|
||||||
CPPUNIT_ASSERT_EQUAL(0, callGetaddrinfo(&res, s.c_str(), 0, AF_INET,
|
CPPUNIT_ASSERT_EQUAL(0, callGetaddrinfo(&res, s.c_str(), 0, AF_INET,
|
||||||
SOCK_STREAM, 0, 0));
|
SOCK_STREAM, 0, 0));
|
||||||
WSAAPI_AUTO_DELETE<struct addrinfo*> resDeleter(res, freeaddrinfo);
|
std::unique_ptr<addrinfo, decltype(&freeaddrinfo)> resDeleter
|
||||||
|
(res, freeaddrinfo);
|
||||||
sockaddr_in addr;
|
sockaddr_in addr;
|
||||||
memcpy(&addr, res->ai_addr, sizeof(addr));
|
memcpy(&addr, res->ai_addr, sizeof(addr));
|
||||||
CPPUNIT_ASSERT_EQUAL(0, inetNtop(AF_INET, &addr.sin_addr,
|
CPPUNIT_ASSERT_EQUAL(0, inetNtop(AF_INET, &addr.sin_addr,
|
||||||
|
@ -101,7 +102,8 @@ void SocketCoreTest::testInetNtop()
|
||||||
addrinfo* res;
|
addrinfo* res;
|
||||||
CPPUNIT_ASSERT_EQUAL(0, callGetaddrinfo(&res, s.c_str(), 0, AF_INET6,
|
CPPUNIT_ASSERT_EQUAL(0, callGetaddrinfo(&res, s.c_str(), 0, AF_INET6,
|
||||||
SOCK_STREAM, 0, 0));
|
SOCK_STREAM, 0, 0));
|
||||||
WSAAPI_AUTO_DELETE<struct addrinfo*> resDeleter(res, freeaddrinfo);
|
std::unique_ptr<addrinfo, decltype(&freeaddrinfo)> resDeleter
|
||||||
|
(res, freeaddrinfo);
|
||||||
sockaddr_in6 addr;
|
sockaddr_in6 addr;
|
||||||
memcpy(&addr, res->ai_addr, sizeof(addr));
|
memcpy(&addr, res->ai_addr, sizeof(addr));
|
||||||
CPPUNIT_ASSERT_EQUAL(0, inetNtop(AF_INET6, &addr.sin6_addr,
|
CPPUNIT_ASSERT_EQUAL(0, inetNtop(AF_INET6, &addr.sin6_addr,
|
||||||
|
|
Loading…
Reference in New Issue