bonjour

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2014 License: MIT Imports: 13 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/socketplane/bonjour"
)

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

    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.")
    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/socketplane/bonjour"
)

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

    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", "")
    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/socketplane/bonjour"
)

func main() {
    // Run registration (blocking call)
    exitCh, err := bonjour.Register("Foo Service", "_foobar._tcp", "", 9999, []string{"txtv=1", "app=test"}, nil)
    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 {
            exitCh <- true
            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/socketplane/bonjour"
)

func main() {
    // Run registration (blocking call)
    exitCh, err := bonjour.RegisterProxy("Proxy Service", "_foobar._tcp", "", 9999, "octopus", "10.0.0.111", []string{"txtv=1", "app=test"}, nil)
    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 {
            exitCh <- true
            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

View Source
const (
	ECHO_REPLY = iota
	NO_REPLY
	ERROR
)

Variables

This section is empty.

Functions

func EligibleInterfacesToBind added in v0.1.2

func EligibleInterfacesToBind() []*net.Interface

func InterfaceToBind added in v0.1.2

func InterfaceToBind() *net.Interface

func IsInterfaceEligible added in v0.1.2

func IsInterfaceEligible(bIntf *net.Interface) bool

func Register

func Register(instance, service, domain string, port int, text []string, iface *net.Interface,
	bindToIntf bool) (chan<- bool, error)

Register a service by given arguments. This call will take the system's hostname and lookup IP by that hostname.

func RegisterProxy

func RegisterProxy(instance, service, domain string, port int, host, ip string, text []string, iface *net.Interface) (chan<- bool, error)

Register a service proxy by given argument. This call will skip the hostname/IP lookup and will use the provided values.

Types

type Bonjour added in v0.1.2

type Bonjour struct {
	ServiceName   string
	ServiceDomain string
	ServicePort   int
	InterfaceName string
	BindToIntf    bool
	Notify        BonjourNotify
}

func (Bonjour) Start added in v0.1.2

func (b Bonjour) Start() error

type BonjourNotify added in v0.1.2

type BonjourNotify interface {
	NewMember(net.IP)
	RemoveMember(net.IP)
}

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, serviceChan chan<- *ServiceEntry) (*Resolver, error)

Resolver structure constructor

func (*Resolver) Browse

func (r *Resolver) Browse(service, domain string) error

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

func (*Resolver) Lookup

func (r *Resolver) Lookup(instance, service, domain string) error

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

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.

Directories

Path Synopsis
Godeps
_workspace/src/github.com/miekg/dns
Package dns implements a full featured interface to the Domain Name System.
Package dns implements a full featured interface to the Domain Name System.
_workspace/src/github.com/miekg/dns/idn
Package idn implements encoding from and to punycode as speficied by RFC 3492.
Package idn implements encoding from and to punycode as speficied by RFC 3492.
_workspace/src/github.com/socketplane/go-fastping
Package fastping is an ICMP ping library inspired by AnyEvent::FastPing Perl module to send ICMP ECHO REQUEST packets quickly.
Package fastping is an ICMP ping library inspired by AnyEvent::FastPing Perl module to send ICMP ECHO REQUEST packets quickly.
_workspace/src/golang.org/x/net/internal/iana
Package iana provides protocol number resources managed by the Internet Assigned Numbers Authority (IANA).
Package iana provides protocol number resources managed by the Internet Assigned Numbers Authority (IANA).
_workspace/src/golang.org/x/net/ipv4
Package ipv4 implements IP-level socket options for the Internet Protocol version 4.
Package ipv4 implements IP-level socket options for the Internet Protocol version 4.
_workspace/src/golang.org/x/net/ipv6
Package ipv6 implements IP-level socket options for the Internet Protocol version 6.
Package ipv6 implements IP-level socket options for the Internet Protocol version 6.

Jump to

Keyboard shortcuts

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