name_server.c 12.8 KB
Newer Older
1 2
/* DPLAYX.DLL name server implementation
 *
3
 * Copyright 2000-2001 - Peter Hunnisett
4 5 6
 *
 * <presently under construction - contact hunnise@nortelnetworks.com>
 *
7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
 */
21

22 23
/* NOTE: Methods with the NS_ prefix are name server methods */

24
#include <string.h>
25 26 27

#define NONAMELESSUNION
#define NONAMELESSSTRUCT
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 37 38
#include "dplaysp.h"
#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 70 71
/* Function prototypes */
DPQ_DECL_DELETECB( cbDeleteNSNodeFromHeap, lpNSCacheData );

72 73
/* Name Server functions
 * ---------------------
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 82 83 84 85 86 87 88 89 90
  lpNSCache lpCache = (lpNSCache)lpNSInfo;

  lpCache->bNsIsLocal = TRUE;
}

void NS_SetRemoteComputerAsNameServer( LPCDPSESSIONDESC2 lpsd, LPVOID lpNSInfo )
{
  lpNSCache lpCache = (lpNSCache)lpNSInfo;

  lpCache->bNsIsLocal = FALSE;
91 92
}

93 94 95 96 97
DPQ_DECL_COMPARECB( cbUglyPig, GUID )
{
  return IsEqualGUID( elem1, elem2 );
}

98
/* Store the given NS remote address for future reference */
99 100
/* FIXME: LPDPMSG_ENUMSESSIONSREPLY should be const */
void NS_AddRemoteComputerAsNameServer( LPCVOID                   lpcNSAddrHdr,
101 102 103 104
                                       DWORD                     dwHdrSize,
                                       LPDPMSG_ENUMSESSIONSREPLY lpMsg,
                                       LPVOID                    lpNSInfo )
{
105
  DWORD len;
106 107 108
  lpNSCache     lpCache = (lpNSCache)lpNSInfo;
  lpNSCacheData lpCacheNode;

109
  TRACE( "%p, %p, %p\n", lpcNSAddrHdr, lpMsg, lpNSInfo );
110 111 112

  /* 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,
113
                       lpMsg->sd.guidInstance, lpCacheNode );
114 115 116 117 118 119 120 121 122

  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 */
123
  lpCacheNode = (lpNSCacheData)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
124 125 126 127 128 129 130 131 132 133
                                          sizeof( *lpCacheNode ) );

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

  lpCacheNode->lpNSAddrHdr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
                                        dwHdrSize );
134
  CopyMemory( lpCacheNode->lpNSAddrHdr, lpcNSAddrHdr, dwHdrSize );
135

136 137

  lpCacheNode->data = (LPDPSESSIONDESC2)HeapAlloc( GetProcessHeap(),
138
                                                   HEAP_ZERO_MEMORY,
139
                                                   sizeof( *(lpCacheNode->data) ) );
140 141 142 143 144 145 146 147

  if( lpCacheNode->data == NULL )
  {
    ERR( "no memory for SESSIONDESC2\n" );
    return;
  }

  CopyMemory( lpCacheNode->data, &lpMsg->sd, sizeof( *lpCacheNode->data ) );
148
  len = WideCharToMultiByte( CP_ACP, 0, (LPWSTR)(lpMsg+1), -1, NULL, 0, NULL, NULL );
149
  if ((lpCacheNode->data->u1.lpszSessionNameA = HeapAlloc( GetProcessHeap(), 0, len )))
150
  {
151
      WideCharToMultiByte( CP_ACP, 0, (LPWSTR)(lpMsg+1), -1,
152
                           lpCacheNode->data->u1.lpszSessionNameA, len, NULL, NULL );
153
  }
154

155
  lpCacheNode->dwTime = timeGetTime();
156 157 158 159 160

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

  lpCache->present = lpCacheNode;

161
  /* Use this message as an opportunity to weed out any old sessions so
162
   * that we don't enum them again
163 164 165 166 167 168 169 170 171 172 173 174
   */
  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?
