Snap for 8564071 from 027f535ce1478f028cb991682161012b3ca1a253 to mainline-art-release

Change-Id: I1619630c324cd8d7bda3eb21031c5d4bed16938f
diff --git a/Android.bp b/Android.bp
index 30cba16..ff8eec8 100644
--- a/Android.bp
+++ b/Android.bp
@@ -102,85 +102,31 @@
 cc_binary {
     name: "mdnsd",
     defaults: ["mdnsresponder_default_cflags"],
-    host_supported: true,
     srcs: [
-        "mDNSCore/mDNS.c",
-        "mDNSCore/DNSDigest.c",
-        "mDNSCore/uDNS.c",
         "mDNSCore/DNSCommon.c",
-        "mDNSShared/uds_daemon.c",
-        "mDNSShared/mDNSDebug.c",
+        "mDNSCore/DNSDigest.c",
+        "mDNSCore/mDNS.c",
+        "mDNSCore/uDNS.c",
+        "mDNSPosix/mDNSPosix.c",
+        "mDNSPosix/mDNSUNP.c",
+        "mDNSPosix/PosixDaemon.c",
         "mDNSShared/dnssd_ipc.c",
         "mDNSShared/GenLinkedList.c",
+        "mDNSShared/mDNSDebug.c",
+        "mDNSShared/PlatformCommon.c",
+        "mDNSShared/uds_daemon.c",
     ],
 
     local_include_dirs: [
         "mDNSCore",
+        "mDNSPosix",
         "mDNSShared",
     ],
     init_rc: ["mdnsd.rc"],
 
     cflags: ["-DMDNS_VERSIONSTR_NODTS=1"],
 
-    target: {
-        android: {
-            static_executable: true,
-            local_include_dirs: ["mDNSPosix"],
-            srcs: [
-                "mDNSShared/PlatformCommon.c",
-                "mDNSPosix/PosixDaemon.c",
-                "mDNSPosix/mDNSPosix.c",
-                "mDNSPosix/mDNSUNP.c",
-            ],
-        },
-        not_windows: {
-            local_include_dirs: ["mDNSPosix"],
-            srcs: [
-                "mDNSShared/PlatformCommon.c",
-                "mDNSPosix/PosixDaemon.c",
-                "mDNSPosix/mDNSPosix.c",
-                "mDNSPosix/mDNSUNP.c",
-            ],
-        },
-        windows: {
-            cflags: [
-                "-DTARGET_OS_WIN32",
-                "-D_CONSOLE",
-                "-DPLATFORM_NO_STRSEP",
-                "-DPLATFORM_NO_EPIPE",
-                "-DPLATFORM_NO_RLIMIT",
-                "-DPID_FILE=\"\"",
-                "-DUNICODE",
-                "-D_UNICODE",
-                "-D_LEGACY_NAT_TRAVERSAL",
-                "-Wno-empty-body",
-            ],
-
-            ldflags: ["-municode"],
-            host_ldlibs: [
-                "-lws2_32",
-                "-liphlpapi",
-                "-lpowrprof",
-                "-lnetapi32",
-            ],
-
-            srcs: [
-                "mDNSWindows/SystemService/main.c",
-                "mDNSWindows/SystemService/Service.c",
-                "mDNSWindows/SystemService/EventLog.mc",
-                "mDNSWindows/Secret.c",
-                "mDNSWindows/mDNSWin32.c",
-                "mDNSShared/DebugServices.c",
-                "android/windows_firewall_dummy.c",
-            ],
-
-            local_include_dirs: [
-                "mDNSWindows/SystemService",
-            ],
-        },
-    },
-
-    static_libs: [
+    shared_libs: [
         "libcutils",
         "liblog",
     ],
diff --git a/METADATA b/METADATA
index d97975c..71acc7b 100644
--- a/METADATA
+++ b/METADATA
@@ -1,3 +1,7 @@
 third_party {
   license_type: NOTICE
+  security {
+    # Common Platform Enumeration tags
+    tag: "NVD-CPE2.3:cpe:/a:apple:mdnsresponder:320.10.80"
+  }
 }
diff --git a/OWNERS b/OWNERS
index fe287c2..d95f549 100644
--- a/OWNERS
+++ b/OWNERS
@@ -1,5 +1,4 @@
-# Default code reviewers picked from top 3 or more developers.
-# Please update this list if you find better candidates.
-sadmac@google.com
+set noparent
+file:platform/packages/modules/Connectivity:master:/OWNERS_core_networking
+
 include platform/packages/modules/adb:/OWNERS
-include platform/system/netd:/OWNERS
diff --git a/mDNSCore/mDNS.c b/mDNSCore/mDNS.c
index e126754..257ddfd 100755
--- a/mDNSCore/mDNS.c
+++ b/mDNSCore/mDNS.c
@@ -4436,7 +4436,7 @@
 				}
 			}
 		m->NewLocalRecords = head;
-		// debugf("mDNS_Execute: Setting NewLocalRecords to %s", (head ? ARDisplayString(m, head) : "NULL"));
+		debugf("mDNS_Execute: Setting NewLocalRecords to %s", (head ? ARDisplayString(m, head) : "NULL"));
 
 		if (i >= 1000) LogMsg("mDNS_Execute: m->NewLocalRecords exceeded loop limit");
 
diff --git a/mDNSPosix/mDNSPosix.c b/mDNSPosix/mDNSPosix.c
index 5e47657..8960c93 100644
--- a/mDNSPosix/mDNSPosix.c
+++ b/mDNSPosix/mDNSPosix.c
@@ -505,7 +505,7 @@
 	{
 	char line[256];
 	char nameserver[16];
-	char keyword[10];
+	char keyword[11];
 	int  numOfServers = 0;
 	FILE *fp = fopen(filePath, "r");
 	if (fp == NULL) return -1;
@@ -581,42 +581,88 @@
 	return intf ? intf->index : 0;
 	}
 
-// Frees the specified PosixNetworkInterface structure. The underlying
+// Remove an interface identified by its index from the provided list, and return it.
+// This takes a pointer to a list of interfaces, where NULL represents the empty list.
+mDNSlocal PosixNetworkInterface *RemoveInterfaceFromListByIndex(PosixNetworkInterface **list, int index)
+	{
+	while (list && *list)
+		{
+		PosixNetworkInterface *current = *list;
+		if (current->index == index)
+			{
+			*list = (PosixNetworkInterface*)current->coreIntf.next;
+			current->coreIntf.next = NULL;
+			return current;
+			}
+		list = (PosixNetworkInterface**)&(current->coreIntf.next);
+		}
+	return NULL;
+	}
+
+// Close sockets on the specified PosixNetworkInterface structure. The underlying
 // interface must have already been deregistered with the mDNS core.
-mDNSlocal void FreePosixNetworkInterface(PosixNetworkInterface *intf)
+mDNSlocal void ClosePosixNetworkInterface(PosixNetworkInterface *intf)
 	{
 	assert(intf != NULL);
-	if (intf->intfName != NULL)        free((void *)intf->intfName);
 	if (intf->multicastSocket4 != -1)
 		{
 		int ipv4_closed = close(intf->multicastSocket4);
 		assert(ipv4_closed == 0);
+		intf->multicastSocket4 = -1;
 		}
 #if HAVE_IPV6
 	if (intf->multicastSocket6 != -1)
 		{
 		int ipv6_closed = close(intf->multicastSocket6);
 		assert(ipv6_closed == 0);
+		intf->multicastSocket6 = -1;
 		}
 #endif
+	}
+
+// Free the specified PosixNetworkInterface structure. The underlying
+// interface must have already been deregistered with the mDNS core.
+mDNSlocal void FreePosixNetworkInterface(PosixNetworkInterface *intf)
+	{
+	assert(intf != NULL);
+	assert(intf->coreIntf.next == NULL);
+	ClosePosixNetworkInterface(intf);
+	free((void *)intf->intfName);
 	free(intf);
 	}
 
-// Grab the first interface, deregister it, free it, and repeat until done.
-mDNSlocal void ClearInterfaceList(mDNS *const m)
+// Frees a list of PosixNetworkInterfaces
+mDNSlocal void FreePosixNetworkInterfaceList(PosixNetworkInterface *intfList)
+	{
+	while (intfList)
+	  {
+		PosixNetworkInterface *next = (PosixNetworkInterface*)(intfList->coreIntf.next);
+		intfList->coreIntf.next = NULL;
+		FreePosixNetworkInterface(intfList);
+		intfList = next;
+	  }
+	}
+
+// Grab the first interface, deregister it, close it, and repeat until done.
+// Returns the list of deregistered interfaces, or NULL if none.
+mDNSlocal PosixNetworkInterface *CloseInterfaceList(mDNS *const m)
 	{
 	assert(m != NULL);
-
+	PosixNetworkInterface *ret = NULL;
 	while (m->HostInterfaces)
 		{
 		PosixNetworkInterface *intf = (PosixNetworkInterface*)(m->HostInterfaces);
 		mDNS_DeregisterInterface(m, &intf->coreIntf, mDNSfalse);
 		if (gMDNSPlatformPosixVerboseLevel > 0) fprintf(stderr, "Deregistered interface %s\n", intf->intfName);
-		FreePosixNetworkInterface(intf);
+		ClosePosixNetworkInterface(intf);
+		assert(intf->coreIntf.next == NULL);
+		intf->coreIntf.next = (NetworkInterfaceInfo*)ret;
+		ret = intf;
 		}
 	num_registered_interfaces = 0;
 	num_pkts_accepted = 0;
 	num_pkts_rejected = 0;
+	return ret;
 	}
 
 // Sets up a send/receive socket.
@@ -848,10 +894,10 @@
 
 // Creates a PosixNetworkInterface for the interface whose IP address is
 // intfAddr and whose name is intfName and registers it with mDNS core.
