source: Daodan/MSVC/PortForwardWrapper.cpp@ 712

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

stuff

File size: 4.1 KB
RevLine 
[589]1#include <exception>
[588]2#include <atlbase.h>
3#include "PortForwardWrapper.h"
4#include <Natupnp.h>
5#include <UPnP.h>
6#include <stdlib.h>
7#include "Daodan_Console.h"
[589]8#include "Daodan_Utility.h"
9
10
11extern "C" int uPnP_Remove( PortMappingContainer_C* ptr )
12{
13 try
14 {
15 bool bContinue = true;
16 IUPnPNAT* piNAT = NULL;
17 IStaticPortMappingCollection* piPortMappingCollection = NULL;
18
19 return 0;
20 CoInitialize(NULL);
21 HRESULT CoResult;
22
23 if ( !bContinue || !SUCCEEDED( CoResult = CoCreateInstance(
24 __uuidof(UPnPNAT),
25 NULL,
26 CLSCTX_ALL,
27 __uuidof(IUPnPNAT),
28 (void **)&piNAT)
29 ) )
30 bContinue = FALSE;
31
32 // Get the collection of forwarded ports
33
34 if ( !bContinue || !SUCCEEDED( CoResult = piNAT->get_StaticPortMappingCollection(&piPortMappingCollection)) || (piPortMappingCollection==NULL ) )
35 bContinue = FALSE;
36
37
38
39 // add the new mapping
40
41 IStaticPortMapping* piStaticPortMapping = NULL;
42 USES_CONVERSION; // for conversion from CString's
43
44 //VARIANT_BOOL vb = ( ( newMapping.Enabled == _T("Yes") ) ? VARIANT_TRUE : VARIANT_FALSE );
45 BSTR Protocol = SysAllocString( CT2W(ptr->Protocol) );
46 BSTR InternalClient = SysAllocString( CT2W(ptr->InternalClient) );
47 BSTR Description = SysAllocString( CT2W(ptr->Description) );
48
49 //Remove the old binding, just in case. Probably not the best option, but it shall do for now.
50 if ( !bContinue ) piPortMappingCollection->Remove( _ttol( ptr->ExternalPort), Protocol );
51
52 // clean up and de-initialize COM
53
54 if ( piStaticPortMapping != NULL )
55 {
56 piStaticPortMapping->Release();
57 piStaticPortMapping = NULL;
58 }
59
60
61 if ( piPortMappingCollection != NULL )
62 {
63 piPortMappingCollection->Release();
64 piPortMappingCollection = NULL;
65 }
66
67 if ( piNAT != NULL )
68 {
69 piNAT->Release();
70 piNAT = NULL;
71 }
72
73
74 CoUninitialize();
75 if(bContinue) return 0;
76 else return CoResult;
77 }
78 catch(std::exception& ex)
79 {
80 DDrConsole_Print(ex.what());
81 return 0;
82 }
83}
84
85
[588]86extern "C" int uPnP_Forward( PortMappingContainer_C* ptr )
87{
[589]88 try
89 {
90 bool bContinue = true;
91 IUPnPNAT* piNAT = NULL;
92 IStaticPortMappingCollection* piPortMappingCollection = NULL;
[588]93
[589]94 return 0;
95 CoInitialize(NULL);
96 HRESULT CoResult;
[588]97
[589]98 if ( !bContinue || !SUCCEEDED( CoResult = CoCreateInstance(
99 __uuidof(UPnPNAT),
100 NULL,
101 CLSCTX_ALL,
102 __uuidof(IUPnPNAT),
103 (void **)&piNAT)
104 ) )
105 bContinue = FALSE;
[588]106
[589]107 // Get the collection of forwarded ports
[588]108
[589]109 if ( !bContinue || !SUCCEEDED( CoResult = piNAT->get_StaticPortMappingCollection(&piPortMappingCollection)) || (piPortMappingCollection==NULL ) )
110 bContinue = FALSE;
[588]111
112
[589]113
114 // add the new mapping
115
116 IStaticPortMapping* piStaticPortMapping = NULL;
117 USES_CONVERSION; // for conversion from CString's
118
119 //VARIANT_BOOL vb = ( ( newMapping.Enabled == _T("Yes") ) ? VARIANT_TRUE : VARIANT_FALSE );
120 BSTR Protocol = SysAllocString( CT2W(ptr->Protocol) );
121 BSTR InternalClient = SysAllocString( CT2W(ptr->InternalClient) );
122 BSTR Description = SysAllocString( CT2W(ptr->Description) );
123
124 //Remove the old binding, just in case. Probably not the best option, but it shall do for now.
125 if ( !bContinue ) piPortMappingCollection->Remove( _ttol( ptr->ExternalPort), Protocol );
126
127 if ( !bContinue ||
128 !SUCCEEDED( CoResult =
129 piPortMappingCollection->Add(
[588]130 _ttol( ptr->ExternalPort),
131 Protocol,
132 _ttol( ptr->InternalPort),
133 InternalClient,
134 -1,
135 Description,
[589]136 &piStaticPortMapping ) ) || (piStaticPortMapping==NULL) )
137
138 bContinue = FALSE;
139
140 SysFreeString(Protocol);
141 SysFreeString(InternalClient);
142 SysFreeString(Description);
143
144
145 // clean up and de-initialize COM
146
147 if ( piStaticPortMapping != NULL )
148 {
149 piStaticPortMapping->Release();
150 piStaticPortMapping = NULL;
151 }
152
153
154 if ( piPortMappingCollection != NULL )
155 {
156 piPortMappingCollection->Release();
157 piPortMappingCollection = NULL;
158 }
159
160 if ( piNAT != NULL )
161 {
162 piNAT->Release();
163 piNAT = NULL;
164 }
165
166
167 CoUninitialize();
168 if(bContinue) return 0;
169 else return CoResult;
[588]170 }
[589]171 catch(std::exception& ex)
[588]172 {
[589]173 DDrConsole_Print(ex.what());
174 return 0;
[588]175 }
176}
Note: See TracBrowser for help on using the repository browser.