1 /* DPLAYX.DLL name server implementation
3 * Copyright 2000-2001 - Peter Hunnisett
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 /* NOTE: Methods with the NS_ prefix are name server methods */
22 #include "dplayx_global.h"
26 /* FIXME: Need to create a crit section, store and use it */
28 /* NS specific structures */
31 DPQ_ENTRY(NSCacheData
) next
;
33 DWORD dwTime
; /* Time at which data was last known valid */
34 LPDPSESSIONDESC2 data
;
39 typedef struct NSCacheData NSCacheData
, *lpNSCacheData
;
43 lpNSCacheData present
; /* keep track of what is to be looked at when walking */
45 DPQ_HEAD(NSCacheData
) first
;
48 LPVOID lpLocalAddrHdr
; /* FIXME: Not yet used */
49 LPVOID lpRemoteAddrHdr
; /* FIXME: Not yet used */
51 typedef struct NSCache NSCache
, *lpNSCache
;
53 /* Function prototypes */
54 static DPQ_DECL_DELETECB( cbDeleteNSNodeFromHeap
, lpNSCacheData
);
56 /* Name Server functions
57 * ---------------------
59 void NS_SetLocalComputerAsNameServer( LPCDPSESSIONDESC2 lpsd
, LPVOID lpNSInfo
)
61 lpNSCache lpCache
= (lpNSCache
)lpNSInfo
;
63 lpCache
->bNsIsLocal
= TRUE
;
66 static DPQ_DECL_COMPARECB( cbUglyPig
, GUID
)
68 return IsEqualGUID( elem1
, elem2
);
71 /* Store the given NS remote address for future reference */
72 void NS_AddRemoteComputerAsNameServer( LPCVOID lpcNSAddrHdr
,
74 LPCDPMSG_ENUMSESSIONSREPLY lpcMsg
,
78 lpNSCache lpCache
= (lpNSCache
)lpNSInfo
;
79 lpNSCacheData lpCacheNode
;
81 TRACE( "%p, %p, %p\n", lpcNSAddrHdr
, lpcMsg
, lpNSInfo
);
83 /* See if we can find this session. If we can, remove it as it's a dup */
84 DPQ_REMOVE_ENTRY_CB( lpCache
->first
, next
, data
->guidInstance
, cbUglyPig
,
85 lpcMsg
->sd
.guidInstance
, lpCacheNode
);
87 if( lpCacheNode
!= NULL
)
89 TRACE( "Duplicate session entry for %s removed - updated version kept\n",
90 debugstr_guid( &lpCacheNode
->data
->guidInstance
) );
91 cbDeleteNSNodeFromHeap( lpCacheNode
);
94 /* Add this to the list */
95 lpCacheNode
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof( *lpCacheNode
) );
97 if( lpCacheNode
== NULL
)
99 ERR( "no memory for NS node\n" );
103 lpCacheNode
->lpNSAddrHdr
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
105 CopyMemory( lpCacheNode
->lpNSAddrHdr
, lpcNSAddrHdr
, dwHdrSize
);
107 lpCacheNode
->data
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof( *(lpCacheNode
->data
) ) );
109 if( lpCacheNode
->data
== NULL
)
111 ERR( "no memory for SESSIONDESC2\n" );
112 HeapFree( GetProcessHeap(), 0, lpCacheNode
);
116 *lpCacheNode
->data
= lpcMsg
->sd
;
117 len
= WideCharToMultiByte( CP_ACP
, 0, (LPCWSTR
)(lpcMsg
+1), -1, NULL
, 0, NULL
, NULL
);
118 if ((lpCacheNode
->data
->u1
.lpszSessionNameA
= HeapAlloc( GetProcessHeap(), 0, len
)))
120 WideCharToMultiByte( CP_ACP
, 0, (LPCWSTR
)(lpcMsg
+1), -1,
121 lpCacheNode
->data
->u1
.lpszSessionNameA
, len
, NULL
, NULL
);
124 lpCacheNode
->dwTime
= timeGetTime();
126 DPQ_INSERT(lpCache
->first
, lpCacheNode
, next
);
128 lpCache
->present
= lpCacheNode
;
130 /* Use this message as an opportunity to weed out any old sessions so
131 * that we don't enum them again
133 NS_PruneSessionCache( lpNSInfo
);
136 LPVOID
NS_GetNSAddr( LPVOID lpNSInfo
)
138 lpNSCache lpCache
= (lpNSCache
)lpNSInfo
;
140 FIXME( ":quick stub\n" );
142 /* Ok. Cheat and don't search for the correct stuff just take the first.
143 * FIXME: In the future how are we to know what is _THE_ enum we used?
144 * This is going to have to go into dplay somehow. Perhaps it
145 * comes back with app server id for the join command! Oh... that
146 * must be it. That would make this method obsolete once that's
150 return lpCache
->first
.lpQHFirst
->lpNSAddrHdr
;
152 /* FIXME: Should convert over to this */
153 return lpCache
->bNsIsLocal
? lpCache
->lpLocalAddrHdr
154 : lpCache
->lpRemoteAddrHdr
;
158 /* Get the magic number associated with the Name Server */
159 DWORD
NS_GetNsMagic( LPVOID lpNSInfo
)
161 LPDWORD lpHdrInfo
= NS_GetNSAddr( lpNSInfo
);
166 void NS_SetLocalAddr( LPVOID lpNSInfo
, LPCVOID lpHdr
, DWORD dwHdrSize
)
168 lpNSCache lpCache
= (lpNSCache
)lpNSInfo
;
170 lpCache
->lpLocalAddrHdr
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, dwHdrSize
);
172 CopyMemory( lpCache
->lpLocalAddrHdr
, lpHdr
, dwHdrSize
);
175 /* This function is responsible for sending a request for all other known
176 nameservers to send us what sessions they have registered locally
178 HRESULT
NS_SendSessionRequestBroadcast( LPCGUID lpcGuid
,
180 const SPINITDATA
*lpSpData
)
183 DPSP_ENUMSESSIONSDATA data
;
184 LPDPMSG_ENUMSESSIONSREQUEST lpMsg
;
186 TRACE( "enumerating for guid %s\n", debugstr_guid( lpcGuid
) );
188 /* Get the SP to deal with sending the EnumSessions request */
189 FIXME( ": not all data fields are correct\n" );
191 data
.dwMessageSize
= lpSpData
->dwSPHeaderSize
+ sizeof( *lpMsg
); /*FIXME!*/
192 data
.lpMessage
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
193 data
.dwMessageSize
);
194 data
.lpISP
= lpSpData
->lpISP
;
195 data
.bReturnStatus
= (dwFlags
& DPENUMSESSIONS_RETURNSTATUS
) != 0;
198 lpMsg
= (LPDPMSG_ENUMSESSIONSREQUEST
)(((BYTE
*)data
.lpMessage
)+lpSpData
->dwSPHeaderSize
);
200 /* Setup EnumSession request message */
201 lpMsg
->envelope
.dwMagic
= DPMSGMAGIC_DPLAYMSG
;
202 lpMsg
->envelope
.wCommandId
= DPMSGCMD_ENUMSESSIONSREQUEST
;
203 lpMsg
->envelope
.wVersion
= DPMSGVER_DP6
;
205 lpMsg
->dwPasswordSize
= 0; /* FIXME: If enumerating passwords..? */
206 lpMsg
->dwFlags
= dwFlags
;
208 lpMsg
->guidApplication
= *lpcGuid
;
210 return (lpSpData
->lpCB
->EnumSessions
)( &data
);
213 /* Delete a name server node which has been allocated on the heap */
214 static DPQ_DECL_DELETECB( cbDeleteNSNodeFromHeap
, lpNSCacheData
)
216 /* NOTE: This proc doesn't deal with the walking pointer */
218 /* FIXME: Memory leak on data (contained ptrs) */
219 HeapFree( GetProcessHeap(), 0, elem
->data
);
220 HeapFree( GetProcessHeap(), 0, elem
->lpNSAddrHdr
);
221 HeapFree( GetProcessHeap(), 0, elem
);
224 /* Render all data in a session cache invalid */
225 void NS_InvalidateSessionCache( LPVOID lpNSInfo
)
227 lpNSCache lpCache
= (lpNSCache
)lpNSInfo
;
229 if( lpCache
== NULL
)
231 ERR( ": invalidate nonexistent cache\n" );
235 DPQ_DELETEQ( lpCache
->first
, next
, lpNSCacheData
, cbDeleteNSNodeFromHeap
);
237 /* NULL out the walking pointer */
238 lpCache
->present
= NULL
;
240 lpCache
->bNsIsLocal
= FALSE
;
244 /* Create and initialize a session cache */
245 BOOL
NS_InitializeSessionCache( LPVOID
* lplpNSInfo
)
247 lpNSCache lpCache
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof( *lpCache
) );
249 *lplpNSInfo
= lpCache
;
251 if( lpCache
== NULL
)
256 DPQ_INIT(lpCache
->first
);
257 lpCache
->present
= NULL
;
259 lpCache
->bNsIsLocal
= FALSE
;
264 /* Delete a session cache */
265 void NS_DeleteSessionCache( LPVOID lpNSInfo
)
267 NS_InvalidateSessionCache( (lpNSCache
)lpNSInfo
);
270 /* Reinitialize the present pointer for this cache */
271 void NS_ResetSessionEnumeration( LPVOID lpNSInfo
)
273 ((lpNSCache
)lpNSInfo
)->present
= ((lpNSCache
)lpNSInfo
)->first
.lpQHFirst
;
276 LPDPSESSIONDESC2
NS_WalkSessions( LPVOID lpNSInfo
)
278 LPDPSESSIONDESC2 lpSessionDesc
;
279 lpNSCache lpCache
= (lpNSCache
)lpNSInfo
;
281 /* FIXME: The pointers could disappear when walking if a prune happens */
283 /* Test for end of the list */
284 if( lpCache
->present
== NULL
)
289 lpSessionDesc
= lpCache
->present
->data
;
291 /* Advance tracking pointer */
292 lpCache
->present
= lpCache
->present
->next
.lpQNext
;
294 return lpSessionDesc
;
297 /* This method should check to see if there are any sessions which are
298 * older than the criteria. If so, just delete that information.
300 /* FIXME: This needs to be called by some periodic timer */
301 void NS_PruneSessionCache( LPVOID lpNSInfo
)
303 lpNSCache lpCache
= lpNSInfo
;
305 const DWORD dwPresentTime
= timeGetTime();
306 const DWORD dwPrunePeriod
= DPMSG_WAIT_60_SECS
; /* is 60 secs enough? */
308 /* This silly little algorithm is based on the fact we keep entries in
309 * the queue in a time based order. It also assumes that it is not possible
310 * to wrap around over yourself (which is not unreasonable).
311 * The if statements verify if the first entry in the queue is less
312 * than dwPrunePeriod old depending on the "clock" roll over.
316 lpNSCacheData lpFirstData
;
318 if( DPQ_IS_EMPTY(lpCache
->first
) )
320 /* Nothing to prune */
324 /* Deal with time in a wrap around safe manner - unsigned arithmetic.
325 * Check the difference in time */
326 if( (dwPresentTime
- (DPQ_FIRST(lpCache
->first
)->dwTime
)) < dwPrunePeriod
)
328 /* First entry has not expired yet; don't prune */
332 lpFirstData
= DPQ_FIRST(lpCache
->first
);
333 DPQ_REMOVE( lpCache
->first
, DPQ_FIRST(lpCache
->first
), next
);
334 cbDeleteNSNodeFromHeap( lpFirstData
);
339 /* NAME SERVER Message stuff */
340 void NS_ReplyToEnumSessionsRequest( const void *lpcMsg
, void **lplpReplyData
, DWORD
*lpdwReplySize
,
341 IDirectPlayImpl
*lpDP
)
343 LPDPMSG_ENUMSESSIONSREPLY rmsg
;
344 DWORD dwVariableSize
;
346 /* LPCDPMSG_ENUMSESSIONSREQUEST msg = (LPDPMSG_ENUMSESSIONSREQUEST)lpcMsg; */
348 /* FIXME: Should handle ANSI or WIDECHAR input. Currently just ANSI input */
349 FIXME( ": few fixed + need to check request for response, might need UNICODE input ability.\n" );
351 dwVariableLen
= MultiByteToWideChar( CP_ACP
, 0,
352 lpDP
->dp2
->lpSessionDesc
->u1
.lpszSessionNameA
,
354 dwVariableSize
= dwVariableLen
* sizeof( WCHAR
);
356 *lpdwReplySize
= lpDP
->dp2
->spData
.dwSPHeaderSize
+
357 sizeof( *rmsg
) + dwVariableSize
;
358 *lplpReplyData
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
361 rmsg
= (LPDPMSG_ENUMSESSIONSREPLY
)( (BYTE
*)*lplpReplyData
+
362 lpDP
->dp2
->spData
.dwSPHeaderSize
);
364 rmsg
->envelope
.dwMagic
= DPMSGMAGIC_DPLAYMSG
;
365 rmsg
->envelope
.wCommandId
= DPMSGCMD_ENUMSESSIONSREPLY
;
366 rmsg
->envelope
.wVersion
= DPMSGVER_DP6
;
368 CopyMemory( &rmsg
->sd
, lpDP
->dp2
->lpSessionDesc
,
369 lpDP
->dp2
->lpSessionDesc
->dwSize
);
370 rmsg
->dwUnknown
= 0x0000005c;
371 MultiByteToWideChar( CP_ACP
, 0, lpDP
->dp2
->lpSessionDesc
->u1
.lpszSessionNameA
, -1,
372 (LPWSTR
)(rmsg
+1), dwVariableLen
);