lanbcast

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: Apache-2.0 Imports: 2 Imported by: 1

README

lanbcast

Sends a LAN discovery datagram to the limited broadcast address and to the directed broadcast address of every IPv4 LAN interface, in Go.

Go Reference test Go

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). The directed broadcast addresses (for example 192.168.0.255) still reach the LAN. Standard library only, no cgo.

Features

Behaviour Status
Send to 255.255.255.255 and to the directed broadcast address of each interface that is up and can broadcast Implemented
Skip loopback and point-to-point (VPN) interfaces, and /31 and /32 networks Implemented
Send any other destination alone Implemented
Succeed when at least one send succeeded; otherwise return the first error Implemented
IPv6 (multicast discovery) Not in scope

A device that hears both the limited and a directed broadcast answers twice; callers key answers by device.

Install

go get github.com/cB-Sigma/lanbcast@latest

Requires Go 1.22 or later.

Quick start

From ExampleWriteBroadcast:

import (
	"net"

	"github.com/cB-Sigma/lanbcast"
)

conn, err := net.ListenPacket("udp4", ":0")
if err != nil {
	return err
}
defer conn.Close()
probe := []byte(`{"method":"getPilot","params":{}}`)
err = lanbcast.WriteBroadcast(conn, probe, &net.UDPAddr{IP: net.IPv4bcast, Port: 38899})

Read the answers from conn as usual.

Documentation

Reference: pkg.go.dev/github.com/cB-Sigma/lanbcast.

Package Contents
lanbcast WriteBroadcast

Testing

go test -race ./...

The tests use a fake net.PacketConn and this host's real interfaces. // Covers: SIG-... comments in tests are requirement IDs of the SigmaM2M project's compliance report and have no meaning outside it.

Interop tested against

Not applicable: the module only chooses addresses and sends. The libraries that use it are tested against their devices or simulators.

  • github.com/cB-Sigma/wiz uses it for WiZ light discovery.
  • SigmaM2M's EtherNet/IP, Xiaomi miIO and TP-Link libraries use it for their discovery broadcasts.

This module has no simulator.

Status

v0.x. The API may change before v1.

Contributing

See CONTRIBUTING.md.

Security

See SECURITY.md for how to report a vulnerability.

Licence

Apache-2.0 (see LICENSE and NOTICE). Copyright 2026 Abhinav Gautam.

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

func WriteBroadcast(c net.PacketConn, payload []byte, dst *net.UDPAddr) error

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.

Jump to

Keyboard shortcuts

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