Minetest  5.4.0
address.h
Go to the documentation of this file.
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14 
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 
20 #pragma once
21 
22 #ifdef _WIN32
23 #ifndef _WIN32_WINNT
24 #define _WIN32_WINNT 0x0501
25 #endif
26 #include <windows.h>
27 #include <winsock2.h>
28 #include <ws2tcpip.h>
29 #else
30 #include <netinet/in.h>
31 #include <sys/socket.h>
32 #endif
33 
34 #include <ostream>
35 #include <cstring>
36 #include "irrlichttypes.h"
37 #include "networkexceptions.h"
38 
40 {
41 public:
42  u8 bytes[16];
43  IPv6AddressBytes() { memset(bytes, 0, 16); }
44 };
45 
46 class Address
47 {
48 public:
49  Address();
50  Address(u32 address, u16 port);
51  Address(u8 a, u8 b, u8 c, u8 d, u16 port);
52  Address(const IPv6AddressBytes *ipv6_bytes, u16 port);
53  bool operator==(const Address &address);
54  bool operator!=(const Address &address);
55  // Resolve() may throw ResolveError (address is unchanged in this case)
56  void Resolve(const char *name);
57  struct sockaddr_in getAddress() const;
58  unsigned short getPort() const;
59  void setAddress(u32 address);
60  void setAddress(u8 a, u8 b, u8 c, u8 d);
61  void setAddress(const IPv6AddressBytes *ipv6_bytes);
62  struct sockaddr_in6 getAddress6() const;
63  int getFamily() const;
64  bool isIPv6() const;
65  bool isZero() const;
66  void setPort(unsigned short port);
67  void print(std::ostream *s) const;
68  std::string serializeString() const;
69  bool isLocalhost() const;
70 
71 private:
72  unsigned int m_addr_family = 0;
73  union
74  {
75  struct sockaddr_in ipv4;
76  struct sockaddr_in6 ipv6;
78  u16 m_port = 0; // Port is separate from sockaddr structures
79 };
Definition: address.h:47
unsigned short getPort() const
Definition: address.cpp:209
bool isIPv6() const
Definition: address.cpp:219
int getFamily() const
Definition: address.cpp:214
void print(std::ostream *s) const
Definition: address.cpp:267
void setAddress(u32 address)
Definition: address.cpp:237
bool operator!=(const Address &address)
Definition: address.cpp:109
Address()
Definition: address.cpp:64
void Resolve(const char *name)
Definition: address.cpp:114
bool operator==(const Address &address)
Definition: address.cpp:91
struct sockaddr_in6 ipv6
Definition: address.h:76
unsigned int m_addr_family
Definition: address.h:72
bool isLocalhost() const
Definition: address.cpp:275
bool isZero() const
Definition: address.cpp:224
void setPort(unsigned short port)
Definition: address.cpp:262
u16 m_port
Definition: address.h:78
struct sockaddr_in6 getAddress6() const
Definition: address.cpp:204
std::string serializeString() const
Definition: address.cpp:161
struct sockaddr_in getAddress() const
Definition: address.cpp:199
union Address::@19 m_address
struct sockaddr_in ipv4
Definition: address.h:75
Definition: address.h:40
IPv6AddressBytes()
Definition: address.h:43
u8 bytes[16]
Definition: address.h:42