name_server.c 11.8 KB
Newer Older
1 2
/* DPLAYX.DLL name server implementation
 *
3
 * Copyright 2000-2001 - Peter Hunnisett
4
 *
5 6 7 8 9 10 11 12 13 14 15 16
 * 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
17
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18
 */
19

20 21
/* NOTE: Methods with the NS_ prefix are name server methods */

22
#include <stdarg.h>
23
#include <string.h>
24 25 26

#define NONAMELESSUNION
#define NONAMELESSSTRUCT
27
#include "windef.h"
28
#include "winbase.h"
29 30
#include "winnls.h"
#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 "wine/dplaysp.h"
37 38
#include "dplayx_messages.h"
#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 51
  LPDPSESSIONDESC2 data;

52
  LPVOID lpNSAddrHdr;
53

54 55
};
typedef struct NSCacheData NSCacheData, *lpNSCacheData;
56

57 58 59
struct NSCache
{
  lpNSCacheData present; /* keep track of what is to be looked at when walking */
60

61
  DPQ_HEAD(NSCacheData) first;
62 63 64 65

  BOOL bNsIsLocal;
  LPVOID lpLocalAddrHdr;  /* FIXME: Not yet used */
  LPVOID lpRemoteAddrHdr; /* FIXME: Not yet used */
66
};
67
typedef struct NSCache NSCache, *lpNSCache;
68

69
/* Function prototypes */
70
static DPQ_DECL_DELETECB( cbDeleteNSNodeFromHeap, lpNSCacheData );
71

72 73
/* Name Server functions
 * ---------------------
74
 */
75
void NS_SetLocalComputerAsNameServer( LPCDPSESSIONDESC2 lpsd, LPVOID lpNSInfo )
76
{
77 78 79 80 81
  lpNSCache lpCache = (lpNSCache)lpNSInfo;

  lpCache->bNsIsLocal = TRUE;
}

82
static DPQ_DECL_COMPARECB( cbUglyPig, GUID )
83 84 85 86
{
  return IsEqualGUID( elem1, elem2 );
}

87
/* Store the given NS remote address for future reference */
88 89 90 91
void NS_AddRemoteComputerAsNameServer( LPCVOID                      lpcNSAddrHdr,
                                       DWORD                        dwHdrSize,
                                       LPCDPMSG_ENUMSESSIONSREPLY   lpcMsg,
                                       LPVOID                       lpNSInfo )
92
{
93
  DWORD len;
94 95 96
  lpNSCache     lpCache = (lpNSCache)lpNSInfo;
  lpNSCacheData lpCacheNode;

97
  TRACE( "%p, %p, %p\n", lpcNSAddrHdr, lpcMsg, lpNSInfo );
98 99 100

  /* 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,
101
                       lpcMsg->sd.guidInstance, lpCacheNode );
102 103 104 105 106 107 108 109 110

  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 */
111
  lpCacheNode = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof( *lpCacheNode ) );
112 113 114 115 116 117 118 119 120

  if( lpCacheNode == NULL )
  {
    ERR( "no memory for NS node\n" );
    return;
  }

  lpCacheNode->lpNSAddrHdr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
                                        dwHdrSize );
121
  CopyMemory( lpCacheNode->lpNSAddrHdr, lpcNSAddrHdr, dwHdrSize );
122

123
  lpCacheNode->data = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof( *(lpCacheNode->data) ) );
124 125 126 127

  if( lpCacheNode->data == NULL )
  {
    ERR( "no memory for SESSIONDESC2\n" );
128
    HeapFree( GetProcessHeap(), 0, lpCacheNode );
129 130 131
    return;
  }

132
  *lpCacheNode->data = lpcMsg->sd;
133
  len = WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)(lpcMsg+1), -1, NULL, 0, NULL, NULL );
134
  if ((lpCacheNode->data->u1.lpszSessionNameA = HeapAlloc( GetProcessHeap(), 0, len )))
135
  {
136
      WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)(lpcMsg+1), -1,
137
                           lpCacheNode->data->u1.lpszSessionNameA, len, NULL, NULL );
138
  }
139

140
  lpCacheNode->dwTime = timeGetTime();
141 142 143 144 145

  DPQ_INSERT(lpCache->first, lpCacheNode, next );

  lpCache->present = lpCacheNode;

146
  /* Use this message as an opportunity to weed out any old sessions so
147
   * that we don't enum them again
148 149 150 151 152 153 154 155 156 157 158 159
   */
  NS_PruneSessionCache( lpNSInfo );
}

