From 98432be171dee1a1ad4774b5600c4f91bfa1f810 Mon Sep 17 00:00:00 2001 From: Aki Date: Wed, 30 Mar 2022 19:57:43 +0200 Subject: Brought NetHost closer to standard --- NetEx/NetHost.cpp | 43 +++++++++++++++++++++++++++++-------------- NetEx/NetHost.h | 18 ++++++++---------- 2 files changed, 37 insertions(+), 24 deletions(-) (limited to 'NetEx') diff --git a/NetEx/NetHost.cpp b/NetEx/NetHost.cpp index 1c24ff0..4c8c89f 100644 --- a/NetEx/NetHost.cpp +++ b/NetEx/NetHost.cpp @@ -3,40 +3,52 @@ Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors Copyright (c) 1997-2006, Destroyer Studios LLC. - AUTHOR: John DiCamillo + AUTHOR: John DiCamillo - OVERVIEW - ======== - Network Host + OVERVIEW + ======== + Network Host */ #include "NetHost.h" -#include "NetLayer.h" -#include + +#ifdef _WIN32 +#include +#else +#include +#include +#include +#include +#endif + +#include NetHost::NetHost() { char host_name[256]; - gethostname(host_name, sizeof(host_name)); + ::gethostname(host_name, sizeof(host_name)); Init(host_name); } + NetHost::NetHost(const char* host_name) { Init(host_name); } -void NetHost::Init(const char* host_name) + +void +NetHost::Init(const char* host_name) { if (host_name && *host_name) { - HOSTENT* h = 0; + struct hostent* h = nullptr; - if (isdigit(*host_name)) { - DWORD addr = inet_addr(host_name); - h = gethostbyaddr((const char*) &addr, 4, AF_INET); + if (std::isdigit(*host_name)) { + auto a = inet_addr(host_name); + h = gethostbyaddr((const char*) &a, 4, AF_INET); } else { h = gethostbyname(host_name); @@ -53,7 +65,7 @@ void NetHost::Init(const char* host_name) char** addr = h->h_addr_list; while (*addr) { - NetAddr* pna = new NetAddr(**(DWORD**) addr); + NetAddr* pna = new NetAddr(**(std::uint32_t**) addr); if (pna) addresses.append(pna); addr++; @@ -62,6 +74,7 @@ void NetHost::Init(const char* host_name) } } + NetHost::NetHost(const NetHost& n) { if (&n != this) { @@ -79,6 +92,7 @@ NetHost::NetHost(const NetHost& n) } } + NetHost::~NetHost() { aliases.destroy(); @@ -91,11 +105,12 @@ NetHost::Name() return name; } + NetAddr NetHost::Address() { if (addresses.size()) return *(addresses[0]); - return NetAddr((DWORD) 0); + return NetAddr((std::uint32_t) 0); } \ No newline at end of file diff --git a/NetEx/NetHost.h b/NetEx/NetHost.h index a5a4bd0..c45f04c 100644 --- a/NetEx/NetHost.h +++ b/NetEx/NetHost.h @@ -3,23 +3,21 @@ Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors Copyright (c) 1997-2006, Destroyer Studios LLC. - AUTHOR: John DiCamillo + AUTHOR: John DiCamillo - OVERVIEW - ======== - Network Host + OVERVIEW + ======== + Network Host */ -#ifndef NET_HOST_H -#define NET_HOST_H +#ifndef NetHost_h +#define NetHost_h -#include +#include "List.h" #include "NetAddr.h" #include "Text.h" -#include "List.h" -// +-------------------------------------------------------------------+ class NetHost { @@ -46,4 +44,4 @@ private: }; -#endif // NET_HOST_H +#endif // NetHost_h -- cgit v1.1