crypt32: Avoid printing pointer differences in traces.
[wine] / dlls / dplayx / name_server.c
index 07a66dc..928e082 100644 (file)
@@ -1,35 +1,45 @@
 /* DPLAYX.DLL name server implementation
  *
- * Copyright 2000 - Peter Hunnisett
+ * Copyright 2000-2001 - Peter Hunnisett
  *
- * <presently under construction - contact hunnise@nortelnetworks.com>
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
  *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  */
+
 /* NOTE: Methods with the NS_ prefix are name server methods */
 
+#include <stdarg.h>
+#include <string.h>
+
+#define NONAMELESSUNION
+#define NONAMELESSSTRUCT
+#include "windef.h"
 #include "winbase.h"
-#include "debugtools.h"
-#include "heap.h"
+#include "winnls.h"
+#include "wine/unicode.h"
+#include "wine/debug.h"
+#include "mmsystem.h"
 
 #include "dplayx_global.h"
 #include "name_server.h"
-#include "dplaysp.h"
+#include "wine/dplaysp.h"
 #include "dplayx_messages.h"
 #include "dplayx_queue.h"
 
-/* Can't seem to solve unresolved reference even with import */
-#define HACK_TIMEGETTIME
-
-#if defined( HACK_TIMEGETTIME )
-static DWORD timeGetTime(void);
-#else
-#include "mmsystem.h"
-#endif
-
 /* FIXME: Need to create a crit section, store and use it */
 
-DEFAULT_DEBUG_CHANNEL(dplay);
+WINE_DEFAULT_DEBUG_CHANNEL(dplay);
 
 /* NS specific structures */
 struct NSCacheData
@@ -49,35 +59,67 @@ struct NSCache
   lpNSCacheData present; /* keep track of what is to be looked at when walking */
 
   DPQ_HEAD(NSCacheData) first;
-}; 
+
+  BOOL bNsIsLocal;
+  LPVOID lpLocalAddrHdr;  /* FIXME: Not yet used */
+  LPVOID lpRemoteAddrHdr; /* FIXME: Not yet used */
+};
 typedef struct NSCache NSCache, *lpNSCache;
 
-/* Name Server functions 
- * --------------------- 
+/* Function prototypes */
+static DPQ_DECL_DELETECB( cbDeleteNSNodeFromHeap, lpNSCacheData );
+
+/* Name Server functions
+ * ---------------------
  */
-void NS_SetLocalComputerAsNameServer( LPCDPSESSIONDESC2 lpsd )
+void NS_SetLocalComputerAsNameServer( LPCDPSESSIONDESC2 lpsd, LPVOID lpNSInfo )
 {
 #if 0
+  /* FIXME: Remove this method? */
   DPLAYX_SetLocalSession( lpsd );
 #endif
+  lpNSCache lpCache = (lpNSCache)lpNSInfo;
+
+  lpCache->bNsIsLocal = TRUE;
+}
+
+void NS_SetRemoteComputerAsNameServer( LPCDPSESSIONDESC2 lpsd, LPVOID lpNSInfo )
+{
+  lpNSCache lpCache = (lpNSCache)lpNSInfo;
+
+  lpCache->bNsIsLocal = FALSE;
+}
+
+static DPQ_DECL_COMPARECB( cbUglyPig, GUID )
+{
+  return IsEqualGUID( elem1, elem2 );
 }
 
 /* Store the given NS remote address for future reference */
