Skip to content

Commit

Permalink
Support multiple network interfaces in "-Interface" flag in dnssd com…
Browse files Browse the repository at this point in the history
…mand #46 #47
  • Loading branch information
brutella committed Oct 22, 2024
1 parent 104764c commit 91c1c03
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 18 deletions.
17 changes: 14 additions & 3 deletions browse.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type AddFunc func(BrowseEntry)
// RmvFunc is called when a service instance disappared.
type RmvFunc func(BrowseEntry)

// LookupType browses for service instanced with a specified service type.
// LookupType browses for service instances.
func LookupType(ctx context.Context, service string, add AddFunc, rmv RmvFunc) (err error) {
conn, err := newMDNSConn()
if err != nil {
Expand All @@ -38,6 +38,17 @@ func LookupType(ctx context.Context, service string, add AddFunc, rmv RmvFunc) (
return lookupType(ctx, service, conn, add, rmv)
}

// LookupTypeAtInterface browses for service instances at specific network interfaces.
func LookupTypeAtInterfaces(ctx context.Context, service string, add AddFunc, rmv RmvFunc, ifaces ...string) (err error) {
conn, err := newMDNSConn(ifaces...)
if err != nil {
return err
}
defer conn.close()

return lookupType(ctx, service, conn, add, rmv, ifaces...)
}

// ServiceInstanceName returns the service instance name
// in the form of <instance name>.<service>.<domain>.
// (Note the trailing dot.)
Expand All @@ -51,7 +62,7 @@ func (e BrowseEntry) ServiceInstanceName() string {
return fmt.Sprintf("%s.%s.%s.", e.Name, e.Type, e.Domain)
}

func lookupType(ctx context.Context, service string, conn MDNSConn, add AddFunc, rmv RmvFunc) (err error) {
func lookupType(ctx context.Context, service string, conn MDNSConn, add AddFunc, rmv RmvFunc, ifaces ...string) (err error) {
var cache = NewCache()

m := new(dns.Msg)
Expand All @@ -73,7 +84,7 @@ func lookupType(ctx context.Context, service string, conn MDNSConn, add AddFunc,

qs := make(chan *Query)
go func() {
for _, iface := range MulticastInterfaces() {
for _, iface := range MulticastInterfaces(ifaces...) {
iface := iface
q := &Query{msg: m, iface: iface}
qs <- q
Expand Down
52 changes: 37 additions & 15 deletions cmd/dnssd/dnssd.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,19 @@ var name = filepath.Base(os.Args[0])
func printUsage() {
log.Info.Println("A DNS-SD utilty to register, browse and resolve Bonjour services.\n\n" +
"Usage:\n" +
" " + name + " register -Name <string> -Type <string> -Port <int> [-Domain <string> (-Interface <string> | -Host <string> -IP <string> )]\n" +
" " + name + " browse -Type <string> [-Domain <string>]\n" +
" " + name + " resolve -Name <string> -Type <string> [-Domain <string>]\n")
" " + name + " register -Name <string> -Type <string> -Port <int> [-Domain <string> -Interface <string[,string]> -Host <string> -IP <string>]\n" +
" " + name + " browse -Type <string> [-Domain <string> -Interface <string[,string]>]\n" +
" " + name + " resolve -Name <string> -Type <string> [-Domain <string> -Interface <string[,string]>]\n")
}

func resolve(typee, instance string) {
fmt.Printf("Lookup %s\n", instance)
ifaces := parseInterfaceFlag()
ifaceDesc := "all interfaces"
if len(ifaces) > 0 {
ifaceDesc = strings.Join(ifaces, ", ")
}

fmt.Printf("Lookup %s at %s\n", instance, ifaceDesc)
fmt.Printf("DATE: –––%s–––\n", time.Now().Format("Mon Jan 2 2006"))
fmt.Printf("%s ...STARTING...\n", time.Now().Format(timeFormat))

Expand All @@ -55,7 +61,7 @@ func resolve(typee, instance string) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

if err := dnssd.LookupType(ctx, typee, addFn, func(dnssd.BrowseEntry) {}); err != nil {
if err := dnssd.LookupTypeAtInterfaces(ctx, typee, addFn, func(dnssd.BrowseEntry) {}, ifaces...); err != nil {
fmt.Println(err)
return
}
Expand All @@ -69,7 +75,7 @@ func resolve(typee, instance string) {

func register(instance string) {
if *portFlag == 0 {
log.Info.Println("dnssd: invalid port", *portFlag)
log.Info.Println("invalid port", *portFlag)
printUsage()
return
}
Expand All @@ -78,7 +84,7 @@ func register(instance string) {
if *ipFlag != "" {
ip := net.ParseIP(*ipFlag)
if ip == nil {
log.Info.Println("dnssd: invalid ip", *ipFlag)
log.Info.Println("invalid ip", *ipFlag)
printUsage()
return
}
Expand All @@ -95,17 +101,12 @@ func register(instance string) {
if resp, err := dnssd.NewResponder(); err != nil {
fmt.Println(err)
} else {
ifaces := []string{}
if len(*interfaceFlag) > 0 {
ifaces = append(ifaces, *interfaceFlag)
}

cfg := dnssd.Config{
Name: *nameFlag,
Type: *typeFlag,
Domain: *domainFlag,
Port: *portFlag,
Ifaces: ifaces,
Ifaces: parseInterfaceFlag(),
IPs: ips,
Host: *hostFlag,
}
Expand Down Expand Up @@ -139,11 +140,32 @@ func register(instance string) {
}
}

func parseInterfaceFlag() []string {
ifaces := []string{}
if len(*interfaceFlag) > 0 {
for _, iface := range strings.Split(*interfaceFlag, ",") {
trimmed := strings.TrimSpace(iface)
if len(trimmed) == 0 {
continue
}
ifaces = append(ifaces, trimmed)
}
}

return ifaces
}

func browse(typee string) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

fmt.Printf("Browsing for %s\n", typee)
ifaces := parseInterfaceFlag()
ifaceDesc := "all interfaces"
if len(ifaces) > 0 {
ifaceDesc = strings.Join(ifaces, ", ")
}

fmt.Printf("Browsing for %s at %s\n", typee, ifaceDesc)
fmt.Printf("DATE: –––%s–––\n", time.Now().Format("Mon Jan 2 2006"))
fmt.Printf("%s ...STARTING...\n", time.Now().Format(timeFormat))
fmt.Printf("Timestamp A/R if Domain Service Type Instance Name\n")
Expand All @@ -156,7 +178,7 @@ func browse(typee string) {
fmt.Printf("%s Rmv %s %s %s %s\n", time.Now().Format(timeFormat), e.IfaceName, e.Domain, e.Type, e.Name)
}

if err := dnssd.LookupType(ctx, typee, addFn, rmvFn); err != nil {
if err := dnssd.LookupTypeAtInterfaces(ctx, typee, addFn, rmvFn, ifaces...); err != nil {
fmt.Println(err)
return
}
Expand Down

0 comments on commit 91c1c03

Please sign in to comment.