setupapi: Remove some dead code: VCP_VirtnodeDelete() and VCP_RenameFiles().
[wine/wine-gecko.git] / dlls / dplayx / name_server.c
blob58402cc12dec0254e32d0df93003c6522ccdbfc1
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 <stdarg.h>
23 #include <string.h>
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winnls.h"
30 #include "wine/unicode.h"
31 #include "wine/debug.h"
32 #include "mmsystem.h"
34 #include "dplayx_global.h"
35 #include "name_server.h"
36 #include "wine/dplaysp.h"
37 #include "dplayx_messages.h"
38 #include "dplayx_queue.h"
40 /* FIXME: Need to create a crit section, store and use it */
42 WINE_DEFAULT_DEBUG_CHANNEL(dplay);
44 /* NS specific structures */
45 struct NSCacheData
47 DPQ_ENTRY(NSCacheData) next;
49 DWORD dwTime; /* Time at which data was last known valid */
50 LPDPSESSIONDESC2 data;
52 LPVOID lpNSAddrHdr;
55 typedef struct NSCacheData NSCacheData, *lpNSCacheData;
57 struct NSCache
59 lpNSCacheData present; /* keep track of what is to be looked at when walking */
61 DPQ_HEAD(NSCacheData) first;
63 BOOL bNsIsLocal;
64 LPVOID lpLocalAddrHdr; /* FIXME: Not yet used */
65 LPVOID lpRemoteAddrHdr; /* FIXME: Not yet used */
67 typedef struct NSCache NSCache, *lpNSCache;
69 /* Function prototypes */
70 static DPQ_DECL_DELETECB( cbDeleteNSNodeFromHeap, lpNSCacheData );
72 /* Name Server functions
73 * ---------------------
75 void NS_SetLocalComputerAsNameServer( LPCDPSESSIONDESC2 lpsd, LPVOID lpNSInfo )
77 #if 0
78 /* FIXME: Remove this method? */
79 DPLAYX_SetLocalSession( lpsd );
80 #endif
81 lpNSCache lpCache = (lpNSCache)lpNSInfo;
83 lpCache->bNsIsLocal = TRUE;
86 static DPQ_DECL_COMPARECB( cbUglyPig, GUID )
88 return IsEqualGUID( elem1, elem2 );
91 /* Store the given NS remote address for future reference */
92 void NS_AddRemoteComputerAsNameServer( LPCVOID lpcNSAddrHdr,
93 DWORD dwHdrSize,
94 LPCDPMSG_ENUMSESSIONSREPLY lpcMsg,
95 LPVOID lpNSInfo )
97 DWORD len;
98 lpNSCache lpCache = (lpNSCache)lpNSInfo;
99 lpNSCacheData lpCacheNode;
101 TRACE( "%p, %p, %p\n", lpcNSAddrHdr, lpcMsg, lpNSInfo );
103 /* See if we can find this session. If we can, remove it as it's a dup */
104 DPQ_REMOVE_ENTRY_CB( lpCache->first, next, data->guidInstance, cbUglyPig,
105 lpcMsg->sd.guidInstance, lpCacheNode );
107 if( lpCacheNode != NULL )
109 TRACE( "Duplicate session entry for %s removed - updated version kept\n",
110 debugstr_guid( &lpCacheNode->data->guidInstance ) );
111 cbDeleteNSNodeFromHeap( lpCacheNode );
114 /* Add this to the list */
115 lpCacheNode = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof( *lpCacheNode ) );
117 if( lpCacheNode == NULL )
119 ERR( "no memory for NS node\n" );
120 return;
123 lpCacheNode->lpNSAddrHdr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
124 dwHdrSize );
125 CopyMemory( lpCacheNode->lpNSAddrHdr, lpcNSAddrHdr, dwHdrSize );
127 lpCacheNode->data = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof( *(lpCacheNode->data) ) );
129 if( lpCacheNode->data == NULL )
131 ERR( "no memory for SESSIONDESC2\n" );
132 HeapFree( GetProcessHeap(), 0, lpCacheNode );
133 return;
136 *lpCacheNode->data = lpcMsg->sd;
137 len = WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)(lpcMsg+1), -1, NULL, 0, NULL, NULL );
138 if ((lpCacheNode->data->u1.lpszSessionNameA = HeapAlloc( GetProcessHeap(), 0, len )))
140 WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)(lpcMsg+1), -1,
141 lpCacheNode->data->u1.lpszSessionNameA, len, NULL, NULL );
144 lpCacheNode->dwTime = timeGetTime();
146 DPQ_INSERT(lpCache->first, lpCacheNode, next );
148 lpCache->present = lpCacheNode;
150 /* Use this message as an opportunity to weed out any old sessions so
151 * that we don't enum them again
153 NS_PruneSessionCache( lpNSInfo );
156 LPVOID NS_GetNSAddr( LPVOID lpNSInfo )
158 lpNSCache lpCache = (lpNSCache)lpNSInfo;
160 FIXME( ":quick stub\n" );
162 /* Ok. Cheat and don't search for the correct stuff just take the first.
163 * FIXME: In the future how are we to know what is _THE_ enum we used?
164 * This is going to have to go into dplay somehow. Perhaps it
165 * comes back with app server id for the join command! Oh... that
166 * must be it. That would make this method obsolete once that's
167 * in place.
169 #if 1
170 return lpCache->first.lpQHFirst->lpNSAddrHdr;
171 #else
172 /* FIXME: Should convert over to this */
173 return lpCache->bNsIsLocal ? lpCache->lpLocalAddrHdr
174 : lpCache->lpRemoteAddrHdr;
175 #endif
178 /* Get the magic number associated with the Name Server */
179 DWORD NS_GetNsMagic( LPVOID lpNSInfo )
181 LPDWORD lpHdrInfo = NS_GetNSAddr( lpNSInfo );
183 return lpHdrInfo[1];
186 void NS_SetLocalAddr( LPVOID lpNSInfo, LPCVOID lpHdr, DWORD dwHdrSize )
188 lpNSCache lpCache = (lpNSCache)lpNSInfo;
190 lpCache->lpLocalAddrHdr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, dwHdrSize );
192 CopyMemory( lpCache->lpLocalAddrHdr, lpHdr, dwHdrSize );
195 /* This function is responsible for sending a request for all other known
196 nameservers to send us what sessions they have registered locally
198 HRESULT NS_SendSessionRequestBroadcast( LPCGUID lpcGuid,
199 DWORD dwFlags,
200 const SPINITDATA *lpSpData )
203 DPSP_ENUMSESSIONSDATA data;
204 LPDPMSG_ENUMSESSIONSREQUEST lpMsg;
206 TRACE( "enumerating for guid %s\n", debugstr_guid( lpcGuid ) );
208 /* Get the SP to deal with sending the EnumSessions request */
209 FIXME( ": not all data fields are correct\n" );
211 data.dwMessageSize = lpSpData->dwSPHeaderSize + sizeof( *lpMsg ); /*FIXME!*/
212 data.lpMessage = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
213 data.dwMessageSize );
214 data.lpISP = lpSpData->lpISP;
215 data.bReturnStatus = (dwFlags & DPENUMSESSIONS_RETURNSTATUS) ? TRUE : FALSE;
218 lpMsg = (LPDPMSG_ENUMSESSIONSREQUEST)(((BYTE*)data.lpMessage)+lpSpData->dwSPHeaderSize);
220 /* Setup EnumSession request message */
221 lpMsg->envelope.dwMagic = DPMSGMAGIC_DPLAYMSG;
222 lpMsg->envelope.wCommandId = DPMSGCMD_ENUMSESSIONSREQUEST;
223 lpMsg->envelope.wVersion = DPMSGVER_DP6;
225 lpMsg->dwPasswordSize = 0; /* FIXME: If enumerating passwords..? */
226 lpMsg->dwFlags = dwFlags;
228 lpMsg->guidApplication = *lpcGuid;
230 return (lpSpData->lpCB->EnumSessions)( &data );
233 /* Delete a name server node which has been allocated on the heap */
234 static DPQ_DECL_DELETECB( cbDeleteNSNodeFromHeap, lpNSCacheData )
236 /* NOTE: This proc doesn't deal with the walking pointer */
238 /* FIXME: Memory leak on data (contained ptrs) */
239 HeapFree( GetProcessHeap(), 0, elem->data );
240 HeapFree( GetProcessHeap(), 0, elem->lpNSAddrHdr );
241 HeapFree( GetProcessHeap(), 0, elem );
244 /* Render all data in a session cache invalid */
245 void NS_InvalidateSessionCache( LPVOID lpNSInfo )
247 lpNSCache lpCache = (lpNSCache)lpNSInfo;
249 if( lpCache == NULL )
251 ERR( ": invalidate nonexistent cache\n" );
252 return;
255 DPQ_DELETEQ( lpCache->first, next, lpNSCacheData, cbDeleteNSNodeFromHeap );
257 /* NULL out the walking pointer */
258 lpCache->present = NULL;
260 lpCache->bNsIsLocal = FALSE;
264 /* Create and initialize a session cache */
265 BOOL NS_InitializeSessionCache( LPVOID* lplpNSInfo )
267 lpNSCache lpCache = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof( *lpCache ) );
269 *lplpNSInfo = lpCache;
271 if( lpCache == NULL )
273 return FALSE;
276 DPQ_INIT(lpCache->first);
277 lpCache->present = NULL;
279 lpCache->bNsIsLocal = FALSE;
281 return TRUE;
284 /* Delete a session cache */
285 void NS_DeleteSessionCache( LPVOID lpNSInfo )
287 NS_InvalidateSessionCache( (lpNSCache)lpNSInfo );
290 /* Reinitialize the present pointer for this cache */
291 void NS_ResetSessionEnumeration( LPVOID lpNSInfo )
293 ((lpNSCache)lpNSInfo)->present = ((lpNSCache)lpNSInfo)->first.lpQHFirst;
296 LPDPSESSIONDESC2 NS_WalkSessions( LPVOID lpNSInfo )
298 LPDPSESSIONDESC2 lpSessionDesc;
299 lpNSCache lpCache = (lpNSCache)lpNSInfo;
301 /* FIXME: The pointers could disappear when walking if a prune happens */
303 /* Test for end of the list */
304 if( lpCache->present == NULL )
306 return NULL;
309 lpSessionDesc = lpCache->present->data;
311 /* Advance tracking pointer */
312 lpCache->present = lpCache->present->next.lpQNext;
314 return lpSessionDesc;
317 /* This method should check to see if there are any sessions which are
318 * older than the criteria. If so, just delete that information.
320 /* FIXME: This needs to be called by some periodic timer */
321 void NS_PruneSessionCache( LPVOID lpNSInfo )
323 lpNSCache lpCache = lpNSInfo;
325 const DWORD dwPresentTime = timeGetTime();
326 const DWORD dwPrunePeriod = DPMSG_WAIT_60_SECS; /* is 60 secs enough? */
328 /* This silly little algorithm is based on the fact we keep entries in
329 * the queue in a time based order. It also assumes that it is not possible
330 * to wrap around over yourself (which is not unreasonable).
331 * The if statements verify if the first entry in the queue is less
332 * than dwPrunePeriod old depending on the "clock" roll over.
334 for( ;; )
336 lpNSCacheData lpFirstData;
338 if( DPQ_IS_EMPTY(lpCache->first) )
340 /* Nothing to prune */
341 break;
344 /* Deal with time in a wrap around safe manner - unsigned arithmetic.
345 * Check the difference in time */
346 if( (dwPresentTime - (DPQ_FIRST(lpCache->first)->dwTime)) < dwPrunePeriod )
348 /* First entry has not expired yet; don't prune */
349 break;
352 lpFirstData = DPQ_FIRST(lpCache->first);
353 DPQ_REMOVE( lpCache->first, DPQ_FIRST(lpCache->first), next );
354 cbDeleteNSNodeFromHeap( lpFirstData );
359 /* NAME SERVER Message stuff */
360 void NS_ReplyToEnumSessionsRequest( LPCVOID lpcMsg,
361 LPVOID* lplpReplyData,
362 LPDWORD lpdwReplySize,
363 IDirectPlay2Impl* lpDP )
365 LPDPMSG_ENUMSESSIONSREPLY rmsg;
366 DWORD dwVariableSize;
367 DWORD dwVariableLen;
368 /* LPCDPMSG_ENUMSESSIONSREQUEST msg = (LPDPMSG_ENUMSESSIONSREQUEST)lpcMsg; */
369 BOOL bAnsi = TRUE; /* FIXME: This needs to be in the DPLAY interface */
371 FIXME( ": few fixed + need to check request for response\n" );
373 if (bAnsi)
375 dwVariableLen = MultiByteToWideChar( CP_ACP, 0,
376 lpDP->dp2->lpSessionDesc->u1.lpszSessionNameA,
377 -1, NULL, 0 );
379 else
381 dwVariableLen = strlenW( lpDP->dp2->lpSessionDesc->u1.lpszSessionName ) + 1;
384 dwVariableSize = dwVariableLen * sizeof( WCHAR );
386 *lpdwReplySize = lpDP->dp2->spData.dwSPHeaderSize +
387 sizeof( *rmsg ) + dwVariableSize;
388 *lplpReplyData = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
389 *lpdwReplySize );
391 rmsg = (LPDPMSG_ENUMSESSIONSREPLY)( (BYTE*)*lplpReplyData +
392 lpDP->dp2->spData.dwSPHeaderSize);
394 rmsg->envelope.dwMagic = DPMSGMAGIC_DPLAYMSG;
395 rmsg->envelope.wCommandId = DPMSGCMD_ENUMSESSIONSREPLY;
396 rmsg->envelope.wVersion = DPMSGVER_DP6;
398 CopyMemory( &rmsg->sd, lpDP->dp2->lpSessionDesc,
399 lpDP->dp2->lpSessionDesc->dwSize );
400 rmsg->dwUnknown = 0x0000005c;
401 if( bAnsi )
403 MultiByteToWideChar( CP_ACP, 0, lpDP->dp2->lpSessionDesc->u1.lpszSessionNameA, -1,
404 (LPWSTR)(rmsg+1), dwVariableLen );
406 else
408 strcpyW( (LPWSTR)(rmsg+1), lpDP->dp2->lpSessionDesc->u1.lpszSessionName );