2016-04-30 12:53:26 +03:00
|
|
|
# MDNS [![GoDoc](https://godoc.org/github.com/micro/mdns?status.svg)](https://godoc.org/github.com/micro/mdns)
|
2014-01-29 23:39:18 +04:00
|
|
|
|
2018-12-02 02:02:27 +03:00
|
|
|
MDNS is a simple mdns client/server library by Hashicorp.
|
|
|
|
|
|
|
|
We maintain a fork with updates for PRs and issues they have not merged or addressed.
|
|
|
|
|
|
|
|
## Overview
|
|
|
|
|
|
|
|
MDNS or Multicast DNS can be used to discover services on the local network without the use of an authoritative
|
2014-01-30 04:16:47 +04:00
|
|
|
DNS server. This enables peer-to-peer discovery. It is important to note that many
|
|
|
|
networks restrict the use of multicasting, which prevents mDNS from functioning.
|
|
|
|
Notably, multicast cannot be used in any sort of cloud, or shared infrastructure
|
|
|
|
environment. However it works well in most office, home, or private infrastructure
|
|
|
|
environments.
|
|
|
|
|
2018-12-02 02:02:27 +03:00
|
|
|
## Usage
|
|
|
|
|
2014-01-30 04:16:47 +04:00
|
|
|
Using the library is very simple, here is an example of publishing a service entry:
|
|
|
|
|
2018-12-02 02:03:01 +03:00
|
|
|
```go
|
2016-04-30 01:53:34 +03:00
|
|
|
package main
|
2014-01-30 04:16:47 +04:00
|
|
|
|
2016-04-30 01:53:34 +03:00
|
|
|
import (
|
|
|
|
"github.com/micro/mdns"
|
|
|
|
"os"
|
|
|
|
)
|
2014-01-30 04:16:47 +04:00
|
|
|
|
2016-04-30 01:53:34 +03:00
|
|
|
func main() {
|
|
|
|
|
|
|
|
// Setup our service export
|
|
|
|
host, _ := os.Hostname()
|
|
|
|
info := []string{"My awesome service"}
|
|
|
|
service, _ := mdns.NewMDNSService(host, "_foobar._tcp", "", "", 8000, nil, info)
|
|
|
|
|
|
|
|
// Create the mDNS server, defer shutdown
|
|
|
|
server, _ := mdns.NewServer(&mdns.Config{Zone: service})
|
|
|
|
|
|
|
|
defer server.Shutdown()
|
|
|
|
}
|
|
|
|
```
|
2014-01-30 04:16:47 +04:00
|
|
|
|
|
|
|
Doing a lookup for service providers is also very simple:
|
|
|
|
|
2018-12-02 02:03:01 +03:00
|
|
|
```go
|
2016-04-30 01:53:34 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-04-30 02:09:01 +03:00
|
|
|
"github.com/micro/mdns"
|
2016-04-30 01:53:34 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
|
|
|
// Make a channel for results and start listening
|
|
|
|
entriesCh := make(chan *mdns.ServiceEntry, 8)
|
|
|
|
go func() {
|
|
|
|
for entry := range entriesCh {
|
|
|
|
fmt.Printf("Got new entry: %v\n", entry)
|
|
|
|
}
|
|
|
|
}()
|
2014-01-30 04:16:47 +04:00
|
|
|
|
2016-04-30 01:53:34 +03:00
|
|
|
// Start the lookup
|
|
|
|
err := mdns.Lookup("_foobar._tcp", entriesCh)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2014-01-30 04:16:47 +04:00
|
|
|
|
2016-04-30 01:53:34 +03:00
|
|
|
close(entriesCh)
|
|
|
|
}
|
|
|
|
```
|