Newer
Older
#ifndef _IPADDR_HPP
#define _IPADDR_HPP
#include <string>
#include <netinet/in.h>
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
* @brief The IpAddr class represents an ip address that can either be of type
* Ipv4 or of type Ipv6.
*/
class IpAddr
{
private:
/**
* @brief The type of this IpAddr (either Ipv4, Ipv6 or undefined)
*/
enum class Type {
/** @brief The IpAddr represents an Ipv4 address. */
V4,
/** @brief The IpAddr represents an Ipv6 address. */
V6,
/** @brief The IpAddr has not been set, so the address type is undefined. */
Undef
};
/**
* @brief The low level raw in_addr or in6_addr struct, depending on the
* type.
*
* If the IpAddr type is V4, this is in_addr, if the type is V6, this is
* in6_addr.
*/
union {
/** @brief Raw struct representing an ipv4 address */
in_addr v4;
/** @brief Raw struct representing an ipv6 address */
in6_addr v6;
} raw_addr;
/**
* @brief The ip address in string representation
*/
std::string str_addr;
/**
* @brief The ip address type (Ipv4, Ipv6 or undefined)
*/
Type type;
public:
/**
* @brief The default constructor creates the Ipv4 address "0.0.0.0".
*/
IpAddr();
/**
* @brief Create an IpAddr from the given string representation. The
* address type is determined automatically.
*
* If string can't be parsed as either Ipv4 or Ipv6 address, an exception
* is thrown.
*
* @param address The string representing an ip address.
*/
IpAddr(const std::string &address);
/**
* @brief Create an Ipv4 IpAddr from the given string representation.
*
* If the string cannot be parsed as an Ipv4 address, an exception is
* thrown.
*
* @param address The string representing an Ipv4 ip address.
*
* @returns The newly constructed IpAddr object.
*/
static IpAddr V4(const std::string &address);
/**
* @brief Create an Ipv6 IpAddr from the given string representation.
*
* If the string cannot be parsed as an Ipv6 address, an exception is
* thrown.
*
* @param address The string representing an Ipv6 ip address.
*
* @return The newly constructed IpAddr object.
*/
static IpAddr V6(const std::string &address);
/**
* @brief Check if the IpAddr is of type Ipv4.
*
* @return True if it is an Ipv4 address, false otherwise.
*/
bool isIpv4() const;
/**
* @brief Check if the IpAddr is of type Ipv6.
*
* @return True if it is an Ipv6 address, false otherwise.
*/
bool isIpv6() const;
/**
* @brief Check if the IpAddr is of type undefined.
*
* @return True if the address is not defined, false otherwise.
*/
bool isUndefined() const;
/**
* @brief Get the string representation of the IpAddr.
*
* @returns A string that represents the IpAddr. The string is in the same
* form as it was provided while constructing the address.
*/
const std::string & getAddressString() const;
friend class SockAddr;
friend class TcpStream;
friend class TcpListener;
friend class UdpSocket;
friend class Resolver;
};