diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.in | 3 | ||||
-rw-r--r-- | src/Net/ClientConnection.cpp | 82 | ||||
-rw-r--r-- | src/Net/ClientConnection.h | 57 | ||||
-rw-r--r-- | src/Net/Connection.h | 20 | ||||
-rw-r--r-- | src/Net/ConnectionException.h | 42 | ||||
-rw-r--r-- | src/Net/Exception.h (renamed from src/Net/Connection.cpp) | 20 | ||||
-rw-r--r-- | src/Net/IPAddress.h | 54 | ||||
-rw-r--r-- | src/Net/InvalidAddressException.h | 41 | ||||
-rw-r--r-- | src/Net/Makefile.am | 5 | ||||
-rw-r--r-- | src/Net/Makefile.in | 19 | ||||
-rw-r--r-- | src/config.h.in | 15 | ||||
-rw-r--r-- | src/madc.cpp | 19 |
12 files changed, 359 insertions, 18 deletions
diff --git a/src/Makefile.in b/src/Makefile.in index ce5da2d..738f494 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -111,6 +111,8 @@ EXEEXT = @EXEEXT@ F77 = @F77@ FFLAGS = @FFLAGS@ GREP = @GREP@ +GnuTLS_CFLAGS = @GnuTLS_CFLAGS@ +GnuTLS_LIBS = @GnuTLS_LIBS@ INSTALL = @INSTALL@ INSTALL_DATA = @INSTALL_DATA@ INSTALL_PROGRAM = @INSTALL_PROGRAM@ @@ -134,6 +136,7 @@ PACKAGE_STRING = @PACKAGE_STRING@ PACKAGE_TARNAME = @PACKAGE_TARNAME@ PACKAGE_VERSION = @PACKAGE_VERSION@ PATH_SEPARATOR = @PATH_SEPARATOR@ +PKG_CONFIG = @PKG_CONFIG@ RANLIB = @RANLIB@ SED = @SED@ SET_MAKE = @SET_MAKE@ diff --git a/src/Net/ClientConnection.cpp b/src/Net/ClientConnection.cpp new file mode 100644 index 0000000..396c70a --- /dev/null +++ b/src/Net/ClientConnection.cpp @@ -0,0 +1,82 @@ +/* + * ClientConnection.cpp + * + * Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de> + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "ClientConnection.h" +#include "IPAddress.h" +#include <cstring> +#include <errno.h> +#include <sys/socket.h> + +namespace Mad { +namespace Net { + +void ClientConnection::connect(const IPAddress &address) throw(ConnectionException) { + const int kx_list[] = {GNUTLS_KX_ANON_DH, 0}; + + if(connected) + disconnect(); + + peer = new IPAddress(address); + + sock = socket(PF_INET, SOCK_STREAM, 0); + if(sock < 0) + throw ConnectionException("socket()", std::strerror(errno)); + + if(::connect(sock, peer->getSockAddr(), peer->getSockAddrLength()) < 0) + throw ConnectionException("connect()", std::strerror(errno)); + + gnutls_anon_allocate_client_credentials(&anoncred); + gnutls_init(&session, GNUTLS_CLIENT); + + gnutls_set_default_priority(session); + gnutls_kx_set_priority(session, kx_list); + + gnutls_credentials_set(session, GNUTLS_CRD_ANON, anoncred); + + gnutls_transport_set_ptr(session, reinterpret_cast<gnutls_transport_ptr_t>(sock)); + + int ret = gnutls_handshake(session); + if(ret < 0) + throw ConnectionException("gnutls_handshake()", gnutls_strerror(ret)); + + connected = true; +} + +void ClientConnection::disconnect() { + gnutls_bye(session, GNUTLS_SHUT_RDWR); + + if(sock >= 0) { + shutdown(sock, SHUT_RDWR); /* no more receptions */ + close(sock); + sock = -1; + } + + gnutls_deinit(session); + gnutls_anon_free_client_credentials(anoncred); + + if(peer) { + delete peer; + peer = 0; + } + + connected = false; +} + +} +} diff --git a/src/Net/ClientConnection.h b/src/Net/ClientConnection.h new file mode 100644 index 0000000..684dbfc --- /dev/null +++ b/src/Net/ClientConnection.h @@ -0,0 +1,57 @@ +/* + * ClientConnection.h + * + * Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de> + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef MAD_NET_CLIENTCONNECTION_H_ +#define MAD_NET_CLIENTCONNECTION_H_ + +#include "Connection.h" +#include "ConnectionException.h" + +namespace Mad { +namespace Net { + +class IPAddress; + +class ClientConnection : public Connection { + private: + bool connected; + IPAddress *peer; + + int sock; + gnutls_session_t session; + gnutls_anon_client_credentials_t anoncred; + + public: + ClientConnection() : connected(false), peer(0), sock(-1), session(0) {} + virtual ~ClientConnection() { + if(connected) + disconnect(); + } + + void connect(const IPAddress &address) throw(ConnectionException); + void disconnect(); + + virtual bool isConnected() const {return connected;} + virtual const IPAddress* getPeer() const {return peer;} +}; + +} +} + +#endif /*MAD_NET_CLIENTCONNECTION_H_*/ diff --git a/src/Net/Connection.h b/src/Net/Connection.h index a40407c..c8b45c6 100644 --- a/src/Net/Connection.h +++ b/src/Net/Connection.h @@ -20,16 +20,30 @@ #ifndef MAD_NET_CONNECTION_H_ #define MAD_NET_CONNECTION_H_ +#include <gnutls/gnutls.h> + namespace Mad { namespace Net { +class IPAddress; + class Connection { public: - Connection(); - virtual ~Connection(); + virtual ~Connection() {} + + virtual bool isConnected() const = 0; + virtual const IPAddress* getPeer() const = 0; + + static void init() { + gnutls_global_init(); + } + + static void deinit() { + gnutls_global_deinit(); + } }; } } -#endif /*CONNECTION_H_*/ +#endif /*MAD_NET_CONNECTION_H_*/ diff --git a/src/Net/ConnectionException.h b/src/Net/ConnectionException.h new file mode 100644 index 0000000..cb9e10e --- /dev/null +++ b/src/Net/ConnectionException.h @@ -0,0 +1,42 @@ +/* + * ConnectionException.h + * + * Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de> + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef MAD_NET_CONNECTIONEXCEPTION_H_ +#define MAD_NET_CONNECTIONEXCEPTION_H_ + +#include "Exception.h" + +namespace Mad { +namespace Net { + +class ConnectionException : public Exception { + private: + std::string str; + + public: + ConnectionException(const std::string &during, const std::string &error) + : str(during + ": " + error) {} + + virtual std::string& what() {return str;} +}; + +} +} + +#endif /*MAD_NET_CONNECTIONEXCEPTION_H_*/ diff --git a/src/Net/Connection.cpp b/src/Net/Exception.h index 1c2cc62..10a9167 100644 --- a/src/Net/Connection.cpp +++ b/src/Net/Exception.h @@ -1,5 +1,5 @@ /* - * Connection.cpp + * Exception.h * * Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de> * @@ -17,18 +17,20 @@ * with this program. If not, see <http://www.gnu.org/licenses/>. */ -#include "Connection.h" +#ifndef MAD_NET_EXCEPTION_H_ +#define MAD_NET_EXCEPTION_H_ + +#include <string> namespace Mad { namespace Net { -Connection::Connection() { - -} - -Connection::~Connection() { - -} +class Exception { + public: + virtual std::string& what() = 0; +}; } } + +#endif /*MAD_NET_EXCEPTION_H_*/ diff --git a/src/Net/IPAddress.h b/src/Net/IPAddress.h new file mode 100644 index 0000000..e892323 --- /dev/null +++ b/src/Net/IPAddress.h @@ -0,0 +1,54 @@ +/* + * IPAddress.h + * + * Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de> + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef MAD_NET_IPADDRESS_H_ +#define MAD_NET_IPADDRESS_H_ + +#include <string> +#include <arpa/inet.h> +#include "InvalidAddressException.h" + +namespace Mad { +namespace Net { + +class IPAddress { + private: + std::string addr; + unsigned short p; + struct sockaddr_in sa; + + public: + IPAddress(const std::string &address, unsigned short port) throw(InvalidAddressException) : addr(address), p(port) { + sa.sin_family = AF_INET; + sa.sin_port = htons(port); + if(!inet_pton(AF_INET, address.c_str(), &sa.sin_addr)) + throw InvalidAddressException(address); + } + + const std::string& getAddress() const {return addr;} + unsigned short getPort() const {return p;} + + struct sockaddr* getSockAddr() {return reinterpret_cast<struct sockaddr*>(&sa);} + socklen_t getSockAddrLength() const {return sizeof(sa);} +}; + +} +} + +#endif /*MAD_NET_IPADDRESS_H_*/ diff --git a/src/Net/InvalidAddressException.h b/src/Net/InvalidAddressException.h new file mode 100644 index 0000000..a547106 --- /dev/null +++ b/src/Net/InvalidAddressException.h @@ -0,0 +1,41 @@ +/* + * InvalidAddressException.h + * + * Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de> + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef MAD_NET_INVALIDADDRESSEXCEPTION_H_ +#define MAD_NET_INVALIDADDRESSEXCEPTION_H_ + +#include "Exception.h" + +namespace Mad { +namespace Net { + +class InvalidAddressException : public Exception { + private: + std::string str; + + public: + InvalidAddressException(const std::string &addr) : str("Invalid address: " + addr) {} + + virtual std::string& what() {return str;} +}; + +} +} + +#endif /*MAD_NET_INVALIDADDRESSEXCEPTION_H_*/ diff --git a/src/Net/Makefile.am b/src/Net/Makefile.am index 18d86ea..0fe1c42 100644 --- a/src/Net/Makefile.am +++ b/src/Net/Makefile.am @@ -1,3 +1,6 @@ noinst_LTLIBRARIES = libnet.la -libnet_la_SOURCES = Connection.cpp +libnet_la_SOURCES = ClientConnection.cpp + +noinst_HEADERS = ClientConnection.h Connection.h Exception.h ConnectionException.h \ + InvalidAddressException.h IPAddress.h diff --git a/src/Net/Makefile.in b/src/Net/Makefile.in index 130539a..3281bc4 100644 --- a/src/Net/Makefile.in +++ b/src/Net/Makefile.in @@ -14,6 +14,7 @@ @SET_MAKE@ + VPATH = @srcdir@ pkgdatadir = $(datadir)/@PACKAGE@ pkglibdir = $(libdir)/@PACKAGE@ @@ -33,7 +34,8 @@ POST_UNINSTALL = : build_triplet = @build@ host_triplet = @host@ subdir = src/Net -DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in +DIST_COMMON = $(noinst_HEADERS) $(srcdir)/Makefile.am \ + $(srcdir)/Makefile.in ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 am__aclocal_m4_deps = $(top_srcdir)/configure.ac am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \ @@ -43,7 +45,7 @@ CONFIG_HEADER = $(top_builddir)/src/config.h CONFIG_CLEAN_FILES = LTLIBRARIES = $(noinst_LTLIBRARIES) libnet_la_LIBADD = -am_libnet_la_OBJECTS = Connection.lo +am_libnet_la_OBJECTS = ClientConnection.lo libnet_la_OBJECTS = $(am_libnet_la_OBJECTS) DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/src depcomp = $(SHELL) $(top_srcdir)/depcomp @@ -59,6 +61,7 @@ CXXLINK = $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \ $(LDFLAGS) -o $@ SOURCES = $(libnet_la_SOURCES) DIST_SOURCES = $(libnet_la_SOURCES) +HEADERS = $(noinst_HEADERS) ETAGS = etags CTAGS = ctags DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) @@ -91,6 +94,8 @@ EXEEXT = @EXEEXT@ F77 = @F77@ FFLAGS = @FFLAGS@ GREP = @GREP@ +GnuTLS_CFLAGS = @GnuTLS_CFLAGS@ +GnuTLS_LIBS = @GnuTLS_LIBS@ INSTALL = @INSTALL@ INSTALL_DATA = @INSTALL_DATA@ INSTALL_PROGRAM = @INSTALL_PROGRAM@ @@ -114,6 +119,7 @@ PACKAGE_STRING = @PACKAGE_STRING@ PACKAGE_TARNAME = @PACKAGE_TARNAME@ PACKAGE_VERSION = @PACKAGE_VERSION@ PATH_SEPARATOR = @PATH_SEPARATOR@ +PKG_CONFIG = @PKG_CONFIG@ RANLIB = @RANLIB@ SED = @SED@ SET_MAKE = @SET_MAKE@ @@ -172,7 +178,10 @@ target_alias = @target_alias@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ noinst_LTLIBRARIES = libnet.la -libnet_la_SOURCES = Connection.cpp +libnet_la_SOURCES = ClientConnection.cpp +noinst_HEADERS = ClientConnection.h Connection.h Exception.h ConnectionException.h \ + InvalidAddressException.h IPAddress.h + all: all-am .SUFFIXES: @@ -224,7 +233,7 @@ mostlyclean-compile: distclean-compile: -rm -f *.tab.c -@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Connection.Plo@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ClientConnection.Plo@am__quote@ .cpp.o: @am__fastdepCXX_TRUE@ $(CXXCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< @@ -328,7 +337,7 @@ distdir: $(DISTFILES) done check-am: all-am check: check-am -all-am: Makefile $(LTLIBRARIES) +all-am: Makefile $(LTLIBRARIES) $(HEADERS) installdirs: install: install-am install-exec: install-exec-am diff --git a/src/config.h.in b/src/config.h.in index f806305..35132f2 100644 --- a/src/config.h.in +++ b/src/config.h.in @@ -1,5 +1,8 @@ /* src/config.h.in. Generated from configure.ac by autoheader. */ +/* Define to 1 if you have the <arpa/inet.h> header file. */ +#undef HAVE_ARPA_INET_H + /* Define to 1 if you have the <dlfcn.h> header file. */ #undef HAVE_DLFCN_H @@ -9,6 +12,9 @@ /* Define to 1 if you have the <memory.h> header file. */ #undef HAVE_MEMORY_H +/* Define to 1 if stdbool.h conforms to C99. */ +#undef HAVE_STDBOOL_H + /* Define to 1 if you have the <stdint.h> header file. */ #undef HAVE_STDINT_H @@ -21,6 +27,9 @@ /* Define to 1 if you have the <string.h> header file. */ #undef HAVE_STRING_H +/* Define to 1 if you have the <sys/socket.h> header file. */ +#undef HAVE_SYS_SOCKET_H + /* Define to 1 if you have the <sys/stat.h> header file. */ #undef HAVE_SYS_STAT_H @@ -30,6 +39,9 @@ /* Define to 1 if you have the <unistd.h> header file. */ #undef HAVE_UNISTD_H +/* Define to 1 if the system has the type `_Bool'. */ +#undef HAVE__BOOL + /* Define to 1 if your C compiler doesn't accept -c and -o together. */ #undef NO_MINUS_C_MINUS_O @@ -56,3 +68,6 @@ /* Version number of package */ #undef VERSION + +/* Define to empty if `const' does not conform to ANSI C. */ +#undef const diff --git a/src/madc.cpp b/src/madc.cpp index 15c35ce..f55779e 100644 --- a/src/madc.cpp +++ b/src/madc.cpp @@ -17,6 +17,25 @@ * with this program. If not, see <http://www.gnu.org/licenses/>. */ +#include "Net/ClientConnection.h" +#include "Net/IPAddress.h" +#include <iostream> + int main() { + Mad::Net::Connection::init(); + + Mad::Net::ClientConnection connection; + + try { + connection.connect(Mad::Net::IPAddress("127.0.0.1", 6666)); + } + catch(Mad::Net::Exception &e) { + std::cerr << "Connection error: " << e.what() << std::endl; + } + + connection.disconnect(); + + Mad::Net::Connection::deinit(); + return 0; } |