sources/network/ipaddress.cpp

Wed, 27 Jan 2021 19:44:36 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 27 Jan 2021 19:44:36 +0200
changeset 194
0c7e44e1078a
parent 190
90bf9049e5eb
permissions
-rw-r--r--

added a lot of 'this->'

9
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
1 /*
190
90bf9049e5eb updated copyright year
Teemu Piippo <teemu@hecknology.net>
parents: 188
diff changeset
2 Copyright 2014 - 2021 Teemu Piippo
9
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
3 All rights reserved.
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
4
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
5 Redistribution and use in source and binary forms, with or without
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
6 modification, are permitted provided that the following conditions
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
7 are met:
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
8
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
9 1. Redistributions of source code must retain the above copyright
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
10 notice, this list of conditions and the following disclaimer.
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
11 2. Redistributions in binary form must reproduce the above copyright
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
12 notice, this list of conditions and the following disclaimer in the
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
13 documentation and/or other materials provided with the distribution.
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
14 3. Neither the name of the copyright holder nor the names of its
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
15 contributors may be used to endorse or promote products derived from
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
16 this software without specific prior written permission.
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
17
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
19 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
20 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
21 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
22 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
23 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
24 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
25 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
26 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
29 */
e7a09ceb4505 - now with more license headers
Teemu Piippo <crimsondusk64@gmail.com>
parents: 8
diff changeset
30
81
a18aaf460648 Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
31 #include "ipaddress.h"
a18aaf460648 Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
32
a18aaf460648 Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
33 #ifndef _WIN32
a18aaf460648 Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
34 # include <netinet/in.h>
a18aaf460648 Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
35 # include <netdb.h>
a18aaf460648 Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
36 #else
a18aaf460648 Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
37 # include <winsock2.h>
a18aaf460648 Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
38 # include <ws2tcpip.h>
a18aaf460648 Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
39 #endif
a18aaf460648 Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
40
88
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 81
diff changeset
41 BEGIN_ZFC_NAMESPACE
81
a18aaf460648 Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
42
a18aaf460648 Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
43 #ifdef _WIN32
88
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 81
diff changeset
44 typedef ADDRINFOA AddrInfo;
81
a18aaf460648 Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
45 #else
88
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 81
diff changeset
46 typedef struct addrinfo AddrInfo;
81
a18aaf460648 Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
47 #endif
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48
186
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
49 net::octet_t net::ip_octet(const net::ip_address& address, unsigned char n)
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
50 {
186
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
51 assert(n < 4);
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
52 return (address.host >> ((3 - n) * 8)) & 0xFF;
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
53 }
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54
186
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
55 void net::ip_set_octet(net::ip_address* address, unsigned char n, net::octet_t octet)
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
56 {
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
57 // TODO: make a big-endian version
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
58 assert(n < 4);
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
59 address->host &= ~(0xFF << (3 - n) * 8);
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
60 address->host |= octet << ((3 - n) * 8);
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
61 }
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62
186
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
63 std::string net::ip_address_to_string(const net::ip_address& address)
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
64 {
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
65 const auto octet = [&](unsigned char n) { return ip_octet(address, n); };
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
66 std::string val = sprintf ("%u.%u.%u.%u:%u", octet(0), octet(1), octet(2), octet(3), address.port);
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67 return val;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69
186
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
70 int net::ip_compare(const net::ip_address &one, const net::ip_address &other)
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71 {
186
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
72 if (one.host != other.host)
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
73 {
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
74 return one.host - other.host;
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
75 }
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
76 return one.port - other.port;
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
77 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
78
186
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
79 bool net::ip_address::operator<(const ip_address& other) const
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80 {
186
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
81 return ip_compare(*this, other) < 0;
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
82 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83
186
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
84 bool net::ip_address::operator==(const net::ip_address &other) const
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85 {
186
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
86 return this->host == other.host and this->port == other.port;
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
89 // -----------------------------------------------------------------------------
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90 //
186
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
91 sockaddr_in net::ip_address_to_sockaddr_in(const net::ip_address& address)
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
92 {
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
93 sockaddr_in claddr;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94 memset (&claddr, 0, sizeof claddr);
186
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
95 claddr.sin_addr.s_addr = htonl(address.host);
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
96 claddr.sin_port = htons(address.port);
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97 claddr.sin_family = AF_INET;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
98 return claddr;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
99 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
100
186
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
101 std::optional<unsigned short> net::ip_parse_port(const char* port_string, std::ostream& errorStream)
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102 {
186
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
103 std::optional<long> opt = to_int(port_string);
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
104 if (opt.has_value())
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
105 {
186
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
106 if (*opt >= 0 and *opt < 65536)
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
107 {
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
108 return static_cast<unsigned short>(opt.value());
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
109 }
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
110 else
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
111 {
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
112 return {};
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
113 errorStream << "port is not in range"s;
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
114 }
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
115 }
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
116 else
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
117 {
186
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
118 return {};
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
119 errorStream << "could not parse port"s;
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
120 }
186
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
121 }
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
122
186
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
123 std::optional<net::ip_address> net::ip_resolve_hostname(const std::string& node, std::ostream& errorStream)
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
124 {
81
a18aaf460648 Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
125 AddrInfo hints;
a18aaf460648 Allow compilation on Windows/MinGW
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
126 AddrInfo* lookup;
186
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
127 memset(&hints, 0, sizeof hints);
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
128 hints.ai_family = AF_INET;
186
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
129 std::optional<net::ip_address> result = {};
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
130 if (::getaddrinfo(node.data(), nullptr, &hints, &lookup) != 0)
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
131 {
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
132 errorStream << "unknown host "s + node;
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
133 }
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
134 else
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
135 {
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
136 assert (lookup != nullptr);
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
137 sockaddr_in* addr = reinterpret_cast<sockaddr_in*>(lookup[0].ai_addr);
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
138 result = net::ip_address{ntohl(addr->sin_addr.s_addr), ntohs(addr->sin_port)};
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
139 ::freeaddrinfo(lookup);
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
140 }
5
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
141 return result;
146825d63b9a - code cleanup
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
142 }
88
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 81
diff changeset
143
186
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
144 std::optional<net::ip_address> net::ip_resolve(const std::string& input_string, std::ostream& errorStream)
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
145 {
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
146 int colonpos = input_string.find(":");
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
147 std::string addressString = colonpos == -1 ? input_string : mid(input_string, 0, colonpos);
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
148 std::optional<net::ip_address> value;
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
149 // Try scanf the IPv4 host first
188
5fc32e4b2a8c validate the ip address octets better
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
150 int parts[4];
5fc32e4b2a8c validate the ip address octets better
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
151 if (std::sscanf(addressString.data(), "%d.%d.%d.%d", &parts[0], &parts[1], &parts[2], &parts[3]))
5fc32e4b2a8c validate the ip address octets better
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
152 {
5fc32e4b2a8c validate the ip address octets better
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
153 value = net::ip_address{};
5fc32e4b2a8c validate the ip address octets better
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
154 for (unsigned char i = 0; i < 4; i += 1)
5fc32e4b2a8c validate the ip address octets better
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
155 {
5fc32e4b2a8c validate the ip address octets better
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
156 if (parts[i] >= 0 and parts[i] < 256)
5fc32e4b2a8c validate the ip address octets better
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
157 {
5fc32e4b2a8c validate the ip address octets better
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
158 ip_set_octet(&*value, i, parts[i]);
5fc32e4b2a8c validate the ip address octets better
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
159 }
5fc32e4b2a8c validate the ip address octets better
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
160 else
5fc32e4b2a8c validate the ip address octets better
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
161 {
5fc32e4b2a8c validate the ip address octets better
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
162 value.reset();
5fc32e4b2a8c validate the ip address octets better
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
163 errorStream << "IP address value out of range";
5fc32e4b2a8c validate the ip address octets better
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
164 break;
5fc32e4b2a8c validate the ip address octets better
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
165 }
5fc32e4b2a8c validate the ip address octets better
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
166 }
5fc32e4b2a8c validate the ip address octets better
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
167 }
5fc32e4b2a8c validate the ip address octets better
Teemu Piippo <teemu@hecknology.net>
parents: 186
diff changeset
168 else
186
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
169 {
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
170 // Possibly a hostname, try resolve it
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
171 value = ip_resolve_hostname(addressString, errorStream);
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
172 }
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
173 if (value.has_value() and colonpos != -1)
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
174 {
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
175 std::optional<unsigned short> port_opt = ip_parse_port(&input_string.data()[colonpos + 1], errorStream);
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
176 if (port_opt.has_value())
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
177 {
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
178 value->port = *port_opt;
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
179 }
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
180 else
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
181 {
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
182 value.reset();
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
183 }
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
184 }
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
185 return value;
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
186 }
9330b93d9946 cleanup ip address code
Teemu Piippo <teemu@hecknology.net>
parents: 185
diff changeset
187
88
08ccaf26cffd Now works with MSVC 2010/pdcurses-win32a
Teemu Piippo <tsapii@utu.fi>
parents: 81
diff changeset
188 END_ZFC_NAMESPACE

mercurial