summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.hgignore3
-rw-r--r--src/Net/ServerConnection.cpp48
-rw-r--r--src/Net/ServerConnection.h8
-rw-r--r--src/mad-core.cpp12
-rw-r--r--src/madc.cpp5
5 files changed, 24 insertions, 52 deletions
diff --git a/.hgignore b/.hgignore
index 98bbe1f..a8cfb71 100644
--- a/.hgignore
+++ b/.hgignore
@@ -35,6 +35,3 @@ syntax: regexp
syntax: regexp
^autom4te\.cache/
~$
-
-syntax: regexp
-^src/dh\.pem$ \ No newline at end of file
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;