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