Fixed some HFILE vs. HANDLE mismatches.
[wine/dcerpc.git] / dlls / dplayx / name_server.c
blob68174a3d86979efc1a57fc44fe13fe43f23ff698
1 /* DPLAYX.DLL name server implementation
3 * Copyright 2000 - Peter Hunnisett
5 * <presently under construction - contact hunnise@nortelnetworks.com>
7 */
9 /* NOTE: Methods with the NS_ prefix are name server methods */
11 #include "winbase.h"
12 #include "winnls.h"
13 #include "wine/unicode.h"
14 #include "debugtools.h"
15 #include "mmsystem.h"
17 #include "dplayx_global.h"
18 #include "name_server.h"
19 #include "dplaysp.h"
20 #include "dplayx_messages.h"
21 #include "dplayx_queue.h"
23 /* FIXME: Need to create a crit section, store and use it */
25 DEFAULT_DEBUG_CHANNEL(dplay);
27 /* NS specific structures */
28 struct NSCacheData
30 DPQ_ENTRY(NSCacheData) next;
32 DWORD dwTime; /* Time at which data was last known valid */
33 LPDPSESSIONDESC2 data;
35 LPVOID lpNSAddrHdr;
38 typedef struct NSCacheData NSCacheData, *lpNSCacheData;
40 struct NSCache
42 lpNSCacheData present; /* keep track of what is to be looked at when walking */
44 DPQ_HEAD(NSCacheData) first;
45 };
46 typedef struct NSCache NSCache, *lpNSCache;
48 /* Function prototypes */
49 DPQ_DECL_DELETECB( cbDeleteNSNodeFromHeap, lpNSCacheData );
51 /* Name Server functions
52 * ---------------------
54 void NS_SetLocalComputerAsNameServer( LPCDPSESSIONDESC2 lpsd )
56 #if 0
57 /* FIXME: Remove this method? */
58 DPLAYX_SetLocalSession( lpsd );
59 #endif
62 DPQ_DECL_COMPARECB( cbUglyPig, GUID )
64 return IsEqualGUID( elem1, elem2 );
67 /* Store the given NS remote address for future reference */
68 void NS_SetRemoteComputerAsNameServer( LPVOID lpNSAddrHdr,
69 DWORD dwHdrSize,
70 LPDPMSG_ENUMSESSIONSREPLY lpMsg,
71 LPVOID lpNSInfo )
73 DWORD len;
74 lpNSCache lpCache = (lpNSCache)lpNSInfo;
75 lpNSCacheData lpCacheNode;
77 TRACE( "%p, %p, %p\n", lpNSAddrHdr, lpMsg, lpNSInfo );
79 /* FIXME: Should check to see if the reply is for an existing session. If
80 * so we remove the old and add the new so oldest is at front.
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 lpMsg->sd.guidInstance, lpCacheNode );
87 if( lpCacheNode != NULL )
89 TRACE( "Duplicate session entry for %s removed - updated version kept\n",
90 debugstr_guid( &lpCacheNode->data->guidInstance ) );
91 cbDeleteNSNodeFromHeap( lpCacheNode );
94 /* Add this to the list */
95 lpCacheNode = (lpNSCacheData)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
96 sizeof( *lpCacheNode ) );
98 if( lpCacheNode == NULL )
100 ERR( "no memory for NS node\n" );
101 return;
104 lpCacheNode->lpNSAddrHdr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
105 dwHdrSize );
106 CopyMemory( lpCacheNode->lpNSAddrHdr, lpNSAddrHdr, dwHdrSize );
109 lpCacheNode->data = (LPDPSESSIONDESC2)HeapAlloc( GetProcessHeap(),
110 HEAP_ZERO_MEMORY,
111 sizeof( *lpCacheNode->data ) );
113 if( lpCacheNode->data == NULL )
115 ERR( "no memory for SESSIONDESC2\n" );
116 return;
119 CopyMemory( lpCacheNode->data, &lpMsg->sd, sizeof( *lpCacheNode->data ) );
120 len = WideCharToMultiByte( CP_ACP, 0, (LPWSTR)(lpMsg+1), -1, NULL, 0, NULL, NULL );
121 if ((lpCacheNode->data->u1.lpszSessionNameA = HeapAlloc( GetProcessHeap(), 0, len )))
122 WideCharToMultiByte( CP_ACP, 0, (LPWSTR)(lpMsg+1), -1,
123 lpCacheNode->data->u1.lpszSessionNameA, len, NULL, NULL );
125 lpCacheNode->dwTime = timeGetTime();
127 DPQ_INSERT(lpCache->first, lpCacheNode, next );
129 lpCache->present = lpCacheNode;
131 /* Use this message as an oportunity to weed out any old sessions so
132 * that we don't enum them again
134 NS_PruneSessionCache( lpNSInfo );
137 LPVOID NS_GetNSAddr( LPVOID lpNSInfo )
139 lpNSCache lpCache = (lpNSCache)lpNSInfo;
141 FIXME( ":quick stub\n" );
143 /* Ok. Cheat and don't search for the correct stuff just take the first.
144 * FIXME: In the future how are we to know what is _THE_ enum we used?
145 * This is going to have to go into dplay somehow. Perhaps it
146 * comes back with app server id for the join command! Oh...that
147 * must be it. That would make this method obsolete once that's
148 * in place.
151 return lpCache->first.lpQHFirst->lpNSAddrHdr;
154 /* This function is responsible for sending a request for all other known
155 nameservers to send us what sessions they have registered locally
157 HRESULT NS_SendSessionRequestBroadcast( LPCGUID lpcGuid,
158 DWORD dwFlags,
159 LPSPINITDATA lpSpData )
162 DPSP_ENUMSESSIONSDATA data;
163 LPDPMSG_ENUMSESSIONSREQUEST lpMsg;
165 TRACE( "enumerating for guid %s\n", debugstr_guid( lpcGuid ) );
167 /* Get the SP to deal with sending the EnumSessions request */
168 FIXME( ": not all data fields are correct\n" );
170 data.dwMessageSize = lpSpData->dwSPHeaderSize + sizeof( *lpMsg ); /*FIXME!*/
171 data.lpMessage = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
172 data.dwMessageSize );
173 data.lpISP = lpSpData->lpISP;
174 data.bReturnStatus = (dwFlags & DPENUMSESSIONS_RETURNSTATUS) ? TRUE : FALSE;
177 lpMsg = (LPDPMSG_ENUMSESSIONSREQUEST)(((BYTE*)data.lpMessage)+lpSpData->dwSPHeaderSize);
179 /* Setup EnumSession reqest message */
180 lpMsg->envelope.dwMagic = DPMSGMAGIC_DPLAYMSG;
181 lpMsg->envelope.wCommandId = DPMSGCMD_ENUMSESSIONSREQUEST;
182 lpMsg->envelope.wVersion = DPMSGVER_DP6;
184 lpMsg->dwPasswordSize = 0; /* FIXME: If enumerating passwords..? */
185 lpMsg->dwFlags = dwFlags;
187 CopyMemory( &lpMsg->guidApplication, lpcGuid, sizeof( *lpcGuid ) );
189 return (lpSpData->lpCB->EnumSessions)( &data );
192 /* Delete a name server node which has been allocated on the heap */
193 DPQ_DECL_DELETECB( cbDeleteNSNodeFromHeap, lpNSCacheData )
195 /* NOTE: This proc doesn't deal with the walking pointer */
197 /* FIXME: Memory leak on data (contained ptrs) */
198 HeapFree( GetProcessHeap(), 0, elem->data );
199 HeapFree( GetProcessHeap(), 0, elem->lpNSAddrHdr );
200 HeapFree( GetProcessHeap(), 0, elem );
203 /* Render all data in a session cache invalid */
204 void NS_InvalidateSessionCache( LPVOID lpNSInfo )
206 lpNSCache lpCache = (lpNSCache)lpNSInfo;
208 if( lpCache == NULL )
210 ERR( ": invalidate non existant cache\n" );
211 return;
214 DPQ_DELETEQ( lpCache->first, next, lpNSCacheData, cbDeleteNSNodeFromHeap );
216 /* NULL out the walking pointer */
217 lpCache->present = NULL;
220 /* Create and initialize a session cache */
221 BOOL NS_InitializeSessionCache( LPVOID* lplpNSInfo )
223 lpNSCache lpCache = (lpNSCache)HeapAlloc( GetProcessHeap(),
224 HEAP_ZERO_MEMORY,
225 sizeof( *lpCache ) );
227 *lplpNSInfo = lpCache;
229 if( lpCache == NULL )
231 return FALSE;
234 DPQ_INIT(lpCache->first);
235 lpCache->present = NULL;
237 return TRUE;
240 /* Delete a session cache */
241 void NS_DeleteSessionCache( LPVOID lpNSInfo )
243 NS_InvalidateSessionCache( (lpNSCache)lpNSInfo );
246 /* Reinitialize the present pointer for this cache */
247 void NS_ResetSessionEnumeration( LPVOID lpNSInfo )
249 ((lpNSCache)lpNSInfo)->present = ((lpNSCache)lpNSInfo)->first.lpQHFirst;
252 LPDPSESSIONDESC2 NS_WalkSessions( LPVOID lpNSInfo )
254 LPDPSESSIONDESC2 lpSessionDesc;
255 lpNSCache lpCache = (lpNSCache)lpNSInfo;
257 /* FIXME: The pointers could disappear when walking if a prune happens */
259 /* Test for end of the list */
260 if( lpCache->present == NULL )
262 return NULL;
265 lpSessionDesc = lpCache->present->data;
267 /* Advance tracking pointer */
268 lpCache->present = lpCache->present->next.lpQNext;
270 return lpSessionDesc;
273 /* This method should check to see if there are any sessions which are
274 * older than the criteria. If so, just delete that information.
276 /* FIXME: This needs to be called by some periodic timer */
277 void NS_PruneSessionCache( LPVOID lpNSInfo )
279 lpNSCache lpCache = lpNSInfo;
281 const DWORD dwPresentTime = timeGetTime();
282 const DWORD dwPrunePeriod = 60000; /* is 60 secs enough? */
283 const DWORD dwPruneTime = dwPresentTime - dwPrunePeriod;
285 /* This silly little algorithm is based on the fact we keep entries in
286 * the queue in a time based order. It also assumes that it is not possible
287 * to wrap around over yourself (which is not unreasonable).
288 * The if statements verify if the first entry in the queue is less
289 * than dwPrunePeriod old depending on the "clock" roll over.
291 for( ;; )
293 lpNSCacheData lpFirstData;
295 if( DPQ_IS_EMPTY(lpCache->first) )
297 /* Nothing to prune */
298 break;
301 if( dwPruneTime > dwPresentTime ) /* 0 <= dwPresentTime <= dwPrunePeriod */
303 if( ( DPQ_FIRST(lpCache->first)->dwTime <= dwPresentTime ) ||
304 ( DPQ_FIRST(lpCache->first)->dwTime > dwPruneTime )
307 /* Less than dwPrunePeriod old - keep */
308 break;
311 else /* dwPrunePeriod <= dwPresentTime <= max dword */
313 if( ( DPQ_FIRST(lpCache->first)->dwTime <= dwPresentTime ) &&
314 ( DPQ_FIRST(lpCache->first)->dwTime > dwPruneTime )
317 /* Less than dwPrunePeriod old - keep */
318 break;
322 lpFirstData = DPQ_FIRST(lpCache->first);
323 DPQ_REMOVE( lpCache->first, DPQ_FIRST(lpCache->first), next );
324 cbDeleteNSNodeFromHeap( lpFirstData );
329 /* NAME SERVER Message stuff */
330 void NS_ReplyToEnumSessionsRequest( LPVOID lpMsg,
331 LPDPSP_REPLYDATA lpReplyData,
332 IDirectPlay2Impl* lpDP )
334 LPDPMSG_ENUMSESSIONSREPLY rmsg;
335 DWORD dwVariableSize;
336 DWORD dwVariableLen;
337 /* LPDPMSG_ENUMSESSIONSREQUEST msg = (LPDPMSG_ENUMSESSIONSREQUEST)lpMsg; */
338 BOOL bAnsi = TRUE; /* FIXME: This needs to be in the DPLAY interface */
340 FIXME( ": few fixed + need to check request for response\n" );
342 if (bAnsi)
343 dwVariableLen = MultiByteToWideChar( CP_ACP, 0,
344 lpDP->dp2->lpSessionDesc->u1.lpszSessionNameA,
345 -1, NULL, 0 );
346 else
347 dwVariableLen = strlenW( lpDP->dp2->lpSessionDesc->u1.lpszSessionName ) + 1;
349 dwVariableSize = dwVariableLen * sizeof( WCHAR );
351 lpReplyData->dwMessageSize = lpDP->dp2->spData.dwSPHeaderSize +
352 sizeof( *rmsg ) + dwVariableSize;
353 lpReplyData->lpMessage = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
354 lpReplyData->dwMessageSize );
356 rmsg = (LPDPMSG_ENUMSESSIONSREPLY)( (BYTE*)lpReplyData->lpMessage +
357 lpDP->dp2->spData.dwSPHeaderSize);
359 rmsg->envelope.dwMagic = DPMSGMAGIC_DPLAYMSG;
360 rmsg->envelope.wCommandId = DPMSGCMD_ENUMSESSIONSREPLY;
361 rmsg->envelope.wVersion = DPMSGVER_DP6;
363 CopyMemory( &rmsg->sd, lpDP->dp2->lpSessionDesc,
364 sizeof( lpDP->dp2->lpSessionDesc->dwSize ) );
365 rmsg->dwUnknown = 0x0000005c;
366 if( bAnsi )
367 MultiByteToWideChar( CP_ACP, 0, lpDP->dp2->lpSessionDesc->u1.lpszSessionNameA, -1,
368 (LPWSTR)(rmsg+1), dwVariableLen );
369 else
370 strcpyW( (LPWSTR)(rmsg+1), lpDP->dp2->lpSessionDesc->u1.lpszSessionName );