Added memicmpW.
[wine.git] / dlls / dplayx / name_server.c
blobcbca83ad0eb53860a76bdecfd1964e3b8aa614f2
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 "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 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 void NS_SetRemoteComputerAsNameServer( LPCDPSESSIONDESC2 lpsd, LPVOID lpNSInfo )
88 lpNSCache lpCache = (lpNSCache)lpNSInfo;
90 lpCache->bNsIsLocal = FALSE;
93 DPQ_DECL_COMPARECB( cbUglyPig, GUID )
95 return IsEqualGUID( elem1, elem2 );
98 /* Store the given NS remote address for future reference */
99 /* FIXME: LPDPMSG_ENUMSESSIONSREPLY should be const */
100 void NS_AddRemoteComputerAsNameServer( LPCVOID lpcNSAddrHdr,
101 DWORD dwHdrSize,
102 LPDPMSG_ENUMSESSIONSREPLY lpMsg,
103 LPVOID lpNSInfo )
105 DWORD len;
106 lpNSCache lpCache = (lpNSCache)lpNSInfo;
107 lpNSCacheData lpCacheNode;
109 TRACE( "%p, %p, %p\n", lpcNSAddrHdr, lpMsg, lpNSInfo );
111 /* See if we can find this session. If we can, remove it as it's a dup */
112 DPQ_REMOVE_ENTRY_CB( lpCache->first, next, data->guidInstance, cbUglyPig,
113 lpMsg->sd.guidInstance, lpCacheNode );
115 if( lpCacheNode != NULL )
117 TRACE( "Duplicate session entry for %s removed - updated version kept\n",
118 debugstr_guid( &lpCacheNode->data->guidInstance ) );
119 cbDeleteNSNodeFromHeap( lpCacheNode );
122 /* Add this to the list */
123 lpCacheNode = (lpNSCacheData)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
124 sizeof( *lpCacheNode ) );
126 if( lpCacheNode == NULL )
128 ERR( "no memory for NS node\n" );
129 return;
132 lpCacheNode->lpNSAddrHdr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
133 dwHdrSize );
134 CopyMemory( lpCacheNode->lpNSAddrHdr, lpcNSAddrHdr, dwHdrSize );
137 lpCacheNode->data = (LPDPSESSIONDESC2)HeapAlloc( GetProcessHeap(),
138 HEAP_ZERO_MEMORY,
139 sizeof( *(lpCacheNode->data) ) );
141 if( lpCacheNode->data == NULL )
143 ERR( "no memory for SESSIONDESC2\n" );
144 return;
147 CopyMemory( lpCacheNode->data, &lpMsg->sd, sizeof( *lpCacheNode->data ) );
148 len = WideCharToMultiByte( CP_ACP, 0, (LPWSTR)(lpMsg+1), -1, NULL, 0, NULL, NULL );
149 if ((lpCacheNode->data->u1.lpszSessionNameA = HeapAlloc( GetProcessHeap(), 0, len )))
151 WideCharToMultiByte( CP_ACP, 0, (LPWSTR)(lpMsg+1), -1,
152 lpCacheNode->data->u1.lpszSessionNameA, len, NULL, NULL );
155 lpCacheNode->dwTime = timeGetTime();
157 DPQ_INSERT(lpCache->first, lpCacheNode, next );
159 lpCache->present = lpCacheNode;
161 /* Use this message as an opportunity to weed out any old sessions so
162 * that we don't enum them again
164 NS_PruneSessionCache( lpNSInfo );
167 LPVOID NS_GetNSAddr( LPVOID lpNSInfo )
169 lpNSCache lpCache = (lpNSCache)lpNSInfo;
171 FIXME( ":quick stub\n" );
173 /* Ok. Cheat and don't search for the correct stuff just take the first.
174 * FIXME: In the future how are we to know what is _THE_ enum we used?
175 * This is going to have to go into dplay somehow. Perhaps it
176 * comes back with app server id for the join command! Oh...that
177 * must be it. That would make this method obsolete once that's
178 * in place.
180 #if 1
181 return lpCache->first.lpQHFirst->lpNSAddrHdr;
182 #else
183 /* FIXME: Should convert over to this */
184 return lpCache->bNsIsLocal ? lpCache->lpLocalAddrHdr
185 : lpCache->lpRemoteAddrHdr;
186 #endif
189 /* Get the magic number associated with the Name Server */
190 DWORD NS_GetNsMagic( LPVOID lpNSInfo )
192 LPDWORD lpHdrInfo = (LPDWORD)NS_GetNSAddr( lpNSInfo );
194 return lpHdrInfo[1];
197 /* Get the magic number associated with the non NS end */
198 DWORD NS_GetOtherMagic( LPVOID lpNSInfo )
200 lpNSCache lpCache = (lpNSCache)lpNSInfo;
202 return ((LPDWORD)lpCache->lpLocalAddrHdr)[1];
205 void NS_SetLocalAddr( LPVOID lpNSInfo, LPCVOID lpHdr, DWORD dwHdrSize )
207 lpNSCache lpCache = (lpNSCache)lpNSInfo;
209 lpCache->lpLocalAddrHdr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, dwHdrSize );
211 CopyMemory( lpCache->lpLocalAddrHdr, lpHdr, dwHdrSize );
214 /* This function is responsible for sending a request for all other known
215 nameservers to send us what sessions they have registered locally
217 HRESULT NS_SendSessionRequestBroadcast( LPCGUID lpcGuid,
218 DWORD dwFlags,
219 LPSPINITDATA lpSpData )
222 DPSP_ENUMSESSIONSDATA data;
223 LPDPMSG_ENUMSESSIONSREQUEST lpMsg;
225 TRACE( "enumerating for guid %s\n", debugstr_guid( lpcGuid ) );
227 /* Get the SP to deal with sending the EnumSessions request */
228 FIXME( ": not all data fields are correct\n" );
230 data.dwMessageSize = lpSpData->dwSPHeaderSize + sizeof( *lpMsg ); /*FIXME!*/
231 data.lpMessage = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
232 data.dwMessageSize );
233 data.lpISP = lpSpData->lpISP;
234 data.bReturnStatus = (dwFlags & DPENUMSESSIONS_RETURNSTATUS) ? TRUE : FALSE;
237 lpMsg = (LPDPMSG_ENUMSESSIONSREQUEST)(((BYTE*)data.lpMessage)+lpSpData->dwSPHeaderSize);
239 /* Setup EnumSession request message */
240 lpMsg->envelope.dwMagic = DPMSGMAGIC_DPLAYMSG;
241 lpMsg->envelope.wCommandId = DPMSGCMD_ENUMSESSIONSREQUEST;
242 lpMsg->envelope.wVersion = DPMSGVER_DP6;
244 lpMsg->dwPasswordSize = 0; /* FIXME: If enumerating passwords..? */
245 lpMsg->dwFlags = dwFlags;
247 CopyMemory( &lpMsg->guidApplication, lpcGuid, sizeof( *lpcGuid ) );
249 return (lpSpData->lpCB->EnumSessions)( &data );
252 /* Delete a name server node which has been allocated on the heap */
253 DPQ_DECL_DELETECB( cbDeleteNSNodeFromHeap, lpNSCacheData )
255 /* NOTE: This proc doesn't deal with the walking pointer */
257 /* FIXME: Memory leak on data (contained ptrs) */
258 HeapFree( GetProcessHeap(), 0, elem->data );
259 HeapFree( GetProcessHeap(), 0, elem->lpNSAddrHdr );
260 HeapFree( GetProcessHeap(), 0, elem );
263 /* Render all data in a session cache invalid */
264 void NS_InvalidateSessionCache( LPVOID lpNSInfo )
266 lpNSCache lpCache = (lpNSCache)lpNSInfo;
268 if( lpCache == NULL )
270 ERR( ": invalidate non existent cache\n" );
271 return;
274 DPQ_DELETEQ( lpCache->first, next, lpNSCacheData, cbDeleteNSNodeFromHeap );
276 /* NULL out the walking pointer */
277 lpCache->present = NULL;
279 lpCache->bNsIsLocal = FALSE;
283 /* Create and initialize a session cache */
284 BOOL NS_InitializeSessionCache( LPVOID* lplpNSInfo )
286 lpNSCache lpCache = (lpNSCache)HeapAlloc( GetProcessHeap(),
287 HEAP_ZERO_MEMORY,
288 sizeof( *lpCache ) );
290 *lplpNSInfo = lpCache;
292 if( lpCache == NULL )
294 return FALSE;
297 DPQ_INIT(lpCache->first);
298 lpCache->present = NULL;
300 lpCache->bNsIsLocal = FALSE;
302 return TRUE;
305 /* Delete a session cache */
306 void NS_DeleteSessionCache( LPVOID lpNSInfo )
308 NS_InvalidateSessionCache( (lpNSCache)lpNSInfo );
311 /* Reinitialize the present pointer for this cache */
312 void NS_ResetSessionEnumeration( LPVOID lpNSInfo )
314 ((lpNSCache)lpNSInfo)->present = ((lpNSCache)lpNSInfo)->first.lpQHFirst;
317 LPDPSESSIONDESC2 NS_WalkSessions( LPVOID lpNSInfo )
319 LPDPSESSIONDESC2 lpSessionDesc;
320 lpNSCache lpCache = (lpNSCache)lpNSInfo;
322 /* FIXME: The pointers could disappear when walking if a prune happens */
324 /* Test for end of the list */
325 if( lpCache->present == NULL )
327 return NULL;
330 lpSessionDesc = lpCache->present->data;
332 /* Advance tracking pointer */
333 lpCache->present = lpCache->present->next.lpQNext;
335 return lpSessionDesc;
338 /* This method should check to see if there are any sessions which are
339 * older than the criteria. If so, just delete that information.
341 /* FIXME: This needs to be called by some periodic timer */
342 void NS_PruneSessionCache( LPVOID lpNSInfo )
344 lpNSCache lpCache = lpNSInfo;
346 const DWORD dwPresentTime = timeGetTime();
347 const DWORD dwPrunePeriod = DPMSG_WAIT_60_SECS; /* is 60 secs enough? */
349 /* This silly little algorithm is based on the fact we keep entries in
350 * the queue in a time based order. It also assumes that it is not possible
351 * to wrap around over yourself (which is not unreasonable).
352 * The if statements verify if the first entry in the queue is less
353 * than dwPrunePeriod old depending on the "clock" roll over.
355 for( ;; )
357 lpNSCacheData lpFirstData;
359 if( DPQ_IS_EMPTY(lpCache->first) )
361 /* Nothing to prune */
362 break;
365 /* Deal with time in a wrap around safe manner - unsigned arithmetic.
366 * Check the difference in time */
367 if( (dwPresentTime - (DPQ_FIRST(lpCache->first)->dwTime)) < dwPrunePeriod )
369 /* First entry has not expired yet; don't prune */
370 break;
373 lpFirstData = DPQ_FIRST(lpCache->first);
374 DPQ_REMOVE( lpCache->first, DPQ_FIRST(lpCache->first), next );
375 cbDeleteNSNodeFromHeap( lpFirstData );
380 /* NAME SERVER Message stuff */
381 void NS_ReplyToEnumSessionsRequest( LPCVOID lpcMsg,
382 LPVOID* lplpReplyData,
383 LPDWORD lpdwReplySize,
384 IDirectPlay2Impl* lpDP )
386 LPDPMSG_ENUMSESSIONSREPLY rmsg;
387 DWORD dwVariableSize;
388 DWORD dwVariableLen;
389 /* LPCDPMSG_ENUMSESSIONSREQUEST msg = (LPDPMSG_ENUMSESSIONSREQUEST)lpcMsg; */
390 BOOL bAnsi = TRUE; /* FIXME: This needs to be in the DPLAY interface */
392 FIXME( ": few fixed + need to check request for response\n" );
394 if (bAnsi)
396 dwVariableLen = MultiByteToWideChar( CP_ACP, 0,
397 lpDP->dp2->lpSessionDesc->u1.lpszSessionNameA,
398 -1, NULL, 0 );
400 else
402 dwVariableLen = strlenW( lpDP->dp2->lpSessionDesc->u1.lpszSessionName ) + 1;
405 dwVariableSize = dwVariableLen * sizeof( WCHAR );
407 *lpdwReplySize = lpDP->dp2->spData.dwSPHeaderSize +
408 sizeof( *rmsg ) + dwVariableSize;
409 *lplpReplyData = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
410 *lpdwReplySize );
412 rmsg = (LPDPMSG_ENUMSESSIONSREPLY)( (BYTE*)*lplpReplyData +
413 lpDP->dp2->spData.dwSPHeaderSize);
415 rmsg->envelope.dwMagic = DPMSGMAGIC_DPLAYMSG;
416 rmsg->envelope.wCommandId = DPMSGCMD_ENUMSESSIONSREPLY;
417 rmsg->envelope.wVersion = DPMSGVER_DP6;
419 CopyMemory( &rmsg->sd, lpDP->dp2->lpSessionDesc,
420 sizeof( lpDP->dp2->lpSessionDesc->dwSize ) );
421 rmsg->dwUnknown = 0x0000005c;
422 if( bAnsi )
424 MultiByteToWideChar( CP_ACP, 0, lpDP->dp2->lpSessionDesc->u1.lpszSessionNameA, -1,
425 (LPWSTR)(rmsg+1), dwVariableLen );
427 else
429 strcpyW( (LPWSTR)(rmsg+1), lpDP->dp2->lpSessionDesc->u1.lpszSessionName );