summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--NetEx/NetAddr.cpp65
-rw-r--r--NetEx/NetAddr.h39
2 files changed, 60 insertions, 44 deletions
diff --git a/NetEx/NetAddr.cpp b/NetEx/NetAddr.cpp
index 62eb24a..5e5468d 100644
--- a/NetEx/NetAddr.cpp
+++ b/NetEx/NetAddr.cpp
@@ -3,28 +3,39 @@
Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
Copyright (c) 1997-2006, Destroyer Studios LLC.
- AUTHOR: John DiCamillo
+ AUTHOR: John DiCamillo
- OVERVIEW
- ========
- Network Address
+ OVERVIEW
+ ========
+ Network Address
*/
#include "NetAddr.h"
-#include "NetHost.h"
-#include "NetLayer.h"
-#include <ctype.h>
+#ifdef _WIN32
+#include <winsock2.h>
+#else
+#include <arpa/inet.h>
+#include <netdb.h>
+#include <sys/socket.h>
+#endif
-NetAddr::NetAddr(const char* host_name, WORD p)
- : addr(0), port(p)
+#include <cctype>
+#include <cstddef>
+#include <cstdint>
+#include <cstring>
+
+
+NetAddr::NetAddr(const char* host_name, std::uint16_t p) :
+ addr(0),
+ port(p)
{
if (host_name && *host_name) {
- HOSTENT* h = 0;
+ struct hostent* h = nullptr;
- if (isdigit(*host_name)) {
- DWORD a = inet_addr(host_name);
+ if (std::isdigit(*host_name)) {
+ auto a = inet_addr(host_name);
h = gethostbyaddr((const char*) &a, 4, AF_INET);
}
else {
@@ -33,7 +44,7 @@ NetAddr::NetAddr(const char* host_name, WORD p)
if (h) {
if (h->h_addr_list) {
- addr = **(DWORD**) (h->h_addr_list);
+ addr = **(std::uint32_t**) (h->h_addr_list);
}
}
}
@@ -41,30 +52,34 @@ NetAddr::NetAddr(const char* host_name, WORD p)
Init();
}
-NetAddr::NetAddr(DWORD a, WORD p)
- : addr(a), port(p)
+
+NetAddr::NetAddr(std::uint32_t a, std::uint16_t p) :
+ addr(a),
+ port(p)
{
Init();
}
-NetAddr::NetAddr(const NetAddr& n)
- : addr(n.addr), port(n.port)
+
+NetAddr::NetAddr(const NetAddr& n) :
+ addr(n.addr),
+ port(n.port)
{
Init();
}
-// +--------------------------------------------------------------------+
void
NetAddr::Init()
{
- ZeroMemory(&sadr, sizeof(sadr));
+ std::memset(&sadr, 0, sizeof(sadr));
sadr.sin_family = AF_INET;
sadr.sin_port = ::htons(port);
sadr.sin_addr.s_addr = addr;
}
+
void
NetAddr::InitFromSockAddr()
{
@@ -72,7 +87,6 @@ NetAddr::InitFromSockAddr()
port = ::ntohs(sadr.sin_port);
}
-// +--------------------------------------------------------------------+
sockaddr*
NetAddr::GetSockAddr() const
@@ -80,26 +94,25 @@ NetAddr::GetSockAddr() const
return (sockaddr*) &sadr;
}
-size_t
+
+std::size_t
NetAddr::GetSockAddrLength() const
{
return sizeof(sadr);
}
-// +--------------------------------------------------------------------+
void
NetAddr::SetSockAddr(sockaddr* s, int size)
{
if (s) {
- ZeroMemory(&sadr, sizeof(sadr));
+ std::memset(&sadr, 0, sizeof(sadr));
if (size > sizeof(sadr))
- CopyMemory(&sadr, s, sizeof(sadr));
+ std::memcpy(&sadr, s, sizeof(sadr));
else
- CopyMemory(&sadr, s, size);
+ std::memcpy(&sadr, s, sizeof(sadr));
InitFromSockAddr();
}
}
-
diff --git a/NetEx/NetAddr.h b/NetEx/NetAddr.h
index 82f3420..b3d1c87 100644
--- a/NetEx/NetAddr.h
+++ b/NetEx/NetAddr.h
@@ -3,41 +3,44 @@
Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
Copyright (c) 1997-2006, Destroyer Studios LLC.
- AUTHOR: John DiCamillo
+ AUTHOR: John DiCamillo
- OVERVIEW
- ========
- Network Address (specifically, Internet Protocol)
+ OVERVIEW
+ ========
+ Network Address (specifically, Internet Protocol)
*/
-
#ifndef NetAddr_h
#define NetAddr_h
-#include <windows.h>
-#include <winsock.h>
+#ifdef _WIN32
+#include <winsock2.h>
+#else
+#include <sys/socket.h>
+#endif
+
+#include <cstdint>
-// +-------------------------------------------------------------------+
class NetAddr
{
public:
static const char* TYPENAME() { return "NetAddr"; }
- NetAddr(const char* a, WORD p=0);
- NetAddr(DWORD a=0, WORD p=0);
+ NetAddr(const char* a, std::uint16_t p=0);
+ NetAddr(std::uint32_t a=0, std::uint16_t p=0);
NetAddr(const NetAddr& n);
int operator == (const NetAddr& a) const { return addr==a.addr && port==a.port; }
- DWORD IPAddr() const { return addr; }
- BYTE B4() const { return (BYTE) ((addr & 0xff000000) >> 24); }
- BYTE B3() const { return (BYTE) ((addr & 0x00ff0000) >> 16); }
- BYTE B2() const { return (BYTE) ((addr & 0x0000ff00) >> 8); }
- BYTE B1() const { return (BYTE) ((addr & 0x000000ff) ); }
+ std::uint32_t IPAddr() const { return addr; }
+ std::uint8_t B4() const { return static_cast<std::uint8_t>((addr & 0xff000000) >> 24); }
+ std::uint8_t B3() const { return static_cast<std::uint8_t>((addr & 0x00ff0000) >> 16); }
+ std::uint8_t B2() const { return static_cast<std::uint8_t>((addr & 0x0000ff00) >> 8); }
+ std::uint8_t B1() const { return static_cast<std::uint8_t>((addr & 0x000000ff) ); }
- WORD Port() const { return port; }
+ std::uint16_t Port() const { return port; }
void SetPort(WORD p) { port = p; }
sockaddr* GetSockAddr() const;
@@ -49,8 +52,8 @@ public:
private:
void Init();
- DWORD addr; // IP addr in host byte order
- WORD port; // IP port in host byte order
+ std::uint32_t addr; // IP addr in host byte order
+ std::uint16_t port; // IP port in host byte order
sockaddr_in sadr;
};