1/* -*- Mode: C; tab-width: 4 -*-
2 *
3 * Copyright (c) 2009 Apple Computer, Inc. All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
19
20#pragma once
21
22#include "resource.h"       // main symbols
23
24
25
26#include "DLLX.h"
27
28#include "DNSSDEventManager.h"
29
30#include <CommonServices.h>
31
32#include <DebugServices.h>
33
34#include <dns_sd.h>
35
36#include <map>
37
38
39
40
41
42#if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
43
44#error "Single-threaded COM objects are not properly supported on Windows CE platform, such as the Windows Mobile platforms that do not include full DCOM support. Define _CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA to force ATL to support creating single-thread COM object's and allow use of it's single-threaded COM object implementations. The threading model in your rgs file was set to 'Free' as that is the only threading model supported in non DCOM Windows CE platforms."
45
46#endif
47
48
49
50
51
52
53
54// CDNSSDService
55
56
57
58class ATL_NO_VTABLE CDNSSDService :
59
60	public CComObjectRootEx<CComSingleThreadModel>,
61
62	public CComCoClass<CDNSSDService, &CLSID_DNSSDService>,
63
64	public IDispatchImpl<IDNSSDService, &IID_IDNSSDService, &LIBID_Bonjour, /*wMajor =*/ 1, /*wMinor =*/ 0>
65
66{
67
68public:
69
70
71
72	typedef CComObjectRootEx<CComSingleThreadModel> Super;
73
74
75
76	CDNSSDService()
77
78	:
79
80		m_isPrimary( FALSE ),
81
82		m_eventManager( NULL ),
83
84		m_stopped( FALSE ),
85
86		m_primary( NULL ),
87
88		m_subord( NULL )
89
90	{
91
92	}
93
94
95
96DECLARE_REGISTRY_RESOURCEID(IDR_DNSSDSERVICE)
97
98
99
100
101
102BEGIN_COM_MAP(CDNSSDService)
103
104	COM_INTERFACE_ENTRY(IDNSSDService)
105
106	COM_INTERFACE_ENTRY(IDispatch)
107
108END_COM_MAP()
109
110
111
112	DECLARE_PROTECT_FINAL_CONSTRUCT()
113
114
115
116	HRESULT
117
118	FinalConstruct();
119
120
121
122	void
123
124	FinalRelease();
125
126
127
128public:
129
130
131
132	inline DNSServiceRef
133
134	GetPrimaryRef()
135
136	{
137
138		return m_primary;
139
140	}
141
142
143
144	inline void
145
146	SetPrimaryRef( DNSServiceRef primary )
147
148	{
149
150		m_primary = primary;
151
152	}
153
154
155
156	inline DNSServiceRef
157
158	GetSubordRef()
159
160	{
161
162		return m_subord;
163
164	}
165
166
167
168	inline void
169
170	SetSubordRef( DNSServiceRef subord )
171
172	{
173
174		m_subord = subord;
175
176	}
177
178
179
180	inline CDNSSDEventManager*
181
182	GetEventManager()
183
184	{
185
186		return m_eventManager;
187
188	}
189
190
191
192	inline void
193
194	SetEventManager( IDNSSDEventManager * eventManager )
195
196	{
197
198		if ( m_eventManager )
199
200		{
201
202			m_eventManager->Release();
203
204			m_eventManager = NULL;
205
206		}
207
208
209
210		if ( eventManager )
211
212		{
213
214			m_eventManager = dynamic_cast< CDNSSDEventManager* >( eventManager );
215
216			check( m_eventManager );
217
218			m_eventManager->AddRef();
219
220		}
221
222	}
223
224
225
226	inline BOOL
227
228	Stopped()
229
230	{
231
232		return m_stopped;
233
234	}
235
236
237
238private:
239
240
241
242	static void DNSSD_API
243	DomainEnumReply
244		(
245		DNSServiceRef                       sdRef,
246		DNSServiceFlags                     flags,
247		uint32_t                            ifIndex,
248		DNSServiceErrorType                 errorCode,
249		const char                          *replyDomain,
250		void                                *context
251		);
252
253
254
255	static void DNSSD_API
256	BrowseReply
257		(
258		DNSServiceRef                       sdRef,
259		DNSServiceFlags                     flags,
260		uint32_t                            interfaceIndex,
261		DNSServiceErrorType                 errorCode,
262		const char                          *serviceName,
263		const char                          *regtype,
264		const char                          *replyDomain,
265		void                                *context
266		);
267
268
269
270	static void DNSSD_API
271
272	ResolveReply
273
274		(
275		DNSServiceRef                       sdRef,
276		DNSServiceFlags                     flags,
277		uint32_t                            interfaceIndex,
278		DNSServiceErrorType                 errorCode,
279		const char                          *fullname,
280		const char                          *hosttarget,
281		uint16_t                            port,
282		uint16_t                            txtLen,
283		const unsigned char                 *txtRecord,
284		void                                *context
285
286		);
287
288
289
290	static void DNSSD_API
291	RegisterReply
292		(
293		DNSServiceRef                       sdRef,
294		DNSServiceFlags                     flags,
295		DNSServiceErrorType                 errorCode,
296		const char                          *name,
297		const char                          *regtype,
298		const char                          *domain,
299		void                                *context
300		);
301
302
303
304	static void DNSSD_API
305	QueryRecordReply
306		(
307		DNSServiceRef                       sdRef,
308		DNSServiceFlags                     flags,
309		uint32_t                            interfaceIndex,
310		DNSServiceErrorType                 errorCode,
311		const char                          *fullname,
312		uint16_t                            rrtype,
313		uint16_t                            rrclass,
314		uint16_t                            rdlen,
315		const void                          *rdata,
316		uint32_t                            ttl,
317		void                                *context
318		);
319
320
321
322	static void DNSSD_API
323    GetAddrInfoReply
324		(
325		DNSServiceRef                    sdRef,
326		DNSServiceFlags                  flags,
327		uint32_t                         interfaceIndex,
328		DNSServiceErrorType              errorCode,
329		const char                       *hostname,
330		const struct sockaddr            *address,
331		uint32_t                         ttl,
332		void                             *context
333		);
334
335
336
337	static void DNSSD_API
338	NATPortMappingReply
339		(
340		DNSServiceRef                    sdRef,
341		DNSServiceFlags                  flags,
342		uint32_t                         interfaceIndex,
343		DNSServiceErrorType              errorCode,
344		uint32_t                         externalAddress,   /* four byte IPv4 address in network byte order */
345		DNSServiceProtocol               protocol,
346		uint16_t                         internalPort,
347		uint16_t                         externalPort,      /* may be different than the requested port     */
348		uint32_t                         ttl,               /* may be different than the requested ttl      */
349		void                             *context
350		);
351
352
353
354	static void DNSSD_API
355	RegisterRecordReply
356		(
357		DNSServiceRef                       sdRef,
358		DNSRecordRef                        RecordRef,
359		DNSServiceFlags                     flags,
360		DNSServiceErrorType                 errorCode,
361		void                                *context
362		);
363
364
365
366	inline BOOL
367
368	ShouldHandleReply( DNSServiceErrorType errorCode, CDNSSDEventManager *& eventManager );
369
370
371
372	static LRESULT CALLBACK
373
374	WndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );
375
376
377
378	typedef std::map< SOCKET, CDNSSDService* > SocketMap;
379
380
381
382	static BOOL				m_registeredWindowClass;
383
384	static HWND				m_hiddenWindow;
385
386	static SocketMap		m_socketMap;
387
388	CDNSSDEventManager	*	m_eventManager;
389
390	BOOL					m_stopped;
391
392	BOOL					m_isPrimary;
393
394	DNSServiceRef			m_primary;
395
396	DNSServiceRef			m_subord;
397
398public:
399
400	STDMETHOD(EnumerateDomains)(DNSSDFlags flags, ULONG ifIndex, IDNSSDEventManager *eventManager, IDNSSDService **service);
401
402	STDMETHOD(Browse)(DNSSDFlags flags, ULONG interfaceIndex, BSTR regtype, BSTR domain, IDNSSDEventManager* eventManager, IDNSSDService** sdref);
403
404	STDMETHOD(Resolve)(DNSSDFlags flags, ULONG ifIndex, BSTR serviceName, BSTR regType, BSTR domain, IDNSSDEventManager* eventManager, IDNSSDService** service);
405
406	STDMETHOD(Register)(DNSSDFlags flags, ULONG ifIndex, BSTR name, BSTR regType, BSTR domain, BSTR host, USHORT port, ITXTRecord *record, IDNSSDEventManager *eventManager, IDNSSDService **service);
407
408	STDMETHOD(QueryRecord)(DNSSDFlags flags, ULONG ifIndex, BSTR fullname, DNSSDRRType rrtype, DNSSDRRClass rrclass, IDNSSDEventManager *eventManager, IDNSSDService **service);
409
410	STDMETHOD(RegisterRecord)(DNSSDFlags flags, ULONG ifIndex, BSTR fullname, DNSSDRRType rrtype, DNSSDRRClass rrclass, VARIANT rdata, ULONG ttl, IDNSSDEventManager* eventManager, IDNSSDRecord** record);
411
412	STDMETHOD(AddRecord)(DNSSDFlags flags, DNSSDRRType rrtype, VARIANT rdata, ULONG ttl, IDNSSDRecord ** record);
413
414	STDMETHOD(ReconfirmRecord)(DNSSDFlags flags, ULONG ifIndex, BSTR fullname, DNSSDRRType rrtype, DNSSDRRClass rrclass, VARIANT rdata);
415
416	STDMETHOD(GetProperty)(BSTR prop, VARIANT * value);
417
418	STDMETHOD(GetAddrInfo)(DNSSDFlags flags, ULONG ifIndex, DNSSDAddressFamily addressFamily, BSTR hostname, IDNSSDEventManager *eventManager, IDNSSDService **service);
419
420	STDMETHOD(NATPortMappingCreate)(DNSSDFlags flags, ULONG ifIndex, DNSSDAddressFamily addressFamily, DNSSDProtocol protocol, USHORT internalPort, USHORT externalPort, ULONG ttl, IDNSSDEventManager *eventManager, IDNSSDService **service);
421
422	STDMETHOD(Stop)(void);
423
424};
425
426
427
428OBJECT_ENTRY_AUTO(__uuidof(DNSSDService), CDNSSDService)
429
430