ReactOS 0.4.15-dev-7918-g2a2556c
name_server.c
Go to the documentation of this file.
1/* DPLAYX.DLL name server implementation
2 *
3 * Copyright 2000-2001 - Peter Hunnisett
4 *
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.
9 *
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.
14 *
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
18 */
19
20/* NOTE: Methods with the NS_ prefix are name server methods */
21
22#include <stdarg.h>
23#include <string.h>
24
25#define NONAMELESSUNION
26
27#include "windef.h"
28#include "winbase.h"
29#include "winnls.h"
30#include "wine/debug.h"
31#include "mmsystem.h"
32
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"
38
39/* FIXME: Need to create a crit section, store and use it */
40
42
43/* NS specific structures */
45{
47
48 DWORD dwTime; /* Time at which data was last known valid */
50
51 LPVOID lpNSAddrHdr;
52
53};
55
56struct NSCache
57{
58 lpNSCacheData present; /* keep track of what is to be looked at when walking */
59
61
62 BOOL bNsIsLocal;
63 LPVOID lpLocalAddrHdr; /* FIXME: Not yet used */
64 LPVOID lpRemoteAddrHdr; /* FIXME: Not yet used */
65};
66typedef struct NSCache NSCache, *lpNSCache;
67
68/* Function prototypes */
69static DPQ_DECL_DELETECB( cbDeleteNSNodeFromHeap, lpNSCacheData );
70
71/* Name Server functions
72 * ---------------------
73 */
75{
76 lpNSCache lpCache = (lpNSCache)lpNSInfo;
77
78 lpCache->bNsIsLocal = TRUE;
79}
80
81static DPQ_DECL_COMPARECB( cbUglyPig, GUID )
82{
83 return IsEqualGUID( elem1, elem2 );
84}
85
86/* Store the given NS remote address for future reference */
88 DWORD dwHdrSize,
90 LPVOID lpNSInfo )
91{
92 DWORD len;
93 lpNSCache lpCache = (lpNSCache)lpNSInfo;
94 lpNSCacheData lpCacheNode;
95
96 TRACE( "%p, %p, %p\n", lpcNSAddrHdr, lpcMsg, lpNSInfo );
97
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 );
101
102 if( lpCacheNode != NULL )
103 {
104 TRACE( "Duplicate session entry for %s removed - updated version kept\n",
105 debugstr_guid( &lpCacheNode->data->guidInstance ) );
106 cbDeleteNSNodeFromHeap( lpCacheNode );
107 }
108
109 /* Add this to the list */
110 lpCacheNode = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof( *lpCacheNode ) );
111
112 if( lpCacheNode == NULL )
113 {
114 ERR( "no memory for NS node\n" );
115 return;
116 }
117
118 lpCacheNode->lpNSAddrHdr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
119 dwHdrSize );
120 CopyMemory( lpCacheNode->lpNSAddrHdr, lpcNSAddrHdr, dwHdrSize );
121
122 lpCacheNode->data = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof( *(lpCacheNode->data) ) );
123
124 if( lpCacheNode->data == NULL )
125 {
126 ERR( "no memory for SESSIONDESC2\n" );
127 HeapFree( GetProcessHeap(), 0, lpCacheNode );
128 return;
129 }
130
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 )))
134 {
135 WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)(lpcMsg+1), -1,
136 lpCacheNode->data->u1.lpszSessionNameA, len, NULL, NULL );
137 }
138
139 lpCacheNode->dwTime = timeGetTime();
140
141 DPQ_INSERT(lpCache->first, lpCacheNode, next );
142
143 lpCache->present = lpCacheNode;
144
145 /* Use this message as an opportunity to weed out any old sessions so
146 * that we don't enum them again
147 */
148 NS_PruneSessionCache( lpNSInfo );
149}
150
152{
153 lpNSCache lpCache = (lpNSCache)lpNSInfo;
154
155 FIXME( ":quick stub\n" );
156
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.
163 */
164#if 1
165 if ( lpCache->first.lpQHFirst )
166 return lpCache->first.lpQHFirst->lpNSAddrHdr;
167
168 return NULL;
169#else
170 /* FIXME: Should convert over to this */
171 return lpCache->bNsIsLocal ? lpCache->lpLocalAddrHdr
172 : lpCache->lpRemoteAddrHdr;
173#endif
174}
175
176/* Get the magic number associated with the Name Server */
178{
179 LPDWORD lpHdrInfo = NS_GetNSAddr( lpNSInfo );
180
181 return lpHdrInfo[1];
182}
183
184void NS_SetLocalAddr( LPVOID lpNSInfo, LPCVOID lpHdr, DWORD dwHdrSize )
185{
186 lpNSCache lpCache = (lpNSCache)lpNSInfo;
187
188 lpCache->lpLocalAddrHdr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, dwHdrSize );
189
190 CopyMemory( lpCache->lpLocalAddrHdr, lpHdr, dwHdrSize );
191}
192
193/* This function is responsible for sending a request for all other known
194 nameservers to send us what sessions they have registered locally
195 */
198 const SPINITDATA *lpSpData )
199
200{
203
204 TRACE( "enumerating for guid %s\n", debugstr_guid( lpcGuid ) );
205
206 /* Get the SP to deal with sending the EnumSessions request */
207 FIXME( ": not all data fields are correct\n" );
208
209 data.dwMessageSize = lpSpData->dwSPHeaderSize + sizeof( *lpMsg ); /*FIXME!*/
211 data.dwMessageSize );
212 data.lpISP = lpSpData->lpISP;
213 data.bReturnStatus = (dwFlags & DPENUMSESSIONS_RETURNSTATUS) != 0;
214
215
216 lpMsg = (LPDPMSG_ENUMSESSIONSREQUEST)(((BYTE*)data.lpMessage)+lpSpData->dwSPHeaderSize);
217
218 /* Setup EnumSession request message */
222
223 lpMsg->dwPasswordSize = 0; /* FIXME: If enumerating passwords..? */
224 lpMsg->dwFlags = dwFlags;
225
226 lpMsg->guidApplication = *lpcGuid;
227
228 return (lpSpData->lpCB->EnumSessions)( &data );
229}
230
231/* Delete a name server node which has been allocated on the heap */
232static DPQ_DECL_DELETECB( cbDeleteNSNodeFromHeap, lpNSCacheData )
233{
234 /* NOTE: This proc doesn't deal with the walking pointer */
235
236 /* FIXME: Memory leak on data (contained ptrs) */
237 HeapFree( GetProcessHeap(), 0, elem->data );
238 HeapFree( GetProcessHeap(), 0, elem->lpNSAddrHdr );
240}
241
242/* Render all data in a session cache invalid */
244{
245 lpNSCache lpCache = (lpNSCache)lpNSInfo;
246
247 if( lpCache == NULL )
248 {
249 ERR( ": invalidate nonexistent cache\n" );
250 return;
251 }
252
253 DPQ_DELETEQ( lpCache->first, next, lpNSCacheData, cbDeleteNSNodeFromHeap );
254
255 /* NULL out the walking pointer */
256 lpCache->present = NULL;
257
258 lpCache->bNsIsLocal = FALSE;
259
260}
261
262/* Create and initialize a session cache */
264{
265 lpNSCache lpCache = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof( *lpCache ) );
266
267 *lplpNSInfo = lpCache;
268
269 if( lpCache == NULL )
270 {
271 return FALSE;
272 }
273
274 DPQ_INIT(lpCache->first);
275 lpCache->present = NULL;
276
277 lpCache->bNsIsLocal = FALSE;
278
279 return TRUE;
280}
281
282/* Delete a session cache */
284{
286}
287
288/* Reinitialize the present pointer for this cache */
290{
291 ((lpNSCache)lpNSInfo)->present = ((lpNSCache)lpNSInfo)->first.lpQHFirst;
292}
293
295{
296 LPDPSESSIONDESC2 lpSessionDesc;
297 lpNSCache lpCache = (lpNSCache)lpNSInfo;
298
299 /* FIXME: The pointers could disappear when walking if a prune happens */
300
301 /* Test for end of the list */
302 if( lpCache->present == NULL )
303 {
304 return NULL;
305 }
306
307 lpSessionDesc = lpCache->present->data;
308
309 /* Advance tracking pointer */
310 lpCache->present = lpCache->present->next.lpQNext;
311
312 return lpSessionDesc;
313}
314
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.
317 */
318/* FIXME: This needs to be called by some periodic timer */
320{
321 lpNSCache lpCache = lpNSInfo;
322
323 const DWORD dwPresentTime = timeGetTime();
324 const DWORD dwPrunePeriod = DPMSG_WAIT_60_SECS; /* is 60 secs enough? */
325
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.
331 */
332 for( ;; )
333 {
334 lpNSCacheData lpFirstData;
335
336 if( DPQ_IS_EMPTY(lpCache->first) )
337 {
338 /* Nothing to prune */
339 break;
340 }
341
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 )
345 {
346 /* First entry has not expired yet; don't prune */
347 break;
348 }
349
350 lpFirstData = DPQ_FIRST(lpCache->first);
351 DPQ_REMOVE( lpCache->first, DPQ_FIRST(lpCache->first), next );
352 cbDeleteNSNodeFromHeap( lpFirstData );
353 }
354
355}
356
357/* NAME SERVER Message stuff */
358void NS_ReplyToEnumSessionsRequest( const void *lpcMsg, void **lplpReplyData, DWORD *lpdwReplySize,
359 IDirectPlayImpl *lpDP )
360{
362 DWORD dwVariableSize;
363 DWORD dwVariableLen;
364 /* LPCDPMSG_ENUMSESSIONSREQUEST msg = (LPDPMSG_ENUMSESSIONSREQUEST)lpcMsg; */
365
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" );
368
369 dwVariableLen = MultiByteToWideChar( CP_ACP, 0,
371 -1, NULL, 0 );
372 dwVariableSize = dwVariableLen * sizeof( WCHAR );
373
374 *lpdwReplySize = lpDP->dp2->spData.dwSPHeaderSize +
375 sizeof( *rmsg ) + dwVariableSize;
376 *lplpReplyData = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
377 *lpdwReplySize );
378
379 rmsg = (LPDPMSG_ENUMSESSIONSREPLY)( (BYTE*)*lplpReplyData +
380 lpDP->dp2->spData.dwSPHeaderSize);
381
385
386 CopyMemory( &rmsg->sd, lpDP->dp2->lpSessionDesc,
387 lpDP->dp2->lpSessionDesc->dwSize );
388 rmsg->dwUnknown = 0x0000005c;
390 (LPWSTR)(rmsg+1), dwVariableLen );
391}
#define WINE_DEFAULT_DEBUG_CHANNEL(t)
Definition: precomp.h:23
#define FIXME(fmt,...)
Definition: debug.h:111
#define ERR(fmt,...)
Definition: debug.h:110
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define GetProcessHeap()
Definition: compat.h:736
#define CP_ACP
Definition: compat.h:109
#define HeapAlloc
Definition: compat.h:733
#define HeapFree(x, y, z)
Definition: compat.h:735
#define WideCharToMultiByte
Definition: compat.h:111
#define MultiByteToWideChar
Definition: compat.h:110
#define HEAP_ZERO_MEMORY
Definition: compat.h:134
DWORD WINAPI timeGetTime(void)
Definition: time.c:466
#define DPENUMSESSIONS_RETURNSTATUS
Definition: dplay.h:1021
struct tagDPMSG_ENUMSESSIONSREQUEST * LPDPMSG_ENUMSESSIONSREQUEST
#define DPMSGVER_DP6
#define DPMSGCMD_ENUMSESSIONSREQUEST
struct tagDPMSG_ENUMSESSIONSREPLY * LPDPMSG_ENUMSESSIONSREPLY
#define DPMSGCMD_ENUMSESSIONSREPLY
#define DPMSG_WAIT_60_SECS
#define DPMSGMAGIC_DPLAYMSG
#define DPQ_INIT(head)
Definition: dplayx_queue.h:54
#define DPQ_INSERT(a, b, c)
Definition: dplayx_queue.h:34
#define DPQ_ENTRY(type)
Definition: dplayx_queue.h:45
#define DPQ_DECL_COMPARECB(name, type)
Definition: dplayx_queue.h:151
#define DPQ_HEAD(type)
Definition: dplayx_queue.h:39
#define DPQ_REMOVE_ENTRY_CB(head, elm, field, compare_cb, fieldToCompare, rc)
Definition: dplayx_queue.h:181
#define DPQ_FIRST(head)
Definition: dplayx_queue.h:61
#define DPQ_DELETEQ(head, field, type, df)
Definition: dplayx_queue.h:199
#define DPQ_IS_EMPTY(head)
Definition: dplayx_queue.h:64
#define DPQ_REMOVE(head, elm, field)
Definition: dplayx_queue.h:82
#define DPQ_DECL_DELETECB(name, type)
Definition: dplayx_queue.h:211
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
const GLint * first
Definition: glext.h:5794
GLenum GLsizei len
Definition: glext.h:6722
#define debugstr_guid
Definition: kernel32.h:35
static size_t elem
Definition: string.c:68
void NS_AddRemoteComputerAsNameServer(LPCVOID lpcNSAddrHdr, DWORD dwHdrSize, LPCDPMSG_ENUMSESSIONSREPLY lpcMsg, LPVOID lpNSInfo)
Definition: name_server.c:87
void NS_SetLocalComputerAsNameServer(LPCDPSESSIONDESC2 lpsd, LPVOID lpNSInfo)
Definition: name_server.c:74
BOOL NS_InitializeSessionCache(LPVOID *lplpNSInfo)
Definition: name_server.c:263
void NS_InvalidateSessionCache(LPVOID lpNSInfo)
Definition: name_server.c:243
LPVOID NS_GetNSAddr(LPVOID lpNSInfo)
Definition: name_server.c:151
HRESULT NS_SendSessionRequestBroadcast(LPCGUID lpcGuid, DWORD dwFlags, const SPINITDATA *lpSpData)
Definition: name_server.c:196
LPDPSESSIONDESC2 NS_WalkSessions(LPVOID lpNSInfo)
Definition: name_server.c:294
void NS_ReplyToEnumSessionsRequest(const void *lpcMsg, void **lplpReplyData, DWORD *lpdwReplySize, IDirectPlayImpl *lpDP)
Definition: name_server.c:358
void NS_ResetSessionEnumeration(LPVOID lpNSInfo)
Definition: name_server.c:289
void NS_PruneSessionCache(LPVOID lpNSInfo)
Definition: name_server.c:319
struct NSCache * lpNSCache
Definition: name_server.c:66
struct NSCacheData * lpNSCacheData
Definition: name_server.c:54
void NS_SetLocalAddr(LPVOID lpNSInfo, LPCVOID lpHdr, DWORD dwHdrSize)
Definition: name_server.c:184
void NS_DeleteSessionCache(LPVOID lpNSInfo)
Definition: name_server.c:283
DWORD NS_GetNsMagic(LPVOID lpNSInfo)
Definition: name_server.c:177
#define IsEqualGUID(rguid1, rguid2)
Definition: guiddef.h:147
static unsigned __int64 next
Definition: rand_nt.c:6
#define TRACE(s)
Definition: solgame.cpp:4
DWORD dwTime
Definition: solitaire.cpp:27
DirectPlay2Data * dp2
Definition: dplay_global.h:195
lpNSCacheData present
Definition: name_server.c:58
DPMSG_SENDENVELOPE envelope
LPSTR lpszSessionNameA
Definition: dplay.h:244
GUID guidInstance
Definition: dplay.h:234
DWORD dwSize
Definition: dplay.h:232
LPDPSP_ENUMSESSIONS EnumSessions
Definition: dplaysp.h:326
LPDPSESSIONDESC2 lpSessionDesc
Definition: dplay_global.h:157
LPDPSP_SPCALLBACKS lpCB
Definition: dplaysp.h:354
IDirectPlaySP * lpISP
Definition: dplaysp.h:355
DWORD dwSPHeaderSize
Definition: dplaysp.h:360
uint32_t * LPDWORD
Definition: typedefs.h:59
#define CopyMemory
Definition: winbase.h:1710
_In_ PCCERT_CONTEXT _In_ DWORD dwFlags
Definition: wincrypt.h:1176
CONST void * LPCVOID
Definition: windef.h:191
__wchar_t WCHAR
Definition: xmlstorage.h:180
WCHAR * LPWSTR
Definition: xmlstorage.h:184
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
unsigned char BYTE
Definition: xxhash.c:193