richardschneider / net-mdns

Simple multicast DNS
MIT License
232 stars 81 forks source link

Host names and underscore #68

Closed richardschneider closed 5 years ago

richardschneider commented 5 years ago

A hostname cannot contain an underscore (_). ServiceProfile should replace "_" with "-" when generating a default host name.

Helps with #64

gilzad commented 5 years ago

I assume there is an issue with the host name in the SRV record. ServiceProfile.cs creates the same string for the SRV-hostname and for the Type-A-domain-name. Avahi and Bonjour place the machine's actual host name in the SRV record, if you don't choose to overwrite it.

For my own purposes I changed ServiceProfile() to this older version, which treats Type-A-domain-name and SRV-host-name differently:

public ServiceProfile(string instanceName, string serviceName, ushort port, IEnumerable<IPAddress> addresses = null, string hostName = null)
{
  InstanceName = instanceName;
  ServiceName = serviceName;
  var fqn = FullyQualifiedName;

  var simpleServiceName = new DomainName(ServiceName.ToString()
    .Replace("._tcp", "")
    .Replace("._udp", "")
    .TrimStart('_'));//allowing underscores again

  string domainName = DomainName.Join(InstanceName, simpleServiceName, Domain);
  //host name now defaulting to computer's host name.
  HostName = hostName ?? (Environment.GetEnvironmentVariable("COMPUTERNAME") ?? Environment.GetEnvironmentVariable("HOSTNAME"))  + "." + Domain;
  Resources.Add(new SRVRecord
  {
    Name = fqn,
    Port = port,
    Target = HostName
  });

  //removed default "txtvers=1" TXTRecord.

  foreach (var address in addresses ?? MulticastService.GetLinkLocalAddresses())
  {
    Resources.Add(AddressRecord.Create(domainName, address));
  }
}

I didn't create a PR yet for several reasons.

However, that doesn't mean I'm not willing to contribute. I'm just careful with my current 'solution'.