diff options
author | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2022-09-02 18:00:06 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2022-09-02 18:00:06 +0000 |
commit | 6e45066f94d28f5b98132b41dad62fb2a0ec88b9 (patch) | |
tree | a8cfaed4fbccf460d1e9d43e80c88e4cc9a80efb | |
parent | 7954d70a0bdeb909acf7a5d640435c9806599de2 (diff) | |
parent | aa44607c48b8638cfeac9f329480c7a82d5511d2 (diff) | |
download | mdnsresponder-6e45066f94d28f5b98132b41dad62fb2a0ec88b9.tar.gz |
Merge "Snap for 9023430 from 65aabca6fa05a277c9aa38daec579f1bdb1443f8 to simpleperf-release" into simpleperf-release
-rw-r--r-- | Android.bp | 1 | ||||
-rw-r--r-- | mDNSPosix/mDNSPosix.c | 122 | ||||
-rwxr-xr-x | mDNSPosix/mDNSUNP.c | 2 |
3 files changed, 87 insertions, 38 deletions
@@ -175,6 +175,7 @@ cc_library { static_libs: ["libcutils"], shared_libs: ["liblog"], + min_sdk_version: "apex_inherit", apex_available: [ "//apex_available:platform", "com.android.adbd", diff --git a/mDNSPosix/mDNSPosix.c b/mDNSPosix/mDNSPosix.c index 64658b3..28f4e06 100644 --- a/mDNSPosix/mDNSPosix.c +++ b/mDNSPosix/mDNSPosix.c @@ -581,42 +581,88 @@ mDNSexport mDNSu32 mDNSPlatformInterfaceIndexfromInterfaceID(mDNS *const m, mDNS 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) { - assert(m != NULL); + 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 @@ mDNSlocal int SetupSocket(struct sockaddr *intfAddr, mDNSIPPort port, int interf // 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 @@ mDNSlocal int SetupOneInterface(mDNS *const m, struct sockaddr *intfAddr, struct 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 @@ mDNSlocal int SetupOneInterface(mDNS *const m, struct sockaddr *intfAddr, struct } // 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 @@ mDNSlocal int SetupInterfaceList(mDNS *const m) } 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 @@ mDNSlocal int SetupInterfaceList(mDNS *const m) // 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. @@ -1053,14 +1102,14 @@ mDNSlocal void PrintNetLinkMsg(const struct nlmsghdr *pNLMsg) } #endif -mDNSlocal mDNSu32 ProcessRoutingNotification(int sd) +mDNSlocal mDNSBool ProcessRoutingNotification(int sd) // Read through the messages on sd and if any indicate that any interface records should // be torn down and rebuilt, return affected indices as a bitmask. Otherwise return 0. { ssize_t readCount; char buff[4096]; struct nlmsghdr *pNLMsg = (struct nlmsghdr*) buff; - mDNSu32 result = 0; + mDNSBool result = mDNSfalse; // The structure here is more complex than it really ought to be because, // unfortunately, there's no good way to size a buffer in advance large @@ -1095,10 +1144,9 @@ mDNSlocal mDNSu32 ProcessRoutingNotification(int sd) #endif // Process the NetLink message - if (pNLMsg->nlmsg_type == RTM_GETLINK || pNLMsg->nlmsg_type == RTM_NEWLINK) - result |= 1 << ((struct ifinfomsg*) NLMSG_DATA(pNLMsg))->ifi_index; - else if (pNLMsg->nlmsg_type == RTM_DELADDR || pNLMsg->nlmsg_type == RTM_NEWADDR) - result |= 1 << ((struct ifaddrmsg*) NLMSG_DATA(pNLMsg))->ifa_index; + if (pNLMsg->nlmsg_type == RTM_GETLINK || pNLMsg->nlmsg_type == RTM_NEWLINK || + pNLMsg->nlmsg_type == RTM_DELADDR || pNLMsg->nlmsg_type == RTM_NEWADDR) + result = mDNStrue; // Advance pNLMsg to the next message in the buffer if ((pNLMsg->nlmsg_flags & NLM_F_MULTI) != 0 && pNLMsg->nlmsg_type != NLMSG_DONE) @@ -1142,14 +1190,14 @@ mDNSlocal void PrintRoutingSocketMsg(const struct ifa_msghdr *pRSMsg) } #endif -mDNSlocal mDNSu32 ProcessRoutingNotification(int sd) +mDNSlocal mDNSBool ProcessRoutingNotification(int sd) // Read through the messages on sd and if any indicate that any interface records should // be torn down and rebuilt, return affected indices as a bitmask. Otherwise return 0. { ssize_t readCount; char buff[4096]; struct ifa_msghdr *pRSMsg = (struct ifa_msghdr*) buff; - mDNSu32 result = 0; + mDNSBool result = mDNSfalse; readCount = read(sd, buff, sizeof buff); if (readCount < (ssize_t) sizeof(struct ifa_msghdr)) @@ -1163,10 +1211,7 @@ mDNSlocal mDNSu32 ProcessRoutingNotification(int sd) if (pRSMsg->ifam_type == RTM_NEWADDR || pRSMsg->ifam_type == RTM_DELADDR || pRSMsg->ifam_type == RTM_IFINFO) { - if (pRSMsg->ifam_type == RTM_IFINFO) - result |= 1 << ((struct if_msghdr*) pRSMsg)->ifm_index; - else - result |= 1 << pRSMsg->ifam_index; + result = mDNStrue; } return result; @@ -1179,7 +1224,7 @@ mDNSlocal void InterfaceChangeCallback(int fd, short filter, void *context) { IfChangeRec *pChgRec = (IfChangeRec*) context; fd_set readFDs; - mDNSu32 changedInterfaces = 0; + mDNSBool changedInterfaces = mDNSfalse; struct timeval zeroTimeout = { 0, 0 }; (void)fd; // Unused @@ -1270,7 +1315,7 @@ mDNSexport mStatus mDNSPlatformInit(mDNS *const m) #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 +1347,9 @@ mDNSexport mStatus mDNSPlatformInit(mDNS *const m) 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 +1367,9 @@ mDNSexport void mDNSPlatformClose(mDNS *const m) 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/mDNSPosix/mDNSUNP.c b/mDNSPosix/mDNSUNP.c index 5379b6b..0e3632b 100755 --- a/mDNSPosix/mDNSUNP.c +++ b/mDNSPosix/mDNSUNP.c @@ -106,7 +106,7 @@ struct ifi_info *get_ifi_info_linuxv6(int family, int doaliases) goto gotError; } while (fscanf(fp, - "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %15s\n", + "%4s%4s%4s%4s%4s%4s%4s%4s %x %x %x %x %15s\n", addr[0],addr[1],addr[2],addr[3], addr[4],addr[5],addr[6],addr[7], &index, &plen, &scope, &flags, ifname) != EOF) { |