summaryrefslogtreecommitdiff
path: root/Client/Source/Network/Address.h
blob: fcace880201a82713f94b876644115a913ee92a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
};