-void NS_SetRemoteComputerAsNameServer( LPVOID                    lpNSAddrHdr,
-                                       DWORD                     dwHdrSize,
-                                       LPDPMSG_ENUMSESSIONSREPLY lpMsg,
-                                       LPVOID                    lpNSInfo )
+void NS_AddRemoteComputerAsNameServer( LPCVOID                      lpcNSAddrHdr,
+                                       DWORD                        dwHdrSize,
+                                       LPCDPMSG_ENUMSESSIONSREPLY   lpcMsg,
+                                       LPVOID                       lpNSInfo )
 {
+  DWORD len;
   lpNSCache     lpCache = (lpNSCache)lpNSInfo;
   lpNSCacheData lpCacheNode;
 
-  TRACE( "%p, %p, %p\n", lpNSAddrHdr, lpMsg, lpNSInfo );
+  TRACE( "%p, %p, %p\n", lpcNSAddrHdr, lpcMsg, lpNSInfo );
 
-  /* FIXME: Should check to see if the reply is for an existing session. If
-   *        so we just update the contents and update the timestamp.
-   */
-  lpCacheNode = (lpNSCacheData)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 
-                                          sizeof( *lpCacheNode ) );
+  /* See if we can find this session. If we can, remove it as it's a dup */
+  DPQ_REMOVE_ENTRY_CB( lpCache->first, next, data->guidInstance, cbUglyPig,
+                       lpcMsg->sd.guidInstance, lpCacheNode );
+
+  if( lpCacheNode != NULL )
+  {
+    TRACE( "Duplicate session entry for %s removed - updated version kept\n",
+           debugstr_guid( &lpCacheNode->data->guidInstance ) );
+    cbDeleteNSNodeFromHeap( lpCacheNode );
+  }
+
+  /* Add this to the list */
+  lpCacheNode = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof( *lpCacheNode ) );
 
   if( lpCacheNode == NULL )
   {
@@ -87,23 +129,24 @@ void NS_SetRemoteComputerAsNameServer( LPVOID                    lpNSAddrHdr,
 
   lpCacheNode->lpNSAddrHdr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
                                         dwHdrSize );
-  CopyMemory( lpCacheNode->lpNSAddrHdr, lpNSAddrHdr, dwHdrSize );
-              
+  CopyMemory( lpCacheNode->lpNSAddrHdr, lpcNSAddrHdr, dwHdrSize );
 
-  lpCacheNode->data = (LPDPSESSIONDESC2)HeapAlloc( GetProcessHeap(),
-                                                   HEAP_ZERO_MEMORY, 
-                                                   sizeof( *lpCacheNode->data ) );
+  lpCacheNode->data = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof( *(lpCacheNode->data) ) );
 
   if( lpCacheNode->data == NULL )
   {
     ERR( "no memory for SESSIONDESC2\n" );
+    HeapFree( GetProcessHeap(), 0, lpCacheNode );
     return;
   }
 
-  CopyMemory( lpCacheNode->data, &lpMsg->sd, sizeof( *lpCacheNode->data ) );
-  lpCacheNode->data->sess.lpszSessionNameA = HEAP_strdupWtoA( GetProcessHeap(),
-                                                              HEAP_ZERO_MEMORY, 
-                                                              (LPWSTR)(lpMsg+1) );
+  *lpCacheNode->data = lpcMsg->sd;
+  len = WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)(lpcMsg+1), -1, NULL, 0, NULL, NULL );
+  if ((lpCacheNode->data->u1.lpszSessionNameA = HeapAlloc( GetProcessHeap(), 0, len )))
+  {
+      WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)(lpcMsg+1), -1,
+                           lpCacheNode->data->u1.lpszSessionNameA, len, NULL, NULL );
+  }
 
   lpCacheNode->dwTime = timeGetTime();
 
@@ -111,8 +154,8 @@ void NS_SetRemoteComputerAsNameServer( LPVOID                    lpNSAddrHdr,
 
   lpCache->present = lpCacheNode;
 
-  /* Use this message as an oportunity to weed out any old sessions so 
-   * that we don't enum them again 
+  /* Use this message as an opportunity to weed out any old sessions so
+   * that we don't enum them again
    */
   NS_PruneSessionCache( lpNSInfo );
 }
@@ -125,9 +168,43 @@ LPVOID NS_GetNSAddr( LPVOID lpNSInfo )
 
   /* Ok. Cheat and don't search for the correct stuff just take the first.
    * FIXME: In the future how are we to know what is _THE_ enum we used?
+   *        This is going to have to go into dplay somehow. Perhaps it
+   *        comes back with app server id for the join command! Oh... that
+   *        must be it. That would make this method obsolete once that's
+   *        in place.
    */
-
+#if 1
   return lpCache->first.lpQHFirst->lpNSAddrHdr;
