Wine-0_9_1 vendor import
[reactos.git] / reactos / lib / dplayx / current / name_server.c
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 #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"
33
34 #include "dplayx_global.h"
35 #include "name_server.h"
36 #include "dplaysp.h"
37 #include "dplayx_messages.h"
38 #include "dplayx_queue.h"
39
40 /* FIXME: Need to create a crit section, store and use it */
41
42 WINE_DEFAULT_DEBUG_CHANNEL(dplay);
43
44 /* NS specific structures */
45 struct NSCacheData
46 {
47 DPQ_ENTRY(NSCacheData) next;
48
49 DWORD dwTime; /* Time at which data was last known valid */
50 LPDPSESSIONDESC2 data;
51
52 LPVOID lpNSAddrHdr;
53
54 };
55 typedef struct NSCacheData NSCacheData, *lpNSCacheData;
56
57 struct NSCache
58 {
59 lpNSCacheData present; /* keep track of what is to be looked at when walking */
60
61 DPQ_HEAD(NSCacheData) first;
62
63 BOOL bNsIsLocal;
64 LPVOID lpLocalAddrHdr; /* FIXME: Not yet used */
65 LPVOID lpRemoteAddrHdr; /* FIXME: Not yet used */
66 };
67 typedef struct NSCache NSCache, *lpNSCache;
68
69 /* Function prototypes */
70 DPQ_DECL_DELETECB( cbDeleteNSNodeFromHeap, lpNSCacheData );
71
72 /* Name Server functions
73 * ---------------------
74 */
75 void NS_SetLocalComputerAsNameServer( LPCDPSESSIONDESC2 lpsd, LPVOID lpNSInfo )
76 {
77 #if 0
78 /* FIXME: Remove this method? */
79 DPLAYX_SetLocalSession( lpsd );
80 #endif
81 lpNSCache lpCache = (lpNSCache)lpNSInfo;
82
83 lpCache->bNsIsLocal = TRUE;
84 }
85
86 void NS_SetRemoteComputerAsNameServer( LPCDPSESSIONDESC2 lpsd, LPVOID lpNSInfo )
87 {
88 lpNSCache lpCache = (lpNSCache)lpNSInfo;
89
90 lpCache->bNsIsLocal = FALSE;
91 }
92
93 static DPQ_DECL_COMPARECB( cbUglyPig, GUID )
94 {
95 return IsEqualGUID( elem1, elem2 );
96 }
97
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 )
104 {
105 DWORD len;
106 lpNSCache lpCache = (lpNSCache)lpNSInfo;
107 lpNSCacheData lpCacheNode;
108
109 TRACE( "%p, %p, %p\n", lpcNSAddrHdr, lpMsg, lpNSInfo );
110
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 );
114
115 if( lpCacheNode != NULL )
116 {
117 TRACE( "Duplicate session entry for %s removed - updated version kept\n",
118 debugstr_guid( &lpCacheNode->data->guidInstance ) );
119 cbDeleteNSNodeFromHeap( lpCacheNode );
120 }
121
122 /* Add this to the list */
123 lpCacheNode = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof( *lpCacheNode ) );
124
125 if( lpCacheNode == NULL )
126 {
127 ERR( "no memory for NS node\n" );
128 return;
129 }
130
131 lpCacheNode->lpNSAddrHdr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
132 dwHdrSize );
133 CopyMemory( lpCacheNode->lpNSAddrHdr, lpcNSAddrHdr, dwHdrSize );
134
135 lpCacheNode->data = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof( *(lpCacheNode->data) ) );
136
137 if( lpCacheNode->data == NULL )
138 {
139 ERR( "no memory for SESSIONDESC2\n" );
140 return;
141 }
142
143 CopyMemory( lpCacheNode->data, &lpMsg->sd, sizeof( *lpCacheNode->data ) );
144 len = WideCharToMultiByte( CP_ACP, 0, (LPWSTR)(lpMsg+1), -1, NULL, 0, NULL, NULL );
145 if ((lpCacheNode->data->u1.lpszSessionNameA = HeapAlloc( GetProcessHeap(), 0, len )))
146 {
147 WideCharToMultiByte( CP_ACP, 0, (LPWSTR)(lpMsg+1), -1,
148 lpCacheNode->data->u1.lpszSessionNameA, len, NULL, NULL );
149 }
150
151 lpCacheNode->dwTime = timeGetTime();
152
153 DPQ_INSERT(lpCache->first, lpCacheNode, next );
154
155 lpCache->present = lpCacheNode;
156
157 /* Use this message as an opportunity to weed out any old sessions so
158 * that we don't enum them again
159 */
160 NS_PruneSessionCache( lpNSInfo );
161 }
162
163 LPVOID NS_GetNSAddr( LPVOID lpNSInfo )
164 {
165 lpNSCache lpCache = (lpNSCache)lpNSInfo;
166
167 FIXME( ":quick stub\n" );
168
169 /* Ok. Cheat and don't search for the correct stuff just take the first.
170 * FIXME: In the future how are we to know what is _THE_ enum we used?
171 * This is going to have to go into dplay somehow. Perhaps it
172 * comes back with app server id for the join command! Oh...that
173 * must be it. That would make this method obsolete once that's
174 * in place.
175 */
176 #if 1
177 return lpCache->first.lpQHFirst->lpNSAddrHdr;
178 #else
179 /* FIXME: Should convert over to this */
180 return lpCache->bNsIsLocal ? lpCache->lpLocalAddrHdr
181 : lpCache->lpRemoteAddrHdr;
182 #endif
183 }
184
185 /* Get the magic number associated with the Name Server */
186 DWORD NS_GetNsMagic( LPVOID lpNSInfo )
187 {
188 LPDWORD lpHdrInfo = (LPDWORD)NS_GetNSAddr( lpNSInfo );
189
190 return lpHdrInfo[1];
191 }
192
193 /* Get the magic number associated with the non NS end */
194 DWORD NS_GetOtherMagic( LPVOID lpNSInfo )
195 {
196 lpNSCache lpCache = (lpNSCache)lpNSInfo;
197
198 return ((LPDWORD)lpCache->lpLocalAddrHdr)[1];
199 }
200
201 void NS_SetLocalAddr( LPVOID lpNSInfo, LPCVOID lpHdr, DWORD dwHdrSize )
202 {
203 lpNSCache lpCache = (lpNSCache)lpNSInfo;
204
205 lpCache->lpLocalAddrHdr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, dwHdrSize );
206
207 CopyMemory( lpCache->lpLocalAddrHdr, lpHdr, dwHdrSize );
208 }
209
210 /* This function is responsible for sending a request for all other known
211 nameservers to send us what sessions they have registered locally
212 */
213 HRESULT NS_SendSessionRequestBroadcast( LPCGUID lpcGuid,
214 DWORD dwFlags,
215 LPSPINITDATA lpSpData )
216
217 {
218 DPSP_ENUMSESSIONSDATA data;
219 LPDPMSG_ENUMSESSIONSREQUEST lpMsg;
220
221 TRACE( "enumerating for guid %s\n", debugstr_guid( lpcGuid ) );
222
223 /* Get the SP to deal with sending the EnumSessions request */
224 FIXME( ": not all data fields are correct\n" );
225
226 data.dwMessageSize = lpSpData->dwSPHeaderSize + sizeof( *lpMsg ); /*FIXME!*/
227 data.lpMessage = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
228 data.dwMessageSize );
229 data.lpISP = lpSpData->lpISP;
230 data.bReturnStatus = (dwFlags & DPENUMSESSIONS_RETURNSTATUS) ? TRUE : FALSE;
231
232
233 lpMsg = (LPDPMSG_ENUMSESSIONSREQUEST)(((BYTE*)data.lpMessage)+lpSpData->dwSPHeaderSize);
234
235 /* Setup EnumSession request message */
236 lpMsg->envelope.dwMagic = DPMSGMAGIC_DPLAYMSG;
237 lpMsg->envelope.wCommandId = DPMSGCMD_ENUMSESSIONSREQUEST;
238 lpMsg->envelope.wVersion = DPMSGVER_DP6;
239
240 lpMsg->dwPasswordSize = 0; /* FIXME: If enumerating passwords..? */
241 lpMsg->dwFlags = dwFlags;
242
243 CopyMemory( &lpMsg->guidApplication, lpcGuid, sizeof( *lpcGuid ) );
244
245 return (lpSpData->lpCB->EnumSessions)( &data );
246 }
247
248 /* Delete a name server node which has been allocated on the heap */
249 DPQ_DECL_DELETECB( cbDeleteNSNodeFromHeap, lpNSCacheData )
250 {
251 /* NOTE: This proc doesn't deal with the walking pointer */
252
253 /* FIXME: Memory leak on data (contained ptrs) */
254 HeapFree( GetProcessHeap(), 0, elem->data );
255 HeapFree( GetProcessHeap(), 0, elem->lpNSAddrHdr );
256 HeapFree( GetProcessHeap(), 0, elem );
257 }
258
259 /* Render all data in a session cache invalid */
260 void NS_InvalidateSessionCache( LPVOID lpNSInfo )
261 {
262 lpNSCache lpCache = (lpNSCache)lpNSInfo;
263
264 if( lpCache == NULL )
265 {
266 ERR( ": invalidate nonexistent cache\n" );
267 return;
268 }
269
270 DPQ_DELETEQ( lpCache->first, next, lpNSCacheData, cbDeleteNSNodeFromHeap );
271
272 /* NULL out the walking pointer */
273 lpCache->present = NULL;
274
275 lpCache->bNsIsLocal = FALSE;
276
277 }
278
279 /* Create and initialize a session cache */
280 BOOL NS_InitializeSessionCache( LPVOID* lplpNSInfo )
281 {
282 lpNSCache lpCache = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof( *lpCache ) );
283
284 *lplpNSInfo = lpCache;
285
286 if( lpCache == NULL )
287 {
288 return FALSE;
289 }
290
291 DPQ_INIT(lpCache->first);
292 lpCache->present = NULL;
293
294 lpCache->bNsIsLocal = FALSE;
295
296 return TRUE;
297 }
298
299 /* Delete a session cache */
300 void NS_DeleteSessionCache( LPVOID lpNSInfo )
301 {
302 NS_InvalidateSessionCache( (lpNSCache)lpNSInfo );
303 }
304
305 /* Reinitialize the present pointer for this cache */
306 void NS_ResetSessionEnumeration( LPVOID lpNSInfo )
307 {
308 ((lpNSCache)lpNSInfo)->present = ((lpNSCache)lpNSInfo)->first.lpQHFirst;
309 }
310
311 LPDPSESSIONDESC2 NS_WalkSessions( LPVOID lpNSInfo )
312 {
313 LPDPSESSIONDESC2 lpSessionDesc;
314 lpNSCache lpCache = (lpNSCache)lpNSInfo;
315
316 /* FIXME: The pointers could disappear when walking if a prune happens */
317
318 /* Test for end of the list */
319 if( lpCache->present == NULL )
320 {
321 return NULL;
322 }
323
324 lpSessionDesc = lpCache->present->data;
325
326 /* Advance tracking pointer */
327 lpCache->present = lpCache->present->next.lpQNext;
328
329 return lpSessionDesc;
330 }
331
332 /* This method should check to see if there are any sessions which are
333 * older than the criteria. If so, just delete that information.
334 */
335 /* FIXME: This needs to be called by some periodic timer */
336 void NS_PruneSessionCache( LPVOID lpNSInfo )
337 {
338 lpNSCache lpCache = lpNSInfo;
339
340 const DWORD dwPresentTime = timeGetTime();
341 const DWORD dwPrunePeriod = DPMSG_WAIT_60_SECS; /* is 60 secs enough? */
342
343 /* This silly little algorithm is based on the fact we keep entries in
344 * the queue in a time based order. It also assumes that it is not possible
345 * to wrap around over yourself (which is not unreasonable).
346 * The if statements verify if the first entry in the queue is less
347 * than dwPrunePeriod old depending on the "clock" roll over.
348 */
349 for( ;; )
350 {
351 lpNSCacheData lpFirstData;
352
353 if( DPQ_IS_EMPTY(lpCache->first) )
354 {
355 /* Nothing to prune */
356 break;
357 }
358
359 /* Deal with time in a wrap around safe manner - unsigned arithmetic.
360 * Check the difference in time */
361 if( (dwPresentTime - (DPQ_FIRST(lpCache->first)->dwTime)) < dwPrunePeriod )
362 {
363 /* First entry has not expired yet; don't prune */
364 break;
365 }
366
367 lpFirstData = DPQ_FIRST(lpCache->first);
368 DPQ_REMOVE( lpCache->first, DPQ_FIRST(lpCache->first), next );
369 cbDeleteNSNodeFromHeap( lpFirstData );
370 }
371
372 }
373
374 /* NAME SERVER Message stuff */
375 void NS_ReplyToEnumSessionsRequest( LPCVOID lpcMsg,
376 LPVOID* lplpReplyData,
377 LPDWORD lpdwReplySize,
378 IDirectPlay2Impl* lpDP )
379 {
380 LPDPMSG_ENUMSESSIONSREPLY rmsg;
381 DWORD dwVariableSize;
382 DWORD dwVariableLen;
383 /* LPCDPMSG_ENUMSESSIONSREQUEST msg = (LPDPMSG_ENUMSESSIONSREQUEST)lpcMsg; */
384 BOOL bAnsi = TRUE; /* FIXME: This needs to be in the DPLAY interface */
385
386 FIXME( ": few fixed + need to check request for response\n" );
387
388 if (bAnsi)
389 {
390 dwVariableLen = MultiByteToWideChar( CP_ACP, 0,
391 lpDP->dp2->lpSessionDesc->u1.lpszSessionNameA,
392 -1, NULL, 0 );
393 }
394 else
395 {
396 dwVariableLen = strlenW( lpDP->dp2->lpSessionDesc->u1.lpszSessionName ) + 1;
397 }
398
399 dwVariableSize = dwVariableLen * sizeof( WCHAR );
400
401 *lpdwReplySize = lpDP->dp2->spData.dwSPHeaderSize +
402 sizeof( *rmsg ) + dwVariableSize;
403 *lplpReplyData = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
404 *lpdwReplySize );
405
406 rmsg = (LPDPMSG_ENUMSESSIONSREPLY)( (BYTE*)*lplpReplyData +
407 lpDP->dp2->spData.dwSPHeaderSize);
408
409 rmsg->envelope.dwMagic = DPMSGMAGIC_DPLAYMSG;
410 rmsg->envelope.wCommandId = DPMSGCMD_ENUMSESSIONSREPLY;
411 rmsg->envelope.wVersion = DPMSGVER_DP6;
412
413 CopyMemory( &rmsg->sd, lpDP->dp2->lpSessionDesc,
414 sizeof( lpDP->dp2->lpSessionDesc->dwSize ) );
415 rmsg->dwUnknown = 0x0000005c;
416 if( bAnsi )
417 {
418 MultiByteToWideChar( CP_ACP, 0, lpDP->dp2->lpSessionDesc->u1.lpszSessionNameA, -1,
419 (LPWSTR)(rmsg+1), dwVariableLen );
420 }
421 else
422 {
423 strcpyW( (LPWSTR)(rmsg+1), lpDP->dp2->lpSessionDesc->u1.lpszSessionName );
424 }
425 }