Newer
Older
#include <iostream>
#include <string>
#include <cstring>
// Include the all-in-one headerfile. This will include all other headers
#include "netlib.hpp"
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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
void example_IpAddr()
{
// IpAddr takes an address as string and automatically detects if it is
// Ipv4 or Ipv6
// Autodetected Ipv4 example
IpAddr ip1("192.168.13.37");
std::cout << "IP1\n---\n"
<< "Address=" << ip1.getAddressString() << "\n"
<< "IsIpv4=" << ip1.isIpv4() << "\n"
<< "IsIpv6=" << ip1.isIpv6() << "\n\n";
// Autodetected Ipv6 example
IpAddr ip2("dead:beef::1");
std::cout << "IP2\n---\n"
<< "Address=" << ip2.getAddressString() << "\n"
<< "IsIpv4=" << ip2.isIpv4() << "\n"
<< "IsIpv6=" << ip2.isIpv6() << "\n\n";
// Ipv4 or Ipv6 addresses can also be created explicitely by using
// IpAddr::V4(addr) and IpAddr::V6(addr). If the provided address string is
// not a valid address of the type, an exception will be thrown
// Explicit Ipv6 example
ip1 = IpAddr::V6("dead:beef::1");
std::cout << "IP1 after change\n----------------\n"
<< "Address=" << ip1.getAddressString() << "\n"
<< "IsIpv4=" << ip1.isIpv4() << "\n"
<< "IsIpv6=" << ip1.isIpv6() << "\n\n";
// Explicit Ipv4 example
ip2 = IpAddr::V4("192.168.13.37");
std::cout << "IP2 after change\n----------------\n"
<< "Address=" << ip2.getAddressString() << "\n"
<< "IsIpv4=" << ip2.isIpv4() << "\n"
<< "IsIpv6=" << ip2.isIpv6() << "\n\n";
// Specifying an Ipv6 address with the explicit Ipv4 will throw an exception
try
{
auto ip = IpAddr::V4("::1");
std::cout << "If this code is executed, something went very wrong\n\n";
}
catch (const std::exception & e)
{
std::cout << "This exception is intended: \n"
<< e.what() << "\n\n";
}
// Specifying an Ipv4 address with the explicit Ipv6 will throw an exception
try
{
auto ip = IpAddr::V6("192.168.13.37");
std::cout << "If this code is executed, something went very wrong\n\n";
}
catch (const std::exception & e)
{
std::cout << "This exception is intended: \n"
<< e.what() << "\n\n";
}
}
void example_SockAddr()
{
// SockAddr is the combination of an IpAddress and a port. The IpAddr will
// handle the Ipv4/Ipv6 type and the port is an unsigned 16 bit integer.
// There are multiple ways to create a SockAddr.
// Create a SockAddr by passing an IpAddr and a port
IpAddr ip1("192.168.13.37");
SockAddr sa1(ip1, 1337);
std::cout << "SockAddr1\n--------\n"
<< "IpAddr=" << sa1.getIpAddressString() << "\n"
<< "Port=" << sa1.getPort() << "\n\n";
// Create a SockAddr by passing an IpAddress as string and a port
// The the ip string the same rules apply as for the IpAddr string
// constructor. That means the type (Ipv4/Ipv6) is determined automatically.
SockAddr sa2("10.10.0.69", 48879);
std::cout << "SockAddr2\n--------\n"
<< "IpAddr=" << sa2.getIpAddressString() << "\n"
<< "Port=" << sa2.getPort() << "\n\n";
// Create a SockAddr by passing a "ipaddr:port" string with an Ipv4 address.
// If the port is not a unsigned 16 bit integer, or the IpAddress is invalid,
// an exception will be thrown.
SockAddr sa3("10.11.12.13:443");
std::cout << "SockAddr3\n--------\n"
<< "IpAddr=" << sa3.getIpAddressString() << "\n"
<< "Port=" << sa3.getPort() << "\n\n";
// Create a SockAddr by passing a "ipaddr:port" string with an Ipv6 address.
// For Ipv6 addresses, the address must be placed inside []. Otherwise it
// behaves the same as with Ipv4 addresses. The [] can not be used with
// Ipv4 addresses.
SockAddr sa4("[dead:beef::1337]:8080");
std::cout << "SockAddr4\n--------\n"
<< "IpAddr=" << sa4.getIpAddressString() << "\n"
<< "Port=" << sa4.getPort() << "\n\n";
}
void example_resolver()
{
// The Resolver helps with resolving hostnames to IpAddresses. It can
// resolve either just the first address (which is wanted most of the time),
// or it can fetch all associated IpAddresses for a given hostname. Both
// Ipv4 and Ipv6 addresses are supported.
std::string hostname = "one.one.one.one";
// Fetch Ipv4 address for the given hostname
IpAddr ip4 = Resolver::resolveHostnameIpv4(hostname);
std::cout << "Resolve Ipv4 for " << hostname << "\n"
<< " => " << ip4.getAddressString() << "\n\n";
// Fetch Ipv6 address for the given hostname
IpAddr ip6 = Resolver::resolveHostnameIpv6(hostname);
std::cout << "Resolve Ipv6 for " << hostname << "\n"
<< " => " << ip6.getAddressString() << "\n\n";
// Fetch all associated Ipv4 addresses for the given hostname
auto ip4s = Resolver::resolveHostnameAllIpv4(hostname);
std::cout << "Resolve all Ipv4 addresses for " << hostname << "\n";
for (auto ip : ip4s) std::cout << " => " << ip.getAddressString() << "\n";
std::cout << "\n";
// Fetch all associated Ipv6 addresses for the given hostname
auto ip6s = Resolver::resolveHostnameAllIpv6(hostname);
std::cout << "Resolve all Ipv6 addresses for " << hostname << "\n";
for (auto ip : ip6s) std::cout << " => " << ip.getAddressString() << "\n";
std::cout << "\n";
// Fetch all associated Ipv4 and Ipv6 addresses for the given hostname
auto ip46s = Resolver::resolveHostnameAll(hostname);
std::cout << "Resolve all Ipv4 & Ipv6 addresses for " << hostname << "\n";
for (auto ip : ip46s)
{
std::cout << " => " << "isIpV4=" << ip.isIpv4()
<< " " << ip.getAddressString() << "\n";
}
std::cout << "\n";
// When a given hostname could not be resolved, an exception is thrown
try
{
Resolver::resolveHostnameAll("thisisaninvalidhostname-6iesw5rb7fwa54r.com");
std::cout << "If this code is executed, something went very wrong\n\n";
}
catch (const std::exception & e)
{
std::cout << "This exception is intended: \n"
<< e.what() << "\n\n";
}
}
int main(int argc, char **argv)
{
example_IpAddr();
example_SockAddr();
example_resolver();
return 0;
}