summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/network/core/address.cpp11
-rw-r--r--src/network/core/address.h10
-rw-r--r--src/network/core/tcp_content.cpp8
-rw-r--r--src/network/core/tcp_content.h10
-rw-r--r--src/network/network_content.cpp2
5 files changed, 29 insertions, 12 deletions
diff --git a/src/network/core/address.cpp b/src/network/core/address.cpp
index 637f0fe7b..cbb9defb0 100644
--- a/src/network/core/address.cpp
+++ b/src/network/core/address.cpp
@@ -7,7 +7,9 @@
#ifdef ENABLE_NETWORK
#include "address.h"
+#include "config.h"
#include "host.h"
+#include "../../string_func.h"
const char *NetworkAddress::GetHostname() const
{
@@ -27,4 +29,13 @@ uint32 NetworkAddress::GetIP()
return this->ip;
}
+const char *NetworkAddress::GetAddressAsString() const
+{
+ /* 6 = for the : and 5 for the decimal port number */
+ static char buf[NETWORK_HOSTNAME_LENGTH + 6];
+
+ seprintf(buf, lastof(buf), "%s:%d", this->GetHostname(), this->GetPort());
+ return buf;
+}
+
#endif /* ENABLE_NETWORK */
diff --git a/src/network/core/address.h b/src/network/core/address.h
index 62e79f3de..30d86b5e8 100644
--- a/src/network/core/address.h
+++ b/src/network/core/address.h
@@ -40,9 +40,9 @@ public:
* @param ip the unresolved hostname
* @param port the port
*/
- NetworkAddress(const char *hostname, uint16 port) :
+ NetworkAddress(const char *hostname = NULL, uint16 port = 0) :
resolved(false),
- hostname(strdup(hostname)),
+ hostname(hostname == NULL ? NULL : strdup(hostname)),
ip(0),
port(port)
{
@@ -74,6 +74,12 @@ public:
const char *GetHostname() const;
/**
+ * Get the address as a string, e.g. 127.0.0.1:12345.
+ * @return the address
+ */
+ const char *GetAddressAsString() const;
+
+ /**
* Get the IP address. If the IP has not been resolved yet this will resolve
* it possibly blocking this function for a while
* @return the IP address
diff --git a/src/network/core/tcp_content.cpp b/src/network/core/tcp_content.cpp
index 7596cced8..889f627c7 100644
--- a/src/network/core/tcp_content.cpp
+++ b/src/network/core/tcp_content.cpp
@@ -84,9 +84,9 @@ bool NetworkContentSocketHandler::HandlePacket(Packet *p)
default:
if (this->HasClientQuit()) {
- DEBUG(net, 0, "[tcp/content] received invalid packet type %d from %s:%d", type, inet_ntoa(this->client_addr.sin_addr), ntohs(this->client_addr.sin_port));
+ DEBUG(net, 0, "[tcp/content] received invalid packet type %d from %s", type, this->client_addr.GetAddressAsString());
} else {
- DEBUG(net, 0, "[tcp/content] received illegal packet from %s:%d", inet_ntoa(this->client_addr.sin_addr), ntohs(this->client_addr.sin_port));
+ DEBUG(net, 0, "[tcp/content] received illegal packet from %s", this->client_addr.GetAddressAsString());
}
return false;
}
@@ -114,8 +114,8 @@ void NetworkContentSocketHandler::Recv_Packets()
*/
#define DEFINE_UNAVAILABLE_CONTENT_RECEIVE_COMMAND(type) \
bool NetworkContentSocketHandler::NetworkPacketReceive_## type ##_command(Packet *p) { \
- DEBUG(net, 0, "[tcp/content] received illegal packet type %d from %s:%d", \
- type, inet_ntoa(this->client_addr.sin_addr), ntohs(this->client_addr.sin_port)); \
+ DEBUG(net, 0, "[tcp/content] received illegal packet type %d from %s", \
+ type, this->client_addr.GetAddressAsString()); \
return false; \
}
diff --git a/src/network/core/tcp_content.h b/src/network/core/tcp_content.h
index 26f5878ad..8313c733b 100644
--- a/src/network/core/tcp_content.h
+++ b/src/network/core/tcp_content.h
@@ -101,7 +101,7 @@ struct ContentInfo {
/** Base socket handler for all Content TCP sockets */
class NetworkContentSocketHandler : public NetworkTCPSocketHandler {
protected:
- struct sockaddr_in client_addr; ///< The address we're connected to.
+ struct NetworkAddress client_addr; ///< The address we're connected to.
virtual void Close();
/**
@@ -187,12 +187,12 @@ public:
/**
* Create a new cs socket handler for a given cs
* @param s the socket we are connected with
- * @param sin IP etc. of the client
+ * @param address IP etc. of the client
*/
- NetworkContentSocketHandler(SOCKET s, const struct sockaddr_in *sin) :
- NetworkTCPSocketHandler(s)
+ NetworkContentSocketHandler(SOCKET s = INVALID_SOCKET, const NetworkAddress &address = NetworkAddress()) :
+ NetworkTCPSocketHandler(s),
+ client_addr(address)
{
- if (sin != NULL) this->client_addr = *sin;
}
/** On destructing of this class, the socket needs to be closed */
diff --git a/src/network/network_content.cpp b/src/network/network_content.cpp
index a37e894c1..9a82154b4 100644
--- a/src/network/network_content.cpp
+++ b/src/network/network_content.cpp
@@ -426,7 +426,7 @@ DEF_CONTENT_RECEIVE_COMMAND(Client, PACKET_CONTENT_SERVER_CONTENT)
* @param sin the IP/port of the server
*/
ClientNetworkContentSocketHandler::ClientNetworkContentSocketHandler() :
- NetworkContentSocketHandler(INVALID_SOCKET, NULL),
+ NetworkContentSocketHandler(),
curFile(NULL),
curInfo(NULL),
isConnecting(false)