LPVOID NS_GetNSAddr( LPVOID lpNSInfo )
{
  lpNSCache lpCache = (lpNSCache)lpNSInfo;

  FIXME( ":quick stub\n" );

  /* 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?
160
   *        This is going to have to go into dplay somehow. Perhaps it
161
   *        comes back with app server id for the join command! Oh... that
162 163
   *        must be it. That would make this method obsolete once that's
   *        in place.
164
   */
165
#if 1
166
  return lpCache->first.lpQHFirst->lpNSAddrHdr;
167 168 169 170 171 172 173 174 175 176
#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 )
{
177
  LPDWORD lpHdrInfo = NS_GetNSAddr( lpNSInfo );
178 179 180 181 182 183 184 185 186 187 188

  return lpHdrInfo[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 );
189 190
}

191 192 193
/* This function is responsible for sending a request for all other known
   nameservers to send us what sessions they have registered locally
 */
194 195
HRESULT NS_SendSessionRequestBroadcast( LPCGUID lpcGuid,
                                        DWORD dwFlags,
196
                                        const SPINITDATA *lpSpData )
197

198
{
199 200
  DPSP_ENUMSESSIONSDATA data;
  LPDPMSG_ENUMSESSIONSREQUEST lpMsg;
201

202 203 204 205 206 207
  TRACE( "enumerating for guid %s\n", debugstr_guid( lpcGuid ) );

  /* Get the SP to deal with sending the EnumSessions request */
  FIXME( ": not all data fields are correct\n" );

  data.dwMessageSize = lpSpData->dwSPHeaderSize + sizeof( *lpMsg ); /*FIXME!*/
208
  data.lpMessage = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
209 210
                              data.dwMessageSize );
  data.lpISP = lpSpData->lpISP;
211
  data.bReturnStatus = (dwFlags & DPENUMSESSIONS_RETURNSTATUS) != 0;
212 213


214
  lpMsg = (LPDPMSG_ENUMSESSIONSREQUEST)(((BYTE*)data.lpMessage)+lpSpData->dwSPHeaderSize);
215

216
  /* Setup EnumSession request message */
217 218 219 220 221 222 223
  lpMsg->envelope.dwMagic    = DPMSGMAGIC_DPLAYMSG;
  lpMsg->envelope.wCommandId = DPMSGCMD_ENUMSESSIONSREQUEST;
  lpMsg->envelope.wVersion   = DPMSGVER_DP6;

  lpMsg->dwPasswordSize = 0; /* FIXME: If enumerating passwords..? */
  lpMsg->dwFlags        = dwFlags;

224
  lpMsg->guidApplication = *lpcGuid;
225

226
  return (lpSpData->lpCB->EnumSessions)( &data );
227 228
}

229
/* Delete a name server node which has been allocated on the heap */
230
static DPQ_DECL_DELETECB( cbDeleteNSNodeFromHeap, lpNSCacheData )
231
{
232 233
  /* NOTE: This proc doesn't deal with the walking pointer */

234 235 236 237 238 239
  /* FIXME: Memory leak on data (contained ptrs) */
  HeapFree( GetProcessHeap(), 0, elem->data );
  HeapFree( GetProcessHeap(), 0, elem->lpNSAddrHdr );
  HeapFree( GetProcessHeap(), 0, elem );
}

240
/* Render all data in a session cache invalid */
241
void NS_InvalidateSessionCache( LPVOID lpNSInfo )
242
{
243 244
  lpNSCache lpCache = (lpNSCache)lpNSInfo;

245 246
  if( lpCache == NULL )
  {
247
    ERR( ": invalidate nonexistent cache\n" );
248 249 250
    return;
  }

251
  DPQ_DELETEQ( lpCache->first, next, lpNSCacheData, cbDeleteNSNodeFromHeap );
252

253
  /* NULL out the walking pointer */
254
  lpCache->present = NULL;
255 256 257

  lpCache->bNsIsLocal = FALSE;

258 259 260 261 262
}

/* Create and initialize a session cache */
BOOL NS_InitializeSessionCache( LPVOID* lplpNSInfo )
{
263
  lpNSCache lpCache = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof( *lpCache ) );
264 265 266 267 268 269 270 271

  *lplpNSInfo = lpCache;

  if( lpCache == NULL )
  {
    return FALSE;
  }

272 273
  DPQ_INIT(lpCache->first);
  lpCache->present = NULL;
274

275 276
  lpCache->bNsIsLocal = FALSE;

277 278 279 280 281 282 283 284 285 286 287 288
  return TRUE;
}

/* Delete a session cache */
void NS_DeleteSessionCache( LPVOID lpNSInfo )
{
  NS_InvalidateSessionCache( (lpNSCache)lpNSInfo );
}

/* Reinitialize the present pointer for this cache */
void NS_ResetSessionEnumeration( LPVOID lpNSInfo )
{
289
  ((lpNSCache)lpNSInfo)->present = ((lpNSCache)lpNSInfo)->first.lpQHFirst;
290 291 292 293 294 295 296
}

