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.

335 lines
8.5 KiB

  1. /*****************************************************************************
  2. *
  3. * $Workfile: DevPort.cpp $
  4. *
  5. * Copyright (C) 1997 Hewlett-Packard Company.
  6. * Copyright (C) 1997 Microsoft Corporation.
  7. * All rights reserved.
  8. *
  9. * 11311 Chinden Blvd.
  10. * Boise, Idaho 83714
  11. *
  12. *****************************************************************************/
  13. #include "precomp.h"
  14. #include "DevPort.h"
  15. #include "winreg.h"
  16. #include "TcpMonUI.h"
  17. #include "rtcpdata.h"
  18. #include "lprdata.h"
  19. #include "inisection.h"
  20. //
  21. // FUNCTION: CDevicePortList Constructor
  22. //
  23. // PURPOSE: to construct a list of devices and their associated ports
  24. // from the registry, an ini file or from a static table in the code.
  25. //
  26. CDevicePortList::CDevicePortList() : m_pList( NULL ), m_pCurrent( NULL )
  27. {
  28. } // Constructor
  29. //
  30. // FUNCTION: CDevicePortList Destructor
  31. //
  32. // PURPOSE: clean up
  33. //
  34. CDevicePortList::~CDevicePortList()
  35. {
  36. DeletePortList();
  37. } // Destructor
  38. void
  39. CDevicePortList::DeletePortList()
  40. {
  41. while(m_pList != NULL)
  42. {
  43. m_pCurrent = m_pList->GetNextPtr();
  44. delete m_pList;
  45. m_pList = m_pCurrent;
  46. }
  47. }
  48. //
  49. // FUNCTION: GetDevicePortsList
  50. //
  51. // PURPOSE: To create a Device types list getting values from the ini file.
  52. //
  53. BOOL CDevicePortList::GetDevicePortsList(LPTSTR pszDeviceName)
  54. {
  55. BOOL bRet = FALSE;
  56. TCHAR szSystemPath[MAX_PATH] = NULLSTR;
  57. TCHAR szFileName[MAX_PATH] = NULLSTR;
  58. size_t cchFileName = COUNTOF (szFileName);
  59. DeletePortList();
  60. GetSystemDirectory(szSystemPath, sizeof(szSystemPath) / sizeof (TCHAR));
  61. StringCchCopy (szFileName, cchFileName, szSystemPath);
  62. StringCchCat (szFileName, cchFileName, PORTMONITOR_INI_FILE );
  63. //
  64. // Get the section names from the ini file:
  65. //
  66. if( pszDeviceName == NULL ) { // Get all the devices
  67. DWORD nSize = 0;
  68. TCHAR *lpszReturnBuffer = NULL;
  69. if ( !GetSectionNames(szFileName, &lpszReturnBuffer, nSize) )
  70. goto Done;
  71. //
  72. // For each section name Load up the number of ports
  73. //
  74. TCHAR *lpszSectionName = lpszReturnBuffer;
  75. LPCTSTR lpKeyName = PORTS_KEY;
  76. while( lpszSectionName && *lpszSectionName ) {
  77. TCHAR KeyName[26] = NULLSTR;
  78. LPCTSTR lpPortsKeyName = PORTS_KEY;
  79. UINT NumberOfPorts = GetPrivateProfileInt(lpszSectionName,
  80. lpPortsKeyName,
  81. 0,
  82. szFileName);
  83. //
  84. // Name
  85. //
  86. StringCchPrintf (KeyName, COUNTOF (KeyName), PORT_NAME_KEY);
  87. TCHAR tcsPortName[MAX_PORTNAME_LEN] = NULLSTR;
  88. if ( GetPrivateProfileString(lpszSectionName,
  89. KeyName,
  90. TEXT(""),
  91. tcsPortName,
  92. MAX_PORTNAME_LEN,
  93. szFileName) ) {
  94. //
  95. // Setup a new DevicePort struct
  96. //
  97. if ( m_pCurrent = new CDevicePort() ) {
  98. m_pCurrent->Set(tcsPortName,
  99. _tcslen(tcsPortName),
  100. lpszSectionName,
  101. _tcslen(lpszSectionName),
  102. (NumberOfPorts == 1));
  103. m_pCurrent->SetNextPtr(m_pList);
  104. m_pList = m_pCurrent;
  105. }
  106. else
  107. // Out of memory, abort
  108. //
  109. goto Done;
  110. }
  111. /* else
  112. If the call fails, we continue to the next adapter name */
  113. lpszSectionName = _tcschr(lpszSectionName, '\0'); // find the end of the current string.
  114. lpszSectionName = _tcsinc(lpszSectionName); // increment to the beginning of the next string.
  115. }
  116. //
  117. // free memory
  118. //
  119. free(lpszReturnBuffer);
  120. } else {// Just the names in multiport section
  121. TCHAR KeyName[26];
  122. //
  123. // Name
  124. //
  125. LPCTSTR lpKeyName = PORTS_KEY;
  126. UINT NumberOfPorts = GetPrivateProfileInt(pszDeviceName,
  127. lpKeyName,
  128. 0,
  129. szFileName);
  130. for ( UINT i=0; i<NumberOfPorts; i++ ) {
  131. StringCchPrintf (KeyName, COUNTOF (KeyName), PORT_NAMEI_KEY, i+1);
  132. TCHAR tcsPortName[50] = NULLSTR;
  133. GetPrivateProfileString(pszDeviceName, KeyName,
  134. TEXT(""), tcsPortName, 50, szFileName);
  135. //
  136. // Setup a new DevicePort struct
  137. //
  138. if ( m_pCurrent = new CDevicePort() ) {
  139. m_pCurrent->Set(tcsPortName,
  140. _tcslen(tcsPortName),
  141. pszDeviceName,
  142. _tcslen(pszDeviceName), i+1);
  143. m_pCurrent->SetNextPtr(m_pList);
  144. m_pList = m_pCurrent;
  145. }
  146. else
  147. goto Done;
  148. }
  149. }
  150. bRet = TRUE;
  151. Done:
  152. //
  153. // Do not leave with a partial list
  154. //
  155. if ( !bRet )
  156. DeletePortList();
  157. return bRet;
  158. } // GetDevicePortsList
  159. //
  160. // FUNCTION: ReadPortInfo
  161. //
  162. // PURPOSE: To read information about a device from the ini file.
  163. //
  164. void CDevicePort::ReadPortInfo( LPCTSTR pszAddress, PPORT_DATA_1 pPortInfo, BOOL bBypassNetProbe)
  165. {
  166. IniSection *pIniSection;
  167. if ( m_psztPortKeyName ) {
  168. if ( pIniSection = new IniSection() ) {
  169. pIniSection->SetIniSection( m_psztPortKeyName );
  170. pIniSection->GetPortInfo( pszAddress, pPortInfo, m_dwPortIndex, bBypassNetProbe);
  171. delete( pIniSection );
  172. }
  173. }
  174. } // ReadPortInfo
  175. //
  176. // FUNCTION: GetSectionNames
  177. //
  178. // PURPOSE:
  179. //
  180. BOOL
  181. CDevicePortList::
  182. GetSectionNames(
  183. LPCTSTR lpFileName,
  184. TCHAR **lpszReturnBuffer,
  185. DWORD &nSize
  186. )
  187. {
  188. DWORD nReturnSize = 0;
  189. LPTSTR pNewBuf;
  190. do
  191. {
  192. nSize += 512;
  193. pNewBuf = (TCHAR *)realloc(*lpszReturnBuffer, nSize * sizeof(TCHAR));
  194. if ( pNewBuf == NULL )
  195. {
  196. if ( *lpszReturnBuffer )
  197. {
  198. free(*lpszReturnBuffer);
  199. *lpszReturnBuffer = NULL;
  200. }
  201. return FALSE;
  202. }
  203. *lpszReturnBuffer = pNewBuf;
  204. nReturnSize = GetPrivateProfileSectionNames(*lpszReturnBuffer, nSize, lpFileName);
  205. } while(nReturnSize >= nSize-2);
  206. return TRUE;
  207. } // GetSectionNames
  208. //
  209. // FUNCTION: CDevicePort Constructor
  210. //
  211. // PURPOSE:
  212. //
  213. CDevicePort::CDevicePort()
  214. {
  215. m_psztName = NULL;
  216. m_psztPortKeyName = NULL;
  217. m_pNext = NULL;
  218. } // Constructor
  219. //
  220. // FUNCTION: CDevicePort Destructor
  221. //
  222. // PURPOSE:
  223. //
  224. CDevicePort::~CDevicePort()
  225. {
  226. if(m_psztName != NULL)
  227. {
  228. delete m_psztName;
  229. }
  230. if(m_psztPortKeyName != NULL)
  231. {
  232. delete m_psztPortKeyName;
  233. }
  234. } // destructor
  235. //
  236. // FUNCTION: Set
  237. //
  238. // PURPOSE:
  239. //
  240. void CDevicePort::Set(TCHAR *psztNewName,
  241. DWORD dwNameSize,
  242. TCHAR *psztNewKeyName,
  243. DWORD dwNewKeyNameSize,
  244. DWORD dwPortIndex)
  245. {
  246. if ( psztNewName != NULL ) {
  247. if ( m_psztName != NULL ) {
  248. delete m_psztName;
  249. m_psztName = NULL;
  250. }
  251. if ( dwNameSize == 0 )
  252. dwNameSize = _tcslen(psztNewName);
  253. m_psztName = new TCHAR[(dwNameSize + 1) * sizeof(TCHAR)];
  254. if ( m_psztName )
  255. lstrcpyn(m_psztName, psztNewName, dwNameSize+1);
  256. }
  257. if ( psztNewKeyName != NULL ) {
  258. if ( m_psztPortKeyName != NULL ) {
  259. delete m_psztPortKeyName;
  260. m_psztPortKeyName = NULL;
  261. }
  262. if ( dwNewKeyNameSize == 0 ) {
  263. dwNewKeyNameSize = _tcslen(psztNewKeyName);
  264. }
  265. m_psztPortKeyName = new TCHAR[(dwNewKeyNameSize + 1) * sizeof(TCHAR)];
  266. if ( m_psztPortKeyName ) {
  267. lstrcpyn(m_psztPortKeyName, psztNewKeyName, dwNewKeyNameSize+1);
  268. }
  269. }
  270. m_dwPortIndex = dwPortIndex;
  271. } // Set