Giter Club home page Giter Club logo

dnssd's Introduction

DNS-Based Service Discovery for Go

Go Doc Build Status Go Report Card

DNS-Based service discovery, or DNS-SD (RFC 6763), can be used to discover services on a local area network advertised over Multicast DNS, or mDNS (RFC 6762), 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.

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.

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.

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

package main

import (
    "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)
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.