bonjour

package module
v0.0.0-...-98f05a3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 22, 2016 License: MIT Imports: 11 Imported by: 0

README

bonjour

This is a simple Multicast DNS-SD (Apple Bonjour) library written in Golang. You can use it to discover services in the LAN. Pay attention to the infrastructure you are planning to use it (clouds or shared infrastructures usually prevent mDNS from functioning). But it should work in the most office, home and private environments.

IMPORTANT: It does NOT pretend to be a full & valid implementation of the RFC 6762 & RFC 6763, but it fulfils the requirements of its authors (we just needed service discovery in the LAN environment for our IoT products). The registration code needs a lot of improvements. This code was not tested for Bonjour conformance but have been manually verified to be working using built-in OSX utility /usr/bin/dns-sd.

Detailed documentation: GoDoc

##Browsing available services in your local network

Here is an example how to browse services by their type:

package main

import (
    "log"
    "os"
    "time"

    "github.com/oleksandr/bonjour"
)

func main() {
    resolver, err := bonjour.NewResolver(nil)
    if err != nil {
        log.Println("Failed to initialize resolver:", err.Error())
        os.Exit(1)
    }

    results := make(chan *bonjour.ServiceEntry)

    go func(results chan *bonjour.ServiceEntry, exitCh chan<- bool) {
        for e := range results {
            log.Printf("%s", e.Instance)
            exitCh <- true
            time.Sleep(1e9)
            os.Exit(0)
        }
    }(results, resolver.Exit)

    err = resolver.Browse("_foobar._tcp", "local.", results)
    if err != nil {
        log.Println("Failed to browse:", err.Error())
    }

    select {}
}

##Doing a lookup of a specific service instance

Here is an example of looking up service by service instance name:

package main

import (
    "log"
    "os"
    "time"

    "github.com/oleksandr/bonjour"
)

func main() {
    resolver, err := bonjour.NewResolver(nil)
    if err != nil {
        log.Println("Failed to initialize resolver:", err.Error())
        os.Exit(1)
    }

    results := make(chan *bonjour.ServiceEntry)

    go func(results chan *bonjour.ServiceEntry, exitCh chan<- bool) {
        for e := range results {
            log.Printf("%s", e.Instance)
            exitCh <- true
            time.Sleep(1e9)
            os.Exit(0)
        }
    }(results, resolver.Exit)

    err = resolver.Lookup("DEMO", "_foobar._tcp", "", results)
    if err != nil {
        log.Println("Failed to browse:", err.Error())
    }

    select {}
}

##Registering a service

Registering a service is as simple as the following:

package main

import (
    "log"
    "os"
    "os/signal"
    "time"

    "github.com/oleksandr/bonjour"
)

func main() {
    // Run registration (blocking call)
    s, err := bonjour.Register("Foo Service", "_foobar._tcp", "", 9999, []string{"txtv=1", "app=test"}, nil, 0)
    if err != nil {
        log.Fatalln(err.Error())
    }

    // Ctrl+C handling
    handler := make(chan os.Signal, 1)
    signal.Notify(handler, os.Interrupt)
    for sig := range handler {
        if sig == os.Interrupt {
            s.Shutdown()
            time.Sleep(1e9)
            break
        }
    }
}

##Registering a service proxy (manually specifying host/ip and avoiding lookups)

package main

import (
    "log"
    "os"
    "os/signal"
    "time"

    "github.com/oleksandr/bonjour"
)

func main() {
    // Run registration (blocking call)
    s, err := bonjour.RegisterProxy("Proxy Service", "_foobar._tcp", "", 9999, "octopus", "10.0.0.111", []string{"txtv=1", "app=test"}, nil, 0)
    if err != nil {
        log.Fatalln(err.Error())
    }

    // Ctrl+C handling
    handler := make(chan os.Signal, 1)
    signal.Notify(handler, os.Interrupt)
    for sig := range handler {
        if sig == os.Interrupt {
            s.Shutdown()
            time.Sleep(1e9)
            break
        }
    }
}

Documentation

Overview

bonjour

