sptk2 logo
SPTK Home Page
Host.h
1/*
2╔══════════════════════════════════════════════════════════════════════════════╗
3║ SIMPLY POWERFUL TOOLKIT (SPTK) ║
4╟──────────────────────────────────────────────────────────────────────────────╢
5║ copyright © 1999-2022 Alexey Parshin. All rights reserved. ║
6║ email alexeyp@gmail.com ║
7╚══════════════════════════════════════════════════════════════════════════════╝
8┌──────────────────────────────────────────────────────────────────────────────┐
9│ This library is free software; you can redistribute it and/or modify it │
10│ under the terms of the GNU Library General Public License as published by │
11│ the Free Software Foundation; either version 2 of the License, or (at your │
12│ option) any later version. │
13│ │
14│ This library is distributed in the hope that it will be useful, but │
15│ WITHOUT ANY WARRANTY; without even the implied warranty of │
16│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library │
17│ General Public License for more details. │
18│ │
19│ You should have received a copy of the GNU Library General Public License │
20│ along with this library; if not, write to the Free Software Foundation, │
21│ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. │
22│ │
23│ Please report all bugs and problems to alexeyp@gmail.com. │
24└──────────────────────────────────────────────────────────────────────────────┘
25*/
26
27#pragma once
28
29#include <cstring>
30#include <mutex>
31#include <sptk5/Strings.h>
32#include <sptk5/threads/Locks.h>
33#include <sstream>
34
35#ifndef _WIN32
36
37#include <netinet/in.h>
38
39#else
40#include <WS2tcpip.h>
41#include <winsock2.h>
42#endif
43
44namespace sptk {
45
54class SP_EXPORT Host
55{
56 mutable SharedMutex m_mutex;
57 String m_hostname;
58 uint16_t m_port {0};
59 std::array<uint8_t, sizeof(sockaddr_in6)> m_address {};
60
65 sockaddr& any()
66 {
67 return *(sockaddr*) m_address.data();
68 }
69
74 const sockaddr& any() const
75 {
76 return *(const sockaddr*) m_address.data();
77 }
78
83 sockaddr_in& ip_v4()
84 {
85 return *(sockaddr_in*) m_address.data();
86 }
87
92 const sockaddr_in& ip_v4() const
93 {
94 return *(const sockaddr_in*) m_address.data();
95 }
96
101 sockaddr_in6& ip_v6()
102 {
103 return *(sockaddr_in6*) m_address.data();
104 }
105
110 const sockaddr_in6& ip_v6() const
111 {
112 return *(const sockaddr_in6*) m_address.data();
113 }
114
118 void getHostAddress();
119
124 void setPort(uint16_t p);
125
126public:
130 Host() noexcept;
131
137 Host(const String& hostname, uint16_t port);
138
144 explicit Host(const String& hostAndPort);
145
150 explicit Host(const sockaddr_in* addressAndPort);
151
156 explicit Host(const sockaddr_in6* addressAndPort);
157
162 Host(const Host& other);
163
168 Host(Host&& other) noexcept;
169
173 virtual ~Host() = default;
174
179 Host& operator=(const Host& other);
180
185 Host& operator=(Host&& other) noexcept;
186
192 bool operator==(const Host& other) const;
193
199 bool operator!=(const Host& other) const;
200
205 const String& hostname() const
206 {
207 SharedLock(m_mutex);
208 return m_hostname;
209 }
210
215 void port(uint16_t p)
216 {
217 setPort(p);
218 }
219
224 uint16_t port() const
225 {
226 SharedLock(m_mutex);
227 return m_port;
228 }
229
236 String toString(bool forceAddress = false) const;
237
241 void getAddress(sockaddr_in& address) const
242 {
243 SharedLock(m_mutex);
244 memcpy(&address, &m_address, sizeof(address));
245 }
246
250 void getAddress(sockaddr_in6& address) const
251 {
252 SharedLock(m_mutex);
253 memcpy(&address, &m_address, sizeof(address));
254 }
255
256 void setHostNameFromAddress(socklen_t addressLen);
257};
258
259using SHost = std::shared_ptr<Host>;
260
267class SP_EXPORT HostCompare
268{
269public:
275 bool operator()(const Host& s1, const Host& s2) const
276 {
277#ifdef WIN32
278 return stricmp(s1.toString(true).c_str(), s2.toString(true).c_str()) > 0;
279#else
280 return strcasecmp(s1.toString(true).c_str(), s2.toString(true).c_str()) > 0;
281#endif
282 }
283};
284
285
290} // namespace sptk
Case-insensitive host compare class.
Definition: Host.h:268
bool operator()(const Host &s1, const Host &s2) const
Compare method.
Definition: Host.h:275
Definition: Host.h:55
void port(uint16_t p)
Definition: Host.h:215
String toString(bool forceAddress=false) const
void getAddress(sockaddr_in6 &address) const
Definition: Host.h:250
void getAddress(sockaddr_in &address) const
Definition: Host.h:241
uint16_t port() const
Definition: Host.h:224
Host() noexcept
Definition: String.h:49

Fri Oct 14 2022 09:58:32: SPTK 5.4.1