1 /* DPLAYX.DLL name server implementation
3 * Copyright 2000-2001 - Peter Hunnisett
5 * <presently under construction - contact hunnise@nortelnetworks.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 /* NOTE: Methods with the NS_ prefix are name server methods */
27 #include "wine/unicode.h"
28 #include "wine/debug.h"
31 #include "dplayx_global.h"
32 #include "name_server.h"
34 #include "dplayx_messages.h"
35 #include "dplayx_queue.h"
37 /* FIXME: Need to create a crit section, store and use it */
39 WINE_DEFAULT_DEBUG_CHANNEL(dplay);
41 /* NS specific structures */
44 DPQ_ENTRY(NSCacheData) next;
46 DWORD dwTime; /* Time at which data was last known valid */
47 LPDPSESSIONDESC2 data;
52 typedef struct NSCacheData NSCacheData, *lpNSCacheData;
56 lpNSCacheData present; /* keep track of what is to be looked at when walking */
58 DPQ_HEAD(NSCacheData) first;
61 LPVOID lpLocalAddrHdr; /* FIXME: Not yet used */
62 LPVOID lpRemoteAddrHdr; /* FIXME: Not yet used */
64 typedef struct NSCache NSCache, *lpNSCache;
66 /* Function prototypes */
67 DPQ_DECL_DELETECB( cbDeleteNSNodeFromHeap, lpNSCacheData );
69 /* Name Server functions
70 * ---------------------
72 void NS_SetLocalComputerAsNameServer( LPCDPSESSIONDESC2 lpsd, LPVOID lpNSInfo )
75 /* FIXME: Remove this method? */
76 DPLAYX_SetLocalSession( lpsd );
78 lpNSCache lpCache = (lpNSCache)lpNSInfo;
80 lpCache->bNsIsLocal = TRUE;
83 void NS_SetRemoteComputerAsNameServer( LPCDPSESSIONDESC2 lpsd, LPVOID lpNSInfo )
85 lpNSCache lpCache = (lpNSCache)lpNSInfo;
87 lpCache->bNsIsLocal = FALSE;
90 DPQ_DECL_COMPARECB( cbUglyPig, GUID )
92 return IsEqualGUID( elem1, elem2 );
95 /* Store the given NS remote address for future reference */
96 /* FIXME: LPDPMSG_ENUMSESSIONSREPLY should be const */
97 void NS_AddRemoteComputerAsNameServer( LPCVOID lpcNSAddrHdr,
99 LPDPMSG_ENUMSESSIONSREPLY lpMsg,
103 lpNSCache lpCache = (lpNSCache)lpNSInfo;
104 lpNSCacheData lpCacheNode;
106 TRACE( "%p, %p, %p\n", lpcNSAddrHdr, lpMsg, lpNSInfo );
108 /* See if we can find this session. If we can, remove it as it's a dup */
109 DPQ_REMOVE_ENTRY_CB( lpCache->first, next, data->guidInstance, cbUglyPig,
110 lpMsg->sd.guidInstance, lpCacheNode );
112 if( lpCacheNode != NULL )
114 TRACE( "Duplicate session entry for %s removed - updated version kept\n",
115 debugstr_guid( &lpCacheNode->data->guidInstance ) );
116 cbDeleteNSNodeFromHeap( lpCacheNode );
119 /* Add this to the list */
120 lpCacheNode = (lpNSCacheData)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
121 sizeof( *lpCacheNode ) );
123 if( lpCacheNode == NULL )
125 ERR( "no memory for NS node\n" );
129 lpCacheNode->lpNSAddrHdr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
131 CopyMemory( lpCacheNode->lpNSAddrHdr, lpcNSAddrHdr, dwHdrSize );
134 lpCacheNode->data = (LPDPSESSIONDESC2)HeapAlloc( GetProcessHeap(),
136 sizeof( *(lpCacheNode->data) ) );
138 if( lpCacheNode->data == NULL )
140 ERR( "no memory for SESSIONDESC2\n" );
144 CopyMemory( lpCacheNode->data, &lpMsg->sd, sizeof( *lpCacheNode->data ) );
145 len = WideCharToMultiByte( CP_ACP, 0, (LPWSTR)(lpMsg+1), -1, NULL, 0, NULL, NULL );
146 if ((lpCacheNode->data->u1.lpszSessionNameA = HeapAlloc( GetProcessHeap(), 0, len )))
148 WideCharToMultiByte( CP_ACP, 0, (LPWSTR)(lpMsg+1), -1,
149 lpCacheNode->data->u1.lpszSessionNameA, len, NULL, NULL );
152 lpCacheNode->dwTime = timeGetTime();
154 DPQ_INSERT(lpCache->first, lpCacheNode, next );
156 lpCache->present = lpCacheNode;
158 /* Use this message as an oportunity to weed out any old sessions so
159 * that we don't enum them again
161 NS_PruneSessionCache( lpNSInfo );
164 LPVOID NS_GetNSAddr( LPVOID lpNSInfo )
166 lpNSCache lpCache = (lpNSCache)lpNSInfo;
168 FIXME( ":quick stub\n" );
170 /* Ok. Cheat and don't search for the correct stuff just take the first.
171 * FIXME: In the future how are we to know what is _THE_ enum we used?
172 * This is going to have to go into dplay somehow. Perhaps it
173 * comes back with app server id for the join command! Oh...that
174 * must be it. That would make this method obsolete once that's
178 return lpCache->first.lpQHFirst->lpNSAddrHdr;
180 /* FIXME: Should convert over to this */
181 return lpCache->bNsIsLocal ? lpCache->lpLocalAddrHdr
182 : lpCache->lpRemoteAddrHdr;
186 /* Get the magic number associated with the Name Server */
187 DWORD NS_GetNsMagic( LPVOID lpNSInfo )
189 LPDWORD lpHdrInfo = (LPDWORD)NS_GetNSAddr( lpNSInfo );
194 /* Get the magic number associated with the non NS end */
195 DWORD NS_GetOtherMagic( LPVOID lpNSInfo )
197 lpNSCache lpCache = (lpNSCache)lpNSInfo;
199 return ((LPDWORD)lpCache->lpLocalAddrHdr)[1];
202 void NS_SetLocalAddr( LPVOID lpNSInfo, LPCVOID lpHdr, DWORD dwHdrSize )
204 lpNSCache lpCache = (lpNSCache)lpNSInfo;
206 lpCache->lpLocalAddrHdr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, dwHdrSize );
208 CopyMemory( lpCache->lpLocalAddrHdr, lpHdr, dwHdrSize );
211 /* This function is responsible for sending a request for all other known
212 nameservers to send us what sessions they have registered locally
214 HRESULT NS_SendSessionRequestBroadcast( LPCGUID lpcGuid,
216 LPSPINITDATA lpSpData )
219 DPSP_ENUMSESSIONSDATA data;
220 LPDPMSG_ENUMSESSIONSREQUEST lpMsg;
222 TRACE( "enumerating for guid %s\n", debugstr_guid( lpcGuid ) );
224 /* Get the SP to deal with sending the EnumSessions request */
225 FIXME( ": not all data fields are correct\n" );
227 data.dwMessageSize = lpSpData->dwSPHeaderSize + sizeof( *lpMsg ); /*FIXME!*/
228 data.lpMessage = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
229 data.dwMessageSize );
230 data.lpISP = lpSpData->lpISP;
231 data.bReturnStatus = (dwFlags & DPENUMSESSIONS_RETURNSTATUS) ? TRUE : FALSE;
234 lpMsg = (LPDPMSG_ENUMSESSIONSREQUEST)(((BYTE*)data.lpMessage)+lpSpData->dwSPHeaderSize);
236 /* Setup EnumSession reqest message */
237 lpMsg->envelope.dwMagic = DPMSGMAGIC_DPLAYMSG;
238 lpMsg->envelope.wCommandId = DPMSGCMD_ENUMSESSIONSREQUEST;
239 lpMsg->envelope.wVersion = DPMSGVER_DP6;
241 lpMsg->dwPasswordSize = 0; /* FIXME: If enumerating passwords..? */
242 lpMsg->dwFlags = dwFlags;
244 CopyMemory( &lpMsg->guidApplication, lpcGuid, sizeof( *lpcGuid ) );
246 return (lpSpData->lpCB->EnumSessions)( &data );
249 /* Delete a name server node which has been allocated on the heap */
250 DPQ_DECL_DELETECB( cbDeleteNSNodeFromHeap, lpNSCacheData )
252 /* NOTE: This proc doesn't deal with the walking pointer */
254 /* FIXME: Memory leak on data (contained ptrs) */
255 HeapFree( GetProcessHeap(), 0, elem->data );
256 HeapFree( GetProcessHeap(), 0, elem->lpNSAddrHdr );
257 HeapFree( GetProcessHeap(), 0, elem );
260 /* Render all data in a session cache invalid */
261 void NS_InvalidateSessionCache( LPVOID lpNSInfo )
263 lpNSCache lpCache = (lpNSCache)lpNSInfo;
265 if( lpCache == NULL )
267 ERR( ": invalidate non existant cache\n" );
271 DPQ_DELETEQ( lpCache->first, next, lpNSCacheData, cbDeleteNSNodeFromHeap );
273 /* NULL out the walking pointer */
274 lpCache->present = NULL;
276 lpCache->bNsIsLocal = FALSE;
280 /* Create and initialize a session cache */
281 BOOL NS_InitializeSessionCache( LPVOID* lplpNSInfo )
283 lpNSCache lpCache = (lpNSCache)HeapAlloc( GetProcessHeap(),
285 sizeof( *lpCache ) );
287 *lplpNSInfo = lpCache;
289 if( lpCache == NULL )
294 DPQ_INIT(lpCache->first);
295 lpCache->present = NULL;
297 lpCache->bNsIsLocal = FALSE;
302 /* Delete a session cache */
303 void NS_DeleteSessionCache( LPVOID lpNSInfo )
305 NS_InvalidateSessionCache( (lpNSCache)lpNSInfo );
308 /* Reinitialize the present pointer for this cache */
309 void NS_ResetSessionEnumeration( LPVOID lpNSInfo )
311 ((lpNSCache)lpNSInfo)->present = ((lpNSCache)lpNSInfo)->first.lpQHFirst;
314 LPDPSESSIONDESC2 NS_WalkSessions( LPVOID lpNSInfo )
316 LPDPSESSIONDESC2 lpSessionDesc;
317 lpNSCache lpCache = (lpNSCache)lpNSInfo;
319 /* FIXME: The pointers could disappear when walking if a prune happens */
321 /* Test for end of the list */
322 if( lpCache->present == NULL )
327 lpSessionDesc = lpCache->present->data;
329 /* Advance tracking pointer */
330 lpCache->present = lpCache->present->next.lpQNext;
332 return lpSessionDesc;
335 /* This method should check to see if there are any sessions which are
336 * older than the criteria. If so, just delete that information.
338 /* FIXME: This needs to be called by some periodic timer */
339 void NS_PruneSessionCache( LPVOID lpNSInfo )
341 lpNSCache lpCache = lpNSInfo;
343 const DWORD dwPresentTime = timeGetTime();
344 const DWORD dwPrunePeriod = DPMSG_WAIT_60_SECS; /* is 60 secs enough? */
346 /* This silly little algorithm is based on the fact we keep entries in
347 * the queue in a time based order. It also assumes that it is not possible
348 * to wrap around over yourself (which is not unreasonable).
349 * The if statements verify if the first entry in the queue is less
350 * than dwPrunePeriod old depending on the "clock" roll over.
354 lpNSCacheData lpFirstData;
356 if( DPQ_IS_EMPTY(lpCache->first) )
358 /* Nothing to prune */
362 /* Deal with time in a wrap around safe manner - unsigned arithmatic.
363 * Check the difference in time */
364 if( (dwPresentTime - (DPQ_FIRST(lpCache->first)->dwTime)) < dwPrunePeriod )
366 /* First entry has not expired yet; don't prune */
370 lpFirstData = DPQ_FIRST(lpCache->first);
371 DPQ_REMOVE( lpCache->first, DPQ_FIRST(lpCache->first), next );
372 cbDeleteNSNodeFromHeap( lpFirstData );
377 /* NAME SERVER Message stuff */
378 void NS_ReplyToEnumSessionsRequest( LPCVOID lpcMsg,
379 LPVOID* lplpReplyData,
380 LPDWORD lpdwReplySize,
381 IDirectPlay2Impl* lpDP )
383 LPDPMSG_ENUMSESSIONSREPLY rmsg;
384 DWORD dwVariableSize;
386 /* LPCDPMSG_ENUMSESSIONSREQUEST msg = (LPDPMSG_ENUMSESSIONSREQUEST)lpcMsg; */
387 BOOL bAnsi = TRUE; /* FIXME: This needs to be in the DPLAY interface */
389 FIXME( ": few fixed + need to check request for response\n" );
393 dwVariableLen = MultiByteToWideChar( CP_ACP, 0,
394 lpDP->dp2->lpSessionDesc->u1.lpszSessionNameA,
399 dwVariableLen = strlenW( lpDP->dp2->lpSessionDesc->u1.lpszSessionName ) + 1;
402 dwVariableSize = dwVariableLen * sizeof( WCHAR );
404 *lpdwReplySize = lpDP->dp2->spData.dwSPHeaderSize +
405 sizeof( *rmsg ) + dwVariableSize;
406 *lplpReplyData = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
409 rmsg = (LPDPMSG_ENUMSESSIONSREPLY)( (BYTE*)*lplpReplyData +
410 lpDP->dp2->spData.dwSPHeaderSize);
412 rmsg->envelope.dwMagic = DPMSGMAGIC_DPLAYMSG;
413 rmsg->envelope.wCommandId = DPMSGCMD_ENUMSESSIONSREPLY;
414 rmsg->envelope.wVersion = DPMSGVER_DP6;
416 CopyMemory( &rmsg->sd, lpDP->dp2->lpSessionDesc,
417 sizeof( lpDP->dp2->lpSessionDesc->dwSize ) );
418 rmsg->dwUnknown = 0x0000005c;
421 MultiByteToWideChar( CP_ACP, 0, lpDP->dp2->lpSessionDesc->u1.lpszSessionNameA, -1,
422 (LPWSTR)(rmsg+1), dwVariableLen );
426 strcpyW( (LPWSTR)(rmsg+1), lpDP->dp2->lpSessionDesc->u1.lpszSessionName );