/ Win32 / Win32NetState.h
Win32NetState.h
 1  /*
 2  * Copyright (c) 2013-2024, The PurpleI2P Project
 3  *
 4  * This file is part of Purple i2pd project and licensed under BSD3
 5  *
 6  * See full license text in LICENSE file at top of project tree
 7  */
 8  
 9  #ifndef WIN_32_NETSTATE_H__
10  #define WIN_32_NETSTATE_H__
11  
12  #if WINVER != 0x0501 // supported since Vista
13  #include <netlistmgr.h>
14  #include <ocidl.h>
15  #include "Log.h"
16  #include "Transports.h"
17  
18  class CNetworkListManagerEvent final : public INetworkListManagerEvents
19  {
20  public:
21  	CNetworkListManagerEvent() : m_ref(1) { }
22  	~CNetworkListManagerEvent() { }
23  
24  	HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void **ppvObject)
25  	{
26  		if (IsEqualIID(riid, IID_IUnknown)) {
27  			*ppvObject = (IUnknown *)this;
28  		} else if (IsEqualIID(riid ,IID_INetworkListManagerEvents)) {
29  			*ppvObject = (INetworkListManagerEvents *)this;
30  		} else {
31  			return E_NOINTERFACE;
32  		}
33  		AddRef();
34  		return S_OK;
35  	}
36  
37  	ULONG STDMETHODCALLTYPE AddRef()
38  	{
39  		return (ULONG)InterlockedIncrement(&m_ref);
40  	}
41  
42  	ULONG STDMETHODCALLTYPE Release()
43  	{
44  		LONG Result = InterlockedDecrement(&m_ref);
45  		if (Result == 0)
46  			delete this;
47  		return (ULONG)Result;
48  	}
49  
50  	virtual HRESULT STDMETHODCALLTYPE ConnectivityChanged(NLM_CONNECTIVITY newConnectivity)
51  	{
52  		if (newConnectivity == NLM_CONNECTIVITY_DISCONNECTED) {
53  			i2p::transport::transports.SetOnline (false);
54  			LogPrint(eLogInfo, "NetState: disconnected from network");
55  		}
56  
57  		if (((int)newConnectivity & (int)NLM_CONNECTIVITY_IPV4_INTERNET) != 0) {
58  			i2p::transport::transports.SetOnline (true);
59  			LogPrint(eLogInfo, "NetState: connected to internet with IPv4 capability");
60  		}
61  
62  		if (((int)newConnectivity & (int)NLM_CONNECTIVITY_IPV6_INTERNET) != 0) {
63  			i2p::transport::transports.SetOnline (true);
64  			LogPrint(eLogInfo, "NetState: connected to internet with IPv6 capability");
65  		}
66  
67  		if (
68  			(((int)newConnectivity & (int)NLM_CONNECTIVITY_IPV4_INTERNET) == 0) &&
69  			(((int)newConnectivity & (int)NLM_CONNECTIVITY_IPV6_INTERNET) == 0)
70  		) {
71  			i2p::transport::transports.SetOnline (false);
72  			LogPrint(eLogInfo, "NetState: connected without internet access");
73  		}
74  
75  		return S_OK;
76  	}
77  
78  private:
79  
80  	LONG m_ref;
81  };
82  
83  void SubscribeToEvents();
84  void UnSubscribeFromEvents();
85  
86  #else // WINVER == 0x0501
87  
88  void SubscribeToEvents() { }
89  void UnSubscribeFromEvents() { }
90  
91  #endif // WINVER
92  #endif