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