Skip to content

Commit

Permalink
[mdns] add Linux specific patches
Browse files Browse the repository at this point in the history
  • Loading branch information
agners committed Aug 3, 2023
1 parent 1e4d58b commit 19ff1b0
Show file tree
Hide file tree
Showing 13 changed files with 874 additions and 6 deletions.
1 change: 1 addition & 0 deletions etc/docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ RUN apt-get update \
&& ln -fs /usr/share/zoneinfo/UTC /etc/localtime

COPY ./script /app/script
COPY ./third_party/mDNSResponder /app/third_party/mDNSResponder
WORKDIR /app

RUN ./script/bootstrap
Expand Down
11 changes: 8 additions & 3 deletions script/bootstrap
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,18 @@ install_packages_apt()
# mDNS
sudo apt-get install --no-install-recommends -y libavahi-client3 libavahi-common-dev libavahi-client-dev avahi-daemon
(MDNS_RESPONDER_SOURCE_NAME=mDNSResponder-1790.80.10 \
&& MDNS_RESPONDER_PATCH_PATH=$(realpath "$(dirname "$0")"/../third_party/mDNSResponder) \
&& cd /tmp \
&& wget --no-check-certificate https://github.com/apple-oss-distributions/mDNSResponder/archive/refs/tags/$MDNS_RESPONDER_SOURCE_NAME.tar.gz \
&& mkdir -p $MDNS_RESPONDER_SOURCE_NAME \
&& tar xvf $MDNS_RESPONDER_SOURCE_NAME.tar.gz -C $MDNS_RESPONDER_SOURCE_NAME --strip-components=1 \
&& cd /tmp/"$MDNS_RESPONDER_SOURCE_NAME"/mDNSShared \
&& sed -i 's/__block mStatus err;/mStatus err;/' uds_daemon.c \
&& cd /tmp/"$MDNS_RESPONDER_SOURCE_NAME"/mDNSPosix \
&& cd /tmp/"$MDNS_RESPONDER_SOURCE_NAME" \
&& (
for patch in "$MDNS_RESPONDER_PATCH_PATH"/*.patch; do
patch -p1 <"$patch"
done
) \
&& cd mDNSPosix \
&& make os=linux tls=no && sudo make install os=linux tls=no)

# Boost
Expand Down
12 changes: 9 additions & 3 deletions tests/scripts/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ set -euxo pipefail
TOOLS_HOME="$HOME"/.cache/tools
[[ -d $TOOLS_HOME ]] || mkdir -p "$TOOLS_HOME"

MDNSRESPONDER_PATCH_PATH=$(realpath "$(dirname "$0")"/../../third_party/mDNSResponder)

disable_install_recommends()
{
OTBR_APT_CONF_FILE=/etc/apt/apt.conf
Expand Down Expand Up @@ -130,9 +132,13 @@ case "$(uname)" in
wget https://github.com/apple-oss-distributions/mDNSResponder/archive/refs/tags/$SOURCE_NAME.tar.gz \
&& mkdir -p $SOURCE_NAME \
&& tar xvf $SOURCE_NAME.tar.gz -C $SOURCE_NAME --strip-components=1 \
&& cd "$SOURCE_NAME"/mDNSShared \
&& sed -i 's/__block mStatus err;/mStatus err;/' uds_daemon.c \
&& cd ../mDNSPosix \
&& cd "$SOURCE_NAME" \
&& (
for patch in "$MDNSRESPONDER_PATCH_PATH"/*.patch; do
patch -p1 <"$patch"
done
) \
&& cd mDNSPosix \
&& make os=linux tls=no && sudo make install os=linux tls=no
fi

Expand Down
32 changes: 32 additions & 0 deletions third_party/mDNSResponder/0001-Fix-Linux-build.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
From e136dcdcdd93ef32ada981e89c195905eb809eea Mon Sep 17 00:00:00 2001
Message-ID: <e136dcdcdd93ef32ada981e89c195905eb809eea.1687508149.git.stefan@agner.ch>
From: Nate Karstens <[email protected]>
Date: Thu, 23 Mar 2023 00:15:52 -0500
Subject: [PATCH] Fix Linux build

The __block qualifier is not used in Linux builds.

Signed-off-by: Nate Karstens <[email protected]>
---
mDNSShared/uds_daemon.c | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/mDNSShared/uds_daemon.c b/mDNSShared/uds_daemon.c
index 9ae5f78..5a00bb5 100644
--- a/mDNSShared/uds_daemon.c
+++ b/mDNSShared/uds_daemon.c
@@ -2912,7 +2912,11 @@ exit:
mDNSlocal mStatus add_domain_to_browser(request_state *info, const domainname *d)
{
browser_t *b, *p;
+#if defined(TARGET_OS_MAC) && TARGET_OS_MAC
__block mStatus err;
+#else
+ mStatus err;
+#endif

for (p = info->u.browser.browsers; p; p = p->next)
{
--
2.41.0

Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
From 4f7970ac1615aba7a39ae94c1ca14135265574e9 Mon Sep 17 00:00:00 2001
Message-ID: <4f7970ac1615aba7a39ae94c1ca14135265574e9.1687508149.git.stefan@agner.ch>
In-Reply-To: <e136dcdcdd93ef32ada981e89c195905eb809eea.1687508149.git.stefan@agner.ch>
References: <e136dcdcdd93ef32ada981e89c195905eb809eea.1687508149.git.stefan@agner.ch>
From: Nate Karstens <[email protected]>
Date: Wed, 28 Jun 2017 17:30:00 -0500
Subject: [PATCH] Create subroutine for cleaning recent interfaces

Moves functionality for cleaning the list of recent
interfaces into its own subroutine.

Upstream-Status: Submitted [[email protected]]

Signed-off-by: Nate Karstens <[email protected]>
Signed-off-by: Alex Kiernan <[email protected]>
---
mDNSPosix/mDNSPosix.c | 24 ++++++++++++++----------
1 file changed, 14 insertions(+), 10 deletions(-)

diff --git a/mDNSPosix/mDNSPosix.c b/mDNSPosix/mDNSPosix.c
index 0a7c3df..fe7242d 100644
--- a/mDNSPosix/mDNSPosix.c
+++ b/mDNSPosix/mDNSPosix.c
@@ -1322,6 +1322,19 @@ mDNSlocal int SetupSocket(struct sockaddr *intfAddr, mDNSIPPort port, int interf
return err;
}

+// Clean up any interfaces that have been hanging around on the RecentInterfaces list for more than a minute
+mDNSlocal void CleanRecentInterfaces(void)
+{
+ PosixNetworkInterface **ri = &gRecentInterfaces;
+ const mDNSs32 utc = mDNSPlatformUTC();
+ while (*ri)
+ {
+ PosixNetworkInterface *pi = *ri;
+ if (utc - pi->LastSeen < 60) ri = (PosixNetworkInterface **)&pi->coreIntf.next;
+ else { *ri = (PosixNetworkInterface *)pi->coreIntf.next; mdns_free(pi); }
+ }
+}
+
// 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,
@@ -1559,16 +1572,7 @@ mDNSlocal int SetupInterfaceList(mDNS *const m)

// Clean up.
if (intfList != NULL) freeifaddrs(intfList);
-
- // Clean up any interfaces that have been hanging around on the RecentInterfaces list for more than a minute
- PosixNetworkInterface **ri = &gRecentInterfaces;
- const mDNSs32 utc = mDNSPlatformUTC();
- while (*ri)
- {
- PosixNetworkInterface *pi = *ri;
- if (utc - pi->LastSeen < 60) ri = (PosixNetworkInterface **)&pi->coreIntf.next;
- else { *ri = (PosixNetworkInterface *)pi->coreIntf.next; mdns_free(pi); }
- }
+ CleanRecentInterfaces();

return err;
}
--
2.41.0

Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
From f7ab91f739b936305ca56743adfb4673e3f2f4ba Mon Sep 17 00:00:00 2001
Message-ID: <f7ab91f739b936305ca56743adfb4673e3f2f4ba.1687508149.git.stefan@agner.ch>
In-Reply-To: <e136dcdcdd93ef32ada981e89c195905eb809eea.1687508149.git.stefan@agner.ch>
References: <e136dcdcdd93ef32ada981e89c195905eb809eea.1687508149.git.stefan@agner.ch>
From: Nate Karstens <[email protected]>
Date: Wed, 28 Jun 2017 17:30:00 -0500
Subject: [PATCH] Create subroutine for tearing down an interface

Creates a subroutine for tearing down an interface.

Upstream-Status: Submitted [[email protected]]

Signed-off-by: Nate Karstens <[email protected]>
Signed-off-by: Alex Kiernan <[email protected]>
---
mDNSPosix/mDNSPosix.c | 22 ++++++++++++++++------
1 file changed, 16 insertions(+), 6 deletions(-)

diff --git a/mDNSPosix/mDNSPosix.c b/mDNSPosix/mDNSPosix.c
index fe7242d..a32a880 100644
--- a/mDNSPosix/mDNSPosix.c
+++ b/mDNSPosix/mDNSPosix.c
@@ -1043,6 +1043,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)
{
@@ -1051,13 +1064,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);
}

mDNSlocal int SetupIPv6Socket(int fd)
--
2.41.0

54 changes: 54 additions & 0 deletions third_party/mDNSResponder/0004-Track-interface-socket-family.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
From 542c1b2ce1dcc069cf848d11978c8b6ae5982b6e Mon Sep 17 00:00:00 2001
Message-ID: <542c1b2ce1dcc069cf848d11978c8b6ae5982b6e.1687508149.git.stefan@agner.ch>
In-Reply-To: <e136dcdcdd93ef32ada981e89c195905eb809eea.1687508149.git.stefan@agner.ch>
References: <e136dcdcdd93ef32ada981e89c195905eb809eea.1687508149.git.stefan@agner.ch>
From: Nate Karstens <[email protected]>
Date: Wed, 28 Jun 2017 17:30:00 -0500
Subject: [PATCH] Track interface socket family

Tracks the socket family associated with the interface.

Upstream-Status: Submitted [[email protected]]

Signed-off-by: Nate Karstens <[email protected]>
Signed-off-by: Alex Kiernan <[email protected]>
---
mDNSPosix/mDNSPosix.c | 1 +
mDNSPosix/mDNSPosix.h | 2 ++
2 files changed, 3 insertions(+)

diff --git a/mDNSPosix/mDNSPosix.c b/mDNSPosix/mDNSPosix.c
index a32a880..9a5b4d7 100644
--- a/mDNSPosix/mDNSPosix.c
+++ b/mDNSPosix/mDNSPosix.c
@@ -1415,6 +1415,7 @@ mDNSlocal int SetupOneInterface(mDNS *const m, struct sockaddr *intfAddr, struct
// Set up the extra fields in PosixNetworkInterface.
assert(intf->intfName != NULL); // intf->intfName already set up above
intf->index = intfIndex;
+ intf->sa_family = intfAddr->sa_family;
intf->multicastSocket4 = -1;
#if HAVE_IPV6
intf->multicastSocket6 = -1;
diff --git a/mDNSPosix/mDNSPosix.h b/mDNSPosix/mDNSPosix.h
index 9675591..dd7864c 100644
--- a/mDNSPosix/mDNSPosix.h
+++ b/mDNSPosix/mDNSPosix.h
@@ -19,6 +19,7 @@
#define __mDNSPlatformPosix_h

#include <signal.h>
+#include <sys/socket.h>
#include <sys/time.h>

#ifdef __cplusplus
@@ -40,6 +41,7 @@ struct PosixNetworkInterface
char * intfName;
PosixNetworkInterface * aliasIntf;
int index;
+ sa_family_t sa_family;
int multicastSocket4;
#if HAVE_IPV6
int multicastSocket6;
--
2.41.0

Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
From 44385771ef63f081ed7e80eae6f24591046b4c7c Mon Sep 17 00:00:00 2001
Message-ID: <44385771ef63f081ed7e80eae6f24591046b4c7c.1687508149.git.stefan@agner.ch>
In-Reply-To: <e136dcdcdd93ef32ada981e89c195905eb809eea.1687508149.git.stefan@agner.ch>
References: <e136dcdcdd93ef32ada981e89c195905eb809eea.1687508149.git.stefan@agner.ch>
From: Nate Karstens <[email protected]>
Date: Tue, 1 Aug 2017 17:06:01 -0500
Subject: [PATCH] Indicate loopback interface to mDNS core

Tells the mDNS core if an interface is a loopback interface,
similar to AddInterfaceToList() in the MacOS implementation.

Upstream-Status: Submitted [[email protected]]

Signed-off-by: Nate Karstens <[email protected]>
Signed-off-by: Alex Kiernan <[email protected]>
---
mDNSPosix/mDNSPosix.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/mDNSPosix/mDNSPosix.c b/mDNSPosix/mDNSPosix.c
index 9a5b4d7..02a19b4 100644
--- a/mDNSPosix/mDNSPosix.c
+++ b/mDNSPosix/mDNSPosix.c
@@ -1348,7 +1348,7 @@ mDNSlocal void CleanRecentInterfaces(void)
// 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 mDNSu8 *intfHaddr, mDNSu16 intfHlen, const char *intfName, int intfIndex)
+ const mDNSu8 *intfHaddr, mDNSu16 intfHlen, const char *intfName, int intfIndex, int intfFlags)
{
int err = 0;
PosixNetworkInterface *intf;
@@ -1411,6 +1411,7 @@ mDNSlocal int SetupOneInterface(mDNS *const m, struct sockaddr *intfAddr, struct

intf->coreIntf.Advertise = m->AdvertiseLocalAddresses;
intf->coreIntf.McastTxRx = mDNStrue;
+ intf->coreIntf.Loopback = ((intfFlags & IFF_LOOPBACK) != 0) ? mDNStrue : mDNSfalse;

// Set up the extra fields in PosixNetworkInterface.
assert(intf->intfName != NULL); // intf->intfName already set up above
@@ -1561,7 +1562,7 @@ mDNSlocal int SetupInterfaceList(mDNS *const m)
}
#endif
if (SetupOneInterface(m, i->ifa_addr, i->ifa_netmask,
- hwaddr, hwaddr_len, i->ifa_name, ifIndex) == 0)
+ hwaddr, hwaddr_len, i->ifa_name, ifIndex, i->ifa_flags) == 0)
{
if (i->ifa_addr->sa_family == AF_INET)
foundav4 = mDNStrue;
@@ -1578,7 +1579,7 @@ mDNSlocal int SetupInterfaceList(mDNS *const m)
// if ((m->HostInterfaces == NULL) && (firstLoopback != NULL))
if (!foundav4 && firstLoopback)
(void) SetupOneInterface(m, firstLoopback->ifa_addr, firstLoopback->ifa_netmask,
- NULL, 0, firstLoopback->ifa_name, firstLoopbackIndex);
+ NULL, 0, firstLoopback->ifa_name, firstLoopbackIndex, firstLoopback->ifa_flags);
}

// Clean up.
--
2.41.0

Loading

0 comments on commit 19ff1b0

Please sign in to comment.