This is a simple Multicast DNS-SD (Apple Bonjour) library written in Golang. You can use it to discover services in the LAN. Pay attention to the infrastructure you are planning to use it (clouds or shared infrastructures usually prevent mDNS from functioning). But it should work in the most office, home and private environments.

**IMPORTANT**: It does NOT pretend to be a full & valid implementation of the RFC 6762 & RFC 6763, but it fulfils the requirements of its authors (we just needed service discovery in the LAN environment for our IoT products). The registration code needs a lot of improvements. This code was not tested for Bonjour conformance but have been manually verified to be working using built-in OSX utility `/usr/bin/dns-sd`.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LookupParams

type LookupParams struct {
	ServiceRecord
	Entries chan<- *ServiceEntry // Entries Channel
}

LookupParams contains configurable properties to create a service discovery request

func NewLookupParams

func NewLookupParams(instance, service, domain string, entries chan<- *ServiceEntry) *LookupParams

Constructs a LookupParams structure by given arguments

type Resolver

type Resolver struct {
	Exit chan<- bool
	// contains filtered or unexported fields
}

Main client data structure to run browse/lookup queries

func NewResolver

func NewResolver(iface *net.Interface) (*Resolver, error)

Resolver structure constructor

func (*Resolver) Browse

func (r *Resolver) Browse(service, domain string, entries chan<- *ServiceEntry) error

Browse for all services of a given type in a given domain

func (*Resolver) Lookup

func (r *Resolver) Lookup(instance, service, domain string, entries chan<- *ServiceEntry) error

Look up a specific service by its name and type in a given domain

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server structure encapsulates both IPv4/IPv6 UDP connections

func Register

func Register(instance, service, domain string, port int, text []string, iface *net.Interface, ttl uint32) (*Server, error)

Register a service by given arguments. This call will take the system's hostname and lookup IP by that hostname. If ttl is 0 the default will be used.

func RegisterProxy

func RegisterProxy(instance, service, domain string, port int, host string, ips []string, text []string, iface *net.Interface, ttl uint32) (*Server, error)

Register a service proxy by given argument. This call will skip the hostname/IP lookup and will use the provided values. If ttl is 0 the default will be used.

func (*Server) SetText

func (s *Server) SetText(text []string)

SetText updates and announces the TXT records

func (*Server) Shutdown

func (s *Server) Shutdown()

Shutdown closes all udp connections and unregisters the service

func (*Server) TTL

func (s *Server) TTL(ttl uint32)

TTL sets the TTL for DNS replies

type ServiceEntry

type ServiceEntry struct {
	ServiceRecord
	HostName string   `json:"hostname"` // Host machine DNS name
	Port     int      `json:"port"`     // Service Port
	Text     []string `json:"text"`     // Service info served as a TXT record
	TTL      uint32   `json:"ttl"`      // TTL of the service record
	AddrIPv4 []net.IP `json:"-"`        // Host machine IPv4 address
	AddrIPv6 []net.IP `json:"-"`        // Host machine IPv6 address
}

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.

func NewServiceEntry

func NewServiceEntry(instance, service, domain string) *ServiceEntry

Constructs a ServiceEntry structure by given arguments

type ServiceRecord

type ServiceRecord struct {
	Instance string `json:"name"`   // Instance name (e.g. "My web page")
	Service  string `json:"type"`   // Service name (e.g. _http._tcp.)
	Domain   string `json:"domain"` // If blank, assumes "local"
	// contains filtered or unexported fields
}

ServiceRecord contains the basic description of a service, which contains instance name, service type & domain

func NewServiceRecord

func NewServiceRecord(instance, service, domain string) *ServiceRecord

Constructs a ServiceRecord structure by given arguments

func (*ServiceRecord) ServiceInstanceName

func (s *ServiceRecord) ServiceInstanceName() string

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 (*ServiceRecord) ServiceName

func (s *ServiceRecord) ServiceName() string

Returns complete service name (e.g. _foobar._tcp.local.), which is composed from a service name (also referred as service type) and a domain.

func (*ServiceRecord) ServiceTypeName

func (s *ServiceRecord) ServiceTypeName() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL