dsound/tests: Win64 printf format warning fixes.
[wine] / dlls / dplayx / name_server.c
1 /* DPLAYX.DLL name server implementation
2  *
3  * Copyright 2000-2001 - Peter Hunnisett
4  *
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.
9  *
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.
14  *
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
18  */
19
20 /* NOTE: Methods with the NS_ prefix are name server methods */
21
22 #include <stdarg.h>
23 #include <string.h>
24
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winnls.h"
30 #include "wine/unicode.h"
31 #include "wine/debug.h"
32 #include "mmsystem.h"
33
34 #include "dplayx_global.h"
35 #include "name_server.h"
36 #include "dplaysp.h"
37 #include "dplayx_messages.h"
38 #include "dplayx_queue.h"
39
40 /* FIXME: Need to create a crit section, store and use it */
41
42 WINE_DEFAULT_DEBUG_CHANNEL(dplay);
43
44 /* NS specific structures */
45 struct NSCacheData
46 {
47   DPQ_ENTRY(NSCacheData) next;
48
49   DWORD dwTime; /* Time at which data was last known valid */
50   LPDPSESSIONDESC2 data;
51
52   LPVOID lpNSAddrHdr;
53
54 };
55 typedef struct NSCacheData NSCacheData, *lpNSCacheData;
56
57 struct NSCache
58 {
59   lpNSCacheData present; /* keep track of what is to be looked at when walking */
60
61   DPQ_HEAD(NSCacheData) first;
62
63   BOOL bNsIsLocal;
64   LPVOID lpLocalAddrHdr;  /* FIXME: Not yet used */
65   LPVOID lpRemoteAddrHdr; /* FIXME: Not yet used */
66 };
67 typedef struct NSCache NSCache, *lpNSCache;
68
69 /* Function prototypes */
70 DPQ_DECL_DELETECB( cbDeleteNSNodeFromHeap, lpNSCacheData );
71
72 /* Name Server functions
73  * ---------------------
74  */
75 void NS_SetLocalComputerAsNameServer( LPCDPSESSIONDESC2 lpsd, LPVOID lpNSInfo )
76 {
77 #if 0
78   /* FIXME: Remove this method? */
79   DPLAYX_SetLocalSession( lpsd );
80 #endif
81   lpNSCache lpCache = (lpNSCache)lpNSInfo;
82
83   lpCache->bNsIsLocal = TRUE;
84 }
85
86 void NS_SetRemoteComputerAsNameServer( LPCDPSESSIONDESC2 lpsd, LPVOID lpNSInfo )
87 {
88   lpNSCache lpCache = (lpNSCache)lpNSInfo;
89
90   lpCache->bNsIsLocal = FALSE;
91 }
92
93 static DPQ_DECL_COMPARECB( cbUglyPig, GUID )
94 {
95   return IsEqualGUID( elem1, elem2 );
96 }
97
98 /* Store the given NS remote address for future reference */
99 void NS_AddRemoteComputerAsNameServer( LPCVOID                      lpcNSAddrHdr,
100                                        DWORD                        dwHdrSize,
101                                        LPCDPMSG_ENUMSESSIONSREPLY   lpcMsg,
102                                        LPVOID                       lpNSInfo )
103 {
104   DWORD len;
105   lpNSCache     lpCache = (lpNSCache)lpNSInfo;
106   lpNSCacheData lpCacheNode;
107
108   TRACE( "%p, %p, %p\n", lpcNSAddrHdr, lpcMsg, lpNSInfo );
109
110   /* See if we can find this session. If we can, remove it as it's a dup */
111   DPQ_REMOVE_ENTRY_CB( lpCache->first, next, data->guidInstance, cbUglyPig,
112                        lpcMsg->sd.guidInstance, lpCacheNode );
113
114   if( lpCacheNode != NULL )
115   {
116     TRACE( "Duplicate session entry for %s removed - updated version kept\n",
117            debugstr_guid( &lpCacheNode->data->guidInstance ) );
118     cbDeleteNSNodeFromHeap( lpCacheNode );
119   }
120
121   /* Add this to the list */
122   lpCacheNode = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof( *lpCacheNode ) );
123
124   if( lpCacheNode == NULL )
125   {
126     ERR( "no memory for NS node\n" );
127     return;
128   }
129
130   lpCacheNode->lpNSAddrHdr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
131                                         dwHdrSize );
132   CopyMemory( lpCacheNode->lpNSAddrHdr, lpcNSAddrHdr, dwHdrSize );
133
134   lpCacheNode->data = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof( *(lpCacheNode->data) ) );
135
136   if( lpCacheNode->data == NULL )
137   {
138     ERR( "no memory for SESSIONDESC2\n" );
139     return;
140   }
141
142   CopyMemory( lpCacheNode->data, &lpcMsg->sd, sizeof( *lpCacheNode->data ) );
143   len = WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)(lpcMsg+1), -1, NULL, 0, NULL, NULL );
144   if ((lpCacheNode->data->u1.lpszSessionNameA = HeapAlloc( GetProcessHeap(), 0, len )))
145   {
146       WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)(lpcMsg+1), -1,
147                            lpCacheNode->data->u1.lpszSessionNameA, len, NULL, NULL );
148   }
149
150   lpCacheNode->dwTime = timeGetTime();
151
152   DPQ_INSERT(lpCache->first, lpCacheNode, next );
153
154   lpCache->present = lpCacheNode;
155
156   /* Use this message as an opportunity to weed out any old sessions so
157    * that we don't enum them again
158    */
159   NS_PruneSessionCache( lpNSInfo );
160 }
161
162 LPVOID NS_GetNSAddr( LPVOID lpNSInfo )
163 {
164   lpNSCache lpCache = (lpNSCache)lpNSInfo;
165
166   FIXME( ":quick stub\n" );
167
168   /* Ok. Cheat and don't search for the correct stuff just take the first.
169    * FIXME: In the future how are we to know what is _THE_ enum we used?
170    *        This is going to have to go into dplay somehow. Perhaps it
171    *        comes back with app server id for the join command! Oh...that
172    *        must be it. That would make this method obsolete once that's
173    *        in place.
174    */
175 #if 1
176   return lpCache->first.lpQHFirst->lpNSAddrHdr;
177 #else
178   /* FIXME: Should convert over to this */
179   return lpCache->bNsIsLocal ? lpCache->lpLocalAddrHdr
180                              : lpCache->lpRemoteAddrHdr;
181 #endif
182 }
183
184 /* Get the magic number associated with the Name Server */
185 DWORD NS_GetNsMagic( LPVOID lpNSInfo )
186 {
187   LPDWORD lpHdrInfo = (LPDWORD)NS_GetNSAddr( lpNSInfo );
188
189   return lpHdrInfo[1];
190 }
191
192 /* Get the magic number associated with the non NS end */
193 DWORD NS_GetOtherMagic( LPVOID lpNSInfo )
194 {
195   lpNSCache lpCache = (lpNSCache)lpNSInfo;
196
197   return ((LPDWORD)lpCache->lpLocalAddrHdr)[1];
198 }
199
200 void NS_SetLocalAddr( LPVOID lpNSInfo, LPCVOID lpHdr, DWORD dwHdrSize )
201 {
202   lpNSCache lpCache = (lpNSCache)lpNSInfo;
203
204   lpCache->lpLocalAddrHdr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, dwHdrSize );
205
206   CopyMemory( lpCache->lpLocalAddrHdr, lpHdr, dwHdrSize );
207 }
208
209 /* This function is responsible for sending a request for all other known
210    nameservers to send us what sessions they have registered locally
211  */
212 HRESULT NS_SendSessionRequestBroadcast( LPCGUID lpcGuid,
213                                         DWORD dwFlags,
214                                         LPSPINITDATA lpSpData )
215
216 {
217   DPSP_ENUMSESSIONSDATA data;
218   LPDPMSG_ENUMSESSIONSREQUEST lpMsg;
219
220   TRACE( "enumerating for guid %s\n", debugstr_guid( lpcGuid ) );
221
222   /* Get the SP to deal with sending the EnumSessions request */
223   FIXME( ": not all data fields are correct\n" );
224
225   data.dwMessageSize = lpSpData->dwSPHeaderSize + sizeof( *lpMsg ); /*FIXME!*/
226   data.lpMessage = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
227                               data.dwMessageSize );
228   data.lpISP = lpSpData->lpISP;
229   data.bReturnStatus = (dwFlags & DPENUMSESSIONS_RETURNSTATUS) ? TRUE : FALSE;
230
231
232   lpMsg = (LPDPMSG_ENUMSESSIONSREQUEST)(((BYTE*)data.lpMessage)+lpSpData->dwSPHeaderSize);
233
234   /* Setup EnumSession request message */
235   lpMsg->envelope.dwMagic    = DPMSGMAGIC_DPLAYMSG;
236   lpMsg->envelope.wCommandId = DPMSGCMD_ENUMSESSIONSREQUEST;
237   lpMsg->envelope.wVersion   = DPMSGVER_DP6;
238
239   lpMsg->dwPasswordSize = 0; /* FIXME: If enumerating passwords..? */
240   lpMsg->dwFlags        = dwFlags;
241
242   CopyMemory( &lpMsg->guidApplication, lpcGuid, sizeof( *lpcGuid ) );
243
244   return (lpSpData->lpCB->EnumSessions)( &data );
245 }
246
247 /* Delete a name server node which has been allocated on the heap */
248 DPQ_DECL_DELETECB( cbDeleteNSNodeFromHeap, lpNSCacheData )
249 {
250   /* NOTE: This proc doesn't deal with the walking pointer */
251
252   /* FIXME: Memory leak on data (contained ptrs) */
253   HeapFree( GetProcessHeap(), 0, elem->data );
254   HeapFree( GetProcessHeap(), 0, elem->lpNSAddrHdr );
255   HeapFree( GetProcessHeap(), 0, elem );
256 }
257
258 /* Render all data in a session cache invalid */
259 void NS_InvalidateSessionCache( LPVOID lpNSInfo )
260 {
261   lpNSCache lpCache = (lpNSCache)lpNSInfo;
262
263   if( lpCache == NULL )
264   {
265     ERR( ": invalidate nonexistent cache\n" );
266     return;
267   }
268
269   DPQ_DELETEQ( lpCache->first, next, lpNSCacheData, cbDeleteNSNodeFromHeap );
270
271   /* NULL out the walking pointer */
272   lpCache->present = NULL;
273
274   lpCache->bNsIsLocal = FALSE;
275
276 }
277
278 /* Create and initialize a session cache */
279 BOOL NS_InitializeSessionCache( LPVOID* lplpNSInfo )
280 {
281   lpNSCache lpCache = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof( *lpCache ) );
282
283   *lplpNSInfo = lpCache;
284
285   if( lpCache == NULL )
286   {
287     return FALSE;
288   }
289
290   DPQ_INIT(lpCache->first);
291   lpCache->present = NULL;
292
293   lpCache->bNsIsLocal = FALSE;
294
295   return TRUE;
296 }
297
298 /* Delete a session cache */
299 void NS_DeleteSessionCache( LPVOID lpNSInfo )
300 {
301   NS_InvalidateSessionCache( (lpNSCache)lpNSInfo );
302 }
303
304 /* Reinitialize the present pointer for this cache */
305 void NS_ResetSessionEnumeration( LPVOID lpNSInfo )
306 {
307   ((lpNSCache)lpNSInfo)->present = ((lpNSCache)lpNSInfo)->first.lpQHFirst;
308 }
309
310 LPDPSESSIONDESC2 NS_WalkSessions( LPVOID lpNSInfo )
311 {
312   LPDPSESSIONDESC2 lpSessionDesc;
313   lpNSCache lpCache = (lpNSCache)lpNSInfo;
314
315   /* FIXME: The pointers could disappear when walking if a prune happens */
316
317   /* Test for end of the list */
318   if( lpCache->present == NULL )
319   {
320     return NULL;
321   }
322
323   lpSessionDesc = lpCache->present->data;
324
325   /* Advance tracking pointer */
326   lpCache->present = lpCache->present->next.lpQNext;
327
328   return lpSessionDesc;
329 }
330
331 /* This method should check to see if there are any sessions which are
332  * older than the criteria. If so, just delete that information.
333  */
334 /* FIXME: This needs to be called by some periodic timer */
335 void NS_PruneSessionCache( LPVOID lpNSInfo )
336 {
337   lpNSCache     lpCache = lpNSInfo;
338
339   const DWORD dwPresentTime = timeGetTime();
340   const DWORD dwPrunePeriod = DPMSG_WAIT_60_SECS; /* is 60 secs enough? */
341
342   /* This silly little algorithm is based on the fact we keep entries in
343    * the queue in a time based order. It also assumes that it is not possible
344    * to wrap around over yourself (which is not unreasonable).
345    * The if statements verify if the first entry in the queue is less
346    * than dwPrunePeriod old depending on the "clock" roll over.
347    */
348   for( ;; )
349   {
350     lpNSCacheData lpFirstData;
351
352     if( DPQ_IS_EMPTY(lpCache->first) )
353     {
354       /* Nothing to prune */
355       break;
356     }
357
358     /* Deal with time in a wrap around safe manner - unsigned arithmetic.
359      * Check the difference in time */
360     if( (dwPresentTime - (DPQ_FIRST(lpCache->first)->dwTime)) < dwPrunePeriod )
361     {
362       /* First entry has not expired yet; don't prune */
363       break;
364     }
365
366     lpFirstData = DPQ_FIRST(lpCache->first);
367     DPQ_REMOVE( lpCache->first, DPQ_FIRST(lpCache->first), next );
368     cbDeleteNSNodeFromHeap( lpFirstData );
369   }
370
371 }
372
373 /* NAME SERVER Message stuff */
374 void NS_ReplyToEnumSessionsRequest( LPCVOID lpcMsg,
375                                     LPVOID* lplpReplyData,
376                                     LPDWORD lpdwReplySize,
377                                     IDirectPlay2Impl* lpDP )
378 {
379   LPDPMSG_ENUMSESSIONSREPLY rmsg;
380   DWORD dwVariableSize;
381   DWORD dwVariableLen;
382   /* LPCDPMSG_ENUMSESSIONSREQUEST msg = (LPDPMSG_ENUMSESSIONSREQUEST)lpcMsg; */
383   BOOL bAnsi = TRUE; /* FIXME: This needs to be in the DPLAY interface */
384
385   FIXME( ": few fixed + need to check request for response\n" );
386
387   if (bAnsi)
388   {
389       dwVariableLen = MultiByteToWideChar( CP_ACP, 0,
390                                            lpDP->dp2->lpSessionDesc->u1.lpszSessionNameA,
391                                            -1, NULL, 0 );
392   }
393   else
394   {
395       dwVariableLen = strlenW( lpDP->dp2->lpSessionDesc->u1.lpszSessionName ) + 1;
396   }
397
398   dwVariableSize = dwVariableLen * sizeof( WCHAR );
399
400   *lpdwReplySize = lpDP->dp2->spData.dwSPHeaderSize +
401                      sizeof( *rmsg ) + dwVariableSize;
402   *lplpReplyData = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
403                               *lpdwReplySize );
404
405   rmsg = (LPDPMSG_ENUMSESSIONSREPLY)( (BYTE*)*lplpReplyData +
406                                              lpDP->dp2->spData.dwSPHeaderSize);
407
408   rmsg->envelope.dwMagic    = DPMSGMAGIC_DPLAYMSG;
409   rmsg->envelope.wCommandId = DPMSGCMD_ENUMSESSIONSREPLY;
410   rmsg->envelope.wVersion   = DPMSGVER_DP6;
411
412   CopyMemory( &rmsg->sd, lpDP->dp2->lpSessionDesc,
413               sizeof( lpDP->dp2->lpSessionDesc->dwSize ) );
414   rmsg->dwUnknown = 0x0000005c;
415   if( bAnsi )
416   {
417       MultiByteToWideChar( CP_ACP, 0, lpDP->dp2->lpSessionDesc->u1.lpszSessionNameA, -1,
418                            (LPWSTR)(rmsg+1), dwVariableLen );
419   }
420   else
421   {
422       strcpyW( (LPWSTR)(rmsg+1), lpDP->dp2->lpSessionDesc->u1.lpszSessionName );
423   }
424 }