-mDNSlocal int SetupOneInterface(mDNS *const m, struct sockaddr *intfAddr, struct sockaddr *intfMask, const char *intfName, int intfIndex)
+mDNSlocal int SetupOneInterface(mDNS *const m, struct sockaddr *intfAddr, struct sockaddr *intfMask, const char *intfName, int intfIndex, PosixNetworkInterface **cachedList)
 	{
 	int err = 0;
-	PosixNetworkInterface *intf;
+	PosixNetworkInterface *intf = NULL;
 	PosixNetworkInterface *alias = NULL;
 
 	assert(m != NULL);
@@ -859,15 +905,18 @@
 	assert(intfName != NULL);
 	assert(intfMask != NULL);
 
-	// Allocate the interface structure itself.
-	intf = (PosixNetworkInterface*)malloc(sizeof(*intf));
-	if (intf == NULL) { assert(0); err = ENOMEM; }
-
-	// And make a copy of the intfName.
-	if (err == 0)
+	intf = RemoveInterfaceFromListByIndex(cachedList, intfIndex);
+	if (intf == NULL)
 		{
-		intf->intfName = strdup(intfName);
-		if (intf->intfName == NULL) { assert(0); err = ENOMEM; }
+		// Allocate the interface structure itself.
+		intf = (PosixNetworkInterface*)malloc(sizeof(*intf));
+		if (intf == NULL) { assert(0); err = ENOMEM; }
+		// And make a copy of the intfName.
+		if (err == 0)
+			{
+			intf->intfName = strdup(intfName);
+			if (intf->intfName == NULL) { assert(0); err = ENOMEM; }
+			}
 		}
 
 	if (err == 0)
@@ -933,7 +982,7 @@
 	}
 
 // Call get_ifi_info() to obtain a list of active interfaces and call SetupOneInterface() on each one.
-mDNSlocal int SetupInterfaceList(mDNS *const m)
+mDNSlocal int SetupInterfaceList(mDNS *const m, PosixNetworkInterface **cachedList)
 	{
 	mDNSBool        foundav4       = mDNSfalse;
 	int             err            = 0;
@@ -972,7 +1021,7 @@
 					}
 				else if (i->ifi_flags & (IFF_MULTICAST | IFF_BROADCAST))  // http://b/25669326
 					{
-					if (SetupOneInterface(m, i->ifi_addr, i->ifi_netmask, i->ifi_name, i->ifi_index) == 0)
+					if (SetupOneInterface(m, i->ifi_addr, i->ifi_netmask, i->ifi_name, i->ifi_index, cachedList) == 0)
 						if (i->ifi_addr->sa_family == AF_INET)
 							foundav4 = mDNStrue;
 					}
@@ -986,7 +1035,7 @@
 		// In the interim, we skip loopback interface only if we found at least one v4 interface to use
 		// if ((m->HostInterfaces == NULL) && (firstLoopback != NULL))
 		if (!foundav4 && firstLoopback)
-			(void) SetupOneInterface(m, firstLoopback->ifi_addr, firstLoopback->ifi_netmask, firstLoopback->ifi_name, firstLoopback->ifi_index);
+			(void) SetupOneInterface(m, firstLoopback->ifi_addr, firstLoopback->ifi_netmask, firstLoopback->ifi_name, firstLoopback->ifi_index, cachedList);
 		}
 
 	// Clean up.
@@ -1270,7 +1319,7 @@
 #endif
 
 	// Tell mDNS core about the network interfaces on this machine.
-	if (err == mStatus_NoError) err = SetupInterfaceList(m);
+	if (err == mStatus_NoError) err = SetupInterfaceList(m, NULL);
 
 	// Tell mDNS core about DNS Servers
 	mDNS_Lock(m);
@@ -1302,7 +1351,9 @@
 mDNSexport void mDNSPlatformClose(mDNS *const m)
 	{
 	assert(m != NULL);
-	ClearInterfaceList(m);
+	PosixNetworkInterface *closedList = CloseInterfaceList(m);
+	FreePosixNetworkInterfaceList(closedList);
+
 	if (m->p->unicastSocket4 != -1)
 		{
 		int ipv4_closed = close(m->p->unicastSocket4);
@@ -1320,8 +1371,9 @@
 mDNSexport mStatus mDNSPlatformPosixRefreshInterfaceList(mDNS *const m)
 	{
 	int err;
-	ClearInterfaceList(m);
-	err = SetupInterfaceList(m);
+	PosixNetworkInterface *closedList = CloseInterfaceList(m);
+	err = SetupInterfaceList(m, &closedList);
+	FreePosixNetworkInterfaceList(closedList);
 	return PosixErrorToStatus(err);
 	}
 
diff --git a/mDNSShared/dnssd_clientstub.c b/mDNSShared/dnssd_clientstub.c
index 637c83c..574c77b 100644
--- a/mDNSShared/dnssd_clientstub.c
+++ b/mDNSShared/dnssd_clientstub.c
@@ -75,7 +75,7 @@
 #else
 
 #ifndef __ANDROID__
-	#include <sys/fcntl.h>		// For O_RDWR etc.
+	#include <fcntl.h>		// For O_RDWR etc.
 #else
 	#include <fcntl.h>
 	#define LOG_TAG "libmdns"