improved error handling

This commit is contained in:
Arvid Norberg 2008-01-14 23:51:04 +00:00
parent 97511f2819
commit f7d34794a9
2 changed files with 33 additions and 19 deletions

View File

@ -333,7 +333,11 @@ namespace libtorrent
catch (std::exception& e) catch (std::exception& e)
{ {
// std::cerr << "DISK THREAD: exception: " << e.what() << std::endl; // std::cerr << "DISK THREAD: exception: " << e.what() << std::endl;
try
{
j.str = e.what(); j.str = e.what();
}
catch (std::exception&) {}
ret = -1; ret = -1;
} }

View File

@ -1,6 +1,6 @@
/* /*
Copyright (c) 2007, Un Shyam Copyright (c) 2007, Un Shyam & Arvid Norberg
All rights reserved. All rights reserved.
Redistribution and use in source and binary forms, with or without Redistribution and use in source and binary forms, with or without
@ -40,23 +40,33 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/pe_crypto.hpp" #include "libtorrent/pe_crypto.hpp"
#include "libtorrent/assert.hpp" #include "libtorrent/assert.hpp"
namespace libtorrent { namespace libtorrent
{
// Set the prime P and the generator, generate local public key // Set the prime P and the generator, generate local public key
DH_key_exchange::DH_key_exchange() DH_key_exchange::DH_key_exchange()
{ {
m_DH = DH_new(); m_DH = DH_new();
if (m_DH == 0) throw std::bad_alloc();
m_DH->p = BN_bin2bn(m_dh_prime, sizeof(m_dh_prime), NULL); m_DH->p = BN_bin2bn(m_dh_prime, sizeof(m_dh_prime), NULL);
m_DH->g = BN_bin2bn(m_dh_generator, sizeof(m_dh_generator), NULL); m_DH->g = BN_bin2bn(m_dh_generator, sizeof(m_dh_generator), NULL);
if (m_DH->p == 0 || m_DH->g == 0)
{
DH_free(m_DH);
throw std::bad_alloc();
}
m_DH->length = 160l; m_DH->length = 160l;
TORRENT_ASSERT(sizeof(m_dh_prime) == DH_size(m_DH)); TORRENT_ASSERT(sizeof(m_dh_prime) == DH_size(m_DH));
DH_generate_key (m_DH); // TODO Check != 0 DH_generate_key(m_DH);
if (m_DH->pub_key == 0)
TORRENT_ASSERT(m_DH->pub_key); {
DH_free(m_DH);
throw std::bad_alloc();
}
// DH can generate key sizes that are smaller than the size of // DH can generate key sizes that are smaller than the size of
// P with exponentially decreasing probability, in which case // P with exponentially decreasing probability, in which case
@ -93,10 +103,11 @@ namespace libtorrent {
{ {
TORRENT_ASSERT(remote_pubkey); TORRENT_ASSERT(remote_pubkey);
BIGNUM* bn_remote_pubkey = BN_bin2bn ((unsigned char*)remote_pubkey, 96, NULL); BIGNUM* bn_remote_pubkey = BN_bin2bn ((unsigned char*)remote_pubkey, 96, NULL);
if (bn_remote_pubkey == 0) throw std::bad_alloc();
char dh_secret[96]; char dh_secret[96];
int secret_size = DH_compute_key ( (unsigned char*)dh_secret, int secret_size = DH_compute_key((unsigned char*)dh_secret
bn_remote_pubkey, m_DH); // TODO Check for errors , bn_remote_pubkey, m_DH);
if (secret_size != 96) if (secret_size != 96)
{ {
@ -104,7 +115,6 @@ namespace libtorrent {
std::fill(m_dh_secret, m_dh_secret + 96 - secret_size, 0); std::fill(m_dh_secret, m_dh_secret + 96 - secret_size, 0);
} }
std::copy(dh_secret, dh_secret + secret_size, m_dh_secret + 96 - secret_size); std::copy(dh_secret, dh_secret + secret_size, m_dh_secret + 96 - secret_size);
BN_free(bn_remote_pubkey); BN_free(bn_remote_pubkey);
} }