| 1 |
#include <atlbase.h> |
| 2 |
#include "PortForwardWrapper.h" |
| 3 |
#include <Natupnp.h> |
| 4 |
#include <UPnP.h> |
| 5 |
#include <stdlib.h> |
| 6 |
#include "Daodan_Console.h" |
| 7 |
extern "C" int uPnP_Forward( PortMappingContainer_C* ptr ) |
| 8 |
{ |
| 9 |
bool bContinue = true; |
| 10 |
IUPnPNAT* piNAT = NULL; |
| 11 |
IStaticPortMappingCollection* piPortMappingCollection = NULL; |
| 12 |
CoInitialize(NULL); |
| 13 |
HRESULT CoResult; |
| 14 |
|
| 15 |
if ( !bContinue || !SUCCEEDED( CoResult = CoCreateInstance( |
| 16 |
__uuidof(UPnPNAT), |
| 17 |
NULL, |
| 18 |
CLSCTX_ALL, |
| 19 |
__uuidof(IUPnPNAT), |
| 20 |
(void **)&piNAT) |
| 21 |
) ) |
| 22 |
bContinue = FALSE; |
| 23 |
|
| 24 |
// Get the collection of forwarded ports |
| 25 |
|
| 26 |
if ( !bContinue || !SUCCEEDED( CoResult = piNAT->get_StaticPortMappingCollection(&piPortMappingCollection)) || (piPortMappingCollection==NULL ) ) |
| 27 |
bContinue = FALSE; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
// add the new mapping |
| 32 |
|
| 33 |
IStaticPortMapping* piStaticPortMapping = NULL; |
| 34 |
USES_CONVERSION; // for conversion from CString's |
| 35 |
|
| 36 |
//VARIANT_BOOL vb = ( ( newMapping.Enabled == _T("Yes") ) ? VARIANT_TRUE : VARIANT_FALSE ); |
| 37 |
BSTR Protocol = SysAllocString( CT2W(ptr->Protocol) ); |
| 38 |
BSTR InternalClient = SysAllocString( CT2W(ptr->InternalClient) ); |
| 39 |
BSTR Description = SysAllocString( CT2W(ptr->Description) ); |
| 40 |
|
| 41 |
//Remove the old binding, just in case. Probably not the best option, but it shall do for now. |
| 42 |
if ( !bContinue ) piPortMappingCollection->Remove( _ttol( ptr->ExternalPort), Protocol ); |
| 43 |
|
| 44 |
if ( !bContinue || |
| 45 |
!SUCCEEDED( CoResult = |
| 46 |
piPortMappingCollection->Add( |
| 47 |
_ttol( ptr->ExternalPort), |
| 48 |
Protocol, |
| 49 |
_ttol( ptr->InternalPort), |
| 50 |
InternalClient, |
| 51 |
-1, |
| 52 |
Description, |
| 53 |
&piStaticPortMapping ) ) || (piStaticPortMapping==NULL) ) |
| 54 |
|
| 55 |
bContinue = FALSE; |
| 56 |
|
| 57 |
SysFreeString(Protocol); |
| 58 |
SysFreeString(InternalClient); |
| 59 |
SysFreeString(Description); |
| 60 |
|
| 61 |
|
| 62 |
// clean up and de-initialize COM |
| 63 |
|
| 64 |
if ( piStaticPortMapping != NULL ) |
| 65 |
{ |
| 66 |
piStaticPortMapping->Release(); |
| 67 |
piStaticPortMapping = NULL; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
if ( piPortMappingCollection != NULL ) |
| 72 |
{ |
| 73 |
piPortMappingCollection->Release(); |
| 74 |
piPortMappingCollection = NULL; |
| 75 |
} |
| 76 |
|
| 77 |
if ( piNAT != NULL ) |
| 78 |
{ |
| 79 |
piNAT->Release(); |
| 80 |
piNAT = NULL; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
CoUninitialize(); |
| 85 |
if(bContinue) return 0; |
| 86 |
else return CoResult; |
| 87 |
} |