https://github.com/gatkin/dnssd
DNS Service Discovery for Go
https://github.com/gatkin/dnssd
dns-sd go golang mdns service-discovery
Last synced: 4 months ago
JSON representation
DNS Service Discovery for Go
- Host: GitHub
- URL: https://github.com/gatkin/dnssd
- Owner: gatkin
- License: mit
- Created: 2018-03-04T00:23:39.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-03-13T02:05:15.000Z (over 7 years ago)
- Last Synced: 2025-01-19T08:27:03.918Z (9 months ago)
- Topics: dns-sd, go, golang, mdns, service-discovery
- Language: Go
- Size: 64.5 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# DNS-Based Service Discovery for Go
[](http://godoc.org/github.com/gatkin/dnssd)
[](https://travis-ci.org/gatkin/dnssd)
[](https://goreportcard.com/report/github.com/gatkin/dnssd)DNS-Based service discovery, or DNS-SD ([RFC 6763](https://tools.ietf.org/html/rfc6763)), can be used to discover services on a local area network advertised over Multicast DNS, or mDNS ([RFC 6762](https://tools.ietf.org/html/rfc6762)), enabling peer-to-peer discovery.
This library provides DNS-SD capabilities in Go. To use it, you first create a resolver by specifying the interfaces and address family on which to browse for services.
```go
ifi, err := net.InterfaceByName("en0")
if err != nil {
log.Fatal(err)
}resolver, err := dnssd.NewResolver(dnssd.AddrFamilyIPv4, []net.Interface{*ifi})
if err != nil {
log.Fatal(err)
}
defer resolver.Close()
```Next, you provide the names of the services which you wish to browse for to the resolver. The same resolver can be used to browse for multiple services.
```go
resolver.BrowseService("_http._tcp.local.")
resolver.BrowseService("_googlecast._tcp.local.")
```Finally, you can query the resolver for the set of fully resolved service instances by either retrieving all resolved instances or only instances for a particular service.
```go
instances := resolver.GetAllResolvedInstances()
for _, instance := range instances {
fmt.Printf("%v\n", instance)
}chromecasts := resolver.GetResolvedInstances("_googlecast._tcp.local.")
for _, instance := range instances {
fmt.Printf("%v\n", instance)
}
```We can put all of this together to discover all instances of the `_http._tcp` service on the local network
```go
package mainimport (
"fmt"
"log"
"net"
"time""github.com/gatkin/dnssd"
)func main() {
ifi, err := net.InterfaceByName("en0")
if err != nil {
log.Fatal(err)
}resolver, err := dnssd.NewResolver(dnssd.AddrFamilyIPv4, []net.Interface{*ifi})
if err != nil {
log.Fatal(err)
}
defer resolver.Close()resolver.BrowseService("_http._tcp.local.")
resolver.BrowseService("_googlecast._tcp.local.")// Wait some time to allow all service instances to be discovered.
time.Sleep(1 * time.Second)instances := resolver.GetAllResolvedInstances()
for _, instance := range instances {
fmt.Printf("%v\n", instance)
}
}
```