diff options
author | Matthias Schiffer <matthias@gamezock.de> | 2008-06-12 22:51:52 +0200 |
---|---|---|
committer | Matthias Schiffer <matthias@gamezock.de> | 2008-06-12 22:51:52 +0200 |
commit | fb86311df4e1db902cbb6f527ad6c038543973a8 (patch) | |
tree | ddf09e4af448c9bf36f2272b656117e71ee6be05 /src | |
parent | 09e0fc185219e9e625baf096a68a221be55e0284 (diff) | |
download | mad-fb86311df4e1db902cbb6f527ad6c038543973a8.tar mad-fb86311df4e1db902cbb6f527ad6c038543973a8.zip |
Einige Verbesserungen
Diffstat (limited to 'src')
-rw-r--r-- | src/Net/ServerConnection.cpp | 48 | ||||
-rw-r--r-- | src/Net/ServerConnection.h | 8 | ||||
-rw-r--r-- | src/mad-core.cpp | 12 | ||||
-rw-r--r-- | src/madc.cpp | 5 |
4 files changed, 24 insertions, 49 deletions
diff --git a/src/Net/ServerConnection.cpp b/src/Net/ServerConnection.cpp index 4a0bf15..1e0959d 100644 --- a/src/Net/ServerConnection.cpp +++ b/src/Net/ServerConnection.cpp @@ -27,38 +27,7 @@ namespace Mad { namespace Net { -bool ServerConnection::loadDHParams(const std::string &file) { - std::ifstream stream(file.c_str()); - - if(!stream.is_open()) - return false; - - std::string data, line; - - while(!stream.eof()) { - std::getline(stream, line); - data += line + "\n"; - } - - stream.close(); - - gnutls_datum_t datum; - datum.data = new unsigned char[data.size()]; - datum.size = data.size(); - - std::memcpy(datum.data, data.data(), data.size()); - - int ret = gnutls_dh_params_import_pkcs3(dh_params, &datum, GNUTLS_X509_FMT_PEM); - - if(ret == 0) - gnutls_anon_set_server_dh_params(anoncred, dh_params); - - delete [] datum.data; - - return (ret == 0); -} - -bool ServerConnection::listen(const IPAddress &address) { +void ServerConnection::listen(const IPAddress &address) throw(ConnectionException) { const int kx_list[] = {GNUTLS_KX_ANON_DH, 0}; if(connected) @@ -66,27 +35,30 @@ bool ServerConnection::listen(const IPAddress &address) { int listen_sock = socket(PF_INET, SOCK_STREAM, 0); if(listen_sock < 0) - return false; + throw ConnectionException("socket()", std::strerror(errno)); peer = new IPAddress(address); if(bind(listen_sock, peer->getSockAddr(), peer->getSockAddrLength()) < 0) { close(listen_sock); delete peer; - return false; + + throw ConnectionException("bind()", std::strerror(errno)); } if(::listen(listen_sock, 1024) < 0) { close(listen_sock); delete peer; - return false; + + throw ConnectionException("listen()", std::strerror(errno)); } sock = accept(listen_sock, NULL, NULL); if(sock < 0) { close(listen_sock); delete peer; - return false; + + throw ConnectionException("accept()", std::strerror(errno)); } close(listen_sock); @@ -106,10 +78,8 @@ bool ServerConnection::listen(const IPAddress &address) { if(ret < 0) { disconnect(); - return false; + throw ConnectionException("gnutls_handshake()", gnutls_strerror(ret)); } - - return true; } void ServerConnection::disconnect() { diff --git a/src/Net/ServerConnection.h b/src/Net/ServerConnection.h index a1e839f..09f9d6b 100644 --- a/src/Net/ServerConnection.h +++ b/src/Net/ServerConnection.h @@ -21,6 +21,7 @@ #define MAD_NET_SERVERCONNECTION_H_ #include "Connection.h" +#include "ConnectionException.h" #include <string> namespace Mad { @@ -44,7 +45,10 @@ class ServerConnection : public Connection { public: ServerConnection() : connected(false) { gnutls_anon_allocate_server_credentials(&anoncred); + gnutls_dh_params_init(&dh_params); + gnutls_dh_params_generate2(dh_params, 768); + gnutls_anon_set_server_dh_params(anoncred, dh_params); } virtual ~ServerConnection() { @@ -55,9 +59,7 @@ class ServerConnection : public Connection { gnutls_anon_free_server_credentials(anoncred); } - bool loadDHParams(const std::string &file); - - bool listen(const IPAddress &address); + void listen(const IPAddress &address) throw(ConnectionException); void disconnect(); virtual bool dataPending(); diff --git a/src/mad-core.cpp b/src/mad-core.cpp index 219621f..76e7b9a 100644 --- a/src/mad-core.cpp +++ b/src/mad-core.cpp @@ -35,11 +35,13 @@ int main() { connection.signalRecieve().connect(sigc::ptr_fun(recieveHandler)); - connection.loadDHParams("dh.pem"); - - connection.listen(Mad::Net::IPAddress("0.0.0.0", 6666)); - - while(!connection.recieve()); + try { + connection.listen(Mad::Net::IPAddress("0.0.0.0", 6666)); + while(!connection.recieve()); + } + catch(Mad::Net::Exception &e) { + std::cerr << "Connection error: " << e.what() << std::endl; + } connection.disconnect(); diff --git a/src/madc.cpp b/src/madc.cpp index a4fc2bf..9d525b2 100644 --- a/src/madc.cpp +++ b/src/madc.cpp @@ -29,13 +29,14 @@ int main() { try { connection.connect(Mad::Net::IPAddress("127.0.0.1", 6666)); - std::cout << connection.send(Mad::Net::Packet(0x0001, 0xABCD)) << std::endl; - connection.disconnect(); + connection.send(Mad::Net::Packet(0x0001, 0xABCD)); } catch(Mad::Net::Exception &e) { std::cerr << "Connection error: " << e.what() << std::endl; } + connection.disconnect(); + Mad::Net::Connection::deinit(); return 0; |