diff --git a/src/crypto/crypto.h b/src/crypto/crypto.h index c2eb996..1e467a6 100644 --- a/src/crypto/crypto.h +++ b/src/crypto/crypto.h @@ -38,18 +38,21 @@ #include #include #include +#include using std::string; long int myatoi( const char *str ); namespace Crypto { - class CryptoException { + class CryptoException : public std::exception { public: string text; bool fatal; CryptoException( string s_text, bool s_fatal = false ) : text( s_text ), fatal( s_fatal ) {}; + const char *what() const throw () { return text.c_str(); } + ~CryptoException() throw () {} }; /* 16-byte-aligned buffer, with length. */ diff --git a/src/examples/decrypt.cc b/src/examples/decrypt.cc index 26c7a8c..f9868c5 100644 --- a/src/examples/decrypt.cc +++ b/src/examples/decrypt.cc @@ -62,7 +62,7 @@ int main( int argc, char *argv[] ) (long)message.nonce.val() ); cout << message.text; } catch ( const CryptoException &e ) { - cerr << e.text << endl; + cerr << e.what() << endl; exit( 1 ); } diff --git a/src/examples/encrypt.cc b/src/examples/encrypt.cc index d7a5588..b71cac5 100644 --- a/src/examples/encrypt.cc +++ b/src/examples/encrypt.cc @@ -63,7 +63,7 @@ int main( int argc, char *argv[] ) cout << ciphertext; } catch ( const CryptoException &e ) { - cerr << e.text << endl; + cerr << e.what() << endl; exit( 1 ); } diff --git a/src/examples/ntester.cc b/src/examples/ntester.cc index 1ca23d3..fe20827 100644 --- a/src/examples/ntester.cc +++ b/src/examples/ntester.cc @@ -66,7 +66,7 @@ int main( int argc, char *argv[] ) n = new Transport( me, remote, NULL, NULL ); } } catch ( const CryptoException &e ) { - fprintf( stderr, "Fatal error: %s\n", e.text.c_str() ); + fprintf( stderr, "Fatal error: %s\n", e.what() ); exit( 1 ); } @@ -98,7 +98,7 @@ int main( int argc, char *argv[] ) } } } catch ( const CryptoException &e ) { - fprintf( stderr, "Cryptographic error: %s\n", e.text.c_str() ); + fprintf( stderr, "Cryptographic error: %s\n", e.what() ); } } } else { @@ -164,10 +164,10 @@ int main( int argc, char *argv[] ) n->recv(); } } catch ( const NetworkException &e ) { - fprintf( stderr, "%s: %s\r\n", e.function.c_str(), strerror( e.the_errno ) ); + fprintf( stderr, "%s\n", e.what() ); break; } catch ( const CryptoException &e ) { - fprintf( stderr, "Cryptographic error: %s\n", e.text.c_str() ); + fprintf( stderr, "Cryptographic error: %s\n", e.what() ); } } diff --git a/src/frontend/mosh-client.cc b/src/frontend/mosh-client.cc index fcf8b7a..b7715df 100644 --- a/src/frontend/mosh-client.cc +++ b/src/frontend/mosh-client.cc @@ -34,7 +34,6 @@ #include "version.h" #include -#include #include #include "stmclient.h" @@ -177,11 +176,11 @@ int main( int argc, char *argv[] ) client.shutdown(); } catch ( const Network::NetworkException& e ) { - fprintf( stderr, "Network exception: %s: %s\r\n", - e.function.c_str(), strerror( e.the_errno ) ); + fprintf( stderr, "Network exception: %s\r\n", + e.what() ); } catch ( const Crypto::CryptoException& e ) { fprintf( stderr, "Crypto exception: %s\r\n", - e.text.c_str() ); + e.what() ); } catch ( const std::string& s ) { fprintf( stderr, "Error: %s\r\n", s.c_str() ); } diff --git a/src/frontend/mosh-server.cc b/src/frontend/mosh-server.cc index 6b9443e..f2b22d1 100644 --- a/src/frontend/mosh-server.cc +++ b/src/frontend/mosh-server.cc @@ -311,12 +311,12 @@ int main( int argc, char *argv[] ) try { return run_server( desired_ip, desired_port, command_path, command_argv, colors, verbose, with_motd ); } catch ( const Network::NetworkException& e ) { - fprintf( stderr, "Network exception: %s: %s\n", - e.function.c_str(), strerror( e.the_errno ) ); + fprintf( stderr, "Network exception: %s\n", + e.what() ); return 1; } catch ( const Crypto::CryptoException& e ) { fprintf( stderr, "Crypto exception: %s\n", - e.text.c_str() ); + e.what() ); return 1; } } @@ -491,11 +491,11 @@ int run_server( const char *desired_ip, const char *desired_port, try { serve( master, terminal, *network ); } catch ( const Network::NetworkException& e ) { - fprintf( stderr, "Network exception: %s: %s\n", - e.function.c_str(), strerror( e.the_errno ) ); + fprintf( stderr, "Network exception: %s\n", + e.what() ); } catch ( const Crypto::CryptoException& e ) { fprintf( stderr, "Crypto exception: %s\n", - e.text.c_str() ); + e.what() ); } #ifdef HAVE_UTEMPTER @@ -727,13 +727,13 @@ void serve( int host_fd, Terminal::Complete &terminal, ServerConnection &network network.tick(); } catch ( const Network::NetworkException& e ) { - fprintf( stderr, "%s: %s\n", e.function.c_str(), strerror( e.the_errno ) ); + fprintf( stderr, "%s\n", e.what() ); spin(); } catch ( const Crypto::CryptoException& e ) { if ( e.fatal ) { throw; } else { - fprintf( stderr, "Crypto exception: %s\n", e.text.c_str() ); + fprintf( stderr, "Crypto exception: %s\n", e.what() ); } } } diff --git a/src/frontend/stmclient.cc b/src/frontend/stmclient.cc index daee1f6..651cd3e 100644 --- a/src/frontend/stmclient.cc +++ b/src/frontend/stmclient.cc @@ -568,7 +568,7 @@ void STMClient::main( void ) throw; } else { wchar_t tmp[ 128 ]; - swprintf( tmp, 128, L"Crypto exception: %s", e.text.c_str() ); + swprintf( tmp, 128, L"Crypto exception: %s", e.what() ); overlays.get_notification_engine().set_notification_string( wstring( tmp ) ); } } diff --git a/src/frontend/terminaloverlay.h b/src/frontend/terminaloverlay.h index e196019..f6109c8 100644 --- a/src/frontend/terminaloverlay.h +++ b/src/frontend/terminaloverlay.h @@ -40,6 +40,7 @@ #include #include +#include namespace Overlay { using namespace Terminal; @@ -173,10 +174,10 @@ namespace Overlay { show_quit_keystroke = s_show_quit_keystroke; } - void set_network_exception( const NetworkException &e ) + void set_network_exception( const std::exception &e ) { wchar_t tmp[ 128 ]; - swprintf( tmp, 128, L"%s: %s", e.function.c_str(), strerror( e.the_errno ) ); + swprintf( tmp, 128, L"%s", e.what() ); message = tmp; message_is_network_exception = true; diff --git a/src/network/network.cc b/src/network/network.cc index e55259c..03db52f 100644 --- a/src/network/network.cc +++ b/src/network/network.cc @@ -260,9 +260,9 @@ Connection::Connection( const char *desired_ip, const char *desired_port ) /* se try { if ( try_bind( desired_ip, desired_port_low, desired_port_high ) ) { return; } } catch ( const NetworkException& e ) { - fprintf( stderr, "Error binding to IP %s: %s: %s\n", + fprintf( stderr, "Error binding to IP %s: %s\n", desired_ip, - e.function.c_str(), strerror( e.the_errno ) ); + e.what() ); } } @@ -270,8 +270,8 @@ Connection::Connection( const char *desired_ip, const char *desired_port ) /* se try { if ( try_bind( NULL, desired_port_low, desired_port_high ) ) { return; } } catch ( const NetworkException& e ) { - fprintf( stderr, "Error binding to any interface: %s: %s\n", - e.function.c_str(), strerror( e.the_errno ) ); + fprintf( stderr, "Error binding to any interface: %s\n", + e.what() ); throw; /* this time it's fatal */ } diff --git a/src/network/network.h b/src/network/network.h index c248ff7..6672572 100644 --- a/src/network/network.h +++ b/src/network/network.h @@ -41,6 +41,8 @@ #include #include #include +#include +#include #include "crypto.h" @@ -53,12 +55,18 @@ namespace Network { uint16_t timestamp16( void ); uint16_t timestamp_diff( uint16_t tsnew, uint16_t tsold ); - class NetworkException { + class NetworkException : public std::exception { public: string function; int the_errno; - NetworkException( string s_function, int s_errno ) : function( s_function ), the_errno( s_errno ) {} - NetworkException() : function( "" ), the_errno( 0 ) {} + private: + string my_what; + public: + NetworkException( string s_function="", int s_errno=0) + : function( s_function ), the_errno( s_errno ), + my_what(function + ": " + strerror(the_errno)) {} + const char *what() const throw () { return my_what.c_str(); } + ~NetworkException() throw () {} }; enum Direction { diff --git a/src/tests/encrypt-decrypt.cc b/src/tests/encrypt-decrypt.cc index 685780f..1f82a48 100644 --- a/src/tests/encrypt-decrypt.cc +++ b/src/tests/encrypt-decrypt.cc @@ -144,7 +144,7 @@ int main( int argc, char *argv[] ) { test_one_session(); } catch ( const CryptoException& e ) { fprintf( stderr, "Crypto exception: %s\r\n", - e.text.c_str() ); + e.what() ); fatal_assert( false ); } }