source: Daodan/MSVC/PortForwardWrapper.cpp@ 734

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

stuff

File size: 4.1 KB
Line 
1#include <exception>
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"
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
86extern "C" int uPnP_Forward( PortMappingContainer_C* ptr )
87{
88 try
89 {
90 bool bContinue = true;
91 IUPnPNAT* piNAT = NULL;
92 IStaticPortMappingCollection* piPortMappingCollection = NULL;
93
94 return 0;
95 CoInitialize(NULL);
96 HRESULT CoResult;
97
98 if ( !bContinue || !SUCCEEDED( CoResult = CoCreateInstance(
99 __uuidof(UPnPNAT),
100 NULL,
101 CLSCTX_ALL,
102 __uuidof(IUPnPNAT),
103 (void **)&piNAT)
104 ) )
105 bContinue = FALSE;
106
107 // Get the collection of forwarded ports
108
109 if ( !bContinue || !SUCCEEDED( CoResult = piNAT->get_StaticPortMappingCollection(&piPortMappingCollection)) || (piPortMappingCollection==NULL ) )
110 bContinue = FALSE;
111
112
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(
130 _ttol( ptr->ExternalPort),
131 Protocol,
132 _ttol( ptr->InternalPort),
133 InternalClient,
134 -1,
135 Description,
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;
170 }
171 catch(std::exception& ex)
172 {
173 DDrConsole_Print(ex.what());
174 return 0;
175 }
176}
Note: See TracBrowser for help on using the repository browser.