+#else
+  /* FIXME: Should convert over to this */
+  return lpCache->bNsIsLocal ? lpCache->lpLocalAddrHdr
+                             : lpCache->lpRemoteAddrHdr;
+#endif
+}
+
+/* Get the magic number associated with the Name Server */
+DWORD NS_GetNsMagic( LPVOID lpNSInfo )
+{
+  LPDWORD lpHdrInfo = (LPDWORD)NS_GetNSAddr( lpNSInfo );
+
+  return lpHdrInfo[1];
+}
+
+/* Get the magic number associated with the non NS end */
+DWORD NS_GetOtherMagic( LPVOID lpNSInfo )
+{
+  lpNSCache lpCache = (lpNSCache)lpNSInfo;
+
+  return ((LPDWORD)lpCache->lpLocalAddrHdr)[1];
+}
+
+void NS_SetLocalAddr( LPVOID lpNSInfo, LPCVOID lpHdr, DWORD dwHdrSize )
+{
+  lpNSCache lpCache = (lpNSCache)lpNSInfo;
+
+  lpCache->lpLocalAddrHdr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, dwHdrSize );
+
+  CopyMemory( lpCache->lpLocalAddrHdr, lpHdr, dwHdrSize );
 }
 
 /* This function is responsible for sending a request for all other known
@@ -135,8 +212,8 @@ LPVOID NS_GetNSAddr( LPVOID lpNSInfo )
  */
 HRESULT NS_SendSessionRequestBroadcast( LPCGUID lpcGuid,
                                         DWORD dwFlags,
-                                        LPSPINITDATA lpSpData )
-                                     
+                                        const SPINITDATA *lpSpData )
+
 {
   DPSP_ENUMSESSIONSDATA data;
   LPDPMSG_ENUMSESSIONSREQUEST lpMsg;
@@ -147,15 +224,15 @@ HRESULT NS_SendSessionRequestBroadcast( LPCGUID lpcGuid,
   FIXME( ": not all data fields are correct\n" );
 
   data.dwMessageSize = lpSpData->dwSPHeaderSize + sizeof( *lpMsg ); /*FIXME!*/
-  data.lpMessage = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 
+  data.lpMessage = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
                               data.dwMessageSize );
   data.lpISP = lpSpData->lpISP;
   data.bReturnStatus = (dwFlags & DPENUMSESSIONS_RETURNSTATUS) ? TRUE : FALSE;
 
 
-  lpMsg = (LPDPMSG_ENUMSESSIONSREQUEST)(((BYTE*)data.lpMessage)+lpSpData->dwSPHeaderSize); 
+  lpMsg = (LPDPMSG_ENUMSESSIONSREQUEST)(((BYTE*)data.lpMessage)+lpSpData->dwSPHeaderSize);
 
-  /* Setup EnumSession reqest message */
+  /* Setup EnumSession request message */
   lpMsg->envelope.dwMagic    = DPMSGMAGIC_DPLAYMSG;
   lpMsg->envelope.wCommandId = DPMSGCMD_ENUMSESSIONSREQUEST;
   lpMsg->envelope.wVersion   = DPMSGVER_DP6;
@@ -163,22 +240,22 @@ HRESULT NS_SendSessionRequestBroadcast( LPCGUID lpcGuid,
   lpMsg->dwPasswordSize = 0; /* FIXME: If enumerating passwords..? */
   lpMsg->dwFlags        = dwFlags;
 
-  CopyMemory( &lpMsg->guidApplication, lpcGuid, sizeof( *lpcGuid ) );
+  lpMsg->guidApplication = *lpcGuid;
 
-  return (lpSpData->lpCB->EnumSessions)( &data ); 
+  return (lpSpData->lpCB->EnumSessions)( &data );
 }
 
-DPQ_DECL_DELETECB( cbDeleteNSNodeFromHeap, lpNSCacheData );
-DPQ_DECL_DELETECB( cbDeleteNSNodeFromHeap, lpNSCacheData )
+/* Delete a name server node which has been allocated on the heap */
+static DPQ_DECL_DELETECB( cbDeleteNSNodeFromHeap, lpNSCacheData )
 {
+  /* NOTE: This proc doesn't deal with the walking pointer */
+
   /* FIXME: Memory leak on data (contained ptrs) */
   HeapFree( GetProcessHeap(), 0, elem->data );
   HeapFree( GetProcessHeap(), 0, elem->lpNSAddrHdr );
   HeapFree( GetProcessHeap(), 0, elem );
 }
 
-
-
 /* Render all data in a session cache invalid */
 void NS_InvalidateSessionCache( LPVOID lpNSInfo )
 {
@@ -186,7 +263,7 @@ void NS_InvalidateSessionCache( LPVOID lpNSInfo )
 
   if( lpCache == NULL )
   {
-    ERR( ": invalidate non existant cache\n" );
+    ERR( ": invalidate nonexistent cache\n" );
     return;
   }
 
@@ -194,14 +271,15 @@ void NS_InvalidateSessionCache( LPVOID lpNSInfo )
 
   /* NULL out the walking pointer */
   lpCache->present = NULL;
+
+  lpCache->bNsIsLocal = FALSE;
+
 }
 
 /* Create and initialize a session cache */
 BOOL NS_InitializeSessionCache( LPVOID* lplpNSInfo )
 {
-  lpNSCache lpCache = (lpNSCache)HeapAlloc( GetProcessHeap(),
-                                            HEAP_ZERO_MEMORY,
-                                            sizeof( *lpCache ) );
+  lpNSCache lpCache = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof( *lpCache ) );
 
   *lplpNSInfo = lpCache;
 
@@ -213,6 +291,8 @@ BOOL NS_InitializeSessionCache( LPVOID* lplpNSInfo )
   DPQ_INIT(lpCache->first);
   lpCache->present = NULL;
 
+  lpCache->bNsIsLocal = FALSE;
+
   return TRUE;
 }
 
