Fixed some issues found by winapi_check.
[wine] / dlls / dplayx / name_server.c
1 /* DPLAYX.DLL name server implementation
2  *
3  * Copyright 2000 - Peter Hunnisett
4  *
5  * <presently under construction - contact hunnise@nortelnetworks.com>
6  *
7  */
8  
9 /* NOTE: Methods with the NS_ prefix are name server methods */
10
11 #include "winbase.h"
12 #include "winnls.h"
13 #include "wine/unicode.h"
14 #include "debugtools.h"
15 #include "mmsystem.h"
16
17 #include "dplayx_global.h"
18 #include "name_server.h"
19 #include "dplaysp.h"
20 #include "dplayx_messages.h"
21 #include "dplayx_queue.h"
22
23 /* FIXME: Need to create a crit section, store and use it */
24
25 DEFAULT_DEBUG_CHANNEL(dplay);
26
27 /* NS specific structures */
28 struct NSCacheData
29 {
30   DPQ_ENTRY(NSCacheData) next;
31
32   DWORD dwTime; /* Time at which data was last known valid */
33   LPDPSESSIONDESC2 data;
34
35   LPVOID lpNSAddrHdr;
36
37 };
38 typedef struct NSCacheData NSCacheData, *lpNSCacheData;
39
40 struct NSCache
41 {
42   lpNSCacheData present; /* keep track of what is to be looked at when walking */
43
44   DPQ_HEAD(NSCacheData) first;
45 }; 
46 typedef struct NSCache NSCache, *lpNSCache;
47
48 /* Function prototypes */
49 DPQ_DECL_DELETECB( cbDeleteNSNodeFromHeap, lpNSCacheData );
50
51 /* Name Server functions 
52  * --------------------- 
53  */
54 void NS_SetLocalComputerAsNameServer( LPCDPSESSIONDESC2 lpsd )
55 {
56 #if 0
57   /* FIXME: Remove this method? */
58   DPLAYX_SetLocalSession( lpsd );
59 #endif
60 }
61
62 DPQ_DECL_COMPARECB( cbUglyPig, GUID )
63 {
64   return IsEqualGUID( elem1, elem2 );
65 }
66
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 )
72 {
73   DWORD len;
74   lpNSCache     lpCache = (lpNSCache)lpNSInfo;
75   lpNSCacheData lpCacheNode;
76
77   TRACE( "%p, %p, %p\n", lpNSAddrHdr, lpMsg, lpNSInfo );
78
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.
81    */
82
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 );
86
87   if( lpCacheNode != NULL )
88   {
89     TRACE( "Duplicate session entry for %s removed - updated version kept\n",
90            debugstr_guid( &lpCacheNode->data->guidInstance ) );
91     cbDeleteNSNodeFromHeap( lpCacheNode );
92   }
93
94   /* Add this to the list */
95   lpCacheNode = (lpNSCacheData)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 
96                                           sizeof( *lpCacheNode ) );
97
98   if( lpCacheNode == NULL )
99   {
100     ERR( "no memory for NS node\n" );
101     return;
102   }
103
104   lpCacheNode->lpNSAddrHdr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
105                                         dwHdrSize );
106   CopyMemory( lpCacheNode->lpNSAddrHdr, lpNSAddrHdr, dwHdrSize );
107               
108
109   lpCacheNode->data = (LPDPSESSIONDESC2)HeapAlloc( GetProcessHeap(),
110                                                    HEAP_ZERO_MEMORY, 
111                                                    sizeof( *lpCacheNode->data ) );
112
113   if( lpCacheNode->data == NULL )
114   {
115     ERR( "no memory for SESSIONDESC2\n" );
116     return;
117   }
118
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 );
124
125   lpCacheNode->dwTime = timeGetTime();
126
127   DPQ_INSERT(lpCache->first, lpCacheNode, next );
128
129   lpCache->present = lpCacheNode;
130
131   /* Use this message as an oportunity to weed out any old sessions so 
132    * that we don't enum them again 
133    */
134   NS_PruneSessionCache( lpNSInfo );
135 }
136
137 LPVOID NS_GetNSAddr( LPVOID lpNSInfo )
138 {
139   lpNSCache lpCache = (lpNSCache)lpNSInfo;
140
141   FIXME( ":quick stub\n" );
142
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.
149    */
150
151   return lpCache->first.lpQHFirst->lpNSAddrHdr;
152 }
153
154 /* This function is responsible for sending a request for all other known
155    nameservers to send us what sessions they have registered locally
156  */
157 HRESULT NS_SendSessionRequestBroadcast( LPCGUID lpcGuid,
158                                         DWORD dwFlags,
159                                         LPSPINITDATA lpSpData )
160                                      
161 {
162   DPSP_ENUMSESSIONSDATA data;
163   LPDPMSG_ENUMSESSIONSREQUEST lpMsg;
164
165   TRACE( "enumerating for guid %s\n", debugstr_guid( lpcGuid ) );
166
167   /* Get the SP to deal with sending the EnumSessions request */
168   FIXME( ": not all data fields are correct\n" );
169
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;
175
176
177   lpMsg = (LPDPMSG_ENUMSESSIONSREQUEST)(((BYTE*)data.lpMessage)+lpSpData->dwSPHeaderSize); 
178
179   /* Setup EnumSession reqest message */
180   lpMsg->envelope.dwMagic    = DPMSGMAGIC_DPLAYMSG;
181   lpMsg->envelope.wCommandId = DPMSGCMD_ENUMSESSIONSREQUEST;
182   lpMsg->envelope.wVersion   = DPMSGVER_DP6;
183
184   lpMsg->dwPasswordSize = 0; /* FIXME: If enumerating passwords..? */
185   lpMsg->dwFlags        = dwFlags;
186
187   CopyMemory( &lpMsg->guidApplication, lpcGuid, sizeof( *lpcGuid ) );
188
189   return (lpSpData->lpCB->EnumSessions)( &data ); 
190 }
191
192 /* Delete a name server node which has been allocated on the heap */
193 DPQ_DECL_DELETECB( cbDeleteNSNodeFromHeap, lpNSCacheData )
194 {
195   /* NOTE: This proc doesn't deal with the walking pointer */
196
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 );
201 }
202
203 /* Render all data in a session cache invalid */
204 void NS_InvalidateSessionCache( LPVOID lpNSInfo )
205 {
206   lpNSCache lpCache = (lpNSCache)lpNSInfo;
207
208   if( lpCache == NULL )
209   {
210     ERR( ": invalidate non existant cache\n" );
211     return;
212   }
213
214   DPQ_DELETEQ( lpCache->first, next, lpNSCacheData, cbDeleteNSNodeFromHeap );
215
216   /* NULL out the walking pointer */
217   lpCache->present = NULL;
218 }
219
220 /* Create and initialize a session cache */
221 BOOL NS_InitializeSessionCache( LPVOID* lplpNSInfo )
222 {
223   lpNSCache lpCache = (lpNSCache)HeapAlloc( GetProcessHeap(),
224                                             HEAP_ZERO_MEMORY,
225                                             sizeof( *lpCache ) );
226
227   *lplpNSInfo = lpCache;
228
229   if( lpCache == NULL )
230   {
231     return FALSE;
232   }
233
234   DPQ_INIT(lpCache->first);
235   lpCache->present = NULL;
236
237   return TRUE;
238 }
239
240 /* Delete a session cache */
241 void NS_DeleteSessionCache( LPVOID lpNSInfo )
242 {
243   NS_InvalidateSessionCache( (lpNSCache)lpNSInfo );
244 }
245
246 /* Reinitialize the present pointer for this cache */
247 void NS_ResetSessionEnumeration( LPVOID lpNSInfo )
248 {
249   ((lpNSCache)lpNSInfo)->present = ((lpNSCache)lpNSInfo)->first.lpQHFirst;
250 }
251
252 LPDPSESSIONDESC2 NS_WalkSessions( LPVOID lpNSInfo )
253 {
254   LPDPSESSIONDESC2 lpSessionDesc;
255   lpNSCache lpCache = (lpNSCache)lpNSInfo;
256
257   /* FIXME: The pointers could disappear when walking if a prune happens */
258
259   /* Test for end of the list */ 
260   if( lpCache->present == NULL )
261   {
262     return NULL;
263   }
264
265   lpSessionDesc = lpCache->present->data;
266
267   /* Advance tracking pointer */
268   lpCache->present = lpCache->present->next.lpQNext;
269
270   return lpSessionDesc;
271 }
272
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.
275  */
276 /* FIXME: This needs to be called by some periodic timer */
277 void NS_PruneSessionCache( LPVOID lpNSInfo )
278 {
279   lpNSCache     lpCache = lpNSInfo;
280
281   const DWORD dwPresentTime = timeGetTime();
282   const DWORD dwPrunePeriod = 60000; /* is 60 secs enough? */ 
283   const DWORD dwPruneTime   = dwPresentTime - dwPrunePeriod;
284
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.
290    */
291   for( ;; )
292   {
293     lpNSCacheData lpFirstData;
294
295     if( DPQ_IS_EMPTY(lpCache->first) )
296     {
297       /* Nothing to prune */
298       break;
299     }
300
301     if( dwPruneTime > dwPresentTime ) /* 0 <= dwPresentTime <= dwPrunePeriod */
302     {
303       if( ( DPQ_FIRST(lpCache->first)->dwTime <= dwPresentTime ) ||
304           ( DPQ_FIRST(lpCache->first)->dwTime > dwPruneTime )
305         )
306       {
307         /* Less than dwPrunePeriod old - keep */
308         break; 
309       }
310     }
311     else /* dwPrunePeriod <= dwPresentTime <= max dword */
312     {
313       if( ( DPQ_FIRST(lpCache->first)->dwTime <= dwPresentTime ) &&
314           ( DPQ_FIRST(lpCache->first)->dwTime > dwPruneTime ) 
315         )
316       {
317         /* Less than dwPrunePeriod old - keep */
318         break;
319       }
320     }
321
322     lpFirstData = DPQ_FIRST(lpCache->first);
323     DPQ_REMOVE( lpCache->first, DPQ_FIRST(lpCache->first), next );
324     cbDeleteNSNodeFromHeap( lpFirstData );
325   }
326
327 }
328
329 /* NAME SERVER Message stuff */
330 void NS_ReplyToEnumSessionsRequest( LPVOID lpMsg, 
331                                     LPDPSP_REPLYDATA lpReplyData,
332                                     IDirectPlay2Impl* lpDP )
333 {
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 */
339
340   FIXME( ": few fixed + need to check request for response\n" );
341
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;
348
349   dwVariableSize = dwVariableLen * sizeof( WCHAR );
350
351   lpReplyData->dwMessageSize = lpDP->dp2->spData.dwSPHeaderSize +
352                                  sizeof( *rmsg ) + dwVariableSize;
353   lpReplyData->lpMessage     = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
354                                           lpReplyData->dwMessageSize );
355
356   rmsg = (LPDPMSG_ENUMSESSIONSREPLY)( (BYTE*)lpReplyData->lpMessage + 
357                                              lpDP->dp2->spData.dwSPHeaderSize);
358
359   rmsg->envelope.dwMagic    = DPMSGMAGIC_DPLAYMSG; 
360   rmsg->envelope.wCommandId = DPMSGCMD_ENUMSESSIONSREPLY;
361   rmsg->envelope.wVersion   = DPMSGVER_DP6;
362
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 );
371 }