Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

913 lines
22 KiB

  1. /*==========================================================================
  2. *
  3. * Copyright (C) 2000-2002 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: AppDesc.cpp
  6. * Content: Application Description Object
  7. *@@BEGIN_MSINTERNAL
  8. * History:
  9. * Date By Reason
  10. * ==== == ======
  11. * 09/02/00 mjn Created
  12. * 09/14/2000 rmt Bug #44625 - CORE: Multihomed machines are not always enumerable (extra spew)
  13. * 01/25/01 mjn Fixed 64-bit alignment problem when unpacking AppDesc
  14. *@@END_MSINTERNAL
  15. *
  16. ***************************************************************************/
  17. #include "dncorei.h"
  18. //**********************************************************************
  19. // Constant definitions
  20. //**********************************************************************
  21. //**********************************************************************
  22. // Macro definitions
  23. //**********************************************************************
  24. //**********************************************************************
  25. // Structure definitions
  26. //**********************************************************************
  27. //**********************************************************************
  28. // Variable definitions
  29. //**********************************************************************
  30. //**********************************************************************
  31. // Function prototypes
  32. //**********************************************************************
  33. //**********************************************************************
  34. // Function definitions
  35. //**********************************************************************
  36. #undef DPF_MODNAME
  37. #define DPF_MODNAME "CApplicationDesc::Initialize"
  38. HRESULT CApplicationDesc::Initialize( void )
  39. {
  40. memset(this,0,sizeof(CApplicationDesc));
  41. m_Sig[0] = 'A';
  42. m_Sig[1] = 'P';
  43. m_Sig[2] = 'P';
  44. m_Sig[3] = 'D';
  45. if (!DNInitializeCriticalSection(&m_cs))
  46. {
  47. return( DPNERR_OUTOFMEMORY );
  48. }
  49. return( DPN_OK );
  50. }
  51. #undef DPF_MODNAME
  52. #define DPF_MODNAME "CApplicationDesc::Deinitialize"
  53. void CApplicationDesc::Deinitialize( void )
  54. {
  55. if (m_pwszSessionName)
  56. {
  57. DNFree(m_pwszSessionName);
  58. m_pwszSessionName = NULL;
  59. m_dwSessionNameSize = 0;
  60. }
  61. if (m_pwszPassword)
  62. {
  63. DNFree(m_pwszPassword);
  64. m_pwszPassword = NULL;
  65. m_dwPasswordSize = 0;
  66. }
  67. if (m_pvReservedData)
  68. {
  69. DNFree(m_pvReservedData);
  70. m_pvReservedData = NULL;
  71. m_dwReservedDataSize = 0;
  72. }
  73. if (m_pvApplicationReservedData)
  74. {
  75. DNFree(m_pvApplicationReservedData);
  76. m_pvApplicationReservedData = NULL;
  77. m_dwApplicationReservedDataSize = 0;
  78. }
  79. DNDeleteCriticalSection(&m_cs);
  80. }
  81. #undef DPF_MODNAME
  82. #define DPF_MODNAME "CApplicationDesc::Pack"
  83. HRESULT CApplicationDesc::Pack(CPackedBuffer *const pPackedBuffer,
  84. const DWORD dwFlags)
  85. {
  86. HRESULT hResultCode;
  87. DPN_APPLICATION_DESC *pdpnAppDesc;
  88. DPFX(DPFPREP, 6,"Parameters: pPackedBuffer [0x%p], dwFlags [0x%lx]",pPackedBuffer,dwFlags);
  89. Lock();
  90. //
  91. // Add structure
  92. //
  93. pdpnAppDesc = reinterpret_cast<DPN_APPLICATION_DESC*>(pPackedBuffer->GetHeadAddress());
  94. hResultCode = pPackedBuffer->AddToFront(NULL,sizeof(DPN_APPLICATION_DESC));
  95. //
  96. // Add session name
  97. //
  98. if ((dwFlags & DN_APPDESCINFO_FLAG_SESSIONNAME) && (m_dwSessionNameSize > 0))
  99. {
  100. hResultCode = pPackedBuffer->AddToBack(m_pwszSessionName,m_dwSessionNameSize);
  101. if (hResultCode == DPN_OK)
  102. {
  103. pdpnAppDesc->pwszSessionName = static_cast<WCHAR*>(pPackedBuffer->GetTailAddress());
  104. }
  105. }
  106. else
  107. {
  108. if (hResultCode == DPN_OK)
  109. {
  110. pdpnAppDesc->pwszSessionName = NULL;
  111. }
  112. }
  113. //
  114. // Add password
  115. //
  116. if ((dwFlags & DN_APPDESCINFO_FLAG_PASSWORD) && (m_dwPasswordSize > 0))
  117. {
  118. hResultCode = pPackedBuffer->AddToBack(m_pwszPassword,m_dwPasswordSize);
  119. if (hResultCode == DPN_OK)
  120. {
  121. pdpnAppDesc->pwszPassword = static_cast<WCHAR*>(pPackedBuffer->GetTailAddress());
  122. }
  123. }
  124. else
  125. {
  126. if (hResultCode == DPN_OK)
  127. {
  128. pdpnAppDesc->pwszPassword = NULL;
  129. }
  130. }
  131. //
  132. // Add reserved data
  133. //
  134. if ((dwFlags & DN_APPDESCINFO_FLAG_RESERVEDDATA) && (m_dwReservedDataSize > 0))
  135. {
  136. PVOID pvReservedData;
  137. DWORD dwReservedDataSize;
  138. BYTE AppDescReservedData[DPN_MAX_APPDESC_RESERVEDDATA_SIZE];
  139. //
  140. // If we understand the reserved data, we want to pad the buffer so the user doesn't
  141. // assume the data is less than DPN_MAX_APPDESC_RESERVEDDATA_SIZE bytes long.
  142. //
  143. if ((m_dwReservedDataSize == sizeof(SPSESSIONDATA_XNET)) &&
  144. (*((DWORD*) m_pvReservedData) == SPSESSIONDATAINFO_XNET))
  145. {
  146. SPSESSIONDATA_XNET * pSessionDataXNet;
  147. pSessionDataXNet = (SPSESSIONDATA_XNET*) AppDescReservedData;
  148. memcpy(pSessionDataXNet, m_pvReservedData, m_dwReservedDataSize);
  149. memset((pSessionDataXNet + 1),
  150. (((BYTE*) (&pSessionDataXNet->ullKeyID))[1] ^ ((BYTE*) (&pSessionDataXNet->guidKey))[2]),
  151. (DPN_MAX_APPDESC_RESERVEDDATA_SIZE - sizeof(SPSESSIONDATA_XNET)));
  152. pvReservedData = AppDescReservedData;
  153. dwReservedDataSize = DPN_MAX_APPDESC_RESERVEDDATA_SIZE;
  154. }
  155. else
  156. {
  157. pvReservedData = m_pvReservedData;
  158. dwReservedDataSize = m_dwReservedDataSize;
  159. }
  160. hResultCode = pPackedBuffer->AddToBack(pvReservedData, dwReservedDataSize);
  161. if (hResultCode == DPN_OK)
  162. {
  163. pdpnAppDesc->pvReservedData = pPackedBuffer->GetTailAddress();
  164. pdpnAppDesc->dwReservedDataSize = dwReservedDataSize;
  165. }
  166. }
  167. else
  168. {
  169. if (hResultCode == DPN_OK)
  170. {
  171. pdpnAppDesc->pvReservedData = NULL;
  172. pdpnAppDesc->dwReservedDataSize = 0;
  173. }
  174. }
  175. //
  176. // Add app reserved data
  177. //
  178. if ((dwFlags & DN_APPDESCINFO_FLAG_APPRESERVEDDATA) && (m_dwApplicationReservedDataSize > 0))
  179. {
  180. hResultCode = pPackedBuffer->AddToBack(m_pvApplicationReservedData,m_dwApplicationReservedDataSize);
  181. if (hResultCode == DPN_OK)
  182. {
  183. pdpnAppDesc->pvApplicationReservedData = pPackedBuffer->GetTailAddress();
  184. pdpnAppDesc->dwApplicationReservedDataSize = m_dwApplicationReservedDataSize;
  185. }
  186. }
  187. else
  188. {
  189. if (hResultCode == DPN_OK)
  190. {
  191. pdpnAppDesc->pvApplicationReservedData = NULL;
  192. pdpnAppDesc->dwApplicationReservedDataSize = 0;
  193. }
  194. }
  195. //
  196. // Fill in remainder of structure (if space)
  197. //
  198. if (hResultCode == DPN_OK)
  199. {
  200. pdpnAppDesc->dwSize = sizeof(DPN_APPLICATION_DESC);
  201. pdpnAppDesc->dwFlags = m_dwFlags;
  202. pdpnAppDesc->dwMaxPlayers = m_dwMaxPlayers;
  203. pdpnAppDesc->dwCurrentPlayers = m_dwCurrentPlayers;
  204. memcpy(&pdpnAppDesc->guidInstance,&m_guidInstance,sizeof(GUID));
  205. memcpy(&pdpnAppDesc->guidApplication,&m_guidApplication,sizeof(GUID));
  206. }
  207. Unlock();
  208. DPFX(DPFPREP, 6,"Returning: [0x%lx]",hResultCode);
  209. return(hResultCode);
  210. }
  211. #undef DPF_MODNAME
  212. #define DPF_MODNAME "CApplicationDesc::PackInfo"
  213. HRESULT CApplicationDesc::PackInfo(CPackedBuffer *const pPackedBuffer,
  214. const DWORD dwFlags)
  215. {
  216. HRESULT hResultCode;
  217. DPN_APPLICATION_DESC_INFO *pInfo;
  218. DPFX(DPFPREP, 6,"Parameters: pPackedBuffer [0x%p], dwFlags [0x%lx]",pPackedBuffer,dwFlags);
  219. //
  220. // Add structure
  221. //
  222. pInfo = reinterpret_cast<DPN_APPLICATION_DESC_INFO*>(pPackedBuffer->GetHeadAddress());
  223. hResultCode = pPackedBuffer->AddToFront(NULL,sizeof(DPN_APPLICATION_DESC_INFO));
  224. Lock();
  225. //
  226. // Add session name
  227. //
  228. if ((dwFlags & DN_APPDESCINFO_FLAG_SESSIONNAME) && (m_dwSessionNameSize > 0))
  229. {
  230. hResultCode = pPackedBuffer->AddToBack(m_pwszSessionName,m_dwSessionNameSize);
  231. if (hResultCode == DPN_OK)
  232. {
  233. pInfo->dwSessionNameOffset = pPackedBuffer->GetTailOffset();
  234. pInfo->dwSessionNameSize = m_dwSessionNameSize;
  235. }
  236. }
  237. else
  238. {
  239. if (hResultCode == DPN_OK)
  240. {
  241. pInfo->dwSessionNameOffset = 0;
  242. pInfo->dwSessionNameSize = 0;
  243. }
  244. }
  245. //
  246. // Add password
  247. //
  248. if ((dwFlags & DN_APPDESCINFO_FLAG_PASSWORD) && (m_dwPasswordSize > 0))
  249. {
  250. hResultCode = pPackedBuffer->AddToBack(m_pwszPassword,m_dwPasswordSize);
  251. if (hResultCode == DPN_OK)
  252. {
  253. pInfo->dwPasswordOffset = pPackedBuffer->GetTailOffset();
  254. pInfo->dwPasswordSize = m_dwPasswordSize;
  255. }
  256. }
  257. else
  258. {
  259. if (hResultCode == DPN_OK)
  260. {
  261. pInfo->dwPasswordOffset = 0;
  262. pInfo->dwPasswordSize = 0;
  263. }
  264. }
  265. //
  266. // Add reserved data
  267. //
  268. if ((dwFlags & DN_APPDESCINFO_FLAG_RESERVEDDATA) && (m_dwReservedDataSize > 0))
  269. {
  270. hResultCode = pPackedBuffer->AddToBack(m_pvReservedData,m_dwReservedDataSize);
  271. if (hResultCode == DPN_OK)
  272. {
  273. pInfo->dwReservedDataOffset = pPackedBuffer->GetTailOffset();
  274. pInfo->dwReservedDataSize = m_dwReservedDataSize;
  275. }
  276. }
  277. else
  278. {
  279. if (hResultCode == DPN_OK)
  280. {
  281. pInfo->dwReservedDataOffset = 0;
  282. pInfo->dwReservedDataSize = 0;
  283. }
  284. }
  285. //
  286. // Add app reserved data
  287. //
  288. if ((dwFlags & DN_APPDESCINFO_FLAG_APPRESERVEDDATA) && (m_dwApplicationReservedDataSize > 0))
  289. {
  290. hResultCode = pPackedBuffer->AddToBack(m_pvApplicationReservedData,m_dwApplicationReservedDataSize);
  291. if (hResultCode == DPN_OK)
  292. {
  293. pInfo->dwApplicationReservedDataOffset = pPackedBuffer->GetTailOffset();
  294. pInfo->dwApplicationReservedDataSize = m_dwApplicationReservedDataSize;
  295. }
  296. }
  297. else
  298. {
  299. if (hResultCode == DPN_OK)
  300. {
  301. pInfo->dwApplicationReservedDataOffset = 0;
  302. pInfo->dwApplicationReservedDataSize = 0;
  303. }
  304. }
  305. //
  306. // Fill in remainder of structure (if space)
  307. //
  308. if (hResultCode == DPN_OK)
  309. {
  310. pInfo->dwFlags = m_dwFlags;
  311. pInfo->dwMaxPlayers = m_dwMaxPlayers;
  312. pInfo->dwCurrentPlayers = m_dwCurrentPlayers;
  313. memcpy(&pInfo->guidInstance,&m_guidInstance,sizeof(GUID));
  314. memcpy(&pInfo->guidApplication,&m_guidApplication,sizeof(GUID));
  315. }
  316. Unlock();
  317. DPFX(DPFPREP, 6,"Returning: [0x%lx]",hResultCode);
  318. return(hResultCode);
  319. }
  320. #undef DPF_MODNAME
  321. #define DPF_MODNAME "CApplicationDesc::UnpackInfo"
  322. HRESULT CApplicationDesc::UnpackInfo(UNALIGNED DPN_APPLICATION_DESC_INFO *const pdnAppDescInfo,
  323. void *const pBufferStart,
  324. const DWORD dwFlags)
  325. {
  326. HRESULT hResultCode;
  327. WCHAR *pwszSessionName;
  328. WCHAR *pwszPassword;
  329. void *pvReservedData;
  330. void *pvApplicationReservedData;
  331. DPFX(DPFPREP, 6,"Parameters: pdnAppDescInfo [0x%p], pBufferStart [0x%p], dwFlags [0x%lx]",pdnAppDescInfo,pBufferStart,dwFlags);
  332. pwszSessionName = NULL;
  333. pwszPassword = NULL;
  334. pvReservedData = NULL;
  335. pvApplicationReservedData = NULL;
  336. //
  337. // We will allocate the required memory for the data fields first.
  338. // If everything succeeds, we will update the object
  339. //
  340. //
  341. // Session Name
  342. //
  343. if ((dwFlags & DN_APPDESCINFO_FLAG_SESSIONNAME) && (pdnAppDescInfo->dwSessionNameSize))
  344. {
  345. if ((pwszSessionName = static_cast<WCHAR*>(DNMalloc(pdnAppDescInfo->dwSessionNameSize))) == NULL)
  346. {
  347. DPFERR("Could not allocate application desc session name");
  348. hResultCode = DPNERR_OUTOFMEMORY;
  349. goto Failure;
  350. }
  351. memcpy( pwszSessionName,
  352. static_cast<BYTE*>(pBufferStart) + pdnAppDescInfo->dwSessionNameOffset,
  353. pdnAppDescInfo->dwSessionNameSize);
  354. }
  355. //
  356. // Password
  357. //
  358. if ((dwFlags & DN_APPDESCINFO_FLAG_PASSWORD) && (pdnAppDescInfo->dwPasswordSize))
  359. {
  360. if ((pwszPassword = static_cast<WCHAR*>(DNMalloc(pdnAppDescInfo->dwPasswordSize))) == NULL)
  361. {
  362. DPFERR("Could not allocate application desc password");
  363. hResultCode = DPNERR_OUTOFMEMORY;
  364. goto Failure;
  365. }
  366. memcpy( pwszPassword,
  367. static_cast<BYTE*>(pBufferStart) + pdnAppDescInfo->dwPasswordOffset,
  368. pdnAppDescInfo->dwPasswordSize);
  369. }
  370. //
  371. // Reserved Data
  372. //
  373. if ((dwFlags & DN_APPDESCINFO_FLAG_RESERVEDDATA) && (pdnAppDescInfo->dwReservedDataSize))
  374. {
  375. if ((pvReservedData = DNMalloc(pdnAppDescInfo->dwReservedDataSize)) == NULL)
  376. {
  377. DPFERR("Could not allocate application desc reserved data");
  378. hResultCode = DPNERR_OUTOFMEMORY;
  379. goto Failure;
  380. }
  381. memcpy( pvReservedData,
  382. static_cast<BYTE*>(pBufferStart) + pdnAppDescInfo->dwReservedDataOffset,
  383. pdnAppDescInfo->dwReservedDataSize);
  384. }
  385. //
  386. // Application Reserved Data
  387. //
  388. if ((dwFlags & DN_APPDESCINFO_FLAG_APPRESERVEDDATA) && (pdnAppDescInfo->dwApplicationReservedDataSize))
  389. {
  390. if ((pvApplicationReservedData = DNMalloc(pdnAppDescInfo->dwApplicationReservedDataSize)) == NULL)
  391. {
  392. DPFERR("Could not allocate application desc app reserved data");
  393. hResultCode = DPNERR_OUTOFMEMORY;
  394. goto Failure;
  395. }
  396. memcpy( pvApplicationReservedData,
  397. static_cast<BYTE*>(pBufferStart) + pdnAppDescInfo->dwApplicationReservedDataOffset,
  398. pdnAppDescInfo->dwApplicationReservedDataSize);
  399. }
  400. //
  401. // Replace old values in object
  402. //
  403. Lock();
  404. //
  405. // Session Name
  406. //
  407. if (dwFlags & DN_APPDESCINFO_FLAG_SESSIONNAME)
  408. {
  409. if (m_pwszSessionName)
  410. {
  411. DNFree(m_pwszSessionName);
  412. m_pwszSessionName = NULL;
  413. m_dwSessionNameSize = 0;
  414. }
  415. m_pwszSessionName = pwszSessionName;
  416. m_dwSessionNameSize = pdnAppDescInfo->dwSessionNameSize;
  417. }
  418. //
  419. // Password
  420. //
  421. if (dwFlags & DN_APPDESCINFO_FLAG_PASSWORD)
  422. {
  423. if (m_pwszPassword)
  424. {
  425. DNFree(m_pwszPassword);
  426. m_pwszPassword = NULL;
  427. m_dwPasswordSize = 0;
  428. }
  429. m_pwszPassword = pwszPassword;
  430. m_dwPasswordSize = pdnAppDescInfo->dwPasswordSize;
  431. }
  432. //
  433. // Reserved Data
  434. //
  435. if (dwFlags & DN_APPDESCINFO_FLAG_RESERVEDDATA)
  436. {
  437. if (m_pvReservedData)
  438. {
  439. DNFree(m_pvReservedData);
  440. m_pvReservedData = NULL;
  441. m_dwReservedDataSize = 0;
  442. }
  443. m_pvReservedData = pvReservedData;
  444. m_dwReservedDataSize = pdnAppDescInfo->dwReservedDataSize;
  445. }
  446. //
  447. // Application Reserved Data
  448. //
  449. if (dwFlags & DN_APPDESCINFO_FLAG_APPRESERVEDDATA)
  450. {
  451. if (m_pvApplicationReservedData)
  452. {
  453. DNFree(m_pvApplicationReservedData);
  454. m_pvApplicationReservedData = NULL;
  455. m_dwApplicationReservedDataSize = 0;
  456. }
  457. m_pvApplicationReservedData = pvApplicationReservedData;
  458. m_dwApplicationReservedDataSize = pdnAppDescInfo->dwApplicationReservedDataSize;
  459. }
  460. //
  461. // Remaining fields
  462. //
  463. m_dwMaxPlayers = pdnAppDescInfo->dwMaxPlayers;
  464. m_dwFlags = pdnAppDescInfo->dwFlags;
  465. memcpy(&m_guidInstance,&pdnAppDescInfo->guidInstance,sizeof(GUID));
  466. memcpy(&m_guidApplication,&pdnAppDescInfo->guidApplication,sizeof(GUID));
  467. Unlock();
  468. hResultCode = DPN_OK;
  469. Exit:
  470. DPFX(DPFPREP, 6,"Returning: [0x%lx]",hResultCode);
  471. return(hResultCode);
  472. Failure:
  473. if (pwszSessionName)
  474. {
  475. DNFree(pwszSessionName);
  476. pwszSessionName = NULL;
  477. }
  478. if (pwszPassword)
  479. {
  480. DNFree(pwszPassword);
  481. pwszPassword = NULL;
  482. }
  483. if (pvReservedData)
  484. {
  485. DNFree(pvReservedData);
  486. pvReservedData = NULL;
  487. }
  488. if (pvApplicationReservedData)
  489. {
  490. DNFree(pvApplicationReservedData);
  491. pvApplicationReservedData = NULL;
  492. }
  493. goto Exit;
  494. }
  495. #undef DPF_MODNAME
  496. #define DPF_MODNAME "CApplicationDesc::Update"
  497. HRESULT CApplicationDesc::Update(const DPN_APPLICATION_DESC *const pdnAppDesc,
  498. const DWORD dwFlags)
  499. {
  500. HRESULT hResultCode;
  501. WCHAR *pwszSessionName;
  502. DWORD dwSessionNameSize;
  503. WCHAR *pwszPassword;
  504. DWORD dwPasswordSize;
  505. void *pvReservedData;
  506. DWORD dwReservedDataSize;
  507. void *pvApplicationReservedData;
  508. DPFX(DPFPREP, 6,"Parameters: pdnAppDesc [0x%p], dwFlags [0x%lx]",pdnAppDesc,dwFlags);
  509. pwszSessionName = NULL;
  510. dwSessionNameSize = 0;
  511. pwszPassword = NULL;
  512. dwPasswordSize = 0;
  513. pvReservedData = NULL;
  514. dwReservedDataSize = 0;
  515. pvApplicationReservedData = NULL;
  516. //
  517. // We will allocate the required memory for the data fields first.
  518. // If everything succeeds, we will update the object
  519. //
  520. //
  521. // Session Name
  522. //
  523. if ((dwFlags & DN_APPDESCINFO_FLAG_SESSIONNAME) && (pdnAppDesc->pwszSessionName))
  524. {
  525. dwSessionNameSize = (wcslen(pdnAppDesc->pwszSessionName) + 1) * sizeof(WCHAR);
  526. if ((pwszSessionName = static_cast<WCHAR*>(DNMalloc(dwSessionNameSize))) == NULL)
  527. {
  528. DPFERR("Could not allocate application desc session name");
  529. hResultCode = DPNERR_OUTOFMEMORY;
  530. goto Failure;
  531. }
  532. memcpy( pwszSessionName,
  533. pdnAppDesc->pwszSessionName,
  534. dwSessionNameSize);
  535. }
  536. //
  537. // Password
  538. //
  539. if ((dwFlags & DN_APPDESCINFO_FLAG_PASSWORD) && (pdnAppDesc->pwszPassword))
  540. {
  541. dwPasswordSize = (wcslen(pdnAppDesc->pwszPassword) + 1) * sizeof(WCHAR);
  542. if ((pwszPassword = static_cast<WCHAR*>(DNMalloc(dwPasswordSize))) == NULL)
  543. {
  544. DPFERR("Could not allocate application desc password");
  545. hResultCode = DPNERR_OUTOFMEMORY;
  546. goto Failure;
  547. }
  548. memcpy( pwszPassword,
  549. pdnAppDesc->pwszPassword,
  550. dwPasswordSize);
  551. }
  552. //
  553. // Reserved Data
  554. //
  555. if ((dwFlags & DN_APPDESCINFO_FLAG_RESERVEDDATA) && (pdnAppDesc->pvReservedData))
  556. {
  557. dwReservedDataSize = pdnAppDesc->dwReservedDataSize;
  558. //
  559. // We don't need to store all of the reserved data for types we understand.
  560. //
  561. if ((dwReservedDataSize == DPN_MAX_APPDESC_RESERVEDDATA_SIZE) &&
  562. (*((DWORD*) pdnAppDesc->pvReservedData) == SPSESSIONDATAINFO_XNET))
  563. {
  564. dwReservedDataSize = sizeof(SPSESSIONDATA_XNET);
  565. }
  566. if ((pvReservedData = DNMalloc(dwReservedDataSize)) == NULL)
  567. {
  568. DPFERR("Could not allocate application desc reserved data");
  569. hResultCode = DPNERR_OUTOFMEMORY;
  570. goto Failure;
  571. }
  572. memcpy( pvReservedData,
  573. pdnAppDesc->pvReservedData,
  574. dwReservedDataSize);
  575. }
  576. //
  577. // Application Reserved Data
  578. //
  579. if ((dwFlags & DN_APPDESCINFO_FLAG_APPRESERVEDDATA) && (pdnAppDesc->pvApplicationReservedData))
  580. {
  581. if ((pvApplicationReservedData = DNMalloc(pdnAppDesc->dwApplicationReservedDataSize)) == NULL)
  582. {
  583. DPFERR("Could not allocate application desc app reserved data");
  584. hResultCode = DPNERR_OUTOFMEMORY;
  585. goto Failure;
  586. }
  587. memcpy( pvApplicationReservedData,
  588. pdnAppDesc->pvApplicationReservedData,
  589. pdnAppDesc->dwApplicationReservedDataSize);
  590. }
  591. //
  592. // Replace old values in object
  593. //
  594. //
  595. // Session Name
  596. //
  597. if (dwFlags & DN_APPDESCINFO_FLAG_SESSIONNAME)
  598. {
  599. if (m_pwszSessionName)
  600. {
  601. DNFree(m_pwszSessionName);
  602. m_pwszSessionName = NULL;
  603. m_dwSessionNameSize = 0;
  604. }
  605. m_pwszSessionName = pwszSessionName;
  606. m_dwSessionNameSize = dwSessionNameSize;
  607. }
  608. //
  609. // Password
  610. //
  611. if (dwFlags & DN_APPDESCINFO_FLAG_PASSWORD)
  612. {
  613. if (m_pwszPassword)
  614. {
  615. DNFree(m_pwszPassword);
  616. m_pwszPassword = NULL;
  617. m_dwPasswordSize = 0;
  618. }
  619. m_pwszPassword = pwszPassword;
  620. m_dwPasswordSize = dwPasswordSize;
  621. }
  622. //
  623. // Reserved Data
  624. //
  625. if (dwFlags & DN_APPDESCINFO_FLAG_RESERVEDDATA)
  626. {
  627. if (m_pvReservedData)
  628. {
  629. DNFree(m_pvReservedData);
  630. m_pvReservedData = NULL;
  631. m_dwReservedDataSize = 0;
  632. }
  633. m_pvReservedData = pvReservedData;
  634. m_dwReservedDataSize = dwReservedDataSize;
  635. }
  636. //
  637. // Application Reserved Data
  638. //
  639. if (dwFlags & DN_APPDESCINFO_FLAG_APPRESERVEDDATA)
  640. {
  641. if (m_pvApplicationReservedData)
  642. {
  643. DNFree(m_pvApplicationReservedData);
  644. m_pvApplicationReservedData = NULL;
  645. m_dwApplicationReservedDataSize = 0;
  646. }
  647. m_pvApplicationReservedData = pvApplicationReservedData;
  648. m_dwApplicationReservedDataSize = pdnAppDesc->dwApplicationReservedDataSize;
  649. }
  650. //
  651. // Remaining fields
  652. //
  653. m_dwMaxPlayers = pdnAppDesc->dwMaxPlayers;
  654. m_dwFlags = pdnAppDesc->dwFlags;
  655. if (dwFlags & DN_APPDESCINFO_FLAG_GUIDS)
  656. {
  657. memcpy(&m_guidInstance,&pdnAppDesc->guidInstance,sizeof(GUID));
  658. memcpy(&m_guidApplication,&pdnAppDesc->guidApplication,sizeof(GUID));
  659. }
  660. hResultCode = DPN_OK;
  661. Exit:
  662. DPFX(DPFPREP, 6,"Returning: [0x%lx]",hResultCode);
  663. return(hResultCode);
  664. Failure:
  665. if (pwszSessionName)
  666. {
  667. DNFree(pwszSessionName);
  668. pwszSessionName = NULL;
  669. }
  670. if (pwszPassword)
  671. {
  672. DNFree(pwszPassword);
  673. pwszPassword = NULL;
  674. }
  675. if (pvReservedData)
  676. {
  677. DNFree(pvReservedData);
  678. pvReservedData = NULL;
  679. }
  680. if (pvApplicationReservedData)
  681. {
  682. DNFree(pvApplicationReservedData);
  683. pvApplicationReservedData = NULL;
  684. }
  685. goto Exit;
  686. }
  687. #undef DPF_MODNAME
  688. #define DPF_MODNAME "CApplicationDesc::CreateNewInstanceGuid"
  689. HRESULT CApplicationDesc::CreateNewInstanceGuid( void )
  690. {
  691. HRESULT hResultCode;
  692. hResultCode = DNCoCreateGuid(&m_guidInstance);
  693. return(hResultCode);
  694. }
  695. #ifndef DPNBUILD_SINGLEPROCESS
  696. #undef DPF_SUBCOMP
  697. #define DPF_SUBCOMP DN_SUBCOMP_DPNSVR
  698. #undef DPF_MODNAME
  699. #define DPF_MODNAME "CApplicationDesc::RegisterWithDPNSVR"
  700. HRESULT CApplicationDesc::RegisterWithDPNSVR( IDirectPlay8Address *const pListenAddr )
  701. {
  702. HRESULT hResultCode;
  703. #ifdef DBG
  704. DPFX(DPFPREP, 8, "Registering w/DPNSVR" );
  705. TCHAR szTmpAddress[200];
  706. DWORD dwSize = 200;
  707. hResultCode = IDirectPlay8Address_GetURL( pListenAddr, szTmpAddress, &dwSize );
  708. if( FAILED( hResultCode ) )
  709. {
  710. DPFX(DPFPREP, 1, "Failed to get URL of listen for debug purposes hr=0x%x", hResultCode );
  711. }
  712. else
  713. {
  714. DPFX(DPFPREP, 8, "Listen on: [%s]", szTmpAddress );
  715. }
  716. #endif // DBG
  717. hResultCode = DPNSVR_Register( &m_guidApplication,&m_guidInstance,pListenAddr );
  718. DPFX(DPFPREP, 8, "Request result hr=0x%x", hResultCode );
  719. return(hResultCode);
  720. }
  721. #undef DPF_MODNAME
  722. #define DPF_MODNAME "CApplicationDesc::UnregisterWithDPNSVR"
  723. HRESULT CApplicationDesc::UnregisterWithDPNSVR( void )
  724. {
  725. HRESULT hResultCode;
  726. hResultCode = DPNSVR_UnRegister( &m_guidApplication,&m_guidInstance );
  727. return(hResultCode);
  728. }
  729. #endif // ! DPNBUILD_SINGLEPROCESS
  730. #undef DPF_SUBCOMP
  731. #define DPF_SUBCOMP DN_SUBCOMP_CORE
  732. #undef DPF_MODNAME
  733. #define DPF_MODNAME "CApplicationDesc::IncPlayerCount"
  734. HRESULT CApplicationDesc::IncPlayerCount(const BOOL fCheckLimit)
  735. {
  736. HRESULT hResultCode;
  737. Lock();
  738. if ((fCheckLimit) && (m_dwMaxPlayers) && (m_dwCurrentPlayers >= m_dwMaxPlayers))
  739. {
  740. hResultCode = DPNERR_SESSIONFULL;
  741. }
  742. else
  743. {
  744. m_dwCurrentPlayers++;
  745. hResultCode = DPN_OK;
  746. }
  747. Unlock();
  748. return(hResultCode);
  749. }
  750. #undef DPF_MODNAME
  751. #define DPF_MODNAME "CApplicationDesc::DecPlayerCount"
  752. void CApplicationDesc::DecPlayerCount( void )
  753. {
  754. Lock();
  755. m_dwCurrentPlayers--;
  756. Unlock();
  757. }
  758. #undef DPF_MODNAME
  759. #define DPF_MODNAME "DNProcessUpdateAppDesc"
  760. HRESULT DNProcessUpdateAppDesc(DIRECTNETOBJECT *const pdnObject,
  761. DPN_APPLICATION_DESC_INFO *const pv)
  762. {
  763. HRESULT hResultCode;
  764. DPFX(DPFPREP, 6,"Parameters: pv [0x%p]",pv);
  765. DNASSERT(pdnObject != NULL);
  766. DNASSERT(pv != NULL);
  767. hResultCode = pdnObject->ApplicationDesc.UnpackInfo(pv,
  768. pv,
  769. DN_APPDESCINFO_FLAG_SESSIONNAME | DN_APPDESCINFO_FLAG_PASSWORD |
  770. DN_APPDESCINFO_FLAG_RESERVEDDATA | DN_APPDESCINFO_FLAG_APPRESERVEDDATA);
  771. hResultCode = DNUserUpdateAppDesc(pdnObject);
  772. DPFX(DPFPREP, 6,"Returning: [0x%lx]",hResultCode);
  773. return(hResultCode);
  774. }