ViewVC Help
View File | Revision Log | View Changeset | Root Listing
root/Oni2/Daodan/MSVC/PortForwardWrapper.cpp
(Generate patch)

Comparing Daodan/MSVC/PortForwardWrapper.cpp (file contents):
Revision 588 by gumby, Fri Feb 11 08:21:14 2011 UTC vs.
Revision 589 by gumby, Sat Aug 20 09:47:49 2011 UTC

# Line 1 | Line 1
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 +
11 + extern "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 +
86   extern "C" int uPnP_Forward( PortMappingContainer_C* ptr )
87   {
88 <        bool bContinue = true;
89 <        IUPnPNAT* piNAT = NULL;
90 <        IStaticPortMappingCollection* piPortMappingCollection = NULL;  
91 <        CoInitialize(NULL);
92 <        HRESULT CoResult;
93 <
94 <        if ( !bContinue || !SUCCEEDED( CoResult  = CoCreateInstance(
95 <                __uuidof(UPnPNAT),
96 <                NULL,
97 <                CLSCTX_ALL,
98 <                __uuidof(IUPnPNAT),
99 <                (void **)&piNAT)
100 <                ) )
101 <                bContinue = FALSE;
102 <
103 <        // Get the collection of forwarded ports
104 <
105 <        if ( !bContinue || !SUCCEEDED( CoResult = piNAT->get_StaticPortMappingCollection(&piPortMappingCollection)) || (piPortMappingCollection==NULL ) )
106 <                bContinue = FALSE;
107 <
108 <        
109 <        
110 <        // add the new mapping
111 <
112 <        IStaticPortMapping* piStaticPortMapping = NULL;
113 <        USES_CONVERSION;  // for conversion from CString's
114 <
115 <        //VARIANT_BOOL vb = ( ( newMapping.Enabled == _T("Yes") ) ? VARIANT_TRUE : VARIANT_FALSE );
116 <        BSTR Protocol = SysAllocString( CT2W(ptr->Protocol) );
117 <        BSTR InternalClient = SysAllocString( CT2W(ptr->InternalClient) );
118 <        BSTR Description = SysAllocString( CT2W(ptr->Description) );
119 <        
120 <        //Remove the old binding, just in case. Probably not the best option, but it shall do for now.
121 <        if ( !bContinue ) piPortMappingCollection->Remove( _ttol( ptr->ExternalPort), Protocol );
122 <        
123 <        if ( !bContinue ||
124 <                !SUCCEEDED( CoResult =
125 <                piPortMappingCollection->Add(
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;
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 <        
77 <        if ( piNAT != NULL )
171 >        catch(std::exception& ex)
172          {
173 <                piNAT->Release();
174 <                piNAT = NULL;
173 >                DDrConsole_Print(ex.what());
174 >                return 0;
175          }
82        
83        
84        CoUninitialize();
85        if(bContinue) return 0;
86        else return CoResult;
176   }

Diff Legend

Removed lines
+ Added lines
< Changed lines (old)
> Changed lines (new)