Documentation
¶
Overview ¶
Package lanbcast sends LAN discovery datagrams to the limited broadcast address and to the directed broadcast address of every LAN interface.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WriteBroadcast ¶
WriteBroadcast sends one discovery datagram to dst. When dst is the limited broadcast address it also goes to the directed broadcast address (for example 192.168.0.255) of every LAN interface that is up: a host with a VPN or several interfaces may route 255.255.255.255 somewhere it cannot be sent (macOS answers "can't assign requested address" while a VPN tunnel holds that route), and the directed address still reaches the LAN. A device that hears both answers twice; callers already key answers by device. It fails only when no send succeeded, with the first error.
Example ¶
Send a discovery probe to the limited broadcast address. It goes there first, then to the directed broadcast address of each LAN interface of this host (how many depends on the host). Any other destination is sent to alone. In real use the connection is a UDP socket from net.ListenPacket("udp4", ":0").
package main
import (
"fmt"
"net"
"github.com/cB-Sigma/lanbcast"
)
// recorder is a net.PacketConn that records where datagrams were sent.
type recorder struct {
net.PacketConn
sent []string
}
func (r *recorder) WriteTo(b []byte, a net.Addr) (int, error) {
r.sent = append(r.sent, a.String())
return len(b), nil
}
// Send a discovery probe to the limited broadcast address. It goes there
// first, then to the directed broadcast address of each LAN interface of
// this host (how many depends on the host). Any other destination is sent
// to alone. In real use the connection is a UDP socket from
// net.ListenPacket("udp4", ":0").
func main() {
conn := &recorder{}
probe := []byte(`{"method":"getPilot","params":{}}`)
err := lanbcast.WriteBroadcast(conn, probe, &net.UDPAddr{IP: net.IPv4bcast, Port: 38899})
fmt.Println("first send:", conn.sent[0], err)
conn.sent = nil
err = lanbcast.WriteBroadcast(conn, probe, &net.UDPAddr{IP: net.IPv4(192, 168, 7, 255), Port: 38899})
fmt.Println("explicit address:", conn.sent, err)
}
Output: first send: 255.255.255.255:38899 <nil> explicit address: [192.168.7.255:38899] <nil>
Types ¶
This section is empty.