1 /* DPLAYX.DLL name server implementation
3 * Copyright 2000-2001 - Peter Hunnisett
5 * <presently under construction - contact hunnise@nortelnetworks.com>
9 /* NOTE: Methods with the NS_ prefix are name server methods */
14 #include "wine/unicode.h"
15 #include "debugtools.h"
18 #include "dplayx_global.h"
19 #include "name_server.h"
21 #include "dplayx_messages.h"
22 #include "dplayx_queue.h"
24 /* FIXME: Need to create a crit section, store and use it */
26 DEFAULT_DEBUG_CHANNEL(dplay
);
28 /* NS specific structures */
31 DPQ_ENTRY(NSCacheData
) next
;
33 DWORD dwTime
; /* Time at which data was last known valid */
34 LPDPSESSIONDESC2 data
;
39 typedef struct NSCacheData NSCacheData
, *lpNSCacheData
;
43 lpNSCacheData present
; /* keep track of what is to be looked at when walking */
45 DPQ_HEAD(NSCacheData
) first
;
48 LPVOID lpLocalAddrHdr
; /* FIXME: Not yet used */
49 LPVOID lpRemoteAddrHdr
; /* FIXME: Not yet used */
51 typedef struct NSCache NSCache
, *lpNSCache
;
53 /* Function prototypes */
54 DPQ_DECL_DELETECB( cbDeleteNSNodeFromHeap
, lpNSCacheData
);
56 /* Name Server functions
57 * ---------------------
59 void NS_SetLocalComputerAsNameServer( LPCDPSESSIONDESC2 lpsd
, LPVOID lpNSInfo
)
62 /* FIXME: Remove this method? */
63 DPLAYX_SetLocalSession( lpsd
);
65 lpNSCache lpCache
= (lpNSCache
)lpNSInfo
;
67 lpCache
->bNsIsLocal
= TRUE
;
70 void NS_SetRemoteComputerAsNameServer( LPCDPSESSIONDESC2 lpsd
, LPVOID lpNSInfo
)
72 lpNSCache lpCache
= (lpNSCache
)lpNSInfo
;
74 lpCache
->bNsIsLocal
= FALSE
;
77 DPQ_DECL_COMPARECB( cbUglyPig
, GUID
)
79 return IsEqualGUID( elem1
, elem2
);
82 /* Store the given NS remote address for future reference */
83 /* FIXME: LPDPMSG_ENUMSESSIONSREPLY should be const */
84 void NS_AddRemoteComputerAsNameServer( LPCVOID lpcNSAddrHdr
,
86 LPDPMSG_ENUMSESSIONSREPLY lpMsg
,
90 lpNSCache lpCache
= (lpNSCache
)lpNSInfo
;
91 lpNSCacheData lpCacheNode
;
93 TRACE( "%p, %p, %p\n", lpcNSAddrHdr
, lpMsg
, lpNSInfo
);
95 /* See if we can find this session. If we can, remove it as it's a dup */
96 DPQ_REMOVE_ENTRY_CB( lpCache
->first
, next
, data
->guidInstance
, cbUglyPig
,
97 lpMsg
->sd
.guidInstance
, lpCacheNode
);
99 if( lpCacheNode
!= NULL
)
101 TRACE( "Duplicate session entry for %s removed - updated version kept\n",
102 debugstr_guid( &lpCacheNode
->data
->guidInstance
) );
103 cbDeleteNSNodeFromHeap( lpCacheNode
);
106 /* Add this to the list */
107 lpCacheNode
= (lpNSCacheData
)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
108 sizeof( *lpCacheNode
) );
110 if( lpCacheNode
== NULL
)
112 ERR( "no memory for NS node\n" );
116 lpCacheNode
->lpNSAddrHdr
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
118 CopyMemory( lpCacheNode
->lpNSAddrHdr
, lpcNSAddrHdr
, dwHdrSize
);
121 lpCacheNode
->data
= (LPDPSESSIONDESC2
)HeapAlloc( GetProcessHeap(),
123 sizeof( *(lpCacheNode
->data
) ) );
125 if( lpCacheNode
->data
== NULL
)
127 ERR( "no memory for SESSIONDESC2\n" );
131 CopyMemory( lpCacheNode
->data
, &lpMsg
->sd
, sizeof( *lpCacheNode
->data
) );
132 len
= WideCharToMultiByte( CP_ACP
, 0, (LPWSTR
)(lpMsg
+1), -1, NULL
, 0, NULL
, NULL
);
133 if ((lpCacheNode
->data
->u1
.lpszSessionNameA
= HeapAlloc( GetProcessHeap(), 0, len
)))
135 WideCharToMultiByte( CP_ACP
, 0, (LPWSTR
)(lpMsg
+1), -1,
136 lpCacheNode
->data
->u1
.lpszSessionNameA
, len
, NULL
, NULL
);
139 lpCacheNode
->dwTime
= timeGetTime();
141 DPQ_INSERT(lpCache
->first
, lpCacheNode
, next
);
143 lpCache
->present
= lpCacheNode
;
145 /* Use this message as an oportunity to weed out any old sessions so
146 * that we don't enum them again
148 NS_PruneSessionCache( lpNSInfo
);
151 LPVOID
NS_GetNSAddr( LPVOID lpNSInfo
)
153 lpNSCache lpCache
= (lpNSCache
)lpNSInfo
;
155 FIXME( ":quick stub\n" );
157 /* Ok. Cheat and don't search for the correct stuff just take the first.
158 * FIXME: In the future how are we to know what is _THE_ enum we used?
159 * This is going to have to go into dplay somehow. Perhaps it
160 * comes back with app server id for the join command! Oh...that
161 * must be it. That would make this method obsolete once that's
165 return lpCache
->first
.lpQHFirst
->lpNSAddrHdr
;
167 /* FIXME: Should convert over to this */
168 return lpCache
->bNsIsLocal
? lpCache
->lpLocalAddrHdr
169 : lpCache
->lpRemoteAddrHdr
;
173 /* Get the magic number associated with the Name Server */
174 DWORD
NS_GetNsMagic( LPVOID lpNSInfo
)
176 LPDWORD lpHdrInfo
= (LPDWORD
)NS_GetNSAddr( lpNSInfo
);
181 /* Get the magic number associated with the non NS end */
182 DWORD
NS_GetOtherMagic( LPVOID lpNSInfo
)
184 lpNSCache lpCache
= (lpNSCache
)lpNSInfo
;
186 return ((LPDWORD
)lpCache
->lpLocalAddrHdr
)[1];
189 void NS_SetLocalAddr( LPVOID lpNSInfo
, LPCVOID lpHdr
, DWORD dwHdrSize
)
191 lpNSCache lpCache
= (lpNSCache
)lpNSInfo
;
193 lpCache
->lpLocalAddrHdr
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, dwHdrSize
);
195 CopyMemory( lpCache
->lpLocalAddrHdr
, lpHdr
, dwHdrSize
);
198 /* This function is responsible for sending a request for all other known
199 nameservers to send us what sessions they have registered locally
201 HRESULT
NS_SendSessionRequestBroadcast( LPCGUID lpcGuid
,
203 LPSPINITDATA lpSpData
)
206 DPSP_ENUMSESSIONSDATA data
;
207 LPDPMSG_ENUMSESSIONSREQUEST lpMsg
;
209 TRACE( "enumerating for guid %s\n", debugstr_guid( lpcGuid
) );
211 /* Get the SP to deal with sending the EnumSessions request */
212 FIXME( ": not all data fields are correct\n" );
214 data
.dwMessageSize
= lpSpData
->dwSPHeaderSize
+ sizeof( *lpMsg
); /*FIXME!*/
215 data
.lpMessage
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
216 data
.dwMessageSize
);
217 data
.lpISP
= lpSpData
->lpISP
;
218 data
.bReturnStatus
= (dwFlags
& DPENUMSESSIONS_RETURNSTATUS
) ? TRUE
: FALSE
;
221 lpMsg
= (LPDPMSG_ENUMSESSIONSREQUEST
)(((BYTE
*)data
.lpMessage
)+lpSpData
->dwSPHeaderSize
);
223 /* Setup EnumSession reqest message */
224 lpMsg
->envelope
.dwMagic
= DPMSGMAGIC_DPLAYMSG
;
225 lpMsg
->envelope
.wCommandId
= DPMSGCMD_ENUMSESSIONSREQUEST
;
226 lpMsg
->envelope
.wVersion
= DPMSGVER_DP6
;
228 lpMsg
->dwPasswordSize
= 0; /* FIXME: If enumerating passwords..? */
229 lpMsg
->dwFlags
= dwFlags
;
231 CopyMemory( &lpMsg
->guidApplication
, lpcGuid
, sizeof( *lpcGuid
) );
233 return (lpSpData
->lpCB
->EnumSessions
)( &data
);
236 /* Delete a name server node which has been allocated on the heap */
237 DPQ_DECL_DELETECB( cbDeleteNSNodeFromHeap
, lpNSCacheData
)
239 /* NOTE: This proc doesn't deal with the walking pointer */
241 /* FIXME: Memory leak on data (contained ptrs) */
242 HeapFree( GetProcessHeap(), 0, elem
->data
);
243 HeapFree( GetProcessHeap(), 0, elem
->lpNSAddrHdr
);
244 HeapFree( GetProcessHeap(), 0, elem
);
247 /* Render all data in a session cache invalid */
248 void NS_InvalidateSessionCache( LPVOID lpNSInfo
)
250 lpNSCache lpCache
= (lpNSCache
)lpNSInfo
;
252 if( lpCache
== NULL
)
254 ERR( ": invalidate non existant cache\n" );
258 DPQ_DELETEQ( lpCache
->first
, next
, lpNSCacheData
, cbDeleteNSNodeFromHeap
);
260 /* NULL out the walking pointer */
261 lpCache
->present
= NULL
;
263 lpCache
->bNsIsLocal
= FALSE
;
267 /* Create and initialize a session cache */
268 BOOL
NS_InitializeSessionCache( LPVOID
* lplpNSInfo
)
270 lpNSCache lpCache
= (lpNSCache
)HeapAlloc( GetProcessHeap(),
272 sizeof( *lpCache
) );
274 *lplpNSInfo
= lpCache
;
276 if( lpCache
== NULL
)
281 DPQ_INIT(lpCache
->first
);
282 lpCache
->present
= NULL
;
284 lpCache
->bNsIsLocal
= FALSE
;
289 /* Delete a session cache */
290 void NS_DeleteSessionCache( LPVOID lpNSInfo
)
292 NS_InvalidateSessionCache( (lpNSCache
)lpNSInfo
);
295 /* Reinitialize the present pointer for this cache */
296 void NS_ResetSessionEnumeration( LPVOID lpNSInfo
)
298 ((lpNSCache
)lpNSInfo
)->present
= ((lpNSCache
)lpNSInfo
)->first
.lpQHFirst
;
301 LPDPSESSIONDESC2
NS_WalkSessions( LPVOID lpNSInfo
)
303 LPDPSESSIONDESC2 lpSessionDesc
;
304 lpNSCache lpCache
= (lpNSCache
)lpNSInfo
;
306 /* FIXME: The pointers could disappear when walking if a prune happens */
308 /* Test for end of the list */
309 if( lpCache
->present
== NULL
)
314 lpSessionDesc
= lpCache
->present
->data
;
316 /* Advance tracking pointer */
317 lpCache
->present
= lpCache
->present
->next
.lpQNext
;
319 return lpSessionDesc
;
322 /* This method should check to see if there are any sessions which are
323 * older than the criteria. If so, just delete that information.
325 /* FIXME: This needs to be called by some periodic timer */
326 void NS_PruneSessionCache( LPVOID lpNSInfo
)
328 lpNSCache lpCache
= lpNSInfo
;
330 const DWORD dwPresentTime
= timeGetTime();
331 const DWORD dwPrunePeriod
= DPMSG_WAIT_60_SECS
; /* is 60 secs enough? */
333 /* This silly little algorithm is based on the fact we keep entries in
334 * the queue in a time based order. It also assumes that it is not possible
335 * to wrap around over yourself (which is not unreasonable).
336 * The if statements verify if the first entry in the queue is less
337 * than dwPrunePeriod old depending on the "clock" roll over.
341 lpNSCacheData lpFirstData
;
343 if( DPQ_IS_EMPTY(lpCache
->first
) )
345 /* Nothing to prune */
349 /* Deal with time in a wrap around safe manner - unsigned arithmatic.
350 * Check the difference in time */
351 if( (dwPresentTime
- (DPQ_FIRST(lpCache
->first
)->dwTime
)) < dwPrunePeriod
)
353 /* First entry has not expired yet; don't prune */
357 lpFirstData
= DPQ_FIRST(lpCache
->first
);
358 DPQ_REMOVE( lpCache
->first
, DPQ_FIRST(lpCache
->first
), next
);
359 cbDeleteNSNodeFromHeap( lpFirstData
);
364 /* NAME SERVER Message stuff */
365 void NS_ReplyToEnumSessionsRequest( LPCVOID lpcMsg
,
366 LPVOID
* lplpReplyData
,
367 LPDWORD lpdwReplySize
,
368 IDirectPlay2Impl
* lpDP
)
370 LPDPMSG_ENUMSESSIONSREPLY rmsg
;
371 DWORD dwVariableSize
;
373 /* LPCDPMSG_ENUMSESSIONSREQUEST msg = (LPDPMSG_ENUMSESSIONSREQUEST)lpcMsg; */
374 BOOL bAnsi
= TRUE
; /* FIXME: This needs to be in the DPLAY interface */
376 FIXME( ": few fixed + need to check request for response\n" );
380 dwVariableLen
= MultiByteToWideChar( CP_ACP
, 0,
381 lpDP
->dp2
->lpSessionDesc
->u1
.lpszSessionNameA
,
386 dwVariableLen
= strlenW( lpDP
->dp2
->lpSessionDesc
->u1
.lpszSessionName
) + 1;
389 dwVariableSize
= dwVariableLen
* sizeof( WCHAR
);
391 *lpdwReplySize
= lpDP
->dp2
->spData
.dwSPHeaderSize
+
392 sizeof( *rmsg
) + dwVariableSize
;
393 *lplpReplyData
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
396 rmsg
= (LPDPMSG_ENUMSESSIONSREPLY
)( (BYTE
*)*lplpReplyData
+
397 lpDP
->dp2
->spData
.dwSPHeaderSize
);
399 rmsg
->envelope
.dwMagic
= DPMSGMAGIC_DPLAYMSG
;
400 rmsg
->envelope
.wCommandId
= DPMSGCMD_ENUMSESSIONSREPLY
;
401 rmsg
->envelope
.wVersion
= DPMSGVER_DP6
;
403 CopyMemory( &rmsg
->sd
, lpDP
->dp2
->lpSessionDesc
,
404 sizeof( lpDP
->dp2
->lpSessionDesc
->dwSize
) );
405 rmsg
->dwUnknown
= 0x0000005c;
408 MultiByteToWideChar( CP_ACP
, 0, lpDP
->dp2
->lpSessionDesc
->u1
.lpszSessionNameA
, -1,
409 (LPWSTR
)(rmsg
+1), dwVariableLen
);
413 strcpyW( (LPWSTR
)(rmsg
+1), lpDP
->dp2
->lpSessionDesc
->u1
.lpszSessionName
);