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