diff options
Diffstat (limited to 'src/Net')
-rw-r--r-- | src/Net/Connection.cpp | 62 | ||||
-rw-r--r-- | src/Net/Connection.h | 58 | ||||
-rw-r--r-- | src/Net/IPAddress.cpp | 92 | ||||
-rw-r--r-- | src/Net/IPAddress.h | 73 | ||||
-rw-r--r-- | src/Net/Makefile.am | 2 | ||||
-rw-r--r-- | src/Net/Makefile.in | 5 |
6 files changed, 169 insertions, 123 deletions
diff --git a/src/Net/Connection.cpp b/src/Net/Connection.cpp index 22adeaf..da1ff3f 100644 --- a/src/Net/Connection.cpp +++ b/src/Net/Connection.cpp @@ -22,11 +22,42 @@ #include <cstring> #include <sys/socket.h> -#include <iostream> - namespace Mad { namespace Net { +Connection::~Connection() { + if(isConnected()) + doDisconnect(); + + if(transR.data) + delete [] transR.data; + + while(!sendQueueEmpty()) { + delete [] transS.front().data; + transS.pop(); + } + + gnutls_certificate_free_credentials(x509_cred); +} + +void Connection::handshake() { + if(isConnected()) + return; + + state = HANDSHAKE; + + doHandshake(); +} + +void Connection::bye() { + if(state != DISCONNECT) + return; + + state = BYE; + + doBye(); +} + void Connection::doHandshake() { if(state != HANDSHAKE) return; @@ -36,8 +67,6 @@ void Connection::doHandshake() { if(ret == GNUTLS_E_INTERRUPTED || ret == GNUTLS_E_AGAIN) return; - std::cerr << "Handshake error: " << gnutls_strerror(ret) << std::endl; - // TODO: Error doDisconnect(); return; @@ -64,6 +93,15 @@ void Connection::doBye() { doDisconnect(); } +bool Connection::enterReceiveLoop() { + if(!isConnected() || isDisconnecting()) + return false; + + state = PACKET_HEADER; + + return rawReceive(sizeof(Packet::Data), sigc::mem_fun(this, &Connection::packetHeaderReceiveHandler)); +} + void Connection::packetHeaderReceiveHandler(const void *data, unsigned long length) { if(state != PACKET_HEADER) return; @@ -213,6 +251,22 @@ void Connection::sendReceive(short events) { bye(); } +bool Connection::send(const Packet &packet) { + if(!isConnected() || isConnecting() || isDisconnecting()) + return false; + + return rawSend((const uint8_t*)packet.getRawData(), packet.getRawDataLength()); +} + +void Connection::disconnect() { + if(isConnected() && !isDisconnecting()) { + state = DISCONNECT; + + if(sendQueueEmpty()) + bye(); + } +} + void Connection::doDisconnect() { if(!isConnected()) return; diff --git a/src/Net/Connection.h b/src/Net/Connection.h index 0f012f1..58e7291 100644 --- a/src/Net/Connection.h +++ b/src/Net/Connection.h @@ -72,14 +72,7 @@ class Connection { return (transR.length == transR.transmitted); } - void bye() { - if(state != DISCONNECT) - return; - - state = BYE; - - doBye(); - } + void bye(); // Prevent shallow copy Connection(const Connection &o); @@ -104,28 +97,14 @@ class Connection { IPAddress *peer; - void handshake() { - if(isConnected()) - return; - - state = HANDSHAKE; - - doHandshake(); - } + void handshake(); virtual void connectionHeader() = 0; bool rawReceive(unsigned long length, const sigc::slot<void,const void*,unsigned long> ¬ify); bool rawSend(const uint8_t *data, unsigned long length); - bool enterReceiveLoop() { - if(!isConnected() || isDisconnecting()) - return false; - - state = PACKET_HEADER; - - return rawReceive(sizeof(Packet::Data), sigc::mem_fun(this, &Connection::packetHeaderReceiveHandler)); - } + bool enterReceiveLoop(); public: Connection() : state(DISCONNECTED), authenticated(false), peer(0) { @@ -135,20 +114,7 @@ class Connection { gnutls_certificate_allocate_credentials(&x509_cred); } - virtual ~Connection() { - if(isConnected()) - doDisconnect(); - - if(transR.data) - delete [] transR.data; - - while(!sendQueueEmpty()) { - delete [] transS.front().data; - transS.pop(); - } - - gnutls_certificate_free_credentials(x509_cred); - } + virtual ~Connection(); bool isConnected() const {return (state != DISCONNECTED);} bool isConnecting() const { @@ -174,23 +140,11 @@ class Connection { const IPAddress* getPeer() {return peer;} int getSocket() const {return sock;} - void disconnect() { - if(isConnected() && !isDisconnecting()) { - state = DISCONNECT; - - if(sendQueueEmpty()) - bye(); - } - } + void disconnect(); struct pollfd getPollfd() const; - bool send(const Packet &packet) { - if(!isConnected() || isConnecting() || isDisconnecting()) - return false; - - return rawSend((const uint8_t*)packet.getRawData(), packet.getRawDataLength()); - } + bool send(const Packet &packet); void sendReceive(short events = POLLIN|POLLOUT); diff --git a/src/Net/IPAddress.cpp b/src/Net/IPAddress.cpp new file mode 100644 index 0000000..9c9793e --- /dev/null +++ b/src/Net/IPAddress.cpp @@ -0,0 +1,92 @@ +/* + * IPAddress.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 "IPAddress.h" + +#include <cstdlib> + +namespace Mad { +namespace Net { + +IPAddress::IPAddress(uint16_t port0) : addr(INADDR_ANY), port(port0) { + sa.sin_family = AF_INET; + sa.sin_port = htons(port); + sa.sin_addr.s_addr = INADDR_ANY; +} + +IPAddress::IPAddress(uint32_t address, uint16_t port0) : addr(address), port(port0) { + sa.sin_family = AF_INET; + sa.sin_port = htons(port); + sa.sin_addr.s_addr = htonl(addr); +} + +IPAddress::IPAddress(const std::string &address) throw(InvalidAddressException) { + std::string ip; + size_t pos = address.find_first_of(':'); + + if(pos == std::string::npos) { + ip = address; + // TODO Default port + port = 6666; + } + else { + ip = address.substr(0, pos); + + char *endptr; + port = std::strtol(address.substr(pos+1).c_str(), &endptr, 10); + if(*endptr != 0 || port == 0) + throw InvalidAddressException(address); + } + + sa.sin_family = AF_INET; + sa.sin_port = htons(port); + + if(ip == "*") + sa.sin_addr.s_addr = INADDR_ANY; + else if(!inet_pton(AF_INET, ip.c_str(), &sa.sin_addr)) + throw InvalidAddressException(address); + + addr = ntohl(sa.sin_addr.s_addr); +} + +IPAddress::IPAddress(const std::string &address, uint16_t port0) throw(InvalidAddressException) : port(port0) { + sa.sin_family = AF_INET; + sa.sin_port = htons(port); + + if(!inet_pton(AF_INET, address.c_str(), &sa.sin_addr)) + throw InvalidAddressException(address); + + addr = ntohl(sa.sin_addr.s_addr); +} + +IPAddress::IPAddress(const struct sockaddr_in &address) : sa(address) { + port = ntohs(sa.sin_port); + addr = ntohl(sa.sin_addr.s_addr); +} + +std::string IPAddress::getAddressString() const { + char buf[INET_ADDRSTRLEN]; + uint32_t address = htonl(addr); + + inet_ntop(AF_INET, &address, buf, sizeof(buf)); + return std::string(buf); +} + +} +} diff --git a/src/Net/IPAddress.h b/src/Net/IPAddress.h index 78b41dd..1d6140f 100644 --- a/src/Net/IPAddress.h +++ b/src/Net/IPAddress.h @@ -20,10 +20,11 @@ #ifndef MAD_NET_IPADDRESS_H_ #define MAD_NET_IPADDRESS_H_ +#include "InvalidAddressException.h" + #include <string> -#include <cstdlib> #include <arpa/inet.h> -#include "InvalidAddressException.h" +#include <stdint.h> namespace Mad { namespace Net { @@ -36,72 +37,16 @@ class IPAddress { public: // TODO Default port - IPAddress(uint16_t port0 = 6666) : addr(INADDR_ANY), port(port0) { - sa.sin_family = AF_INET; - sa.sin_port = htons(port); - sa.sin_addr.s_addr = INADDR_ANY; - } - - IPAddress(uint32_t address, uint16_t port0) : addr(address), port(port0) { - sa.sin_family = AF_INET; - sa.sin_port = htons(port); - sa.sin_addr.s_addr = htonl(addr); - } - - IPAddress(const std::string &address) throw(InvalidAddressException) { - std::string ip; - size_t pos = address.find_first_of(':'); - - if(pos == std::string::npos) { - ip = address; - // TODO Default port - port = 6666; - } - else { - ip = address.substr(0, pos); - - char *endptr; - port = std::strtol(address.substr(pos+1).c_str(), &endptr, 10); - if(*endptr != 0 || port == 0) - throw InvalidAddressException(address); - } - - sa.sin_family = AF_INET; - sa.sin_port = htons(port); - - if(ip == "*") - sa.sin_addr.s_addr = INADDR_ANY; - else if(!inet_pton(AF_INET, ip.c_str(), &sa.sin_addr)) - throw InvalidAddressException(address); - - addr = ntohl(sa.sin_addr.s_addr); - } - - IPAddress(const std::string &address, uint16_t port0) throw(InvalidAddressException) : port(port0) { - sa.sin_family = AF_INET; - sa.sin_port = htons(port); - - if(!inet_pton(AF_INET, address.c_str(), &sa.sin_addr)) - throw InvalidAddressException(address); - - addr = ntohl(sa.sin_addr.s_addr); - } - - IPAddress(const struct sockaddr_in &address) : sa(address) { - port = ntohs(sa.sin_port); - addr = ntohl(sa.sin_addr.s_addr); - } + IPAddress(uint16_t port0 = 6666); + IPAddress(uint32_t address, uint16_t port0); + IPAddress(const std::string &address) throw(InvalidAddressException); + IPAddress(const std::string &address, uint16_t port0) throw(InvalidAddressException); + IPAddress(const struct sockaddr_in &address); uint32_t getAddress() const {return addr;} uint16_t getPort() const {return port;} - std::string getAddressString() const { - char buf[INET_ADDRSTRLEN]; - uint32_t address = htonl(addr); - - inet_ntop(AF_INET, &address, buf, sizeof(buf)); - return std::string(buf); - } + std::string getAddressString() const; struct sockaddr* getSockAddr() {return (struct sockaddr*)&sa;} socklen_t getSockAddrLength() const {return sizeof(sa);} diff --git a/src/Net/Makefile.am b/src/Net/Makefile.am index 2fd4f12..967e989 100644 --- a/src/Net/Makefile.am +++ b/src/Net/Makefile.am @@ -1,7 +1,7 @@ SUBDIRS = Packets noinst_LTLIBRARIES = libnet.la -libnet_la_SOURCES = ClientConnection.cpp ServerConnection.cpp Connection.cpp Listener.cpp Packet.cpp +libnet_la_SOURCES = ClientConnection.cpp ServerConnection.cpp Connection.cpp IPAddress.cpp Listener.cpp Packet.cpp libnet_la_LIBADD = Packets/libpackets.la noinst_HEADERS = ClientConnection.h ServerConnection.h Connection.h Exception.h ConnectionException.h \ diff --git a/src/Net/Makefile.in b/src/Net/Makefile.in index 3d9f7bf..163ca89 100644 --- a/src/Net/Makefile.in +++ b/src/Net/Makefile.in @@ -46,7 +46,7 @@ CONFIG_CLEAN_FILES = LTLIBRARIES = $(noinst_LTLIBRARIES) libnet_la_DEPENDENCIES = Packets/libpackets.la am_libnet_la_OBJECTS = ClientConnection.lo ServerConnection.lo \ - Connection.lo Listener.lo Packet.lo + Connection.lo IPAddress.lo Listener.lo Packet.lo libnet_la_OBJECTS = $(am_libnet_la_OBJECTS) DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir)/src depcomp = $(SHELL) $(top_srcdir)/depcomp @@ -194,7 +194,7 @@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ SUBDIRS = Packets noinst_LTLIBRARIES = libnet.la -libnet_la_SOURCES = ClientConnection.cpp ServerConnection.cpp Connection.cpp Listener.cpp Packet.cpp +libnet_la_SOURCES = ClientConnection.cpp ServerConnection.cpp Connection.cpp IPAddress.cpp Listener.cpp Packet.cpp libnet_la_LIBADD = Packets/libpackets.la noinst_HEADERS = ClientConnection.h ServerConnection.h Connection.h Exception.h ConnectionException.h \ InvalidAddressException.h IPAddress.h Listener.h Packet.h @@ -252,6 +252,7 @@ distclean-compile: @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)/IPAddress.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Listener.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Packet.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ServerConnection.Plo@am__quote@ |