Luanti 5.11.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#include "networkexceptions.h"
20
22{
23 u8 bytes[16];
24 IPv6AddressBytes() { memset(bytes, 0, 16); }
25};
26
28{
29public:
30 Address();
31 Address(u32 address, u16 port);
32 Address(u8 a, u8 b, u8 c, u8 d, u16 port);
33 Address(const IPv6AddressBytes *ipv6_bytes, u16 port);
34
35 bool operator==(const Address &address) const;
36 bool operator!=(const Address &address) const { return !(*this == address); }
37
38 int getFamily() const { return m_addr_family; }
39 bool isValid() const { return m_addr_family != 0; }
40 bool isIPv6() const { return m_addr_family == AF_INET6; }
41 struct in_addr getAddress() const { return m_address.ipv4; }
42 struct in6_addr getAddress6() const { return m_address.ipv6; }
43 u16 getPort() const { return m_port; }
44
45 void print(std::ostream &s) const;
46 std::string serializeString() const;
47
48 // Is this an address that binds to all interfaces (like INADDR_ANY)?
49 bool isAny() const;
50 // Is this an address referring to the local host?
51 bool isLocalhost() const;
52
53 // `name`: hostname or numeric IP
54 // `fallback`: fallback IP to try gets written here
55 // any empty name resets the IP to the "any address"
56 // may throw ResolveError (address is unchanged in this case)
57 void Resolve(const char *name, Address *fallback = nullptr);
58
59 void setAddress(u32 address);
60 void setAddress(u8 a, u8 b, u8 c, u8 d);
61 void setAddress(const IPv6AddressBytes *ipv6_bytes);
62 void setPort(u16 port);
63
64private:
65 unsigned short m_addr_family = 0;
66 union
67 {
68 struct in_addr ipv4;
69 struct in6_addr ipv6;
71 // port is separate from in_addr structures
72 u16 m_port = 0;
73};
Definition address.h:28
struct in6_addr getAddress6() const
Definition address.h:42
u16 getPort() const
Definition address.h:43
void print(std::ostream &s) const
Definition address.cpp:193
bool isIPv6() const
Definition address.h:40
int getFamily() const
Definition address.h:38
union Address::@20 m_address
void setAddress(u32 address)
Definition address.cpp:167
Address()
Definition address.cpp:45
unsigned short m_addr_family
Definition address.h:65
bool isValid() const
Definition address.h:39
struct in_addr getAddress() const
Definition address.h:41
bool operator!=(const Address &address) const
Definition address.h:36
bool operator==(const Address &address) const
Definition address.cpp:72
struct in_addr ipv4
Definition address.h:68
bool isLocalhost() const
Definition address.cpp:203
u16 m_port
Definition address.h:72
std::string serializeString() const
Definition address.cpp:147
void setPort(u16 port)
Definition address.cpp:188
bool isAny() const
Definition address.cpp:155
struct in6_addr ipv6
Definition address.h:69
void Resolve(const char *name, Address *fallback=nullptr)
Definition address.cpp:89
Definition address.h:22
IPv6AddressBytes()
Definition address.h:24
u8 bytes[16]
Definition address.h:23