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.

2285 lines
50 KiB

  1. /*++
  2. Copyright (c) 1998, Microsoft Corporation
  3. Module Name:
  4. dnsif.c
  5. Abstract:
  6. This module contains code for the DNS proxy's interface management.
  7. Author:
  8. Abolade Gbadegesin (aboladeg) 9-Mar-1998
  9. Revision History:
  10. --*/
  11. #include "precomp.h"
  12. #pragma hdrstop
  13. //
  14. // LOCAL TYPE DECLARATIONS
  15. //
  16. typedef struct _DNS_DEFER_READ_CONTEXT {
  17. ULONG Index;
  18. SOCKET Socket;
  19. ULONG DeferralCount;
  20. } DNS_DEFER_READ_CONTEXT, *PDNS_DEFER_READ_CONTEXT;
  21. #define DNS_DEFER_READ_INITIAL_TIMEOUT (1 * 1000)
  22. #define DNS_DEFER_READ_TIMEOUT (5 * 1000)
  23. #define DNS_CONNECT_TIMEOUT (60 * 1000)
  24. //
  25. // GLOBAL DATA DEFINITIONS
  26. //
  27. LIST_ENTRY DnsInterfaceList;
  28. CRITICAL_SECTION DnsInterfaceLock;
  29. ULONG DnspLastConnectAttemptTickCount;
  30. //
  31. // Forward declarations
  32. //
  33. VOID NTAPI
  34. DnspDeferReadCallbackRoutine(
  35. PVOID Context,
  36. BOOLEAN TimedOut
  37. );
  38. VOID APIENTRY
  39. DnspDeferReadWorkerRoutine(
  40. PVOID Context
  41. );
  42. ULONG NTAPI
  43. DnspSaveFileWorkerRoutine(
  44. PVOID Context
  45. );
  46. ULONG
  47. DnsActivateInterface(
  48. PDNS_INTERFACE Interfacep
  49. )
  50. /*++
  51. Routine Description:
  52. This routine is called to activate an interface, when the interface
  53. becomes both enabled and bound.
  54. Activation involves
  55. (a) creating sockets for each binding of the interface
  56. (b) initiating datagram-reads on each created socket
  57. Arguments:
  58. Interfacep - the interface to be activated
  59. Return Value:
  60. ULONG - Win32 status code indicating success or failure.
  61. Environment:
  62. Always invoked locally, with 'Interfacep' referenced by caller and/or
  63. 'DnsInterfaceLock' held by caller.
  64. --*/
  65. {
  66. BOOLEAN EnableDns;
  67. BOOLEAN EnableWins = FALSE;
  68. ULONG Error;
  69. ULONG i;
  70. BOOLEAN IsNatInterface;
  71. PROFILE("DnsActivateInterface");
  72. EnterCriticalSection(&DnsGlobalInfoLock);
  73. EnableDns =
  74. (DnsGlobalInfo->Flags & IP_DNS_PROXY_FLAG_ENABLE_DNS) ? TRUE : FALSE;
  75. LeaveCriticalSection(&DnsGlobalInfoLock);
  76. //
  77. // (re)take the interface lock for the duration of the routine
  78. //
  79. EnterCriticalSection(&DnsInterfaceLock);
  80. if (!(EnableDns || EnableWins) ||
  81. DNS_INTERFACE_ADMIN_DISABLED(Interfacep)) {
  82. LeaveCriticalSection(&DnsInterfaceLock);
  83. return NO_ERROR;
  84. }
  85. if (NhIsBoundaryInterface(Interfacep->Index, &IsNatInterface)) {
  86. NhTrace(
  87. TRACE_FLAG_DNS,
  88. "DnsActivateInterface: ignoring NAT interface %d",
  89. Interfacep->Index
  90. );
  91. NhWarningLog(
  92. IP_DNS_PROXY_LOG_NAT_INTERFACE_IGNORED,
  93. 0,
  94. "%d",
  95. Interfacep->Index
  96. );
  97. LeaveCriticalSection(&DnsInterfaceLock);
  98. return NO_ERROR;
  99. }
  100. if (!IsNatInterface) {
  101. NhTrace(
  102. TRACE_FLAG_DNS,
  103. "DnsActivateInterface: ignoring non-NAT interface %d",
  104. Interfacep->Index
  105. );
  106. LeaveCriticalSection(&DnsInterfaceLock);
  107. return NO_ERROR;
  108. }
  109. //
  110. // Create datagram sockets for receiving data on each logical network
  111. //
  112. Error = NO_ERROR;
  113. ACQUIRE_LOCK(Interfacep);
  114. for (i = 0; i < Interfacep->BindingCount; i++) {
  115. if (EnableDns) {
  116. Error =
  117. NhCreateDatagramSocket(
  118. Interfacep->BindingArray[i].Address,
  119. DNS_PORT_SERVER,
  120. &Interfacep->BindingArray[i].Socket[DnsProxyDns]
  121. );
  122. if (Error) { break; }
  123. }
  124. if (EnableWins) {
  125. Error =
  126. NhCreateDatagramSocket(
  127. Interfacep->BindingArray[i].Address,
  128. WINS_PORT_SERVER,
  129. &Interfacep->BindingArray[i].Socket[DnsProxyWins]
  130. );
  131. if (Error) { break; }
  132. }
  133. }
  134. //
  135. // If an error occurred, roll back all work done so far and fail.
  136. //
  137. if (Error) {
  138. ULONG FailedAddress = i;
  139. for (; (LONG)i >= 0; i--) {
  140. NhDeleteDatagramSocket(
  141. Interfacep->BindingArray[i].Socket[DnsProxyDns]
  142. );
  143. Interfacep->BindingArray[i].Socket[DnsProxyDns] = INVALID_SOCKET;
  144. NhDeleteDatagramSocket(
  145. Interfacep->BindingArray[i].Socket[DnsProxyWins]
  146. );
  147. Interfacep->BindingArray[i].Socket[DnsProxyWins] = INVALID_SOCKET;
  148. }
  149. NhErrorLog(
  150. IP_DNS_PROXY_LOG_ACTIVATE_FAILED,
  151. Error,
  152. "%I",
  153. Interfacep->BindingArray[FailedAddress].Address
  154. );
  155. RELEASE_LOCK(Interfacep);
  156. LeaveCriticalSection(&DnsInterfaceLock);
  157. return Error;
  158. }
  159. if (EnableWins && DNS_REFERENCE_INTERFACE(Interfacep)) {
  160. Error =
  161. NhReadDatagramSocket(
  162. &DnsComponentReference,
  163. DnsGlobalSocket,
  164. NULL,
  165. DnsReadCompletionRoutine,
  166. Interfacep,
  167. NULL
  168. );
  169. if (Error) { DNS_DEREFERENCE_INTERFACE(Interfacep); }
  170. }
  171. //
  172. // Initiate read-operations on each socket
  173. //
  174. for (i = 0; i < Interfacep->BindingCount; i++) {
  175. if (EnableDns) {
  176. //
  177. // Make a reference to the interface;
  178. // this reference is released in the completion routine
  179. //
  180. if (!DNS_REFERENCE_INTERFACE(Interfacep)) { continue; }
  181. //
  182. // Initiate the read-operation
  183. //
  184. Error =
  185. NhReadDatagramSocket(
  186. &DnsComponentReference,
  187. Interfacep->BindingArray[i].Socket[DnsProxyDns],
  188. NULL,
  189. DnsReadCompletionRoutine,
  190. Interfacep,
  191. UlongToPtr(Interfacep->BindingArray[i].Address)
  192. );
  193. //
  194. // Drop the reference if a failure occurred
  195. //
  196. if (Error) {
  197. NhErrorLog(
  198. IP_DNS_PROXY_LOG_RECEIVE_FAILED,
  199. Error,
  200. "%I",
  201. Interfacep->BindingArray[i].Address
  202. );
  203. DNS_DEREFERENCE_INTERFACE(Interfacep);
  204. //
  205. // Reissue the read-operation later
  206. //
  207. DnsDeferReadInterface(
  208. Interfacep,
  209. Interfacep->BindingArray[i].Socket[DnsProxyDns]
  210. );
  211. Error = NO_ERROR;
  212. }
  213. }
  214. if (EnableWins) {
  215. //
  216. // Reference the interface for the WINS socket receive
  217. //
  218. if (!DNS_REFERENCE_INTERFACE(Interfacep)) { continue; }
  219. //
  220. // Initiate the read-operation
  221. //
  222. Error =
  223. NhReadDatagramSocket(
  224. &DnsComponentReference,
  225. Interfacep->BindingArray[i].Socket[DnsProxyWins],
  226. NULL,
  227. DnsReadCompletionRoutine,
  228. Interfacep,
  229. UlongToPtr(Interfacep->BindingArray[i].Address)
  230. );
  231. //
  232. // Drop the reference if a failure occurred
  233. //
  234. if (Error) {
  235. NhErrorLog(
  236. IP_DNS_PROXY_LOG_RECEIVE_FAILED,
  237. Error,
  238. "%I",
  239. Interfacep->BindingArray[i].Address
  240. );
  241. DNS_DEREFERENCE_INTERFACE(Interfacep);
  242. //
  243. // Reissue the read-operation later
  244. //
  245. DnsDeferReadInterface(
  246. Interfacep,
  247. Interfacep->BindingArray[i].Socket[DnsProxyWins]
  248. );
  249. Error = NO_ERROR;
  250. }
  251. }
  252. }
  253. RELEASE_LOCK(Interfacep);
  254. LeaveCriticalSection(&DnsInterfaceLock);
  255. //
  256. // queue a write to disk (ip address may have changed)
  257. // (necessary to do this to prevent possible deadlock)
  258. //
  259. if (REFERENCE_DNS())
  260. {
  261. if (!QueueUserWorkItem(DnspSaveFileWorkerRoutine, NULL, WT_EXECUTEDEFAULT))
  262. {
  263. Error = GetLastError();
  264. NhTrace(
  265. TRACE_FLAG_DNS,
  266. "DnsActivateInterface: QueueUserWorkItem failed with error %d (0x%08x)",
  267. Error,
  268. Error
  269. );
  270. DEREFERENCE_DNS();
  271. }
  272. else
  273. {
  274. NhTrace(
  275. TRACE_FLAG_DNS,
  276. "DnsActivateInterface: queued a write of %s file",
  277. HOSTSICSFILE
  278. );
  279. }
  280. }
  281. return NO_ERROR;
  282. } // DnsActivateInterface
  283. ULONG
  284. DnsBindInterface(
  285. ULONG Index,
  286. PIP_ADAPTER_BINDING_INFO BindingInfo
  287. )
  288. /*++
  289. Routine Description:
  290. This routine is invoked to supply the binding for an interface.
  291. It records the binding information received, and if necessary,
  292. it activates the interface.
  293. Arguments:
  294. Index - the index of the interface to be bound
  295. BindingInfo - the binding-information for the interface
  296. Return Value:
  297. ULONG - Win32 status code.
  298. Environment:
  299. Invoked internally in the context of an IP router-manager thread.
  300. (See 'RMDNS.C').
  301. --*/
  302. {
  303. ULONG Error = NO_ERROR;
  304. ULONG i;
  305. PDNS_INTERFACE Interfacep;
  306. PROFILE("DnsBindInterface");
  307. EnterCriticalSection(&DnsInterfaceLock);
  308. //
  309. // Retrieve the interface to be bound
  310. //
  311. if (!(Interfacep = DnsLookupInterface(Index, NULL))) {
  312. LeaveCriticalSection(&DnsInterfaceLock);
  313. NhTrace(
  314. TRACE_FLAG_IF,
  315. "DnsBindInterface: interface %d not found",
  316. Index
  317. );
  318. return ERROR_NO_SUCH_INTERFACE;
  319. }
  320. //
  321. // Make sure the interface isn't already bound
  322. //
  323. if (DNS_INTERFACE_BOUND(Interfacep)) {
  324. LeaveCriticalSection(&DnsInterfaceLock);
  325. NhTrace(
  326. TRACE_FLAG_IF,
  327. "DnsBindInterface: interface %d is already bound",
  328. Index
  329. );
  330. return ERROR_ADDRESS_ALREADY_ASSOCIATED;
  331. }
  332. //
  333. // Reference the interface
  334. //
  335. if (!DNS_REFERENCE_INTERFACE(Interfacep)) {
  336. LeaveCriticalSection(&DnsInterfaceLock);
  337. NhTrace(
  338. TRACE_FLAG_IF,
  339. "DnsBindInterface: interface %d cannot be referenced",
  340. Index
  341. );
  342. return ERROR_INTERFACE_DISABLED;
  343. }
  344. //
  345. // Update the interface's flags
  346. //
  347. Interfacep->Flags |= DNS_INTERFACE_FLAG_BOUND;
  348. LeaveCriticalSection(&DnsInterfaceLock);
  349. ACQUIRE_LOCK(Interfacep);
  350. //
  351. // Allocate space for the binding
  352. //
  353. if (!BindingInfo->AddressCount) {
  354. Interfacep->BindingCount = 0;
  355. Interfacep->BindingArray = NULL;
  356. }
  357. else {
  358. Interfacep->BindingArray =
  359. reinterpret_cast<PDNS_BINDING>(
  360. NH_ALLOCATE(BindingInfo->AddressCount * sizeof(DNS_BINDING))
  361. );
  362. if (!Interfacep->BindingArray) {
  363. RELEASE_LOCK(Interfacep);
  364. DNS_DEREFERENCE_INTERFACE(Interfacep);
  365. NhTrace(
  366. TRACE_FLAG_IF,
  367. "DnsBindInterface: allocation failed for interface %d binding",
  368. Index
  369. );
  370. NhErrorLog(
  371. IP_DNS_PROXY_LOG_ALLOCATION_FAILED,
  372. 0,
  373. "%d",
  374. BindingInfo->AddressCount * sizeof(DNS_BINDING)
  375. );
  376. return ERROR_NOT_ENOUGH_MEMORY;
  377. }
  378. Interfacep->BindingCount = BindingInfo->AddressCount;
  379. }
  380. //
  381. // Copy the binding
  382. //
  383. for (i = 0; i < BindingInfo->AddressCount; i++) {
  384. Interfacep->BindingArray[i].Address = BindingInfo->Address[i].Address;
  385. Interfacep->BindingArray[i].Mask = BindingInfo->Address[i].Mask;
  386. Interfacep->BindingArray[i].Socket[DnsProxyDns] = INVALID_SOCKET;
  387. Interfacep->BindingArray[i].TimerPending[DnsProxyDns] = FALSE;
  388. Interfacep->BindingArray[i].Socket[DnsProxyWins] = INVALID_SOCKET;
  389. Interfacep->BindingArray[i].TimerPending[DnsProxyWins] = FALSE;
  390. }
  391. RELEASE_LOCK(Interfacep);
  392. //
  393. // Activate the interface if necessary
  394. //
  395. if (DNS_INTERFACE_ACTIVE(Interfacep)) {
  396. Error = DnsActivateInterface(Interfacep);
  397. }
  398. DNS_DEREFERENCE_INTERFACE(Interfacep);
  399. return Error;
  400. } // DnsBindInterface
  401. VOID
  402. DnsCleanupInterface(
  403. PDNS_INTERFACE Interfacep
  404. )
  405. /*++
  406. Routine Description:
  407. This routine is invoked when the very last reference to an interface
  408. is released, and the interface must be destroyed.
  409. Arguments:
  410. Interfacep - the interface to be destroyed
  411. Return Value:
  412. none.
  413. Environment:
  414. Invoked internally from an arbitrary context.
  415. --*/
  416. {
  417. PLIST_ENTRY Link;
  418. PDNS_QUERY Queryp;
  419. PROFILE("DnsCleanupInterface");
  420. if (Interfacep->BindingArray) {
  421. NH_FREE(Interfacep->BindingArray);
  422. Interfacep->BindingArray = NULL;
  423. }
  424. while (!IsListEmpty(&Interfacep->QueryList)) {
  425. Link = Interfacep->QueryList.Flink;
  426. Queryp = CONTAINING_RECORD(Link, DNS_QUERY, Link);
  427. DnsDeleteQuery(Interfacep, Queryp);
  428. }
  429. DeleteCriticalSection(&Interfacep->Lock);
  430. NH_FREE(Interfacep);
  431. } // DnsCleanupInterface
  432. VOID
  433. DnsConnectDefaultInterface(
  434. PVOID Unused
  435. )
  436. /*++
  437. Routine Description:
  438. This routine is invoked to attempt to initiate a demand-dial connection
  439. on the interface marked as 'default' for DNS requests.
  440. If no such interface is found, no operation is performed.
  441. Arguments:
  442. none used.
  443. Return Value:
  444. ULONG - Win32 status code.
  445. Environment:
  446. The routine is invoked from the context of an RTUTILS work item.
  447. --*/
  448. {
  449. ULONG Error;
  450. ULONG Index;
  451. PDNS_INTERFACE Interfacep;
  452. PLIST_ENTRY Link;
  453. ULONG TickCount;
  454. ROUTER_INTERFACE_TYPE Type;
  455. PROFILE("DnsConnectDefaultInterface");
  456. //
  457. // To avoid repeated autodial dialogs, we record the last time
  458. // we attempted to connect the default interface.
  459. // If we did so recently, return silently.
  460. // N.B. If the tick-count wrapped, we reset the last-attempt counter.
  461. //
  462. EnterCriticalSection(&DnsGlobalInfoLock);
  463. TickCount = NtGetTickCount();
  464. if (TickCount > DnspLastConnectAttemptTickCount &&
  465. TickCount <= (DnspLastConnectAttemptTickCount + DNS_CONNECT_TIMEOUT)
  466. ) {
  467. LeaveCriticalSection(&DnsGlobalInfoLock);
  468. DEREFERENCE_DNS();
  469. return;
  470. }
  471. DnspLastConnectAttemptTickCount = TickCount;
  472. LeaveCriticalSection(&DnsGlobalInfoLock);
  473. //
  474. // Look through the interface list for one which is marked as default
  475. //
  476. EnterCriticalSection(&DnsInterfaceLock);
  477. for (Link = DnsInterfaceList.Flink;
  478. Link != &DnsInterfaceList;
  479. Link = Link->Flink
  480. ) {
  481. Interfacep = CONTAINING_RECORD(Link, DNS_INTERFACE, Link);
  482. if (!DNS_INTERFACE_ADMIN_DEFAULT(Interfacep)) { continue; }
  483. //
  484. // We've found the default interface.
  485. //
  486. Index = Interfacep->Index;
  487. LeaveCriticalSection(&DnsInterfaceLock);
  488. //
  489. // Attempt to connect it.
  490. //
  491. EnterCriticalSection(&DnsGlobalInfoLock);
  492. Error =
  493. DnsSupportFunctions.DemandDialRequest(
  494. MS_IP_DNS_PROXY,
  495. Index
  496. );
  497. LeaveCriticalSection(&DnsGlobalInfoLock);
  498. DEREFERENCE_DNS();
  499. return;
  500. }
  501. //
  502. // No interface is marked as the default.
  503. //
  504. LeaveCriticalSection(&DnsInterfaceLock);
  505. NhDialSharedConnection();
  506. NhWarningLog(
  507. IP_DNS_PROXY_LOG_NO_DEFAULT_INTERFACE,
  508. 0,
  509. ""
  510. );
  511. DEREFERENCE_DNS();
  512. } // DnsConnectDefaultInterface
  513. ULONG
  514. DnsConfigureInterface(
  515. ULONG Index,
  516. PIP_DNS_PROXY_INTERFACE_INFO InterfaceInfo
  517. )
  518. /*++
  519. Routine Description:
  520. This routine is called to set the configuration for an interface.
  521. Arguments:
  522. Index - the interface to be configured
  523. InterfaceInfo - the new configuration
  524. Return Value:
  525. ULONG - Win32 status code
  526. Environment:
  527. Invoked internally in the context of a IP router-manager thread.
  528. (See 'RMDNS.C').
  529. --*/
  530. {
  531. ULONG Error;
  532. PDNS_INTERFACE Interfacep;
  533. ULONG NewFlags;
  534. ULONG OldFlags;
  535. PROFILE("DnsConfigureInterface");
  536. //
  537. // Retrieve the interface to be configured
  538. //
  539. EnterCriticalSection(&DnsInterfaceLock);
  540. if (!(Interfacep = DnsLookupInterface(Index, NULL))) {
  541. LeaveCriticalSection(&DnsInterfaceLock);
  542. NhTrace(
  543. TRACE_FLAG_IF,
  544. "DnsConfigureInterface: interface %d not found",
  545. Index
  546. );
  547. return ERROR_NO_SUCH_INTERFACE;
  548. }
  549. //
  550. // Reference the interface
  551. //
  552. if (!DNS_REFERENCE_INTERFACE(Interfacep)) {
  553. LeaveCriticalSection(&DnsInterfaceLock);
  554. NhTrace(
  555. TRACE_FLAG_IF,
  556. "DnsConfigureInterface: interface %d cannot be referenced",
  557. Index
  558. );
  559. return ERROR_INTERFACE_DISABLED;
  560. }
  561. LeaveCriticalSection(&DnsInterfaceLock);
  562. Error = NO_ERROR;
  563. ACQUIRE_LOCK(Interfacep);
  564. //
  565. // Compare the interface's current and new configuration
  566. //
  567. OldFlags = Interfacep->Info.Flags;
  568. NewFlags =
  569. (InterfaceInfo
  570. ? (InterfaceInfo->Flags|DNS_INTERFACE_FLAG_CONFIGURED) : 0);
  571. Interfacep->Flags &= ~OldFlags;
  572. Interfacep->Flags |= NewFlags;
  573. if (!InterfaceInfo) {
  574. ZeroMemory(&Interfacep->Info, sizeof(*InterfaceInfo));
  575. //
  576. // The interface no longer has any information;
  577. // default to being enabled.
  578. //
  579. if (OldFlags & IP_DNS_PROXY_INTERFACE_FLAG_DISABLED) {
  580. //
  581. // Activate the interface if necessary
  582. //
  583. if (DNS_INTERFACE_ACTIVE(Interfacep)) {
  584. RELEASE_LOCK(Interfacep);
  585. Error = DnsActivateInterface(Interfacep);
  586. ACQUIRE_LOCK(Interfacep);
  587. }
  588. }
  589. }
  590. else {
  591. CopyMemory(&Interfacep->Info, InterfaceInfo, sizeof(*InterfaceInfo));
  592. //
  593. // Activate or deactivate the interface if its status changed
  594. //
  595. if ((OldFlags & IP_DNS_PROXY_INTERFACE_FLAG_DISABLED) &&
  596. !(NewFlags & IP_DNS_PROXY_INTERFACE_FLAG_DISABLED)) {
  597. //
  598. // Activate the interface
  599. //
  600. if (DNS_INTERFACE_ACTIVE(Interfacep)) {
  601. RELEASE_LOCK(Interfacep);
  602. Error = DnsActivateInterface(Interfacep);
  603. ACQUIRE_LOCK(Interfacep);
  604. }
  605. }
  606. else
  607. if (!(OldFlags & IP_DNS_PROXY_INTERFACE_FLAG_DISABLED) &&
  608. (NewFlags & IP_DNS_PROXY_INTERFACE_FLAG_DISABLED)) {
  609. //
  610. // Deactivate the interface if necessary
  611. //
  612. if (DNS_INTERFACE_ACTIVE(Interfacep)) {
  613. RELEASE_LOCK(Interfacep);
  614. DnsDeactivateInterface(Interfacep);
  615. ACQUIRE_LOCK(Interfacep);
  616. }
  617. }
  618. }
  619. RELEASE_LOCK(Interfacep);
  620. DNS_DEREFERENCE_INTERFACE(Interfacep);
  621. return Error;
  622. } // DnsConfigureInterface
  623. ULONG
  624. DnsCreateInterface(
  625. ULONG Index,
  626. NET_INTERFACE_TYPE Type,
  627. PIP_DNS_PROXY_INTERFACE_INFO InterfaceInfo,
  628. OUT PDNS_INTERFACE* InterfaceCreated
  629. )
  630. /*++
  631. Routine Description:
  632. This routine is invoked by the router-manager to add a new interface
  633. to the DNS proxy.
  634. Arguments:
  635. Index - the index of the new interface
  636. Type - the media type of the new interface
  637. InterfaceInfo - the interface's configuration
  638. Interfacep - receives the interface created
  639. Return Value:
  640. ULONG - Win32 error code
  641. Environment:
  642. Invoked internally in the context of an IP router-manager thread.
  643. (See 'RMDNS.C').
  644. --*/
  645. {
  646. PLIST_ENTRY InsertionPoint;
  647. PDNS_INTERFACE Interfacep;
  648. PROFILE("DnsCreateInterface");
  649. EnterCriticalSection(&DnsInterfaceLock);
  650. //
  651. // See if the interface already exists;
  652. // If not, this obtains the insertion point
  653. //
  654. if (DnsLookupInterface(Index, &InsertionPoint)) {
  655. LeaveCriticalSection(&DnsInterfaceLock);
  656. NhTrace(
  657. TRACE_FLAG_IF,
  658. "DnsCreateInterface: duplicate index found for %d",
  659. Index
  660. );
  661. return ERROR_INTERFACE_ALREADY_EXISTS;
  662. }
  663. //
  664. // Allocate a new interface
  665. //
  666. Interfacep = reinterpret_cast<PDNS_INTERFACE>(
  667. NH_ALLOCATE(sizeof(DNS_INTERFACE))
  668. );
  669. if (!Interfacep) {
  670. LeaveCriticalSection(&DnsInterfaceLock);
  671. NhTrace(
  672. TRACE_FLAG_IF, "DnsCreateInterface: error allocating interface"
  673. );
  674. NhErrorLog(
  675. IP_DNS_PROXY_LOG_ALLOCATION_FAILED,
  676. 0,
  677. "%d",
  678. sizeof(DNS_INTERFACE)
  679. );
  680. return ERROR_NOT_ENOUGH_MEMORY;
  681. }
  682. //
  683. // Initialize the new interface
  684. //
  685. ZeroMemory(Interfacep, sizeof(*Interfacep));
  686. __try {
  687. InitializeCriticalSection(&Interfacep->Lock);
  688. }
  689. __except(EXCEPTION_EXECUTE_HANDLER) {
  690. LeaveCriticalSection(&DnsInterfaceLock);
  691. NH_FREE(Interfacep);
  692. return GetExceptionCode();
  693. }
  694. Interfacep->Index = Index;
  695. Interfacep->Type = Type;
  696. if (InterfaceInfo) {
  697. Interfacep->Flags = InterfaceInfo->Flags|DNS_INTERFACE_FLAG_CONFIGURED;
  698. CopyMemory(&Interfacep->Info, InterfaceInfo, sizeof(*InterfaceInfo));
  699. }
  700. Interfacep->ReferenceCount = 1;
  701. InitializeListHead(&Interfacep->QueryList);
  702. InsertTailList(InsertionPoint, &Interfacep->Link);
  703. LeaveCriticalSection(&DnsInterfaceLock);
  704. if (InterfaceCreated) { *InterfaceCreated = Interfacep; }
  705. return NO_ERROR;
  706. } // DnsCreateInterface
  707. VOID
  708. DnsDeactivateInterface(
  709. PDNS_INTERFACE Interfacep
  710. )
  711. /*++
  712. Routine Description:
  713. This routine is called to deactivate an interface.
  714. It closes all sockets on the interface's bindings (if any).
  715. Arguments:
  716. Interfacep - the interface to be deactivated
  717. Return Value:
  718. none.
  719. Environment:
  720. Always invoked locally, with 'Interfacep' referenced by caller and/or
  721. 'DnsInterfaceLock' held by caller.
  722. --*/
  723. {
  724. ULONG i;
  725. PLIST_ENTRY Link;
  726. PDNS_QUERY Queryp;
  727. PROFILE("DnsDeactivateInterface");
  728. ACQUIRE_LOCK(Interfacep);
  729. //
  730. // Stop all network I/O on the interface's logical networks
  731. //
  732. for (i = 0; i < Interfacep->BindingCount; i++) {
  733. NhDeleteDatagramSocket(
  734. Interfacep->BindingArray[i].Socket[DnsProxyDns]
  735. );
  736. Interfacep->BindingArray[i].Socket[DnsProxyDns] = INVALID_SOCKET;
  737. NhDeleteDatagramSocket(
  738. Interfacep->BindingArray[i].Socket[DnsProxyWins]
  739. );
  740. Interfacep->BindingArray[i].Socket[DnsProxyWins] = INVALID_SOCKET;
  741. }
  742. //
  743. // Eliminate all pending queries
  744. //
  745. while (!IsListEmpty(&Interfacep->QueryList)) {
  746. Link = RemoveHeadList(&Interfacep->QueryList);
  747. Queryp = CONTAINING_RECORD(Link, DNS_QUERY, Link);
  748. NH_FREE(Queryp);
  749. }
  750. RELEASE_LOCK(Interfacep);
  751. } // DnsDeactivateInterface
  752. VOID NTAPI
  753. DnspDeferReadCallbackRoutine(
  754. PVOID Context,
  755. BOOLEAN TimedOut
  756. )
  757. /*++
  758. Routine Description:
  759. This routine is invoked to re-issue a deferred read when the countdown
  760. for the deferral completes.
  761. Arguments:
  762. Context - holds information identifying the interface and socket
  763. TimedOut - indicates whether the countdown completed
  764. Return Value:
  765. none.
  766. Environment:
  767. Invoked with an outstanding reference to the component on our behalf.
  768. --*/
  769. {
  770. PDNS_DEFER_READ_CONTEXT Contextp;
  771. ULONG Error;
  772. ULONG i;
  773. PDNS_INTERFACE Interfacep;
  774. NTSTATUS status;
  775. DNS_PROXY_TYPE Type;
  776. PROFILE("DnspDeferReadCallbackRoutine");
  777. Contextp = (PDNS_DEFER_READ_CONTEXT)Context;
  778. //
  779. // Find the interface on which the read was deferred
  780. //
  781. EnterCriticalSection(&DnsInterfaceLock);
  782. Interfacep = DnsLookupInterface(Contextp->Index, NULL);
  783. if (!Interfacep ||
  784. !DNS_INTERFACE_ACTIVE(Interfacep) ||
  785. !DNS_REFERENCE_INTERFACE(Interfacep)) {
  786. LeaveCriticalSection(&DnsInterfaceLock);
  787. NH_FREE(Contextp);
  788. DEREFERENCE_DNS();
  789. return;
  790. }
  791. LeaveCriticalSection(&DnsInterfaceLock);
  792. ACQUIRE_LOCK(Interfacep);
  793. //
  794. // Search for the socket on which to reissue the read
  795. //
  796. for (i = 0; i < Interfacep->BindingCount; i++) {
  797. if (Interfacep->BindingArray[i].Socket[Type = DnsProxyDns]
  798. != Contextp->Socket &&
  799. Interfacep->BindingArray[i].Socket[Type = DnsProxyWins]
  800. != Contextp->Socket) {
  801. continue;
  802. }
  803. //
  804. // This is the binding on which to reissue the read.
  805. // If no pending timer is recorded, assume a rebind occurred, and quit.
  806. //
  807. if (!Interfacep->BindingArray[i].TimerPending[Type]) { break; }
  808. Interfacep->BindingArray[i].TimerPending[Type] = FALSE;
  809. Error =
  810. NhReadDatagramSocket(
  811. &DnsComponentReference,
  812. Contextp->Socket,
  813. NULL,
  814. DnsReadCompletionRoutine,
  815. Interfacep,
  816. UlongToPtr(Interfacep->BindingArray[i].Mask)
  817. );
  818. RELEASE_LOCK(Interfacep);
  819. if (!Error) {
  820. NH_FREE(Contextp);
  821. DEREFERENCE_DNS();
  822. return;
  823. }
  824. //
  825. // An error occurred; we'll have to retry later.
  826. // we queue a work item which sets the timer.
  827. //
  828. NhTrace(
  829. TRACE_FLAG_DNS,
  830. "DnspDeferReadCallbackRoutine: error %d reading interface %d",
  831. Error,
  832. Interfacep->Index
  833. );
  834. //
  835. // Reference the component on behalf of the work-item
  836. //
  837. if (REFERENCE_DNS()) {
  838. //
  839. // Queue a work-item, reusing the deferral context
  840. //
  841. status =
  842. RtlQueueWorkItem(
  843. DnspDeferReadWorkerRoutine,
  844. Contextp,
  845. WT_EXECUTEINIOTHREAD
  846. );
  847. if (NT_SUCCESS(status)) {
  848. Contextp = NULL;
  849. }
  850. else {
  851. NH_FREE(Contextp);
  852. NhTrace(
  853. TRACE_FLAG_DNS,
  854. "DnspDeferReadCallbackRoutine: error %d deferring %d",
  855. Error,
  856. Interfacep->Index
  857. );
  858. DEREFERENCE_DNS();
  859. }
  860. }
  861. DNS_DEREFERENCE_INTERFACE(Interfacep);
  862. DEREFERENCE_DNS();
  863. return;
  864. }
  865. //
  866. // The interface was not found; never mind.
  867. //
  868. RELEASE_LOCK(Interfacep);
  869. DNS_DEREFERENCE_INTERFACE(Interfacep);
  870. NH_FREE(Contextp);
  871. DEREFERENCE_DNS();
  872. } // DnspDeferReadCallbackRoutine
  873. VOID
  874. DnsDeferReadInterface(
  875. PDNS_INTERFACE Interfacep,
  876. SOCKET Socket
  877. )
  878. /*++
  879. Routine Description:
  880. This routine is invoked to defer a read-request on an interface,
  881. typically if an attempt to post a read failed.
  882. Arguments:
  883. Interfacep - the interface on which to defer the request
  884. Socket - the socket on which to defer the request
  885. Return Value:
  886. none.
  887. Environment:
  888. Invoked with 'Interfacep' referenced and locked by the caller.
  889. The caller may release the reference upon return.
  890. --*/
  891. {
  892. PDNS_DEFER_READ_CONTEXT Contextp;
  893. ULONG i;
  894. NTSTATUS status;
  895. DNS_PROXY_TYPE Type;
  896. PROFILE("DnsDeferReadInterface");
  897. //
  898. // Find the binding for the given socket.
  899. //
  900. status = STATUS_SUCCESS;
  901. for (i = 0; i < Interfacep->BindingCount; i++) {
  902. if (Interfacep->BindingArray[i].Socket[Type = DnsProxyDns] != Socket &&
  903. Interfacep->BindingArray[i].Socket[Type = DnsProxyWins] != Socket) {
  904. continue;
  905. }
  906. //
  907. // This is the binding. If there is already a timer for it,
  908. // then just return silently.
  909. //
  910. if (Interfacep->BindingArray[i].TimerPending[Type]) {
  911. status = STATUS_UNSUCCESSFUL;
  912. break;
  913. }
  914. //
  915. // Allocate a context block for the deferral.
  916. //
  917. Contextp =
  918. (PDNS_DEFER_READ_CONTEXT)
  919. NH_ALLOCATE(sizeof(DNS_DEFER_READ_CONTEXT));
  920. if (!Contextp) {
  921. NhTrace(
  922. TRACE_FLAG_DNS,
  923. "DnsDeferReadInterface: cannot allocate deferral context"
  924. );
  925. status = STATUS_NO_MEMORY;
  926. break;
  927. }
  928. Contextp->Index = Interfacep->Index;
  929. Contextp->Socket = Socket;
  930. Contextp->DeferralCount = 1;
  931. //
  932. // Install a timer to re-issue the read request
  933. //
  934. status =
  935. NhSetTimer(
  936. &DnsComponentReference,
  937. NULL,
  938. DnspDeferReadCallbackRoutine,
  939. Contextp,
  940. DNS_DEFER_READ_INITIAL_TIMEOUT
  941. );
  942. if (NT_SUCCESS(status)) {
  943. Interfacep->BindingArray[i].TimerPending[Type] = TRUE;
  944. }
  945. else {
  946. NH_FREE(Contextp);
  947. NhTrace(
  948. TRACE_FLAG_DNS,
  949. "DnsDeferReadInterface: status %08x setting deferral timer",
  950. status
  951. );
  952. }
  953. break;
  954. }
  955. if (i >= Interfacep->BindingCount) { status = STATUS_UNSUCCESSFUL; }
  956. } // DnsDeferReadInterface
  957. VOID APIENTRY
  958. DnspDeferReadWorkerRoutine(
  959. PVOID Context
  960. )
  961. /*++
  962. Routine Description:
  963. This routine is invoked to set a timer for reissuing a deferred read.
  964. Arguments:
  965. Context - contains the context for the timer.
  966. Return Value:
  967. none.
  968. Environment:
  969. Invoked with an outstanding reference to the module made on our behalf.
  970. --*/
  971. {
  972. PDNS_DEFER_READ_CONTEXT Contextp;
  973. ULONG i;
  974. PDNS_INTERFACE Interfacep;
  975. NTSTATUS status;
  976. DNS_PROXY_TYPE Type;
  977. PROFILE("DnspDeferReadWorkerRoutine");
  978. Contextp = (PDNS_DEFER_READ_CONTEXT)Context;
  979. ++Contextp->DeferralCount;
  980. //
  981. // Find the interface on which the read was deferred
  982. //
  983. EnterCriticalSection(&DnsInterfaceLock);
  984. Interfacep = DnsLookupInterface(Contextp->Index, NULL);
  985. if (!Interfacep ||
  986. !DNS_INTERFACE_ACTIVE(Interfacep) ||
  987. !DNS_REFERENCE_INTERFACE(Interfacep)) {
  988. LeaveCriticalSection(&DnsInterfaceLock);
  989. NH_FREE(Contextp);
  990. DEREFERENCE_DNS();
  991. return;
  992. }
  993. LeaveCriticalSection(&DnsInterfaceLock);
  994. ACQUIRE_LOCK(Interfacep);
  995. //
  996. // Search for the binding on which to set the timer
  997. //
  998. for (i = 0; i < Interfacep->BindingCount; i++) {
  999. if (Interfacep->BindingArray[i].Socket[Type = DnsProxyDns]
  1000. != Contextp->Socket &&
  1001. Interfacep->BindingArray[i].Socket[Type = DnsProxyWins]
  1002. != Contextp->Socket) {
  1003. continue;
  1004. }
  1005. //
  1006. // This is the binding on which to reissue the read.
  1007. // If a timer is already pending, assume a rebind occurred, and quit.
  1008. //
  1009. if (Interfacep->BindingArray[i].TimerPending[Type]) { break; }
  1010. //
  1011. // Install a timer to re-issue the read request,
  1012. // reusing the deferral context.
  1013. //
  1014. status =
  1015. NhSetTimer(
  1016. &DnsComponentReference,
  1017. NULL,
  1018. DnspDeferReadCallbackRoutine,
  1019. Contextp,
  1020. DNS_DEFER_READ_TIMEOUT
  1021. );
  1022. if (NT_SUCCESS(status)) {
  1023. Contextp = NULL;
  1024. Interfacep->BindingArray[i].TimerPending[Type] = TRUE;
  1025. }
  1026. else {
  1027. NhTrace(
  1028. TRACE_FLAG_DNS,
  1029. "DnspDeferReadWorkerRoutine: status %08x setting timer",
  1030. status
  1031. );
  1032. }
  1033. }
  1034. RELEASE_LOCK(Interfacep);
  1035. DNS_DEREFERENCE_INTERFACE(Interfacep);
  1036. if (Contextp) { NH_FREE(Contextp); }
  1037. DEREFERENCE_DNS();
  1038. } // DnspDeferReadWorkerRoutine
  1039. ULONG
  1040. DnsDeleteInterface(
  1041. ULONG Index
  1042. )
  1043. /*++
  1044. Routine Description:
  1045. This routine is called to delete an interface.
  1046. It drops the reference count on the interface so that the last
  1047. dereferencer will delete the interface, and sets the 'deleted' flag
  1048. so that further references to the interface will fail.
  1049. Arguments:
  1050. Index - the index of the interface to be deleted
  1051. Return Value:
  1052. ULONG - Win32 status code.
  1053. Environment:
  1054. Invoked internally in the context of an IP router-manager thread.
  1055. (See 'RMDNS.C').
  1056. --*/
  1057. {
  1058. PDNS_INTERFACE Interfacep;
  1059. PROFILE("DnsDeleteInterface");
  1060. //
  1061. // Retrieve the interface to be deleted
  1062. //
  1063. EnterCriticalSection(&DnsInterfaceLock);
  1064. if (!(Interfacep = DnsLookupInterface(Index, NULL))) {
  1065. LeaveCriticalSection(&DnsInterfaceLock);
  1066. NhTrace(
  1067. TRACE_FLAG_IF,
  1068. "DnsDeleteInterface: interface %d not found",
  1069. Index
  1070. );
  1071. return ERROR_NO_SUCH_INTERFACE;
  1072. }
  1073. //
  1074. // Deactivate the interface
  1075. //
  1076. DnsDeactivateInterface(Interfacep);
  1077. //
  1078. // Mark the interface as deleted and take it off the interface list
  1079. //
  1080. Interfacep->Flags |= DNS_INTERFACE_FLAG_DELETED;
  1081. Interfacep->Flags &= ~DNS_INTERFACE_FLAG_ENABLED;
  1082. RemoveEntryList(&Interfacep->Link);
  1083. //
  1084. // Drop the reference count; if it is non-zero,
  1085. // the deletion will complete later.
  1086. //
  1087. if (--Interfacep->ReferenceCount) {
  1088. LeaveCriticalSection(&DnsInterfaceLock);
  1089. NhTrace(
  1090. TRACE_FLAG_IF,
  1091. "DnsDeleteInterface: interface %d deletion pending",
  1092. Index
  1093. );
  1094. return NO_ERROR;
  1095. }
  1096. //
  1097. // The reference count is zero, so perform final cleanup
  1098. //
  1099. DnsCleanupInterface(Interfacep);
  1100. LeaveCriticalSection(&DnsInterfaceLock);
  1101. return NO_ERROR;
  1102. } // DnsDeleteInterface
  1103. ULONG
  1104. DnsDisableInterface(
  1105. ULONG Index
  1106. )
  1107. /*++
  1108. Routine Description:
  1109. This routine is called to disable I/O on an interface.
  1110. If the interface is active, it is deactivated.
  1111. Arguments:
  1112. Index - the index of the interface to be disabled.
  1113. Return Value:
  1114. none.
  1115. Environment:
  1116. Invoked internally in the context of an IP router-manager thread.
  1117. (See 'RMDNS.C').
  1118. --*/
  1119. {
  1120. PDNS_INTERFACE Interfacep;
  1121. PROFILE("DnsDisableInterface");
  1122. //
  1123. // Retrieve the interface to be disabled
  1124. //
  1125. EnterCriticalSection(&DnsInterfaceLock);
  1126. if (!(Interfacep = DnsLookupInterface(Index, NULL))) {
  1127. LeaveCriticalSection(&DnsInterfaceLock);
  1128. NhTrace(
  1129. TRACE_FLAG_IF,
  1130. "DnsDisableInterface: interface %d not found",
  1131. Index
  1132. );
  1133. return ERROR_NO_SUCH_INTERFACE;
  1134. }
  1135. //
  1136. // Make sure the interface is not already disabled
  1137. //
  1138. if (!DNS_INTERFACE_ENABLED(Interfacep)) {
  1139. LeaveCriticalSection(&DnsInterfaceLock);
  1140. NhTrace(
  1141. TRACE_FLAG_IF,
  1142. "DnsDisableInterface: interface %d already disabled",
  1143. Index
  1144. );
  1145. return ERROR_INTERFACE_DISABLED;
  1146. }
  1147. //
  1148. // Reference the interface
  1149. //
  1150. if (!DNS_REFERENCE_INTERFACE(Interfacep)) {
  1151. LeaveCriticalSection(&DnsInterfaceLock);
  1152. NhTrace(
  1153. TRACE_FLAG_IF,
  1154. "DnsDisableInterface: interface %d cannot be referenced",
  1155. Index
  1156. );
  1157. return ERROR_INTERFACE_DISABLED;
  1158. }
  1159. //
  1160. // Clear the 'enabled' flag
  1161. //
  1162. Interfacep->Flags &= ~DNS_INTERFACE_FLAG_ENABLED;
  1163. //
  1164. // Deactivate the interface, if necessary
  1165. //
  1166. if (DNS_INTERFACE_BOUND(Interfacep)) {
  1167. DnsDeactivateInterface(Interfacep);
  1168. }
  1169. LeaveCriticalSection(&DnsInterfaceLock);
  1170. DNS_DEREFERENCE_INTERFACE(Interfacep);
  1171. return NO_ERROR;
  1172. } // DnsDisableInterface
  1173. ULONG
  1174. DnsEnableInterface(
  1175. ULONG Index
  1176. )
  1177. /*++
  1178. Routine Description:
  1179. This routine is called to enable I/O on an interface.
  1180. If the interface is already bound, this enabling activates it.
  1181. Arguments:
  1182. Index - the index of the interfaec to be enabled
  1183. Return Value:
  1184. ULONG - Win32 status code.
  1185. Environment:
  1186. Invoked internally in the context of an IP router-manager thread.
  1187. (See 'RMDNS.C').
  1188. --*/
  1189. {
  1190. ULONG Error = NO_ERROR;
  1191. PDNS_INTERFACE Interfacep;
  1192. PROFILE("DnsEnableInterface");
  1193. //
  1194. // Retrieve the interface to be enabled
  1195. //
  1196. EnterCriticalSection(&DnsInterfaceLock);
  1197. if (!(Interfacep = DnsLookupInterface(Index, NULL))) {
  1198. LeaveCriticalSection(&DnsInterfaceLock);
  1199. NhTrace(
  1200. TRACE_FLAG_IF,
  1201. "DnsEnableInterface: interface %d not found",
  1202. Index
  1203. );
  1204. return ERROR_NO_SUCH_INTERFACE;
  1205. }
  1206. //
  1207. // Make sure the interface is not already enabled
  1208. //
  1209. if (DNS_INTERFACE_ENABLED(Interfacep)) {
  1210. LeaveCriticalSection(&DnsInterfaceLock);
  1211. NhTrace(
  1212. TRACE_FLAG_IF,
  1213. "DnsEnableInterface: interface %d already enabled",
  1214. Index
  1215. );
  1216. return ERROR_INTERFACE_ALREADY_EXISTS;
  1217. }
  1218. //
  1219. // Reference the interface
  1220. //
  1221. if (!DNS_REFERENCE_INTERFACE(Interfacep)) {
  1222. LeaveCriticalSection(&DnsInterfaceLock);
  1223. NhTrace(
  1224. TRACE_FLAG_IF,
  1225. "DnsEnableInterface: interface %d cannot be referenced",
  1226. Index
  1227. );
  1228. return ERROR_INTERFACE_DISABLED;
  1229. }
  1230. //
  1231. // Set the 'enabled' flag
  1232. //
  1233. Interfacep->Flags |= DNS_INTERFACE_FLAG_ENABLED;
  1234. //
  1235. // Activate the interface, if necessary
  1236. //
  1237. if (DNS_INTERFACE_ACTIVE(Interfacep)) {
  1238. Error = DnsActivateInterface(Interfacep);
  1239. }
  1240. LeaveCriticalSection(&DnsInterfaceLock);
  1241. DNS_DEREFERENCE_INTERFACE(Interfacep);
  1242. return Error;
  1243. } // DnsEnableInterface
  1244. ULONG
  1245. DnsInitializeInterfaceManagement(
  1246. VOID
  1247. )
  1248. /*++
  1249. Routine Description:
  1250. This routine is called to initialize the interface-management module.
  1251. Arguments:
  1252. none.
  1253. Return Value:
  1254. ULONG - Win32 status code.
  1255. Environment:
  1256. Invoked internally in the context of an IP router-manager thread.
  1257. (See 'RMDNS.C').
  1258. --*/
  1259. {
  1260. ULONG Error = NO_ERROR;
  1261. PROFILE("DnsInitializeInterfaceManagement");
  1262. InitializeListHead(&DnsInterfaceList);
  1263. __try {
  1264. InitializeCriticalSection(&DnsInterfaceLock);
  1265. }
  1266. __except(EXCEPTION_EXECUTE_HANDLER) {
  1267. NhTrace(
  1268. TRACE_FLAG_IF,
  1269. "DnsInitializeInterfaceManagement: exception %d creating lock",
  1270. Error = GetExceptionCode()
  1271. );
  1272. }
  1273. DnspLastConnectAttemptTickCount = NtGetTickCount();
  1274. return Error;
  1275. } // DnsInitializeInterfaceManagement
  1276. PDNS_INTERFACE
  1277. DnsLookupInterface(
  1278. ULONG Index,
  1279. OUT PLIST_ENTRY* InsertionPoint OPTIONAL
  1280. )
  1281. /*++
  1282. Routine Description:
  1283. This routine is called to retrieve an interface given its index.
  1284. Arguments:
  1285. Index - the index of the interface to be retrieved
  1286. InsertionPoint - if the interface is not found, optionally receives
  1287. the point where the interface would be inserted in the interface list
  1288. Return Value:
  1289. PDNS_INTERFACE - the interface, if found; otherwise, NULL.
  1290. Environment:
  1291. Invoked internally from an arbitrary context, with 'DnsInterfaceLock'
  1292. held by caller.
  1293. --*/
  1294. {
  1295. PDNS_INTERFACE Interfacep;
  1296. PLIST_ENTRY Link;
  1297. PROFILE("DnsLookupInterface");
  1298. for (Link = DnsInterfaceList.Flink;
  1299. Link != &DnsInterfaceList;
  1300. Link = Link->Flink
  1301. ) {
  1302. Interfacep = CONTAINING_RECORD(Link, DNS_INTERFACE, Link);
  1303. if (Index > Interfacep->Index) { continue; }
  1304. else
  1305. if (Index < Interfacep->Index) { break; }
  1306. return Interfacep;
  1307. }
  1308. if (InsertionPoint) { *InsertionPoint = Link; }
  1309. return NULL;
  1310. } // DnsLookupInterface
  1311. ULONG
  1312. DnsQueryInterface(
  1313. ULONG Index,
  1314. PVOID InterfaceInfo,
  1315. PULONG InterfaceInfoSize
  1316. )
  1317. /*++
  1318. Routine Description:
  1319. This routine is invoked to retrieve the configuration for an interface.
  1320. Arguments:
  1321. Index - the interface to be queried
  1322. InterfaceInfo - receives the retrieved information
  1323. InterfaceInfoSize - receives the (required) size of the information
  1324. Return Value:
  1325. ULONG - Win32 status code.
  1326. --*/
  1327. {
  1328. PDNS_INTERFACE Interfacep;
  1329. PROFILE("DnsQueryInterface");
  1330. //
  1331. // Check the caller's buffer size
  1332. //
  1333. if (!InterfaceInfoSize) { return ERROR_INVALID_PARAMETER; }
  1334. //
  1335. // Retrieve the interface to be configured
  1336. //
  1337. EnterCriticalSection(&DnsInterfaceLock);
  1338. if (!(Interfacep = DnsLookupInterface(Index, NULL))) {
  1339. LeaveCriticalSection(&DnsInterfaceLock);
  1340. NhTrace(
  1341. TRACE_FLAG_IF,
  1342. "DnsQueryInterface: interface %d not found",
  1343. Index
  1344. );
  1345. return ERROR_NO_SUCH_INTERFACE;
  1346. }
  1347. //
  1348. // Reference the interface
  1349. //
  1350. if (!DNS_REFERENCE_INTERFACE(Interfacep)) {
  1351. LeaveCriticalSection(&DnsInterfaceLock);
  1352. NhTrace(
  1353. TRACE_FLAG_IF,
  1354. "DnsQueryInterface: interface %d cannot be referenced",
  1355. Index
  1356. );
  1357. return ERROR_INTERFACE_DISABLED;
  1358. }
  1359. //
  1360. // See if there is any explicit config on this interface
  1361. //
  1362. if (!DNS_INTERFACE_CONFIGURED(Interfacep)) {
  1363. LeaveCriticalSection(&DnsInterfaceLock);
  1364. DNS_DEREFERENCE_INTERFACE(Interfacep);
  1365. NhTrace(
  1366. TRACE_FLAG_IF,
  1367. "DnsQueryInterface: interface %d has no configuration",
  1368. Index
  1369. );
  1370. *InterfaceInfoSize = 0;
  1371. return NO_ERROR;
  1372. }
  1373. //
  1374. // See if there is enough buffer space
  1375. //
  1376. if (*InterfaceInfoSize < sizeof(IP_DNS_PROXY_INTERFACE_INFO)) {
  1377. LeaveCriticalSection(&DnsInterfaceLock);
  1378. DNS_DEREFERENCE_INTERFACE(Interfacep);
  1379. *InterfaceInfoSize = sizeof(IP_DNS_PROXY_INTERFACE_INFO);
  1380. return ERROR_INSUFFICIENT_BUFFER;
  1381. }
  1382. //
  1383. // Copy the requested data
  1384. //
  1385. CopyMemory(
  1386. InterfaceInfo,
  1387. &Interfacep->Info,
  1388. sizeof(IP_DNS_PROXY_INTERFACE_INFO)
  1389. );
  1390. *InterfaceInfoSize = sizeof(IP_DNS_PROXY_INTERFACE_INFO);
  1391. LeaveCriticalSection(&DnsInterfaceLock);
  1392. DNS_DEREFERENCE_INTERFACE(Interfacep);
  1393. return NO_ERROR;
  1394. } // DnsQueryInterface
  1395. VOID
  1396. DnsReactivateEveryInterface(
  1397. VOID
  1398. )
  1399. /*++
  1400. Routine Description:
  1401. This routine is called to reactivate all activate interfaces
  1402. when a change occurs to the global DNS or WINS proxy setting.
  1403. Thus if, for instance, WINS proxy is disabled, during deactivation
  1404. all such sockets are closed, and during reactivation they are
  1405. not reopened.
  1406. Arguments:
  1407. none.
  1408. Return Value:
  1409. none.
  1410. Environment:
  1411. Invoked from a router-manager thread with no locks held.
  1412. --*/
  1413. {
  1414. PDNS_INTERFACE Interfacep;
  1415. PLIST_ENTRY Link;
  1416. PROFILE("DnsReactivateEveryInterface");
  1417. EnterCriticalSection(&DnsInterfaceLock);
  1418. for (Link = DnsInterfaceList.Flink;
  1419. Link != &DnsInterfaceList;
  1420. Link = Link->Flink
  1421. ) {
  1422. Interfacep = CONTAINING_RECORD(Link, DNS_INTERFACE, Link);
  1423. if (!DNS_REFERENCE_INTERFACE(Interfacep)) { continue; }
  1424. if (DNS_INTERFACE_ACTIVE(Interfacep)) {
  1425. DnsDeactivateInterface(Interfacep);
  1426. DnsActivateInterface(Interfacep);
  1427. }
  1428. DNS_DEREFERENCE_INTERFACE(Interfacep);
  1429. }
  1430. LeaveCriticalSection(&DnsInterfaceLock);
  1431. } // DnsReactivateEveryInterface
  1432. ULONG NTAPI
  1433. DnspSaveFileWorkerRoutine(
  1434. PVOID Context
  1435. )
  1436. {
  1437. //
  1438. // Context unused
  1439. //
  1440. PROFILE("DnspSaveFileWorkerRoutine");
  1441. SaveHostsIcsFile(FALSE);
  1442. DEREFERENCE_DNS();
  1443. return NO_ERROR;
  1444. } // DnspSaveFileWorkerRoutine
  1445. VOID
  1446. DnsShutdownInterfaceManagement(
  1447. VOID
  1448. )
  1449. /*++
  1450. Routine Description:
  1451. This routine is called to shutdown the interface-management module.
  1452. Arguments:
  1453. none.
  1454. Return Value:
  1455. none.
  1456. Environment:
  1457. Invoked in an arbitrary thread context, after all references
  1458. to all interfaces have been released.
  1459. --*/
  1460. {
  1461. PDNS_INTERFACE Interfacep;
  1462. PLIST_ENTRY Link;
  1463. PROFILE("DnsShutdownInterfaceManagement");
  1464. while (!IsListEmpty(&DnsInterfaceList)) {
  1465. Link = RemoveHeadList(&DnsInterfaceList);
  1466. Interfacep = CONTAINING_RECORD(Link, DNS_INTERFACE, Link);
  1467. if (DNS_INTERFACE_ACTIVE(Interfacep)) {
  1468. DnsDeactivateInterface(Interfacep);
  1469. }
  1470. DnsCleanupInterface(Interfacep);
  1471. }
  1472. DeleteCriticalSection(&DnsInterfaceLock);
  1473. } // DnsShutdownInterfaceManagement
  1474. VOID
  1475. DnsSignalNatInterface(
  1476. ULONG Index,
  1477. BOOLEAN Boundary
  1478. )
  1479. /*++
  1480. Routine Description:
  1481. This routine is invoked upon reconfiguration of a NAT interface.
  1482. Note that this routine may be invoked even when the DNS proxy
  1483. is neither installed nor running; it operates as expected,
  1484. since the interface list and lock are always initialized.
  1485. Upon invocation, the routine activates or deactivates the interface
  1486. depending on whether the NAT is not or is running on the interface,
  1487. respectively.
  1488. Arguments:
  1489. Index - the reconfigured interface
  1490. Boundary - indicates whether the interface is now a boundary interface
  1491. Return Value:
  1492. none.
  1493. Environment:
  1494. Invoked from an arbitrary context.
  1495. --*/
  1496. {
  1497. PDNS_INTERFACE Interfacep;
  1498. PROFILE("DnsSignalNatInterface");
  1499. EnterCriticalSection(&DnsGlobalInfoLock);
  1500. if (!DnsGlobalInfo) {
  1501. LeaveCriticalSection(&DnsGlobalInfoLock);
  1502. return;
  1503. }
  1504. LeaveCriticalSection(&DnsGlobalInfoLock);
  1505. EnterCriticalSection(&DnsInterfaceLock);
  1506. if (!(Interfacep = DnsLookupInterface(Index, NULL))) {
  1507. LeaveCriticalSection(&DnsInterfaceLock);
  1508. return;
  1509. }
  1510. DnsDeactivateInterface(Interfacep);
  1511. if (DNS_INTERFACE_ACTIVE(Interfacep)) {
  1512. DnsActivateInterface(Interfacep);
  1513. }
  1514. LeaveCriticalSection(&DnsInterfaceLock);
  1515. } // DnsSignalNatInterface
  1516. ULONG
  1517. DnsUnbindInterface(
  1518. ULONG Index
  1519. )
  1520. /*++
  1521. Routine Description:
  1522. This routine is invoked to revoke the binding on an interface.
  1523. This involves deactivating the interface if it is active.
  1524. Arguments:
  1525. Index - the index of the interface to be unbound
  1526. Return Value:
  1527. none.
  1528. Environment:
  1529. Invoked internally in the context of an IP router-manager thread.
  1530. (See 'RMDNS.C').
  1531. --*/
  1532. {
  1533. PDNS_INTERFACE Interfacep;
  1534. PROFILE("DnsUnbindInterface");
  1535. //
  1536. // Retrieve the interface to be unbound
  1537. //
  1538. EnterCriticalSection(&DnsInterfaceLock);
  1539. if (!(Interfacep = DnsLookupInterface(Index, NULL))) {
  1540. LeaveCriticalSection(&DnsInterfaceLock);
  1541. NhTrace(
  1542. TRACE_FLAG_IF,
  1543. "DnsUnbindInterface: interface %d not found",
  1544. Index
  1545. );
  1546. return ERROR_NO_SUCH_INTERFACE;
  1547. }
  1548. //
  1549. // Make sure the interface is not already unbound
  1550. //
  1551. if (!DNS_INTERFACE_BOUND(Interfacep)) {
  1552. LeaveCriticalSection(&DnsInterfaceLock);
  1553. NhTrace(
  1554. TRACE_FLAG_IF,
  1555. "DnsUnbindInterface: interface %d already unbound",
  1556. Index
  1557. );
  1558. return ERROR_ADDRESS_NOT_ASSOCIATED;
  1559. }
  1560. //
  1561. // Reference the interface
  1562. //
  1563. if (!DNS_REFERENCE_INTERFACE(Interfacep)) {
  1564. LeaveCriticalSection(&DnsInterfaceLock);
  1565. NhTrace(
  1566. TRACE_FLAG_IF,
  1567. "DnsUnbindInterface: interface %d cannot be referenced",
  1568. Index
  1569. );
  1570. return ERROR_INTERFACE_DISABLED;
  1571. }
  1572. //
  1573. // Clear the 'bound' flag
  1574. //
  1575. Interfacep->Flags &= ~DNS_INTERFACE_FLAG_BOUND;
  1576. //
  1577. // Deactivate the interface, if necessary
  1578. //
  1579. if (DNS_INTERFACE_ENABLED(Interfacep)) {
  1580. DnsDeactivateInterface(Interfacep);
  1581. }
  1582. LeaveCriticalSection(&DnsInterfaceLock);
  1583. //
  1584. // Destroy the interface's binding
  1585. //
  1586. ACQUIRE_LOCK(Interfacep);
  1587. NH_FREE(Interfacep->BindingArray);
  1588. Interfacep->BindingArray = NULL;
  1589. Interfacep->BindingCount = 0;
  1590. RELEASE_LOCK(Interfacep);
  1591. DNS_DEREFERENCE_INTERFACE(Interfacep);
  1592. return NO_ERROR;
  1593. } // DnsUnbindInterface
  1594. ULONG
  1595. DnsGetPrivateInterfaceAddress(
  1596. VOID
  1597. )
  1598. /*++
  1599. Routine Description:
  1600. This routine is invoked to return the IP address on which DNS
  1601. has been enabled.
  1602. Arguments:
  1603. none.
  1604. Return Value:
  1605. Bound IP address if an address is found (else 0).
  1606. Environment:
  1607. Invoked from an arbitrary context.
  1608. --*/
  1609. {
  1610. PROFILE("DnsGetPrivateInterfaceAddress");
  1611. ULONG ipAddr = 0;
  1612. ULONG ulRet = NO_ERROR;
  1613. //
  1614. // Find out the first available interface on which we are enabled and
  1615. // return the primary IP address to which we are bound.
  1616. //
  1617. PDNS_INTERFACE Interfacep = NULL;
  1618. PLIST_ENTRY Link;
  1619. ULONG i;
  1620. EnterCriticalSection(&DnsInterfaceLock);
  1621. for (Link = DnsInterfaceList.Flink;
  1622. Link != &DnsInterfaceList;
  1623. Link = Link->Flink
  1624. )
  1625. {
  1626. Interfacep = CONTAINING_RECORD(Link, DNS_INTERFACE, Link);
  1627. ACQUIRE_LOCK(Interfacep);
  1628. for (i = 0; i < Interfacep->BindingCount; i++)
  1629. {
  1630. NhTrace(
  1631. TRACE_FLAG_DNS,
  1632. "DnsGetPrivateInterfaceAddress: IP address %s (Index %d)",
  1633. INET_NTOA(Interfacep->BindingArray[i].Address),
  1634. Interfacep->Index
  1635. );
  1636. if (Interfacep->BindingArray[i].Address &
  1637. Interfacep->BindingArray[i].Mask)
  1638. {
  1639. ipAddr = Interfacep->BindingArray[i].Address;
  1640. break;
  1641. }
  1642. }
  1643. RELEASE_LOCK(Interfacep);
  1644. if (ipAddr)
  1645. {
  1646. LeaveCriticalSection(&DnsInterfaceLock);
  1647. NhTrace(
  1648. TRACE_FLAG_DNS,
  1649. "DnsGetPrivateInterfaceAddress: Dns private interface IP address %s (Index %d)",
  1650. INET_NTOA(ipAddr),
  1651. Interfacep->Index
  1652. );
  1653. return ipAddr;
  1654. }
  1655. }
  1656. LeaveCriticalSection(&DnsInterfaceLock);
  1657. return ipAddr;
  1658. } // DnsGetPrivateInterfaceAddress