• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2018 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5package osp
6
7// TODO(pthatcher):
8// - Make our own abstraction that has
9//   .InstanceName, .HostName, .MetadataVersion, .FingerPrint
10//   rather than using mdns.ServiceEntry
11// - Advertise TXT (text below) with "fp" and "mv"
12
13import (
14	"context"
15
16	mdns "github.com/grandcat/zeroconf"
17)
18
19const (
20	MdnsServiceType = "_openscreen._udp"
21	MdnsDomain      = "local"
22)
23
24// Returns a channel of mDNS entries
25// The critical parts are entry.Target (name) entry.HostName (address)
26func BrowseMdns(ctx context.Context) (<-chan *mdns.ServiceEntry, error) {
27	entries := make(chan *mdns.ServiceEntry)
28
29	resolver, err := mdns.NewResolver(nil)
30	if err != nil {
31		return entries, err
32	}
33
34	err = resolver.Browse(ctx, MdnsServiceType, MdnsDomain, entries)
35	return entries, err
36}
37
38func RunMdnsServer(ctx context.Context, instance string, port int) error {
39	var text []string
40	server, err := mdns.Register(instance, MdnsServiceType, MdnsDomain, port, text, nil /* ifaces */)
41	if err != nil {
42		return err
43	}
44	waitUntilDone(ctx)
45	server.Shutdown()
46	return nil
47}
48