1 /* DPLAYX.DLL name server implementation
3 * Copyright 2000-2001 - Peter Hunnisett
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.
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.
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
20 /* NOTE: Methods with the NS_ prefix are name server methods */
25 #define NONAMELESSUNION
30 #include "wine/unicode.h"
31 #include "wine/debug.h"
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"
40 /* FIXME: Need to create a crit section, store and use it */
42 WINE_DEFAULT_DEBUG_CHANNEL(dplay
);
44 /* NS specific structures */
47 DPQ_ENTRY(NSCacheData
) next
;
49 DWORD dwTime
; /* Time at which data was last known valid */
50 LPDPSESSIONDESC2 data
;
55 typedef struct NSCacheData NSCacheData
, *lpNSCacheData
;
59 lpNSCacheData present
; /* keep track of what is to be looked at when walking */
61 DPQ_HEAD(NSCacheData
) first
;
64 LPVOID lpLocalAddrHdr
; /* FIXME: Not yet used */
65 LPVOID lpRemoteAddrHdr
; /* FIXME: Not yet used */
67 typedef struct NSCache NSCache
, *lpNSCache
;
69 /* Function prototypes */
70 static DPQ_DECL_DELETECB( cbDeleteNSNodeFromHeap
, lpNSCacheData
);
72 /* Name Server functions
73 * ---------------------
75 void NS_SetLocalComputerAsNameServer( LPCDPSESSIONDESC2 lpsd
, LPVOID lpNSInfo
)
77 lpNSCache lpCache
= (lpNSCache
)lpNSInfo
;
79 lpCache
->bNsIsLocal
= TRUE
;
82 static DPQ_DECL_COMPARECB( cbUglyPig
, GUID
)
84 return IsEqualGUID( elem1
, elem2
);
87 /* Store the given NS remote address for future reference */
88 void NS_AddRemoteComputerAsNameServer( LPCVOID lpcNSAddrHdr
,
90 LPCDPMSG_ENUMSESSIONSREPLY lpcMsg
,
94 lpNSCache lpCache
= (lpNSCache
)lpNSInfo
;
95 lpNSCacheData lpCacheNode
;
97 TRACE( "%p, %p, %p\n", lpcNSAddrHdr
, lpcMsg
, lpNSInfo
);
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
);
103 if( lpCacheNode
!= NULL
)
105 TRACE( "Duplicate session entry for %s removed - updated version kept\n",
106 debugstr_guid( &lpCacheNode
->data
->guidInstance
) );
107 cbDeleteNSNodeFromHeap( lpCacheNode
);
110 /* Add this to the list */
111 lpCacheNode
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof( *lpCacheNode
) );
113 if( lpCacheNode
== NULL
)
115 ERR( "no memory for NS node\n" );
119 lpCacheNode
->lpNSAddrHdr
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
121 CopyMemory( lpCacheNode
->lpNSAddrHdr
, lpcNSAddrHdr
, dwHdrSize
);
123 lpCacheNode
->data
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof( *(lpCacheNode
->data
) ) );
125 if( lpCacheNode
->data
== NULL
)
127 ERR( "no memory for SESSIONDESC2\n" );
128 HeapFree( GetProcessHeap(), 0, lpCacheNode
);
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
)))
136 WideCharToMultiByte( CP_ACP
, 0, (LPCWSTR
)(lpcMsg
+1), -1,
137 lpCacheNode
->data
->u1
.lpszSessionNameA
, len
, NULL
, NULL
);
140 lpCacheNode
->dwTime
= timeGetTime();
142 DPQ_INSERT(lpCache
->first
, lpCacheNode
, next
);
144 lpCache
->present
= lpCacheNode
;
146 /* Use this message as an opportunity to weed out any old sessions so
147 * that we don't enum them again
149 NS_PruneSessionCache( lpNSInfo
);
152 LPVOID
NS_GetNSAddr( LPVOID lpNSInfo
)
154 lpNSCache lpCache
= (lpNSCache
)lpNSInfo
;
156 FIXME( ":quick stub\n" );
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
166 if ( lpCache
->first
.lpQHFirst
)
167 return lpCache
->first
.lpQHFirst
->lpNSAddrHdr
;
171 /* FIXME: Should convert over to this */
172 return lpCache
->bNsIsLocal
? lpCache
->lpLocalAddrHdr
173 : lpCache
->lpRemoteAddrHdr
;
177 /* Get the magic number associated with the Name Server */
178 DWORD
NS_GetNsMagic( LPVOID lpNSInfo
)
180 LPDWORD lpHdrInfo
= NS_GetNSAddr( lpNSInfo
);
185 void NS_SetLocalAddr( LPVOID lpNSInfo
, LPCVOID lpHdr
, DWORD dwHdrSize
)
187 lpNSCache lpCache
= (lpNSCache
)lpNSInfo
;
189 lpCache
->lpLocalAddrHdr
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, dwHdrSize
);
191 CopyMemory( lpCache
->lpLocalAddrHdr
, lpHdr
, dwHdrSize
);
194 /* This function is responsible for sending a request for all other known
195 nameservers to send us what sessions they have registered locally
197 HRESULT
NS_SendSessionRequestBroadcast( LPCGUID lpcGuid
,
199 const SPINITDATA
*lpSpData
)
202 DPSP_ENUMSESSIONSDATA data
;
203 LPDPMSG_ENUMSESSIONSREQUEST lpMsg
;
205 TRACE( "enumerating for guid %s\n", debugstr_guid( lpcGuid
) );
207 /* Get the SP to deal with sending the EnumSessions request */
208 FIXME( ": not all data fields are correct\n" );
210 data
.dwMessageSize
= lpSpData
->dwSPHeaderSize
+ sizeof( *lpMsg
); /*FIXME!*/
211 data
.lpMessage
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
212 data
.dwMessageSize
);
213 data
.lpISP
= lpSpData
->lpISP
;
214 data
.bReturnStatus
= (dwFlags
& DPENUMSESSIONS_RETURNSTATUS
) != 0;
217 lpMsg
= (LPDPMSG_ENUMSESSIONSREQUEST
)(((BYTE
*)data
.lpMessage
)+lpSpData
->dwSPHeaderSize
);
219 /* Setup EnumSession request message */
220 lpMsg
->envelope
.dwMagic
= DPMSGMAGIC_DPLAYMSG
;
221 lpMsg
->envelope
.wCommandId
= DPMSGCMD_ENUMSESSIONSREQUEST
;
222 lpMsg
->envelope
.wVersion
= DPMSGVER_DP6
;
224 lpMsg
->dwPasswordSize
= 0; /* FIXME: If enumerating passwords..? */
225 lpMsg
->dwFlags
= dwFlags
;
227 lpMsg
->guidApplication
= *lpcGuid
;
229 return (lpSpData
->lpCB
->EnumSessions
)( &data
);
232 /* Delete a name server node which has been allocated on the heap */
233 static DPQ_DECL_DELETECB( cbDeleteNSNodeFromHeap
, lpNSCacheData
)
235 /* NOTE: This proc doesn't deal with the walking pointer */
237 /* FIXME: Memory leak on data (contained ptrs) */
238 HeapFree( GetProcessHeap(), 0, elem
->data
);
239 HeapFree( GetProcessHeap(), 0, elem
->lpNSAddrHdr
);
240 HeapFree( GetProcessHeap(), 0, elem
);
243 /* Render all data in a session cache invalid */
244 void NS_InvalidateSessionCache( LPVOID lpNSInfo
)
246 lpNSCache lpCache
= (lpNSCache
)lpNSInfo
;
248 if( lpCache
== NULL
)
250 ERR( ": invalidate nonexistent cache\n" );
254 DPQ_DELETEQ( lpCache
->first
, next
, lpNSCacheData
, cbDeleteNSNodeFromHeap
);
256 /* NULL out the walking pointer */
257 lpCache
->present
= NULL
;
259 lpCache
->bNsIsLocal
= FALSE
;
263 /* Create and initialize a session cache */
264 BOOL
NS_InitializeSessionCache( LPVOID
* lplpNSInfo
)
266 lpNSCache lpCache
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof( *lpCache
) );
268 *lplpNSInfo
= lpCache
;
270 if( lpCache
== NULL
)
275 DPQ_INIT(lpCache
->first
);
276 lpCache
->present
= NULL
;
278 lpCache
->bNsIsLocal
= FALSE
;
283 /* Delete a session cache */
284 void NS_DeleteSessionCache( LPVOID lpNSInfo
)
286 NS_InvalidateSessionCache( (lpNSCache
)lpNSInfo
);
289 /* Reinitialize the present pointer for this cache */
290 void NS_ResetSessionEnumeration( LPVOID lpNSInfo
)
292 ((lpNSCache
)lpNSInfo
)->present
= ((lpNSCache
)lpNSInfo
)->first
.lpQHFirst
;
295 LPDPSESSIONDESC2
NS_WalkSessions( LPVOID lpNSInfo
)
297 LPDPSESSIONDESC2 lpSessionDesc
;
298 lpNSCache lpCache
= (lpNSCache
)lpNSInfo
;
300 /* FIXME: The pointers could disappear when walking if a prune happens */
302 /* Test for end of the list */
303 if( lpCache
->present
== NULL
)
308 lpSessionDesc
= lpCache
->present
->data
;
310 /* Advance tracking pointer */
311 lpCache
->present
= lpCache
->present
->next
.lpQNext
;
313 return lpSessionDesc
;
316 /* This method should check to see if there are any sessions which are
317 * older than the criteria. If so, just delete that information.
319 /* FIXME: This needs to be called by some periodic timer */
320 void NS_PruneSessionCache( LPVOID lpNSInfo
)
322 lpNSCache lpCache
= lpNSInfo
;
324 const DWORD dwPresentTime
= timeGetTime();
325 const DWORD dwPrunePeriod
= DPMSG_WAIT_60_SECS
; /* is 60 secs enough? */
327 /* This silly little algorithm is based on the fact we keep entries in
328 * the queue in a time based order. It also assumes that it is not possible
329 * to wrap around over yourself (which is not unreasonable).
330 * The if statements verify if the first entry in the queue is less
331 * than dwPrunePeriod old depending on the "clock" roll over.
335 lpNSCacheData lpFirstData
;
337 if( DPQ_IS_EMPTY(lpCache
->first
) )
339 /* Nothing to prune */
343 /* Deal with time in a wrap around safe manner - unsigned arithmetic.
344 * Check the difference in time */
345 if( (dwPresentTime
- (DPQ_FIRST(lpCache
->first
)->dwTime
)) < dwPrunePeriod
)
347 /* First entry has not expired yet; don't prune */
351 lpFirstData
= DPQ_FIRST(lpCache
->first
);
352 DPQ_REMOVE( lpCache
->first
, DPQ_FIRST(lpCache
->first
), next
);
353 cbDeleteNSNodeFromHeap( lpFirstData
);
358 /* NAME SERVER Message stuff */
359 void NS_ReplyToEnumSessionsRequest( const void *lpcMsg
, void **lplpReplyData
, DWORD
*lpdwReplySize
,
360 IDirectPlayImpl
*lpDP
)
362 LPDPMSG_ENUMSESSIONSREPLY rmsg
;
363 DWORD dwVariableSize
;
365 /* LPCDPMSG_ENUMSESSIONSREQUEST msg = (LPDPMSG_ENUMSESSIONSREQUEST)lpcMsg; */
367 /* FIXME: Should handle ANSI or WIDECHAR input. Currently just ANSI input */
368 FIXME( ": few fixed + need to check request for response, might need UNICODE input ability.\n" );
370 dwVariableLen
= MultiByteToWideChar( CP_ACP
, 0,
371 lpDP
->dp2
->lpSessionDesc
->u1
.lpszSessionNameA
,
373 dwVariableSize
= dwVariableLen
* sizeof( WCHAR
);
375 *lpdwReplySize
= lpDP
->dp2
->spData
.dwSPHeaderSize
+
376 sizeof( *rmsg
) + dwVariableSize
;
377 *lplpReplyData
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
380 rmsg
= (LPDPMSG_ENUMSESSIONSREPLY
)( (BYTE
*)*lplpReplyData
+
381 lpDP
->dp2
->spData
.dwSPHeaderSize
);
383 rmsg
->envelope
.dwMagic
= DPMSGMAGIC_DPLAYMSG
;
384 rmsg
->envelope
.wCommandId
= DPMSGCMD_ENUMSESSIONSREPLY
;
385 rmsg
->envelope
.wVersion
= DPMSGVER_DP6
;
387 CopyMemory( &rmsg
->sd
, lpDP
->dp2
->lpSessionDesc
,
388 lpDP
->dp2
->lpSessionDesc
->dwSize
);
389 rmsg
->dwUnknown
= 0x0000005c;
390 MultiByteToWideChar( CP_ACP
, 0, lpDP
->dp2
->lpSessionDesc
->u1
.lpszSessionNameA
, -1,
391 (LPWSTR
)(rmsg
+1), dwVariableLen
);