LPDPSESSIONDESC2 NS_WalkSessions( LPVOID lpNSInfo )
{
  LPDPSESSIONDESC2 lpSessionDesc;
  lpNSCache lpCache = (lpNSCache)lpNSInfo;

297 298
  /* FIXME: The pointers could disappear when walking if a prune happens */

299
  /* Test for end of the list */
300 301 302 303 304 305 306 307
  if( lpCache->present == NULL )
  {
    return NULL;
  }

  lpSessionDesc = lpCache->present->data;

  /* Advance tracking pointer */
308
  lpCache->present = lpCache->present->next.lpQNext;
309 310 311

  return lpSessionDesc;
}
312 313 314 315

/* This method should check to see if there are any sessions which are
 * older than the criteria. If so, just delete that information.
 */
316
/* FIXME: This needs to be called by some periodic timer */
317 318 319 320
void NS_PruneSessionCache( LPVOID lpNSInfo )
{
  lpNSCache     lpCache = lpNSInfo;

321
  const DWORD dwPresentTime = timeGetTime();
322
  const DWORD dwPrunePeriod = DPMSG_WAIT_60_SECS; /* is 60 secs enough? */
323

324
  /* This silly little algorithm is based on the fact we keep entries in
325 326
   * the queue in a time based order. It also assumes that it is not possible
   * to wrap around over yourself (which is not unreasonable).
327
   * The if statements verify if the first entry in the queue is less
328 329 330
   * than dwPrunePeriod old depending on the "clock" roll over.
   */
  for( ;; )
331
  {
332 333 334 335 336 337 338 339
    lpNSCacheData lpFirstData;

    if( DPQ_IS_EMPTY(lpCache->first) )
    {
      /* Nothing to prune */
      break;
    }

340
    /* Deal with time in a wrap around safe manner - unsigned arithmetic.
341 342
     * Check the difference in time */
    if( (dwPresentTime - (DPQ_FIRST(lpCache->first)->dwTime)) < dwPrunePeriod )
343
    {
344 345
      /* First entry has not expired yet; don't prune */
      break;
346 347 348 349 350
    }

    lpFirstData = DPQ_FIRST(lpCache->first);
    DPQ_REMOVE( lpCache->first, DPQ_FIRST(lpCache->first), next );
    cbDeleteNSNodeFromHeap( lpFirstData );
351 352 353 354
  }

}

355
/* NAME SERVER Message stuff */
356
void NS_ReplyToEnumSessionsRequest( LPCVOID lpcMsg,
357 358
                                    LPVOID* lplpReplyData,
                                    LPDWORD lpdwReplySize,
359 360 361 362 363
                                    IDirectPlay2Impl* lpDP )
{
  LPDPMSG_ENUMSESSIONSREPLY rmsg;
  DWORD dwVariableSize;
  DWORD dwVariableLen;
364
  /* LPCDPMSG_ENUMSESSIONSREQUEST msg = (LPDPMSG_ENUMSESSIONSREQUEST)lpcMsg; */
365

366 367
  /* FIXME: Should handle ANSI or WIDECHAR input. Currently just ANSI input */
  FIXME( ": few fixed + need to check request for response, might need UNICODE input ability.\n" );
368

369 370 371
  dwVariableLen = MultiByteToWideChar( CP_ACP, 0,
                                       lpDP->dp2->lpSessionDesc->u1.lpszSessionNameA,
                                       -1, NULL, 0 );
372 373
  dwVariableSize = dwVariableLen * sizeof( WCHAR );

374 375 376 377
  *lpdwReplySize = lpDP->dp2->spData.dwSPHeaderSize +
                     sizeof( *rmsg ) + dwVariableSize;
  *lplpReplyData = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
                              *lpdwReplySize );
378

379
  rmsg = (LPDPMSG_ENUMSESSIONSREPLY)( (BYTE*)*lplpReplyData +
Peter Hunnisett's avatar
Peter Hunnisett committed
380
                                             lpDP->dp2->spData.dwSPHeaderSize);
381

382
  rmsg->envelope.dwMagic    = DPMSGMAGIC_DPLAYMSG;
383 384 385
  rmsg->envelope.wCommandId = DPMSGCMD_ENUMSESSIONSREPLY;
  rmsg->envelope.wVersion   = DPMSGVER_DP6;

386
  CopyMemory( &rmsg->sd, lpDP->dp2->lpSessionDesc,
387
              lpDP->dp2->lpSessionDesc->dwSize );
388
  rmsg->dwUnknown = 0x0000005c;
389 390
  MultiByteToWideChar( CP_ACP, 0, lpDP->dp2->lpSessionDesc->u1.lpszSessionNameA, -1,
                       (LPWSTR)(rmsg+1), dwVariableLen );
391
}