xref: /reactos/dll/win32/hnetcfg/port.c (revision c2c66aff)
1 /*
2  * Copyright 2009 Hans Leidekker for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18 
19 #include "hnetcfg_private.h"
20 
21 #include <ole2.h>
22 
23 typedef struct fw_port
24 {
25     INetFwOpenPort INetFwOpenPort_iface;
26     LONG refs;
27     BSTR name;
28     NET_FW_IP_PROTOCOL protocol;
29     LONG port;
30 } fw_port;
31 
32 static inline fw_port *impl_from_INetFwOpenPort( INetFwOpenPort *iface )
33 {
34     return CONTAINING_RECORD(iface, fw_port, INetFwOpenPort_iface);
35 }
36 
37 static ULONG WINAPI fw_port_AddRef(
38     INetFwOpenPort *iface )
39 {
40     fw_port *fw_port = impl_from_INetFwOpenPort( iface );
41     return InterlockedIncrement( &fw_port->refs );
42 }
43 
44 static ULONG WINAPI fw_port_Release(
45     INetFwOpenPort *iface )
46 {
47     fw_port *fw_port = impl_from_INetFwOpenPort( iface );
48     LONG refs = InterlockedDecrement( &fw_port->refs );
49     if (!refs)
50     {
51         TRACE("destroying %p\n", fw_port);
52         SysFreeString( fw_port->name );
53         HeapFree( GetProcessHeap(), 0, fw_port );
54     }
55     return refs;
56 }
57 
58 static HRESULT WINAPI fw_port_QueryInterface(
59     INetFwOpenPort *iface,
60     REFIID riid,
61     void **ppvObject )
62 {
63     fw_port *This = impl_from_INetFwOpenPort( iface );
64 
65     TRACE("%p %s %p\n", This, debugstr_guid( riid ), ppvObject );
66 
67     if ( IsEqualGUID( riid, &IID_INetFwOpenPort ) ||
68          IsEqualGUID( riid, &IID_IDispatch ) ||
69          IsEqualGUID( riid, &IID_IUnknown ) )
70     {
71         *ppvObject = iface;
72     }
73     else
74     {
75         FIXME("interface %s not implemented\n", debugstr_guid(riid));
76         return E_NOINTERFACE;
77     }
78     INetFwOpenPort_AddRef( iface );
79     return S_OK;
80 }
81 
82 static HRESULT WINAPI fw_port_GetTypeInfoCount(
83     INetFwOpenPort *iface,
84     UINT *pctinfo )
85 {
86     fw_port *This = impl_from_INetFwOpenPort( iface );
87 
88     TRACE("%p %p\n", This, pctinfo);
89     *pctinfo = 1;
90     return S_OK;
91 }
92 
93 static HRESULT WINAPI fw_port_GetTypeInfo(
94     INetFwOpenPort *iface,
95     UINT iTInfo,
96     LCID lcid,
97     ITypeInfo **ppTInfo )
98 {
99     fw_port *This = impl_from_INetFwOpenPort( iface );
100 
101     TRACE("%p %u %u %p\n", This, iTInfo, lcid, ppTInfo);
102     return get_typeinfo( INetFwOpenPort_tid, ppTInfo );
103 }
104 
105 static HRESULT WINAPI fw_port_GetIDsOfNames(
106     INetFwOpenPort *iface,
107     REFIID riid,
108     LPOLESTR *rgszNames,
109     UINT cNames,
110     LCID lcid,
111     DISPID *rgDispId )
112 {
113     fw_port *This = impl_from_INetFwOpenPort( iface );
114     ITypeInfo *typeinfo;
115     HRESULT hr;
116 
117     TRACE("%p %s %p %u %u %p\n", This, debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);
118 
119     hr = get_typeinfo( INetFwOpenPort_tid, &typeinfo );
120     if (SUCCEEDED(hr))
121     {
122         hr = ITypeInfo_GetIDsOfNames( typeinfo, rgszNames, cNames, rgDispId );
123         ITypeInfo_Release( typeinfo );
124     }
125     return hr;
126 }
127 
128 static HRESULT WINAPI fw_port_Invoke(
129     INetFwOpenPort *iface,
130     DISPID dispIdMember,
131     REFIID riid,
132     LCID lcid,
133     WORD wFlags,
134     DISPPARAMS *pDispParams,
135     VARIANT *pVarResult,
136     EXCEPINFO *pExcepInfo,
137     UINT *puArgErr )
138 {
139     fw_port *This = impl_from_INetFwOpenPort( iface );
140     ITypeInfo *typeinfo;
141     HRESULT hr;
142 
143     TRACE("%p %d %s %d %d %p %p %p %p\n", This, dispIdMember, debugstr_guid(riid),
144           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
145 
146     hr = get_typeinfo( INetFwOpenPort_tid, &typeinfo );
147     if (SUCCEEDED(hr))
148     {
149         hr = ITypeInfo_Invoke( typeinfo, &This->INetFwOpenPort_iface, dispIdMember,
150                                wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr );
151         ITypeInfo_Release( typeinfo );
152     }
153     return hr;
154 }
155 
156 static HRESULT WINAPI fw_port_get_Name(
157     INetFwOpenPort *iface,
158     BSTR *name)
159 {
160     fw_port *This = impl_from_INetFwOpenPort( iface );
161 
162     FIXME("%p %p\n", This, name);
163     return E_NOTIMPL;
164 }
165 
166 static HRESULT WINAPI fw_port_put_Name(
167     INetFwOpenPort *iface,
168     BSTR name)
169 {
170     fw_port *This = impl_from_INetFwOpenPort( iface );
171 
172     TRACE("%p %s\n", This, debugstr_w(name));
173 
174     if (!(name = SysAllocString( name )))
175         return E_OUTOFMEMORY;
176 
177     SysFreeString( This->name );
178     This->name = name;
179     return S_OK;
180 }
181 
182 static HRESULT WINAPI fw_port_get_IpVersion(
183     INetFwOpenPort *iface,
184     NET_FW_IP_VERSION *ipVersion)
185 {
186     fw_port *This = impl_from_INetFwOpenPort( iface );
187 
188     FIXME("%p %p\n", This, ipVersion);
189     return E_NOTIMPL;
190 }
191 
192 static HRESULT WINAPI fw_port_put_IpVersion(
193     INetFwOpenPort *iface,
194     NET_FW_IP_VERSION ipVersion)
195 {
196     fw_port *This = impl_from_INetFwOpenPort( iface );
197 
198     FIXME("%p %u\n", This, ipVersion);
199     return E_NOTIMPL;
200 }
201 
202 static HRESULT WINAPI fw_port_get_Protocol(
203     INetFwOpenPort *iface,
204     NET_FW_IP_PROTOCOL *ipProtocol)
205 {
206     fw_port *This = impl_from_INetFwOpenPort( iface );
207 
208     FIXME("%p %p\n", This, ipProtocol);
209     return E_NOTIMPL;
210 }
211 
212 static HRESULT WINAPI fw_port_put_Protocol(
213     INetFwOpenPort *iface,
214     NET_FW_IP_PROTOCOL ipProtocol)
215 {
216     fw_port *This = impl_from_INetFwOpenPort( iface );
217 
218     TRACE("%p %u\n", This, ipProtocol);
219 
220     if (ipProtocol != NET_FW_IP_PROTOCOL_TCP && ipProtocol != NET_FW_IP_PROTOCOL_UDP)
221         return E_INVALIDARG;
222 
223     This->protocol = ipProtocol;
224     return S_OK;
225 }
226 
227 static HRESULT WINAPI fw_port_get_Port(
228     INetFwOpenPort *iface,
229     LONG *portNumber)
230 {
231     fw_port *This = impl_from_INetFwOpenPort( iface );
232 
233     FIXME("%p %p\n", This, portNumber);
234     return E_NOTIMPL;
235 }
236 
237 static HRESULT WINAPI fw_port_put_Port(
238     INetFwOpenPort *iface,
239     LONG portNumber)
240 {
241     fw_port *This = impl_from_INetFwOpenPort( iface );
242 
243     TRACE("%p %d\n", This, portNumber);
244     This->port = portNumber;
245     return S_OK;
246 }
247 
248 static HRESULT WINAPI fw_port_get_Scope(
249     INetFwOpenPort *iface,
250     NET_FW_SCOPE *scope)
251 {
252     fw_port *This = impl_from_INetFwOpenPort( iface );
253 
254     FIXME("%p %p\n", This, scope);
255     return E_NOTIMPL;
256 }
257 
258 static HRESULT WINAPI fw_port_put_Scope(
259     INetFwOpenPort *iface,
260     NET_FW_SCOPE scope)
261 {
262     fw_port *This = impl_from_INetFwOpenPort( iface );
263 
264     FIXME("%p %u\n", This, scope);
265     return E_NOTIMPL;
266 }
267 
268 static HRESULT WINAPI fw_port_get_RemoteAddresses(
269     INetFwOpenPort *iface,
270     BSTR *remoteAddrs)
271 {
272     fw_port *This = impl_from_INetFwOpenPort( iface );
273 
274     FIXME("%p %p\n", This, remoteAddrs);
275     return E_NOTIMPL;
276 }
277 
278 static HRESULT WINAPI fw_port_put_RemoteAddresses(
279     INetFwOpenPort *iface,
280     BSTR remoteAddrs)
281 {
282     fw_port *This = impl_from_INetFwOpenPort( iface );
283 
284     FIXME("%p %s\n", This, debugstr_w(remoteAddrs));
285     return E_NOTIMPL;
286 }
287 
288 static HRESULT WINAPI fw_port_get_Enabled(
289     INetFwOpenPort *iface,
290     VARIANT_BOOL *enabled)
291 {
292     fw_port *This = impl_from_INetFwOpenPort( iface );
293 
294     FIXME("%p %p\n", This, enabled);
295 
296     *enabled = VARIANT_TRUE;
297     return S_OK;
298 }
299 
300 static HRESULT WINAPI fw_port_put_Enabled(
301     INetFwOpenPort *iface,
302     VARIANT_BOOL enabled)
303 {
304     fw_port *This = impl_from_INetFwOpenPort( iface );
305 
306     FIXME("%p %d\n", This, enabled);
307     return S_OK;
308 }
309 
310 static HRESULT WINAPI fw_port_get_BuiltIn(
311     INetFwOpenPort *iface,
312     VARIANT_BOOL *builtIn)
313 {
314     fw_port *This = impl_from_INetFwOpenPort( iface );
315 
316     FIXME("%p %p\n", This, builtIn);
317     return E_NOTIMPL;
318 }
319 
320 static const struct INetFwOpenPortVtbl fw_port_vtbl =
321 {
322     fw_port_QueryInterface,
323     fw_port_AddRef,
324     fw_port_Release,
325     fw_port_GetTypeInfoCount,
326     fw_port_GetTypeInfo,
327     fw_port_GetIDsOfNames,
328     fw_port_Invoke,
329     fw_port_get_Name,
330     fw_port_put_Name,
331     fw_port_get_IpVersion,
332     fw_port_put_IpVersion,
333     fw_port_get_Protocol,
334     fw_port_put_Protocol,
335     fw_port_get_Port,
336     fw_port_put_Port,
337     fw_port_get_Scope,
338     fw_port_put_Scope,
339     fw_port_get_RemoteAddresses,
340     fw_port_put_RemoteAddresses,
341     fw_port_get_Enabled,
342     fw_port_put_Enabled,
343     fw_port_get_BuiltIn
344 };
345 
346 HRESULT NetFwOpenPort_create( IUnknown *pUnkOuter, LPVOID *ppObj )
347 {
348     fw_port *fp;
349 
350     TRACE("(%p,%p)\n", pUnkOuter, ppObj);
351 
352     fp = HeapAlloc( GetProcessHeap(), 0, sizeof(*fp) );
353     if (!fp) return E_OUTOFMEMORY;
354 
355     fp->INetFwOpenPort_iface.lpVtbl = &fw_port_vtbl;
356     fp->refs = 1;
357     fp->name = NULL;
358     fp->protocol = NET_FW_IP_PROTOCOL_TCP;
359     fp->port = 0;
360 
361     *ppObj = &fp->INetFwOpenPort_iface;
362 
363     TRACE("returning iface %p\n", *ppObj);
364     return S_OK;
365 }
366 
367 typedef struct fw_ports
368 {
369     INetFwOpenPorts INetFwOpenPorts_iface;
370     LONG refs;
371 } fw_ports;
372 
373 static inline fw_ports *impl_from_INetFwOpenPorts( INetFwOpenPorts *iface )
374 {
375     return CONTAINING_RECORD(iface, fw_ports, INetFwOpenPorts_iface);
376 }
377 
378 static ULONG WINAPI fw_ports_AddRef(
379     INetFwOpenPorts *iface )
380 {
381     fw_ports *fw_ports = impl_from_INetFwOpenPorts( iface );
382     return InterlockedIncrement( &fw_ports->refs );
383 }
384 
385 static ULONG WINAPI fw_ports_Release(
386     INetFwOpenPorts *iface )
387 {
388     fw_ports *fw_ports = impl_from_INetFwOpenPorts( iface );
389     LONG refs = InterlockedDecrement( &fw_ports->refs );
390     if (!refs)
391     {
392         TRACE("destroying %p\n", fw_ports);
393         HeapFree( GetProcessHeap(), 0, fw_ports );
394     }
395     return refs;
396 }
397 
398 static HRESULT WINAPI fw_ports_QueryInterface(
399     INetFwOpenPorts *iface,
400     REFIID riid,
401     void **ppvObject )
402 {
403     fw_ports *This = impl_from_INetFwOpenPorts( iface );
404 
405     TRACE("%p %s %p\n", This, debugstr_guid( riid ), ppvObject );
406 
407     if ( IsEqualGUID( riid, &IID_INetFwOpenPorts ) ||
408          IsEqualGUID( riid, &IID_IDispatch ) ||
409          IsEqualGUID( riid, &IID_IUnknown ) )
410     {
411         *ppvObject = iface;
412     }
413     else
414     {
415         FIXME("interface %s not implemented\n", debugstr_guid(riid));
416         return E_NOINTERFACE;
417     }
418     INetFwOpenPorts_AddRef( iface );
419     return S_OK;
420 }
421 
422 static HRESULT WINAPI fw_ports_GetTypeInfoCount(
423     INetFwOpenPorts *iface,
424     UINT *pctinfo )
425 {
426     fw_ports *This = impl_from_INetFwOpenPorts( iface );
427 
428     TRACE("%p %p\n", This, pctinfo);
429     *pctinfo = 1;
430     return S_OK;
431 }
432 
433 static HRESULT WINAPI fw_ports_GetTypeInfo(
434     INetFwOpenPorts *iface,
435     UINT iTInfo,
436     LCID lcid,
437     ITypeInfo **ppTInfo )
438 {
439     fw_ports *This = impl_from_INetFwOpenPorts( iface );
440 
441     TRACE("%p %u %u %p\n", This, iTInfo, lcid, ppTInfo);
442     return get_typeinfo( INetFwOpenPorts_tid, ppTInfo );
443 }
444 
445 static HRESULT WINAPI fw_ports_GetIDsOfNames(
446     INetFwOpenPorts *iface,
447     REFIID riid,
448     LPOLESTR *rgszNames,
449     UINT cNames,
450     LCID lcid,
451     DISPID *rgDispId )
452 {
453     fw_ports *This = impl_from_INetFwOpenPorts( iface );
454     ITypeInfo *typeinfo;
455     HRESULT hr;
456 
457     TRACE("%p %s %p %u %u %p\n", This, debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);
458 
459     hr = get_typeinfo( INetFwOpenPorts_tid, &typeinfo );
460     if (SUCCEEDED(hr))
461     {
462         hr = ITypeInfo_GetIDsOfNames( typeinfo, rgszNames, cNames, rgDispId );
463         ITypeInfo_Release( typeinfo );
464     }
465     return hr;
466 }
467 
468 static HRESULT WINAPI fw_ports_Invoke(
469     INetFwOpenPorts *iface,
470     DISPID dispIdMember,
471     REFIID riid,
472     LCID lcid,
473     WORD wFlags,
474     DISPPARAMS *pDispParams,
475     VARIANT *pVarResult,
476     EXCEPINFO *pExcepInfo,
477     UINT *puArgErr )
478 {
479     fw_ports *This = impl_from_INetFwOpenPorts( iface );
480     ITypeInfo *typeinfo;
481     HRESULT hr;
482 
483     TRACE("%p %d %s %d %d %p %p %p %p\n", This, dispIdMember, debugstr_guid(riid),
484           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
485 
486     hr = get_typeinfo( INetFwOpenPorts_tid, &typeinfo );
487     if (SUCCEEDED(hr))
488     {
489         hr = ITypeInfo_Invoke( typeinfo, &This->INetFwOpenPorts_iface, dispIdMember,
490                                wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr );
491         ITypeInfo_Release( typeinfo );
492     }
493     return hr;
494 }
495 
496 static HRESULT WINAPI fw_ports_get_Count(
497     INetFwOpenPorts *iface,
498     LONG *count)
499 {
500     fw_ports *This = impl_from_INetFwOpenPorts( iface );
501 
502     FIXME("%p, %p\n", This, count);
503 
504     *count = 0;
505     return S_OK;
506 }
507 
508 static HRESULT WINAPI fw_ports_Add(
509     INetFwOpenPorts *iface,
510     INetFwOpenPort *port)
511 {
512     fw_ports *This = impl_from_INetFwOpenPorts( iface );
513 
514     FIXME("%p, %p\n", This, port);
515     return S_OK;
516 }
517 
518 static HRESULT WINAPI fw_ports_Remove(
519     INetFwOpenPorts *iface,
520     LONG portNumber,
521     NET_FW_IP_PROTOCOL ipProtocol)
522 {
523     fw_ports *This = impl_from_INetFwOpenPorts( iface );
524 
525     FIXME("%p, %d, %u\n", This, portNumber, ipProtocol);
526     return E_NOTIMPL;
527 }
528 
529 static HRESULT WINAPI fw_ports_Item(
530     INetFwOpenPorts *iface,
531     LONG portNumber,
532     NET_FW_IP_PROTOCOL ipProtocol,
533     INetFwOpenPort **openPort)
534 {
535     HRESULT hr;
536     fw_ports *This = impl_from_INetFwOpenPorts( iface );
537 
538     FIXME("%p, %d, %u, %p\n", This, portNumber, ipProtocol, openPort);
539 
540     hr = NetFwOpenPort_create( NULL, (void **)openPort );
541     if (SUCCEEDED(hr))
542     {
543         INetFwOpenPort_put_Protocol( *openPort, ipProtocol );
544         INetFwOpenPort_put_Port( *openPort, portNumber );
545     }
546     return hr;
547 }
548 
549 static HRESULT WINAPI fw_ports_get__NewEnum(
550     INetFwOpenPorts *iface,
551     IUnknown **newEnum)
552 {
553     fw_ports *This = impl_from_INetFwOpenPorts( iface );
554 
555     FIXME("%p, %p\n", This, newEnum);
556     return E_NOTIMPL;
557 }
558 
559 static const struct INetFwOpenPortsVtbl fw_ports_vtbl =
560 {
561     fw_ports_QueryInterface,
562     fw_ports_AddRef,
563     fw_ports_Release,
564     fw_ports_GetTypeInfoCount,
565     fw_ports_GetTypeInfo,
566     fw_ports_GetIDsOfNames,
567     fw_ports_Invoke,
568     fw_ports_get_Count,
569     fw_ports_Add,
570     fw_ports_Remove,
571     fw_ports_Item,
572     fw_ports_get__NewEnum
573 };
574 
575 HRESULT NetFwOpenPorts_create( IUnknown *pUnkOuter, LPVOID *ppObj )
576 {
577     fw_ports *fp;
578 
579     TRACE("(%p,%p)\n", pUnkOuter, ppObj);
580 
581     fp = HeapAlloc( GetProcessHeap(), 0, sizeof(*fp) );
582     if (!fp) return E_OUTOFMEMORY;
583 
584     fp->INetFwOpenPorts_iface.lpVtbl = &fw_ports_vtbl;
585     fp->refs = 1;
586 
587     *ppObj = &fp->INetFwOpenPorts_iface;
588 
589     TRACE("returning iface %p\n", *ppObj);
590     return S_OK;
591 }
592