ws2_32: Return a valid value for WSAIoctl SIO_IDEAL_SEND_BACKLOG_QUERY.
[wine.git] / dlls / dplayx / name_server.c
blobf1ebc4403c149681795b1b15ae4dc29a2f0301dd
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
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winnls.h"
30 #include "wine/debug.h"
31 #include "mmsystem.h"
33 #include "dplayx_global.h"
34 #include "name_server.h"
35 #include "wine/dplaysp.h"
36 #include "dplayx_messages.h"
37 #include "dplayx_queue.h"
39 /* FIXME: Need to create a crit section, store and use it */
41 WINE_DEFAULT_DEBUG_CHANNEL(dplay);
43 /* NS specific structures */
44 struct NSCacheData
46 DPQ_ENTRY(NSCacheData) next;
48 DWORD dwTime; /* Time at which data was last known valid */
49 LPDPSESSIONDESC2 data;
51 LPVOID lpNSAddrHdr;
54 typedef struct NSCacheData NSCacheData, *lpNSCacheData;
56 struct NSCache
58 lpNSCacheData present; /* keep track of what is to be looked at when walking */
60 DPQ_HEAD(NSCacheData) first;
62 BOOL bNsIsLocal;
63 LPVOID lpLocalAddrHdr; /* FIXME: Not yet used */
64 LPVOID lpRemoteAddrHdr; /* FIXME: Not yet used */
66 typedef struct NSCache NSCache, *lpNSCache;
68 /* Function prototypes */
69 static DPQ_DECL_DELETECB( cbDeleteNSNodeFromHeap, lpNSCacheData );
71 /* Name Server functions
72 * ---------------------
74 void NS_SetLocalComputerAsNameServer( LPCDPSESSIONDESC2 lpsd, LPVOID lpNSInfo )
76 lpNSCache lpCache = (lpNSCache)lpNSInfo;
78 lpCache->bNsIsLocal = TRUE;
81 static DPQ_DECL_COMPARECB( cbUglyPig, GUID )
83 return IsEqualGUID( elem1, elem2 );
86 /* Store the given NS remote address for future reference */
87 void NS_AddRemoteComputerAsNameServer( LPCVOID lpcNSAddrHdr,
88 DWORD dwHdrSize,
89 LPCDPMSG_ENUMSESSIONSREPLY lpcMsg,
90 LPVOID lpNSInfo )
92 DWORD len;
93 lpNSCache lpCache = (lpNSCache)lpNSInfo;
94 lpNSCacheData lpCacheNode;
96 TRACE( "%p, %p, %p\n", lpcNSAddrHdr, lpcMsg, lpNSInfo );
98 /* See if we can find this session. If we can, remove it as it's a dup */
99 DPQ_REMOVE_ENTRY_CB( lpCache->first, next, data->guidInstance, cbUglyPig,
100 lpcMsg->sd.guidInstance, lpCacheNode );
102 if( lpCacheNode != NULL )
104 TRACE( "Duplicate session entry for %s removed - updated version kept\n",
105 debugstr_guid( &lpCacheNode->data->guidInstance ) );
106 cbDeleteNSNodeFromHeap( lpCacheNode );
109 /* Add this to the list */
110 lpCacheNode = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof( *lpCacheNode ) );
112 if( lpCacheNode == NULL )
114 ERR( "no memory for NS node\n" );
115 return;
118 lpCacheNode->lpNSAddrHdr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
119 dwHdrSize );
120 CopyMemory( lpCacheNode->lpNSAddrHdr, lpcNSAddrHdr, dwHdrSize );
122 lpCacheNode->data = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof( *(lpCacheNode->data) ) );
124 if( lpCacheNode->data == NULL )
126 ERR( "no memory for SESSIONDESC2\n" );
127 HeapFree( GetProcessHeap(), 0, lpCacheNode );
128 return;
131 *lpCacheNode->data = lpcMsg->sd;
132 len = WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)(lpcMsg+1), -1, NULL, 0, NULL, NULL );
133 if ((lpCacheNode->data->u1.lpszSessionNameA = HeapAlloc( GetProcessHeap(), 0, len )))
135 WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)(lpcMsg+1), -1,
136 lpCacheNode->data->u1.lpszSessionNameA, len, NULL, NULL );
139 lpCacheNode->dwTime = timeGetTime();
141 DPQ_INSERT(lpCache->first, lpCacheNode, next );
143 lpCache->present = lpCacheNode;
145 /* Use this message as an opportunity to weed out any old sessions so
146 * that we don't enum them again
148 NS_PruneSessionCache( lpNSInfo );
151 LPVOID NS_GetNSAddr( LPVOID lpNSInfo )
153 lpNSCache lpCache = (lpNSCache)lpNSInfo;
155 FIXME( ":quick stub\n" );
157 /* Ok. Cheat and don't search for the correct stuff just take the first.
158 * FIXME: In the future how are we to know what is _THE_ enum we used?
159 * This is going to have to go into dplay somehow. Perhaps it
160 * comes back with app server id for the join command! Oh... that
161 * must be it. That would make this method obsolete once that's
162 * in place.
164 #if 1
165 if ( lpCache->first.lpQHFirst )
166 return lpCache->first.lpQHFirst->lpNSAddrHdr;
168 return NULL;
169 #else
170 /* FIXME: Should convert over to this */
171 return lpCache->bNsIsLocal ? lpCache->lpLocalAddrHdr
172 : lpCache->lpRemoteAddrHdr;
173 #endif
176 /* Get the magic number associated with the Name Server */
177 DWORD NS_GetNsMagic( LPVOID lpNSInfo )
179 LPDWORD lpHdrInfo = NS_GetNSAddr( lpNSInfo );
181 return lpHdrInfo[1];
184 void NS_SetLocalAddr( LPVOID lpNSInfo, LPCVOID lpHdr, DWORD dwHdrSize )
186 lpNSCache lpCache = (lpNSCache)lpNSInfo;
188 lpCache->lpLocalAddrHdr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, dwHdrSize );
190 CopyMemory( lpCache->lpLocalAddrHdr, lpHdr, dwHdrSize );
193 /* This function is responsible for sending a request for all other known
194 nameservers to send us what sessions they have registered locally
196 HRESULT NS_SendSessionRequestBroadcast( LPCGUID lpcGuid,
197 DWORD dwFlags,
198 const SPINITDATA *lpSpData )
201 DPSP_ENUMSESSIONSDATA data;
202 LPDPMSG_ENUMSESSIONSREQUEST lpMsg;
204 TRACE( "enumerating for guid %s\n", debugstr_guid( lpcGuid ) );
206 /* Get the SP to deal with sending the EnumSessions request */
207 FIXME( ": not all data fields are correct\n" );
209 data.dwMessageSize = lpSpData->dwSPHeaderSize + sizeof( *lpMsg ); /*FIXME!*/
210 data.lpMessage = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
211 data.dwMessageSize );
212 data.lpISP = lpSpData->lpISP;
213 data.bReturnStatus = (dwFlags & DPENUMSESSIONS_RETURNSTATUS) != 0;
216 lpMsg = (LPDPMSG_ENUMSESSIONSREQUEST)(((BYTE*)data.lpMessage)+lpSpData->dwSPHeaderSize);
218 /* Setup EnumSession request message */
219 lpMsg->envelope.dwMagic = DPMSGMAGIC_DPLAYMSG;
220 lpMsg->envelope.wCommandId = DPMSGCMD_ENUMSESSIONSREQUEST;
221 lpMsg->envelope.wVersion = DPMSGVER_DP6;
223 lpMsg->dwPasswordSize = 0; /* FIXME: If enumerating passwords..? */
224 lpMsg->dwFlags = dwFlags;
226 lpMsg->guidApplication = *lpcGuid;
228 return (lpSpData->lpCB->EnumSessions)( &data );
231 /* Delete a name server node which has been allocated on the heap */
232 static DPQ_DECL_DELETECB( cbDeleteNSNodeFromHeap, lpNSCacheData )
234 /* NOTE: This proc doesn't deal with the walking pointer */
236 /* FIXME: Memory leak on data (contained ptrs) */
237 HeapFree( GetProcessHeap(), 0, elem->data );
238 HeapFree( GetProcessHeap(), 0, elem->lpNSAddrHdr );
239 HeapFree( GetProcessHeap(), 0, elem );
242 /* Render all data in a session cache invalid */
243 void NS_InvalidateSessionCache( LPVOID lpNSInfo )
245 lpNSCache lpCache = (lpNSCache)lpNSInfo;
247 if( lpCache == NULL )
249 ERR( ": invalidate nonexistent cache\n" );
250 return;
253 DPQ_DELETEQ( lpCache->first, next, lpNSCacheData, cbDeleteNSNodeFromHeap );
255 /* NULL out the walking pointer */
256 lpCache->present = NULL;
258 lpCache->bNsIsLocal = FALSE;
262 /* Create and initialize a session cache */
263 BOOL NS_InitializeSessionCache( LPVOID* lplpNSInfo )
265 lpNSCache lpCache = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof( *lpCache ) );
267 *lplpNSInfo = lpCache;
269 if( lpCache == NULL )
271 return FALSE;
274 DPQ_INIT(lpCache->first);
275 lpCache->present = NULL;
277 lpCache->bNsIsLocal = FALSE;
279 return TRUE;
282 /* Delete a session cache */
283 void NS_DeleteSessionCache( LPVOID lpNSInfo )
285 NS_InvalidateSessionCache( (lpNSCache)lpNSInfo );
288 /* Reinitialize the present pointer for this cache */
289 void NS_ResetSessionEnumeration( LPVOID lpNSInfo )
291 ((lpNSCache)lpNSInfo)->present = ((lpNSCache)lpNSInfo)->first.lpQHFirst;
294 LPDPSESSIONDESC2 NS_WalkSessions( LPVOID lpNSInfo )
296 LPDPSESSIONDESC2 lpSessionDesc;
297 lpNSCache lpCache = (lpNSCache)lpNSInfo;
299 /* FIXME: The pointers could disappear when walking if a prune happens */
301 /* Test for end of the list */
302 if( lpCache->present == NULL )
304 return NULL;
307 lpSessionDesc = lpCache->present->data;
309 /* Advance tracking pointer */
310 lpCache->present = lpCache->present->next.lpQNext;
312 return lpSessionDesc;
315 /* This method should check to see if there are any sessions which are
316 * older than the criteria. If so, just delete that information.
318 /* FIXME: This needs to be called by some periodic timer */
319 void NS_PruneSessionCache( LPVOID lpNSInfo )
321 lpNSCache lpCache = lpNSInfo;
323 const DWORD dwPresentTime = timeGetTime();
324 const DWORD dwPrunePeriod = DPMSG_WAIT_60_SECS; /* is 60 secs enough? */
326 /* This silly little algorithm is based on the fact we keep entries in
327 * the queue in a time based order. It also assumes that it is not possible
328 * to wrap around over yourself (which is not unreasonable).
329 * The if statements verify if the first entry in the queue is less
330 * than dwPrunePeriod old depending on the "clock" roll over.
332 for( ;; )
334 lpNSCacheData lpFirstData;
336 if( DPQ_IS_EMPTY(lpCache->first) )
338 /* Nothing to prune */
339 break;
342 /* Deal with time in a wrap around safe manner - unsigned arithmetic.
343 * Check the difference in time */
344 if( (dwPresentTime - (DPQ_FIRST(lpCache->first)->dwTime)) < dwPrunePeriod )
346 /* First entry has not expired yet; don't prune */
347 break;
350 lpFirstData = DPQ_FIRST(lpCache->first);
351 DPQ_REMOVE( lpCache->first, DPQ_FIRST(lpCache->first), next );
352 cbDeleteNSNodeFromHeap( lpFirstData );
357 /* NAME SERVER Message stuff */
358 void NS_ReplyToEnumSessionsRequest( const void *lpcMsg, void **lplpReplyData, DWORD *lpdwReplySize,
359 IDirectPlayImpl *lpDP )
361 LPDPMSG_ENUMSESSIONSREPLY rmsg;
362 DWORD dwVariableSize;
363 DWORD dwVariableLen;
364 /* LPCDPMSG_ENUMSESSIONSREQUEST msg = (LPDPMSG_ENUMSESSIONSREQUEST)lpcMsg; */
366 /* FIXME: Should handle ANSI or WIDECHAR input. Currently just ANSI input */
367 FIXME( ": few fixed + need to check request for response, might need UNICODE input ability.\n" );
369 dwVariableLen = MultiByteToWideChar( CP_ACP, 0,
370 lpDP->dp2->lpSessionDesc->u1.lpszSessionNameA,
371 -1, NULL, 0 );
372 dwVariableSize = dwVariableLen * sizeof( WCHAR );
374 *lpdwReplySize = lpDP->dp2->spData.dwSPHeaderSize +
375 sizeof( *rmsg ) + dwVariableSize;
376 *lplpReplyData = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
377 *lpdwReplySize );
379 rmsg = (LPDPMSG_ENUMSESSIONSREPLY)( (BYTE*)*lplpReplyData +
380 lpDP->dp2->spData.dwSPHeaderSize);
382 rmsg->envelope.dwMagic = DPMSGMAGIC_DPLAYMSG;
383 rmsg->envelope.wCommandId = DPMSGCMD_ENUMSESSIONSREPLY;
384 rmsg->envelope.wVersion = DPMSGVER_DP6;
386 CopyMemory( &rmsg->sd, lpDP->dp2->lpSessionDesc,
387 lpDP->dp2->lpSessionDesc->dwSize );
388 rmsg->dwUnknown = 0x0000005c;
389 MultiByteToWideChar( CP_ACP, 0, lpDP->dp2->lpSessionDesc->u1.lpszSessionNameA, -1,
390 (LPWSTR)(rmsg+1), dwVariableLen );