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.

432 lines
10 KiB

  1. /*++
  2. Copyright (c) 97 Microsoft Corporation
  3. All rights reserved.
  4. Module Name:
  5. IniSection.cpp
  6. Abstract:
  7. Standard TCP/IP Port Monitor class to handle INI file settings
  8. Author:
  9. Muhunthan Sivapragasam (MuhuntS) 19-Nov-1997
  10. Revision History:
  11. --*/
  12. #include "precomp.h"
  13. #include "tcpmonui.h"
  14. #include "rtcpdata.h"
  15. #include "lprdata.h"
  16. #include "IniSection.h"
  17. BOOL
  18. StringMatch(
  19. LPCTSTR psz1,
  20. LPCTSTR psz2 // * is a wild char in this
  21. )
  22. {
  23. LPCTSTR p1 = NULL, p2 = NULL;
  24. for ( p1 = psz1, p2 = psz2 ; *p1 && *p2 ; ) {
  25. //
  26. // A * matches any sub-string
  27. //
  28. if ( *p2 == TEXT('*') ) {
  29. ++p2;
  30. if ( !*p2 ) {
  31. return TRUE;
  32. }
  33. for ( ; *p1 ; ++p1 )
  34. if ( StringMatch(p1, p2) ) {
  35. return TRUE;
  36. }
  37. break;
  38. } else if ( *p1 == *p2 ) {
  39. ++p1;
  40. ++p2;
  41. } else
  42. break;
  43. }
  44. if( !*p1 && *p2 == TEXT( '*' ))
  45. {
  46. ++p2;
  47. if (!*p2 ) {
  48. return TRUE;
  49. }
  50. }
  51. return !*p1 && !*p2;
  52. }
  53. BOOL
  54. IniSection::
  55. FindINISectionFromPortDescription(
  56. LPCTSTR pszPortDesc
  57. )
  58. /*++
  59. --*/
  60. {
  61. LPTSTR pszBuf = NULL, pszKey = NULL;
  62. DWORD rc = 0, dwLen = 0, dwBufLen = 1024;
  63. BOOL bRet = FALSE;
  64. pszBuf = (LPTSTR) malloc(dwBufLen*sizeof(TCHAR));
  65. //
  66. // Read all the key names in the ini file
  67. //
  68. while ( pszBuf ) {
  69. rc = GetPrivateProfileString(PORT_SECTION,
  70. NULL,
  71. NULL,
  72. pszBuf,
  73. dwBufLen,
  74. m_szIniFileName);
  75. if ( rc == 0 ) {
  76. goto Done;
  77. }
  78. if ( rc < dwBufLen - 2 ) {
  79. break; // Succesful exit; Read all port descriptions
  80. }
  81. free(pszBuf);
  82. dwBufLen *= 2;
  83. pszBuf = (LPTSTR) malloc(dwBufLen*sizeof(TCHAR));
  84. }
  85. if ( !pszBuf )
  86. goto Done;
  87. //
  88. // Go through the list of key names in the .INI till we find a match
  89. //
  90. for ( pszKey = pszBuf ; *pszKey ; pszKey += dwLen + 1 ) {
  91. //
  92. // Keys start and end with " we need to do match w/o them
  93. //
  94. dwLen = _tcslen(pszKey);
  95. pszKey[dwLen-1] = TCHAR('\0');
  96. if ( StringMatch(pszPortDesc, pszKey+1) ) {
  97. pszKey[dwLen-1] = TCHAR('\"');
  98. GetPrivateProfileString(PORT_SECTION,
  99. pszKey,
  100. NULL,
  101. m_szSectionName,
  102. MAX_SECTION_NAME,
  103. m_szIniFileName);
  104. bRet = TRUE;
  105. goto Done;
  106. }
  107. }
  108. Done:
  109. if ( pszBuf ) {
  110. free(pszBuf);
  111. }
  112. return( bRet );
  113. }
  114. IniSection::
  115. IniSection(
  116. void
  117. )
  118. {
  119. DWORD dwLen = 0, dwSize = 0;
  120. m_szSectionName[0] = TEXT('\0');
  121. m_szIniFileName[0] = TEXT('\0');
  122. dwSize = sizeof(m_szIniFileName)/sizeof(m_szIniFileName[0]);
  123. dwLen = GetSystemDirectory(m_szIniFileName, dwSize);
  124. if ( dwLen + _tcslen(PORTMONITOR_INI_FILE) > dwSize ) {
  125. return;
  126. }
  127. StringCchCat (m_szIniFileName, dwSize, PORTMONITOR_INI_FILE);
  128. }
  129. IniSection::
  130. ~IniSection(
  131. )
  132. {
  133. // Nothing to do
  134. }
  135. BOOL
  136. IniSection::
  137. GetString(
  138. IN LPTSTR pszKey,
  139. OUT TCHAR szBuf[],
  140. IN DWORD cchBuf
  141. )
  142. {
  143. DWORD rc = 0;
  144. rc = GetPrivateProfileString(m_szSectionName,
  145. pszKey,
  146. NULL,
  147. szBuf,
  148. cchBuf,
  149. m_szIniFileName);
  150. return rc > 0 && rc < cchBuf - 1;
  151. }
  152. BOOL
  153. IniSection::
  154. GetDWord(
  155. IN LPTSTR pszKey,
  156. OUT LPDWORD pdwValue
  157. )
  158. {
  159. UINT uVal;
  160. uVal = GetPrivateProfileInt(m_szSectionName,
  161. pszKey,
  162. -1,
  163. m_szIniFileName);
  164. if ( uVal != -1 ) {
  165. *pdwValue = (DWORD) uVal;
  166. return TRUE;
  167. } else {
  168. return FALSE;
  169. }
  170. }
  171. BOOL
  172. IniSection::
  173. SetIniSection(
  174. LPTSTR pszPortSection
  175. )
  176. {
  177. lstrcpyn( m_szSectionName, pszPortSection, MAX_SECTION_NAME );
  178. return( TRUE );
  179. }
  180. BOOL
  181. IniSection::
  182. GetIniSection(
  183. LPTSTR pszPortDescription
  184. )
  185. {
  186. BOOL bRet = FALSE;
  187. if ( m_szIniFileName[0] != 0 ) {
  188. bRet = FindINISectionFromPortDescription(pszPortDescription);
  189. }
  190. return( bRet );
  191. }
  192. //
  193. // FUNCTION: GetPortInfo
  194. //
  195. // PURPOSE: To read information about a device from the ini file.
  196. //
  197. BOOL
  198. IniSection::
  199. GetPortInfo(LPCTSTR pAddress,
  200. PPORT_DATA_1 pPortInfo,
  201. DWORD dwPortIndex,
  202. BOOL bBypassMibProbe)
  203. {
  204. BOOL bRet = TRUE;
  205. TCHAR KeyName[26];
  206. if( !Valid() ) {
  207. bRet = FALSE;
  208. goto Done;
  209. }
  210. //
  211. // Protocol
  212. //
  213. StringCchPrintf (KeyName, COUNTOF (KeyName), PROTOCOL_KEY, dwPortIndex);
  214. TCHAR tcsProtocol[50];
  215. GetPrivateProfileString(m_szSectionName,
  216. KeyName,
  217. TEXT(""),
  218. tcsProtocol,
  219. 50,
  220. m_szIniFileName);
  221. if( !_tcsicmp( RAW_PROTOCOL_TEXT, tcsProtocol)) {
  222. pPortInfo->dwProtocol = PROTOCOL_RAWTCP_TYPE;
  223. //
  224. // Port Number
  225. //
  226. StringCchPrintf (KeyName, COUNTOF (KeyName), PORT_NUMBER_KEY, dwPortIndex);
  227. pPortInfo->dwPortNumber = GetPrivateProfileInt(m_szSectionName,
  228. KeyName,
  229. DEFAULT_PORT_NUMBER,
  230. m_szIniFileName);
  231. } else if( !_tcsicmp( LPR_PROTOCOL_TEXT, tcsProtocol)) {
  232. pPortInfo->dwProtocol = PROTOCOL_LPR_TYPE;
  233. pPortInfo->dwPortNumber = LPR_DEFAULT_PORT_NUMBER;
  234. //
  235. // LPR QUEUE
  236. //
  237. StringCchPrintf (KeyName, COUNTOF (KeyName), QUEUE_KEY, dwPortIndex);
  238. GetPrivateProfileString(m_szSectionName,
  239. KeyName,
  240. DEFAULT_QUEUE,
  241. pPortInfo->sztQueue,
  242. MAX_QUEUENAME_LEN,
  243. m_szIniFileName);
  244. //
  245. // LPR Double Spool - default 0
  246. //
  247. StringCchPrintf (KeyName, COUNTOF (KeyName), DOUBLESPOOL_KEY, dwPortIndex);
  248. pPortInfo->dwDoubleSpool = GetPrivateProfileInt(m_szSectionName,
  249. KeyName,
  250. 0,
  251. m_szIniFileName);
  252. }
  253. //
  254. // CommunityName
  255. //
  256. StringCchPrintf (KeyName, COUNTOF (KeyName), COMMUNITY_KEY, dwPortIndex);
  257. GetPrivateProfileString(m_szSectionName,
  258. KeyName,
  259. DEFAULT_SNMP_COMUNITY,
  260. pPortInfo->sztSNMPCommunity,
  261. MAX_SNMP_COMMUNITY_STR_LEN,
  262. m_szIniFileName);
  263. //
  264. // DeviceIndex - default 1
  265. //
  266. StringCchPrintf (KeyName, COUNTOF (KeyName), DEVICE_KEY, dwPortIndex);
  267. pPortInfo->dwSNMPDevIndex = GetPrivateProfileInt(m_szSectionName,
  268. KeyName,
  269. 1,
  270. m_szIniFileName);
  271. //
  272. // SNMP Status Enabled - default ON
  273. //
  274. TCHAR szTemp[50];
  275. StringCchPrintf (KeyName, COUNTOF (KeyName), PORT_STATUS_ENABLED_KEY, dwPortIndex);
  276. GetPrivateProfileString(m_szSectionName,
  277. KeyName,
  278. YES_TEXT,
  279. szTemp,
  280. SIZEOF_IN_CHAR(szTemp),
  281. m_szIniFileName);
  282. if ( !(_tcsicmp( szTemp, YES_TEXT ))){
  283. pPortInfo->dwSNMPEnabled = TRUE;
  284. } else if (!(_tcsicmp( szTemp, NO_TEXT ))) {
  285. pPortInfo->dwSNMPEnabled = FALSE;
  286. } else {
  287. if (bBypassMibProbe)
  288. pPortInfo->dwSNMPEnabled = FALSE;
  289. else {
  290. BOOL bSupported;
  291. if (SupportsPrinterMIB( pAddress, &bSupported)) {
  292. pPortInfo->dwSNMPEnabled = bSupported;
  293. }
  294. else {
  295. // Error case, we have to disable SNMP
  296. pPortInfo->dwSNMPEnabled = FALSE;
  297. // The caller can check the returned error code to determine
  298. // whether the last error is "Device Not Found". If so,
  299. // the client should by pass Mib Probe in the next call
  300. //
  301. bRet = FALSE;
  302. }
  303. }
  304. }
  305. Done:
  306. return( bRet );
  307. } // GetPortInfo
  308. //
  309. BOOL
  310. IniSection::
  311. SupportsPrinterMIB(
  312. LPCTSTR pAddress,
  313. PBOOL pbSupported
  314. )
  315. {
  316. BOOL bRet = FALSE;
  317. CTcpMibABC *pTcpMib = NULL;
  318. FARPROC pfnGetTcpMibPtr = NULL;
  319. if ( !g_hTcpMibLib ) {
  320. goto Done;
  321. }
  322. pfnGetTcpMibPtr = ::GetProcAddress(g_hTcpMibLib, "GetTcpMibPtr");
  323. if ( !pfnGetTcpMibPtr ) {
  324. goto Done;
  325. }
  326. if ( pTcpMib = (CTcpMibABC *) pfnGetTcpMibPtr() ) {
  327. char HostName[MAX_NETWORKNAME_LEN] = "";
  328. UNICODE_TO_MBCS(HostName, MAX_NETWORKNAME_LEN, pAddress, -1);
  329. bRet = pTcpMib->SupportsPrinterMib(HostName,
  330. DEFAULT_SNMP_COMMUNITYA,
  331. DEFAULT_SNMP_DEVICE_INDEX,
  332. pbSupported);
  333. }
  334. Done:
  335. return bRet;
  336. } // GetDeviceType