diff options
Diffstat (limited to 'Client/Source/Network/Address.h')
-rw-r--r-- | Client/Source/Network/Address.h | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/Client/Source/Network/Address.h b/Client/Source/Network/Address.h index e69de29..fcace88 100644 --- a/Client/Source/Network/Address.h +++ b/Client/Source/Network/Address.h @@ -0,0 +1,59 @@ +#pragma once + +#include "../Utilities/Type.h" + +#include <memory.h> +#include <ostream> +#ifdef _WIN32 +#ifndef _WIN32_WINNT +#define _WIN32_WINNT 0x0501 +#endif +#include <windows.h> +#include <winsock2.h> +#include <ws2tcpip.h> +#else +#include <netinet/in.h> +#include <sys/socket.h> +#endif + +class IPv6AddressBytes +{ +public: + u8 bytes[16]; + IPv6AddressBytes() { memset(bytes, 0, 16); } +}; + +class Address +{ +public: + Address(); + Address(u32 address, u16 port); + Address(u8 a, u8 b, u8 c, u8 d, u16 port); + Address(const IPv6AddressBytes *ipv6_bytes, u16 port); + bool operator==(const Address &address); + bool operator!=(const Address &address); + // Resolve() may throw ResolveError (address is unchanged in this case) + void Resolve(const char *name); + struct sockaddr_in getAddress() const; + unsigned short getPort() const; + void setAddress(u32 address); + void setAddress(u8 a, u8 b, u8 c, u8 d); + void setAddress(const IPv6AddressBytes *ipv6_bytes); + struct sockaddr_in6 getAddress6() const; + int getFamily() const; + bool isIPv6() const; + bool isZero() const; + void setPort(unsigned short port); + void print(std::ostream *s) const; + std::string serializeString() const; + bool isLocalhost() const; + +private: + unsigned int m_addr_family = 0; + union + { + struct sockaddr_in ipv4; + struct sockaddr_in6 ipv6; + } m_address; + u16 m_port = 0; // Port is separate from sockaddr structures +}; |