175 176 177 178
   *        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.
179
   */
180
#if 1
181
  return lpCache->first.lpQHFirst->lpNSAddrHdr;
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
#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 );
212 213
}

214 215 216
/* This function is responsible for sending a request for all other known
   nameservers to send us what sessions they have registered locally
 */
217 218 219
HRESULT NS_SendSessionRequestBroadcast( LPCGUID lpcGuid,
                                        DWORD dwFlags,
                                        LPSPINITDATA lpSpData )
220

221
{
222 223
  DPSP_ENUMSESSIONSDATA data;
  LPDPMSG_ENUMSESSIONSREQUEST lpMsg;
224

225 226 227 228 229 230
  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!*/
231
  data.lpMessage = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
232 233 234 235 236
                              data.dwMessageSize );
  data.lpISP = lpSpData->lpISP;
  data.bReturnStatus = (dwFlags & DPENUMSESSIONS_RETURNSTATUS) ? TRUE : FALSE;


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

239
  /* Setup EnumSession request message */
240 241 242 243 244 245 246 247
  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;

  CopyMemory( &lpMsg->guidApplication, lpcGuid, sizeof( *lpcGuid ) );
248

249
  return (lpSpData->lpCB->EnumSessions)( &data );
250 251
}

252
/* Delete a name server node which has been allocated on the heap */
253 254
DPQ_DECL_DELETECB( cbDeleteNSNodeFromHeap, lpNSCacheData )
{
255 256
  /* NOTE: This proc doesn't deal with the walking pointer */

257 258 259 260 261 262
  /* FIXME: Memory leak on data (contained ptrs) */
  HeapFree( GetProcessHeap(), 0, elem->data );
  HeapFree( GetProcessHeap(), 0, elem->lpNSAddrHdr );
  HeapFree( GetProcessHeap(), 0, elem );
}

263
/* Render all data in a session cache invalid */
264
void NS_InvalidateSessionCache( LPVOID lpNSInfo )
265
{
266 267
  lpNSCache lpCache = (lpNSCache)lpNSInfo;

268 269
  if( lpCache == NULL )
  {
270
    ERR( ": invalidate non existent cache\n" );
271 272 273
    return;
  }

274
  DPQ_DELETEQ( lpCache->first, next, lpNSCacheData, cbDeleteNSNodeFromHeap );
275

276
  /* NULL out the walking pointer */
277
  lpCache->present = NULL;
278 279 280

  lpCache->bNsIsLocal = FALSE;

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
}

/* Create and initialize a session cache */
BOOL NS_InitializeSessionCache( LPVOID* lplpNSInfo )
{
  lpNSCache lpCache = (lpNSCache)HeapAlloc( GetProcessHeap(),
                                            HEAP_ZERO_MEMORY,
                                            sizeof( *lpCache ) );

  *lplpNSInfo = lpCache;

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

297 298
  DPQ_INIT(lpCache->first);
  lpCache->present = NULL;
299

300 301
  lpCache->bNsIsLocal = FALSE;

302 303 304 305 306 307 308 309 310 311 312 313
  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 )
{
314
  ((lpNSCache)lpNSInfo)->present = ((lpNSCache)lpNSInfo)->first.lpQHFirst;
315 316 317 318 319 320 321
}

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

322 323
  /* FIXME: The pointers could disappear when walking if a prune happens */

324
  /* Test for end of the list */
325 326 327 328 329 330 331 332
  if( lpCache->present == NULL )
  {
    return NULL;
  }

  lpSessionDesc = lpCache->present->data;

  /* Advance tracking pointer */
333
  lpCache->present = lpCache->present->next.lpQNext;
334 335 336

  return lpSessionDesc;
}
337 338 339 340

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

346
  const DWORD dwPresentTime = timeGetTime();
347
  const DWORD dwPrunePeriod = DPMSG_WAIT_60_SECS; /* is 60 secs enough? */
