Sync up with trunk r61578.
[reactos.git] / dll / directx / wine / dplayx / 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., 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 #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 static DPQ_DECL_DELETECB( cbDeleteNSNodeFromHeap, lpNSCacheData );
71
72 /* Name Server functions
73 * ---------------------
74 */
75 void NS_SetLocalComputerAsNameServer( LPCDPSESSIONDESC2 lpsd, LPVOID lpNSInfo )
76 {
77 lpNSCache lpCache = (lpNSCache)lpNSInfo;
78
79 lpCache->bNsIsLocal = TRUE;
80 }
81
82 static DPQ_DECL_COMPARECB( cbUglyPig, GUID )
83 {
84 return IsEqualGUID( elem1, elem2 );
85 }
86
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 )
92 {
93 DWORD len;
94 lpNSCache lpCache = (lpNSCache)lpNSInfo;
95 lpNSCacheData lpCacheNode;
96
97 TRACE( "%p, %p, %p\n", lpcNSAddrHdr, lpcMsg, lpNSInfo );
98
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 );
102
103 if( lpCacheNode != NULL )
104 {
105 TRACE( "Duplicate session entry for %s removed - updated version kept\n",
106 debugstr_guid( &lpCacheNode->data->guidInstance ) );
107 cbDeleteNSNodeFromHeap( lpCacheNode );
108 }
109
110 /* Add this to the list */
111 lpCacheNode = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof( *lpCacheNode ) );
112
113 if( lpCacheNode == NULL )
114 {
115 ERR( "no memory for NS node\n" );
116 return;
117 }
118
119 lpCacheNode->lpNSAddrHdr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
120 dwHdrSize );
121 CopyMemory( lpCacheNode->lpNSAddrHdr, lpcNSAddrHdr, dwHdrSize );
122
123 lpCacheNode->data = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof( *(lpCacheNode->data) ) );
124
125 if( lpCacheNode->data == NULL )
126 {
127 ERR( "no memory for SESSIONDESC2\n" );
128 HeapFree( GetProcessHeap(), 0, lpCacheNode );
129 return;
130 }
131
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 )))
135 {
136 WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)(lpcMsg+1), -1,
137 lpCacheNode->data->u1.lpszSessionNameA, len, NULL, NULL );
138 }
139
140 lpCacheNode->dwTime = timeGetTime();
141
142 DPQ_INSERT(lpCache->first, lpCacheNode, next );
143
144 lpCache->present = lpCacheNode;
145
146 /* Use this message as an opportunity to weed out any old sessions so
147 * that we don't enum them again
148 */
149 NS_PruneSessionCache( lpNSInfo );
150 }
151
152 LPVOID NS_GetNSAddr( LPVOID lpNSInfo )
153 {
154 lpNSCache lpCache = (lpNSCache)lpNSInfo;
155
156 FIXME( ":quick stub\n" );
157
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.
164 */
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
172 }
173
174 /* Get the magic number associated with the Name Server */
175 DWORD NS_GetNsMagic( LPVOID lpNSInfo )
176 {
177 LPDWORD lpHdrInfo = NS_GetNSAddr( lpNSInfo );
178
179 return lpHdrInfo[1];
180 }
181
182 void NS_SetLocalAddr( LPVOID lpNSInfo, LPCVOID lpHdr, DWORD dwHdrSize )
183 {
184 lpNSCache lpCache = (lpNSCache)lpNSInfo;
185
186 lpCache->lpLocalAddrHdr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, dwHdrSize );
187
188 CopyMemory( lpCache->lpLocalAddrHdr, lpHdr, dwHdrSize );
189 }
190
191 /* This function is responsible for sending a request for all other known
192 nameservers to send us what sessions they have registered locally
193 */
194 HRESULT NS_SendSessionRequestBroadcast( LPCGUID lpcGuid,
195 DWORD dwFlags,
196 const SPINITDATA *lpSpData )
197
198 {
199 DPSP_ENUMSESSIONSDATA data;
200 LPDPMSG_ENUMSESSIONSREQUEST lpMsg;
201
202 TRACE( "enumerating for guid %s\n", debugstr_guid( lpcGuid ) );
203
204 /* Get the SP to deal with sending the EnumSessions request */
205 FIXME( ": not all data fields are correct\n" );
206
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;
212
213
214 lpMsg = (LPDPMSG_ENUMSESSIONSREQUEST)(((BYTE*)data.lpMessage)+lpSpData->dwSPHeaderSize);
215
216 /* Setup EnumSession request message */
217 lpMsg->envelope.dwMagic = DPMSGMAGIC_DPLAYMSG;
218 lpMsg->envelope.wCommandId = DPMSGCMD_ENUMSESSIONSREQUEST;
219 lpMsg->envelope.wVersion = DPMSGVER_DP6;
220
221 lpMsg->dwPasswordSize = 0; /* FIXME: If enumerating passwords..? */
222 lpMsg->dwFlags = dwFlags;
223
224 lpMsg->guidApplication = *lpcGuid;
225
226 return (lpSpData->lpCB->EnumSessions)( &data );
227 }
228
229 /* Delete a name server node which has been allocated on the heap */
230 static DPQ_DECL_DELETECB( cbDeleteNSNodeFromHeap, lpNSCacheData )
231 {
232 /* NOTE: This proc doesn't deal with the walking pointer */
233
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 );
238 }
239
240 /* Render all data in a session cache invalid */
241 void NS_InvalidateSessionCache( LPVOID lpNSInfo )
242 {
243 lpNSCache lpCache = (lpNSCache)lpNSInfo;
244
245 if( lpCache == NULL )
246 {
247 ERR( ": invalidate nonexistent cache\n" );
248 return;
249 }
250
251 DPQ_DELETEQ( lpCache->first, next, lpNSCacheData, cbDeleteNSNodeFromHeap );
252
253 /* NULL out the walking pointer */
254 lpCache->present = NULL;
255
256 lpCache->bNsIsLocal = FALSE;
257
258 }
259
260 /* Create and initialize a session cache */
261 BOOL NS_InitializeSessionCache( LPVOID* lplpNSInfo )
262 {
263 lpNSCache lpCache = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof( *lpCache ) );
264
265 *lplpNSInfo = lpCache;
266
267 if( lpCache == NULL )
268 {
269 return FALSE;
270 }
271
272 DPQ_INIT(lpCache->first);
273 lpCache->present = NULL;
274
275 lpCache->bNsIsLocal = FALSE;
276
277 return TRUE;
278 }
279
280 /* Delete a session cache */
281 void NS_DeleteSessionCache( LPVOID lpNSInfo )
282 {
283 NS_InvalidateSessionCache( (lpNSCache)lpNSInfo );
284 }
285
286 /* Reinitialize the present pointer for this cache */
287 void NS_ResetSessionEnumeration( LPVOID lpNSInfo )
288 {
289 ((lpNSCache)lpNSInfo)->present = ((lpNSCache)lpNSInfo)->first.lpQHFirst;
290 }
291
292 LPDPSESSIONDESC2 NS_WalkSessions( LPVOID lpNSInfo )
293 {
294 LPDPSESSIONDESC2 lpSessionDesc;
295 lpNSCache lpCache = (lpNSCache)lpNSInfo;
296
297 /* FIXME: The pointers could disappear when walking if a prune happens */
298
299 /* Test for end of the list */
300 if( lpCache->present == NULL )
301 {
302 return NULL;
303 }
304
305 lpSessionDesc = lpCache->present->data;
306
307 /* Advance tracking pointer */
308 lpCache->present = lpCache->present->next.lpQNext;
309
310 return lpSessionDesc;
311 }
312
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.
315 */
316 /* FIXME: This needs to be called by some periodic timer */
317 void NS_PruneSessionCache( LPVOID lpNSInfo )
318 {
319 lpNSCache lpCache = lpNSInfo;
320
321 const DWORD dwPresentTime = timeGetTime();
322 const DWORD dwPrunePeriod = DPMSG_WAIT_60_SECS; /* is 60 secs enough? */
323
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.
329 */
330 for( ;; )
331 {
332 lpNSCacheData lpFirstData;
333
334 if( DPQ_IS_EMPTY(lpCache->first) )
335 {
336 /* Nothing to prune */
337 break;
338 }
339
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 )
343 {
344 /* First entry has not expired yet; don't prune */
345 break;
346 }
347
348 lpFirstData = DPQ_FIRST(lpCache->first);
349 DPQ_REMOVE( lpCache->first, DPQ_FIRST(lpCache->first), next );
350 cbDeleteNSNodeFromHeap( lpFirstData );
351 }
352
353 }
354
355 /* NAME SERVER Message stuff */
356 void NS_ReplyToEnumSessionsRequest( const void *lpcMsg, void **lplpReplyData, DWORD *lpdwReplySize,
357 IDirectPlayImpl *lpDP )
358 {
359 LPDPMSG_ENUMSESSIONSREPLY rmsg;
360 DWORD dwVariableSize;
361 DWORD dwVariableLen;
362 /* LPCDPMSG_ENUMSESSIONSREQUEST msg = (LPDPMSG_ENUMSESSIONSREQUEST)lpcMsg; */
363
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" );
366
367 dwVariableLen = MultiByteToWideChar( CP_ACP, 0,
368 lpDP->dp2->lpSessionDesc->u1.lpszSessionNameA,
369 -1, NULL, 0 );
370 dwVariableSize = dwVariableLen * sizeof( WCHAR );
371
372 *lpdwReplySize = lpDP->dp2->spData.dwSPHeaderSize +
373 sizeof( *rmsg ) + dwVariableSize;
374 *lplpReplyData = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
375 *lpdwReplySize );
376
377 rmsg = (LPDPMSG_ENUMSESSIONSREPLY)( (BYTE*)*lplpReplyData +
378 lpDP->dp2->spData.dwSPHeaderSize);
379
380 rmsg->envelope.dwMagic = DPMSGMAGIC_DPLAYMSG;
381 rmsg->envelope.wCommandId = DPMSGCMD_ENUMSESSIONSREPLY;
382 rmsg->envelope.wVersion = DPMSGVER_DP6;
383
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 );
389 }