summaryrefslogtreecommitdiff
path: root/src/network/core/address.cpp
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2009-04-02 19:21:26 +0000
committerrubidium <rubidium@openttd.org>2009-04-02 19:21:26 +0000
commitc0c6e07081fb55d1ec5c46cc3606185062cfe45c (patch)
treef53550975dd7d0507b3e94bded12ae75496aa6a7 /src/network/core/address.cpp
parent804370d964486ab0ac51a7402e6b30fced5d31af (diff)
downloadopenttd-c0c6e07081fb55d1ec5c46cc3606185062cfe45c.tar.xz
(svn r15915) -Codechange: let the udp code use NetworkAddress.
Diffstat (limited to 'src/network/core/address.cpp')
-rw-r--r--src/network/core/address.cpp36
1 files changed, 27 insertions, 9 deletions
diff --git a/src/network/core/address.cpp b/src/network/core/address.cpp
index cbb9defb0..bd7f569ae 100644
--- a/src/network/core/address.cpp
+++ b/src/network/core/address.cpp
@@ -11,25 +11,37 @@
#include "host.h"
#include "../../string_func.h"
-const char *NetworkAddress::GetHostname() const
+const char *NetworkAddress::GetHostname()
{
- if (this->hostname != NULL) return this->hostname;
-
- in_addr addr;
- addr.s_addr = this->ip;
- return inet_ntoa(addr);
+ if (this->hostname == NULL) {
+ this->hostname = strdup(inet_ntoa(((struct sockaddr_in *)&this->address)->sin_addr));
+ }
+ return this->hostname;
}
uint32 NetworkAddress::GetIP()
{
+ assert(this->address.ss_family == AF_INET);
+
if (!this->resolved) {
- this->ip = NetworkResolveHost(this->hostname);
+ ((struct sockaddr_in *)&this->address)->sin_addr.s_addr = NetworkResolveHost(this->hostname);
this->resolved = true;
}
- return this->ip;
+ return ((struct sockaddr_in *)&this->address)->sin_addr.s_addr;
}
-const char *NetworkAddress::GetAddressAsString() const
+uint16 NetworkAddress::GetPort() const
+{
+ switch (this->address.ss_family) {
+ case AF_INET:
+ return ntohs(((struct sockaddr_in *)&this->address)->sin_port);
+
+ default:
+ NOT_REACHED();
+ }
+}
+
+const char *NetworkAddress::GetAddressAsString()
{
/* 6 = for the : and 5 for the decimal port number */
static char buf[NETWORK_HOSTNAME_LENGTH + 6];
@@ -38,4 +50,10 @@ const char *NetworkAddress::GetAddressAsString() const
return buf;
}
+const sockaddr_storage *NetworkAddress::GetAddress()
+{
+ if (!this->resolved) this->GetIP();
+ return &this->address;
+}
+
#endif /* ENABLE_NETWORK */