9 // ServiceRecord contains the basic description of a service, which contains instance name, service type & domain
10 type ServiceRecord struct {
11 Instance string `json:"name"` // Instance name (e.g. "My web page")
12 Service string `json:"type"` // Service name (e.g. _http._tcp.)
13 Domain string `json:"domain"` // If blank, assumes "local"
15 // private variable populated on ServiceRecord creation
17 serviceInstanceName string
18 serviceTypeName string
21 // ServiceName returns a complete service name (e.g. _foobar._tcp.local.), which is composed
22 // of a service name (also referred as service type) and a domain.
23 func (s *ServiceRecord) ServiceName() string {
27 // ServiceInstanceName returns a complete service instance name (e.g. MyDemo\ Service._foobar._tcp.local.),
28 // which is composed from service instance name, service name and a domain.
29 func (s *ServiceRecord) ServiceInstanceName() string {
30 return s.serviceInstanceName
33 // ServiceTypeName returns the complete identifier for a DNS-SD query.
34 func (s *ServiceRecord) ServiceTypeName() string {
35 return s.serviceTypeName
38 // NewServiceRecord constructs a ServiceRecord.
39 func NewServiceRecord(instance, service, domain string) *ServiceRecord {
44 serviceName: fmt.Sprintf("%s.%s.", trimDot(service), trimDot(domain)),
47 // Cache service instance name
49 s.serviceInstanceName = fmt.Sprintf("%s.%s", trimDot(s.Instance), s.ServiceName())
52 // Cache service type name domain
53 typeNameDomain := "local"
54 if len(s.Domain) > 0 {
55 typeNameDomain = trimDot(s.Domain)
57 s.serviceTypeName = fmt.Sprintf("_services._dns-sd._udp.%s.", typeNameDomain)
62 // LookupParams contains configurable properties to create a service discovery request
63 type LookupParams struct {
65 Entries chan<- *ServiceEntry // Entries Channel
67 stopProbing chan struct{}
71 // NewLookupParams constructs a LookupParams.
72 func NewLookupParams(instance, service, domain string, entries chan<- *ServiceEntry) *LookupParams {
74 ServiceRecord: *NewServiceRecord(instance, service, domain),
77 stopProbing: make(chan struct{}),
81 // Notify subscriber that no more entries will arrive. Mostly caused
82 // by an expired context.
83 func (l *LookupParams) done() {
87 func (l *LookupParams) disableProbing() {
88 l.once.Do(func() { close(l.stopProbing) })
91 // ServiceEntry represents a browse/lookup result for client API.
92 // It is also used to configure service registration (server API), which is
93 // used to answer multicast queries.
94 type ServiceEntry struct {
96 HostName string `json:"hostname"` // Host machine DNS name
97 Port int `json:"port"` // Service Port
98 Text []string `json:"text"` // Service info served as a TXT record
99 TTL uint32 `json:"ttl"` // TTL of the service record
100 AddrIPv4 []net.IP `json:"-"` // Host machine IPv4 address
101 AddrIPv6 []net.IP `json:"-"` // Host machine IPv6 address
104 // NewServiceEntry constructs a ServiceEntry.
105 func NewServiceEntry(instance, service, domain string) *ServiceEntry {
106 return &ServiceEntry{
107 ServiceRecord: *NewServiceRecord(instance, service, domain),