@@ -225,7 +305,6 @@ void NS_DeleteSessionCache( LPVOID lpNSInfo )
 /* Reinitialize the present pointer for this cache */
 void NS_ResetSessionEnumeration( LPVOID lpNSInfo )
 {
   ((lpNSCache)lpNSInfo)->present = ((lpNSCache)lpNSInfo)->first.lpQHFirst;
 }
 
@@ -236,7 +315,7 @@ LPDPSESSIONDESC2 NS_WalkSessions( LPVOID lpNSInfo )
 
   /* FIXME: The pointers could disappear when walking if a prune happens */
 
-  /* Test for end of the list */ 
+  /* Test for end of the list */
   if( lpCache->present == NULL )
   {
     return NULL;
@@ -253,85 +332,94 @@ LPDPSESSIONDESC2 NS_WalkSessions( LPVOID lpNSInfo )
 /* This method should check to see if there are any sessions which are
  * older than the criteria. If so, just delete that information.
  */
+/* FIXME: This needs to be called by some periodic timer */
 void NS_PruneSessionCache( LPVOID lpNSInfo )
 {
   lpNSCache     lpCache = lpNSInfo;
-  lpNSCacheData lpCacheEntry;
 
-  DWORD dwPresentTime = timeGetTime();
-#if defined( HACK_TIMEGETTIME )
-  DWORD dwPruneTime   = dwPresentTime - 2; /* One iteration with safety */
-#else
-  DWORD dwPruneTime   = dwPresentTime - 10000 /* 10 secs? */;
-#endif
+  const DWORD dwPresentTime = timeGetTime();
+  const DWORD dwPrunePeriod = DPMSG_WAIT_60_SECS; /* is 60 secs enough? */
 
-  FIXME( ": semi stub\n" );
-
-  /* FIXME: This doesn't handle time roll over correctly */
-  /* FIXME: Session memory leak on delete */
-  do
+  /* This silly little algorithm is based on the fact we keep entries in
+   * the queue in a time based order. It also assumes that it is not possible
+   * to wrap around over yourself (which is not unreasonable).
+   * The if statements verify if the first entry in the queue is less
+   * than dwPrunePeriod old depending on the "clock" roll over.
+   */
+  for( ;; )
   {
-    DPQ_FIND_ENTRY( lpCache->first, next, dwTime, <=, dwPruneTime, lpCacheEntry );
+    lpNSCacheData lpFirstData;
+
+    if( DPQ_IS_EMPTY(lpCache->first) )
+    {
+      /* Nothing to prune */
+      break;
+    }
+
+    /* Deal with time in a wrap around safe manner - unsigned arithmetic.
+     * Check the difference in time */
+    if( (dwPresentTime - (DPQ_FIRST(lpCache->first)->dwTime)) < dwPrunePeriod )
+    {
+      /* First entry has not expired yet; don't prune */
+      break;
+    }
+
+    lpFirstData = DPQ_FIRST(lpCache->first);
+    DPQ_REMOVE( lpCache->first, DPQ_FIRST(lpCache->first), next );
+    cbDeleteNSNodeFromHeap( lpFirstData );
   }
-  while( lpCacheEntry != NULL );
 
 }
 
-
-
-/* Message stuff */
-void NS_ReplyToEnumSessionsRequest( LPVOID lpMsg, 
-                                    LPDPSP_REPLYDATA lpReplyData,
+/* NAME SERVER Message stuff */
+void NS_ReplyToEnumSessionsRequest( LPCVOID lpcMsg,
+                                    LPVOID* lplpReplyData,
+                                    LPDWORD lpdwReplySize,
                                     IDirectPlay2Impl* lpDP )
 {
   LPDPMSG_ENUMSESSIONSREPLY rmsg;
   DWORD dwVariableSize;
   DWORD dwVariableLen;
-  LPWSTR string;
-  /* LPDPMSG_ENUMSESSIONSREQUEST msg = (LPDPMSG_ENUMSESSIONSREQUEST)lpMsg; */
+  /* LPCDPMSG_ENUMSESSIONSREQUEST msg = (LPDPMSG_ENUMSESSIONSREQUEST)lpcMsg; */
   BOOL bAnsi = TRUE; /* FIXME: This needs to be in the DPLAY interface */
 
   FIXME( ": few fixed + need to check request for response\n" );
 
-  dwVariableLen = bAnsi ? lstrlenA( lpDP->dp2->lpSessionDesc->sess.lpszSessionNameA ) + 1 
-                         : lstrlenW( lpDP->dp2->lpSessionDesc->sess.lpszSessionName ) + 1;
+  if (bAnsi)
+  {
+      dwVariableLen = MultiByteToWideChar( CP_ACP, 0,
+                                           lpDP->dp2->lpSessionDesc->u1.lpszSessionNameA,
+                                           -1, NULL, 0 );
+  }
+  else
+  {
+      dwVariableLen = strlenW( lpDP->dp2->lpSessionDesc->u1.lpszSessionName ) + 1;
+  }
 
   dwVariableSize = dwVariableLen * sizeof( WCHAR );
 
-  lpReplyData->dwMessageSize = lpDP->dp2->spData.dwSPHeaderSize +
-                                 sizeof( *rmsg ) + dwVariableSize;
-  lpReplyData->lpMessage     = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
-                                          lpReplyData->dwMessageSize );
+  *lpdwReplySize = lpDP->dp2->spData.dwSPHeaderSize +
+                     sizeof( *rmsg ) + dwVariableSize;
+  *lplpReplyData = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
+                              *lpdwReplySize );
 
-  rmsg = (LPDPMSG_ENUMSESSIONSREPLY)lpReplyData->lpMessage;  
+  rmsg = (LPDPMSG_ENUMSESSIONSREPLY)( (BYTE*)*lplpReplyData +
+                                             lpDP->dp2->spData.dwSPHeaderSize);
 
-  rmsg->envelope.dwMagic    = DPMSGMAGIC_DPLAYMSG; 
+  rmsg->envelope.dwMagic    = DPMSGMAGIC_DPLAYMSG;
   rmsg->envelope.wCommandId = DPMSGCMD_ENUMSESSIONSREPLY;
   rmsg->envelope.wVersion   = DPMSGVER_DP6;
 
-  CopyMemory( &rmsg->sd, lpDP->dp2->lpSessionDesc, 
-              sizeof( lpDP->dp2->lpSessionDesc->dwSize ) ); 
+  CopyMemory( &rmsg->sd, lpDP->dp2->lpSessionDesc,
+              lpDP->dp2->lpSessionDesc->dwSize );
   rmsg->dwUnknown = 0x0000005c;
   if( bAnsi )
   {
-    string = HEAP_strdupAtoW( GetProcessHeap(), 0, 
-                              lpDP->dp2->lpSessionDesc->sess.lpszSessionNameA );
-    /* FIXME: Memory leak */
+      MultiByteToWideChar( CP_ACP, 0, lpDP->dp2->lpSessionDesc->u1.lpszSessionNameA, -1,
+                           (LPWSTR)(rmsg+1), dwVariableLen );
   }
   else
   {
-    string = lpDP->dp2->lpSessionDesc->sess.lpszSessionName;
+      strcpyW( (LPWSTR)(rmsg+1), lpDP->dp2->lpSessionDesc->u1.lpszSessionName );
   }
-
-  lstrcpyW( (LPWSTR)rmsg+1, string );
 }
-
-#if defined( HACK_TIMEGETTIME )
-DWORD timeGetTime(void)
-{
-  static DWORD time = 0;
-  
-  return time++;
-}
-#endif