UPnPsdk 0.1
Universal Plug and Play +, Software Development Kit
 
Loading...
Searching...
No Matches
sockaddr.hpp
Go to the documentation of this file.
1#ifndef UPnPsdk_NET_SOCKADDR_HPP
2#define UPnPsdk_NET_SOCKADDR_HPP
3// Copyright (C) 2022+ GPL 3 and higher by Ingo Höft, <Ingo@Hoeft-online.de>
4// Redistribution only with this Copyright remark. Last modified: 2026-02-22
11#include <UPnPsdk/port.hpp>
12#include <UPnPsdk/port_sock.hpp>
14#ifdef _MSC_VER
15#include <afunix.h>
16#else
17#include <sys/un.h>
18#endif
19
20#include <string>
22
23namespace UPnPsdk {
24
35 ::sockaddr_storage ss;
36 ::sockaddr_un sun;
37 ::sockaddr_in6 sin6;
38 ::sockaddr_in sin;
39 ::sockaddr sa;
40};
41
42
74int to_port( //
76 std::string_view a_port_str,
79 in_port_t* const a_port_num = nullptr) noexcept;
80
81
106void split_addr_port( //
109 std::string_view a_addr_str,
112 std::string& a_addr,
114 std::string& a_serv);
115
116
133struct UPnPsdk_API SSockaddr {
135 sockaddr_storage& ss = m_sa_union.ss;
137 sockaddr_un& sun = m_sa_union.sun;
139 sockaddr_in6& sin6 = m_sa_union.sin6;
141 sockaddr_in& sin = m_sa_union.sin;
143 sockaddr& sa = m_sa_union.sa;
144
145 // Constructor
146 // -----------
147 SSockaddr();
148
149 // Destructor
150 // ----------
151 virtual ~SSockaddr();
152
153 // Get reference to the sockaddr_storage structure.
154 // Only as example, I don't use it because it may be confusing. I only use
155 // SSockaddr::ss (instantiated e.g. ssObj.ss) to access the trivial
156 // member structure.
157 // operator const ::sockaddr_storage&() const;
158
159 // Copy constructor
168 SSockaddr(const SSockaddr&);
169
170
171 // Copy assignment operator
178 // Strong exception guarantee with value argument as given.
179 SSockaddr& operator=(SSockaddr); // value argument
180
181
185 // Assignment operator to set a netaddress
186 // ---------------------------------------
231 void operator=(
233 std::string_view a_addr_str); // noexept?
234
235
236 // Assignment operator= to set socket port from an integer
237 // -------------------------------------------------------
244 void operator=(const in_port_t a_port);
245
246
247 // Assignment operator= to set socket address from a trivial socket address
248 // structure
249 // ------------------------------------------------------------------------
257 void operator=(const ::sockaddr_storage& a_ss);
259
260
264 // Compare operator
265 // ----------------
273 bool operator==(const SSockaddr&) const;
274
275
276 // Getter for a netaddress
277 // -----------------------
285 const std::string netaddr() noexcept;
286
287
288 // Getter for a netaddress with port
289 // ---------------------------------
296 const std::string netaddrp() noexcept;
297
298
300 // ----------------------------
301 in_port_t port() const;
302
303
305 // ----------------------------------------------------------------------
306 socklen_t sizeof_saddr() const;
308
309
311 // -------------------------------------------------------
312 bool is_loopback() const;
313
314
315 private:
316 sockaddr_t m_sa_union{}; // this is the union of trivial sockaddr structures
317 // that is managed.
318};
319
320
321// Getter of the netaddress to output stream
322// -----------------------------------------
332UPnPsdk_API ::std::ostream& operator<<(::std::ostream& os, SSockaddr& saddr);
333
334} // namespace UPnPsdk
335
336#endif // UPnPsdk_NET_SOCKADDR_HPP
Helpful union of the different socket address structures.
Definition sockaddr.hpp:34
int to_port(std::string_view a_port_str, in_port_t *const a_port_num=nullptr) noexcept
Free function to check if a string represents a valid port number.
Definition sockaddr.cpp:89
UPnPsdk_API::std::ostream & operator<<(::std::ostream &os, SSockaddr &saddr)
output the netaddress
void split_addr_port(std::string_view a_addr_str, std::string &a_addr, std::string &a_serv)
Free function to split inet address and port(service)
Definition sockaddr.cpp:185
Reengineered Object Oriented UPnP+ program code.
Specifications to be portable between different platforms.
Specifications to be portable with sockets between different platforms.
Trivial ::sockaddr structures enhanced with methods.
Definition sockaddr.hpp:133
sockaddr_storage & ss
Reference to sockaddr_storage struct.
Definition sockaddr.hpp:135
Macros to support visibility of external symbols.