source: Daodan/MSVC/PortForwardEngine.hpp@ 869

Last change on this file since 869 was 589, checked in by gumby, 13 years ago

stuff

File size: 9.9 KB
Line 
1// PortForwardEngine.h: interface for the CPortForwardEngine class.
2//
3//////////////////////////////////////////////////////////////////////
4
5#if !defined(AFX_PORTFORWARDENGINE_H__003E19B2_EC21_4097_8A62_D28641F61CC8__INCLUDED_)
6#define AFX_PORTFORWARDENGINE_H__003E19B2_EC21_4097_8A62_D28641F61CC8__INCLUDED_
7
8#if _MSC_VER > 1000
9#pragma once
10#endif // _MSC_VER > 1000
11#include <afx.h>
12#include <Natupnp.h>
13#include <UPnP.h>
14
15#include <vector>
16
17using namespace std;
18
19class CPortForwardChangeCallbacks
20{
21public:
22 CPortForwardChangeCallbacks();
23 virtual ~CPortForwardChangeCallbacks();
24
25 virtual HRESULT OnNewNumberOfEntries( long lNewNumberOfEntries );
26 virtual HRESULT OnNewExternalIPAddress( BSTR bstrNewExternalIPAddress );
27
28};
29
30
31
32class CPortForwardEngine
33{
34public:
35
36 // forward declarations
37
38protected:
39 interface IDerivedNATExternalIPAddressCallback;
40 interface IDerivedNATNumberOfEntriesCallback;
41
42public:
43 class PortMappingContainer;
44 class DeviceInformationContainer;
45
46
47public:
48
49 // public functions -- there are only a few
50
51 CPortForwardEngine();
52 virtual ~CPortForwardEngine();
53
54 HRESULT ListenForUpnpChanges(CPortForwardChangeCallbacks *pCallbacks = NULL); // NULL==default object; if you provide your own pointer to a CPortForwardChangeCallbacks-derived object it is deleted for you automatically
55 HRESULT StopListeningForUpnpChanges( ); // Stops listenting for UPnP change events on the router and deletes any CPortForwardChangeCallbacks-derived objects
56
57 BOOL GetDeviceInformationUsingThread( HWND hWnd ); // starts a thread that will get IGD (router) device information; the thread posts a UWM_PORT_FORWARD_ENGINE_THREAD_NOTIFICATION message to hWnd when it's done
58 BOOL GetMappingsUsingThread( HWND hWnd ); // starts a thread that will get all mappings; the thread posts a UWM_PORT_FORWARD_ENGINE_THREAD_NOTIFICATION message to hWnd when it's done
59 BOOL EditMappingUsingThread( PortMappingContainer& oldMapping, PortMappingContainer& newMapping, HWND hWnd ); // starts a thread that will edit one specific mapping; the thread posts a UWM_PORT_FORWARD_ENGINE_THREAD_NOTIFICATION message to hWnd when it's done
60 BOOL AddMappingUsingThread( PortMappingContainer& newMapping, HWND hWnd ); // starts a thread that will add one new mapping; the thread posts a UWM_PORT_FORWARD_ENGINE_THREAD_NOTIFICATION message to hWnd when it's done
61 BOOL DeleteMappingUsingThread( PortMappingContainer& oldMapping, HWND hWnd ); // starts a thread that will delete one specific mapping; the thread posts a UWM_PORT_FORWARD_ENGINE_THREAD_NOTIFICATION message to hWnd when it's done
62
63 std::vector<PortMappingContainer> GetPortMappingVector() const; // gets a copy of currently-known port mappings
64 DeviceInformationContainer GetDeviceInformationContainer() const; // gets a copy of currently-know device information
65
66 BOOL IsAnyThreadRunning() const; // returns TRUE if there is any thread currently running
67
68
69protected:
70
71 // protected functions used internally by PortMappingEngine
72
73 void InitializeMembersToNull();
74 void DeinitializeCom();
75 HRESULT PopulateDeviceInfoContainer( IUPnPDevice* piDevice, DeviceInformationContainer& deviceInfo, HWND hWnd=NULL );
76 HRESULT GetNextMapping( IEnumVARIANT* piEnumerator, PortMappingContainer& mappingContainer );
77 HRESULT SetChangeEventCallbackPointer(CPortForwardChangeCallbacks *pCallbacks);
78
79 static UINT ThreadForPortRetrieval( LPVOID pVoid );
80 static UINT ThreadForDeviceInformationRetrieval( LPVOID pVoid );
81 static UINT ThreadToEditMapping( LPVOID pVoid );
82 static UINT ThreadToAddMapping( LPVOID pVoid );
83 static UINT ThreadToDeleteMapping( LPVOID pVoid );
84
85
86
87protected:
88
89 // protected members
90
91 IUPnPNAT* m_piNAT;
92 IDerivedNATExternalIPAddressCallback* m_piExternalIPAddressCallback;
93 IDerivedNATNumberOfEntriesCallback* m_piNumberOfEntriesCallback;
94
95 INATEventManager* m_piEventManager;
96 CPortForwardChangeCallbacks* m_pChangeCallbackFunctions;
97
98 BOOL m_bListeningForUpnpChanges;
99
100 CWinThread* m_pPortMappingThread;
101 CWinThread* m_pDeviceInfoThread;
102 CWinThread* m_pAddMappingThread;
103 CWinThread* m_pEditMappingThread;
104 CWinThread* m_pDeleteMappingThread;
105
106 std::vector<PortMappingContainer> m_MappingContainer;
107
108 CRITICAL_SECTION m_cs;
109
110
111protected:
112
113 // protected interfaces, which were forward-declared above, and which are used for event notifications from COM
114 // most of the code is here in this .h file, except for the QueryInterface method which is in the .cpp file
115
116 interface IDerivedNATExternalIPAddressCallback : public INATExternalIPAddressCallback
117 {
118 IDerivedNATExternalIPAddressCallback( CPortForwardChangeCallbacks* p ) : m_pointer( p ), m_dwRef( 0 ) { };
119
120 HRESULT STDMETHODCALLTYPE NewExternalIPAddress( BSTR bstrNewExternalIPAddress )
121 {
122 ASSERT( m_pointer != NULL );
123 return m_pointer->OnNewExternalIPAddress( bstrNewExternalIPAddress );
124 }
125
126 HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void ** ppvObject);
127
128 ULONG STDMETHODCALLTYPE AddRef() { return ++m_dwRef; }
129
130 ULONG STDMETHODCALLTYPE Release()
131 {
132 if ( --m_dwRef == 0 )
133 delete this;
134
135 return m_dwRef;
136 }
137
138 DWORD m_dwRef;
139 CPortForwardChangeCallbacks* m_pointer;
140 };
141
142 interface IDerivedNATNumberOfEntriesCallback : public INATNumberOfEntriesCallback
143 {
144 IDerivedNATNumberOfEntriesCallback( CPortForwardChangeCallbacks* p ) : m_pointer( p ), m_dwRef( 0 ) { };
145
146 HRESULT STDMETHODCALLTYPE NewNumberOfEntries( long lNewNumberOfEntries )
147 {
148 ASSERT( m_pointer != NULL );
149 return m_pointer->OnNewNumberOfEntries( lNewNumberOfEntries );
150 }
151
152 HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void ** ppvObject);
153
154 ULONG STDMETHODCALLTYPE AddRef() { return ++m_dwRef; }
155
156 ULONG STDMETHODCALLTYPE Release()
157 {
158 if ( --m_dwRef == 0 )
159 delete this;
160
161 return m_dwRef;
162 }
163
164 DWORD m_dwRef;
165 CPortForwardChangeCallbacks* m_pointer;
166 };
167
168
169public:
170
171 // public nested classes, which were forward-declared above
172 // these are public because they are needed by classes that call the
173 // GetPortMappingVector() and GetDeviceInformationContainer() methods
174
175 class PortMappingContainer
176 {
177 public:
178 PortMappingContainer() : ExternalIPAddress(_T("")), ExternalPort(_T("")),
179 InternalPort(_T("")), Protocol(_T("")), InternalClient(_T("")),
180 Enabled(_T("")), Description(_T("")) { };
181 CString ExternalIPAddress;
182 CString ExternalPort;
183 CString InternalPort;
184 CString Protocol;
185 CString InternalClient;
186 CString Enabled;
187 CString Description;
188 };
189
190 class DeviceInformationContainer
191 {
192 public:
193 DeviceInformationContainer() : Children(_T("")), Description(_T("")),
194 FriendlyName(_T("")), HasChildren(_T("")), IconURL(_T("")), IsRootDevice(_T("")),
195 ManufacturerName(_T("")), ManufacturerURL(_T("")), ModelName(_T("")),
196 ModelNumber(_T("")), ModelURL(_T("")), ParentDevice(_T("")),
197 PresentationURL(_T("")), RootDevice(_T("")), SerialNumber(_T("")),
198 Services(_T("")), Type(_T("")), UniqueDeviceName(_T("")), UPC(_T("")) { };
199
200 // see http://msdn.microsoft.com/library/en-us/upnp/upnp/iupnpdevice.asp
201
202 CString Children; // Child devices of the device.
203 CString Description; // Human-readable form of the summary of a device's functionality.
204 CString FriendlyName; // Device display name.
205 CString HasChildren; // Indicates whether the device has any child devices.
206 CString IconURL; // URL of icon
207 CString IsRootDevice; // Indicates whether the device is the top-most device in the device tree.
208 CString ManufacturerName; // Human-readable form of the manufacturer name.
209 CString ManufacturerURL; // URL for the manufacturer's Web site.
210 CString ModelName; // Human-readable form of the model name.
211 CString ModelNumber; // Human-readable form of the model number.
212 CString ModelURL; // URL for a Web page that contains model-specific information.
213 CString ParentDevice; // Parent of the device.
214 CString PresentationURL; // Presentation URL for a Web page that can be used to control the device.
215 CString RootDevice; // Top-most device in the device tree.
216 CString SerialNumber; // Human-readable form of the serial number.
217 CString Services; // List of services provided by the device.
218 CString Type; // Uniform resource identifier (URI) for the device type.
219 CString UniqueDeviceName; // Unique device name (UDN) of the device.
220 CString UPC; // Human-readable form of the product code.
221 };
222
223
224
225protected:
226
227 // some more protected members, most of which could not be declared until after full-declaration
228 // of the DeviceInformationContainer and PortMappingContainer nested classes
229
230 DeviceInformationContainer m_DeviceInfo;
231
232 // good-enough method for inter-thread transfer of information, since only
233 // one thread of each type is ever permitted at one time
234
235 PortMappingContainer m_scratchpadOldMapping;
236 PortMappingContainer m_scratchpadNewMapping;
237 PortMappingContainer m_scratchpadAddedMapping;
238 PortMappingContainer m_scratchpadDeletedMapping;
239
240 HWND m_hWndForPortMappingThread;
241 HWND m_hWndForDeviceInfoThread;
242 HWND m_hWndForAddMappingThread;
243 HWND m_hWndForEditMappingThread;
244 HWND m_hWndForDeleteMappingThread;
245
246
247public:
248
249 // a public enum which is used by classes that respond to the registered window message
250 // UWM_PORT_FORWARD_ENGINE_THREAD_NOTIFICATION, so they can decode the wParam and lParam values
251
252 enum THREAD_STATUS {
253 EnumPortRetrieveInterval = 0x0001,
254 EnumPortRetrieveDone = 0x0002,
255 EnumDeviceInfoInterval = 0x0011,
256 EnumDeviceInfoDone = 0x0012,
257 EnumAddMappingInterval = 0x0021,
258 EnumAddMappingDone = 0x0022,
259 EnumEditMappingInterval = 0x0041,
260 EnumEditMappingDone = 0x0042,
261 EnumDeleteMappingInterval = 0x0081,
262 EnumDeleteMappingDone = 0x0082
263 };
264
265};
266
267
268
269#endif // !defined(AFX_PORTFORWARDENGINE_H__003E19B2_EC21_4097_8A62_D28641F61CC8__INCLUDED_)
Note: See TracBrowser for help on using the repository browser.