winehid.sys: Implement handing internal ioctls.
[wine.git] / dlls / dplayx / name_server.c
blob7b04bcd96fbc33feab8190a6e5314d74ec89a499
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/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 lpNSCache lpCache = (lpNSCache)lpNSInfo;
79 lpCache->bNsIsLocal = TRUE;
82 static DPQ_DECL_COMPARECB( cbUglyPig, GUID )
84 return IsEqualGUID( elem1, elem2 );
87 /* Store the given NS remote address for future reference */
88 void NS_AddRemoteComputerAsNameServer( LPCVOID lpcNSAddrHdr,
89 DWORD dwHdrSize,
90 LPCDPMSG_ENUMSESSIONSREPLY lpcMsg,
91 LPVOID lpNSInfo )
93 DWORD len;
94 lpNSCache lpCache = (lpNSCache)lpNSInfo;
95 lpNSCacheData lpCacheNode;
97 TRACE( "%p, %p, %p\n", lpcNSAddrHdr, lpcMsg, lpNSInfo );
99 /* See if we can find this session. If we can, remove it as it's a dup */
100 DPQ_REMOVE_ENTRY_CB( lpCache->first, next, data->guidInstance, cbUglyPig,
101 lpcMsg->sd.guidInstance, lpCacheNode );
103 if( lpCacheNode != NULL )
105 TRACE( "Duplicate session entry for %s removed - updated version kept\n",
106 debugstr_guid( &lpCacheNode->data->guidInstance ) );
107 cbDeleteNSNodeFromHeap( lpCacheNode );
110 /* Add this to the list */
111 lpCacheNode = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof( *lpCacheNode ) );
113 if( lpCacheNode == NULL )
115 ERR( "no memory for NS node\n" );
116 return;
119 lpCacheNode->lpNSAddrHdr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
120 dwHdrSize );
121 CopyMemory( lpCacheNode->lpNSAddrHdr, lpcNSAddrHdr, dwHdrSize );
123 lpCacheNode->data = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof( *(lpCacheNode->data) ) );
125 if( lpCacheNode->data == NULL )
127 ERR( "no memory for SESSIONDESC2\n" );
128 HeapFree( GetProcessHeap(), 0, lpCacheNode );
129 return;
132 *lpCacheNode->data = lpcMsg->sd;
133 len = WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)(lpcMsg+1), -1, NULL, 0, NULL, NULL );
134 if ((lpCacheNode->data->u1.lpszSessionNameA = HeapAlloc( GetProcessHeap(), 0, len )))
136 WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)(lpcMsg+1), -1,
137 lpCacheNode->data->u1.lpszSessionNameA, len, NULL, NULL );
140 lpCacheNode->dwTime = timeGetTime();
142 DPQ_INSERT(lpCache->first, lpCacheNode, next );
144 lpCache->present = lpCacheNode;
146 /* Use this message as an opportunity to weed out any old sessions so
147 * that we don't enum them again
149 NS_PruneSessionCache( lpNSInfo );
152 LPVOID NS_GetNSAddr( LPVOID lpNSInfo )
154 lpNSCache lpCache = (lpNSCache)lpNSInfo;
156 FIXME( ":quick stub\n" );
158 /* Ok. Cheat and don't search for the correct stuff just take the first.
159 * FIXME: In the future how are we to know what is _THE_ enum we used?
160 * This is going to have to go into dplay somehow. Perhaps it
161 * comes back with app server id for the join command! Oh... that
162 * must be it. That would make this method obsolete once that's
163 * in place.
165 #if 1
166 return lpCache->first.lpQHFirst->lpNSAddrHdr;
167 #else
168 /* FIXME: Should convert over to this */
169 return lpCache->bNsIsLocal ? lpCache->lpLocalAddrHdr
170 : lpCache->lpRemoteAddrHdr;
171 #endif
174 /* Get the magic number associated with the Name Server */
175 DWORD NS_GetNsMagic( LPVOID lpNSInfo )
177 LPDWORD lpHdrInfo = NS_GetNSAddr( lpNSInfo );
179 return lpHdrInfo[1];
182 void NS_SetLocalAddr( LPVOID lpNSInfo, LPCVOID lpHdr, DWORD dwHdrSize )
184 lpNSCache lpCache = (lpNSCache)lpNSInfo;
186 lpCache->lpLocalAddrHdr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, dwHdrSize );
188 CopyMemory( lpCache->lpLocalAddrHdr, lpHdr, dwHdrSize );
191 /* This function is responsible for sending a request for all other known
192 nameservers to send us what sessions they have registered locally
194 HRESULT NS_SendSessionRequestBroadcast( LPCGUID lpcGuid,
195 DWORD dwFlags,
196 const SPINITDATA *lpSpData )
199 DPSP_ENUMSESSIONSDATA data;
200 LPDPMSG_ENUMSESSIONSREQUEST lpMsg;
202 TRACE( "enumerating for guid %s\n", debugstr_guid( lpcGuid ) );
204 /* Get the SP to deal with sending the EnumSessions request */
205 FIXME( ": not all data fields are correct\n" );
207 data.dwMessageSize = lpSpData->dwSPHeaderSize + sizeof( *lpMsg ); /*FIXME!*/
208 data.lpMessage = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
209 data.dwMessageSize );
210 data.lpISP = lpSpData->lpISP;
211 data.bReturnStatus = (dwFlags & DPENUMSESSIONS_RETURNSTATUS) != 0;
214 lpMsg = (LPDPMSG_ENUMSESSIONSREQUEST)(((BYTE*)data.lpMessage)+lpSpData->dwSPHeaderSize);
216 /* Setup EnumSession request message */
217 lpMsg->envelope.dwMagic = DPMSGMAGIC_DPLAYMSG;
218 lpMsg->envelope.wCommandId = DPMSGCMD_ENUMSESSIONSREQUEST;
219 lpMsg->envelope.wVersion = DPMSGVER_DP6;
221 lpMsg->dwPasswordSize = 0; /* FIXME: If enumerating passwords..? */
222 lpMsg->dwFlags = dwFlags;
224 lpMsg->guidApplication = *lpcGuid;
226 return (lpSpData->lpCB->EnumSessions)( &data );
229 /* Delete a name server node which has been allocated on the heap */
230 static DPQ_DECL_DELETECB( cbDeleteNSNodeFromHeap, lpNSCacheData )
232 /* NOTE: This proc doesn't deal with the walking pointer */
234 /* FIXME: Memory leak on data (contained ptrs) */
235 HeapFree( GetProcessHeap(), 0, elem->data );
236 HeapFree( GetProcessHeap(), 0, elem->lpNSAddrHdr );
237 HeapFree( GetProcessHeap(), 0, elem );
240 /* Render all data in a session cache invalid */
241 void NS_InvalidateSessionCache( LPVOID lpNSInfo )
243 lpNSCache lpCache = (lpNSCache)lpNSInfo;
245 if( lpCache == NULL )
247 ERR( ": invalidate nonexistent cache\n" );
248 return;
251 DPQ_DELETEQ( lpCache->first, next, lpNSCacheData, cbDeleteNSNodeFromHeap );
253 /* NULL out the walking pointer */
254 lpCache->present = NULL;
256 lpCache->bNsIsLocal = FALSE;
260 /* Create and initialize a session cache */
261 BOOL NS_InitializeSessionCache( LPVOID* lplpNSInfo )
263 lpNSCache lpCache = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof( *lpCache ) );
265 *lplpNSInfo = lpCache;
267 if( lpCache == NULL )
269 return FALSE;
272 DPQ_INIT(lpCache->first);
273 lpCache->present = NULL;
275 lpCache->bNsIsLocal = FALSE;
277 return TRUE;
280 /* Delete a session cache */
281 void NS_DeleteSessionCache( LPVOID lpNSInfo )
283 NS_InvalidateSessionCache( (lpNSCache)lpNSInfo );
286 /* Reinitialize the present pointer for this cache */
287 void NS_ResetSessionEnumeration( LPVOID lpNSInfo )
289 ((lpNSCache)lpNSInfo)->present = ((lpNSCache)lpNSInfo)->first.lpQHFirst;
292 LPDPSESSIONDESC2 NS_WalkSessions( LPVOID lpNSInfo )
294 LPDPSESSIONDESC2 lpSessionDesc;
295 lpNSCache lpCache = (lpNSCache)lpNSInfo;
297 /* FIXME: The pointers could disappear when walking if a prune happens */
299 /* Test for end of the list */
300 if( lpCache->present == NULL )
302 return NULL;
305 lpSessionDesc = lpCache->present->data;
307 /* Advance tracking pointer */
308 lpCache->present = lpCache->present->next.lpQNext;
310 return lpSessionDesc;
313 /* This method should check to see if there are any sessions which are
314 * older than the criteria. If so, just delete that information.
316 /* FIXME: This needs to be called by some periodic timer */
317 void NS_PruneSessionCache( LPVOID lpNSInfo )
319 lpNSCache lpCache = lpNSInfo;
321 const DWORD dwPresentTime = timeGetTime();
322 const DWORD dwPrunePeriod = DPMSG_WAIT_60_SECS; /* is 60 secs enough? */
324 /* This silly little algorithm is based on the fact we keep entries in
325 * the queue in a time based order. It also assumes that it is not possible
326 * to wrap around over yourself (which is not unreasonable).
327 * The if statements verify if the first entry in the queue is less
328 * than dwPrunePeriod old depending on the "clock" roll over.
330 for( ;; )
332 lpNSCacheData lpFirstData;
334 if( DPQ_IS_EMPTY(lpCache->first) )
336 /* Nothing to prune */
337 break;
340 /* Deal with time in a wrap around safe manner - unsigned arithmetic.
341 * Check the difference in time */
342 if( (dwPresentTime - (DPQ_FIRST(lpCache->first)->dwTime)) < dwPrunePeriod )
344 /* First entry has not expired yet; don't prune */
345 break;
348 lpFirstData = DPQ_FIRST(lpCache->first);
349 DPQ_REMOVE( lpCache->first, DPQ_FIRST(lpCache->first), next );
350 cbDeleteNSNodeFromHeap( lpFirstData );
355 /* NAME SERVER Message stuff */
356 void NS_ReplyToEnumSessionsRequest( const void *lpcMsg, void **lplpReplyData, DWORD *lpdwReplySize,
357 IDirectPlayImpl *lpDP )
359 LPDPMSG_ENUMSESSIONSREPLY rmsg;
360 DWORD dwVariableSize;
361 DWORD dwVariableLen;
362 /* LPCDPMSG_ENUMSESSIONSREQUEST msg = (LPDPMSG_ENUMSESSIONSREQUEST)lpcMsg; */
364 /* FIXME: Should handle ANSI or WIDECHAR input. Currently just ANSI input */
365 FIXME( ": few fixed + need to check request for response, might need UNICODE input ability.\n" );
367 dwVariableLen = MultiByteToWideChar( CP_ACP, 0,
368 lpDP->dp2->lpSessionDesc->u1.lpszSessionNameA,
369 -1, NULL, 0 );
370 dwVariableSize = dwVariableLen * sizeof( WCHAR );
372 *lpdwReplySize = lpDP->dp2->spData.dwSPHeaderSize +
373 sizeof( *rmsg ) + dwVariableSize;
374 *lplpReplyData = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
375 *lpdwReplySize );
377 rmsg = (LPDPMSG_ENUMSESSIONSREPLY)( (BYTE*)*lplpReplyData +
378 lpDP->dp2->spData.dwSPHeaderSize);
380 rmsg->envelope.dwMagic = DPMSGMAGIC_DPLAYMSG;
381 rmsg->envelope.wCommandId = DPMSGCMD_ENUMSESSIONSREPLY;
382 rmsg->envelope.wVersion = DPMSGVER_DP6;
384 CopyMemory( &rmsg->sd, lpDP->dp2->lpSessionDesc,
385 lpDP->dp2->lpSessionDesc->dwSize );
386 rmsg->dwUnknown = 0x0000005c;
387 MultiByteToWideChar( CP_ACP, 0, lpDP->dp2->lpSessionDesc->u1.lpszSessionNameA, -1,
388 (LPWSTR)(rmsg+1), dwVariableLen );