348

349
  /* This silly little algorithm is based on the fact we keep entries in
350 351
   * the queue in a time based order. It also assumes that it is not possible
   * to wrap around over yourself (which is not unreasonable).
352
   * The if statements verify if the first entry in the queue is less
353 354 355
   * than dwPrunePeriod old depending on the "clock" roll over.
   */
  for( ;; )
356
  {
357 358 359 360 361 362 363 364
    lpNSCacheData lpFirstData;

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

365
    /* Deal with time in a wrap around safe manner - unsigned arithmetic.
366 367
     * Check the difference in time */
    if( (dwPresentTime - (DPQ_FIRST(lpCache->first)->dwTime)) < dwPrunePeriod )
368
    {
369 370
      /* First entry has not expired yet; don't prune */
      break;
371 372 373 374 375
    }

    lpFirstData = DPQ_FIRST(lpCache->first);
    DPQ_REMOVE( lpCache->first, DPQ_FIRST(lpCache->first), next );
    cbDeleteNSNodeFromHeap( lpFirstData );
376 377 378 379
  }

}

380
/* NAME SERVER Message stuff */
381
void NS_ReplyToEnumSessionsRequest( LPCVOID lpcMsg,
382 383
                                    LPVOID* lplpReplyData,
                                    LPDWORD lpdwReplySize,
384 385 386 387 388
                                    IDirectPlay2Impl* lpDP )
{
  LPDPMSG_ENUMSESSIONSREPLY rmsg;
  DWORD dwVariableSize;
  DWORD dwVariableLen;
389
  /* LPCDPMSG_ENUMSESSIONSREQUEST msg = (LPDPMSG_ENUMSESSIONSREQUEST)lpcMsg; */
390 391 392 393
  BOOL bAnsi = TRUE; /* FIXME: This needs to be in the DPLAY interface */

  FIXME( ": few fixed + need to check request for response\n" );

394
  if (bAnsi)
395
  {
396
      dwVariableLen = MultiByteToWideChar( CP_ACP, 0,
397
                                           lpDP->dp2->lpSessionDesc->u1.lpszSessionNameA,
398
                                           -1, NULL, 0 );
399
  }
400
  else
401
  {
402
      dwVariableLen = strlenW( lpDP->dp2->lpSessionDesc->u1.lpszSessionName ) + 1;
403
  }
404 405 406

  dwVariableSize = dwVariableLen * sizeof( WCHAR );

407 408 409 410
  *lpdwReplySize = lpDP->dp2->spData.dwSPHeaderSize +
                     sizeof( *rmsg ) + dwVariableSize;
  *lplpReplyData = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
                              *lpdwReplySize );
411

412
  rmsg = (LPDPMSG_ENUMSESSIONSREPLY)( (BYTE*)*lplpReplyData +
Peter Hunnisett's avatar
Peter Hunnisett committed
413
                                             lpDP->dp2->spData.dwSPHeaderSize);
414

415
  rmsg->envelope.dwMagic    = DPMSGMAGIC_DPLAYMSG;
416 417 418
  rmsg->envelope.wCommandId = DPMSGCMD_ENUMSESSIONSREPLY;
  rmsg->envelope.wVersion   = DPMSGVER_DP6;

419 420
  CopyMemory( &rmsg->sd, lpDP->dp2->lpSessionDesc,
              sizeof( lpDP->dp2->lpSessionDesc->dwSize ) );
421 422
  rmsg->dwUnknown = 0x0000005c;
  if( bAnsi )
423
  {
424
      MultiByteToWideChar( CP_ACP, 0, lpDP->dp2->lpSessionDesc->u1.lpszSessionNameA, -1,
425
                           (LPWSTR)(rmsg+1), dwVariableLen );
426
  }
427
  else
428
  {
429
      strcpyW( (LPWSTR)(rmsg+1), lpDP->dp2->lpSessionDesc->u1.lpszSessionName );
430
  }
431
}