UPnPsdk 0.1
Universal Plug and Play +, Software Development Kit
 
Loading...
Searching...
No Matches
netadapter.hpp
Go to the documentation of this file.
1#ifndef UPnPsdk_NETADAPTER_HPP
2#define UPnPsdk_NETADAPTER_HPP
3// Copyright (C) 2024+ GPL 3 and higher by Ingo Höft, <Ingo@Hoeft-online.de>
4// Redistribution only with this Copyright remark. Last modified: 2025-05-15
10#include <UPnPsdk/netadapter_platform.hpp>
11
12namespace UPnPsdk {
13
34uint8_t netmask_to_bitmask(
36 const ::sockaddr_storage* a_netmask);
37
38
59 const ::sockaddr_storage* a_saddr,
62 const unsigned int a_prefixlength,
65 SSockaddr& a_saddrObj);
66
67
100 // Due to warning C4251 "'type' : class 'type1' needs to have dll-interface
101 // to be used by clients of class 'type2'" on Microsoft Windows each member
102 // function needs to be decorated with UPnPsdk_API instead of just only the
103 // class. The reason is 'm_na_platformPtr'.
104 public:
106 UPnPsdk_API CNetadapter(
112 PNetadapter_platform a_na_platformPtr =
113 std::make_shared<CNetadapter_platform>());
114
115 /* \brief Destructor */
116 UPnPsdk_API virtual ~CNetadapter();
117
119 // Copy constructor
120 // We cannot use the default copy constructor because there is also
121 // allocated memory for the ifaddrs structure to copy. We get segfaults
122 // and program aborts. This class is not usable to copy the object.
123 CNetadapter(const CNetadapter&) = delete;
124
125 // Copy assignment operator
126 // Same as with the copy constructor.
127 CNetadapter& operator=(CNetadapter) = delete;
129
130 // methods from injected object
132 UPnPsdk_API void get_first();
134 UPnPsdk_API bool get_next();
136 UPnPsdk_API unsigned int index() const;
138 UPnPsdk_API std::string name() const;
140 UPnPsdk_API void sockaddr(SSockaddr& a_saddr) const;
142 UPnPsdk_API void socknetmask(SSockaddr& a_snetmask) const;
144 UPnPsdk_API unsigned int bitmask() const;
145
146 // Own methods
190 UPnPsdk_API bool find_first(
199 std::string_view a_name_or_addr = "");
200
213 UPnPsdk_API bool find_first(
215 const unsigned int a_index);
216
229 UPnPsdk_API bool find_next();
230
231 private:
233 // Injected smart pointer to the netadapter object of the current operating
234 // system. It may also point to a mocking object.
235 PNetadapter_platform m_na_platformPtr;
236
237 // State of the finding steps.
238 enum struct Find { finish, best, loopback, index } m_state{};
239
240 // Index of the first found network adapter.
241 // Only used with m_state = Find::index;
242 unsigned int m_find_index{};
244
245 // Private helper methods.
246 void reset() noexcept;
247};
248
249} // namespace UPnPsdk
250
251#endif // UPnPsdk_NETADAPTER_HPP
Get information from local network adapters.
UPnPsdk_API void get_first()
Load a list of network adapters from the operating system and select its first entry.
UPnPsdk_API void socknetmask(SSockaddr &a_snetmask) const
Get socket address netmask from current selected list entry.
UPnPsdk_API void sockaddr(SSockaddr &a_saddr) const
Get socket address from current selected list entry.
UPnPsdk_API bool find_next()
Find next ip address from local network adapters.
UPnPsdk_API unsigned int index() const
Get index number from current selected list entry.
UPnPsdk_API std::string name() const
Get network adapter name from current selected list entry.
UPnPsdk_API bool get_next()
Select next entry from the network adapter list that was initial loaded with get_first().
UPnPsdk_API bool find_first(std::string_view a_name_or_addr="")
Find local network adapter with given name or ip address.
UPnPsdk_API unsigned int bitmask() const
Get prefix length from the ip address of the current selected local network adapter.
uint8_t netmask_to_bitmask(const ::sockaddr_storage *a_netmask)
Get prefix bit number from a network address mask.
void bitmask_to_netmask(const ::sockaddr_storage *a_saddr, const unsigned int a_prefixlength, SSockaddr &a_saddrObj)
Get network address mask from address prefix bit number.
Reengineered Object Oriented UPnP+ program code.
std::shared_ptr< INetadapter > PNetadapter_platform
Smart pointer to hold the injected pointer to the netadapter object for the current used platform.
Trivial ::sockaddr structures enhanced with methods.
Definition sockaddr.hpp:94