Skip to content

Commit

Permalink
fix(ServiceProfile): only advertise link local addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed Jul 22, 2018
1 parent 1b976bb commit 9bd6908
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Mdns.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

<ItemGroup>
<PackageReference Include="Common.Logging" Version="3.4.1" />
<PackageReference Include="IPNetwork2" Version="2.1.2" />
<PackageReference Include="Makaretu.Dns" Version="0.13.4" />
</ItemGroup>

Expand Down
22 changes: 22 additions & 0 deletions src/MulticastService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public class MulticastService : IResolver, IDisposable
static readonly ILog log = LogManager.GetLogger(typeof(MulticastService));
static readonly IPAddress MulticastAddressIp4 = IPAddress.Parse("224.0.0.251");
static readonly IPAddress MulticastAddressIp6 = IPAddress.Parse("FF02::FB");
static readonly IPNetwork[] linkLocalNetworks = new IPNetwork[]
{
IPNetwork.Parse("169.254.0.0/16"),
IPNetwork.Parse("fe80::/10")
};

const int MulticastPort = 5353;
// IP header (20 bytes for IPv4; 40 bytes for IPv6) and the UDP header(8 bytes).
Expand Down Expand Up @@ -152,6 +157,23 @@ public static IEnumerable<IPAddress> GetIPAddresses()
.Select(u => u.Address);
}

/// <summary>
/// Get the link local IP addresses of the local machine.
/// </summary>
/// <returns>
/// A sequence of IP addresses.
/// </returns>
/// <remarks>
/// All IPv4 addresses are considered link local.
/// </remarks>
/// <seealso href="https://en.wikipedia.org/wiki/Link-local_address"/>
public static IEnumerable<IPAddress> GetLinkLocalAddresses()
{
return GetIPAddresses()
.Where(a => a.AddressFamily == AddressFamily.InterNetwork ||
(a.AddressFamily == AddressFamily.InterNetworkV6 && a.IsIPv6LinkLocal));
}

/// <summary>
/// Start the service.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/ServiceProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public ServiceProfile(string instanceName, string serviceName, ushort port, IEnu
Strings = { "txtvers=1" }
});

foreach (var address in addresses ?? MulticastService.GetIPAddresses())
foreach (var address in addresses ?? MulticastService.GetLinkLocalAddresses())
{
Resources.Add(AddressRecord.Create(HostName, address));
}
Expand Down

0 comments on commit 9bd6908

Please # to comment.