1 /* DPLAYX.DLL name server implementation
3 * Copyright 2000-2001 - Peter Hunnisett
5 * <presently under construction - contact hunnise@nortelnetworks.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 /* NOTE: Methods with the NS_ prefix are name server methods */
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
32 #include "wine/unicode.h"
33 #include "wine/debug.h"
36 #include "dplayx_global.h"
37 #include "name_server.h"
39 #include "dplayx_messages.h"
40 #include "dplayx_queue.h"
42 /* FIXME: Need to create a crit section, store and use it */
44 WINE_DEFAULT_DEBUG_CHANNEL(dplay
);
46 /* NS specific structures */
49 DPQ_ENTRY(NSCacheData
) next
;
51 DWORD dwTime
; /* Time at which data was last known valid */
52 LPDPSESSIONDESC2 data
;
57 typedef struct NSCacheData NSCacheData
, *lpNSCacheData
;
61 lpNSCacheData present
; /* keep track of what is to be looked at when walking */
63 DPQ_HEAD(NSCacheData
) first
;
66 LPVOID lpLocalAddrHdr
; /* FIXME: Not yet used */
67 LPVOID lpRemoteAddrHdr
; /* FIXME: Not yet used */
69 typedef struct NSCache NSCache
, *lpNSCache
;
71 /* Function prototypes */
72 DPQ_DECL_DELETECB( cbDeleteNSNodeFromHeap
, lpNSCacheData
);
74 /* Name Server functions
75 * ---------------------
77 void NS_SetLocalComputerAsNameServer( LPCDPSESSIONDESC2 lpsd
, LPVOID lpNSInfo
)
80 /* FIXME: Remove this method? */
81 DPLAYX_SetLocalSession( lpsd
);
83 lpNSCache lpCache
= (lpNSCache
)lpNSInfo
;
85 lpCache
->bNsIsLocal
= TRUE
;
88 void NS_SetRemoteComputerAsNameServer( LPCDPSESSIONDESC2 lpsd
, LPVOID lpNSInfo
)
90 lpNSCache lpCache
= (lpNSCache
)lpNSInfo
;
92 lpCache
->bNsIsLocal
= FALSE
;
95 DPQ_DECL_COMPARECB( cbUglyPig
, GUID
)
97 return IsEqualGUID( elem1
, elem2
);
100 /* Store the given NS remote address for future reference */
101 /* FIXME: LPDPMSG_ENUMSESSIONSREPLY should be const */
102 void NS_AddRemoteComputerAsNameServer( LPCVOID lpcNSAddrHdr
,
104 LPDPMSG_ENUMSESSIONSREPLY lpMsg
,
108 lpNSCache lpCache
= (lpNSCache
)lpNSInfo
;
109 lpNSCacheData lpCacheNode
;
111 TRACE( "%p, %p, %p\n", lpcNSAddrHdr
, lpMsg
, lpNSInfo
);
113 /* See if we can find this session. If we can, remove it as it's a dup */
114 DPQ_REMOVE_ENTRY_CB( lpCache
->first
, next
, data
->guidInstance
, cbUglyPig
,
115 lpMsg
->sd
.guidInstance
, lpCacheNode
);
117 if( lpCacheNode
!= NULL
)
119 TRACE( "Duplicate session entry for %s removed - updated version kept\n",
120 debugstr_guid( &lpCacheNode
->data
->guidInstance
) );
121 cbDeleteNSNodeFromHeap( lpCacheNode
);
124 /* Add this to the list */
125 lpCacheNode
= (lpNSCacheData
)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
126 sizeof( *lpCacheNode
) );
128 if( lpCacheNode
== NULL
)
130 ERR( "no memory for NS node\n" );
134 lpCacheNode
->lpNSAddrHdr
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
136 CopyMemory( lpCacheNode
->lpNSAddrHdr
, lpcNSAddrHdr
, dwHdrSize
);
139 lpCacheNode
->data
= (LPDPSESSIONDESC2
)HeapAlloc( GetProcessHeap(),
141 sizeof( *(lpCacheNode
->data
) ) );
143 if( lpCacheNode
->data
== NULL
)
145 ERR( "no memory for SESSIONDESC2\n" );
149 CopyMemory( lpCacheNode
->data
, &lpMsg
->sd
, sizeof( *lpCacheNode
->data
) );
150 len
= WideCharToMultiByte( CP_ACP
, 0, (LPWSTR
)(lpMsg
+1), -1, NULL
, 0, NULL
, NULL
);
151 if ((lpCacheNode
->data
->u1
.lpszSessionNameA
= HeapAlloc( GetProcessHeap(), 0, len
)))
153 WideCharToMultiByte( CP_ACP
, 0, (LPWSTR
)(lpMsg
+1), -1,
154 lpCacheNode
->data
->u1
.lpszSessionNameA
, len
, NULL
, NULL
);
157 lpCacheNode
->dwTime
= timeGetTime();
159 DPQ_INSERT(lpCache
->first
, lpCacheNode
, next
);
161 lpCache
->present
= lpCacheNode
;
163 /* Use this message as an opportunity to weed out any old sessions so
164 * that we don't enum them again
166 NS_PruneSessionCache( lpNSInfo
);
169 LPVOID
NS_GetNSAddr( LPVOID lpNSInfo
)
171 lpNSCache lpCache
= (lpNSCache
)lpNSInfo
;
173 FIXME( ":quick stub\n" );
175 /* Ok. Cheat and don't search for the correct stuff just take the first.
176 * FIXME: In the future how are we to know what is _THE_ enum we used?
177 * This is going to have to go into dplay somehow. Perhaps it
178 * comes back with app server id for the join command! Oh...that
179 * must be it. That would make this method obsolete once that's
183 return lpCache
->first
.lpQHFirst
->lpNSAddrHdr
;
185 /* FIXME: Should convert over to this */
186 return lpCache
->bNsIsLocal
? lpCache
->lpLocalAddrHdr
187 : lpCache
->lpRemoteAddrHdr
;
191 /* Get the magic number associated with the Name Server */
192 DWORD
NS_GetNsMagic( LPVOID lpNSInfo
)
194 LPDWORD lpHdrInfo
= (LPDWORD
)NS_GetNSAddr( lpNSInfo
);
199 /* Get the magic number associated with the non NS end */
200 DWORD
NS_GetOtherMagic( LPVOID lpNSInfo
)
202 lpNSCache lpCache
= (lpNSCache
)lpNSInfo
;
204 return ((LPDWORD
)lpCache
->lpLocalAddrHdr
)[1];
207 void NS_SetLocalAddr( LPVOID lpNSInfo
, LPCVOID lpHdr
, DWORD dwHdrSize
)
209 lpNSCache lpCache
= (lpNSCache
)lpNSInfo
;
211 lpCache
->lpLocalAddrHdr
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, dwHdrSize
);
213 CopyMemory( lpCache
->lpLocalAddrHdr
, lpHdr
, dwHdrSize
);
216 /* This function is responsible for sending a request for all other known
217 nameservers to send us what sessions they have registered locally
219 HRESULT
NS_SendSessionRequestBroadcast( LPCGUID lpcGuid
,
221 LPSPINITDATA lpSpData
)
224 DPSP_ENUMSESSIONSDATA data
;
225 LPDPMSG_ENUMSESSIONSREQUEST lpMsg
;
227 TRACE( "enumerating for guid %s\n", debugstr_guid( lpcGuid
) );
229 /* Get the SP to deal with sending the EnumSessions request */
230 FIXME( ": not all data fields are correct\n" );
232 data
.dwMessageSize
= lpSpData
->dwSPHeaderSize
+ sizeof( *lpMsg
); /*FIXME!*/
233 data
.lpMessage
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
234 data
.dwMessageSize
);
235 data
.lpISP
= lpSpData
->lpISP
;
236 data
.bReturnStatus
= (dwFlags
& DPENUMSESSIONS_RETURNSTATUS
) ? TRUE
: FALSE
;
239 lpMsg
= (LPDPMSG_ENUMSESSIONSREQUEST
)(((BYTE
*)data
.lpMessage
)+lpSpData
->dwSPHeaderSize
);
241 /* Setup EnumSession request message */
242 lpMsg
->envelope
.dwMagic
= DPMSGMAGIC_DPLAYMSG
;
243 lpMsg
->envelope
.wCommandId
= DPMSGCMD_ENUMSESSIONSREQUEST
;
244 lpMsg
->envelope
.wVersion
= DPMSGVER_DP6
;
246 lpMsg
->dwPasswordSize
= 0; /* FIXME: If enumerating passwords..? */
247 lpMsg
->dwFlags
= dwFlags
;
249 CopyMemory( &lpMsg
->guidApplication
, lpcGuid
, sizeof( *lpcGuid
) );
251 return (lpSpData
->lpCB
->EnumSessions
)( &data
);
254 /* Delete a name server node which has been allocated on the heap */
255 DPQ_DECL_DELETECB( cbDeleteNSNodeFromHeap
, lpNSCacheData
)
257 /* NOTE: This proc doesn't deal with the walking pointer */
259 /* FIXME: Memory leak on data (contained ptrs) */
260 HeapFree( GetProcessHeap(), 0, elem
->data
);
261 HeapFree( GetProcessHeap(), 0, elem
->lpNSAddrHdr
);
262 HeapFree( GetProcessHeap(), 0, elem
);
265 /* Render all data in a session cache invalid */
266 void NS_InvalidateSessionCache( LPVOID lpNSInfo
)
268 lpNSCache lpCache
= (lpNSCache
)lpNSInfo
;
270 if( lpCache
== NULL
)
272 ERR( ": invalidate non existent cache\n" );
276 DPQ_DELETEQ( lpCache
->first
, next
, lpNSCacheData
, cbDeleteNSNodeFromHeap
);
278 /* NULL out the walking pointer */
279 lpCache
->present
= NULL
;
281 lpCache
->bNsIsLocal
= FALSE
;
285 /* Create and initialize a session cache */
286 BOOL
NS_InitializeSessionCache( LPVOID
* lplpNSInfo
)
288 lpNSCache lpCache
= (lpNSCache
)HeapAlloc( GetProcessHeap(),
290 sizeof( *lpCache
) );
292 *lplpNSInfo
= lpCache
;
294 if( lpCache
== NULL
)
299 DPQ_INIT(lpCache
->first
);
300 lpCache
->present
= NULL
;
302 lpCache
->bNsIsLocal
= FALSE
;
307 /* Delete a session cache */
308 void NS_DeleteSessionCache( LPVOID lpNSInfo
)
310 NS_InvalidateSessionCache( (lpNSCache
)lpNSInfo
);
313 /* Reinitialize the present pointer for this cache */
314 void NS_ResetSessionEnumeration( LPVOID lpNSInfo
)
316 ((lpNSCache
)lpNSInfo
)->present
= ((lpNSCache
)lpNSInfo
)->first
.lpQHFirst
;
319 LPDPSESSIONDESC2
NS_WalkSessions( LPVOID lpNSInfo
)
321 LPDPSESSIONDESC2 lpSessionDesc
;
322 lpNSCache lpCache
= (lpNSCache
)lpNSInfo
;
324 /* FIXME: The pointers could disappear when walking if a prune happens */
326 /* Test for end of the list */
327 if( lpCache
->present
== NULL
)
332 lpSessionDesc
= lpCache
->present
->data
;
334 /* Advance tracking pointer */
335 lpCache
->present
= lpCache
->present
->next
.lpQNext
;
337 return lpSessionDesc
;
340 /* This method should check to see if there are any sessions which are
341 * older than the criteria. If so, just delete that information.
343 /* FIXME: This needs to be called by some periodic timer */
344 void NS_PruneSessionCache( LPVOID lpNSInfo
)
346 lpNSCache lpCache
= lpNSInfo
;
348 const DWORD dwPresentTime
= timeGetTime();
349 const DWORD dwPrunePeriod
= DPMSG_WAIT_60_SECS
; /* is 60 secs enough? */
351 /* This silly little algorithm is based on the fact we keep entries in
352 * the queue in a time based order. It also assumes that it is not possible
353 * to wrap around over yourself (which is not unreasonable).
354 * The if statements verify if the first entry in the queue is less
355 * than dwPrunePeriod old depending on the "clock" roll over.
359 lpNSCacheData lpFirstData
;
361 if( DPQ_IS_EMPTY(lpCache
->first
) )
363 /* Nothing to prune */
367 /* Deal with time in a wrap around safe manner - unsigned arithmetic.
368 * Check the difference in time */
369 if( (dwPresentTime
- (DPQ_FIRST(lpCache
->first
)->dwTime
)) < dwPrunePeriod
)
371 /* First entry has not expired yet; don't prune */
375 lpFirstData
= DPQ_FIRST(lpCache
->first
);
376 DPQ_REMOVE( lpCache
->first
, DPQ_FIRST(lpCache
->first
), next
);
377 cbDeleteNSNodeFromHeap( lpFirstData
);
382 /* NAME SERVER Message stuff */
383 void NS_ReplyToEnumSessionsRequest( LPCVOID lpcMsg
,
384 LPVOID
* lplpReplyData
,
385 LPDWORD lpdwReplySize
,
386 IDirectPlay2Impl
* lpDP
)
388 LPDPMSG_ENUMSESSIONSREPLY rmsg
;
389 DWORD dwVariableSize
;
391 /* LPCDPMSG_ENUMSESSIONSREQUEST msg = (LPDPMSG_ENUMSESSIONSREQUEST)lpcMsg; */
392 BOOL bAnsi
= TRUE
; /* FIXME: This needs to be in the DPLAY interface */
394 FIXME( ": few fixed + need to check request for response\n" );
398 dwVariableLen
= MultiByteToWideChar( CP_ACP
, 0,
399 lpDP
->dp2
->lpSessionDesc
->u1
.lpszSessionNameA
,
404 dwVariableLen
= strlenW( lpDP
->dp2
->lpSessionDesc
->u1
.lpszSessionName
) + 1;
407 dwVariableSize
= dwVariableLen
* sizeof( WCHAR
);
409 *lpdwReplySize
= lpDP
->dp2
->spData
.dwSPHeaderSize
+
410 sizeof( *rmsg
) + dwVariableSize
;
411 *lplpReplyData
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
414 rmsg
= (LPDPMSG_ENUMSESSIONSREPLY
)( (BYTE
*)*lplpReplyData
+
415 lpDP
->dp2
->spData
.dwSPHeaderSize
);
417 rmsg
->envelope
.dwMagic
= DPMSGMAGIC_DPLAYMSG
;
418 rmsg
->envelope
.wCommandId
= DPMSGCMD_ENUMSESSIONSREPLY
;
419 rmsg
->envelope
.wVersion
= DPMSGVER_DP6
;
421 CopyMemory( &rmsg
->sd
, lpDP
->dp2
->lpSessionDesc
,
422 sizeof( lpDP
->dp2
->lpSessionDesc
->dwSize
) );
423 rmsg
->dwUnknown
= 0x0000005c;
426 MultiByteToWideChar( CP_ACP
, 0, lpDP
->dp2
->lpSessionDesc
->u1
.lpszSessionNameA
, -1,
427 (LPWSTR
)(rmsg
+1), dwVariableLen
);
431 strcpyW( (LPWSTR
)(rmsg
+1), lpDP
->dp2
->lpSessionDesc
->u1
.lpszSessionName
);