diff options
-rw-r--r-- | src/Net/ClientConnection.cpp | 7 | ||||
-rw-r--r-- | src/Net/ClientConnection.h | 5 | ||||
-rw-r--r-- | src/Net/Connection.cpp | 52 | ||||
-rw-r--r-- | src/Net/Connection.h | 6 | ||||
-rw-r--r-- | src/Net/Makefile.am | 4 | ||||
-rw-r--r-- | src/Net/Makefile.in | 8 | ||||
-rw-r--r-- | src/Net/Packet.cpp | 28 | ||||
-rw-r--r-- | src/Net/Packet.h | 102 | ||||
-rw-r--r-- | src/madc.cpp | 3 |
9 files changed, 208 insertions, 7 deletions
diff --git a/src/Net/ClientConnection.cpp b/src/Net/ClientConnection.cpp index 396c70a..66df2ea 100644 --- a/src/Net/ClientConnection.cpp +++ b/src/Net/ClientConnection.cpp @@ -20,7 +20,7 @@ #include "ClientConnection.h" #include "IPAddress.h" #include <cstring> -#include <errno.h> +#include <cerrno> #include <sys/socket.h> namespace Mad { @@ -59,10 +59,13 @@ void ClientConnection::connect(const IPAddress &address) throw(ConnectionExcepti } void ClientConnection::disconnect() { + if(!connected) + return; + gnutls_bye(session, GNUTLS_SHUT_RDWR); if(sock >= 0) { - shutdown(sock, SHUT_RDWR); /* no more receptions */ + shutdown(sock, SHUT_RDWR); close(sock); sock = -1; } diff --git a/src/Net/ClientConnection.h b/src/Net/ClientConnection.h index 684dbfc..e90b2db 100644 --- a/src/Net/ClientConnection.h +++ b/src/Net/ClientConnection.h @@ -37,6 +37,11 @@ class ClientConnection : public Connection { gnutls_session_t session; gnutls_anon_client_credentials_t anoncred; + protected: + virtual gnutls_session_t& getSession() { + return session; + } + public: ClientConnection() : connected(false), peer(0), sock(-1), session(0) {} virtual ~ClientConnection() { diff --git a/src/Net/Connection.cpp b/src/Net/Connection.cpp new file mode 100644 index 0000000..34b6ed6 --- /dev/null +++ b/src/Net/Connection.cpp @@ -0,0 +1,52 @@ +/* + * Connection.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 "Connection.h" +#include "Packet.h" + +namespace Mad { +namespace Net { + +bool Connection::send(const Packet &packet) { + if(!isConnected()) + return false; + + const unsigned char *data = reinterpret_cast<const unsigned char*>(packet.getRawData()); + unsigned long dataLength = packet.getRawDataLength(); + + while(dataLength > 0) { + ssize_t ret = gnutls_record_send(getSession(), data, dataLength); + + if(ret < 0) { + if(ret == GNUTLS_E_INTERRUPTED || ret == GNUTLS_E_AGAIN) + continue; + else + return false; + } + else { + data += ret; + dataLength -= ret; + } + } + + return true; +} + +} +} diff --git a/src/Net/Connection.h b/src/Net/Connection.h index c8b45c6..6e8d8de 100644 --- a/src/Net/Connection.h +++ b/src/Net/Connection.h @@ -26,14 +26,20 @@ namespace Mad { namespace Net { class IPAddress; +class Packet; class Connection { + protected: + virtual gnutls_session_t& getSession() = 0; + public: virtual ~Connection() {} virtual bool isConnected() const = 0; virtual const IPAddress* getPeer() const = 0; + bool send(const Packet &packet); + static void init() { gnutls_global_init(); } diff --git a/src/Net/Makefile.am b/src/Net/Makefile.am index 0fe1c42..b52b7b9 100644 --- a/src/Net/Makefile.am +++ b/src/Net/Makefile.am @@ -1,6 +1,6 @@ noinst_LTLIBRARIES = libnet.la -libnet_la_SOURCES = ClientConnection.cpp +libnet_la_SOURCES = ClientConnection.cpp Connection.cpp Packet.cpp noinst_HEADERS = ClientConnection.h Connection.h Exception.h ConnectionException.h \ - InvalidAddressException.h IPAddress.h + InvalidAddressException.h IPAddress.h Packet.h diff --git a/src/Net/Makefile.in b/src/Net/Makefile.in index 3281bc4..0715d58 100644 --- a/src/Net/Makefile.in +++ b/src/Net/Makefile.in @@ -45,7 +45,7 @@ CONFIG_HEADER = $(top_builddir)/src/config.h CONFIG_CLEAN_FILES = LTLIBRARIES = $(noinst_LTLIBRARIES) libnet_la_LIBADD = -am_libnet_la_OBJECTS = ClientConnection.lo +am_libnet_la_OBJECTS = ClientConnection.lo Connection.lo Packet.lo libnet_la_OBJECTS = $(am_libnet_la_OBJECTS) DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/src depcomp = $(SHELL) $(top_srcdir)/depcomp @@ -178,9 +178,9 @@ target_alias = @target_alias@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ noinst_LTLIBRARIES = libnet.la -libnet_la_SOURCES = ClientConnection.cpp +libnet_la_SOURCES = ClientConnection.cpp Connection.cpp Packet.cpp noinst_HEADERS = ClientConnection.h Connection.h Exception.h ConnectionException.h \ - InvalidAddressException.h IPAddress.h + InvalidAddressException.h IPAddress.h Packet.h all: all-am @@ -234,6 +234,8 @@ distclean-compile: -rm -f *.tab.c @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ClientConnection.Plo@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Connection.Plo@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Packet.Plo@am__quote@ .cpp.o: @am__fastdepCXX_TRUE@ $(CXXCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< diff --git a/src/Net/Packet.cpp b/src/Net/Packet.cpp new file mode 100644 index 0000000..9c90db7 --- /dev/null +++ b/src/Net/Packet.cpp @@ -0,0 +1,28 @@ +/* + * Packet.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 "Packet.h" + +namespace Mad { +namespace Net { + + + +} +} diff --git a/src/Net/Packet.h b/src/Net/Packet.h new file mode 100644 index 0000000..28dcb86 --- /dev/null +++ b/src/Net/Packet.h @@ -0,0 +1,102 @@ +/* + * Packet.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_PACKET_H_ +#define MAD_NET_PACKET_H_ + +#include <cstdlib> +#include <cstring> + +namespace Mad { +namespace Net { + +class Packet { + public: + struct Data { + unsigned short type; + unsigned short requestId; + unsigned long length; + unsigned char data[0]; + }; + + protected: + Data *rawData; + + public: + Packet(unsigned short type, unsigned short requestId, void *data = NULL, unsigned long length = 0) { + rawData = (Data*)std::malloc(sizeof(Data)+length); + + rawData->type = type; + rawData->requestId = requestId; + rawData->length = length; + + if(length) + std::memcpy(rawData->data, data, length); + } + + Packet(const Packet &p) { + rawData = (Data*)std::malloc(p.getRawDataLength()); + std::memcpy(rawData, p.rawData, p.getRawDataLength()); + } + + Packet& operator=(Packet &p) { + if(&p == this) + return *this; + + std::free(rawData); + + rawData = (Data*)std::malloc(p.getRawDataLength()); + std::memcpy(rawData, p.rawData, p.getRawDataLength()); + + return *this; + } + + virtual ~Packet() { + std::free(rawData); + } + + unsigned short getType() const { + return rawData->type; + } + + unsigned short getRequestId() const { + return rawData->requestId; + } + + unsigned long getLength() const { + return rawData->length; + } + + const unsigned char* getData() const { + return rawData->data; + } + + const Data* getRawData() const { + return rawData; + } + + unsigned long getRawDataLength() const { + return sizeof(Data) + rawData->length; + } +}; + +} +} + +#endif /*MAD_NET_PACKET_H_*/ diff --git a/src/madc.cpp b/src/madc.cpp index f55779e..63516d9 100644 --- a/src/madc.cpp +++ b/src/madc.cpp @@ -19,6 +19,7 @@ #include "Net/ClientConnection.h" #include "Net/IPAddress.h" +#include "Net/Packet.h" #include <iostream> int main() { @@ -33,6 +34,8 @@ int main() { std::cerr << "Connection error: " << e.what() << std::endl; } + connection.send(Mad::Net::Packet(0x0001, 0xABCD)); + connection.disconnect(); Mad::Net::Connection::deinit(); |