From a2148df99ddcd122247f95c4cbcce5c4118581a1 Mon Sep 17 00:00:00 2001
From: Nate Karstens <nate.karstens@garmin.com>
Date: Wed, 28 Jun 2017 17:30:00 -0500
Subject: [PATCH 02/11] Create subroutine for tearing down an interface

Creates a subroutine for tearing down an interface.

Upstream-Status: Submitted [dts@apple.com]

Signed-off-by: Nate Karstens <nate.karstens@garmin.com>
---
 mDNSPosix/mDNSPosix.c | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)

diff --git a/mDNSPosix/mDNSPosix.c b/mDNSPosix/mDNSPosix.c
index ffc9696..5e5b2cd 100644
--- a/mDNSPosix/mDNSPosix.c
+++ b/mDNSPosix/mDNSPosix.c
@@ -591,6 +591,19 @@ mDNSlocal void FreePosixNetworkInterface(PosixNetworkInterface *intf)
     gRecentInterfaces = intf;
 }
 
+mDNSlocal void TearDownInterface(mDNS *const m, PosixNetworkInterface *intf)
+{
+    mDNS_DeregisterInterface(m, &intf->coreIntf, NormalActivation);
+    if (gMDNSPlatformPosixVerboseLevel > 0) fprintf(stderr, "Deregistered interface %s\n", intf->intfName);
+    FreePosixNetworkInterface(intf);
+
+    num_registered_interfaces--;
+    if (num_registered_interfaces == 0) {
+        num_pkts_accepted = 0;
+        num_pkts_rejected = 0;
+    }
+}
+
 // Grab the first interface, deregister it, free it, and repeat until done.
 mDNSlocal void ClearInterfaceList(mDNS *const m)
 {
@@ -599,13 +612,10 @@ mDNSlocal void ClearInterfaceList(mDNS *const m)
     while (m->HostInterfaces)
     {
         PosixNetworkInterface *intf = (PosixNetworkInterface*)(m->HostInterfaces);
-        mDNS_DeregisterInterface(m, &intf->coreIntf, NormalActivation);
-        if (gMDNSPlatformPosixVerboseLevel > 0) fprintf(stderr, "Deregistered interface %s\n", intf->intfName);
-        FreePosixNetworkInterface(intf);
+        TearDownInterface(m, intf);
     }
-    num_registered_interfaces = 0;
-    num_pkts_accepted = 0;
-    num_pkts_rejected = 0;
+
+    assert(num_registered_interfaces == 0);
 }
 
 // Sets up a send/receive socket.
-- 
2.17.1

