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.

194 lines
4.4 KiB

  1. #ifndef __TRACKNET_H__
  2. #define __TRACKNET_H__
  3. #include "resource.h"
  4. class CWNetConnectionTrackerGlobal
  5. {
  6. public:
  7. CWNetConnectionTrackerGlobal(){};
  8. ~CWNetConnectionTrackerGlobal(){};
  9. public:
  10. void Add(LPCTSTR pItem)
  11. {
  12. // add to our list,so we can delete it.
  13. IISOpenedNetConnections.AddTail(pItem);
  14. return;
  15. }
  16. void Del(LPCTSTR pItem)
  17. {
  18. POSITION pos = IISOpenedNetConnections.Find(pItem);
  19. if (pos)
  20. {
  21. IISOpenedNetConnections.RemoveAt(pos);
  22. }
  23. return;
  24. }
  25. void Clear()
  26. {
  27. // loop thru everything and disconnect all
  28. CString strOneItem;
  29. POSITION pos = IISOpenedNetConnections.GetTailPosition();
  30. while (pos)
  31. {
  32. strOneItem = IISOpenedNetConnections.GetPrev(pos);
  33. if (!strOneItem.IsEmpty())
  34. {
  35. WNetCancelConnection2((LPCTSTR) strOneItem, 0, TRUE);
  36. }
  37. }
  38. IISOpenedNetConnections.RemoveAll();
  39. }
  40. void Dump()
  41. {
  42. #if defined(_DEBUG) || DBG
  43. int iCount = 0;
  44. CString strOneItem;
  45. if (!(g_iDebugOutputLevel & DEBUG_FLAG_CIISOBJECT))
  46. {
  47. return;
  48. }
  49. DebugTrace(_T("Dump Global NetConnections -------------- start (count=%d)\r\n"),IISOpenedNetConnections.GetCount());
  50. POSITION pos = IISOpenedNetConnections.GetHeadPosition();
  51. while (pos)
  52. {
  53. strOneItem = IISOpenedNetConnections.GetNext(pos);
  54. if (!strOneItem.IsEmpty())
  55. {
  56. iCount++;
  57. DebugTrace(_T("Dump:[%3d] %s\r\n"),iCount,strOneItem);
  58. }
  59. }
  60. DebugTrace(_T("Dump Global NetConnections -------------- end\r\n"));
  61. #endif // _DEBUG
  62. }
  63. private:
  64. CStringList IISOpenedNetConnections;
  65. };
  66. class CWNetConnectionTracker
  67. {
  68. public:
  69. CWNetConnectionTracker(CWNetConnectionTrackerGlobal * pGlobalList) : m_GlobalList(pGlobalList) {};
  70. ~CWNetConnectionTracker(){};
  71. public:
  72. DWORD Connect(
  73. LPNETRESOURCE lpNetResource, // connection details
  74. LPCTSTR lpPassword, // password
  75. LPCTSTR lpUsername, // user name
  76. DWORD dwFlags
  77. )
  78. {
  79. DWORD rc = NO_ERROR;
  80. // see if we already have a connection to this resource from this machine...
  81. POSITION posFound = IISOpenedNetConnections.Find(lpNetResource->lpRemoteName);
  82. if (posFound)
  83. {
  84. // a connection already exists to it
  85. // just return NO_ERROR
  86. #if defined(_DEBUG) || DBG
  87. DebugTrace(_T("WNetAddConnection2:%s,Connection already exists...\r\n"),lpNetResource->lpRemoteName);
  88. #endif
  89. }
  90. else
  91. {
  92. rc = WNetAddConnection2(lpNetResource, lpPassword, lpUsername, dwFlags);
  93. #if defined(_DEBUG) || DBG
  94. DebugTrace(_T("WNetAddConnection2:%s (user=%s), err=%d\r\n"),lpNetResource->lpRemoteName,lpUsername,rc);
  95. #endif
  96. if (NO_ERROR == rc)
  97. {
  98. // add to our list,so we can delete it.
  99. IISOpenedNetConnections.AddTail(lpNetResource->lpRemoteName);
  100. if (m_GlobalList)
  101. {
  102. m_GlobalList->Add(lpNetResource->lpRemoteName);
  103. }
  104. }
  105. }
  106. return rc;
  107. }
  108. DWORD Disconnect(LPCTSTR pItem)
  109. {
  110. DWORD rc = WNetCancelConnection2(pItem, 0, TRUE);
  111. if (NO_ERROR == rc)
  112. {
  113. #if defined(_DEBUG) || DBG
  114. DebugTrace(_T("WNetCancelConnection2:%s\r\n"),pItem);
  115. #endif
  116. POSITION pos = IISOpenedNetConnections.Find(pItem);
  117. if (pos)
  118. {
  119. IISOpenedNetConnections.RemoveAt(pos);
  120. if (m_GlobalList)
  121. {
  122. m_GlobalList->Del(pItem);
  123. }
  124. }
  125. }
  126. return rc;
  127. }
  128. void Clear()
  129. {
  130. // loop thru everything and disconnect all
  131. CString strOneItem;
  132. POSITION pos = IISOpenedNetConnections.GetTailPosition();
  133. while (pos)
  134. {
  135. strOneItem = IISOpenedNetConnections.GetPrev(pos);
  136. if (!strOneItem.IsEmpty())
  137. {
  138. Disconnect(strOneItem);
  139. }
  140. }
  141. }
  142. void Dump()
  143. {
  144. #if defined(_DEBUG) || DBG
  145. int iCount = 0;
  146. CString strOneItem;
  147. if (!(g_iDebugOutputLevel & DEBUG_FLAG_CIISOBJECT))
  148. {
  149. return;
  150. }
  151. DebugTrace(_T("Dump Machine NetConnections -------------- start (count=%d)\r\n"),IISOpenedNetConnections.GetCount());
  152. POSITION pos = IISOpenedNetConnections.GetHeadPosition();
  153. while (pos)
  154. {
  155. strOneItem = IISOpenedNetConnections.GetNext(pos);
  156. if (!strOneItem.IsEmpty())
  157. {
  158. iCount++;
  159. DebugTrace(_T("Dump:[%3d] %s\r\n"),iCount,strOneItem);
  160. }
  161. }
  162. DebugTrace(_T("Dump Machine NetConnections -------------- end\r\n"));
  163. #endif // _DEBUG
  164. }
  165. private:
  166. CWNetConnectionTrackerGlobal * m_GlobalList;
  167. CStringList IISOpenedNetConnections;
  168. };
  169. #endif // __TRACKNET_H__