Source code of Windows XP (NT5)
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.

382 lines
12 KiB

  1. //*********************************************************************
  2. //* Microsoft Windows **
  3. //* Copyright (c) 1994-1998 Microsoft Corporation
  4. //*********************************************************************
  5. //
  6. // TCPCFG.C - Functions to read and set TCP/IP configuration
  7. //
  8. // HISTORY:
  9. //
  10. // 96/05/22 markdu Created (from inetcfg.dll)
  11. //
  12. #include "pch.hpp"
  13. // function prototypes
  14. UINT DetectModifyTCPIPBindings(DWORD dwCardFlags,LPCSTR pszBoundTo,BOOL fRemove,BOOL * pfBound);
  15. //*******************************************************************
  16. //
  17. // FUNCTION: IcfgIsGlobalDNS
  18. //
  19. // PURPOSE: Determines whether there is Global DNS set.
  20. //
  21. // PARAMETERS: lpfGlobalDNS - TRUE if global DNS is set, FALSE otherwise.
  22. //
  23. // RETURNS: HRESULT code, ERROR_SUCCESS if no errors occurred
  24. // NOTE: This function is for Windows 95 only, and
  25. // should always return ERROR_SUCCESS and set lpfGlobalDNS
  26. // to FALSE in Windows NT.
  27. //
  28. //*******************************************************************
  29. extern "C" HRESULT IcfgIsGlobalDNS(LPBOOL lpfGlobalDNS)
  30. {
  31. CHAR szDNSEnabled[2]; // big enough for "1"
  32. BOOL fGlobalDNS = FALSE;
  33. // open the global TCP/IP key
  34. RegEntry reTcp(szTCPGlobalKeyName,HKEY_LOCAL_MACHINE);
  35. HRESULT hr = reTcp.GetError();
  36. if (hr == ERROR_SUCCESS)
  37. {
  38. // read the registry value to see if DNS is enabled
  39. reTcp.GetString(szRegValEnableDNS,szDNSEnabled,sizeof(szDNSEnabled));
  40. hr = reTcp.GetError();
  41. if ((hr == ERROR_SUCCESS) && (!lstrcmpi(szDNSEnabled,sz1)))
  42. {
  43. // DNS is enabled
  44. fGlobalDNS = TRUE;
  45. }
  46. }
  47. if (NULL != lpfGlobalDNS)
  48. {
  49. *lpfGlobalDNS = fGlobalDNS;
  50. }
  51. return hr;
  52. }
  53. //*******************************************************************
  54. //
  55. // FUNCTION: IcfgRemoveGlobalDNS
  56. //
  57. // PURPOSE: Removes global DNS info from registry.
  58. //
  59. // PARAMETERS: None.
  60. //
  61. // RETURNS: HRESULT code, ERROR_SUCCESS if no errors occurred
  62. // NOTE: This function is for Windows 95 only, and
  63. // should always return ERROR_SUCCESS in Windows NT.
  64. //
  65. //*******************************************************************
  66. extern "C" HRESULT IcfgRemoveGlobalDNS(void)
  67. {
  68. HRESULT hr = ERROR_SUCCESS;
  69. // open the global TCP/IP key
  70. RegEntry reTcp(szTCPGlobalKeyName,HKEY_LOCAL_MACHINE);
  71. hr = reTcp.GetError();
  72. ASSERT(hr == ERROR_SUCCESS);
  73. if (ERROR_SUCCESS == hr)
  74. {
  75. // no name servers; disable DNS. Set registry switch to "0".
  76. hr = reTcp.SetValue(szRegValEnableDNS,sz0);
  77. ASSERT(hr == ERROR_SUCCESS);
  78. }
  79. return hr;
  80. }
  81. //*******************************************************************
  82. //
  83. // FUNCTION: IcfgIsFileSharingTurnedOn
  84. //
  85. // PURPOSE: Determines if file server (VSERVER) is bound to TCP/IP
  86. // for specified driver type (net card or PPP).
  87. //
  88. // PARAMETERS: dwfDriverType - a combination of DRIVERTYPE_ flags
  89. // that specify what driver type to check server-TCP/IP
  90. // bindings for as follows:
  91. //
  92. // DRIVERTYPE_NET - net card
  93. // DRIVERTYPE_PPP - PPPMAC
  94. //
  95. // lpfSharingOn - TRUE if bound once or more, FALSE if not bound
  96. //
  97. // RETURNS: HRESULT code, ERROR_SUCCESS if no errors occurred
  98. //
  99. //*******************************************************************
  100. extern "C" HRESULT IcfgIsFileSharingTurnedOn(DWORD dwfDriverType, LPBOOL lpfSharingOn)
  101. {
  102. BOOL fBound = FALSE;
  103. ASSERT(lpfSharingOn);
  104. // call worker function
  105. HRESULT hr = DetectModifyTCPIPBindings(dwfDriverType,szVSERVER,FALSE,&fBound);
  106. if (NULL != lpfSharingOn)
  107. {
  108. *lpfSharingOn = fBound;
  109. }
  110. return hr;
  111. }
  112. //*******************************************************************
  113. //
  114. // FUNCTION: IcfgTurnOffFileSharing
  115. //
  116. // PURPOSE: Unbinds file server (VSERVER) from TCP/IP for
  117. // specified driver type (net card or PPP).
  118. //
  119. // PARAMETERS: dwfDriverType - a combination of DRIVERTYPE_ flags
  120. // that specify what driver type to remove server-TCP/IP
  121. // bindings for as follows:
  122. //
  123. // DRIVERTYPE_NET - net card
  124. // DRIVERTYPE_PPP - PPPMAC
  125. //
  126. // RETURNS: HRESULT code, ERROR_SUCCESS if no errors occurred
  127. //
  128. //*******************************************************************
  129. extern "C" HRESULT IcfgTurnOffFileSharing(DWORD dwfDriverType, HWND hwndParent)
  130. {
  131. BOOL fBound;
  132. // call worker function
  133. return DetectModifyTCPIPBindings(dwfDriverType,szVSERVER,TRUE,&fBound);
  134. }
  135. /*******************************************************************
  136. NAME: DetectModifyTCPIPBindings
  137. SYNOPSIS: Finds (and optionally removes) bindings between
  138. VSERVER and TCP/IP for TCP/IP instances on a particular
  139. card type.
  140. ENTRY: dwCardFlags - an INSTANCE_xxx flag to specify what
  141. card type to find/remove server-TCP/IP bindings for
  142. pszBoundTo - name of component to look for or modify bindings
  143. to. Can be VSERVER or VREDIR
  144. fRemove - if TRUE, all bindings are removed as we find them.
  145. If FALSE, bindings are left alone but *pfBound is set
  146. to TRUE if bindings exist.
  147. pfBound - pointer to BOOL to be filled in
  148. EXIT: ERROR_SUCCESS if successful, or a standard error code
  149. NOTES: Worker function for TurnOffFileSharing and IsFileSharingTurnedOn
  150. ********************************************************************/
  151. UINT DetectModifyTCPIPBindings(DWORD dwCardFlags,LPCSTR pszBoundTo,
  152. BOOL fRemove,BOOL * pfBound)
  153. {
  154. ASSERT(pfBound);
  155. *pfBound = FALSE; // assume not bound until proven otherwise
  156. ENUM_TCP_INSTANCE EnumTcpInstance(dwCardFlags,NT_ENUMNODE);
  157. UINT err = EnumTcpInstance.GetError();
  158. if (err != ERROR_SUCCESS)
  159. return err;
  160. HKEY hkeyInstance = EnumTcpInstance.Next();
  161. // for every TCP/IP node in enum branch, look at bindings key.
  162. // Scan the bindings (values in bindings key), if they begin
  163. // with the string pszBoundTo ("VSERVER" or "VREDIR") then
  164. // the binding exists.
  165. while (hkeyInstance) {
  166. // open bindings key
  167. RegEntry reBindings(szRegKeyBindings,hkeyInstance);
  168. ASSERT(reBindings.GetError() == ERROR_SUCCESS);
  169. if (reBindings.GetError() == ERROR_SUCCESS) {
  170. RegEnumValues * preBindingVals = new RegEnumValues(&reBindings);
  171. ASSERT(preBindingVals);
  172. if (!preBindingVals)
  173. return ERROR_NOT_ENOUGH_MEMORY;
  174. // enumerate binding values
  175. while (preBindingVals->Next() == ERROR_SUCCESS) {
  176. ASSERT(preBindingVals->GetName()); // should always have a valid ptr
  177. // does this binding begin with the string we were passed in
  178. // pszBoundTo
  179. CHAR szBindingVal[SMALL_BUF_LEN+1];
  180. DWORD dwBoundToLen = lstrlen(pszBoundTo);
  181. lstrcpy(szBindingVal,preBindingVals->GetName());
  182. if (((DWORD)lstrlen(szBindingVal)) >= dwBoundToLen) {
  183. // NULL-terminate the copy at the appropriate place
  184. // so we can do a strcmp rather than a strncmp, which
  185. // would involve pulling in C runtime or implementing
  186. // our own strncmp
  187. szBindingVal[dwBoundToLen] = '\0';
  188. if (!lstrcmpi(szBindingVal,pszBoundTo)) {
  189. *pfBound = TRUE;
  190. // remove the binding if specified by caller
  191. if (fRemove) {
  192. // delete the value
  193. reBindings.DeleteValue(preBindingVals->GetName());
  194. // destroy and reconstruct RegEnumValues object, otherwise
  195. // RegEnumValues api gets confused because we deleted a
  196. // value during enum
  197. delete preBindingVals;
  198. preBindingVals = new RegEnumValues(&reBindings);
  199. ASSERT(preBindingVals);
  200. if (!preBindingVals)
  201. return ERROR_NOT_ENOUGH_MEMORY;
  202. } else {
  203. // caller just wants to know if binding exists, we
  204. // filled in pfBound above so we're done
  205. return ERROR_SUCCESS;
  206. }
  207. }
  208. }
  209. }
  210. }
  211. hkeyInstance = EnumTcpInstance.Next();
  212. }
  213. return ERROR_SUCCESS;
  214. }
  215. /*******************************************************************
  216. NAME: ENUM_TCP_INSTANCE::ENUM_TCP_INSTANCE
  217. SYNOPSIS: Constructor for class to enumerate TCP/IP registry nodes
  218. according to type of card they are bound to
  219. ENTRY: dwCardFlags - combination of INSTANCE_x flags indicating
  220. what kind of card to enumerate instances for
  221. dwNodeFlags - combination of NT_ flags indicating what
  222. type of node to return (driver node, enum node)
  223. ********************************************************************/
  224. ENUM_TCP_INSTANCE::ENUM_TCP_INSTANCE(DWORD dwCardFlags,DWORD dwNodeFlags) :
  225. _dwCardFlags (dwCardFlags), _dwNodeFlags (dwNodeFlags)
  226. {
  227. _hkeyTcpNode = NULL;
  228. _error = ERROR_SUCCESS;
  229. // init/reset netcard enumeration
  230. BeginNetcardTCPIPEnum();
  231. }
  232. /*******************************************************************
  233. NAME: ENUM_TCP_INSTANCE::~ENUM_TCP_INSTANCE
  234. SYNOPSIS: Destructor for class
  235. ********************************************************************/
  236. ENUM_TCP_INSTANCE::~ENUM_TCP_INSTANCE()
  237. {
  238. // close current TCP node key, if any
  239. CloseNode();
  240. }
  241. /*******************************************************************
  242. NAME: ENUM_TCP_INSTANCE::Next
  243. SYNOPSIS: Enumerates next TCP/IP driver node
  244. EXIT: Returns an open registry key handle, or NULL if
  245. no more nodes.
  246. NOTES: Caller should not close the HKEY that is returned. This
  247. HKEY will be valid until the next time the Next() method
  248. is called or until the object is destructed.
  249. ********************************************************************/
  250. HKEY ENUM_TCP_INSTANCE::Next()
  251. {
  252. CHAR szSubkeyName[MAX_PATH+1];
  253. // close current TCP node key, if any
  254. CloseNode();
  255. while (_error == ERROR_SUCCESS) {
  256. CHAR szInstancePath[SMALL_BUF_LEN+1];
  257. CHAR szDriverPath[SMALL_BUF_LEN+1];
  258. if (!GetNextNetcardTCPIPNode(szSubkeyName,sizeof(szSubkeyName),
  259. _dwCardFlags))
  260. return NULL; // no more nodes
  261. // open the enum branch, find the specified subkey
  262. RegEntry reEnumNet(szRegPathEnumNet,HKEY_LOCAL_MACHINE);
  263. // if caller wanted enum node, just open that node
  264. if (_dwNodeFlags & NT_ENUMNODE) {
  265. _error = RegOpenKey(reEnumNet.GetKey(),szSubkeyName,
  266. &_hkeyTcpNode);
  267. // return open key
  268. return _hkeyTcpNode;
  269. } else {
  270. // from enum node, figure out path to driver node
  271. reEnumNet.MoveToSubKey(szSubkeyName);
  272. if (reEnumNet.GetError() != ERROR_SUCCESS)
  273. continue;
  274. // find the driver path to the driver node
  275. if (!reEnumNet.GetString(szRegValDriver,szDriverPath,
  276. sizeof(szDriverPath))) {
  277. ASSERTSZ(FALSE,"No driver path in enum branch for TCP/IP instance");
  278. continue;
  279. }
  280. // build the path to registry node for this instance
  281. lstrcpy(szInstancePath,szRegPathClass);
  282. lstrcat(szInstancePath,szDriverPath);
  283. _error = RegOpenKey(HKEY_LOCAL_MACHINE,szInstancePath,
  284. &_hkeyTcpNode);
  285. // return open key
  286. return _hkeyTcpNode;
  287. }
  288. }
  289. // ran through all net cards of specified type w/o finding TCP/IP bound
  290. _error = ERROR_NO_MORE_ITEMS;
  291. return NULL;
  292. }
  293. /*******************************************************************
  294. NAME: ENUM_TCP_INSTANCE::CloseNode
  295. SYNOPSIS: Private worker function to close TCP/IP node handle
  296. ********************************************************************/
  297. VOID ENUM_TCP_INSTANCE::CloseNode()
  298. {
  299. if (_hkeyTcpNode) {
  300. RegCloseKey(_hkeyTcpNode);
  301. _hkeyTcpNode = NULL;
  302. }
  303. }