Luanti 5.15.0-dev
 
Loading...
Searching...
No Matches
address.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
7#ifdef _WIN32
8#include <windows.h>
9#include <winsock2.h>
10#include <ws2tcpip.h>
11#else
12#include <netinet/in.h>
13#include <sys/socket.h>
14#endif
15
16#include <ostream>
17#include <cstring>
18#include "irrlichttypes.h"
19
21{
22 u8 bytes[16];
23 IPv6AddressBytes() { memset(bytes, 0, 16); }
24};
25
27{
28public:
29 Address();
30 Address(u32 address, u16 port);
31 Address(u8 a, u8 b, u8 c, u8 d, u16 port);
32 Address(const IPv6AddressBytes *ipv6_bytes, u16 port);
33
34 bool operator==(const Address &address) const;
35 bool operator!=(const Address &address) const { return !(*this == address); }
36
37 int getFamily() const { return m_addr_family; }
38 bool isValid() const { return m_addr_family != 0; }
39 bool isIPv6() const { return m_addr_family == AF_INET6; }
40 struct in_addr getAddress() const { return m_address.ipv4; }
41 struct in6_addr getAddress6() const { return m_address.ipv6; }
42 u16 getPort() const { return m_port; }
43
44 void print(std::ostream &s) const;
45 std::string serializeString() const;
46
47 // Is this an address that binds to all interfaces (like INADDR_ANY)?
48 bool isAny() const;
49 // Is this an address referring to the local host?
50 bool isLocalhost() const;
51
52 // `name`: hostname or numeric IP
53 // `fallback`: fallback IP to try gets written here
54 // any empty name resets the IP to the "any address"
55 // may throw ResolveError (address is unchanged in this case)
56 void Resolve(const char *name, Address *fallback = nullptr);
57
58 void setAddress(u32 address);
59 void setAddress(u8 a, u8 b, u8 c, u8 d);
60 void setAddress(const IPv6AddressBytes *ipv6_bytes);
61 void setPort(u16 port);
62
63private:
64 unsigned short m_addr_family = 0;
65 union
66 {
67 struct in_addr ipv4;
68 struct in6_addr ipv6;
70 // port is separate from in_addr structures
71 u16 m_port = 0;
72};
Definition address.h:27
struct in6_addr getAddress6() const
Definition address.h:41
u16 getPort() const
Definition address.h:42
void print(std::ostream &s) const
Definition address.cpp:184
bool isIPv6() const
Definition address.h:39
int getFamily() const
Definition address.h:37
void setAddress(u32 address)
Definition address.cpp:158
Address()
Definition address.cpp:36
unsigned short m_addr_family
Definition address.h:64
bool isValid() const
Definition address.h:38
union Address::@21 m_address
struct in_addr getAddress() const
Definition address.h:40
bool operator!=(const Address &address) const
Definition address.h:35
bool operator==(const Address &address) const
Definition address.cpp:63
struct in_addr ipv4
Definition address.h:67
bool isLocalhost() const
Definition address.cpp:194
u16 m_port
Definition address.h:71
std::string serializeString() const
Definition address.cpp:138
void setPort(u16 port)
Definition address.cpp:179
bool isAny() const
Definition address.cpp:146
struct in6_addr ipv6
Definition address.h:68
void Resolve(const char *name, Address *fallback=nullptr)
Definition address.cpp:80
Definition address.h:21
IPv6AddressBytes()
Definition address.h:23
u8 bytes[16]
Definition address.h:22