-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathservice.go
107 lines (95 loc) · 2.97 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"fmt"
"net"
)
var (
// Multicast groups used by mDNS
mdnsGroupIPv4 = net.IPv4(224, 0, 0, 251)
mdnsGroupIPv6 = net.ParseIP("ff02::fb")
// mDNS wildcard addresses
mdnsWildcardAddrIPv4 = &net.UDPAddr{
IP: net.ParseIP("224.0.0.0"),
Port: 5353,
}
mdnsWildcardAddrIPv6 = &net.UDPAddr{
IP: net.ParseIP("ff02::"),
Port: 5353,
}
// mDNS endpoint addresses
ipv4Addr = &net.UDPAddr{
IP: mdnsGroupIPv4,
Port: 5353,
}
ipv6Addr = &net.UDPAddr{
IP: mdnsGroupIPv6,
Port: 5353,
}
)
// ServiceRecord contains the basic description of a service, which contains instance name, service type & domain
type ServiceRecord struct {
Instance string // Instance name (e.g. "My web page")
Service string // Service name (e.g. _http._tcp.)
Domain string // If blank, assumes "local"
// private variable populated on the first call to ServiceName()/ServiceInstanceName()
serviceName string
serviceInstanceName string
serviceTypeName string
}
// ServiceInstanceName returns complete service instance name (e.g. MyDemo\ Service._foobar._tcp.local.),
// which is composed from service instance name, service name and a domain.
func (s *ServiceRecord) ServiceInstanceName() string {
// If no instance name provided we cannot compose service instance name
if s.Instance == "" {
return ""
}
// If not cached - compose and cache
if s.serviceInstanceName == "" {
s.serviceInstanceName = fmt.Sprintf("%s.%s.%s", trimDot(s.Instance), s.Service, trimDot(s.Domain))
}
return s.serviceInstanceName
}
// NewServiceRecord constructs a ServiceRecord structure by given arguments
func NewServiceRecord(instance, service, domain string) *ServiceRecord {
return &ServiceRecord{instance, service, domain, "", "", ""}
}
// LookupParams contains configurable properties to create a service discovery request
type LookupParams struct {
ServiceRecord
Rrtype uint16
unicastFlag bool
Entries chan<- *ServiceEntry // Entries Channel
}
// NewLookupParams constructs a LookupParams structure by given arguments
func NewLookupParams(instance, service, domain string, entries chan<- *ServiceEntry) *LookupParams {
return &LookupParams{
*NewServiceRecord(instance, service, domain),
0,
false,
entries,
}
}
// ServiceEntry represents a browse/lookup result for client API.
// It is also used to configure service registration (server API), which is
// used to answer multicast queries.
type ServiceEntry struct {
ServiceRecord
HostName string // Host machine DNS name
Port int // Service Port
Text []string // Service info served as a TXT record
TTL uint32 // TTL of the service record
AddrIPv4 net.IP // Host machine IPv4 address
AddrIPv6 net.IP // Host machine IPv6 address
}
// NewServiceEntry constructs a ServiceEntry structure by given arguments
func NewServiceEntry(instance, service, domain string) *ServiceEntry {
return &ServiceEntry{
*NewServiceRecord(instance, service, domain),
"",
0,
[]string{},
0,
nil,
nil,
}
}